internxt-crypto 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -7
- package/dist/{email-crypto-Di814Pbk.js → email-crypto-CLlLUgA6.js} +30 -98
- package/dist/email-crypto-CLlLUgA6.js.map +1 -0
- package/dist/{email-crypto-DpjfXDes.mjs → email-crypto-v_-nFOl2.mjs} +31 -87
- package/dist/email-crypto-v_-nFOl2.mjs.map +1 -0
- package/dist/email-crypto.d.mts +17 -27
- package/dist/email-crypto.d.mts.map +1 -1
- package/dist/email-crypto.d.ts +17 -27
- package/dist/email-crypto.d.ts.map +1 -1
- package/dist/email-crypto.js +1 -3
- package/dist/email-crypto.mjs +2 -2
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -3
- package/dist/index.mjs +2 -2
- package/dist/keystore-crypto.d.mts +1 -1
- package/dist/keystore-crypto.d.ts +1 -1
- package/dist/{types-CrAiKyb0.d.ts → types-D1R4_m5r.d.mts} +6 -12
- package/dist/types-D1R4_m5r.d.mts.map +1 -0
- package/dist/{types-D6hsUfma.d.mts → types-Y0wL7HUH.d.ts} +6 -12
- package/dist/types-Y0wL7HUH.d.ts.map +1 -0
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/types.js.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/email-crypto-Di814Pbk.js.map +0 -1
- package/dist/email-crypto-DpjfXDes.mjs.map +0 -1
- package/dist/types-CrAiKyb0.d.ts.map +0 -1
- package/dist/types-D6hsUfma.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -109,26 +109,28 @@ const { key, salt } = await getKeyFromPassword(password);
|
|
|
109
109
|
// Hybrid email encryption
|
|
110
110
|
const email: Email = {
|
|
111
111
|
text: 'email text',
|
|
112
|
-
|
|
112
|
+
preview: 'email preview',
|
|
113
|
+
attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]),
|
|
113
114
|
};
|
|
114
115
|
const { secretKey: bobPrivateKeys, publicKey: bobPublicKeys } = await generateEmailKeys();
|
|
115
116
|
const bobWithPublicKeys = {
|
|
116
117
|
email: 'bob email',
|
|
117
118
|
publicHybridKey: bobPublicKeys,
|
|
118
119
|
};
|
|
119
|
-
const
|
|
120
|
-
const decryptedEmail = await decryptEmailHybrid(
|
|
120
|
+
const {encryptedKeys, encEmail} = await encryptEmailHybridForMultipleRecipients(email, [bobWithPublicKeys]);
|
|
121
|
+
const decryptedEmail = await decryptEmailHybrid(encEmail, encryptedKeys[0], bobPrivateKeys);
|
|
121
122
|
|
|
122
123
|
expect(decryptedEmail).toStrictEqual(email);
|
|
123
124
|
|
|
124
125
|
// Hybrid email and subject encryption
|
|
125
126
|
const emailAndSubject: EmailAndSubject = {
|
|
126
127
|
text: 'email text',
|
|
127
|
-
subject: 'email subject'
|
|
128
|
-
|
|
128
|
+
subject: 'email subject',
|
|
129
|
+
preview: 'email preview,',
|
|
130
|
+
attachmentsSessionKey: new Uint8Array([1, 2, 3, 4]),
|
|
129
131
|
};
|
|
130
|
-
const
|
|
131
|
-
const decryptedEmailAndSubject = await decryptEmailAndSubjectHybrid(
|
|
132
|
+
const {encryptedKeys, encEmail} = await encryptEmailAndSubjectHybridForMultipleRecipients(emailAndSubject, [bobWithPublicKeys]);
|
|
133
|
+
const decryptedEmailAndSubject = await decryptEmailAndSubjectHybrid(encEmail, encryptedKeys[0], bobPrivateKeys);
|
|
132
134
|
|
|
133
135
|
expect(encryptedEmailAndSubject.encEmail.encSubject).not.toBe(emailAndSubject.subject);
|
|
134
136
|
expect(decryptedEmailAndSubject).toStrictEqual(emailAndSubject);
|
|
@@ -114,14 +114,13 @@ async function encryptEmail(email, aux) {
|
|
|
114
114
|
*/
|
|
115
115
|
async function encryptEmailWithKey(email, encryptionKey, aux) {
|
|
116
116
|
try {
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
return enc;
|
|
117
|
+
const text = require_utils.UTF8ToUint8(email.text);
|
|
118
|
+
const preview = require_utils.UTF8ToUint8(email.preview);
|
|
119
|
+
return {
|
|
120
|
+
encText: require_utils.uint8ArrayToBase64(await require_symmetric_crypto.encryptSymmetrically(encryptionKey, text, aux)),
|
|
121
|
+
encPreview: require_utils.uint8ArrayToBase64(await require_symmetric_crypto.encryptSymmetrically(encryptionKey, preview, aux)),
|
|
122
|
+
encAttachmentsSessionKey: require_utils.uint8ArrayToBase64(await require_symmetric_crypto.encryptSymmetrically(encryptionKey, email.attachmentsSessionKey, aux))
|
|
123
|
+
};
|
|
125
124
|
} catch (error) {
|
|
126
125
|
throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
|
|
127
126
|
}
|
|
@@ -136,12 +135,11 @@ async function encryptEmailWithKey(email, encryptionKey, aux) {
|
|
|
136
135
|
*/
|
|
137
136
|
async function decryptEmail(encEmail, encryptionKey, aux) {
|
|
138
137
|
try {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
return email;
|
|
138
|
+
return {
|
|
139
|
+
text: require_utils.uint8ToUTF8(await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmail.encText), aux)),
|
|
140
|
+
preview: require_utils.uint8ToUTF8(await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmail.encPreview), aux)),
|
|
141
|
+
attachmentsSessionKey: await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmail.encAttachmentsSessionKey), aux)
|
|
142
|
+
};
|
|
145
143
|
} catch (error) {
|
|
146
144
|
throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));
|
|
147
145
|
}
|
|
@@ -218,48 +216,21 @@ async function removePasswordProtection(emailEncryptionKey, password) {
|
|
|
218
216
|
//#endregion
|
|
219
217
|
//#region src/email-crypto/hybridEncyptedEmail.ts
|
|
220
218
|
/**
|
|
221
|
-
* Encrypts the email using hybrid encryption.
|
|
222
|
-
*
|
|
223
|
-
* @param email - The email to encrypt.
|
|
224
|
-
* @param recipientPublicKeys - The public keys of the recipient.
|
|
225
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
226
|
-
* @returns The encrypted email
|
|
227
|
-
*/
|
|
228
|
-
async function encryptEmailHybrid(email, recipient, aux) {
|
|
229
|
-
try {
|
|
230
|
-
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
231
|
-
return {
|
|
232
|
-
encEmail,
|
|
233
|
-
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
234
|
-
};
|
|
235
|
-
} catch (error) {
|
|
236
|
-
if (error instanceof InvalidInputEmail) throw error;
|
|
237
|
-
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
238
|
-
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
239
|
-
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
219
|
* Encrypts the email using hybrid encryption for multiple recipients.
|
|
244
220
|
*
|
|
245
221
|
* @param email - The email to encrypt for multiple recipients.
|
|
246
222
|
* @param recipients - The recipients with corresponding public keys.
|
|
247
223
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
248
|
-
* @returns The set of encrypted
|
|
224
|
+
* @returns The set of encrypted keys (one per user) and encrypted email
|
|
249
225
|
*/
|
|
250
226
|
async function encryptEmailHybridForMultipleRecipients(email, recipients, aux) {
|
|
251
227
|
try {
|
|
252
228
|
if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
|
|
253
229
|
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
encEmail,
|
|
259
|
-
encryptedKey
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
return encryptedEmails;
|
|
230
|
+
return {
|
|
231
|
+
encryptedKeys: await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient))),
|
|
232
|
+
encEmail
|
|
233
|
+
};
|
|
263
234
|
} catch (error) {
|
|
264
235
|
if (error instanceof InvalidInputEmail) throw error;
|
|
265
236
|
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
@@ -271,14 +242,14 @@ async function encryptEmailHybridForMultipleRecipients(email, recipients, aux) {
|
|
|
271
242
|
* Decrypts the email using hybrid encryption.
|
|
272
243
|
*
|
|
273
244
|
* @param encEmail - The encrypted email.
|
|
245
|
+
* @param encryptedKey - The encrypted key for this recipient.
|
|
274
246
|
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
275
247
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
276
248
|
* @returns The decrypted email
|
|
277
249
|
*/
|
|
278
|
-
async function decryptEmailHybrid(encEmail, recipientPrivateHybridKeys, aux) {
|
|
250
|
+
async function decryptEmailHybrid(encEmail, encryptedKey, recipientPrivateHybridKeys, aux) {
|
|
279
251
|
try {
|
|
280
|
-
|
|
281
|
-
return await decryptEmail(encEmail.encEmail, encryptionKey, aux);
|
|
252
|
+
return await decryptEmail(encEmail, await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys), aux);
|
|
282
253
|
} catch (error) {
|
|
283
254
|
if (error instanceof EmailHybridDecryptionError) throw error;
|
|
284
255
|
if (error instanceof EmailSymmetricDecryptionError) throw error;
|
|
@@ -391,48 +362,21 @@ async function decryptEmailAndSubject(encEmail, encryptionKey, aux) {
|
|
|
391
362
|
//#endregion
|
|
392
363
|
//#region src/email-crypto/hybridEncryptedEmailAndSubject.ts
|
|
393
364
|
/**
|
|
394
|
-
* Encrypts the email and its subject using hybrid encryption.
|
|
395
|
-
*
|
|
396
|
-
* @param email - The email and subject to encrypt.
|
|
397
|
-
* @param recipientPublicKeys - The public keys of the recipient.
|
|
398
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
399
|
-
* @returns The encrypted email and subject
|
|
400
|
-
*/
|
|
401
|
-
async function encryptEmailAndSubjectHybrid(email, recipient, aux) {
|
|
402
|
-
try {
|
|
403
|
-
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
404
|
-
return {
|
|
405
|
-
encEmail,
|
|
406
|
-
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
407
|
-
};
|
|
408
|
-
} catch (error) {
|
|
409
|
-
if (error instanceof InvalidInputEmail) throw error;
|
|
410
|
-
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
411
|
-
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
412
|
-
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
365
|
* Encrypts the email and its subject using hybrid encryption for multiple recipients.
|
|
417
366
|
*
|
|
418
367
|
* @param email - The email and subject to encrypt for multiple recipients.
|
|
419
368
|
* @param recipients - The recipients with corresponding public keys.
|
|
420
369
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
421
|
-
* @returns The set of encrypted
|
|
370
|
+
* @returns The set of encrypted keys and encrypted email and subject
|
|
422
371
|
*/
|
|
423
372
|
async function encryptEmailAndSubjectHybridForMultipleRecipients(email, recipients, aux) {
|
|
424
373
|
try {
|
|
425
374
|
if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
|
|
426
375
|
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
encEmail,
|
|
432
|
-
encryptedKey
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
return encryptedEmails;
|
|
376
|
+
return {
|
|
377
|
+
encEmail,
|
|
378
|
+
encryptedKeys: await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient)))
|
|
379
|
+
};
|
|
436
380
|
} catch (error) {
|
|
437
381
|
if (error instanceof InvalidInputEmail) throw error;
|
|
438
382
|
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
@@ -443,15 +387,15 @@ async function encryptEmailAndSubjectHybridForMultipleRecipients(email, recipien
|
|
|
443
387
|
/**
|
|
444
388
|
* Decrypts the email and its subject using hybrid encryption.
|
|
445
389
|
*
|
|
446
|
-
* @param
|
|
390
|
+
* @param encEmail - The encrypted email and subject.
|
|
391
|
+
* @param encryptedKey - The encrypted key for this recipient.
|
|
447
392
|
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
448
393
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
449
394
|
* @returns The decrypted email and subject
|
|
450
395
|
*/
|
|
451
|
-
async function decryptEmailAndSubjectHybrid(
|
|
396
|
+
async function decryptEmailAndSubjectHybrid(encEmail, encryptedKey, recipientPrivateHybridKeys, aux) {
|
|
452
397
|
try {
|
|
453
|
-
|
|
454
|
-
return await decryptEmailAndSubject(hybridEmail.encEmail, encryptionKey, aux);
|
|
398
|
+
return await decryptEmailAndSubject(encEmail, await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys), aux);
|
|
455
399
|
} catch (error) {
|
|
456
400
|
if (error instanceof InvalidInputEmail) throw error;
|
|
457
401
|
if (error instanceof EmailHybridDecryptionError) throw error;
|
|
@@ -663,12 +607,6 @@ Object.defineProperty(exports, "encryptEmailAndSubject", {
|
|
|
663
607
|
return encryptEmailAndSubject;
|
|
664
608
|
}
|
|
665
609
|
});
|
|
666
|
-
Object.defineProperty(exports, "encryptEmailAndSubjectHybrid", {
|
|
667
|
-
enumerable: true,
|
|
668
|
-
get: function() {
|
|
669
|
-
return encryptEmailAndSubjectHybrid;
|
|
670
|
-
}
|
|
671
|
-
});
|
|
672
610
|
Object.defineProperty(exports, "encryptEmailAndSubjectHybridForMultipleRecipients", {
|
|
673
611
|
enumerable: true,
|
|
674
612
|
get: function() {
|
|
@@ -681,12 +619,6 @@ Object.defineProperty(exports, "encryptEmailAndSubjectWithKey", {
|
|
|
681
619
|
return encryptEmailAndSubjectWithKey;
|
|
682
620
|
}
|
|
683
621
|
});
|
|
684
|
-
Object.defineProperty(exports, "encryptEmailHybrid", {
|
|
685
|
-
enumerable: true,
|
|
686
|
-
get: function() {
|
|
687
|
-
return encryptEmailHybrid;
|
|
688
|
-
}
|
|
689
|
-
});
|
|
690
622
|
Object.defineProperty(exports, "encryptEmailHybridForMultipleRecipients", {
|
|
691
623
|
enumerable: true,
|
|
692
624
|
get: function() {
|
|
@@ -736,4 +668,4 @@ Object.defineProperty(exports, "wrapKey", {
|
|
|
736
668
|
}
|
|
737
669
|
});
|
|
738
670
|
|
|
739
|
-
//# sourceMappingURL=email-crypto-
|
|
671
|
+
//# sourceMappingURL=email-crypto-CLlLUgA6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-crypto-CLlLUgA6.js","names":["genSymmetricKey","UTF8ToUint8","uint8ArrayToBase64","encryptSymmetrically","uint8ToUTF8","decryptSymmetrically","base64ToUint8Array","encapsulateHybrid","decapsulateHybrid","getKeyFromPassword","getKeyFromPasswordAndSalt","genSymmetricKey","uint8ArrayToBase64","encryptSymmetrically","UTF8ToUint8","uint8ToUTF8","decryptSymmetrically","base64ToUint8Array","genHybridKeys","deriveKeyFromMnemonic","CONTEXT_DATABASE","CONTEXT_DRAFT"],"sources":["../src/key-wrapper/aesWrapper.ts","../src/email-crypto/errors.ts","../src/email-crypto/core.ts","../src/email-crypto/hybridEncyptedEmail.ts","../src/email-crypto/pwdProtectedEmail.ts","../src/email-crypto/coreSubject.ts","../src/email-crypto/hybridEncryptedEmailAndSubject.ts","../src/email-crypto/pwdProtectedEmailAndSubject.ts","../src/email-crypto/emailKeys.ts"],"sourcesContent":["import { aeskw } from '@noble/ciphers/aes.js';\n\n/**\n * Unwraps the given wrapped key\n *\n * @param encryptedKey - The wrapped key\n * @param wrappingKey - The secret key used for unwrapping\n * @returns The resulting key\n */\nexport async function unwrapKey(encryptedKey: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).decrypt(encryptedKey);\n}\n\n/**\n * Wraps the given key\n *\n * @param key - The key to be wrapped\n * @param wrappingKey - The secret key used for wrapping\n * @returns The resulting ciphertext\n */\nexport async function wrapKey(key: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).encrypt(key);\n}\n","export class FailedToEncryptEmail extends Error {\n constructor(errorMsg?: string) {\n super('Failed to encrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToEncryptEmail.prototype);\n }\n}\n\nexport class EmailSymmetricEncryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to symmetrically encrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailSymmetricEncryptionError.prototype);\n }\n}\n\nexport class EmailSymmetricDecryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to symmetrically decrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailSymmetricDecryptionError.prototype);\n }\n}\n\nexport class EmailHybridEncryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to hybridly encrypt the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailHybridEncryptionError.prototype);\n }\n}\n\nexport class EmailHybridDecryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to hybridly decrypt the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailHybridDecryptionError.prototype);\n }\n}\n\nexport class EmailPasswordProtectError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to password-protect the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailPasswordProtectError.prototype);\n }\n}\n\nexport class EmailPasswordOpenError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open password-protected key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailPasswordOpenError.prototype);\n }\n}\n\nexport class InvalidInputEmail extends Error {\n constructor() {\n super('Invalid input');\n\n Object.setPrototypeOf(this, InvalidInputEmail.prototype);\n }\n}\n\nexport class FailedToDecryptEmail extends Error {\n constructor(errorMsg?: string) {\n super('Failed to decrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToDecryptEmail.prototype);\n }\n}\n","import { HybridEncKey, PwdProtectedKey, Email, RecipientWithPublicKey, EmailEncrypted } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encapsulateHybrid, decapsulateHybrid } from '../hybrid-crypto';\nimport { wrapKey, unwrapKey } from '../key-wrapper';\nimport { getKeyFromPassword, getKeyFromPasswordAndSalt } from '../derive-password';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\nimport {\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n EmailPasswordOpenError,\n EmailPasswordProtectError,\n} from './errors';\n\n/**\n * Symmetrically encrypts email.\n *\n * @param email - The email to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmail(\n email: Email,\n aux?: Uint8Array,\n): Promise<{\n encEmail: EmailEncrypted;\n encryptionKey: Uint8Array;\n}> {\n if (!email.text) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = genSymmetricKey();\n const encEmail = await encryptEmailWithKey(email, encryptionKey, aux);\n\n return { encEmail, encryptionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Symmetrically encrypts email with the given key.\n *\n * @param email - The email to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailWithKey(\n email: Email,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailEncrypted> {\n try {\n const text = UTF8ToUint8(email.text);\n const preview = UTF8ToUint8(email.preview);\n\n const encryptedText = await encryptSymmetrically(encryptionKey, text, aux);\n const encText = uint8ArrayToBase64(encryptedText);\n\n const encryptedPreview = await encryptSymmetrically(encryptionKey, preview, aux);\n const encPreview = uint8ArrayToBase64(encryptedPreview);\n\n const encryptedAttachmentsSessionKey = await encryptSymmetrically(encryptionKey, email.attachmentsSessionKey, aux);\n const encAttachmentsSessionKey = uint8ArrayToBase64(encryptedAttachmentsSessionKey);\n\n return { encText, encPreview, encAttachmentsSessionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email.\n *\n * @param encEmail - The email to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email\n */\nexport async function decryptEmail(\n encEmail: EmailEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<Email> {\n try {\n const encText = base64ToUint8Array(encEmail.encText);\n const textArray = await decryptSymmetrically(encryptionKey, encText, aux);\n const text = uint8ToUTF8(textArray);\n\n const encPreview = base64ToUint8Array(encEmail.encPreview);\n const previewArray = await decryptSymmetrically(encryptionKey, encPreview, aux);\n const preview = uint8ToUTF8(previewArray);\n\n const encAttachementSessionKey = base64ToUint8Array(encEmail.encAttachmentsSessionKey);\n const attachmentsSessionKey = await decryptSymmetrically(encryptionKey, encAttachementSessionKey, aux);\n\n return { text, preview, attachmentsSessionKey };\n } catch (error) {\n throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Encrypts the email symmetric key using hybrid encryption.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param recipient - The recipient with a public hybrid key.\n * @returns The encrypted email symmetric key\n */\nexport async function encryptKeysHybrid(\n emailEncryptionKey: Uint8Array,\n recipient: RecipientWithPublicKey,\n): Promise<HybridEncKey> {\n try {\n const { cipherText, sharedSecret } = encapsulateHybrid(recipient.publicHybridKey);\n const encryptedKey = await wrapKey(emailEncryptionKey, sharedSecret);\n const encryptedKeyBase64 = uint8ArrayToBase64(encryptedKey);\n const kyberCiphertextBase64 = uint8ArrayToBase64(cipherText);\n\n return {\n encryptedKey: encryptedKeyBase64,\n hybridCiphertext: kyberCiphertextBase64,\n encryptedForEmail: recipient.email,\n };\n } catch (error) {\n throw new EmailHybridEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email symmetric key encrypted via hybrid encryption.\n *\n * @param encryptedKey - The encrypted email key.\n * @param recipientPrivateKey - The private key of the recipient.\n * @returns The email encryption key\n */\nexport async function decryptKeysHybrid(\n encryptedKey: HybridEncKey,\n recipientPrivateKey: Uint8Array,\n): Promise<Uint8Array> {\n try {\n const kyberCiphertext = base64ToUint8Array(encryptedKey.hybridCiphertext);\n const encKey = base64ToUint8Array(encryptedKey.encryptedKey);\n const sharedSecret = decapsulateHybrid(kyberCiphertext, recipientPrivateKey);\n const encryptionKey = await unwrapKey(encKey, sharedSecret);\n return encryptionKey;\n } catch (error) {\n throw new EmailHybridDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Password-protects the email symmetric key.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param password - The secret password for key protection.\n * @returns The password-protected email symmetric key\n */\nexport async function passwordProtectKey(emailEncryptionKey: Uint8Array, password: string): Promise<PwdProtectedKey> {\n try {\n const { key, salt } = await getKeyFromPassword(password);\n const encryptedKey = await wrapKey(emailEncryptionKey, key);\n const saltStr = uint8ArrayToBase64(salt);\n const encryptedKeyStr = uint8ArrayToBase64(encryptedKey);\n return { encryptedKey: encryptedKeyStr, salt: saltStr };\n } catch (error) {\n throw new EmailPasswordProtectError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Removes passoword-protection and exposes the email symmetric key.\n *\n * @param emailEncryptionKey - The password-protected email key.\n * @param password - The secret password for key protection.\n * @returns The email encryption key\n */\nexport async function removePasswordProtection(\n emailEncryptionKey: PwdProtectedKey,\n password: string,\n): Promise<Uint8Array> {\n try {\n const salt = base64ToUint8Array(emailEncryptionKey.salt);\n const encryptedKey = base64ToUint8Array(emailEncryptionKey.encryptedKey);\n const key = await getKeyFromPasswordAndSalt(password, salt);\n const encryptionKey = await unwrapKey(encryptedKey, key);\n return encryptionKey;\n } catch (error) {\n throw new EmailPasswordOpenError(error instanceof Error ? error.message : String(error));\n }\n}\n","import { Email, RecipientWithPublicKey, HybridEncKey, EmailEncrypted } from '../types';\nimport { decryptEmail, encryptKeysHybrid, decryptKeysHybrid, encryptEmail } from './core';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Encrypts the email using hybrid encryption for multiple recipients.\n *\n * @param email - The email to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted keys (one per user) and encrypted email\n */\nexport async function encryptEmailHybridForMultipleRecipients(\n email: Email,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<{ encryptedKeys: HybridEncKey[]; encEmail: EmailEncrypted }> {\n try {\n if (!recipients || recipients.length === 0) {\n throw new InvalidInputEmail();\n }\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n\n const encryptedKeys = await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient)));\n\n return { encryptedKeys, encEmail };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email using hybrid encryption.\n *\n * @param encEmail - The encrypted email.\n * @param encryptedKey - The encrypted key for this recipient.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email\n */\nexport async function decryptEmailHybrid(\n encEmail: EmailEncrypted,\n encryptedKey: HybridEncKey,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<Email> {\n try {\n const encryptionKey = await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys);\n return await decryptEmail(encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailHybridDecryptionError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { PwdProtectedEmail, Email } from '../types';\nimport { decryptEmail, passwordProtectKey, removePasswordProtection, encryptEmail } from './core';\nimport {\n EmailSymmetricEncryptionError,\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailPasswordProtectError,\n EmailSymmetricDecryptionError,\n EmailPasswordOpenError,\n InvalidInputEmail,\n} from './errors';\n/**\n * Creates a password-protected email.\n *\n * @param email - The email to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmail(\n email: Email,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmail> {\n try {\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailPasswordProtectError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens a password-protected email.\n *\n * @param encryptedEmail - The encrypted email\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email\n */\nexport async function decryptPwdProtectedEmail(\n encryptedEmail: PwdProtectedEmail,\n password: string,\n aux?: Uint8Array,\n): Promise<Email> {\n if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n return await decryptEmail(encryptedEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailPasswordOpenError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { EmailAndSubject, EmailAndSubjectEncrypted } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encryptEmailWithKey, decryptEmail } from './core';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\nimport { InvalidInputEmail, EmailSymmetricDecryptionError, EmailSymmetricEncryptionError } from './errors';\n\n/**\n * Symmetrically encrypts email and subject.\n *\n * @param email - The email and subject to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailAndSubject(\n email: EmailAndSubject,\n aux?: Uint8Array,\n): Promise<{\n encEmail: EmailAndSubjectEncrypted;\n encryptionKey: Uint8Array;\n}> {\n if (!email.text || !email.subject) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = genSymmetricKey();\n const encEmail = await encryptEmailAndSubjectWithKey(email, encryptionKey, aux);\n\n return { encEmail, encryptionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Symmetrically encrypts email and subject with the given key.\n *\n * @param email - The email and subject to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailAndSubjectWithKey(\n email: EmailAndSubject,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubjectEncrypted> {\n try {\n const enc = await encryptEmailWithKey(email, encryptionKey, aux);\n const subject = UTF8ToUint8(email.subject);\n const subjectEnc = await encryptSymmetrically(encryptionKey, subject, aux);\n const encSubject = uint8ArrayToBase64(subjectEnc);\n\n return { ...enc, encSubject };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email and email subject.\n *\n * @param encEmail - The encrypted email and subject to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email and subject\n */\nexport async function decryptEmailAndSubject(\n encEmail: EmailAndSubjectEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n try {\n const encSubject = base64ToUint8Array(encEmail.encSubject);\n const subjectArray = await decryptSymmetrically(encryptionKey, encSubject, aux);\n const subject = uint8ToUTF8(subjectArray);\n const email = await decryptEmail(encEmail, encryptionKey, aux);\n\n return { ...email, subject };\n } catch (error) {\n throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n","import { RecipientWithPublicKey, EmailAndSubject, EmailAndSubjectEncrypted, HybridEncKey } from '../types';\nimport { encryptKeysHybrid, decryptKeysHybrid } from './core';\nimport { encryptEmailAndSubject, decryptEmailAndSubject } from './coreSubject';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Encrypts the email and its subject using hybrid encryption for multiple recipients.\n *\n * @param email - The email and subject to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted keys and encrypted email and subject\n */\nexport async function encryptEmailAndSubjectHybridForMultipleRecipients(\n email: EmailAndSubject,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<{ encryptedKeys: HybridEncKey[]; encEmail: EmailAndSubjectEncrypted }> {\n try {\n if (!recipients || recipients.length === 0) {\n throw new InvalidInputEmail();\n }\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n\n const encryptedKeys = await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient)));\n\n return { encEmail, encryptedKeys };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email and its subject using hybrid encryption.\n *\n * @param encEmail - The encrypted email and subject.\n * @param encryptedKey - The encrypted key for this recipient.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email and subject\n */\nexport async function decryptEmailAndSubjectHybrid(\n encEmail: EmailAndSubjectEncrypted,\n encryptedKey: HybridEncKey,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n try {\n const encryptionKey = await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys);\n return await decryptEmailAndSubject(encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailHybridDecryptionError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { EmailAndSubject, PwdProtectedEmailAndSubject } from '../types';\nimport { passwordProtectKey, removePasswordProtection } from './core';\nimport { encryptEmailAndSubject, decryptEmailAndSubject } from './coreSubject';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n InvalidInputEmail,\n EmailPasswordOpenError,\n EmailPasswordProtectError,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Creates a password-protected email and subject.\n *\n * @param email - The email and subject to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmailAndSubject(\n email: EmailAndSubject,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmailAndSubject> {\n try {\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailPasswordProtectError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens a password-protected email and subject.\n *\n * @param encryptedEmail - The encrypted email and subject\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email and subject\n */\nexport async function decryptPwdProtectedEmailAndSubject(\n encryptedEmail: PwdProtectedEmailAndSubject,\n password: string,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n return await decryptEmailAndSubject(encryptedEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailPasswordOpenError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { genHybridKeys } from '../hybrid-crypto';\nimport { HybridKeyPair } from '../types';\nimport { deriveKeyFromMnemonic } from '../derive-key';\nimport { CONTEXT_DATABASE, CONTEXT_DRAFT } from '../constants';\n\n/**\n * Generates public and private keys for email encryption.\n *\n * @returns The user's private and public keys\n */\nexport async function generateEmailKeys(): Promise<HybridKeyPair> {\n return genHybridKeys();\n}\n\n/**\n * Derives database encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting database\n */\nexport const deriveDatabaseKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DATABASE);\n};\n\n/**\n * Derives email draft encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting email drafts\n */\nexport const deriveEmailDraftKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);\n};\n"],"mappings":";;;;;;;;;;;;;;;;AASA,eAAsB,UAAU,cAA0B,aAA8C;AACtG,SAAA,GAAA,sBAAA,OAAa,YAAY,CAAC,QAAQ,aAAa;;;;;;;;;AAUjD,eAAsB,QAAQ,KAAiB,aAA8C;AAC3F,SAAA,GAAA,sBAAA,OAAa,YAAY,CAAC,QAAQ,IAAI;;;;ACrBxC,IAAa,uBAAb,MAAa,6BAA6B,MAAM;CAC9C,YAAY,UAAmB;AAC7B,QAAM,8BAA8B,SAAS;AAE7C,SAAO,eAAe,MAAM,qBAAqB,UAAU;;;AAI/D,IAAa,gCAAb,MAAa,sCAAsC,MAAM;CACvD,YAAY,UAAmB;AAC7B,QAAM,4CAA4C,SAAS;AAE3D,SAAO,eAAe,MAAM,8BAA8B,UAAU;;;AAIxE,IAAa,gCAAb,MAAa,sCAAsC,MAAM;CACv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ACnD,MAAI,iBAAiB,8BAA+B,OAAM;AAC1D,QAAM,IAAI,qBAAqB,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,CAAC;;;;;;;;;;ACnD1F,eAAsB,oBAA4C;AAChE,QAAOC,cAAAA,eAAe;;;;;;;;AASxB,MAAa,oBAAoB,OAAO,aAA0C;AAChF,QAAOC,mBAAAA,sBAAsB,UAAUC,kBAAAA,iBAAiB;;;;;;;;AAS1D,MAAa,sBAAsB,OAAO,aAA0C;AAClF,QAAOD,mBAAAA,sBAAsB,UAAUE,kBAAAA,cAAc"}
|
|
@@ -113,14 +113,13 @@ async function encryptEmail(email, aux) {
|
|
|
113
113
|
*/
|
|
114
114
|
async function encryptEmailWithKey(email, encryptionKey, aux) {
|
|
115
115
|
try {
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
return enc;
|
|
116
|
+
const text = UTF8ToUint8(email.text);
|
|
117
|
+
const preview = UTF8ToUint8(email.preview);
|
|
118
|
+
return {
|
|
119
|
+
encText: uint8ArrayToBase64(await encryptSymmetrically(encryptionKey, text, aux)),
|
|
120
|
+
encPreview: uint8ArrayToBase64(await encryptSymmetrically(encryptionKey, preview, aux)),
|
|
121
|
+
encAttachmentsSessionKey: uint8ArrayToBase64(await encryptSymmetrically(encryptionKey, email.attachmentsSessionKey, aux))
|
|
122
|
+
};
|
|
124
123
|
} catch (error) {
|
|
125
124
|
throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
|
|
126
125
|
}
|
|
@@ -135,12 +134,11 @@ async function encryptEmailWithKey(email, encryptionKey, aux) {
|
|
|
135
134
|
*/
|
|
136
135
|
async function decryptEmail(encEmail, encryptionKey, aux) {
|
|
137
136
|
try {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
return email;
|
|
137
|
+
return {
|
|
138
|
+
text: uint8ToUTF8(await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmail.encText), aux)),
|
|
139
|
+
preview: uint8ToUTF8(await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmail.encPreview), aux)),
|
|
140
|
+
attachmentsSessionKey: await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmail.encAttachmentsSessionKey), aux)
|
|
141
|
+
};
|
|
144
142
|
} catch (error) {
|
|
145
143
|
throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));
|
|
146
144
|
}
|
|
@@ -217,48 +215,21 @@ async function removePasswordProtection(emailEncryptionKey, password) {
|
|
|
217
215
|
//#endregion
|
|
218
216
|
//#region src/email-crypto/hybridEncyptedEmail.ts
|
|
219
217
|
/**
|
|
220
|
-
* Encrypts the email using hybrid encryption.
|
|
221
|
-
*
|
|
222
|
-
* @param email - The email to encrypt.
|
|
223
|
-
* @param recipientPublicKeys - The public keys of the recipient.
|
|
224
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
225
|
-
* @returns The encrypted email
|
|
226
|
-
*/
|
|
227
|
-
async function encryptEmailHybrid(email, recipient, aux) {
|
|
228
|
-
try {
|
|
229
|
-
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
230
|
-
return {
|
|
231
|
-
encEmail,
|
|
232
|
-
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
233
|
-
};
|
|
234
|
-
} catch (error) {
|
|
235
|
-
if (error instanceof InvalidInputEmail) throw error;
|
|
236
|
-
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
237
|
-
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
238
|
-
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
218
|
* Encrypts the email using hybrid encryption for multiple recipients.
|
|
243
219
|
*
|
|
244
220
|
* @param email - The email to encrypt for multiple recipients.
|
|
245
221
|
* @param recipients - The recipients with corresponding public keys.
|
|
246
222
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
247
|
-
* @returns The set of encrypted
|
|
223
|
+
* @returns The set of encrypted keys (one per user) and encrypted email
|
|
248
224
|
*/
|
|
249
225
|
async function encryptEmailHybridForMultipleRecipients(email, recipients, aux) {
|
|
250
226
|
try {
|
|
251
227
|
if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
|
|
252
228
|
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
encEmail,
|
|
258
|
-
encryptedKey
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
return encryptedEmails;
|
|
229
|
+
return {
|
|
230
|
+
encryptedKeys: await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient))),
|
|
231
|
+
encEmail
|
|
232
|
+
};
|
|
262
233
|
} catch (error) {
|
|
263
234
|
if (error instanceof InvalidInputEmail) throw error;
|
|
264
235
|
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
@@ -270,14 +241,14 @@ async function encryptEmailHybridForMultipleRecipients(email, recipients, aux) {
|
|
|
270
241
|
* Decrypts the email using hybrid encryption.
|
|
271
242
|
*
|
|
272
243
|
* @param encEmail - The encrypted email.
|
|
244
|
+
* @param encryptedKey - The encrypted key for this recipient.
|
|
273
245
|
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
274
246
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
275
247
|
* @returns The decrypted email
|
|
276
248
|
*/
|
|
277
|
-
async function decryptEmailHybrid(encEmail, recipientPrivateHybridKeys, aux) {
|
|
249
|
+
async function decryptEmailHybrid(encEmail, encryptedKey, recipientPrivateHybridKeys, aux) {
|
|
278
250
|
try {
|
|
279
|
-
|
|
280
|
-
return await decryptEmail(encEmail.encEmail, encryptionKey, aux);
|
|
251
|
+
return await decryptEmail(encEmail, await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys), aux);
|
|
281
252
|
} catch (error) {
|
|
282
253
|
if (error instanceof EmailHybridDecryptionError) throw error;
|
|
283
254
|
if (error instanceof EmailSymmetricDecryptionError) throw error;
|
|
@@ -390,48 +361,21 @@ async function decryptEmailAndSubject(encEmail, encryptionKey, aux) {
|
|
|
390
361
|
//#endregion
|
|
391
362
|
//#region src/email-crypto/hybridEncryptedEmailAndSubject.ts
|
|
392
363
|
/**
|
|
393
|
-
* Encrypts the email and its subject using hybrid encryption.
|
|
394
|
-
*
|
|
395
|
-
* @param email - The email and subject to encrypt.
|
|
396
|
-
* @param recipientPublicKeys - The public keys of the recipient.
|
|
397
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
398
|
-
* @returns The encrypted email and subject
|
|
399
|
-
*/
|
|
400
|
-
async function encryptEmailAndSubjectHybrid(email, recipient, aux) {
|
|
401
|
-
try {
|
|
402
|
-
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
403
|
-
return {
|
|
404
|
-
encEmail,
|
|
405
|
-
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
406
|
-
};
|
|
407
|
-
} catch (error) {
|
|
408
|
-
if (error instanceof InvalidInputEmail) throw error;
|
|
409
|
-
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
410
|
-
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
411
|
-
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
364
|
* Encrypts the email and its subject using hybrid encryption for multiple recipients.
|
|
416
365
|
*
|
|
417
366
|
* @param email - The email and subject to encrypt for multiple recipients.
|
|
418
367
|
* @param recipients - The recipients with corresponding public keys.
|
|
419
368
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
420
|
-
* @returns The set of encrypted
|
|
369
|
+
* @returns The set of encrypted keys and encrypted email and subject
|
|
421
370
|
*/
|
|
422
371
|
async function encryptEmailAndSubjectHybridForMultipleRecipients(email, recipients, aux) {
|
|
423
372
|
try {
|
|
424
373
|
if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
|
|
425
374
|
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
encEmail,
|
|
431
|
-
encryptedKey
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
return encryptedEmails;
|
|
375
|
+
return {
|
|
376
|
+
encEmail,
|
|
377
|
+
encryptedKeys: await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient)))
|
|
378
|
+
};
|
|
435
379
|
} catch (error) {
|
|
436
380
|
if (error instanceof InvalidInputEmail) throw error;
|
|
437
381
|
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
@@ -442,15 +386,15 @@ async function encryptEmailAndSubjectHybridForMultipleRecipients(email, recipien
|
|
|
442
386
|
/**
|
|
443
387
|
* Decrypts the email and its subject using hybrid encryption.
|
|
444
388
|
*
|
|
445
|
-
* @param
|
|
389
|
+
* @param encEmail - The encrypted email and subject.
|
|
390
|
+
* @param encryptedKey - The encrypted key for this recipient.
|
|
446
391
|
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
447
392
|
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
448
393
|
* @returns The decrypted email and subject
|
|
449
394
|
*/
|
|
450
|
-
async function decryptEmailAndSubjectHybrid(
|
|
395
|
+
async function decryptEmailAndSubjectHybrid(encEmail, encryptedKey, recipientPrivateHybridKeys, aux) {
|
|
451
396
|
try {
|
|
452
|
-
|
|
453
|
-
return await decryptEmailAndSubject(hybridEmail.encEmail, encryptionKey, aux);
|
|
397
|
+
return await decryptEmailAndSubject(encEmail, await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys), aux);
|
|
454
398
|
} catch (error) {
|
|
455
399
|
if (error instanceof InvalidInputEmail) throw error;
|
|
456
400
|
if (error instanceof EmailHybridDecryptionError) throw error;
|
|
@@ -530,6 +474,6 @@ const deriveEmailDraftKey = async (mnemonic) => {
|
|
|
530
474
|
return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);
|
|
531
475
|
};
|
|
532
476
|
//#endregion
|
|
533
|
-
export {
|
|
477
|
+
export { InvalidInputEmail as A, EmailHybridEncryptionError as C, EmailSymmetricEncryptionError as D, EmailSymmetricDecryptionError as E, wrapKey as M, FailedToDecryptEmail as O, EmailHybridDecryptionError as S, EmailPasswordProtectError as T, encryptEmail as _, decryptPwdProtectedEmailAndSubject as a, passwordProtectKey as b, decryptEmailAndSubject as c, createPwdProtectedEmail as d, decryptPwdProtectedEmail as f, decryptKeysHybrid as g, decryptEmail as h, createPwdProtectedEmailAndSubject as i, unwrapKey as j, FailedToEncryptEmail as k, encryptEmailAndSubject as l, encryptEmailHybridForMultipleRecipients as m, deriveEmailDraftKey as n, decryptEmailAndSubjectHybrid as o, decryptEmailHybrid as p, generateEmailKeys as r, encryptEmailAndSubjectHybridForMultipleRecipients as s, deriveDatabaseKey as t, encryptEmailAndSubjectWithKey as u, encryptEmailWithKey as v, EmailPasswordOpenError as w, removePasswordProtection as x, encryptKeysHybrid as y };
|
|
534
478
|
|
|
535
|
-
//# sourceMappingURL=email-crypto-
|
|
479
|
+
//# sourceMappingURL=email-crypto-v_-nFOl2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-crypto-v_-nFOl2.mjs","names":[],"sources":["../src/key-wrapper/aesWrapper.ts","../src/email-crypto/errors.ts","../src/email-crypto/core.ts","../src/email-crypto/hybridEncyptedEmail.ts","../src/email-crypto/pwdProtectedEmail.ts","../src/email-crypto/coreSubject.ts","../src/email-crypto/hybridEncryptedEmailAndSubject.ts","../src/email-crypto/pwdProtectedEmailAndSubject.ts","../src/email-crypto/emailKeys.ts"],"sourcesContent":["import { aeskw } from '@noble/ciphers/aes.js';\n\n/**\n * Unwraps the given wrapped key\n *\n * @param encryptedKey - The wrapped key\n * @param wrappingKey - The secret key used for unwrapping\n * @returns The resulting key\n */\nexport async function unwrapKey(encryptedKey: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).decrypt(encryptedKey);\n}\n\n/**\n * Wraps the given key\n *\n * @param key - The key to be wrapped\n * @param wrappingKey - The secret key used for wrapping\n * @returns The resulting ciphertext\n */\nexport async function wrapKey(key: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).encrypt(key);\n}\n","export class FailedToEncryptEmail extends Error {\n constructor(errorMsg?: string) {\n super('Failed to encrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToEncryptEmail.prototype);\n }\n}\n\nexport class EmailSymmetricEncryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to symmetrically encrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailSymmetricEncryptionError.prototype);\n }\n}\n\nexport class EmailSymmetricDecryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to symmetrically decrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailSymmetricDecryptionError.prototype);\n }\n}\n\nexport class EmailHybridEncryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to hybridly encrypt the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailHybridEncryptionError.prototype);\n }\n}\n\nexport class EmailHybridDecryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to hybridly decrypt the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailHybridDecryptionError.prototype);\n }\n}\n\nexport class EmailPasswordProtectError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to password-protect the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailPasswordProtectError.prototype);\n }\n}\n\nexport class EmailPasswordOpenError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open password-protected key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailPasswordOpenError.prototype);\n }\n}\n\nexport class InvalidInputEmail extends Error {\n constructor() {\n super('Invalid input');\n\n Object.setPrototypeOf(this, InvalidInputEmail.prototype);\n }\n}\n\nexport class FailedToDecryptEmail extends Error {\n constructor(errorMsg?: string) {\n super('Failed to decrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToDecryptEmail.prototype);\n }\n}\n","import { HybridEncKey, PwdProtectedKey, Email, RecipientWithPublicKey, EmailEncrypted } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encapsulateHybrid, decapsulateHybrid } from '../hybrid-crypto';\nimport { wrapKey, unwrapKey } from '../key-wrapper';\nimport { getKeyFromPassword, getKeyFromPasswordAndSalt } from '../derive-password';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\nimport {\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n EmailPasswordOpenError,\n EmailPasswordProtectError,\n} from './errors';\n\n/**\n * Symmetrically encrypts email.\n *\n * @param email - The email to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmail(\n email: Email,\n aux?: Uint8Array,\n): Promise<{\n encEmail: EmailEncrypted;\n encryptionKey: Uint8Array;\n}> {\n if (!email.text) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = genSymmetricKey();\n const encEmail = await encryptEmailWithKey(email, encryptionKey, aux);\n\n return { encEmail, encryptionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Symmetrically encrypts email with the given key.\n *\n * @param email - The email to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailWithKey(\n email: Email,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailEncrypted> {\n try {\n const text = UTF8ToUint8(email.text);\n const preview = UTF8ToUint8(email.preview);\n\n const encryptedText = await encryptSymmetrically(encryptionKey, text, aux);\n const encText = uint8ArrayToBase64(encryptedText);\n\n const encryptedPreview = await encryptSymmetrically(encryptionKey, preview, aux);\n const encPreview = uint8ArrayToBase64(encryptedPreview);\n\n const encryptedAttachmentsSessionKey = await encryptSymmetrically(encryptionKey, email.attachmentsSessionKey, aux);\n const encAttachmentsSessionKey = uint8ArrayToBase64(encryptedAttachmentsSessionKey);\n\n return { encText, encPreview, encAttachmentsSessionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email.\n *\n * @param encEmail - The email to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email\n */\nexport async function decryptEmail(\n encEmail: EmailEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<Email> {\n try {\n const encText = base64ToUint8Array(encEmail.encText);\n const textArray = await decryptSymmetrically(encryptionKey, encText, aux);\n const text = uint8ToUTF8(textArray);\n\n const encPreview = base64ToUint8Array(encEmail.encPreview);\n const previewArray = await decryptSymmetrically(encryptionKey, encPreview, aux);\n const preview = uint8ToUTF8(previewArray);\n\n const encAttachementSessionKey = base64ToUint8Array(encEmail.encAttachmentsSessionKey);\n const attachmentsSessionKey = await decryptSymmetrically(encryptionKey, encAttachementSessionKey, aux);\n\n return { text, preview, attachmentsSessionKey };\n } catch (error) {\n throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Encrypts the email symmetric key using hybrid encryption.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param recipient - The recipient with a public hybrid key.\n * @returns The encrypted email symmetric key\n */\nexport async function encryptKeysHybrid(\n emailEncryptionKey: Uint8Array,\n recipient: RecipientWithPublicKey,\n): Promise<HybridEncKey> {\n try {\n const { cipherText, sharedSecret } = encapsulateHybrid(recipient.publicHybridKey);\n const encryptedKey = await wrapKey(emailEncryptionKey, sharedSecret);\n const encryptedKeyBase64 = uint8ArrayToBase64(encryptedKey);\n const kyberCiphertextBase64 = uint8ArrayToBase64(cipherText);\n\n return {\n encryptedKey: encryptedKeyBase64,\n hybridCiphertext: kyberCiphertextBase64,\n encryptedForEmail: recipient.email,\n };\n } catch (error) {\n throw new EmailHybridEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email symmetric key encrypted via hybrid encryption.\n *\n * @param encryptedKey - The encrypted email key.\n * @param recipientPrivateKey - The private key of the recipient.\n * @returns The email encryption key\n */\nexport async function decryptKeysHybrid(\n encryptedKey: HybridEncKey,\n recipientPrivateKey: Uint8Array,\n): Promise<Uint8Array> {\n try {\n const kyberCiphertext = base64ToUint8Array(encryptedKey.hybridCiphertext);\n const encKey = base64ToUint8Array(encryptedKey.encryptedKey);\n const sharedSecret = decapsulateHybrid(kyberCiphertext, recipientPrivateKey);\n const encryptionKey = await unwrapKey(encKey, sharedSecret);\n return encryptionKey;\n } catch (error) {\n throw new EmailHybridDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Password-protects the email symmetric key.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param password - The secret password for key protection.\n * @returns The password-protected email symmetric key\n */\nexport async function passwordProtectKey(emailEncryptionKey: Uint8Array, password: string): Promise<PwdProtectedKey> {\n try {\n const { key, salt } = await getKeyFromPassword(password);\n const encryptedKey = await wrapKey(emailEncryptionKey, key);\n const saltStr = uint8ArrayToBase64(salt);\n const encryptedKeyStr = uint8ArrayToBase64(encryptedKey);\n return { encryptedKey: encryptedKeyStr, salt: saltStr };\n } catch (error) {\n throw new EmailPasswordProtectError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Removes passoword-protection and exposes the email symmetric key.\n *\n * @param emailEncryptionKey - The password-protected email key.\n * @param password - The secret password for key protection.\n * @returns The email encryption key\n */\nexport async function removePasswordProtection(\n emailEncryptionKey: PwdProtectedKey,\n password: string,\n): Promise<Uint8Array> {\n try {\n const salt = base64ToUint8Array(emailEncryptionKey.salt);\n const encryptedKey = base64ToUint8Array(emailEncryptionKey.encryptedKey);\n const key = await getKeyFromPasswordAndSalt(password, salt);\n const encryptionKey = await unwrapKey(encryptedKey, key);\n return encryptionKey;\n } catch (error) {\n throw new EmailPasswordOpenError(error instanceof Error ? error.message : String(error));\n }\n}\n","import { Email, RecipientWithPublicKey, HybridEncKey, EmailEncrypted } from '../types';\nimport { decryptEmail, encryptKeysHybrid, decryptKeysHybrid, encryptEmail } from './core';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Encrypts the email using hybrid encryption for multiple recipients.\n *\n * @param email - The email to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted keys (one per user) and encrypted email\n */\nexport async function encryptEmailHybridForMultipleRecipients(\n email: Email,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<{ encryptedKeys: HybridEncKey[]; encEmail: EmailEncrypted }> {\n try {\n if (!recipients || recipients.length === 0) {\n throw new InvalidInputEmail();\n }\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n\n const encryptedKeys = await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient)));\n\n return { encryptedKeys, encEmail };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email using hybrid encryption.\n *\n * @param encEmail - The encrypted email.\n * @param encryptedKey - The encrypted key for this recipient.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email\n */\nexport async function decryptEmailHybrid(\n encEmail: EmailEncrypted,\n encryptedKey: HybridEncKey,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<Email> {\n try {\n const encryptionKey = await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys);\n return await decryptEmail(encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailHybridDecryptionError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { PwdProtectedEmail, Email } from '../types';\nimport { decryptEmail, passwordProtectKey, removePasswordProtection, encryptEmail } from './core';\nimport {\n EmailSymmetricEncryptionError,\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailPasswordProtectError,\n EmailSymmetricDecryptionError,\n EmailPasswordOpenError,\n InvalidInputEmail,\n} from './errors';\n/**\n * Creates a password-protected email.\n *\n * @param email - The email to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmail(\n email: Email,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmail> {\n try {\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailPasswordProtectError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens a password-protected email.\n *\n * @param encryptedEmail - The encrypted email\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email\n */\nexport async function decryptPwdProtectedEmail(\n encryptedEmail: PwdProtectedEmail,\n password: string,\n aux?: Uint8Array,\n): Promise<Email> {\n if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n return await decryptEmail(encryptedEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailPasswordOpenError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { EmailAndSubject, EmailAndSubjectEncrypted } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encryptEmailWithKey, decryptEmail } from './core';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\nimport { InvalidInputEmail, EmailSymmetricDecryptionError, EmailSymmetricEncryptionError } from './errors';\n\n/**\n * Symmetrically encrypts email and subject.\n *\n * @param email - The email and subject to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailAndSubject(\n email: EmailAndSubject,\n aux?: Uint8Array,\n): Promise<{\n encEmail: EmailAndSubjectEncrypted;\n encryptionKey: Uint8Array;\n}> {\n if (!email.text || !email.subject) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = genSymmetricKey();\n const encEmail = await encryptEmailAndSubjectWithKey(email, encryptionKey, aux);\n\n return { encEmail, encryptionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Symmetrically encrypts email and subject with the given key.\n *\n * @param email - The email and subject to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailAndSubjectWithKey(\n email: EmailAndSubject,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubjectEncrypted> {\n try {\n const enc = await encryptEmailWithKey(email, encryptionKey, aux);\n const subject = UTF8ToUint8(email.subject);\n const subjectEnc = await encryptSymmetrically(encryptionKey, subject, aux);\n const encSubject = uint8ArrayToBase64(subjectEnc);\n\n return { ...enc, encSubject };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email and email subject.\n *\n * @param encEmail - The encrypted email and subject to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email and subject\n */\nexport async function decryptEmailAndSubject(\n encEmail: EmailAndSubjectEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n try {\n const encSubject = base64ToUint8Array(encEmail.encSubject);\n const subjectArray = await decryptSymmetrically(encryptionKey, encSubject, aux);\n const subject = uint8ToUTF8(subjectArray);\n const email = await decryptEmail(encEmail, encryptionKey, aux);\n\n return { ...email, subject };\n } catch (error) {\n throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n","import { RecipientWithPublicKey, EmailAndSubject, EmailAndSubjectEncrypted, HybridEncKey } from '../types';\nimport { encryptKeysHybrid, decryptKeysHybrid } from './core';\nimport { encryptEmailAndSubject, decryptEmailAndSubject } from './coreSubject';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Encrypts the email and its subject using hybrid encryption for multiple recipients.\n *\n * @param email - The email and subject to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted keys and encrypted email and subject\n */\nexport async function encryptEmailAndSubjectHybridForMultipleRecipients(\n email: EmailAndSubject,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<{ encryptedKeys: HybridEncKey[]; encEmail: EmailAndSubjectEncrypted }> {\n try {\n if (!recipients || recipients.length === 0) {\n throw new InvalidInputEmail();\n }\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n\n const encryptedKeys = await Promise.all(recipients.map((recipient) => encryptKeysHybrid(encryptionKey, recipient)));\n\n return { encEmail, encryptedKeys };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email and its subject using hybrid encryption.\n *\n * @param encEmail - The encrypted email and subject.\n * @param encryptedKey - The encrypted key for this recipient.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email and subject\n */\nexport async function decryptEmailAndSubjectHybrid(\n encEmail: EmailAndSubjectEncrypted,\n encryptedKey: HybridEncKey,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n try {\n const encryptionKey = await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys);\n return await decryptEmailAndSubject(encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailHybridDecryptionError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { EmailAndSubject, PwdProtectedEmailAndSubject } from '../types';\nimport { passwordProtectKey, removePasswordProtection } from './core';\nimport { encryptEmailAndSubject, decryptEmailAndSubject } from './coreSubject';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n InvalidInputEmail,\n EmailPasswordOpenError,\n EmailPasswordProtectError,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Creates a password-protected email and subject.\n *\n * @param email - The email and subject to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmailAndSubject(\n email: EmailAndSubject,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmailAndSubject> {\n try {\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailPasswordProtectError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens a password-protected email and subject.\n *\n * @param encryptedEmail - The encrypted email and subject\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email and subject\n */\nexport async function decryptPwdProtectedEmailAndSubject(\n encryptedEmail: PwdProtectedEmailAndSubject,\n password: string,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n return await decryptEmailAndSubject(encryptedEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailPasswordOpenError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { genHybridKeys } from '../hybrid-crypto';\nimport { HybridKeyPair } from '../types';\nimport { deriveKeyFromMnemonic } from '../derive-key';\nimport { CONTEXT_DATABASE, CONTEXT_DRAFT } from '../constants';\n\n/**\n * Generates public and private keys for email encryption.\n *\n * @returns The user's private and public keys\n */\nexport async function generateEmailKeys(): Promise<HybridKeyPair> {\n return genHybridKeys();\n}\n\n/**\n * Derives database encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting database\n */\nexport const deriveDatabaseKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DATABASE);\n};\n\n/**\n * Derives email draft encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting email drafts\n */\nexport const deriveEmailDraftKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);\n};\n"],"mappings":";;;;;;;;;;;;;;;AASA,eAAsB,UAAU,cAA0B,aAA8C;AACtG,QAAO,MAAM,YAAY,CAAC,QAAQ,aAAa;;;;;;;;;AAUjD,eAAsB,QAAQ,KAAiB,aAA8C;AAC3F,QAAO,MAAM,YAAY,CAAC,QAAQ,IAAI;;;;ACrBxC,IAAa,uBAAb,MAAa,6BAA6B,MAAM;CAC9C,YAAY,UAAmB;AAC7B,QAAM,8BAA8B,SAAS;AAE7C,SAAO,eAAe,MAAM,qBAAqB,UAAU;;;AAI/D,IAAa,gCAAb,MAAa,sCAAsC,MAAM;CACvD,YAAY,UAAmB;AAC7B,QAAM,4CAA4C,SAAS;AAE3D,SAAO,eAAe,MAAM,8BAA8B,UAAU;;;AAIxE,IAAa,gCAAb,MAAa,sCAAsC,MAAM;CAC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