@shieldfive/crypto 1.0.0-alpha.11
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/CHANGELOG.md +228 -0
- package/LICENSE +201 -0
- package/README.md +275 -0
- package/SECURITY.md +130 -0
- package/dist/format/header.d.ts +73 -0
- package/dist/format/header.d.ts.map +1 -0
- package/dist/format/header.js +230 -0
- package/dist/format/header.js.map +1 -0
- package/dist/identity/index.d.ts +119 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +217 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/identity/sign.d.ts +92 -0
- package/dist/identity/sign.d.ts.map +1 -0
- package/dist/identity/sign.js +182 -0
- package/dist/identity/sign.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +146 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/encoding.d.ts +33 -0
- package/dist/internal/encoding.d.ts.map +1 -0
- package/dist/internal/encoding.js +135 -0
- package/dist/internal/encoding.js.map +1 -0
- package/dist/internal/hkdf.d.ts +14 -0
- package/dist/internal/hkdf.d.ts.map +1 -0
- package/dist/internal/hkdf.js +27 -0
- package/dist/internal/hkdf.js.map +1 -0
- package/dist/internal/hmac.d.ts +9 -0
- package/dist/internal/hmac.d.ts.map +1 -0
- package/dist/internal/hmac.js +18 -0
- package/dist/internal/hmac.js.map +1 -0
- package/dist/internal/runtime.d.ts +29 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +67 -0
- package/dist/internal/runtime.js.map +1 -0
- package/dist/internal/sodium.d.ts +12 -0
- package/dist/internal/sodium.d.ts.map +1 -0
- package/dist/internal/sodium.js +34 -0
- package/dist/internal/sodium.js.map +1 -0
- package/dist/internal/types.d.ts +98 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +70 -0
- package/dist/internal/types.js.map +1 -0
- package/dist/kdf/argon2id.d.ts +72 -0
- package/dist/kdf/argon2id.d.ts.map +1 -0
- package/dist/kdf/argon2id.js +98 -0
- package/dist/kdf/argon2id.js.map +1 -0
- package/dist/migration/v0-bridge.d.ts +94 -0
- package/dist/migration/v0-bridge.d.ts.map +1 -0
- package/dist/migration/v0-bridge.js +203 -0
- package/dist/migration/v0-bridge.js.map +1 -0
- package/dist/streams/aes-gcm-v1.d.ts +144 -0
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
- package/dist/streams/aes-gcm-v1.js +456 -0
- package/dist/streams/aes-gcm-v1.js.map +1 -0
- package/dist/streams/pq-hybrid-v1.d.ts +219 -0
- package/dist/streams/pq-hybrid-v1.d.ts.map +1 -0
- package/dist/streams/pq-hybrid-v1.js +473 -0
- package/dist/streams/pq-hybrid-v1.js.map +1 -0
- package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
- package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v0/api.js +110 -0
- package/dist/suites/aes-gcm-v0/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/api.js +181 -0
- package/dist/suites/aes-gcm-v1/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/index.d.ts +59 -0
- package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/index.js +177 -0
- package/dist/suites/aes-gcm-v1/index.js.map +1 -0
- package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
- package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v2/api.js +162 -0
- package/dist/suites/aes-gcm-v2/api.js.map +1 -0
- package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
- package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v2/index.js +193 -0
- package/dist/suites/aes-gcm-v2/index.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts +67 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.js +190 -0
- package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.js +225 -0
- package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
- package/dist/suites/xchacha-v1/api.d.ts +29 -0
- package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/api.js +154 -0
- package/dist/suites/xchacha-v1/api.js.map +1 -0
- package/dist/suites/xchacha-v1/index.d.ts +50 -0
- package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/index.js +133 -0
- package/dist/suites/xchacha-v1/index.js.map +1 -0
- package/package.json +124 -0
- package/spec/format-v0.md +81 -0
- package/spec/format-v1.md +400 -0
- package/spec/key-derivation.md +167 -0
- package/spec/threat-model.md +242 -0
- package/src/format/header.ts +316 -0
- package/src/identity/index.ts +371 -0
- package/src/identity/sign.ts +221 -0
- package/src/index.ts +224 -0
- package/src/internal/encoding.ts +152 -0
- package/src/internal/hkdf.ts +47 -0
- package/src/internal/hmac.ts +33 -0
- package/src/internal/runtime.ts +80 -0
- package/src/internal/sodium.ts +38 -0
- package/src/internal/types.ts +134 -0
- package/src/kdf/argon2id.ts +165 -0
- package/src/migration/v0-bridge.ts +347 -0
- package/src/streams/aes-gcm-v1.ts +647 -0
- package/src/streams/pq-hybrid-v1.ts +722 -0
- package/src/suites/aes-gcm-v0/api.ts +155 -0
- package/src/suites/aes-gcm-v1/api.ts +292 -0
- package/src/suites/aes-gcm-v1/index.ts +269 -0
- package/src/suites/aes-gcm-v2/api.ts +268 -0
- package/src/suites/aes-gcm-v2/index.ts +289 -0
- package/src/suites/pq-hybrid-v1/api.ts +299 -0
- package/src/suites/pq-hybrid-v1/index.ts +343 -0
- package/src/suites/xchacha-v1/api.ts +254 -0
- package/src/suites/xchacha-v1/index.ts +213 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming encrypt/decrypt for the v1 wire format.
|
|
3
|
+
*
|
|
4
|
+
* The whole-blob APIs in `suites/<name>/api.ts` materialize entire files in
|
|
5
|
+
* memory. That's fine for small files; for multi-gigabyte files on a
|
|
6
|
+
* mobile device it isn't.
|
|
7
|
+
*
|
|
8
|
+
* This module exposes a WHATWG TransformStream factory that ingests
|
|
9
|
+
* plaintext (or ciphertext) chunks from any ReadableStream and emits the
|
|
10
|
+
* other side, without ever holding more than one chunk-plus-header in
|
|
11
|
+
* memory at once.
|
|
12
|
+
*
|
|
13
|
+
* Currently implemented:
|
|
14
|
+
* - Suite 0x01 (aes-256-gcm-v1) encrypt
|
|
15
|
+
* - Suite 0x01 (aes-256-gcm-v1) decrypt
|
|
16
|
+
*
|
|
17
|
+
* The XChaCha and PQ-hybrid suites use the same chunk format and AAD;
|
|
18
|
+
* adding them is a copy of the AES module with the suite primitive
|
|
19
|
+
* swapped, but we ship one suite first to lock the streaming contract.
|
|
20
|
+
*/
|
|
21
|
+
import { buildHeaderUnauthenticated, deriveHeaderMacKey, HeaderError, parseHeader, } from '../format/header.js';
|
|
22
|
+
import { AES_GCM_V1_SUITE_PAYLOAD_LENGTH, AES_GCM_V1_TAG_BYTES, createChunkContext, decryptChunk, encryptChunk, generateContentMaterial, } from '../suites/aes-gcm-v1/index.js';
|
|
23
|
+
import { DEFAULT_CHUNK_SIZE, HEADER_SIZES, MAX_TOTAL_CHUNKS, SUITE, } from '../internal/types.js';
|
|
24
|
+
import { concatBytes, readUint32BE, uint32BE } from '../internal/encoding.js';
|
|
25
|
+
import { hmacSha256 } from '../internal/hmac.js';
|
|
26
|
+
import { buildSignatureBlock, deriveEd25519PublicKey, ED25519_SECRET_KEY_LENGTH, parseSignatureBlock, SIGNATURE_ALGO_ED25519, signHeaderAndMacs, SignatureError, verifyHeaderAndMacs, } from '../identity/sign.js';
|
|
27
|
+
const LENGTH_PREFIX_BYTES = 4;
|
|
28
|
+
/**
|
|
29
|
+
* Build a TransformStream that encrypts plaintext into v1 wire-format
|
|
30
|
+
* ciphertext on the fly. The stream emits the header first, then each
|
|
31
|
+
* chunk as it arrives.
|
|
32
|
+
*
|
|
33
|
+
* The caller MUST pass `plaintextSize` because the header records it.
|
|
34
|
+
* If the caller doesn't know the size up front, they must accumulate
|
|
35
|
+
* the plaintext into a buffer first — there is no way to write a
|
|
36
|
+
* self-describing header otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export function createAesGcmV1EncryptStream(options) {
|
|
39
|
+
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
40
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
|
|
41
|
+
throw new RangeError('encrypt stream: chunkSize must be positive integer');
|
|
42
|
+
}
|
|
43
|
+
if (!Number.isSafeInteger(options.plaintextSize) ||
|
|
44
|
+
options.plaintextSize < 0) {
|
|
45
|
+
throw new RangeError('encrypt stream: plaintextSize must be a non-negative integer');
|
|
46
|
+
}
|
|
47
|
+
const totalChunks = options.plaintextSize === 0
|
|
48
|
+
? 0
|
|
49
|
+
: Math.ceil(options.plaintextSize / chunkSize);
|
|
50
|
+
if (totalChunks > MAX_TOTAL_CHUNKS) {
|
|
51
|
+
throw new RangeError('encrypt stream: file would exceed total_chunks limit');
|
|
52
|
+
}
|
|
53
|
+
const generated = generateContentMaterial();
|
|
54
|
+
const contentKey = options.contentKey ?? generated.contentKey;
|
|
55
|
+
const fileId = options.fileId ?? generated.fileId;
|
|
56
|
+
if (contentKey.length !== 32) {
|
|
57
|
+
throw new RangeError('encrypt stream: contentKey must be 32 bytes');
|
|
58
|
+
}
|
|
59
|
+
if (fileId.length !== HEADER_SIZES.FILE_ID) {
|
|
60
|
+
throw new RangeError('encrypt stream: fileId must be 16 bytes');
|
|
61
|
+
}
|
|
62
|
+
const suitePayload = options.suitePayloadOverride ??
|
|
63
|
+
new Uint8Array(AES_GCM_V1_SUITE_PAYLOAD_LENGTH);
|
|
64
|
+
const ed25519SecretKey = options.ed25519SecretKey;
|
|
65
|
+
if (ed25519SecretKey && ed25519SecretKey.length !== ED25519_SECRET_KEY_LENGTH) {
|
|
66
|
+
throw new RangeError(`encrypt stream: ed25519SecretKey must be ${ED25519_SECRET_KEY_LENGTH} bytes`);
|
|
67
|
+
}
|
|
68
|
+
// State held across the lifetime of the stream:
|
|
69
|
+
// - pending plaintext buffer (collects until we have a full chunk)
|
|
70
|
+
// - chunk index counter
|
|
71
|
+
// - bytes consumed counter (for sanity check vs plaintextSize)
|
|
72
|
+
// - context (lazily initialized)
|
|
73
|
+
// - unauthenticated header bytes (kept for signing)
|
|
74
|
+
// - per-chunk MAC tags (last 16 bytes of each chunk ciphertext)
|
|
75
|
+
let pending = new Uint8Array(0);
|
|
76
|
+
let chunkIndex = 0;
|
|
77
|
+
let bytesConsumed = 0;
|
|
78
|
+
let contextPromise = null;
|
|
79
|
+
let headerEmitted = false;
|
|
80
|
+
let unauthenticatedHeader = null;
|
|
81
|
+
const chunkMacs = [];
|
|
82
|
+
async function ensureHeader(controller) {
|
|
83
|
+
if (headerEmitted)
|
|
84
|
+
return;
|
|
85
|
+
const unauth = buildHeaderUnauthenticated({
|
|
86
|
+
suite: SUITE.AES_256_GCM_V1,
|
|
87
|
+
fileId,
|
|
88
|
+
chunkSize,
|
|
89
|
+
totalChunks,
|
|
90
|
+
plaintextSize: options.plaintextSize,
|
|
91
|
+
suitePayload,
|
|
92
|
+
});
|
|
93
|
+
const macKey = await deriveHeaderMacKey(contentKey, fileId);
|
|
94
|
+
const mac = await hmacSha256(macKey, unauth);
|
|
95
|
+
unauthenticatedHeader = unauth;
|
|
96
|
+
controller.enqueue(concatBytes([unauth, mac]));
|
|
97
|
+
headerEmitted = true;
|
|
98
|
+
}
|
|
99
|
+
async function emitChunk(plaintext, controller) {
|
|
100
|
+
if (!contextPromise) {
|
|
101
|
+
contextPromise = createChunkContext(contentKey, fileId, totalChunks);
|
|
102
|
+
}
|
|
103
|
+
const ctx = await contextPromise;
|
|
104
|
+
const ct = await encryptChunk(ctx, chunkIndex, plaintext);
|
|
105
|
+
controller.enqueue(uint32BE(ct.length));
|
|
106
|
+
controller.enqueue(ct);
|
|
107
|
+
if (ed25519SecretKey) {
|
|
108
|
+
// AEAD tag is the trailing AES_GCM_V1_TAG_BYTES of the chunk ct.
|
|
109
|
+
chunkMacs.push(ct.slice(ct.length - AES_GCM_V1_TAG_BYTES));
|
|
110
|
+
}
|
|
111
|
+
chunkIndex += 1;
|
|
112
|
+
}
|
|
113
|
+
const transformer = {
|
|
114
|
+
async transform(chunk, controller) {
|
|
115
|
+
await ensureHeader(controller);
|
|
116
|
+
bytesConsumed += chunk.length;
|
|
117
|
+
if (bytesConsumed > options.plaintextSize) {
|
|
118
|
+
controller.error(new RangeError('encrypt stream: input exceeded declared plaintextSize'));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Append to pending buffer
|
|
122
|
+
const merged = new Uint8Array(pending.length + chunk.length);
|
|
123
|
+
merged.set(pending, 0);
|
|
124
|
+
merged.set(chunk, pending.length);
|
|
125
|
+
pending = merged;
|
|
126
|
+
// Drain whole chunks
|
|
127
|
+
while (pending.length >= chunkSize && chunkIndex < totalChunks - 1) {
|
|
128
|
+
const slice = pending.subarray(0, chunkSize);
|
|
129
|
+
await emitChunk(slice, controller);
|
|
130
|
+
pending = pending.subarray(chunkSize);
|
|
131
|
+
// Subarray shares the buffer; copy out so we can drop the rest.
|
|
132
|
+
pending = pending.slice(0);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
async flush(controller) {
|
|
136
|
+
await ensureHeader(controller);
|
|
137
|
+
if (bytesConsumed !== options.plaintextSize) {
|
|
138
|
+
controller.error(new RangeError(`encrypt stream: input was ${bytesConsumed} bytes, declared ${options.plaintextSize}`));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Emit any remaining whole chunks plus the final partial chunk.
|
|
142
|
+
while (chunkIndex < totalChunks) {
|
|
143
|
+
const isLastIteration = chunkIndex === totalChunks - 1;
|
|
144
|
+
const sliceSize = isLastIteration
|
|
145
|
+
? pending.length // final chunk: whatever's left
|
|
146
|
+
: chunkSize;
|
|
147
|
+
const slice = pending.subarray(0, sliceSize);
|
|
148
|
+
await emitChunk(slice, controller);
|
|
149
|
+
pending = pending.subarray(sliceSize).slice(0);
|
|
150
|
+
}
|
|
151
|
+
if (pending.length !== 0) {
|
|
152
|
+
controller.error(new RangeError('encrypt stream: residual bytes after final chunk'));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (ed25519SecretKey) {
|
|
156
|
+
if (!unauthenticatedHeader) {
|
|
157
|
+
controller.error(new Error('encrypt stream: header not finalized before signing'));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const signature = signHeaderAndMacs({
|
|
161
|
+
ed25519SecretKey,
|
|
162
|
+
header: unauthenticatedHeader,
|
|
163
|
+
chunkMacs,
|
|
164
|
+
});
|
|
165
|
+
const publicKey = deriveEd25519PublicKey(ed25519SecretKey);
|
|
166
|
+
controller.enqueue(buildSignatureBlock({
|
|
167
|
+
algorithm: SIGNATURE_ALGO_ED25519,
|
|
168
|
+
publicKey,
|
|
169
|
+
signature,
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
stream: new TransformStream(transformer),
|
|
176
|
+
contentKey,
|
|
177
|
+
fileId,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Build a TransformStream that decrypts a v1 ciphertext stream into
|
|
182
|
+
* plaintext. Unlike the whole-blob decryptor, this version processes
|
|
183
|
+
* input incrementally — useful for very large files or when ciphertext
|
|
184
|
+
* arrives chunked over the network.
|
|
185
|
+
*
|
|
186
|
+
* Files written by an older library version (no signature block) decrypt
|
|
187
|
+
* unchanged: `metadata.signature` resolves to `null`. Files with a
|
|
188
|
+
* trailing signature block are parsed; if `expectedSignerPublicKey` is
|
|
189
|
+
* supplied, the signature is verified and `metadata.signature.verified`
|
|
190
|
+
* reflects the outcome.
|
|
191
|
+
*/
|
|
192
|
+
export function createAesGcmV1DecryptStream(options) {
|
|
193
|
+
const { contentKey } = options;
|
|
194
|
+
if (contentKey.length !== 32) {
|
|
195
|
+
throw new RangeError('decrypt stream: contentKey must be 32 bytes');
|
|
196
|
+
}
|
|
197
|
+
const expectedSignerPublicKey = options.expectedSignerPublicKey;
|
|
198
|
+
let resolveMetadata;
|
|
199
|
+
let rejectMetadata;
|
|
200
|
+
const metadata = new Promise((resolve, reject) => {
|
|
201
|
+
resolveMetadata = resolve;
|
|
202
|
+
rejectMetadata = reject;
|
|
203
|
+
});
|
|
204
|
+
// Pre-attach a no-op handler so the user-facing promise doesn't trigger
|
|
205
|
+
// an unhandledRejection if the caller never awaits metadata (they
|
|
206
|
+
// typically await the plaintext drain, which surfaces the same error).
|
|
207
|
+
// This does NOT swallow the error for callers who DO await metadata —
|
|
208
|
+
// .catch() returns a new promise; the original `metadata` still rejects.
|
|
209
|
+
metadata.catch(() => { });
|
|
210
|
+
// Phases:
|
|
211
|
+
// 0 = waiting for header bytes
|
|
212
|
+
// 1 = streaming chunks: alternating length-prefix + ciphertext
|
|
213
|
+
let phase = 0;
|
|
214
|
+
let buffer = new Uint8Array(0);
|
|
215
|
+
let parsedHeader = null;
|
|
216
|
+
let chunkIndex = 0;
|
|
217
|
+
let bytesEmitted = 0;
|
|
218
|
+
let nextCipherLen = -1;
|
|
219
|
+
let contextPromise = null;
|
|
220
|
+
const chunkMacs = [];
|
|
221
|
+
function append(input) {
|
|
222
|
+
const merged = new Uint8Array(buffer.length + input.length);
|
|
223
|
+
merged.set(buffer, 0);
|
|
224
|
+
merged.set(input, buffer.length);
|
|
225
|
+
buffer = merged;
|
|
226
|
+
}
|
|
227
|
+
function consume(n) {
|
|
228
|
+
const out = buffer.subarray(0, n).slice(0);
|
|
229
|
+
buffer = buffer.subarray(n).slice(0);
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
async function tryParseHeader() {
|
|
233
|
+
// Need at least the fixed prefix + MAC (no suite_payload).
|
|
234
|
+
if (buffer.length <
|
|
235
|
+
HEADER_SIZES.MAGIC +
|
|
236
|
+
HEADER_SIZES.SUITE +
|
|
237
|
+
HEADER_SIZES.FLAGS +
|
|
238
|
+
HEADER_SIZES.FILE_ID +
|
|
239
|
+
HEADER_SIZES.CHUNK_SIZE +
|
|
240
|
+
HEADER_SIZES.TOTAL_CHUNKS +
|
|
241
|
+
HEADER_SIZES.PLAINTEXT_SIZE +
|
|
242
|
+
HEADER_SIZES.SUITE_PAYLOAD_LEN) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
// Read suite_payload_len so we know how much more to wait for.
|
|
246
|
+
const suitePayloadLenOffset = HEADER_SIZES.MAGIC +
|
|
247
|
+
HEADER_SIZES.SUITE +
|
|
248
|
+
HEADER_SIZES.FLAGS +
|
|
249
|
+
HEADER_SIZES.FILE_ID +
|
|
250
|
+
HEADER_SIZES.CHUNK_SIZE +
|
|
251
|
+
HEADER_SIZES.TOTAL_CHUNKS +
|
|
252
|
+
HEADER_SIZES.PLAINTEXT_SIZE;
|
|
253
|
+
const suitePayloadLen = (buffer[suitePayloadLenOffset] << 8) |
|
|
254
|
+
buffer[suitePayloadLenOffset + 1];
|
|
255
|
+
const fullHeaderLen = suitePayloadLenOffset +
|
|
256
|
+
HEADER_SIZES.SUITE_PAYLOAD_LEN +
|
|
257
|
+
suitePayloadLen +
|
|
258
|
+
HEADER_SIZES.HEADER_MAC;
|
|
259
|
+
if (buffer.length < fullHeaderLen)
|
|
260
|
+
return false;
|
|
261
|
+
const headerBytes = consume(fullHeaderLen);
|
|
262
|
+
parsedHeader = parseHeader(headerBytes);
|
|
263
|
+
if (parsedHeader.suite !== SUITE.AES_256_GCM_V1) {
|
|
264
|
+
throw new HeaderError(`decrypt stream: this stream only supports aes-256-gcm-v1; got suite 0x${parsedHeader.suite.toString(16)}`);
|
|
265
|
+
}
|
|
266
|
+
// Verify header MAC using parseHeader's exposed unauthenticated bytes
|
|
267
|
+
// and mac fields.
|
|
268
|
+
const { verifyHeaderMac } = await import('../format/header.js');
|
|
269
|
+
await verifyHeaderMac(parsedHeader, contentKey);
|
|
270
|
+
contextPromise = createChunkContext(contentKey, parsedHeader.fileId, parsedHeader.totalChunks);
|
|
271
|
+
phase = 1;
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
async function tryEmitChunk(controller) {
|
|
275
|
+
if (!parsedHeader)
|
|
276
|
+
return false;
|
|
277
|
+
if (chunkIndex >= parsedHeader.totalChunks) {
|
|
278
|
+
// Already consumed all expected chunks; anything remaining is trailing
|
|
279
|
+
// garbage and must be flagged at flush time.
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
if (nextCipherLen < 0) {
|
|
283
|
+
if (buffer.length < LENGTH_PREFIX_BYTES)
|
|
284
|
+
return false;
|
|
285
|
+
const lenBytes = consume(LENGTH_PREFIX_BYTES);
|
|
286
|
+
nextCipherLen = readUint32BE(lenBytes);
|
|
287
|
+
const isFinal = chunkIndex === parsedHeader.totalChunks - 1;
|
|
288
|
+
const minCipher = 1 + AES_GCM_V1_TAG_BYTES;
|
|
289
|
+
const maxCipher = parsedHeader.chunkSize + AES_GCM_V1_TAG_BYTES;
|
|
290
|
+
if (nextCipherLen < minCipher || nextCipherLen > maxCipher) {
|
|
291
|
+
throw new HeaderError('chunk_length_out_of_range');
|
|
292
|
+
}
|
|
293
|
+
if (!isFinal && nextCipherLen !== maxCipher) {
|
|
294
|
+
throw new HeaderError('non_final_chunk_wrong_size');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (buffer.length < nextCipherLen)
|
|
298
|
+
return false;
|
|
299
|
+
const ctBytes = consume(nextCipherLen);
|
|
300
|
+
nextCipherLen = -1;
|
|
301
|
+
const ctx = await contextPromise;
|
|
302
|
+
const pt = await decryptChunk(ctx, chunkIndex, ctBytes);
|
|
303
|
+
bytesEmitted += pt.length;
|
|
304
|
+
controller.enqueue(pt);
|
|
305
|
+
// AEAD tag is the trailing AES_GCM_V1_TAG_BYTES of ctBytes. Kept
|
|
306
|
+
// for optional signature verification at flush time.
|
|
307
|
+
chunkMacs.push(ctBytes.slice(ctBytes.length - AES_GCM_V1_TAG_BYTES));
|
|
308
|
+
chunkIndex += 1;
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
stream: new TransformStream({
|
|
313
|
+
async transform(chunk, controller) {
|
|
314
|
+
append(chunk);
|
|
315
|
+
try {
|
|
316
|
+
if (phase === 0) {
|
|
317
|
+
const ok = await tryParseHeader();
|
|
318
|
+
if (!ok)
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
// Drain as many chunks as we have data for.
|
|
322
|
+
while (await tryEmitChunk(controller)) {
|
|
323
|
+
/* loop */
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
catch (err) {
|
|
327
|
+
controller.error(err);
|
|
328
|
+
rejectMetadata(err);
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
async flush(controller) {
|
|
332
|
+
try {
|
|
333
|
+
if (phase === 0) {
|
|
334
|
+
throw new HeaderError('header_too_short');
|
|
335
|
+
}
|
|
336
|
+
// No header yet means truncated input.
|
|
337
|
+
if (!parsedHeader) {
|
|
338
|
+
throw new HeaderError('header_too_short');
|
|
339
|
+
}
|
|
340
|
+
// Try once more in case flush is called with everything queued.
|
|
341
|
+
while (await tryEmitChunk(controller)) {
|
|
342
|
+
/* loop */
|
|
343
|
+
}
|
|
344
|
+
if (chunkIndex !== parsedHeader.totalChunks) {
|
|
345
|
+
throw new HeaderError('chunk_truncated');
|
|
346
|
+
}
|
|
347
|
+
if (bytesEmitted !== parsedHeader.plaintextSize) {
|
|
348
|
+
throw new HeaderError('plaintext_size_mismatch');
|
|
349
|
+
}
|
|
350
|
+
const signatureResult = finalizeSignatureMetadata({
|
|
351
|
+
trailingBytes: buffer,
|
|
352
|
+
header: parsedHeader.unauthenticatedBytes,
|
|
353
|
+
chunkMacs,
|
|
354
|
+
expectedSignerPublicKey,
|
|
355
|
+
});
|
|
356
|
+
buffer = new Uint8Array(0);
|
|
357
|
+
resolveMetadata({ signature: signatureResult });
|
|
358
|
+
}
|
|
359
|
+
catch (err) {
|
|
360
|
+
controller.error(err);
|
|
361
|
+
rejectMetadata(err);
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
}),
|
|
365
|
+
metadata,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
369
|
+
// Helpers for ergonomics
|
|
370
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
371
|
+
/**
|
|
372
|
+
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 encrypt
|
|
373
|
+
* transform and return the resulting ReadableStream + key material.
|
|
374
|
+
*/
|
|
375
|
+
export function encryptStreamAesGcmV1(source, options) {
|
|
376
|
+
const { stream, contentKey, fileId } = createAesGcmV1EncryptStream(options);
|
|
377
|
+
return {
|
|
378
|
+
ciphertext: source.pipeThrough(stream),
|
|
379
|
+
contentKey,
|
|
380
|
+
fileId,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 decrypt
|
|
385
|
+
* transform and return the resulting plaintext ReadableStream alongside
|
|
386
|
+
* a `metadata` Promise that resolves at end-of-stream with the parsed
|
|
387
|
+
* signature block (or `signature: null` for unsigned / legacy files).
|
|
388
|
+
*/
|
|
389
|
+
export function decryptStreamAesGcmV1(source, options) {
|
|
390
|
+
const { stream, metadata } = createAesGcmV1DecryptStream(options);
|
|
391
|
+
return {
|
|
392
|
+
plaintext: source.pipeThrough(stream),
|
|
393
|
+
metadata,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Parse a trailing signature block (if any) from the decrypt stream's
|
|
398
|
+
* residual buffer, optionally verifying it against the caller's
|
|
399
|
+
* trusted public key.
|
|
400
|
+
*
|
|
401
|
+
* Shared between the AES-GCM and PQ-hybrid stream decoders so the
|
|
402
|
+
* signature semantics live in one place.
|
|
403
|
+
*/
|
|
404
|
+
export function finalizeSignatureMetadata(input) {
|
|
405
|
+
if (input.trailingBytes.length === 0) {
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
let parsed;
|
|
409
|
+
try {
|
|
410
|
+
parsed = parseSignatureBlock(input.trailingBytes, 0);
|
|
411
|
+
}
|
|
412
|
+
catch (err) {
|
|
413
|
+
if (err instanceof SignatureError) {
|
|
414
|
+
throw new HeaderError(`signature_block_${err.code}`);
|
|
415
|
+
}
|
|
416
|
+
throw err;
|
|
417
|
+
}
|
|
418
|
+
if (parsed.consumed !== input.trailingBytes.length) {
|
|
419
|
+
throw new HeaderError('trailing_bytes_after_signature_block');
|
|
420
|
+
}
|
|
421
|
+
const { algorithm, publicKey, signature } = parsed.block;
|
|
422
|
+
let verified = null;
|
|
423
|
+
if (input.expectedSignerPublicKey) {
|
|
424
|
+
if (algorithm !== SIGNATURE_ALGO_ED25519) {
|
|
425
|
+
// Unknown / unsupported algorithm: cannot verify with our
|
|
426
|
+
// Ed25519-only verifier; treat as a verification failure under
|
|
427
|
+
// the caller's policy (they asked us to verify, we couldn't).
|
|
428
|
+
verified = false;
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
// Pubkey-pinning: if the embedded pubkey doesn't match the caller's
|
|
432
|
+
// expected one, fail without doing the (still-honest) Ed25519
|
|
433
|
+
// math. This keeps the "verified=true" contract tight: it means
|
|
434
|
+
// the file was signed by the key the caller expected.
|
|
435
|
+
verified =
|
|
436
|
+
bytesEqualConstantTime(publicKey, input.expectedSignerPublicKey) &&
|
|
437
|
+
verifyHeaderAndMacs({
|
|
438
|
+
ed25519PublicKey: input.expectedSignerPublicKey,
|
|
439
|
+
header: input.header,
|
|
440
|
+
chunkMacs: input.chunkMacs,
|
|
441
|
+
signature,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return { algorithm, publicKey, signature, verified };
|
|
446
|
+
}
|
|
447
|
+
function bytesEqualConstantTime(a, b) {
|
|
448
|
+
if (a.length !== b.length)
|
|
449
|
+
return false;
|
|
450
|
+
let diff = 0;
|
|
451
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
452
|
+
diff |= a[i] ^ b[i];
|
|
453
|
+
}
|
|
454
|
+
return diff === 0;
|
|
455
|
+
}
|
|
456
|
+
//# sourceMappingURL=aes-gcm-v1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm-v1.js","sourceRoot":"","sources":["../../src/streams/aes-gcm-v1.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EACL,0BAA0B,EAC1B,kBAAkB,EAClB,WAAW,EACX,WAAW,GACZ,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EACpB,kBAAkB,EAClB,YAAY,EACZ,YAAY,EACZ,uBAAuB,GACxB,MAAM,+BAA+B,CAAA;AACtC,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,KAAK,GACN,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChD,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,yBAAyB,EACzB,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EAEjB,cAAc,EACd,mBAAmB,GACpB,MAAM,qBAAqB,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