Skip to content

Commit 843edb7

Browse files
committed
crypto: use WebIDL convertors in WebCryptoAPI
1 parent 9eb363a commit 843edb7

24 files changed

+1303
-256
lines changed

lib/internal/crypto/aes.js

-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const {
3232
} = internalBinding('crypto');
3333

3434
const {
35-
getArrayBufferOrView,
3635
hasAnyNotIn,
3736
jobPromise,
3837
validateByteLength,
@@ -112,7 +111,6 @@ function getVariant(name, length) {
112111
}
113112

114113
function asyncAesCtrCipher(mode, key, data, { counter, length }) {
115-
counter = getArrayBufferOrView(counter, 'algorithm.counter');
116114
validateByteLength(counter, 'algorithm.counter', 16);
117115
// The length must specify an integer between 1 and 128. While
118116
// there is no default, this should typically be 64.
@@ -135,7 +133,6 @@ function asyncAesCtrCipher(mode, key, data, { counter, length }) {
135133
}
136134

137135
function asyncAesCbcCipher(mode, key, data, { iv }) {
138-
iv = getArrayBufferOrView(iv, 'algorithm.iv');
139136
validateByteLength(iv, 'algorithm.iv', 16);
140137
return jobPromise(() => new AESCipherJob(
141138
kCryptoJobAsync,
@@ -166,12 +163,9 @@ function asyncAesGcmCipher(
166163
'OperationError'));
167164
}
168165

169-
iv = getArrayBufferOrView(iv, 'algorithm.iv');
170166
validateMaxBufferLength(iv, 'algorithm.iv');
171167

172168
if (additionalData !== undefined) {
173-
additionalData =
174-
getArrayBufferOrView(additionalData, 'algorithm.additionalData');
175169
validateMaxBufferLength(additionalData, 'algorithm.additionalData');
176170
}
177171

lib/internal/crypto/cfrg.js

-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const {
1818
} = internalBinding('crypto');
1919

2020
const {
21-
getArrayBufferOrView,
2221
getUsagesUnion,
2322
hasAnyNotIn,
2423
jobPromise,
@@ -73,7 +72,6 @@ function verifyAcceptableCfrgKeyUse(name, isPublic, usages) {
7372

7473
function createCFRGRawKey(name, keyData, isPublic) {
7574
const handle = new KeyObjectHandle();
76-
keyData = getArrayBufferOrView(keyData, 'keyData');
7775

7876
switch (name) {
7977
case 'Ed25519':
@@ -337,8 +335,6 @@ function eddsaSignVerify(key, data, { name, context }, signature) {
337335
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
338336

339337
if (name === 'Ed448' && context !== undefined) {
340-
context =
341-
getArrayBufferOrView(context, 'algorithm.context');
342338
if (context.byteLength !== 0) {
343339
throw lazyDOMException(
344340
'Non zero-length context is not yet supported.', 'NotSupportedError');

lib/internal/crypto/diffiehellman.js

-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ const {
4848

4949
const {
5050
KeyObject,
51-
isCryptoKey,
5251
} = require('internal/crypto/keys');
5352

5453
const {
@@ -324,8 +323,6 @@ async function ecdhDeriveBits(algorithm, baseKey, length) {
324323
// give us everything that is generated.
325324
if (length !== null)
326325
validateUint32(length, 'length');
327-
if (!isCryptoKey(key))
328-
throw new ERR_INVALID_ARG_TYPE('algorithm.public', 'CryptoKey', key);
329326

330327
if (key.type !== 'public') {
331328
throw lazyDOMException(

lib/internal/crypto/ec.js

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const {
2424
} = require('internal/errors');
2525

2626
const {
27-
getArrayBufferOrView,
2827
getUsagesUnion,
2928
hasAnyNotIn,
3029
jobPromise,
@@ -76,7 +75,6 @@ function verifyAcceptableEcKeyUse(name, isPublic, usages) {
7675

7776
function createECPublicKeyRaw(namedCurve, keyData) {
7877
const handle = new KeyObjectHandle();
79-
keyData = getArrayBufferOrView(keyData, 'keyData');
8078

8179
if (!handle.initECRaw(kNamedCurveAliases[namedCurve], keyData)) {
8280
throw lazyDOMException('Invalid keyData', 'DataError');

lib/internal/crypto/hash.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ const {
1414
} = internalBinding('crypto');
1515

1616
const {
17-
getArrayBufferOrView,
1817
getDefaultEncoding,
1918
getStringOption,
2019
jobPromise,
21-
normalizeAlgorithm,
2220
normalizeHashName,
2321
validateMaxBufferLength,
2422
kHandle,
@@ -168,13 +166,8 @@ Hmac.prototype._transform = Hash.prototype._transform;
168166
// Implementation for WebCrypto subtle.digest()
169167

170168
async function asyncDigest(algorithm, data) {
171-
algorithm = normalizeAlgorithm(algorithm);
172-
data = getArrayBufferOrView(data, 'data');
173169
validateMaxBufferLength(data, 'data');
174170

175-
if (algorithm.length !== undefined)
176-
validateUint32(algorithm.length, 'algorithm.length');
177-
178171
switch (algorithm.name) {
179172
case 'SHA-1':
180173
// Fall through
@@ -186,8 +179,7 @@ async function asyncDigest(algorithm, data) {
186179
return jobPromise(() => new HashJob(
187180
kCryptoJobAsync,
188181
normalizeHashName(algorithm.name),
189-
data,
190-
algorithm.length));
182+
data));
191183
}
192184

193185
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');

lib/internal/crypto/hkdf.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
const { kMaxLength } = require('buffer');
2020

2121
const {
22-
getArrayBufferOrView,
2322
normalizeHashName,
2423
toBuf,
2524
validateByteSource,
@@ -45,7 +44,6 @@ const {
4544
codes: {
4645
ERR_INVALID_ARG_TYPE,
4746
ERR_OUT_OF_RANGE,
48-
ERR_MISSING_OPTION,
4947
},
5048
hideStackFrames,
5149
} = require('internal/errors');
@@ -140,11 +138,7 @@ function hkdfSync(hash, key, salt, info, length) {
140138

141139
const hkdfPromise = promisify(hkdf);
142140
async function hkdfDeriveBits(algorithm, baseKey, length) {
143-
const { hash } = algorithm;
144-
const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
145-
const info = getArrayBufferOrView(algorithm.info, 'algorithm.info');
146-
if (hash === undefined)
147-
throw new ERR_MISSING_OPTION('algorithm.hash');
141+
const { hash, salt, info } = algorithm;
148142

149143
if (length === 0)
150144
throw lazyDOMException('length cannot be zero', 'OperationError');

lib/internal/crypto/pbkdf2.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ const {
1515
const {
1616
validateFunction,
1717
validateInt32,
18-
validateInteger,
1918
validateString,
2019
} = require('internal/validators');
2120

22-
const { ERR_MISSING_OPTION } = require('internal/errors').codes;
23-
2421
const {
2522
getArrayBufferOrView,
2623
getDefaultEncoding,
@@ -101,19 +98,12 @@ function check(password, salt, iterations, keylen, digest) {
10198

10299
const pbkdf2Promise = promisify(pbkdf2);
103100
async function pbkdf2DeriveBits(algorithm, baseKey, length) {
104-
const { iterations } = algorithm;
105-
let { hash } = algorithm;
106-
const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
107-
if (hash === undefined)
108-
throw new ERR_MISSING_OPTION('algorithm.hash');
109-
validateInteger(iterations, 'algorithm.iterations');
101+
const { iterations, hash, salt } = algorithm;
110102
if (iterations === 0)
111103
throw lazyDOMException(
112104
'iterations cannot be zero',
113105
'OperationError');
114106

115-
hash = normalizeHashName(hash.name);
116-
117107
const raw = baseKey[kKeyObject].export();
118108

119109
if (length === 0)
@@ -128,7 +118,9 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
128118

129119
let result;
130120
try {
131-
result = await pbkdf2Promise(raw, salt, iterations, length / 8, hash);
121+
result = await pbkdf2Promise(
122+
raw, salt, iterations, length / 8, normalizeHashName(hash.name),
123+
);
132124
} catch (err) {
133125
throw lazyDOMException(
134126
'The operation failed for an operation-specific reason',

lib/internal/crypto/rsa.js

-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const {
3535

3636
const {
3737
bigIntArrayToUnsignedInt,
38-
getArrayBufferOrView,
3938
getUsagesUnion,
4039
hasAnyNotIn,
4140
jobPromise,
@@ -104,7 +103,6 @@ function rsaOaepCipher(mode, key, data, { label }) {
104103
'InvalidAccessError');
105104
}
106105
if (label !== undefined) {
107-
label = getArrayBufferOrView(label, 'algorithm.label');
108106
validateMaxBufferLength(label, 'algorithm.label');
109107
}
110108

0 commit comments

Comments
 (0)