Skip to content

Commit

Permalink
Merge pull request #211 from dajiaji/separate-prim-from-dhkem-class
Browse files Browse the repository at this point in the history
Separate dhkemPrimitives from dhkem classes.
  • Loading branch information
dajiaji authored Jul 28, 2023
2 parents b6082d8 + 55bcc90 commit b0c9aaf
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 109 deletions.
12 changes: 5 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ export * from "./src/errors.ts";
export { Aead, AeadId, Kdf, KdfId, Kem, KemId } from "./src/identifiers.ts";
export { CipherSuite } from "./src/cipherSuite.ts";

export {
DhkemP256HkdfSha256,
DhkemP384HkdfSha384,
DhkemP521HkdfSha512,
} from "./src/kems/dhkems/ec.ts";
export { DhkemX25519HkdfSha256 } from "./src/kems/dhkems/x25519.ts";
export { DhkemX448HkdfSha512 } from "./src/kems/dhkems/x448.ts";
export { DhkemP256HkdfSha256 } from "./src/kems/dhkemP256.ts";
export { DhkemP384HkdfSha384 } from "./src/kems/dhkemP384.ts";
export { DhkemP521HkdfSha512 } from "./src/kems/dhkemP521.ts";
export { DhkemX25519HkdfSha256 } from "./src/kems/dhkemX25519.ts";
export { DhkemX448HkdfSha512 } from "./src/kems/dhkemX448.ts";

export { HkdfSha256, HkdfSha384, HkdfSha512 } from "./src/kdfs/hkdf.ts";
export { Aes128Gcm, Aes256Gcm } from "./src/aeads/aesGcm.ts";
Expand Down
12 changes: 5 additions & 7 deletions src/cipherSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import { HkdfSha256, HkdfSha384, HkdfSha512 } from "./kdfs/hkdf.ts";
import { RecipientContext } from "./recipientContext.ts";
import { SenderContext } from "./senderContext.ts";
import { loadSubtleCrypto } from "./webCrypto.ts";
import {
DhkemP256HkdfSha256,
DhkemP384HkdfSha384,
DhkemP521HkdfSha512,
} from "./kems/dhkems/ec.ts";
import { DhkemX25519HkdfSha256 } from "./kems/dhkems/x25519.ts";
import { DhkemX448HkdfSha512 } from "./kems/dhkems/x448.ts";
import { DhkemP256HkdfSha256 } from "./kems/dhkemP256.ts";
import { DhkemP384HkdfSha384 } from "./kems/dhkemP384.ts";
import { DhkemP521HkdfSha512 } from "./kems/dhkemP521.ts";
import { DhkemX25519HkdfSha256 } from "./kems/dhkemX25519.ts";
import { DhkemX448HkdfSha512 } from "./kems/dhkemX448.ts";
import { i2Osp } from "./utils/misc.ts";

import * as consts from "./consts.ts";
Expand Down
18 changes: 18 additions & 0 deletions src/kems/dhkemP256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KemId } from "../identifiers.ts";
import { HkdfSha256 } from "../kdfs/hkdf.ts";
import { Dhkem } from "./dhkem.ts";
import { Ec } from "./dhkemPrimitives/ec.ts";

export class DhkemP256HkdfSha256 extends Dhkem {
public readonly id: KemId = KemId.DhkemP256HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 65;
public readonly publicKeySize: number = 65;
public readonly privateKeySize: number = 32;

constructor() {
const kdf = new HkdfSha256();
const prim = new Ec(KemId.DhkemP256HkdfSha256, kdf);
super(prim, kdf);
}
}
18 changes: 18 additions & 0 deletions src/kems/dhkemP384.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KemId } from "../identifiers.ts";
import { HkdfSha384 } from "../kdfs/hkdf.ts";
import { Dhkem } from "./dhkem.ts";
import { Ec } from "./dhkemPrimitives/ec.ts";

export class DhkemP384HkdfSha384 extends Dhkem {
public readonly id: KemId = KemId.DhkemP384HkdfSha384;
public readonly secretSize: number = 48;
public readonly encSize: number = 97;
public readonly publicKeySize: number = 97;
public readonly privateKeySize: number = 48;

constructor() {
const kdf = new HkdfSha384();
const prim = new Ec(KemId.DhkemP384HkdfSha384, kdf);
super(prim, kdf);
}
}
18 changes: 18 additions & 0 deletions src/kems/dhkemP521.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KemId } from "../identifiers.ts";
import { HkdfSha512 } from "../kdfs/hkdf.ts";
import { Dhkem } from "./dhkem.ts";
import { Ec } from "./dhkemPrimitives/ec.ts";

