From f760ca293ec7555f2ba5d575b91f81c8de47d7eb Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 31 Jan 2022 11:41:06 +0100 Subject: [PATCH 1/4] crypto: clarify `require("crypto").getRandomValues` is Node.js specific Refs: https://github.com/nodejs/node/pull/41779 Refs: https://github.com/nodejs/node/pull/41760 --- doc/api/crypto.md | 4 +++- lib/crypto.js | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 6d1fc3a5672ff8..53e3d94882d5a2 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -4112,7 +4112,8 @@ added: v17.4.0 * `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer} * Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`. -A convenient alias for [`crypto.webcrypto.getRandomValues()`][]. +A convenient alias for [`crypto.randomFillSync()`][] that has the same signature as +[`crypto.webcrypto.getRandomValues()`][]. ### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)` @@ -6005,6 +6006,7 @@ See the [list of SSL OP Flags][] for details. [`crypto.publicEncrypt()`]: #cryptopublicencryptkey-buffer [`crypto.randomBytes()`]: #cryptorandombytessize-callback [`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback +[`crypto.randomFillSync()`]: #cryptorandomfillsyncbuffer-offset-size [`crypto.scrypt()`]: #cryptoscryptpassword-salt-keylen-options-callback [`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray [`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto diff --git a/lib/crypto.js b/lib/crypto.js index ac601a7664697d..412235da2c0b71 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -252,6 +252,10 @@ function getFipsForced() { return 1; } +function getRandomValues(array) { + return randomFillSync(array); +} + ObjectDefineProperty(constants, 'defaultCipherList', { value: getOptionValue('--tls-cipher-list') }); @@ -303,7 +307,7 @@ ObjectDefineProperties(module.exports, { getRandomValues: { configurable: false, enumerable: true, - get() { return lazyWebCrypto().crypto.getRandomValues; }, + get: () => getRandomValues, set: undefined, }, From e77161e0dd5774fb93d026a6a119ea2140541974 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 31 Jan 2022 12:02:17 +0100 Subject: [PATCH 2/4] fixup! crypto: clarify `require("crypto").getRandomValues` is Node.js specific --- doc/api/crypto.md | 4 ++-- lib/crypto.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 53e3d94882d5a2..a85bcc7494f939 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -4112,8 +4112,8 @@ added: v17.4.0 * `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer} * Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`. -A convenient alias for [`crypto.randomFillSync()`][] that has the same signature as -[`crypto.webcrypto.getRandomValues()`][]. +A convenient alias for [`crypto.randomFillSync()`][] that has the same signature +as [`crypto.webcrypto.getRandomValues()`][]. ### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)` diff --git a/lib/crypto.js b/lib/crypto.js index 412235da2c0b71..a839f5fe27b029 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -253,7 +253,7 @@ function getFipsForced() { } function getRandomValues(array) { - return randomFillSync(array); + return lazyWebCrypto().crypto.getRandomValues(array); } ObjectDefineProperty(constants, 'defaultCipherList', { From a3415236e47745f465bdc34ce875254ccced6d26 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 19 Feb 2022 12:27:32 +0100 Subject: [PATCH 3/4] fixup! crypto: clarify `require("crypto").getRandomValues` is Node.js specific --- doc/api/crypto.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index a85bcc7494f939..7ff3f2ab38cc92 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -4112,8 +4112,9 @@ added: v17.4.0 * `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer} * Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`. -A convenient alias for [`crypto.randomFillSync()`][] that has the same signature -as [`crypto.webcrypto.getRandomValues()`][]. +A convenient alias for [`crypto.webcrypto.getRandomValues()`][]. This +implementation is not compliant with the Web Crypto spec, to write +web-compatible code use [`crypto.webcrypto.getRandomValues()`][] instead. ### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)` From 6e0f7877176114679875b9a21103f815265bcdb6 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 19 Feb 2022 16:21:29 +0100 Subject: [PATCH 4/4] fixup! fixup! crypto: clarify `require("crypto").getRandomValues` is Node.js specific --- doc/api/crypto.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 7ff3f2ab38cc92..8498e3b706e895 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -6007,7 +6007,6 @@ See the [list of SSL OP Flags][] for details. [`crypto.publicEncrypt()`]: #cryptopublicencryptkey-buffer [`crypto.randomBytes()`]: #cryptorandombytessize-callback [`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback -[`crypto.randomFillSync()`]: #cryptorandomfillsyncbuffer-offset-size [`crypto.scrypt()`]: #cryptoscryptpassword-salt-keylen-options-callback [`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray [`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto