signature_creator.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef CRYPTO_SIGNATURE_CREATOR_H_
  5. #define CRYPTO_SIGNATURE_CREATOR_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "build/build_config.h"
  11. #include "crypto/crypto_export.h"
  12. #include "third_party/boringssl/src/include/openssl/base.h"
  13. namespace crypto {
  14. class RSAPrivateKey;
  15. // Signs data using a bare private key (as opposed to a full certificate).
  16. // Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5.
  17. class CRYPTO_EXPORT SignatureCreator {
  18. public:
  19. // The set of supported hash functions. Extend as required.
  20. enum HashAlgorithm {
  21. SHA1,
  22. SHA256,
  23. };
  24. SignatureCreator(const SignatureCreator&) = delete;
  25. SignatureCreator& operator=(const SignatureCreator&) = delete;
  26. ~SignatureCreator();
  27. // Create an instance. The caller must ensure that the provided PrivateKey
  28. // instance outlives the created SignatureCreator. Uses the HashAlgorithm
  29. // specified.
  30. static std::unique_ptr<SignatureCreator> Create(RSAPrivateKey* key,
  31. HashAlgorithm hash_alg);
  32. // Signs the precomputed |hash_alg| digest |data| using private |key| as
  33. // specified in PKCS #1 v1.5.
  34. static bool Sign(RSAPrivateKey* key,
  35. HashAlgorithm hash_alg,
  36. const uint8_t* data,
  37. int data_len,
  38. std::vector<uint8_t>* signature);
  39. // Update the signature with more data.
  40. bool Update(const uint8_t* data_part, int data_part_len);
  41. // Finalize the signature.
  42. bool Final(std::vector<uint8_t>* signature);
  43. private:
  44. // Private constructor. Use the Create() method instead.
  45. SignatureCreator();
  46. raw_ptr<EVP_MD_CTX> sign_context_;
  47. };
  48. } // namespace crypto
  49. #endif // CRYPTO_SIGNATURE_CREATOR_H_