diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2021-09-12 02:19:53 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-11 16:49:53 -0400 |
commit | 40c63d1255642b8d70d7b5ce5b85a50f6af8a00d (patch) | |
tree | abdfe6e1df461b3d8106fc3c75a7c879cd9381a2 /cli/tests/unit/webcrypto_test.ts | |
parent | d236f432b86de55c6006778b0c68fe60b6419069 (diff) |
feat(ext/crypto): verify ECDSA signatures (#11739)
Diffstat (limited to 'cli/tests/unit/webcrypto_test.ts')
-rw-r--r-- | cli/tests/unit/webcrypto_test.ts | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 475efde6c..a5de9fa36 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -189,7 +189,7 @@ unitTest(async function testGenerateHMACKey() { assert(key.usages.includes("sign")); }); -unitTest(async function testSignECDSA() { +unitTest(async function testECDSASignVerify() { const key = await window.crypto.subtle.generateKey( { name: "ECDSA", @@ -208,6 +208,56 @@ unitTest(async function testSignECDSA() { ); assert(signature); + assert(signature instanceof ArrayBuffer); + + const verified = await window.crypto.subtle.verify( + { hash: { name: "SHA-384" }, name: "ECDSA" }, + key.publicKey, + signature, + encoded, + ); + assert(verified); +}); + +// Tests the "bad paths" as a temporary replacement for sign_verify/ecdsa WPT. +unitTest(async function testECDSASignVerifyFail() { + const key = await window.crypto.subtle.generateKey( + { + name: "ECDSA", + namedCurve: "P-384", + }, + true, + ["sign", "verify"], + ); + + const encoded = new Uint8Array([1]); + // Signing with a public key (InvalidAccessError) + await assertThrowsAsync(async () => { + await window.crypto.subtle.sign( + { name: "ECDSA", hash: "SHA-384" }, + key.publicKey, + new Uint8Array([1]), + ); + throw new TypeError("unreachable"); + }, DOMException); + + // Do a valid sign for later verifying. + const signature = await window.crypto.subtle.sign( + { name: "ECDSA", hash: "SHA-384" }, + key.privateKey, + encoded, + ); + + // Verifying with a private key (InvalidAccessError) + await assertThrowsAsync(async () => { + await window.crypto.subtle.verify( + { hash: { name: "SHA-384" }, name: "ECDSA" }, + key.privateKey, + signature, + encoded, + ); + throw new TypeError("unreachable"); + }, DOMException); }); // https://github.com/denoland/deno/issues/11313 |