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

Side by Side Diff: content/child/webcrypto/nss/aes_cbc_nss.cc

Issue 1077273002: html_viewer: Move webcrypto to a place where html_viewer can use it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to ToT Created 5 years, 8 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 <cryptohi.h>
6
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_algorithm_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"
17
18 namespace content {
19
20 namespace webcrypto {
21
22 namespace {
23
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();
32
33 CryptoData iv(params->iv().data(), params->iv().size());
34 if (iv.byte_length() != 16)
35 return Status::ErrorIncorrectSizeAesCbcIv();
36
37 PK11SymKey* sym_key = SymKeyNss::Cast(key)->key();
38
39 CK_ATTRIBUTE_TYPE operation = (mode == ENCRYPT) ? CKA_ENCRYPT : CKA_DECRYPT;
40
41 SECItem iv_item = MakeSECItemForBuffer(iv);
42
43 crypto::ScopedSECItem param(PK11_ParamFromIV(CKM_AES_CBC_PAD, &iv_item));
44 if (!param)
45 return Status::OperationError();
46
47 crypto::ScopedPK11Context context(PK11_CreateContextBySymKey(
48 CKM_AES_CBC_PAD, operation, sym_key, param.get()));
49
50 if (!context.get())
51 return Status::OperationError();
52
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();
62 }
63
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();
70 }
71
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());
75
76 unsigned char* buffer_data = vector_as_array(buffer);
77
78 int output_len;
79 if (SECSuccess != PK11_CipherOp(context.get(), buffer_data, &output_len,
80 buffer->size(), data.bytes(),
81 data.byte_length())) {
82 return Status::OperationError();
83 }
84
85 unsigned int final_output_chunk_len;
86 if (SECSuccess !=
87 PK11_DigestFinal(context.get(), buffer_data + output_len,
88 &final_output_chunk_len,
89 (output_max_len - output_len).ValueOrDie())) {
90 return Status::OperationError();
91 }
92
93 buffer->resize(final_output_chunk_len + output_len);
94 return Status::Success();
95 }
96
97 class AesCbcImplementation : public AesAlgorithm {
98 public:
99 AesCbcImplementation() : AesAlgorithm(CKM_AES_CBC, "CBC") {}
100
101 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
102 const blink::WebCryptoKey& key,
103 const CryptoData& data,
104 std::vector<uint8_t>* buffer) const override {
105 return AesCbcEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
106 }
107
108 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
109 const blink::WebCryptoKey& key,
110 const CryptoData& data,
111 std::vector<uint8_t>* buffer) const override {
112 return AesCbcEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
113 }
114 };
115
116 } // namespace
117
118 AlgorithmImplementation* CreatePlatformAesCbcImplementation() {
119 return new AesCbcImplementation;
120 }
121
122 } // namespace webcrypto
123
124 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/nss/aes_algorithm_nss.cc ('k') | content/child/webcrypto/nss/aes_gcm_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698