From 13b51d249f8160d2ffb22820f8bc3df7fcc06ba0 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Jun 2019 08:09:17 +0200 Subject: [PATCH 1/5] crypto: move _scrypt call out of handleError funct This commit moves the _scrypt function call out of the handleError function, which now only takes in an error object as its parameter. The motivation for this is to hopefully improve readability as it was not clear to me the first time I stepped through the code where the actual call to _scrypt was. --- lib/internal/crypto/scrypt.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/internal/crypto/scrypt.js b/lib/internal/crypto/scrypt.js index 0ed4140c9c5df5..f1c81b0d2dbac6 100644 --- a/lib/internal/crypto/scrypt.js +++ b/lib/internal/crypto/scrypt.js @@ -44,7 +44,7 @@ function scrypt(password, salt, keylen, options, callback = defaults) { callback.call(wrap, null, keybuf.toString(encoding)); }; - handleError(keybuf, password, salt, N, r, p, maxmem, wrap); + handleError(_scrypt(keybuf, password, salt, N, r, p, maxmem, wrap)); } function scryptSync(password, salt, keylen, options = defaults) { @@ -52,22 +52,20 @@ function scryptSync(password, salt, keylen, options = defaults) { const { N, r, p, maxmem } = options; ({ password, salt, keylen } = options); const keybuf = Buffer.alloc(keylen); - handleError(keybuf, password, salt, N, r, p, maxmem); + handleError(_scrypt(keybuf, password, salt, N, r, p, maxmem)); const encoding = getDefaultEncoding(); if (encoding === 'buffer') return keybuf; return keybuf.toString(encoding); } -function handleError(keybuf, password, salt, N, r, p, maxmem, wrap) { - const ex = _scrypt(keybuf, password, salt, N, r, p, maxmem, wrap); - - if (ex === undefined) +function handleError(error) { + if (error === undefined) return; - if (ex === null) + if (error === null) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER(); // Bad N, r, p, or maxmem. - throw ex; // Scrypt operation failed, exception object contains details. + throw error; // Scrypt operation failed, exception object contains details. } function check(password, salt, keylen, options) { From ec549532192779a830a7ff95b39dd5a8055430b0 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Jun 2019 11:47:41 +0200 Subject: [PATCH 2/5] fixup! crypto: move _scrypt call out of handleError funct --- lib/internal/crypto/scrypt.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/crypto/scrypt.js b/lib/internal/crypto/scrypt.js index f1c81b0d2dbac6..50a2bbc1536026 100644 --- a/lib/internal/crypto/scrypt.js +++ b/lib/internal/crypto/scrypt.js @@ -58,14 +58,14 @@ function scryptSync(password, salt, keylen, options = defaults) { return keybuf.toString(encoding); } -function handleError(error) { - if (error === undefined) +function handleError(ex) { + if (ex === undefined) return; - if (error === null) + if (ex === null) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER(); // Bad N, r, p, or maxmem. - throw error; // Scrypt operation failed, exception object contains details. + throw ex; // Scrypt operation failed, exception object contains details. } function check(password, salt, keylen, options) { From 3b3908d92016e2760ed6fa0dca34fbf0b6080b2c Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Jun 2019 08:09:17 +0200 Subject: [PATCH 3/5] crypto: move _randomBytes call out of handleError funct This commit moves the _randomBytes function call out of the handleError function, which now it takes in an error and a buf object as its parameters. --- lib/internal/crypto/random.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 257b00a9ce0519..f42458aa788985 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -51,7 +51,7 @@ function randomBytes(size, cb) { const buf = Buffer.alloc(size); - if (!cb) return handleError(buf, 0, size); + if (!cb) return handleError(_randomBytes(buf, 0, size), buf); const wrap = new AsyncWrap(Providers.RANDOMBYTESREQUEST); wrap.ondone = (ex) => { // Retains buf while request is in flight. @@ -77,7 +77,7 @@ function randomFillSync(buf, offset = 0, size) { size = assertSize(size, elementSize, offset, buf.byteLength); } - return handleError(buf, offset, size); + return handleError(_randomBytes(buf, offset, size), buf); } function randomFill(buf, offset, size, cb) { @@ -115,8 +115,7 @@ function randomFill(buf, offset, size, cb) { _randomBytes(buf, offset, size, wrap); } -function handleError(buf, offset, size) { - const ex = _randomBytes(buf, offset, size); +function handleError(ex, buf) { if (ex) throw ex; return buf; } From d96eff6dc32a14de516dd5642e92aa7a99456a5a Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Jun 2019 08:09:17 +0200 Subject: [PATCH 4/5] crypto: move _pbkdf2 call out of handleError funct This commit moves the _pbkdf2 function call out of the handleError function, which now only takes in an error and a digest object as its parameters. --- lib/internal/crypto/pbkdf2.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/internal/crypto/pbkdf2.js b/lib/internal/crypto/pbkdf2.js index fc6341b83f4110..d8c4bc9b518dc1 100644 --- a/lib/internal/crypto/pbkdf2.js +++ b/lib/internal/crypto/pbkdf2.js @@ -38,14 +38,15 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) { callback.call(wrap, null, keybuf.toString(encoding)); }; - handleError(keybuf, password, salt, iterations, digest, wrap); + handleError(_pbkdf2(keybuf, password, salt, iterations, digest, wrap), + digest); } function pbkdf2Sync(password, salt, iterations, keylen, digest) { ({ password, salt, iterations, keylen, digest } = check(password, salt, iterations, keylen, digest)); const keybuf = Buffer.alloc(keylen); - handleError(keybuf, password, salt, iterations, digest); + handleError(_pbkdf2(keybuf, password, salt, iterations, digest), digest); const encoding = getDefaultEncoding(); if (encoding === 'buffer') return keybuf; return keybuf.toString(encoding); @@ -71,9 +72,7 @@ function check(password, salt, iterations, keylen, digest) { return { password, salt, iterations, keylen, digest }; } -function handleError(keybuf, password, salt, iterations, digest, wrap) { - const rc = _pbkdf2(keybuf, password, salt, iterations, digest, wrap); - +function handleError(rc, digest) { if (rc === -1) throw new ERR_CRYPTO_INVALID_DIGEST(digest); From 247df3ce021c8e8f89e9005bef7b458ae353bb4c Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Jun 2019 08:09:17 +0200 Subject: [PATCH 5/5] crypto: move _impl call out of handleError funct This commit moves the _impl function call out of the handleError function, which now only takes in an object as its parameter. --- lib/internal/crypto/keygen.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/internal/crypto/keygen.js b/lib/internal/crypto/keygen.js index f646f436f7678a..0a4bde77fa369b 100644 --- a/lib/internal/crypto/keygen.js +++ b/lib/internal/crypto/keygen.js @@ -60,7 +60,7 @@ function generateKeyPair(type, options, callback) { callback.call(wrap, null, pubkey, privkey); }; - handleError(impl, wrap); + handleError(impl(wrap)); } Object.defineProperty(generateKeyPair, customPromisifyArgs, { @@ -70,11 +70,10 @@ Object.defineProperty(generateKeyPair, customPromisifyArgs, { function generateKeyPairSync(type, options) { const impl = check(type, options); - return handleError(impl); + return handleError(impl()); } -function handleError(impl, wrap) { - const ret = impl(wrap); +function handleError(ret) { if (ret === undefined) return; // async