@@ -89,11 +89,10 @@ WebCryptoCipherStatus AES_Cipher(
8989 case kWebCryptoCipherDecrypt :
9090 // If in decrypt mode, the auth tag must be set in the params.tag.
9191 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 >()))) {
9796 return WebCryptoCipherStatus::FAILED;
9897 }
9998 break ;
@@ -125,9 +124,7 @@ WebCryptoCipherStatus AES_Cipher(
125124 return WebCryptoCipherStatus::FAILED;
126125 }
127126
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);
131128
132129 // In some outdated version of OpenSSL (e.g.
133130 // ubi81_sharedlibs_openssl111fips_x64) may be used in sharedlib mode, the
@@ -139,36 +136,36 @@ WebCryptoCipherStatus AES_Cipher(
139136 // Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
140137 if (in.size () == 0 ) {
141138 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 ())) {
148144 return WebCryptoCipherStatus::FAILED;
149145 }
150146
151147 total += out_len;
152148 CHECK_LE (out_len, buf_len);
153- ptr += out_len;
154149 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)) {
156152 return WebCryptoCipherStatus::FAILED;
157153 }
158154 total += out_len;
159155
160156 // If using AES_GCM, grab the generated auth tag and append
161157 // it to the end of the ciphertext.
162158 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))
165163 return WebCryptoCipherStatus::FAILED;
166164 total += tag_len;
167165 }
168166
169167 // 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);
172169
173170 return WebCryptoCipherStatus::OK;
174171}
@@ -295,38 +292,34 @@ WebCryptoCipherStatus AES_CTR_Cipher(
295292 return WebCryptoCipherStatus::FAILED;
296293 }
297294
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 ());
302297
303298 // Also just like in chromium's implementation, if we can process
304299 // the input without wrapping the counter, we'll do it as a single
305300 // call here. If we can't, we'll fallback to the a two-step approach
306301 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 ();
316309 return status;
317310 }
318311
319312 BN_ULONG blocks_part1 = BN_get_word (remaining_until_reset.get ());
320313 BN_ULONG input_size_part1 = blocks_part1 * kAesBlockSize ;
321314
322315 // 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 >() );
330323
331324 if (status != WebCryptoCipherStatus::OK)
332325 return status;
@@ -335,18 +328,16 @@ WebCryptoCipherStatus AES_CTR_Cipher(
335328 std::vector<unsigned char > new_counter_block = BlockWithZeroedCounter (params);
336329
337330 // 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 ();
350341
351342 return status;
352343}
0 commit comments