diff --git a/src/email-crypto/core.ts b/src/email-crypto/core.ts index 55be416..0554432 100644 --- a/src/email-crypto/core.ts +++ b/src/email-crypto/core.ts @@ -1,10 +1,79 @@ -import { HybridEncKey, PwdProtectedKey, PublicKeys, PrivateKeys, EmailBody, EmailBodyEncrypted } from '../types'; +import { + HybridEncKey, + PwdProtectedKey, + PublicKeys, + PrivateKeys, + EmailBody, + EmailBodyEncrypted, + Email, + EmailPublicParameters, +} from '../types'; import { genSymmetricCryptoKey, encryptSymmetrically, decryptSymmetrically } from '../symmetric-crypto'; import { encapsulateKyber, decapsulateKyber } from '../post-quantum-crypto'; import { deriveWrappingKey, wrapKey, unwrapKey, importWrappingKey } from '../key-wrapper'; import { deriveSecretKey } from '../asymmetric-crypto'; import { getKeyFromPassword, getKeyFromPasswordAndSalt } from '../derive-key'; import { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8, uuidToBytes } from '../utils'; +import { getAux } from './utils'; + +export async function encryptEmailBody( + email: Email, + isSubjectEncrypted: boolean, +): Promise<{ + enc: EmailBodyEncrypted; + params: EmailPublicParameters; + encryptionKey: CryptoKey; +}> { + try { + const aux = getAux(email.params, isSubjectEncrypted); + + let enc: EmailBodyEncrypted; + let encryptionKey: CryptoKey; + let params = email.params; + + if (isSubjectEncrypted) { + const result = await encryptEmailContentAndSubjectSymmetrically(email.body, email.params.subject, aux, email.id); + enc = result.enc; + encryptionKey = result.encryptionKey; + params = { ...email.params, subject: result.encSubject }; + } else { + const result = await encryptEmailContentSymmetrically(email.body, aux, email.id); + enc = result.enc; + encryptionKey = result.encryptionKey; + } + + return { encryptionKey, enc, params }; + } catch (error) { + throw new Error('Failed to encrypt email body', { cause: error }); + } +} + +export async function decryptEmailBody( + enc: EmailBodyEncrypted, + encParams: EmailPublicParameters, + encryptionKey: CryptoKey, + isSubjectEncrypted: boolean, +): Promise<{ + params: EmailPublicParameters; + body: EmailBody; +}> { + try { + const aux = getAux(encParams, isSubjectEncrypted); + let body: EmailBody; + let params = encParams; + if (isSubjectEncrypted) { + const result = await decryptEmailAndSubjectSymmetrically(encryptionKey, aux, encParams.subject, enc); + body = result.body; + params = { ...encParams, subject: result.subject }; + } else { + body = await decryptEmailSymmetrically(encryptionKey, aux, enc); + } + + return { body, params }; + } catch (error) { + throw new Error('Failed to encrypt email body', { cause: error }); + } +} /** * Symmetrically encrypts an email with a randomly sampled key. diff --git a/src/email-crypto/hybridEncyptedEmail.ts b/src/email-crypto/hybridEncyptedEmail.ts index e30b435..e152664 100644 --- a/src/email-crypto/hybridEncyptedEmail.ts +++ b/src/email-crypto/hybridEncyptedEmail.ts @@ -1,11 +1,5 @@ import { PublicKeys, PrivateKeys, HybridEncryptedEmail, Email, UserWithPublicKeys } from '../types'; -import { - encryptEmailContentSymmetrically, - decryptEmailSymmetrically, - encryptKeysHybrid, - decryptKeysHybrid, -} from './core'; -import { getAux } from './utils'; +import { decryptEmailBody, encryptKeysHybrid, decryptKeysHybrid, encryptEmailBody } from './core'; /** * Encrypts the email using hybrid encryption. @@ -13,18 +7,19 @@ import { getAux } from './utils'; * @param email - The email to encrypt. * @param recipientPublicKeys - The public keys of the recipient. * @param senderPrivateKey - The private key of the sender. + * @param isSubjectEncrypted - Indicates if the email subject field should be encrypted * @returns The encrypted email */ export async function encryptEmailHybrid( email: Email, recipient: UserWithPublicKeys, senderPrivateKey: PrivateKeys, + isSubjectEncrypted: boolean = false, ): Promise { try { - const aux = getAux(email.params); - const { enc, encryptionKey } = await encryptEmailContentSymmetrically(email.body, aux, email.id); + const { encryptionKey, params, enc } = await encryptEmailBody(email, isSubjectEncrypted); const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); - return { enc, encryptedKey, recipientEmail: recipient.email, params: email.params, id: email.id }; + return { enc, encryptedKey, recipientEmail: recipient.email, params, isSubjectEncrypted, id: email.id }; } catch (error) { throw new Error('Failed to encrypt email with hybrid encryption', { cause: error }); } @@ -36,16 +31,17 @@ export async function encryptEmailHybrid( * @param email - The email to encrypt. * @param recipients - The recipients with corresponding public keys. * @param senderPrivateKey - The private key of the sender. + * @param isSubjectEncrypted - Indicates if the email subject field should be encrypted * @returns The set of encrypted email */ export async function encryptEmailHybridForMultipleRecipients( email: Email, recipients: UserWithPublicKeys[], senderPrivateKey: PrivateKeys, + isSubjectEncrypted: boolean = false, ): Promise { try { - const aux = getAux(email.params); - const { enc, encryptionKey } = await encryptEmailContentSymmetrically(email.body, aux, email.id); + const { encryptionKey, params, enc } = await encryptEmailBody(email, isSubjectEncrypted); const encryptedEmails: HybridEncryptedEmail[] = []; for (const recipient of recipients) { @@ -54,7 +50,8 @@ export async function encryptEmailHybridForMultipleRecipients( enc, encryptedKey, recipientEmail: recipient.email, - params: email.params, + params, + isSubjectEncrypted, id: email.id, }); } @@ -78,10 +75,10 @@ export async function decryptEmailHybrid( recipientPrivateKeys: PrivateKeys, ): Promise { try { - const aux = getAux(encryptedEmail.params); - const encryptionKey = await decryptKeysHybrid(encryptedEmail.encryptedKey, senderPublicKeys, recipientPrivateKeys); - const body = await decryptEmailSymmetrically(encryptionKey, aux, encryptedEmail.enc); - return { body, params: encryptedEmail.params, id: encryptedEmail.id }; + const { isSubjectEncrypted, params: encParams, enc, encryptedKey, id } = encryptedEmail; + const encryptionKey = await decryptKeysHybrid(encryptedKey, senderPublicKeys, recipientPrivateKeys); + const { body, params } = await decryptEmailBody(enc, encParams, encryptionKey, isSubjectEncrypted); + return { body, params, 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 deleted file mode 100644 index 598f14e..0000000 --- a/src/email-crypto/hybridEncyptedEmailAndSubject.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { PublicKeys, PrivateKeys, HybridEncryptedEmail, Email, UserWithPublicKeys } from '../types'; -import { - encryptEmailContentAndSubjectSymmetrically, - decryptEmailAndSubjectSymmetrically, - encryptKeysHybrid, - decryptKeysHybrid, -} from './core'; -import { getAuxWithoutSubject } from './utils'; - -/** - * Encrypts the email and its subject using hybrid encryption. - * - * @param email - The email to encrypt. - * @param recipientPublicKeys - The public keys of the recipient. - * @param senderPrivateKey - The private key of the sender. - * @returns The encrypted email - */ -export async function encryptEmailAndSubjectHybrid( - email: Email, - recipient: UserWithPublicKeys, - senderPrivateKey: PrivateKeys, -): Promise { - try { - const aux = getAuxWithoutSubject(email.params); - const { enc, encSubject, encryptionKey } = await encryptEmailContentAndSubjectSymmetrically( - email.body, - email.params.subject, - aux, - email.id, - ); - const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); - 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 }); - } -} - -/** - * Encrypts the email using hybrid encryption for multiple recipients. - * - * @param email - The email to encrypt. - * @param recipients - The recipients with corresponding public keys. - * @param senderPrivateKey - The private key of the sender. - * @returns The set of encrypted email - */ -export async function encryptEmailAndSubjectHybridForMultipleRecipients( - email: Email, - recipients: UserWithPublicKeys[], - senderPrivateKey: PrivateKeys, -): Promise { - try { - const aux = getAuxWithoutSubject(email.params); - const { enc, encSubject, encryptionKey } = await encryptEmailContentAndSubjectSymmetrically( - email.body, - email.params.subject, - aux, - email.id, - ); - - const encryptedEmails: HybridEncryptedEmail[] = []; - for (const recipient of recipients) { - const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); - const params = { ...email.params, subject: encSubject }; - encryptedEmails.push({ enc, encryptedKey, recipientEmail: recipient.email, params, id: email.id }); - } - return encryptedEmails; - } catch (error) { - throw new Error('Failed to encrypt the email and its subject to multiple recipients with hybrid encryption', { - cause: error, - }); - } -} - -/** - * Decrypts the email using hybrid encryption. - * - * @param encryptedEmail - The encrypted email. - * @param senderPublicKeys - The public key of the sender. - * @param recipientPrivateKeys - The private key of the recipient. - * @returns The decrypted email - */ -export async function decryptEmailAndSubjectHybrid( - encryptedEmail: HybridEncryptedEmail, - senderPublicKeys: PublicKeys, - recipientPrivateKeys: PrivateKeys, -): Promise { - try { - const aux = getAuxWithoutSubject(encryptedEmail.params); - const encryptionKey = await decryptKeysHybrid(encryptedEmail.encryptedKey, senderPublicKeys, recipientPrivateKeys); - 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) { - throw new Error('Failed to decrypt the email and its subject with hybrid encryption', { cause: error }); - } -} diff --git a/src/email-crypto/index.ts b/src/email-crypto/index.ts index 52ff7db..71d730a 100644 --- a/src/email-crypto/index.ts +++ b/src/email-crypto/index.ts @@ -1,7 +1,5 @@ export * from './hybridEncyptedEmail'; -export * from './hybridEncyptedEmailAndSubject'; export * from './pwdProtectedEmail'; -export * from './pwdProtectedEmailAndSubject'; export * from './converters'; export * from './emailKeys'; export * from './utils'; diff --git a/src/email-crypto/pwdProtectedEmail.ts b/src/email-crypto/pwdProtectedEmail.ts index 5a2955d..e99e60a 100644 --- a/src/email-crypto/pwdProtectedEmail.ts +++ b/src/email-crypto/pwdProtectedEmail.ts @@ -1,28 +1,27 @@ import { PwdProtectedEmail, Email } from '../types'; -import { - encryptEmailContentSymmetrically, - decryptEmailSymmetrically, - passwordProtectKey, - removePasswordProtection, -} from './core'; -import { getAux } from './utils'; +import { decryptEmailBody, passwordProtectKey, removePasswordProtection, encryptEmailBody } from './core'; /** * Creates a password-protected email. * * @param email - The email to password-protect - * @param password - The secret password shared among recipients. + * @param password - The secret password shared among recipients + * @param isSubjectEncrypted - Indicates if the email subject field should be encrypted * @returns The password-protected email */ -export async function createPwdProtectedEmail(email: Email, password: string): Promise { +export async function createPwdProtectedEmail( + email: Email, + password: string, + isSubjectEncrypted: boolean = false, +): Promise { try { if (!email?.body || !email.params) { throw new Error('Failed to password-protect email: Invalid email structure'); } - const aux = getAux(email.params); - const { enc, encryptionKey } = await encryptEmailContentSymmetrically(email.body, aux, email.id); + const { encryptionKey, params, enc } = await encryptEmailBody(email, isSubjectEncrypted); const encryptedKey = await passwordProtectKey(encryptionKey, password); - return { enc, encryptedKey, params: email.params, id: email.id }; + + return { enc, encryptedKey, params, id: email.id, isSubjectEncrypted }; } catch (error) { throw new Error('Failed to password-protect email', { cause: error }); } @@ -37,10 +36,10 @@ export async function createPwdProtectedEmail(email: Email, password: string): P */ export async function decryptPwdProtectedEmail(encryptedEmail: PwdProtectedEmail, password: string): Promise { try { - const aux = getAux(encryptedEmail.params); + const { isSubjectEncrypted, params: encParams, enc, id } = encryptedEmail; const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password); - const body = await decryptEmailSymmetrically(encryptionKey, aux, encryptedEmail.enc); - return { body, params: encryptedEmail.params, id: encryptedEmail.id }; + const { body, params } = await decryptEmailBody(enc, encParams, encryptionKey, isSubjectEncrypted); + return { body, params, 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 deleted file mode 100644 index fb05554..0000000 --- a/src/email-crypto/pwdProtectedEmailAndSubject.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { PwdProtectedEmail, Email } from '../types'; -import { - encryptEmailContentAndSubjectSymmetrically, - decryptEmailAndSubjectSymmetrically, - passwordProtectKey, - removePasswordProtection, -} from './core'; -import { getAuxWithoutSubject } from './utils'; - -/** - * Creates a password-protected email and its subject. - * - * @param email - The email to password-protect - * @param password - The secret password shared among recipients. - * @returns The password-protected email with encrypted email subject - */ -export async function createPwdProtectedEmailAndSubject(email: Email, password: string): Promise { - try { - if (!email?.body || !email.params) { - throw new Error('Failed to password-protect email and subject: Invalid email structure'); - } - const aux = getAuxWithoutSubject(email.params); - const { enc, encryptionKey, encSubject } = await encryptEmailContentAndSubjectSymmetrically( - email.body, - email.params.subject, - aux, - email.id, - ); - const encryptedKey = await passwordProtectKey(encryptionKey, password); - 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 }); - } -} - -/** - * Opens a password-protected email. - * - * @param encryptedEmail - The encrypted email with encrypted email subject. - * @param password - The secret password shared among recipients. - * @returns The decrypted email and email subject - */ -export async function decryptPwdProtectedEmailAndSubject( - encryptedEmail: PwdProtectedEmail, - password: string, -): Promise { - try { - const aux = getAuxWithoutSubject(encryptedEmail.params); - const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password); - 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) { - throw new Error('Failed to decrypt password-protect email', { cause: error }); - } -} diff --git a/src/email-crypto/utils.ts b/src/email-crypto/utils.ts index 41ae48a..1fb7e83 100644 --- a/src/email-crypto/utils.ts +++ b/src/email-crypto/utils.ts @@ -4,46 +4,27 @@ import { UTF8ToUint8, uuidToBytes } from '../utils'; import { userToBytes, recipientsToBytes } from './converters'; /** - * Creates an auxilary string from public fields of the email. + * Creates an auxiliary string from public fields of the email. * * @param params - The email public parameters. - * @returns The resulting auxilary string + * @param isSubjectEncrypted - Indicates if the email subject field should be encrypted + * @returns The resulting auxiliary string */ -export function getAux(params: EmailPublicParameters): Uint8Array { +export function getAux(params: EmailPublicParameters, isSubjectEncrypted: boolean): Uint8Array { try { const { subject, replyToEmailID, sender, recipient, recipients } = params; - const subjectBytes = UTF8ToUint8(subject); const replyBytes = replyToEmailID ? uuidToBytes(replyToEmailID) : new Uint8Array(); const senderBytes = userToBytes(sender); const recipientBytes = userToBytes(recipient); const recipientsBytes = recipients ? recipientsToBytes(recipients) : new Uint8Array(); - const aux = concatBytes(subjectBytes, replyBytes, senderBytes, recipientBytes, recipientsBytes); - - return aux; + if (isSubjectEncrypted) { + return concatBytes(replyBytes, senderBytes, recipientBytes, recipientsBytes); + } else { + const subjectBytes = UTF8ToUint8(subject); + return concatBytes(subjectBytes, replyBytes, senderBytes, recipientBytes, recipientsBytes); + } } catch (error) { throw new Error('Failed to create aux', { cause: error }); } } - -/** - * Creates an auxilary string from public fields of the email (except for subject field). - * - * @param params - The email public parameters. - * @returns The resulting auxilary string - */ -export function getAuxWithoutSubject(params: EmailPublicParameters): Uint8Array { - try { - const { replyToEmailID, sender, recipient, recipients } = params; - const replyBytes = replyToEmailID ? uuidToBytes(replyToEmailID) : new Uint8Array(); - const senderBytes = userToBytes(sender); - const recipientBytes = userToBytes(recipient); - const recipientsBytes = recipients ? recipientsToBytes(recipients) : new Uint8Array(); - - const aux = concatBytes(replyBytes, senderBytes, recipientBytes, recipientsBytes); - - return aux; - } catch (error) { - throw new Error('Failed to create aux without subject', { cause: error }); - } -} diff --git a/src/types.ts b/src/types.ts index 8d18b62..3b0bbe8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -49,6 +49,7 @@ export type HybridEncryptedEmail = { recipientEmail: string; params: EmailPublicParameters; id: string; + isSubjectEncrypted: boolean; }; export type PwdProtectedEmail = { @@ -56,6 +57,7 @@ export type PwdProtectedEmail = { enc: EmailBodyEncrypted; params: EmailPublicParameters; id: string; + isSubjectEncrypted: boolean; }; export type StoredEmail = { diff --git a/tests/email-crypto/core.test.ts b/tests/email-crypto/core.test.ts index 2205da4..b4a7d23 100644 --- a/tests/email-crypto/core.test.ts +++ b/tests/email-crypto/core.test.ts @@ -7,7 +7,7 @@ import { encryptEmailContentSymmetrically, } from '../../src/email-crypto/core'; import { generateUuid } from '../../src/utils'; -import { getAux, getAuxWithoutSubject } from '../../src/email-crypto'; +import { getAux } from '../../src/email-crypto'; import { genSymmetricCryptoKey } from '../../src/symmetric-crypto'; describe('Test email crypto functions', () => { @@ -36,7 +36,7 @@ describe('Test email crypto functions', () => { const id = generateUuid(); - const aux = getAux(emailParams); + const aux = getAux(emailParams, false); it('should generate email id', async () => { const result1 = generateUuid(); @@ -85,9 +85,9 @@ describe('Test email crypto functions', () => { it('should throw an error if cannot create aux', async () => { const bad_params = { replyToEmailID: BigInt(423) }; // eslint-disable-next-line @typescript-eslint/no-explicit-any - expect(() => getAux(bad_params as any as EmailPublicParameters)).toThrowError(/Failed to create aux/); + expect(() => getAux(bad_params as any as EmailPublicParameters, false)).toThrowError(/Failed to create aux/); // eslint-disable-next-line @typescript-eslint/no-explicit-any - expect(() => getAuxWithoutSubject(bad_params as any as EmailPublicParameters)).toThrowError(/Failed to create aux/); + expect(() => getAux(bad_params as any as EmailPublicParameters, true)).toThrowError(/Failed to create aux/); }); it('should throw an error if cannot encrypt', async () => { diff --git a/tests/email-crypto/hybridEmail.test.ts b/tests/email-crypto/hybridEmail.test.ts index a6cddb4..d7b141c 100644 --- a/tests/email-crypto/hybridEmail.test.ts +++ b/tests/email-crypto/hybridEmail.test.ts @@ -91,6 +91,7 @@ describe('Test email crypto functions', async () => { recipientEmail: userBob.email, params: emailParams, id: generateUuid(), + isSubjectEncrypted: false, }; await expect(decryptEmailHybrid(bad_encrypted_email, alicePublicKeys, bobPrivateKeys)).rejects.toThrowError( diff --git a/tests/email-crypto/hybridEmailAndSubject.test.ts b/tests/email-crypto/hybridEmailAndSubject.test.ts index 3f80820..ac4b447 100644 --- a/tests/email-crypto/hybridEmailAndSubject.test.ts +++ b/tests/email-crypto/hybridEmailAndSubject.test.ts @@ -1,8 +1,8 @@ import { describe, expect, it } from 'vitest'; import { - encryptEmailAndSubjectHybrid, - decryptEmailAndSubjectHybrid, - encryptEmailAndSubjectHybridForMultipleRecipients, + encryptEmailHybrid, + decryptEmailHybrid, + encryptEmailHybridForMultipleRecipients, generateEmailKeys, } from '../../src/email-crypto'; @@ -58,8 +58,8 @@ describe('Test email crypto functions', async () => { }; it('should encrypt and decrypt email sucessfully', async () => { - const encryptedEmail = await encryptEmailAndSubjectHybrid(email, bobWithPublicKeys, alicePrivateKeys); - const decryptedEmail = await decryptEmailAndSubjectHybrid(encryptedEmail, alicePublicKeys, bobPrivateKeys); + const encryptedEmail = await encryptEmailHybrid(email, bobWithPublicKeys, alicePrivateKeys, true); + const decryptedEmail = await decryptEmailHybrid(encryptedEmail, alicePublicKeys, bobPrivateKeys); expect(decryptedEmail).toStrictEqual(email); expect(encryptedEmail.params.subject).not.toBe(email.params.subject); @@ -74,8 +74,8 @@ describe('Test email crypto functions', async () => { kyberPrivateKey: kyberKeys.secretKey, }; - await expect(encryptEmailAndSubjectHybrid(email, bobWithPublicKeys, bad_alicePrivateKey)).rejects.toThrowError( - /Failed to encrypt the email and its subject with hybrid encryption/, + await expect(encryptEmailHybrid(email, bobWithPublicKeys, bad_alicePrivateKey, true)).rejects.toThrowError( + /Failed to encrypt email with hybrid encryption/, ); }); @@ -86,18 +86,21 @@ describe('Test email crypto functions', async () => { }; const bad_encrypted_email: HybridEncryptedEmail = { encryptedKey: encKey, - encText: 'mock encrypted email text', + enc: { + encText: 'mock encrypted email text', + }, recipientEmail: userBob.email, params: emailParams, id: 'test id', + isSubjectEncrypted: true, }; - await expect( - decryptEmailAndSubjectHybrid(bad_encrypted_email, alicePublicKeys, bobPrivateKeys), - ).rejects.toThrowError(/Failed to decrypt the email and its subject with hybrid encryption/); + await expect(decryptEmailHybrid(bad_encrypted_email, alicePublicKeys, bobPrivateKeys)).rejects.toThrowError( + /Failed to decrypt email with hybrid encryption/, + ); }); - it('should encrypt email to multiple senders sucessfully', async () => { + it('should encrypt the email to multiple senders sucessfully', async () => { const { privateKeys: evePrivateKeys, publicKeys: evePublicKeys } = await generateEmailKeys(); const eveWithPublicKeys = { @@ -107,21 +110,22 @@ describe('Test email crypto functions', async () => { publicKeys: evePublicKeys, }; - const encryptedEmail = await encryptEmailAndSubjectHybridForMultipleRecipients( + const encryptedEmail = await encryptEmailHybridForMultipleRecipients( email, [bobWithPublicKeys, eveWithPublicKeys], alicePrivateKeys, + true, ); expect(encryptedEmail.length).toBe(2); - expect(encryptedEmail[0].encText).toBe(encryptedEmail[1].encText); + expect(encryptedEmail[0].enc).toBe(encryptedEmail[1].enc); expect(encryptedEmail[0].params.subject).toBe(encryptedEmail[1].params.subject); expect(encryptedEmail[0].params.subject).not.toBe(email.params.subject); - const decEmailBob = await decryptEmailAndSubjectHybrid(encryptedEmail[0], alicePublicKeys, bobPrivateKeys); + const decEmailBob = await decryptEmailHybrid(encryptedEmail[0], alicePublicKeys, bobPrivateKeys); expect(decEmailBob).toStrictEqual(email); - const decEmailEve = await decryptEmailAndSubjectHybrid(encryptedEmail[1], alicePublicKeys, evePrivateKeys); + const decEmailEve = await decryptEmailHybrid(encryptedEmail[1], alicePublicKeys, evePrivateKeys); expect(decEmailEve).toStrictEqual(email); }); @@ -141,11 +145,12 @@ describe('Test email crypto functions', async () => { publicKeys: bad_evePublicKeys, }; await expect( - encryptEmailAndSubjectHybridForMultipleRecipients( + encryptEmailHybridForMultipleRecipients( email, [bobWithPublicKeys, bad_eveWithPublicKeys], alicePrivateKeys, + true, ), - ).rejects.toThrowError(/Failed to encrypt the email and its subject to multiple recipients with hybrid encryption/); + ).rejects.toThrowError(/Failed to encrypt email to multiple recipients with hybrid encryption/); }); }); diff --git a/tests/email-crypto/pwdProtectedEmailAndSubject.test.ts b/tests/email-crypto/pwdProtectedEmailAndSubject.test.ts index e431678..f315c61 100644 --- a/tests/email-crypto/pwdProtectedEmailAndSubject.test.ts +++ b/tests/email-crypto/pwdProtectedEmailAndSubject.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { createPwdProtectedEmailAndSubject, decryptPwdProtectedEmailAndSubject } from '../../src/email-crypto'; +import { createPwdProtectedEmail, decryptPwdProtectedEmail } from '../../src/email-crypto'; import { EmailBody, Email, User, EmailPublicParameters } from '../../src/types'; import { generateUuid } from '../../src/utils'; @@ -34,8 +34,8 @@ describe('Test email crypto functions', () => { }; it('should encrypt and decrypt email sucessfully', async () => { - const encryptedEmail = await createPwdProtectedEmailAndSubject(email, sharedSecret); - const decryptedEmail = await decryptPwdProtectedEmailAndSubject(encryptedEmail, sharedSecret); + const encryptedEmail = await createPwdProtectedEmail(email, sharedSecret, true); + const decryptedEmail = await decryptPwdProtectedEmail(encryptedEmail, sharedSecret); expect(decryptedEmail).toStrictEqual(email); expect(encryptedEmail.params.subject).not.toBe(email.params.subject); }); @@ -44,15 +44,15 @@ describe('Test email crypto functions', () => { const bad_email = { params: emailParams, } as unknown as Email; - await expect(createPwdProtectedEmailAndSubject(bad_email, sharedSecret)).rejects.toThrowError( + await expect(createPwdProtectedEmail(bad_email, sharedSecret, true)).rejects.toThrowError( /Failed to password-protect email/, ); }); it('should throw an error if a different secret used for decryption', async () => { - const encryptedEmail = await createPwdProtectedEmailAndSubject(email, sharedSecret); + const encryptedEmail = await createPwdProtectedEmail(email, sharedSecret, true); const wrongSecret = 'different secret'; - await expect(decryptPwdProtectedEmailAndSubject(encryptedEmail, wrongSecret)).rejects.toThrowError( + await expect(decryptPwdProtectedEmail(encryptedEmail, wrongSecret)).rejects.toThrowError( /Failed to decrypt password-protect email/, ); });