internxt-crypto 1.3.0 → 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.
Files changed (101) hide show
  1. package/README.md +15 -73
  2. package/dist/asymmetric-crypto.js +3 -4
  3. package/dist/asymmetric-crypto.js.map +1 -1
  4. package/dist/asymmetric-crypto.mjs +1 -2
  5. package/dist/asymmetric-crypto.mjs.map +1 -1
  6. package/dist/chunk-C13QxCFV.js +50 -0
  7. package/dist/chunk-CfYAbeIz.mjs +13 -0
  8. package/dist/constants.js +2 -3
  9. package/dist/constants.js.map +1 -1
  10. package/dist/constants.mjs +1 -1
  11. package/dist/derive-key.js +50 -5
  12. package/dist/derive-key.js.map +1 -0
  13. package/dist/derive-key.mjs +46 -2
  14. package/dist/derive-key.mjs.map +1 -0
  15. package/dist/{deriveKeysFromPassword-iMHv-tIA.js → derive-password-CbYRDq7V.js} +25 -30
  16. package/dist/derive-password-CbYRDq7V.js.map +1 -0
  17. package/dist/{deriveKeysFromPassword-DFzo2OII.mjs → derive-password-PED-YfwE.mjs} +27 -34
  18. package/dist/derive-password-PED-YfwE.mjs.map +1 -0
  19. package/dist/derive-password.js +5 -6
  20. package/dist/derive-password.mjs +2 -3
  21. package/dist/email-crypto-Di814Pbk.js +739 -0
  22. package/dist/email-crypto-Di814Pbk.js.map +1 -0
  23. package/dist/email-crypto-DpjfXDes.mjs +535 -0
  24. package/dist/email-crypto-DpjfXDes.mjs.map +1 -0
  25. package/dist/email-crypto.d.mts +138 -29
  26. package/dist/email-crypto.d.mts.map +1 -1
  27. package/dist/email-crypto.d.ts +138 -29
  28. package/dist/email-crypto.d.ts.map +1 -1
  29. package/dist/email-crypto.js +34 -18
  30. package/dist/email-crypto.mjs +2 -3
  31. package/dist/hash.js +3 -4
  32. package/dist/hash.js.map +1 -1
  33. package/dist/hash.mjs +1 -2
  34. package/dist/hash.mjs.map +1 -1
  35. package/dist/index.d.mts +3 -3
  36. package/dist/index.d.ts +3 -3
  37. package/dist/index.js +72 -56
  38. package/dist/index.mjs +7 -8
  39. package/dist/{emailEncryptionKey-CYvr26FG.mjs → keystore-crypto-C6F-NQdX.mjs} +12 -15
  40. package/dist/keystore-crypto-C6F-NQdX.mjs.map +1 -0
  41. package/dist/{emailEncryptionKey-Ba95VuFO.js → keystore-crypto-CFiiGohl.js} +67 -70
  42. package/dist/keystore-crypto-CFiiGohl.js.map +1 -0
  43. package/dist/keystore-crypto.d.mts +1 -1
  44. package/dist/keystore-crypto.d.ts +1 -1
  45. package/dist/keystore-crypto.js +11 -12
  46. package/dist/keystore-crypto.mjs +2 -3
  47. package/dist/post-quantum-crypto.js +3 -4
  48. package/dist/post-quantum-crypto.js.map +1 -1
  49. package/dist/post-quantum-crypto.mjs +1 -2
  50. package/dist/post-quantum-crypto.mjs.map +1 -1
  51. package/dist/storage-service.js +13 -15
  52. package/dist/storage-service.js.map +1 -1
  53. package/dist/storage-service.mjs +2 -4
  54. package/dist/storage-service.mjs.map +1 -1
  55. package/dist/symmetric-crypto.js +51 -5
  56. package/dist/symmetric-crypto.js.map +1 -0
  57. package/dist/symmetric-crypto.mjs +47 -2
  58. package/dist/symmetric-crypto.mjs.map +1 -0
  59. package/dist/types-CrAiKyb0.d.ts +61 -0
  60. package/dist/types-CrAiKyb0.d.ts.map +1 -0
  61. package/dist/types-D6hsUfma.d.mts +61 -0
  62. package/dist/types-D6hsUfma.d.mts.map +1 -0
  63. package/dist/types.d.mts +2 -49
  64. package/dist/types.d.ts +2 -49
  65. package/dist/types.js +2 -3
  66. package/dist/types.js.map +1 -1
  67. package/dist/types.mjs +1 -1
  68. package/dist/types.mjs.map +1 -1
  69. package/dist/utils.js +127 -14
  70. package/dist/utils.js.map +1 -0
  71. package/dist/utils.mjs +113 -2
  72. package/dist/utils.mjs.map +1 -0
  73. package/dist/{xwing-iRWVRtNE.js → xwing-7fEqAT7W.js} +18 -19
  74. package/dist/{xwing-iRWVRtNE.js.map → xwing-7fEqAT7W.js.map} +1 -1
  75. package/dist/{xwing-CQmQX6hn.mjs → xwing-B34h5e3Z.mjs} +2 -3
  76. package/dist/{xwing-CQmQX6hn.mjs.map → xwing-B34h5e3Z.mjs.map} +1 -1
  77. package/package.json +22 -22
  78. package/dist/aes-B8TA-JKq.mjs +0 -49
  79. package/dist/aes-B8TA-JKq.mjs.map +0 -1
  80. package/dist/aes-CuW_SPl-.js +0 -67
  81. package/dist/aes-CuW_SPl-.js.map +0 -1
  82. package/dist/chunk-DQk6qfdC.mjs +0 -18
  83. package/dist/chunk-EbLFQX5m.js +0 -60
  84. package/dist/deriveKeysFromMnemonic-DB0UHE-y.mjs +0 -49
  85. package/dist/deriveKeysFromMnemonic-DB0UHE-y.mjs.map +0 -1
  86. package/dist/deriveKeysFromMnemonic-Dbx7VSBQ.js +0 -67
  87. package/dist/deriveKeysFromMnemonic-Dbx7VSBQ.js.map +0 -1
  88. package/dist/deriveKeysFromPassword-DFzo2OII.mjs.map +0 -1
  89. package/dist/deriveKeysFromPassword-iMHv-tIA.js.map +0 -1
  90. package/dist/emailEncryptionKey-Ba95VuFO.js.map +0 -1
  91. package/dist/emailEncryptionKey-CYvr26FG.mjs.map +0 -1
  92. package/dist/emailKeys-BFI-paQq.js +0 -406
  93. package/dist/emailKeys-BFI-paQq.js.map +0 -1
  94. package/dist/emailKeys-BjIeINfC.mjs +0 -304
  95. package/dist/emailKeys-BjIeINfC.mjs.map +0 -1
  96. package/dist/genID-D08K7JPr.mjs +0 -117
  97. package/dist/genID-D08K7JPr.mjs.map +0 -1
  98. package/dist/genID-cWOPmspk.js +0 -190
  99. package/dist/genID-cWOPmspk.js.map +0 -1
  100. package/dist/types.d.mts.map +0 -1
  101. package/dist/types.d.ts.map +0 -1
