@shieldfive/crypto 1.0.0-rc.3 → 1.0.0-rc.4
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/dist/format/signature-tail.d.ts +54 -0
- package/dist/format/signature-tail.d.ts.map +1 -0
- package/dist/format/signature-tail.js +82 -0
- package/dist/format/signature-tail.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/migration/v0-bridge.d.ts +22 -2
- package/dist/migration/v0-bridge.d.ts.map +1 -1
- package/dist/migration/v0-bridge.js.map +1 -1
- package/dist/streams/aes-gcm-v1.d.ts +3 -37
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
- package/dist/streams/aes-gcm-v1.js +8 -64
- package/dist/streams/aes-gcm-v1.js.map +1 -1
- package/dist/suites/aes-gcm-v1/api.d.ts +18 -0
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v1/api.js +23 -5
- package/dist/suites/aes-gcm-v1/api.js.map +1 -1
- package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v1/index.js +11 -0
- package/dist/suites/aes-gcm-v1/index.js.map +1 -1
- package/dist/suites/aes-gcm-v2/api.d.ts +17 -0
- package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v2/api.js +19 -3
- package/dist/suites/aes-gcm-v2/api.js.map +1 -1
- package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v2/index.js +7 -0
- package/dist/suites/aes-gcm-v2/index.js.map +1 -1
- package/dist/suites/pq-hybrid-v1/api.d.ts +17 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -1
- package/dist/suites/pq-hybrid-v1/api.js +19 -3
- package/dist/suites/pq-hybrid-v1/api.js.map +1 -1
- package/dist/suites/xchacha-v1/api.d.ts +17 -0
- package/dist/suites/xchacha-v1/api.d.ts.map +1 -1
- package/dist/suites/xchacha-v1/api.js +19 -3
- package/dist/suites/xchacha-v1/api.js.map +1 -1
- package/dist/suites/xchacha-v1/index.d.ts.map +1 -1
- package/dist/suites/xchacha-v1/index.js +8 -0
- package/dist/suites/xchacha-v1/index.js.map +1 -1
- package/package.json +1 -1
- package/spec/key-derivation.md +19 -5
- package/src/format/signature-tail.ts +114 -0
- package/src/index.ts +1 -1
- package/src/migration/v0-bridge.ts +22 -2
- package/src/streams/aes-gcm-v1.ts +17 -91
- package/src/suites/aes-gcm-v1/api.ts +46 -5
- package/src/suites/aes-gcm-v1/index.ts +11 -0
- package/src/suites/aes-gcm-v2/api.ts +41 -3
- package/src/suites/aes-gcm-v2/index.ts +7 -0
- package/src/suites/pq-hybrid-v1/api.ts +41 -3
- package/src/suites/xchacha-v1/api.ts +41 -3
- package/src/suites/xchacha-v1/index.ts +8 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared trailing-signature-block handling for the v1 wire format.
|
|
3
|
+
*
|
|
4
|
+
* A v1 file MAY carry an optional trailing Ed25519 signature block after
|
|
5
|
+
* the last chunk (see `spec/format-v1.md § "Signature block"`). Both the
|
|
6
|
+
* streaming decoders and the whole-blob decryptors need identical
|
|
7
|
+
* semantics for that tail — parse it, reject malformed trailing garbage,
|
|
8
|
+
* and optionally verify it against a caller-pinned public key — so the
|
|
9
|
+
* logic lives here, in one place, rather than being duplicated (or, as
|
|
10
|
+
* happened before, implemented by the streams and silently omitted by the
|
|
11
|
+
* blob readers).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { HeaderError } from './header.js'
|
|
15
|
+
import {
|
|
16
|
+
parseSignatureBlock,
|
|
17
|
+
type SignatureBlock,
|
|
18
|
+
SignatureError,
|
|
19
|
+
SIGNATURE_ALGO_ED25519,
|
|
20
|
+
verifySenderTranscript,
|
|
21
|
+
} from '../identity/sign.js'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Verified signature metadata surfaced by a v1 decryptor.
|
|
25
|
+
*
|
|
26
|
+
* - `algorithm`, `publicKey`, `signature` come from the parsed block.
|
|
27
|
+
* - `verified` is `true` if the caller passed `expectedSignerPublicKey`
|
|
28
|
+
* AND it matched the embedded pubkey AND the signature verifies; it
|
|
29
|
+
* is `false` if the check ran and failed; it is `null` if the caller
|
|
30
|
+
* passed no `expectedSignerPublicKey` (the application policy layer
|
|
31
|
+
* is expected to do its own verification using the surfaced bytes).
|
|
32
|
+
*/
|
|
33
|
+
export interface VerifiedSignature {
|
|
34
|
+
algorithm: number
|
|
35
|
+
publicKey: Uint8Array
|
|
36
|
+
signature: Uint8Array
|
|
37
|
+
verified: boolean | null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DecryptionMetadata {
|
|
41
|
+
/** Null when the file has no signature block (legacy / unsigned). */
|
|
42
|
+
signature: VerifiedSignature | null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parse a trailing signature block (if any) from a decryptor's residual
|
|
47
|
+
* bytes, optionally verifying it against the caller's trusted public key.
|
|
48
|
+
*
|
|
49
|
+
* `transcriptDigest` is the {@link SenderSigTranscript} digest rebuilt
|
|
50
|
+
* over the header and every chunk's full ciphertext as it was decrypted,
|
|
51
|
+
* so `verified: true` means the signer signed exactly these bytes. It is
|
|
52
|
+
* only consulted when `expectedSignerPublicKey` is supplied.
|
|
53
|
+
*
|
|
54
|
+
* Returns `null` for a file with no trailing bytes (legacy / unsigned).
|
|
55
|
+
* Throws `HeaderError` if the trailing bytes are not a well-formed
|
|
56
|
+
* signature block or carry garbage after it — the spec's MUST-reject.
|
|
57
|
+
*
|
|
58
|
+
* Shared between the AES-GCM and PQ-hybrid stream decoders and every
|
|
59
|
+
* whole-blob decryptor so the signature semantics live in one place.
|
|
60
|
+
*/
|
|
61
|
+
export function finalizeSignatureMetadata(input: {
|
|
62
|
+
trailingBytes: Uint8Array
|
|
63
|
+
transcriptDigest: Uint8Array
|
|
64
|
+
expectedSignerPublicKey: Uint8Array | undefined
|
|
65
|
+
}): VerifiedSignature | null {
|
|
66
|
+
if (input.trailingBytes.length === 0) {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
let parsed: { block: SignatureBlock; consumed: number }
|
|
70
|
+
try {
|
|
71
|
+
parsed = parseSignatureBlock(input.trailingBytes, 0)
|
|
72
|
+
} catch (err) {
|
|
73
|
+
if (err instanceof SignatureError) {
|
|
74
|
+
throw new HeaderError(`signature_block_${err.code}`)
|
|
75
|
+
}
|
|
76
|
+
throw err
|
|
77
|
+
}
|
|
78
|
+
if (parsed.consumed !== input.trailingBytes.length) {
|
|
79
|
+
throw new HeaderError('trailing_bytes_after_signature_block')
|
|
80
|
+
}
|
|
81
|
+
const { algorithm, publicKey, signature } = parsed.block
|
|
82
|
+
|
|
83
|
+
let verified: boolean | null = null
|
|
84
|
+
if (input.expectedSignerPublicKey) {
|
|
85
|
+
if (algorithm !== SIGNATURE_ALGO_ED25519) {
|
|
86
|
+
// Unknown / unsupported algorithm: cannot verify with our
|
|
87
|
+
// Ed25519-only verifier; treat as a verification failure under
|
|
88
|
+
// the caller's policy (they asked us to verify, we couldn't).
|
|
89
|
+
verified = false
|
|
90
|
+
} else {
|
|
91
|
+
// Pubkey-pinning: if the embedded pubkey doesn't match the caller's
|
|
92
|
+
// expected one, fail without doing the (still-honest) Ed25519
|
|
93
|
+
// math. This keeps the "verified=true" contract tight: it means
|
|
94
|
+
// the file was signed by the key the caller expected.
|
|
95
|
+
verified =
|
|
96
|
+
bytesEqualConstantTime(publicKey, input.expectedSignerPublicKey) &&
|
|
97
|
+
verifySenderTranscript({
|
|
98
|
+
ed25519PublicKey: input.expectedSignerPublicKey,
|
|
99
|
+
transcriptDigest: input.transcriptDigest,
|
|
100
|
+
signature,
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { algorithm, publicKey, signature, verified }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function bytesEqualConstantTime(a: Uint8Array, b: Uint8Array): boolean {
|
|
108
|
+
if (a.length !== b.length) return false
|
|
109
|
+
let diff = 0
|
|
110
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
111
|
+
diff |= a[i]! ^ b[i]!
|
|
112
|
+
}
|
|
113
|
+
return diff === 0
|
|
114
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -219,7 +219,7 @@ export async function autoDecryptBlob(
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
/** Library version, also written into format documentation. */
|
|
222
|
-
export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-rc.
|
|
222
|
+
export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-rc.4'
|
|
223
223
|
|
|
224
224
|
/** Default cipher suite for new files when not specified by the caller. */
|
|
225
225
|
export const DEFAULT_SUITE = SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1
|
|
@@ -52,9 +52,29 @@ export {
|
|
|
52
52
|
export interface V0EncryptOptions {
|
|
53
53
|
/** Plaintext blob. */
|
|
54
54
|
blob: Blob
|
|
55
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* 32-byte AES-256 content key. Must already be unwrapped.
|
|
57
|
+
*
|
|
58
|
+
* SECURITY: `contentKey` and `noncePrefix` together form this file's
|
|
59
|
+
* AES-GCM nonce namespace. Reusing the same (contentKey, noncePrefix)
|
|
60
|
+
* pair across two DIFFERENT plaintexts reuses every chunk's (key, IV)
|
|
61
|
+
* and is catastrophic — it reproduces the keystream (ct1 ^ ct2 = pt1 ^
|
|
62
|
+
* pt2, enabling plaintext recovery) and leaks the GHASH authentication
|
|
63
|
+
* key (enabling forgery). See the note on `noncePrefix`.
|
|
64
|
+
*/
|
|
56
65
|
contentKey: Uint8Array
|
|
57
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* 4-byte per-file nonce prefix. Generated fresh (random) when omitted —
|
|
68
|
+
* that is the safe default and requires no caller cooperation.
|
|
69
|
+
*
|
|
70
|
+
* SECURITY: if you persist and re-supply a prefix (which the returned
|
|
71
|
+
* {@link V0EncryptResult} explicitly invites you to do), you MUST use a
|
|
72
|
+
* FRESH prefix whenever you re-encrypt an edited file under the same
|
|
73
|
+
* `contentKey`. Re-encrypting different content under an unchanged
|
|
74
|
+
* (contentKey, noncePrefix) pair reuses the AES-GCM nonce and is
|
|
75
|
+
* catastrophic (see `contentKey`). This legacy v0 writer exists solely
|
|
76
|
+
* for the Phase-1 migration; new data must use a v1 suite instead.
|
|
77
|
+
*/
|
|
58
78
|
noncePrefix?: Uint8Array
|
|
59
79
|
/** Plaintext bytes per chunk. Production uses 5 MiB or 8 MiB. */
|
|
60
80
|
chunkSize: number
|
|
@@ -46,14 +46,20 @@ import {
|
|
|
46
46
|
buildSignatureBlock,
|
|
47
47
|
deriveEd25519PublicKey,
|
|
48
48
|
ED25519_SECRET_KEY_LENGTH,
|
|
49
|
-
parseSignatureBlock,
|
|
50
49
|
SenderSigTranscript,
|
|
51
50
|
SIGNATURE_ALGO_ED25519,
|
|
52
51
|
signSenderTranscript,
|
|
53
|
-
type SignatureBlock,
|
|
54
|
-
SignatureError,
|
|
55
|
-
verifySenderTranscript,
|
|
56
52
|
} from '../identity/sign.js'
|
|
53
|
+
import {
|
|
54
|
+
finalizeSignatureMetadata,
|
|
55
|
+
type DecryptionMetadata,
|
|
56
|
+
} from '../format/signature-tail.js'
|
|
57
|
+
|
|
58
|
+
// Re-exported so `streams/aes-gcm-v1.js` stays the import site these
|
|
59
|
+
// symbols have always had (the PQ-hybrid stream and public barrels
|
|
60
|
+
// import them from here).
|
|
61
|
+
export { finalizeSignatureMetadata }
|
|
62
|
+
export type { DecryptionMetadata, VerifiedSignature } from '../format/signature-tail.js'
|
|
57
63
|
|
|
58
64
|
const LENGTH_PREFIX_BYTES = 4
|
|
59
65
|
|
|
@@ -295,27 +301,11 @@ export interface AesGcmV1DecryptStreamOptions {
|
|
|
295
301
|
expectedSignerPublicKey?: Uint8Array
|
|
296
302
|
}
|
|
297
303
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
* AND it matched the embedded pubkey AND the signature verifies; it
|
|
304
|
-
* is `false` if the check ran and failed; it is `null` if the caller
|
|
305
|
-
* passed no `expectedSignerPublicKey` (the application policy layer
|
|
306
|
-
* is expected to do its own verification using the surfaced bytes).
|
|
307
|
-
*/
|
|
308
|
-
export interface VerifiedSignature {
|
|
309
|
-
algorithm: number
|
|
310
|
-
publicKey: Uint8Array
|
|
311
|
-
signature: Uint8Array
|
|
312
|
-
verified: boolean | null
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
export interface DecryptionMetadata {
|
|
316
|
-
/** Null when the file has no signature block (legacy / unsigned). */
|
|
317
|
-
signature: VerifiedSignature | null
|
|
318
|
-
}
|
|
304
|
+
// `VerifiedSignature` and `DecryptionMetadata` (and the shared
|
|
305
|
+
// `finalizeSignatureMetadata` helper) now live in
|
|
306
|
+
// `../format/signature-tail.js` so the whole-blob decryptors can reuse
|
|
307
|
+
// the exact same tail semantics. They are re-exported below so existing
|
|
308
|
+
// import paths (`streams/aes-gcm-v1.js`) keep working unchanged.
|
|
319
309
|
|
|
320
310
|
export interface AesGcmV1DecryptStreamResult {
|
|
321
311
|
stream: TransformStream<Uint8Array, Uint8Array>
|
|
@@ -598,69 +588,5 @@ export function decryptStreamAesGcmV1(
|
|
|
598
588
|
}
|
|
599
589
|
}
|
|
600
590
|
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
* residual buffer, optionally verifying it against the caller's
|
|
604
|
-
* trusted public key.
|
|
605
|
-
*
|
|
606
|
-
* `transcriptDigest` is the {@link SenderSigTranscript} digest rebuilt
|
|
607
|
-
* over the header and every chunk's full ciphertext as it was decrypted,
|
|
608
|
-
* so `verified: true` means the signer signed exactly these bytes.
|
|
609
|
-
*
|
|
610
|
-
* Shared between the AES-GCM and PQ-hybrid stream decoders so the
|
|
611
|
-
* signature semantics live in one place.
|
|
612
|
-
*/
|
|
613
|
-
export function finalizeSignatureMetadata(input: {
|
|
614
|
-
trailingBytes: Uint8Array
|
|
615
|
-
transcriptDigest: Uint8Array
|
|
616
|
-
expectedSignerPublicKey: Uint8Array | undefined
|
|
617
|
-
}): VerifiedSignature | null {
|
|
618
|
-
if (input.trailingBytes.length === 0) {
|
|
619
|
-
return null
|
|
620
|
-
}
|
|
621
|
-
let parsed: { block: SignatureBlock; consumed: number }
|
|
622
|
-
try {
|
|
623
|
-
parsed = parseSignatureBlock(input.trailingBytes, 0)
|
|
624
|
-
} catch (err) {
|
|
625
|
-
if (err instanceof SignatureError) {
|
|
626
|
-
throw new HeaderError(`signature_block_${err.code}`)
|
|
627
|
-
}
|
|
628
|
-
throw err
|
|
629
|
-
}
|
|
630
|
-
if (parsed.consumed !== input.trailingBytes.length) {
|
|
631
|
-
throw new HeaderError('trailing_bytes_after_signature_block')
|
|
632
|
-
}
|
|
633
|
-
const { algorithm, publicKey, signature } = parsed.block
|
|
634
|
-
|
|
635
|
-
let verified: boolean | null = null
|
|
636
|
-
if (input.expectedSignerPublicKey) {
|
|
637
|
-
if (algorithm !== SIGNATURE_ALGO_ED25519) {
|
|
638
|
-
// Unknown / unsupported algorithm: cannot verify with our
|
|
639
|
-
// Ed25519-only verifier; treat as a verification failure under
|
|
640
|
-
// the caller's policy (they asked us to verify, we couldn't).
|
|
641
|
-
verified = false
|
|
642
|
-
} else {
|
|
643
|
-
// Pubkey-pinning: if the embedded pubkey doesn't match the caller's
|
|
644
|
-
// expected one, fail without doing the (still-honest) Ed25519
|
|
645
|
-
// math. This keeps the "verified=true" contract tight: it means
|
|
646
|
-
// the file was signed by the key the caller expected.
|
|
647
|
-
verified =
|
|
648
|
-
bytesEqualConstantTime(publicKey, input.expectedSignerPublicKey) &&
|
|
649
|
-
verifySenderTranscript({
|
|
650
|
-
ed25519PublicKey: input.expectedSignerPublicKey,
|
|
651
|
-
transcriptDigest: input.transcriptDigest,
|
|
652
|
-
signature,
|
|
653
|
-
})
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
return { algorithm, publicKey, signature, verified }
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
function bytesEqualConstantTime(a: Uint8Array, b: Uint8Array): boolean {
|
|
660
|
-
if (a.length !== b.length) return false
|
|
661
|
-
let diff = 0
|
|
662
|
-
for (let i = 0; i < a.length; i += 1) {
|
|
663
|
-
diff |= a[i]! ^ b[i]!
|
|
664
|
-
}
|
|
665
|
-
return diff === 0
|
|
666
|
-
}
|
|
591
|
+
// `finalizeSignatureMetadata` moved to `../format/signature-tail.js`
|
|
592
|
+
// (re-exported above) so the whole-blob decryptors share it.
|
|
@@ -38,6 +38,11 @@ import {
|
|
|
38
38
|
readUint32BE,
|
|
39
39
|
uint32BE,
|
|
40
40
|
} from '../../internal/encoding.js'
|
|
41
|
+
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
42
|
+
import {
|
|
43
|
+
finalizeSignatureMetadata,
|
|
44
|
+
type VerifiedSignature,
|
|
45
|
+
} from '../../format/signature-tail.js'
|
|
41
46
|
|
|
42
47
|
const LENGTH_PREFIX_BYTES = 4
|
|
43
48
|
|
|
@@ -165,6 +170,23 @@ export interface DecryptOptions {
|
|
|
165
170
|
* an explicit allowlist.
|
|
166
171
|
*/
|
|
167
172
|
allowedSuites?: ReadonlyArray<number>
|
|
173
|
+
/**
|
|
174
|
+
* If provided, a trailing sender-attribution signature block (if
|
|
175
|
+
* present) is verified against this Ed25519 public key, and the result
|
|
176
|
+
* is surfaced via `onSignature` with `verified` set accordingly. If
|
|
177
|
+
* omitted, a present block is still parsed and surfaced but not
|
|
178
|
+
* verified (`verified: null`). See `spec/format-v1.md § "Signature
|
|
179
|
+
* block"`. Reaches parity with the streaming decoder.
|
|
180
|
+
*/
|
|
181
|
+
expectedSignerPublicKey?: Uint8Array
|
|
182
|
+
/**
|
|
183
|
+
* Called once, after the plaintext is fully decrypted, with the file's
|
|
184
|
+
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
185
|
+
* Callers that require attribution MUST gate on
|
|
186
|
+
* `signature?.verified === true`; a `null` signature or any other
|
|
187
|
+
* `verified` value means the plaintext is unattributed.
|
|
188
|
+
*/
|
|
189
|
+
onSignature?: (signature: VerifiedSignature | null) => void
|
|
168
190
|
}
|
|
169
191
|
|
|
170
192
|
/**
|
|
@@ -231,6 +253,13 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
231
253
|
parsed.totalChunks,
|
|
232
254
|
)
|
|
233
255
|
|
|
256
|
+
// Rebuild the sender-attribution transcript only when the caller wants
|
|
257
|
+
// to verify — otherwise a present block is still surfaced (verified:
|
|
258
|
+
// null) without paying for the extra hash over every ciphertext byte.
|
|
259
|
+
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
260
|
+
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
261
|
+
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
262
|
+
|
|
234
263
|
const plaintextParts: BlobPart[] = []
|
|
235
264
|
let cursor = parsed.headerLength
|
|
236
265
|
let bytesEmitted = 0
|
|
@@ -269,18 +298,30 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
269
298
|
const plaintext = await decryptChunk(ctx, i, cipherBytes)
|
|
270
299
|
bytesEmitted += plaintext.length
|
|
271
300
|
plaintextParts.push(asBlobPart(plaintext))
|
|
301
|
+
transcript?.absorbChunk(i, cipherBytes)
|
|
272
302
|
|
|
273
303
|
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1))
|
|
274
304
|
}
|
|
275
305
|
|
|
276
|
-
//
|
|
277
|
-
// the file must have no trailing garbage past the last chunk.
|
|
306
|
+
// Total bytes emitted must equal plaintextSize.
|
|
278
307
|
if (bytesEmitted !== parsed.plaintextSize) {
|
|
279
308
|
throw new HeaderError('plaintext_size_mismatch')
|
|
280
309
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
310
|
+
|
|
311
|
+
// Any bytes after the last chunk are an optional Ed25519 signature
|
|
312
|
+
// block (spec/format-v1.md § "Signature block"): parse and surface it,
|
|
313
|
+
// rejecting only malformed trailing garbage — instead of rejecting
|
|
314
|
+
// every signed file, which diverged from the streaming decoder.
|
|
315
|
+
const trailingBytes =
|
|
316
|
+
cursor < blob.size
|
|
317
|
+
? new Uint8Array(await blob.slice(cursor).arrayBuffer())
|
|
318
|
+
: new Uint8Array(0)
|
|
319
|
+
const signature = finalizeSignatureMetadata({
|
|
320
|
+
trailingBytes,
|
|
321
|
+
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
322
|
+
expectedSignerPublicKey,
|
|
323
|
+
})
|
|
324
|
+
options.onSignature?.(signature)
|
|
284
325
|
|
|
285
326
|
if (parsed.totalChunks === 0) onProgress?.(1)
|
|
286
327
|
|
|
@@ -52,6 +52,17 @@ export function parseAesGcmV1SuitePayload(
|
|
|
52
52
|
'aes-gcm-v1: suite payload must be 72 bytes',
|
|
53
53
|
)
|
|
54
54
|
}
|
|
55
|
+
// The 48-byte AES-GCM wrap (32-byte key + 16-byte tag) is zero-padded to
|
|
56
|
+
// 60 bytes; the reserved pad (bytes 48..60) MUST be all zero, mirroring
|
|
57
|
+
// suite 0x03's M6 check (src/suites/pq-hybrid-v1/index.ts). Without it a
|
|
58
|
+
// content-key holder could set arbitrary pad bytes, recompute the header
|
|
59
|
+
// MAC, and ship a MAC-valid file no reader validates — a canonicalization
|
|
60
|
+
// gap where two bit-distinct files decrypt identically.
|
|
61
|
+
for (let i = 48; i < 60; i += 1) {
|
|
62
|
+
if (bytes[i] !== 0) {
|
|
63
|
+
throw new RangeError('aes-gcm-v1: reserved wrapped_key pad must be zero')
|
|
64
|
+
}
|
|
65
|
+
}
|
|
55
66
|
return {
|
|
56
67
|
wrappedKey: bytes.slice(0, 60),
|
|
57
68
|
wrapIv: bytes.slice(60, 72),
|
|
@@ -34,6 +34,11 @@ import {
|
|
|
34
34
|
readUint32BE,
|
|
35
35
|
uint32BE,
|
|
36
36
|
} from '../../internal/encoding.js'
|
|
37
|
+
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
38
|
+
import {
|
|
39
|
+
finalizeSignatureMetadata,
|
|
40
|
+
type VerifiedSignature,
|
|
41
|
+
} from '../../format/signature-tail.js'
|
|
37
42
|
|
|
38
43
|
const LENGTH_PREFIX_BYTES = 4
|
|
39
44
|
|
|
@@ -154,6 +159,22 @@ export interface DecryptOptions {
|
|
|
154
159
|
contentKey: Uint8Array
|
|
155
160
|
onProgress?: ProgressCallback
|
|
156
161
|
allowedSuites?: ReadonlyArray<number>
|
|
162
|
+
/**
|
|
163
|
+
* If provided, a trailing sender-attribution signature block (if
|
|
164
|
+
* present) is verified against this Ed25519 public key, and the result
|
|
165
|
+
* is surfaced via `onSignature` with `verified` set accordingly. If
|
|
166
|
+
* omitted, a present block is still parsed and surfaced but not
|
|
167
|
+
* verified (`verified: null`). See `spec/format-v1.md § "Signature
|
|
168
|
+
* block"`.
|
|
169
|
+
*/
|
|
170
|
+
expectedSignerPublicKey?: Uint8Array
|
|
171
|
+
/**
|
|
172
|
+
* Called once, after the plaintext is fully decrypted, with the file's
|
|
173
|
+
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
174
|
+
* Callers that require attribution MUST gate on
|
|
175
|
+
* `signature?.verified === true`.
|
|
176
|
+
*/
|
|
177
|
+
onSignature?: (signature: VerifiedSignature | null) => void
|
|
157
178
|
}
|
|
158
179
|
|
|
159
180
|
/**
|
|
@@ -211,6 +232,12 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
211
232
|
parsed.totalChunks,
|
|
212
233
|
)
|
|
213
234
|
|
|
235
|
+
// Rebuild the sender-attribution transcript only when verifying, so an
|
|
236
|
+
// unsigned decrypt pays nothing for the extra hash over ciphertext.
|
|
237
|
+
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
238
|
+
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
239
|
+
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
240
|
+
|
|
214
241
|
const plaintextParts: BlobPart[] = []
|
|
215
242
|
let cursor = parsed.headerLength
|
|
216
243
|
let bytesEmitted = 0
|
|
@@ -247,6 +274,7 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
247
274
|
const plaintext = await decryptChunk(ctx, i, cipherBytes)
|
|
248
275
|
bytesEmitted += plaintext.length
|
|
249
276
|
plaintextParts.push(asBlobPart(plaintext))
|
|
277
|
+
transcript?.absorbChunk(i, cipherBytes)
|
|
250
278
|
|
|
251
279
|
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1))
|
|
252
280
|
}
|
|
@@ -254,9 +282,19 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
254
282
|
if (bytesEmitted !== parsed.plaintextSize) {
|
|
255
283
|
throw new HeaderError('plaintext_size_mismatch')
|
|
256
284
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
285
|
+
|
|
286
|
+
// Trailing bytes are an optional signature block, not corruption; parse
|
|
287
|
+
// and surface it, rejecting only malformed trailing garbage.
|
|
288
|
+
const trailingBytes =
|
|
289
|
+
cursor < blob.size
|
|
290
|
+
? new Uint8Array(await blob.slice(cursor).arrayBuffer())
|
|
291
|
+
: new Uint8Array(0)
|
|
292
|
+
const signature = finalizeSignatureMetadata({
|
|
293
|
+
trailingBytes,
|
|
294
|
+
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
295
|
+
expectedSignerPublicKey,
|
|
296
|
+
})
|
|
297
|
+
options.onSignature?.(signature)
|
|
260
298
|
|
|
261
299
|
if (parsed.totalChunks === 0) onProgress?.(1)
|
|
262
300
|
|
|
@@ -54,6 +54,13 @@ export function parseAesGcmV2SuitePayload(
|
|
|
54
54
|
if (bytes.length !== AES_GCM_V2_SUITE_PAYLOAD_LENGTH) {
|
|
55
55
|
throw new RangeError('aes-gcm-v2: suite payload must be 72 bytes')
|
|
56
56
|
}
|
|
57
|
+
// Reserved pad after the 48-byte wrap (bytes 48..60) MUST be all zero —
|
|
58
|
+
// same invariant suite 0x03 enforces (M6). See parseAesGcmV1SuitePayload.
|
|
59
|
+
for (let i = 48; i < 60; i += 1) {
|
|
60
|
+
if (bytes[i] !== 0) {
|
|
61
|
+
throw new RangeError('aes-gcm-v2: reserved wrapped_key pad must be zero')
|
|
62
|
+
}
|
|
63
|
+
}
|
|
57
64
|
return {
|
|
58
65
|
wrappedKey: bytes.slice(0, 60),
|
|
59
66
|
wrapIv: bytes.slice(60, 72),
|
|
@@ -40,6 +40,11 @@ import {
|
|
|
40
40
|
uint32BE,
|
|
41
41
|
} from '../../internal/encoding.js'
|
|
42
42
|
import { randomBytes } from '../../internal/runtime.js'
|
|
43
|
+
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
44
|
+
import {
|
|
45
|
+
finalizeSignatureMetadata,
|
|
46
|
+
type VerifiedSignature,
|
|
47
|
+
} from '../../format/signature-tail.js'
|
|
43
48
|
|
|
44
49
|
// Re-exports of suite primitives that are useful to callers outside the
|
|
45
50
|
// streaming/whole-blob API surfaces. `decapsulateFromHeader` lets a
|
|
@@ -162,6 +167,22 @@ export interface DecryptOptions {
|
|
|
162
167
|
/** Classical envelope key */
|
|
163
168
|
envelopeKey: Uint8Array
|
|
164
169
|
onProgress?: ProgressCallback
|
|
170
|
+
/**
|
|
171
|
+
* If provided, a trailing sender-attribution signature block (if
|
|
172
|
+
* present) is verified against this Ed25519 public key, and the result
|
|
173
|
+
* is surfaced via `onSignature` with `verified` set accordingly. If
|
|
174
|
+
* omitted, a present block is still parsed and surfaced but not
|
|
175
|
+
* verified (`verified: null`). See `spec/format-v1.md § "Signature
|
|
176
|
+
* block"`.
|
|
177
|
+
*/
|
|
178
|
+
expectedSignerPublicKey?: Uint8Array
|
|
179
|
+
/**
|
|
180
|
+
* Called once, after the plaintext is fully decrypted, with the file's
|
|
181
|
+
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
182
|
+
* Callers that require attribution MUST gate on
|
|
183
|
+
* `signature?.verified === true`.
|
|
184
|
+
*/
|
|
185
|
+
onSignature?: (signature: VerifiedSignature | null) => void
|
|
165
186
|
}
|
|
166
187
|
|
|
167
188
|
export async function decryptBlob(
|
|
@@ -218,6 +239,12 @@ export async function decryptBlob(
|
|
|
218
239
|
parsed.totalChunks,
|
|
219
240
|
)
|
|
220
241
|
|
|
242
|
+
// Rebuild the sender-attribution transcript only when verifying, so an
|
|
243
|
+
// unsigned decrypt pays nothing for the extra hash over ciphertext.
|
|
244
|
+
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
245
|
+
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
246
|
+
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
247
|
+
|
|
221
248
|
const plaintextParts: BlobPart[] = []
|
|
222
249
|
let cursor = parsed.headerLength
|
|
223
250
|
let bytesEmitted = 0
|
|
@@ -254,6 +281,7 @@ export async function decryptBlob(
|
|
|
254
281
|
const plaintext = await decryptChunk(ctx, i, cipherBytes)
|
|
255
282
|
bytesEmitted += plaintext.length
|
|
256
283
|
plaintextParts.push(asBlobPart(plaintext))
|
|
284
|
+
transcript?.absorbChunk(i, cipherBytes)
|
|
257
285
|
|
|
258
286
|
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1))
|
|
259
287
|
}
|
|
@@ -261,9 +289,19 @@ export async function decryptBlob(
|
|
|
261
289
|
if (bytesEmitted !== parsed.plaintextSize) {
|
|
262
290
|
throw new HeaderError('plaintext_size_mismatch')
|
|
263
291
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
292
|
+
|
|
293
|
+
// Trailing bytes are an optional signature block, not corruption; parse
|
|
294
|
+
// and surface it, rejecting only malformed trailing garbage.
|
|
295
|
+
const trailingBytes =
|
|
296
|
+
cursor < blob.size
|
|
297
|
+
? new Uint8Array(await blob.slice(cursor).arrayBuffer())
|
|
298
|
+
: new Uint8Array(0)
|
|
299
|
+
const signature = finalizeSignatureMetadata({
|
|
300
|
+
trailingBytes,
|
|
301
|
+
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
302
|
+
expectedSignerPublicKey,
|
|
303
|
+
})
|
|
304
|
+
options.onSignature?.(signature)
|
|
267
305
|
|
|
268
306
|
if (parsed.totalChunks === 0) onProgress?.(1)
|
|
269
307
|
|
|
@@ -32,6 +32,11 @@ import {
|
|
|
32
32
|
readUint32BE,
|
|
33
33
|
uint32BE,
|
|
34
34
|
} from '../../internal/encoding.js'
|
|
35
|
+
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
36
|
+
import {
|
|
37
|
+
finalizeSignatureMetadata,
|
|
38
|
+
type VerifiedSignature,
|
|
39
|
+
} from '../../format/signature-tail.js'
|
|
35
40
|
|
|
36
41
|
const LENGTH_PREFIX_BYTES = 4
|
|
37
42
|
|
|
@@ -137,6 +142,22 @@ export interface DecryptOptions {
|
|
|
137
142
|
contentKey: Uint8Array
|
|
138
143
|
onProgress?: ProgressCallback
|
|
139
144
|
allowedSuites?: ReadonlyArray<number>
|
|
145
|
+
/**
|
|
146
|
+
* If provided, a trailing sender-attribution signature block (if
|
|
147
|
+
* present) is verified against this Ed25519 public key, and the result
|
|
148
|
+
* is surfaced via `onSignature` with `verified` set accordingly. If
|
|
149
|
+
* omitted, a present block is still parsed and surfaced but not
|
|
150
|
+
* verified (`verified: null`). See `spec/format-v1.md § "Signature
|
|
151
|
+
* block"`.
|
|
152
|
+
*/
|
|
153
|
+
expectedSignerPublicKey?: Uint8Array
|
|
154
|
+
/**
|
|
155
|
+
* Called once, after the plaintext is fully decrypted, with the file's
|
|
156
|
+
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
157
|
+
* Callers that require attribution MUST gate on
|
|
158
|
+
* `signature?.verified === true`.
|
|
159
|
+
*/
|
|
160
|
+
onSignature?: (signature: VerifiedSignature | null) => void
|
|
140
161
|
}
|
|
141
162
|
|
|
142
163
|
export async function decryptBlob(
|
|
@@ -195,6 +216,12 @@ export async function decryptBlob(
|
|
|
195
216
|
parsed.totalChunks,
|
|
196
217
|
)
|
|
197
218
|
|
|
219
|
+
// Rebuild the sender-attribution transcript only when verifying, so an
|
|
220
|
+
// unsigned decrypt pays nothing for the extra hash over ciphertext.
|
|
221
|
+
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
222
|
+
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
223
|
+
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
224
|
+
|
|
198
225
|
const plaintextParts: BlobPart[] = []
|
|
199
226
|
let cursor = parsed.headerLength
|
|
200
227
|
let bytesEmitted = 0
|
|
@@ -231,6 +258,7 @@ export async function decryptBlob(
|
|
|
231
258
|
const plaintext = await decryptChunk(ctx, i, cipherBytes)
|
|
232
259
|
bytesEmitted += plaintext.length
|
|
233
260
|
plaintextParts.push(asBlobPart(plaintext))
|
|
261
|
+
transcript?.absorbChunk(i, cipherBytes)
|
|
234
262
|
|
|
235
263
|
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1))
|
|
236
264
|
}
|
|
@@ -238,9 +266,19 @@ export async function decryptBlob(
|
|
|
238
266
|
if (bytesEmitted !== parsed.plaintextSize) {
|
|
239
267
|
throw new HeaderError('plaintext_size_mismatch')
|
|
240
268
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
269
|
+
|
|
270
|
+
// Trailing bytes are an optional signature block, not corruption; parse
|
|
271
|
+
// and surface it, rejecting only malformed trailing garbage.
|
|
272
|
+
const trailingBytes =
|
|
273
|
+
cursor < blob.size
|
|
274
|
+
? new Uint8Array(await blob.slice(cursor).arrayBuffer())
|
|
275
|
+
: new Uint8Array(0)
|
|
276
|
+
const signature = finalizeSignatureMetadata({
|
|
277
|
+
trailingBytes,
|
|
278
|
+
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
279
|
+
expectedSignerPublicKey,
|
|
280
|
+
})
|
|
281
|
+
options.onSignature?.(signature)
|
|
244
282
|
|
|
245
283
|
if (parsed.totalChunks === 0) onProgress?.(1)
|
|
246
284
|
|
|
@@ -52,6 +52,14 @@ export function parseXChaChaV1SuitePayload(
|
|
|
52
52
|
if (bytes.length !== XCHACHA_V1_SUITE_PAYLOAD_LENGTH) {
|
|
53
53
|
throw new RangeError('xchacha-v1: suite payload must be 96 bytes')
|
|
54
54
|
}
|
|
55
|
+
// The 48-byte secretbox wrap (32-byte key + 16-byte tag) is zero-padded to
|
|
56
|
+
// 72 bytes; the reserved pad (bytes 48..72) MUST be all zero — same
|
|
57
|
+
// invariant suite 0x03 enforces (M6). See parseAesGcmV1SuitePayload.
|
|
58
|
+
for (let i = 48; i < 72; i += 1) {
|
|
59
|
+
if (bytes[i] !== 0) {
|
|
60
|
+
throw new RangeError('xchacha-v1: reserved wrapped_key pad must be zero')
|
|
61
|
+
}
|
|
62
|
+
}
|
|
55
63
|
return {
|
|
56
64
|
wrappedKey: bytes.slice(0, 72),
|
|
57
65
|
wrapNonce: bytes.slice(72, 96),
|