simple-hpke 0.0.11 → 0.1.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.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ create: () => create2,
24
25
  decrypt: () => decrypt,
25
26
  encrypt: () => encrypt,
26
27
  open: () => open,
@@ -1365,14 +1366,24 @@ async function aeadOpen(key, nonce, ciphertext) {
1365
1366
  __name(aeadOpen, "aeadOpen");
1366
1367
  async function encryptBytes(recipient, message, aesKey, opts) {
1367
1368
  const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
1368
- const { wrapped, key } = await seal(recipient, aesKey, opts);
1369
+ let enc;
1370
+ let key;
1371
+ if (!aesKey) {
1372
+ const keys = await create2(recipient, opts);
1373
+ enc = keys.enc;
1374
+ key = keys.key;
1375
+ } else {
1376
+ const keys = await seal(recipient, aesKey, opts);
1377
+ enc = keys.enc;
1378
+ key = keys.key;
1379
+ }
1369
1380
  const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
1370
1381
  const ct = new Uint8Array(await subtle.encrypt(
1371
1382
  { name: "AES-GCM", iv },
1372
1383
  key,
1373
1384
  plaintext
1374
1385
  ));
1375
- return concat2(i2osp(wrapped.length, WRAPPED_LEN_PREFIX), wrapped, iv, ct);
1386
+ return concat2(i2osp(enc.length, WRAPPED_LEN_PREFIX), enc, iv, ct);
1376
1387
  }
1377
1388
  __name(encryptBytes, "encryptBytes");
1378
1389
  async function encryptToString(recipient, message, aesKey, opts) {
@@ -1573,51 +1584,19 @@ __name(concat2, "concat");
1573
1584
 
1574
1585
  // src/index.ts
1575
1586
  var subtle2 = globalThis.crypto.subtle;
1587
+ async function create2(recipient, opts) {
1588
+ const { wrapped, key } = await encryptKey(recipient, null, opts);
1589
+ return { enc: wrapped, key };
1590
+ }
1591
+ __name(create2, "create");
1576
1592
  async function seal(recipient, aesKey, opts) {
1577
- const info = normalizeInfo(opts?.info);
1578
- let keyBytes;
1579
- if (aesKey instanceof Uint8Array) {
1580
- validateRawKeyBytes(aesKey);
1581
- keyBytes = aesKey;
1582
- } else if (aesKey) {
1583
- keyBytes = await exportAesKeyBytes(aesKey);
1584
- validateRawKeyBytes(keyBytes);
1585
- } else {
1586
- const keysize = opts?.keysize ?? 256;
1587
- validateKeysize(keysize);
1588
- keyBytes = globalThis.crypto.getRandomValues(
1589
- new Uint8Array(keysize / 8)
1590
- );
1591
- }
1592
- const publicKey = await resolveRecipientPublicKey(recipient);
1593
- const { sharedSecret, enc } = await encap(publicKey);
1594
- const { key, baseNonce } = await keySchedule(sharedSecret, info);
1595
- const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
1596
- const wrapped = concat2(enc, ciphertext);
1597
- const aesGcmKey = await importAesKey(keyBytes);
1598
- return { wrapped, key: aesGcmKey };
1593
+ const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
1594
+ return { enc: wrapped, key };
1599
1595
  }
1600
1596
  __name(seal, "seal");
1601
1597
  var open = Object.assign(openBytes, {
1602
1598
  raw: openRawBytes
1603
1599
  });
1604
- async function openBytes(keypair, wrapped, opts) {
1605
- const keyBytes = await openRawBytes(keypair, wrapped, opts);
1606
- return importAesKey(keyBytes);
1607
- }
1608
- __name(openBytes, "openBytes");
1609
- async function openRawBytes(keypair, wrapped, opts) {
1610
- if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
1611
- throw new Error("malformed envelope: too short");
1612
- }
1613
- const info = normalizeInfo(opts?.info);
1614
- const enc = wrapped.slice(0, ENC_LENGTH);
1615
- const ciphertext = wrapped.slice(ENC_LENGTH);
1616
- const sharedSecret = await decap(enc, keypair);
1617
- const { key, baseNonce } = await keySchedule(sharedSecret, info);
1618
- return aeadOpen(key, baseNonce, ciphertext);
1619
- }
1620
- __name(openRawBytes, "openRawBytes");
1621
1600
  var encrypt = Object.assign(encryptBytes, {
1622
1601
  asString: encryptToString
1623
1602
  });
@@ -1756,4 +1735,46 @@ async function keySchedule(sharedSecret, info) {
1756
1735
  return { key, baseNonce };
1757
1736
  }
1758
1737
  __name(keySchedule, "keySchedule");
1738
+ async function openRawBytes(keypair, wrapped, opts) {
1739
+ if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
1740
+ throw new Error("malformed envelope: too short");
1741
+ }
1742
+ const info = normalizeInfo(opts?.info);
1743
+ const enc = wrapped.slice(0, ENC_LENGTH);
1744
+ const ciphertext = wrapped.slice(ENC_LENGTH);
1745
+ const sharedSecret = await decap(enc, keypair);
1746
+ const { key, baseNonce } = await keySchedule(sharedSecret, info);
1747
+ return aeadOpen(key, baseNonce, ciphertext);
1748
+ }
1749
+ __name(openRawBytes, "openRawBytes");
1750
+ async function openBytes(keypair, wrapped, opts) {
1751
+ const keyBytes = await openRawBytes(keypair, wrapped, opts);
1752
+ return importAesKey(keyBytes);
1753
+ }
1754
+ __name(openBytes, "openBytes");
1755
+ async function encryptKey(recipient, aesKey, opts) {
1756
+ const info = normalizeInfo(opts?.info);
1757
+ let keyBytes;
1758
+ if (aesKey instanceof Uint8Array) {
1759
+ validateRawKeyBytes(aesKey);
1760
+ keyBytes = aesKey;
1761
+ } else if (aesKey) {
1762
+ keyBytes = await exportAesKeyBytes(aesKey);
1763
+ validateRawKeyBytes(keyBytes);
1764
+ } else {
1765
+ const keysize = opts?.size ?? 256;
1766
+ validateKeysize(keysize);
1767
+ keyBytes = globalThis.crypto.getRandomValues(
1768
+ new Uint8Array(keysize / 8)
1769
+ );
1770
+ }
1771
+ const publicKey = await resolveRecipientPublicKey(recipient);
1772
+ const { sharedSecret, enc } = await encap(publicKey);
1773
+ const { key, baseNonce } = await keySchedule(sharedSecret, info);
1774
+ const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
1775
+ const wrapped = concat2(enc, ciphertext);
1776
+ const aesGcmKey = await importAesKey(keyBytes);
1777
+ return { wrapped, key: aesGcmKey };
1778
+ }
1779
+ __name(encryptKey, "encryptKey");
1759
1780
  //# sourceMappingURL=index.cjs.map