Skip to content

Commit 4957562

Browse 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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 4a54ebc commit 4957562

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

‎lib/internal/crypto/pbkdf2.js‎

Lines 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
functionpbkdf2(password,salt,iterations,keylen,digest,callback){
2218
if(typeofdigest==='function'){
@@ -39,22 +35,12 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback){
3935
if(digest!==null&&typeofdigest!=='string')
4036
thrownewERR_INVALID_ARG_TYPE('digest',['string','null'],digest);
4137

42-
password=checkIsArrayBufferView('password',toBuf(password));
43-
salt=checkIsArrayBufferView('salt',toBuf(salt));
44-
45-
if(typeofiterations!=='number')
46-
thrownewERR_INVALID_ARG_TYPE('iterations','number',iterations);
47-
48-
if(iterations<0)
49-
thrownewERR_OUT_OF_RANGE('iterations',
50-
'a non-negative number',
51-
iterations);
52-
53-
if(typeofkeylen!=='number')
54-
thrownewERR_INVALID_ARG_TYPE('keylen','number',keylen);
55-
56-
if(keylen<0||!Number.isInteger(keylen)||keylen>INT_MAX)
57-
thrownewERR_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
constencoding=getDefaultEncoding();
6046

‎lib/internal/crypto/sig.js‎

Lines 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
varpssSaltLength=getSaltLength(options);
7979

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

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

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

115115
varpssSaltLength=getSaltLength(options);
116116

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

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

‎lib/internal/crypto/util.js‎

Lines 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
vardefaultEncoding='buffer';
3034

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

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

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

0 commit comments

Comments
(0)