aes.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "components/webcrypto/algorithms/aes.h"
  5. #include <stddef.h>
  6. #include "components/webcrypto/algorithms/secret_key_util.h"
  7. #include "components/webcrypto/algorithms/util.h"
  8. #include "components/webcrypto/blink_key_handle.h"
  9. #include "components/webcrypto/jwk.h"
  10. #include "components/webcrypto/status.h"
  11. #include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
  12. #include "third_party/blink/public/platform/web_crypto_key_algorithm.h"
  13. namespace webcrypto {
  14. namespace {
  15. // Creates an AES algorithm name for the given key size (in bytes). For
  16. // instance "A128CBC" is the result of suffix="CBC", keylen_bytes=16.
  17. std::string MakeJwkAesAlgorithmName(const std::string& suffix,
  18. size_t keylen_bytes) {
  19. if (keylen_bytes == 16)
  20. return std::string("A128") + suffix;
  21. if (keylen_bytes == 24)
  22. return std::string("A192") + suffix;
  23. if (keylen_bytes == 32)
  24. return std::string("A256") + suffix;
  25. return std::string();
  26. }
  27. // Synthesizes an import algorithm given a key algorithm, so that
  28. // deserialization can re-use the ImportKey*() methods.
  29. blink::WebCryptoAlgorithm SynthesizeImportAlgorithmForClone(
  30. const blink::WebCryptoKeyAlgorithm& algorithm) {
  31. return blink::WebCryptoAlgorithm::AdoptParamsAndCreate(algorithm.Id(),
  32. nullptr);
  33. }
  34. } // namespace
  35. AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages,
  36. const std::string& jwk_suffix)
  37. : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) {
  38. }
  39. AesAlgorithm::AesAlgorithm(const std::string& jwk_suffix)
  40. : all_key_usages_(blink::kWebCryptoKeyUsageEncrypt |
  41. blink::kWebCryptoKeyUsageDecrypt |
  42. blink::kWebCryptoKeyUsageWrapKey |
  43. blink::kWebCryptoKeyUsageUnwrapKey),
  44. jwk_suffix_(jwk_suffix) {}
  45. Status AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
  46. bool extractable,
  47. blink::WebCryptoKeyUsageMask usages,
  48. GenerateKeyResult* result) const {
  49. Status status = CheckKeyCreationUsages(all_key_usages_, usages);
  50. if (status.IsError())
  51. return status;
  52. uint16_t keylen_bits = algorithm.AesKeyGenParams()->LengthBits();
  53. // 192-bit AES is intentionally unsupported (http://crbug.com/533699).
  54. if (keylen_bits == 192)
  55. return Status::ErrorAes192BitUnsupported();
  56. if (keylen_bits != 128 && keylen_bits != 256)
  57. return Status::ErrorGenerateAesKeyLength();
  58. return GenerateWebCryptoSecretKey(
  59. blink::WebCryptoKeyAlgorithm::CreateAes(algorithm.Id(), keylen_bits),
  60. extractable, usages, keylen_bits, result);
  61. }
  62. Status AesAlgorithm::ImportKey(blink::WebCryptoKeyFormat format,
  63. base::span<const uint8_t> key_data,
  64. const blink::WebCryptoAlgorithm& algorithm,
  65. bool extractable,
  66. blink::WebCryptoKeyUsageMask usages,
  67. blink::WebCryptoKey* key) const {
  68. switch (format) {
  69. case blink::kWebCryptoKeyFormatRaw:
  70. return ImportKeyRaw(key_data, algorithm, extractable, usages, key);
  71. case blink::kWebCryptoKeyFormatJwk:
  72. return ImportKeyJwk(key_data, algorithm, extractable, usages, key);
  73. default:
  74. return Status::ErrorUnsupportedImportKeyFormat();
  75. }
  76. }
  77. Status AesAlgorithm::ExportKey(blink::WebCryptoKeyFormat format,
  78. const blink::WebCryptoKey& key,
  79. std::vector<uint8_t>* buffer) const {
  80. switch (format) {
  81. case blink::kWebCryptoKeyFormatRaw:
  82. return ExportKeyRaw(key, buffer);
  83. case blink::kWebCryptoKeyFormatJwk:
  84. return ExportKeyJwk(key, buffer);
  85. default:
  86. return Status::ErrorUnsupportedExportKeyFormat();
  87. }
  88. }
  89. Status AesAlgorithm::ImportKeyRaw(base::span<const uint8_t> key_data,
  90. const blink::WebCryptoAlgorithm& algorithm,
  91. bool extractable,
  92. blink::WebCryptoKeyUsageMask usages,
  93. blink::WebCryptoKey* key) const {
  94. Status status = CheckKeyCreationUsages(all_key_usages_, usages);
  95. if (status.IsError())
  96. return status;
  97. const size_t keylen_bytes = key_data.size();
  98. // 192-bit AES is intentionally unsupported (http://crbug.com/533699).
  99. if (keylen_bytes == 24)
  100. return Status::ErrorAes192BitUnsupported();
  101. if (keylen_bytes != 16 && keylen_bytes != 32)
  102. return Status::ErrorImportAesKeyLength();
  103. // No possibility of overflow.
  104. unsigned int keylen_bits = keylen_bytes * 8;
  105. return CreateWebCryptoSecretKey(
  106. key_data,
  107. blink::WebCryptoKeyAlgorithm::CreateAes(algorithm.Id(), keylen_bits),
  108. extractable, usages, key);
  109. }
  110. Status AesAlgorithm::ImportKeyJwk(base::span<const uint8_t> key_data,
  111. const blink::WebCryptoAlgorithm& algorithm,
  112. bool extractable,
  113. blink::WebCryptoKeyUsageMask usages,
  114. blink::WebCryptoKey* key) const {
  115. Status status = CheckKeyCreationUsages(all_key_usages_, usages);
  116. if (status.IsError())
  117. return status;
  118. std::vector<uint8_t> raw_data;
  119. JwkReader jwk;
  120. status = ReadSecretKeyNoExpectedAlgJwk(key_data, extractable, usages,
  121. &raw_data, &jwk);
  122. if (status.IsError())
  123. return status;
  124. bool has_jwk_alg;
  125. std::string jwk_alg;
  126. status = jwk.GetAlg(&jwk_alg, &has_jwk_alg);
  127. if (status.IsError())
  128. return status;
  129. if (has_jwk_alg) {
  130. std::string expected_algorithm_name =
  131. MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size());
  132. if (jwk_alg != expected_algorithm_name) {
  133. // Give a different error message if the key length was wrong.
  134. if (jwk_alg == MakeJwkAesAlgorithmName(jwk_suffix_, 16) ||
  135. jwk_alg == MakeJwkAesAlgorithmName(jwk_suffix_, 24) ||
  136. jwk_alg == MakeJwkAesAlgorithmName(jwk_suffix_, 32)) {
  137. return Status::ErrorJwkIncorrectKeyLength();
  138. }
  139. return Status::ErrorJwkAlgorithmInconsistent();
  140. }
  141. }
  142. return ImportKeyRaw(raw_data, algorithm, extractable, usages, key);
  143. }
  144. Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key,
  145. std::vector<uint8_t>* buffer) const {
  146. *buffer = GetSymmetricKeyData(key);
  147. return Status::Success();
  148. }
  149. Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key,
  150. std::vector<uint8_t>* buffer) const {
  151. const std::vector<uint8_t>& raw_data = GetSymmetricKeyData(key);
  152. WriteSecretKeyJwk(raw_data,
  153. MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()),
  154. key.Extractable(), key.Usages(), buffer);
  155. return Status::Success();
  156. }
  157. Status AesAlgorithm::DeserializeKeyForClone(
  158. const blink::WebCryptoKeyAlgorithm& algorithm,
  159. blink::WebCryptoKeyType type,
  160. bool extractable,
  161. blink::WebCryptoKeyUsageMask usages,
  162. base::span<const uint8_t> key_data,
  163. blink::WebCryptoKey* key) const {
  164. if (algorithm.ParamsType() != blink::kWebCryptoKeyAlgorithmParamsTypeAes ||
  165. type != blink::kWebCryptoKeyTypeSecret)
  166. return Status::ErrorUnexpected();
  167. return ImportKeyRaw(key_data, SynthesizeImportAlgorithmForClone(algorithm),
  168. extractable, usages, key);
  169. }
  170. Status AesAlgorithm::GetKeyLength(
  171. const blink::WebCryptoAlgorithm& key_length_algorithm,
  172. bool* has_length_bits,
  173. unsigned int* length_bits) const {
  174. *has_length_bits = true;
  175. *length_bits = key_length_algorithm.AesDerivedKeyParams()->LengthBits();
  176. if (*length_bits == 128 || *length_bits == 256)
  177. return Status::Success();
  178. // 192-bit AES is intentionally unsupported (http://crbug.com/533699).
  179. if (*length_bits == 192)
  180. return Status::ErrorAes192BitUnsupported();
  181. return Status::ErrorGetAesKeyLength();
  182. }
  183. } // namespace webcrypto