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