From a2f06b12f82bb2ac1c28636b0c606b7c42e740a2 Mon Sep 17 00:00:00 2001 From: tamarafinogina Date: Wed, 15 Jul 2026 14:55:32 +0200 Subject: [PATCH 1/2] encrypt preview and session key too --- src/email-crypto/core.ts | 20 +++++++++++++++++-- src/types.ts | 4 ++++ tests/email-crypto/core.test.ts | 4 ++++ tests/email-crypto/hybridEmail.test.ts | 8 ++++++++ tests/email-crypto/pwdProtectedEmail.test.ts | 4 ++++ .../pwdProtectedEmailCoreErrors.test.ts | 4 ++++ .../pwdProtectedEmailNobleErrors.test.ts | 4 ++++ 7 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/email-crypto/core.ts b/src/email-crypto/core.ts index 9e33e02..f061b2f 100644 --- a/src/email-crypto/core.ts +++ b/src/email-crypto/core.ts @@ -56,10 +56,19 @@ export async function encryptEmailWithKey( ): Promise { try { const text = UTF8ToUint8(email.text); + const preview = UTF8ToUint8(email.preview); const encryptedText = await encryptSymmetrically(encryptionKey, text, aux); const encText = uint8ArrayToBase64(encryptedText); - return { encText }; + + const encryptedPreview = await encryptSymmetrically(encryptionKey, preview, aux); + const encPreview = uint8ArrayToBase64(encryptedPreview); + + + const encryptedAttachmentsSessionKey = await encryptSymmetrically(encryptionKey, email.attachmentsSessionKey, aux); + const encAttachmentsSessionKey = uint8ArrayToBase64(encryptedAttachmentsSessionKey); + + return { encText, encPreview, encAttachmentsSessionKey }; } catch (error) { throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error)); } @@ -83,7 +92,14 @@ export async function decryptEmail( const textArray = await decryptSymmetrically(encryptionKey, encText, aux); const text = uint8ToUTF8(textArray); - return { text }; + const encPreview = base64ToUint8Array(encEmail.encPreview); + const previewArray = await decryptSymmetrically(encryptionKey, encPreview, aux); + const preview = uint8ToUTF8(previewArray); + + const encAttachementSessionKey = base64ToUint8Array(encEmail.encAttachmentsSessionKey); + const attachmentsSessionKey = await decryptSymmetrically(encryptionKey, encAttachementSessionKey, aux); + + return { text, preview, attachmentsSessionKey}; } catch (error) { throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error)); } diff --git a/src/types.ts b/src/types.ts index 82c49ba..523536e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -48,6 +48,8 @@ export type PwdProtectedKey = { export type EmailEncrypted = { encText: string; + encPreview: string; + encAttachmentsSessionKey: string; }; export type EmailAndSubjectEncrypted = EmailEncrypted & { @@ -56,6 +58,8 @@ export type EmailAndSubjectEncrypted = EmailEncrypted & { export type Email = { text: string; + preview: string; + attachmentsSessionKey: Uint8Array; }; export type EmailAndSubject = Email & { diff --git a/tests/email-crypto/core.test.ts b/tests/email-crypto/core.test.ts index 015b34e..23a1bda 100644 --- a/tests/email-crypto/core.test.ts +++ b/tests/email-crypto/core.test.ts @@ -16,11 +16,15 @@ import { EmailSymmetricDecryptionError, InvalidInputEmail } from '../../src/emai describe('Test email crypto functions', () => { const email: Email = { text: 'test email', + preview: 'email preview', + attachmentsSessionKey: new Uint8Array([1,2,3,4,5,6,7,8]), }; const emailAndSubject: EmailAndSubject = { text: 'test email text', subject: 'test email subject', + preview: 'email preview', + attachmentsSessionKey: new Uint8Array([1,2,3,4,5,6,7,8]), }; const aux = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); diff --git a/tests/email-crypto/hybridEmail.test.ts b/tests/email-crypto/hybridEmail.test.ts index d76ccbf..1b8e349 100644 --- a/tests/email-crypto/hybridEmail.test.ts +++ b/tests/email-crypto/hybridEmail.test.ts @@ -27,11 +27,15 @@ import { describe('Test email crypto functions', async () => { const email: Email = { text: 'test email text', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const emailAndSubject: EmailAndSubject = { text: 'test email text', subject: 'test email subject', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const { secretKey: alicePrivateKeys, publicKey: alicePublicKeys } = await generateEmailKeys(); @@ -90,6 +94,8 @@ describe('Test email crypto functions', async () => { encryptedKey: encKey, encEmail: { encText: 'mock encrypted text', + encPreview: 'mock encryped preview', + encAttachmentsSessionKey: 'mock encrypted attachement session key', }, }; @@ -98,6 +104,8 @@ describe('Test email crypto functions', async () => { encEmail: { encText: 'mock encrypted text', encSubject: 'mock encrypted subject', + encPreview: 'mock encryped preview', + encAttachmentsSessionKey: 'mock encrypted attachement session key', }, }; diff --git a/tests/email-crypto/pwdProtectedEmail.test.ts b/tests/email-crypto/pwdProtectedEmail.test.ts index ffadeeb..497428d 100644 --- a/tests/email-crypto/pwdProtectedEmail.test.ts +++ b/tests/email-crypto/pwdProtectedEmail.test.ts @@ -13,11 +13,15 @@ import { Email, EmailAndSubject, PwdProtectedEmail, PwdProtectedEmailAndSubject describe('Test email crypto functions', () => { const email: Email = { text: 'Hi Bob, This is a test message. -Alice.', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const emailAndSubject: EmailAndSubject = { text: 'Hi Bob, This is a test message. -Alice.', subject: 'test subject', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const sharedSecret = 'test shared secret'; diff --git a/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts b/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts index 1a13c65..ab63f5e 100644 --- a/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts +++ b/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts @@ -26,11 +26,15 @@ describe('Test email crypto functions', () => { const email: Email = { text: 'Hi Bob, This is a test message. -Alice.', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const emailAndSubject: EmailAndSubject = { text: 'Hi Bob, This is a test message. -Alice.', subject: 'test subject', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const sharedSecret = 'test shared secret'; diff --git a/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts b/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts index b9d176f..dadb1b1 100644 --- a/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts +++ b/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts @@ -37,11 +37,15 @@ describe('Test email crypto functions', () => { const email: Email = { text: 'Hi Bob, This is a test message. -Alice.', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const emailAndSubject: EmailAndSubject = { text: 'Hi Bob, This is a test message. -Alice.', subject: 'test subject', + preview: 'Hi Bib,', + attachmentsSessionKey: new Uint8Array([1,2,3,4]), }; const sharedSecret = 'test shared secret'; From b77435f89c66e0d88bbc49bd75af9784c42e6a7e Mon Sep 17 00:00:00 2001 From: tamarafinogina Date: Wed, 15 Jul 2026 14:58:13 +0200 Subject: [PATCH 2/2] up the version --- package.json | 2 +- src/email-crypto/core.ts | 3 +-- tests/email-crypto/core.test.ts | 4 ++-- tests/email-crypto/hybridEmail.test.ts | 4 ++-- tests/email-crypto/pwdProtectedEmail.test.ts | 4 ++-- tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts | 4 ++-- tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts | 4 ++-- 7 files changed, 12 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 3bd6699..b64aa34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "internxt-crypto", - "version": "1.5.0", + "version": "1.6.0", "main": "dist/index.js", "types": "dist/index.d.ts", "module": "dist/index.js", diff --git a/src/email-crypto/core.ts b/src/email-crypto/core.ts index f061b2f..a904d4a 100644 --- a/src/email-crypto/core.ts +++ b/src/email-crypto/core.ts @@ -64,7 +64,6 @@ export async function encryptEmailWithKey( const encryptedPreview = await encryptSymmetrically(encryptionKey, preview, aux); const encPreview = uint8ArrayToBase64(encryptedPreview); - const encryptedAttachmentsSessionKey = await encryptSymmetrically(encryptionKey, email.attachmentsSessionKey, aux); const encAttachmentsSessionKey = uint8ArrayToBase64(encryptedAttachmentsSessionKey); @@ -99,7 +98,7 @@ export async function decryptEmail( const encAttachementSessionKey = base64ToUint8Array(encEmail.encAttachmentsSessionKey); const attachmentsSessionKey = await decryptSymmetrically(encryptionKey, encAttachementSessionKey, aux); - return { text, preview, attachmentsSessionKey}; + return { text, preview, attachmentsSessionKey }; } catch (error) { throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error)); } diff --git a/tests/email-crypto/core.test.ts b/tests/email-crypto/core.test.ts index 23a1bda..7d075db 100644 --- a/tests/email-crypto/core.test.ts +++ b/tests/email-crypto/core.test.ts @@ -17,14 +17,14 @@ describe('Test email crypto functions', () => { const email: Email = { text: 'test email', preview: 'email preview', - attachmentsSessionKey: new Uint8Array([1,2,3,4,5,6,7,8]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]), }; const emailAndSubject: EmailAndSubject = { text: 'test email text', subject: 'test email subject', preview: 'email preview', - attachmentsSessionKey: new Uint8Array([1,2,3,4,5,6,7,8]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]), }; const aux = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); diff --git a/tests/email-crypto/hybridEmail.test.ts b/tests/email-crypto/hybridEmail.test.ts index 1b8e349..6ff45c7 100644 --- a/tests/email-crypto/hybridEmail.test.ts +++ b/tests/email-crypto/hybridEmail.test.ts @@ -28,14 +28,14 @@ describe('Test email crypto functions', async () => { const email: Email = { text: 'test email text', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const emailAndSubject: EmailAndSubject = { text: 'test email text', subject: 'test email subject', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const { secretKey: alicePrivateKeys, publicKey: alicePublicKeys } = await generateEmailKeys(); diff --git a/tests/email-crypto/pwdProtectedEmail.test.ts b/tests/email-crypto/pwdProtectedEmail.test.ts index 497428d..57f28c6 100644 --- a/tests/email-crypto/pwdProtectedEmail.test.ts +++ b/tests/email-crypto/pwdProtectedEmail.test.ts @@ -14,14 +14,14 @@ describe('Test email crypto functions', () => { const email: Email = { text: 'Hi Bob, This is a test message. -Alice.', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const emailAndSubject: EmailAndSubject = { text: 'Hi Bob, This is a test message. -Alice.', subject: 'test subject', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const sharedSecret = 'test shared secret'; diff --git a/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts b/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts index ab63f5e..4c4b049 100644 --- a/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts +++ b/tests/email-crypto/pwdProtectedEmailCoreErrors.test.ts @@ -27,14 +27,14 @@ describe('Test email crypto functions', () => { const email: Email = { text: 'Hi Bob, This is a test message. -Alice.', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const emailAndSubject: EmailAndSubject = { text: 'Hi Bob, This is a test message. -Alice.', subject: 'test subject', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const sharedSecret = 'test shared secret'; diff --git a/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts b/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts index dadb1b1..3cab3d7 100644 --- a/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts +++ b/tests/email-crypto/pwdProtectedEmailNobleErrors.test.ts @@ -38,14 +38,14 @@ describe('Test email crypto functions', () => { const email: Email = { text: 'Hi Bob, This is a test message. -Alice.', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const emailAndSubject: EmailAndSubject = { text: 'Hi Bob, This is a test message. -Alice.', subject: 'test subject', preview: 'Hi Bib,', - attachmentsSessionKey: new Uint8Array([1,2,3,4]), + attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]), }; const sharedSecret = 'test shared secret';