Skip to content

Commit 4c2fd05

Browse files
twisspanva
andauthored
[WebCryptoAPI] ECDH and ECDSA: test that generated key length is consistent (#61799)
Co-authored-by: Filip Skokan <[email protected]>
1 parent 8011b28 commit 4c2fd05

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎WebCryptoAPI/generateKey/successes.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ function run_test(algorithmNames, slowTest) {
110110
assert_unreached("exportKey threw an unexpected error: " + err.toString());
111111
})
112112
}, testTag + ": generateKey" + parameterString(algorithm, extractable, usages));
113+
114+
// Special case for ECDH and ECDSA: check that the generated key length is consistent.
115+
// Particularly for P-521, there is a high risk of the generated key being one byte short
116+
// if the implementation isn't careful.
117+
if (algorithm.namedCurve && extractable) {
118+
promise_test(async function(test) {
119+
// We run about 20 variants of this test, times 10 key generations below,
120+
// so this should have a decent chance of catching issues.
121+
await Promise.all(Array.from({ length: 10 }).map(async () => {
122+
const { privateKey, publicKey } = await subtle.generateKey(algorithm, extractable, usages);
123+
const [jwkPub, jwkPriv] = await Promise.all([
124+
subtle.exportKey('jwk', publicKey),
125+
subtle.exportKey('jwk', privateKey),
126+
]);
127+
const expectedLength = Math.ceil(Math.ceil(parseInt(algorithm.namedCurve.substring(2)) / 8) * 4/3);
128+
assert_equals(jwkPub.x.length, expectedLength, "Public key value x has correct length");
129+
assert_equals(jwkPub.y.length, expectedLength, "Public key value y has correct length");
130+
assert_equals(jwkPriv.d.length, expectedLength, "Private key value d has correct length");
131+
}));
132+
}, testTag + ": generateKey" + parameterString(algorithm, extractable, usages) + " produces consistent length key");
133+
}
113134
}
114135

115136
// Test all valid sets of parameters for successful

0 commit comments

Comments
 (0)