-
Notifications
You must be signed in to change notification settings - Fork 0
[_] Switch to noble hybrid #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,11 @@ | ||
| import { generateEccKeys } from '../asymmetric-crypto'; | ||
| import { generateKyberKeys } from '../post-quantum-crypto'; | ||
| import { EmailKeys } from '../types'; | ||
| import { genHybridKeys } from '../hybrid-crypto'; | ||
| import { HybridKeyPair } from '../types'; | ||
|
|
||
| /** | ||
| * Generates public and private keys for email service. | ||
| * Generates public and private keys for email encryption. | ||
| * | ||
| * @returns The user's private and public keys | ||
| */ | ||
| export async function generateEmailKeys(): Promise<EmailKeys> { | ||
| try { | ||
| const kyberKeys = generateKyberKeys(); | ||
| const keys = await generateEccKeys(); | ||
|
|
||
| const emailKeys: EmailKeys = { | ||
| publicKeys: { | ||
| eccPublicKey: keys.publicKey, | ||
| kyberPublicKey: kyberKeys.publicKey, | ||
| }, | ||
| privateKeys: { | ||
| eccPrivateKey: keys.privateKey, | ||
| kyberPrivateKey: kyberKeys.secretKey, | ||
| }, | ||
| }; | ||
|
|
||
| return emailKeys; | ||
| } catch (error) { | ||
| throw new Error('Failed to generate keys for email service', { cause: error }); | ||
| } | ||
| export async function generateEmailKeys(): Promise<HybridKeyPair> { | ||
| return genHybridKeys(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,22 @@ | ||
| import { PublicKeys, PrivateKeys, HybridEncryptedEmail, Email, UserWithPublicKeys } from '../types'; | ||
| import { HybridEncryptedEmail, Email, UserWithPublicKey } from '../types'; | ||
| import { decryptEmailBody, encryptKeysHybrid, decryptKeysHybrid, encryptEmailBody } from './core'; | ||
|
|
||
| /** | ||
| * Encrypts the email using hybrid encryption. | ||
| * | ||
| * @param email - The email to encrypt. | ||
| * @param recipientPublicKeys - The public keys of the recipient. | ||
| * @param senderPrivateKey - The private key of the sender. | ||
| * @param isSubjectEncrypted - Indicates if the email subject field should be encrypted | ||
| * @returns The encrypted email | ||
| */ | ||
| export async function encryptEmailHybrid( | ||
| email: Email, | ||
| recipient: UserWithPublicKeys, | ||
| senderPrivateKey: PrivateKeys, | ||
| recipient: UserWithPublicKey, | ||
| isSubjectEncrypted: boolean = false, | ||
| ): Promise<HybridEncryptedEmail> { | ||
| try { | ||
| const { encryptionKey, params, enc } = await encryptEmailBody(email, isSubjectEncrypted); | ||
| const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); | ||
| const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicHybridKey); | ||
| return { enc, encryptedKey, recipientEmail: recipient.email, params, isSubjectEncrypted, id: email.id }; | ||
| } catch (error) { | ||
| throw new Error('Failed to encrypt email with hybrid encryption', { cause: error }); | ||
|
|
@@ -30,22 +28,20 @@ export async function encryptEmailHybrid( | |
| * | ||
| * @param email - The email to encrypt. | ||
| * @param recipients - The recipients with corresponding public keys. | ||
| * @param senderPrivateKey - The private key of the sender. | ||
| * @param isSubjectEncrypted - Indicates if the email subject field should be encrypted | ||
| * @returns The set of encrypted email | ||
| */ | ||
| export async function encryptEmailHybridForMultipleRecipients( | ||
| email: Email, | ||
| recipients: UserWithPublicKeys[], | ||
| senderPrivateKey: PrivateKeys, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
| recipients: UserWithPublicKey[], | ||
| isSubjectEncrypted: boolean = false, | ||
| ): Promise<HybridEncryptedEmail[]> { | ||
| try { | ||
| const { encryptionKey, params, enc } = await encryptEmailBody(email, isSubjectEncrypted); | ||
|
|
||
| const encryptedEmails: HybridEncryptedEmail[] = []; | ||
| for (const recipient of recipients) { | ||
| const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicKeys, senderPrivateKey); | ||
| const encryptedKey = await encryptKeysHybrid(encryptionKey, recipient.publicHybridKey); | ||
| encryptedEmails.push({ | ||
| enc, | ||
| encryptedKey, | ||
|
|
@@ -65,18 +61,16 @@ export async function encryptEmailHybridForMultipleRecipients( | |
| * Decrypts the email using hybrid encryption. | ||
| * | ||
| * @param encryptedEmail - The encrypted email. | ||
| * @param senderPublicKeys - The public key of the sender. | ||
| * @param recipientPrivateKeys - The private key of the recipient. | ||
| * @param recipientPrivateHybridKeys - The private key of the recipient. | ||
| * @returns The decrypted email | ||
| */ | ||
| export async function decryptEmailHybrid( | ||
| encryptedEmail: HybridEncryptedEmail, | ||
| senderPublicKeys: PublicKeys, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same (also remember to remove the params from the JSDoc) :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, will check
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| recipientPrivateKeys: PrivateKeys, | ||
| recipientPrivateHybridKeys: Uint8Array, | ||
| ): Promise<Email> { | ||
| try { | ||
| const { isSubjectEncrypted, params: encParams, enc, encryptedKey, id } = encryptedEmail; | ||
| const encryptionKey = await decryptKeysHybrid(encryptedKey, senderPublicKeys, recipientPrivateKeys); | ||
| const encryptionKey = await decryptKeysHybrid(encryptedKey, recipientPrivateHybridKeys); | ||
| const { body, params } = await decryptEmailBody(enc, encParams, encryptionKey, isSubjectEncrypted); | ||
| return { body, params, id }; | ||
| } catch (error) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why sender key has been removed here? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because X-Wing doesn't need it. Before, I was doing ECC secret derivation, Kyber KEM encapsulation that gave another secret, then deriving one key from both secrets. X-Wing does it differently; they take public key of the intended recipient as input and handle everything else. The hybrid key is this:
