internxt-crypto 1.3.1 → 1.4.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.
@@ -1,401 +0,0 @@
1
- require("./chunk-C13QxCFV.js");
2
- const require_constants = require("./constants.js");
3
- const require_utils = require("./utils.js");
4
- const require_derive_key = require("./derive-key.js");
5
- const require_derive_password = require("./derive-password-CbYRDq7V.js");
6
- const require_symmetric_crypto = require("./symmetric-crypto.js");
7
- const require_xwing = require("./xwing-7fEqAT7W.js");
8
- let _noble_ciphers_aes_js = require("@noble/ciphers/aes.js");
9
- //#region src/key-wrapper/aesWrapper.ts
10
- /**
11
- * Unwraps the given wrapped key
12
- *
13
- * @param encryptedKey - The wrapped key
14
- * @param wrappingKey - The secret key used for unwrapping
15
- * @returns The resulting key
16
- */
17
- async function unwrapKey(encryptedKey, wrappingKey) {
18
- return (0, _noble_ciphers_aes_js.aeskw)(wrappingKey).decrypt(encryptedKey);
19
- }
20
- /**
21
- * Wraps the given key
22
- *
23
- * @param key - The key to be wrapped
24
- * @param wrappingKey - The secret key used for wrapping
25
- * @returns The resulting ciphertext
26
- */
27
- async function wrapKey(key, wrappingKey) {
28
- return (0, _noble_ciphers_aes_js.aeskw)(wrappingKey).encrypt(key);
29
- }
30
- //#endregion
31
- //#region src/email-crypto/core.ts
32
- /**
33
- * Symmetrically encrypts email body.
34
- *
35
- * @param body - The email body to encrypt.
36
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
37
- * @returns The resulting encrypted email body and symmetric key used for encryption
38
- */
39
- async function encryptEmailBody(body, aux) {
40
- try {
41
- if (!body.text || !body.subject) throw new Error("Invalid input");
42
- const encryptionKey = require_symmetric_crypto.genSymmetricKey();
43
- return {
44
- encEmailBody: await encryptEmailBodyWithKey(body, encryptionKey, aux),
45
- encryptionKey
46
- };
47
- } catch (error) {
48
- throw new Error("Failed to symmetrically encrypt email body", { cause: error });
49
- }
50
- }
51
- /**
52
- * Symmetrically encrypts email body with the given key.
53
- *
54
- * @param body - The email body to encrypt.
55
- * @param encryptionKey - The symmetric key to encrypt the email.
56
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
57
- * @returns The resulting encrypted email body and symmetric key used for encryption
58
- */
59
- async function encryptEmailBodyWithKey(body, encryptionKey, aux) {
60
- try {
61
- const text = require_utils.UTF8ToUint8(body.text);
62
- const subjectEnc = await require_symmetric_crypto.encryptSymmetrically(encryptionKey, require_utils.UTF8ToUint8(body.subject), aux);
63
- const enc = {
64
- encText: require_utils.uint8ArrayToBase64(await require_symmetric_crypto.encryptSymmetrically(encryptionKey, text, aux)),
65
- encSubject: require_utils.uint8ArrayToBase64(subjectEnc)
66
- };
67
- if (body.attachments) {
68
- const promises = body.attachments.map((attachment) => {
69
- return require_symmetric_crypto.encryptSymmetrically(encryptionKey, require_utils.UTF8ToUint8(attachment), aux);
70
- });
71
- enc.encAttachments = (await Promise.all(promises))?.map(require_utils.uint8ArrayToBase64);
72
- }
73
- return enc;
74
- } catch (error) {
75
- throw new Error("Failed to encrypt email body", { cause: error });
76
- }
77
- }
78
- /**
79
- * Decrypts symmetrically encrypted email body.
80
- *
81
- * @param encEmailBody - The email body to decrypt.
82
- * @param encryptionKey - The symmetric key to decrypt the email.
83
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
84
- * @returns The resulting decrypted email body
85
- */
86
- async function decryptEmailBody(encEmailBody, encryptionKey, aux) {
87
- try {
88
- const subject = require_utils.uint8ToUTF8(await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmailBody.encSubject), aux));
89
- const body = {
90
- text: require_utils.uint8ToUTF8(await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmailBody.encText), aux)),
91
- subject
92
- };
93
- if (encEmailBody.encAttachments) {
94
- const promises = (encEmailBody.encAttachments?.map(require_utils.base64ToUint8Array))?.map((encAtt) => require_symmetric_crypto.decryptSymmetrically(encryptionKey, encAtt, aux));
95
- body.attachments = (await Promise.all(promises))?.map((att) => require_utils.uint8ToUTF8(att));
96
- }
97
- return body;
98
- } catch (error) {
99
- throw new Error("Failed to symmetrically decrypt email body", { cause: error });
100
- }
101
- }
102
- /**
103
- * Encrypts the email symmetric key using hybrid encryption.
104
- *
105
- * @param emailEncryptionKey - The symmetric key used for email encryption.
106
- * @param recipient - The recipient with a public hybrid key.
107
- * @returns The encrypted email symmetric key
108
- */
109
- async function encryptKeysHybrid(emailEncryptionKey, recipient) {
110
- try {
111
- const { cipherText, sharedSecret } = require_xwing.encapsulateHybrid(recipient.publicHybridKey);
112
- return {
113
- encryptedKey: require_utils.uint8ArrayToBase64(await wrapKey(emailEncryptionKey, sharedSecret)),
114
- hybridCiphertext: require_utils.uint8ArrayToBase64(cipherText),
115
- encryptedForEmail: recipient.email
116
- };
117
- } catch (error) {
118
- throw new Error("Failed to encrypt email key using hybrid encryption", { cause: error });
119
- }
120
- }
121
- /**
122
- * Decrypts the email symmetric key encrypted via hybrid encryption.
123
- *
124
- * @param encryptedKey - The encrypted email key.
125
- * @param recipientPrivateKey - The private key of the recipient.
126
- * @returns The email encryption key
127
- */
128
- async function decryptKeysHybrid(encryptedKey, recipientPrivateKey) {
129
- try {
130
- const kyberCiphertext = require_utils.base64ToUint8Array(encryptedKey.hybridCiphertext);
131
- return await unwrapKey(require_utils.base64ToUint8Array(encryptedKey.encryptedKey), require_xwing.decapsulateHybrid(kyberCiphertext, recipientPrivateKey));
132
- } catch (error) {
133
- throw new Error("Failed to decrypt email key encrypted via hybrid encryption", { cause: error });
134
- }
135
- }
136
- /**
137
- * Password-protects the email symmetric key.
138
- *
139
- * @param emailEncryptionKey - The symmetric key used for email encryption.
140
- * @param password - The secret password for key protection.
141
- * @returns The password-protected email symmetric key
142
- */
143
- async function passwordProtectKey(emailEncryptionKey, password) {
144
- try {
145
- const { key, salt } = await require_derive_password.getKeyFromPassword(password);
146
- const encryptedKey = await wrapKey(emailEncryptionKey, key);
147
- const saltStr = require_utils.uint8ArrayToBase64(salt);
148
- return {
149
- encryptedKey: require_utils.uint8ArrayToBase64(encryptedKey),
150
- salt: saltStr
151
- };
152
- } catch (error) {
153
- throw new Error("Failed to password-protect email key", { cause: error });
154
- }
155
- }
156
- /**
157
- * Removes passoword-protection and exposes the email symmetric key.
158
- *
159
- * @param emailEncryptionKey - The password-protected email key.
160
- * @param password - The secret password for key protection.
161
- * @returns The email encryption key
162
- */
163
- async function removePasswordProtection(emailEncryptionKey, password) {
164
- try {
165
- const salt = require_utils.base64ToUint8Array(emailEncryptionKey.salt);
166
- return await unwrapKey(require_utils.base64ToUint8Array(emailEncryptionKey.encryptedKey), await require_derive_password.getKeyFromPasswordAndSalt(password, salt));
167
- } catch (error) {
168
- throw new Error("Failed to remove password-protection from email key", { cause: error });
169
- }
170
- }
171
- //#endregion
172
- //#region src/email-crypto/hybridEncyptedEmail.ts
173
- /**
174
- * Encrypts the email body using hybrid encryption.
175
- *
176
- * @param body - The email body to encrypt.
177
- * @param recipientPublicKeys - The public keys of the recipient.
178
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
179
- * @returns The encrypted email body
180
- */
181
- async function encryptEmailHybrid(body, recipient, aux) {
182
- try {
183
- const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);
184
- return {
185
- encEmailBody,
186
- encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
187
- };
188
- } catch (error) {
189
- throw new Error("Failed to encrypt email body with hybrid encryption", { cause: error });
190
- }
191
- }
192
- /**
193
- * Encrypts the email body using hybrid encryption for multiple recipients.
194
- *
195
- * @param body - The email body to encrypt for multiple recipients.
196
- * @param recipients - The recipients with corresponding public keys.
197
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
198
- * @returns The set of encrypted email bodies
199
- */
200
- async function encryptEmailHybridForMultipleRecipients(body, recipients, aux) {
201
- try {
202
- const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);
203
- const encryptedEmails = [];
204
- for (const recipient of recipients) {
205
- const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
206
- encryptedEmails.push({
207
- encEmailBody,
208
- encryptedKey
209
- });
210
- }
211
- return encryptedEmails;
212
- } catch (error) {
213
- throw new Error("Failed to encrypt email to multiple recipients with hybrid encryption", { cause: error });
214
- }
215
- }
216
- /**
217
- * Decrypts the email using hybrid encryption.
218
- *
219
- * @param encEmailBody - The encrypted email.
220
- * @param recipientPrivateHybridKeys - The private key of the recipient.
221
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
222
- * @returns The decrypted email body
223
- */
224
- async function decryptEmailHybrid(encEmailBody, recipientPrivateHybridKeys, aux) {
225
- try {
226
- const encryptionKey = await decryptKeysHybrid(encEmailBody.encryptedKey, recipientPrivateHybridKeys);
227
- return await decryptEmailBody(encEmailBody.encEmailBody, encryptionKey, aux);
228
- } catch (error) {
229
- throw new Error("Failed to decrypt email with hybrid encryption", { cause: error });
230
- }
231
- }
232
- //#endregion
233
- //#region src/email-crypto/pwdProtectedEmail.ts
234
- /**
235
- * Creates a password-protected email.
236
- *
237
- * @param email - The email to password-protect
238
- * @param password - The secret password shared among recipients
239
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
240
- * @returns The password-protected email
241
- */
242
- async function createPwdProtectedEmail(emailBody, password, aux) {
243
- try {
244
- const { encryptionKey, encEmailBody } = await encryptEmailBody(emailBody, aux);
245
- return {
246
- encEmailBody,
247
- encryptedKey: await passwordProtectKey(encryptionKey, password)
248
- };
249
- } catch (error) {
250
- throw new Error("Failed to password-protect email", { cause: error });
251
- }
252
- }
253
- /**
254
- * Opens a password-protected email.
255
- *
256
- * @param encryptedEmail - The encrypted email
257
- * @param password - The secret password shared among recipients.
258
- * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
259
- * @returns The decrypted email body
260
- */
261
- async function decryptPwdProtectedEmail(encryptedEmail, password, aux) {
262
- try {
263
- const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
264
- return await decryptEmailBody(encryptedEmail.encEmailBody, encryptionKey, aux);
265
- } catch (error) {
266
- throw new Error("Failed to decrypt password-protect email", { cause: error });
267
- }
268
- }
269
- //#endregion
270
- //#region src/email-crypto/emailKeys.ts
271
- /**
272
- * Generates public and private keys for email encryption.
273
- *
274
- * @returns The user's private and public keys
275
- */
276
- async function generateEmailKeys() {
277
- return require_xwing.genHybridKeys();
278
- }
279
- /**
280
- * Derives database encryption key for the given user
281
- *
282
- * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
283
- * @returns The symmetric key for protecting database
284
- */
285
- const deriveDatabaseKey = async (mnemonic) => {
286
- return require_derive_key.deriveKeyFromMnemonic(mnemonic, require_constants.CONTEXT_DATABASE);
287
- };
288
- /**
289
- * Derives email draft encryption key for the given user
290
- *
291
- * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
292
- * @returns The symmetric key for protecting email drafts
293
- */
294
- const deriveEmailDraftKey = async (mnemonic) => {
295
- return require_derive_key.deriveKeyFromMnemonic(mnemonic, require_constants.CONTEXT_DRAFT);
296
- };
297
- //#endregion
298
- Object.defineProperty(exports, "createPwdProtectedEmail", {
299
- enumerable: true,
300
- get: function() {
301
- return createPwdProtectedEmail;
302
- }
303
- });
304
- Object.defineProperty(exports, "decryptEmailBody", {
305
- enumerable: true,
306
- get: function() {
307
- return decryptEmailBody;
308
- }
309
- });
310
- Object.defineProperty(exports, "decryptEmailHybrid", {
311
- enumerable: true,
312
- get: function() {
313
- return decryptEmailHybrid;
314
- }
315
- });
316
- Object.defineProperty(exports, "decryptKeysHybrid", {
317
- enumerable: true,
318
- get: function() {
319
- return decryptKeysHybrid;
320
- }
321
- });
322
- Object.defineProperty(exports, "decryptPwdProtectedEmail", {
323
- enumerable: true,
324
- get: function() {
325
- return decryptPwdProtectedEmail;
326
- }
327
- });
328
- Object.defineProperty(exports, "deriveDatabaseKey", {
329
- enumerable: true,
330
- get: function() {
331
- return deriveDatabaseKey;
332
- }
333
- });
334
- Object.defineProperty(exports, "deriveEmailDraftKey", {
335
- enumerable: true,
336
- get: function() {
337
- return deriveEmailDraftKey;
338
- }
339
- });
340
- Object.defineProperty(exports, "encryptEmailBody", {
341
- enumerable: true,
342
- get: function() {
343
- return encryptEmailBody;
344
- }
345
- });
346
- Object.defineProperty(exports, "encryptEmailBodyWithKey", {
347
- enumerable: true,
348
- get: function() {
349
- return encryptEmailBodyWithKey;
350
- }
351
- });
352
- Object.defineProperty(exports, "encryptEmailHybrid", {
353
- enumerable: true,
354
- get: function() {
355
- return encryptEmailHybrid;
356
- }
357
- });
358
- Object.defineProperty(exports, "encryptEmailHybridForMultipleRecipients", {
359
- enumerable: true,
360
- get: function() {
361
- return encryptEmailHybridForMultipleRecipients;
362
- }
363
- });
364
- Object.defineProperty(exports, "encryptKeysHybrid", {
365
- enumerable: true,
366
- get: function() {
367
- return encryptKeysHybrid;
368
- }
369
- });
370
- Object.defineProperty(exports, "generateEmailKeys", {
371
- enumerable: true,
372
- get: function() {
373
- return generateEmailKeys;
374
- }
375
- });
376
- Object.defineProperty(exports, "passwordProtectKey", {
377
- enumerable: true,
378
- get: function() {
379
- return passwordProtectKey;
380
- }
381
- });
382
- Object.defineProperty(exports, "removePasswordProtection", {
383
- enumerable: true,
384
- get: function() {
385
- return removePasswordProtection;
386
- }
387
- });
388
- Object.defineProperty(exports, "unwrapKey", {
389
- enumerable: true,
390
- get: function() {
391
- return unwrapKey;
392
- }
393
- });
394
- Object.defineProperty(exports, "wrapKey", {
395
- enumerable: true,
396
- get: function() {
397
- return wrapKey;
398
- }
399
- });
400
-
401
- //# sourceMappingURL=email-crypto-Cr7znO1a.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"email-crypto-Cr7znO1a.js","names":["genSymmetricKey","UTF8ToUint8","encryptSymmetrically","uint8ArrayToBase64","uint8ToUTF8","decryptSymmetrically","base64ToUint8Array","encapsulateHybrid","decapsulateHybrid","getKeyFromPassword","getKeyFromPasswordAndSalt","genHybridKeys","deriveKeyFromMnemonic","CONTEXT_DATABASE","CONTEXT_DRAFT"],"sources":["../src/key-wrapper/aesWrapper.ts","../src/email-crypto/core.ts","../src/email-crypto/hybridEncyptedEmail.ts","../src/email-crypto/pwdProtectedEmail.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","import { HybridEncKey, PwdProtectedKey, EmailBody, EmailBodyEncrypted, RecipientWithPublicKey } 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';\n\n/**\n * Symmetrically encrypts email body.\n *\n * @param body - The email body to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email body and symmetric key used for encryption\n */\nexport async function encryptEmailBody(\n body: EmailBody,\n aux?: Uint8Array,\n): Promise<{\n encEmailBody: EmailBodyEncrypted;\n encryptionKey: Uint8Array;\n}> {\n try {\n if (!body.text || !body.subject) {\n throw new Error('Invalid input');\n }\n const encryptionKey = genSymmetricKey();\n const encEmailBody = await encryptEmailBodyWithKey(body, encryptionKey, aux);\n\n return { encEmailBody, encryptionKey };\n } catch (error) {\n throw new Error('Failed to symmetrically encrypt email body', { cause: error });\n }\n}\n\n/**\n * Symmetrically encrypts email body with the given key.\n *\n * @param body - The email body 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 body and symmetric key used for encryption\n */\nexport async function encryptEmailBodyWithKey(\n body: EmailBody,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailBodyEncrypted> {\n try {\n const text = UTF8ToUint8(body.text);\n const subject = UTF8ToUint8(body.subject);\n const subjectEnc = await encryptSymmetrically(encryptionKey, subject, aux);\n const encryptedText = await encryptSymmetrically(encryptionKey, text, aux);\n const encText = uint8ArrayToBase64(encryptedText);\n const encSubject = uint8ArrayToBase64(subjectEnc);\n const enc: EmailBodyEncrypted = { encText, encSubject };\n\n if (body.attachments) {\n const promises = body.attachments.map((attachment) => {\n const binaryAttachment = UTF8ToUint8(attachment);\n return encryptSymmetrically(encryptionKey, binaryAttachment, aux);\n });\n const encryptedAttachments = await Promise.all(promises);\n enc.encAttachments = encryptedAttachments?.map(uint8ArrayToBase64);\n }\n\n return enc;\n } catch (error) {\n throw new Error('Failed to encrypt email body', { cause: error });\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email body.\n *\n * @param encEmailBody - The email body 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 body\n */\nexport async function decryptEmailBody(\n encEmailBody: EmailBodyEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailBody> {\n try {\n const encSubject = base64ToUint8Array(encEmailBody.encSubject);\n const subjectArray = await decryptSymmetrically(encryptionKey, encSubject, aux);\n const subject = uint8ToUTF8(subjectArray);\n const encText = base64ToUint8Array(encEmailBody.encText);\n const textArray = await decryptSymmetrically(encryptionKey, encText, aux);\n const text = uint8ToUTF8(textArray);\n const body: EmailBody = { text, subject };\n\n if (encEmailBody.encAttachments) {\n const encAttachments = encEmailBody.encAttachments?.map(base64ToUint8Array);\n const promises = encAttachments?.map((encAtt) => decryptSymmetrically(encryptionKey, encAtt, aux));\n const decryptedAttachments = await Promise.all(promises);\n body.attachments = decryptedAttachments?.map((att) => uint8ToUTF8(att));\n }\n\n return body;\n } catch (error) {\n throw new Error('Failed to symmetrically decrypt email body', { cause: 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 Error('Failed to encrypt email key using hybrid encryption', { cause: 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 Error('Failed to decrypt email key encrypted via hybrid encryption', { cause: 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 Error('Failed to password-protect email key', { cause: 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 Error('Failed to remove password-protection from email key', { cause: error });\n }\n}\n","import { HybridEncryptedEmail, EmailBody, RecipientWithPublicKey } from '../types';\nimport { decryptEmailBody, encryptKeysHybrid, decryptKeysHybrid, encryptEmailBody } from './core';\n\n/**\n * Encrypts the email body using hybrid encryption.\n *\n * @param body - The email body to encrypt.\n * @param recipientPublicKeys - The public keys of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The encrypted email body\n */\nexport async function encryptEmailHybrid(\n body: EmailBody,\n recipient: RecipientWithPublicKey,\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmail> {\n try {\n const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n return { encEmailBody, encryptedKey };\n } catch (error) {\n throw new Error('Failed to encrypt email body with hybrid encryption', { cause: error });\n }\n}\n\n/**\n * Encrypts the email body using hybrid encryption for multiple recipients.\n *\n * @param body - The email body 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 email bodies\n */\nexport async function encryptEmailHybridForMultipleRecipients(\n body: EmailBody,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmail[]> {\n try {\n const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);\n\n const encryptedEmails: HybridEncryptedEmail[] = [];\n for (const recipient of recipients) {\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n encryptedEmails.push({\n encEmailBody: encEmailBody,\n encryptedKey,\n });\n }\n return encryptedEmails;\n } catch (error) {\n throw new Error('Failed to encrypt email to multiple recipients with hybrid encryption', { cause: error });\n }\n}\n\n/**\n * Decrypts the email using hybrid encryption.\n *\n * @param encEmailBody - The encrypted email.\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 body\n */\nexport async function decryptEmailHybrid(\n encEmailBody: HybridEncryptedEmail,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailBody> {\n try {\n const encryptionKey = await decryptKeysHybrid(encEmailBody.encryptedKey, recipientPrivateHybridKeys);\n const body = await decryptEmailBody(encEmailBody.encEmailBody, encryptionKey, aux);\n return body;\n } catch (error) {\n throw new Error('Failed to decrypt email with hybrid encryption', { cause: error });\n }\n}\n","import { PwdProtectedEmail, EmailBody } from '../types';\nimport { decryptEmailBody, passwordProtectKey, removePasswordProtection, encryptEmailBody } from './core';\n\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 emailBody: EmailBody,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmail> {\n try {\n const { encryptionKey, encEmailBody } = await encryptEmailBody(emailBody, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmailBody, encryptedKey };\n } catch (error) {\n throw new Error('Failed to password-protect email', { cause: 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 body\n */\nexport async function decryptPwdProtectedEmail(\n encryptedEmail: PwdProtectedEmail,\n password: string,\n aux?: Uint8Array,\n): Promise<EmailBody> {\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n const body = await decryptEmailBody(encryptedEmail.encEmailBody, encryptionKey, aux);\n return body;\n } catch (error) {\n throw new Error('Failed to decrypt password-protect email', { cause: 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 =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