Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 63a138e

Browse filesBrowse 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 <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 0b70822 commit 63a138e
Copy full SHA for 63a138e

File tree

Expand file treeCollapse file tree

2 files changed

+45
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+45
-3
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/aes.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/aes.js
+6-2Lines changed: 6 additions & 2 deletions
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;
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-encrypt-decrypt-aes.js
+39-1Lines changed: 39 additions & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.