@sebastienrousseau/crypto-lib 0.0.35 → 0.0.37
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
|
@@ -7,18 +7,19 @@ exports.verifyFeldmanShare = verifyFeldmanShare;
|
|
|
7
7
|
exports.splitSecretWithCommitments = splitSecretWithCommitments;
|
|
8
8
|
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
9
9
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
10
|
+
const errors_1 = require("../errors");
|
|
11
|
+
const encoding_1 = require("../encoding");
|
|
10
12
|
const P = BigInt("7237005577332262213973186563042994240857116359379907606001950938285454250989");
|
|
11
|
-
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
12
13
|
function bytesToHex(bytes) {
|
|
13
|
-
return
|
|
14
|
+
return (0, encoding_1.toHex)(bytes);
|
|
14
15
|
}
|
|
15
16
|
function scalarToHex(scalar) {
|
|
16
17
|
const hex = scalar.toString(16).padStart(64, "0");
|
|
17
18
|
return hex;
|
|
18
19
|
}
|
|
19
20
|
function hexToScalar(hex) {
|
|
20
|
-
if (!
|
|
21
|
-
throw new
|
|
21
|
+
if (!(0, encoding_1.isHex)(hex)) {
|
|
22
|
+
throw new errors_1.CryptoError("Invalid hex string", errors_1.CryptoErrorCode.INVALID_HEX);
|
|
22
23
|
}
|
|
23
24
|
return BigInt("0x" + hex);
|
|
24
25
|
}
|
|
@@ -70,17 +71,17 @@ function evaluatePolynomial(coefficients, x) {
|
|
|
70
71
|
}
|
|
71
72
|
function splitSecret(secret, n, threshold) {
|
|
72
73
|
if (threshold < 2) {
|
|
73
|
-
throw new
|
|
74
|
+
throw new errors_1.CryptoError("Threshold must be at least 2", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
74
75
|
}
|
|
75
76
|
if (n < threshold) {
|
|
76
|
-
throw new
|
|
77
|
+
throw new errors_1.CryptoError("Number of shares must be >= threshold", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
77
78
|
}
|
|
78
79
|
if (n > 255) {
|
|
79
|
-
throw new
|
|
80
|
+
throw new errors_1.CryptoError("Maximum 255 shares supported", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
80
81
|
}
|
|
81
82
|
const secretScalar = hexToScalar(secret);
|
|
82
83
|
if (secretScalar >= P) {
|
|
83
|
-
throw new
|
|
84
|
+
throw new errors_1.CryptoError("Secret must be less than the Ed25519 group order; reduce or derive it first", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
84
85
|
}
|
|
85
86
|
const coefficients = [secretScalar];
|
|
86
87
|
for (let i = 1; i < threshold; i++) {
|
|
@@ -103,11 +104,11 @@ function splitSecret(secret, n, threshold) {
|
|
|
103
104
|
}
|
|
104
105
|
function combineShares(shares) {
|
|
105
106
|
if (shares.length < 2) {
|
|
106
|
-
throw new
|
|
107
|
+
throw new errors_1.CryptoError("Need at least 2 shares to reconstruct", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
107
108
|
}
|
|
108
109
|
const indices = new Set(shares.map((s) => s.index));
|
|
109
110
|
if (indices.size !== shares.length) {
|
|
110
|
-
throw new
|
|
111
|
+
throw new errors_1.CryptoError("Duplicate share indices detected", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
111
112
|
}
|
|
112
113
|
let secret = BigInt(0);
|
|
113
114
|
for (let i = 0; i < shares.length; i++) {
|
|
@@ -129,7 +130,7 @@ function combineShares(shares) {
|
|
|
129
130
|
}
|
|
130
131
|
function generateFeldmanCommitments(coefficients) {
|
|
131
132
|
if (coefficients.length < 2) {
|
|
132
|
-
throw new
|
|
133
|
+
throw new errors_1.CryptoError("Need at least 2 coefficients (threshold >= 2)", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
133
134
|
}
|
|
134
135
|
const commitments = [];
|
|
135
136
|
for (const coeff of coefficients) {
|
|
@@ -159,17 +160,17 @@ function verifyFeldmanShare(share, commitments) {
|
|
|
159
160
|
}
|
|
160
161
|
function splitSecretWithCommitments(secret, n, threshold) {
|
|
161
162
|
if (threshold < 2) {
|
|
162
|
-
throw new
|
|
163
|
+
throw new errors_1.CryptoError("Threshold must be at least 2", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
163
164
|
}
|
|
164
165
|
if (n < threshold) {
|
|
165
|
-
throw new
|
|
166
|
+
throw new errors_1.CryptoError("Number of shares must be >= threshold", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
166
167
|
}
|
|
167
168
|
if (n > 255) {
|
|
168
|
-
throw new
|
|
169
|
+
throw new errors_1.CryptoError("Maximum 255 shares supported", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
169
170
|
}
|
|
170
171
|
const secretScalar = hexToScalar(secret);
|
|
171
172
|
if (secretScalar >= P) {
|
|
172
|
-
throw new
|
|
173
|
+
throw new errors_1.CryptoError("Secret must be less than the Ed25519 group order; reduce or derive it first", errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
173
174
|
}
|
|
174
175
|
const coefficients = [secretScalar];
|
|
175
176
|
for (let i = 1; i < threshold; i++) {
|
package/dist/registry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAaA,MAAM,MAAM,iBAAiB,GAC3B,YAAY,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,SAAS,GAAG,cAAc,GAAG,KAAK,CAAC;AAG7E,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAG1C,MAAM,MAAM,eAAe,GACzB,aAAa,GAAG,YAAY,GAAG,YAAY,GAAG,cAAc,CAAC;AAG/D,MAAM,WAAW,aAAa;IAE5B,EAAE,EAAE,MAAM,CAAC;IAEX,IAAI,EAAE,MAAM,CAAC;IAEb,QAAQ,EAAE,iBAAiB,CAAC;IAE5B,aAAa,EAAE,aAAa,CAAC;IAE7B,MAAM,EAAE,eAAe,CAAC;IAExB,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9B,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;CAChC;
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAaA,MAAM,MAAM,iBAAiB,GAC3B,YAAY,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,SAAS,GAAG,cAAc,GAAG,KAAK,CAAC;AAG7E,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAG1C,MAAM,MAAM,eAAe,GACzB,aAAa,GAAG,YAAY,GAAG,YAAY,GAAG,cAAc,CAAC;AAG/D,MAAM,WAAW,aAAa;IAE5B,EAAE,EAAE,MAAM,CAAC;IAEX,IAAI,EAAE,MAAM,CAAC;IAEb,QAAQ,EAAE,iBAAiB,CAAC;IAE5B,aAAa,EAAE,aAAa,CAAC;IAE7B,MAAM,EAAE,eAAe,CAAC;IAExB,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9B,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;CAChC;AA+ZD,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAGzE;AAKD,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE;IACvC,QAAQ,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;IACzC,MAAM,CAAC,EAAE,eAAe,GAAG,SAAS,CAAC;CACtC,GAAG,aAAa,EAAE,CASlB;AAKD,wBAAgB,WAAW,CAAC,QAAQ,CAAC,EAAE,iBAAiB,GAAG,aAAa,EAAE,CAQzE;AAKD,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAGvD"}
|
package/dist/registry.js
CHANGED
|
@@ -268,6 +268,24 @@ const REGISTRY = [
|
|
|
268
268
|
standard: "BIP-340",
|
|
269
269
|
aliases: ["bip340", "schnorr-secp256k1"],
|
|
270
270
|
},
|
|
271
|
+
{
|
|
272
|
+
id: "ed25519-ml-dsa-44",
|
|
273
|
+
name: "Composite ML-DSA-44 + Ed25519",
|
|
274
|
+
category: "signing",
|
|
275
|
+
securityLevel: 2,
|
|
276
|
+
status: "recommended",
|
|
277
|
+
standard: "draft-ietf-lamps-pq-composite-sigs-19",
|
|
278
|
+
aliases: ["id-mldsa44-ed25519-sha512"],
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
id: "ed25519-ml-dsa-65",
|
|
282
|
+
name: "Composite ML-DSA-65 + Ed25519",
|
|
283
|
+
category: "signing",
|
|
284
|
+
securityLevel: 3,
|
|
285
|
+
status: "recommended",
|
|
286
|
+
standard: "draft-ietf-lamps-pq-composite-sigs-19",
|
|
287
|
+
aliases: ["id-mldsa65-ed25519-sha512"],
|
|
288
|
+
},
|
|
271
289
|
{
|
|
272
290
|
id: "x25519",
|
|
273
291
|
name: "X25519",
|
|
@@ -358,6 +376,14 @@ const REGISTRY = [
|
|
|
358
376
|
securityLevel: 5,
|
|
359
377
|
status: "recommended",
|
|
360
378
|
},
|
|
379
|
+
{
|
|
380
|
+
id: "x-wing",
|
|
381
|
+
name: "X-Wing (X25519+ML-KEM-768)",
|
|
382
|
+
category: "kem",
|
|
383
|
+
securityLevel: 3,
|
|
384
|
+
status: "recommended",
|
|
385
|
+
standard: "draft-connolly-cfrg-xwing-kem-06 (HPKE KEM 0x647A)",
|
|
386
|
+
},
|
|
361
387
|
];
|
|
362
388
|
const aliasMap = new Map();
|
|
363
389
|
const idMap = new Map();
|
|
@@ -5,5 +5,36 @@ export declare const CHUNK_TAG_FINAL = 1;
|
|
|
5
5
|
export type ChunkNonce = (baseNonce: Uint8Array, counter: number, tag: number) => Uint8Array;
|
|
6
6
|
export declare function chunkCount(ptLen: number, chunkSize: number): number;
|
|
7
7
|
export declare function streamSize(headerLen: number, ptLen: number, chunkSize: number): number;
|
|
8
|
+
export declare class ChunkEncryptor {
|
|
9
|
+
private readonly key;
|
|
10
|
+
private readonly baseNonce;
|
|
11
|
+
private readonly chunkSize;
|
|
12
|
+
private readonly chunkNonce;
|
|
13
|
+
private readonly queue;
|
|
14
|
+
private counter;
|
|
15
|
+
constructor(key: Uint8Array, baseNonce: Uint8Array, chunkSize: number, chunkNonce: ChunkNonce);
|
|
16
|
+
push(plaintext: Uint8Array): Uint8Array[];
|
|
17
|
+
finish(): Uint8Array;
|
|
18
|
+
private seal;
|
|
19
|
+
}
|
|
20
|
+
export interface TruncationMessages {
|
|
21
|
+
incomplete: string;
|
|
22
|
+
finalMissing: string;
|
|
23
|
+
}
|
|
24
|
+
export declare class ChunkDecryptor {
|
|
25
|
+
private readonly keys;
|
|
26
|
+
private readonly baseNonce;
|
|
27
|
+
private readonly chunkSize;
|
|
28
|
+
private readonly chunkNonce;
|
|
29
|
+
private readonly truncated;
|
|
30
|
+
private readonly queue;
|
|
31
|
+
private counter;
|
|
32
|
+
private key;
|
|
33
|
+
constructor(keys: Uint8Array | readonly Uint8Array[], baseNonce: Uint8Array, chunkSize: number, chunkNonce: ChunkNonce, truncated?: TruncationMessages);
|
|
34
|
+
push(ciphertext: Uint8Array): Uint8Array[];
|
|
35
|
+
finish(): Uint8Array;
|
|
36
|
+
private open;
|
|
37
|
+
}
|
|
38
|
+
export declare function decryptChunks(decryptor: ChunkDecryptor, body: Uint8Array): Uint8Array;
|
|
8
39
|
export declare function encryptChunksInto(plaintext: Uint8Array, key: Uint8Array, baseNonce: Uint8Array, chunkSize: number, output: Uint8Array, offset: number, chunkNonce: ChunkNonce): void;
|
|
9
40
|
//# sourceMappingURL=chunks.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chunks.d.ts","sourceRoot":"","sources":["../../src/streaming/chunks.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"chunks.d.ts","sourceRoot":"","sources":["../../src/streaming/chunks.ts"],"names":[],"mappings":"AAkBA,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAOvD;AAGD,eAAO,MAAM,OAAO,KAAK,CAAC;AAE1B,eAAO,MAAM,iBAAiB,IAAO,CAAC;AAEtC,eAAO,MAAM,eAAe,IAAO,CAAC;AAGpC,MAAM,MAAM,UAAU,GAAG,CACvB,SAAS,EAAE,UAAU,EACrB,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,KACR,UAAU,CAAC;AAMhB,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAEnE;AAMD,wBAAgB,UAAU,CACxB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,GAChB,MAAM,CAER;AA2CD,qBAAa,cAAc;IAKvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAP7B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmB;IACzC,OAAO,CAAC,OAAO,CAAK;gBAGD,GAAG,EAAE,UAAU,EACf,SAAS,EAAE,UAAU,EACrB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,UAAU;IAMzC,IAAI,CAAC,SAAS,EAAE,UAAU,GAAG,UAAU,EAAE;IAUzC,MAAM,IAAI,UAAU;IAKpB,OAAO,CAAC,IAAI;CAQb;AAGD,MAAM,WAAW,kBAAkB;IAEjC,UAAU,EAAE,MAAM,CAAC;IAEnB,YAAY,EAAE,MAAM,CAAC;CACtB;AAkBD,qBAAa,cAAc;IAMvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAT5B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmB;IACzC,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,GAAG,CAAyB;gBAGjB,IAAI,EAAE,UAAU,GAAG,SAAS,UAAU,EAAE,EACxC,SAAS,EAAE,UAAU,EACrB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,UAAU,EACtB,SAAS,GAAE,kBAA8B;IAM5D,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,EAAE;IA4B1C,MAAM,IAAI,UAAU;IAgBpB,OAAO,CAAC,IAAI;CAkBb;AAGD,wBAAgB,aAAa,CAC3B,SAAS,EAAE,cAAc,EACzB,IAAI,EAAE,UAAU,GACf,UAAU,CAIZ;AAOD,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,UAAU,EACrB,GAAG,EAAE,UAAU,EACf,SAAS,EAAE,UAAU,EACrB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,UAAU,GACrB,IAAI,CAMN"}
|
package/dist/streaming/chunks.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CHUNK_TAG_FINAL = exports.CHUNK_TAG_MESSAGE = exports.TAG_LEN = void 0;
|
|
3
|
+
exports.ChunkDecryptor = exports.ChunkEncryptor = exports.CHUNK_TAG_FINAL = exports.CHUNK_TAG_MESSAGE = exports.TAG_LEN = void 0;
|
|
4
4
|
exports.assertChunkSize = assertChunkSize;
|
|
5
5
|
exports.chunkCount = chunkCount;
|
|
6
6
|
exports.streamSize = streamSize;
|
|
7
|
+
exports.decryptChunks = decryptChunks;
|
|
7
8
|
exports.encryptChunksInto = encryptChunksInto;
|
|
8
9
|
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
10
|
+
const errors_1 = require("../errors");
|
|
11
|
+
const encoding_1 = require("../encoding");
|
|
9
12
|
function assertChunkSize(chunkSize) {
|
|
10
13
|
if (!Number.isSafeInteger(chunkSize) || chunkSize < 1) {
|
|
11
|
-
throw new
|
|
14
|
+
throw new errors_1.CryptoError(`chunkSize must be a positive integer, got ${chunkSize}`, errors_1.CryptoErrorCode.INVALID_INPUT);
|
|
12
15
|
}
|
|
13
16
|
}
|
|
14
17
|
exports.TAG_LEN = 16;
|
|
@@ -20,18 +23,151 @@ function chunkCount(ptLen, chunkSize) {
|
|
|
20
23
|
function streamSize(headerLen, ptLen, chunkSize) {
|
|
21
24
|
return headerLen + ptLen + chunkCount(ptLen, chunkSize) * (1 + exports.TAG_LEN);
|
|
22
25
|
}
|
|
26
|
+
class ByteQueue {
|
|
27
|
+
parts = [];
|
|
28
|
+
length = 0;
|
|
29
|
+
push(data) {
|
|
30
|
+
if (data.length === 0)
|
|
31
|
+
return;
|
|
32
|
+
this.parts.push(data);
|
|
33
|
+
this.length += data.length;
|
|
34
|
+
}
|
|
35
|
+
first() {
|
|
36
|
+
return this.parts[0][0];
|
|
37
|
+
}
|
|
38
|
+
take(n) {
|
|
39
|
+
const out = new Uint8Array(n);
|
|
40
|
+
let filled = 0;
|
|
41
|
+
while (filled < n) {
|
|
42
|
+
const part = this.parts[0];
|
|
43
|
+
const used = Math.min(part.length, n - filled);
|
|
44
|
+
out.set(part.subarray(0, used), filled);
|
|
45
|
+
filled += used;
|
|
46
|
+
if (used === part.length)
|
|
47
|
+
this.parts.shift();
|
|
48
|
+
else
|
|
49
|
+
this.parts[0] = part.subarray(used);
|
|
50
|
+
}
|
|
51
|
+
this.length -= n;
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
class ChunkEncryptor {
|
|
56
|
+
key;
|
|
57
|
+
baseNonce;
|
|
58
|
+
chunkSize;
|
|
59
|
+
chunkNonce;
|
|
60
|
+
queue = new ByteQueue();
|
|
61
|
+
counter = 0;
|
|
62
|
+
constructor(key, baseNonce, chunkSize, chunkNonce) {
|
|
63
|
+
this.key = key;
|
|
64
|
+
this.baseNonce = baseNonce;
|
|
65
|
+
this.chunkSize = chunkSize;
|
|
66
|
+
this.chunkNonce = chunkNonce;
|
|
67
|
+
assertChunkSize(chunkSize);
|
|
68
|
+
}
|
|
69
|
+
push(plaintext) {
|
|
70
|
+
this.queue.push(plaintext);
|
|
71
|
+
const out = [];
|
|
72
|
+
while (this.queue.length > this.chunkSize) {
|
|
73
|
+
out.push(this.seal(this.queue.take(this.chunkSize), exports.CHUNK_TAG_MESSAGE));
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
finish() {
|
|
78
|
+
return this.seal(this.queue.take(this.queue.length), exports.CHUNK_TAG_FINAL);
|
|
79
|
+
}
|
|
80
|
+
seal(chunk, tag) {
|
|
81
|
+
const nonce = this.chunkNonce(this.baseNonce, this.counter++, tag);
|
|
82
|
+
const encrypted = (0, chacha_js_1.xchacha20poly1305)(this.key, nonce).encrypt(chunk);
|
|
83
|
+
const out = new Uint8Array(1 + encrypted.length);
|
|
84
|
+
out[0] = tag;
|
|
85
|
+
out.set(encrypted, 1);
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.ChunkEncryptor = ChunkEncryptor;
|
|
90
|
+
const TRUNCATED = {
|
|
91
|
+
incomplete: "Ciphertext truncated — incomplete chunk",
|
|
92
|
+
finalMissing: "Ciphertext truncated — final chunk missing",
|
|
93
|
+
};
|
|
94
|
+
class ChunkDecryptor {
|
|
95
|
+
keys;
|
|
96
|
+
baseNonce;
|
|
97
|
+
chunkSize;
|
|
98
|
+
chunkNonce;
|
|
99
|
+
truncated;
|
|
100
|
+
queue = new ByteQueue();
|
|
101
|
+
counter = 0;
|
|
102
|
+
key;
|
|
103
|
+
constructor(keys, baseNonce, chunkSize, chunkNonce, truncated = TRUNCATED) {
|
|
104
|
+
this.keys = keys;
|
|
105
|
+
this.baseNonce = baseNonce;
|
|
106
|
+
this.chunkSize = chunkSize;
|
|
107
|
+
this.chunkNonce = chunkNonce;
|
|
108
|
+
this.truncated = truncated;
|
|
109
|
+
assertChunkSize(chunkSize);
|
|
110
|
+
}
|
|
111
|
+
push(ciphertext) {
|
|
112
|
+
this.queue.push(ciphertext);
|
|
113
|
+
const full = 1 + this.chunkSize + exports.TAG_LEN;
|
|
114
|
+
const out = [];
|
|
115
|
+
while (this.queue.length > 0) {
|
|
116
|
+
const tag = this.queue.first();
|
|
117
|
+
if (tag !== exports.CHUNK_TAG_MESSAGE && tag !== exports.CHUNK_TAG_FINAL) {
|
|
118
|
+
throw new errors_1.CryptoError(`Invalid chunk tag: 0x${tag.toString(16)}`, errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
119
|
+
}
|
|
120
|
+
if (tag === exports.CHUNK_TAG_FINAL) {
|
|
121
|
+
if (this.queue.length > full) {
|
|
122
|
+
throw new errors_1.CryptoError("Final chunk is longer than the chunk size", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (this.queue.length < full)
|
|
127
|
+
break;
|
|
128
|
+
out.push(this.open(this.queue.take(full)));
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
finish() {
|
|
133
|
+
if (this.queue.length === 0)
|
|
134
|
+
throw new errors_1.CryptoError(this.truncated.finalMissing, errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
135
|
+
if (this.queue.first() !== exports.CHUNK_TAG_FINAL) {
|
|
136
|
+
throw new errors_1.CryptoError(this.truncated.incomplete, errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
137
|
+
}
|
|
138
|
+
return this.open(this.queue.take(this.queue.length));
|
|
139
|
+
}
|
|
140
|
+
open(chunk) {
|
|
141
|
+
const nonce = this.chunkNonce(this.baseNonce, this.counter++, chunk[0]);
|
|
142
|
+
const body = chunk.subarray(1);
|
|
143
|
+
if (this.key)
|
|
144
|
+
return (0, chacha_js_1.xchacha20poly1305)(this.key, nonce).decrypt(body);
|
|
145
|
+
const candidates = this.keys instanceof Uint8Array ? [this.keys] : this.keys;
|
|
146
|
+
let firstError;
|
|
147
|
+
for (const key of candidates) {
|
|
148
|
+
try {
|
|
149
|
+
const plaintext = (0, chacha_js_1.xchacha20poly1305)(key, nonce).decrypt(body);
|
|
150
|
+
this.key = key;
|
|
151
|
+
return plaintext;
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
firstError ??= e;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
throw firstError;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.ChunkDecryptor = ChunkDecryptor;
|
|
161
|
+
function decryptChunks(decryptor, body) {
|
|
162
|
+
const parts = decryptor.push(body);
|
|
163
|
+
parts.push(decryptor.finish());
|
|
164
|
+
return Uint8Array.from((0, encoding_1.concat)(parts));
|
|
165
|
+
}
|
|
23
166
|
function encryptChunksInto(plaintext, key, baseNonce, chunkSize, output, offset, chunkNonce) {
|
|
24
|
-
const
|
|
25
|
-
for (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const end = isFinal ? plaintext.length : (i + 1) * chunkSize;
|
|
29
|
-
const chunk = plaintext.subarray(i * chunkSize, end);
|
|
30
|
-
const nonce = chunkNonce(baseNonce, i, tag);
|
|
31
|
-
const encrypted = (0, chacha_js_1.xchacha20poly1305)(key, nonce).encrypt(chunk);
|
|
32
|
-
output[offset] = tag;
|
|
33
|
-
output.set(encrypted, offset + 1);
|
|
34
|
-
offset += 1 + encrypted.length;
|
|
167
|
+
const encryptor = new ChunkEncryptor(key, baseNonce, chunkSize, chunkNonce);
|
|
168
|
+
for (const chunk of [...encryptor.push(plaintext), encryptor.finish()]) {
|
|
169
|
+
output.set(chunk, offset);
|
|
170
|
+
offset += chunk.length;
|
|
35
171
|
}
|
|
36
172
|
}
|
|
37
173
|
//# sourceMappingURL=chunks.js.map
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { ChunkDecryptor, ChunkEncryptor } from "./chunks";
|
|
2
|
+
export declare const AEAD_STREAM_HEADER_LEN = 24;
|
|
3
|
+
export declare const DEFAULT_CHUNK_SIZE: number;
|
|
1
4
|
export interface StreamEncryptOptions {
|
|
2
5
|
key: string | Uint8Array;
|
|
3
6
|
plaintext: Uint8Array;
|
|
@@ -12,6 +15,12 @@ export interface StreamDecryptOptions {
|
|
|
12
15
|
ciphertext: Uint8Array;
|
|
13
16
|
chunkSize?: number;
|
|
14
17
|
}
|
|
18
|
+
export declare function toKey(key: string | Uint8Array): Uint8Array;
|
|
19
|
+
export declare function aeadStreamSender(key: Uint8Array, chunkSize: number): {
|
|
20
|
+
header: Uint8Array;
|
|
21
|
+
encryptor: ChunkEncryptor;
|
|
22
|
+
};
|
|
23
|
+
export declare function aeadStreamReceiver(key: Uint8Array, header: Uint8Array, chunkSize: number): ChunkDecryptor;
|
|
15
24
|
export declare function streamEncrypt(options: StreamEncryptOptions): StreamEncryptResult;
|
|
16
25
|
export declare function streamDecrypt(options: StreamDecryptOptions): Uint8Array;
|
|
17
26
|
//# sourceMappingURL=stream-aead.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-aead.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-aead.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-aead.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-aead.ts"],"names":[],"mappings":"AAuBA,OAAO,EACL,cAAc,EACd,cAAc,EAMf,MAAM,UAAU,CAAC;AAOlB,eAAO,MAAM,sBAAsB,KAAY,CAAC;AAEhD,eAAO,MAAM,kBAAkB,QAAY,CAAC;AAgC5C,MAAM,WAAW,oBAAoB;IAEnC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC;IAEzB,SAAS,EAAE,UAAU,CAAC;IAEtB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAGD,MAAM,WAAW,mBAAmB;IAElC,UAAU,EAAE,UAAU,CAAC;IAEvB,SAAS,EAAE,2BAA2B,CAAC;CACxC;AAGD,MAAM,WAAW,oBAAoB;IAEnC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC;IAEzB,UAAU,EAAE,UAAU,CAAC;IAEvB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAGD,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,CAoB1D;AAMD,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,UAAU,EACf,SAAS,EAAE,MAAM,GAChB;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,cAAc,CAAA;CAAE,CAMnD;AAGD,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,UAAU,EACf,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,MAAM,GAChB,cAAc,CAEhB;AAQD,wBAAgB,aAAa,CAC3B,OAAO,EAAE,oBAAoB,GAC5B,mBAAmB,CAyBrB;AAQD,wBAAgB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,UAAU,CAqBvE"}
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_CHUNK_SIZE = exports.AEAD_STREAM_HEADER_LEN = void 0;
|
|
4
|
+
exports.toKey = toKey;
|
|
5
|
+
exports.aeadStreamSender = aeadStreamSender;
|
|
6
|
+
exports.aeadStreamReceiver = aeadStreamReceiver;
|
|
3
7
|
exports.streamEncrypt = streamEncrypt;
|
|
4
8
|
exports.streamDecrypt = streamDecrypt;
|
|
5
|
-
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
6
9
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
7
10
|
const chunks_1 = require("./chunks");
|
|
11
|
+
const errors_1 = require("../errors");
|
|
12
|
+
const encoding_1 = require("../encoding");
|
|
8
13
|
const NONCE_LEN = 24;
|
|
9
|
-
|
|
14
|
+
exports.AEAD_STREAM_HEADER_LEN = NONCE_LEN;
|
|
15
|
+
exports.DEFAULT_CHUNK_SIZE = 64 * 1024;
|
|
10
16
|
function deriveChunkNonce(baseNonce, counter, tag) {
|
|
11
17
|
const nonce = new Uint8Array(baseNonce);
|
|
12
18
|
const view = new DataView(nonce.buffer, nonce.byteOffset, nonce.byteLength);
|
|
@@ -17,25 +23,34 @@ function deriveChunkNonce(baseNonce, counter, tag) {
|
|
|
17
23
|
nonce[0] = nonce[0] ^ tag;
|
|
18
24
|
return nonce;
|
|
19
25
|
}
|
|
20
|
-
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
21
26
|
function toKey(key) {
|
|
22
27
|
if (key instanceof Uint8Array) {
|
|
23
28
|
if (key.length !== 32) {
|
|
24
|
-
throw new
|
|
29
|
+
throw new errors_1.CryptoError(`Key must be 32 bytes, got ${key.length}`, errors_1.CryptoErrorCode.INVALID_KEY);
|
|
25
30
|
}
|
|
26
31
|
return key;
|
|
27
32
|
}
|
|
28
|
-
if (!
|
|
29
|
-
throw new
|
|
30
|
-
const buf =
|
|
33
|
+
if (!(0, encoding_1.isHex)(key))
|
|
34
|
+
throw new errors_1.CryptoError("Invalid hex key", errors_1.CryptoErrorCode.INVALID_HEX);
|
|
35
|
+
const buf = (0, encoding_1.decodeHex)(key);
|
|
31
36
|
if (buf.length !== 32) {
|
|
32
|
-
throw new
|
|
37
|
+
throw new errors_1.CryptoError(`Key must be 32 bytes (256 bits), got ${buf.length}`, errors_1.CryptoErrorCode.INVALID_KEY);
|
|
33
38
|
}
|
|
34
39
|
return buf;
|
|
35
40
|
}
|
|
41
|
+
function aeadStreamSender(key, chunkSize) {
|
|
42
|
+
const baseNonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
|
|
43
|
+
return {
|
|
44
|
+
header: baseNonce,
|
|
45
|
+
encryptor: new chunks_1.ChunkEncryptor(key, baseNonce, chunkSize, deriveChunkNonce),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function aeadStreamReceiver(key, header, chunkSize) {
|
|
49
|
+
return new chunks_1.ChunkDecryptor(key, header, chunkSize, deriveChunkNonce);
|
|
50
|
+
}
|
|
36
51
|
function streamEncrypt(options) {
|
|
37
52
|
const key = toKey(options.key);
|
|
38
|
-
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
53
|
+
const chunkSize = options.chunkSize ?? exports.DEFAULT_CHUNK_SIZE;
|
|
39
54
|
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
40
55
|
const plaintext = options.plaintext;
|
|
41
56
|
const baseNonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
|
|
@@ -47,56 +62,15 @@ function streamEncrypt(options) {
|
|
|
47
62
|
algorithm: "xchacha20-poly1305-stream",
|
|
48
63
|
};
|
|
49
64
|
}
|
|
50
|
-
function readChunkHeader(ciphertext, offset, chunkSize) {
|
|
51
|
-
const chunkTag = ciphertext[offset];
|
|
52
|
-
if (chunkTag !== chunks_1.CHUNK_TAG_MESSAGE && chunkTag !== chunks_1.CHUNK_TAG_FINAL) {
|
|
53
|
-
throw new Error(`Invalid chunk tag: 0x${chunkTag.toString(16)}`);
|
|
54
|
-
}
|
|
55
|
-
const isFinal = chunkTag === chunks_1.CHUNK_TAG_FINAL;
|
|
56
|
-
const start = offset + 1;
|
|
57
|
-
const encLen = isFinal ? ciphertext.length - start : chunkSize + chunks_1.TAG_LEN;
|
|
58
|
-
if (start + encLen > ciphertext.length) {
|
|
59
|
-
throw new Error("Ciphertext truncated — incomplete chunk");
|
|
60
|
-
}
|
|
61
|
-
return { chunkTag, isFinal, encLen };
|
|
62
|
-
}
|
|
63
65
|
function streamDecrypt(options) {
|
|
64
66
|
const key = toKey(options.key);
|
|
65
|
-
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
67
|
+
const chunkSize = options.chunkSize ?? exports.DEFAULT_CHUNK_SIZE;
|
|
66
68
|
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
67
69
|
const ciphertext = options.ciphertext;
|
|
68
70
|
if (ciphertext.length < NONCE_LEN + 1 + chunks_1.TAG_LEN) {
|
|
69
|
-
throw new
|
|
71
|
+
throw new errors_1.CryptoError("Ciphertext too short — missing header or chunk data", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
|
|
70
72
|
}
|
|
71
73
|
const baseNonce = ciphertext.subarray(0, NONCE_LEN);
|
|
72
|
-
|
|
73
|
-
const plaintextChunks = [];
|
|
74
|
-
let totalPtLen = 0;
|
|
75
|
-
let sawFinal = false;
|
|
76
|
-
while (offset < ciphertext.length) {
|
|
77
|
-
const { chunkTag, isFinal, encLen } = readChunkHeader(ciphertext, offset, chunkSize);
|
|
78
|
-
offset += 1;
|
|
79
|
-
const chunkIdx = plaintextChunks.length;
|
|
80
|
-
const nonce = deriveChunkNonce(baseNonce, chunkIdx, chunkTag);
|
|
81
|
-
const cipher = (0, chacha_js_1.xchacha20poly1305)(key, nonce);
|
|
82
|
-
const decrypted = cipher.decrypt(ciphertext.subarray(offset, offset + encLen));
|
|
83
|
-
plaintextChunks.push(decrypted);
|
|
84
|
-
totalPtLen += decrypted.length;
|
|
85
|
-
offset += encLen;
|
|
86
|
-
if (isFinal) {
|
|
87
|
-
sawFinal = true;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (!sawFinal) {
|
|
92
|
-
throw new Error("Ciphertext truncated — final chunk missing");
|
|
93
|
-
}
|
|
94
|
-
const result = new Uint8Array(totalPtLen);
|
|
95
|
-
let pos = 0;
|
|
96
|
-
for (const chunk of plaintextChunks) {
|
|
97
|
-
result.set(chunk, pos);
|
|
98
|
-
pos += chunk.length;
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
74
|
+
return (0, chunks_1.decryptChunks)(new chunks_1.ChunkDecryptor(key, baseNonce, chunkSize, deriveChunkNonce), ciphertext.subarray(NONCE_LEN));
|
|
101
75
|
}
|
|
102
76
|
//# sourceMappingURL=stream-aead.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-hash.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-hash.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-hash.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-hash.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,sBAAsB,sFAQzB,CAAC;AAEX,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAoB1E,MAAM,WAAW,eAAe;IAE9B,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,eAAe,CAAC;IAEnD,MAAM,IAAI,MAAM,CAAC;IAEjB,WAAW,IAAI,UAAU,CAAC;CAC3B;AAYD,wBAAgB,YAAY,CAAC,SAAS,EAAE,mBAAmB,GAAG,eAAe,CA+B5E"}
|
|
@@ -6,6 +6,8 @@ const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
|
6
6
|
const sha3_js_1 = require("@noble/hashes/sha3.js");
|
|
7
7
|
const blake2_js_1 = require("@noble/hashes/blake2.js");
|
|
8
8
|
const blake3_js_1 = require("@noble/hashes/blake3.js");
|
|
9
|
+
const errors_1 = require("../errors");
|
|
10
|
+
const encoding_1 = require("../encoding");
|
|
9
11
|
exports.STREAM_HASH_ALGORITHMS = [
|
|
10
12
|
"sha256",
|
|
11
13
|
"sha384",
|
|
@@ -27,21 +29,21 @@ const factories = {
|
|
|
27
29
|
function createHasher(algorithm) {
|
|
28
30
|
const factory = factories[algorithm];
|
|
29
31
|
if (!factory) {
|
|
30
|
-
throw new
|
|
32
|
+
throw new errors_1.CryptoError(`Unsupported streaming hash algorithm: ${algorithm}`, errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
|
|
31
33
|
}
|
|
32
34
|
const inner = factory();
|
|
33
35
|
let finalized = false;
|
|
34
36
|
return {
|
|
35
37
|
update(data) {
|
|
36
38
|
if (finalized)
|
|
37
|
-
throw new
|
|
38
|
-
const bytes = typeof data === "string" ?
|
|
39
|
+
throw new errors_1.CryptoError("Hasher already finalized", errors_1.CryptoErrorCode.INVALID_STATE);
|
|
40
|
+
const bytes = typeof data === "string" ? (0, encoding_1.utf8)(data) : data;
|
|
39
41
|
inner.update(bytes);
|
|
40
42
|
return this;
|
|
41
43
|
},
|
|
42
44
|
digest() {
|
|
43
45
|
finalized = true;
|
|
44
|
-
return
|
|
46
|
+
return (0, encoding_1.toHex)(inner.digest());
|
|
45
47
|
},
|
|
46
48
|
digestBytes() {
|
|
47
49
|
finalized = true;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ChunkDecryptor, ChunkEncryptor } from "./chunks";
|
|
1
2
|
export declare const MULTI_PQ_STREAM_MAGIC: Uint8Array<ArrayBuffer>;
|
|
2
3
|
export declare const MULTI_PQ_STREAM_VERSION = 1;
|
|
3
4
|
export declare const MULTI_PQ_PREFIX_LEN = 35;
|
|
@@ -31,5 +32,16 @@ export interface StreamMultiPqDecryptResult {
|
|
|
31
32
|
recipientId: string;
|
|
32
33
|
}
|
|
33
34
|
export declare function streamMultiPqEncrypt(options: StreamMultiPqEncryptOptions): StreamMultiPqEncryptResult;
|
|
35
|
+
export declare function multiPqStreamSender(options: Omit<StreamMultiPqEncryptOptions, "plaintext">): {
|
|
36
|
+
header: Uint8Array;
|
|
37
|
+
encryptor: ChunkEncryptor;
|
|
38
|
+
wipe: () => void;
|
|
39
|
+
};
|
|
40
|
+
export declare function multiPqHeaderLength(bytes: Uint8Array): number | undefined;
|
|
41
|
+
export declare function multiPqStreamReceiver(options: Omit<StreamMultiPqDecryptOptions, "ciphertext">, header: Uint8Array): {
|
|
42
|
+
decryptor: ChunkDecryptor;
|
|
43
|
+
recipientId: string;
|
|
44
|
+
wipe: () => void;
|
|
45
|
+
};
|
|
34
46
|
export declare function streamMultiPqDecrypt(options: StreamMultiPqDecryptOptions): StreamMultiPqDecryptResult;
|
|
35
47
|
//# sourceMappingURL=stream-multi-pq-aead.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-multi-pq-aead.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-multi-pq-aead.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-multi-pq-aead.d.ts","sourceRoot":"","sources":["../../src/streaming/stream-multi-pq-aead.ts"],"names":[],"mappings":"AAwCA,OAAO,EACL,cAAc,EACd,cAAc,EAIf,MAAM,UAAU,CAAC;AAKlB,eAAO,MAAM,qBAAqB,yBAA2C,CAAC;AAG9E,eAAO,MAAM,uBAAuB,IAAO,CAAC;AAG5C,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAGtC,eAAO,MAAM,OAAO,KAAK,CAAC;AAG1B,eAAO,MAAM,eAAe,QAAoB,CAAC;AAGjD,eAAO,MAAM,mBAAmB,QACiC,CAAC;AAOlE,MAAM,WAAW,gBAAgB;IAE/B,WAAW,EAAE,MAAM,CAAC;IAEpB,qBAAqB,EAAE,MAAM,GAAG,UAAU,CAAC;IAE3C,oBAAoB,EAAE,MAAM,GAAG,UAAU,CAAC;CAC3C;AAGD,MAAM,WAAW,2BAA2B;IAE1C,UAAU,EAAE,gBAAgB,EAAE,CAAC;IAE/B,SAAS,EAAE,UAAU,CAAC;IAEtB,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAGD,MAAM,WAAW,0BAA0B;IAEzC,UAAU,EAAE,UAAU,CAAC;IAEvB,SAAS,EAAE,mDAAmD,CAAC;IAE/D,cAAc,EAAE,MAAM,CAAC;CACxB;AAGD,MAAM,WAAW,2BAA2B;IAE1C,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEjC,qBAAqB,EAAE,MAAM,GAAG,UAAU,CAAC;IAE3C,oBAAoB,EAAE,MAAM,GAAG,UAAU,CAAC;IAE1C,UAAU,EAAE,UAAU,CAAC;IAEvB,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAGD,MAAM,WAAW,0BAA0B;IAEzC,SAAS,EAAE,UAAU,CAAC;IAEtB,WAAW,EAAE,MAAM,CAAC;CACrB;AAuJD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,2BAA2B,GACnC,0BAA0B,CAY5B;AAOD,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,IAAI,CAAC,2BAA2B,EAAE,WAAW,CAAC,GACtD;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,cAAc,CAAC;IAAC,IAAI,EAAE,MAAM,IAAI,CAAA;CAAE,CAiBrE;AA8JD,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CASzE;AAOD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,IAAI,CAAC,2BAA2B,EAAE,YAAY,CAAC,EACxD,MAAM,EAAE,UAAU,GACjB;IAAE,SAAS,EAAE,cAAc,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,IAAI,CAAA;CAAE,CA+BtE;AA6CD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,2BAA2B,GACnC,0BAA0B,CAiB5B"}
|