Skip to content

Commit 40a0757

Browse files
authored
crypto: remove webcrypto HKDF and PBKDF2 default-applied lengths
PR-URL: #44945 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent eeec3eb commit 40a0757

4 files changed

Lines changed: 47 additions & 48 deletions

File tree

‎lib/internal/crypto/hkdf.js‎

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const{
44
FunctionPrototypeCall,
5-
Promise,
65
}=primordials;
76

87
const{
@@ -15,7 +14,6 @@ const {
1514
validateFunction,
1615
validateInteger,
1716
validateString,
18-
validateUint32,
1917
}=require('internal/validators');
2018

2119
const{ kMaxLength }=require('buffer');
@@ -35,6 +33,7 @@ const {
3533

3634
const{
3735
lazyDOMException,
36+
promisify,
3837
}=require('internal/util');
3938

4039
const{
@@ -139,40 +138,33 @@ function hkdfSync(hash, key, salt, info, length) {
139138
returnbits;
140139
}
141140

141+
consthkdfPromise=promisify(hkdf);
142142
asyncfunctionhkdfDeriveBits(algorithm,baseKey,length){
143143
const{ hash }=algorithm;
144144
constsalt=getArrayBufferOrView(algorithm.salt,'algorithm.salt');
145145
constinfo=getArrayBufferOrView(algorithm.info,'algorithm.info');
146146
if(hash===undefined)
147147
thrownewERR_MISSING_OPTION('algorithm.hash');
148148

149-
letbyteLength=512/8;
150-
if(length!==undefined){
151-
if(length===0)
152-
throwlazyDOMException('length cannot be zero','OperationError');
153-
if(length===null)
154-
throwlazyDOMException('length cannot be null','OperationError');
155-
validateUint32(length,'length');
156-
if(length%8){
157-
throwlazyDOMException(
158-
'length must be a multiple of 8',
159-
'OperationError');
160-
}
161-
byteLength=length/8;
149+
if(length===0)
150+
throwlazyDOMException('length cannot be zero','OperationError');
151+
if(length===null)
152+
throwlazyDOMException('length cannot be null','OperationError');
153+
if(length%8){
154+
throwlazyDOMException(
155+
'length must be a multiple of 8',
156+
'OperationError');
162157
}
163158

164-
returnnewPromise((resolve,reject)=>{
165-
hkdf(
166-
normalizeHashName(hash.name),
167-
baseKey[kKeyObject],
168-
salt,
169-
info,
170-
byteLength,
171-
(err,bits)=>{
172-
if(err)returnreject(err);
173-
resolve(bits);
174-
});
175-
});
159+
try{
160+
returnawaithkdfPromise(
161+
normalizeHashName(hash.name),baseKey[kKeyObject],salt,info,length/8,
162+
);
163+
}catch(err){
164+
throwlazyDOMException(
165+
'The operation failed for an operation-specific reason',
166+
{name: 'OperationError',cause: err});
167+
}
176168
}
177169

178170
module.exports={

‎lib/internal/crypto/pbkdf2.js‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const{
44
FunctionPrototypeCall,
5-
Promise,
65
}=primordials;
76

87
const{ Buffer }=require('buffer');
@@ -18,7 +17,6 @@ const {
1817
validateInt32,
1918
validateInteger,
2019
validateString,
21-
validateUint32,
2220
}=require('internal/validators');
2321

2422
const{ERR_MISSING_OPTION}=require('internal/errors').codes;
@@ -32,6 +30,7 @@ const {
3230

3331
const{
3432
lazyDOMException,
33+
promisify,
3534
}=require('internal/util');
3635

3736
functionpbkdf2(password,salt,iterations,keylen,digest,callback){
@@ -100,6 +99,7 @@ function check(password, salt, iterations, keylen, digest) {
10099
return{ password, salt, iterations, keylen, digest };
101100
}
102101

102+
constpbkdf2Promise=promisify(pbkdf2);
103103
asyncfunctionpbkdf2DeriveBits(algorithm,baseKey,length){
104104
const{ iterations }=algorithm;
105105
let{ hash }=algorithm;
@@ -116,27 +116,26 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
116116

117117
constraw=baseKey[kKeyObject].export();
118118

119-
letbyteLength=64;// the default
120-
if(length!==undefined){
121-
if(length===0)
122-
throwlazyDOMException('length cannot be zero','OperationError');
123-
if(length===null)
124-
throwlazyDOMException('length cannot be null','OperationError');
125-
validateUint32(length,'length');
126-
if(length%8){
127-
throwlazyDOMException(
128-
'length must be a multiple of 8',
129-
'OperationError');
130-
}
131-
byteLength=length/8;
119+
if(length===0)
120+
throwlazyDOMException('length cannot be zero','OperationError');
121+
if(length===null)
122+
throwlazyDOMException('length cannot be null','OperationError');
123+
if(length%8){
124+
throwlazyDOMException(
125+
'length must be a multiple of 8',
126+
'OperationError');
127+
}
128+
129+
letresult;
130+
try{
131+
result=awaitpbkdf2Promise(raw,salt,iterations,length/8,hash);
132+
}catch(err){
133+
throwlazyDOMException(
134+
'The operation failed for an operation-specific reason',
135+
{name: 'OperationError',cause: err});
132136
}
133137

134-
returnnewPromise((resolve,reject)=>{
135-
pbkdf2(raw,salt,iterations,byteLength,hash,(err,result)=>{
136-
if(err)returnreject(err);
137-
resolve(result.buffer);
138-
});
139-
});
138+
returnresult.buffer;
140139
}
141140

142141
module.exports={

‎test/parallel/test-webcrypto-derivebits-hkdf.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ async function testDeriveBitsBadLengths(
257257
};
258258

259259
returnPromise.all([
260+
assert.rejects(
261+
subtle.deriveBits(algorithm,baseKeys[size],undefined),{
262+
name: 'OperationError',
263+
}),
260264
assert.rejects(
261265
subtle.deriveBits(algorithm,baseKeys[size],0),{
262266
message: /lengthcannotbezero/,

‎test/pummel/test-webcrypto-derivebits-pbkdf2.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ async function testDeriveBitsBadLengths(
445445
};
446446

447447
returnPromise.all([
448+
assert.rejects(
449+
subtle.deriveBits(algorithm,baseKeys[size],undefined),{
450+
name: 'OperationError',
451+
}),
448452
assert.rejects(
449453
subtle.deriveBits(algorithm,baseKeys[size],0),{
450454
message: /lengthcannotbezero/,

0 commit comments

Comments
 (0)