From d16d36f1c2a898fdc3c3d9dab1946665558deea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 19 Jul 2021 13:55:53 +0200 Subject: [PATCH] crypto: support Big(U)Int64Array in getRandomValues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/w3c/webcrypto/issues/255 Fixes: https://github.com/nodejs/node/issues/39442 PR-URL: https://github.com/nodejs/node/pull/39443 Reviewed-By: Colin Ihrig Reviewed-By: Tobias Nießen Reviewed-By: James M Snell Reviewed-By: Zeyu Yang --- lib/internal/crypto/random.js | 2 -- test/parallel/test-webcrypto-random.js | 46 ++++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 5ce158324851d4..c45d53c796d26a 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -50,7 +50,6 @@ const { const { isArrayBufferView, isAnyArrayBuffer, - isBigInt64Array, isFloat32Array, isFloat64Array, } = require('internal/util/types'); @@ -309,7 +308,6 @@ function onJobDone(buf, callback, error) { // be an integer-type TypedArray. function getRandomValues(data) { if (!isArrayBufferView(data) || - isBigInt64Array(data) || isFloat32Array(data) || isFloat64Array(data)) { // Ordinarily this would be an ERR_INVALID_ARG_TYPE. However, diff --git a/test/parallel/test-webcrypto-random.js b/test/parallel/test-webcrypto-random.js index fd933915c5e48e..f2bf2c396fd20a 100644 --- a/test/parallel/test-webcrypto-random.js +++ b/test/parallel/test-webcrypto-random.js @@ -9,8 +9,15 @@ const { Buffer } = require('buffer'); const assert = require('assert'); const { getRandomValues } = require('crypto').webcrypto; -[undefined, null, '', 1, {}, []].forEach((i) => { - assert.throws(() => getRandomValues(i), { code: 17 }); +[ + undefined, null, '', 1, {}, [], + new Float32Array(1), + new Float64Array(1), +].forEach((i) => { + assert.throws( + () => getRandomValues(i), + { name: 'TypeMismatchError', code: 17 }, + ); }); { @@ -18,32 +25,27 @@ const { getRandomValues } = require('crypto').webcrypto; getRandomValues(buf); } -{ - const buf = new Uint8Array(new Array(10).fill(0)); - const before = Buffer.from(buf).toString('hex'); - getRandomValues(buf); - const after = Buffer.from(buf).toString('hex'); - assert.notStrictEqual(before, after); -} - -{ - const buf = new Uint16Array(new Array(10).fill(0)); - const before = Buffer.from(buf).toString('hex'); - getRandomValues(buf); - const after = Buffer.from(buf).toString('hex'); - assert.notStrictEqual(before, after); -} +const intTypedConstructors = [ + Int8Array, + Int16Array, + Int32Array, + Uint8Array, + Uint16Array, + Uint32Array, + BigInt64Array, + BigUint64Array, +]; -{ - const buf = new Uint32Array(new Array(10).fill(0)); - const before = Buffer.from(buf).toString('hex'); +for (const ctor of intTypedConstructors) { + const buf = new ctor(10); + const before = Buffer.from(buf.buffer).toString('hex'); getRandomValues(buf); - const after = Buffer.from(buf).toString('hex'); + const after = Buffer.from(buf.buffer).toString('hex'); assert.notStrictEqual(before, after); } { - const buf = new Uint16Array(new Array(10).fill(0)); + const buf = new Uint16Array(10); const before = Buffer.from(buf).toString('hex'); getRandomValues(new DataView(buf.buffer)); const after = Buffer.from(buf).toString('hex');