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 and simplify prime option validation #37164

Merged
merged 1 commit into from
Feb 6, 2021
Merged
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
99 changes: 33 additions & 66 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,9 @@ function randomUUID(options) {
return uuid.latin1Slice(0, 36);
}

function generatePrime(size, options, callback) {
validateUint32(size, 'size', true);
if (typeof options === 'function') {
callback = options;
options = {};
}
validateCallback(callback);
function createRandomPrimeJob(type, size, options) {
validateObject(options, 'options');

const {
safe = false,
bigint = false,
Expand All @@ -413,7 +408,7 @@ function generatePrime(size, options, callback) {

if (add !== undefined) {
if (typeof add === 'bigint') {
add = Buffer.from(toHexPadded(add), 'hex');
add = unsignedBigIntToBuffer(add, 'options.add');
} else if (!isAnyArrayBuffer(add) && !isArrayBufferView(add)) {
throw new ERR_INVALID_ARG_TYPE(
'options.add',
Expand All @@ -430,7 +425,7 @@ function generatePrime(size, options, callback) {

if (rem !== undefined) {
if (typeof rem === 'bigint') {
rem = Buffer.from(toHexPadded(rem), 'hex');
rem = unsignedBigIntToBuffer(rem, 'options.rem');
} else if (!isAnyArrayBuffer(rem) && !isArrayBufferView(rem)) {
throw new ERR_INVALID_ARG_TYPE(
'options.rem',
Expand All @@ -445,7 +440,20 @@ function generatePrime(size, options, callback) {
}
}

const job = new RandomPrimeJob(kCryptoJobAsync, size, safe, add, rem);
const job = new RandomPrimeJob(type, size, safe, add, rem);
job.result = bigint ? arrayBufferToUnsignedBigInt : (p) => p;
return job;
}

function generatePrime(size, options, callback) {
validateUint32(size, 'size', true);
if (typeof options === 'function') {
callback = options;
options = {};
}
validateCallback(callback);

const job = createRandomPrimeJob(kCryptoJobAsync, size, options);
job.ondone = (err, prime) => {
if (err) {
callback(err);
Expand All @@ -454,79 +462,38 @@ function generatePrime(size, options, callback) {

callback(
undefined,
bigint ?
BigInt(`0x${Buffer.from(prime).toString('hex')}`) :
prime);
job.result(prime));
};
job.run();
}

function generatePrimeSync(size, options = {}) {
validateUint32(size, 'size', true);
validateObject(options, 'options');
const {
safe = false,
bigint = false,
} = options;
let {
add,
rem,
} = options;
validateBoolean(safe, 'options.safe');
validateBoolean(bigint, 'options.bigint');

if (add !== undefined) {
if (typeof add === 'bigint') {
add = Buffer.from(toHexPadded(add), 'hex');
} else if (!isAnyArrayBuffer(add) && !isArrayBufferView(add)) {
throw new ERR_INVALID_ARG_TYPE(
'options.add',
[
'ArrayBuffer',
'TypedArray',
'Buffer',
'DataView',
'bigint',
],
add);
}
}

if (rem !== undefined) {
if (typeof rem === 'bigint') {
rem = Buffer.from(toHexPadded(rem), 'hex');
} else if (!isAnyArrayBuffer(rem) && !isArrayBufferView(rem)) {
throw new ERR_INVALID_ARG_TYPE(
'options.rem',
[
'ArrayBuffer',
'TypedArray',
'Buffer',
'DataView',
'bigint',
],
rem);
}
}

const job = new RandomPrimeJob(kCryptoJobSync, size, safe, add, rem);
const job = createRandomPrimeJob(kCryptoJobSync, size, options);
const [err, prime] = job.run();
if (err)
throw err;
return job.result(prime);
}

return bigint ?
BigInt(`0x${Buffer.from(prime).toString('hex')}`) :
prime;
function arrayBufferToUnsignedBigInt(arrayBuffer) {
return BigInt(`0x${Buffer.from(arrayBuffer).toString('hex')}`);
}

function toHexPadded(bigint) {
function unsignedBigIntToBuffer(bigint, name) {
if (bigint < 0) {
throw new ERR_OUT_OF_RANGE(name, '>= 0', bigint);
}

const hex = bigint.toString(16);
Copy link
Member Author

@tniessen tniessen Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation: The value hex begins with a minus sign if bigint < 0, causing Buffer.from(hex, 'hex') to return an empty buffer, which OpenSSL treats as a 0.

return hex.padStart(hex.length + (hex.length % 2), 0);
const padded = hex.padStart(hex.length + (hex.length % 2), 0);
return Buffer.from(padded, 'hex');
}

function checkPrime(candidate, options = {}, callback) {
if (typeof candidate === 'bigint')
candidate = Buffer.from(toHexPadded(candidate), 'hex');
candidate = unsignedBigIntToBuffer(candidate, 'candidate');
if (!isAnyArrayBuffer(candidate) && !isArrayBufferView(candidate)) {
throw new ERR_INVALID_ARG_TYPE(
'candidate',
Expand Down Expand Up @@ -559,7 +526,7 @@ function checkPrime(candidate, options = {}, callback) {

function checkPrimeSync(candidate, options = {}) {
if (typeof candidate === 'bigint')
candidate = Buffer.from(toHexPadded(candidate), 'hex');
candidate = unsignedBigIntToBuffer(candidate, 'candidate');
if (!isAnyArrayBuffer(candidate) && !isArrayBufferView(candidate)) {
throw new ERR_INVALID_ARG_TYPE(
'candidate',
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-crypto-prime.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ const pCheckPrime = promisify(checkPrime);
});
});

{
// Negative BigInts should not be converted to 0 silently.

assert.throws(() => generatePrime(20, { add: -1n }, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "options.add" is out of range. It must be >= 0. ' +
'Received -1n'
});

assert.throws(() => generatePrime(20, { rem: -1n }, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "options.rem" is out of range. It must be >= 0. ' +
'Received -1n'
});

assert.throws(() => checkPrime(-1n, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "candidate" is out of range. It must be >= 0. ' +
'Received -1n'
});
}

generatePrime(80, common.mustSucceed((prime) => {
assert(checkPrimeSync(prime));
checkPrime(prime, common.mustSucceed((result) => {
Expand Down