diff options
author | Sean Michael Wykes <8363933+SeanWykes@users.noreply.github.com> | 2022-01-11 01:44:47 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-11 10:14:47 +0530 |
commit | 91f6c5fc7e6f66f0e963c5cfbec281da4bcfc496 (patch) | |
tree | 299ec26d38c6be74f5980c4b71877c3e974551e5 /cli/tests | |
parent | 605b8db8f61fc4c0c71d11cde873af18d87c49bf (diff) |
feat(ext/crypto): implement AES-KW for wrapKey/unwrapKey (#13286)
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/webcrypto_test.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 926ed6b6c..3caa927ec 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -1561,3 +1561,61 @@ Deno.test(async function testSecretJwkBase64Url() { }, ); }); + +Deno.test(async function testAESWrapKey() { + const key = await crypto.subtle.generateKey( + { + name: "AES-KW", + length: 128, + }, + true, + ["wrapKey", "unwrapKey"], + ); + + const hmacKey = await crypto.subtle.generateKey( + { + name: "HMAC", + hash: "SHA-256", + length: 128, + }, + true, + ["sign"], + ); + + //round-trip + // wrap-unwrap-export compare + const wrappedKey = await crypto.subtle.wrapKey( + "raw", + hmacKey, + key, + { + name: "AES-KW", + }, + ); + + assert(wrappedKey instanceof ArrayBuffer); + assertEquals(wrappedKey.byteLength, 16 + 8); // 8 = 'auth tag' + + const unwrappedKey = await crypto.subtle.unwrapKey( + "raw", + wrappedKey, + key, + { + name: "AES-KW", + }, + { + name: "HMAC", + hash: "SHA-256", + }, + true, + ["sign"], + ); + + assert(unwrappedKey instanceof CryptoKey); + assertEquals((unwrappedKey.algorithm as HmacKeyAlgorithm).length, 128); + + const hmacKeyBytes = await crypto.subtle.exportKey("raw", hmacKey); + const unwrappedKeyBytes = await crypto.subtle.exportKey("raw", unwrappedKey); + + assertEquals(new Uint8Array(hmacKeyBytes), new Uint8Array(unwrappedKeyBytes)); +}); |