signature_verifier.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_VERIFIER_H_
  5. #define CRYPTO_SIGNATURE_VERIFIER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/containers/span.h"
  10. #include "build/build_config.h"
  11. #include "crypto/crypto_export.h"
  12. namespace crypto {
  13. // The SignatureVerifier class verifies a signature using a bare public key
  14. // (as opposed to a certificate).
  15. class CRYPTO_EXPORT SignatureVerifier {
  16. public:
  17. // The set of supported signature algorithms. Extend as required.
  18. enum SignatureAlgorithm {
  19. RSA_PKCS1_SHA1,
  20. RSA_PKCS1_SHA256,
  21. ECDSA_SHA256,
  22. // This is RSA-PSS with SHA-256 as both signing hash and MGF-1 hash, and the
  23. // salt length matching the hash length.
  24. RSA_PSS_SHA256,
  25. };
  26. SignatureVerifier();
  27. ~SignatureVerifier();
  28. // Streaming interface:
  29. // Initiates a signature verification operation. This should be followed
  30. // by one or more VerifyUpdate calls and a VerifyFinal call.
  31. //
  32. // The signature is encoded according to the signature algorithm.
  33. //
  34. // The public key is specified as a DER encoded ASN.1 SubjectPublicKeyInfo
  35. // structure, which contains not only the public key but also its type
  36. // (algorithm):
  37. // SubjectPublicKeyInfo ::= SEQUENCE {
  38. // algorithm AlgorithmIdentifier,
  39. // subjectPublicKey BIT STRING }
  40. bool VerifyInit(SignatureAlgorithm signature_algorithm,
  41. base::span<const uint8_t> signature,
  42. base::span<const uint8_t> public_key_info);
  43. // Feeds a piece of the data to the signature verifier.
  44. void VerifyUpdate(base::span<const uint8_t> data_part);
  45. // Concludes a signature verification operation. Returns true if the
  46. // signature is valid. Returns false if the signature is invalid or an
  47. // error occurred.
  48. bool VerifyFinal();
  49. private:
  50. void Reset();
  51. std::vector<uint8_t> signature_;
  52. struct VerifyContext;
  53. std::unique_ptr<VerifyContext> verify_context_;
  54. };
  55. } // namespace crypto
  56. #endif // CRYPTO_SIGNATURE_VERIFIER_H_