1- import { HybridEncKey , PwdProtectedKey , PublicKeys , PrivateKeys , EmailBody } from '../types' ;
1+ import { HybridEncKey , PwdProtectedKey , PublicKeys , PrivateKeys , EmailBody , EmailBodyEncrypted } from '../types' ;
22import { genSymmetricCryptoKey , encryptSymmetrically , decryptSymmetrically } from '../symmetric-crypto' ;
3- import { emailBodyToBinary , binaryToEmailBody } from './converters' ;
43import { encapsulateKyber , decapsulateKyber } from '../post-quantum-crypto' ;
54import { deriveWrappingKey , wrapKey , unwrapKey , importWrappingKey } from '../key-wrapper' ;
65import { deriveSecretKey } from '../asymmetric-crypto' ;
@@ -19,8 +18,11 @@ export async function encryptEmailContentSymmetrically(
1918 email : EmailBody ,
2019 aux : Uint8Array ,
2120 emailID : string ,
22- ) : Promise < { enc : Uint8Array ; encryptionKey : CryptoKey } > {
21+ ) : Promise < { enc : EmailBodyEncrypted ; encryptionKey : CryptoKey } > {
2322 try {
23+ if ( ! email . text ) {
24+ throw new Error ( 'Invalid input' ) ;
25+ }
2426 const encryptionKey = await genSymmetricCryptoKey ( ) ;
2527 const enc = await encryptEmailContentSymmetricallyWithKey ( email , encryptionKey , aux , emailID ) ;
2628 return { enc, encryptionKey } ;
@@ -43,13 +45,17 @@ export async function encryptEmailContentAndSubjectSymmetrically(
4345 subject : string ,
4446 aux : Uint8Array ,
4547 emailID : string ,
46- ) : Promise < { enc : Uint8Array ; subjectEnc : Uint8Array ; encryptionKey : CryptoKey } > {
48+ ) : Promise < { enc : EmailBodyEncrypted ; encSubject : string ; encryptionKey : CryptoKey } > {
4749 try {
50+ if ( ! subject || ! email . text ) {
51+ throw new Error ( 'Invalid input' ) ;
52+ }
4853 const encryptionKey = await genSymmetricCryptoKey ( ) ;
4954 const enc = await encryptEmailContentSymmetricallyWithKey ( email , encryptionKey , aux , emailID ) ;
5055 const subjectBuff = UTF8ToUint8 ( subject ) ;
5156 const subjectEnc = await encryptSymmetrically ( encryptionKey , subjectBuff , aux ) ;
52- return { enc, encryptionKey, subjectEnc } ;
57+ const encSubject = uint8ArrayToBase64 ( subjectEnc ) ;
58+ return { enc, encSubject, encryptionKey } ;
5359 } catch ( error ) {
5460 throw new Error ( 'Failed to symmetrically encrypt email and subject' , { cause : error } ) ;
5561 }
@@ -63,16 +69,17 @@ export async function encryptEmailContentAndSubjectSymmetrically(
6369 * @returns The decrypted email
6470 */
6571export async function decryptEmailAndSubjectSymmetrically (
66- emailCiphertext : Uint8Array ,
67- encSubject : Uint8Array ,
6872 encryptionKey : CryptoKey ,
6973 aux : Uint8Array ,
74+ encSubject : string ,
75+ enc : EmailBodyEncrypted ,
7076) : Promise < { body : EmailBody ; subject : string } > {
7177 try {
72- const binaryEmail = await decryptSymmetrically ( encryptionKey , emailCiphertext , aux ) ;
73- const subject = await decryptSymmetrically ( encryptionKey , encSubject , aux ) ;
74- const body = binaryToEmailBody ( binaryEmail ) ;
75- return { body, subject : uint8ToUTF8 ( subject ) } ;
78+ const array = base64ToUint8Array ( encSubject ) ;
79+ const subjectArray = await decryptSymmetrically ( encryptionKey , array , aux ) ;
80+ const body = await decryptEmailSymmetrically ( encryptionKey , aux , enc ) ;
81+ const subject = uint8ToUTF8 ( subjectArray ) ;
82+ return { body, subject } ;
7683 } catch ( error ) {
7784 throw new Error ( 'Failed to symmetrically decrypt email and subject' , { cause : error } ) ;
7885 }
@@ -89,17 +96,61 @@ export async function encryptEmailContentSymmetricallyWithKey(
8996 encryptionKey : CryptoKey ,
9097 aux : Uint8Array ,
9198 emailID : string ,
92- ) : Promise < Uint8Array > {
99+ ) : Promise < EmailBodyEncrypted > {
93100 try {
94101 const freeField = uuidToBytes ( emailID ) ;
95- const binaryEmail = emailBodyToBinary ( emailBody ) ;
96- const ciphertext = await encryptSymmetrically ( encryptionKey , binaryEmail , aux , freeField ) ;
97- return ciphertext ;
102+ const text = UTF8ToUint8 ( emailBody . text ) ;
103+ const encryptedText = await encryptSymmetrically ( encryptionKey , text , aux , freeField ) ;
104+ const encText = uint8ArrayToBase64 ( encryptedText ) ;
105+ const result : EmailBodyEncrypted = { encText } ;
106+
107+ if ( emailBody . attachments ) {
108+ const encryptedAttachements = await encryptEmailAttachements ( emailBody . attachments , encryptionKey , aux , emailID ) ;
109+ result . encAttachments = encryptedAttachements ?. map ( uint8ArrayToBase64 ) ;
110+ }
111+ return result ;
98112 } catch ( error ) {
99113 throw new Error ( 'Failed to symmetrically encrypt email with the given key' , { cause : error } ) ;
100114 }
101115}
102116
117+ async function encryptEmailAttachements (
118+ attachments : string [ ] ,
119+ encryptionKey : CryptoKey ,
120+ aux : Uint8Array ,
121+ emailID : string ,
122+ ) : Promise < Uint8Array [ ] > {
123+ try {
124+ const freeField = uuidToBytes ( emailID ) ;
125+ const encryptedAttachments = await Promise . all (
126+ attachments . map ( ( attachment ) => {
127+ const binaryAttachment = UTF8ToUint8 ( attachment ) ;
128+ return encryptSymmetrically ( encryptionKey , binaryAttachment , aux , freeField ) ;
129+ } ) ,
130+ ) ;
131+ return encryptedAttachments ;
132+ } catch ( error ) {
133+ throw new Error ( 'Failed to symmetrically encrypt email attachements' , { cause : error } ) ;
134+ }
135+ }
136+
137+ async function decryptEmailAttachements (
138+ encryptedAttachments : Uint8Array [ ] ,
139+ encryptionKey : CryptoKey ,
140+ aux : Uint8Array ,
141+ ) : Promise < Uint8Array [ ] > {
142+ try {
143+ const decryptedAttachments = await Promise . all (
144+ encryptedAttachments . map ( ( attachment ) => {
145+ return decryptSymmetrically ( encryptionKey , attachment , aux ) ;
146+ } ) ,
147+ ) ;
148+ return decryptedAttachments ;
149+ } catch ( error ) {
150+ throw new Error ( 'Failed to symmetrically decrypt email attachements' , { cause : error } ) ;
151+ }
152+ }
153+
103154/**
104155 * Decrypts symmetrically encrypted email.
105156 *
@@ -108,14 +159,22 @@ export async function encryptEmailContentSymmetricallyWithKey(
108159 * @returns The decrypted email
109160 */
110161export async function decryptEmailSymmetrically (
111- emailCiphertext : Uint8Array ,
112162 encryptionKey : CryptoKey ,
113163 aux : Uint8Array ,
164+ enc : EmailBodyEncrypted ,
114165) : Promise < EmailBody > {
115166 try {
116- const binaryEmail = await decryptSymmetrically ( encryptionKey , emailCiphertext , aux ) ;
117- const body = binaryToEmailBody ( binaryEmail ) ;
118- return body ;
167+ const cipher = base64ToUint8Array ( enc . encText ) ;
168+ const textArray = await decryptSymmetrically ( encryptionKey , cipher , aux ) ;
169+ const text = uint8ToUTF8 ( textArray ) ;
170+ const result : EmailBody = { text } ;
171+
172+ if ( enc . encAttachments ) {
173+ const encAttachements = enc . encAttachments ?. map ( base64ToUint8Array ) ;
174+ const attachmentsArray = await decryptEmailAttachements ( encAttachements , encryptionKey , aux ) ;
175+ result . attachments = attachmentsArray ?. map ( ( att ) => uint8ToUTF8 ( att ) ) ;
176+ }
177+ return result ;
119178 } catch ( error ) {
120179 throw new Error ( 'Failed to symmetrically decrypt email' , { cause : error } ) ;
121180 }
0 commit comments