@shieldfive/crypto 1.0.0-rc.2 → 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.
Files changed (62) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/format/signature-tail.d.ts +54 -0
  3. package/dist/format/signature-tail.d.ts.map +1 -0
  4. package/dist/format/signature-tail.js +82 -0
  5. package/dist/format/signature-tail.js.map +1 -0
  6. package/dist/identity/inbound.d.ts +35 -0
  7. package/dist/identity/inbound.d.ts.map +1 -1
  8. package/dist/identity/inbound.js +71 -1
  9. package/dist/identity/inbound.js.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/internal/types.d.ts +1 -0
  13. package/dist/internal/types.d.ts.map +1 -1
  14. package/dist/internal/types.js +3 -0
  15. package/dist/internal/types.js.map +1 -1
  16. package/dist/migration/v0-bridge.d.ts +22 -2
  17. package/dist/migration/v0-bridge.d.ts.map +1 -1
  18. package/dist/migration/v0-bridge.js.map +1 -1
  19. package/dist/streams/aes-gcm-v1.d.ts +3 -37
  20. package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
  21. package/dist/streams/aes-gcm-v1.js +8 -64
  22. package/dist/streams/aes-gcm-v1.js.map +1 -1
  23. package/dist/suites/aes-gcm-v1/api.d.ts +18 -0
  24. package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
  25. package/dist/suites/aes-gcm-v1/api.js +23 -5
  26. package/dist/suites/aes-gcm-v1/api.js.map +1 -1
  27. package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -1
  28. package/dist/suites/aes-gcm-v1/index.js +11 -0
  29. package/dist/suites/aes-gcm-v1/index.js.map +1 -1
  30. package/dist/suites/aes-gcm-v2/api.d.ts +17 -0
  31. package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -1
  32. package/dist/suites/aes-gcm-v2/api.js +19 -3
  33. package/dist/suites/aes-gcm-v2/api.js.map +1 -1
  34. package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -1
  35. package/dist/suites/aes-gcm-v2/index.js +7 -0
  36. package/dist/suites/aes-gcm-v2/index.js.map +1 -1
  37. package/dist/suites/pq-hybrid-v1/api.d.ts +17 -0
  38. package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -1
  39. package/dist/suites/pq-hybrid-v1/api.js +19 -3
  40. package/dist/suites/pq-hybrid-v1/api.js.map +1 -1
  41. package/dist/suites/xchacha-v1/api.d.ts +17 -0
  42. package/dist/suites/xchacha-v1/api.d.ts.map +1 -1
  43. package/dist/suites/xchacha-v1/api.js +19 -3
  44. package/dist/suites/xchacha-v1/api.js.map +1 -1
  45. package/dist/suites/xchacha-v1/index.d.ts.map +1 -1
  46. package/dist/suites/xchacha-v1/index.js +8 -0
  47. package/dist/suites/xchacha-v1/index.js.map +1 -1
  48. package/package.json +1 -1
  49. package/spec/key-derivation.md +19 -5
  50. package/src/format/signature-tail.ts +114 -0
  51. package/src/identity/inbound.ts +118 -1
  52. package/src/index.ts +1 -1
  53. package/src/internal/types.ts +3 -0
  54. package/src/migration/v0-bridge.ts +22 -2
  55. package/src/streams/aes-gcm-v1.ts +17 -91
  56. package/src/suites/aes-gcm-v1/api.ts +46 -5
  57. package/src/suites/aes-gcm-v1/index.ts +11 -0
  58. package/src/suites/aes-gcm-v2/api.ts +41 -3
  59. package/src/suites/aes-gcm-v2/index.ts +7 -0
  60. package/src/suites/pq-hybrid-v1/api.ts +41 -3
  61. package/src/suites/xchacha-v1/api.ts +41 -3
  62. package/src/suites/xchacha-v1/index.ts +8 -0
@@ -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
- // Final consistency: total bytes emitted must equal plaintextSize, and
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
- if (cursor !== blob.size) {
282
- throw new HeaderError('trailing_bytes_after_last_chunk')
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
- if (cursor !== blob.size) {
258
- throw new HeaderError('trailing_bytes_after_last_chunk')
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
- if (cursor !== blob.size) {
265
- throw new HeaderError('trailing_bytes_after_last_chunk')
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
- if (cursor !== blob.size) {
242
- throw new HeaderError('trailing_bytes_after_last_chunk')
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),