From 91f6c5fc7e6f66f0e963c5cfbec281da4bcfc496 Mon Sep 17 00:00:00 2001 From: Sean Michael Wykes <8363933+SeanWykes@users.noreply.github.com> Date: Tue, 11 Jan 2022 01:44:47 -0300 Subject: feat(ext/crypto): implement AES-KW for wrapKey/unwrapKey (#13286) --- cli/tests/unit/webcrypto_test.ts | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'cli') 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)); +}); -- cgit v1.2.3