xref: /aosp_15_r20/external/cronet/crypto/signature_creator.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CRYPTO_SIGNATURE_CREATOR_H_
6 #define CRYPTO_SIGNATURE_CREATOR_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "build/build_config.h"
14 #include "crypto/crypto_export.h"
15 #include "third_party/boringssl/src/include/openssl/base.h"
16 
17 namespace crypto {
18 
19 class RSAPrivateKey;
20 
21 // Signs data using a bare private key (as opposed to a full certificate).
22 // Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5.
23 class CRYPTO_EXPORT SignatureCreator {
24  public:
25   // The set of supported hash functions. Extend as required.
26   enum HashAlgorithm {
27     SHA1,
28     SHA256,
29   };
30 
31   SignatureCreator(const SignatureCreator&) = delete;
32   SignatureCreator& operator=(const SignatureCreator&) = delete;
33 
34   ~SignatureCreator();
35 
36   // Create an instance. The caller must ensure that the provided PrivateKey
37   // instance outlives the created SignatureCreator. Uses the HashAlgorithm
38   // specified.
39   static std::unique_ptr<SignatureCreator> Create(RSAPrivateKey* key,
40                                                   HashAlgorithm hash_alg);
41 
42   // Signs the precomputed |hash_alg| digest |data| using private |key| as
43   // specified in PKCS #1 v1.5.
44   static bool Sign(RSAPrivateKey* key,
45                    HashAlgorithm hash_alg,
46                    const uint8_t* data,
47                    int data_len,
48                    std::vector<uint8_t>* signature);
49 
50   // Update the signature with more data.
51   bool Update(const uint8_t* data_part, int data_part_len);
52 
53   // Finalize the signature.
54   bool Final(std::vector<uint8_t>* signature);
55 
56  private:
57   // Private constructor. Use the Create() method instead.
58   SignatureCreator();
59 
60   bssl::UniquePtr<EVP_MD_CTX> sign_context_;
61 };
62 
63 }  // namespace crypto
64 
65 #endif  // CRYPTO_SIGNATURE_CREATOR_H_
66