From 3b2cb8e7113b19344209eddc8bc1bd447fcec4ea Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 11 Oct 2021 20:07:51 +0530 Subject: feat(ext/crypto): implement AES-CBC encryption & decryption (#12123) * initial stuff * stuff * merge stuff * cleanup * fmt * length * update lockfile * decrypt * fixy * clippy hello? * hmm * fixs * fix lint * add AesCbcParams * fixes * fixy * lockfile fixy * fix dumb assertions * re run CI * rerun CI * rerun CI --- cli/tests/unit/webcrypto_test.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'cli') diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 80275b002..420fa2a7f 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -513,6 +513,40 @@ unitTest(async function testHkdfDeriveBits() { assertEquals(result.byteLength, 128 / 8); }); +unitTest(async function testAesCbcEncryptDecrypt() { + const key = await crypto.subtle.generateKey( + { name: "AES-CBC", length: 128 }, + true, + ["encrypt", "decrypt"], + ); + + const iv = await crypto.getRandomValues(new Uint8Array(16)); + const encrypted = await crypto.subtle.encrypt( + { + name: "AES-CBC", + iv, + }, + key as CryptoKey, + new Uint8Array([1, 2, 3, 4, 5, 6]), + ); + + assert(encrypted instanceof ArrayBuffer); + assertEquals(encrypted.byteLength, 16); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-CBC", + iv, + }, + key as CryptoKey, + encrypted, + ); + + assert(decrypted instanceof ArrayBuffer); + assertEquals(decrypted.byteLength, 6); + assertEquals(new Uint8Array(decrypted), new Uint8Array([1, 2, 3, 4, 5, 6])); +}); + // TODO(@littledivy): Enable WPT when we have importKey support unitTest(async function testECDH() { const namedCurve = "P-256"; -- cgit v1.2.3