secret_key_util.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_ALGORITHMS_SECRET_KEY_UTIL_H_
  5. #define COMPONENTS_WEBCRYPTO_ALGORITHMS_SECRET_KEY_UTIL_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/containers/span.h"
  10. #include "third_party/blink/public/platform/web_crypto_algorithm.h"
  11. #include "third_party/blink/public/platform/web_crypto_key.h"
  12. // This file contains functions shared by multiple symmetric key algorithms.
  13. namespace webcrypto {
  14. class GenerateKeyResult;
  15. class JwkReader;
  16. class Status;
  17. // Generates a random secret key of the given bit length. If the bit length is
  18. // not a multiple of 8, then the resulting key will have ceil(keylen_bits / 8)
  19. // bytes, and the "unused" bits will be set to zero. This function does not do
  20. // any validation checks on the provided parameters.
  21. Status GenerateWebCryptoSecretKey(const blink::WebCryptoKeyAlgorithm& algorithm,
  22. bool extractable,
  23. blink::WebCryptoKeyUsageMask usages,
  24. unsigned int keylen_bits,
  25. GenerateKeyResult* result);
  26. // Creates a WebCrypto secret key given the raw data. The provided |key_data|
  27. // will be copied into the new key. This function does not do any validation
  28. // checks for the provided parameters.
  29. Status CreateWebCryptoSecretKey(base::span<const uint8_t> key_data,
  30. const blink::WebCryptoKeyAlgorithm& algorithm,
  31. bool extractable,
  32. blink::WebCryptoKeyUsageMask usages,
  33. blink::WebCryptoKey* key);
  34. // Writes a JWK-formatted symmetric key to |jwk_key_data|.
  35. // * raw_key_data: The actual key data
  36. // * algorithm: The JWK algorithm name (i.e. "alg")
  37. // * extractable: The JWK extractability (i.e. "ext")
  38. // * usages: The JWK usages (i.e. "key_ops")
  39. void WriteSecretKeyJwk(base::span<const uint8_t> raw_key_data,
  40. const std::string& algorithm,
  41. bool extractable,
  42. blink::WebCryptoKeyUsageMask usages,
  43. std::vector<uint8_t>* jwk_key_data);
  44. // Parses a UTF-8 encoded JWK (key_data), and extracts the key material to
  45. // |*raw_key_data|. Returns Status::Success() on success, otherwise an error.
  46. // In order for this to succeed:
  47. // * expected_extractable must be consistent with the JWK's "ext", if
  48. // present.
  49. // * expected_usages must be a subset of the JWK's "key_ops" if present.
  50. Status ReadSecretKeyNoExpectedAlgJwk(
  51. base::span<const uint8_t> key_data,
  52. bool expected_extractable,
  53. blink::WebCryptoKeyUsageMask expected_usages,
  54. std::vector<uint8_t>* raw_key_data,
  55. JwkReader* jwk);
  56. } // namespace webcrypto
  57. #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_SECRET_KEY_UTIL_H_