@@ -89,11 +89,10 @@ WebCryptoCipherStatus AES_Cipher(
89
89
case kWebCryptoCipherDecrypt :
90
90
// If in decrypt mode, the auth tag must be set in the params.tag.
91
91
CHECK (params.tag );
92
- if (!EVP_CIPHER_CTX_ctrl (
93
- ctx.get (),
94
- EVP_CTRL_AEAD_SET_TAG,
95
- params.tag .size (),
96
- const_cast <char *>(params.tag .get ()))) {
92
+ if (!EVP_CIPHER_CTX_ctrl (ctx.get (),
93
+ EVP_CTRL_AEAD_SET_TAG,
94
+ params.tag .size (),
95
+ const_cast <char *>(params.tag .data <char >()))) {
97
96
return WebCryptoCipherStatus::FAILED;
98
97
}
99
98
break ;
@@ -125,9 +124,7 @@ WebCryptoCipherStatus AES_Cipher(
125
124
return WebCryptoCipherStatus::FAILED;
126
125
}
127
126
128
- char * data = MallocOpenSSL<char >(buf_len);
129
- ByteSource buf = ByteSource::Allocated (data, buf_len);
130
- unsigned char * ptr = reinterpret_cast <unsigned char *>(data);
127
+ ByteSource::Builder buf (buf_len);
131
128
132
129
// In some outdated version of OpenSSL (e.g.
133
130
// ubi81_sharedlibs_openssl111fips_x64) may be used in sharedlib mode, the
@@ -139,36 +136,36 @@ WebCryptoCipherStatus AES_Cipher(
139
136
// Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
140
137
if (in.size () == 0 ) {
141
138
out_len = 0 ;
142
- } else if (!EVP_CipherUpdate (
143
- ctx.get (),
144
- ptr,
145
- &out_len,
146
- in.data <unsigned char >(),
147
- in.size ())) {
139
+ } else if (!EVP_CipherUpdate (ctx.get (),
140
+ buf.data <unsigned char >(),
141
+ &out_len,
142
+ in.data <unsigned char >(),
143
+ in.size ())) {
148
144
return WebCryptoCipherStatus::FAILED;
149
145
}
150
146
151
147
total += out_len;
152
148
CHECK_LE (out_len, buf_len);
153
- ptr += out_len;
154
149
out_len = EVP_CIPHER_CTX_block_size (ctx.get ());
155
- if (!EVP_CipherFinal_ex (ctx.get (), ptr, &out_len)) {
150
+ if (!EVP_CipherFinal_ex (
151
+ ctx.get (), buf.data <unsigned char >() + total, &out_len)) {
156
152
return WebCryptoCipherStatus::FAILED;
157
153
}
158
154
total += out_len;
159
155
160
156
// If using AES_GCM, grab the generated auth tag and append
161
157
// it to the end of the ciphertext.
162
158
if (cipher_mode == kWebCryptoCipherEncrypt && mode == EVP_CIPH_GCM_MODE) {
163
- data += out_len;
164
- if (!EVP_CIPHER_CTX_ctrl (ctx.get (), EVP_CTRL_AEAD_GET_TAG, tag_len, ptr))
159
+ if (!EVP_CIPHER_CTX_ctrl (ctx.get (),
160
+ EVP_CTRL_AEAD_GET_TAG,
161
+ tag_len,
162
+ buf.data <unsigned char >() + total))
165
163
return WebCryptoCipherStatus::FAILED;
166
164
total += tag_len;
167
165
}
168
166
169
167
// It's possible that we haven't used the full allocated space. Size down.
170
- buf.Resize (total);
171
- *out = std::move (buf);
168
+ *out = std::move (buf).release (total);
172
169
173
170
return WebCryptoCipherStatus::OK;
174
171
}
@@ -295,38 +292,34 @@ WebCryptoCipherStatus AES_CTR_Cipher(
295
292
return WebCryptoCipherStatus::FAILED;
296
293
}
297
294
298
- // Output size is identical to the input size
299
- char * data = MallocOpenSSL<char >(in.size ());
300
- ByteSource buf = ByteSource::Allocated (data, in.size ());
301
- unsigned char * ptr = reinterpret_cast <unsigned char *>(data);
295
+ // Output size is identical to the input size.
296
+ ByteSource::Builder buf (in.size ());
302
297
303
298
// Also just like in chromium's implementation, if we can process
304
299
// the input without wrapping the counter, we'll do it as a single
305
300
// call here. If we can't, we'll fallback to the a two-step approach
306
301
if (BN_cmp (remaining_until_reset.get (), num_output.get ()) >= 0 ) {
307
- auto status = AES_CTR_Cipher2 (
308
- key_data,
309
- cipher_mode,
310
- params,
311
- in,
312
- params.iv .data <unsigned char >(),
313
- ptr);
314
- if (status == WebCryptoCipherStatus::OK)
315
- *out = std::move (buf);
302
+ auto status = AES_CTR_Cipher2 (key_data,
303
+ cipher_mode,
304
+ params,
305
+ in,
306
+ params.iv .data <unsigned char >(),
307
+ buf.data <unsigned char >());
308
+ if (status == WebCryptoCipherStatus::OK) *out = std::move (buf).release ();
316
309
return status;
317
310
}
318
311
319
312
BN_ULONG blocks_part1 = BN_get_word (remaining_until_reset.get ());
320
313
BN_ULONG input_size_part1 = blocks_part1 * kAesBlockSize ;
321
314
322
315
// Encrypt the first part...
323
- auto status = AES_CTR_Cipher2 (
324
- key_data,
325
- cipher_mode,
326
- params,
327
- ByteSource::Foreign (in.get (), input_size_part1),
328
- params.iv .data <unsigned char >(),
329
- ptr );
316
+ auto status =
317
+ AES_CTR_Cipher2 ( key_data,
318
+ cipher_mode,
319
+ params,
320
+ ByteSource::Foreign (in.data < char > (), input_size_part1),
321
+ params.iv .data <unsigned char >(),
322
+ buf. data < unsigned char >() );
330
323
331
324
if (status != WebCryptoCipherStatus::OK)
332
325
return status;
@@ -335,18 +328,16 @@ WebCryptoCipherStatus AES_CTR_Cipher(
335
328
std::vector<unsigned char > new_counter_block = BlockWithZeroedCounter (params);
336
329
337
330
// Encrypt the second part...
338
- status = AES_CTR_Cipher2 (
339
- key_data,
340
- cipher_mode,
341
- params,
342
- ByteSource::Foreign (
343
- in.get () + input_size_part1,
344
- in.size () - input_size_part1),
345
- new_counter_block.data (),
346
- ptr + input_size_part1);
347
-
348
- if (status == WebCryptoCipherStatus::OK)
349
- *out = std::move (buf);
331
+ status =
332
+ AES_CTR_Cipher2 (key_data,
333
+ cipher_mode,
334
+ params,
335
+ ByteSource::Foreign (in.data <char >() + input_size_part1,
336
+ in.size () - input_size_part1),
337
+ new_counter_block.data (),
338
+ buf.data <unsigned char >() + input_size_part1);
339
+
340
+ if (status == WebCryptoCipherStatus::OK) *out = std::move (buf).release ();
350
341
351
342
return status;
352
343
}
0 commit comments