Skip to content

Commit b5af458

Browse files
authored
[WebCryptoAPI] Add tests for [EnforceRange] WebIDL attribute (#62086)
The `[EnforceRange]` attribute of the `length` parameter of `deriveBits` was added in w3c/webcrypto#555. The `[EnforceRange]` attribute of the `length` and `modulusLength` properties of the `{Aes,Hmac,Rsa}KeyGenParams` dictionaries was already there. This change adds tests for both.
1 parent db80bd2 commit b5af458

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

WebCryptoAPI/derive_bits_keys/derived_bits_length.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ function define_tests() {
1717
derivedBits = await subtle.deriveBits(testData.deriveAlg, privateKey);
1818
else
1919
derivedBits = await subtle.deriveBits(testData.deriveAlg, privateKey, testParam.length);
20-
if (testParam.expected === undefined) {
21-
assert_unreached("deriveBits should have thrown an OperationError exception.");
20+
if (testParam.expected === "TypeError" || testParam.expected === "OperationError") {
21+
assert_unreached("deriveBits should have thrown an " + testParam.expected + " exception.");
2222
}
2323
assert_array_equals(new Uint8Array(derivedBits), testParam.expected, "Derived bits do not match the expected result.");
2424
} catch (err) {
25-
if (err instanceof AssertionError || testParam.expected !== undefined) {
25+
if (err instanceof AssertionError || !(testParam.expected === "TypeError" || testParam.expected === "OperationError")) {
2626
throw err;
2727
}
2828
assert_true(privateKey !== undefined, "Key should be valid.");
29-
assert_equals(err.name, "OperationError", "deriveBits correctly threw OperationError: " + err.message);
29+
assert_equals(err.name, testParam.expected, "deriveBits correctly threw " + testParam.expected + ": " + err.message);
3030
}
3131
}, algorithm + " derivation with " + testParam.length + " as 'length' parameter");
3232
});
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1+
var enforceRangeTestCases = [
2+
// These cases should throw an error in all algorithms due to [EnforceRange]
3+
{length: NaN, expected: "TypeError"},
4+
{length: Infinity, expected: "TypeError"},
5+
{length: -8, expected: "TypeError"},
6+
{length: 2**32 + 8, expected: "TypeError"},
7+
];
18
var testCases = {
29
"HKDF": [
310
{length: 256, expected: algorithms["HKDF"].derivation},
411
{length: 384, expected: algorithms["HKDF"].derivation384},
5-
{length: 230, expected: undefined}, // should throw an exception, not multiple of 8
12+
{length: 230, expected: "OperationError"}, // should throw an exception, not multiple of 8
613
{length: 0, expected: emptyArray},
7-
{length: null, expected: undefined }, // should throw an exception
8-
{length: undefined, expected: undefined }, // should throw an exception
9-
{length: "omitted", expected: undefined }, // default value is null, so should throw
14+
{length: null, expected: "OperationError"}, // should throw an exception
15+
{length: undefined, expected: "OperationError"}, // should throw an exception
16+
{length: "omitted", expected: "OperationError"}, // default value is null, so should throw
17+
...enforceRangeTestCases,
1018
],
1119
"PBKDF2": [
1220
{length: 256, expected: algorithms["PBKDF2"].derivation},
1321
{length: 384, expected: algorithms["PBKDF2"].derivation384},
14-
{length: 230, expected: undefined}, // should throw an exception, not multiple of 8
22+
{length: 230, expected: "OperationError"}, // should throw an exception, not multiple of 8
1523
{length: 0, expected: emptyArray},
16-
{length: null, expected: undefined }, // should throw an exception
17-
{length: undefined, expected: undefined }, // should throw an exception
18-
{length: "omitted", expected: undefined }, // default value is null, so should throw
24+
{length: null, expected: "OperationError"}, // should throw an exception
25+
{length: undefined, expected: "OperationError"}, // should throw an exception
26+
{length: "omitted", expected: "OperationError"}, // default value is null, so should throw
27+
...enforceRangeTestCases,
1928
],
2029
"ECDH": [
2130
{length: 256, expected: algorithms["ECDH"].derivation},
22-
{length: 384, expected: undefined}, // should throw an exception, bigger than the output size
31+
{length: 384, expected: "OperationError"}, // should throw an exception, bigger than the output size
2332
{length: 230, expected: algorithms["ECDH"].derivation230},
2433
{length: 0, expected: emptyArray},
2534
{length: null, expected: algorithms["ECDH"].derivation},
2635
{length: undefined, expected: algorithms["ECDH"].derivation},
27-
{length: "omitted", expected: algorithms["ECDH"].derivation }, // default value is null
36+
{length: "omitted", expected: algorithms["ECDH"].derivation}, // default value is null
37+
...enforceRangeTestCases,
2838
],
2939
"X25519": [
3040
{length: 256, expected: algorithms["X25519"].derivation},
31-
{length: 384, expected: undefined}, // should throw an exception, bigger than the output size
41+
{length: 384, expected: "OperationError"}, // should throw an exception, bigger than the output size
3242
{length: 230, expected: algorithms["X25519"].derivation230},
3343
{length: 0, expected: emptyArray},
3444
{length: null, expected: algorithms["X25519"].derivation},
3545
{length: undefined, expected: algorithms["X25519"].derivation},
36-
{length: "omitted", expected: algorithms["X25519"].derivation }, // default value is null
46+
{length: "omitted", expected: algorithms["X25519"].derivation}, // default value is null
47+
...enforceRangeTestCases,
3748
],
3849
}

