mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
allow userId and pub key to be passed in
This commit is contained in:
@@ -16,7 +16,7 @@ export abstract class CryptoService {
|
|||||||
getEncKey: () => Promise<SymmetricCryptoKey>;
|
getEncKey: () => Promise<SymmetricCryptoKey>;
|
||||||
getPublicKey: () => Promise<ArrayBuffer>;
|
getPublicKey: () => Promise<ArrayBuffer>;
|
||||||
getPrivateKey: () => Promise<ArrayBuffer>;
|
getPrivateKey: () => Promise<ArrayBuffer>;
|
||||||
getFingerprint: () => Promise<string[]>;
|
getFingerprint: (userId: string, publicKey?: ArrayBuffer) => Promise<string[]>;
|
||||||
getOrgKeys: () => Promise<Map<string, SymmetricCryptoKey>>;
|
getOrgKeys: () => Promise<Map<string, SymmetricCryptoKey>>;
|
||||||
getOrgKey: (orgId: string) => Promise<SymmetricCryptoKey>;
|
getOrgKey: (orgId: string) => Promise<SymmetricCryptoKey>;
|
||||||
hasKey: () => Promise<boolean>;
|
hasKey: () => Promise<boolean>;
|
||||||
|
|||||||
@@ -166,13 +166,15 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return this.privateKey;
|
return this.privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFingerprint(): Promise<string[]> {
|
async getFingerprint(userId: string, publicKey?: ArrayBuffer): Promise<string[]> {
|
||||||
const publicKey = await this.getPublicKey();
|
if (publicKey == null) {
|
||||||
|
publicKey = await this.getPublicKey();
|
||||||
|
}
|
||||||
if (publicKey === null) {
|
if (publicKey === null) {
|
||||||
throw new Error('No public key available.');
|
throw new Error('No public key available.');
|
||||||
}
|
}
|
||||||
const keyFingerprint = await this.cryptoFunctionService.hash(publicKey, 'sha256');
|
const keyFingerprint = await this.cryptoFunctionService.hash(publicKey, 'sha256');
|
||||||
const userFingerprint = await this.hkdfExpand(keyFingerprint, Utils.fromUtf8ToArray('USER-ID'), 32);
|
const userFingerprint = await this.hkdfExpand(keyFingerprint, Utils.fromUtf8ToArray(userId), 32);
|
||||||
return this.hashPhrase(userFingerprint.buffer);
|
return this.hashPhrase(userFingerprint.buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -688,12 +690,12 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return okm;
|
return okm;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async hashPhrase(data: ArrayBuffer, minimumEntropy: number = 64) {
|
private async hashPhrase(data: ArrayBuffer, minimumEntropy: number = 64, hashIterations: number = 50000) {
|
||||||
const wordListLength = EEFLongWordList.length;
|
const entropyPerWord = Math.log(EEFLongWordList.length) / Math.log(2);
|
||||||
const entropyPerWord = Math.log(wordListLength) / Math.log(2);
|
|
||||||
let numWords = Math.ceil(minimumEntropy / entropyPerWord);
|
let numWords = Math.ceil(minimumEntropy / entropyPerWord);
|
||||||
|
|
||||||
const hashBuffer = await this.cryptoFunctionService.pbkdf2(data, '', 'sha256', 50000);
|
const hashBuffer = await this.cryptoFunctionService.pbkdf2(data, new Uint8Array([]).buffer,
|
||||||
|
'sha256', hashIterations);
|
||||||
const hash = Array.from(new Uint8Array(hashBuffer));
|
const hash = Array.from(new Uint8Array(hashBuffer));
|
||||||
const entropyAvailable = hash.length * 4;
|
const entropyAvailable = hash.length * 4;
|
||||||
if (numWords * entropyPerWord > entropyAvailable) {
|
if (numWords * entropyPerWord > entropyAvailable) {
|
||||||
@@ -703,8 +705,8 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
const phrase: string[] = [];
|
const phrase: string[] = [];
|
||||||
let hashNumber = bigInt.fromArray(hash, 256);
|
let hashNumber = bigInt.fromArray(hash, 256);
|
||||||
while (numWords--) {
|
while (numWords--) {
|
||||||
const remainder = hashNumber.mod(wordListLength);
|
const remainder = hashNumber.mod(EEFLongWordList.length);
|
||||||
hashNumber = hashNumber.divide(wordListLength);
|
hashNumber = hashNumber.divide(EEFLongWordList.length);
|
||||||
phrase.push(EEFLongWordList[remainder as any]);
|
phrase.push(EEFLongWordList[remainder as any]);
|
||||||
}
|
}
|
||||||
return phrase;
|
return phrase;
|
||||||
|
|||||||
Reference in New Issue
Block a user