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/meta.json CHANGED
@@ -691,7 +691,7 @@
691
691
  "format": "esm"
692
692
  },
693
693
  "src/index.ts": {
694
- "bytes": 11805,
694
+ "bytes": 13195,
695
695
  "imports": [
696
696
  {
697
697
  "path": "node_modules/uint8arrays/dist/src/index.js",
@@ -717,7 +717,7 @@
717
717
  "format": "esm"
718
718
  },
719
719
  "src/util.ts": {
720
- "bytes": 13984,
720
+ "bytes": 14284,
721
721
  "imports": [
722
722
  {
723
723
  "path": "node_modules/uint8arrays/dist/src/index.js",
@@ -759,7 +759,7 @@
759
759
  "imports": [],
760
760
  "exports": [],
761
761
  "inputs": {},
762
- "bytes": 112099
762
+ "bytes": 114273
763
763
  },
764
764
  "dist/constants.js": {
765
765
  "imports": [],
@@ -865,26 +865,27 @@
865
865
  "bytesInOutput": 258
866
866
  },
867
867
  "src/index.ts": {
868
- "bytesInOutput": 5466
868
+ "bytesInOutput": 5825
869
869
  },
870
870
  "src/util.ts": {
871
- "bytesInOutput": 6624
871
+ "bytesInOutput": 6789
872
872
  },
873
873
  "src/constants.ts": {
874
874
  "bytesInOutput": 452
875
875
  }
876
876
  },
877
- "bytes": 50973
877
+ "bytes": 51497
878
878
  },
879
879
  "dist/index.js.map": {
880
880
  "imports": [],
881
881
  "exports": [],
882
882
  "inputs": {},
883
- "bytes": 112090
883
+ "bytes": 114264
884
884
  },
885
885
  "dist/index.js": {
886
886
  "imports": [],
887
887
  "exports": [
888
+ "create",
888
889
  "decrypt",
889
890
  "encrypt",
890
891
  "open",
@@ -980,19 +981,19 @@
980
981
  "bytesInOutput": 452
981
982
  },
982
983
  "src/util.ts": {
983
- "bytesInOutput": 6611
984
+ "bytesInOutput": 6776
984
985
  },
985
986
  "src/index.ts": {
986
- "bytesInOutput": 5471
987
+ "bytesInOutput": 5830
987
988
  }
988
989
  },
989
- "bytes": 50833
990
+ "bytes": 51378
990
991
  },
991
992
  "dist/util.js.map": {
992
993
  "imports": [],
993
994
  "exports": [],
994
995
  "inputs": {},
995
- "bytes": 112096
996
+ "bytes": 114270
996
997
  },
997
998
  "dist/util.js": {
998
999
  "imports": [],
@@ -1100,13 +1101,13 @@
1100
1101
  "bytesInOutput": 452
1101
1102
  },
1102
1103
  "src/index.ts": {
1103
- "bytesInOutput": 5466
1104
+ "bytesInOutput": 5825
1104
1105
  },
1105
1106
  "src/util.ts": {
1106
- "bytesInOutput": 6624
1107
+ "bytesInOutput": 6789
1107
1108
  }
1108
1109
  },
1109
- "bytes": 50981
1110
+ "bytes": 51505
1110
1111
  }
1111
1112
  }
1112
1113
  }
package/dist/util.cjs CHANGED
@@ -1339,51 +1339,19 @@ var HPKE_SUITE_ID = concat2(
1339
1339
 
1340
1340
  // src/index.ts
1341
1341
  var subtle = globalThis.crypto.subtle;
1342
+ async function create2(recipient, opts) {
1343
+ const { wrapped, key } = await encryptKey(recipient, null, opts);
1344
+ return { enc: wrapped, key };
1345
+ }
1346
+ __name(create2, "create");
1342
1347
  async function seal(recipient, aesKey, opts) {
1343
- const info = normalizeInfo(opts?.info);
1344
- let keyBytes;
1345
- if (aesKey instanceof Uint8Array) {
1346
- validateRawKeyBytes(aesKey);
1347
- keyBytes = aesKey;
1348
- } else if (aesKey) {
1349
- keyBytes = await exportAesKeyBytes(aesKey);
1350
- validateRawKeyBytes(keyBytes);
1351
- } else {
1352
- const keysize = opts?.keysize ?? 256;
1353
- validateKeysize(keysize);
1354
- keyBytes = globalThis.crypto.getRandomValues(
1355
- new Uint8Array(keysize / 8)
1356
- );
1357
- }
1358
- const publicKey = await resolveRecipientPublicKey(recipient);
1359
- const { sharedSecret, enc } = await encap(publicKey);
1360
- const { key, baseNonce } = await keySchedule(sharedSecret, info);
1361
- const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
1362
- const wrapped = concat2(enc, ciphertext);
1363
- const aesGcmKey = await importAesKey(keyBytes);
1364
- return { wrapped, key: aesGcmKey };
1348
+ const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
1349
+ return { enc: wrapped, key };
1365
1350
  }
1366
1351
  __name(seal, "seal");
1367
1352
  var open = Object.assign(openBytes, {
1368
1353
  raw: openRawBytes
1369
1354
  });
1370
- async function openBytes(keypair, wrapped, opts) {
1371
- const keyBytes = await openRawBytes(keypair, wrapped, opts);
1372
- return importAesKey(keyBytes);
1373
- }
1374
- __name(openBytes, "openBytes");
1375
- async function openRawBytes(keypair, wrapped, opts) {
1376
- if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
1377
- throw new Error("malformed envelope: too short");
1378
- }
1379
- const info = normalizeInfo(opts?.info);
1380
- const enc = wrapped.slice(0, ENC_LENGTH);
1381
- const ciphertext = wrapped.slice(ENC_LENGTH);
1382
- const sharedSecret = await decap(enc, keypair);
1383
- const { key, baseNonce } = await keySchedule(sharedSecret, info);
1384
- return aeadOpen(key, baseNonce, ciphertext);
1385
- }
1386
- __name(openRawBytes, "openRawBytes");
1387
1355
  var encrypt = Object.assign(encryptBytes, {
1388
1356
  asString: encryptToString
1389
1357
  });
