FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

lib: add and test [EnforceRange] in webcrypto dictionaries · nodejs/node@7f7df06 · GitHub

/ node Public

Commit 7f7df06

Browse files
authored andcommitted
lib: add and test [EnforceRange] in webcrypto dictionaries
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65091 Refs: w3c/webcrypto#555 Refs: WICG/webcrypto-modern-algos#65 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fa52f11 commit 7f7df06

3 files changed

Lines changed: 139 additions & 1 deletion

File tree

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ function deriveBitsImpl(algorithm, baseKey, length = null) {
255255
prefix, 'AlgorithmIdentifier', algorithm, i++);
256256
baseKey = convertSubtleArgument(prefix, 'CryptoKey', baseKey, i++);
257257
if (length !== null) {
258-
length = convertSubtleArgument(prefix, 'unsigned long', length, i++);
258+
length = webidl.converters['unsigned long'](length, {
259+
prefix,
260+
context: kArgumentContexts[i++],
261+
enforceRange: true,
262+
});
259263
}
260264

261265
const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'deriveBits');
@@ -1695,6 +1699,7 @@ class SubtleCrypto {
16951699
length = webidl.converters['unsigned long'](length, {
16961700
prefix,
16971701
context: '3rd argument',
1702+
enforceRange: true,
16981703
});
16991704
}
17001705
} else if (operation === 'getPublicKey') {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const assert = require('assert');
9+
const { SubtleCrypto } = globalThis;
10+
const { subtle } = globalThis.crypto;
11+
12+
const algorithm = {
13+
name: 'HKDF',
14+
hash: 'SHA-256',
15+
info: new Uint8Array(),
16+
salt: new Uint8Array(32),
17+
};
18+
const invalidLength = 2 ** 32;
19+
const expectedError = {
20+
code: 'ERR_OUT_OF_RANGE',
21+
name: 'TypeError',
22+
};
23+
24+
assert.throws(
25+
() => SubtleCrypto.supports('deriveBits', algorithm, invalidLength),
26+
expectedError);
27+
28+
(async () => {
29+
const key = await subtle.importKey(
30+
'raw', new Uint8Array(32), 'HKDF', false, ['deriveBits']);
31+
32+
await assert.rejects(
33+
subtle.deriveBits(algorithm, key, invalidLength),
34+
expectedError);
35+
})().then(common.mustCall());

‎test/parallel/test-webcrypto-webidl.js‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,104 @@ function assertJsonWebKey(actual, expected) {
119119
}
120120
}
121121

122+
// [EnforceRange] integer dictionary members
123+
{
124+
const kOctetMax = 2 ** 8 - 1;
125+
const kUnsignedShortMax = 2 ** 16 - 1;
126+
const kUnsignedLongMax = 2 ** 32 - 1;
127+
const empty = Buffer.alloc(0);
128+
const rsaKeyGen = {
129+
name: 'RSA-PSS',
130+
modulusLength: 2048,
131+
publicExponent: new Uint8Array([1, 0, 1]),
132+
};
133+
const aesKeyParams = { name: 'AES-GCM', length: 128 };
134+
const hmacKeyParams = {
135+
name: 'HMAC',
136+
hash: 'SHA-256',
137+
length: 128,
138+
};
139+
const kmacKeyParams = { name: 'KMAC128', length: 128 };
140+
const cases = [
141+
['RsaKeyGenParams', rsaKeyGen,
142+
{ modulusLength: kUnsignedLongMax }],
143+
['RsaHashedKeyGenParams', { ...rsaKeyGen, hash: 'SHA-256' },
144+
{ modulusLength: kUnsignedLongMax }],
145+
['AesKeyGenParams', aesKeyParams,
146+
{ length: kUnsignedShortMax }],
147+
['RsaPssParams', { name: 'RSA-PSS', saltLength: 20 },
148+
{ saltLength: kUnsignedLongMax }],
149+
['HmacKeyGenParams', hmacKeyParams,
150+
{ length: kUnsignedLongMax }],
151+
['HmacImportParams', hmacKeyParams,
152+
{ length: kUnsignedLongMax }],
153+
['CShakeParams', { name: 'cSHAKE128', outputLength: 256 },
154+
{ outputLength: kUnsignedLongMax }],
155+
['Pbkdf2Params', {
156+
name: 'PBKDF2',
157+
salt: empty,
158+
iterations: 1,
159+
hash: 'SHA-256',
160+
}, { iterations: kUnsignedLongMax }],
161+
['AesDerivedKeyParams', aesKeyParams,
162+
{ length: kUnsignedShortMax }],
163+
['AeadParams', {
164+
name: 'AES-GCM',
165+
iv: Buffer.alloc(12),
166+
tagLength: 128,
167+
}, { tagLength: kOctetMax }],
168+
['AesCtrParams', {
169+
name: 'AES-CTR',
170+
counter: Buffer.alloc(16),
171+
length: 128,
172+
}, { length: kOctetMax }],
173+
['Argon2Params', {
174+
name: 'Argon2id',
175+
nonce: Buffer.alloc(8),
176+
parallelism: 1,
177+
memory: 8,
178+
passes: 1,
179+
version: 0x13,
180+
}, {
181+
parallelism: kUnsignedLongMax,
182+
memory: kUnsignedLongMax,
183+
passes: kUnsignedLongMax,
184+
version: kOctetMax,
185+
}],
186+
['KmacKeyGenParams', kmacKeyParams,
187+
{ length: kUnsignedLongMax }],
188+
['KmacImportParams', kmacKeyParams,
189+
{ length: kUnsignedLongMax }],
190+
['KmacParams', { name: 'KMAC128', outputLength: 256 },
191+
{ outputLength: kUnsignedLongMax }],
192+
['KangarooTwelveParams', { name: 'KT128', outputLength: 256 },
193+
{ outputLength: kUnsignedLongMax }],
194+
['TurboShakeParams', {
195+
name: 'TurboSHAKE128',
196+
outputLength: 256,
197+
domainSeparation: 0x1f,
198+
}, {
199+
outputLength: kUnsignedLongMax,
200+
domainSeparation: kOctetMax,
201+
}],
202+
];
203+
204+
for (const [dictionary, base, members] of cases) {
205+
const converter = converters[dictionary];
206+
assertIdlDictionary(converter(base, opts), base);
207+
208+
for (const [member, max] of Object.entries(members)) {
209+
assert.throws(
210+
() => converter({ ...base, [member]: -1 }, opts), {
211+
name: 'TypeError',
212+
code: 'ERR_OUT_OF_RANGE',
213+
message: `${prefix}: ${member} in ${context} is outside ` +
214+
`the expected range of 0 to ${max}.`,
215+
});
216+
}
217+
}
218+
}
219+
122220
// DOMString
123221
{
124222
assert.strictEqual(converters.DOMString(1), '1');

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL