Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4957562

Browse filesBrowse files
bnoordhuistargos
authored andcommitted
crypto: DRY type checking
Factor out some common code. The `checkUint()` function will also be used in a follow-up commit that adds scrypt support to core. PR-URL: #20816 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 4a54ebc commit 4957562
Copy full SHA for 4957562

File tree

Expand file treeCollapse file tree

3 files changed

+26
-24
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-24
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/pbkdf2.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js
+7-21Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ const {
44
ERR_INVALID_ARG_TYPE,
55
ERR_INVALID_CALLBACK,
66
ERR_CRYPTO_INVALID_DIGEST,
7-
ERR_OUT_OF_RANGE
87
} = require('internal/errors').codes;
98
const {
109
checkIsArrayBufferView,
10+
checkIsUint,
1111
getDefaultEncoding,
12-
toBuf
1312
} = require('internal/crypto/util');
1413
const {
1514
PBKDF2
1615
} = process.binding('crypto');
17-
const {
18-
INT_MAX
19-
} = process.binding('constants').crypto;
2016

2117
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
2218
if (typeof digest === 'function') {
@@ -39,22 +35,12 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
3935
if (digest !== null && typeof digest !== 'string')
4036
throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null'], digest);
4137

42-
password = checkIsArrayBufferView('password', toBuf(password));
43-
salt = checkIsArrayBufferView('salt', toBuf(salt));
44-
45-
if (typeof iterations !== 'number')
46-
throw new ERR_INVALID_ARG_TYPE('iterations', 'number', iterations);
47-
48-
if (iterations < 0)
49-
throw new ERR_OUT_OF_RANGE('iterations',
50-
'a non-negative number',
51-
iterations);
52-
53-
if (typeof keylen !== 'number')
54-
throw new ERR_INVALID_ARG_TYPE('keylen', 'number', keylen);
55-
56-
if (keylen < 0 || !Number.isInteger(keylen) || keylen > INT_MAX)
57-
throw new ERR_OUT_OF_RANGE('keylen', `>= 0 && <= ${INT_MAX}`, keylen);
38+
password = checkIsArrayBufferView('password', password);
39+
salt = checkIsArrayBufferView('salt', salt);
40+
// FIXME(bnoordhuis) The error message is in fact wrong since |iterations|
41+
// cannot be > INT_MAX. Adjust in the next major release.
42+
iterations = checkIsUint('iterations', iterations, 'a non-negative number');
43+
keylen = checkIsUint('keylen', keylen);
5844

5945
const encoding = getDefaultEncoding();
6046

Collapse file

‎lib/internal/crypto/sig.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) {
7777

7878
var pssSaltLength = getSaltLength(options);
7979

80-
key = checkIsArrayBufferView('key', toBuf(key));
80+
key = checkIsArrayBufferView('key', key);
8181

8282
var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);
8383

@@ -114,7 +114,7 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
114114

115115
var pssSaltLength = getSaltLength(options);
116116

117-
key = checkIsArrayBufferView('key', toBuf(key));
117+
key = checkIsArrayBufferView('key', key);
118118

119119
signature = checkIsArrayBufferView('signature',
120120
toBuf(signature, sigEncoding));
Collapse file

‎lib/internal/crypto/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const {
1515
const {
1616
ERR_CRYPTO_ENGINE_UNKNOWN,
1717
ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH,
18-
ERR_INVALID_ARG_TYPE
18+
ERR_INVALID_ARG_TYPE,
19+
ERR_OUT_OF_RANGE,
1920
} = require('internal/errors').codes;
2021
const { Buffer } = require('buffer');
2122
const {
@@ -25,6 +26,9 @@ const {
2526
const {
2627
isArrayBufferView
2728
} = require('internal/util/types');
29+
const {
30+
INT_MAX
31+
} = process.binding('constants').crypto;
2832

2933
var defaultEncoding = 'buffer';
3034

@@ -84,6 +88,7 @@ function timingSafeEqual(buf1, buf2) {
8488
}
8589

8690
function checkIsArrayBufferView(name, buffer) {
91+
buffer = toBuf(buffer);
8792
if (!isArrayBufferView(buffer)) {
8893
throw new ERR_INVALID_ARG_TYPE(
8994
name,
@@ -94,8 +99,19 @@ function checkIsArrayBufferView(name, buffer) {
9499
return buffer;
95100
}
96101

102+
function checkIsUint(name, value, errmsg = `>= 0 && <= ${INT_MAX}`) {
103+
if (typeof value !== 'number')
104+
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
105+
106+
if (value < 0 || !Number.isInteger(value) || value > INT_MAX)
107+
throw new ERR_OUT_OF_RANGE(name, errmsg, value);
108+
109+
return value;
110+
}
111+
97112
module.exports = {
98113
checkIsArrayBufferView,
114+
checkIsUint,
99115
getCiphers,
100116
getCurves,
101117
getDefaultEncoding,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.