Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for serialize/deserializePrivateKey. #443

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion packages/dhkem-secp256k1/test/dhkemSecp256k1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {
AeadId,
Aes128Gcm,
CipherSuite,
DeserializeError,
DhkemP256HkdfSha256,
ExportOnly,
HkdfSha256,
KdfId,
KemId,
SerializeError,
} from "@hpke/core";

import { DhkemSecp256k1HkdfSha256 } from "../mod.ts";

describe("DhkemSecp256k1Hkdf256", () => {
describe("constructor", () => {
describe("with valid parameters", () => {
it("should have a correct KEM object", () => {
// assert
Expand Down Expand Up @@ -93,6 +96,72 @@ describe("serialize/deserializePublicKey", () => {
// assertEquals(pubKey.usages[0], "deriveBits");
});
});
describe("with invalid parameters", () => {
it("should throw SerializeError on DhkemSecp256k1HkdfSha256.serializePublicKey with a public key for DhkemP256HkdfSha256", async () => {
// assert
const ctx = new DhkemP256HkdfSha256();
const kp = await ctx.generateKeyPair();
const kemContext = new DhkemSecp256k1HkdfSha256();
await assertRejects(
() => kemContext.serializePublicKey(kp.publicKey),
SerializeError,
);
});

it("should throw DeserializeError on DhkemP256HkdfSha256.deserializePublicKey with invalid length key", async () => {
// assert
const kemContext = new DhkemSecp256k1HkdfSha256();
const cryptoApi = await loadCrypto();
const rawKey = new Uint8Array(33 - 1);
cryptoApi.getRandomValues(rawKey);
await assertRejects(
() => kemContext.deserializePublicKey(rawKey.buffer),
DeserializeError,
);
});
});
});

describe("serialize/deserializePrivateKey", () => {
describe("with valid parameters", () => {
it("should return a proper instance with DhkemSecp256k1HkdfSha256", async () => {
// assert
const kemContext = new DhkemSecp256k1HkdfSha256();
const kp = await kemContext.generateKeyPair();
const bPrivKey = await kemContext.serializePrivateKey(kp.privateKey);
const privKey = await kemContext.deserializePrivateKey(bPrivKey);
assertEquals(privKey.type, "private");
assertEquals(privKey.extractable, true);
assertEquals(privKey.algorithm.name, "ECDH");
// assertEquals(pubKey.algorithm.namedCurve, "secp256k1");
assertEquals(privKey.usages.length, 1);
assertEquals(privKey.usages[0], "deriveBits");
});
});

describe("with invalid parameters", () => {
it("should throw SerializeError on DhkemSecp256k1HkdfSha256.serializePrivateKey with a private key for DhkemP256HkdfSha256", async () => {
// assert
const ctx = new DhkemP256HkdfSha256();
const kp = await ctx.generateKeyPair();
const kemContext = new DhkemSecp256k1HkdfSha256();
await assertRejects(
() => kemContext.serializePrivateKey(kp.privateKey),
SerializeError,
);
});
it("should throw DeserializeError on DhkemSecp256k1HkdfSha256.deserializePrivateKey with invalid length key", async () => {
// assert
const kemContext = new DhkemSecp256k1HkdfSha256();
const cryptoApi = await loadCrypto();
const rawKey = new Uint8Array(33);
cryptoApi.getRandomValues(rawKey);
await assertRejects(
() => kemContext.deserializePrivateKey(rawKey),
DeserializeError,
);
});
});
});

describe("importKey", () => {
Expand Down
Loading