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.
- package/README.md +15 -73
- package/dist/asymmetric-crypto.js +3 -4
- package/dist/asymmetric-crypto.js.map +1 -1
- package/dist/asymmetric-crypto.mjs +1 -2
- package/dist/asymmetric-crypto.mjs.map +1 -1
- package/dist/chunk-C13QxCFV.js +50 -0
- package/dist/chunk-CfYAbeIz.mjs +13 -0
- package/dist/constants.js +2 -3
- package/dist/constants.js.map +1 -1
- package/dist/constants.mjs +1 -1
- package/dist/derive-key.js +50 -5
- package/dist/derive-key.js.map +1 -0
- package/dist/derive-key.mjs +46 -2
- package/dist/derive-key.mjs.map +1 -0
- package/dist/{deriveKeysFromPassword-iMHv-tIA.js → derive-password-CbYRDq7V.js} +25 -30
- package/dist/derive-password-CbYRDq7V.js.map +1 -0
- package/dist/{deriveKeysFromPassword-DFzo2OII.mjs → derive-password-PED-YfwE.mjs} +27 -34
- package/dist/derive-password-PED-YfwE.mjs.map +1 -0
- package/dist/derive-password.js +5 -6
- package/dist/derive-password.mjs +2 -3
- 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 +34 -18
- package/dist/email-crypto.mjs +2 -3
- package/dist/hash.js +3 -4
- package/dist/hash.js.map +1 -1
- package/dist/hash.mjs +1 -2
- package/dist/hash.mjs.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +72 -56
- package/dist/index.mjs +7 -8
- package/dist/{emailEncryptionKey-CYvr26FG.mjs → keystore-crypto-C6F-NQdX.mjs} +12 -15
- package/dist/keystore-crypto-C6F-NQdX.mjs.map +1 -0
- package/dist/{emailEncryptionKey-Ba95VuFO.js → keystore-crypto-CFiiGohl.js} +67 -70
- package/dist/keystore-crypto-CFiiGohl.js.map +1 -0
- package/dist/keystore-crypto.d.mts +1 -1
- package/dist/keystore-crypto.d.ts +1 -1
- package/dist/keystore-crypto.js +11 -12
- package/dist/keystore-crypto.mjs +2 -3
- package/dist/post-quantum-crypto.js +3 -4
- package/dist/post-quantum-crypto.js.map +1 -1
- package/dist/post-quantum-crypto.mjs +1 -2
- package/dist/post-quantum-crypto.mjs.map +1 -1
- package/dist/storage-service.js +13 -15
- package/dist/storage-service.js.map +1 -1
- package/dist/storage-service.mjs +2 -4
- package/dist/storage-service.mjs.map +1 -1
- package/dist/symmetric-crypto.js +51 -5
- package/dist/symmetric-crypto.js.map +1 -0
- package/dist/symmetric-crypto.mjs +47 -2
- package/dist/symmetric-crypto.mjs.map +1 -0
- package/dist/types-CrAiKyb0.d.ts +61 -0
- package/dist/types-CrAiKyb0.d.ts.map +1 -0
- package/dist/types-D6hsUfma.d.mts +61 -0
- package/dist/types-D6hsUfma.d.mts.map +1 -0
- package/dist/types.d.mts +2 -49
- package/dist/types.d.ts +2 -49
- package/dist/types.js +2 -3
- package/dist/types.js.map +1 -1
- package/dist/types.mjs +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils.js +127 -14
- package/dist/utils.js.map +1 -0
- package/dist/utils.mjs +113 -2
- package/dist/utils.mjs.map +1 -0
- package/dist/{xwing-iRWVRtNE.js → xwing-7fEqAT7W.js} +18 -19
- package/dist/{xwing-iRWVRtNE.js.map → xwing-7fEqAT7W.js.map} +1 -1
- package/dist/{xwing-CQmQX6hn.mjs → xwing-B34h5e3Z.mjs} +2 -3
- package/dist/{xwing-CQmQX6hn.mjs.map → xwing-B34h5e3Z.mjs.map} +1 -1
- package/package.json +22 -22
- package/dist/aes-B8TA-JKq.mjs +0 -49
- package/dist/aes-B8TA-JKq.mjs.map +0 -1
- package/dist/aes-CuW_SPl-.js +0 -67
- package/dist/aes-CuW_SPl-.js.map +0 -1
- package/dist/chunk-DQk6qfdC.mjs +0 -18
- package/dist/chunk-EbLFQX5m.js +0 -60
- package/dist/deriveKeysFromMnemonic-DB0UHE-y.mjs +0 -49
- package/dist/deriveKeysFromMnemonic-DB0UHE-y.mjs.map +0 -1
- package/dist/deriveKeysFromMnemonic-Dbx7VSBQ.js +0 -67
- package/dist/deriveKeysFromMnemonic-Dbx7VSBQ.js.map +0 -1
- package/dist/deriveKeysFromPassword-DFzo2OII.mjs.map +0 -1
- package/dist/deriveKeysFromPassword-iMHv-tIA.js.map +0 -1
- package/dist/emailEncryptionKey-Ba95VuFO.js.map +0 -1
- package/dist/emailEncryptionKey-CYvr26FG.mjs.map +0 -1
- package/dist/emailKeys-BFI-paQq.js +0 -406
- package/dist/emailKeys-BFI-paQq.js.map +0 -1
- package/dist/emailKeys-BjIeINfC.mjs +0 -304
- package/dist/emailKeys-BjIeINfC.mjs.map +0 -1
- package/dist/genID-D08K7JPr.mjs +0 -117
- package/dist/genID-D08K7JPr.mjs.map +0 -1
- package/dist/genID-cWOPmspk.js +0 -190
- package/dist/genID-cWOPmspk.js.map +0 -1
- package/dist/types.d.mts.map +0 -1
- package/dist/types.d.ts.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-crypto-Di814Pbk.js","names":["genSymmetricKey","uint8ArrayToBase64","encryptSymmetrically","UTF8ToUint8","uint8ToUTF8","decryptSymmetrically","base64ToUint8Array","encapsulateHybrid","decapsulateHybrid","getKeyFromPassword","getKeyFromPasswordAndSalt","genSymmetricKey","uint8ArrayToBase64","encryptSymmetrically","UTF8ToUint8","uint8ToUTF8","decryptSymmetrically","base64ToUint8Array","genHybridKeys","deriveKeyFromMnemonic","CONTEXT_DATABASE","CONTEXT_DRAFT"],"sources":["../src/key-wrapper/aesWrapper.ts","../src/email-crypto/errors.ts","../src/email-crypto/core.ts","../src/email-crypto/hybridEncyptedEmail.ts","../src/email-crypto/pwdProtectedEmail.ts","../src/email-crypto/coreSubject.ts","../src/email-crypto/hybridEncryptedEmailAndSubject.ts","../src/email-crypto/pwdProtectedEmailAndSubject.ts","../src/email-crypto/emailKeys.ts"],"sourcesContent":["import { aeskw } from '@noble/ciphers/aes.js';\n\n/**\n * Unwraps the given wrapped key\n *\n * @param encryptedKey - The wrapped key\n * @param wrappingKey - The secret key used for unwrapping\n * @returns The resulting key\n */\nexport async function unwrapKey(encryptedKey: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).decrypt(encryptedKey);\n}\n\n/**\n * Wraps the given key\n *\n * @param key - The key to be wrapped\n * @param wrappingKey - The secret key used for wrapping\n * @returns The resulting ciphertext\n */\nexport async function wrapKey(key: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).encrypt(key);\n}\n","export class FailedToEncryptEmail extends Error {\n constructor(errorMsg?: string) {\n super('Failed to encrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToEncryptEmail.prototype);\n }\n}\n\nexport class EmailSymmetricEncryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to symmetrically encrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailSymmetricEncryptionError.prototype);\n }\n}\n\nexport class EmailSymmetricDecryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to symmetrically decrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailSymmetricDecryptionError.prototype);\n }\n}\n\nexport class EmailHybridEncryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to hybridly encrypt the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailHybridEncryptionError.prototype);\n }\n}\n\nexport class EmailHybridDecryptionError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to hybridly decrypt the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailHybridDecryptionError.prototype);\n }\n}\n\nexport class EmailPasswordProtectError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to password-protect the key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailPasswordProtectError.prototype);\n }\n}\n\nexport class EmailPasswordOpenError extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open password-protected key: ' + errorMsg);\n\n Object.setPrototypeOf(this, EmailPasswordOpenError.prototype);\n }\n}\n\nexport class InvalidInputEmail extends Error {\n constructor() {\n super('Invalid input');\n\n Object.setPrototypeOf(this, InvalidInputEmail.prototype);\n }\n}\n\nexport class FailedToDecryptEmail extends Error {\n constructor(errorMsg?: string) {\n super('Failed to decrypt email: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToDecryptEmail.prototype);\n }\n}\n","import { HybridEncKey, PwdProtectedKey, Email, RecipientWithPublicKey, EmailEncrypted } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encapsulateHybrid, decapsulateHybrid } from '../hybrid-crypto';\nimport { wrapKey, unwrapKey } from '../key-wrapper';\nimport { getKeyFromPassword, getKeyFromPasswordAndSalt } from '../derive-password';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\nimport {\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n EmailPasswordOpenError,\n EmailPasswordProtectError,\n} from './errors';\n\n/**\n * Symmetrically encrypts email.\n *\n * @param email - The email to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmail(\n email: Email,\n aux?: Uint8Array,\n): Promise<{\n encEmail: EmailEncrypted;\n encryptionKey: Uint8Array;\n}> {\n if (!email.text) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = genSymmetricKey();\n const encEmail = await encryptEmailWithKey(email, encryptionKey, aux);\n\n return { encEmail, encryptionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Symmetrically encrypts email with the given key.\n *\n * @param email - The email to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailWithKey(\n email: Email,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailEncrypted> {\n try {\n const text = UTF8ToUint8(email.text);\n\n const encryptedText = await encryptSymmetrically(encryptionKey, text, aux);\n const encText = uint8ArrayToBase64(encryptedText);\n const enc: EmailEncrypted = { encText };\n if (email.attachments) {\n const promises = email.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 return enc;\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email.\n *\n * @param encEmail - The email to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email\n */\nexport async function decryptEmail(\n encEmail: EmailEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<Email> {\n try {\n const encText = base64ToUint8Array(encEmail.encText);\n const textArray = await decryptSymmetrically(encryptionKey, encText, aux);\n const text = uint8ToUTF8(textArray);\n const email: Email = { text };\n\n if (encEmail.encAttachments) {\n const encAttachments = encEmail.encAttachments?.map(base64ToUint8Array);\n const promises = encAttachments?.map((encAtt) => decryptSymmetrically(encryptionKey, encAtt, aux));\n const decryptedAttachments = await Promise.all(promises);\n email.attachments = decryptedAttachments?.map((att) => uint8ToUTF8(att));\n }\n\n return email;\n } catch (error) {\n throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Encrypts the email symmetric key using hybrid encryption.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param recipient - The recipient with a public hybrid key.\n * @returns The encrypted email symmetric key\n */\nexport async function encryptKeysHybrid(\n emailEncryptionKey: Uint8Array,\n recipient: RecipientWithPublicKey,\n): Promise<HybridEncKey> {\n try {\n const { cipherText, sharedSecret } = encapsulateHybrid(recipient.publicHybridKey);\n const encryptedKey = await wrapKey(emailEncryptionKey, sharedSecret);\n const encryptedKeyBase64 = uint8ArrayToBase64(encryptedKey);\n const kyberCiphertextBase64 = uint8ArrayToBase64(cipherText);\n\n return {\n encryptedKey: encryptedKeyBase64,\n hybridCiphertext: kyberCiphertextBase64,\n encryptedForEmail: recipient.email,\n };\n } catch (error) {\n throw new EmailHybridEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email symmetric key encrypted via hybrid encryption.\n *\n * @param encryptedKey - The encrypted email key.\n * @param recipientPrivateKey - The private key of the recipient.\n * @returns The email encryption key\n */\nexport async function decryptKeysHybrid(\n encryptedKey: HybridEncKey,\n recipientPrivateKey: Uint8Array,\n): Promise<Uint8Array> {\n try {\n const kyberCiphertext = base64ToUint8Array(encryptedKey.hybridCiphertext);\n const encKey = base64ToUint8Array(encryptedKey.encryptedKey);\n const sharedSecret = decapsulateHybrid(kyberCiphertext, recipientPrivateKey);\n const encryptionKey = await unwrapKey(encKey, sharedSecret);\n return encryptionKey;\n } catch (error) {\n throw new EmailHybridDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Password-protects the email symmetric key.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param password - The secret password for key protection.\n * @returns The password-protected email symmetric key\n */\nexport async function passwordProtectKey(emailEncryptionKey: Uint8Array, password: string): Promise<PwdProtectedKey> {\n try {\n const { key, salt } = await getKeyFromPassword(password);\n const encryptedKey = await wrapKey(emailEncryptionKey, key);\n const saltStr = uint8ArrayToBase64(salt);\n const encryptedKeyStr = uint8ArrayToBase64(encryptedKey);\n return { encryptedKey: encryptedKeyStr, salt: saltStr };\n } catch (error) {\n throw new EmailPasswordProtectError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Removes passoword-protection and exposes the email symmetric key.\n *\n * @param emailEncryptionKey - The password-protected email key.\n * @param password - The secret password for key protection.\n * @returns The email encryption key\n */\nexport async function removePasswordProtection(\n emailEncryptionKey: PwdProtectedKey,\n password: string,\n): Promise<Uint8Array> {\n try {\n const salt = base64ToUint8Array(emailEncryptionKey.salt);\n const encryptedKey = base64ToUint8Array(emailEncryptionKey.encryptedKey);\n const key = await getKeyFromPasswordAndSalt(password, salt);\n const encryptionKey = await unwrapKey(encryptedKey, key);\n return encryptionKey;\n } catch (error) {\n throw new EmailPasswordOpenError(error instanceof Error ? error.message : String(error));\n }\n}\n","import { HybridEncryptedEmail, Email, RecipientWithPublicKey } from '../types';\nimport { decryptEmail, encryptKeysHybrid, decryptKeysHybrid, encryptEmail } from './core';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n/**\n * Encrypts the email using hybrid encryption.\n *\n * @param email - The email 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\n */\nexport async function encryptEmailHybrid(\n email: Email,\n recipient: RecipientWithPublicKey,\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmail> {\n try {\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Encrypts the email using hybrid encryption for multiple recipients.\n *\n * @param email - The email to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted emails\n */\nexport async function encryptEmailHybridForMultipleRecipients(\n email: Email,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmail[]> {\n try {\n if (!recipients || recipients.length === 0) {\n throw new InvalidInputEmail();\n }\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n\n const encryptedEmails: HybridEncryptedEmail[] = [];\n for (const recipient of recipients) {\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n encryptedEmails.push({\n encEmail,\n encryptedKey,\n });\n }\n return encryptedEmails;\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email using hybrid encryption.\n *\n * @param encEmail - The encrypted email.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email\n */\nexport async function decryptEmailHybrid(\n encEmail: HybridEncryptedEmail,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<Email> {\n try {\n const encryptionKey = await decryptKeysHybrid(encEmail.encryptedKey, recipientPrivateHybridKeys);\n return await decryptEmail(encEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailHybridDecryptionError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { PwdProtectedEmail, Email } from '../types';\nimport { decryptEmail, passwordProtectKey, removePasswordProtection, encryptEmail } from './core';\nimport {\n EmailSymmetricEncryptionError,\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailPasswordProtectError,\n EmailSymmetricDecryptionError,\n EmailPasswordOpenError,\n InvalidInputEmail,\n} from './errors';\n/**\n * Creates a password-protected email.\n *\n * @param email - The email to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmail(\n email: Email,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmail> {\n try {\n const { encryptionKey, encEmail } = await encryptEmail(email, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailPasswordProtectError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens a password-protected email.\n *\n * @param encryptedEmail - The encrypted email\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email\n */\nexport async function decryptPwdProtectedEmail(\n encryptedEmail: PwdProtectedEmail,\n password: string,\n aux?: Uint8Array,\n): Promise<Email> {\n if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n return await decryptEmail(encryptedEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailPasswordOpenError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { EmailAndSubject, EmailAndSubjectEncrypted } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encryptEmailWithKey, decryptEmail } from './core';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\nimport { InvalidInputEmail, EmailSymmetricDecryptionError, EmailSymmetricEncryptionError } from './errors';\n\n/**\n * Symmetrically encrypts email and subject.\n *\n * @param email - The email and subject to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailAndSubject(\n email: EmailAndSubject,\n aux?: Uint8Array,\n): Promise<{\n encEmail: EmailAndSubjectEncrypted;\n encryptionKey: Uint8Array;\n}> {\n if (!email.text || !email.subject) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = genSymmetricKey();\n const encEmail = await encryptEmailAndSubjectWithKey(email, encryptionKey, aux);\n\n return { encEmail, encryptionKey };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Symmetrically encrypts email and subject with the given key.\n *\n * @param email - The email and subject to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email and symmetric key used for encryption\n */\nexport async function encryptEmailAndSubjectWithKey(\n email: EmailAndSubject,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubjectEncrypted> {\n try {\n const enc = await encryptEmailWithKey(email, encryptionKey, aux);\n const subject = UTF8ToUint8(email.subject);\n const subjectEnc = await encryptSymmetrically(encryptionKey, subject, aux);\n const encSubject = uint8ArrayToBase64(subjectEnc);\n\n return { ...enc, encSubject };\n } catch (error) {\n throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email and email subject.\n *\n * @param encEmail - The encrypted email and subject to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email and subject\n */\nexport async function decryptEmailAndSubject(\n encEmail: EmailAndSubjectEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n try {\n const encSubject = base64ToUint8Array(encEmail.encSubject);\n const subjectArray = await decryptSymmetrically(encryptionKey, encSubject, aux);\n const subject = uint8ToUTF8(subjectArray);\n const email = await decryptEmail(encEmail, encryptionKey, aux);\n\n return { ...email, subject };\n } catch (error) {\n throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));\n }\n}\n","import { RecipientWithPublicKey, EmailAndSubject, HybridEncryptedEmailAndSubject } from '../types';\nimport { encryptKeysHybrid, decryptKeysHybrid } from './core';\nimport { encryptEmailAndSubject, decryptEmailAndSubject } from './coreSubject';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n EmailHybridDecryptionError,\n EmailHybridEncryptionError,\n InvalidInputEmail,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Encrypts the email and its subject using hybrid encryption.\n *\n * @param email - The email and subject 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 and subject\n */\nexport async function encryptEmailAndSubjectHybrid(\n email: EmailAndSubject,\n recipient: RecipientWithPublicKey,\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmailAndSubject> {\n try {\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Encrypts the email and its subject using hybrid encryption for multiple recipients.\n *\n * @param email - The email and subject to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted emails and subjects\n */\nexport async function encryptEmailAndSubjectHybridForMultipleRecipients(\n email: EmailAndSubject,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmailAndSubject[]> {\n try {\n if (!recipients || recipients.length === 0) {\n throw new InvalidInputEmail();\n }\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n\n const encryptedEmails: HybridEncryptedEmailAndSubject[] = [];\n for (const recipient of recipients) {\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n encryptedEmails.push({\n encEmail,\n encryptedKey,\n });\n }\n return encryptedEmails;\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailHybridEncryptionError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Decrypts the email and its subject using hybrid encryption.\n *\n * @param hybridEmail - The encrypted email and subject.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email and subject\n */\nexport async function decryptEmailAndSubjectHybrid(\n hybridEmail: HybridEncryptedEmailAndSubject,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n try {\n const encryptionKey = await decryptKeysHybrid(hybridEmail.encryptedKey, recipientPrivateHybridKeys);\n return await decryptEmailAndSubject(hybridEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailHybridDecryptionError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { EmailAndSubject, PwdProtectedEmailAndSubject } from '../types';\nimport { passwordProtectKey, removePasswordProtection } from './core';\nimport { encryptEmailAndSubject, decryptEmailAndSubject } from './coreSubject';\nimport {\n FailedToDecryptEmail,\n FailedToEncryptEmail,\n InvalidInputEmail,\n EmailPasswordOpenError,\n EmailPasswordProtectError,\n EmailSymmetricDecryptionError,\n EmailSymmetricEncryptionError,\n} from './errors';\n\n/**\n * Creates a password-protected email and subject.\n *\n * @param email - The email and subject to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmailAndSubject(\n email: EmailAndSubject,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmailAndSubject> {\n try {\n const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmail, encryptedKey };\n } catch (error) {\n if (error instanceof InvalidInputEmail) throw error;\n if (error instanceof EmailSymmetricEncryptionError) throw error;\n if (error instanceof EmailPasswordProtectError) throw error;\n throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens a password-protected email and subject.\n *\n * @param encryptedEmail - The encrypted email and subject\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email and subject\n */\nexport async function decryptPwdProtectedEmailAndSubject(\n encryptedEmail: PwdProtectedEmailAndSubject,\n password: string,\n aux?: Uint8Array,\n): Promise<EmailAndSubject> {\n if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) {\n throw new InvalidInputEmail();\n }\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n return await decryptEmailAndSubject(encryptedEmail.encEmail, encryptionKey, aux);\n } catch (error) {\n if (error instanceof EmailPasswordOpenError) throw error;\n if (error instanceof EmailSymmetricDecryptionError) throw error;\n throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));\n }\n}\n","import { genHybridKeys } from '../hybrid-crypto';\nimport { HybridKeyPair } from '../types';\nimport { deriveKeyFromMnemonic } from '../derive-key';\nimport { CONTEXT_DATABASE, CONTEXT_DRAFT } from '../constants';\n\n/**\n * Generates public and private keys for email encryption.\n *\n * @returns The user's private and public keys\n */\nexport async function generateEmailKeys(): Promise<HybridKeyPair> {\n return genHybridKeys();\n}\n\n/**\n * Derives database encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting database\n */\nexport const deriveDatabaseKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DATABASE);\n};\n\n/**\n * Derives email draft encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting email drafts\n */\nexport const deriveEmailDraftKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);\n};\n"],"mappings":";;;;;;;;;;;;;;;;AASA,eAAsB,UAAU,cAA0B,aAA8C;AACtG,SAAA,GAAA,sBAAA,OAAa,YAAY,CAAC,QAAQ,aAAa;;;;;;;;;AAUjD,eAAsB,QAAQ,KAAiB,aAA8C;AAC3F,SAAA,GAAA,sBAAA,OAAa,YAAY,CAAC,QAAQ,IAAI;;;;ACrBxC,IAAa,uBAAb,MAAa,6BAA6B,MAAM;CAC9C,YAAY,UAAmB;AAC7B,QAAM,8BAA8B,SAAS;AAE7C,SAAO,eAAe,MAAM,qBAAqB,UAAU;;;AAI/D,IAAa,gCAAb,MAAa,sCAAsC,MAAM;CACvD,YAAY,UAAmB;AAC7B,QAAM,4CAA4C,SAAS;AAE3D,SAAO,eAAe,MAAM,8BAA8B,UAAU;;;AAIxE,IAAa,gCAAb,MAAa,sCAAsC,MAAM;CACvD,YAAY,UAAmB;AAC7B,QAAM,4CAA4C,SAAS;AAE3D,SAAO,eAAe,MAAM,8BAA8B,UAAU;;;AAIxE,IAAa,6BAAb,MAAa,m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
|
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/errors.ts
|
|
31
|
+
var FailedToEncryptEmail = class FailedToEncryptEmail extends Error {
|
|
32
|
+
constructor(errorMsg) {
|
|
33
|
+
super("Failed to encrypt email: " + errorMsg);
|
|
34
|
+
Object.setPrototypeOf(this, FailedToEncryptEmail.prototype);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var EmailSymmetricEncryptionError = class EmailSymmetricEncryptionError extends Error {
|
|
38
|
+
constructor(errorMsg) {
|
|
39
|
+
super("Failed to symmetrically encrypt email: " + errorMsg);
|
|
40
|
+
Object.setPrototypeOf(this, EmailSymmetricEncryptionError.prototype);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var EmailSymmetricDecryptionError = class EmailSymmetricDecryptionError extends Error {
|
|
44
|
+
constructor(errorMsg) {
|
|
45
|
+
super("Failed to symmetrically decrypt email: " + errorMsg);
|
|
46
|
+
Object.setPrototypeOf(this, EmailSymmetricDecryptionError.prototype);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var EmailHybridEncryptionError = class EmailHybridEncryptionError extends Error {
|
|
50
|
+
constructor(errorMsg) {
|
|
51
|
+
super("Failed to hybridly encrypt the key: " + errorMsg);
|
|
52
|
+
Object.setPrototypeOf(this, EmailHybridEncryptionError.prototype);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var EmailHybridDecryptionError = class EmailHybridDecryptionError extends Error {
|
|
56
|
+
constructor(errorMsg) {
|
|
57
|
+
super("Failed to hybridly decrypt the key: " + errorMsg);
|
|
58
|
+
Object.setPrototypeOf(this, EmailHybridDecryptionError.prototype);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var EmailPasswordProtectError = class EmailPasswordProtectError extends Error {
|
|
62
|
+
constructor(errorMsg) {
|
|
63
|
+
super("Failed to password-protect the key: " + errorMsg);
|
|
64
|
+
Object.setPrototypeOf(this, EmailPasswordProtectError.prototype);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var EmailPasswordOpenError = class EmailPasswordOpenError extends Error {
|
|
68
|
+
constructor(errorMsg) {
|
|
69
|
+
super("Failed to open password-protected key: " + errorMsg);
|
|
70
|
+
Object.setPrototypeOf(this, EmailPasswordOpenError.prototype);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var InvalidInputEmail = class InvalidInputEmail extends Error {
|
|
74
|
+
constructor() {
|
|
75
|
+
super("Invalid input");
|
|
76
|
+
Object.setPrototypeOf(this, InvalidInputEmail.prototype);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var FailedToDecryptEmail = class FailedToDecryptEmail extends Error {
|
|
80
|
+
constructor(errorMsg) {
|
|
81
|
+
super("Failed to decrypt email: " + errorMsg);
|
|
82
|
+
Object.setPrototypeOf(this, FailedToDecryptEmail.prototype);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/email-crypto/core.ts
|
|
87
|
+
/**
|
|
88
|
+
* Symmetrically encrypts email.
|
|
89
|
+
*
|
|
90
|
+
* @param email - The email to encrypt.
|
|
91
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
92
|
+
* @returns The resulting encrypted email and symmetric key used for encryption
|
|
93
|
+
*/
|
|
94
|
+
async function encryptEmail(email, aux) {
|
|
95
|
+
if (!email.text) throw new InvalidInputEmail();
|
|
96
|
+
try {
|
|
97
|
+
const encryptionKey = genSymmetricKey();
|
|
98
|
+
return {
|
|
99
|
+
encEmail: await encryptEmailWithKey(email, encryptionKey, aux),
|
|
100
|
+
encryptionKey
|
|
101
|
+
};
|
|
102
|
+
} catch (error) {
|
|
103
|
+
throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Symmetrically encrypts email with the given key.
|
|
108
|
+
*
|
|
109
|
+
* @param email - The email to encrypt.
|
|
110
|
+
* @param encryptionKey - The symmetric key to encrypt the email.
|
|
111
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
112
|
+
* @returns The resulting encrypted email and symmetric key used for encryption
|
|
113
|
+
*/
|
|
114
|
+
async function encryptEmailWithKey(email, encryptionKey, aux) {
|
|
115
|
+
try {
|
|
116
|
+
const enc = { encText: uint8ArrayToBase64(await encryptSymmetrically(encryptionKey, UTF8ToUint8(email.text), aux)) };
|
|
117
|
+
if (email.attachments) {
|
|
118
|
+
const promises = email.attachments.map((attachment) => {
|
|
119
|
+
return encryptSymmetrically(encryptionKey, UTF8ToUint8(attachment), aux);
|
|
120
|
+
});
|
|
121
|
+
enc.encAttachments = (await Promise.all(promises))?.map(uint8ArrayToBase64);
|
|
122
|
+
}
|
|
123
|
+
return enc;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Decrypts symmetrically encrypted email.
|
|
130
|
+
*
|
|
131
|
+
* @param encEmail - The email to decrypt.
|
|
132
|
+
* @param encryptionKey - The symmetric key to decrypt the email.
|
|
133
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
134
|
+
* @returns The resulting decrypted email
|
|
135
|
+
*/
|
|
136
|
+
async function decryptEmail(encEmail, encryptionKey, aux) {
|
|
137
|
+
try {
|
|
138
|
+
const email = { text: uint8ToUTF8(await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmail.encText), aux)) };
|
|
139
|
+
if (encEmail.encAttachments) {
|
|
140
|
+
const promises = (encEmail.encAttachments?.map(base64ToUint8Array))?.map((encAtt) => decryptSymmetrically(encryptionKey, encAtt, aux));
|
|
141
|
+
email.attachments = (await Promise.all(promises))?.map((att) => uint8ToUTF8(att));
|
|
142
|
+
}
|
|
143
|
+
return email;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Encrypts the email symmetric key using hybrid encryption.
|
|
150
|
+
*
|
|
151
|
+
* @param emailEncryptionKey - The symmetric key used for email encryption.
|
|
152
|
+
* @param recipient - The recipient with a public hybrid key.
|
|
153
|
+
* @returns The encrypted email symmetric key
|
|
154
|
+
*/
|
|
155
|
+
async function encryptKeysHybrid(emailEncryptionKey, recipient) {
|
|
156
|
+
try {
|
|
157
|
+
const { cipherText, sharedSecret } = encapsulateHybrid(recipient.publicHybridKey);
|
|
158
|
+
return {
|
|
159
|
+
encryptedKey: uint8ArrayToBase64(await wrapKey(emailEncryptionKey, sharedSecret)),
|
|
160
|
+
hybridCiphertext: uint8ArrayToBase64(cipherText),
|
|
161
|
+
encryptedForEmail: recipient.email
|
|
162
|
+
};
|
|
163
|
+
} catch (error) {
|
|
164
|
+
throw new EmailHybridEncryptionError(error instanceof Error ? error.message : String(error));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Decrypts the email symmetric key encrypted via hybrid encryption.
|
|
169
|
+
*
|
|
170
|
+
* @param encryptedKey - The encrypted email key.
|
|
171
|
+
* @param recipientPrivateKey - The private key of the recipient.
|
|
172
|
+
* @returns The email encryption key
|
|
173
|
+
*/
|
|
174
|
+
async function decryptKeysHybrid(encryptedKey, recipientPrivateKey) {
|
|
175
|
+
try {
|
|
176
|
+
const kyberCiphertext = base64ToUint8Array(encryptedKey.hybridCiphertext);
|
|
177
|
+
return await unwrapKey(base64ToUint8Array(encryptedKey.encryptedKey), decapsulateHybrid(kyberCiphertext, recipientPrivateKey));
|
|
178
|
+
} catch (error) {
|
|
179
|
+
throw new EmailHybridDecryptionError(error instanceof Error ? error.message : String(error));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Password-protects the email symmetric key.
|
|
184
|
+
*
|
|
185
|
+
* @param emailEncryptionKey - The symmetric key used for email encryption.
|
|
186
|
+
* @param password - The secret password for key protection.
|
|
187
|
+
* @returns The password-protected email symmetric key
|
|
188
|
+
*/
|
|
189
|
+
async function passwordProtectKey(emailEncryptionKey, password) {
|
|
190
|
+
try {
|
|
191
|
+
const { key, salt } = await getKeyFromPassword(password);
|
|
192
|
+
const encryptedKey = await wrapKey(emailEncryptionKey, key);
|
|
193
|
+
const saltStr = uint8ArrayToBase64(salt);
|
|
194
|
+
return {
|
|
195
|
+
encryptedKey: uint8ArrayToBase64(encryptedKey),
|
|
196
|
+
salt: saltStr
|
|
197
|
+
};
|
|
198
|
+
} catch (error) {
|
|
199
|
+
throw new EmailPasswordProtectError(error instanceof Error ? error.message : String(error));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Removes passoword-protection and exposes the email symmetric key.
|
|
204
|
+
*
|
|
205
|
+
* @param emailEncryptionKey - The password-protected email key.
|
|
206
|
+
* @param password - The secret password for key protection.
|
|
207
|
+
* @returns The email encryption key
|
|
208
|
+
*/
|
|
209
|
+
async function removePasswordProtection(emailEncryptionKey, password) {
|
|
210
|
+
try {
|
|
211
|
+
const salt = base64ToUint8Array(emailEncryptionKey.salt);
|
|
212
|
+
return await unwrapKey(base64ToUint8Array(emailEncryptionKey.encryptedKey), await getKeyFromPasswordAndSalt(password, salt));
|
|
213
|
+
} catch (error) {
|
|
214
|
+
throw new EmailPasswordOpenError(error instanceof Error ? error.message : String(error));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/email-crypto/hybridEncyptedEmail.ts
|
|
219
|
+
/**
|
|
220
|
+
* Encrypts the email using hybrid encryption.
|
|
221
|
+
*
|
|
222
|
+
* @param email - The email to encrypt.
|
|
223
|
+
* @param recipientPublicKeys - The public keys of the recipient.
|
|
224
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
225
|
+
* @returns The encrypted email
|
|
226
|
+
*/
|
|
227
|
+
async function encryptEmailHybrid(email, recipient, aux) {
|
|
228
|
+
try {
|
|
229
|
+
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
230
|
+
return {
|
|
231
|
+
encEmail,
|
|
232
|
+
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
233
|
+
};
|
|
234
|
+
} catch (error) {
|
|
235
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
236
|
+
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
237
|
+
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
238
|
+
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Encrypts the email using hybrid encryption for multiple recipients.
|
|
243
|
+
*
|
|
244
|
+
* @param email - The email to encrypt for multiple recipients.
|
|
245
|
+
* @param recipients - The recipients with corresponding public keys.
|
|
246
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
247
|
+
* @returns The set of encrypted emails
|
|
248
|
+
*/
|
|
249
|
+
async function encryptEmailHybridForMultipleRecipients(email, recipients, aux) {
|
|
250
|
+
try {
|
|
251
|
+
if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
|
|
252
|
+
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
253
|
+
const encryptedEmails = [];
|
|
254
|
+
for (const recipient of recipients) {
|
|
255
|
+
const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
|
|
256
|
+
encryptedEmails.push({
|
|
257
|
+
encEmail,
|
|
258
|
+
encryptedKey
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
return encryptedEmails;
|
|
262
|
+
} catch (error) {
|
|
263
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
264
|
+
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
265
|
+
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
266
|
+
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Decrypts the email using hybrid encryption.
|
|
271
|
+
*
|
|
272
|
+
* @param encEmail - The encrypted email.
|
|
273
|
+
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
274
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
275
|
+
* @returns The decrypted email
|
|
276
|
+
*/
|
|
277
|
+
async function decryptEmailHybrid(encEmail, recipientPrivateHybridKeys, aux) {
|
|
278
|
+
try {
|
|
279
|
+
const encryptionKey = await decryptKeysHybrid(encEmail.encryptedKey, recipientPrivateHybridKeys);
|
|
280
|
+
return await decryptEmail(encEmail.encEmail, encryptionKey, aux);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
if (error instanceof EmailHybridDecryptionError) throw error;
|
|
283
|
+
if (error instanceof EmailSymmetricDecryptionError) throw error;
|
|
284
|
+
throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/email-crypto/pwdProtectedEmail.ts
|
|
289
|
+
/**
|
|
290
|
+
* Creates a password-protected email.
|
|
291
|
+
*
|
|
292
|
+
* @param email - The email to password-protect
|
|
293
|
+
* @param password - The secret password shared among recipients
|
|
294
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
295
|
+
* @returns The password-protected email
|
|
296
|
+
*/
|
|
297
|
+
async function createPwdProtectedEmail(email, password, aux) {
|
|
298
|
+
try {
|
|
299
|
+
const { encryptionKey, encEmail } = await encryptEmail(email, aux);
|
|
300
|
+
return {
|
|
301
|
+
encEmail,
|
|
302
|
+
encryptedKey: await passwordProtectKey(encryptionKey, password)
|
|
303
|
+
};
|
|
304
|
+
} catch (error) {
|
|
305
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
306
|
+
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
307
|
+
if (error instanceof EmailPasswordProtectError) throw error;
|
|
308
|
+
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Opens a password-protected email.
|
|
313
|
+
*
|
|
314
|
+
* @param encryptedEmail - The encrypted email
|
|
315
|
+
* @param password - The secret password shared among recipients.
|
|
316
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
317
|
+
* @returns The decrypted email
|
|
318
|
+
*/
|
|
319
|
+
async function decryptPwdProtectedEmail(encryptedEmail, password, aux) {
|
|
320
|
+
if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) throw new InvalidInputEmail();
|
|
321
|
+
try {
|
|
322
|
+
const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
|
|
323
|
+
return await decryptEmail(encryptedEmail.encEmail, encryptionKey, aux);
|
|
324
|
+
} catch (error) {
|
|
325
|
+
if (error instanceof EmailPasswordOpenError) throw error;
|
|
326
|
+
if (error instanceof EmailSymmetricDecryptionError) throw error;
|
|
327
|
+
throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
//#endregion
|
|
331
|
+
//#region src/email-crypto/coreSubject.ts
|
|
332
|
+
/**
|
|
333
|
+
* Symmetrically encrypts email and subject.
|
|
334
|
+
*
|
|
335
|
+
* @param email - The email and subject to encrypt.
|
|
336
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
337
|
+
* @returns The resulting encrypted email and symmetric key used for encryption
|
|
338
|
+
*/
|
|
339
|
+
async function encryptEmailAndSubject(email, aux) {
|
|
340
|
+
if (!email.text || !email.subject) throw new InvalidInputEmail();
|
|
341
|
+
try {
|
|
342
|
+
const encryptionKey = genSymmetricKey();
|
|
343
|
+
return {
|
|
344
|
+
encEmail: await encryptEmailAndSubjectWithKey(email, encryptionKey, aux),
|
|
345
|
+
encryptionKey
|
|
346
|
+
};
|
|
347
|
+
} catch (error) {
|
|
348
|
+
throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Symmetrically encrypts email and subject with the given key.
|
|
353
|
+
*
|
|
354
|
+
* @param email - The email and subject to encrypt.
|
|
355
|
+
* @param encryptionKey - The symmetric key to encrypt the email.
|
|
356
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
357
|
+
* @returns The resulting encrypted email and symmetric key used for encryption
|
|
358
|
+
*/
|
|
359
|
+
async function encryptEmailAndSubjectWithKey(email, encryptionKey, aux) {
|
|
360
|
+
try {
|
|
361
|
+
const enc = await encryptEmailWithKey(email, encryptionKey, aux);
|
|
362
|
+
const encSubject = uint8ArrayToBase64(await encryptSymmetrically(encryptionKey, UTF8ToUint8(email.subject), aux));
|
|
363
|
+
return {
|
|
364
|
+
...enc,
|
|
365
|
+
encSubject
|
|
366
|
+
};
|
|
367
|
+
} catch (error) {
|
|
368
|
+
throw new EmailSymmetricEncryptionError(error instanceof Error ? error.message : String(error));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Decrypts symmetrically encrypted email and email subject.
|
|
373
|
+
*
|
|
374
|
+
* @param encEmail - The encrypted email and subject to decrypt.
|
|
375
|
+
* @param encryptionKey - The symmetric key to decrypt the email.
|
|
376
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
377
|
+
* @returns The resulting decrypted email and subject
|
|
378
|
+
*/
|
|
379
|
+
async function decryptEmailAndSubject(encEmail, encryptionKey, aux) {
|
|
380
|
+
try {
|
|
381
|
+
const subject = uint8ToUTF8(await decryptSymmetrically(encryptionKey, base64ToUint8Array(encEmail.encSubject), aux));
|
|
382
|
+
return {
|
|
383
|
+
...await decryptEmail(encEmail, encryptionKey, aux),
|
|
384
|
+
subject
|
|
385
|
+
};
|
|
386
|
+
} catch (error) {
|
|
387
|
+
throw new EmailSymmetricDecryptionError(error instanceof Error ? error.message : String(error));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/email-crypto/hybridEncryptedEmailAndSubject.ts
|
|
392
|
+
/**
|
|
393
|
+
* Encrypts the email and its subject using hybrid encryption.
|
|
394
|
+
*
|
|
395
|
+
* @param email - The email and subject to encrypt.
|
|
396
|
+
* @param recipientPublicKeys - The public keys of the recipient.
|
|
397
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
398
|
+
* @returns The encrypted email and subject
|
|
399
|
+
*/
|
|
400
|
+
async function encryptEmailAndSubjectHybrid(email, recipient, aux) {
|
|
401
|
+
try {
|
|
402
|
+
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
403
|
+
return {
|
|
404
|
+
encEmail,
|
|
405
|
+
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
406
|
+
};
|
|
407
|
+
} catch (error) {
|
|
408
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
409
|
+
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
410
|
+
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
411
|
+
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Encrypts the email and its subject using hybrid encryption for multiple recipients.
|
|
416
|
+
*
|
|
417
|
+
* @param email - The email and subject to encrypt for multiple recipients.
|
|
418
|
+
* @param recipients - The recipients with corresponding public keys.
|
|
419
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
420
|
+
* @returns The set of encrypted emails and subjects
|
|
421
|
+
*/
|
|
422
|
+
async function encryptEmailAndSubjectHybridForMultipleRecipients(email, recipients, aux) {
|
|
423
|
+
try {
|
|
424
|
+
if (!recipients || recipients.length === 0) throw new InvalidInputEmail();
|
|
425
|
+
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
426
|
+
const encryptedEmails = [];
|
|
427
|
+
for (const recipient of recipients) {
|
|
428
|
+
const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
|
|
429
|
+
encryptedEmails.push({
|
|
430
|
+
encEmail,
|
|
431
|
+
encryptedKey
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return encryptedEmails;
|
|
435
|
+
} catch (error) {
|
|
436
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
437
|
+
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
438
|
+
if (error instanceof EmailHybridEncryptionError) throw error;
|
|
439
|
+
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Decrypts the email and its subject using hybrid encryption.
|
|
444
|
+
*
|
|
445
|
+
* @param hybridEmail - The encrypted email and subject.
|
|
446
|
+
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
447
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
448
|
+
* @returns The decrypted email and subject
|
|
449
|
+
*/
|
|
450
|
+
async function decryptEmailAndSubjectHybrid(hybridEmail, recipientPrivateHybridKeys, aux) {
|
|
451
|
+
try {
|
|
452
|
+
const encryptionKey = await decryptKeysHybrid(hybridEmail.encryptedKey, recipientPrivateHybridKeys);
|
|
453
|
+
return await decryptEmailAndSubject(hybridEmail.encEmail, encryptionKey, aux);
|
|
454
|
+
} catch (error) {
|
|
455
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
456
|
+
if (error instanceof EmailHybridDecryptionError) throw error;
|
|
457
|
+
if (error instanceof EmailSymmetricDecryptionError) throw error;
|
|
458
|
+
throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
//#endregion
|
|
462
|
+
//#region src/email-crypto/pwdProtectedEmailAndSubject.ts
|
|
463
|
+
/**
|
|
464
|
+
* Creates a password-protected email and subject.
|
|
465
|
+
*
|
|
466
|
+
* @param email - The email and subject to password-protect
|
|
467
|
+
* @param password - The secret password shared among recipients
|
|
468
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
469
|
+
* @returns The password-protected email
|
|
470
|
+
*/
|
|
471
|
+
async function createPwdProtectedEmailAndSubject(email, password, aux) {
|
|
472
|
+
try {
|
|
473
|
+
const { encryptionKey, encEmail } = await encryptEmailAndSubject(email, aux);
|
|
474
|
+
return {
|
|
475
|
+
encEmail,
|
|
476
|
+
encryptedKey: await passwordProtectKey(encryptionKey, password)
|
|
477
|
+
};
|
|
478
|
+
} catch (error) {
|
|
479
|
+
if (error instanceof InvalidInputEmail) throw error;
|
|
480
|
+
if (error instanceof EmailSymmetricEncryptionError) throw error;
|
|
481
|
+
if (error instanceof EmailPasswordProtectError) throw error;
|
|
482
|
+
throw new FailedToEncryptEmail(error instanceof Error ? error.message : String(error));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Opens a password-protected email and subject.
|
|
487
|
+
*
|
|
488
|
+
* @param encryptedEmail - The encrypted email and subject
|
|
489
|
+
* @param password - The secret password shared among recipients.
|
|
490
|
+
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
491
|
+
* @returns The decrypted email and subject
|
|
492
|
+
*/
|
|
493
|
+
async function decryptPwdProtectedEmailAndSubject(encryptedEmail, password, aux) {
|
|
494
|
+
if (!encryptedEmail?.encEmail || !encryptedEmail?.encryptedKey) throw new InvalidInputEmail();
|
|
495
|
+
try {
|
|
496
|
+
const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
|
|
497
|
+
return await decryptEmailAndSubject(encryptedEmail.encEmail, encryptionKey, aux);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
if (error instanceof EmailPasswordOpenError) throw error;
|
|
500
|
+
if (error instanceof EmailSymmetricDecryptionError) throw error;
|
|
501
|
+
throw new FailedToDecryptEmail(error instanceof Error ? error.message : String(error));
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
//#endregion
|
|
505
|
+
//#region src/email-crypto/emailKeys.ts
|
|
506
|
+
/**
|
|
507
|
+
* Generates public and private keys for email encryption.
|
|
508
|
+
*
|
|
509
|
+
* @returns The user's private and public keys
|
|
510
|
+
*/
|
|
511
|
+
async function generateEmailKeys() {
|
|
512
|
+
return genHybridKeys();
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Derives database encryption key for the given user
|
|
516
|
+
*
|
|
517
|
+
* @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
|
|
518
|
+
* @returns The symmetric key for protecting database
|
|
519
|
+
*/
|
|
520
|
+
const deriveDatabaseKey = async (mnemonic) => {
|
|
521
|
+
return deriveKeyFromMnemonic(mnemonic, CONTEXT_DATABASE);
|
|
522
|
+
};
|
|
523
|
+
/**
|
|
524
|
+
* Derives email draft encryption key for the given user
|
|
525
|
+
*
|
|
526
|
+
* @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
|
|
527
|
+
* @returns The symmetric key for protecting email drafts
|
|
528
|
+
*/
|
|
529
|
+
const deriveEmailDraftKey = async (mnemonic) => {
|
|
530
|
+
return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);
|
|
531
|
+
};
|
|
532
|
+
//#endregion
|
|
533
|
+
export { FailedToDecryptEmail as A, removePasswordProtection as C, EmailPasswordProtectError as D, EmailPasswordOpenError as E, InvalidInputEmail as M, unwrapKey as N, EmailSymmetricDecryptionError as O, wrapKey as P, passwordProtectKey as S, EmailHybridEncryptionError as T, decryptEmail as _, decryptPwdProtectedEmailAndSubject as a, encryptEmailWithKey as b, encryptEmailAndSubjectHybridForMultipleRecipients as c, encryptEmailAndSubjectWithKey as d, createPwdProtectedEmail as f, encryptEmailHybridForMultipleRecipients as g, encryptEmailHybrid as h, createPwdProtectedEmailAndSubject as i, FailedToEncryptEmail as j, EmailSymmetricEncryptionError as k, decryptEmailAndSubject as l, decryptEmailHybrid as m, deriveEmailDraftKey as n, decryptEmailAndSubjectHybrid as o, decryptPwdProtectedEmail as p, generateEmailKeys as r, encryptEmailAndSubjectHybrid as s, deriveDatabaseKey as t, encryptEmailAndSubject as u, decryptKeysHybrid as v, EmailHybridDecryptionError as w, encryptKeysHybrid as x, encryptEmail as y };
|
|
534
|
+
|
|
535
|
+
//# sourceMappingURL=email-crypto-DpjfXDes.mjs.map
|