@sebastienrousseau/crypto-lib 0.0.33 → 0.0.35
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 +22 -18
- package/dist/streaming/chunks.d.ts +9 -0
- package/dist/streaming/chunks.d.ts.map +1 -0
- package/dist/streaming/chunks.js +37 -0
- package/dist/streaming/stream-aead.d.ts.map +1 -1
- package/dist/streaming/stream-aead.js +9 -32
- package/dist/streaming/stream-multi-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-multi-pq-aead.js +14 -47
- package/dist/streaming/stream-pq-aead.d.ts +2 -3
- package/dist/streaming/stream-pq-aead.d.ts.map +1 -1
- package/dist/streaming/stream-pq-aead.js +13 -43
- package/package.json +22 -7
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
<h1 align="center">@sebastienrousseau/crypto-lib</h1>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
|
|
10
|
+
Post-quantum cryptography for TypeScript and Node.js: ML-KEM, ML-DSA, SLH-DSA and HPKE, alongside AES-GCM, XChaCha20-Poly1305, Ed25519, X25519, SHA-3, BLAKE3, Argon2id and OpenPGP.
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
|
|
33
33
|
**The Crypto Service ecosystem**
|
|
34
34
|
|
|
35
|
-
- [The Crypto Service ecosystem](#the-crypto-service-ecosystem) — full
|
|
35
|
+
- [The Crypto Service ecosystem](#the-crypto-service-ecosystem) — full 18-package suite overview
|
|
36
36
|
|
|
37
37
|
**Package reference**
|
|
38
38
|
|
|
@@ -106,22 +106,26 @@ const ok = crypto.verify("ed25519", kp.publicKey, "message", sig);
|
|
|
106
106
|
|
|
107
107
|
Crypto Service provides a complete cryptography stack across 14 specialized packages:
|
|
108
108
|
|
|
109
|
-
| Package
|
|
110
|
-
|
|
|
111
|
-
| [`@sebastienrousseau/crypto-api`](
|
|
112
|
-
| [`@sebastienrousseau/crypto-
|
|
113
|
-
| [`@sebastienrousseau/crypto-
|
|
114
|
-
| [`@sebastienrousseau/crypto-
|
|
115
|
-
|
|
|
116
|
-
| [`@sebastienrousseau/crypto-
|
|
117
|
-
| [`@sebastienrousseau/crypto-
|
|
118
|
-
| [`@sebastienrousseau/crypto-
|
|
119
|
-
| [`@sebastienrousseau/crypto-
|
|
120
|
-
| [`@sebastienrousseau/crypto-
|
|
121
|
-
| [`@sebastienrousseau/crypto-
|
|
122
|
-
| [`@sebastienrousseau/crypto-
|
|
123
|
-
| [`@sebastienrousseau/crypto-
|
|
124
|
-
| [`@sebastienrousseau/crypto-
|
|
109
|
+
| Package | Role | Description |
|
|
110
|
+
| :---------------------------------------------------------------------------------------------------------------------------------------- | :--------------- | :----------------------------------------------------------------------------------------------------------------------------- |
|
|
111
|
+
| [`@sebastienrousseau/crypto-api`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-api) | API Schemas | Shared TypeScript types and utilities for the Crypto Service Suite, defining the canonical API surface. |
|
|
112
|
+
| [`@sebastienrousseau/crypto-benchmarks`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-benchmarks) | Benchmarks | Benchmarks comparing classical and post-quantum cryptography: ops/sec, latency and throughput. |
|
|
113
|
+
| [`@sebastienrousseau/crypto-cbom`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-cbom) | CBOM | Cryptographic Bill of Materials generator and auditor (CycloneDX 1.6, SPDX 3.0). |
|
|
114
|
+
| [`@sebastienrousseau/crypto-cli`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-cli) | Terminal CLI | Command-line interface: hashing, key generation, encryption, signing, HPKE, Argon2id and CBOM scans, with an interactive menu. |
|
|
115
|
+
| [`@sebastienrousseau/crypto-edge`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-edge) | Edge Runtime | Edge-runtime cryptographic operations using the Web Crypto API, optimized for Cloudflare Workers, Vercel Edge, and Deno. |
|
|
116
|
+
| [`@sebastienrousseau/crypto-kms`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-kms) | Cloud KMS | Unified KMS interface: AWS KMS and local providers implemented; GCP, Azure and Vault are stubs. |
|
|
117
|
+
| **[`@sebastienrousseau/crypto-lib`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-lib)** _(this package)_ | **Core Library** | **Core library: post-quantum ML-KEM, ML-DSA and SLH-DSA, plus classical, modern and OpenPGP cryptography for TypeScript.** |
|
|
118
|
+
| [`@sebastienrousseau/crypto-lsp`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-lsp) | IDE Analysis | Language server that flags weak and quantum-vulnerable cryptography in the editor, with quick fixes. |
|
|
119
|
+
| [`@sebastienrousseau/crypto-mcp`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-mcp) | AI Agents | Model Context Protocol server exposing cryptography tools to AI assistants and agents. |
|
|
120
|
+
| [`@sebastienrousseau/crypto-middleware`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-middleware) | Middleware | Cryptographic middleware for Express and Fastify: decrypt requests, encrypt responses, verify HMAC signatures and JWTs. |
|
|
121
|
+
| [`@sebastienrousseau/crypto-prisma`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-prisma) | ORM Adapter | Transparent field-level encryption extension for Prisma Client, using XChaCha20-Poly1305. |
|
|
122
|
+
| [`@sebastienrousseau/crypto-react`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-react) | React Hooks | React hooks and context provider for client-side cryptographic operations with zero boilerplate. |
|
|
123
|
+
| [`@sebastienrousseau/crypto-sdk`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-sdk) | Client SDK | A zero-dependency, typed HTTP client for the Crypto Service REST API, with full post-quantum support. |
|
|
124
|
+
| [`@sebastienrousseau/crypto-server`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-server) | HTTP API | A hardened Fastify REST API for cryptographic operations, with rate limiting, OpenAPI schemas, and post-quantum endpoints. |
|
|
125
|
+
| [`@sebastienrousseau/crypto-testing`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-testing) | Test Support | Deterministic keys, fast mocks, and test fixtures for crypto-lib |
|
|
126
|
+
| [`@sebastienrousseau/crypto-typeorm`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-typeorm) | ORM Adapter | TypeORM column-level encryption with a single decorator, powered by crypto-lib. |
|
|
127
|
+
| [`@sebastienrousseau/crypto-vue`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-vue) | Vue Composables | Vue 3 composables for client-side cryptography |
|
|
128
|
+
| [`@sebastienrousseau/crypto-wasm`](https://github.com/sebastienrousseau/crypto-service/tree/main/packages/crypto-wasm) | Acceleration | Placeholder for a WebAssembly accelerator; no WebAssembly module ships yet. |
|
|
125
129
|
|
|
126
130
|
<p align="right"><a href="#contents">Back to Top</a></p>
|
|
127
131
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function assertChunkSize(chunkSize: number): void;
|
|
2
|
+
export declare const TAG_LEN = 16;
|
|
3
|
+
export declare const CHUNK_TAG_MESSAGE = 0;
|
|
4
|
+
export declare const CHUNK_TAG_FINAL = 1;
|
|
5
|
+
export type ChunkNonce = (baseNonce: Uint8Array, counter: number, tag: number) => Uint8Array;
|
|
6
|
+
export declare function chunkCount(ptLen: number, chunkSize: number): number;
|
|
7
|
+
export declare function streamSize(headerLen: number, ptLen: number, chunkSize: number): number;
|
|
8
|
+
export declare function encryptChunksInto(plaintext: Uint8Array, key: Uint8Array, baseNonce: Uint8Array, chunkSize: number, output: Uint8Array, offset: number, chunkNonce: ChunkNonce): void;
|
|
9
|
+
//# sourceMappingURL=chunks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chunks.d.ts","sourceRoot":"","sources":["../../src/streaming/chunks.ts"],"names":[],"mappings":"AAgBA,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAIvD;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;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,CAaN"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CHUNK_TAG_FINAL = exports.CHUNK_TAG_MESSAGE = exports.TAG_LEN = void 0;
|
|
4
|
+
exports.assertChunkSize = assertChunkSize;
|
|
5
|
+
exports.chunkCount = chunkCount;
|
|
6
|
+
exports.streamSize = streamSize;
|
|
7
|
+
exports.encryptChunksInto = encryptChunksInto;
|
|
8
|
+
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
9
|
+
function assertChunkSize(chunkSize) {
|
|
10
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize < 1) {
|
|
11
|
+
throw new Error(`chunkSize must be a positive integer, got ${chunkSize}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.TAG_LEN = 16;
|
|
15
|
+
exports.CHUNK_TAG_MESSAGE = 0x00;
|
|
16
|
+
exports.CHUNK_TAG_FINAL = 0x01;
|
|
17
|
+
function chunkCount(ptLen, chunkSize) {
|
|
18
|
+
return Math.max(1, Math.ceil(ptLen / chunkSize));
|
|
19
|
+
}
|
|
20
|
+
function streamSize(headerLen, ptLen, chunkSize) {
|
|
21
|
+
return headerLen + ptLen + chunkCount(ptLen, chunkSize) * (1 + exports.TAG_LEN);
|
|
22
|
+
}
|
|
23
|
+
function encryptChunksInto(plaintext, key, baseNonce, chunkSize, output, offset, chunkNonce) {
|
|
24
|
+
const total = chunkCount(plaintext.length, chunkSize);
|
|
25
|
+
for (let i = 0; i < total; i++) {
|
|
26
|
+
const isFinal = i === total - 1;
|
|
27
|
+
const tag = isFinal ? exports.CHUNK_TAG_FINAL : exports.CHUNK_TAG_MESSAGE;
|
|
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;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=chunks.js.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":"AAoEA,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;AA2BD,wBAAgB,aAAa,CAC3B,OAAO,EAAE,oBAAoB,GAC5B,mBAAmB,CAyBrB;AAsCD,wBAAgB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,UAAU,CAyDvE"}
|
|
@@ -4,11 +4,9 @@ exports.streamEncrypt = streamEncrypt;
|
|
|
4
4
|
exports.streamDecrypt = streamDecrypt;
|
|
5
5
|
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
6
6
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
7
|
+
const chunks_1 = require("./chunks");
|
|
7
8
|
const NONCE_LEN = 24;
|
|
8
|
-
const TAG_LEN = 16;
|
|
9
9
|
const DEFAULT_CHUNK_SIZE = 64 * 1024;
|
|
10
|
-
const CHUNK_TAG_MESSAGE = 0x00;
|
|
11
|
-
const CHUNK_TAG_FINAL = 0x01;
|
|
12
10
|
function deriveChunkNonce(baseNonce, counter, tag) {
|
|
13
11
|
const nonce = new Uint8Array(baseNonce);
|
|
14
12
|
const view = new DataView(nonce.buffer, nonce.byteOffset, nonce.byteLength);
|
|
@@ -38,34 +36,12 @@ function toKey(key) {
|
|
|
38
36
|
function streamEncrypt(options) {
|
|
39
37
|
const key = toKey(options.key);
|
|
40
38
|
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
39
|
+
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
41
40
|
const plaintext = options.plaintext;
|
|
42
41
|
const baseNonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
|
|
43
|
-
const
|
|
44
|
-
const lastChunkLen = plaintext.length - numFullChunks * chunkSize;
|
|
45
|
-
const hasExtraChunk = lastChunkLen > 0 || plaintext.length === 0;
|
|
46
|
-
const totalChunks = hasExtraChunk ? numFullChunks + 1 : numFullChunks;
|
|
47
|
-
let outputSize = NONCE_LEN;
|
|
48
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
49
|
-
const ptLen = i < numFullChunks ? chunkSize : lastChunkLen > 0 ? lastChunkLen : 0;
|
|
50
|
-
outputSize += 1 + ptLen + TAG_LEN;
|
|
51
|
-
}
|
|
52
|
-
const output = new Uint8Array(outputSize);
|
|
42
|
+
const output = new Uint8Array((0, chunks_1.streamSize)(NONCE_LEN, plaintext.length, chunkSize));
|
|
53
43
|
output.set(baseNonce);
|
|
54
|
-
|
|
55
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
56
|
-
const isFinal = i === totalChunks - 1;
|
|
57
|
-
const chunkTag = isFinal ? CHUNK_TAG_FINAL : CHUNK_TAG_MESSAGE;
|
|
58
|
-
const start = i * chunkSize;
|
|
59
|
-
const end = isFinal ? plaintext.length : start + chunkSize;
|
|
60
|
-
const chunk = plaintext.subarray(start, end);
|
|
61
|
-
const nonce = deriveChunkNonce(baseNonce, i, chunkTag);
|
|
62
|
-
const cipher = (0, chacha_js_1.xchacha20poly1305)(key, nonce);
|
|
63
|
-
const encrypted = cipher.encrypt(chunk);
|
|
64
|
-
output[offset] = chunkTag;
|
|
65
|
-
offset += 1;
|
|
66
|
-
output.set(encrypted, offset);
|
|
67
|
-
offset += encrypted.length;
|
|
68
|
-
}
|
|
44
|
+
(0, chunks_1.encryptChunksInto)(plaintext, key, baseNonce, chunkSize, output, NONCE_LEN, deriveChunkNonce);
|
|
69
45
|
return {
|
|
70
46
|
ciphertext: output,
|
|
71
47
|
algorithm: "xchacha20-poly1305-stream",
|
|
@@ -73,12 +49,12 @@ function streamEncrypt(options) {
|
|
|
73
49
|
}
|
|
74
50
|
function readChunkHeader(ciphertext, offset, chunkSize) {
|
|
75
51
|
const chunkTag = ciphertext[offset];
|
|
76
|
-
if (chunkTag !== CHUNK_TAG_MESSAGE && chunkTag !== CHUNK_TAG_FINAL) {
|
|
52
|
+
if (chunkTag !== chunks_1.CHUNK_TAG_MESSAGE && chunkTag !== chunks_1.CHUNK_TAG_FINAL) {
|
|
77
53
|
throw new Error(`Invalid chunk tag: 0x${chunkTag.toString(16)}`);
|
|
78
54
|
}
|
|
79
|
-
const isFinal = chunkTag === CHUNK_TAG_FINAL;
|
|
55
|
+
const isFinal = chunkTag === chunks_1.CHUNK_TAG_FINAL;
|
|
80
56
|
const start = offset + 1;
|
|
81
|
-
const encLen = isFinal ? ciphertext.length - start : chunkSize + TAG_LEN;
|
|
57
|
+
const encLen = isFinal ? ciphertext.length - start : chunkSize + chunks_1.TAG_LEN;
|
|
82
58
|
if (start + encLen > ciphertext.length) {
|
|
83
59
|
throw new Error("Ciphertext truncated — incomplete chunk");
|
|
84
60
|
}
|
|
@@ -87,8 +63,9 @@ function readChunkHeader(ciphertext, offset, chunkSize) {
|
|
|
87
63
|
function streamDecrypt(options) {
|
|
88
64
|
const key = toKey(options.key);
|
|
89
65
|
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
66
|
+
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
90
67
|
const ciphertext = options.ciphertext;
|
|
91
|
-
if (ciphertext.length < NONCE_LEN + 1 + TAG_LEN) {
|
|
68
|
+
if (ciphertext.length < NONCE_LEN + 1 + chunks_1.TAG_LEN) {
|
|
92
69
|
throw new Error("Ciphertext too short — missing header or chunk data");
|
|
93
70
|
}
|
|
94
71
|
const baseNonce = ciphertext.subarray(0, NONCE_LEN);
|
|
@@ -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":"AAqCA,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;AAgJD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,2BAA2B,GACnC,0BAA0B,CAoC5B;AAwND,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,2BAA2B,GACnC,0BAA0B,CAyC5B"}
|
|
@@ -11,6 +11,7 @@ const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
|
11
11
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
12
12
|
const utils_1 = require("../utils");
|
|
13
13
|
const stream_pq_aead_1 = require("./stream-pq-aead");
|
|
14
|
+
const chunks_1 = require("./chunks");
|
|
14
15
|
exports.MULTI_PQ_STREAM_MAGIC = new Uint8Array([0x4d, 0x53, 0x50, 0x51]);
|
|
15
16
|
exports.MULTI_PQ_STREAM_VERSION = 0x01;
|
|
16
17
|
exports.MULTI_PQ_PREFIX_LEN = 35;
|
|
@@ -75,38 +76,6 @@ function serializeSlot(idBytes, ephPub, mlKemCt, slotNonce, wrappedCek) {
|
|
|
75
76
|
out.set(wrappedCek, pos);
|
|
76
77
|
return out;
|
|
77
78
|
}
|
|
78
|
-
function computeMultiStreamOutputSize(headerLen, ptLen, chunkSize) {
|
|
79
|
-
const numFullChunks = Math.floor(ptLen / chunkSize);
|
|
80
|
-
const lastChunkLen = ptLen - numFullChunks * chunkSize;
|
|
81
|
-
const hasExtra = lastChunkLen > 0 || ptLen === 0;
|
|
82
|
-
const totalChunks = hasExtra ? numFullChunks + 1 : numFullChunks;
|
|
83
|
-
let totalSize = headerLen;
|
|
84
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
85
|
-
const len = i < numFullChunks ? chunkSize : lastChunkLen > 0 ? lastChunkLen : 0;
|
|
86
|
-
totalSize += 1 + len + stream_pq_aead_1.TAG_LEN;
|
|
87
|
-
}
|
|
88
|
-
return totalSize;
|
|
89
|
-
}
|
|
90
|
-
function encryptStreamChunks(plaintext, cek, baseNonce, chunkSize, output, headerLen) {
|
|
91
|
-
const numFullChunks = Math.floor(plaintext.length / chunkSize);
|
|
92
|
-
const hasExtra = plaintext.length % chunkSize > 0 || plaintext.length === 0;
|
|
93
|
-
const totalChunks = hasExtra ? numFullChunks + 1 : numFullChunks;
|
|
94
|
-
let offset = headerLen;
|
|
95
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
96
|
-
const isFinal = i === totalChunks - 1;
|
|
97
|
-
const chunkTag = isFinal ? stream_pq_aead_1.CHUNK_TAG_FINAL : stream_pq_aead_1.CHUNK_TAG_MESSAGE;
|
|
98
|
-
const start = i * chunkSize;
|
|
99
|
-
const end = isFinal ? plaintext.length : start + chunkSize;
|
|
100
|
-
const chunk = plaintext.subarray(start, end);
|
|
101
|
-
const nonce = (0, stream_pq_aead_1.derivePqChunkNonce)(baseNonce, i, chunkTag);
|
|
102
|
-
const cipher = (0, chacha_js_1.xchacha20poly1305)(cek, nonce);
|
|
103
|
-
const encrypted = cipher.encrypt(chunk);
|
|
104
|
-
output[offset] = chunkTag;
|
|
105
|
-
offset += 1;
|
|
106
|
-
output.set(encrypted, offset);
|
|
107
|
-
offset += encrypted.length;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
79
|
function writeHeaderPrefix(out, chunkSize, baseNonce, count) {
|
|
111
80
|
out.set(exports.MULTI_PQ_STREAM_MAGIC, 0);
|
|
112
81
|
out[4] = exports.MULTI_PQ_STREAM_VERSION;
|
|
@@ -115,7 +84,7 @@ function writeHeaderPrefix(out, chunkSize, baseNonce, count) {
|
|
|
115
84
|
out.set(baseNonce, 9);
|
|
116
85
|
view.setUint16(33, count, false);
|
|
117
86
|
}
|
|
118
|
-
function
|
|
87
|
+
function checkEncryptOptions(options) {
|
|
119
88
|
const recipients = options.recipients;
|
|
120
89
|
if (!Array.isArray(recipients) || recipients.length === 0) {
|
|
121
90
|
throw new Error("At least one recipient is required");
|
|
@@ -124,28 +93,26 @@ function streamMultiPqEncrypt(options) {
|
|
|
124
93
|
throw new Error("Recipient count exceeds maximum limit of 1000");
|
|
125
94
|
}
|
|
126
95
|
const chunkSize = options.chunkSize ?? stream_pq_aead_1.DEFAULT_CHUNK_SIZE;
|
|
96
|
+
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
127
97
|
if (chunkSize < 1024 || chunkSize > 16 * 1024 * 1024) {
|
|
128
98
|
throw new Error("Chunk size must be between 1024 and 16777216 bytes");
|
|
129
99
|
}
|
|
100
|
+
return { recipients, chunkSize };
|
|
101
|
+
}
|
|
102
|
+
function streamMultiPqEncrypt(options) {
|
|
103
|
+
const { recipients, chunkSize } = checkEncryptOptions(options);
|
|
130
104
|
const cek = (0, utils_js_1.randomBytes)(exports.CEK_LEN);
|
|
131
105
|
const baseNonce = (0, utils_js_1.randomBytes)(stream_pq_aead_1.NONCE_LEN);
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const slotBytes = createSlot(cek, i, recipients[i]);
|
|
136
|
-
serializedSlots.push(slotBytes);
|
|
137
|
-
slotsTotalLen += slotBytes.length;
|
|
138
|
-
}
|
|
139
|
-
const headerLen = exports.MULTI_PQ_PREFIX_LEN + slotsTotalLen;
|
|
140
|
-
const totalSize = computeMultiStreamOutputSize(headerLen, options.plaintext.length, chunkSize);
|
|
141
|
-
const output = new Uint8Array(totalSize);
|
|
106
|
+
const slots = recipients.map((r, i) => createSlot(cek, i, r));
|
|
107
|
+
const headerLen = exports.MULTI_PQ_PREFIX_LEN + slots.reduce((sum, slot) => sum + slot.length, 0);
|
|
108
|
+
const output = new Uint8Array((0, chunks_1.streamSize)(headerLen, options.plaintext.length, chunkSize));
|
|
142
109
|
writeHeaderPrefix(output, chunkSize, baseNonce, recipients.length);
|
|
143
110
|
let slotOffset = exports.MULTI_PQ_PREFIX_LEN;
|
|
144
|
-
for (const
|
|
145
|
-
output.set(
|
|
146
|
-
slotOffset +=
|
|
111
|
+
for (const slot of slots) {
|
|
112
|
+
output.set(slot, slotOffset);
|
|
113
|
+
slotOffset += slot.length;
|
|
147
114
|
}
|
|
148
|
-
|
|
115
|
+
(0, chunks_1.encryptChunksInto)(options.plaintext, cek, baseNonce, chunkSize, output, headerLen, stream_pq_aead_1.derivePqChunkNonce);
|
|
149
116
|
(0, utils_1.wipeMemory)(cek);
|
|
150
117
|
return {
|
|
151
118
|
ciphertext: output,
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
+
import { CHUNK_TAG_FINAL, CHUNK_TAG_MESSAGE, TAG_LEN } from "./chunks";
|
|
2
|
+
export { CHUNK_TAG_FINAL, CHUNK_TAG_MESSAGE, TAG_LEN };
|
|
1
3
|
export declare const EPHEMERAL_LEN = 32;
|
|
2
4
|
export declare const ML_KEM_CT_LEN = 1088;
|
|
3
5
|
export declare const NONCE_LEN = 24;
|
|
4
|
-
export declare const TAG_LEN = 16;
|
|
5
6
|
export declare const PQ_STREAM_HEADER_LEN: number;
|
|
6
7
|
export declare const DEFAULT_CHUNK_SIZE: number;
|
|
7
|
-
export declare const CHUNK_TAG_MESSAGE = 0;
|
|
8
|
-
export declare const CHUNK_TAG_FINAL = 1;
|
|
9
8
|
export declare function toBytes(data: string | Uint8Array, expectedLen?: number, name?: string): Uint8Array;
|
|
10
9
|
export declare function derivePqChunkNonce(baseNonce: Uint8Array, counter: number, tag: number): Uint8Array;
|
|
11
10
|
export interface StreamPqEncryptOptions {
|
|
@@ -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":["../../src/streaming/stream-pq-aead.ts"],"names":[],"mappings":"AAsBA,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,OAAO,EAIR,MAAM,UAAU,CAAC;AAIlB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,OAAO,EAAE,CAAC;AAGvD,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,eAAO,MAAM,aAAa,OAAO,CAAC;AAElC,eAAO,MAAM,SAAS,KAAK,CAAC;AAE5B,eAAO,MAAM,oBAAoB,QAA4C,CAAC;AAE9E,eAAO,MAAM,kBAAkB,QAAY,CAAC;AAK5C,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,WAAW,CAAC,EAAE,MAAM,EACpB,IAAI,SAAS,GACZ,UAAU,CAYZ;AAGD,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,UAAU,EACrB,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,GACV,UAAU,CAiBZ;AAGD,MAAM,WAAW,sBAAsB;IACrC,qBAAqB,EAAE,MAAM,GAAG,UAAU,CAAC;IAC3C,oBAAoB,EAAE,MAAM,GAAG,UAAU,CAAC;IAC1C,SAAS,EAAE,UAAU,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAGD,MAAM,WAAW,qBAAqB;IACpC,UAAU,EAAE,UAAU,CAAC;IACvB,SAAS,EAAE,6CAA6C,CAAC;CAC1D;AAGD,MAAM,WAAW,sBAAsB;IACrC,qBAAqB,EAAE,MAAM,GAAG,UAAU,CAAC;IAC3C,oBAAoB,EAAE,MAAM,GAAG,UAAU,CAAC;IAC1C,UAAU,EAAE,UAAU,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAsDD,wBAAgB,eAAe,CAC7B,OAAO,EAAE,sBAAsB,GAC9B,qBAAqB,CA2CvB;AA6ED,wBAAgB,eAAe,CAAC,OAAO,EAAE,sBAAsB,GAAG,UAAU,CAqC3E"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.DEFAULT_CHUNK_SIZE = exports.PQ_STREAM_HEADER_LEN = exports.NONCE_LEN = exports.ML_KEM_CT_LEN = exports.EPHEMERAL_LEN = exports.TAG_LEN = exports.CHUNK_TAG_MESSAGE = exports.CHUNK_TAG_FINAL = void 0;
|
|
4
4
|
exports.toBytes = toBytes;
|
|
5
5
|
exports.derivePqChunkNonce = derivePqChunkNonce;
|
|
6
6
|
exports.streamPqEncrypt = streamPqEncrypt;
|
|
@@ -11,14 +11,15 @@ const hkdf_js_1 = require("@noble/hashes/hkdf.js");
|
|
|
11
11
|
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
12
12
|
const chacha_js_1 = require("@noble/ciphers/chacha.js");
|
|
13
13
|
const utils_js_1 = require("@noble/ciphers/utils.js");
|
|
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
|
+
Object.defineProperty(exports, "TAG_LEN", { enumerable: true, get: function () { return chunks_1.TAG_LEN; } });
|
|
14
18
|
exports.EPHEMERAL_LEN = 32;
|
|
15
19
|
exports.ML_KEM_CT_LEN = 1088;
|
|
16
20
|
exports.NONCE_LEN = 24;
|
|
17
|
-
exports.TAG_LEN = 16;
|
|
18
21
|
exports.PQ_STREAM_HEADER_LEN = exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN + exports.NONCE_LEN;
|
|
19
22
|
exports.DEFAULT_CHUNK_SIZE = 64 * 1024;
|
|
20
|
-
exports.CHUNK_TAG_MESSAGE = 0x00;
|
|
21
|
-
exports.CHUNK_TAG_FINAL = 0x01;
|
|
22
23
|
const HEX_RE = /^[0-9a-fA-F]*$/;
|
|
23
24
|
function toBytes(data, expectedLen, name = "Data") {
|
|
24
25
|
let buf;
|
|
@@ -64,51 +65,19 @@ function decapsulateKeyMaterial(recipX25519Sec, recipMlKemSec, ephPub, mlKemCt)
|
|
|
64
65
|
const x25519Shared = ed25519_js_1.x25519.getSharedSecret(recipX25519Sec, ephPub);
|
|
65
66
|
return deriveSharedKey(x25519Shared, mlKemShared, ephPub);
|
|
66
67
|
}
|
|
67
|
-
function computeStreamOutputSize(ptLen, chunkSize) {
|
|
68
|
-
const numFullChunks = Math.floor(ptLen / chunkSize);
|
|
69
|
-
const lastChunkLen = ptLen - numFullChunks * chunkSize;
|
|
70
|
-
const hasExtraChunk = lastChunkLen > 0 || ptLen === 0;
|
|
71
|
-
const totalChunks = hasExtraChunk ? numFullChunks + 1 : numFullChunks;
|
|
72
|
-
let totalSize = exports.PQ_STREAM_HEADER_LEN;
|
|
73
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
74
|
-
const len = i < numFullChunks ? chunkSize : lastChunkLen > 0 ? lastChunkLen : 0;
|
|
75
|
-
totalSize += 1 + len + exports.TAG_LEN;
|
|
76
|
-
}
|
|
77
|
-
return totalSize;
|
|
78
|
-
}
|
|
79
|
-
function encryptChunks(plaintext, key, baseNonce, chunkSize, output) {
|
|
80
|
-
const numFullChunks = Math.floor(plaintext.length / chunkSize);
|
|
81
|
-
const hasExtra = plaintext.length % chunkSize > 0 || plaintext.length === 0;
|
|
82
|
-
const totalChunks = hasExtra ? numFullChunks + 1 : numFullChunks;
|
|
83
|
-
let offset = exports.PQ_STREAM_HEADER_LEN;
|
|
84
|
-
for (let i = 0; i < totalChunks; i++) {
|
|
85
|
-
const isFinal = i === totalChunks - 1;
|
|
86
|
-
const chunkTag = isFinal ? exports.CHUNK_TAG_FINAL : exports.CHUNK_TAG_MESSAGE;
|
|
87
|
-
const start = i * chunkSize;
|
|
88
|
-
const end = isFinal ? plaintext.length : start + chunkSize;
|
|
89
|
-
const chunk = plaintext.subarray(start, end);
|
|
90
|
-
const nonce = derivePqChunkNonce(baseNonce, i, chunkTag);
|
|
91
|
-
const cipher = (0, chacha_js_1.xchacha20poly1305)(key, nonce);
|
|
92
|
-
const encrypted = cipher.encrypt(chunk);
|
|
93
|
-
output[offset] = chunkTag;
|
|
94
|
-
offset += 1;
|
|
95
|
-
output.set(encrypted, offset);
|
|
96
|
-
offset += encrypted.length;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
68
|
function streamPqEncrypt(options) {
|
|
100
69
|
const recipX25519Pub = toBytes(options.recipientX25519Public, 32, "recipientX25519Public");
|
|
101
70
|
const recipMlKemPub = toBytes(options.recipientMlKemPublic, 1184, "recipientMlKemPublic");
|
|
102
71
|
const chunkSize = options.chunkSize ?? exports.DEFAULT_CHUNK_SIZE;
|
|
72
|
+
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
103
73
|
const plaintext = options.plaintext;
|
|
104
74
|
const { ephPub, mlKemCt, key } = encapsulateKeyMaterial(recipX25519Pub, recipMlKemPub);
|
|
105
75
|
const baseNonce = (0, utils_js_1.randomBytes)(exports.NONCE_LEN);
|
|
106
|
-
const
|
|
107
|
-
const output = new Uint8Array(totalSize);
|
|
76
|
+
const output = new Uint8Array((0, chunks_1.streamSize)(exports.PQ_STREAM_HEADER_LEN, plaintext.length, chunkSize));
|
|
108
77
|
output.set(ephPub, 0);
|
|
109
78
|
output.set(mlKemCt, exports.EPHEMERAL_LEN);
|
|
110
79
|
output.set(baseNonce, exports.EPHEMERAL_LEN + exports.ML_KEM_CT_LEN);
|
|
111
|
-
|
|
80
|
+
(0, chunks_1.encryptChunksInto)(plaintext, key, baseNonce, chunkSize, output, exports.PQ_STREAM_HEADER_LEN, derivePqChunkNonce);
|
|
112
81
|
return {
|
|
113
82
|
ciphertext: output,
|
|
114
83
|
algorithm: "x25519-ml-kem-768-xchacha20-poly1305-stream",
|
|
@@ -116,12 +85,12 @@ function streamPqEncrypt(options) {
|
|
|
116
85
|
}
|
|
117
86
|
function readPqChunkHeader(ciphertext, offset, chunkSize) {
|
|
118
87
|
const chunkTag = ciphertext[offset];
|
|
119
|
-
if (chunkTag !==
|
|
88
|
+
if (chunkTag !== chunks_1.CHUNK_TAG_MESSAGE && chunkTag !== chunks_1.CHUNK_TAG_FINAL) {
|
|
120
89
|
throw new Error(`Invalid chunk tag: 0x${chunkTag.toString(16)}`);
|
|
121
90
|
}
|
|
122
|
-
const isFinal = chunkTag ===
|
|
91
|
+
const isFinal = chunkTag === chunks_1.CHUNK_TAG_FINAL;
|
|
123
92
|
const start = offset + 1;
|
|
124
|
-
const encLen = isFinal ? ciphertext.length - start : chunkSize +
|
|
93
|
+
const encLen = isFinal ? ciphertext.length - start : chunkSize + chunks_1.TAG_LEN;
|
|
125
94
|
if (start + encLen > ciphertext.length) {
|
|
126
95
|
throw new Error("Ciphertext truncated — incomplete chunk");
|
|
127
96
|
}
|
|
@@ -162,8 +131,9 @@ function streamPqDecrypt(options) {
|
|
|
162
131
|
const recipX25519Sec = toBytes(options.recipientX25519Secret, 32, "recipientX25519Secret");
|
|
163
132
|
const recipMlKemSec = toBytes(options.recipientMlKemSecret, 2400, "recipientMlKemSecret");
|
|
164
133
|
const chunkSize = options.chunkSize ?? exports.DEFAULT_CHUNK_SIZE;
|
|
134
|
+
(0, chunks_1.assertChunkSize)(chunkSize);
|
|
165
135
|
const ciphertext = options.ciphertext;
|
|
166
|
-
if (ciphertext.length < exports.PQ_STREAM_HEADER_LEN + 1 +
|
|
136
|
+
if (ciphertext.length < exports.PQ_STREAM_HEADER_LEN + 1 + chunks_1.TAG_LEN) {
|
|
167
137
|
throw new Error("Ciphertext too short — missing header or chunk data");
|
|
168
138
|
}
|
|
169
139
|
const ephPub = ciphertext.subarray(0, exports.EPHEMERAL_LEN);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebastienrousseau/crypto-lib",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.35",
|
|
4
|
+
"description": "Post-quantum cryptography library for TypeScript and Node.js: ML-KEM, ML-DSA, SLH-DSA, HPKE, AES-GCM, XChaCha20-Poly1305, Ed25519, X25519, SHA-3, BLAKE3, Argon2id and OpenPGP.",
|
|
5
5
|
"author": "Sebastien Rousseau <sebastienrousseau@users.noreply.github.com>",
|
|
6
6
|
"license": "MIT OR Apache-2.0",
|
|
7
7
|
"homepage": "https://crypto-lib.io",
|
|
@@ -126,13 +126,28 @@
|
|
|
126
126
|
"typescript": "~5.9.3"
|
|
127
127
|
},
|
|
128
128
|
"keywords": [
|
|
129
|
-
"crypto-library",
|
|
130
129
|
"cryptography",
|
|
131
|
-
"openpgp",
|
|
132
130
|
"encryption",
|
|
133
|
-
"
|
|
134
|
-
"
|
|
135
|
-
"
|
|
131
|
+
"post-quantum",
|
|
132
|
+
"pqc",
|
|
133
|
+
"quantum-safe",
|
|
134
|
+
"ml-kem",
|
|
135
|
+
"ml-dsa",
|
|
136
|
+
"slh-dsa",
|
|
137
|
+
"kyber",
|
|
138
|
+
"dilithium",
|
|
139
|
+
"fips-203",
|
|
140
|
+
"fips-204",
|
|
141
|
+
"fips-205",
|
|
142
|
+
"hpke",
|
|
143
|
+
"aes-gcm",
|
|
144
|
+
"xchacha20-poly1305",
|
|
145
|
+
"ed25519",
|
|
146
|
+
"x25519",
|
|
147
|
+
"argon2",
|
|
148
|
+
"blake3",
|
|
149
|
+
"openpgp",
|
|
150
|
+
"typescript"
|
|
136
151
|
],
|
|
137
152
|
"publishConfig": {
|
|
138
153
|
"access": "public"
|