1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-27 13:43:41 +00:00

[PM-16831] TS Strict crypto function service (#12737)

* strict types in crypto function services

* Improve aesDecrypt types
This commit is contained in:
Matt Gibson
2025-01-09 18:58:22 -05:00
committed by GitHub
parent 8cabb36c99
commit 6ef3e9a076
8 changed files with 117 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { DecryptParameters } from "@bitwarden/common/platform/models/domain/decrypt-parameters";
import { EcbDecryptParameters } from "@bitwarden/common/platform/models/domain/decrypt-parameters";
import { SymmetricCryptoKey } from "@bitwarden/common/platform/models/domain/symmetric-crypto-key";
import { NodeCryptoFunctionService } from "./node-crypto-function.service";
@@ -193,8 +193,8 @@ describe("NodeCrypto Function Service", () => {
const iv = Utils.fromBufferToB64(makeStaticByteArray(16));
const symKey = new SymmetricCryptoKey(makeStaticByteArray(32));
const data = "ByUF8vhyX4ddU9gcooznwA==";
const params = nodeCryptoFunctionService.aesDecryptFastParameters(data, iv, null, symKey);
const decValue = await nodeCryptoFunctionService.aesDecryptFast(params, "cbc");
const parameters = nodeCryptoFunctionService.aesDecryptFastParameters(data, iv, null, symKey);
const decValue = await nodeCryptoFunctionService.aesDecryptFast({ mode: "cbc", parameters });
expect(decValue).toBe("EncryptMe!");
});
});
@@ -202,10 +202,11 @@ describe("NodeCrypto Function Service", () => {
describe("aesDecryptFast ECB mode", () => {
it("should successfully decrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const params = new DecryptParameters<Uint8Array>();
params.encKey = makeStaticByteArray(32);
params.data = Utils.fromB64ToArray("z5q2XSxYCdQFdI+qK2yLlw==");
const decValue = await nodeCryptoFunctionService.aesDecryptFast(params, "ecb");
const parameters: EcbDecryptParameters<Uint8Array> = {
encKey: makeStaticByteArray(32),
data: Utils.fromB64ToArray("z5q2XSxYCdQFdI+qK2yLlw=="),
};
const decValue = await nodeCryptoFunctionService.aesDecryptFast({ mode: "ecb", parameters });
expect(decValue).toBe("EncryptMe!");
});
});
@@ -219,6 +220,15 @@ describe("NodeCrypto Function Service", () => {
const decValue = await nodeCryptoFunctionService.aesDecrypt(data, iv, key, "cbc");
expect(Utils.fromBufferToUtf8(decValue)).toBe("EncryptMe!");
});
it("throws if IV is not provided", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const key = makeStaticByteArray(32);
const data = Utils.fromB64ToArray("ByUF8vhyX4ddU9gcooznwA==");
await expect(
async () => await nodeCryptoFunctionService.aesDecrypt(data, null, key, "cbc"),
).rejects.toThrow("Invalid initialization vector");
});
});
describe("aesDecrypt ECB mode", () => {
@@ -454,7 +464,7 @@ function testHmac(algorithm: "sha1" | "sha256" | "sha512", mac: string, fast = f
const cryptoFunctionService = new NodeCryptoFunctionService();
const value = Utils.fromUtf8ToArray("SignMe!!");
const key = Utils.fromUtf8ToArray("secretkey");
let computedMac: ArrayBuffer = null;
let computedMac: ArrayBuffer;
if (fast) {
computedMac = await cryptoFunctionService.hmacFast(value, key, algorithm);
} else {