Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / child / webcrypto / nss / aes_cbc_nss.cc
blob03184404f5d2600971832e6fd5af7e3f7f59d932
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 <cryptohi.h>
7 #include "base/numerics/safe_math.h"
8 #include "base/stl_util.h"
9 #include "content/child/webcrypto/crypto_data.h"
10 #include "content/child/webcrypto/nss/aes_key_nss.h"
11 #include "content/child/webcrypto/nss/key_nss.h"
12 #include "content/child/webcrypto/nss/util_nss.h"
13 #include "content/child/webcrypto/status.h"
14 #include "content/child/webcrypto/webcrypto_util.h"
15 #include "crypto/scoped_nss_types.h"
16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
18 namespace content {
20 namespace webcrypto {
22 namespace {
24 Status AesCbcEncryptDecrypt(EncryptOrDecrypt mode,
25 const blink::WebCryptoAlgorithm& algorithm,
26 const blink::WebCryptoKey& key,
27 const CryptoData& data,
28 std::vector<uint8_t>* buffer) {
29 const blink::WebCryptoAesCbcParams* params = algorithm.aesCbcParams();
30 if (!params)
31 return Status::ErrorUnexpected();
33 CryptoData iv(params->iv().data(), params->iv().size());
34 if (iv.byte_length() != 16)
35 return Status::ErrorIncorrectSizeAesCbcIv();
37 PK11SymKey* sym_key = SymKeyNss::Cast(key)->key();
39 CK_ATTRIBUTE_TYPE operation = (mode == ENCRYPT) ? CKA_ENCRYPT : CKA_DECRYPT;
41 SECItem iv_item = MakeSECItemForBuffer(iv);
43 crypto::ScopedSECItem param(PK11_ParamFromIV(CKM_AES_CBC_PAD, &iv_item));
44 if (!param)
45 return Status::OperationError();
47 crypto::ScopedPK11Context context(PK11_CreateContextBySymKey(
48 CKM_AES_CBC_PAD, operation, sym_key, param.get()));
50 if (!context.get())
51 return Status::OperationError();
53 // Oddly PK11_CipherOp takes input and output lengths as "int" rather than
54 // "unsigned int". Do some checks now to avoid integer overflowing.
55 base::CheckedNumeric<int> output_max_len = data.byte_length();
56 output_max_len += AES_BLOCK_SIZE;
57 if (!output_max_len.IsValid()) {
58 // TODO(eroman): Handle this by chunking the input fed into NSS. Right now
59 // it doesn't make much difference since the one-shot API would end up
60 // blowing out the memory and crashing anyway.
61 return Status::ErrorDataTooLarge();
64 // PK11_CipherOp does an invalid memory access when given empty decryption
65 // input, or input which is not a multiple of the block size. See also
66 // https://bugzilla.mozilla.com/show_bug.cgi?id=921687.
67 if (operation == CKA_DECRYPT &&
68 (data.byte_length() == 0 || (data.byte_length() % AES_BLOCK_SIZE != 0))) {
69 return Status::OperationError();
72 // TODO(eroman): Refine the output buffer size. It can be computed exactly for
73 // encryption, and can be smaller for decryption.
74 buffer->resize(output_max_len.ValueOrDie());
76 unsigned char* buffer_data = vector_as_array(buffer);
78 int output_len;
79 if (SECSuccess != PK11_CipherOp(context.get(),
80 buffer_data,
81 &output_len,
82 buffer->size(),
83 data.bytes(),
84 data.byte_length())) {
85 return Status::OperationError();
88 unsigned int final_output_chunk_len;
89 if (SECSuccess !=
90 PK11_DigestFinal(context.get(),
91 buffer_data + output_len,
92 &final_output_chunk_len,
93 (output_max_len - output_len).ValueOrDie())) {
94 return Status::OperationError();
97 buffer->resize(final_output_chunk_len + output_len);
98 return Status::Success();
101 class AesCbcImplementation : public AesAlgorithm {
102 public:
103 AesCbcImplementation() : AesAlgorithm(CKM_AES_CBC, "CBC") {}
105 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
106 const blink::WebCryptoKey& key,
107 const CryptoData& data,
108 std::vector<uint8_t>* buffer) const OVERRIDE {
109 return AesCbcEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
112 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
113 const blink::WebCryptoKey& key,
114 const CryptoData& data,
115 std::vector<uint8_t>* buffer) const OVERRIDE {
116 return AesCbcEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
120 } // namespace
122 AlgorithmImplementation* CreatePlatformAesCbcImplementation() {
123 return new AesCbcImplementation;
126 } // namespace webcrypto
128 } // namespace content