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

Fix encSize and publicKeySize of DhkemSecp256k1HkdfSha256. #208

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion x/dhkem-secp256k1/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align="center">@hpke/dhkem-secp256k1</h1>

<div align="center">
A TypeScript <a href="https://datatracker.ietf.org/doc/html/rfc9180">Hybrid Public Key Encryption (HPKE)</a> module extension for DH-KEM with secp256k1 curve, which is implemented by using [@noble/curves/secp256k1](https://github.com/paulmillr/noble-curves). Note that the extension is EXPERIMENTAL and NOT STANDARDIZED.</div>
A TypeScript <a href="https://datatracker.ietf.org/doc/html/rfc9180">Hybrid Public Key Encryption (HPKE)</a> module extension for DH-KEM with secp256k1 curve, which is implemented by using <a href="https://github.com/paulmillr/noble-curves">@noble/curves/secp256k1</a>. Note that the extension is EXPERIMENTAL and NOT STANDARDIZED.</div>
<p></p>

<div align="center">
Expand Down
23 changes: 20 additions & 3 deletions x/dhkem-secp256k1/src/dhkem-secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,28 @@ class Secp256k1 extends Algorithm implements KemPrimitives {
}
}

/**
* The class of the DH-KEM with secp256k1 curve.
*
* The public keys are assumed to be compressed.
* Note that it is experimental and not standardized.
*
* The instance of this class can be specified to the CipherSuiteParams as follows:
*
* @example
* import { KdfId, AeadId, CipherSuite } from "http://deno.land/x/hpke/mod.ts";
* import { DhkemSecp256k1HkdfSha256} from "https://deno.land/x/hpke/x/dhkem-secp256k1/mod.ts";
* const suite = new CipherSuite({
* kem: new DhkemSecp256k1HkdfSha256(),
* kdf: KdfId.HkdfSha256,
* aead: AeadId.Aes128Gcm,
* });
*/
export class DhkemSecp256k1HkdfSha256 extends Dhkem implements KemInterface {
public readonly id: KemId = KemId.DhkemSecp256K1HkdfSha256;
public readonly id: KemId = KemId.DhkemSecp256k1HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 65;
public readonly publicKeySize: number = 65;
public readonly encSize: number = 33;
public readonly publicKeySize: number = 33;
public readonly privateKeySize: number = 32;

constructor() {
Expand Down
15 changes: 9 additions & 6 deletions x/dhkem-secp256k1/test/dhkem-secp256k1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function loadSubtleCrypto(): Promise<SubtleCrypto> {
}
}

describe("DhkemP256k1Hkdf256", () => {
describe("DhkemSecp256k1Hkdf256", () => {
describe("with valid parameters", () => {
it("should have a correct KEM object", async () => {
const api = await loadSubtleCrypto();
Expand All @@ -70,8 +70,8 @@ describe("DhkemP256k1Hkdf256", () => {
assertEquals(typeof dhkemSecp256k1, "object");
assertEquals(dhkemSecp256k1.id, KemId.DhkemSecp256K1HkdfSha256);
assertEquals(dhkemSecp256k1.secretSize, 32);
assertEquals(dhkemSecp256k1.encSize, 65);
assertEquals(dhkemSecp256k1.publicKeySize, 65);
assertEquals(dhkemSecp256k1.encSize, 33);
assertEquals(dhkemSecp256k1.publicKeySize, 33);
assertEquals(dhkemSecp256k1.privateKeySize, 32);
});
});
Expand Down Expand Up @@ -230,8 +230,8 @@ describe("CipherSuite", () => {
});
const kem = await suite.kemContext();
assertEquals(kem.secretSize, 32);
assertEquals(kem.encSize, 65);
assertEquals(kem.publicKeySize, 65);
assertEquals(kem.encSize, 33);
assertEquals(kem.publicKeySize, 33);
assertEquals(kem.privateKeySize, 32);

// assert
Expand All @@ -247,8 +247,9 @@ describe("CipherSuite", () => {
describe("A README example of Base mode (DhkemSecp256k1HkdfSha256/KdfId.HkdfSha256)", () => {
it("should work normally", async () => {
// setup
const kemInstance = new DhkemSecp256k1HkdfSha256();
const suite = new CipherSuite({
kem: new DhkemSecp256k1HkdfSha256(),
kem: kemInstance,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
});
Expand All @@ -263,6 +264,8 @@ describe("CipherSuite", () => {
recipientKey: rkp,
enc: sender.enc,
});
assertEquals(sender.enc.byteLength, kemInstance.encSize);
assertEquals(sender.enc.byteLength, kemInstance.publicKeySize);

// encrypt
const ct = await sender.seal(
Expand Down
Loading