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.
- package/README.md +15 -73
- package/dist/email-crypto-Di814Pbk.js +739 -0
- package/dist/email-crypto-Di814Pbk.js.map +1 -0
- package/dist/email-crypto-DpjfXDes.mjs +535 -0
- package/dist/email-crypto-DpjfXDes.mjs.map +1 -0
- package/dist/email-crypto.d.mts +138 -29
- package/dist/email-crypto.d.mts.map +1 -1
- package/dist/email-crypto.d.ts +138 -29
- package/dist/email-crypto.d.ts.map +1 -1
- package/dist/email-crypto.js +21 -4
- package/dist/email-crypto.mjs +2 -2
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +21 -4
- package/dist/index.mjs +2 -2
- package/dist/keystore-crypto.d.mts +1 -1
- package/dist/keystore-crypto.d.ts +1 -1
- package/dist/{types-4P0WII1D.d.ts → types-CrAiKyb0.d.ts} +20 -8
- package/dist/types-CrAiKyb0.d.ts.map +1 -0
- package/dist/{types-DAPJQG8U.d.mts → types-D6hsUfma.d.mts} +20 -8
- package/dist/types-D6hsUfma.d.mts.map +1 -0
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/types.js.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/email-crypto-Cr7znO1a.js +0 -401
- package/dist/email-crypto-Cr7znO1a.js.map +0 -1
- package/dist/email-crypto-cHDsjSb5.mjs +0 -299
- package/dist/email-crypto-cHDsjSb5.mjs.map +0 -1
- package/dist/types-4P0WII1D.d.ts.map +0 -1
- package/dist/types-DAPJQG8U.d.mts.map +0 -1
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import { CONTEXT_DATABASE, CONTEXT_DRAFT } from "./constants.mjs";
|
|
2
|
-
import { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from "./utils.mjs";
|
|
3
|
-
import { deriveKeyFromMnemonic } from "./derive-key.mjs";
|
|
4
|
-
import { n as getKeyFromPassword, r as getKeyFromPasswordAndSalt } from "./derive-password-PED-YfwE.mjs";
|
|
5
|
-
import { decryptSymmetrically, encryptSymmetrically, genSymmetricKey } from "./symmetric-crypto.mjs";
|
|
6
|
-
import { n as encapsulateHybrid, r as genHybridKeys, t as decapsulateHybrid } from "./xwing-B34h5e3Z.mjs";
|
|
7
|
-
import { aeskw } from "@noble/ciphers/aes.js";
|
|
8
|
-
//#region src/key-wrapper/aesWrapper.ts
|
|
9
|
-
/**
|
|
10
|
-
* Unwraps the given wrapped key
|
|
11
|
-
*
|
|
12
|
-
* @param encryptedKey - The wrapped key
|
|
13
|
-
* @param wrappingKey - The secret key used for unwrapping
|
|
14
|
-
* @returns The resulting key
|
|
15
|
-
*/
|
|
16
|
-
async function unwrapKey(encryptedKey, wrappingKey) {
|
|
17
|
-
return aeskw(wrappingKey).decrypt(encryptedKey);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Wraps the given key
|
|
21
|
-
*
|
|
22
|
-
* @param key - The key to be wrapped
|
|
23
|
-
* @param wrappingKey - The secret key used for wrapping
|
|
24
|
-
* @returns The resulting ciphertext
|
|
25
|
-
*/
|
|
26
|
-
async function wrapKey(key, wrappingKey) {
|
|
27
|
-
return aeskw(wrappingKey).encrypt(key);
|
|
28
|
-
}
|
|
29
|
-
//#endregion
|
|
30
|
-
//#region src/email-crypto/core.ts
|
|
31
|
-
/**
|
|
32
|
-
* Symmetrically encrypts email body.
|
|
33
|
-
*
|
|
34
|
-
* @param body - The email body to encrypt.
|
|
35
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
36
|
-
* @returns The resulting encrypted email body and symmetric key used for encryption
|
|
37
|
-
*/
|
|
38
|
-
async function encryptEmailBody(body, aux) {
|
|
39
|
-
try {
|
|
40
|
-
if (!body.text || !body.subject) throw new Error("Invalid input");
|
|
41
|
-
const encryptionKey = genSymmetricKey();
|
|
42
|
-
return {
|
|
43
|
-
encEmailBody: await encryptEmailBodyWithKey(body, encryptionKey, aux),
|
|
44
|
-
encryptionKey
|
|
45
|
-
};
|
|
46
|
-
} catch (error) {
|
|
47
|
-
throw new Error("Failed to symmetrically encrypt email body", { cause: error });
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Symmetrically encrypts email body with the given key.
|
|
52
|
-
*
|
|
53
|
-
* @param body - The email body to encrypt.
|
|
54
|
-
* @param encryptionKey - The symmetric key to encrypt the email.
|
|
55
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
56
|
-
* @returns The resulting encrypted email body and symmetric key used for encryption
|
|
57
|
-
*/
|
|
58
|
-
async function encryptEmailBodyWithKey(body, encryptionKey, aux) {
|
|
59
|
-
try {
|
|
60
|
-
const text = UTF8ToUint8(body.text);
|
|
61
|
-
const subjectEnc = await encryptSymmetrically(encryptionKey, UTF8ToUint8(body.subject), aux);
|
|
62
|
-
const enc = {
|
|
63
|
-
encText: uint8ArrayToBase64(await encryptSymmetrically(encryptionKey, text, aux)),
|
|
64
|
-
encSubject: uint8ArrayToBase64(subjectEnc)
|
|
65
|
-
};
|
|
66
|
-
if (body.attachments) {
|
|
67
|
-
const promises = body.attachments.map((attachment) => {
|
|
68
|
-
return encryptSymmetrically(encryptionKey, UTF8ToUint8(attachment), aux);
|
|
69
|
-
});
|
|
70
|
-
enc.encAttachments = (await Promise.all(promises))?.map(uint8ArrayToBase64);
|
|
71
|
-
}
|
|
72
|
-
return enc;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
throw new Error("Failed to encrypt email body", { cause: error });
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Decrypts symmetrically encrypted email body.
|
|
79
|
-
*
|
|
80
|
-
* @param encEmailBody - The email body to decrypt.
|
|
81
|
-
* @param encryptionKey - The symmetric key to decrypt the email.
|
|
82
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
83
|
-
* @returns The resulting decrypted email body
|
|
84
|
-
*/
|
|
85
|
-
async function decryptEmailBody(encEmailBody, encryptionKey, aux) {
|
|
86
|
-
try {
|
|
87
|
-
const subject = uint8ToUTF8(await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmailBody.encSubject), aux));
|
|
88
|
-
const body = {
|
|
89
|
-
text: uint8ToUTF8(await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmailBody.encText), aux)),
|
|
90
|
-
subject
|
|
91
|
-
};
|
|
92
|
-
if (encEmailBody.encAttachments) {
|
|
93
|
-
const promises = (encEmailBody.encAttachments?.map(base64ToUint8Array))?.map((encAtt) => decryptSymmetrically(encryptionKey, encAtt, aux));
|
|
94
|
-
body.attachments = (await Promise.all(promises))?.map((att) => uint8ToUTF8(att));
|
|
95
|
-
}
|
|
96
|
-
return body;
|
|
97
|
-
} catch (error) {
|
|
98
|
-
throw new Error("Failed to symmetrically decrypt email body", { cause: error });
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Encrypts the email symmetric key using hybrid encryption.
|
|
103
|
-
*
|
|
104
|
-
* @param emailEncryptionKey - The symmetric key used for email encryption.
|
|
105
|
-
* @param recipient - The recipient with a public hybrid key.
|
|
106
|
-
* @returns The encrypted email symmetric key
|
|
107
|
-
*/
|
|
108
|
-
async function encryptKeysHybrid(emailEncryptionKey, recipient) {
|
|
109
|
-
try {
|
|
110
|
-
const { cipherText, sharedSecret } = encapsulateHybrid(recipient.publicHybridKey);
|
|
111
|
-
return {
|
|
112
|
-
encryptedKey: uint8ArrayToBase64(await wrapKey(emailEncryptionKey, sharedSecret)),
|
|
113
|
-
hybridCiphertext: uint8ArrayToBase64(cipherText),
|
|
114
|
-
encryptedForEmail: recipient.email
|
|
115
|
-
};
|
|
116
|
-
} catch (error) {
|
|
117
|
-
throw new Error("Failed to encrypt email key using hybrid encryption", { cause: error });
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Decrypts the email symmetric key encrypted via hybrid encryption.
|
|
122
|
-
*
|
|
123
|
-
* @param encryptedKey - The encrypted email key.
|
|
124
|
-
* @param recipientPrivateKey - The private key of the recipient.
|
|
125
|
-
* @returns The email encryption key
|
|
126
|
-
*/
|
|
127
|
-
async function decryptKeysHybrid(encryptedKey, recipientPrivateKey) {
|
|
128
|
-
try {
|
|
129
|
-
const kyberCiphertext = base64ToUint8Array(encryptedKey.hybridCiphertext);
|
|
130
|
-
return await unwrapKey(base64ToUint8Array(encryptedKey.encryptedKey), decapsulateHybrid(kyberCiphertext, recipientPrivateKey));
|
|
131
|
-
} catch (error) {
|
|
132
|
-
throw new Error("Failed to decrypt email key encrypted via hybrid encryption", { cause: error });
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Password-protects the email symmetric key.
|
|
137
|
-
*
|
|
138
|
-
* @param emailEncryptionKey - The symmetric key used for email encryption.
|
|
139
|
-
* @param password - The secret password for key protection.
|
|
140
|
-
* @returns The password-protected email symmetric key
|
|
141
|
-
*/
|
|
142
|
-
async function passwordProtectKey(emailEncryptionKey, password) {
|
|
143
|
-
try {
|
|
144
|
-
const { key, salt } = await getKeyFromPassword(password);
|
|
145
|
-
const encryptedKey = await wrapKey(emailEncryptionKey, key);
|
|
146
|
-
const saltStr = uint8ArrayToBase64(salt);
|
|
147
|
-
return {
|
|
148
|
-
encryptedKey: uint8ArrayToBase64(encryptedKey),
|
|
149
|
-
salt: saltStr
|
|
150
|
-
};
|
|
151
|
-
} catch (error) {
|
|
152
|
-
throw new Error("Failed to password-protect email key", { cause: error });
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Removes passoword-protection and exposes the email symmetric key.
|
|
157
|
-
*
|
|
158
|
-
* @param emailEncryptionKey - The password-protected email key.
|
|
159
|
-
* @param password - The secret password for key protection.
|
|
160
|
-
* @returns The email encryption key
|
|
161
|
-
*/
|
|
162
|
-
async function removePasswordProtection(emailEncryptionKey, password) {
|
|
163
|
-
try {
|
|
164
|
-
const salt = base64ToUint8Array(emailEncryptionKey.salt);
|
|
165
|
-
return await unwrapKey(base64ToUint8Array(emailEncryptionKey.encryptedKey), await getKeyFromPasswordAndSalt(password, salt));
|
|
166
|
-
} catch (error) {
|
|
167
|
-
throw new Error("Failed to remove password-protection from email key", { cause: error });
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
//#endregion
|
|
171
|
-
//#region src/email-crypto/hybridEncyptedEmail.ts
|
|
172
|
-
/**
|
|
173
|
-
* Encrypts the email body using hybrid encryption.
|
|
174
|
-
*
|
|
175
|
-
* @param body - The email body to encrypt.
|
|
176
|
-
* @param recipientPublicKeys - The public keys of the recipient.
|
|
177
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
178
|
-
* @returns The encrypted email body
|
|
179
|
-
*/
|
|
180
|
-
async function encryptEmailHybrid(body, recipient, aux) {
|
|
181
|
-
try {
|
|
182
|
-
const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);
|
|
183
|
-
return {
|
|
184
|
-
encEmailBody,
|
|
185
|
-
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
186
|
-
};
|
|
187
|
-
} catch (error) {
|
|
188
|
-
throw new Error("Failed to encrypt email body with hybrid encryption", { cause: error });
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Encrypts the email body using hybrid encryption for multiple recipients.
|
|
193
|
-
*
|
|
194
|
-
* @param body - The email body to encrypt for multiple recipients.
|
|
195
|
-
* @param recipients - The recipients with corresponding public keys.
|
|
196
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
197
|
-
* @returns The set of encrypted email bodies
|
|
198
|
-
*/
|
|
199
|
-
async function encryptEmailHybridForMultipleRecipients(body, recipients, aux) {
|
|
200
|
-
try {
|
|
201
|
-
const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);
|
|
202
|
-
const encryptedEmails = [];
|
|
203
|
-
for (const recipient of recipients) {
|
|
204
|
-
const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
|
|
205
|
-
encryptedEmails.push({
|
|
206
|
-
encEmailBody,
|
|
207
|
-
encryptedKey
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
return encryptedEmails;
|
|
211
|
-
} catch (error) {
|
|
212
|
-
throw new Error("Failed to encrypt email to multiple recipients with hybrid encryption", { cause: error });
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Decrypts the email using hybrid encryption.
|
|
217
|
-
*
|
|
218
|
-
* @param encEmailBody - The encrypted email.
|
|
219
|
-
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
220
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
221
|
-
* @returns The decrypted email body
|
|
222
|
-
*/
|
|
223
|
-
async function decryptEmailHybrid(encEmailBody, recipientPrivateHybridKeys, aux) {
|
|
224
|
-
try {
|
|
225
|
-
const encryptionKey = await decryptKeysHybrid(encEmailBody.encryptedKey, recipientPrivateHybridKeys);
|
|
226
|
-
return await decryptEmailBody(encEmailBody.encEmailBody, encryptionKey, aux);
|
|
227
|
-
} catch (error) {
|
|
228
|
-
throw new Error("Failed to decrypt email with hybrid encryption", { cause: error });
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
//#endregion
|
|
232
|
-
//#region src/email-crypto/pwdProtectedEmail.ts
|
|
233
|
-
/**
|
|
234
|
-
* Creates a password-protected email.
|
|
235
|
-
*
|
|
236
|
-
* @param email - The email to password-protect
|
|
237
|
-
* @param password - The secret password shared among recipients
|
|
238
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
239
|
-
* @returns The password-protected email
|
|
240
|
-
*/
|
|
241
|
-
async function createPwdProtectedEmail(emailBody, password, aux) {
|
|
242
|
-
try {
|
|
243
|
-
const { encryptionKey, encEmailBody } = await encryptEmailBody(emailBody, aux);
|
|
244
|
-
return {
|
|
245
|
-
encEmailBody,
|
|
246
|
-
encryptedKey: await passwordProtectKey(encryptionKey, password)
|
|
247
|
-
};
|
|
248
|
-
} catch (error) {
|
|
249
|
-
throw new Error("Failed to password-protect email", { cause: error });
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Opens a password-protected email.
|
|
254
|
-
*
|
|
255
|
-
* @param encryptedEmail - The encrypted email
|
|
256
|
-
* @param password - The secret password shared among recipients.
|
|
257
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
258
|
-
* @returns The decrypted email body
|
|
259
|
-
*/
|
|
260
|
-
async function decryptPwdProtectedEmail(encryptedEmail, password, aux) {
|
|
261
|
-
try {
|
|
262
|
-
const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
|
|
263
|
-
return await decryptEmailBody(encryptedEmail.encEmailBody, encryptionKey, aux);
|
|
264
|
-
} catch (error) {
|
|
265
|
-
throw new Error("Failed to decrypt password-protect email", { cause: error });
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
//#endregion
|
|
269
|
-
//#region src/email-crypto/emailKeys.ts
|
|
270
|
-
/**
|
|
271
|
-
* Generates public and private keys for email encryption.
|
|
272
|
-
*
|
|
273
|
-
* @returns The user's private and public keys
|
|
274
|
-
*/
|
|
275
|
-
async function generateEmailKeys() {
|
|
276
|
-
return genHybridKeys();
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Derives database encryption key for the given user
|
|
280
|
-
*
|
|
281
|
-
* @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
|
|
282
|
-
* @returns The symmetric key for protecting database
|
|
283
|
-
*/
|
|
284
|
-
const deriveDatabaseKey = async (mnemonic) => {
|
|
285
|
-
return deriveKeyFromMnemonic(mnemonic, CONTEXT_DATABASE);
|
|
286
|
-
};
|
|
287
|
-
/**
|
|
288
|
-
* Derives email draft encryption key for the given user
|
|
289
|
-
*
|
|
290
|
-
* @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
|
|
291
|
-
* @returns The symmetric key for protecting email drafts
|
|
292
|
-
*/
|
|
293
|
-
const deriveEmailDraftKey = async (mnemonic) => {
|
|
294
|
-
return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);
|
|
295
|
-
};
|
|
296
|
-
//#endregion
|
|
297
|
-
export { wrapKey as _, decryptPwdProtectedEmail as a, encryptEmailHybridForMultipleRecipients as c, encryptEmailBody as d, encryptEmailBodyWithKey as f, unwrapKey as g, removePasswordProtection as h, createPwdProtectedEmail as i, decryptEmailBody as l, passwordProtectKey as m, deriveEmailDraftKey as n, decryptEmailHybrid as o, encryptKeysHybrid as p, generateEmailKeys as r, encryptEmailHybrid as s, deriveDatabaseKey as t, decryptKeysHybrid as u };
|
|
298
|
-
|
|
299
|
-
//# sourceMappingURL=email-crypto-cHDsjSb5.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"email-crypto-cHDsjSb5.mjs","names":[],"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 = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);\n};\n"],"mappings":";;;;;;;;;;;;;;;AASA,eAAsB,UAAU,cAA0B,aAA8C;AACtG,QAAO,MAAM,YAAY,CAAC,QAAQ,aAAa;;;;;;;;;AAUjD,eAAsB,QAAQ,KAAiB,aAA8C;AAC3F,QAAO,MAAM,YAAY,CAAC,QAAQ,IAAI;;;;;;;;;;;ACPxC,eAAsB,iBACpB,MACA,KAIC;AACD,KAAI;AACF,MAAI,CAAC,KAAK,QAAQ,CAAC,KAAK,QACtB,OAAM,IAAI,MAAM,gBAAgB;EAElC,MAAM,gBAAgB,iBAAiB;AAGvC,SAAO;GAAE,cAAA,MAFkB,wBAAwB,MAAM,eAAe,IAAI;GAErD;GAAe;UAC/B,OAAO;AACd,QAAM,IAAI,MAAM,8CAA8C,EAAE,OAAO,OAAO,CAAC;;;;;;;;;;;AAYnF,eAAs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sB,UAAU,cAAc"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-4P0WII1D.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";KAAY,iBAAA;EACV,SAAA;EACA,IAAA,EAAM,YAAA;EACN,SAAA;EACA,mBAAA;AAAA;AAAA,KAGU,sBAAA;EACV,KAAA;EACA,eAAA,EAAiB,UAAA;AAAA;AAAA,KAGP,aAAA;EACV,SAAA,EAAW,UAAA;EACX,SAAA,EAAW,UAAA;AAAA;AAAA,KAGD,oBAAA;EACV,YAAA,EAAc,YAAA;EACd,YAAA,EAAc,kBAAA;AAAA;AAAA,KAGJ,iBAAA;EACV,YAAA,EAAc,eAAA;EACd,YAAA,EAAc,kBAAA;AAAA;AAAA,KAGJ,YAAA;EACV,gBAAA;EACA,YAAA;EACA,iBAAA;AAAA;AAAA,KAGU,eAAA;EACV,YAAA;EACA,IAAA;AAAA;AAAA,KAGU,kBAAA;EACV,OAAA;EACA,UAAA;EACA,cAAA;AAAA;AAAA,KAGU,SAAA;EACV,IAAA;EACA,OAAA;EACA,WAAA;AAAA;AAAA,aAGU,YAAA;EACV,UAAA;EACA,QAAA;AAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-DAPJQG8U.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";KAAY,iBAAA;EACV,SAAA;EACA,IAAA,EAAM,YAAA;EACN,SAAA;EACA,mBAAA;AAAA;AAAA,KAGU,sBAAA;EACV,KAAA;EACA,eAAA,EAAiB,UAAA;AAAA;AAAA,KAGP,aAAA;EACV,SAAA,EAAW,UAAA;EACX,SAAA,EAAW,UAAA;AAAA;AAAA,KAGD,oBAAA;EACV,YAAA,EAAc,YAAA;EACd,YAAA,EAAc,kBAAA;AAAA;AAAA,KAGJ,iBAAA;EACV,YAAA,EAAc,eAAA;EACd,YAAA,EAAc,kBAAA;AAAA;AAAA,KAGJ,YAAA;EACV,gBAAA;EACA,YAAA;EACA,iBAA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