2022-05-23 13:12:03 +00:00
|
|
|
import * as core from "@peculiar/webcrypto-core";
|
|
|
|
import * as types from "@peculiar/webcrypto-types";
|
|
|
|
import * as assert from "assert";
|
|
|
|
|
|
|
|
class TestProvider extends core.ProviderCrypto {
|
|
|
|
public name = "CUSTOM-ALG";
|
|
|
|
public usages: types.KeyUsage[] = ["sign"];
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestAsymmetricProvider extends core.ProviderCrypto {
|
|
|
|
public name = "CUSTOM-ALG";
|
|
|
|
public usages: types.ProviderKeyPairUsage = {
|
|
|
|
privateKey: ["sign"],
|
|
|
|
publicKey: ["verify"],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
context("ProviderCrypto", () => {
|
|
|
|
|
|
|
|
const crypto = new TestProvider();
|
|
|
|
|
|
|
|
context("checkGenerateKey", () => {
|
|
|
|
|
|
|
|
it("error if `keyUsages` argument is empty list", () => {
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.checkGenerateKey({ name: "CUSTOM-ALG" }, true, []);
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("check usages for symmetric key", () => {
|
|
|
|
const aProv = new TestAsymmetricProvider();
|
|
|
|
aProv.checkGenerateKey({ name: "CUSTOM-ALG" }, true, ["sign", "verify"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
context("digest", () => {
|
|
|
|
|
|
|
|
it("correct data", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.digest({ name: "custom-alg" }, new ArrayBuffer(0)),
|
|
|
|
core.UnsupportedOperationError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong name of algorithm", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.digest({ name: "wrong" }, new ArrayBuffer(0)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
context("generateKey", () => {
|
|
|
|
|
|
|
|
it("correct data", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.generateKey({ name: "custom-alg" }, true, ["sign"]),
|
|
|
|
core.UnsupportedOperationError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong name of algorithm", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.generateKey({ name: "wrong" }, false, ["sign"]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong key usages", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.generateKey({ name: "custom-alg" }, false, ["verify"]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
context("sign", () => {
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const correctKey = core.CryptoKey.create(
|
2022-05-23 13:12:03 +00:00
|
|
|
{ name: "custom-alg" },
|
|
|
|
"secret",
|
|
|
|
false,
|
|
|
|
["sign"],
|
|
|
|
);
|
|
|
|
|
|
|
|
it("correct data", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.sign(
|
|
|
|
{ name: "custom-alg" },
|
|
|
|
correctKey,
|
|
|
|
new ArrayBuffer(0),
|
|
|
|
),
|
|
|
|
core.UnsupportedOperationError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong name of algorithm", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.sign(
|
|
|
|
{ name: "wrong" },
|
|
|
|
correctKey,
|
|
|
|
new ArrayBuffer(0),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong key type", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.sign(
|
|
|
|
{ name: "custom-alg" },
|
2022-05-23 21:09:47 +00:00
|
|
|
{} as core.CryptoKey,
|
2022-05-23 13:12:03 +00:00
|
|
|
new ArrayBuffer(0),
|
|
|
|
),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong key algorithm", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.sign(
|
|
|
|
{ name: "custom-alg" },
|
2022-05-23 21:09:47 +00:00
|
|
|
core.CryptoKey.create(
|
2022-05-23 13:12:03 +00:00
|
|
|
{ name: "wrong" },
|
|
|
|
"secret",
|
|
|
|
true,
|
|
|
|
["sign", "decrypt"],
|
|
|
|
),
|
|
|
|
new ArrayBuffer(0),
|
|
|
|
),
|
|
|
|
core.AlgorithmError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("wrong key usage", async () => {
|
|
|
|
await assert.rejects(
|
|
|
|
crypto.sign(
|
|
|
|
{ name: "custom-alg" },
|
2022-05-23 21:09:47 +00:00
|
|
|
core.CryptoKey.create(
|
2022-05-23 13:12:03 +00:00
|
|
|
{ name: "custom-alg" },
|
|
|
|
"secret",
|
|
|
|
true,
|
|
|
|
["verify"],
|
|
|
|
),
|
|
|
|
new ArrayBuffer(0),
|
|
|
|
),
|
|
|
|
core.CryptoError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
context("checkDeriveBits", () => {
|
|
|
|
|
|
|
|
it("error if length is not multiple 8", () => {
|
|
|
|
const algorithm: types.Algorithm = { name: "custom-alg" };
|
2022-05-23 21:09:47 +00:00
|
|
|
const key = core.CryptoKey.create(algorithm, "secret", false, ["deriveBits"]);
|
2022-05-23 13:12:03 +00:00
|
|
|
assert.throws(() => {
|
|
|
|
crypto.checkDeriveBits(algorithm, key, 7);
|
|
|
|
}, core.OperationError);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
context("checkKeyFormat", () => {
|
|
|
|
|
|
|
|
it("error if wrong value", () => {
|
|
|
|
assert.throws(() => {
|
|
|
|
crypto.checkKeyFormat("wrong");
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|