@shieldfive/crypto 1.0.0-alpha.9 → 1.0.0-beta.2
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 +157 -0
- package/README.md +32 -15
- package/SECURITY.md +1 -1
- package/dist/identity/index.d.ts +16 -0
- package/dist/identity/index.d.ts.map +1 -1
- package/dist/identity/index.js +84 -22
- package/dist/identity/index.js.map +1 -1
- 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 +10 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -4
- package/dist/index.js.map +1 -1
- package/dist/internal/encoding.d.ts.map +1 -1
- package/dist/internal/encoding.js +0 -1
- package/dist/internal/encoding.js.map +1 -1
- package/dist/internal/types.d.ts +4 -1
- package/dist/internal/types.d.ts.map +1 -1
- package/dist/internal/types.js +8 -0
- package/dist/internal/types.js.map +1 -1
- package/dist/streams/aes-gcm-v1.d.ts +71 -3
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
- package/dist/streams/aes-gcm-v1.js +192 -50
- package/dist/streams/aes-gcm-v1.js.map +1 -1
- package/dist/streams/pq-hybrid-v1.d.ts +34 -3
- package/dist/streams/pq-hybrid-v1.d.ts.map +1 -1
- package/dist/streams/pq-hybrid-v1.js +126 -50
- package/dist/streams/pq-hybrid-v1.js.map +1 -1
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v1/api.js +1 -1
- package/dist/suites/aes-gcm-v1/api.js.map +1 -1
- package/dist/suites/aes-gcm-v1/index.d.ts +3 -0
- package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v1/index.js +3 -0
- package/dist/suites/aes-gcm-v1/index.js.map +1 -1
- 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/index.d.ts +4 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -1
- package/dist/suites/pq-hybrid-v1/index.js +15 -1
- package/dist/suites/pq-hybrid-v1/index.js.map +1 -1
- package/package.json +12 -2
- package/spec/format-v1.md +200 -7
- package/spec/key-derivation.md +18 -2
- package/spec/threat-model.md +36 -21
- package/src/identity/index.ts +107 -28
- package/src/identity/sign.ts +221 -0
- package/src/index.ts +32 -5
- package/src/internal/encoding.ts +0 -1
- package/src/internal/types.ts +9 -0
- package/src/streams/aes-gcm-v1.ts +275 -59
- package/src/streams/pq-hybrid-v1.ts +182 -59
- package/src/suites/aes-gcm-v1/api.ts +0 -1
- package/src/suites/aes-gcm-v1/index.ts +3 -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/index.ts +25 -4
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
import {
|
|
23
|
-
|
|
23
|
+
buildHeaderUnauthenticated,
|
|
24
|
+
deriveHeaderMacKey,
|
|
24
25
|
HeaderError,
|
|
25
26
|
parseHeader,
|
|
26
27
|
} from '../format/header.js'
|
|
@@ -31,6 +32,7 @@ import {
|
|
|
31
32
|
decryptChunk,
|
|
32
33
|
encryptChunk,
|
|
33
34
|
generateContentMaterial,
|
|
35
|
+
parseAesGcmV1SuitePayload,
|
|
34
36
|
} from '../suites/aes-gcm-v1/index.js'
|
|
35
37
|
import {
|
|
36
38
|
DEFAULT_CHUNK_SIZE,
|
|
@@ -38,7 +40,19 @@ import {
|
|
|
38
40
|
MAX_TOTAL_CHUNKS,
|
|
39
41
|
SUITE,
|
|
40
42
|
} from '../internal/types.js'
|
|
41
|
-
import { readUint32BE, uint32BE } from '../internal/encoding.js'
|
|
43
|
+
import { concatBytes, readUint32BE, uint32BE } from '../internal/encoding.js'
|
|
44
|
+
import { hmacSha256 } from '../internal/hmac.js'
|
|
45
|
+
import {
|
|
46
|
+
buildSignatureBlock,
|
|
47
|
+
deriveEd25519PublicKey,
|
|
48
|
+
ED25519_SECRET_KEY_LENGTH,
|
|
49
|
+
parseSignatureBlock,
|
|
50
|
+
SIGNATURE_ALGO_ED25519,
|
|
51
|
+
signHeaderAndMacs,
|
|
52
|
+
type SignatureBlock,
|
|
53
|
+
SignatureError,
|
|
54
|
+
verifyHeaderAndMacs,
|
|
55
|
+
} from '../identity/sign.js'
|
|
42
56
|
|
|
43
57
|
const LENGTH_PREFIX_BYTES = 4
|
|
44
58
|
|
|
@@ -57,6 +71,13 @@ export interface AesGcmV1EncryptStreamOptions {
|
|
|
57
71
|
fileId?: Uint8Array
|
|
58
72
|
/** Optional embedded suite payload (60-byte wrapped key + 12-byte IV). */
|
|
59
73
|
suitePayloadOverride?: Uint8Array
|
|
74
|
+
/**
|
|
75
|
+
* Optional 32-byte Ed25519 secret seed. When provided, the stream
|
|
76
|
+
* appends a signature block over `header_unauthenticated_bytes ||
|
|
77
|
+
* concat(chunk_macs)` after the final chunk. When omitted, no block
|
|
78
|
+
* is emitted and the file decrypts as unsigned (legacy behavior).
|
|
79
|
+
*/
|
|
80
|
+
ed25519SecretKey?: Uint8Array
|
|
60
81
|
}
|
|
61
82
|
|
|
62
83
|
export interface AesGcmV1EncryptStreamResult {
|
|
@@ -113,34 +134,50 @@ export function createAesGcmV1EncryptStream(
|
|
|
113
134
|
const suitePayload =
|
|
114
135
|
options.suitePayloadOverride ??
|
|
115
136
|
new Uint8Array(AES_GCM_V1_SUITE_PAYLOAD_LENGTH)
|
|
137
|
+
if (suitePayload.length !== AES_GCM_V1_SUITE_PAYLOAD_LENGTH) {
|
|
138
|
+
throw new RangeError(
|
|
139
|
+
`encrypt stream: suitePayloadOverride must be ${AES_GCM_V1_SUITE_PAYLOAD_LENGTH} bytes`,
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const ed25519SecretKey = options.ed25519SecretKey
|
|
144
|
+
if (ed25519SecretKey && ed25519SecretKey.length !== ED25519_SECRET_KEY_LENGTH) {
|
|
145
|
+
throw new RangeError(
|
|
146
|
+
`encrypt stream: ed25519SecretKey must be ${ED25519_SECRET_KEY_LENGTH} bytes`,
|
|
147
|
+
)
|
|
148
|
+
}
|
|
116
149
|
|
|
117
150
|
// State held across the lifetime of the stream:
|
|
118
151
|
// - pending plaintext buffer (collects until we have a full chunk)
|
|
119
152
|
// - chunk index counter
|
|
120
153
|
// - bytes consumed counter (for sanity check vs plaintextSize)
|
|
121
154
|
// - context (lazily initialized)
|
|
155
|
+
// - unauthenticated header bytes (kept for signing)
|
|
156
|
+
// - per-chunk MAC tags (last 16 bytes of each chunk ciphertext)
|
|
122
157
|
let pending = new Uint8Array(0)
|
|
123
158
|
let chunkIndex = 0
|
|
124
159
|
let bytesConsumed = 0
|
|
125
160
|
let contextPromise: ReturnType<typeof createChunkContext> | null = null
|
|
126
161
|
let headerEmitted = false
|
|
162
|
+
let unauthenticatedHeader: Uint8Array | null = null
|
|
163
|
+
const chunkMacs: Uint8Array[] = []
|
|
127
164
|
|
|
128
165
|
async function ensureHeader(
|
|
129
166
|
controller: TransformStreamDefaultController<Uint8Array>,
|
|
130
167
|
): Promise<void> {
|
|
131
168
|
if (headerEmitted) return
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
controller.enqueue(
|
|
169
|
+
const unauth = buildHeaderUnauthenticated({
|
|
170
|
+
suite: SUITE.AES_256_GCM_V1,
|
|
171
|
+
fileId,
|
|
172
|
+
chunkSize,
|
|
173
|
+
totalChunks,
|
|
174
|
+
plaintextSize: options.plaintextSize,
|
|
175
|
+
suitePayload,
|
|
176
|
+
})
|
|
177
|
+
const macKey = await deriveHeaderMacKey(contentKey, fileId)
|
|
178
|
+
const mac = await hmacSha256(macKey, unauth)
|
|
179
|
+
unauthenticatedHeader = unauth
|
|
180
|
+
controller.enqueue(concatBytes([unauth, mac]))
|
|
144
181
|
headerEmitted = true
|
|
145
182
|
}
|
|
146
183
|
|
|
@@ -155,6 +192,10 @@ export function createAesGcmV1EncryptStream(
|
|
|
155
192
|
const ct = await encryptChunk(ctx, chunkIndex, plaintext)
|
|
156
193
|
controller.enqueue(uint32BE(ct.length))
|
|
157
194
|
controller.enqueue(ct)
|
|
195
|
+
if (ed25519SecretKey) {
|
|
196
|
+
// AEAD tag is the trailing AES_GCM_V1_TAG_BYTES of the chunk ct.
|
|
197
|
+
chunkMacs.push(ct.slice(ct.length - AES_GCM_V1_TAG_BYTES))
|
|
198
|
+
}
|
|
158
199
|
chunkIndex += 1
|
|
159
200
|
}
|
|
160
201
|
|
|
@@ -207,6 +248,28 @@ export function createAesGcmV1EncryptStream(
|
|
|
207
248
|
controller.error(
|
|
208
249
|
new RangeError('encrypt stream: residual bytes after final chunk'),
|
|
209
250
|
)
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
if (ed25519SecretKey) {
|
|
254
|
+
if (!unauthenticatedHeader) {
|
|
255
|
+
controller.error(
|
|
256
|
+
new Error('encrypt stream: header not finalized before signing'),
|
|
257
|
+
)
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
const signature = signHeaderAndMacs({
|
|
261
|
+
ed25519SecretKey,
|
|
262
|
+
header: unauthenticatedHeader,
|
|
263
|
+
chunkMacs,
|
|
264
|
+
})
|
|
265
|
+
const publicKey = deriveEd25519PublicKey(ed25519SecretKey)
|
|
266
|
+
controller.enqueue(
|
|
267
|
+
buildSignatureBlock({
|
|
268
|
+
algorithm: SIGNATURE_ALGO_ED25519,
|
|
269
|
+
publicKey,
|
|
270
|
+
signature,
|
|
271
|
+
}),
|
|
272
|
+
)
|
|
210
273
|
}
|
|
211
274
|
},
|
|
212
275
|
}
|
|
@@ -224,6 +287,45 @@ export function createAesGcmV1EncryptStream(
|
|
|
224
287
|
|
|
225
288
|
export interface AesGcmV1DecryptStreamOptions {
|
|
226
289
|
contentKey: Uint8Array
|
|
290
|
+
/**
|
|
291
|
+
* If provided, a trailing signature block (if present) is verified
|
|
292
|
+
* against this Ed25519 public key, and the resolved metadata's
|
|
293
|
+
* `signature.verified` reflects the outcome. If omitted, a present
|
|
294
|
+
* block is parsed and surfaced but not verified; `verified` is null.
|
|
295
|
+
*/
|
|
296
|
+
expectedSignerPublicKey?: Uint8Array
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Verified signature metadata surfaced by the decrypt stream.
|
|
301
|
+
*
|
|
302
|
+
* - `algorithm`, `publicKey`, `signature` come from the parsed block.
|
|
303
|
+
* - `verified` is `true` if the caller passed `expectedSignerPublicKey`
|
|
304
|
+
* AND it matched the embedded pubkey AND the signature verifies; it
|
|
305
|
+
* is `false` if the check ran and failed; it is `null` if the caller
|
|
306
|
+
* passed no `expectedSignerPublicKey` (the application policy layer
|
|
307
|
+
* is expected to do its own verification using the surfaced bytes).
|
|
308
|
+
*/
|
|
309
|
+
export interface VerifiedSignature {
|
|
310
|
+
algorithm: number
|
|
311
|
+
publicKey: Uint8Array
|
|
312
|
+
signature: Uint8Array
|
|
313
|
+
verified: boolean | null
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export interface DecryptionMetadata {
|
|
317
|
+
/** Null when the file has no signature block (legacy / unsigned). */
|
|
318
|
+
signature: VerifiedSignature | null
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export interface AesGcmV1DecryptStreamResult {
|
|
322
|
+
stream: TransformStream<Uint8Array, Uint8Array>
|
|
323
|
+
/**
|
|
324
|
+
* Resolves once the stream finishes flushing. Rejects if the stream
|
|
325
|
+
* errored — callers awaiting metadata MUST handle rejection (or await
|
|
326
|
+
* the plaintext drain first, which surfaces the same error).
|
|
327
|
+
*/
|
|
328
|
+
metadata: Promise<DecryptionMetadata>
|
|
227
329
|
}
|
|
228
330
|
|
|
229
331
|
/**
|
|
@@ -231,14 +333,34 @@ export interface AesGcmV1DecryptStreamOptions {
|
|
|
231
333
|
* plaintext. Unlike the whole-blob decryptor, this version processes
|
|
232
334
|
* input incrementally — useful for very large files or when ciphertext
|
|
233
335
|
* arrives chunked over the network.
|
|
336
|
+
*
|
|
337
|
+
* Files written by an older library version (no signature block) decrypt
|
|
338
|
+
* unchanged: `metadata.signature` resolves to `null`. Files with a
|
|
339
|
+
* trailing signature block are parsed; if `expectedSignerPublicKey` is
|
|
340
|
+
* supplied, the signature is verified and `metadata.signature.verified`
|
|
341
|
+
* reflects the outcome.
|
|
234
342
|
*/
|
|
235
343
|
export function createAesGcmV1DecryptStream(
|
|
236
344
|
options: AesGcmV1DecryptStreamOptions,
|
|
237
|
-
):
|
|
345
|
+
): AesGcmV1DecryptStreamResult {
|
|
238
346
|
const { contentKey } = options
|
|
239
347
|
if (contentKey.length !== 32) {
|
|
240
348
|
throw new RangeError('decrypt stream: contentKey must be 32 bytes')
|
|
241
349
|
}
|
|
350
|
+
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
351
|
+
|
|
352
|
+
let resolveMetadata!: (m: DecryptionMetadata) => void
|
|
353
|
+
let rejectMetadata!: (err: unknown) => void
|
|
354
|
+
const metadata = new Promise<DecryptionMetadata>((resolve, reject) => {
|
|
355
|
+
resolveMetadata = resolve
|
|
356
|
+
rejectMetadata = reject
|
|
357
|
+
})
|
|
358
|
+
// Pre-attach a no-op handler so the user-facing promise doesn't trigger
|
|
359
|
+
// an unhandledRejection if the caller never awaits metadata (they
|
|
360
|
+
// typically await the plaintext drain, which surfaces the same error).
|
|
361
|
+
// This does NOT swallow the error for callers who DO await metadata —
|
|
362
|
+
// .catch() returns a new promise; the original `metadata` still rejects.
|
|
363
|
+
metadata.catch(() => {})
|
|
242
364
|
|
|
243
365
|
// Phases:
|
|
244
366
|
// 0 = waiting for header bytes
|
|
@@ -250,6 +372,7 @@ export function createAesGcmV1DecryptStream(
|
|
|
250
372
|
let bytesEmitted = 0
|
|
251
373
|
let nextCipherLen = -1
|
|
252
374
|
let contextPromise: ReturnType<typeof createChunkContext> | null = null
|
|
375
|
+
const chunkMacs: Uint8Array[] = []
|
|
253
376
|
|
|
254
377
|
function append(input: Uint8Array): void {
|
|
255
378
|
const merged = new Uint8Array(buffer.length + input.length)
|
|
@@ -311,6 +434,12 @@ export function createAesGcmV1DecryptStream(
|
|
|
311
434
|
const { verifyHeaderMac } = await import('../format/header.js')
|
|
312
435
|
await verifyHeaderMac(parsedHeader, contentKey)
|
|
313
436
|
|
|
437
|
+
// Enforce the suite_payload structure the whole-blob reader also checks
|
|
438
|
+
// (length == 72), so the stream and blob readers agree on what is a
|
|
439
|
+
// well-formed file. Validated after the MAC so it only interprets
|
|
440
|
+
// authenticated bytes.
|
|
441
|
+
parseAesGcmV1SuitePayload(parsedHeader.suitePayload)
|
|
442
|
+
|
|
314
443
|
contextPromise = createChunkContext(
|
|
315
444
|
contentKey,
|
|
316
445
|
parsedHeader.fileId,
|
|
@@ -357,53 +486,66 @@ export function createAesGcmV1DecryptStream(
|
|
|
357
486
|
const pt = await decryptChunk(ctx, chunkIndex, ctBytes)
|
|
358
487
|
bytesEmitted += pt.length
|
|
359
488
|
controller.enqueue(pt)
|
|
489
|
+
// AEAD tag is the trailing AES_GCM_V1_TAG_BYTES of ctBytes. Kept
|
|
490
|
+
// for optional signature verification at flush time.
|
|
491
|
+
chunkMacs.push(ctBytes.slice(ctBytes.length - AES_GCM_V1_TAG_BYTES))
|
|
360
492
|
chunkIndex += 1
|
|
361
493
|
return true
|
|
362
494
|
}
|
|
363
495
|
|
|
364
|
-
return
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
async flush(controller) {
|
|
381
|
-
try {
|
|
382
|
-
if (phase === 0) {
|
|
383
|
-
throw new HeaderError('header_too_short')
|
|
384
|
-
}
|
|
385
|
-
// No header yet means truncated input.
|
|
386
|
-
if (!parsedHeader) {
|
|
387
|
-
throw new HeaderError('header_too_short')
|
|
388
|
-
}
|
|
389
|
-
// Try once more in case flush is called with everything queued.
|
|
390
|
-
while (await tryEmitChunk(controller)) {
|
|
391
|
-
/* loop */
|
|
392
|
-
}
|
|
393
|
-
if (chunkIndex !== parsedHeader.totalChunks) {
|
|
394
|
-
throw new HeaderError('chunk_truncated')
|
|
395
|
-
}
|
|
396
|
-
if (bytesEmitted !== parsedHeader.plaintextSize) {
|
|
397
|
-
throw new HeaderError('plaintext_size_mismatch')
|
|
496
|
+
return {
|
|
497
|
+
stream: new TransformStream<Uint8Array, Uint8Array>({
|
|
498
|
+
async transform(chunk, controller) {
|
|
499
|
+
append(chunk)
|
|
500
|
+
try {
|
|
501
|
+
if (phase === 0) {
|
|
502
|
+
const ok = await tryParseHeader()
|
|
503
|
+
if (!ok) return
|
|
504
|
+
}
|
|
505
|
+
// Drain as many chunks as we have data for.
|
|
506
|
+
while (await tryEmitChunk(controller)) {
|
|
507
|
+
/* loop */
|
|
508
|
+
}
|
|
509
|
+
} catch (err) {
|
|
510
|
+
controller.error(err as Error)
|
|
511
|
+
rejectMetadata(err)
|
|
398
512
|
}
|
|
399
|
-
|
|
400
|
-
|
|
513
|
+
},
|
|
514
|
+
async flush(controller) {
|
|
515
|
+
try {
|
|
516
|
+
if (phase === 0) {
|
|
517
|
+
throw new HeaderError('header_too_short')
|
|
518
|
+
}
|
|
519
|
+
// No header yet means truncated input.
|
|
520
|
+
if (!parsedHeader) {
|
|
521
|
+
throw new HeaderError('header_too_short')
|
|
522
|
+
}
|
|
523
|
+
// Try once more in case flush is called with everything queued.
|
|
524
|
+
while (await tryEmitChunk(controller)) {
|
|
525
|
+
/* loop */
|
|
526
|
+
}
|
|
527
|
+
if (chunkIndex !== parsedHeader.totalChunks) {
|
|
528
|
+
throw new HeaderError('chunk_truncated')
|
|
529
|
+
}
|
|
530
|
+
if (bytesEmitted !== parsedHeader.plaintextSize) {
|
|
531
|
+
throw new HeaderError('plaintext_size_mismatch')
|
|
532
|
+
}
|
|
533
|
+
const signatureResult = finalizeSignatureMetadata({
|
|
534
|
+
trailingBytes: buffer,
|
|
535
|
+
header: parsedHeader.unauthenticatedBytes,
|
|
536
|
+
chunkMacs,
|
|
537
|
+
expectedSignerPublicKey,
|
|
538
|
+
})
|
|
539
|
+
buffer = new Uint8Array(0)
|
|
540
|
+
resolveMetadata({ signature: signatureResult })
|
|
541
|
+
} catch (err) {
|
|
542
|
+
controller.error(err as Error)
|
|
543
|
+
rejectMetadata(err)
|
|
401
544
|
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
})
|
|
545
|
+
},
|
|
546
|
+
}),
|
|
547
|
+
metadata,
|
|
548
|
+
}
|
|
407
549
|
}
|
|
408
550
|
|
|
409
551
|
// ─────────────────────────────────────────────────────────────────────
|
|
@@ -433,11 +575,85 @@ export function encryptStreamAesGcmV1(
|
|
|
433
575
|
|
|
434
576
|
/**
|
|
435
577
|
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 decrypt
|
|
436
|
-
* transform and return the resulting plaintext ReadableStream
|
|
578
|
+
* transform and return the resulting plaintext ReadableStream alongside
|
|
579
|
+
* a `metadata` Promise that resolves at end-of-stream with the parsed
|
|
580
|
+
* signature block (or `signature: null` for unsigned / legacy files).
|
|
437
581
|
*/
|
|
438
582
|
export function decryptStreamAesGcmV1(
|
|
439
583
|
source: ReadableStream<Uint8Array>,
|
|
440
584
|
options: AesGcmV1DecryptStreamOptions,
|
|
441
|
-
):
|
|
442
|
-
|
|
585
|
+
): {
|
|
586
|
+
plaintext: ReadableStream<Uint8Array>
|
|
587
|
+
metadata: Promise<DecryptionMetadata>
|
|
588
|
+
} {
|
|
589
|
+
const { stream, metadata } = createAesGcmV1DecryptStream(options)
|
|
590
|
+
return {
|
|
591
|
+
plaintext: source.pipeThrough(stream),
|
|
592
|
+
metadata,
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Parse a trailing signature block (if any) from the decrypt stream's
|
|
598
|
+
* residual buffer, optionally verifying it against the caller's
|
|
599
|
+
* trusted public key.
|
|
600
|
+
*
|
|
601
|
+
* Shared between the AES-GCM and PQ-hybrid stream decoders so the
|
|
602
|
+
* signature semantics live in one place.
|
|
603
|
+
*/
|
|
604
|
+
export function finalizeSignatureMetadata(input: {
|
|
605
|
+
trailingBytes: Uint8Array
|
|
606
|
+
header: Uint8Array
|
|
607
|
+
chunkMacs: ReadonlyArray<Uint8Array>
|
|
608
|
+
expectedSignerPublicKey: Uint8Array | undefined
|
|
609
|
+
}): VerifiedSignature | null {
|
|
610
|
+
if (input.trailingBytes.length === 0) {
|
|
611
|
+
return null
|
|
612
|
+
}
|
|
613
|
+
let parsed: { block: SignatureBlock; consumed: number }
|
|
614
|
+
try {
|
|
615
|
+
parsed = parseSignatureBlock(input.trailingBytes, 0)
|
|
616
|
+
} catch (err) {
|
|
617
|
+
if (err instanceof SignatureError) {
|
|
618
|
+
throw new HeaderError(`signature_block_${err.code}`)
|
|
619
|
+
}
|
|
620
|
+
throw err
|
|
621
|
+
}
|
|
622
|
+
if (parsed.consumed !== input.trailingBytes.length) {
|
|
623
|
+
throw new HeaderError('trailing_bytes_after_signature_block')
|
|
624
|
+
}
|
|
625
|
+
const { algorithm, publicKey, signature } = parsed.block
|
|
626
|
+
|
|
627
|
+
let verified: boolean | null = null
|
|
628
|
+
if (input.expectedSignerPublicKey) {
|
|
629
|
+
if (algorithm !== SIGNATURE_ALGO_ED25519) {
|
|
630
|
+
// Unknown / unsupported algorithm: cannot verify with our
|
|
631
|
+
// Ed25519-only verifier; treat as a verification failure under
|
|
632
|
+
// the caller's policy (they asked us to verify, we couldn't).
|
|
633
|
+
verified = false
|
|
634
|
+
} else {
|
|
635
|
+
// Pubkey-pinning: if the embedded pubkey doesn't match the caller's
|
|
636
|
+
// expected one, fail without doing the (still-honest) Ed25519
|
|
637
|
+
// math. This keeps the "verified=true" contract tight: it means
|
|
638
|
+
// the file was signed by the key the caller expected.
|
|
639
|
+
verified =
|
|
640
|
+
bytesEqualConstantTime(publicKey, input.expectedSignerPublicKey) &&
|
|
641
|
+
verifyHeaderAndMacs({
|
|
642
|
+
ed25519PublicKey: input.expectedSignerPublicKey,
|
|
643
|
+
header: input.header,
|
|
644
|
+
chunkMacs: input.chunkMacs,
|
|
645
|
+
signature,
|
|
646
|
+
})
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return { algorithm, publicKey, signature, verified }
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function bytesEqualConstantTime(a: Uint8Array, b: Uint8Array): boolean {
|
|
653
|
+
if (a.length !== b.length) return false
|
|
654
|
+
let diff = 0
|
|
655
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
656
|
+
diff |= a[i]! ^ b[i]!
|
|
657
|
+
}
|
|
658
|
+
return diff === 0
|
|
443
659
|
}
|