@@ -0,0 +1,739 @@
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/errors.ts
32
+ var FailedToEncryptEmail = class FailedToEncryptEmail extends Error {
33
+ constructor(errorMsg) {
34
+ super("Failed to encrypt email: " + errorMsg);
35
+ Object.setPrototypeOf(this, FailedToEncryptEmail.prototype);
36
+ }
37
+ };
38
+ var EmailSymmetricEncryptionError = class EmailSymmetricEncryptionError extends Error {
39
+ constructor(errorMsg) {
40
+ super("Failed to symmetrically encrypt email: " + errorMsg);
41
+ Object.setPrototypeOf(this, EmailSymmetricEncryptionError.prototype);
42
+ }
43
+ };
44
+ var EmailSymmetricDecryptionError = class EmailSymmetricDecryptionError extends Error {
45
+ constructor(errorMsg) {
46
+ super("Failed to symmetrically decrypt email: " + errorMsg);
47
+ Object.setPrototypeOf(this, EmailSymmetricDecryptionError.prototype);
48
+ }
49
+ };
50
+ var EmailHybridEncryptionError = class EmailHybridEncryptionError extends Error {
51
+ constructor(errorMsg) {
52
+ super("Failed to hybridly encrypt the key: " + errorMsg);
53
+ Object.setPrototypeOf(this, EmailHybridEncryptionError.prototype);
54
+ }
55
+ };
56
+ var EmailHybridDecryptionError = class EmailHybridDecryptionError extends Error {
57
+ constructor(errorMsg) {
58
+ super("Failed to hybridly decrypt the key: " + errorMsg);
59
+ Object.setPrototypeOf(this, EmailHybridDecryptionError.prototype);
60
+ }
61
+ };
62
+ var EmailPasswordProtectError = class EmailPasswordProtectError extends Error {
63
+ constructor(errorMsg) {
64
+ super("Failed to password-protect the key: " + errorMsg);
65
+ Object.setPrototypeOf(this, EmailPasswordProtectError.prototype);
66
+ }
67
+ };
68
+ var EmailPasswordOpenError = class EmailPasswordOpenError extends Error {
69
+ constructor(errorMsg) {
70
+ super("Failed to open password-protected key: " + errorMsg);
71
+ Object.setPrototypeOf(this, EmailPasswordOpenError.prototype);
72
+ }
73
+ };
74
+ var InvalidInputEmail = class InvalidInputEmail extends Error {
75
+ constructor() {
76
+ super("Invalid input");
77
+ Object.setPrototypeOf(this, InvalidInputEmail.prototype);
78
+ }
79
+ };
80
+ var FailedToDecryptEmail = class FailedToDecryptEmail extends Error {
81
+ constructor(errorMsg) {
82
+ super("Failed to decrypt email: " + errorMsg);
83
+ Object.setPrototypeOf(this, FailedToDecryptEmail.prototype);
84
+ }
85
+ };
86
+ //#endregion
87
+ //#region src/email-crypto/core.ts
88
+ /**
89
+ * Symmetrically encrypts email.
90
+ *
91
+ * @param email - The email to encrypt.
92
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
93
+ * @returns The resulting encrypted email and symmetric key used for encryption
94
+ */
95
+ async function encryptEmail(email, aux) {
96
+ if (!email.text) throw new InvalidInputEmail();
97
+ try {
98
+ const encryptionKey = require_symmetric_crypto.genSymmetricKey();
99
+ return {
100
+ encEmail: await encryptEmailWithKey(email, encryptionKey, aux),
101
+ encryptionKey
102
+ };
103
+ } catch (error) {
104
+ throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
105
+ }
106
+ }
107
+ /**
108
+ * Symmetrically encrypts email with the given key.
109
+ *
110
+ * @param email - The email to encrypt.
111
+ * @param encryptionKey - The symmetric key to encrypt the email.
112
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
113
+ * @returns The resulting encrypted email and symmetric key used for encryption
114
+ */
115
+ async function encryptEmailWithKey(email, encryptionKey, aux) {
116
+ try {
117
+ const enc = { encText: require_utils.uint8ArrayToBase64(await require_symmetric_crypto.encryptSymmetrically(encryptionKey, require_utils.UTF8ToUint8(email.text), aux)) };
118
+ if (email.attachments) {
119
+ const promises = email.attachments.map((attachment) => {
120
+ return require_symmetric_crypto.encryptSymmetrically(encryptionKey, require_utils.UTF8ToUint8(attachment), aux);
121
+ });
122
+ enc.encAttachments = (await Promise.all(promises))?.map(require_utils.uint8ArrayToBase64);
123
+ }
124
+ return enc;
125
+ } catch (error) {
126
+ throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
127
+ }
128
+ }
129
+ /**
130
+ * Decrypts symmetrically encrypted email.
131
+ *
132
+ * @param encEmail - The email to decrypt.
133
+ * @param encryptionKey - The symmetric key to decrypt the email.
134
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
135
+ * @returns The resulting decrypted email
136
+ */
137
+ async function decryptEmail(encEmail, encryptionKey, aux) {
138
+ try {
139
+ const email = { text: require_utils.uint8ToUTF8(await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmail.encText), aux)) };
140
+ if (encEmail.encAttachments) {
141
+ const promises = (encEmail.encAttachments?.map(require_utils.base64ToUint8Array))?.map((encAtt) => require_symmetric_crypto.decryptSymmetrically(encryptionKey, encAtt, aux));
142
+ email.attachments = (await Promise.all(promises))?.map((att) => require_utils.uint8ToUTF8(att));
143
+ }
144
+ return email;
145
+ } catch (error) {
146
+ throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));
147
+ }
148
+ }
149
+ /**
150
+ * Encrypts the email symmetric key using hybrid encryption.
151
+ *
152
+ * @param emailEncryptionKey - The symmetric key used for email encryption.
153
+ * @param recipient - The recipient with a public hybrid key.
154
+ * @returns The encrypted email symmetric key
155
+ */
156
+ async function encryptKeysHybrid(emailEncryptionKey, recipient) {
157
+ try {
158
+ const { cipherText, sharedSecret } = require_xwing.encapsulateHybrid(recipient.publicHybridKey);
159
+ return {
160
+ encryptedKey: require_utils.uint8ArrayToBase64(await wrapKey(emailEncryptionKey, sharedSecret)),
161
+ hybridCiphertext: require_utils.uint8ArrayToBase64(cipherText),
162
+ encryptedForEmail: recipient.email
163
+ };
164
+ } catch (error) {
165
+ throw new EmailHybridEncryptionError(error instanceof Error ? error.message : String(error));
166
+ }
167
+ }
168
+ /**
169
+ * Decrypts the email symmetric key encrypted via hybrid encryption.
170
+ *
171
+ * @param encryptedKey - The encrypted email key.
172
+ * @param recipientPrivateKey - The private key of the recipient.
173
+ * @returns The email encryption key
174
+ */
175
+ async function decryptKeysHybrid(encryptedKey, recipientPrivateKey) {
176
+ try {
177
+ const kyberCiphertext = require_utils.base64ToUint8Array(encryptedKey.hybridCiphertext);
178
+ return await unwrapKey(require_utils.base64ToUint8Array(encryptedKey.encryptedKey), require_xwing.decapsulateHybrid(kyberCiphertext, recipientPrivateKey));
179
+ } catch (error) {
180
+ throw new EmailHybridDecryptionError(error instanceof Error ? error.message : String(error));
181
+ }
182
+ }
183
+ /**
184
+ * Password-protects the email symmetric key.
185
+ *
186
+ * @param emailEncryptionKey - The symmetric key used for email encryption.
187
+ * @param password - The secret password for key protection.
188
+ * @returns The password-protected email symmetric key
189
+ */
190
+ async function passwordProtectKey(emailEncryptionKey, password) {
191
+ try {
192
+ const { key, salt } = await require_derive_password.getKeyFromPassword(password);
193
+ const encryptedKey = await wrapKey(emailEncryptionKey, key);
194
+ const saltStr = require_utils.uint8ArrayToBase64(salt);
195
+ return {
196
+ encryptedKey: require_utils.uint8ArrayToBase64(encryptedKey),
197
+ salt: saltStr
198
+ };
199
+ } catch (error) {
200
+ throw new EmailPasswordProtectError(error instanceof Error ? error.message : String(error));
201
+ }
202
+ }
203
+ /**
204
+ * Removes passoword-protection and exposes the email symmetric key.
205
+ *
206
+ * @param emailEncryptionKey - The password-protected email key.
207
+ * @param password - The secret password for key protection.
208
+ * @returns The email encryption key
209
+ */
210
+ async function removePasswordProtection(emailEncryptionKey, password) {
211
+ try {
212
+ const salt = require_utils.base64ToUint8Array(emailEncryptionKey.salt);
213
+ return await unwrapKey(require_utils.base64ToUint8Array(emailEncryptionKey.encryptedKey), await require_derive_password.getKeyFromPasswordAndSalt(password, salt));
214
+ } catch (error) {
215
+ throw new EmailPasswordOpenError(error instanceof Error ? error.message : String(error));
216
+ }
217
+ }
218
+ //#endregion
219
+ //#region src/email-crypto/hybridEncyptedEmail.ts
220
+ /**
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
+ * Encrypts the email using hybrid encryption for multiple recipients.
244
+ *
245
+ * @param email - The email to encrypt for multiple recipients.
246
+ * @param recipients - The recipients with corresponding public keys.
247
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
248
+ * @returns The set of encrypted emails
249
+ */
250
+ async function encryptEmailHybridForMultipleRecipients(email, recipients, aux) {
251
+ try {
252
+ if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
253
+ const { encryptionKey, encEmail } = await encryptEmail(email, aux);
254
+ const encryptedEmails = [];
255
+ for (const recipient of recipients) {
256
+ const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
257
+ encryptedEmails.push({
258
+ encEmail,
259
+ encryptedKey
260
+ });
261
+ }
262
+ return encryptedEmails;
263
+ } catch (error) {
264
+ if (error instanceof InvalidInputEmail) throw error;
265
+ if (error instanceof EmailSymmetricEncryptionError) throw error;
266
+ if (error instanceof EmailHybridEncryptionError) throw error;
267
+ throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
268
+ }
269
+ }
270
+ /**
271
+ * Decrypts the email using hybrid encryption.
272
+ *
273
+ * @param encEmail - The encrypted email.
274
+ * @param recipientPrivateHybridKeys - The private key of the recipient.
275
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
276
+ * @returns The decrypted email
277
+ */
278
+ async function decryptEmailHybrid(encEmail, recipientPrivateHybridKeys, aux) {
279
+ try {
280
+ const encryptionKey = await decryptKeysHybrid(encEmail.encryptedKey, recipientPrivateHybridKeys);
281
+ return await decryptEmail(encEmail.encEmail, encryptionKey, aux);
282
+ } catch (error) {
283
+ if (error instanceof EmailHybridDecryptionError) throw error;
284
+ if (error instanceof EmailSymmetricDecryptionError) throw error;
285
+ throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
286
+ }
287
+ }
288
+ //#endregion
289
+ //#region src/email-crypto/pwdProtectedEmail.ts
290
+ /**
291
+ * Creates a password-protected email.
292
+ *
293
+ * @param email - The email to password-protect
294
+ * @param password - The secret password shared among recipients
295
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
296
+ * @returns The password-protected email
297
+ */
298
+ async function createPwdProtectedEmail(email, password, aux) {
299
+ try {
300
+ const { encryptionKey, encEmail } = await encryptEmail(email, aux);
301
+ return {
302
+ encEmail,
303
+ encryptedKey: await passwordProtectKey(encryptionKey, password)
304
+ };
305
+ } catch (error) {
306
+ if (error instanceof InvalidInputEmail) throw error;
307
+ if (error instanceof EmailSymmetricEncryptionError) throw error;
308
+ if (error instanceof EmailPasswordProtectError) throw error;
309
+ throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
310
+ }
311
+ }
312
+ /**
313
+ * Opens a password-protected email.
314
+ *
315
+ * @param encryptedEmail - The encrypted email
316
+ * @param password - The secret password shared among recipients.
317
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
318
+ * @returns The decrypted email
319
+ */
320
+ async function decryptPwdProtectedEmail(encryptedEmail, password, aux) {
321
+ if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) throw new InvalidInputEmail();
322
+ try {
323
+ const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
324
+ return await decryptEmail(encryptedEmail.encEmail, encryptionKey, aux);
325
+ } catch (error) {
326
+ if (error instanceof EmailPasswordOpenError) throw error;
327
+ if (error instanceof EmailSymmetricDecryptionError) throw error;
328
+ throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
329
+ }
330
+ }
331
+ //#endregion
332
+ //#region src/email-crypto/coreSubject.ts
333
+ /**
334
+ * Symmetrically encrypts email and subject.
335
+ *
336
+ * @param email - The email and subject to encrypt.
337
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
338
+ * @returns The resulting encrypted email and symmetric key used for encryption
339
+ */
340
+ async function encryptEmailAndSubject(email, aux) {
341
+ if (!email.text || !email.subject) throw new InvalidInputEmail();
342
+ try {
343
+ const encryptionKey = require_symmetric_crypto.genSymmetricKey();
344
+ return {
345
+ encEmail: await encryptEmailAndSubjectWithKey(email, encryptionKey, aux),
346
+ encryptionKey
347
+ };
348
+ } catch (error) {
349
+ throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
350
+ }
351
+ }
352
+ /**
353
+ * Symmetrically encrypts email and subject with the given key.
354
+ *
355
+ * @param email - The email and subject to encrypt.
356
+ * @param encryptionKey - The symmetric key to encrypt the email.
357
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
358
+ * @returns The resulting encrypted email and symmetric key used for encryption
359
+ */
360
+ async function encryptEmailAndSubjectWithKey(email, encryptionKey, aux) {
361
+ try {
362
+ const enc = await encryptEmailWithKey(email, encryptionKey, aux);
363
+ const encSubject = require_utils.uint8ArrayToBase64(await require_symmetric_crypto.encryptSymmetrically(encryptionKey, require_utils.UTF8ToUint8(email.subject), aux));
364
+ return {
365
+ ...enc,
366
+ encSubject
367
+ };
368
+ } catch (error) {
369
+ throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
370
+ }
371
+ }
372
+ /**
373
+ * Decrypts symmetrically encrypted email and email subject.
374
+ *
375
+ * @param encEmail - The encrypted email and subject to decrypt.
376
+ * @param encryptionKey - The symmetric key to decrypt the email.
377
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
378
+ * @returns The resulting decrypted email and subject
379
+ */
380
+ async function decryptEmailAndSubject(encEmail, encryptionKey, aux) {
381
+ try {
382
+ const subject = require_utils.uint8ToUTF8(await require_symmetric_crypto.decryptSymmetrically(encryptionKey, require_utils.base64ToUint8Array(encEmail.encSubject), aux));
383
+ return {
384
+ ...await decryptEmail(encEmail, encryptionKey, aux),
385
+ subject
386
+ };
387
+ } catch (error) {
388
+ throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));
389
+ }
390
+ }
391
+ //#endregion
392
+ //#region src/email-crypto/hybridEncryptedEmailAndSubject.ts
393
+ /**
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
+ * Encrypts the email and its subject using hybrid encryption for multiple recipients.
417
+ *
418
+ * @param email - The email and subject to encrypt for multiple recipients.
419
+ * @param recipients - The recipients with corresponding public keys.
420
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
421
+ * @returns The set of encrypted emails and subjects
422
+ */
423
+ async function encryptEmailAndSubjectHybridForMultipleRecipients(email, recipients, aux) {
424
+ try {
425
+ if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
426
+ const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
427
+ const encryptedEmails = [];
428
+ for (const recipient of recipients) {
429
+ const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
430
+ encryptedEmails.push({
431
+ encEmail,
432
+ encryptedKey
433
+ });
434
+ }
435
+ return encryptedEmails;
436
+ } catch (error) {
437
+ if (error instanceof InvalidInputEmail) throw error;
438
+ if (error instanceof EmailSymmetricEncryptionError) throw error;
439
+ if (error instanceof EmailHybridEncryptionError) throw error;
440
+ throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
441
+ }
442
+ }
443
+ /**
444
+ * Decrypts the email and its subject using hybrid encryption.
445
+ *
446
+ * @param hybridEmail - The encrypted email and subject.
447
+ * @param recipientPrivateHybridKeys - The private key of the recipient.
448
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
449
+ * @returns The decrypted email and subject
450
+ */
451
+ async function decryptEmailAndSubjectHybrid(hybridEmail, recipientPrivateHybridKeys, aux) {
452
+ try {
453
+ const encryptionKey = await decryptKeysHybrid(hybridEmail.encryptedKey, recipientPrivateHybridKeys);
454
+ return await decryptEmailAndSubject(hybridEmail.encEmail, encryptionKey, aux);
455
+ } catch (error) {
456
+ if (error instanceof InvalidInputEmail) throw error;
457
+ if (error instanceof EmailHybridDecryptionError) throw error;
458
+ if (error instanceof EmailSymmetricDecryptionError) throw error;
459
+ throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
460
+ }
461
+ }
462
+ //#endregion
463
+ //#region src/email-crypto/pwdProtectedEmailAndSubject.ts
464
+ /**
465
+ * Creates a password-protected email and subject.
466
+ *
467
+ * @param email - The email and subject to password-protect
468
+ * @param password - The secret password shared among recipients
469
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
470
+ * @returns The password-protected email
471
+ */
472
+ async function createPwdProtectedEmailAndSubject(email, password, aux) {
473
+ try {
474
+ const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
475
+ return {
476
+ encEmail,
477
+ encryptedKey: await passwordProtectKey(encryptionKey, password)
478
+ };
479
+ } catch (error) {
480
+ if (error instanceof InvalidInputEmail) throw error;
481
+ if (error instanceof EmailSymmetricEncryptionError) throw error;
482
+ if (error instanceof EmailPasswordProtectError) throw error;
483
+ throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
484
+ }
485
+ }
486
+ /**
487
+ * Opens a password-protected email and subject.
488
+ *
489
+ * @param encryptedEmail - The encrypted email and subject
490
+ * @param password - The secret password shared among recipients.
491
+ * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
492
+ * @returns The decrypted email and subject
493
+ */
494
+ async function decryptPwdProtectedEmailAndSubject(encryptedEmail, password, aux) {
495
+ if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) throw new InvalidInputEmail();
496
+ try {
497
+ const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
498
+ return await decryptEmailAndSubject(encryptedEmail.encEmail, encryptionKey, aux);
499
+ } catch (error) {
500
+ if (error instanceof EmailPasswordOpenError) throw error;
501
+ if (error instanceof EmailSymmetricDecryptionError) throw error;
502
+ throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
503
+ }
504
+ }
505
+ //#endregion
506
+ //#region src/email-crypto/emailKeys.ts
507
+ /**
508
+ * Generates public and private keys for email encryption.
509
+ *
510
+ * @returns The user's private and public keys
511
+ */
512
+ async function generateEmailKeys() {
513
+ return require_xwing.genHybridKeys();
514
+ }
515
+ /**
516
+ * Derives database encryption key for the given user
517
+ *
518
+ * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
519
+ * @returns The symmetric key for protecting database
520
+ */
521
+ const deriveDatabaseKey = async (mnemonic) => {
522
+ return require_derive_key.deriveKeyFromMnemonic(mnemonic, require_constants.CONTEXT_DATABASE);
523
+ };
524
+ /**
525
+ * Derives email draft encryption key for the given user
526
+ *
527
+ * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
528
+ * @returns The symmetric key for protecting email drafts
529
+ */
530
+ const deriveEmailDraftKey = async (mnemonic) => {
531
+ return require_derive_key.deriveKeyFromMnemonic(mnemonic, require_constants.CONTEXT_DRAFT);
532
+ };
533
+ //#endregion
534
+ Object.defineProperty(exports, "EmailHybridDecryptionError", {
535
+ enumerable: true,
536
+ get: function() {
537
+ return EmailHybridDecryptionError;
538
+ }
539
+ });
540
+ Object.defineProperty(exports, "EmailHybridEncryptionError", {
541
+ enumerable: true,
542
+ get: function() {
543
+ return EmailHybridEncryptionError;
544
+ }
545
+ });
546
+ Object.defineProperty(exports, "EmailPasswordOpenError", {
547
+ enumerable: true,
548
+ get: function() {
549
+ return EmailPasswordOpenError;
550
+ }
551
+ });
552
+ Object.defineProperty(exports, "EmailPasswordProtectError", {
553
+ enumerable: true,
554
+ get: function() {
555
+ return EmailPasswordProtectError;
556
+ }
557
+ });
558
+ Object.defineProperty(exports, "EmailSymmetricDecryptionError", {
559
+ enumerable: true,
560
+ get: function() {
561
+ return EmailSymmetricDecryptionError;
562
+ }
563
+ });
564
+ Object.defineProperty(exports, "EmailSymmetricEncryptionError", {
565
+ enumerable: true,
566
+ get: function() {
567
+ return EmailSymmetricEncryptionError;
568
+ }
569
+ });
570
+ Object.defineProperty(exports, "FailedToDecryptEmail", {
571
+ enumerable: true,
572
+ get: function() {
573
+ return FailedToDecryptEmail;
574
+ }
575
+ });
576
+ Object.defineProperty(exports, "FailedToEncryptEmail", {
577
+ enumerable: true,
578
+ get: function() {
579
+ return FailedToEncryptEmail;
580
+ }
581
+ });
582
+ Object.defineProperty(exports, "InvalidInputEmail", {
583
+ enumerable: true,
584
+ get: function() {
585
+ return InvalidInputEmail;
586
+ }
587
+ });
588
+ Object.defineProperty(exports, "createPwdProtectedEmail", {
589
+ enumerable: true,
590
+ get: function() {
591
+ return createPwdProtectedEmail;
592
+ }
593
+ });
594
+ Object.defineProperty(exports, "createPwdProtectedEmailAndSubject", {
595
+ enumerable: true,
596
+ get: function() {
597
+ return createPwdProtectedEmailAndSubject;
598
+ }
599
+ });
600
+ Object.defineProperty(exports, "decryptEmail", {
601
+ enumerable: true,
602
+ get: function() {
603
+ return decryptEmail;
604
+ }
605
+ });
606
+ Object.defineProperty(exports, "decryptEmailAndSubject", {
607
+ enumerable: true,
608
+ get: function() {
609
+ return decryptEmailAndSubject;
610
+ }
611
+ });
612
+ Object.defineProperty(exports, "decryptEmailAndSubjectHybrid", {
613
+ enumerable: true,
614
+ get: function() {
615
+ return decryptEmailAndSubjectHybrid;
616
+ }
617
+ });
618
+ Object.defineProperty(exports, "decryptEmailHybrid", {
619
+ enumerable: true,
620
+ get: function() {
621
+ return decryptEmailHybrid;
622
+ }
623
+ });
624
+ Object.defineProperty(exports, "decryptKeysHybrid", {
625
+ enumerable: true,
626
+ get: function() {
627
+ return decryptKeysHybrid;
628
+ }
629
+ });
630
+ Object.defineProperty(exports, "decryptPwdProtectedEmail", {
631
+ enumerable: true,
632
+ get: function() {
633
+ return decryptPwdProtectedEmail;
634
+ }
635
+ });
636
+ Object.defineProperty(exports, "decryptPwdProtectedEmailAndSubject", {
637
+ enumerable: true,
638
+ get: function() {
639
+ return decryptPwdProtectedEmailAndSubject;
640
+ }
641
+ });
642
+ Object.defineProperty(exports, "deriveDatabaseKey", {
643
+ enumerable: true,
644
+ get: function() {
645
+ return deriveDatabaseKey;
646
+ }
647
+ });
648
+ Object.defineProperty(exports, "deriveEmailDraftKey", {
649
+ enumerable: true,
650
+ get: function() {
651
+ return deriveEmailDraftKey;
652
+ }
653
+ });
654
+ Object.defineProperty(exports, "encryptEmail", {
655
+ enumerable: true,
656
+ get: function() {
657
+ return encryptEmail;
658
+ }
659
+ });
660
+ Object.defineProperty(exports, "encryptEmailAndSubject", {
661
+ enumerable: true,
662
+ get: function() {
663
+ return encryptEmailAndSubject;
664
+ }
665
+ });
666
+ Object.defineProperty(exports, "encryptEmailAndSubjectHybrid", {
667
+ enumerable: true,
668
+ get: function() {
669
+ return encryptEmailAndSubjectHybrid;
670
+ }
671
+ });
672
+ Object.defineProperty(exports, "encryptEmailAndSubjectHybridForMultipleRecipients", {
673
+ enumerable: true,
674
+ get: function() {
675
+ return encryptEmailAndSubjectHybridForMultipleRecipients;
676
+ }
677
+ });
678
+ Object.defineProperty(exports, "encryptEmailAndSubjectWithKey", {
679
+ enumerable: true,
680
+ get: function() {
681
+ return encryptEmailAndSubjectWithKey;
682
+ }
683
+ });
684
+ Object.defineProperty(exports, "encryptEmailHybrid", {
685
+ enumerable: true,
686
+ get: function() {
687
+ return encryptEmailHybrid;
688
+ }
689
+ });
690
+ Object.defineProperty(exports, "encryptEmailHybridForMultipleRecipients", {
691
+ enumerable: true,
692
+ get: function() {
693
+ return encryptEmailHybridForMultipleRecipients;
694
+ }
695
+ });
696
+ Object.defineProperty(exports, "encryptEmailWithKey", {
697
+ enumerable: true,
698
+ get: function() {
699
+ return encryptEmailWithKey;
700
+ }
701
+ });
702
+ Object.defineProperty(exports, "encryptKeysHybrid", {
703
+ enumerable: true,
704
+ get: function() {
705
+ return encryptKeysHybrid;
706
+ }
707
+ });
708
+ Object.defineProperty(exports, "generateEmailKeys", {
709
+ enumerable: true,
710
+ get: function() {
711
+ return generateEmailKeys;
712
+ }
713
+ });
714
+ Object.defineProperty(exports, "passwordProtectKey", {
715
+ enumerable: true,
716
+ get: function() {
717
+ return passwordProtectKey;
718
+ }
719
+ });
720
+ Object.defineProperty(exports, "removePasswordProtection", {
721
+ enumerable: true,
722
+ get: function() {
723
+ return removePasswordProtection;
724
+ }
725
+ });
726
+ Object.defineProperty(exports, "unwrapKey", {
727
+ enumerable: true,
728
+ get: function() {
729
+ return unwrapKey;
730
+ }
731
+ });
732
+ Object.defineProperty(exports, "wrapKey", {
733
+ enumerable: true,
734
+ get: function() {
735
+ return wrapKey;
736
+ }
737
+ });
738
+
739
+ //# sourceMappingURL=email-crypto-Di814Pbk.js.map