hpke 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,16 +48,11 @@ function getStringFromWasm0(ptr, len) {
48
48
  return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
49
49
  }
50
50
 
51
- let cachegetInt32Memory0 = null;
52
- function getInt32Memory0() {
53
- if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
54
- cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
51
+ function _assertClass(instance, klass) {
52
+ if (!(instance instanceof klass)) {
53
+ throw new Error(`expected instance of ${klass.name}`);
55
54
  }
56
- return cachegetInt32Memory0;
57
- }
58
-
59
- function getArrayU8FromWasm0(ptr, len) {
60
- return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
55
+ return instance.ptr;
61
56
  }
62
57
 
63
58
  let WASM_VECTOR_LEN = 0;
@@ -69,57 +64,67 @@ function passArray8ToWasm0(arg, malloc) {
69
64
  return ptr;
70
65
  }
71
66
 
72
- function _assertClass(instance, klass) {
73
- if (!(instance instanceof klass)) {
74
- throw new Error(`expected instance of ${klass.name}`);
67
+ let cachegetInt32Memory0 = null;
68
+ function getInt32Memory0() {
69
+ if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
70
+ cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
75
71
  }
76
- return instance.ptr;
72
+ return cachegetInt32Memory0;
73
+ }
74
+
75
+ function getArrayU8FromWasm0(ptr, len) {
76
+ return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
77
77
  }
78
78
  /**
79
79
  *
80
- *`base_mode_seal` provides an interface to [`hpke::single_shot_seal`]
81
- *that does not require compile time selection of an
82
- *algorithm. Instead, the selected algorithm is provided through the
83
- *[`Config`] passed as the first argument.
80
+ *`base_mode_open` provides an interface to [`hpke::single_shot_open`]
81
+ *that does not require compile time selection of an algorithm. Instead,
82
+ *the selected algorithm is provided through the [`Config`] passed as
83
+ *the first argument.
84
84
  *
85
- *Requires the `base-mode-seal` crate feature to be enabled.
85
+ *Requires the `base-mode-open` crate feature to be enabled.
86
86
  *
87
87
  *# Errors
88
88
  *
89
89
  *This will return an `Result::Err` variant if:
90
90
  *
91
- ** we are unable to deserialize the recipient public key
92
- ** there is an error in key encapsultion
93
- ** there is an error in encryption
94
- *
91
+ ** we are unable to deserialize the private key or encapsulated key
92
+ ** there is an error in key decapsulation
93
+ ** there is an error in decryption
95
94
  *
96
95
  * @param {Config} config
97
- * @param {Uint8Array} recipient_public_key
96
+ * @param {Uint8Array} private_key
97
+ * @param {Uint8Array} encapped_key
98
98
  * @param {Uint8Array} info
99
- * @param {Uint8Array} plaintext
99
+ * @param {Uint8Array} ciphertext
100
100
  * @param {Uint8Array} aad
101
- * @returns {EncappedKeyAndCiphertext}
101
+ * @returns {Uint8Array}
102
102
  */
103
- module.exports.base_mode_seal = function(config, recipient_public_key, info, plaintext, aad) {
103
+ module.exports.base_mode_open = function(config, private_key, encapped_key, info, ciphertext, aad) {
104
104
  try {
105
105
  const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
106
106
  _assertClass(config, Config);
107
- const ptr0 = passArray8ToWasm0(recipient_public_key, wasm.__wbindgen_malloc);
107
+ const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc);
108
108
  const len0 = WASM_VECTOR_LEN;
109
- const ptr1 = passArray8ToWasm0(info, wasm.__wbindgen_malloc);
109
+ const ptr1 = passArray8ToWasm0(encapped_key, wasm.__wbindgen_malloc);
110
110
  const len1 = WASM_VECTOR_LEN;
111
- const ptr2 = passArray8ToWasm0(plaintext, wasm.__wbindgen_malloc);
111
+ const ptr2 = passArray8ToWasm0(info, wasm.__wbindgen_malloc);
112
112
  const len2 = WASM_VECTOR_LEN;
113
- const ptr3 = passArray8ToWasm0(aad, wasm.__wbindgen_malloc);
113
+ const ptr3 = passArray8ToWasm0(ciphertext, wasm.__wbindgen_malloc);
114
114
  const len3 = WASM_VECTOR_LEN;
115
- wasm.base_mode_seal(retptr, config.ptr, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3);
115
+ const ptr4 = passArray8ToWasm0(aad, wasm.__wbindgen_malloc);
116
+ const len4 = WASM_VECTOR_LEN;
117
+ wasm.base_mode_open(retptr, config.ptr, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4);
116
118
  var r0 = getInt32Memory0()[retptr / 4 + 0];
117
119
  var r1 = getInt32Memory0()[retptr / 4 + 1];
118
120
  var r2 = getInt32Memory0()[retptr / 4 + 2];
119
- if (r2) {
120
- throw takeObject(r1);
121
+ var r3 = getInt32Memory0()[retptr / 4 + 3];
122
+ if (r3) {
123
+ throw takeObject(r2);
121
124
  }
122
- return EncappedKeyAndCiphertext.__wrap(r0);
125
+ var v5 = getArrayU8FromWasm0(r0, r1).slice();
126
+ wasm.__wbindgen_free(r0, r1 * 1);
127
+ return v5;
123
128
  } finally {
124
129
  wasm.__wbindgen_add_to_stack_pointer(16);
125
130
  }
@@ -127,54 +132,49 @@ module.exports.base_mode_seal = function(config, recipient_public_key, info, pla
127
132
 
128
133
  /**
129
134
  *
130
- *`base_mode_open` provides an interface to [`hpke::single_shot_open`]
131
- *that does not require compile time selection of an algorithm. Instead,
132
- *the selected algorithm is provided through the [`Config`] passed as
133
- *the first argument.
135
+ *`base_mode_seal` provides an interface to [`hpke::single_shot_seal`]
136
+ *that does not require compile time selection of an
137
+ *algorithm. Instead, the selected algorithm is provided through the
138
+ *[`Config`] passed as the first argument.
134
139
  *
135
- *Requires the `base-mode-open` crate feature to be enabled.
140
+ *Requires the `base-mode-seal` crate feature to be enabled.
136
141
  *
137
142
  *# Errors
138
143
  *
139
144
  *This will return an `Result::Err` variant if:
140
145
  *
141
- ** we are unable to deserialize the private key or encapsulated key
142
- ** there is an error in key decapsulation
143
- ** there is an error in decryption
146
+ ** we are unable to deserialize the recipient public key
147
+ ** there is an error in key encapsultion
148
+ ** there is an error in encryption
149
+ *
144
150
  *
145
151
  * @param {Config} config
146
- * @param {Uint8Array} private_key
147
- * @param {Uint8Array} encapped_key
152
+ * @param {Uint8Array} recipient_public_key
148
153
  * @param {Uint8Array} info
149
- * @param {Uint8Array} ciphertext
154
+ * @param {Uint8Array} plaintext
150
155
  * @param {Uint8Array} aad
151
- * @returns {Uint8Array}
156
+ * @returns {EncappedKeyAndCiphertext}
152
157
  */
153
- module.exports.base_mode_open = function(config, private_key, encapped_key, info, ciphertext, aad) {
158
+ module.exports.base_mode_seal = function(config, recipient_public_key, info, plaintext, aad) {
154
159
  try {
155
160
  const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
156
161
  _assertClass(config, Config);
157
- const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc);
162
+ const ptr0 = passArray8ToWasm0(recipient_public_key, wasm.__wbindgen_malloc);
158
163
  const len0 = WASM_VECTOR_LEN;
159
- const ptr1 = passArray8ToWasm0(encapped_key, wasm.__wbindgen_malloc);
164
+ const ptr1 = passArray8ToWasm0(info, wasm.__wbindgen_malloc);
160
165
  const len1 = WASM_VECTOR_LEN;
161
- const ptr2 = passArray8ToWasm0(info, wasm.__wbindgen_malloc);
166
+ const ptr2 = passArray8ToWasm0(plaintext, wasm.__wbindgen_malloc);
162
167
  const len2 = WASM_VECTOR_LEN;
163
- const ptr3 = passArray8ToWasm0(ciphertext, wasm.__wbindgen_malloc);
168
+ const ptr3 = passArray8ToWasm0(aad, wasm.__wbindgen_malloc);
164
169
  const len3 = WASM_VECTOR_LEN;
165
- const ptr4 = passArray8ToWasm0(aad, wasm.__wbindgen_malloc);
166
- const len4 = WASM_VECTOR_LEN;
167
- wasm.base_mode_open(retptr, config.ptr, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4);
170
+ wasm.base_mode_seal(retptr, config.ptr, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3);
168
171
  var r0 = getInt32Memory0()[retptr / 4 + 0];
169
172
  var r1 = getInt32Memory0()[retptr / 4 + 1];
170
173
  var r2 = getInt32Memory0()[retptr / 4 + 2];
171
- var r3 = getInt32Memory0()[retptr / 4 + 3];
172
- if (r3) {
173
- throw takeObject(r2);
174
+ if (r2) {
175
+ throw takeObject(r1);
174
176
  }
175
- var v5 = getArrayU8FromWasm0(r0, r1).slice();
176
- wasm.__wbindgen_free(r0, r1 * 1);
177
- return v5;
177
+ return EncappedKeyAndCiphertext.__wrap(r0);
178
178
  } finally {
179
179
  wasm.__wbindgen_add_to_stack_pointer(16);
180
180
  }
@@ -647,13 +647,13 @@ class Keypair {
647
647
  }
648
648
  module.exports.Keypair = Keypair;
649
649
 
650
- module.exports.__wbg_idlookuperror_new = function(arg0) {
651
- const ret = IdLookupError.__wrap(arg0);
650
+ module.exports.__wbg_hpkeerror_new = function(arg0) {
651
+ const ret = HpkeError.__wrap(arg0);
652
652
  return addHeapObject(ret);
653
653
  };
654
654
 
655
- module.exports.__wbg_hpkeerror_new = function(arg0) {
656
- const ret = HpkeError.__wrap(arg0);
655
+ module.exports.__wbg_idlookuperror_new = function(arg0) {
656
+ const ret = IdLookupError.__wrap(arg0);
657
657
  return addHeapObject(ret);
658
658
  };
659
659
 
@@ -695,16 +695,6 @@ module.exports.__wbindgen_is_string = function(arg0) {
695
695
  return ret;
696
696
  };
697
697
 
698
- module.exports.__wbg_static_accessor_NODE_MODULE_33b45247c55045b0 = function() {
699
- const ret = module;
700
- return addHeapObject(ret);
701
- };
702
-
703
- module.exports.__wbg_require_2a93bc09fee45aca = function() { return handleError(function (arg0, arg1, arg2) {
704
- const ret = getObject(arg0).require(getStringFromWasm0(arg1, arg2));
705
- return addHeapObject(ret);
706
- }, arguments) };
707
-
708
698
  module.exports.__wbg_crypto_2f56257a38275dbd = function(arg0) {
709
699
  const ret = getObject(arg0).crypto;
710
700
  return addHeapObject(ret);
@@ -715,6 +705,16 @@ module.exports.__wbg_msCrypto_d07655bf62361f21 = function(arg0) {
715
705
  return addHeapObject(ret);
716
706
  };
717
707
 
708
+ module.exports.__wbg_static_accessor_NODE_MODULE_33b45247c55045b0 = function() {
709
+ const ret = module;
710
+ return addHeapObject(ret);
711
+ };
712
+
713
+ module.exports.__wbg_require_2a93bc09fee45aca = function() { return handleError(function (arg0, arg1, arg2) {
714
+ const ret = getObject(arg0).require(getStringFromWasm0(arg1, arg2));
715
+ return addHeapObject(ret);
716
+ }, arguments) };
717
+
718
718
  module.exports.__wbg_newnoargs_e23b458e372830de = function(arg0, arg1) {
719
719
  const ret = new Function(getStringFromWasm0(arg0, arg1));
720
720
  return addHeapObject(ret);
Binary file
@@ -0,0 +1,32 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export function base_mode_open(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number): void;
5
+ export function __wbg_config_free(a: number): void;
6
+ export function __wbg_get_config_aead(a: number): number;
7
+ export function __wbg_set_config_aead(a: number, b: number): void;
8
+ export function __wbg_get_config_kdf(a: number): number;
9
+ export function __wbg_set_config_kdf(a: number, b: number): void;
10
+ export function __wbg_get_config_kem(a: number): number;
11
+ export function __wbg_set_config_kem(a: number, b: number): void;
12
+ export function config_base_mode_seal(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): void;
13
+ export function config_base_mode_open(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number): void;
14
+ export function config_try_from_ids(a: number, b: number, c: number, d: number): void;
15
+ export function __wbg_keypair_free(a: number): void;
16
+ export function __wbg_get_keypair_public_key(a: number, b: number): void;
17
+ export function __wbg_set_keypair_public_key(a: number, b: number, c: number): void;
18
+ export function __wbg_get_keypair_private_key(a: number, b: number): void;
19
+ export function __wbg_set_keypair_private_key(a: number, b: number, c: number): void;
20
+ export function keypair_new(a: number): number;
21
+ export function base_mode_seal(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): void;
22
+ export function __wbg_encappedkeyandciphertext_free(a: number): void;
23
+ export function __wbg_get_encappedkeyandciphertext_encapped_key(a: number, b: number): void;
24
+ export function __wbg_set_encappedkeyandciphertext_encapped_key(a: number, b: number, c: number): void;
25
+ export function __wbg_get_encappedkeyandciphertext_ciphertext(a: number, b: number): void;
26
+ export function __wbg_set_encappedkeyandciphertext_ciphertext(a: number, b: number, c: number): void;
27
+ export function __wbg_idlookuperror_free(a: number): void;
28
+ export function __wbg_hpkeerror_free(a: number): void;
29
+ export function __wbindgen_add_to_stack_pointer(a: number): number;
30
+ export function __wbindgen_malloc(a: number): number;
31
+ export function __wbindgen_free(a: number, b: number): void;
32
+ export function __wbindgen_exn_store(a: number): void;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
22
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
+ var node_exports = {};
24
+ __export(node_exports, {
25
+ __wasm: () => __wasm
26
+ });
27
+ module.exports = __toCommonJS(node_exports);
28
+ var import_hpke_dispatch_bg = __toESM(require("./hpke_dispatch_bg.wasm"));
29
+ var import_hpke_dispatch = __toESM(require("./hpke_dispatch.js"));
30
+ __reExport(node_exports, require("./hpke_dispatch.js"), module.exports);
31
+ const __wasm = (0, import_hpke_dispatch.default)(import_hpke_dispatch_bg.default);
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["index.ts"],
4
+ "sourcesContent": ["import wasm from \"./hpke_dispatch_bg.wasm\";\nimport init from \"./hpke_dispatch.js\";\n//this is exported so it doesn't get dead-code-eliminated\n// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\nexport const __wasm = init(wasm);\nexport * from \"./hpke_dispatch.js\";\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,8BAAiB;AACjB,2BAAiB;AAIjB,yBAAc,+BALd;AAIO,MAAM,SAAS,kCAAK,+BAAI;",
6
+ "names": []
7
+ }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var import_chai = require("chai");
3
+ var import__ = require(".");
4
+ var import_util = require("util");
5
+ describe("HPKE", () => {
6
+ describe("Config", () => {
7
+ it("throws when the algorithms specified are invalid", () => {
8
+ const [aead, kdf, kem] = [100, 100, 100];
9
+ (0, import_chai.assert)(!(aead in import__.Aead));
10
+ (0, import_chai.assert)(!(kdf in import__.Kdf));
11
+ (0, import_chai.assert)(!(kem in import__.Kem));
12
+ import_chai.assert.throws(() => import__.Config.try_from_ids(aead, kdf, kem));
13
+ });
14
+ });
15
+ describe("Keypair", () => {
16
+ it("generates a (65, 32)-byte keypair for Kem.DhP256HkdfSha256", () => {
17
+ const keypair = new import__.Keypair(import__.Kem.DhP256HkdfSha256);
18
+ import_chai.assert.equal(keypair.public_key.length, 65);
19
+ import_chai.assert.equal(keypair.private_key.length, 32);
20
+ });
21
+ it("generates a (32, 32)-byte keypair for Kem.X25519HkdfSha256", () => {
22
+ const keypair = new import__.Keypair(import__.Kem.X25519HkdfSha256);
23
+ import_chai.assert.equal(keypair.public_key.length, 32);
24
+ import_chai.assert.equal(keypair.private_key.length, 32);
25
+ });
26
+ it("throws when provided an invalid Kem", () => {
27
+ import_chai.assert.throws(() => new import__.Keypair(1e3));
28
+ });
29
+ });
30
+ describe("Round trips", () => {
31
+ const decoder = new import_util.TextDecoder();
32
+ const encoder = new import_util.TextEncoder();
33
+ const startMessage = "plaintext message text";
34
+ const plaintext = encoder.encode(startMessage);
35
+ const info = encoder.encode("app info");
36
+ const aad = encoder.encode("associated data");
37
+ for (const aead of [
38
+ import__.Aead.AesGcm128,
39
+ import__.Aead.AesGcm256,
40
+ import__.Aead.ChaCha20Poly1305
41
+ ]) {
42
+ for (const kdf of [import__.Kdf.Sha256, import__.Kdf.Sha384, import__.Kdf.Sha512]) {
43
+ for (const kem of [import__.Kem.DhP256HkdfSha256, import__.Kem.X25519HkdfSha256]) {
44
+ it(`succeeds for ${import__.Aead[aead]}, ${import__.Kdf[kdf]}, ${import__.Kem[kem]}`, () => {
45
+ const config = import__.Config.try_from_ids(aead, kdf, kem);
46
+ import_chai.assert.equal(config.aead, aead);
47
+ import_chai.assert.equal(config.kem, kem);
48
+ import_chai.assert.equal(config.kdf, kdf);
49
+ const keypair = new import__.Keypair(config.kem);
50
+ const keyAndCiphertext = config.base_mode_seal(keypair.public_key, info, plaintext, aad);
51
+ const roundTrip = config.base_mode_open(keypair.private_key, keyAndCiphertext.encapped_key, info, keyAndCiphertext.ciphertext, aad);
52
+ import_chai.assert.equal(startMessage, decoder.decode(roundTrip));
53
+ });
54
+ }
55
+ }
56
+ }
57
+ });
58
+ });
59
+ //# sourceMappingURL=index.spec.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["index.spec.ts"],
4
+ "sourcesContent": ["import { assert } from \"chai\";\nimport { Keypair, Aead, Kdf, Kem, Config } from \".\";\nimport { TextEncoder, TextDecoder } from \"util\";\n\ndescribe(\"HPKE\", () => {\n describe(\"Config\", () => {\n it(\"throws when the algorithms specified are invalid\", () => {\n const [aead, kdf, kem] = [100, 100, 100];\n\n assert(!(aead in Aead));\n assert(!(kdf in Kdf));\n assert(!(kem in Kem));\n\n assert.throws(() => Config.try_from_ids(aead, kdf, kem));\n });\n });\n\n describe(\"Keypair\", () => {\n it(\"generates a (65, 32)-byte keypair for Kem.DhP256HkdfSha256\", () => {\n const keypair = new Keypair(Kem.DhP256HkdfSha256);\n assert.equal(keypair.public_key.length, 65);\n assert.equal(keypair.private_key.length, 32);\n });\n\n it(\"generates a (32, 32)-byte keypair for Kem.X25519HkdfSha256\", () => {\n const keypair = new Keypair(Kem.X25519HkdfSha256);\n assert.equal(keypair.public_key.length, 32);\n assert.equal(keypair.private_key.length, 32);\n });\n\n it(\"throws when provided an invalid Kem\", () => {\n assert.throws(() => new Keypair(1000));\n });\n });\n\n describe(\"Round trips\", () => {\n const decoder = new TextDecoder();\n const encoder = new TextEncoder();\n\n const startMessage = \"plaintext message text\";\n const plaintext = encoder.encode(startMessage);\n const info = encoder.encode(\"app info\");\n const aad = encoder.encode(\"associated data\");\n\n for (const aead of [\n Aead.AesGcm128,\n Aead.AesGcm256,\n Aead.ChaCha20Poly1305,\n ]) {\n for (const kdf of [Kdf.Sha256, Kdf.Sha384, Kdf.Sha512]) {\n for (const kem of [Kem.DhP256HkdfSha256, Kem.X25519HkdfSha256]) {\n it(`succeeds for ${Aead[aead]}, ${Kdf[kdf]}, ${Kem[kem]}`, () => {\n const config = Config.try_from_ids(aead, kdf, kem);\n\n assert.equal(config.aead, aead);\n assert.equal(config.kem, kem);\n assert.equal(config.kdf, kdf);\n\n const keypair = new Keypair(config.kem);\n\n const keyAndCiphertext = config.base_mode_seal(\n keypair.public_key,\n info,\n plaintext,\n aad\n );\n\n const roundTrip = config.base_mode_open(\n keypair.private_key,\n keyAndCiphertext.encapped_key,\n info,\n keyAndCiphertext.ciphertext,\n aad\n );\n\n assert.equal(startMessage, decoder.decode(roundTrip));\n });\n }\n }\n }\n });\n});\n"],
5
+ "mappings": ";AAAA,kBAAuB;AACvB,eAAgD;AAChD,kBAAyC;AAEzC,SAAS,QAAQ,MAAM;AACrB,WAAS,UAAU,MAAM;AACvB,OAAG,oDAAoD,MAAM;AAC3D,YAAM,CAAC,MAAM,KAAK,OAAO,CAAC,KAAK,KAAK,GAAG;AAEvC,8BAAO,CAAE,SAAQ,cAAK;AACtB,8BAAO,CAAE,QAAO,aAAI;AACpB,8BAAO,CAAE,QAAO,aAAI;AAEpB,yBAAO,OAAO,MAAM,gBAAO,aAAa,MAAM,KAAK,GAAG,CAAC;AAAA,IACzD,CAAC;AAAA,EACH,CAAC;AAED,WAAS,WAAW,MAAM;AACxB,OAAG,8DAA8D,MAAM;AACrE,YAAM,UAAU,IAAI,iBAAQ,aAAI,gBAAgB;AAChD,yBAAO,MAAM,QAAQ,WAAW,QAAQ,EAAE;AAC1C,yBAAO,MAAM,QAAQ,YAAY,QAAQ,EAAE;AAAA,IAC7C,CAAC;AAED,OAAG,8DAA8D,MAAM;AACrE,YAAM,UAAU,IAAI,iBAAQ,aAAI,gBAAgB;AAChD,yBAAO,MAAM,QAAQ,WAAW,QAAQ,EAAE;AAC1C,yBAAO,MAAM,QAAQ,YAAY,QAAQ,EAAE;AAAA,IAC7C,CAAC;AAED,OAAG,uCAAuC,MAAM;AAC9C,yBAAO,OAAO,MAAM,IAAI,iBAAQ,GAAI,CAAC;AAAA,IACvC,CAAC;AAAA,EACH,CAAC;AAED,WAAS,eAAe,MAAM;AAC5B,UAAM,UAAU,IAAI,wBAAY;AAChC,UAAM,UAAU,IAAI,wBAAY;AAEhC,UAAM,eAAe;AACrB,UAAM,YAAY,QAAQ,OAAO,YAAY;AAC7C,UAAM,OAAO,QAAQ,OAAO,UAAU;AACtC,UAAM,MAAM,QAAQ,OAAO,iBAAiB;AAE5C,eAAW,QAAQ;AAAA,MACjB,cAAK;AAAA,MACL,cAAK;AAAA,MACL,cAAK;AAAA,IACP,GAAG;AACD,iBAAW,OAAO,CAAC,aAAI,QAAQ,aAAI,QAAQ,aAAI,MAAM,GAAG;AACtD,mBAAW,OAAO,CAAC,aAAI,kBAAkB,aAAI,gBAAgB,GAAG;AAC9D,aAAG,gBAAgB,cAAK,UAAU,aAAI,SAAS,aAAI,QAAQ,MAAM;AAC/D,kBAAM,SAAS,gBAAO,aAAa,MAAM,KAAK,GAAG;AAEjD,+BAAO,MAAM,OAAO,MAAM,IAAI;AAC9B,+BAAO,MAAM,OAAO,KAAK,GAAG;AAC5B,+BAAO,MAAM,OAAO,KAAK,GAAG;AAE5B,kBAAM,UAAU,IAAI,iBAAQ,OAAO,GAAG;AAEtC,kBAAM,mBAAmB,OAAO,eAC9B,QAAQ,YACR,MACA,WACA,GACF;AAEA,kBAAM,YAAY,OAAO,eACvB,QAAQ,aACR,iBAAiB,cACjB,MACA,iBAAiB,YACjB,GACF;AAEA,+BAAO,MAAM,cAAc,QAAQ,OAAO,SAAS,CAAC;AAAA,UACtD,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,82 @@
1
+ import { assert } from "chai";
2
+ import { Keypair, Aead, Kdf, Kem, Config } from ".";
3
+ import { TextEncoder, TextDecoder } from "util";
4
+
5
+ describe("HPKE", () => {
6
+ describe("Config", () => {
7
+ it("throws when the algorithms specified are invalid", () => {
8
+ const [aead, kdf, kem] = [100, 100, 100];
9
+
10
+ assert(!(aead in Aead));
11
+ assert(!(kdf in Kdf));
12
+ assert(!(kem in Kem));
13
+
14
+ assert.throws(() => Config.try_from_ids(aead, kdf, kem));
15
+ });
16
+ });
17
+
18
+ describe("Keypair", () => {
19
+ it("generates a (65, 32)-byte keypair for Kem.DhP256HkdfSha256", () => {
20
+ const keypair = new Keypair(Kem.DhP256HkdfSha256);
21
+ assert.equal(keypair.public_key.length, 65);
22
+ assert.equal(keypair.private_key.length, 32);
23
+ });
24
+
25
+ it("generates a (32, 32)-byte keypair for Kem.X25519HkdfSha256", () => {
26
+ const keypair = new Keypair(Kem.X25519HkdfSha256);
27
+ assert.equal(keypair.public_key.length, 32);
28
+ assert.equal(keypair.private_key.length, 32);
29
+ });
30
+
31
+ it("throws when provided an invalid Kem", () => {
32
+ assert.throws(() => new Keypair(1000));
33
+ });
34
+ });
35
+
36
+ describe("Round trips", () => {
37
+ const decoder = new TextDecoder();
38
+ const encoder = new TextEncoder();
39
+
40
+ const startMessage = "plaintext message text";
41
+ const plaintext = encoder.encode(startMessage);
42
+ const info = encoder.encode("app info");
43
+ const aad = encoder.encode("associated data");
44
+
45
+ for (const aead of [
46
+ Aead.AesGcm128,
47
+ Aead.AesGcm256,
48
+ Aead.ChaCha20Poly1305,
49
+ ]) {
50
+ for (const kdf of [Kdf.Sha256, Kdf.Sha384, Kdf.Sha512]) {
51
+ for (const kem of [Kem.DhP256HkdfSha256, Kem.X25519HkdfSha256]) {
52
+ it(`succeeds for ${Aead[aead]}, ${Kdf[kdf]}, ${Kem[kem]}`, () => {
53
+ const config = Config.try_from_ids(aead, kdf, kem);
54
+
55
+ assert.equal(config.aead, aead);
56
+ assert.equal(config.kem, kem);
57
+ assert.equal(config.kdf, kdf);
58
+
59
+ const keypair = new Keypair(config.kem);
60
+
61
+ const keyAndCiphertext = config.base_mode_seal(
62
+ keypair.public_key,
63
+ info,
64
+ plaintext,
65
+ aad
66
+ );
67
+
68
+ const roundTrip = config.base_mode_open(
69
+ keypair.private_key,
70
+ keyAndCiphertext.encapped_key,
71
+ info,
72
+ keyAndCiphertext.ciphertext,
73
+ aad
74
+ );
75
+
76
+ assert.equal(startMessage, decoder.decode(roundTrip));
77
+ });
78
+ }
79
+ }
80
+ }
81
+ });
82
+ });
@@ -0,0 +1,6 @@
1
+ import wasm from "./hpke_dispatch_bg.wasm";
2
+ import init from "./hpke_dispatch.js";
3
+ //this is exported so it doesn't get dead-code-eliminated
4
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
5
+ export const __wasm = init(wasm);
6
+ export * from "./hpke_dispatch.js";
@@ -0,0 +1,6 @@
1
+ import wasm from "./hpke_dispatch_bg.wasm";
2
+ import init from "./hpke_dispatch.js";
3
+ //this is exported so it doesn't get dead-code-eliminated
4
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
5
+ export const __wasm = init(wasm);
6
+ export * from "./hpke_dispatch.js";
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "hpke-dispatch",
3
+ "description": "runtime algorithmic selection for hybrid public key encryption",
4
+ "version": "0.3.0",
5
+ "license": "MPL-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/divviup/hpke-dispatch"
9
+ },
10
+ "files": [
11
+ "hpke_dispatch_bg.wasm",
12
+ "hpke_dispatch.js",
13
+ "hpke_dispatch.d.ts"
14
+ ],
15
+ "main": "hpke_dispatch.js",
16
+ "types": "hpke_dispatch.d.ts",
17
+ "keywords": [
18
+ "hpke",
19
+ "encryption"
20
+ ]
21
+ }
@@ -0,0 +1,57 @@
1
+ var import_chai = require("chai");
2
+ var import__ = require(".");
3
+ var import_util = require("util");
4
+ describe("HPKE", () => {
5
+ describe("Config", () => {
6
+ it("throws when the algorithms specified are invalid", () => {
7
+ const [aead, kdf, kem] = [100, 100, 100];
8
+ (0, import_chai.assert)(!(aead in import__.Aead));
9
+ (0, import_chai.assert)(!(kdf in import__.Kdf));
10
+ (0, import_chai.assert)(!(kem in import__.Kem));
11
+ import_chai.assert.throws(() => import__.Config.try_from_ids(aead, kdf, kem));
12
+ });
13
+ });
14
+ describe("Keypair", () => {
15
+ it("generates a (65, 32)-byte keypair for Kem.DhP256HkdfSha256", () => {
16
+ const keypair = new import__.Keypair(import__.Kem.DhP256HkdfSha256);
17
+ import_chai.assert.equal(keypair.public_key.length, 65);
18
+ import_chai.assert.equal(keypair.private_key.length, 32);
19
+ });
20
+ it("generates a (32, 32)-byte keypair for Kem.X25519HkdfSha256", () => {
21
+ const keypair = new import__.Keypair(import__.Kem.X25519HkdfSha256);
22
+ import_chai.assert.equal(keypair.public_key.length, 32);
23
+ import_chai.assert.equal(keypair.private_key.length, 32);
24
+ });
25
+ it("throws when provided an invalid Kem", () => {
26
+ import_chai.assert.throws(() => new import__.Keypair(1e3));
27
+ });
28
+ });
29
+ describe("Round trips", () => {
30
+ const decoder = new import_util.TextDecoder();
31
+ const encoder = new import_util.TextEncoder();
32
+ const startMessage = "plaintext message text";
33
+ const plaintext = encoder.encode(startMessage);
34
+ const info = encoder.encode("app info");
35
+ const aad = encoder.encode("associated data");
36
+ for (const aead of [
37
+ import__.Aead.AesGcm128,
38
+ import__.Aead.AesGcm256,
39
+ import__.Aead.ChaCha20Poly1305
40
+ ]) {
41
+ for (const kdf of [import__.Kdf.Sha256, import__.Kdf.Sha384, import__.Kdf.Sha512]) {
42
+ for (const kem of [import__.Kem.DhP256HkdfSha256, import__.Kem.X25519HkdfSha256]) {
43
+ it(`succeeds for ${import__.Aead[aead]}, ${import__.Kdf[kdf]}, ${import__.Kem[kem]}`, () => {
44
+ const config = import__.Config.try_from_ids(aead, kdf, kem);
45
+ import_chai.assert.equal(config.aead, aead);
46
+ import_chai.assert.equal(config.kem, kem);
47
+ import_chai.assert.equal(config.kdf, kdf);
48
+ const keypair = new import__.Keypair(config.kem);
49
+ const keyAndCiphertext = config.base_mode_seal(keypair.public_key, info, plaintext, aad);
50
+ const roundTrip = config.base_mode_open(keypair.private_key, keyAndCiphertext.encapped_key, info, keyAndCiphertext.ciphertext, aad);
51
+ import_chai.assert.equal(startMessage, decoder.decode(roundTrip));
52
+ });
53
+ }
54
+ }
55
+ }
56
+ });
57
+ });
@@ -0,0 +1 @@
1
+ *