algorithm_dispatch.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef COMPONENTS_WEBCRYPTO_ALGORITHM_DISPATCH_H_
  5. #define COMPONENTS_WEBCRYPTO_ALGORITHM_DISPATCH_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/containers/span.h"
  10. #include "third_party/blink/public/platform/web_crypto.h"
  11. namespace webcrypto {
  12. class GenerateKeyResult;
  13. class Status;
  14. // These functions provide an entry point for synchronous webcrypto operations.
  15. //
  16. // The inputs to these methods come from Blink, and hence the validations done
  17. // by Blink can be assumed:
  18. //
  19. // * The algorithm parameters are consistent with the algorithm
  20. // * The key contains the required usage for the operation
  21. Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
  22. const blink::WebCryptoKey& key,
  23. base::span<const uint8_t> data,
  24. std::vector<uint8_t>* buffer);
  25. Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
  26. const blink::WebCryptoKey& key,
  27. base::span<const uint8_t> data,
  28. std::vector<uint8_t>* buffer);
  29. Status Digest(const blink::WebCryptoAlgorithm& algorithm,
  30. base::span<const uint8_t> data,
  31. std::vector<uint8_t>* buffer);
  32. Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
  33. bool extractable,
  34. blink::WebCryptoKeyUsageMask usages,
  35. GenerateKeyResult* result);
  36. Status ImportKey(blink::WebCryptoKeyFormat format,
  37. base::span<const uint8_t> key_data,
  38. const blink::WebCryptoAlgorithm& algorithm,
  39. bool extractable,
  40. blink::WebCryptoKeyUsageMask usages,
  41. blink::WebCryptoKey* key);
  42. Status ExportKey(blink::WebCryptoKeyFormat format,
  43. const blink::WebCryptoKey& key,
  44. std::vector<uint8_t>* buffer);
  45. Status Sign(const blink::WebCryptoAlgorithm& algorithm,
  46. const blink::WebCryptoKey& key,
  47. base::span<const uint8_t> data,
  48. std::vector<uint8_t>* buffer);
  49. Status Verify(const blink::WebCryptoAlgorithm& algorithm,
  50. const blink::WebCryptoKey& key,
  51. base::span<const uint8_t> signature,
  52. base::span<const uint8_t> data,
  53. bool* signature_match);
  54. Status WrapKey(blink::WebCryptoKeyFormat format,
  55. const blink::WebCryptoKey& key_to_wrap,
  56. const blink::WebCryptoKey& wrapping_key,
  57. const blink::WebCryptoAlgorithm& wrapping_algorithm,
  58. std::vector<uint8_t>* buffer);
  59. Status UnwrapKey(blink::WebCryptoKeyFormat format,
  60. base::span<const uint8_t> wrapped_key_data,
  61. const blink::WebCryptoKey& wrapping_key,
  62. const blink::WebCryptoAlgorithm& wrapping_algorithm,
  63. const blink::WebCryptoAlgorithm& algorithm,
  64. bool extractable,
  65. blink::WebCryptoKeyUsageMask usages,
  66. blink::WebCryptoKey* key);
  67. Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm,
  68. const blink::WebCryptoKey& base_key,
  69. unsigned int length_bits,
  70. std::vector<uint8_t>* derived_bytes);
  71. // Derives a key by calling the underlying deriveBits/getKeyLength/importKey
  72. // operations.
  73. //
  74. // Note that whereas the WebCrypto spec uses a single "derivedKeyType"
  75. // AlgorithmIdentifier in its specification of deriveKey(), here two separate
  76. // AlgorithmIdentifiers are used:
  77. //
  78. // * |import_algorithm| -- The parameters required by the derived key's
  79. // "importKey" operation.
  80. //
  81. // * |key_length_algorithm| -- The parameters required by the derived key's
  82. // "get key length" operation.
  83. //
  84. // WebCryptoAlgorithm is not a flexible type like AlgorithmIdentifier (it cannot
  85. // be easily re-interpreted as a different parameter type).
  86. //
  87. // Therefore being provided with separate parameter types for the import
  88. // parameters and the key length parameters simplifies passing the right
  89. // parameters onto ImportKey() and GetKeyLength() respectively.
  90. Status DeriveKey(const blink::WebCryptoAlgorithm& algorithm,
  91. const blink::WebCryptoKey& base_key,
  92. const blink::WebCryptoAlgorithm& import_algorithm,
  93. const blink::WebCryptoAlgorithm& key_length_algorithm,
  94. bool extractable,
  95. blink::WebCryptoKeyUsageMask usages,
  96. blink::WebCryptoKey* derived_key);
  97. bool SerializeKeyForClone(const blink::WebCryptoKey& key,
  98. blink::WebVector<uint8_t>* key_data);
  99. bool DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm,
  100. blink::WebCryptoKeyType type,
  101. bool extractable,
  102. blink::WebCryptoKeyUsageMask usages,
  103. base::span<const uint8_t> key_data,
  104. blink::WebCryptoKey* key);
  105. } // namespace webcrypto
  106. #endif // COMPONENTS_WEBCRYPTO_ALGORITHM_DISPATCH_H_