@@ -1522,6 +1490,48 @@ async function keySchedule(sharedSecret, info) {
1522
1490
  return { key, baseNonce };
1523
1491
  }
1524
1492
  __name(keySchedule, "keySchedule");
1493
+ async function openRawBytes(keypair, wrapped, opts) {
1494
+ if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
1495
+ throw new Error("malformed envelope: too short");
1496
+ }
1497
+ const info = normalizeInfo(opts?.info);
1498
+ const enc = wrapped.slice(0, ENC_LENGTH);
1499
+ const ciphertext = wrapped.slice(ENC_LENGTH);
1500
+ const sharedSecret = await decap(enc, keypair);
1501
+ const { key, baseNonce } = await keySchedule(sharedSecret, info);
1502
+ return aeadOpen(key, baseNonce, ciphertext);
1503
+ }
1504
+ __name(openRawBytes, "openRawBytes");
1505
+ async function openBytes(keypair, wrapped, opts) {
1506
+ const keyBytes = await openRawBytes(keypair, wrapped, opts);
1507
+ return importAesKey(keyBytes);
1508
+ }
1509
+ __name(openBytes, "openBytes");
1510
+ async function encryptKey(recipient, aesKey, opts) {
1511
+ const info = normalizeInfo(opts?.info);
1512
+ let keyBytes;
1513
+ if (aesKey instanceof Uint8Array) {
1514
+ validateRawKeyBytes(aesKey);
1515
+ keyBytes = aesKey;
1516
+ } else if (aesKey) {
1517
+ keyBytes = await exportAesKeyBytes(aesKey);
1518
+ validateRawKeyBytes(keyBytes);
1519
+ } else {
1520
+ const keysize = opts?.size ?? 256;
1521
+ validateKeysize(keysize);
1522
+ keyBytes = globalThis.crypto.getRandomValues(
1523
+ new Uint8Array(keysize / 8)
1524
+ );
1525
+ }
1526
+ const publicKey = await resolveRecipientPublicKey(recipient);
1527
+ const { sharedSecret, enc } = await encap(publicKey);
1528
+ const { key, baseNonce } = await keySchedule(sharedSecret, info);
1529
+ const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
1530
+ const wrapped = concat2(enc, ciphertext);
1531
+ const aesGcmKey = await importAesKey(keyBytes);
1532
+ return { wrapped, key: aesGcmKey };
1533
+ }
1534
+ __name(encryptKey, "encryptKey");
1525
1535
 
1526
1536
  // src/util.ts
1527
1537
  var subtle2 = globalThis.crypto.subtle;
@@ -1559,14 +1569,24 @@ async function aeadOpen(key, nonce, ciphertext) {
1559
1569
  __name(aeadOpen, "aeadOpen");
1560
1570
  async function encryptBytes(recipient, message, aesKey, opts) {
1561
1571
  const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
1562
- const { wrapped, key } = await seal(recipient, aesKey, opts);
1572
+ let enc;
1573
+ let key;
1574
+ if (!aesKey) {
1575
+ const keys = await create2(recipient, opts);
1576
+ enc = keys.enc;
1577
+ key = keys.key;
1578
+ } else {
1579
+ const keys = await seal(recipient, aesKey, opts);
1580
+ enc = keys.enc;
1581
+ key = keys.key;
1582
+ }
1563
1583
  const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
1564
1584
  const ct = new Uint8Array(await subtle2.encrypt(
1565
1585
  { name: "AES-GCM", iv },
1566
1586
  key,
1567
1587
  plaintext
1568
1588
  ));
1569
- return concat2(i2osp(wrapped.length, WRAPPED_LEN_PREFIX), wrapped, iv, ct);
1589
+ return concat2(i2osp(enc.length, WRAPPED_LEN_PREFIX), enc, iv, ct);
1570
1590
  }
1571
1591
  __name(encryptBytes, "encryptBytes");
1572
1592
  async function encryptToString(recipient, message, aesKey, opts) {