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) --- ext/crypto/00_crypto.js | 59 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) (limited to 'ext/crypto/00_crypto.js') diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js index 880457416..1f49d1849 100644 --- a/ext/crypto/00_crypto.js +++ b/ext/crypto/00_crypto.js @@ -145,12 +145,10 @@ "PBKDF2": null, }, "wrapKey": { - // TODO(@littledivy): Enable this once implemented. - // "AES-KW": "AesKeyWrapParams", + "AES-KW": null, }, "unwrapKey": { - // TODO(@littledivy): Enable this once implemented. - // "AES-KW": "AesKeyWrapParams", + "AES-KW": null, }, }; @@ -1271,14 +1269,30 @@ if ( supportedAlgorithms["wrapKey"][normalizedAlgorithm.name] !== undefined ) { - // TODO(@littledivy): Implement this for AES-KW. - throw new DOMException( - "Not implemented", - "NotSupportedError", - ); + const handle = wrappingKey[_handle]; + const keyData = WeakMapPrototypeGet(KEY_STORE, handle); + + switch (normalizedAlgorithm.name) { + case "AES-KW": { + const cipherText = await core.opSync("op_crypto_wrap_key", { + key: keyData, + algorithm: normalizedAlgorithm.name, + }, bytes); + + // 4. + return cipherText.buffer; + } + default: { + throw new DOMException( + "Not implemented", + "NotSupportedError", + ); + } + } } else if ( supportedAlgorithms["encrypt"][normalizedAlgorithm.name] !== undefined ) { + // must construct a new key, since keyUsages is ["wrapKey"] and not ["encrypt"] return await encrypt( normalizedAlgorithm, constructKey( @@ -1391,14 +1405,31 @@ if ( supportedAlgorithms["unwrapKey"][normalizedAlgorithm.name] !== undefined ) { - // TODO(@littledivy): Implement this for AES-KW. - throw new DOMException( - "Not implemented", - "NotSupportedError", - ); + const handle = unwrappingKey[_handle]; + const keyData = WeakMapPrototypeGet(KEY_STORE, handle); + + switch (normalizedAlgorithm.name) { + case "AES-KW": { + const plainText = await core.opSync("op_crypto_unwrap_key", { + key: keyData, + algorithm: normalizedAlgorithm.name, + }, wrappedKey); + + // 4. + key = plainText.buffer; + break; + } + default: { + throw new DOMException( + "Not implemented", + "NotSupportedError", + ); + } + } } else if ( supportedAlgorithms["decrypt"][normalizedAlgorithm.name] !== undefined ) { + // must construct a new key, since keyUsages is ["unwrapKey"] and not ["decrypt"] key = await this.decrypt( normalizedAlgorithm, constructKey( -- cgit v1.2.3