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 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 "base/macros.h" 14 #include "build/build_config.h" 15 #include "crypto/crypto_export.h" 16 17 // Forward declaration for openssl/*.h 18 typedef struct env_md_ctx_st EVP_MD_CTX; 19 20 namespace crypto { 21 22 class RSAPrivateKey; 23 24 // Signs data using a bare private key (as opposed to a full certificate). 25 // Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5. 26 class CRYPTO_EXPORT SignatureCreator { 27 public: 28 // The set of supported hash functions. Extend as required. 29 enum HashAlgorithm { 30 SHA1, 31 SHA256, 32 }; 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 EVP_MD_CTX* sign_context_; 61 62 DISALLOW_COPY_AND_ASSIGN(SignatureCreator); 63 }; 64 65 } // namespace crypto 66 67 #endif // CRYPTO_SIGNATURE_CREATOR_H_ 68