internxt-crypto 1.5.0 → 1.6.1

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