Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: fix passing TypedArray to webcrypto AES methods #36087

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const {
ArrayBufferIsView,
ArrayBufferPrototypeSlice,
ArrayFrom,
ArrayPrototypeIncludes,
ArrayPrototypePush,
MathFloor,
Promise,
SafeSet,
TypedArrayPrototypeSlice,
} = primordials;

const {
Expand Down Expand Up @@ -183,8 +185,10 @@ function asyncAesGcmCipher(
let tag;
switch (mode) {
case kWebCryptoCipherDecrypt:
tag = ArrayBufferPrototypeSlice(data, -tagByteLength);
data = ArrayBufferPrototypeSlice(data, 0, -tagByteLength);
const slice = ArrayBufferIsView(data) ?
TypedArrayPrototypeSlice : ArrayBufferPrototypeSlice;
tag = slice(data, -tagByteLength);
data = slice(data, 0, -tagByteLength);
break;
case kWebCryptoCipherEncrypt:
tag = tagByteLength;
Expand Down
40 changes: 39 additions & 1 deletion test/parallel/test-webcrypto-encrypt-decrypt-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const { subtle } = require('crypto').webcrypto;
const { getRandomValues, subtle } = require('crypto').webcrypto;

async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
const key = await subtle.importKey(
Expand Down Expand Up @@ -196,3 +196,41 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
await Promise.all(variations);
})().then(common.mustCall());
}

{
(async function() {
const secretKey = await subtle.generateKey(
{
name: 'AES-GCM',
length: 256,
},
false,
['encrypt', 'decrypt'],
);

const iv = getRandomValues(new Uint8Array(12));
const aad = getRandomValues(new Uint8Array(32));

const encrypted = await subtle.encrypt(
{
name: 'AES-GCM',
iv,
additionalData: aad,
tagLength: 128
},
secretKey,
getRandomValues(new Uint8Array(32))
);

await subtle.decrypt(
{
name: 'AES-GCM',
iv,
additionalData: aad,
tagLength: 128,
},
secretKey,
new Uint8Array(encrypted),
);
})().then(common.mustCall());
}