blob: abd1546164c8db22cddb414979480942ef3efe2c [file] [log] [blame]
[email protected]5ee44d42012-02-08 00:14:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]28ae8fe2009-06-05 18:25:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_SIGNATURE_CREATOR_H_
6#define CRYPTO_SIGNATURE_CREATOR_H_
[email protected]28ae8fe2009-06-05 18:25:067
avidd373b8b2015-12-21 21:34:438#include <stdint.h>
9
[email protected]28ae8fe2009-06-05 18:25:0610#include <vector>
11
avidd373b8b2015-12-21 21:34:4312#include "base/macros.h"
[email protected]c9c251d2014-07-22 00:09:2513#include "build/build_config.h"
[email protected]d613a9902011-08-05 20:59:1114#include "crypto/crypto_export.h"
[email protected]28ae8fe2009-06-05 18:25:0615
[email protected]5123d9c2013-06-27 09:18:4316#if defined(USE_OPENSSL)
17// Forward declaration for openssl/*.h
18typedef struct env_md_ctx_st EVP_MD_CTX;
davidben71f35ff2015-04-17 20:54:4819#elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX)
[email protected]5123d9c2013-06-27 09:18:4320// Forward declaration.
21struct SGNContextStr;
[email protected]692033a2010-04-09 18:40:5022#endif
23
[email protected]4b559b4d2011-04-14 17:37:1424namespace crypto {
[email protected]28ae8fe2009-06-05 18:25:0625
[email protected]5ee44d42012-02-08 00:14:5426class RSAPrivateKey;
27
[email protected]28ae8fe2009-06-05 18:25:0628// Signs data using a bare private key (as opposed to a full certificate).
dougsteed0cf460ec2014-09-19 18:46:0929// Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5.
[email protected]d613a9902011-08-05 20:59:1130class CRYPTO_EXPORT SignatureCreator {
[email protected]28ae8fe2009-06-05 18:25:0631 public:
dougsteed0cf460ec2014-09-19 18:46:0932 // The set of supported hash functions. Extend as required.
33 enum HashAlgorithm {
34 SHA1,
35 SHA256,
36 };
37
[email protected]a502bbe72011-01-07 18:06:4538 ~SignatureCreator();
39
[email protected]28ae8fe2009-06-05 18:25:0640 // Create an instance. The caller must ensure that the provided PrivateKey
dougsteed0cf460ec2014-09-19 18:46:0941 // instance outlives the created SignatureCreator. Uses the HashAlgorithm
42 // specified.
43 static SignatureCreator* Create(RSAPrivateKey* key, HashAlgorithm hash_alg);
[email protected]28ae8fe2009-06-05 18:25:0644
dougsteed0cf460ec2014-09-19 18:46:0945
46 // Signs the precomputed |hash_alg| digest |data| using private |key| as
[email protected]ed31834b2013-07-09 08:32:4047 // specified in PKCS #1 v1.5.
48 static bool Sign(RSAPrivateKey* key,
dougsteed0cf460ec2014-09-19 18:46:0949 HashAlgorithm hash_alg,
avidd373b8b2015-12-21 21:34:4350 const uint8_t* data,
[email protected]ed31834b2013-07-09 08:32:4051 int data_len,
avidd373b8b2015-12-21 21:34:4352 std::vector<uint8_t>* signature);
[email protected]ed31834b2013-07-09 08:32:4053
[email protected]28ae8fe2009-06-05 18:25:0654 // Update the signature with more data.
avidd373b8b2015-12-21 21:34:4355 bool Update(const uint8_t* data_part, int data_part_len);
[email protected]28ae8fe2009-06-05 18:25:0656
57 // Finalize the signature.
avidd373b8b2015-12-21 21:34:4358 bool Final(std::vector<uint8_t>* signature);
[email protected]28ae8fe2009-06-05 18:25:0659
60 private:
61 // Private constructor. Use the Create() method instead.
[email protected]71a9f842009-09-24 01:21:1262 SignatureCreator();
[email protected]28ae8fe2009-06-05 18:25:0663
[email protected]be796bb2010-11-18 15:43:4364#if defined(USE_OPENSSL)
65 EVP_MD_CTX* sign_context_;
davidben71f35ff2015-04-17 20:54:4866#elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX)
[email protected]13555c122009-10-08 01:18:0267 SGNContextStr* sign_context_;
[email protected]28ae8fe2009-06-05 18:25:0668#endif
69
70 DISALLOW_COPY_AND_ASSIGN(SignatureCreator);
71};
72
[email protected]4b559b4d2011-04-14 17:37:1473} // namespace crypto
[email protected]28ae8fe2009-06-05 18:25:0674
[email protected]4b559b4d2011-04-14 17:37:1475#endif // CRYPTO_SIGNATURE_CREATOR_H_