export class DhkemP521HkdfSha512 extends Dhkem {
public readonly id: KemId = KemId.DhkemP521HkdfSha512;
public readonly secretSize: number = 64;
public readonly encSize: number = 133;
public readonly publicKeySize: number = 133;
public readonly privateKeySize: number = 64;

constructor() {
const kdf = new HkdfSha512();
const prim = new Ec(KemId.DhkemP521HkdfSha512, kdf);
super(prim, kdf);
}
}
46 changes: 1 addition & 45 deletions src/kems/dhkems/ec.ts → src/kems/dhkemPrimitives/ec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { KdfInterface } from "../../interfaces/kdfInterface.ts";

import { Algorithm } from "../../algorithm.ts";
import { KemId } from "../../identifiers.ts";
import { HkdfSha256, HkdfSha384, HkdfSha512 } from "../../kdfs/hkdf.ts";
import { Dhkem } from "../dhkem.ts";

import { Bignum } from "../../utils/bignum.ts";
import { i2Osp } from "../../utils/misc.ts";
Expand Down Expand Up @@ -119,7 +117,7 @@ const PKCS8_ALG_ID_P_521 = new Uint8Array([
66,
]);

class Ec extends Algorithm implements KemPrimitives {
export class Ec extends Algorithm implements KemPrimitives {
private _hkdf: KdfInterface;
private _alg: EcKeyGenParams;
private _nPk: number;
Expand Down Expand Up @@ -355,45 +353,3 @@ class Ec extends Algorithm implements KemPrimitives {
return bits;
}
}

export class DhkemP256HkdfSha256 extends Dhkem {
public readonly id: KemId = KemId.DhkemP256HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 65;
public readonly publicKeySize: number = 65;
public readonly privateKeySize: number = 32;

constructor() {
const kdf = new HkdfSha256();
const prim = new Ec(KemId.DhkemP256HkdfSha256, kdf);
super(prim, kdf);
}
}

export class DhkemP384HkdfSha384 extends Dhkem {
public readonly id: KemId = KemId.DhkemP384HkdfSha384;
public readonly secretSize: number = 48;
public readonly encSize: number = 97;
public readonly publicKeySize: number = 97;
public readonly privateKeySize: number = 48;

constructor() {
const kdf = new HkdfSha384();
const prim = new Ec(KemId.DhkemP384HkdfSha384, kdf);
super(prim, kdf);
}
}

export class DhkemP521HkdfSha512 extends Dhkem {
public readonly id: KemId = KemId.DhkemP521HkdfSha512;
public readonly secretSize: number = 64;
public readonly encSize: number = 133;
public readonly publicKeySize: number = 133;
public readonly privateKeySize: number = 64;

constructor() {
const kdf = new HkdfSha512();
const prim = new Ec(KemId.DhkemP521HkdfSha512, kdf);
super(prim, kdf);
}
}
19 changes: 1 addition & 18 deletions src/kems/dhkems/x25519.ts → src/kems/dhkemPrimitives/x25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import type { KemPrimitives } from "../../interfaces/kemPrimitives.ts";
import type { KdfInterface } from "../../interfaces/kdfInterface.ts";

import { Algorithm } from "../../algorithm.ts";
import { KemId } from "../../identifiers.ts";
import { XCryptoKey } from "../../xCryptoKey.ts";
import { HkdfSha256 } from "../../kdfs/hkdf.ts";
import { Dhkem } from "../dhkem.ts";

import * as consts from "../../consts.ts";
import { base64UrlToBytes } from "../../utils/misc.ts";

const ALG_NAME = "X25519";

class X25519 extends Algorithm implements KemPrimitives {
export class X25519 extends Algorithm implements KemPrimitives {
private _hkdf: KdfInterface;
private _nPk: number;
private _nSk: number;
Expand Down Expand Up @@ -174,17 +171,3 @@ class X25519 extends Algorithm implements KemPrimitives {
});
}
}

export class DhkemX25519HkdfSha256 extends Dhkem {
public readonly id: KemId = KemId.DhkemX25519HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 32;
public readonly publicKeySize: number = 32;
public readonly privateKeySize: number = 32;

constructor() {
const kdf = new HkdfSha256();
const prim = new X25519(kdf);
super(prim, kdf);
}
}
19 changes: 1 addition & 18 deletions src/kems/dhkems/x448.ts → src/kems/dhkemPrimitives/x448.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import type { KemPrimitives } from "../../interfaces/kemPrimitives.ts";
import type { KdfInterface } from "../../interfaces/kdfInterface.ts";

import { Algorithm } from "../../algorithm.ts";
import { KemId } from "../../identifiers.ts";
import { XCryptoKey } from "../../xCryptoKey.ts";
import { HkdfSha512 } from "../../kdfs/hkdf.ts";
import { Dhkem } from "../dhkem.ts";

import * as consts from "../../consts.ts";
import { base64UrlToBytes } from "../../utils/misc.ts";

const ALG_NAME = "X448";

