simple-hpke 0.0.11 → 0.1.1
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 +35 -29
- 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/constants.js
CHANGED
|
@@ -1285,51 +1285,19 @@ __name(toString2, "toString");
|
|
|
1285
1285
|
|
|
1286
1286
|
// src/index.ts
|
|
1287
1287
|
var subtle = globalThis.crypto.subtle;
|
|
1288
|
+
async function create2(recipient, opts) {
|
|
1289
|
+
const { wrapped, key } = await encryptKey(recipient, null, opts);
|
|
1290
|
+
return { enc: wrapped, key };
|
|
1291
|
+
}
|
|
1292
|
+
__name(create2, "create");
|
|
1288
1293
|
async function seal(recipient, aesKey, opts) {
|
|
1289
|
-
const
|
|
1290
|
-
|
|
1291
|
-
if (aesKey instanceof Uint8Array) {
|
|
1292
|
-
validateRawKeyBytes(aesKey);
|
|
1293
|
-
keyBytes = aesKey;
|
|
1294
|
-
} else if (aesKey) {
|
|
1295
|
-
keyBytes = await exportAesKeyBytes(aesKey);
|
|
1296
|
-
validateRawKeyBytes(keyBytes);
|
|
1297
|
-
} else {
|
|
1298
|
-
const keysize = opts?.keysize ?? 256;
|
|
1299
|
-
validateKeysize(keysize);
|
|
1300
|
-
keyBytes = globalThis.crypto.getRandomValues(
|
|
1301
|
-
new Uint8Array(keysize / 8)
|
|
1302
|
-
);
|
|
1303
|
-
}
|
|
1304
|
-
const publicKey = await resolveRecipientPublicKey(recipient);
|
|
1305
|
-
const { sharedSecret, enc } = await encap(publicKey);
|
|
1306
|
-
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1307
|
-
const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
|
|
1308
|
-
const wrapped = concat2(enc, ciphertext);
|
|
1309
|
-
const aesGcmKey = await importAesKey(keyBytes);
|
|
1310
|
-
return { wrapped, key: aesGcmKey };
|
|
1294
|
+
const { wrapped, key } = await encryptKey(recipient, aesKey, opts);
|
|
1295
|
+
return { enc: wrapped, key };
|
|
1311
1296
|
}
|
|
1312
1297
|
__name(seal, "seal");
|
|
1313
1298
|
var open = Object.assign(openBytes, {
|
|
1314
1299
|
raw: openRawBytes
|
|
1315
1300
|
});
|
|
1316
|
-
async function openBytes(keypair, wrapped, opts) {
|
|
1317
|
-
const keyBytes = await openRawBytes(keypair, wrapped, opts);
|
|
1318
|
-
return importAesKey(keyBytes);
|
|
1319
|
-
}
|
|
1320
|
-
__name(openBytes, "openBytes");
|
|
1321
|
-
async function openRawBytes(keypair, wrapped, opts) {
|
|
1322
|
-
if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
|
|
1323
|
-
throw new Error("malformed envelope: too short");
|
|
1324
|
-
}
|
|
1325
|
-
const info = normalizeInfo(opts?.info);
|
|
1326
|
-
const enc = wrapped.slice(0, ENC_LENGTH);
|
|
1327
|
-
const ciphertext = wrapped.slice(ENC_LENGTH);
|
|
1328
|
-
const sharedSecret = await decap(enc, keypair);
|
|
1329
|
-
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1330
|
-
return aeadOpen(key, baseNonce, ciphertext);
|
|
1331
|
-
}
|
|
1332
|
-
__name(openRawBytes, "openRawBytes");
|
|
1333
1301
|
var encrypt = Object.assign(encryptBytes, {
|
|
1334
1302
|
asString: encryptToString
|
|
1335
1303
|
});
|
|
@@ -1468,6 +1436,48 @@ async function keySchedule(sharedSecret, info) {
|
|
|
1468
1436
|
return { key, baseNonce };
|
|
1469
1437
|
}
|
|
1470
1438
|
__name(keySchedule, "keySchedule");
|
|
1439
|
+
async function openRawBytes(keypair, wrapped, opts) {
|
|
1440
|
+
if (wrapped.byteLength < ENC_LENGTH + AEAD_TAG_LENGTH) {
|
|
1441
|
+
throw new Error("malformed envelope: too short");
|
|
1442
|
+
}
|
|
1443
|
+
const info = normalizeInfo(opts?.info);
|
|
1444
|
+
const enc = wrapped.slice(0, ENC_LENGTH);
|
|
1445
|
+
const ciphertext = wrapped.slice(ENC_LENGTH);
|
|
1446
|
+
const sharedSecret = await decap(enc, keypair);
|
|
1447
|
+
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1448
|
+
return aeadOpen(key, baseNonce, ciphertext);
|
|
1449
|
+
}
|
|
1450
|
+
__name(openRawBytes, "openRawBytes");
|
|
1451
|
+
async function openBytes(keypair, wrapped, opts) {
|
|
1452
|
+
const keyBytes = await openRawBytes(keypair, wrapped, opts);
|
|
1453
|
+
return importAesKey(keyBytes);
|
|
1454
|
+
}
|
|
1455
|
+
__name(openBytes, "openBytes");
|
|
1456
|
+
async function encryptKey(recipient, aesKey, opts) {
|
|
1457
|
+
const info = normalizeInfo(opts?.info);
|
|
1458
|
+
let keyBytes;
|
|
1459
|
+
if (aesKey instanceof Uint8Array) {
|
|
1460
|
+
validateRawKeyBytes(aesKey);
|
|
1461
|
+
keyBytes = aesKey;
|
|
1462
|
+
} else if (aesKey) {
|
|
1463
|
+
keyBytes = await exportAesKeyBytes(aesKey);
|
|
1464
|
+
validateRawKeyBytes(keyBytes);
|
|
1465
|
+
} else {
|
|
1466
|
+
const keysize = opts?.size ?? 256;
|
|
1467
|
+
validateKeysize(keysize);
|
|
1468
|
+
keyBytes = globalThis.crypto.getRandomValues(
|
|
1469
|
+
new Uint8Array(keysize / 8)
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
const publicKey = await resolveRecipientPublicKey(recipient);
|
|
1473
|
+
const { sharedSecret, enc } = await encap(publicKey);
|
|
1474
|
+
const { key, baseNonce } = await keySchedule(sharedSecret, info);
|
|
1475
|
+
const ciphertext = await aeadSeal(key, baseNonce, keyBytes);
|
|
1476
|
+
const wrapped = concat2(enc, ciphertext);
|
|
1477
|
+
const aesGcmKey = await importAesKey(keyBytes);
|
|
1478
|
+
return { wrapped, key: aesGcmKey };
|
|
1479
|
+
}
|
|
1480
|
+
__name(encryptKey, "encryptKey");
|
|
1471
1481
|
|
|
1472
1482
|
// src/util.ts
|
|
1473
1483
|
var subtle2 = globalThis.crypto.subtle;
|
|
@@ -1505,14 +1515,24 @@ async function aeadOpen(key, nonce, ciphertext) {
|
|
|
1505
1515
|
__name(aeadOpen, "aeadOpen");
|
|
1506
1516
|
async function encryptBytes(recipient, message, aesKey, opts) {
|
|
1507
1517
|
const plaintext = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
1508
|
-
|
|
1518
|
+
let enc;
|
|
1519
|
+
let key;
|
|
1520
|
+
if (!aesKey) {
|
|
1521
|
+
const keys = await create2(recipient, opts);
|
|
1522
|
+
enc = keys.enc;
|
|
1523
|
+
key = keys.key;
|
|
1524
|
+
} else {
|
|
1525
|
+
const keys = await seal(recipient, aesKey, opts);
|
|
1526
|
+
enc = keys.enc;
|
|
1527
|
+
key = keys.key;
|
|
1528
|
+
}
|
|
1509
1529
|
const iv = globalThis.crypto.getRandomValues(new Uint8Array(NN));
|
|
1510
1530
|
const ct = new Uint8Array(await subtle2.encrypt(
|
|
1511
1531
|
{ name: "AES-GCM", iv },
|
|
1512
1532
|
key,
|
|
1513
1533
|
plaintext
|
|
1514
1534
|
));
|
|
1515
|
-
return concat2(i2osp(
|
|
1535
|
+
return concat2(i2osp(enc.length, WRAPPED_LEN_PREFIX), enc, iv, ct);
|
|
1516
1536
|
}
|
|
1517
1537
|
__name(encryptBytes, "encryptBytes");
|
|
1518
1538
|
async function encryptToString(recipient, message, aesKey, opts) {
|