WebCryptoAPI/generateKey/failures.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,19 @@ function run_test(algorithmNames) {
101101

102102
if (algorithmName.toUpperCase().substring(0, 3) === "AES") {
103103
// Specifier properties are name and length
104-
[64, 127, 129, 255, 257, 512].forEach(function(length) {
104+
[64, 127, 129, 255, 257, 512, 128 + 2**32, 128 - 2**32].forEach(function(length) {
105105
results.push({name: algorithmName, length: length});
106106
});
107+
} else if (algorithmName.toUpperCase() === "HMAC") {
108+
[128 + 2**32, 128 - 2**32].forEach(function(length) {
109+
results.push({name: algorithmName, hash: "SHA-256", length: length});
110+
});
107111
} else if (algorithmName.toUpperCase().substring(0, 3) === "RSA") {
108112
[new Uint8Array([1]), new Uint8Array([1,0,0])].forEach(function(publicExponent) {
109113
results.push({name: algorithmName, hash: "SHA-256", modulusLength: 1024, publicExponent: publicExponent});
110114
});
115+
results.push({name: algorithmName, hash: "SHA-256", modulusLength: 1024 + 2 ** 32, publicExponent: new Uint8Array([1,0,1])});
116+
results.push({name: algorithmName, hash: "SHA-256", modulusLength: 1024 - 2 ** 32, publicExponent: new Uint8Array([1,0,1])});
111117
} else if (algorithmName.toUpperCase().substring(0, 2) === "EC") {
112118
["P-512", "Curve25519"].forEach(function(curveName) {
113119
results.push({name: algorithmName, namedCurve: curveName});
@@ -177,6 +183,9 @@ function run_test(algorithmNames) {
177183
[false, true].forEach(function(extractable) {
178184
if (name.substring(0,2) === "EC") {
179185
testError(algorithm, extractable, usages, "NotSupportedError", "Bad algorithm property");
186+
} else if (name.substring(0,3) === "RSA" && (algorithm.modulusLength < 0 || algorithm.modulusLength > 2**32) ||
187+
(name.substring(0,3) === "AES" || name === "HMAC") && (algorithm.length < 0 || algorithm.length > 2**32)) {
188+
testError(algorithm, extractable, usages, "TypeError", "Bad algorithm property");
180189
} else {
181190
testError(algorithm, extractable, usages, "OperationError", "Bad algorithm property");
182191
}

0 commit comments

Comments
 (0)