rsa_pss.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2014 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. #include <stdint.h>
  5. #include <memory>
  6. #include "components/webcrypto/algorithms/rsa.h"
  7. #include "components/webcrypto/algorithms/rsa_sign.h"
  8. #include "components/webcrypto/status.h"
  9. #include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
  10. namespace webcrypto {
  11. namespace {
  12. class RsaPssImplementation : public RsaHashedAlgorithm {
  13. public:
  14. RsaPssImplementation()
  15. : RsaHashedAlgorithm(blink::kWebCryptoKeyUsageVerify,
  16. blink::kWebCryptoKeyUsageSign) {}
  17. const char* GetJwkAlgorithm(
  18. const blink::WebCryptoAlgorithmId hash) const override {
  19. switch (hash) {
  20. case blink::kWebCryptoAlgorithmIdSha1:
  21. return "PS1";
  22. case blink::kWebCryptoAlgorithmIdSha256:
  23. return "PS256";
  24. case blink::kWebCryptoAlgorithmIdSha384:
  25. return "PS384";
  26. case blink::kWebCryptoAlgorithmIdSha512:
  27. return "PS512";
  28. default:
  29. return nullptr;
  30. }
  31. }
  32. Status Sign(const blink::WebCryptoAlgorithm& algorithm,
  33. const blink::WebCryptoKey& key,
  34. base::span<const uint8_t> data,
  35. std::vector<uint8_t>* buffer) const override {
  36. return RsaSign(key, algorithm.RsaPssParams()->SaltLengthBytes(), data,
  37. buffer);
  38. }
  39. Status Verify(const blink::WebCryptoAlgorithm& algorithm,
  40. const blink::WebCryptoKey& key,
  41. base::span<const uint8_t> signature,
  42. base::span<const uint8_t> data,
  43. bool* signature_match) const override {
  44. return RsaVerify(key, algorithm.RsaPssParams()->SaltLengthBytes(),
  45. signature, data, signature_match);
  46. }
  47. };
  48. } // namespace
  49. std::unique_ptr<AlgorithmImplementation> CreateRsaPssImplementation() {
  50. return std::make_unique<RsaPssImplementation>();
  51. }
  52. } // namespace webcrypto