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