Update V8 to version 4.6.61.
[chromium-blink-merge.git] / components / webcrypto / openssl / aes_cbc_openssl.cc
blob4d1ece247df358799218f348ff0192f97e890302
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/crypto_data.h"
12 #include "components/webcrypto/openssl/aes_algorithm_openssl.h"
13 #include "components/webcrypto/openssl/key_openssl.h"
14 #include "components/webcrypto/openssl/util_openssl.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"
21 namespace webcrypto {
23 namespace {
25 const EVP_CIPHER* GetAESCipherByKeyLength(size_t key_length_bytes) {
26 // BoringSSL does not support 192-bit AES keys.
27 switch (key_length_bytes) {
28 case 16:
29 return EVP_aes_128_cbc();
30 case 32:
31 return EVP_aes_256_cbc();
32 default:
33 return NULL;
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 =
46 SymKeyOpenSsl::Cast(key)->raw_key_data();
48 if (params->iv().size() != 16)
49 return Status::ErrorIncorrectSizeAesCbcIv();
51 // According to the openssl docs, the amount of data written may be as large
52 // as (data_size + cipher_block_size - 1), constrained to a multiple of
53 // cipher_block_size.
54 base::CheckedNumeric<int> output_max_len = data.byte_length();
55 output_max_len += AES_BLOCK_SIZE - 1;
56 if (!output_max_len.IsValid())
57 return Status::ErrorDataTooLarge();
59 const unsigned remainder = output_max_len.ValueOrDie() % AES_BLOCK_SIZE;
60 if (remainder != 0)
61 output_max_len += AES_BLOCK_SIZE - remainder;
62 if (!output_max_len.IsValid())
63 return Status::ErrorDataTooLarge();
65 // Note: PKCS padding is enabled by default
66 crypto::ScopedOpenSSL<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> context(
67 EVP_CIPHER_CTX_new());
69 if (!context.get())
70 return Status::OperationError();
72 const EVP_CIPHER* const cipher = GetAESCipherByKeyLength(raw_key.size());
73 DCHECK(cipher);
75 if (!EVP_CipherInit_ex(context.get(), cipher, NULL, &raw_key[0],
76 params->iv().data(), cipher_operation)) {
77 return Status::OperationError();
80 buffer->resize(output_max_len.ValueOrDie());
82 unsigned char* const buffer_data = vector_as_array(buffer);
84 int output_len = 0;
85 if (!EVP_CipherUpdate(context.get(), buffer_data, &output_len, data.bytes(),
86 data.byte_length())) {
87 return Status::OperationError();
89 int final_output_chunk_len = 0;
90 if (!EVP_CipherFinal_ex(context.get(), buffer_data + output_len,
91 &final_output_chunk_len)) {
92 return Status::OperationError();
95 const unsigned int final_output_len =
96 static_cast<unsigned int>(output_len) +
97 static_cast<unsigned int>(final_output_chunk_len);
99 buffer->resize(final_output_len);
101 return Status::Success();
104 class AesCbcImplementation : public AesAlgorithm {
105 public:
106 AesCbcImplementation() : AesAlgorithm("CBC") {}
108 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
109 const blink::WebCryptoKey& key,
110 const CryptoData& data,
111 std::vector<uint8_t>* buffer) const override {
112 return AesCbcEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
115 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
116 const blink::WebCryptoKey& key,
117 const CryptoData& data,
118 std::vector<uint8_t>* buffer) const override {
119 return AesCbcEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
123 } // namespace
125 AlgorithmImplementation* CreatePlatformAesCbcImplementation() {
126 return new AesCbcImplementation;
129 } // namespace webcrypto