1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-24 16:49:26 +01:00

remakeEncKey

This commit is contained in:
Kyle Spearrin 2018-08-27 19:06:36 -04:00
parent 1454aff46c
commit 00562d083b
2 changed files with 21 additions and 10 deletions

View File

@ -32,6 +32,7 @@ export abstract class CryptoService {
makeKeyPair: (key?: SymmetricCryptoKey) => Promise<[string, CipherString]>;
hashPassword: (password: string, key: SymmetricCryptoKey) => Promise<string>;
makeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>;
remakeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;

View File

@ -318,16 +318,12 @@ export class CryptoService implements CryptoServiceAbstraction {
async makeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, CipherString]> {
const encKey = await this.cryptoFunctionService.randomBytes(64);
let encKeyEnc: CipherString = null;
if (key.key.byteLength === 32) {
const newKey = await this.stretchKey(key);
encKeyEnc = await this.encrypt(encKey, newKey);
} else if (key.key.byteLength === 64) {
encKeyEnc = await this.encrypt(encKey, key);
} else {
throw new Error('Invalid key size.');
}
return [new SymmetricCryptoKey(encKey), encKeyEnc];
return this.buildEncKey(key, encKey);
}
async remakeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, CipherString]> {
const encKey = await this.getEncKey();
return this.buildEncKey(key, encKey.key);
}
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> {
@ -677,4 +673,18 @@ export class CryptoService implements CryptoServiceAbstraction {
}
return okm;
}
private async buildEncKey(key: SymmetricCryptoKey, encKey: ArrayBuffer = null)
: Promise<[SymmetricCryptoKey, CipherString]> {
let encKeyEnc: CipherString = null;
if (key.key.byteLength === 32) {
const newKey = await this.stretchKey(key);
encKeyEnc = await this.encrypt(encKey, newKey);
} else if (key.key.byteLength === 64) {
encKeyEnc = await this.encrypt(encKey, key);
} else {
throw new Error('Invalid key size.');
}
return [new SymmetricCryptoKey(encKey), encKeyEnc];
}
}