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/README.md +26 -25
- package/dist/constants.cjs +61 -41
- package/dist/constants.cjs.map +3 -3
- package/dist/constants.js +61 -41
- package/dist/constants.js.map +3 -3
- package/dist/constants.min.cjs +1 -1
- package/dist/constants.min.cjs.map +3 -3
- package/dist/constants.min.js +1 -1
- package/dist/constants.min.js.map +3 -3
- package/dist/index.cjs +62 -41
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +46 -41
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -41
- package/dist/index.js.map +3 -3
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +3 -3
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +3 -3
- package/dist/meta.json +15 -14
- package/dist/util.cjs +61 -41
- package/dist/util.cjs.map +3 -3
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +61 -41
- package/dist/util.js.map +3 -3
- package/dist/util.min.cjs +1 -1
- package/dist/util.min.cjs.map +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +1 -1
package/dist/util.js
CHANGED
|
@@ -1308,51 +1308,19 @@ 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");
|
|
1311
1316
|
async function seal(recipient, aesKey, opts) {
|
|
1312
|
-
const
|
|
1313
|
-
|
|
1314
|
-
if (aesKey instanceof Uint8Array) {
|
|
1315
|
-
validateRawKeyBytes(aesKey);
|
|
1316
|
-
keyBytes = aesKey;
|
|
1317
|
-
} else if (aesKey) {
|
|
1318
|
-
keyBytes = await exportAesKeyBytes(aesKey);
|
|
1319
|
-
validateRawKeyBytes(keyBytes);
|
|
1320
|
-
} else {
|
|
1321
|
-
const keysize = opts?.keysize ?? 256;
|
|
1322
|
-
validateKeysize(keysize);
|
|
1323
|
-
keyBytes = globalThis.crypto.getRandomValues(
|
|
1324
|
-
new Uint8Array(keysize / 8)
|
|
1325
|
-
);
|
|
1326
|
-
}
|
|
1327
|
-
const publicKey = await resolveRecipientPublicKey(recipient);
|
|
1328
|
-
const { sharedSecret, enc } = await encap(publicKey);
|
|
1329
|
-
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1330
|
-
const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
|
|
1331
|
-
const wrapped = concat2(enc, ciphertext);
|
|
1332
|
-
const aesGcmKey = await importAesKey(keyBytes);
|
|
1333
|
-
return { wrapped, key: aesGcmKey };
|
|
1317
|
+
const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
|
|
1318
|
+
return { enc: wrapped, key };
|
|
1334
1319
|
}
|
|
1335
1320
|
__name(seal, "seal");
|
|
1336
1321
|
var open = Object.assign(openBytes, {
|
|
1337
1322
|
raw: openRawBytes
|
|
1338
1323
|
});
|
|
1339
|
-
async function openBytes(keypair, wrapped, opts) {
|
|
1340
|
-
const keyBytes = await openRawBytes(keypair, wrapped, opts);
|
|
1341
|
-
return importAesKey(keyBytes);
|
|
1342
|
-
}
|
|
1343
|
-
__name(openBytes, "openBytes");
|
|
1344
|
-
async function openRawBytes(keypair, wrapped, opts) {
|
|
1345
|
-
if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
|
|
1346
|
-
throw new Error("malformed envelope: too short");
|
|
1347
|
-
}
|
|
1348
|
-
const info = normalizeInfo(opts?.info);
|
|
1349
|
-
const enc = wrapped.slice(0, ENC_LENGTH);
|
|
1350
|
-
const ciphertext = wrapped.slice(ENC_LENGTH);
|
|
1351
|
-
const sharedSecret = await decap(enc, keypair);
|
|
1352
|
-
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1353
|
-
return aeadOpen(key, baseNonce, ciphertext);
|
|
1354
|
-
}
|
|
1355
|
-
__name(openRawBytes, "openRawBytes");
|
|
1356
1324
|
var encrypt = Object.assign(encryptBytes, {
|
|
1357
1325
|
asString: encryptToString
|
|
1358
1326
|
});
|
|
@@ -1491,6 +1459,48 @@ async function keySchedule(sharedSecret, info) {
|
|
|
1491
1459
|
return { key, baseNonce };
|
|
1492
1460
|
}
|
|
1493
1461
|
__name(keySchedule, "keySchedule");
|
|
1462
|
+
async function openRawBytes(keypair, wrapped, opts) {
|
|
1463
|
+
if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
|
|
1464
|
+
throw new Error("malformed envelope: too short");
|
|
1465
|
+
}
|
|
1466
|
+
const info = normalizeInfo(opts?.info);
|
|
1467
|
+
const enc = wrapped.slice(0, ENC_LENGTH);
|
|
1468
|
+
const ciphertext = wrapped.slice(ENC_LENGTH);
|
|
1469
|
+
const sharedSecret = await decap(enc, keypair);
|
|
1470
|
+
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1471
|
+
return aeadOpen(key, baseNonce, ciphertext);
|
|
1472
|
+
}
|
|
1473
|
+
__name(openRawBytes, "openRawBytes");
|
|
1474
|
+
async function openBytes(keypair, wrapped, opts) {
|
|
1475
|
+
const keyBytes = await openRawBytes(keypair, wrapped, opts);
|
|
1476
|
+
return importAesKey(keyBytes);
|
|
1477
|
+
}
|
|
1478
|
+
__name(openBytes, "openBytes");
|
|
1479
|
+
async function encryptKey(recipient, aesKey, opts) {
|
|
1480
|
+
const info = normalizeInfo(opts?.info);
|
|
1481
|
+
let keyBytes;
|
|
1482
|
+
if (aesKey instanceof Uint8Array) {
|
|
1483
|
+
validateRawKeyBytes(aesKey);
|
|
1484
|
+
keyBytes = aesKey;
|
|
1485
|
+
} else if (aesKey) {
|
|
1486
|
+
keyBytes = await exportAesKeyBytes(aesKey);
|
|
1487
|
+
validateRawKeyBytes(keyBytes);
|
|
1488
|
+
} else {
|
|
1489
|
+
const keysize = opts?.size ?? 256;
|
|
1490
|
+
validateKeysize(keysize);
|
|
1491
|
+
keyBytes = globalThis.crypto.getRandomValues(
|
|
1492
|
+
new Uint8Array(keysize / 8)
|
|
1493
|
+
);
|
|
1494
|
+
}
|
|
1495
|
+
const publicKey = await resolveRecipientPublicKey(recipient);
|
|
1496
|
+
const { sharedSecret, enc } = await encap(publicKey);
|
|
1497
|
+
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1498
|
+
const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
|
|
1499
|
+
const wrapped = concat2(enc, ciphertext);
|
|
1500
|
+
const aesGcmKey = await importAesKey(keyBytes);
|
|
1501
|
+
return { wrapped, key: aesGcmKey };
|
|
1502
|
+
}
|
|
1503
|
+
__name(encryptKey, "encryptKey");
|
|
1494
1504
|
|
|
1495
1505
|
// src/util.ts
|
|
1496
1506
|
var subtle2 = globalThis.crypto.subtle;
|
|
@@ -1528,14 +1538,24 @@ async function aeadOpen(key, nonce, ciphertext) {
|
|
|
1528
1538
|
__name(aeadOpen, "aeadOpen");
|
|
1529
1539
|
async function encryptBytes(recipient, message, aesKey, opts) {
|
|
1530
1540
|
const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
1531
|
-
|
|
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
|
+
}
|
|
1532
1552
|
const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
|
|
1533
1553
|
const ct = new Uint8Array(await subtle2.encrypt(
|
|
1534
1554
|
{ name: "AES-GCM", iv },
|
|
1535
1555
|
key,
|
|
1536
1556
|
plaintext
|
|
1537
1557
|
));
|
|
1538
|
-
return concat2(i2osp(
|
|
1558
|
+
return concat2(i2osp(enc.length, WRAPPED_LEN_PREFIX), enc, iv, ct);
|
|
1539
1559
|
}
|
|
1540
1560
|
__name(encryptBytes, "encryptBytes");
|
|
1541
1561
|
async function encryptToString(recipient, message, aesKey, opts) {
|