1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-18 02:41:15 +02: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]>; makeKeyPair: (key?: SymmetricCryptoKey) => Promise<[string, CipherString]>;
hashPassword: (password: string, key: SymmetricCryptoKey) => Promise<string>; hashPassword: (password: string, key: SymmetricCryptoKey) => Promise<string>;
makeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>; makeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>;
remakeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>; encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>; encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>; 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]> { async makeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, CipherString]> {
const encKey = await this.cryptoFunctionService.randomBytes(64); const encKey = await this.cryptoFunctionService.randomBytes(64);
let encKeyEnc: CipherString = null; return this.buildEncKey(key, encKey);
if (key.key.byteLength === 32) { }
const newKey = await this.stretchKey(key);
encKeyEnc = await this.encrypt(encKey, newKey); async remakeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, CipherString]> {
} else if (key.key.byteLength === 64) { const encKey = await this.getEncKey();
encKeyEnc = await this.encrypt(encKey, key); return this.buildEncKey(key, encKey.key);
} else {
throw new Error('Invalid key size.');
}
return [new SymmetricCryptoKey(encKey), encKeyEnc];
} }
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> { async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> {
@ -677,4 +673,18 @@ export class CryptoService implements CryptoServiceAbstraction {
} }
return okm; 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];
}
} }