[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_SIGNATURE_CREATOR_H_ |
6 | #define CRYPTO_SIGNATURE_CREATOR_H_ | ||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 7 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame^] | 8 | #include <stdint.h> |
9 | |||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 10 | #include <vector> |
11 | |||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame^] | 12 | #include "base/macros.h" |
[email protected] | c9c251d | 2014-07-22 00:09:25 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 14 | #include "crypto/crypto_export.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 15 | |
[email protected] | 5123d9c | 2013-06-27 09:18:43 | [diff] [blame] | 16 | #if defined(USE_OPENSSL) |
17 | // Forward declaration for openssl/*.h | ||||
18 | typedef struct env_md_ctx_st EVP_MD_CTX; | ||||
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 19 | #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 5123d9c | 2013-06-27 09:18:43 | [diff] [blame] | 20 | // Forward declaration. |
21 | struct SGNContextStr; | ||||
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 22 | #endif |
23 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 24 | namespace crypto { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 25 | |
[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 26 | class RSAPrivateKey; |
27 | |||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 28 | // Signs data using a bare private key (as opposed to a full certificate). |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 29 | // Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 30 | class CRYPTO_EXPORT SignatureCreator { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 31 | public: |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 32 | // The set of supported hash functions. Extend as required. |
33 | enum HashAlgorithm { | ||||
34 | SHA1, | ||||
35 | SHA256, | ||||
36 | }; | ||||
37 | |||||
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 38 | ~SignatureCreator(); |
39 | |||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 40 | // Create an instance. The caller must ensure that the provided PrivateKey |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 41 | // instance outlives the created SignatureCreator. Uses the HashAlgorithm |
42 | // specified. | ||||
43 | static SignatureCreator* Create(RSAPrivateKey* key, HashAlgorithm hash_alg); | ||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 44 | |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 45 | |
46 | // Signs the precomputed |hash_alg| digest |data| using private |key| as | ||||
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 47 | // specified in PKCS #1 v1.5. |
48 | static bool Sign(RSAPrivateKey* key, | ||||
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 49 | HashAlgorithm hash_alg, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame^] | 50 | const uint8_t* data, |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 51 | int data_len, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame^] | 52 | std::vector<uint8_t>* signature); |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 53 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 54 | // Update the signature with more data. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame^] | 55 | bool Update(const uint8_t* data_part, int data_part_len); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 56 | |
57 | // Finalize the signature. | ||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame^] | 58 | bool Final(std::vector<uint8_t>* signature); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 59 | |
60 | private: | ||||
61 | // Private constructor. Use the Create() method instead. | ||||
[email protected] | 71a9f84 | 2009-09-24 01:21:12 | [diff] [blame] | 62 | SignatureCreator(); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 63 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 64 | #if defined(USE_OPENSSL) |
65 | EVP_MD_CTX* sign_context_; | ||||
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 66 | #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 13555c12 | 2009-10-08 01:18:02 | [diff] [blame] | 67 | SGNContextStr* sign_context_; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 68 | #endif |
69 | |||||
70 | DISALLOW_COPY_AND_ASSIGN(SignatureCreator); | ||||
71 | }; | ||||
72 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 73 | } // namespace crypto |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 74 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 75 | #endif // CRYPTO_SIGNATURE_CREATOR_H_ |