Skip to content

Commit

Permalink
Move encryptionContext test to @hpke/core.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajiaji committed Oct 6, 2024
1 parent 08aad96 commit 76d2698
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { describe, it } from "@std/testing/bdd";

import { i2Osp, loadSubtleCrypto } from "@hpke/common";
import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519";

import {
AeadId,
Aes128Gcm,
CipherSuite,
DecapError,
DeserializeError,
DhkemP256HkdfSha256,
DhkemP384HkdfSha384,
EncapError,
ExportError,
ExportOnly,
HkdfSha256,
HkdfSha384,
InvalidParamError,
KdfId,
KemId,
NotSupportedError,
OpenError,
} from "@hpke/core";
import { HkdfSha256 } from "@hpke/dhkem-x25519";

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

import { EncryptionContextImpl } from "../../core/src/encryptionContext.ts";
} from "../mod.ts";
import { EncryptionContextImpl } from "../src/encryptionContext.ts";

// deno-fmt-ignore
const SUITE_ID_HEADER_HPKE = new Uint8Array([
Expand Down Expand Up @@ -106,12 +109,12 @@ describe("open", () => {
describe("by sender", () => {
it("should throw OpenError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();

const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
Expand All @@ -129,13 +132,13 @@ describe("open", () => {
describe("by another recipient (AES-128-GCM)", () => {
it("should throw OpenError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp1 = await suite.generateKeyPair();
const rkp2 = await suite.generateKeyPair();
const rkp1 = await suite.kem.generateKeyPair();
const rkp2 = await suite.kem.generateKeyPair();

const sender1 = await suite.createSenderContext({
recipientPublicKey: rkp1.publicKey,
Expand Down Expand Up @@ -171,13 +174,13 @@ describe("open", () => {
describe("by another recipient (ChaCha20/Poly1305)", () => {
it("should throw OpenError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Chacha20Poly1305,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp1 = await suite.generateKeyPair();
const rkp2 = await suite.generateKeyPair();
const rkp1 = await suite.kem.generateKeyPair();
const rkp2 = await suite.kem.generateKeyPair();

const sender1 = await suite.createSenderContext({
recipientPublicKey: rkp1.publicKey,
Expand Down Expand Up @@ -215,12 +218,12 @@ describe("export", () => {
describe("with invalid argument", () => {
it("should throw ExportError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.ExportOnly,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();

const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
Expand All @@ -239,12 +242,12 @@ describe("export", () => {
describe("with invalid argument", () => {
it("should throw ExportError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.ExportOnly,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();

const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
Expand All @@ -263,12 +266,12 @@ describe("export", () => {
describe("with too long exporter_context", () => {
it("should throw InvalidParamError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.ExportOnly,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();

const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
Expand All @@ -288,18 +291,18 @@ describe("createSenderContext", () => {
describe("with invalid recipientPublicKey", () => {
it("should throw ExportError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemP256HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const suiteX = new CipherSuite({
kem: KemId.DhkemP384HkdfSha384,
kdf: KdfId.HkdfSha384,
aead: AeadId.Aes128Gcm,
kem: new DhkemP384HkdfSha384(),
kdf: new HkdfSha384(),
aead: new Aes128Gcm(),
});

const rkpX = await suiteX.generateKeyPair();
const rkpX = await suiteX.kem.generateKeyPair();

// assert
await assertRejects(
Expand All @@ -317,19 +320,19 @@ describe("createRecipientContext", () => {
describe("with invalid enc", () => {
it("should throw DeserializeError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemP256HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const suiteX = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha384,
aead: AeadId.Aes128Gcm,
kem: new DhkemX25519HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkpX = await suiteX.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();
const rkpX = await suiteX.kem.generateKeyPair();

const senderX = await suiteX.createSenderContext({
recipientPublicKey: rkpX.publicKey,
Expand All @@ -350,19 +353,19 @@ describe("createRecipientContext", () => {
describe("with invalid enc (X25519)", () => {
it("should throw DeserializeError", async () => {
const suite = new CipherSuite({
kem: KemId.DhkemX25519HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
kem: new DhkemX25519HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const suiteX = new CipherSuite({
kem: KemId.DhkemP256HkdfSha256,
kdf: KdfId.HkdfSha384,
aead: AeadId.Aes128Gcm,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkpX = await suiteX.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();
const rkpX = await suiteX.kem.generateKeyPair();

const senderX = await suiteX.createSenderContext({
recipientPublicKey: rkpX.publicKey,
Expand All @@ -384,19 +387,19 @@ describe("createRecipientContext", () => {
it("should throw DecapError", async () => {
// setup
const suite = new CipherSuite({
kem: KemId.DhkemP256HkdfSha256,
kdf: KdfId.HkdfSha256,
aead: AeadId.Aes128Gcm,
kem: new DhkemP256HkdfSha256(),
kdf: new HkdfSha256(),
aead: new Aes128Gcm(),
});

const suiteX = new CipherSuite({
kem: KemId.DhkemP384HkdfSha384,
kdf: KdfId.HkdfSha384,
aead: AeadId.Aes128Gcm,
kem: new DhkemP384HkdfSha384(),
kdf: new HkdfSha384(),
aead: new Aes128Gcm(),
});

const rkp = await suite.generateKeyPair();
const rkpX = await suiteX.generateKeyPair();
const rkp = await suite.kem.generateKeyPair();
const rkpX = await suiteX.kem.generateKeyPair();

const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/hpke-js/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"test:bun": "cd test/runtimes/bun && bun install && bun test",
"cov": "deno coverage ./coverage --lcov --exclude='test'",
"dnt": "deno run --import-map=../../npm/import_map.json -A dnt.ts",
"minify": "esbuild ../../npm/packages/hpke-js/esm/hpke-js/mod.js --bundle --format=esm --minify"
"minify": "esbuild ../../npm/packages/hpke-js/esm/mod.js --bundle --format=esm --minify"
}
}
10 changes: 5 additions & 5 deletions packages/hpke-js/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ await build({
},
homepage: "https://github.com/dajiaji/hpke-js#readme",
license: "MIT",
module: "./esm/hpke-js/mod.js",
main: "./script/hpke-js/mod.js",
types: "./esm/hpke-js/mod.d.ts",
module: "./esm/mod.js",
main: "./script/mod.js",
types: "./esm/mod.d.ts",
sideEffects: false,
exports: {
".": {
"import": "./esm/hpke-js/mod.js",
"require": "./script/hpke-js/mod.js",
"import": "./esm/mod.js",
"require": "./script/mod.js",
},
"./package.json": "./package.json",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/hpke-js/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const isNode = () => (globalThis as any).process?.versions?.node != null;
export function getPath(name: string): string {
const currentPath = dirname(fromFileUrl(import.meta.url));
if (isNode()) {
return join(currentPath, "../../../", name);
return join(currentPath, "../../", name);
}
return join(currentPath, name);
}

0 comments on commit 76d2698

Please sign in to comment.