making crypto interface async

This commit is contained in:
Roland Osborne 2024-09-23 15:32:51 -07:00
parent d08f5e877e
commit 1440ac2bba
2 changed files with 20 additions and 20 deletions

View File

@ -1,29 +1,29 @@
export interface Crypto {
// generate salt for pbk function
pbkdfSalt(): { saltHex: string };
pbkdfSalt(): Promise<{ saltHex: string }>;
// generate aes key with pbkdf2
pbkdfKey(saltHex: string, password: string): { aesKeyHex: string };
pbkdfKey(saltHex: string, password: string): Promise<{ aesKeyHex: string }>;
// generate random aes key
aesKey(): { aesKeyHex: string };
aesKey(): Promise<{ aesKeyHex: string }>;
// generate iv to use to aes function
aesIv(): { ivHex: string };
aesIv(): Promise<{ ivHex: string }>;
// encrypt data with aes key and iv
aesEncrypt(data: string, ivHex: string, aesKeyHex: string): { encryptedDataB64: string };
aesEncrypt(data: string, ivHex: string, aesKeyHex: string): Promise<{ encryptedDataB64: string }>;
// decrypt data with aes key and iv
aesDecrypt(encryptedDataB64: string, ivHex: string, aesKeyHex: string): { data: string };
aesDecrypt(encryptedDataB64: string, ivHex: string, aesKeyHex: string): Promise<{ data: string }>;
// generate rsa key
rsaKey(): { publicKeyB64: string, privateKeyB64: string };
rsaKey(): Promise<{ publicKeyB64: string, privateKeyB64: string }>;
// encrypt data with public rsa key
rsaEncrypt(data: string, publicKeyB64: string): { encryptedDataB64: string }
rsaEncrypt(data: string, publicKeyB64: string): Promise<{ encryptedDataB64: string }>;
// decrypt data with private rsa key
rsaDecrypt(encryptedDataB64: string, privateKeyB64: string): { data: string }
rsaDecrypt(encryptedDataB64: string, privateKeyB64: string): Promise<{ data: string }>;
}

View File

@ -165,11 +165,11 @@ export class SettingsModule implements Settings {
if (!crypto) {
throw new Error('crypto not enabled');
}
const { saltHex } = crypto.pbkdfSalt();
const { aesKeyHex } = crypto.pbkdfKey(saltHex, password);
const { publicKeyB64, privateKeyB64 } = crypto.rsaKey();
const { ivHex } = crypto.aesIv();
const { encryptedDataB64 } = crypto.aesEncrypt(privateKeyB64, ivHex, aesKeyHex);
const { saltHex } = await crypto.pbkdfSalt();
const { aesKeyHex } = await crypto.pbkdfKey(saltHex, password);
const { publicKeyB64, privateKeyB64 } = await crypto.rsaKey();
const { ivHex } = await crypto.aesIv();
const { encryptedDataB64 } = await crypto.aesEncrypt(privateKeyB64, ivHex, aesKeyHex);
const seal = { passwordSalt: saltHex, privateKeyIv: ivHex, privateKeyEncrypted: encryptedDataB64, publicKey: publicKeyB64 };
await setAccountSeal(node, secure, token, seal);
this.seal = { publicKey: publicKeyB64, privateKey: privateKeyB64 };
@ -185,10 +185,10 @@ export class SettingsModule implements Settings {
if (!this.seal || this.seal.publicKey !== config.seal.publicKey) {
throw new Error('seal not unlocked');
}
const { saltHex } = crypto.pbkdfSalt();
const { aesKeyHex } = crypto.pbkdfKey(saltHex, password);
const { ivHex } = crypto.aesIv();
const { encryptedDataB64 } = crypto.aesEncrypt(this.seal.privateKey, ivHex, aesKeyHex);
const { saltHex } = await crypto.pbkdfSalt();
const { aesKeyHex } = await crypto.pbkdfKey(saltHex, password);
const { ivHex } = await crypto.aesIv();
const { encryptedDataB64 } = await crypto.aesEncrypt(this.seal.privateKey, ivHex, aesKeyHex);
const seal = { passwordSalt: saltHex, privateKeyIv: ivHex, privateKeyEncrypted: encryptedDataB64, publicKey: config.seal.publicKey };
await setAccountSeal(node, secure, token, seal);
}
@ -210,8 +210,8 @@ export class SettingsModule implements Settings {
if (!crypto) {
throw new Error('crypto not set');
}
const { aesKeyHex } = crypto.pbkdfKey(passwordSalt, password);
const { data } = crypto.aesDecrypt(privateKeyEncrypted, privateKeyIv, aesKeyHex);
const { aesKeyHex } = await crypto.pbkdfKey(passwordSalt, password);
const { data } = await crypto.aesDecrypt(privateKeyEncrypted, privateKeyIv, aesKeyHex);
const seal = { publicKey: publicKey, privateKey: data };
this.store.setSeal(guid, seal);
this.seal = seal;