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
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"emailEncryptionKey-Ba95VuFO.js","names":["uint8ArrayToBase64","UTF8ToUint8","encryptSymmetrically","base64ToUint8Array","decryptSymmetrically","deriveKeyFromMnemonic","CONTEXT_RECOVERY","CONTEXT_ENC_KEYSTORE","genHybridKeys","KeystoreType","genMnemonic"],"sources":["../src/keystore-crypto/core.ts","../src/keystore-crypto/errors.ts","../src/keystore-crypto/emailEncryptionKey.ts"],"sourcesContent":["import { encryptSymmetrically, decryptSymmetrically } from '../symmetric-crypto';\nimport { base64ToUint8Array, uint8ArrayToBase64, UTF8ToUint8 } from '../utils';\nimport { deriveKeyFromMnemonic } from '../derive-key';\nimport { CONTEXT_ENC_KEYSTORE, CONTEXT_RECOVERY } from '../constants';\nimport { EncryptedKeystore, HybridKeyPair, KeystoreType } from '../types';\n\n/**\n * Encrypts the user's hybrid key using symmetric encryption to get a keystore\n *\n * @param secretKey - The symmetric key to encrypt the keystore content\n * @param key - The hybrid key pair\n * @param userEmail - The email of the user\n * @param type - The keystore type\n * @returns The encrypted keystore\n */\nexport async function encryptKeystoreContent(\n secretKey: Uint8Array,\n keys: HybridKeyPair,\n userEmail: string,\n type: KeystoreType,\n): Promise<EncryptedKeystore> {\n try {\n const publicKey = uint8ArrayToBase64(keys.publicKey);\n\n const aux = UTF8ToUint8(userEmail + type + publicKey);\n const secretKeyEncrypted = await encryptSymmetrically(secretKey, keys.secretKey, aux);\n\n const keystore: EncryptedKeystore = {\n userEmail,\n type,\n publicKey,\n privateKeyEncrypted: uint8ArrayToBase64(secretKeyEncrypted),\n };\n return keystore;\n } catch (error) {\n throw new Error('Failed to encrypt keystore content', { cause: error });\n }\n}\n\n/**\n * Decrypts the keystore content using symmetric encryption\n *\n * @param kesytoreOpeningKey - The symmetric key to decrypt the keystore content\n * @param encryptedKeys - The encrypted keystore content\n * @returns The decrypted hybrid key pair contained in the keystore\n */\nexport async function decryptKeystoreContent(\n kesytoreOpeningKey: Uint8Array,\n encryptedKeystore: EncryptedKeystore,\n): Promise<HybridKeyPair> {\n try {\n const aux = UTF8ToUint8(encryptedKeystore.userEmail + encryptedKeystore.type + encryptedKeystore.publicKey);\n const publicKey = base64ToUint8Array(encryptedKeystore.publicKey);\n const ciphertext = base64ToUint8Array(encryptedKeystore.privateKeyEncrypted);\n const secretKey = await decryptSymmetrically(kesytoreOpeningKey, ciphertext, aux);\n return {\n publicKey,\n secretKey,\n };\n } catch (error) {\n throw new Error('Failed to decrypt keystore content', { cause: error });\n }\n}\n\n/**\n * Derives a secret key for protecting the recovery keystore\n *\n * @param recoveryCodes - The recovery codes (machine-generated with secure PRNG)\n * @returns The derived secret key for protecting the recovery keystore\n */\nexport async function deriveRecoveryKey(recoveryCodes: string): Promise<Uint8Array> {\n return deriveKeyFromMnemonic(recoveryCodes, CONTEXT_RECOVERY);\n}\n\n/**\n * Derives a secret key for protecting the encryption keystore from user's mnemonic\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The derived secret key for protecting the encryption keystore\n */\nexport async function deriveEncryptionKeystoreKeyFromMnemonic(mnemonic: string): Promise<Uint8Array> {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_ENC_KEYSTORE);\n}\n","export class FailedToCreateKeyStores extends Error {\n constructor(errorMsg?: string) {\n super('Failed to create encryption and recovery keystores: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToCreateKeyStores.prototype);\n }\n}\n\nexport class InvalidInputKeyStore extends Error {\n constructor() {\n super('Invalid input');\n\n Object.setPrototypeOf(this, InvalidInputKeyStore.prototype);\n }\n}\n\nexport class FailedToOpenEncryptionKeyStore extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open encryption keystore: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToOpenEncryptionKeyStore.prototype);\n }\n}\n\nexport class FailedToOpenRecoveryKeyStore extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open recovery keystore: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToOpenRecoveryKeyStore.prototype);\n }\n}\n\nexport class FailedToChangeMnemonicForKeyStore extends Error {\n constructor(errorMsg?: string) {\n super('Error while fetching message: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToChangeMnemonicForKeyStore.prototype);\n }\n}\n","import { EncryptedKeystore, KeystoreType, HybridKeyPair } from '../types';\nimport { genMnemonic } from '../utils';\nimport {\n encryptKeystoreContent,\n decryptKeystoreContent,\n deriveEncryptionKeystoreKeyFromMnemonic,\n deriveRecoveryKey,\n} from './core';\nimport { genHybridKeys } from '../hybrid-crypto';\nimport {\n FailedToOpenEncryptionKeyStore,\n FailedToCreateKeyStores,\n FailedToOpenRecoveryKeyStore,\n FailedToChangeMnemonicForKeyStore,\n InvalidInputKeyStore,\n} from './errors';\n\n/**\n * Generates hybrid keys and creates encrypted main and recovery keystores\n * The main keystore encryption key is derived from the user's mnemonic\n * The recovery keystore encryption key is derived from the recovery codes\n *\n * @param userEmail - The user's email\n * @param mnemonic - The user's mnemonic\n * @returns The encryption keys\n *\n * @returns The encryption and recovery keystores, recovery codes and hybrid keys\n */\nexport async function createEncryptionAndRecoveryKeystores(\n userEmail: string,\n mnemonic: string,\n): Promise<{\n encryptionKeystore: EncryptedKeystore;\n recoveryKeystore: EncryptedKeystore;\n recoveryCodes: string;\n keys: HybridKeyPair;\n}> {\n try {\n const keys = genHybridKeys();\n\n const secretKey = await deriveEncryptionKeystoreKeyFromMnemonic(mnemonic);\n const encryptionKeystore = await encryptKeystoreContent(secretKey, keys, userEmail, KeystoreType.ENCRYPTION);\n\n const recoveryCodes = genMnemonic();\n const recoveryKey = await deriveRecoveryKey(recoveryCodes);\n const recoveryKeystore = await encryptKeystoreContent(recoveryKey, keys, userEmail, KeystoreType.RECOVERY);\n\n return { encryptionKeystore, recoveryKeystore, recoveryCodes, keys };\n } catch (error) {\n throw new FailedToCreateKeyStores(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens the encryption keystore and returns the email encryption keys\n * The decryption key is derived from the user mnemonic\n *\n * @param encryptedKeystore - The encrypted keystore containing encryption keys\n * @param mnemonic - The user's mnemonic\n * @returns The encryption keys\n */\nexport async function openEncryptionKeystore(\n encryptedKeystore: EncryptedKeystore,\n mnemonic: string,\n): Promise<HybridKeyPair> {\n try {\n if (encryptedKeystore.type !== KeystoreType.ENCRYPTION) {\n throw new InvalidInputKeyStore();\n }\n const secretKey = await deriveEncryptionKeystoreKeyFromMnemonic(mnemonic);\n const keys = await decryptKeystoreContent(secretKey, encryptedKeystore);\n return keys;\n } catch (error) {\n if (error instanceof InvalidInputKeyStore) throw error;\n throw new FailedToOpenEncryptionKeyStore(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens the recovery keystore and returns the email encryption keys\n * The decryption key is derived from the recovery codes (machine-generated mnemonic)\n *\n * @param recoveryCodes - The user's recovery codes\n * @param encryptedKeystore - The encrypted keystore containing encryption keys\n * @returns The encryption keys\n */\nexport async function openRecoveryKeystore(\n recoveryCodes: string,\n encryptedKeystore: EncryptedKeystore,\n): Promise<HybridKeyPair> {\n try {\n if (encryptedKeystore.type !== KeystoreType.RECOVERY) {\n throw new InvalidInputKeyStore();\n }\n const recoveryKey = await deriveRecoveryKey(recoveryCodes);\n const keys = await decryptKeystoreContent(recoveryKey, encryptedKeystore);\n return keys;\n } catch (error) {\n if (error instanceof InvalidInputKeyStore) throw error;\n throw new FailedToOpenRecoveryKeyStore(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Re-encrypts the encryption keystore with a new mnemonic\n * The decryption key is derived from the user mnemonic\n *\n * @param encryptedKeystore - The encrypted keystore containing encryption keys\n * @param oldMnemonic - The user's old mnemonic\n * @param newMnemonic - The user's new mnemonic\n * @returns The keys and new re-encrypted keystore\n */\nexport async function changeMnemonicForEncryptionKeystore(\n encryptedKeystore: EncryptedKeystore,\n oldMnemonic: string,\n newMnemonic: string,\n): Promise<{ keys: HybridKeyPair; newKeystore: EncryptedKeystore }> {\n try {\n const keys = await openEncryptionKeystore(encryptedKeystore, oldMnemonic);\n\n const secretKey = await deriveEncryptionKeystoreKeyFromMnemonic(newMnemonic);\n const newKeystore = await encryptKeystoreContent(\n secretKey,\n keys,\n encryptedKeystore.userEmail,\n KeystoreType.ENCRYPTION,\n );\n\n return { newKeystore, keys };\n } catch (error) {\n if (error instanceof InvalidInputKeyStore) throw error;\n throw new FailedToChangeMnemonicForKeyStore(error instanceof Error ? error.message : String(error));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAeA,eAAsB,uBACpB,WACA,MACA,WACA,MAC4B;AAC5B,KAAI;EACF,MAAM,YAAYA,iCAAmB,KAAK,UAAU;E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ADR,MAAM,wCAAwC,YAAY,EAG1E,MACA,kBAAkB,WAClBA,2BAAa,WACd;GAEqB;GAAM;UACrB,OAAO;AACd,MAAI,iBAAiB,qBAAsB,OAAM;AACjD,QAAM,IAAI,kCAAkC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"emailEncryptionKey-CYvr26FG.mjs","names":[],"sources":["../src/keystore-crypto/core.ts","../src/keystore-crypto/errors.ts","../src/keystore-crypto/emailEncryptionKey.ts"],"sourcesContent":["import { encryptSymmetrically, decryptSymmetrically } from '../symmetric-crypto';\nimport { base64ToUint8Array, uint8ArrayToBase64, UTF8ToUint8 } from '../utils';\nimport { deriveKeyFromMnemonic } from '../derive-key';\nimport { CONTEXT_ENC_KEYSTORE, CONTEXT_RECOVERY } from '../constants';\nimport { EncryptedKeystore, HybridKeyPair, KeystoreType } from '../types';\n\n/**\n * Encrypts the user's hybrid key using symmetric encryption to get a keystore\n *\n * @param secretKey - The symmetric key to encrypt the keystore content\n * @param key - The hybrid key pair\n * @param userEmail - The email of the user\n * @param type - The keystore type\n * @returns The encrypted keystore\n */\nexport async function encryptKeystoreContent(\n secretKey: Uint8Array,\n keys: HybridKeyPair,\n userEmail: string,\n type: KeystoreType,\n): Promise<EncryptedKeystore> {\n try {\n const publicKey = uint8ArrayToBase64(keys.publicKey);\n\n const aux = UTF8ToUint8(userEmail + type + publicKey);\n const secretKeyEncrypted = await encryptSymmetrically(secretKey, keys.secretKey, aux);\n\n const keystore: EncryptedKeystore = {\n userEmail,\n type,\n publicKey,\n privateKeyEncrypted: uint8ArrayToBase64(secretKeyEncrypted),\n };\n return keystore;\n } catch (error) {\n throw new Error('Failed to encrypt keystore content', { cause: error });\n }\n}\n\n/**\n * Decrypts the keystore content using symmetric encryption\n *\n * @param kesytoreOpeningKey - The symmetric key to decrypt the keystore content\n * @param encryptedKeys - The encrypted keystore content\n * @returns The decrypted hybrid key pair contained in the keystore\n */\nexport async function decryptKeystoreContent(\n kesytoreOpeningKey: Uint8Array,\n encryptedKeystore: EncryptedKeystore,\n): Promise<HybridKeyPair> {\n try {\n const aux = UTF8ToUint8(encryptedKeystore.userEmail + encryptedKeystore.type + encryptedKeystore.publicKey);\n const publicKey = base64ToUint8Array(encryptedKeystore.publicKey);\n const ciphertext = base64ToUint8Array(encryptedKeystore.privateKeyEncrypted);\n const secretKey = await decryptSymmetrically(kesytoreOpeningKey, ciphertext, aux);\n return {\n publicKey,\n secretKey,\n };\n } catch (error) {\n throw new Error('Failed to decrypt keystore content', { cause: error });\n }\n}\n\n/**\n * Derives a secret key for protecting the recovery keystore\n *\n * @param recoveryCodes - The recovery codes (machine-generated with secure PRNG)\n * @returns The derived secret key for protecting the recovery keystore\n */\nexport async function deriveRecoveryKey(recoveryCodes: string): Promise<Uint8Array> {\n return deriveKeyFromMnemonic(recoveryCodes, CONTEXT_RECOVERY);\n}\n\n/**\n * Derives a secret key for protecting the encryption keystore from user's mnemonic\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The derived secret key for protecting the encryption keystore\n */\nexport async function deriveEncryptionKeystoreKeyFromMnemonic(mnemonic: string): Promise<Uint8Array> {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_ENC_KEYSTORE);\n}\n","export class FailedToCreateKeyStores extends Error {\n constructor(errorMsg?: string) {\n super('Failed to create encryption and recovery keystores: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToCreateKeyStores.prototype);\n }\n}\n\nexport class InvalidInputKeyStore extends Error {\n constructor() {\n super('Invalid input');\n\n Object.setPrototypeOf(this, InvalidInputKeyStore.prototype);\n }\n}\n\nexport class FailedToOpenEncryptionKeyStore extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open encryption keystore: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToOpenEncryptionKeyStore.prototype);\n }\n}\n\nexport class FailedToOpenRecoveryKeyStore extends Error {\n constructor(errorMsg?: string) {\n super('Failed to open recovery keystore: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToOpenRecoveryKeyStore.prototype);\n }\n}\n\nexport class FailedToChangeMnemonicForKeyStore extends Error {\n constructor(errorMsg?: string) {\n super('Error while fetching message: ' + errorMsg);\n\n Object.setPrototypeOf(this, FailedToChangeMnemonicForKeyStore.prototype);\n }\n}\n","import { EncryptedKeystore, KeystoreType, HybridKeyPair } from '../types';\nimport { genMnemonic } from '../utils';\nimport {\n encryptKeystoreContent,\n decryptKeystoreContent,\n deriveEncryptionKeystoreKeyFromMnemonic,\n deriveRecoveryKey,\n} from './core';\nimport { genHybridKeys } from '../hybrid-crypto';\nimport {\n FailedToOpenEncryptionKeyStore,\n FailedToCreateKeyStores,\n FailedToOpenRecoveryKeyStore,\n FailedToChangeMnemonicForKeyStore,\n InvalidInputKeyStore,\n} from './errors';\n\n/**\n * Generates hybrid keys and creates encrypted main and recovery keystores\n * The main keystore encryption key is derived from the user's mnemonic\n * The recovery keystore encryption key is derived from the recovery codes\n *\n * @param userEmail - The user's email\n * @param mnemonic - The user's mnemonic\n * @returns The encryption keys\n *\n * @returns The encryption and recovery keystores, recovery codes and hybrid keys\n */\nexport async function createEncryptionAndRecoveryKeystores(\n userEmail: string,\n mnemonic: string,\n): Promise<{\n encryptionKeystore: EncryptedKeystore;\n recoveryKeystore: EncryptedKeystore;\n recoveryCodes: string;\n keys: HybridKeyPair;\n}> {\n try {\n const keys = genHybridKeys();\n\n const secretKey = await deriveEncryptionKeystoreKeyFromMnemonic(mnemonic);\n const encryptionKeystore = await encryptKeystoreContent(secretKey, keys, userEmail, KeystoreType.ENCRYPTION);\n\n const recoveryCodes = genMnemonic();\n const recoveryKey = await deriveRecoveryKey(recoveryCodes);\n const recoveryKeystore = await encryptKeystoreContent(recoveryKey, keys, userEmail, KeystoreType.RECOVERY);\n\n return { encryptionKeystore, recoveryKeystore, recoveryCodes, keys };\n } catch (error) {\n throw new FailedToCreateKeyStores(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens the encryption keystore and returns the email encryption keys\n * The decryption key is derived from the user mnemonic\n *\n * @param encryptedKeystore - The encrypted keystore containing encryption keys\n * @param mnemonic - The user's mnemonic\n * @returns The encryption keys\n */\nexport async function openEncryptionKeystore(\n encryptedKeystore: EncryptedKeystore,\n mnemonic: string,\n): Promise<HybridKeyPair> {\n try {\n if (encryptedKeystore.type !== KeystoreType.ENCRYPTION) {\n throw new InvalidInputKeyStore();\n }\n const secretKey = await deriveEncryptionKeystoreKeyFromMnemonic(mnemonic);\n const keys = await decryptKeystoreContent(secretKey, encryptedKeystore);\n return keys;\n } catch (error) {\n if (error instanceof InvalidInputKeyStore) throw error;\n throw new FailedToOpenEncryptionKeyStore(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Opens the recovery keystore and returns the email encryption keys\n * The decryption key is derived from the recovery codes (machine-generated mnemonic)\n *\n * @param recoveryCodes - The user's recovery codes\n * @param encryptedKeystore - The encrypted keystore containing encryption keys\n * @returns The encryption keys\n */\nexport async function openRecoveryKeystore(\n recoveryCodes: string,\n encryptedKeystore: EncryptedKeystore,\n): Promise<HybridKeyPair> {\n try {\n if (encryptedKeystore.type !== KeystoreType.RECOVERY) {\n throw new InvalidInputKeyStore();\n }\n const recoveryKey = await deriveRecoveryKey(recoveryCodes);\n const keys = await decryptKeystoreContent(recoveryKey, encryptedKeystore);\n return keys;\n } catch (error) {\n if (error instanceof InvalidInputKeyStore) throw error;\n throw new FailedToOpenRecoveryKeyStore(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Re-encrypts the encryption keystore with a new mnemonic\n * The decryption key is derived from the user mnemonic\n *\n * @param encryptedKeystore - The encrypted keystore containing encryption keys\n * @param oldMnemonic - The user's old mnemonic\n * @param newMnemonic - The user's new mnemonic\n * @returns The keys and new re-encrypted keystore\n */\nexport async function changeMnemonicForEncryptionKeystore(\n encryptedKeystore: EncryptedKeystore,\n oldMnemonic: string,\n newMnemonic: string,\n): Promise<{ keys: HybridKeyPair; newKeystore: EncryptedKeystore }> {\n try {\n const keys = await openEncryptionKeystore(encryptedKeystore, oldMnemonic);\n\n const secretKey = await deriveEncryptionKeystoreKeyFromMnemonic(newMnemonic);\n const newKeystore = await encryptKeystoreContent(\n secretKey,\n keys,\n encryptedKeystore.userEmail,\n KeystoreType.ENCRYPTION,\n );\n\n return { newKeystore, keys };\n } catch (error) {\n if (error instanceof InvalidInputKeyStore) throw error;\n throw new FailedToChangeMnemonicForKeyStore(error instanceof Error ? error.message : String(error));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAeA,eAAsB,uBACpB,WACA,MACA,WACA,MAC4B;AAC5B,KAAI;EACF,MAAM,YAAY,mBAAmB,KAAK,UAAU;EAEpD,MAAM,MAAM,YAAY,YAAY,OAAO,UAAU;AASrD,SANoC;GAClC;GACA;GACA;GACA,qBAAqB,mBANI,MAAM,qBAAqB,WAAW,KAAK,WAAW,IAAI,CAMxB;GAC5D;UAEM,OAAO;AACd,QAAM,IAAI,MAAM,sCAAsC,EAAE,OAAO,OAAO,CAAC;;;;;;;;;;AAW3E,eAAsB,uBACpB,oBACA,mBACwB;AACxB,KAAI;EACF,MAAM,MAAM,YAAY,kBAAkB,YAAY,kBAAkB,OAAO,kBAAkB,UAAU;AAI3G,SAAO;GACL,WAJgB,mBAAmB,kBAAkB,UAAU;GAK/D,WAHgB,MAAM,qBAAqB,oBAD1B,mBAAmB,kBAAkB,oBAAoB,EACC,IAAI;GAIhF;UACM,OAAO;AACd,QAAM,IAAI,MAAM,sCAAsC,EAAE,OAAO,OAAO,CAAC;;;;;;;;;AAU3E,eAAsB,kBAAkB,eAA4C;AAClF,QAAO,sBAAsB,eAAe,iBAAiB;;;;;;;;AAS/D,eAAsB,wCAAwC,UAAuC;AACnG,QAAO,sBAAsB,UAAU,qBAAqB;;;;;ACjF9D,IAAa,0BAAb,MAAa,gCAAgC,MAAM;CACjD,YAAY,UAAmB;AAC7B,QAAM,yDAAy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AM,CAAC;;;;;;;;;;;AAYpG,eAAsB,qBACpB,eACA,mBACwB;AACxB,KAAI;AACF,MAAI,kBAAkB,SAAS,aAAa,SAC1C,OAAM,IAAI,sBAAsB;AAIlC,SADa,MAAM,uBADC,MAAM,kBAAkB,cAAc,EACH,kBAAkB;UAElE,OAAO;AACd,MAAI,iBAAiB,qBAAsB,OAAM;AACjD,QAAM,IAAI,6BAA6B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,CAAC;;;;;;;;;;;;AAalG,eAAsB,oCACpB,mBACA,aACA,aACkE;AAClE,KAAI;EACF,MAAM,OAAO,MAAM,uBAAuB,mBAAmB,YAAY;AAUzE,SAAO;GAAE,aAPW,MAAM,uBADR,MAAM,wCAAwC,YAAY,EAG1E,MACA,kBAAkB,WAClB,aAAa,WACd;GAEqB;GAAM;UACrB,OAAO;AACd,MAAI,iBAAiB,qBAAsB,OAAM;AACjD,QAAM,IAAI,kCAAkC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,CAAC"}
|
|
@@ -1,406 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-EbLFQX5m.js');
|
|
2
|
-
const require_constants = require('./constants.js');
|
|
3
|
-
const require_genID = require('./genID-cWOPmspk.js');
|
|
4
|
-
const require_deriveKeysFromMnemonic = require('./deriveKeysFromMnemonic-Dbx7VSBQ.js');
|
|
5
|
-
const require_deriveKeysFromPassword = require('./deriveKeysFromPassword-iMHv-tIA.js');
|
|
6
|
-
const require_aes = require('./aes-CuW_SPl-.js');
|
|
7
|
-
const require_xwing = require('./xwing-iRWVRtNE.js');
|
|
8
|
-
let _noble_ciphers_aes_js = require("@noble/ciphers/aes.js");
|
|
9
|
-
|
|
10
|
-
//#region src/key-wrapper/aesWrapper.ts
|
|
11
|
-
/**
|
|
12
|
-
* Unwraps the given wrapped key
|
|
13
|
-
*
|
|
14
|
-
* @param encryptedKey - The wrapped key
|
|
15
|
-
* @param wrappingKey - The secret key used for unwrapping
|
|
16
|
-
* @returns The resulting key
|
|
17
|
-
*/
|
|
18
|
-
async function unwrapKey(encryptedKey, wrappingKey) {
|
|
19
|
-
return (0, _noble_ciphers_aes_js.aeskw)(wrappingKey).decrypt(encryptedKey);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Wraps the given key
|
|
23
|
-
*
|
|
24
|
-
* @param key - The key to be wrapped
|
|
25
|
-
* @param wrappingKey - The secret key used for wrapping
|
|
26
|
-
* @returns The resulting ciphertext
|
|
27
|
-
*/
|
|
28
|
-
async function wrapKey(key, wrappingKey) {
|
|
29
|
-
return (0, _noble_ciphers_aes_js.aeskw)(wrappingKey).encrypt(key);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
//#endregion
|
|
33
|
-
//#region src/email-crypto/core.ts
|
|
34
|
-
/**
|
|
35
|
-
* Symmetrically encrypts email body.
|
|
36
|
-
*
|
|
37
|
-
* @param body - The email body to encrypt.
|
|
38
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
39
|
-
* @returns The resulting encrypted email body and symmetric key used for encryption
|
|
40
|
-
*/
|
|
41
|
-
async function encryptEmailBody(body, aux) {
|
|
42
|
-
try {
|
|
43
|
-
if (!body.text || !body.subject) throw new Error("Invalid input");
|
|
44
|
-
const encryptionKey = require_aes.genSymmetricKey();
|
|
45
|
-
return {
|
|
46
|
-
encEmailBody: await encryptEmailBodyWithKey(body, encryptionKey, aux),
|
|
47
|
-
encryptionKey
|
|
48
|
-
};
|
|
49
|
-
} catch (error) {
|
|
50
|
-
throw new Error("Failed to symmetrically encrypt email body", { cause: error });
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Symmetrically encrypts email body with the given key.
|
|
55
|
-
*
|
|
56
|
-
* @param body - The email body to encrypt.
|
|
57
|
-
* @param encryptionKey - The symmetric key to encrypt the email.
|
|
58
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
59
|
-
* @returns The resulting encrypted email body and symmetric key used for encryption
|
|
60
|
-
*/
|
|
61
|
-
async function encryptEmailBodyWithKey(body, encryptionKey, aux) {
|
|
62
|
-
try {
|
|
63
|
-
const text = require_genID.UTF8ToUint8(body.text);
|
|
64
|
-
const subjectEnc = await require_aes.encryptSymmetrically(encryptionKey, require_genID.UTF8ToUint8(body.subject), aux);
|
|
65
|
-
const enc = {
|
|
66
|
-
encText: require_genID.uint8ArrayToBase64(await require_aes.encryptSymmetrically(encryptionKey, text, aux)),
|
|
67
|
-
encSubject: require_genID.uint8ArrayToBase64(subjectEnc)
|
|
68
|
-
};
|
|
69
|
-
if (body.attachments) {
|
|
70
|
-
const promises = body.attachments.map((attachment) => {
|
|
71
|
-
return require_aes.encryptSymmetrically(encryptionKey, require_genID.UTF8ToUint8(attachment), aux);
|
|
72
|
-
});
|
|
73
|
-
enc.encAttachments = (await Promise.all(promises))?.map(require_genID.uint8ArrayToBase64);
|
|
74
|
-
}
|
|
75
|
-
return enc;
|
|
76
|
-
} catch (error) {
|
|
77
|
-
throw new Error("Failed to encrypt email body", { cause: error });
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Decrypts symmetrically encrypted email body.
|
|
82
|
-
*
|
|
83
|
-
* @param encEmailBody - The email body to decrypt.
|
|
84
|
-
* @param encryptionKey - The symmetric key to decrypt the email.
|
|
85
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
86
|
-
* @returns The resulting decrypted email body
|
|
87
|
-
*/
|
|
88
|
-
async function decryptEmailBody(encEmailBody, encryptionKey, aux) {
|
|
89
|
-
try {
|
|
90
|
-
const subject = require_genID.uint8ToUTF8(await require_aes.decryptSymmetrically(encryptionKey, require_genID.base64ToUint8Array(encEmailBody.encSubject), aux));
|
|
91
|
-
const body = {
|
|
92
|
-
text: require_genID.uint8ToUTF8(await require_aes.decryptSymmetrically(encryptionKey, require_genID.base64ToUint8Array(encEmailBody.encText), aux)),
|
|
93
|
-
subject
|
|
94
|
-
};
|
|
95
|
-
if (encEmailBody.encAttachments) {
|
|
96
|
-
const promises = (encEmailBody.encAttachments?.map(require_genID.base64ToUint8Array))?.map((encAtt) => require_aes.decryptSymmetrically(encryptionKey, encAtt, aux));
|
|
97
|
-
body.attachments = (await Promise.all(promises))?.map((att) => require_genID.uint8ToUTF8(att));
|
|
98
|
-
}
|
|
99
|
-
return body;
|
|
100
|
-
} catch (error) {
|
|
101
|
-
throw new Error("Failed to symmetrically decrypt email body", { cause: error });
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Encrypts the email symmetric key using hybrid encryption.
|
|
106
|
-
*
|
|
107
|
-
* @param emailEncryptionKey - The symmetric key used for email encryption.
|
|
108
|
-
* @param recipient - The recipient with a public hybrid key.
|
|
109
|
-
* @returns The encrypted email symmetric key
|
|
110
|
-
*/
|
|
111
|
-
async function encryptKeysHybrid(emailEncryptionKey, recipient) {
|
|
112
|
-
try {
|
|
113
|
-
const { cipherText, sharedSecret } = require_xwing.encapsulateHybrid(recipient.publicHybridKey);
|
|
114
|
-
return {
|
|
115
|
-
encryptedKey: require_genID.uint8ArrayToBase64(await wrapKey(emailEncryptionKey, sharedSecret)),
|
|
116
|
-
hybridCiphertext: require_genID.uint8ArrayToBase64(cipherText),
|
|
117
|
-
encryptedForEmail: recipient.email
|
|
118
|
-
};
|
|
119
|
-
} catch (error) {
|
|
120
|
-
throw new Error("Failed to encrypt email key using hybrid encryption", { cause: error });
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Decrypts the email symmetric key encrypted via hybrid encryption.
|
|
125
|
-
*
|
|
126
|
-
* @param encryptedKey - The encrypted email key.
|
|
127
|
-
* @param recipientPrivateKey - The private key of the recipient.
|
|
128
|
-
* @returns The email encryption key
|
|
129
|
-
*/
|
|
130
|
-
async function decryptKeysHybrid(encryptedKey, recipientPrivateKey) {
|
|
131
|
-
try {
|
|
132
|
-
const kyberCiphertext = require_genID.base64ToUint8Array(encryptedKey.hybridCiphertext);
|
|
133
|
-
return await unwrapKey(require_genID.base64ToUint8Array(encryptedKey.encryptedKey), require_xwing.decapsulateHybrid(kyberCiphertext, recipientPrivateKey));
|
|
134
|
-
} catch (error) {
|
|
135
|
-
throw new Error("Failed to decrypt email key encrypted via hybrid encryption", { cause: error });
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Password-protects the email symmetric key.
|
|
140
|
-
*
|
|
141
|
-
* @param emailEncryptionKey - The symmetric key used for email encryption.
|
|
142
|
-
* @param password - The secret password for key protection.
|
|
143
|
-
* @returns The password-protected email symmetric key
|
|
144
|
-
*/
|
|
145
|
-
async function passwordProtectKey(emailEncryptionKey, password) {
|
|
146
|
-
try {
|
|
147
|
-
const { key, salt } = await require_deriveKeysFromPassword.getKeyFromPassword(password);
|
|
148
|
-
const encryptedKey = await wrapKey(emailEncryptionKey, key);
|
|
149
|
-
const saltStr = require_genID.uint8ArrayToBase64(salt);
|
|
150
|
-
return {
|
|
151
|
-
encryptedKey: require_genID.uint8ArrayToBase64(encryptedKey),
|
|
152
|
-
salt: saltStr
|
|
153
|
-
};
|
|
154
|
-
} catch (error) {
|
|
155
|
-
throw new Error("Failed to password-protect email key", { cause: error });
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Removes passoword-protection and exposes the email symmetric key.
|
|
160
|
-
*
|
|
161
|
-
* @param emailEncryptionKey - The password-protected email key.
|
|
162
|
-
* @param password - The secret password for key protection.
|
|
163
|
-
* @returns The email encryption key
|
|
164
|
-
*/
|
|
165
|
-
async function removePasswordProtection(emailEncryptionKey, password) {
|
|
166
|
-
try {
|
|
167
|
-
const salt = require_genID.base64ToUint8Array(emailEncryptionKey.salt);
|
|
168
|
-
return await unwrapKey(require_genID.base64ToUint8Array(emailEncryptionKey.encryptedKey), await require_deriveKeysFromPassword.getKeyFromPasswordAndSalt(password, salt));
|
|
169
|
-
} catch (error) {
|
|
170
|
-
throw new Error("Failed to remove password-protection from email key", { cause: error });
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
//#endregion
|
|
175
|
-
//#region src/email-crypto/hybridEncyptedEmail.ts
|
|
176
|
-
/**
|
|
177
|
-
* Encrypts the email body using hybrid encryption.
|
|
178
|
-
*
|
|
179
|
-
* @param body - The email body to encrypt.
|
|
180
|
-
* @param recipientPublicKeys - The public keys of the recipient.
|
|
181
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
182
|
-
* @returns The encrypted email body
|
|
183
|
-
*/
|
|
184
|
-
async function encryptEmailHybrid(body, recipient, aux) {
|
|
185
|
-
try {
|
|
186
|
-
const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);
|
|
187
|
-
return {
|
|
188
|
-
encEmailBody,
|
|
189
|
-
encryptedKey: await encryptKeysHybrid(encryptionKey, recipient)
|
|
190
|
-
};
|
|
191
|
-
} catch (error) {
|
|
192
|
-
throw new Error("Failed to encrypt email body with hybrid encryption", { cause: error });
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Encrypts the email body using hybrid encryption for multiple recipients.
|
|
197
|
-
*
|
|
198
|
-
* @param body - The email body to encrypt for multiple recipients.
|
|
199
|
-
* @param recipients - The recipients with corresponding public keys.
|
|
200
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
201
|
-
* @returns The set of encrypted email bodies
|
|
202
|
-
*/
|
|
203
|
-
async function encryptEmailHybridForMultipleRecipients(body, recipients, aux) {
|
|
204
|
-
try {
|
|
205
|
-
const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);
|
|
206
|
-
const encryptedEmails = [];
|
|
207
|
-
for (const recipient of recipients) {
|
|
208
|
-
const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);
|
|
209
|
-
encryptedEmails.push({
|
|
210
|
-
encEmailBody,
|
|
211
|
-
encryptedKey
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
return encryptedEmails;
|
|
215
|
-
} catch (error) {
|
|
216
|
-
throw new Error("Failed to encrypt email to multiple recipients with hybrid encryption", { cause: error });
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Decrypts the email using hybrid encryption.
|
|
221
|
-
*
|
|
222
|
-
* @param encEmailBody - The encrypted email.
|
|
223
|
-
* @param recipientPrivateHybridKeys - The private key of the recipient.
|
|
224
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
225
|
-
* @returns The decrypted email body
|
|
226
|
-
*/
|
|
227
|
-
async function decryptEmailHybrid(encEmailBody, recipientPrivateHybridKeys, aux) {
|
|
228
|
-
try {
|
|
229
|
-
const encryptionKey = await decryptKeysHybrid(encEmailBody.encryptedKey, recipientPrivateHybridKeys);
|
|
230
|
-
return await decryptEmailBody(encEmailBody.encEmailBody, encryptionKey, aux);
|
|
231
|
-
} catch (error) {
|
|
232
|
-
throw new Error("Failed to decrypt email with hybrid encryption", { cause: error });
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
//#endregion
|
|
237
|
-
//#region src/email-crypto/pwdProtectedEmail.ts
|
|
238
|
-
/**
|
|
239
|
-
* Creates a password-protected email.
|
|
240
|
-
*
|
|
241
|
-
* @param email - The email to password-protect
|
|
242
|
-
* @param password - The secret password shared among recipients
|
|
243
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
244
|
-
* @returns The password-protected email
|
|
245
|
-
*/
|
|
246
|
-
async function createPwdProtectedEmail(emailBody, password, aux) {
|
|
247
|
-
try {
|
|
248
|
-
const { encryptionKey, encEmailBody } = await encryptEmailBody(emailBody, aux);
|
|
249
|
-
return {
|
|
250
|
-
encEmailBody,
|
|
251
|
-
encryptedKey: await passwordProtectKey(encryptionKey, password)
|
|
252
|
-
};
|
|
253
|
-
} catch (error) {
|
|
254
|
-
throw new Error("Failed to password-protect email", { cause: error });
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Opens a password-protected email.
|
|
259
|
-
*
|
|
260
|
-
* @param encryptedEmail - The encrypted email
|
|
261
|
-
* @param password - The secret password shared among recipients.
|
|
262
|
-
* @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).
|
|
263
|
-
* @returns The decrypted email body
|
|
264
|
-
*/
|
|
265
|
-
async function decryptPwdProtectedEmail(encryptedEmail, password, aux) {
|
|
266
|
-
try {
|
|
267
|
-
const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);
|
|
268
|
-
return await decryptEmailBody(encryptedEmail.encEmailBody, encryptionKey, aux);
|
|
269
|
-
} catch (error) {
|
|
270
|
-
throw new Error("Failed to decrypt password-protect email", { cause: error });
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
//#endregion
|
|
275
|
-
//#region src/email-crypto/emailKeys.ts
|
|
276
|
-
/**
|
|
277
|
-
* Generates public and private keys for email encryption.
|
|
278
|
-
*
|
|
279
|
-
* @returns The user's private and public keys
|
|
280
|
-
*/
|
|
281
|
-
async function generateEmailKeys() {
|
|
282
|
-
return require_xwing.genHybridKeys();
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Derives database encryption key for the given user
|
|
286
|
-
*
|
|
287
|
-
* @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
|
|
288
|
-
* @returns The symmetric key for protecting database
|
|
289
|
-
*/
|
|
290
|
-
const deriveDatabaseKey = async (mnemonic) => {
|
|
291
|
-
return require_deriveKeysFromMnemonic.deriveKeyFromMnemonic(mnemonic, require_constants.CONTEXT_DATABASE);
|
|
292
|
-
};
|
|
293
|
-
/**
|
|
294
|
-
* Derives email draft encryption key for the given user
|
|
295
|
-
*
|
|
296
|
-
* @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)
|
|
297
|
-
* @returns The symmetric key for protecting email drafts
|
|
298
|
-
*/
|
|
299
|
-
const deriveEmailDraftKey = async (mnemonic) => {
|
|
300
|
-
return require_deriveKeysFromMnemonic.deriveKeyFromMnemonic(mnemonic, require_constants.CONTEXT_DRAFT);
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
//#endregion
|
|
304
|
-
Object.defineProperty(exports, 'createPwdProtectedEmail', {
|
|
305
|
-
enumerable: true,
|
|
306
|
-
get: function () {
|
|
307
|
-
return createPwdProtectedEmail;
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
Object.defineProperty(exports, 'decryptEmailBody', {
|
|
311
|
-
enumerable: true,
|
|
312
|
-
get: function () {
|
|
313
|
-
return decryptEmailBody;
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
Object.defineProperty(exports, 'decryptEmailHybrid', {
|
|
317
|
-
enumerable: true,
|
|
318
|
-
get: function () {
|
|
319
|
-
return decryptEmailHybrid;
|
|
320
|
-
}
|
|
321
|
-
});
|
|
322
|
-
Object.defineProperty(exports, 'decryptKeysHybrid', {
|
|
323
|
-
enumerable: true,
|
|
324
|
-
get: function () {
|
|
325
|
-
return decryptKeysHybrid;
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
Object.defineProperty(exports, 'decryptPwdProtectedEmail', {
|
|
329
|
-
enumerable: true,
|
|
330
|
-
get: function () {
|
|
331
|
-
return decryptPwdProtectedEmail;
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
Object.defineProperty(exports, 'deriveDatabaseKey', {
|
|
335
|
-
enumerable: true,
|
|
336
|
-
get: function () {
|
|
337
|
-
return deriveDatabaseKey;
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
Object.defineProperty(exports, 'deriveEmailDraftKey', {
|
|
341
|
-
enumerable: true,
|
|
342
|
-
get: function () {
|
|
343
|
-
return deriveEmailDraftKey;
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
Object.defineProperty(exports, 'encryptEmailBody', {
|
|
347
|
-
enumerable: true,
|
|
348
|
-
get: function () {
|
|
349
|
-
return encryptEmailBody;
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
Object.defineProperty(exports, 'encryptEmailBodyWithKey', {
|
|
353
|
-
enumerable: true,
|
|
354
|
-
get: function () {
|
|
355
|
-
return encryptEmailBodyWithKey;
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
Object.defineProperty(exports, 'encryptEmailHybrid', {
|
|
359
|
-
enumerable: true,
|
|
360
|
-
get: function () {
|
|
361
|
-
return encryptEmailHybrid;
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
Object.defineProperty(exports, 'encryptEmailHybridForMultipleRecipients', {
|
|
365
|
-
enumerable: true,
|
|
366
|
-
get: function () {
|
|
367
|
-
return encryptEmailHybridForMultipleRecipients;
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
Object.defineProperty(exports, 'encryptKeysHybrid', {
|
|
371
|
-
enumerable: true,
|
|
372
|
-
get: function () {
|
|
373
|
-
return encryptKeysHybrid;
|
|
374
|
-
}
|
|
375
|
-
});
|
|
376
|
-
Object.defineProperty(exports, 'generateEmailKeys', {
|
|
377
|
-
enumerable: true,
|
|
378
|
-
get: function () {
|
|
379
|
-
return generateEmailKeys;
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
Object.defineProperty(exports, 'passwordProtectKey', {
|
|
383
|
-
enumerable: true,
|
|
384
|
-
get: function () {
|
|
385
|
-
return passwordProtectKey;
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
Object.defineProperty(exports, 'removePasswordProtection', {
|
|
389
|
-
enumerable: true,
|
|
390
|
-
get: function () {
|
|
391
|
-
return removePasswordProtection;
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
Object.defineProperty(exports, 'unwrapKey', {
|
|
395
|
-
enumerable: true,
|
|
396
|
-
get: function () {
|
|
397
|
-
return unwrapKey;
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
Object.defineProperty(exports, 'wrapKey', {
|
|
401
|
-
enumerable: true,
|
|
402
|
-
get: function () {
|
|
403
|
-
return wrapKey;
|
|
404
|
-
}
|
|
405
|
-
});
|
|
406
|
-
//# sourceMappingURL=emailKeys-BFI-paQq.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"emailKeys-BFI-paQq.js","names":["genSymmetricKey","UTF8ToUint8","encryptSymmetrically","uint8ArrayToBase64","uint8ToUTF8","decryptSymmetrically","base64ToUint8Array","encapsulateHybrid","decapsulateHybrid","getKeyFromPassword","getKeyFromPasswordAndSalt","genHybridKeys","deriveKeyFromMnemonic","CONTEXT_DATABASE","CONTEXT_DRAFT"],"sources":["../src/key-wrapper/aesWrapper.ts","../src/email-crypto/core.ts","../src/email-crypto/hybridEncyptedEmail.ts","../src/email-crypto/pwdProtectedEmail.ts","../src/email-crypto/emailKeys.ts"],"sourcesContent":["import { aeskw } from '@noble/ciphers/aes.js';\n\n/**\n * Unwraps the given wrapped key\n *\n * @param encryptedKey - The wrapped key\n * @param wrappingKey - The secret key used for unwrapping\n * @returns The resulting key\n */\nexport async function unwrapKey(encryptedKey: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).decrypt(encryptedKey);\n}\n\n/**\n * Wraps the given key\n *\n * @param key - The key to be wrapped\n * @param wrappingKey - The secret key used for wrapping\n * @returns The resulting ciphertext\n */\nexport async function wrapKey(key: Uint8Array, wrappingKey: Uint8Array): Promise<Uint8Array> {\n return aeskw(wrappingKey).encrypt(key);\n}\n","import { HybridEncKey, PwdProtectedKey, EmailBody, EmailBodyEncrypted, RecipientWithPublicKey } from '../types';\nimport { encryptSymmetrically, decryptSymmetrically, genSymmetricKey } from '../symmetric-crypto';\nimport { encapsulateHybrid, decapsulateHybrid } from '../hybrid-crypto';\nimport { wrapKey, unwrapKey } from '../key-wrapper';\nimport { getKeyFromPassword, getKeyFromPasswordAndSalt } from '../derive-password';\nimport { UTF8ToUint8, base64ToUint8Array, uint8ArrayToBase64, uint8ToUTF8 } from '../utils';\n\n/**\n * Symmetrically encrypts email body.\n *\n * @param body - The email body to encrypt.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email body and symmetric key used for encryption\n */\nexport async function encryptEmailBody(\n body: EmailBody,\n aux?: Uint8Array,\n): Promise<{\n encEmailBody: EmailBodyEncrypted;\n encryptionKey: Uint8Array;\n}> {\n try {\n if (!body.text || !body.subject) {\n throw new Error('Invalid input');\n }\n const encryptionKey = genSymmetricKey();\n const encEmailBody = await encryptEmailBodyWithKey(body, encryptionKey, aux);\n\n return { encEmailBody, encryptionKey };\n } catch (error) {\n throw new Error('Failed to symmetrically encrypt email body', { cause: error });\n }\n}\n\n/**\n * Symmetrically encrypts email body with the given key.\n *\n * @param body - The email body to encrypt.\n * @param encryptionKey - The symmetric key to encrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting encrypted email body and symmetric key used for encryption\n */\nexport async function encryptEmailBodyWithKey(\n body: EmailBody,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailBodyEncrypted> {\n try {\n const text = UTF8ToUint8(body.text);\n const subject = UTF8ToUint8(body.subject);\n const subjectEnc = await encryptSymmetrically(encryptionKey, subject, aux);\n const encryptedText = await encryptSymmetrically(encryptionKey, text, aux);\n const encText = uint8ArrayToBase64(encryptedText);\n const encSubject = uint8ArrayToBase64(subjectEnc);\n const enc: EmailBodyEncrypted = { encText, encSubject };\n\n if (body.attachments) {\n const promises = body.attachments.map((attachment) => {\n const binaryAttachment = UTF8ToUint8(attachment);\n return encryptSymmetrically(encryptionKey, binaryAttachment, aux);\n });\n const encryptedAttachments = await Promise.all(promises);\n enc.encAttachments = encryptedAttachments?.map(uint8ArrayToBase64);\n }\n\n return enc;\n } catch (error) {\n throw new Error('Failed to encrypt email body', { cause: error });\n }\n}\n\n/**\n * Decrypts symmetrically encrypted email body.\n *\n * @param encEmailBody - The email body to decrypt.\n * @param encryptionKey - The symmetric key to decrypt the email.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The resulting decrypted email body\n */\nexport async function decryptEmailBody(\n encEmailBody: EmailBodyEncrypted,\n encryptionKey: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailBody> {\n try {\n const encSubject = base64ToUint8Array(encEmailBody.encSubject);\n const subjectArray = await decryptSymmetrically(encryptionKey, encSubject, aux);\n const subject = uint8ToUTF8(subjectArray);\n const encText = base64ToUint8Array(encEmailBody.encText);\n const textArray = await decryptSymmetrically(encryptionKey, encText, aux);\n const text = uint8ToUTF8(textArray);\n const body: EmailBody = { text, subject };\n\n if (encEmailBody.encAttachments) {\n const encAttachments = encEmailBody.encAttachments?.map(base64ToUint8Array);\n const promises = encAttachments?.map((encAtt) => decryptSymmetrically(encryptionKey, encAtt, aux));\n const decryptedAttachments = await Promise.all(promises);\n body.attachments = decryptedAttachments?.map((att) => uint8ToUTF8(att));\n }\n\n return body;\n } catch (error) {\n throw new Error('Failed to symmetrically decrypt email body', { cause: error });\n }\n}\n\n/**\n * Encrypts the email symmetric key using hybrid encryption.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param recipient - The recipient with a public hybrid key.\n * @returns The encrypted email symmetric key\n */\nexport async function encryptKeysHybrid(\n emailEncryptionKey: Uint8Array,\n recipient: RecipientWithPublicKey,\n): Promise<HybridEncKey> {\n try {\n const { cipherText, sharedSecret } = encapsulateHybrid(recipient.publicHybridKey);\n const encryptedKey = await wrapKey(emailEncryptionKey, sharedSecret);\n const encryptedKeyBase64 = uint8ArrayToBase64(encryptedKey);\n const kyberCiphertextBase64 = uint8ArrayToBase64(cipherText);\n\n return {\n encryptedKey: encryptedKeyBase64,\n hybridCiphertext: kyberCiphertextBase64,\n encryptedForEmail: recipient.email,\n };\n } catch (error) {\n throw new Error('Failed to encrypt email key using hybrid encryption', { cause: error });\n }\n}\n\n/**\n * Decrypts the email symmetric key encrypted via hybrid encryption.\n *\n * @param encryptedKey - The encrypted email key.\n * @param recipientPrivateKey - The private key of the recipient.\n * @returns The email encryption key\n */\nexport async function decryptKeysHybrid(\n encryptedKey: HybridEncKey,\n recipientPrivateKey: Uint8Array,\n): Promise<Uint8Array> {\n try {\n const kyberCiphertext = base64ToUint8Array(encryptedKey.hybridCiphertext);\n const encKey = base64ToUint8Array(encryptedKey.encryptedKey);\n const sharedSecret = decapsulateHybrid(kyberCiphertext, recipientPrivateKey);\n const encryptionKey = await unwrapKey(encKey, sharedSecret);\n return encryptionKey;\n } catch (error) {\n throw new Error('Failed to decrypt email key encrypted via hybrid encryption', { cause: error });\n }\n}\n\n/**\n * Password-protects the email symmetric key.\n *\n * @param emailEncryptionKey - The symmetric key used for email encryption.\n * @param password - The secret password for key protection.\n * @returns The password-protected email symmetric key\n */\nexport async function passwordProtectKey(emailEncryptionKey: Uint8Array, password: string): Promise<PwdProtectedKey> {\n try {\n const { key, salt } = await getKeyFromPassword(password);\n const encryptedKey = await wrapKey(emailEncryptionKey, key);\n const saltStr = uint8ArrayToBase64(salt);\n const encryptedKeyStr = uint8ArrayToBase64(encryptedKey);\n return { encryptedKey: encryptedKeyStr, salt: saltStr };\n } catch (error) {\n throw new Error('Failed to password-protect email key', { cause: error });\n }\n}\n\n/**\n * Removes passoword-protection and exposes the email symmetric key.\n *\n * @param emailEncryptionKey - The password-protected email key.\n * @param password - The secret password for key protection.\n * @returns The email encryption key\n */\nexport async function removePasswordProtection(\n emailEncryptionKey: PwdProtectedKey,\n password: string,\n): Promise<Uint8Array> {\n try {\n const salt = base64ToUint8Array(emailEncryptionKey.salt);\n const encryptedKey = base64ToUint8Array(emailEncryptionKey.encryptedKey);\n const key = await getKeyFromPasswordAndSalt(password, salt);\n const encryptionKey = await unwrapKey(encryptedKey, key);\n return encryptionKey;\n } catch (error) {\n throw new Error('Failed to remove password-protection from email key', { cause: error });\n }\n}\n","import { HybridEncryptedEmail, EmailBody, RecipientWithPublicKey } from '../types';\nimport { decryptEmailBody, encryptKeysHybrid, decryptKeysHybrid, encryptEmailBody } from './core';\n\n/**\n * Encrypts the email body using hybrid encryption.\n *\n * @param body - The email body to encrypt.\n * @param recipientPublicKeys - The public keys of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The encrypted email body\n */\nexport async function encryptEmailHybrid(\n body: EmailBody,\n recipient: RecipientWithPublicKey,\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmail> {\n try {\n const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n return { encEmailBody, encryptedKey };\n } catch (error) {\n throw new Error('Failed to encrypt email body with hybrid encryption', { cause: error });\n }\n}\n\n/**\n * Encrypts the email body using hybrid encryption for multiple recipients.\n *\n * @param body - The email body to encrypt for multiple recipients.\n * @param recipients - The recipients with corresponding public keys.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The set of encrypted email bodies\n */\nexport async function encryptEmailHybridForMultipleRecipients(\n body: EmailBody,\n recipients: RecipientWithPublicKey[],\n aux?: Uint8Array,\n): Promise<HybridEncryptedEmail[]> {\n try {\n const { encryptionKey, encEmailBody } = await encryptEmailBody(body, aux);\n\n const encryptedEmails: HybridEncryptedEmail[] = [];\n for (const recipient of recipients) {\n const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient);\n encryptedEmails.push({\n encEmailBody: encEmailBody,\n encryptedKey,\n });\n }\n return encryptedEmails;\n } catch (error) {\n throw new Error('Failed to encrypt email to multiple recipients with hybrid encryption', { cause: error });\n }\n}\n\n/**\n * Decrypts the email using hybrid encryption.\n *\n * @param encEmailBody - The encrypted email.\n * @param recipientPrivateHybridKeys - The private key of the recipient.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email body\n */\nexport async function decryptEmailHybrid(\n encEmailBody: HybridEncryptedEmail,\n recipientPrivateHybridKeys: Uint8Array,\n aux?: Uint8Array,\n): Promise<EmailBody> {\n try {\n const encryptionKey = await decryptKeysHybrid(encEmailBody.encryptedKey, recipientPrivateHybridKeys);\n const body = await decryptEmailBody(encEmailBody.encEmailBody, encryptionKey, aux);\n return body;\n } catch (error) {\n throw new Error('Failed to decrypt email with hybrid encryption', { cause: error });\n }\n}\n","import { PwdProtectedEmail, EmailBody } from '../types';\nimport { decryptEmailBody, passwordProtectKey, removePasswordProtection, encryptEmailBody } from './core';\n\n/**\n * Creates a password-protected email.\n *\n * @param email - The email to password-protect\n * @param password - The secret password shared among recipients\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The password-protected email\n */\nexport async function createPwdProtectedEmail(\n emailBody: EmailBody,\n password: string,\n aux?: Uint8Array,\n): Promise<PwdProtectedEmail> {\n try {\n const { encryptionKey, encEmailBody } = await encryptEmailBody(emailBody, aux);\n const encryptedKey = await passwordProtectKey(encryptionKey, password);\n\n return { encEmailBody, encryptedKey };\n } catch (error) {\n throw new Error('Failed to password-protect email', { cause: error });\n }\n}\n\n/**\n * Opens a password-protected email.\n *\n * @param encryptedEmail - The encrypted email\n * @param password - The secret password shared among recipients.\n * @param aux - An optional auxilary sting for AEAD (e.g., email ID or timestamp).\n * @returns The decrypted email body\n */\nexport async function decryptPwdProtectedEmail(\n encryptedEmail: PwdProtectedEmail,\n password: string,\n aux?: Uint8Array,\n): Promise<EmailBody> {\n try {\n const encryptionKey = await removePasswordProtection(encryptedEmail.encryptedKey, password);\n const body = await decryptEmailBody(encryptedEmail.encEmailBody, encryptionKey, aux);\n return body;\n } catch (error) {\n throw new Error('Failed to decrypt password-protect email', { cause: error });\n }\n}\n","import { genHybridKeys } from '../hybrid-crypto';\nimport { HybridKeyPair } from '../types';\nimport { deriveKeyFromMnemonic } from '../derive-key';\nimport { CONTEXT_DATABASE, CONTEXT_DRAFT } from '../constants';\n\n/**\n * Generates public and private keys for email encryption.\n *\n * @returns The user's private and public keys\n */\nexport async function generateEmailKeys(): Promise<HybridKeyPair> {\n return genHybridKeys();\n}\n\n/**\n * Derives database encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting database\n */\nexport const deriveDatabaseKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DATABASE);\n};\n\n/**\n * Derives email draft encryption key for the given user\n *\n * @param mnemonic - The user's mnemonic (machine-generated with secure PRNG)\n * @returns The symmetric key for protecting email drafts\n */\nexport const deriveEmailDraftKey = async (mnemonic: string): Promise<Uint8Array> => {\n return deriveKeyFromMnemonic(mnemonic, CONTEXT_DRAFT);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AASA,eAAsB,UAAU,cAA0B,aAA8C;AACtG,yCAAa,YAAY,CAAC,QAAQ,aAAa;;;;;;;;;AAUjD,eAAsB,QAAQ,KAAiB,aAA8C;AAC3F,yCAAa,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,gBAAgBA,6BAAiB;AAGvC,SAAO;GAAE,cAFY,MAAM,wBAAwB,MAAM,eAAe,IAAI;GAErD;GAAe;UAC/B,OAAO;AACd,QAAM,IAAI,MAAM,8CAA8C,EAAE,OAAO,OAAO,CAAC;;;;;;;;;;;AAYnF,eAAsB,wBACpB,MACA,eACA,KAC6B;AAC7B,KAAI;EACF,MAAM,OAAOC,0BAAY,KAAK,KAAK;EAEnC,MAAM,aAAa,MAAMC,iCAAqB,eAD9BD,0BAAY,KAAK,QAAQ,EAC6B,IAAI;EAI1E,MAAM,MAA0B;GAAE,SAFlBE,iCADM,MAAMD,iCAAqB,eAAe,MAAM,IAAI,CACzB;GAEN,YADxBC,iCAAmB,WAA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