@sebastienrousseau/crypto-lib 0.0.35 → 0.0.36
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 +17 -2
- package/dist/accel/index.d.ts +5 -4
- package/dist/accel/index.d.ts.map +1 -1
- package/dist/accel/index.js +5 -5
- package/dist/accel/kdf-backend.d.ts +13 -0
- package/dist/accel/kdf-backend.d.ts.map +1 -0
- package/dist/accel/kdf-backend.js +81 -0
- package/dist/accel/native-pqc-bridge.d.ts.map +1 -1
- package/dist/accel/native-pqc-bridge.js +12 -52
- package/dist/accel/pq-backend.d.ts +3652 -0
- package/dist/accel/pq-backend.d.ts.map +1 -0
- package/dist/accel/pq-backend.js +215 -0
- package/dist/accel/runtime.d.ts +3 -0
- package/dist/accel/runtime.d.ts.map +1 -0
- package/dist/accel/runtime.js +9 -0
- package/dist/accel/wasm-bridge.d.ts.map +1 -1
- package/dist/accel/wasm-bridge.js +14 -13
- package/dist/accel/webcrypto-bridge.d.ts.map +1 -1
- package/dist/accel/webcrypto-bridge.js +14 -13
- package/dist/accel/webcrypto-modern.d.ts.map +1 -1
- package/dist/accel/webcrypto-modern.js +15 -14
- package/dist/accel/worker-pool.d.ts +18 -0
- package/dist/accel/worker-pool.d.ts.map +1 -1
- package/dist/accel/worker-pool.js +113 -37
- package/dist/bin/cryptolib.d.ts +7 -4
- package/dist/bin/cryptolib.d.ts.map +1 -1
- package/dist/bin/cryptolib.js +6 -6
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +2 -1
- package/dist/encoding.d.ts +10 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +84 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +20 -1
- package/dist/high-level/key-wrap.d.ts.map +1 -1
- package/dist/high-level/key-wrap.js +12 -17
- package/dist/high-level/multi-recipient.d.ts.map +1 -1
- package/dist/high-level/multi-recipient.js +15 -28
- package/dist/high-level/password-encrypt.d.ts.map +1 -1
- package/dist/high-level/password-encrypt.js +11 -11
- package/dist/high-level/sealedbox.d.ts.map +1 -1
- package/dist/high-level/sealedbox.js +16 -28
- package/dist/high-level/secretbox.d.ts.map +1 -1
- package/dist/high-level/secretbox.js +11 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/key/keystore.d.ts.map +1 -1
- package/dist/key/keystore.js +2 -1
- package/dist/keys/keygen.d.ts.map +1 -1
- package/dist/keys/keygen.js +13 -14
- package/dist/keys/keyring.d.ts.map +1 -1
- package/dist/keys/keyring.js +4 -2
- package/dist/keys/serialize.d.ts.map +1 -1
- package/dist/keys/serialize.js +18 -16
- package/dist/lib/decrypt.d.ts.map +1 -1
- package/dist/lib/decrypt.js +2 -1
- package/dist/lib/generate.d.ts +2 -1
- package/dist/lib/generate.d.ts.map +1 -1
- package/dist/lib/generate.js +7 -6
- package/dist/lib/key-output.d.ts +4 -1
- package/dist/lib/key-output.d.ts.map +1 -1
- package/dist/lib/key-output.js +2 -2
- package/dist/lib/reformat.d.ts +2 -1
- package/dist/lib/reformat.d.ts.map +1 -1
- package/dist/lib/reformat.js +2 -2
- package/dist/lib/revoke.d.ts +2 -1
- package/dist/lib/revoke.d.ts.map +1 -1
- package/dist/lib/revoke.js +2 -2
- package/dist/lib/verify.d.ts +6 -2
- package/dist/lib/verify.d.ts.map +1 -1
- package/dist/lib/verify.js +28 -9
- package/dist/modern/aead.d.ts.map +1 -1
- package/dist/modern/aead.js +11 -10
- package/dist/modern/aes.d.ts.map +1 -1
- package/dist/modern/aes.js +10 -9
- package/dist/modern/cost-limits.d.ts.map +1 -1
- package/dist/modern/cost-limits.js +3 -2
- package/dist/modern/curves.d.ts +10 -5
- package/dist/modern/curves.d.ts.map +1 -1
- package/dist/modern/curves.js +43 -36
- package/dist/modern/ecdh.d.ts +2 -1
- package/dist/modern/ecdh.d.ts.map +1 -1
- package/dist/modern/ecdh.js +10 -9
- package/dist/modern/fn-dsa.d.ts.map +1 -1
- package/dist/modern/fn-dsa.js +9 -8
- package/dist/modern/hash.d.ts.map +1 -1
- package/dist/modern/hash.js +5 -3
- package/dist/modern/hpke-xwing.d.ts +10 -0
- package/dist/modern/hpke-xwing.d.ts.map +1 -0
- package/dist/modern/hpke-xwing.js +26 -0
- package/dist/modern/hpke.d.ts +1 -1
- package/dist/modern/hpke.d.ts.map +1 -1
- package/dist/modern/hpke.js +60 -28
- package/dist/modern/hybrid-combiner.d.ts +20 -0
- package/dist/modern/hybrid-combiner.d.ts.map +1 -0
- package/dist/modern/hybrid-combiner.js +79 -0
- package/dist/modern/index.d.ts +10 -10
- package/dist/modern/index.d.ts.map +1 -1
- package/dist/modern/index.js +17 -7
- package/dist/modern/kdf.d.ts.map +1 -1
- package/dist/modern/kdf.js +12 -12
- package/dist/modern/mac.d.ts +1 -0
- package/dist/modern/mac.d.ts.map +1 -1
- package/dist/modern/mac.js +24 -15
- package/dist/modern/password.d.ts.map +1 -1
- package/dist/modern/password.js +19 -18
- package/dist/modern/pq-hash-sign.d.ts.map +1 -1
- package/dist/modern/pq-hash-sign.js +23 -22
- package/dist/modern/pq-kem.d.ts +8 -0
- package/dist/modern/pq-kem.d.ts.map +1 -1
- package/dist/modern/pq-kem.js +71 -79
- package/dist/modern/pq-sign.d.ts +9 -2
- package/dist/modern/pq-sign.d.ts.map +1 -1
- package/dist/modern/pq-sign.js +60 -23
- package/dist/modern/pq.d.ts +0 -17
- package/dist/modern/pq.d.ts.map +1 -1
- package/dist/modern/pq.js +15 -43
- package/dist/modern/signing.d.ts +2 -1
- package/dist/modern/signing.d.ts.map +1 -1
- package/dist/modern/signing.js +14 -13
- package/dist/protocols/iso20022.d.ts.map +1 -1
- package/dist/protocols/iso20022.js +12 -6
- package/dist/protocols/opaque/ksf.js +2 -2
- package/dist/protocols/opaque/suite.d.ts +1 -8
- package/dist/protocols/opaque/suite.d.ts.map +1 -1
- package/dist/protocols/opaque/suite.js +5 -10
- package/dist/protocols/pqxdh.d.ts +9 -2
- package/dist/protocols/pqxdh.d.ts.map +1 -1
- package/dist/protocols/pqxdh.js +99 -94
- package/dist/protocols/ratchet.d.ts.map +1 -1
- package/dist/protocols/ratchet.js +14 -15
- package/dist/protocols/threshold.d.ts.map +1 -1
- package/dist/protocols/threshold.js +16 -15
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +26 -0
- package/dist/streaming/chunks.d.ts +31 -0
- package/dist/streaming/chunks.d.ts.map +1 -1
- package/dist/streaming/chunks.js +149 -13
- package/dist/streaming/stream-aead.d.ts +9 -0
- package/dist/streaming/stream-aead.d.ts.map +1 -1
- package/dist/streaming/stream-aead.js +27 -53
- package/dist/streaming/stream-hash.d.ts.map +1 -1
- package/dist/streaming/stream-hash.js +6 -4
- package/dist/streaming/stream-multi-pq-aead.d.ts +12 -0
- package/dist/streaming/stream-multi-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-multi-pq-aead.js +82 -106
- package/dist/streaming/stream-pq-aead.d.ts +7 -2
- package/dist/streaming/stream-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-pq-aead.js +29 -79
- package/dist/streaming/web-streams.d.ts +5 -2
- package/dist/streaming/web-streams.d.ts.map +1 -1
- package/dist/streaming/web-streams.js +90 -151
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +13 -0
- package/dist/tokens/index.d.ts.map +1 -1
- package/dist/tokens/key-rotation.d.ts.map +1 -1
- package/dist/tokens/key-rotation.js +4 -2
- package/dist/tokens/paseto.d.ts +2 -0
- package/dist/tokens/paseto.d.ts.map +1 -1
- package/dist/tokens/paseto.js +56 -29
- package/dist/types/types.d.ts +1 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -2
- package/package.json +12 -2
- package/dist/config/config.d.ts +0 -8
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/config.js +0 -13
- package/dist/key/key.d.ts +0 -10
- package/dist/key/key.d.ts.map +0 -1
- package/dist/key/key.js +0 -25
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SLOT_FIXED_OVERHEAD = exports.WRAPPED_CEK_LEN = exports.CEK_LEN = exports.MULTI_PQ_PREFIX_LEN = exports.MULTI_PQ_STREAM_VERSION = exports.MULTI_PQ_STREAM_MAGIC = void 0;
|
|
4
4
|
exports.streamMultiPqEncrypt = streamMultiPqEncrypt;
|
|
5
|
+
exports.multiPqStreamSender = multiPqStreamSender;
|
|
6
|
+
exports.multiPqHeaderLength = multiPqHeaderLength;
|
|
7
|
+
exports.multiPqStreamReceiver = multiPqStreamReceiver;
|
|
5
8
|
exports.streamMultiPqDecrypt = streamMultiPqDecrypt;
|
|
6
|
-
const
|
|
7
|
-
const ml_kem_js_1 = require("@noble/post-quantum/ml-kem.js");
|
|
9
|
+
const hybrid_combiner_1 = require("../modern/hybrid-combiner");
|
|
8
10
|
const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
9
11
|
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
10
12
|
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
@@ -12,6 +14,8 @@ const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
|
12
14
|
const utils_1 = require("../utils");
|
|
13
15
|
const stream_pq_aead_1 = require("./stream-pq-aead");
|
|
14
16
|
const chunks_1 = require("./chunks");
|
|
17
|
+
const errors_1 = require("../errors");
|
|
18
|
+
const encoding_1 = require("../encoding");
|
|
15
19
|
exports.MULTI_PQ_STREAM_MAGIC = new Uint8Array([0x4d, 0x53, 0x50, 0x51]);
|
|
16
20
|
exports.MULTI_PQ_STREAM_VERSION = 0x01;
|
|
17
21
|
exports.MULTI_PQ_PREFIX_LEN = 35;
|
|
@@ -23,11 +27,11 @@ const TEXT_DECODER = new TextDecoder();
|
|
|
23
27
|
const KEK_INFO = TEXT_ENCODER.encode("multi-pq-stream-kek-v1");
|
|
24
28
|
function validateRecipient(r) {
|
|
25
29
|
if (!r.recipientId || typeof r.recipientId !== "string") {
|
|
26
|
-
throw new
|
|
30
|
+
throw new errors_1.CryptoError("Recipient ID must be a non-empty string", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
27
31
|
}
|
|
28
32
|
const idBytes = TEXT_ENCODER.encode(r.recipientId);
|
|
29
33
|
if (idBytes.length === 0 || idBytes.length > 255) {
|
|
30
|
-
throw new
|
|
34
|
+
throw new errors_1.CryptoError("Recipient ID must be between 1 and 255 UTF-8 bytes", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
31
35
|
}
|
|
32
36
|
const x25519Pub = (0, stream_pq_aead_1.toBytes)(r.recipientX25519Public, 32, "recipientX25519Public");
|
|
33
37
|
const mlKemPub = (0, stream_pq_aead_1.toBytes)(r.recipientMlKemPublic, 1184, "recipientMlKemPublic");
|
|
@@ -35,21 +39,10 @@ function validateRecipient(r) {
|
|
|
35
39
|
}
|
|
36
40
|
function createSlot(cek, slotIndex, r) {
|
|
37
41
|
const { idBytes, x25519Pub, mlKemPub } = validateRecipient(r);
|
|
38
|
-
const
|
|
39
|
-
const ephPub = ed25519_js_1.x25519.getPublicKey(ephPriv);
|
|
40
|
-
const x25519Shared = ed25519_js_1.x25519.getSharedSecret(ephPriv, x25519Pub);
|
|
41
|
-
const { cipherText: mlKemCt, sharedSecret: mlKemShared } = ml_kem_js_1.ml_kem768.encapsulate(mlKemPub);
|
|
42
|
-
const combined = new Uint8Array(64);
|
|
43
|
-
combined.set(x25519Shared, 0);
|
|
44
|
-
combined.set(mlKemShared, 32);
|
|
45
|
-
const kek = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, KEK_INFO, 32);
|
|
42
|
+
const { ephPub, mlKemCt, key: kek, } = (0, hybrid_combiner_1.hybridSend)(x25519Pub, mlKemPub, "multi-pq-stream");
|
|
46
43
|
const slotNonce = (0, utils_js_1.randomBytes)(stream_pq_aead_1.NONCE_LEN);
|
|
47
44
|
const aad = buildSlotAad(idBytes, slotIndex);
|
|
48
45
|
const wrappedCek = (0, chacha_js_1.xchacha20poly1305)(kek, slotNonce, aad).encrypt(cek);
|
|
49
|
-
(0, utils_1.wipeMemory)(ephPriv);
|
|
50
|
-
(0, utils_1.wipeMemory)(x25519Shared);
|
|
51
|
-
(0, utils_1.wipeMemory)(mlKemShared);
|
|
52
|
-
(0, utils_1.wipeMemory)(combined);
|
|
53
46
|
(0, utils_1.wipeMemory)(kek);
|
|
54
47
|
return serializeSlot(idBytes, ephPub, mlKemCt, slotNonce, wrappedCek);
|
|
55
48
|
}
|
|
@@ -87,60 +80,62 @@ function writeHeaderPrefix(out, chunkSize, baseNonce, count) {
|
|
|
87
80
|
function checkEncryptOptions(options) {
|
|
88
81
|
const recipients = options.recipients;
|
|
89
82
|
if (!Array.isArray(recipients) || recipients.length === 0) {
|
|
90
|
-
throw new
|
|
83
|
+
throw new errors_1.CryptoError("At least one recipient is required", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
91
84
|
}
|
|
92
85
|
if (recipients.length > 1000) {
|
|
93
|
-
throw new
|
|
86
|
+
throw new errors_1.CryptoError("Recipient count exceeds maximum limit of 1000", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
94
87
|
}
|
|
95
88
|
const chunkSize = options.chunkSize ?? stream_pq_aead_1.DEFAULT_CHUNK_SIZE;
|
|
96
89
|
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
97
90
|
if (chunkSize < 1024 || chunkSize > 16 * 1024 * 1024) {
|
|
98
|
-
throw new
|
|
91
|
+
throw new errors_1.CryptoError("Chunk size must be between 1024 and 16777216 bytes", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
99
92
|
}
|
|
100
93
|
return { recipients, chunkSize };
|
|
101
94
|
}
|
|
102
95
|
function streamMultiPqEncrypt(options) {
|
|
96
|
+
const { header, encryptor, wipe } = multiPqStreamSender(options);
|
|
97
|
+
const output = Uint8Array.from((0, encoding_1.concat)([header, ...encryptor.push(options.plaintext), encryptor.finish()]));
|
|
98
|
+
wipe();
|
|
99
|
+
return {
|
|
100
|
+
ciphertext: output,
|
|
101
|
+
algorithm: "multi-x25519-ml-kem-768-xchacha20-poly1305-stream",
|
|
102
|
+
recipientCount: options.recipients.length,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function multiPqStreamSender(options) {
|
|
103
106
|
const { recipients, chunkSize } = checkEncryptOptions(options);
|
|
104
107
|
const cek = (0, utils_js_1.randomBytes)(exports.CEK_LEN);
|
|
105
108
|
const baseNonce = (0, utils_js_1.randomBytes)(stream_pq_aead_1.NONCE_LEN);
|
|
106
109
|
const slots = recipients.map((r, i) => createSlot(cek, i, r));
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
writeHeaderPrefix(output, chunkSize, baseNonce, recipients.length);
|
|
110
|
-
let slotOffset = exports.MULTI_PQ_PREFIX_LEN;
|
|
111
|
-
for (const slot of slots) {
|
|
112
|
-
output.set(slot, slotOffset);
|
|
113
|
-
slotOffset += slot.length;
|
|
114
|
-
}
|
|
115
|
-
(0, chunks_1.encryptChunksInto)(options.plaintext, cek, baseNonce, chunkSize, output, headerLen, stream_pq_aead_1.derivePqChunkNonce);
|
|
116
|
-
(0, utils_1.wipeMemory)(cek);
|
|
110
|
+
const prefix = new Uint8Array(exports.MULTI_PQ_PREFIX_LEN);
|
|
111
|
+
writeHeaderPrefix(prefix, chunkSize, baseNonce, recipients.length);
|
|
117
112
|
return {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
header: Uint8Array.from((0, encoding_1.concat)([prefix, ...slots])),
|
|
114
|
+
encryptor: new chunks_1.ChunkEncryptor(cek, baseNonce, chunkSize, stream_pq_aead_1.derivePqChunkNonce),
|
|
115
|
+
wipe: () => (0, utils_1.wipeMemory)(cek),
|
|
121
116
|
};
|
|
122
117
|
}
|
|
123
118
|
function parseHeaderPrefix(ciphertext) {
|
|
124
119
|
if (ciphertext.length < exports.MULTI_PQ_PREFIX_LEN) {
|
|
125
|
-
throw new
|
|
120
|
+
throw new errors_1.CryptoError("Ciphertext too short: missing header prefix", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
126
121
|
}
|
|
127
122
|
for (let i = 0; i < 4; i++) {
|
|
128
123
|
if (ciphertext[i] !== exports.MULTI_PQ_STREAM_MAGIC[i]) {
|
|
129
|
-
throw new
|
|
124
|
+
throw new errors_1.CryptoError("Invalid multi-recipient post-quantum stream magic", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
130
125
|
}
|
|
131
126
|
}
|
|
132
127
|
if (ciphertext[4] !== exports.MULTI_PQ_STREAM_VERSION) {
|
|
133
|
-
throw new
|
|
128
|
+
throw new errors_1.CryptoError(`Unsupported multi-recipient post-quantum stream version: ${ciphertext[4]}`, errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
134
129
|
}
|
|
135
130
|
const view = new DataView(ciphertext.buffer, ciphertext.byteOffset, ciphertext.byteLength);
|
|
136
131
|
const chunkSize = view.getUint32(5, false);
|
|
137
132
|
if (chunkSize < 1024 || chunkSize > 16 * 1024 * 1024) {
|
|
138
|
-
throw new
|
|
133
|
+
throw new errors_1.CryptoError(`Invalid stream header chunk size: ${chunkSize}`, errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
139
134
|
}
|
|
140
135
|
const baseNonce = ciphertext.subarray(9, 33);
|
|
141
136
|
const recipientCount = view.getUint16(33, false);
|
|
142
137
|
if (recipientCount < 1) {
|
|
143
|
-
throw new
|
|
138
|
+
throw new errors_1.CryptoError("Invalid stream header: zero recipients", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
144
139
|
}
|
|
145
140
|
return { chunkSize, baseNonce, recipientCount };
|
|
146
141
|
}
|
|
@@ -149,12 +144,12 @@ function parseSlots(ciphertext, recipientCount) {
|
|
|
149
144
|
const slots = [];
|
|
150
145
|
for (let i = 0; i < recipientCount; i++) {
|
|
151
146
|
if (offset >= ciphertext.length) {
|
|
152
|
-
throw new
|
|
147
|
+
throw new errors_1.CryptoError("Ciphertext truncated: incomplete slot table", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
153
148
|
}
|
|
154
149
|
const idLen = ciphertext[offset];
|
|
155
150
|
const slotTotalLen = exports.SLOT_FIXED_OVERHEAD + idLen;
|
|
156
151
|
if (offset + slotTotalLen > ciphertext.length) {
|
|
157
|
-
throw new
|
|
152
|
+
throw new errors_1.CryptoError("Ciphertext truncated: incomplete recipient slot", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
158
153
|
}
|
|
159
154
|
const idStart = offset + 1;
|
|
160
155
|
const recipientIdBytes = ciphertext.subarray(idStart, idStart + idLen);
|
|
@@ -180,79 +175,62 @@ function parseSlots(ciphertext, recipientCount) {
|
|
|
180
175
|
}
|
|
181
176
|
return { slots, headerEndOffset: offset };
|
|
182
177
|
}
|
|
178
|
+
function legacyKek(x25519Shared, mlKemShared, ephPub) {
|
|
179
|
+
const combined = (0, encoding_1.concat)([x25519Shared, mlKemShared]);
|
|
180
|
+
const kek = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, KEK_INFO, 32);
|
|
181
|
+
(0, utils_1.wipeMemory)(combined);
|
|
182
|
+
return kek;
|
|
183
|
+
}
|
|
183
184
|
function tryUnwrapSlot(slot, recipX25519Sec, recipMlKemSec) {
|
|
184
185
|
try {
|
|
185
|
-
const
|
|
186
|
-
const x25519Shared = ed25519_js_1.x25519.getSharedSecret(recipX25519Sec, slot.ephPub);
|
|
187
|
-
const combined = new Uint8Array(64);
|
|
188
|
-
combined.set(x25519Shared, 0);
|
|
189
|
-
combined.set(mlKemShared, 32);
|
|
190
|
-
const kek = (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, slot.ephPub, KEK_INFO, 32);
|
|
186
|
+
const keks = (0, hybrid_combiner_1.hybridReceiveKeys)(recipX25519Sec, recipMlKemSec, slot.ephPub, slot.mlKemCt, "multi-pq-stream", (x25519Shared, mlKemShared) => legacyKek(x25519Shared, mlKemShared, slot.ephPub));
|
|
191
187
|
const aad = buildSlotAad(slot.recipientIdBytes, slot.slotIndex);
|
|
192
|
-
const cek = (0, chacha_js_1.xchacha20poly1305)(kek, slot.slotNonce, aad).decrypt(slot.wrappedCek);
|
|
193
|
-
(
|
|
194
|
-
(0, utils_1.wipeMemory)(mlKemShared);
|
|
195
|
-
(0, utils_1.wipeMemory)(combined);
|
|
196
|
-
(0, utils_1.wipeMemory)(kek);
|
|
188
|
+
const cek = (0, hybrid_combiner_1.openWithFirstKey)(keks, (kek) => (0, chacha_js_1.xchacha20poly1305)(kek, slot.slotNonce, aad).decrypt(slot.wrappedCek));
|
|
189
|
+
keks.forEach(utils_1.wipeMemory);
|
|
197
190
|
return cek;
|
|
198
191
|
}
|
|
199
192
|
catch {
|
|
200
193
|
return null;
|
|
201
194
|
}
|
|
202
195
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
196
|
+
const TRUNCATED = {
|
|
197
|
+
incomplete: "Ciphertext truncated: incomplete chunk",
|
|
198
|
+
finalMissing: "Ciphertext truncated: final chunk missing",
|
|
199
|
+
};
|
|
200
|
+
function multiPqHeaderLength(bytes) {
|
|
201
|
+
if (bytes.length < exports.MULTI_PQ_PREFIX_LEN)
|
|
202
|
+
return undefined;
|
|
203
|
+
const { recipientCount } = parseHeaderPrefix(bytes);
|
|
204
|
+
let offset = exports.MULTI_PQ_PREFIX_LEN;
|
|
205
|
+
for (let i = 0; i < recipientCount; i++) {
|
|
206
|
+
if (offset >= bytes.length)
|
|
207
|
+
return undefined;
|
|
208
|
+
offset += exports.SLOT_FIXED_OVERHEAD + bytes[offset];
|
|
213
209
|
}
|
|
214
|
-
return
|
|
210
|
+
return offset <= bytes.length ? offset : undefined;
|
|
215
211
|
}
|
|
216
|
-
function
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
chunks.push(decrypted);
|
|
229
|
-
totalPtLen += decrypted.length;
|
|
230
|
-
offset += encLen;
|
|
231
|
-
if (isFinal) {
|
|
232
|
-
sawFinal = true;
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
if (!sawFinal) {
|
|
237
|
-
throw new Error("Ciphertext truncated: final chunk missing");
|
|
238
|
-
}
|
|
239
|
-
const result = new Uint8Array(totalPtLen);
|
|
240
|
-
let pos = 0;
|
|
241
|
-
for (const chunk of chunks) {
|
|
242
|
-
result.set(chunk, pos);
|
|
243
|
-
pos += chunk.length;
|
|
244
|
-
}
|
|
245
|
-
return result;
|
|
212
|
+
function multiPqStreamReceiver(options, header) {
|
|
213
|
+
const recipX25519Sec = (0, stream_pq_aead_1.toBytes)(options.recipientX25519Secret, 32, "recipientX25519Secret");
|
|
214
|
+
const recipMlKemSec = (0, stream_pq_aead_1.toBytes)(options.recipientMlKemSecret, 2400, "recipientMlKemSecret");
|
|
215
|
+
const { chunkSize, baseNonce, recipientCount } = parseHeaderPrefix(header);
|
|
216
|
+
const { slots } = parseSlots(header, recipientCount);
|
|
217
|
+
const { cek, matchingSlot } = resolveRecipientCek(slots, options.recipientId, recipX25519Sec, recipMlKemSec);
|
|
218
|
+
const decryptor = new chunks_1.ChunkDecryptor(cek, baseNonce, options.chunkSize ?? chunkSize, stream_pq_aead_1.derivePqChunkNonce, TRUNCATED);
|
|
219
|
+
return {
|
|
220
|
+
decryptor,
|
|
221
|
+
recipientId: matchingSlot.recipientId,
|
|
222
|
+
wipe: () => (0, utils_1.wipeMemory)(cek),
|
|
223
|
+
};
|
|
246
224
|
}
|
|
247
225
|
function resolveRecipientCek(slots, recipId, recipX25519Sec, recipMlKemSec) {
|
|
248
226
|
if (recipId !== undefined) {
|
|
249
227
|
const slot = slots.find((s) => s.recipientId === recipId) ?? null;
|
|
250
228
|
if (!slot) {
|
|
251
|
-
throw new
|
|
229
|
+
throw new errors_1.CryptoError(`Recipient ID "${recipId}" not found in stream slots`, errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
252
230
|
}
|
|
253
231
|
const cek = tryUnwrapSlot(slot, recipX25519Sec, recipMlKemSec);
|
|
254
232
|
if (!cek) {
|
|
255
|
-
throw new
|
|
233
|
+
throw new errors_1.CryptoError(`Decryption failed for recipient slot "${recipId}"`, errors_1.CryptoErrorCode.AUTH_FAILED);
|
|
256
234
|
}
|
|
257
235
|
return { cek, matchingSlot: slot };
|
|
258
236
|
}
|
|
@@ -262,21 +240,19 @@ function resolveRecipientCek(slots, recipId, recipX25519Sec, recipMlKemSec) {
|
|
|
262
240
|
return { cek, matchingSlot: slot };
|
|
263
241
|
}
|
|
264
242
|
}
|
|
265
|
-
throw new
|
|
243
|
+
throw new errors_1.CryptoError("No matching recipient key slot could be decrypted", errors_1.CryptoErrorCode.AUTH_FAILED);
|
|
266
244
|
}
|
|
267
245
|
function streamMultiPqDecrypt(options) {
|
|
268
|
-
const recipX25519Sec = (0, stream_pq_aead_1.toBytes)(options.recipientX25519Secret, 32, "recipientX25519Secret");
|
|
269
|
-
const recipMlKemSec = (0, stream_pq_aead_1.toBytes)(options.recipientMlKemSecret, 2400, "recipientMlKemSecret");
|
|
270
246
|
const ciphertext = options.ciphertext;
|
|
271
|
-
const {
|
|
272
|
-
const
|
|
273
|
-
const {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
}
|
|
247
|
+
const { recipientCount } = parseHeaderPrefix(ciphertext);
|
|
248
|
+
const { headerEndOffset } = parseSlots(ciphertext, recipientCount);
|
|
249
|
+
const { decryptor, recipientId, wipe } = multiPqStreamReceiver(options, ciphertext.subarray(0, headerEndOffset));
|
|
250
|
+
try {
|
|
251
|
+
const plaintext = (0, chunks_1.decryptChunks)(decryptor, ciphertext.subarray(headerEndOffset));
|
|
252
|
+
return { plaintext, recipientId };
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
wipe();
|
|
256
|
+
}
|
|
281
257
|
}
|
|
282
258
|
//# sourceMappingURL=stream-multi-pq-aead.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { ChunkDecryptor, ChunkEncryptor, TAG_LEN } from "./chunks";
|
|
2
|
+
export { TAG_LEN };
|
|
3
3
|
export declare const EPHEMERAL_LEN = 32;
|
|
4
4
|
export declare const ML_KEM_CT_LEN = 1088;
|
|
5
5
|
export declare const NONCE_LEN = 24;
|
|
@@ -23,6 +23,11 @@ export interface StreamPqDecryptOptions {
|
|
|
23
23
|
ciphertext: Uint8Array;
|
|
24
24
|
chunkSize?: number | undefined;
|
|
25
25
|
}
|
|
26
|
+
export declare function pqStreamSender(recipX25519Pub: Uint8Array, recipMlKemPub: Uint8Array, chunkSize: number): {
|
|
27
|
+
header: Uint8Array;
|
|
28
|
+
encryptor: ChunkEncryptor;
|
|
29
|
+
};
|
|
30
|
+
export declare function pqStreamReceiver(recipX25519Sec: Uint8Array, recipMlKemSec: Uint8Array, header: Uint8Array, chunkSize: number): ChunkDecryptor;
|
|
26
31
|
export declare function streamPqEncrypt(options: StreamPqEncryptOptions): StreamPqEncryptResult;
|
|
27
32
|
export declare function streamPqDecrypt(options: StreamPqDecryptOptions): Uint8Array;
|
|
28
33
|
//# sourceMappingURL=stream-pq-aead.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-pq-aead.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-pq-aead.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-pq-aead.d.ts","sourceRoot":"","sources":["../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CAAA;CAAE,CAkBnD;AAMD,wBAAgB,gBAAgB,CAC9B,cAAc,EAAE,UAAU,EAC1B,aAAa,EAAE,UAAU,EACzB,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,MAAM,GAChB,cAAc,CAkBhB;AAQD,wBAAgB,eAAe,CAC7B,OAAO,EAAE,sBAAsB,GAC9B,qBAAqB,CA4BvB;AAQD,wBAAgB,eAAe,CAAC,OAAO,EAAE,sBAAsB,GAAG,UAAU,CA+B3E"}
|
|
@@ -1,38 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_CHUNK_SIZE = exports.PQ_STREAM_HEADER_LEN = exports.NONCE_LEN = exports.ML_KEM_CT_LEN = exports.EPHEMERAL_LEN = exports.TAG_LEN =
|
|
3
|
+
exports.DEFAULT_CHUNK_SIZE = exports.PQ_STREAM_HEADER_LEN = exports.NONCE_LEN = exports.ML_KEM_CT_LEN = exports.EPHEMERAL_LEN = exports.TAG_LEN = void 0;
|
|
4
4
|
exports.toBytes = toBytes;
|
|
5
5
|
exports.derivePqChunkNonce = derivePqChunkNonce;
|
|
6
|
+
exports.pqStreamSender = pqStreamSender;
|
|
7
|
+
exports.pqStreamReceiver = pqStreamReceiver;
|
|
6
8
|
exports.streamPqEncrypt = streamPqEncrypt;
|
|
7
9
|
exports.streamPqDecrypt = streamPqDecrypt;
|
|
8
|
-
const
|
|
9
|
-
const ml_kem_js_1 = require("@noble/post-quantum/ml-kem.js");
|
|
10
|
+
const hybrid_combiner_1 = require("../modern/hybrid-combiner");
|
|
10
11
|
const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
11
12
|
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
12
|
-
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
13
13
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
14
14
|
const chunks_1 = require("./chunks");
|
|
15
|
-
Object.defineProperty(exports, "CHUNK_TAG_FINAL", { enumerable: true, get: function () { return chunks_1.CHUNK_TAG_FINAL; } });
|
|
16
|
-
Object.defineProperty(exports, "CHUNK_TAG_MESSAGE", { enumerable: true, get: function () { return chunks_1.CHUNK_TAG_MESSAGE; } });
|
|
17
15
|
Object.defineProperty(exports, "TAG_LEN", { enumerable: true, get: function () { return chunks_1.TAG_LEN; } });
|
|
16
|
+
const errors_1 = require("../errors");
|
|
17
|
+
const encoding_1 = require("../encoding");
|
|
18
18
|
exports.EPHEMERAL_LEN = 32;
|
|
19
19
|
exports.ML_KEM_CT_LEN = 1088;
|
|
20
20
|
exports.NONCE_LEN = 24;
|
|
21
21
|
exports.PQ_STREAM_HEADER_LEN = exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN + exports.NONCE_LEN;
|
|
22
22
|
exports.DEFAULT_CHUNK_SIZE = 64 * 1024;
|
|
23
|
-
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
24
23
|
function toBytes(data, expectedLen, name = "Data") {
|
|
25
24
|
let buf;
|
|
26
25
|
if (typeof data === "string") {
|
|
27
|
-
if (!
|
|
28
|
-
throw new
|
|
29
|
-
buf =
|
|
26
|
+
if (!(0, encoding_1.isHex)(data))
|
|
27
|
+
throw new errors_1.CryptoError(`Invalid hex string for ${name}`, errors_1.CryptoErrorCode.INVALID_HEX);
|
|
28
|
+
buf = (0, encoding_1.decodeHex)(data);
|
|
30
29
|
}
|
|
31
30
|
else {
|
|
32
31
|
buf = new Uint8Array(data);
|
|
33
32
|
}
|
|
34
33
|
if (expectedLen !== undefined && buf.length !== expectedLen) {
|
|
35
|
-
throw new
|
|
34
|
+
throw new errors_1.CryptoError(`${name} must be ${expectedLen} bytes, got ${buf.length}`, errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
36
35
|
}
|
|
37
36
|
return buf;
|
|
38
37
|
}
|
|
@@ -52,18 +51,22 @@ function deriveSharedKey(x25519Shared, mlKemShared, ephPub) {
|
|
|
52
51
|
combined.set(mlKemShared, x25519Shared.length);
|
|
53
52
|
return (0, hkdf_js_1.hkdf)(sha2_js_1.sha256, combined, ephPub, new TextEncoder().encode("pq-stream-aead-v1"), 32);
|
|
54
53
|
}
|
|
55
|
-
function
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
function pqStreamSender(recipX25519Pub, recipMlKemPub, chunkSize) {
|
|
55
|
+
const { ephPub, mlKemCt, key } = (0, hybrid_combiner_1.hybridSend)(recipX25519Pub, recipMlKemPub, "pq-stream");
|
|
56
|
+
const baseNonce = (0, utils_js_1.randomBytes)(exports.NONCE_LEN);
|
|
57
|
+
const header = new Uint8Array(exports.PQ_STREAM_HEADER_LEN);
|
|
58
|
+
header.set(ephPub, 0);
|
|
59
|
+
header.set(mlKemCt, exports.EPHEMERAL_LEN);
|
|
60
|
+
header.set(baseNonce, exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN);
|
|
61
|
+
const encryptor = new chunks_1.ChunkEncryptor(key, baseNonce, chunkSize, derivePqChunkNonce);
|
|
62
|
+
return { header, encryptor };
|
|
62
63
|
}
|
|
63
|
-
function
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
|
|
64
|
+
function pqStreamReceiver(recipX25519Sec, recipMlKemSec, header, chunkSize) {
|
|
65
|
+
const ephPub = header.subarray(0, exports.EPHEMERAL_LEN);
|
|
66
|
+
const mlKemCt = header.subarray(exports.EPHEMERAL_LEN, exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN);
|
|
67
|
+
const baseNonce = header.subarray(exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN, exports.PQ_STREAM_HEADER_LEN);
|
|
68
|
+
const keys = (0, hybrid_combiner_1.hybridReceiveKeys)(recipX25519Sec, recipMlKemSec, ephPub, mlKemCt, "pq-stream", (x25519Shared, mlKemShared) => deriveSharedKey(x25519Shared, mlKemShared, ephPub));
|
|
69
|
+
return new chunks_1.ChunkDecryptor(keys, baseNonce, chunkSize, derivePqChunkNonce);
|
|
67
70
|
}
|
|
68
71
|
function streamPqEncrypt(options) {
|
|
69
72
|
const recipX25519Pub = toBytes(options.recipientX25519Public, 32, "recipientX25519Public");
|
|
@@ -71,62 +74,13 @@ function streamPqEncrypt(options) {
|
|
|
71
74
|
const chunkSize = options.chunkSize ?? exports.DEFAULT_CHUNK_SIZE;
|
|
72
75
|
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
73
76
|
const plaintext = options.plaintext;
|
|
74
|
-
const {
|
|
75
|
-
const
|
|
76
|
-
const output = new Uint8Array((0, chunks_1.streamSize)(exports.PQ_STREAM_HEADER_LEN, plaintext.length, chunkSize));
|
|
77
|
-
output.set(ephPub, 0);
|
|
78
|
-
output.set(mlKemCt, exports.EPHEMERAL_LEN);
|
|
79
|
-
output.set(baseNonce, exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN);
|
|
80
|
-
(0, chunks_1.encryptChunksInto)(plaintext, key, baseNonce, chunkSize, output, exports.PQ_STREAM_HEADER_LEN, derivePqChunkNonce);
|
|
77
|
+
const { header, encryptor } = pqStreamSender(recipX25519Pub, recipMlKemPub, chunkSize);
|
|
78
|
+
const output = Uint8Array.from((0, encoding_1.concat)([header, ...encryptor.push(plaintext), encryptor.finish()]));
|
|
81
79
|
return {
|
|
82
80
|
ciphertext: output,
|
|
83
81
|
algorithm: "x25519-ml-kem-768-xchacha20-poly1305-stream",
|
|
84
82
|
};
|
|
85
83
|
}
|
|
86
|
-
function readPqChunkHeader(ciphertext, offset, chunkSize) {
|
|
87
|
-
const chunkTag = ciphertext[offset];
|
|
88
|
-
if (chunkTag !== chunks_1.CHUNK_TAG_MESSAGE && chunkTag !== chunks_1.CHUNK_TAG_FINAL) {
|
|
89
|
-
throw new Error(`Invalid chunk tag: 0x${chunkTag.toString(16)}`);
|
|
90
|
-
}
|
|
91
|
-
const isFinal = chunkTag === chunks_1.CHUNK_TAG_FINAL;
|
|
92
|
-
const start = offset + 1;
|
|
93
|
-
const encLen = isFinal ? ciphertext.length - start : chunkSize + chunks_1.TAG_LEN;
|
|
94
|
-
if (start + encLen > ciphertext.length) {
|
|
95
|
-
throw new Error("Ciphertext truncated — incomplete chunk");
|
|
96
|
-
}
|
|
97
|
-
return { chunkTag, isFinal, encLen };
|
|
98
|
-
}
|
|
99
|
-
function decryptChunks(ciphertext, key, baseNonce, chunkSize) {
|
|
100
|
-
let offset = exports.PQ_STREAM_HEADER_LEN;
|
|
101
|
-
const chunks = [];
|
|
102
|
-
let totalPtLen = 0;
|
|
103
|
-
let sawFinal = false;
|
|
104
|
-
while (offset < ciphertext.length) {
|
|
105
|
-
const { chunkTag, isFinal, encLen } = readPqChunkHeader(ciphertext, offset, chunkSize);
|
|
106
|
-
offset += 1;
|
|
107
|
-
const chunkIdx = chunks.length;
|
|
108
|
-
const nonce = derivePqChunkNonce(baseNonce, chunkIdx, chunkTag);
|
|
109
|
-
const cipher = (0, chacha_js_1.xchacha20poly1305)(key, nonce);
|
|
110
|
-
const decrypted = cipher.decrypt(ciphertext.subarray(offset, offset + encLen));
|
|
111
|
-
chunks.push(decrypted);
|
|
112
|
-
totalPtLen += decrypted.length;
|
|
113
|
-
offset += encLen;
|
|
114
|
-
if (isFinal) {
|
|
115
|
-
sawFinal = true;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (!sawFinal) {
|
|
120
|
-
throw new Error("Ciphertext truncated — final chunk missing");
|
|
121
|
-
}
|
|
122
|
-
const result = new Uint8Array(totalPtLen);
|
|
123
|
-
let pos = 0;
|
|
124
|
-
for (const chunk of chunks) {
|
|
125
|
-
result.set(chunk, pos);
|
|
126
|
-
pos += chunk.length;
|
|
127
|
-
}
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
84
|
function streamPqDecrypt(options) {
|
|
131
85
|
const recipX25519Sec = toBytes(options.recipientX25519Secret, 32, "recipientX25519Secret");
|
|
132
86
|
const recipMlKemSec = toBytes(options.recipientMlKemSecret, 2400, "recipientMlKemSecret");
|
|
@@ -134,12 +88,8 @@ function streamPqDecrypt(options) {
|
|
|
134
88
|
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
135
89
|
const ciphertext = options.ciphertext;
|
|
136
90
|
if (ciphertext.length < exports.PQ_STREAM_HEADER_LEN + 1 + chunks_1.TAG_LEN) {
|
|
137
|
-
throw new
|
|
91
|
+
throw new errors_1.CryptoError("Ciphertext too short — missing header or chunk data", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
138
92
|
}
|
|
139
|
-
|
|
140
|
-
const mlKemCt = ciphertext.subarray(exports.EPHEMERAL_LEN, exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN);
|
|
141
|
-
const baseNonce = ciphertext.subarray(exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN, exports.PQ_STREAM_HEADER_LEN);
|
|
142
|
-
const key = decapsulateKeyMaterial(recipX25519Sec, recipMlKemSec, ephPub, mlKemCt);
|
|
143
|
-
return decryptChunks(ciphertext, key, baseNonce, chunkSize);
|
|
93
|
+
return (0, chunks_1.decryptChunks)(pqStreamReceiver(recipX25519Sec, recipMlKemSec, ciphertext.subarray(0, exports.PQ_STREAM_HEADER_LEN), chunkSize), ciphertext.subarray(exports.PQ_STREAM_HEADER_LEN));
|
|
144
94
|
}
|
|
145
95
|
//# sourceMappingURL=stream-pq-aead.js.map
|
|
@@ -8,12 +8,13 @@ export interface HashStreamResult {
|
|
|
8
8
|
}
|
|
9
9
|
export interface EncryptStreamOptions {
|
|
10
10
|
key: string | Uint8Array;
|
|
11
|
+
chunkSize?: number | undefined;
|
|
11
12
|
}
|
|
12
13
|
export interface DecryptStreamOptions {
|
|
13
14
|
key: string | Uint8Array;
|
|
15
|
+
chunkSize?: number | undefined;
|
|
16
|
+
format?: "stream" | "single-message" | undefined;
|
|
14
17
|
}
|
|
15
|
-
export declare function createEncryptStream(options: EncryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
16
|
-
export declare function createDecryptStream(options: DecryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
17
18
|
export declare function createHashStream(algorithm: WebStreamHashAlgorithm): CryptoTransformStream<Uint8Array, HashStreamResult>;
|
|
18
19
|
export interface PqEncryptStreamOptions {
|
|
19
20
|
recipientX25519Public: string | Uint8Array;
|
|
@@ -35,6 +36,8 @@ export interface MultiPqDecryptStreamOptions {
|
|
|
35
36
|
recipientMlKemSecret: string | Uint8Array;
|
|
36
37
|
chunkSize?: number | undefined;
|
|
37
38
|
}
|
|
39
|
+
export declare function createEncryptStream(options: EncryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
40
|
+
export declare function createDecryptStream(options: DecryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
38
41
|
export declare function createPqEncryptStream(options: PqEncryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
39
42
|
export declare function createPqDecryptStream(options: PqDecryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
40
43
|
export declare function createMultiPqEncryptStream(options: MultiPqEncryptStreamOptions): CryptoTransformStream<Uint8Array, Uint8Array>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-streams.d.ts","sourceRoot":"","sources":["../../src/streaming/web-streams.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"web-streams.d.ts","sourceRoot":"","sources":["../../src/streaming/web-streams.ts"],"names":[],"mappings":"AAkCA,OAAO,EAIL,KAAK,gBAAgB,EACtB,MAAM,wBAAwB,CAAC;AAehC,MAAM,MAAM,qBAAqB,CAAC,CAAC,EAAE,CAAC,IAAI,OAAO,YAAY,EAAE,eAAe,CAC5E,CAAC,EACD,CAAC,CACF,CAAC;AAMF,MAAM,MAAM,sBAAsB,GAChC,QAAQ,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,GAAG,QAAQ,CAAC;AAY3D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