diff --git a/src/email-crypto/converters.ts b/src/email-crypto/converters.ts index 3286408..2815ba8 100644 --- a/src/email-crypto/converters.ts +++ b/src/email-crypto/converters.ts @@ -1,5 +1,5 @@ -import { UTF8ToUint8, uint8ToUTF8 } from '../utils'; -import { EmailBody, User, Email } from '../types'; +import { UTF8ToUint8 } from '../utils'; +import { User, Email } from '../types'; import { concatBytes } from '@noble/hashes/utils.js'; export function userToBytes(user: User): Uint8Array { @@ -20,37 +20,6 @@ export function recipientsToBytes(recipients: User[]): Uint8Array { } } -/** - * Converts an EmailBody type into a Uint8Array array. - * - * @param body - The email body. - * @returns The Uint8Array array representation of the EmailBody type. - */ -export function emailBodyToBinary(body: EmailBody): Uint8Array { - try { - const json = JSON.stringify(body); - return UTF8ToUint8(json); - } catch (error) { - throw new Error('Failed to convert EmailBody to Uint8Array', { cause: error }); - } -} - -/** - * Converts an Uint8Array array into EmailBody type. - * - * @param array - The Uint8Array array. - * @returns The EmailBody type representation of the Uint8Array. - */ -export function binaryToEmailBody(array: Uint8Array): EmailBody { - try { - const json = uint8ToUTF8(array); - const email: EmailBody = JSON.parse(json); - return email; - } catch (error) { - throw new Error('Failed to convert Uint8Array to EmailBody', { cause: error }); - } -} - /** * Converts an Email type into a Uint8Array array. * diff --git a/src/email-crypto/core.ts b/src/email-crypto/core.ts index c0d9b74..55be416 100644 --- a/src/email-crypto/core.ts +++ b/src/email-crypto/core.ts @@ -1,6 +1,5 @@ -import { HybridEncKey, PwdProtectedKey, PublicKeys, PrivateKeys, EmailBody } from '../types'; +import { HybridEncKey, PwdProtectedKey, PublicKeys, PrivateKeys, EmailBody, EmailBodyEncrypted } from '../types'; import { genSymmetricCryptoKey, encryptSymmetrically, decryptSymmetrically } from '../symmetric-crypto'; -import { emailBodyToBinary, binaryToEmailBody } from './converters'; import { encapsulateKyber, decapsulateKyber } from '../post-quantum-crypto'; import { deriveWrappingKey, wrapKey, unwrapKey, importWrappingKey } from '../key-wrapper'; import { deriveSecretKey } from '../asymmetric-crypto'; @@ -19,8 +18,11 @@ export async function encryptEmailContentSymmetrically( email: EmailBody, aux: Uint8Array, emailID: string, -): Promise<{ enc: Uint8Array; encryptionKey: CryptoKey }> { +): Promise<{ enc: EmailBodyEncrypted; encryptionKey: CryptoKey }> { try { + if (!email.text) { + throw new Error('Invalid input'); + } const encryptionKey = await genSymmetricCryptoKey(); const enc = await encryptEmailContentSymmetricallyWithKey(email, encryptionKey, aux, emailID); return { enc, encryptionKey }; @@ -43,13 +45,17 @@ export async function encryptEmailContentAndSubjectSymmetrically( subject: string, aux: Uint8Array, emailID: string, -): Promise<{ enc: Uint8Array; subjectEnc: Uint8Array; encryptionKey: CryptoKey }> { +): Promise<{ enc: EmailBodyEncrypted; encSubject: string; encryptionKey: CryptoKey }> { try { + if (!subject || !email.text) { + throw new Error('Invalid input'); + } const encryptionKey = await genSymmetricCryptoKey(); const enc = await encryptEmailContentSymmetricallyWithKey(email, encryptionKey, aux, emailID); const subjectBuff = UTF8ToUint8(subject); const subjectEnc = await encryptSymmetrically(encryptionKey, subjectBuff, aux); - return { enc, encryptionKey, subjectEnc }; + const encSubject = uint8ArrayToBase64(subjectEnc); + return { enc, encSubject, encryptionKey }; } catch (error) { throw new Error('Failed to symmetrically encrypt email and subject', { cause: error }); } @@ -63,16 +69,17 @@ export async function encryptEmailContentAndSubjectSymmetrically( * @returns The decrypted email */ export async function decryptEmailAndSubjectSymmetrically( - emailCiphertext: Uint8Array, - encSubject: Uint8Array, encryptionKey: CryptoKey, aux: Uint8Array, + encSubject: string, + enc: EmailBodyEncrypted, ): Promise<{ body: EmailBody; subject: string }> { try { - const binaryEmail = await decryptSymmetrically(encryptionKey, emailCiphertext, aux); - const subject = await decryptSymmetrically(encryptionKey, encSubject, aux); - const body = binaryToEmailBody(binaryEmail); - return { body, subject: uint8ToUTF8(subject) }; + const array = base64ToUint8Array(encSubject); + const subjectArray = await decryptSymmetrically(encryptionKey, array, aux); + const body = await decryptEmailSymmetrically(encryptionKey, aux, enc); + const subject = uint8ToUTF8(subjectArray); + return { body, subject }; } catch (error) { throw new Error('Failed to symmetrically decrypt email and subject', { cause: error }); } @@ -89,17 +96,61 @@ export async function encryptEmailContentSymmetricallyWithKey( encryptionKey: CryptoKey, aux: Uint8Array, emailID: string, -): Promise { +): Promise { try { const freeField = uuidToBytes(emailID); - const binaryEmail = emailBodyToBinary(emailBody); - const ciphertext = await encryptSymmetrically(encryptionKey, binaryEmail, aux, freeField); - return ciphertext; + const text = UTF8ToUint8(emailBody.text); + const encryptedText = await encryptSymmetrically(encryptionKey, text, aux, freeField); + const encText = uint8ArrayToBase64(encryptedText); + const result: EmailBodyEncrypted = { encText }; + + if (emailBody.attachments) { + const encryptedAttachements = await encryptEmailAttachements(emailBody.attachments, encryptionKey, aux, emailID); + result.encAttachments = encryptedAttachements?.map(uint8ArrayToBase64); + } + return result; } catch (error) { throw new Error('Failed to symmetrically encrypt email with the given key', { cause: error }); } } +async function encryptEmailAttachements( + attachments: string[], + encryptionKey: CryptoKey, + aux: Uint8Array, + emailID: string, +): Promise { + try { + const freeField = uuidToBytes(emailID); + const encryptedAttachments = await Promise.all( + attachments.map((attachment) => { + const binaryAttachment = UTF8ToUint8(attachment); + return encryptSymmetrically(encryptionKey, binaryAttachment, aux, freeField); + }), + ); + return encryptedAttachments; + } catch (error) { + throw new Error('Failed to symmetrically encrypt email attachements', { cause: error }); + } +} + +async function decryptEmailAttachements( + encryptedAttachments: Uint8Array[], + encryptionKey: CryptoKey, + aux: Uint8Array, +): Promise { + try { + const decryptedAttachments = await Promise.all( + encryptedAttachments.map((attachment) => { + return decryptSymmetrically(encryptionKey, attachment, aux); + }), + ); + return decryptedAttachments; + } catch (error) { + throw new Error('Failed to symmetrically decrypt email attachements', { cause: error }); + } +} + /** * Decrypts symmetrically encrypted email. * @@ -108,14 +159,22 @@ export async function encryptEmailContentSymmetricallyWithKey( * @returns The decrypted email */ export async function decryptEmailSymmetrically( - emailCiphertext: Uint8Array, encryptionKey: CryptoKey, aux: Uint8Array, + enc: EmailBodyEncrypted, ): Promise { try { - const binaryEmail = await decryptSymmetrically(encryptionKey, emailCiphertext, aux); - const body = binaryToEmailBody(binaryEmail); - return body; + const cipher = base64ToUint8Array(enc.encText); + const textArray = await decryptSymmetrically(encryptionKey, cipher, aux); + const text = uint8ToUTF8(textArray); + const result: EmailBody = { text }; + + if (enc.encAttachments) { + const encAttachements = enc.encAttachments?.map(base64ToUint8Array); + const attachmentsArray = await decryptEmailAttachements(encAttachements, encryptionKey, aux); + result.attachments = attachmentsArray?.map((att) => uint8ToUTF8(att)); + } + return result; } catch (error) { throw new Error('Failed to symmetrically decrypt email', { cause: error }); } diff --git a/src/email-crypto/hybridEncyptedEmail.ts b/src/email-crypto/hybridEncyptedEmail.ts index 565156d..e30b435 100644 --- a/src/email-crypto/hybridEncyptedEmail.ts +++ b/src/email-crypto/hybridEncyptedEmail.ts @@ -1,4 +1,3 @@ -import { base64ToUint8Array, uint8ArrayToBase64 } from '../utils'; import { PublicKeys, PrivateKeys, HybridEncryptedEmail, Email, UserWithPublicKeys } from '../types'; import { encryptEmailContentSymmetrically, @@ -25,8 +24,7 @@ export async function encryptEmailHybrid( const aux = getAux(email.params); const { enc, encryptionKey } = await encryptEmailContentSymmetrically(email.body, aux, email.id); const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); - const encryptedText = uint8ArrayToBase64(enc); - return { enc: encryptedText, encryptedKey, recipientEmail: recipient.email, params: email.params, id: email.id }; + return { enc, encryptedKey, recipientEmail: recipient.email, params: email.params, id: email.id }; } catch (error) { throw new Error('Failed to encrypt email with hybrid encryption', { cause: error }); } @@ -48,13 +46,12 @@ export async function encryptEmailHybridForMultipleRecipients( try { const aux = getAux(email.params); const { enc, encryptionKey } = await encryptEmailContentSymmetrically(email.body, aux, email.id); - const encryptedText = uint8ArrayToBase64(enc); const encryptedEmails: HybridEncryptedEmail[] = []; for (const recipient of recipients) { const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); encryptedEmails.push({ - enc: encryptedText, + enc, encryptedKey, recipientEmail: recipient.email, params: email.params, @@ -83,8 +80,7 @@ export async function decryptEmailHybrid( try { const aux = getAux(encryptedEmail.params); const encryptionKey = await decryptKeysHybrid(encryptedEmail.encryptedKey, senderPublicKeys, recipientPrivateKeys); - const enc = base64ToUint8Array(encryptedEmail.enc); - const body = await decryptEmailSymmetrically(enc, encryptionKey, aux); + const body = await decryptEmailSymmetrically(encryptionKey, aux, encryptedEmail.enc); return { body, params: encryptedEmail.params, id: encryptedEmail.id }; } catch (error) { throw new Error('Failed to decrypt email with hybrid encryption', { cause: error }); diff --git a/src/email-crypto/hybridEncyptedEmailAndSubject.ts b/src/email-crypto/hybridEncyptedEmailAndSubject.ts index 5e971e2..598f14e 100644 --- a/src/email-crypto/hybridEncyptedEmailAndSubject.ts +++ b/src/email-crypto/hybridEncyptedEmailAndSubject.ts @@ -1,4 +1,3 @@ -import { base64ToUint8Array, uint8ArrayToBase64 } from '../utils'; import { PublicKeys, PrivateKeys, HybridEncryptedEmail, Email, UserWithPublicKeys } from '../types'; import { encryptEmailContentAndSubjectSymmetrically, @@ -23,17 +22,15 @@ export async function encryptEmailAndSubjectHybrid( ): Promise { try { const aux = getAuxWithoutSubject(email.params); - const { enc, encryptionKey, subjectEnc } = await encryptEmailContentAndSubjectSymmetrically( + const { enc, encSubject, encryptionKey } = await encryptEmailContentAndSubjectSymmetrically( email.body, email.params.subject, aux, email.id, ); - const encryptedText = uint8ArrayToBase64(enc); - const encSubjectStr = uint8ArrayToBase64(subjectEnc); const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); - const params = { ...email.params, subject: encSubjectStr }; - return { enc: encryptedText, encryptedKey, recipientEmail: recipient.email, params, id: email.id }; + const params = { ...email.params, subject: encSubject }; + return { enc, encryptedKey, recipientEmail: recipient.email, params, id: email.id }; } catch (error) { throw new Error('Failed to encrypt the email and its subject with hybrid encryption', { cause: error }); } @@ -54,20 +51,18 @@ export async function encryptEmailAndSubjectHybridForMultipleRecipients( ): Promise { try { const aux = getAuxWithoutSubject(email.params); - const { enc, encryptionKey, subjectEnc } = await encryptEmailContentAndSubjectSymmetrically( + const { enc, encSubject, encryptionKey } = await encryptEmailContentAndSubjectSymmetrically( email.body, email.params.subject, aux, email.id, ); - const encSubjectStr = uint8ArrayToBase64(subjectEnc); - const encryptedText = uint8ArrayToBase64(enc); const encryptedEmails: HybridEncryptedEmail[] = []; for (const recipient of recipients) { const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); - const params = { ...email.params, subject: encSubjectStr }; - encryptedEmails.push({ enc: encryptedText, encryptedKey, recipientEmail: recipient.email, params, id: email.id }); + const params = { ...email.params, subject: encSubject }; + encryptedEmails.push({ enc, encryptedKey, recipientEmail: recipient.email, params, id: email.id }); } return encryptedEmails; } catch (error) { @@ -93,9 +88,12 @@ export async function decryptEmailAndSubjectHybrid( try { const aux = getAuxWithoutSubject(encryptedEmail.params); const encryptionKey = await decryptKeysHybrid(encryptedEmail.encryptedKey, senderPublicKeys, recipientPrivateKeys); - const encSubject = base64ToUint8Array(encryptedEmail.params.subject); - const enc = base64ToUint8Array(encryptedEmail.enc); - const { body, subject } = await decryptEmailAndSubjectSymmetrically(enc, encSubject, encryptionKey, aux); + const { body, subject } = await decryptEmailAndSubjectSymmetrically( + encryptionKey, + aux, + encryptedEmail.params.subject, + encryptedEmail.enc, + ); const params = { ...encryptedEmail.params, subject }; return { body, params, id: encryptedEmail.id }; } catch (error) { diff --git a/src/email-crypto/pwdProtectedEmail.ts b/src/email-crypto/pwdProtectedEmail.ts index 849476f..5a2955d 100644 --- a/src/email-crypto/pwdProtectedEmail.ts +++ b/src/email-crypto/pwdProtectedEmail.ts @@ -1,5 +1,4 @@ import { PwdProtectedEmail, Email } from '../types'; -import { base64ToUint8Array, uint8ArrayToBase64 } from '../utils'; import { encryptEmailContentSymmetrically, decryptEmailSymmetrically, @@ -22,9 +21,8 @@ export async function createPwdProtectedEmail(email: Email, password: string): P } const aux = getAux(email.params); const { enc, encryptionKey } = await encryptEmailContentSymmetrically(email.body, aux, email.id); - const encryptedText = uint8ArrayToBase64(enc); const encryptedKey = await passwordProtectKey(encryptionKey, password); - return { enc: encryptedText, encryptedKey, params: email.params, id: email.id }; + return { enc, encryptedKey, params: email.params, id: email.id }; } catch (error) { throw new Error('Failed to password-protect email', { cause: error }); } @@ -41,8 +39,7 @@ export async function decryptPwdProtectedEmail(encryptedEmail: PwdProtectedEmail try { const aux = getAux(encryptedEmail.params); const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password); - const enc = base64ToUint8Array(encryptedEmail.enc); - const body = await decryptEmailSymmetrically(enc, encryptionKey, aux); + const body = await decryptEmailSymmetrically(encryptionKey, aux, encryptedEmail.enc); return { body, params: encryptedEmail.params, id: encryptedEmail.id }; } catch (error) { throw new Error('Failed to decrypt password-protect email', { cause: error }); diff --git a/src/email-crypto/pwdProtectedEmailAndSubject.ts b/src/email-crypto/pwdProtectedEmailAndSubject.ts index c7f7487..fb05554 100644 --- a/src/email-crypto/pwdProtectedEmailAndSubject.ts +++ b/src/email-crypto/pwdProtectedEmailAndSubject.ts @@ -1,4 +1,3 @@ -import { base64ToUint8Array, uint8ArrayToBase64 } from '../utils'; import { PwdProtectedEmail, Email } from '../types'; import { encryptEmailContentAndSubjectSymmetrically, @@ -21,17 +20,15 @@ export async function createPwdProtectedEmailAndSubject(email: Email, password: throw new Error('Failed to password-protect email and subject: Invalid email structure'); } const aux = getAuxWithoutSubject(email.params); - const { enc, encryptionKey, subjectEnc } = await encryptEmailContentAndSubjectSymmetrically( + const { enc, encryptionKey, encSubject } = await encryptEmailContentAndSubjectSymmetrically( email.body, email.params.subject, aux, email.id, ); - const encryptedText = uint8ArrayToBase64(enc); const encryptedKey = await passwordProtectKey(encryptionKey, password); - const encSubjectStr = uint8ArrayToBase64(subjectEnc); - const params = { ...email.params, subject: encSubjectStr }; - return { enc: encryptedText, encryptedKey, params, id: email.id }; + const params = { ...email.params, subject: encSubject }; + return { enc, encryptedKey, params, id: email.id }; } catch (error) { throw new Error('Failed to password-protect email and subject', { cause: error }); } @@ -51,9 +48,12 @@ export async function decryptPwdProtectedEmailAndSubject( try { const aux = getAuxWithoutSubject(encryptedEmail.params); const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password); - const encSubject = base64ToUint8Array(encryptedEmail.params.subject); - const enc = base64ToUint8Array(encryptedEmail.enc); - const { body, subject } = await decryptEmailAndSubjectSymmetrically(enc, encSubject, encryptionKey, aux); + const { body, subject } = await decryptEmailAndSubjectSymmetrically( + encryptionKey, + aux, + encryptedEmail.params.subject, + encryptedEmail.enc, + ); const params = { ...encryptedEmail.params, subject }; return { body, params, id: encryptedEmail.id }; } catch (error) { diff --git a/src/email-search/indexedDB.ts b/src/email-search/indexedDB.ts index e624721..dae96b5 100644 --- a/src/email-search/indexedDB.ts +++ b/src/email-search/indexedDB.ts @@ -91,13 +91,8 @@ export const encryptAndStoreEmail = async ( ): Promise => { try { const aux = getAux(newEmailToStore.params); - const ciphertext = await encryptEmailContentSymmetricallyWithKey( - newEmailToStore.body, - indexKey, - aux, - newEmailToStore.id, - ); - const encryptedEmail = { content: ciphertext, params: newEmailToStore.params, id: newEmailToStore.id }; + const enc = await encryptEmailContentSymmetricallyWithKey(newEmailToStore.body, indexKey, aux, newEmailToStore.id); + const encryptedEmail: StoredEmail = { enc, params: newEmailToStore.params, id: newEmailToStore.id }; await esDB.put(DB_LABEL, encryptedEmail); } catch (error) { throw new Error('Cannot encrypt and add the given email to the database', { cause: error }); @@ -120,9 +115,9 @@ export const encryptAndStoreManyEmail = async ( const encryptedEmails = await Promise.all( newEmailsToStore.map(async (email: Email) => { const aux = getAux(email.params); - const ciphertext = await encryptEmailContentSymmetricallyWithKey(email.body, indexKey, aux, email.id); + const enc = await encryptEmailContentSymmetricallyWithKey(email.body, indexKey, aux, email.id); - return { content: ciphertext, params: email.params, id: email.id }; + return { enc, params: email.params, id: email.id }; }), ); @@ -143,7 +138,7 @@ export const encryptAndStoreManyEmail = async ( const decryptEmail = async (indexKey: CryptoKey, encryptedEmail: StoredEmail): Promise => { try { const aux = getAux(encryptedEmail.params); - const email = await decryptEmailSymmetrically(encryptedEmail.content, indexKey, aux); + const email = await decryptEmailSymmetrically(indexKey, aux, encryptedEmail.enc); return { body: email, params: encryptedEmail.params, id: encryptedEmail.id }; } catch (error) { throw new Error('Cannot decrypt the given email', { cause: error }); @@ -184,7 +179,7 @@ export const getAndDecryptAllEmails = async (indexKey: CryptoKey, esDB: MailDB): const decryptedEmails = await Promise.all( encryptedEmails.map(async (encEmail) => { const aux = getAux(encEmail.params); - const body = await decryptEmailSymmetrically(encEmail.content, indexKey, aux); + const body = await decryptEmailSymmetrically(indexKey, aux, encEmail.enc); return { body, params: encEmail.params, id: encEmail.id }; }), ); diff --git a/src/types.ts b/src/types.ts index 6787d6f..8d18b62 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,7 +45,7 @@ export type EmailKeysEncrypted = { export type HybridEncryptedEmail = { encryptedKey: HybridEncKey; - enc: string; + enc: EmailBodyEncrypted; recipientEmail: string; params: EmailPublicParameters; id: string; @@ -53,14 +53,14 @@ export type HybridEncryptedEmail = { export type PwdProtectedEmail = { encryptedKey: PwdProtectedKey; - enc: string; + enc: EmailBodyEncrypted; params: EmailPublicParameters; id: string; }; export type StoredEmail = { params: EmailPublicParameters; - content: Uint8Array; + enc: EmailBodyEncrypted; id: string; }; @@ -74,6 +74,11 @@ export type PwdProtectedKey = { salt: string; }; +export type EmailBodyEncrypted = { + encText: string; + encAttachments?: string[]; +}; + export type EmailBody = { text: string; attachments?: string[]; diff --git a/tests/email-crypto/converter.test.ts b/tests/email-crypto/converter.test.ts deleted file mode 100644 index e376cbe..0000000 --- a/tests/email-crypto/converter.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { EmailBody } from '../../src/types'; -import { emailBodyToBinary, binaryToEmailBody } from '../../src/email-crypto'; -describe('Test email crypto functions', () => { - it('email converter to binary and back works', async () => { - const email: EmailBody = { - text: 'test body', - attachments: ['test attachement 1', 'test attachement 2'], - }; - const binary = emailBodyToBinary(email); - const result = binaryToEmailBody(binary); - expect(result).toEqual(email); - }); - - it('throws error if email converter to binary fails', async () => { - const bad_binary: Uint8Array = new Uint8Array([ - 49, 34, 44, 34, 116, 101, 115, 116, 32, 114, 101, 99, 105, 112, 105, 101, 110, 116, 32, 50, 34, 44, 34, 116, 101, - 115, 116, 32, 114, 101, 99, 105, 112, 105, 101, 110, 116, 32, 51, 34, 93, 44, 34, - ]); - expect(() => binaryToEmailBody(bad_binary)).toThrowError(/Failed to convert Uint8Array to EmailBody/); - }); - - it('throws error if binary to email converter fails', async () => { - const bad_email = { - id: BigInt(42), - subject: 'test subject', - body: 'test body', - sender: 'test sender', - recipient: ['test recipient 1', 'test recipient 2', 'test recipient 3'], - createdAt: '2023-06-14T08:11:22.000Z', - labels: ['test label 1', 'test label2'], - }; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - expect(() => emailBodyToBinary(bad_email as any as EmailBody)).toThrowError( - /Failed to convert EmailBody to Uint8Array/, - ); - }); -}); diff --git a/tests/email-crypto/core.test.ts b/tests/email-crypto/core.test.ts index 7d60126..2205da4 100644 --- a/tests/email-crypto/core.test.ts +++ b/tests/email-crypto/core.test.ts @@ -47,32 +47,37 @@ describe('Test email crypto functions', () => { it('should encrypt and decrypt email', async () => { const { enc, encryptionKey } = await encryptEmailContentSymmetrically(emailBody, aux, id); - const result = await decryptEmailSymmetrically(enc, encryptionKey, aux); + const result = await decryptEmailSymmetrically(encryptionKey, aux, enc); expect(result).toEqual(emailBody); }); it('should throw an error if decryption fails', async () => { const { enc, encryptionKey } = await encryptEmailContentSymmetrically(emailBody, aux, id); const bad_encryptionKey = await genSymmetricCryptoKey(); - await expect(decryptEmailSymmetrically(enc, bad_encryptionKey, aux)).rejects.toThrowError( + await expect(decryptEmailSymmetrically(bad_encryptionKey, aux, enc)).rejects.toThrowError( /Failed to symmetrically decrypt email/, ); - const { - enc: encBody, - encryptionKey: key, - subjectEnc, - } = await encryptEmailContentAndSubjectSymmetrically(emailBody, emailParams.subject, aux, id); - await expect(decryptEmailAndSubjectSymmetrically(encBody, subjectEnc, bad_encryptionKey, aux)).rejects.toThrowError( + const bad_aux = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + await expect(decryptEmailSymmetrically(encryptionKey, bad_aux, enc)).rejects.toThrowError( + /Failed to symmetrically decrypt email/, + ); + }); + it('should throw an error if decryption fails', async () => { + const bad_encryptionKey = await genSymmetricCryptoKey(); + const { enc, encryptionKey, encSubject } = await encryptEmailContentAndSubjectSymmetrically( + emailBody, + emailParams.subject, + aux, + id, + ); + await expect(decryptEmailAndSubjectSymmetrically(bad_encryptionKey, aux, encSubject, enc)).rejects.toThrowError( /Failed to symmetrically decrypt email and subject/, ); const bad_aux = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - await expect(decryptEmailSymmetrically(enc, encryptionKey, bad_aux)).rejects.toThrowError( - /Failed to symmetrically decrypt email/, - ); - await expect(decryptEmailAndSubjectSymmetrically(encBody, subjectEnc, key, bad_aux)).rejects.toThrowError( + await expect(decryptEmailAndSubjectSymmetrically(encryptionKey, bad_aux, encSubject, enc)).rejects.toThrowError( /Failed to symmetrically decrypt email and subject/, ); }); diff --git a/tests/email-crypto/hybridEmail.test.ts b/tests/email-crypto/hybridEmail.test.ts index 1e8a5a2..a6cddb4 100644 --- a/tests/email-crypto/hybridEmail.test.ts +++ b/tests/email-crypto/hybridEmail.test.ts @@ -17,7 +17,6 @@ import { EmailPublicParameters, Email, } from '../../src/types'; -import { encryptSymmetrically, genSymmetricCryptoKey } from '../../src/symmetric-crypto'; import { generateUuid } from '../../src/utils'; describe('Test email crypto functions', async () => { @@ -80,21 +79,15 @@ describe('Test email crypto functions', async () => { }); it('should throw an error if hybrid email decryption fails', async () => { - const key = await genSymmetricCryptoKey(); - const freeField = new Uint8Array([1, 2, 3, 4]); - const emailCiphertext = await encryptSymmetrically( - key, - new Uint8Array([1, 2, 3]), - new TextEncoder().encode('aux'), - freeField, - ); const encKey: HybridEncKey = { - kyberCiphertext: new Uint8Array([1, 2, 3]), - encryptedKey: new Uint8Array([4, 5, 6, 7]), + kyberCiphertext: 'mock kyber ciphertext', + encryptedKey: 'mock encrypted key', }; const bad_encrypted_email: HybridEncryptedEmail = { encryptedKey: encKey, - enc: emailCiphertext, + enc: { + encText: 'mock encrypted text', + }, recipientEmail: userBob.email, params: emailParams, id: generateUuid(), diff --git a/tests/email-crypto/hybridEmailAndSubject.test.ts b/tests/email-crypto/hybridEmailAndSubject.test.ts index e3ae8cb..3f80820 100644 --- a/tests/email-crypto/hybridEmailAndSubject.test.ts +++ b/tests/email-crypto/hybridEmailAndSubject.test.ts @@ -17,7 +17,6 @@ import { EmailPublicParameters, Email, } from '../../src/types'; -import { encryptSymmetrically, genSymmetricCryptoKey } from '../../src/symmetric-crypto'; import { generateUuid } from '../../src/utils'; describe('Test email crypto functions', async () => { @@ -81,18 +80,13 @@ describe('Test email crypto functions', async () => { }); it('should throw an error if hybrid email decryption fails', async () => { - const key = await genSymmetricCryptoKey(); - const aux = new Uint8Array([1, 2, 3, 4]); - const freeField = new Uint8Array([1]); - - const emailCiphertext = await encryptSymmetrically(key, new Uint8Array([1, 2, 3]), aux, freeField); const encKey: HybridEncKey = { - kyberCiphertext: new Uint8Array([1, 2, 3]), - encryptedKey: new Uint8Array([4, 5, 6, 7]), + kyberCiphertext: 'mock kyber ciphertext', + encryptedKey: 'mock encrypted key', }; const bad_encrypted_email: HybridEncryptedEmail = { encryptedKey: encKey, - enc: emailCiphertext, + encText: 'mock encrypted email text', recipientEmail: userBob.email, params: emailParams, id: 'test id', @@ -120,7 +114,7 @@ describe('Test email crypto functions', async () => { ); expect(encryptedEmail.length).toBe(2); - expect(encryptedEmail[0].enc).toBe(encryptedEmail[1].enc); + expect(encryptedEmail[0].encText).toBe(encryptedEmail[1].encText); expect(encryptedEmail[0].params.subject).toBe(encryptedEmail[1].params.subject); expect(encryptedEmail[0].params.subject).not.toBe(email.params.subject);