src: refactor SubtleCrypto algorithm and length validations · nodejs/node@bf788d9 · GitHub
Skip to content

Commit bf788d9

Browse files
panvatargos
authored andcommitted
src: refactor SubtleCrypto algorithm and length validations
PR-URL: #57319 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 37664e8 commit bf788d9

13 files changed

Lines changed: 103 additions & 160 deletions

File tree

lib/internal/crypto/aes.js

Lines changed: 2 additions & 50 deletions

lib/internal/crypto/cfrg.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,7 @@ function cfrgImportKey(
329329
extractable);
330330
}
331331

332-
function validateEdDSASignVerifyAlgorithm(algorithm) {
333-
if (algorithm.name === 'Ed448' && algorithm.context?.byteLength) {
334-
throw lazyDOMException(
335-
'Non zero-length context is not yet supported.', 'NotSupportedError');
336-
}
337-
}
338-
339332
function eddsaSignVerify(key, data, algorithm, signature) {
340-
validateEdDSASignVerifyAlgorithm(algorithm);
341333
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
342334
const type = mode === kSignJobModeSign ? 'private' : 'public';
343335

lib/internal/crypto/diffiehellman.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,9 @@ function diffieHellman(options) {
297297
}
298298

299299
let masks;
300-
301-
function validateEcdhDeriveBitsAlgorithmAndLength(algorithm, length) {
302-
if (algorithm.public.type !== 'public') {
303-
throw lazyDOMException(
304-
'algorithm.public must be a public key', 'InvalidAccessError');
305-
}
306-
307-
if (algorithm.name !== algorithm.public.algorithm.name) {
308-
throw lazyDOMException(`algorithm.public must be an ${algorithm.name} key`, 'InvalidAccessError');
309-
}
310-
}
311-
312300
// The ecdhDeriveBits function is part of the Web Crypto API and serves both
313301
// deriveKeys and deriveBits functions.
314302
async function ecdhDeriveBits(algorithm, baseKey, length) {
315-
validateEcdhDeriveBitsAlgorithmAndLength(algorithm, length);
316303
const { 'public': key } = algorithm;
317304

318305
if (baseKey.type !== 'private') {

lib/internal/crypto/ec.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ObjectPrototypeHasOwnProperty,
54
SafeSet,
65
} = primordials;
76

@@ -76,16 +75,7 @@ function createECPublicKeyRaw(namedCurve, keyData) {
7675
return new PublicKeyObject(handle);
7776
}
7877

79-
function validateEcKeyAlgorithm(algorithm) {
80-
if (!ObjectPrototypeHasOwnProperty(kNamedCurveAliases, algorithm.namedCurve)) {
81-
throw lazyDOMException(
82-
'Unrecognized namedCurve',
83-
'NotSupportedError');
84-
}
85-
}
86-
8778
async function ecGenerateKey(algorithm, extractable, keyUsages) {
88-
validateEcKeyAlgorithm(algorithm);
8979
const { name, namedCurve } = algorithm;
9080

9181
const usageSet = new SafeSet(keyUsages);
@@ -158,7 +148,6 @@ function ecImportKey(
158148
extractable,
159149
keyUsages,
160150
) {
161-
validateEcKeyAlgorithm(algorithm);
162151
const { name, namedCurve } = algorithm;
163152

164153
let keyObject;

lib/internal/crypto/hkdf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function hkdfSync(hash, key, salt, info, length) {
138138
}
139139

140140
const hkdfPromise = promisify(hkdf);
141-
function validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length) {
141+
function validateHkdfDeriveBitsLength(length) {
142142
if (length === null)
143143
throw lazyDOMException('length cannot be null', 'OperationError');
144144
if (length % 8) {
@@ -149,7 +149,7 @@ function validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length) {
149149
}
150150

151151
async function hkdfDeriveBits(algorithm, baseKey, length) {
152-
validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length);
152+
validateHkdfDeriveBitsLength(length);
153153
const { hash, salt, info } = algorithm;
154154

155155
if (length === 0)

lib/internal/crypto/keygen.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ const {
3333
parsePrivateKeyEncoding,
3434
} = require('internal/crypto/keys');
3535

36-
const {
37-
kAesKeyLengths,
38-
} = require('internal/crypto/util');
39-
4036
const {
4137
customPromisifyArgs,
4238
kEmptyObject,
@@ -355,7 +351,7 @@ function generateKeyJob(mode, keyType, options) {
355351
validateInteger(length, 'options.length', 8, 2 ** 31 - 1);
356352
break;
357353
case 'aes':
358-
validateOneOf(length, 'options.length', kAesKeyLengths);
354+
validateOneOf(length, 'options.length', [128, 192, 256]);
359355
break;
360356
default:
361357
throw new ERR_INVALID_ARG_VALUE(

lib/internal/crypto/mac.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,7 @@ const {
4040

4141
const generateKey = promisify(_generateKey);
4242

43-
function validateHmacGenerateKeyAlgorithm(algorithm) {
44-
if (algorithm.length !== undefined) {
45-
if (algorithm.length === 0)
46-
throw lazyDOMException(
47-
'Zero-length key is not supported',
48-
'OperationError');
49-
50-
// The Web Crypto spec allows for key lengths that are not multiples of 8. We don't.
51-
if (algorithm.length % 8) {
52-
throw lazyDOMException(
53-
'Unsupported algorithm.length',
54-
'NotSupportedError');
55-
}
56-
}
57-
}
58-
5943
async function hmacGenerateKey(algorithm, extractable, keyUsages) {
60-
validateHmacGenerateKeyAlgorithm(algorithm);
6144
const { hash, name } = algorithm;
6245
let { length } = algorithm;
6346

@@ -96,27 +79,13 @@ function getAlgorithmName(hash) {
9679
}
9780
}
9881

99-
function validateHmacImportKeyAlgorithm(algorithm) {
100-
if (algorithm.length !== undefined) {
101-
if (algorithm.length === 0) {
102-
throw lazyDOMException('Zero-length key is not supported', 'DataError');
103-
}
104-
105-
// The Web Crypto spec allows for key lengths that are not multiples of 8. We don't.
106-
if (algorithm.length % 8) {
107-
throw lazyDOMException('Unsupported algorithm.length', 'NotSupportedError');
108-
}
109-
}
110-
}
111-
11282
function hmacImportKey(
11383
format,
11484
keyData,
11585
algorithm,
11686
extractable,
11787
keyUsages,
11888
) {
119-
validateHmacImportKeyAlgorithm(algorithm);
12089
const usagesSet = new SafeSet(keyUsages);
12190
if (hasAnyNotIn(usagesSet, ['sign', 'verify'])) {
12291
throw lazyDOMException(

lib/internal/crypto/pbkdf2.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ function check(password, salt, iterations, keylen, digest) {
9292
}
9393

9494
const pbkdf2Promise = promisify(pbkdf2);
95-
function validatePbkdf2DeriveBitsAlgorithmAndLength(algorithm, length) {
96-
if (algorithm.iterations === 0)
97-
throw lazyDOMException(
98-
'iterations cannot be zero',
99-
'OperationError');
100-
95+
function validatePbkdf2DeriveBitsLength(length) {
10196
if (length === null)
10297
throw lazyDOMException('length cannot be null', 'OperationError');
10398

@@ -109,7 +104,7 @@ function validatePbkdf2DeriveBitsAlgorithmAndLength(algorithm, length) {
109104
}
110105

111106
async function pbkdf2DeriveBits(algorithm, baseKey, length) {
112-
validatePbkdf2DeriveBitsAlgorithmAndLength(algorithm, length);
107+
validatePbkdf2DeriveBitsLength(length);
113108
const { iterations, hash, salt } = algorithm;
114109

115110
if (length === 0)

lib/internal/crypto/rsa.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,17 @@ function rsaOaepCipher(mode, key, data, algorithm) {
111111
algorithm.label));
112112
}
113113

114-
function validateRsaKeyGenerateAlgorithm(algorithm) {
114+
async function rsaKeyGenerate(
115+
algorithm,
116+
extractable,
117+
keyUsages,
118+
) {
115119
const publicExponentConverted = bigIntArrayToUnsignedInt(algorithm.publicExponent);
116120
if (publicExponentConverted === undefined) {
117121
throw lazyDOMException(
118122
'The publicExponent must be equivalent to an unsigned 32-bit value',
119123
'OperationError');
120124
}
121-
122-
return publicExponentConverted;
123-
}
124-
125-
async function rsaKeyGenerate(
126-
algorithm,
127-
extractable,
128-
keyUsages,
129-
) {
130-
const publicExponentConverted = validateRsaKeyGenerateAlgorithm(algorithm);
131125
const {
132126
name,
133127
modulusLength,

lib/internal/crypto/util.js

Lines changed: 0 additions & 12 deletions

0 commit comments

Comments
 (0)