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