[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 | |||||
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 11 | #include <vector> |
12 | |||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | c9c251d | 2014-07-22 00:09:25 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 15 | #include "crypto/crypto_export.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 16 | |
[email protected] | 5123d9c | 2013-06-27 09:18:43 | [diff] [blame] | 17 | // Forward declaration for openssl/*.h |
18 | typedef struct env_md_ctx_st EVP_MD_CTX; | ||||
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 19 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 20 | namespace crypto { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 21 | |
[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 22 | class RSAPrivateKey; |
23 | |||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 24 | // Signs data using a bare private key (as opposed to a full certificate). |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 25 | // 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] | 26 | class CRYPTO_EXPORT SignatureCreator { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 27 | public: |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 28 | // The set of supported hash functions. Extend as required. |
29 | enum HashAlgorithm { | ||||
30 | SHA1, | ||||
31 | SHA256, | ||||
32 | }; | ||||
33 | |||||
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 34 | ~SignatureCreator(); |
35 | |||||
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 36 | // Create an instance. The caller must ensure that the provided PrivateKey |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 37 | // instance outlives the created SignatureCreator. Uses the HashAlgorithm |
38 | // specified. | ||||
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 39 | static std::unique_ptr<SignatureCreator> Create(RSAPrivateKey* key, |
40 | HashAlgorithm hash_alg); | ||||
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 41 | |
42 | // Signs the precomputed |hash_alg| digest |data| using private |key| as | ||||
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 43 | // specified in PKCS #1 v1.5. |
44 | static bool Sign(RSAPrivateKey* key, | ||||
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 45 | HashAlgorithm hash_alg, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 46 | const uint8_t* data, |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 47 | int data_len, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 48 | std::vector<uint8_t>* signature); |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 49 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 50 | // Update the signature with more data. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 51 | bool Update(const uint8_t* data_part, int data_part_len); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 52 | |
53 | // Finalize the signature. | ||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 54 | bool Final(std::vector<uint8_t>* signature); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 55 | |
56 | private: | ||||
57 | // Private constructor. Use the Create() method instead. | ||||
[email protected] | 71a9f84 | 2009-09-24 01:21:12 | [diff] [blame] | 58 | SignatureCreator(); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 59 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 60 | EVP_MD_CTX* sign_context_; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 61 | |
62 | DISALLOW_COPY_AND_ASSIGN(SignatureCreator); | ||||
63 | }; | ||||
64 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 65 | } // namespace crypto |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 66 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 67 | #endif // CRYPTO_SIGNATURE_CREATOR_H_ |