-
Notifications
You must be signed in to change notification settings - Fork 33
[PB-4610] Opaque PoC #1690
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
Draft
TamaraFinogina
wants to merge
40
commits into
master
Choose a base branch
from
opaque_poc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[PB-4610] Opaque PoC #1690
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
7909f63
add opaque
TamaraFinogina 496bc7a
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 1839209
add disble 2FA
TamaraFinogina 9489a85
add password change, add login after signup
TamaraFinogina 6a1dcf1
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 628aa90
remove extra changes
TamaraFinogina 04077cc
add tests for opaque versions of signUp, login, disable2FA, and chan…
TamaraFinogina 44db502
add new files
TamaraFinogina 1bc9d59
reduce changes in yarn.lock
TamaraFinogina bf329f2
reduce changes in types
TamaraFinogina 916fc9c
upgrade internxt-crypto
TamaraFinogina 74868ca
put is2FAorOpaqueNeeded separately
TamaraFinogina 76922ee
remove 2FA example
TamaraFinogina 10cefd0
fix PR comments
TamaraFinogina c65e17c
fix test description
TamaraFinogina 080b94e
don't use localStorage for storing values between tests
TamaraFinogina 872dd0c
add more tests
TamaraFinogina c974d3f
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 945e306
fix sonar errors
TamaraFinogina 5d32592
fix _ in crypto test
TamaraFinogina 3fee78b
fix comments
TamaraFinogina e1223b0
Merge branch 'master' into opaque_poc
CandelR 0ab63be
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 4fba4dd
Merge branch 'master' into opaque_poc
CandelR ee02788
Merge branch 'master' into opaque_poc
CandelR 739f848
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 994b85e
update sdk, update internext-crypto
TamaraFinogina 5d0350e
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 8bcdaa8
yarn.lock changes
TamaraFinogina b9be060
increase node version
TamaraFinogina b97e446
use node 20
TamaraFinogina e54ff76
fix crypto package link
TamaraFinogina c1dda59
fix pathes
TamaraFinogina cac4356
merge master
TamaraFinogina baf3538
address PR comments
TamaraFinogina 7b8bca5
merge master
TamaraFinogina 8a70388
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 435b604
remove diasable 2FA
TamaraFinogina 19f663a
Merge remote-tracking branch 'origin/master' into opaque_poc
TamaraFinogina 6a35e91
update sdk and api
TamaraFinogina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -513,4 +513,4 @@ describe('Testing GA Service', () => { | |
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const ECC_KEY_AUX = 'user-private-key'; | ||
| export const KYBER_KEY_AUX = 'user-private-kyber-key'; | ||
| export const MNEMONIC_AUX = 'user-mnemonic'; | ||
| export const SESSION_KEY_AUX = 'User Session Key'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { generateUserSecrets, encryptUserKeysAndMnemonic, decryptUserKeysAndMnemonic } from './auth.crypto'; | ||
|
|
||
| describe('Test auth crypto functions', () => { | ||
| it('should sucessfully encrypt and decrypt user keys and mnemonic', async () => { | ||
| const { keys, mnemonic } = await generateUserSecrets(); | ||
| const exportKey = 'Srp6AzybbyludWuaVwGoHa1C2H0Qtv7JR0sKGLSWe8Ho8_q9hezfYD2RYb9IUrW999pH4VlABgDLse484zAapg'; | ||
|
|
||
| const { encMnemonic, encKeys } = await encryptUserKeysAndMnemonic(keys, mnemonic, exportKey); | ||
|
|
||
| const { keys: decKeys, mnemonic: decMnemonic } = await decryptUserKeysAndMnemonic(encMnemonic, encKeys, exportKey); | ||
|
|
||
| expect(keys).toStrictEqual(decKeys); | ||
| expect(mnemonic).toEqual(decMnemonic); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { | ||
| encryptSymmetrically, | ||
| deriveSymmetricCryptoKey, | ||
| decryptSymmetrically, | ||
| importSymmetricCryptoKey, | ||
| } from 'internxt-crypto/symmetric-crypto'; | ||
| import { getKeyFromPasswordAndSalt, getKeyFromPassword } from 'internxt-crypto/derive-key'; | ||
| import { | ||
| UTF8ToUint8, | ||
| base64ToUint8Array, | ||
| uint8ArrayToBase64, | ||
| genMnemonic, | ||
| mnemonicToBytes, | ||
| bytesToMnemonic, | ||
| uint8ToUTF8, | ||
| } from 'internxt-crypto/utils'; | ||
| import { UserKeys } from '@internxt/sdk'; | ||
| import { generateNewKeys } from 'app/crypto/services/pgp.service'; | ||
| import { ECC_KEY_AUX, KYBER_KEY_AUX, MNEMONIC_AUX, SESSION_KEY_AUX } from './auth.constants'; | ||
|
|
||
| export async function encryptUserKeysAndMnemonic( | ||
| userKeys: UserKeys, | ||
| mnemonic: string, | ||
| exportKey: string, | ||
| ): Promise<{ encMnemonic: string; encKeys: UserKeys }> { | ||
| const exportKeyBytes = safeBase64ToBytes(exportKey); | ||
| const cryptoKey = await deriveSymmetricCryptoKey(exportKeyBytes); | ||
| const key = UTF8ToUint8(userKeys.ecc.privateKey); | ||
| const encPrivateKey = await encryptSymmetrically(cryptoKey, key, UTF8ToUint8(ECC_KEY_AUX)); | ||
| const keyKyber = base64ToUint8Array(userKeys.kyber.privateKey); | ||
| const encPrivateKyberKey = await encryptSymmetrically(cryptoKey, keyKyber, UTF8ToUint8(KYBER_KEY_AUX)); | ||
| const mnemonicArray = mnemonicToBytes(mnemonic); | ||
| const mnemonicCipher = await encryptSymmetrically(cryptoKey, mnemonicArray, UTF8ToUint8(MNEMONIC_AUX)); | ||
| const encMnemonic = uint8ArrayToBase64(mnemonicCipher); | ||
|
|
||
| const encKeys: UserKeys = { | ||
| ecc: { | ||
| privateKey: uint8ArrayToBase64(encPrivateKey), | ||
| publicKey: userKeys.ecc.publicKey, | ||
| }, | ||
| kyber: { | ||
| privateKey: uint8ArrayToBase64(encPrivateKyberKey), | ||
| publicKey: userKeys.kyber.publicKey, | ||
| }, | ||
| }; | ||
| return { encMnemonic, encKeys }; | ||
| } | ||
|
|
||
| export async function decryptUserKeysAndMnemonic( | ||
| encMnemonic: string, | ||
| encKeys: UserKeys, | ||
| exportKey: string, | ||
| ): Promise<{ keys: UserKeys; mnemonic: string }> { | ||
| const exportKeyBytes = safeBase64ToBytes(exportKey); | ||
| const cryptoKey = await deriveSymmetricCryptoKey(exportKeyBytes); | ||
| const encKey = base64ToUint8Array(encKeys.ecc.privateKey); | ||
| const privateKey = await decryptSymmetrically(cryptoKey, encKey, UTF8ToUint8(ECC_KEY_AUX)); | ||
| const encKyberKey = base64ToUint8Array(encKeys.kyber.privateKey); | ||
| const privateKyberKey = await decryptSymmetrically(cryptoKey, encKyberKey, UTF8ToUint8(KYBER_KEY_AUX)); | ||
| const encMnemonicArray = base64ToUint8Array(encMnemonic); | ||
| const mnemonicArray = await decryptSymmetrically(cryptoKey, encMnemonicArray, UTF8ToUint8(MNEMONIC_AUX)); | ||
| const mnemonic = bytesToMnemonic(mnemonicArray); | ||
|
|
||
| const keys: UserKeys = { | ||
| ecc: { | ||
| privateKey: uint8ToUTF8(privateKey), | ||
| publicKey: encKeys.ecc.publicKey, | ||
| }, | ||
| kyber: { | ||
| privateKey: uint8ArrayToBase64(privateKyberKey), | ||
| publicKey: encKeys.kyber.publicKey, | ||
| }, | ||
| }; | ||
| return { keys, mnemonic }; | ||
| } | ||
|
|
||
| export const encryptSessionKey = async ( | ||
| password: string, | ||
| sessionKey: string, | ||
| ): Promise<{ sessionKeyEnc: string; salt: string }> => { | ||
| const { key, salt } = await getKeyFromPassword(password); | ||
| const cryptoKey = await importSymmetricCryptoKey(key); | ||
| const sessionKeyArray = safeBase64ToBytes(sessionKey); | ||
| const sessionKeyEncCipher = await encryptSymmetrically(cryptoKey, sessionKeyArray, UTF8ToUint8(SESSION_KEY_AUX)); | ||
| const sessionKeyEnc = uint8ArrayToBase64(sessionKeyEncCipher); | ||
| return { sessionKeyEnc, salt: uint8ArrayToBase64(salt) }; | ||
| }; | ||
|
|
||
| export const safeBase64ToBytes = (urlSafeBase64: string): Uint8Array => { | ||
| const base64 = urlSafeBase64.replaceAll('-', '+').replaceAll('_', '/'); | ||
| const padding = (4 - (base64.length % 4)) % 4; | ||
| return base64ToUint8Array(base64 + '='.repeat(padding)); | ||
| }; | ||
|
|
||
| export const decryptSessionKey = async (password: string, sessionKeyEnc: string, salt: string): Promise<Uint8Array> => { | ||
| const keyBytes = await getKeyFromPasswordAndSalt(password, base64ToUint8Array(salt)); | ||
| const key = await importSymmetricCryptoKey(keyBytes); | ||
| const sessionKeyCipher = base64ToUint8Array(sessionKeyEnc); | ||
| const sessionKeyArray = await decryptSymmetrically(key, sessionKeyCipher, UTF8ToUint8(SESSION_KEY_AUX)); | ||
| return sessionKeyArray; | ||
| }; | ||
|
|
||
| export const generateUserSecrets = async (): Promise<{ keys: UserKeys; mnemonic: string }> => { | ||
| const mnemonic = genMnemonic(256); | ||
|
|
||
| const { privateKeyArmored, publicKeyArmored, publicKyberKeyBase64, privateKyberKeyBase64 } = await generateNewKeys(); | ||
| const keys: UserKeys = { | ||
| ecc: { privateKey: privateKeyArmored, publicKey: publicKeyArmored }, | ||
| kyber: { | ||
| privateKey: privateKyberKeyBase64, | ||
| publicKey: publicKyberKeyBase64, | ||
| }, | ||
| }; | ||
| return { keys, mnemonic }; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.