simple-hpke 0.1.3 → 0.2.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.
package/dist/util.d.ts CHANGED
@@ -18,7 +18,7 @@ export type RecipientKey = CryptoKey | CryptoKeyPair | Uint8Array | {
18
18
  export declare function aeadSeal(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array): Promise<Uint8Array>;
19
19
  export declare function aeadOpen(key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array>;
20
20
  /**
21
- * Seal a fresh (or supplied) AES key to `recipient`, then AES-GCM encrypt a
21
+ * Wrap a fresh (or supplied) AES key to `recipient`, then AES-GCM encrypt a
22
22
  * message under it. The wrapped key, IV, and ciphertext are concatenated
23
23
  * into a single self-describing envelope.
24
24
  *
@@ -32,20 +32,20 @@ export declare function aeadOpen(key: Uint8Array, nonce: Uint8Array, ciphertext:
32
32
  * @param message Plaintext to encrypt. A `string` is UTF-8 encoded.
33
33
  * @param aesKey Optional key, as either an AES-GCM `CryptoKey` or its raw
34
34
  * bytes (`Uint8Array`, 16 or 32 bytes). Omit to generate a fresh key of
35
- * `opts.keysize` bits. A supplied `CryptoKey` MUST be extractable. Each
35
+ * `opts.size` bits. A supplied `CryptoKey` MUST be extractable. Each
36
36
  * call picks a fresh random 96-bit IV for the message ciphertext, so
37
37
  * reusing the same `aesKey` across many calls carries the standard
38
38
  * birthday bound for random nonces (collision risk becomes
39
39
  * non-negligible around 2^32 messages under one key, NIST SP 800-38D) --
40
40
  * prefer a fresh key per call (the default) over reusing one at scale.
41
- * @param opts `keysize` (128/256, default 256; ignored when `aesKey` is
41
+ * @param opts `size` (128/256, default 256; ignored when `aesKey` is
42
42
  * supplied) and `info` (bound into the HPKE key schedule).
43
43
  * @returns The concatenated envelope bytes.
44
44
  *
45
45
  * `encrypt.asString(...)` returns the same envelope as an encoded string.
46
46
  */
47
47
  export declare function encryptBytes(recipient: RecipientKey, message: Uint8Array | string, aesKey?: CryptoKey | Uint8Array | null, opts?: {
48
- keysize?: 128 | 256;
48
+ size?: 128 | 256;
49
49
  info?: Uint8Array | string;
50
50
  }): Promise<Uint8Array>;
51
51
  /**
@@ -60,19 +60,19 @@ export declare function encryptBytes(recipient: RecipientKey, message: Uint8Arra
60
60
  * @param message Plaintext to encrypt. A `string` is UTF-8 encoded.
61
61
  * @param aesKey Optional key, as either an AES-GCM `CryptoKey` or its raw
62
62
  * bytes (`Uint8Array`, 16 or 32 bytes). Omit to generate a fresh key of
63
- * `opts.keysize` bits. A supplied `CryptoKey` MUST be extractable. Each
63
+ * `opts.size` bits. A supplied `CryptoKey` MUST be extractable. Each
64
64
  * call picks a fresh random 96-bit IV for the message ciphertext, so
65
65
  * reusing the same `aesKey` across many calls carries the standard
66
66
  * birthday bound for random nonces (collision risk becomes
67
67
  * non-negligible around 2^32 messages under one key, NIST SP 800-38D) --
68
68
  * prefer a fresh key per call (the default) over reusing one at scale.
69
- * @param opts `keysize` (128/256, default 256; ignored when `aesKey` is
69
+ * @param opts `size` (128/256, default 256; ignored when `aesKey` is
70
70
  * supplied), `info` (bound into the HPKE key schedule), and `encoding`
71
71
  * (the string encoding of the returned envelope; default `base64url`).
72
72
  * @returns The encoded envelope string.
73
73
  */
74
74
  export declare function encryptToString(recipient: RecipientKey, message: Uint8Array | string, aesKey?: CryptoKey | Uint8Array | null, opts?: {
75
- keysize?: 128 | 256;
75
+ size?: 128 | 256;
76
76
  info?: Uint8Array | string;
77
77
  encoding?: SupportedEncodings;
78
78
  }): Promise<string>;
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,kBAAkB,EAC1B,MAAM,aAAa,CAAA;AAYpB;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAClB,SAAS,GACT,aAAa,GACb,UAAU,GACV;IAAE,SAAS,EAAC,MAAM,CAAC;IAAC,QAAQ,CA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EAAC,YAAY,GACxB,OAAO,CAAC,SAAS,CAAC,CAiCnB;AA6DD,wBAAsB,KAAK,CACvB,GAAG,EAAC,SAAS,GACf,OAAO,CAAC;IAAE,YAAY,EAAC,UAAU,CAAC;IAAC,GAAG,EAAC,UAAU,CAAA;CAAE,CAAC,CAarD;AAID,wBAAsB,KAAK,CACvB,GAAG,EAAC,UAAU,EACd,OAAO,EAAC,aAAa,GACvB,OAAO,CAAC,UAAU,CAAC,CAiBpB;AAKD,wBAAgB,KAAK,CAAE,CAAC,EAAC,MAAM,EAAE,GAAG,EAAC,MAAM,GAAE,UAAU,CAQtD;AAED,wBAAgB,MAAM,CAAE,GAAG,MAAM,EAAC,UAAU,EAAE,GAAE,UAAU,CAUzD"}
package/dist/util.js CHANGED
@@ -1308,16 +1308,6 @@ var HPKE_SUITE_ID = concat2(
1308
1308
 
1309
1309
  // src/index.ts
1310
1310
  var subtle = globalThis.crypto.subtle;
1311
- async function create2(recipient, opts) {
1312
- const { wrapped, key } = await encryptKey(recipient, null, opts);
1313
- return { enc: wrapped, key };
1314
- }
1315
- __name(create2, "create");
1316
- async function seal(recipient, aesKey, opts) {
1317
- const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
1318
- return { enc: wrapped, key };
1319
- }
1320
- __name(seal, "seal");
1321
1311
  var open = Object.assign(openBytes, {
1322
1312
  raw: openRawBytes
1323
1313
  });
@@ -1498,7 +1488,7 @@ async function encryptKey(recipient, aesKey, opts) {
1498
1488
  const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
1499
1489
  const wrapped = concat2(enc, ciphertext);
1500
1490
  const aesGcmKey = await importAesKey(keyBytes);
1501
- return { wrapped, key: aesGcmKey };
1491
+ return { enc: wrapped, key: aesGcmKey };
1502
1492
  }
1503
1493
  __name(encryptKey, "encryptKey");
1504
1494
 
@@ -1538,17 +1528,7 @@ async function aeadOpen(key, nonce, ciphertext) {
1538
1528
  __name(aeadOpen, "aeadOpen");
1539
1529
  async function encryptBytes(recipient, message, aesKey, opts) {
1540
1530
  const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
1541
- let enc;
1542
- let key;
1543
- if (!aesKey) {
1544
- const keys = await create2(recipient, opts);
1545
- enc = keys.enc;
1546
- key = keys.key;
1547
- } else {
1548
- const keys = await seal(recipient, aesKey, opts);
1549
- enc = keys.enc;
1550
- key = keys.key;
1551
- }
1531
+ const { enc, key } = await encryptKey(recipient, aesKey, opts);
1552
1532
  const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
1553
1533
  const ct = new Uint8Array(await subtle2.encrypt(
1554
1534
  { name: "AES-GCM", iv },