simple-hpke 0.1.5 → 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/index.d.ts CHANGED
@@ -15,26 +15,14 @@ export declare function create(recipient: RecipientKey, opts?: {
15
15
  key: CryptoKey;
16
16
  }>;
17
17
  /**
18
- * Encrypt the given AES key to the given public key.
19
- *
20
- * @returns {{ enc, key }} The wrapped envelope bytes and a usable AES-GCM
21
- * key with the same raw bytes as `aesKey`.
22
- */
23
- export declare function seal(recipient: RecipientKey, aesKey: CryptoKey | Uint8Array | null, opts?: {
24
- info?: Uint8Array | string;
25
- }): Promise<{
26
- enc: Uint8Array<ArrayBufferLike>;
27
- key: CryptoKey;
28
- }>;
29
- /**
30
- * Recover the AES key wrapped by `create` or `seal`. Call `open(...)` for a
31
- * usable AES-GCM `CryptoKey`, or `open.raw(...)` for the raw key bytes.
18
+ * Recover the AES key wrapped by `create` or `encryptKey`. Call `open(...)`
19
+ * for a usable AES-GCM `CryptoKey`, or `open.raw(...)` for the raw key bytes.
32
20
  */
33
21
  export declare const open: typeof openBytes & {
34
22
  raw: typeof openRawBytes;
35
23
  };
36
24
  /**
37
- * Seal an AES key to `recipient` and AES-GCM encrypt a message under it.
25
+ * Wrap an AES key to `recipient` and AES-GCM encrypt a message under it.
38
26
  * Call `encrypt(...)` for the raw envelope bytes, or `encrypt.asString(...)`
39
27
  * for the same envelope as an encoded string.
40
28
  */
@@ -102,9 +90,9 @@ declare function decryptFromString(keypair: CryptoKeyPair, message: string, opts
102
90
  * Like `open`, but returns the recovered key as raw bytes instead of
103
91
  * importing it as an AES-GCM `CryptoKey`. Exposed as `open.raw`.
104
92
  *
105
- * @param keypair The same X25519 `CryptoKeyPair` used to create or seal.
106
- * @param wrapped The `enc` bytes returned by `create` or `seal`.
107
- * @param opts `info` — must match the value passed to `create` or `seal`.
93
+ * @param keypair The same X25519 `CryptoKeyPair` used to create or encryptKey.
94
+ * @param wrapped The `enc` bytes returned by `create` or `encryptKey`.
95
+ * @param opts `info` — must match the value passed to `create` or `encryptKey`.
108
96
  * @returns The recovered key bytes (16 or 32 bytes, matching whatever
109
97
  * was wrapped).
110
98
  */
@@ -112,15 +100,38 @@ declare function openRawBytes(keypair: CryptoKeyPair, wrapped: Uint8Array, opts?
112
100
  info: Uint8Array | string;
113
101
  }): Promise<Uint8Array>;
114
102
  /**
115
- * Recover an AES key that was wrapped with `create` or `seal`.
103
+ * Recover an AES key that was wrapped with `create` or `encryptKey`.
116
104
  *
117
- * @param keypair The same X25519 `CryptoKeyPair` used to create or seal.
118
- * @param wrapped The `enc` bytes returned by `create` or `seal`.
119
- * @param opts `info` — must match the value passed to `create` or `seal`.
105
+ * @param keypair The same X25519 `CryptoKeyPair` used to create or encryptKey.
106
+ * @param wrapped The `enc` bytes returned by `create` or `encryptKey`.
107
+ * @param opts `info` — must match the value passed to `create` or `encryptKey`.
120
108
  * @returns The recovered AES-GCM `CryptoKey` (extractable).
121
109
  */
122
110
  declare function openBytes(keypair: CryptoKeyPair, wrapped: Uint8Array, opts?: {
123
111
  info: Uint8Array | string;
124
112
  }): Promise<CryptoKey>;
113
+ /**
114
+ * Wrap an AES key to a recipient's public key. `create(...)` calls this
115
+ * helper with no `aesKey`, so it generates a fresh AES key first.
116
+ *
117
+ * @param recipient The recipient's X25519 public key, as a `CryptoKey`,
118
+ * `CryptoKeyPair` (its `.publicKey` is used), 32 raw bytes (`Uint8Array`),
119
+ * or `{ publicKey:string, encoding? }` (encoding defaults to `base64url`).
120
+ * @param aesKey Optional key to wrap, as either an AES-GCM `CryptoKey` or its
121
+ * raw bytes (`Uint8Array`, 16 or 32 bytes). Omit to generate a fresh
122
+ * extractable key of `opts.size` bits. A supplied `CryptoKey` MUST be
123
+ * extractable (its raw bytes are wrapped).
124
+ * @param opts `size` (128/256, default 256; ignored when `aesKey` is
125
+ * supplied) and `info` (bound into the HPKE key schedule; default empty).
126
+ * @returns { enc:Uint8Array, key:CryptoKey } The wrapped envelope bytes
127
+ * and a usable AES-GCM `CryptoKey`.
128
+ */
129
+ export declare function encryptKey(recipient: RecipientKey, aesKey?: CryptoKey | Uint8Array | null, opts?: {
130
+ size?: 128 | 256;
131
+ info?: Uint8Array | string;
132
+ }): Promise<{
133
+ enc: Uint8Array;
134
+ key: CryptoKey;
135
+ }>;
125
136
  export {};
126
137
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACH,KAAK,YAAY,EAGjB,eAAe,EACf,YAAY,EAOf,MAAM,QAAQ,CAAA;AAoBf;;;;;;;GAOG;AACH,wBAAsB,MAAM,CACxB,SAAS,EAAC,YAAY,EACtB,IAAI,CAAC,EAAC;IAEF,IAAI,CAAC,EAAC,GAAG,GAAC,GAAG,CAAA;IAEb,IAAI,CAAC,EAAC,UAAU,GAAC,MAAM,CAAA;CAC1B,GACH,OAAO,CAAC;IAAE,GAAG,EAAC,UAAU,CAAC,eAAe,CAAC,CAAC;IAAC,GAAG,EAAC,SAAS,CAAA;CAAE,CAAC,CAG5D;AAED;;;;;GAKG;AACH,wBAAsB,IAAI,CACtB,SAAS,EAAC,YAAY,EACtB,MAAM,EAAC,SAAS,GAAC,UAAU,GAAC,IAAI,EAChC,IAAI,CAAC,EAAC;IAEF,IAAI,CAAC,EAAC,UAAU,GAAC,MAAM,CAAA;CAC1B,GACH,OAAO,CAAC;IAAE,GAAG,EAAC,UAAU,CAAC,eAAe,CAAC,CAAC;IAAC,GAAG,EAAC,SAAS,CAAA;CAAE,CAAC,CAG5D;AAED;;;GAGG;AACH,eAAO,MAAM,IAAI;;CAEf,CAAA;AAEF;;;;GAIG;AACH,eAAO,MAAM,OAAO;;CAElB,CAAA;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,OAAO;;;CAGlB,CAAA;AAEF;;;;;;;;GAQG;AACH,iBAAe,YAAY,CACvB,OAAO,EAAC,aA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,iBAAiB,CAC5B,OAAO,EAAC,aAAa,EACrB,OAAO,EAAC,MAAM,EACd,IAAI,EAAC;IAAE,IAAI,CAAC,EAAC,UAAU,GAAC,MAAM,CAAC;IAAC,MAAM,EAAC,IAAI,CAAA;CAAE,GAC/C,OAAO,CAAC,UAAU,CAAC,CAAA;AA0HrB;;;;;;;;;GASG;AACH,iBAAe,YAAY,CACvB,OAAO,EAAC,aAAa,EACrB,OAAO,EAAC,UAAU,EAClB,IAAI,CAAC,EAAC;IAAE,IAAI,EAAC,UAAU,GAAC,MAAM,CAAA;CAAE,GAClC,OAAO,CAAC,UAAU,CAAC,CAYpB;AAED;;;;;;;GAOG;AACH,iBAAe,SAAS,CACpB,OAAO,EAAC,aAAa,EACrB,OAAO,EAAC,UAAU,EAClB,IAAI,CAAC,EAAC;IAAE,IAAI,EAAC,UAAU,GAAC,MAAM,CAAA;CAAE,GAClC,OAAO,CAAC,SAAS,CAAC,CAGnB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,UAAU,CAC5B,SAAS,EAAC,YAAY,EACtB,MAAM,CAAC,EAAC,SAAS,GAAC,UAAU,GAAC,IAAI,EACjC,IAAI,CAAC,EAAC;IAEF,IAAI,CAAC,EAAC,GAAG,GAAC,GAAG,CAAA;IAEb,IAAI,CAAC,EAAC,UAAU,GAAC,MAAM,CAAA;CAC1B,GACH,OAAO,CAAC;IAAE,GAAG,EAAC,UAAU,CAAC;IAAC,GAAG,EAAC,SAAS,CAAA;CAAE,CAAC,CA0B3C"}
package/dist/index.js CHANGED
@@ -1342,17 +1342,7 @@ async function aeadOpen(key, nonce, ciphertext) {
1342
1342
  __name(aeadOpen, "aeadOpen");
1343
1343
  async function encryptBytes(recipient, message, aesKey, opts) {
1344
1344
  const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
1345
- let enc;
1346
- let key;
1347
- if (!aesKey) {
1348
- const keys = await create2(recipient, opts);
1349
- enc = keys.enc;
1350
- key = keys.key;
1351
- } else {
1352
- const keys = await seal(recipient, aesKey, opts);
1353
- enc = keys.enc;
1354
- key = keys.key;
1355
- }
1345
+ const { enc, key } = await encryptKey(recipient, aesKey, opts);
1356
1346
  const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
1357
1347
  const ct = new Uint8Array(await subtle.encrypt(
1358
1348
  { name: "AES-GCM", iv },
@@ -1561,15 +1551,10 @@ __name(concat2, "concat");
1561
1551
  // src/index.ts
1562
1552
  var subtle2 = globalThis.crypto.subtle;
1563
1553
  async function create2(recipient, opts) {
1564
- const { wrapped, key } = await encryptKey(recipient, null, opts);
1565
- return { enc: wrapped, key };
1554
+ const { enc, key } = await encryptKey(recipient, null, opts);
1555
+ return { enc, key };
1566
1556
  }
1567
1557
  __name(create2, "create");
1568
- async function seal(recipient, aesKey, opts) {
1569
- const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
1570
- return { enc: wrapped, key };
1571
- }
1572
- __name(seal, "seal");
1573
1558
  var open = Object.assign(openBytes, {
1574
1559
  raw: openRawBytes
1575
1560
  });
@@ -1750,14 +1735,14 @@ async function encryptKey(recipient, aesKey, opts) {
1750
1735
  const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
1751
1736
  const wrapped = concat2(enc, ciphertext);
1752
1737
  const aesGcmKey = await importAesKey(keyBytes);
1753
- return { wrapped, key: aesGcmKey };
1738
+ return { enc: wrapped, key: aesGcmKey };
1754
1739
  }
1755
1740
  __name(encryptKey, "encryptKey");
1756
1741
  export {
1757
1742
  create2 as create,
1758
1743
  decrypt,
1759
1744
  encrypt,
1760
- open,
1761
- seal
1745
+ encryptKey,
1746
+ open
1762
1747
  };
1763
1748
  //# sourceMappingURL=index.js.map