Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: components/webcrypto/openssl/aes_cbc_openssl.cc

Issue 1304063015: [refactor] Rename the webcrypto/openssl and webcrypto/test directories. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@jwk_refactor
Patch Set: fix filename in gn build Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include <openssl/aes.h>
6 #include <openssl/evp.h>
7
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"
20
21 namespace webcrypto {
22
23 namespace {
24
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;
34 }
35 }
36
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);
43
44 const blink::WebCryptoAesCbcParams* params = algorithm.aesCbcParams();
45 const std::vector<uint8_t>& raw_key =
46 SymKeyOpenSsl::Cast(key)->raw_key_data();
47
48 if (params->iv().size() != 16)
49 return Status::ErrorIncorrectSizeAesCbcIv();
50
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();
58
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();
64
65 // Note: PKCS padding is enabled by default
66 crypto::ScopedOpenSSL<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> context(
67 EVP_CIPHER_CTX_new());
68
69 if (!context.get())
70 return Status::OperationError();
71
72 const EVP_CIPHER* const cipher = GetAESCipherByKeyLength(raw_key.size());
73 DCHECK(cipher);
74
75 if (!EVP_CipherInit_ex(context.get(), cipher, NULL, &raw_key[0],
76 params->iv().data(), cipher_operation)) {
77 return Status::OperationError();
78 }
79
80 buffer->resize(output_max_len.ValueOrDie());
81
82 unsigned char* const buffer_data = vector_as_array(buffer);
83
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();
88 }
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();
93 }
94
95 const unsigned int final_output_len =
96 static_cast<unsigned int>(output_len) +
97 static_cast<unsigned int>(final_output_chunk_len);
98
99 buffer->resize(final_output_len);
100
101 return Status::Success();
102 }
103
104 class AesCbcImplementation : public AesAlgorithm {
105 public:
106 AesCbcImplementation() : AesAlgorithm("CBC") {}
107
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);
113 }
114
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);
120 }
121 };
122
123 } // namespace
124
125 AlgorithmImplementation* CreatePlatformAesCbcImplementation() {
126 return new AesCbcImplementation;
127 }
128
129 } // namespace webcrypto
OLDNEW
« no previous file with comments | « components/webcrypto/openssl/aes_algorithm_openssl.cc ('k') | components/webcrypto/openssl/aes_ctr_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698