refactor(desktop): derivePublicKey via CryptoBackend.scalarMultBase
This commit is contained in:
@@ -25,5 +25,13 @@ export async function createLibsodiumBackend(): Promise<CryptoBackend> {
|
|||||||
s.crypto_box_open_easy(ciphertext, nonce, senderPublicKey, recipientPrivateKey),
|
s.crypto_box_open_easy(ciphertext, nonce, senderPublicKey, recipientPrivateKey),
|
||||||
secretbox: (plaintext, nonce, key) => s.crypto_secretbox_easy(plaintext, nonce, key),
|
secretbox: (plaintext, nonce, key) => s.crypto_secretbox_easy(plaintext, nonce, key),
|
||||||
secretboxOpen: (ciphertext, nonce, key) => s.crypto_secretbox_open_easy(ciphertext, nonce, key),
|
secretboxOpen: (ciphertext, nonce, key) => s.crypto_secretbox_open_easy(ciphertext, nonce, key),
|
||||||
|
pwhashConsts: {
|
||||||
|
OPSLIMIT_MODERATE: s.crypto_pwhash_OPSLIMIT_MODERATE,
|
||||||
|
MEMLIMIT_MODERATE: s.crypto_pwhash_MEMLIMIT_MODERATE,
|
||||||
|
ALG_ARGON2ID13: s.crypto_pwhash_ALG_ARGON2ID13,
|
||||||
|
},
|
||||||
|
pwhash: (outLen, password, salt, opslimit, memlimit, alg) =>
|
||||||
|
s.crypto_pwhash(outLen, password, salt, opslimit, memlimit, alg),
|
||||||
|
scalarMultBase: (priv) => s.crypto_scalarmult_base(priv),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
resetUserKey, tryUnlockUserKey, uploadUserKeyBlob,
|
resetUserKey, tryUnlockUserKey, uploadUserKeyBlob,
|
||||||
} from '@chat-app/shared/auth';
|
} from '@chat-app/shared/auth';
|
||||||
import {
|
import {
|
||||||
generateRecoveryCode, generateUserKeyPair, normalizeRecoveryCode,
|
generateRecoveryCode, generateUserKeyPair, getCryptoBackend, normalizeRecoveryCode,
|
||||||
openUserKey, sealUserKey,
|
openUserKey, sealUserKey,
|
||||||
} from '@chat-app/shared/crypto';
|
} from '@chat-app/shared/crypto';
|
||||||
import { migrateOwnLegacyBundles } from '@chat-app/shared/chat';
|
import { migrateOwnLegacyBundles } from '@chat-app/shared/chat';
|
||||||
@@ -50,7 +50,7 @@ export async function setupNewUserIdentity(p: SetupParams): Promise<SetupResult>
|
|||||||
export async function ensureLegacyMigrated(userId: string): Promise<void> {
|
export async function ensureLegacyMigrated(userId: string): Promise<void> {
|
||||||
const priv = await devLocalSecretStore.getSecret(cacheKey(userId));
|
const priv = await devLocalSecretStore.getSecret(cacheKey(userId));
|
||||||
if (!priv) return;
|
if (!priv) return;
|
||||||
const pub = await derivePublicKey(priv);
|
const pub = derivePublicKey(priv);
|
||||||
await runLegacyMigration(userId, priv, pub);
|
await runLegacyMigration(userId, priv, pub);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ export async function changePin(params: {
|
|||||||
const fresh = await sealUserKey({ privateKey: cached, pin: params.newPin });
|
const fresh = await sealUserKey({ privateKey: cached, pin: params.newPin });
|
||||||
await uploadUserKeyBlob(supabase, {
|
await uploadUserKeyBlob(supabase, {
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
publicKey: await derivePublicKey(cached),
|
publicKey: derivePublicKey(cached),
|
||||||
sealedPrivateKey: fresh.sealedPrivateKey,
|
sealedPrivateKey: fresh.sealedPrivateKey,
|
||||||
salt: fresh.salt,
|
salt: fresh.salt,
|
||||||
kdfParams: fresh.kdfParams,
|
kdfParams: fresh.kdfParams,
|
||||||
@@ -122,7 +122,7 @@ export async function regenerateRecoveryCode(params: { userId: string }): Promis
|
|||||||
const sealed = await sealUserKey({ privateKey: cached, pin: normalizeRecoveryCode(recoveryCode) });
|
const sealed = await sealUserKey({ privateKey: cached, pin: normalizeRecoveryCode(recoveryCode) });
|
||||||
await uploadUserKeyBlob(supabase, {
|
await uploadUserKeyBlob(supabase, {
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
publicKey: await derivePublicKey(cached),
|
publicKey: derivePublicKey(cached),
|
||||||
sealedPrivateKey: blob.sealedPrivateKey,
|
sealedPrivateKey: blob.sealedPrivateKey,
|
||||||
salt: blob.salt,
|
salt: blob.salt,
|
||||||
kdfParams: blob.kdfParams,
|
kdfParams: blob.kdfParams,
|
||||||
@@ -247,12 +247,10 @@ async function runLegacyMigration(
|
|||||||
export async function retryLegacyMigration(userId: string): Promise<LegacyMigrationReport> {
|
export async function retryLegacyMigration(userId: string): Promise<LegacyMigrationReport> {
|
||||||
const priv = await devLocalSecretStore.getSecret(cacheKey(userId));
|
const priv = await devLocalSecretStore.getSecret(cacheKey(userId));
|
||||||
if (!priv) throw new Error('user key not cached locally — re-login required');
|
if (!priv) throw new Error('user key not cached locally — re-login required');
|
||||||
const pub = await derivePublicKey(priv);
|
const pub = derivePublicKey(priv);
|
||||||
return runLegacyMigration(userId, priv, pub);
|
return runLegacyMigration(userId, priv, pub);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function derivePublicKey(privateKey: Uint8Array): Promise<Uint8Array> {
|
function derivePublicKey(privateKey: Uint8Array): Uint8Array {
|
||||||
const sodium = (await import('libsodium-wrappers-sumo')).default;
|
return getCryptoBackend().scalarMultBase(privateKey);
|
||||||
await sodium.ready;
|
|
||||||
return sodium.crypto_scalarmult_base(privateKey);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user