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 #if defined(USE_OPENSSL) 18 // Forward declaration for openssl/*.h 19 typedef struct env_md_ctx_st EVP_MD_CTX; 20 #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) 21 // Forward declaration. 22 struct SGNContextStr; 23 #endif 24 25 namespace crypto { 26 27 class RSAPrivateKey; 28 29 // Signs data using a bare private key (as opposed to a full certificate). 30 // Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5. 31 class CRYPTO_EXPORT SignatureCreator { 32 public: 33 // The set of supported hash functions. Extend as required. 34 enum HashAlgorithm { 35 SHA1, 36 SHA256, 37 }; 38 39 ~SignatureCreator(); 40 41 // Create an instance. The caller must ensure that the provided PrivateKey 42 // instance outlives the created SignatureCreator. Uses the HashAlgorithm 43 // specified. 44 static std::unique_ptr<SignatureCreator> Create(RSAPrivateKey* key, 45 HashAlgorithm hash_alg); 46 47 // Signs the precomputed |hash_alg| digest |data| using private |key| as 48 // specified in PKCS #1 v1.5. 49 static bool Sign(RSAPrivateKey* key, 50 HashAlgorithm hash_alg, 51 const uint8_t* data, 52 int data_len, 53 std::vector<uint8_t>* signature); 54 55 // Update the signature with more data. 56 bool Update(const uint8_t* data_part, int data_part_len); 57 58 // Finalize the signature. 59 bool Final(std::vector<uint8_t>* signature); 60 61 private: 62 // Private constructor. Use the Create() method instead. 63 SignatureCreator(); 64 65 #if defined(USE_OPENSSL) 66 EVP_MD_CTX* sign_context_; 67 #elif defined(USE_NSS_CERTS) || defined(OS_WIN) || defined(OS_MACOSX) 68 SGNContextStr* sign_context_; 69 #endif 70 71 DISALLOW_COPY_AND_ASSIGN(SignatureCreator); 72 }; 73 74 } // namespace crypto 75 76 #endif // CRYPTO_SIGNATURE_CREATOR_H_ 77