diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index b7277b191..c9b6cd564 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -58,7 +58,7 @@ export default function Generate() { const kekHash = await cryptoWorker.hash(kek); const encryptedKeyAttributes = await cryptoWorker.encrypt(key, kek); const keyPair = await cryptoWorker.generateKeyPair(); - const encryptedKeyPairAttributes = await cryptoWorker.encrypt(keyPair.privateKey, kek); + const encryptedKeyPairAttributes = await cryptoWorker.encrypt(keyPair.privateKey, key); const keyAttributes = { kekSalt: await cryptoWorker.toB64(kekSalt), kekHash: kekHash, diff --git a/src/services/fileService.ts b/src/services/fileService.ts index bb6b58c47..a7f262b0a 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -54,8 +54,15 @@ const getCollectionKey = async (collection: collection, key: Uint8Array) => { await worker.fromB64(collection.keyDecryptionNonce), key); } else { - // TODO - decryptedKey = null; + const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES); + const secretKey = await worker.decrypt( + await worker.fromB64(keyAttributes.encryptedSecretKey), + await worker.fromB64(keyAttributes.secretKeyDecryptionNonce), + key); + decryptedKey = await worker.boxSealOpen( + await worker.fromB64(collection.encryptedKey), + await worker.fromB64(keyAttributes.publicKey), + secretKey); } return { ...collection, @@ -81,9 +88,6 @@ export const getFiles = async (sinceTime: string, token: string, limit: string, var files: Array = []; for (const index in collections) { const collection = collections[index]; - if (collection.key == null) { - continue; - } const resp = await HTTPService.get(`${ENDPOINT}/collections/diff`, { 'collectionID': collection.id.toString(), sinceTime, token, limit, }); diff --git a/src/utils/crypto/libsodium.ts b/src/utils/crypto/libsodium.ts index 62cc9f6e9..69f8ae1c4 100644 --- a/src/utils/crypto/libsodium.ts +++ b/src/utils/crypto/libsodium.ts @@ -92,6 +92,11 @@ export async function generateKeyPair() { return sodium.crypto_box_keypair(); } +export async function boxSealOpen(input: Uint8Array, publicKey: Uint8Array, secretKey: Uint8Array) { + await sodium.ready; + return sodium.crypto_box_seal_open(input, publicKey, secretKey); +} + export async function fromB64(input: string) { await sodium.ready; return sodium.from_base64(input, sodium.base64_variants.ORIGINAL); diff --git a/src/worker/crypto.worker.js b/src/worker/crypto.worker.js index 153f12c43..c01aaa5c2 100644 --- a/src/worker/crypto.worker.js +++ b/src/worker/crypto.worker.js @@ -57,6 +57,10 @@ export class Crypto { return libsodium.generateKeyPair(); } + async boxSealOpen(input, publicKey, secretKey) { + return libsodium.boxSealOpen(input, publicKey, secretKey) + } + async fromString(string) { return libsodium.fromString(string); }