diff --git a/src/pages/generate/index.tsx b/src/pages/generate/index.tsx index 198355155..e68656b89 100644 --- a/src/pages/generate/index.tsx +++ b/src/pages/generate/index.tsx @@ -12,7 +12,7 @@ import { getData, LS_KEYS, setData } from 'utils/storage/localStorage'; import { useRouter } from 'next/router'; import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import * as Comlink from 'comlink'; -import { keyEncryptionResult } from 'services/uploadService'; +import { B64EncryptionResult } from 'services/uploadService'; const CryptoWorker: any = typeof window !== 'undefined' && @@ -63,12 +63,12 @@ export default function Generate() { kekSalt ); const kekHash: string = await cryptoWorker.hash(kek); - const encryptedKeyAttributes: keyEncryptionResult = await cryptoWorker.encryptToB64( + const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( key, kek ); const keyPair = await cryptoWorker.generateKeyPair(); - const encryptedKeyPairAttributes: keyEncryptionResult = await cryptoWorker.encryptToB64( + const encryptedKeyPairAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64( keyPair.privateKey, key ); diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 6b608605d..57ac5b034 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -5,7 +5,7 @@ import localForage from 'localforage'; import HTTPService from './HTTPService'; import * as Comlink from 'comlink'; -import { keyEncryptionResult } from './uploadService'; +import { B64EncryptionResult } from './uploadService'; import { getActualKey, getToken } from 'utils/common/key'; const CryptoWorker: any = @@ -77,7 +77,7 @@ const getCollectionSecrets = async ( } collection.name = collection.name || - (await worker.decryptString( + (await worker.decryptToUTF8( collection.encryptedName, collection.nameDecryptionNonce, decryptedKey @@ -208,14 +208,14 @@ export const AddCollection = async ( const { encryptedData: encryptedKey, nonce: keyDecryptionNonce, - }: keyEncryptionResult = await worker.encryptToB64( + }: B64EncryptionResult = await worker.encryptToB64( collectionKey, encryptionKey ); const { encryptedData: encryptedName, nonce: nameDecryptionNonce, - }: keyEncryptionResult = await worker.encryptToB64( + }: B64EncryptionResult = await worker.encryptUTF8( collectionName, collectionKey ); @@ -290,7 +290,7 @@ const addToCollection = async (collection: collection, files: file[]) => { await Promise.all( files.map(async (file) => { file.collectionID = collection.id; - const newEncryptedKey: keyEncryptionResult = await worker.encryptToB64( + const newEncryptedKey: B64EncryptionResult = await worker.encryptToB64( file.key, collection.key ); diff --git a/src/services/uploadService.ts b/src/services/uploadService.ts index f3ff5582f..7ae196659 100644 --- a/src/services/uploadService.ts +++ b/src/services/uploadService.ts @@ -19,7 +19,7 @@ interface encryptionResult { file: fileAttribute; key: string; } -export interface keyEncryptionResult { +export interface B64EncryptionResult { encryptedData: string; key: string; nonce: string; @@ -39,7 +39,7 @@ interface FileinMemory { interface encryptedFile { filedata: fileAttribute; thumbnail: fileAttribute; - fileKey: keyEncryptionResult; + fileKey: B64EncryptionResult; } interface objectKey { @@ -63,7 +63,7 @@ interface uploadFile extends objectKeys { interface UploadFileWithoutMetaData { tempUploadFile: uploadFile; - encryptedFileKey: keyEncryptionResult; + encryptedFileKey: B64EncryptionResult; fileName: string; } @@ -270,7 +270,7 @@ class UploadService { fileKey ); - const encryptedKey: keyEncryptionResult = await worker.encryptToB64( + const encryptedKey: B64EncryptionResult = await worker.encryptB64( fileKey, encryptionKey ); @@ -289,7 +289,7 @@ class UploadService { private async encryptMetadata( worker: any, fileName: string, - encryptedFileKey: keyEncryptionResult + encryptedFileKey: B64EncryptionResult ) { const metaData = this.metadataMap.get(fileName); const fileKey = await worker.decryptB64( @@ -340,7 +340,7 @@ class UploadService { private getuploadFile( collection: collection, - encryptedKey: keyEncryptionResult, + encryptedKey: B64EncryptionResult, objectKeys: objectKeys ): uploadFile { const uploadFile: uploadFile = { diff --git a/src/utils/crypto/libsodium.ts b/src/utils/crypto/libsodium.ts index 25ef8aa9a..ae875815c 100644 --- a/src/utils/crypto/libsodium.ts +++ b/src/utils/crypto/libsodium.ts @@ -142,6 +142,10 @@ export async function encryptToB64(data: string, key?: string) { nonce: await toB64(encrypted.nonce), }; } +export async function encryptUTF8(data: string, key?: string) { + const b64Data = await toB64(await fromString(data)); + return await encryptToB64(b64Data, key); +} export async function decryptB64(data: string, nonce: string, key: string) { await sodium.ready; @@ -154,7 +158,7 @@ export async function decryptB64(data: string, nonce: string, key: string) { return await toB64(decrypted); } -export async function decryptString(data: string, nonce: string, key: string) { +export async function decryptToUTF8(data: string, nonce: string, key: string) { await sodium.ready; const decrypted = await decrypt( await fromB64(data), @@ -250,14 +254,7 @@ export async function boxSealOpen( export async function fromB64(input: string) { await sodium.ready; - let result; - try { - result = sodium.from_base64(input, sodium.base64_variants.ORIGINAL); - } catch (e) { - result = await fromB64(await toB64(await fromString(input))); - } finally { - return result; - } + return sodium.from_base64(input, sodium.base64_variants.ORIGINAL); } export async function toB64(input: Uint8Array) { diff --git a/src/worker/crypto.worker.js b/src/worker/crypto.worker.js index 403628b1b..5518cac94 100644 --- a/src/worker/crypto.worker.js +++ b/src/worker/crypto.worker.js @@ -69,7 +69,7 @@ export class Crypto { return libsodium.decryptB64(data, nonce, key); } - async decryptString(data, nonce, key) { + async decryptToUTF8(data, nonce, key) { return libsodium.decryptString(data, nonce, key); } @@ -77,6 +77,10 @@ export class Crypto { return libsodium.encryptToB64(data, key); } + async encryptUTF8(data, key) { + return libsodium.encryptUTF8(data, key); + } + async generateMasterKey() { return libsodium.generateMasterKey(); }