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: check for valid iteration length in pbkdf2 #3173

Closed
wants to merge 1 commit 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
8 changes: 6 additions & 2 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5250,6 +5250,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
int passlen = -1;
int saltlen = -1;
double raw_keylen = -1;
double raw_iter = -1;
int keylen = -1;
int iter = -1;
PBKDF2Request* req = nullptr;
Expand Down Expand Up @@ -5292,12 +5293,15 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
goto err;
}

iter = args[2]->Int32Value();
if (iter < 0) {
raw_iter = args[2]->NumberValue();
if (raw_iter < 0 || isnan(raw_iter) || isinf(raw_iter) ||
raw_iter > INT_MAX) {
type_error = "Bad iterations";
goto err;
}

iter = static_cast<int>(raw_iter);

if (!args[3]->IsNumber()) {
type_error = "Key length not a number";
goto err;
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,30 @@ assert.throws(function() {
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, 4073741824, 'sha256', common.fail);
}, /Bad key length/);

// Should not work with negative iterations
assert.throws(function() {
crypto.pbkdf2('password', 'salt', -1, 1, 'sha256', common.fail);
}, /Bad iterations/);

// Should not work with Infinity iterations
assert.throws(function() {
crypto.pbkdf2('password', 'salt', Infinity, 1, 'sha256', common.fail);
}, /Bad iterations/);

// Should not work with -Infinity iterations
assert.throws(function() {
crypto.pbkdf2('password', 'salt', -Infinity, 1, 'sha256', common.fail);
}, /Bad iterations/);

// Should not work with NaN iterations
assert.throws(function() {
crypto.pbkdf2('password', 'salt', NaN, 1, 'sha256', common.fail);
}, /Bad iterations/);

// Should not work with an iteration number that does
// not fit into 32 signed bits
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 4073741824, 1, 'sha256', common.fail);
}, /Bad iterations/);