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.
5 #include <openssl/aes.h>
6 #include <openssl/evp.h>
8 #include "base/logging.h"
9 #include "base/numerics/safe_math.h"
10 #include "base/stl_util.h"
11 #include "components/webcrypto/algorithms/aes.h"
12 #include "components/webcrypto/algorithms/util_openssl.h"
13 #include "components/webcrypto/crypto_data.h"
14 #include "components/webcrypto/key.h"
15 #include "components/webcrypto/status.h"
16 #include "components/webcrypto/webcrypto_util.h"
17 #include "crypto/openssl_util.h"
18 #include "crypto/scoped_openssl_types.h"
19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
25 const EVP_CIPHER
* GetAESCipherByKeyLength(size_t key_length_bytes
) {
26 // BoringSSL does not support 192-bit AES keys.
27 switch (key_length_bytes
) {
29 return EVP_aes_128_cbc();
31 return EVP_aes_256_cbc();
37 Status
AesCbcEncryptDecrypt(EncryptOrDecrypt cipher_operation
,
38 const blink::WebCryptoAlgorithm
& algorithm
,
39 const blink::WebCryptoKey
& key
,
40 const CryptoData
& data
,
41 std::vector
<uint8_t>* buffer
) {
42 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
44 const blink::WebCryptoAesCbcParams
* params
= algorithm
.aesCbcParams();
45 const std::vector
<uint8_t>& raw_key
= GetSymmetricKeyData(key
);
47 if (params
->iv().size() != 16)
48 return Status::ErrorIncorrectSizeAesCbcIv();
50 // According to the openssl docs, the amount of data written may be as large
51 // as (data_size + cipher_block_size - 1), constrained to a multiple of
53 base::CheckedNumeric
<int> output_max_len
= data
.byte_length();
54 output_max_len
+= AES_BLOCK_SIZE
- 1;
55 if (!output_max_len
.IsValid())
56 return Status::ErrorDataTooLarge();
58 const unsigned remainder
= output_max_len
.ValueOrDie() % AES_BLOCK_SIZE
;
60 output_max_len
+= AES_BLOCK_SIZE
- remainder
;
61 if (!output_max_len
.IsValid())
62 return Status::ErrorDataTooLarge();
64 // Note: PKCS padding is enabled by default
65 crypto::ScopedOpenSSL
<EVP_CIPHER_CTX
, EVP_CIPHER_CTX_free
> context(
66 EVP_CIPHER_CTX_new());
69 return Status::OperationError();
71 const EVP_CIPHER
* const cipher
= GetAESCipherByKeyLength(raw_key
.size());
74 if (!EVP_CipherInit_ex(context
.get(), cipher
, NULL
, &raw_key
[0],
75 params
->iv().data(), cipher_operation
)) {
76 return Status::OperationError();
79 buffer
->resize(output_max_len
.ValueOrDie());
81 unsigned char* const buffer_data
= vector_as_array(buffer
);
84 if (!EVP_CipherUpdate(context
.get(), buffer_data
, &output_len
, data
.bytes(),
85 data
.byte_length())) {
86 return Status::OperationError();
88 int final_output_chunk_len
= 0;
89 if (!EVP_CipherFinal_ex(context
.get(), buffer_data
+ output_len
,
90 &final_output_chunk_len
)) {
91 return Status::OperationError();
94 const unsigned int final_output_len
=
95 static_cast<unsigned int>(output_len
) +
96 static_cast<unsigned int>(final_output_chunk_len
);
98 buffer
->resize(final_output_len
);
100 return Status::Success();
103 class AesCbcImplementation
: public AesAlgorithm
{
105 AesCbcImplementation() : AesAlgorithm("CBC") {}
107 Status
Encrypt(const blink::WebCryptoAlgorithm
& algorithm
,
108 const blink::WebCryptoKey
& key
,
109 const CryptoData
& data
,
110 std::vector
<uint8_t>* buffer
) const override
{
111 return AesCbcEncryptDecrypt(ENCRYPT
, algorithm
, key
, data
, buffer
);
114 Status
Decrypt(const blink::WebCryptoAlgorithm
& algorithm
,
115 const blink::WebCryptoKey
& key
,
116 const CryptoData
& data
,
117 std::vector
<uint8_t>* buffer
) const override
{
118 return AesCbcEncryptDecrypt(DECRYPT
, algorithm
, key
, data
, buffer
);
124 scoped_ptr
<AlgorithmImplementation
> CreateAesCbcImplementation() {
125 return make_scoped_ptr(new AesCbcImplementation
);
128 } // namespace webcrypto