From e9324ad67882addccf3516139ca6bf7837e548a3 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 11 Nov 2020 11:47:39 +0100 Subject: [PATCH] support jwk keys to encrypt --- src/matrix/e2ee/attachment.js | 2 +- src/platform/web/dom/Crypto.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/matrix/e2ee/attachment.js b/src/matrix/e2ee/attachment.js index 02a051ed..070e7c53 100644 --- a/src/matrix/e2ee/attachment.js +++ b/src/matrix/e2ee/attachment.js @@ -62,7 +62,7 @@ export async function encryptAttachment(platform, blob) { const iv = await crypto.aes.generateIV(); const key = await crypto.aes.generateKey("jwk", 256); const buffer = await blob.readAsBuffer(); - const ciphertext = await crypto.aes.encryptCTR({key, iv, data: buffer}); + const ciphertext = await crypto.aes.encryptCTR({jwkKey: key, iv, data: buffer}); const digest = await crypto.digest("SHA-256", ciphertext); return { blob: platform.createBlob(ciphertext, blob.mimeType), diff --git a/src/platform/web/dom/Crypto.js b/src/platform/web/dom/Crypto.js index 8396176c..be3e4343 100644 --- a/src/platform/web/dom/Crypto.js +++ b/src/platform/web/dom/Crypto.js @@ -199,17 +199,19 @@ class AESCrypto { } } - async encryptCTR({key, iv, data}) { + async encryptCTR({key, jwkKey, iv, data}) { const opts = { name: "AES-CTR", counter: iv, length: 64, }; let aesKey; + const selectedKey = key || jwkKey; + const format = jwkKey ? "jwk" : "raw"; try { aesKey = await subtleCryptoResult(this._subtleCrypto.importKey( - "raw", - key, + format, + selectedKey, opts, false, ['encrypt'],