Skip to content

Commit 63a138e

Browse files
aduh95codebytere
authored andcommitted
crypto: fix passing TypedArray to webcrypto AES methods
Refs: https://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface Fixes: #36083 PR-URL: #36087 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 0b70822 commit 63a138e

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/internal/crypto/aes.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use strict';
22

33
const {
4+
ArrayBufferIsView,
45
ArrayBufferPrototypeSlice,
56
ArrayFrom,
67
ArrayPrototypeIncludes,
78
ArrayPrototypePush,
89
MathFloor,
910
Promise,
1011
SafeSet,
12+
TypedArrayPrototypeSlice,
1113
} = primordials;
1214

1315
const {
@@ -183,8 +185,10 @@ function asyncAesGcmCipher(
183185
let tag;
184186
switch (mode) {
185187
case kWebCryptoCipherDecrypt:
186-
tag = ArrayBufferPrototypeSlice(data, -tagByteLength);
187-
data = ArrayBufferPrototypeSlice(data, 0, -tagByteLength);
188+
const slice = ArrayBufferIsView(data) ?
189+
TypedArrayPrototypeSlice : ArrayBufferPrototypeSlice;
190+
tag = slice(data, -tagByteLength);
191+
data = slice(data, 0, -tagByteLength);
188192
break;
189193
case kWebCryptoCipherEncrypt:
190194
tag = tagByteLength;

test/parallel/test-webcrypto-encrypt-decrypt-aes.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { subtle } = require('crypto').webcrypto;
9+
const { getRandomValues, subtle } = require('crypto').webcrypto;
1010

1111
async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
1212
const key = await subtle.importKey(
@@ -196,3 +196,41 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
196196
await Promise.all(variations);
197197
})().then(common.mustCall());
198198
}
199+
200+
{
201+
(async function() {
202+
const secretKey = await subtle.generateKey(
203+
{
204+
name: 'AES-GCM',
205+
length: 256,
206+
},
207+
false,
208+
['encrypt', 'decrypt'],
209+
);
210+
211+
const iv = getRandomValues(new Uint8Array(12));
212+
const aad = getRandomValues(new Uint8Array(32));
213+
214+
const encrypted = await subtle.encrypt(
215+
{
216+
name: 'AES-GCM',
217+
iv,
218+
additionalData: aad,
219+
tagLength: 128
220+
},
221+
secretKey,
222+
getRandomValues(new Uint8Array(32))
223+
);
224+
225+
await subtle.decrypt(
226+
{
227+
name: 'AES-GCM',
228+
iv,
229+
additionalData: aad,
230+
tagLength: 128,
231+
},
232+
secretKey,
233+
new Uint8Array(encrypted),
234+
);
235+
})().then(common.mustCall());
236+
}

0 commit comments

Comments
 (0)