mirror of
https://github.com/bitwarden/browser
synced 2026-02-13 15:03:26 +00:00
Prefer key encapsulation to shared key derivation
This commit is contained in:
@@ -283,61 +283,6 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
});
|
||||
}
|
||||
|
||||
async diffieHellmanGenerateKeyPair(
|
||||
algorithm: "x25519" | "ecdh",
|
||||
curve: undefined | "P-256" | "P-384" | "P-521",
|
||||
): Promise<{
|
||||
keyPair: CryptoKeyPair;
|
||||
publicKey: Uint8Array;
|
||||
}> {
|
||||
if (algorithm === "x25519" && curve != null) {
|
||||
throw new Error("x25519 does not use the curve parameter.");
|
||||
}
|
||||
|
||||
const keys = await crypto.subtle.generateKey(
|
||||
{
|
||||
name: algorithm,
|
||||
namedCurve: curve,
|
||||
},
|
||||
true,
|
||||
["deriveKey", "deriveBits"],
|
||||
);
|
||||
return {
|
||||
keyPair: keys,
|
||||
publicKey: new Uint8Array(await crypto.subtle.exportKey("raw", keys.publicKey)),
|
||||
};
|
||||
}
|
||||
|
||||
async deriveSharedKeyBits(
|
||||
privateKey: CryptoKey,
|
||||
publicKeyRaw: Uint8Array,
|
||||
algorithm: "x25519" | "ecdh",
|
||||
curve: undefined | "P-256" | "P-384" | "P-521",
|
||||
): Promise<Uint8Array> {
|
||||
if (algorithm === "x25519" && curve != null) {
|
||||
throw new Error("x25519 does not use the curve parameter.");
|
||||
}
|
||||
|
||||
const publicKey = await crypto.subtle.importKey(
|
||||
"raw",
|
||||
publicKeyRaw,
|
||||
{ name: algorithm, namedCurve: curve },
|
||||
true,
|
||||
[],
|
||||
);
|
||||
|
||||
const dhSecret = await crypto.subtle.deriveBits(
|
||||
{
|
||||
name: algorithm,
|
||||
public: publicKey,
|
||||
},
|
||||
privateKey,
|
||||
256,
|
||||
);
|
||||
|
||||
return new Uint8Array(dhSecret);
|
||||
}
|
||||
|
||||
aesGenerateKey(bitLength: 128 | 192 | 256 | 512): Promise<CsprngArray> {
|
||||
return this.randomBytes(bitLength / 8);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user