feat(ext/crypto): implement unwrapKey (#12539)

This commit is contained in:
Divy Srivastava 2021-12-05 09:25:11 +05:30 committed by GitHub
parent c59f90d01f
commit d31378726e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 229 additions and 6 deletions

View file

@ -691,6 +691,60 @@ Deno.test(async function testAesKeyGen() {
assertEquals(algorithm.length, 256);
});
Deno.test(async function testUnwrapKey() {
const subtle = crypto.subtle;
const AES_KEY: AesKeyAlgorithm & AesCbcParams = {
name: "AES-CBC",
length: 128,
iv: new Uint8Array(16),
};
const RSA_KEY: RsaHashedKeyGenParams & RsaOaepParams = {
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-1",
};
const aesKey = await subtle.generateKey(AES_KEY, true, [
"encrypt",
"decrypt",
]);
const rsaKeyPair = await subtle.generateKey(
{
name: "RSA-OAEP",
hash: "SHA-1",
publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 2048,
},
false,
["wrapKey", "encrypt", "unwrapKey", "decrypt"],
);
const enc = await subtle.wrapKey(
"raw",
aesKey,
rsaKeyPair.publicKey,
RSA_KEY,
);
const unwrappedKey = await subtle.unwrapKey(
"raw",
enc,
rsaKeyPair.privateKey,
RSA_KEY,
AES_KEY,
false,
["encrypt", "decrypt"],
);
assert(unwrappedKey instanceof CryptoKey);
assertEquals(unwrappedKey.type, "secret");
assertEquals(unwrappedKey.extractable, false);
assertEquals(unwrappedKey.usages, ["encrypt", "decrypt"]);
});
Deno.test(async function testDecryptWithInvalidIntializationVector() {
const data = new Uint8Array([42, 42, 42, 42]);
const key = await crypto.subtle.generateKey(