class X448 extends Algorithm implements KemPrimitives {
export class X448 extends Algorithm implements KemPrimitives {
private _hkdf: KdfInterface;
private _nPk: number;
private _nSk: number;
Expand Down Expand Up @@ -174,17 +171,3 @@ class X448 extends Algorithm implements KemPrimitives {
});
}
}

export class DhkemX448HkdfSha512 extends Dhkem {
public readonly id: KemId = KemId.DhkemX448HkdfSha512;
public readonly secretSize: number = 64;
public readonly encSize: number = 56;
public readonly publicKeySize: number = 56;
public readonly privateKeySize: number = 56;

constructor() {
const kdf = new HkdfSha512();
const prim = new X448(kdf);
super(prim, kdf);
}
}
18 changes: 18 additions & 0 deletions src/kems/dhkemX25519.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KemId } from "../identifiers.ts";
import { HkdfSha256 } from "../kdfs/hkdf.ts";
import { Dhkem } from "./dhkem.ts";
import { X25519 } from "./dhkemPrimitives/x25519.ts";

export class DhkemX25519HkdfSha256 extends Dhkem {
public readonly id: KemId = KemId.DhkemX25519HkdfSha256;
public readonly secretSize: number = 32;
public readonly encSize: number = 32;
public readonly publicKeySize: number = 32;
public readonly privateKeySize: number = 32;

constructor() {
const kdf = new HkdfSha256();
const prim = new X25519(kdf);
super(prim, kdf);
}
}
18 changes: 18 additions & 0 deletions src/kems/dhkemX448.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KemId } from "../identifiers.ts";
import { HkdfSha512 } from "../kdfs/hkdf.ts";
import { Dhkem } from "./dhkem.ts";
import { X448 } from "./dhkemPrimitives/x448.ts";

export class DhkemX448HkdfSha512 extends Dhkem {
public readonly id: KemId = KemId.DhkemX448HkdfSha512;
public readonly secretSize: number = 64;
public readonly encSize: number = 56;
public readonly publicKeySize: number = 56;
public readonly privateKeySize: number = 56;

constructor() {
const kdf = new HkdfSha512();
const prim = new X448(kdf);
super(prim, kdf);
}
}
12 changes: 5 additions & 7 deletions test/cipherSuite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { CipherSuite } from "../src/cipherSuite.ts";
import { isDeno } from "../src/utils/misc.ts";
import { loadCrypto } from "../src/webCrypto.ts";
import { concat } from "../src/utils/misc.ts";
import {
DhkemP256HkdfSha256,
DhkemP384HkdfSha384,
DhkemP521HkdfSha512,
} from "../src/kems/dhkems/ec.ts";
import { DhkemX25519HkdfSha256 } from "../src/kems/dhkems/x25519.ts";
import { DhkemX448HkdfSha512 } from "../src/kems/dhkems/x448.ts";
import { DhkemP256HkdfSha256 } from "../src/kems/dhkemP256.ts";
import { DhkemP384HkdfSha384 } from "../src/kems/dhkemP384.ts";
import { DhkemP521HkdfSha512 } from "../src/kems/dhkemP521.ts";
import { DhkemX25519HkdfSha256 } from "../src/kems/dhkemX25519.ts";
import { DhkemX448HkdfSha512 } from "../src/kems/dhkemX448.ts";
import { HkdfSha256, HkdfSha384, HkdfSha512 } from "../src/kdfs/hkdf.ts";
import { Aes128Gcm } from "../src/aeads/aesGcm.ts";

Expand Down
12 changes: 5 additions & 7 deletions test/kemContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { assertEquals, assertRejects } from "testing/asserts.ts";
import { describe, it } from "testing/bdd.ts";

import { isDeno } from "../src/utils/misc.ts";
import {
DhkemP256HkdfSha256,
DhkemP384HkdfSha384,
DhkemP521HkdfSha512,
} from "../src/kems/dhkems/ec.ts";
import { DhkemX25519HkdfSha256 } from "../src/kems/dhkems/x25519.ts";
import { DhkemX448HkdfSha512 } from "../src/kems/dhkems/x448.ts";
import { DhkemP256HkdfSha256 } from "../src/kems/dhkemP256.ts";
import { DhkemP384HkdfSha384 } from "../src/kems/dhkemP384.ts";
import { DhkemP521HkdfSha512 } from "../src/kems/dhkemP521.ts";
import { DhkemX25519HkdfSha256 } from "../src/kems/dhkemX25519.ts";
import { DhkemX448HkdfSha512 } from "../src/kems/dhkemX448.ts";
import { KemId } from "../src/identifiers.ts";
import { loadCrypto, loadSubtleCrypto } from "../src/webCrypto.ts";

Expand Down

0 comments on commit b0c9aaf

Please sign in to comment.