aes_cbc.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <stddef.h>
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/check.h"
  8. #include "base/numerics/safe_math.h"
  9. #include "components/webcrypto/algorithms/aes.h"
  10. #include "components/webcrypto/algorithms/util.h"
  11. #include "components/webcrypto/blink_key_handle.h"
  12. #include "components/webcrypto/status.h"
  13. #include "crypto/openssl_util.h"
  14. #include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
  15. #include "third_party/boringssl/src/include/openssl/aes.h"
  16. #include "third_party/boringssl/src/include/openssl/cipher.h"
  17. namespace webcrypto {
  18. namespace {
  19. const EVP_CIPHER* GetAESCipherByKeyLength(size_t key_length_bytes) {
  20. // 192-bit AES is intentionally unsupported (http://crbug.com/533699).
  21. switch (key_length_bytes) {
  22. case 16:
  23. return EVP_aes_128_cbc();
  24. case 32:
  25. return EVP_aes_256_cbc();
  26. default:
  27. return nullptr;
  28. }
  29. }
  30. Status AesCbcEncryptDecrypt(EncryptOrDecrypt cipher_operation,
  31. const blink::WebCryptoAlgorithm& algorithm,
  32. const blink::WebCryptoKey& key,
  33. base::span<const uint8_t> data,
  34. std::vector<uint8_t>* buffer) {
  35. crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  36. const blink::WebCryptoAesCbcParams* params = algorithm.AesCbcParams();
  37. const std::vector<uint8_t>& raw_key = GetSymmetricKeyData(key);
  38. if (params->Iv().size() != 16)
  39. return Status::ErrorIncorrectSizeAesCbcIv();
  40. // According to the openssl docs, the amount of data written may be as large
  41. // as (data_size + cipher_block_size - 1), constrained to a multiple of
  42. // cipher_block_size.
  43. base::CheckedNumeric<int> output_max_len = data.size();
  44. output_max_len += AES_BLOCK_SIZE - 1;
  45. if (!output_max_len.IsValid())
  46. return Status::ErrorDataTooLarge();
  47. const unsigned remainder =
  48. base::ValueOrDieForType<unsigned>(output_max_len % AES_BLOCK_SIZE);
  49. if (remainder != 0)
  50. output_max_len += AES_BLOCK_SIZE - remainder;
  51. if (!output_max_len.IsValid())
  52. return Status::ErrorDataTooLarge();
  53. // Note: PKCS padding is enabled by default
  54. const EVP_CIPHER* const cipher = GetAESCipherByKeyLength(raw_key.size());
  55. DCHECK(cipher);
  56. bssl::ScopedEVP_CIPHER_CTX context;
  57. if (!EVP_CipherInit_ex(context.get(), cipher, nullptr, &raw_key[0],
  58. params->Iv().data(), cipher_operation)) {
  59. return Status::OperationError();
  60. }
  61. buffer->resize(base::ValueOrDieForType<size_t>(output_max_len));
  62. int output_len = 0;
  63. if (!EVP_CipherUpdate(context.get(), buffer->data(), &output_len, data.data(),
  64. base::checked_cast<int>(data.size()))) {
  65. return Status::OperationError();
  66. }
  67. int final_output_chunk_len = 0;
  68. if (!EVP_CipherFinal_ex(context.get(), buffer->data() + output_len,
  69. &final_output_chunk_len)) {
  70. return Status::OperationError();
  71. }
  72. const unsigned int final_output_len =
  73. static_cast<unsigned int>(output_len) +
  74. static_cast<unsigned int>(final_output_chunk_len);
  75. buffer->resize(final_output_len);
  76. return Status::Success();
  77. }
  78. class AesCbcImplementation : public AesAlgorithm {
  79. public:
  80. AesCbcImplementation() : AesAlgorithm("CBC") {}
  81. Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
  82. const blink::WebCryptoKey& key,
  83. base::span<const uint8_t> data,
  84. std::vector<uint8_t>* buffer) const override {
  85. return AesCbcEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
  86. }
  87. Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
  88. const blink::WebCryptoKey& key,
  89. base::span<const uint8_t> data,
  90. std::vector<uint8_t>* buffer) const override {
  91. return AesCbcEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
  92. }
  93. };
  94. } // namespace
  95. std::unique_ptr<AlgorithmImplementation> CreateAesCbcImplementation() {
  96. return std::make_unique<AesCbcImplementation>();
  97. }
  98. } // namespace webcrypto