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: move _scrypt call out of handleError funct #28318

Closed
wants to merge 5 commits 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
7 changes: 3 additions & 4 deletions lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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

Expand Down
9 changes: 4 additions & 5 deletions lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
7 changes: 3 additions & 4 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,21 @@ 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) {
options = check(password, salt, keylen, options);
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);

function handleError(ex) {
if (ex === undefined)
return;

Expand Down