@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.
Files changed (65) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/README.md +32 -15
  3. package/SECURITY.md +1 -1
  4. package/dist/identity/index.d.ts +16 -0
  5. package/dist/identity/index.d.ts.map +1 -1
  6. package/dist/identity/index.js +84 -22
  7. package/dist/identity/index.js.map +1 -1
  8. package/dist/identity/sign.d.ts +92 -0
  9. package/dist/identity/sign.d.ts.map +1 -0
  10. package/dist/identity/sign.js +182 -0
  11. package/dist/identity/sign.js.map +1 -0
  12. package/dist/index.d.ts +10 -4
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +26 -4
  15. package/dist/index.js.map +1 -1
  16. package/dist/internal/encoding.d.ts.map +1 -1
  17. package/dist/internal/encoding.js +0 -1
  18. package/dist/internal/encoding.js.map +1 -1
  19. package/dist/internal/types.d.ts +4 -1
  20. package/dist/internal/types.d.ts.map +1 -1
  21. package/dist/internal/types.js +8 -0
  22. package/dist/internal/types.js.map +1 -1
  23. package/dist/streams/aes-gcm-v1.d.ts +71 -3
  24. package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
  25. package/dist/streams/aes-gcm-v1.js +183 -49
  26. package/dist/streams/aes-gcm-v1.js.map +1 -1
  27. package/dist/streams/pq-hybrid-v1.d.ts +34 -3
  28. package/dist/streams/pq-hybrid-v1.d.ts.map +1 -1
  29. package/dist/streams/pq-hybrid-v1.js +114 -49
  30. package/dist/streams/pq-hybrid-v1.js.map +1 -1
  31. package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
  32. package/dist/suites/aes-gcm-v1/api.js +1 -1
  33. package/dist/suites/aes-gcm-v1/api.js.map +1 -1
  34. package/dist/suites/aes-gcm-v1/index.d.ts +3 -0
  35. package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -1
  36. package/dist/suites/aes-gcm-v1/index.js +3 -0
  37. package/dist/suites/aes-gcm-v1/index.js.map +1 -1
  38. package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
  39. package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
  40. package/dist/suites/aes-gcm-v2/api.js +162 -0
  41. package/dist/suites/aes-gcm-v2/api.js.map +1 -0
  42. package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
  43. package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
  44. package/dist/suites/aes-gcm-v2/index.js +193 -0
  45. package/dist/suites/aes-gcm-v2/index.js.map +1 -0
  46. package/dist/suites/pq-hybrid-v1/index.d.ts +4 -0
  47. package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -1
  48. package/dist/suites/pq-hybrid-v1/index.js +15 -1
  49. package/dist/suites/pq-hybrid-v1/index.js.map +1 -1
  50. package/package.json +12 -2
  51. package/spec/format-v1.md +200 -7
  52. package/spec/key-derivation.md +18 -2
  53. package/spec/threat-model.md +36 -21
  54. package/src/identity/index.ts +107 -28
  55. package/src/identity/sign.ts +221 -0
  56. package/src/index.ts +32 -5
  57. package/src/internal/encoding.ts +0 -1
  58. package/src/internal/types.ts +9 -0
  59. package/src/streams/aes-gcm-v1.ts +263 -59
  60. package/src/streams/pq-hybrid-v1.ts +169 -59
  61. package/src/suites/aes-gcm-v1/api.ts +0 -1
  62. package/src/suites/aes-gcm-v1/index.ts +3 -0
  63. package/src/suites/aes-gcm-v2/api.ts +268 -0
  64. package/src/suites/aes-gcm-v2/index.ts +289 -0
  65. package/src/suites/pq-hybrid-v1/index.ts +25 -4
package/spec/format-v1.md CHANGED
@@ -29,6 +29,7 @@ All multi-byte integers are big-endian. All byte counts are in octets.
29
29
 
30
30
  ```
31
31
  encrypted_file := header || chunk_0 || chunk_1 || ... || chunk_n
32
+ || signature_block? (optional, see § "Signature block")
32
33
  ```
33
34
 
34
35
  ### Header
@@ -59,6 +60,7 @@ future minor-version bumps that remain backward-compatible.
59
60
  | `0x01` | `aes-256-gcm-v1` | required |
60
61
  | `0x02` | `xchacha20-poly1305-v1` | required |
61
62
  | `0x03` | `pq-hybrid-xchacha-mlkem1024-v1` | default |
63
+ | `0x04` | `aes-256-gcm-v2` | required |
62
64
  | `0x80` | (reserved, custom-suite range begins) | reserved |
63
65
 
64
66
  `flags` is reserved; readers MUST reject any header with a non-zero flags
@@ -92,8 +94,9 @@ header_mac_key := HKDF-SHA-256(
92
94
  ```
93
95
 
94
96
  For suites whose header MAC key is not derived from `suite_payload`
95
- (currently `0x01` aes-gcm-v1, `0x02` xchacha-v1), readers MUST verify
96
- `header_mac` before parsing `suite_payload` or any chunk. KEM suites
97
+ (currently `0x01` aes-gcm-v1, `0x02` xchacha-v1, `0x04` aes-gcm-v2),
98
+ readers MUST verify `header_mac` before parsing `suite_payload` or any
99
+ chunk. KEM suites
97
100
  (currently `0x03` pq-hybrid-v1) verify `header_mac` after decapsulation
98
101
  per their suite-specific decryption flow; see § "`0x03` — pq-hybrid"
99
102
  below for the required ordering and security argument. A header_mac
@@ -211,10 +214,18 @@ wrapped key from the out-of-band channel. The field length is fixed at
211
214
 
212
215
  ```
213
216
  suite_payload := mlkem_ciphertext (1568 bytes) = ML-KEM-1024 ciphertext
214
- || classical_wrapped (72 bytes) = secretbox-wrapped classical share
217
+ || classical_wrapped (72 bytes) = 48-byte secretbox-wrapped
218
+ classical share || 24-byte
219
+ reserved pad
215
220
  || classical_nonce (24 bytes) = secretbox nonce for classical wrap
216
221
  ```
217
222
 
223
+ `classical_wrapped` is a fixed 72 bytes: the first 48 are the
224
+ XSalsa20-Poly1305 secretbox of the 32-byte classical share, and the
225
+ trailing 24 bytes are a reserved pad that MUST be all-zero. Readers MUST
226
+ reject a suite payload whose reserved pad is non-zero, so the field
227
+ cannot be used as a malleable, unauthenticated side channel.
228
+
218
229
  The recipient holds:
219
230
 
220
231
  - A classical key `K_c` (32 bytes), wrapped at the envelope layer like the
@@ -230,10 +241,25 @@ Encryption:
230
241
  3. Combined content key `K = HKDF-SHA-256(ikm = S_c || S_pq, salt = file_id,
231
242
  info = "shieldfive/v1/pq-hybrid/combine", L = 32)`.
232
243
  4. Sender wraps `S_c` with `K_c` using XSalsa20-Poly1305 secretbox to
233
- produce `classical_wrapped`. (The PQ share is recovered from
234
- `mlkem_ciphertext` via decapsulation; only the classical share needs
235
- wrapping.)
236
- 5. Subsequent chunks use the XChaCha20-Poly1305 chunk format with `K`.
244
+ produce the 48-byte secretbox, then left-pads it into the 72-byte
245
+ `classical_wrapped` field with a 24-byte all-zero reserved pad. (The PQ
246
+ share is recovered from `mlkem_ciphertext` via decapsulation; only the
247
+ classical share needs wrapping.)
248
+ 5. Subsequent chunks use the XChaCha20-Poly1305 chunk format with `K`. The
249
+ chunk-key and nonce-prefix derivations are identical to the `0x02`
250
+ XChaCha suite's, with `K` in the role of the content key. See
251
+ `spec/key-derivation.md` for why reusing those labels is safe here.
252
+
253
+ > **Planned hardening — suite-id binding (audit M5, future suite version).**
254
+ > The `0x03` chunk-key / nonce-prefix derivations currently reuse the `0x02`
255
+ > XChaCha HKDF labels and do not fold the suite id into the IKM. This is
256
+ > **not exploitable**: the header MAC authenticates the suite byte, so a
257
+ > `0x02` and a `0x03` file are never interchangeable in practice. The
258
+ > `0x03` derivation is intentionally left **unchanged** here — production
259
+ > `cipher_version-3` files already exist and depend on it, and altering it
260
+ > would render them undecryptable. A future suite version (a new suite id,
261
+ > not `0x03`) will additionally bind the suite id into the chunk-key
262
+ > derivation; this document will be updated when that suite is defined.
237
263
 
238
264
  Decryption:
239
265
 
@@ -298,6 +324,173 @@ decapsulation) and therefore cannot be stored out-of-band. The full
298
324
  1664-byte `suite_payload` MUST be present inline in every PQ-hybrid
299
325
  file.
300
326
 
327
+ #### Share bundle (re-encrypting `K` to another recipient)
328
+
329
+ Sharing a `0x03` file with an additional recipient does not re-encrypt the
330
+ chunks. The owner recovers the file's combined key `K` and produces a
331
+ per-recipient *share bundle* that re-wraps `K` to the recipient's ML-KEM
332
+ public key + envelope key. The share bundle is an application-layer record
333
+ (it is not part of the on-disk file), but its wire format is specified here
334
+ because it carries `0x03` key material. It carries its own magic and version
335
+ byte (`magic`, below) — distinct from the `SF5` file magic — so the hardened
336
+ format is unambiguously distinguishable from the earlier unversioned one.
337
+
338
+ ```
339
+ share_bundle := magic (5 bytes) = "SF5S" (0x53 46 35 53) || version (0x02)
340
+ || pq_len (4 bytes) = uint32 BE, length of pq_payload (= 1664)
341
+ || pq_payload (1664 bytes) = a 0x03 suite_payload encapsulated to the recipient
342
+ || wrap_nonce (24 bytes) = XChaCha20-Poly1305 nonce
343
+ || wrapped_key (48 bytes) = AEAD(K) = 32-byte K + 16-byte tag
344
+ ```
345
+
346
+ Construction:
347
+
348
+ 1. The owner encapsulates to the recipient (ML-KEM + classical wrap under
349
+ the recipient's envelope key), yielding `pq_payload` and a transport
350
+ secret `T` (the `0x03` combined key for the recipient, derived under
351
+ `info="shieldfive/v1/pq-hybrid/combine"`).
352
+ 2. The wrapping key is domain-separated from `T` — it is NOT `T` directly,
353
+ so a share key can never equal a file's combined key:
354
+
355
+ ```
356
+ share_transport_key = HKDF(ikm=T, salt=file_id,
357
+ info="shieldfive/v1/share-transport", L=32)
358
+ ```
359
+ 3. `K` is wrapped with XChaCha20-Poly1305 under `share_transport_key`, with
360
+ the AAD authenticating the WHOLE bundle prefix — including the magic and
361
+ version — so neither the version marker nor any PQ material can be
362
+ substituted, stripped, or downgraded:
363
+
364
+ ```
365
+ aad = magic || uint32_be(pq_len) || pq_payload || wrap_nonce
366
+ wrapped_key = XChaCha20-Poly1305-Encrypt(key=share_transport_key,
367
+ nonce=wrap_nonce, aad=aad, plaintext=K)
368
+ ```
369
+
370
+ The recipient reverses this: verify `magic` (rejecting any bundle without
371
+ the `"SF5S"` + version-2 prefix), parse the bundle (rejecting a non-zero
372
+ reserved pad in `pq_payload`), decapsulate to recover `T`, derive
373
+ `share_transport_key`, reconstruct `aad`, and AEAD-open `wrapped_key`. Any
374
+ modification to `magic`, `pq_len`, `pq_payload`, or `wrap_nonce` breaks the
375
+ tag.
376
+
377
+ > **Wire-format change (alpha):** earlier alpha builds emitted an
378
+ > *unversioned* bundle (no magic): the wrapping key was derived directly
379
+ > from `T` (reusing the file-combiner label) and `K` was wrapped with an
380
+ > XSalsa20-Poly1305 secretbox that authenticated only `K` (no AAD over
381
+ > `pq_payload`). Such bundles are rejected by the magic check and must be
382
+ > re-issued. No real shares exist pre-launch, so this is a clean break.
383
+
384
+ ### `0x04` — `aes-256-gcm-v2`
385
+
386
+ ```
387
+ suite_payload := wrapped_key (60 bytes) = AES-GCM-wrapped 32-byte content key
388
+ || wrap_iv (12 bytes) = AES-GCM IV used for wrapping
389
+ ```
390
+
391
+ `0x04` is a WebCrypto-only AEAD suite (no WASM dependency) that differs
392
+ from `0x01` in exactly one respect: the split of the 12-byte AES-GCM IV.
393
+ It is the suite new AES-GCM writes use; `0x01` remains defined so that
394
+ files written before `0x04` existed stay readable.
395
+
396
+ - `chunk_key(content_key, file_id)` =
397
+ `HKDF-SHA-256(ikm=content_key, salt=file_id, info="shieldfive/v1/aes-gcm/chunk-key", L=32)`
398
+
399
+ This is bit-for-bit identical to the `0x01` chunk-key derivation — same
400
+ HKDF `info` string (`shieldfive/v1/aes-gcm/chunk-key`), same inputs. A
401
+ `0x01` file and a `0x04` file sharing a `content_key` and `file_id`
402
+ derive the same chunk key. They never collide on AEAD inputs, because
403
+ the nonce-prefix derivation below uses a distinct `info` string, giving
404
+ the two suites disjoint `(prefix, counter)` spaces.
405
+
406
+ - `chunk_nonce(file_id, i)` =
407
+ `HKDF-SHA-256(ikm=file_id, salt=zeros(32), info="shieldfive/v1/aes-gcm-v2/nonce-prefix", L=8)`
408
+ `|| uint32_be(i)`
409
+
410
+ The 12-byte GCM IV is an 8-byte file-derived prefix followed by a 4-byte
411
+ big-endian chunk counter — the inverse of `0x01`'s split, which is a
412
+ 4-byte prefix and an 8-byte counter. Widening the prefix shrinks the
413
+ cross-file `(prefix, counter)` collision space (for two files that reuse
414
+ a `content_key` — which never happens under correct use, but defense in
415
+ depth) from 2^32 to 2^64. The trade is a per-file chunk ceiling of 2^32
416
+ chunks rather than 2^64; this is far above the format's
417
+ `MAX_TOTAL_CHUNKS` bound (1e9), so it is not a practical constraint.
418
+ Readers MUST reject a `0x04` file whose `total_chunks` exceeds 2^32. The
419
+ salt is the RFC 5869 absent-salt zero-fill (32 zero bytes), identical to
420
+ the `0x01` and `0x02` suites.
421
+
422
+ The wrapped-content-key field and the all-zero `suite_payload` convention
423
+ for out-of-band key storage are identical to `0x01`: the field is a fixed
424
+ 72 bytes (60-byte `wrapped_key` + 12-byte `wrap_iv`), zero-filled when the
425
+ wrapped key is stored out-of-band (e.g. in the ShieldFive vault database).
426
+ Readers MUST accept all-zero `suite_payload` bytes in that case and obtain
427
+ the wrapped key from the out-of-band channel.
428
+
429
+ ## Signature block
430
+
431
+ The signature block is an OPTIONAL trailing structure that carries a
432
+ detached sender signature over `header_unauthenticated_bytes ||
433
+ concat(per-chunk MAC tags)`. It provides *sender attribution*: a
434
+ recipient who trusts a sender's public key can verify the file was
435
+ produced by that sender. It does NOT add confidentiality or chunk
436
+ integrity — those are provided by the per-suite AEAD and by
437
+ `header_mac` regardless of whether a signature block is present.
438
+
439
+ ```
440
+ signature_block := algorithm (1 byte) = signature algorithm identifier
441
+ || pubkey_len (2 bytes) = uint16 BE, length of public_key
442
+ || public_key (variable) = sender's verifying key
443
+ || signature_len (2 bytes) = uint16 BE, length of signature
444
+ || signature (variable) = detached signature bytes
445
+ ```
446
+
447
+ `algorithm` is one of:
448
+
449
+ | Value | Algorithm | Public key | Signature | Status |
450
+ | ------ | ------------- | ---------- | --------- | -------- |
451
+ | `0x00` | (reserved) | — | — | invalid |
452
+ | `0x01` | Ed25519 | 32 bytes | 64 bytes | defined |
453
+ | `0x02` | ML-DSA-65 | 1952 bytes | 3309 bytes | reserved (not yet implemented) |
454
+ | `0x80` | (reserved, custom-algorithm range begins) | — | — | reserved |
455
+
456
+ The message signed for `0x01 = Ed25519` is the byte string
457
+
458
+ ```
459
+ signed_message := header_unauthenticated_bytes
460
+ || concat(chunk_0_mac, chunk_1_mac, ..., chunk_{n-1}_mac)
461
+ ```
462
+
463
+ where `header_unauthenticated_bytes` is exactly the bytes covered by
464
+ `header_mac` (i.e. everything in the header before the `header_mac`
465
+ field itself), and each `chunk_i_mac` is the suite's AEAD authenticator
466
+ tag for chunk `i`. For all suites defined in this version the AEAD tag
467
+ is the trailing 16 bytes of the chunk's ciphertext field (AES-GCM-128
468
+ and Poly1305 both append a 16-byte tag).
469
+
470
+ The signature block is OPTIONAL. Implementations:
471
+
472
+ - **Writers MAY** append a signature block. Writers MUST NOT emit more
473
+ than one. The block, if present, MUST appear immediately after the
474
+ final chunk's ciphertext field and MUST NOT be followed by any further
475
+ bytes.
476
+ - **Readers MUST** treat a file with no bytes after the last chunk as
477
+ unsigned (signature absent). Readers MUST treat any trailing bytes as
478
+ a signature block and MUST reject the file if those bytes do not parse
479
+ as a well-formed block or are followed by trailing garbage.
480
+ - **Readers SHOULD** expose the parsed block (algorithm, public key,
481
+ signature) to callers so an application-level identity policy can
482
+ decide whether the file is acceptable.
483
+ - **Readers MAY** verify the signature against a caller-supplied public
484
+ key. A signature block whose `algorithm` is unknown to the reader is
485
+ treated as unverifiable; the reader returns the block to the caller
486
+ without raising and lets the application policy decide.
487
+
488
+ A signature block does not weaken the format's existing security
489
+ guarantees: an attacker who tampers with any byte covered by `header_mac`
490
+ fails MAC verification under the content key, and an attacker who
491
+ tampers with any chunk ciphertext fails per-chunk AEAD verification. The
492
+ signature is a separate, application-level trust layer.
493
+
301
494
  ## Versioning policy
302
495
 
303
496
  - **Format major version (4th byte of magic)** changes when the parser
@@ -91,9 +91,11 @@ chunk_key = HKDF(content_key, salt=file_id,
91
91
 
92
92
  nonce_prefix = HKDF(file_id,
93
93
  info="shieldfive/v1/<suite>/nonce-prefix",
94
- L = 4 (AES-GCM) or 16 (XChaCha))
94
+ L = 4 (aes-gcm-v1), 8 (aes-gcm-v2), or 16 (xchacha))
95
95
 
96
- iv / nonce_i = nonce_prefix || uint64_be(chunk_index)
96
+ iv / nonce_i = nonce_prefix || counter
97
+ (counter = uint32_be(chunk_index) for aes-gcm-v2's
98
+ 8-byte prefix; uint64_be(chunk_index) otherwise)
97
99
  ```
98
100
 
99
101
  For the PQ-hybrid suite, `content_key` is replaced by the combined key
@@ -109,6 +111,18 @@ file-bound HKDF output `K`. Both are 32 bytes of uniformly random or
109
111
  pseudorandom material per file; cross-suite output collision
110
112
  probability is `2^-256`.
111
113
 
114
+ > **Planned hardening — suite-id binding (audit M5, future suite version).**
115
+ > The reuse above is safe but implicit: it relies on the `content_key` /`K`
116
+ > input spaces being disjoint rather than binding the suite id directly into
117
+ > the derivation. This is **not exploitable** in the current format — the
118
+ > header MAC authenticates the suite byte, so a `0x02` and a `0x03` file are
119
+ > never interchangeable. The `0x03` derivation is therefore left
120
+ > **unchanged**: production `cipher_version-3` files already depend on it,
121
+ > and changing the labels would make them undecryptable. A future suite
122
+ > version (a new suite id, not `0x03`) will fold the suite id into the
123
+ > chunk-key / nonce-prefix IKM and use dedicated `pq-hybrid` labels; this
124
+ > section will be updated when that suite is defined.
125
+
112
126
  ## Forbidden cross-context usage
113
127
 
114
128
  These domain strings are consumed by the crypto layer. The host
@@ -118,10 +132,12 @@ application MUST NOT reuse any of them for unrelated purposes:
118
132
  shieldfive/v1/header-mac
119
133
  shieldfive/v1/aes-gcm/chunk-key
120
134
  shieldfive/v1/aes-gcm/nonce-prefix
135
+ shieldfive/v1/aes-gcm-v2/nonce-prefix
121
136
  shieldfive/v1/xchacha/chunk-key
122
137
  shieldfive/v1/xchacha/nonce-prefix
123
138
  shieldfive/v1/pq-hybrid/combine
124
139
  shieldfive/v1/pq-hybrid/ml-kem-1024-seed
140
+ shieldfive/v1/share-transport
125
141
  shieldfive/v1/argon2id/salt-compression
126
142
  ```
127
143
 
@@ -63,9 +63,10 @@ v0 files have no chunk AAD, no `file_id` binding, and no truncation
63
63
  detection. Defense-in-depth for v0 files relies on the application's
64
64
  stored per-chunk SHA-1 hashes (`cipher_parts_sha1`), verified on
65
65
  download. Migration to v1 is required to gain the AEAD-bound integrity
66
- properties documented above. As of 2026-05-11, the v1 writer is
67
- enabled but most live data is still v0; migration policy and timeline
68
- are tracked separately.
66
+ properties documented above. As of 2026-05-17, the v1 PQ-hybrid
67
+ writer (Suite 0x03) is the production default for new uploads;
68
+ existing v0 files remain readable indefinitely and in-place migration
69
+ is tracked separately.
69
70
 
70
71
  ### A3 — Future quantum adversary
71
72
 
@@ -88,13 +89,10 @@ hybrid suite when migration is feasible.
88
89
 
89
90
  #### Current deployment status
90
91
 
91
- As of 2026-05-11, the production web client emits suite `0x01`
92
- (`aes-256-gcm-v1`) for new uploads. Suite `0x03`
93
- (`pq-hybrid-xchacha-mlkem1024-v1`) is implemented in the
94
- `@shieldfive/crypto` library but is not yet wired into the web worker;
95
- until Phase 3 ships, all newly-uploaded files are classical-only and
96
- are NOT protected against A3. This document will be updated when
97
- `0x03` becomes the default writer suite.
92
+ As of 2026-05-17, the production web client emits Suite `0x03`
93
+ (`pq-hybrid-xchacha-mlkem1024-v1`) for new uploads; files written
94
+ under the previous default (v0, Suite `0x01`) remain readable
95
+ indefinitely.
98
96
 
99
97
  ## Trust principals
100
98
 
@@ -111,14 +109,30 @@ by:
111
109
  expiry timestamp.
112
110
  - **No key reach.** The share endpoint never returns `rk`, parent
113
111
  folder keys, or other CSKs.
114
- - **Brute force.** Bcrypt-wrapped `share_password_hash` is rate
115
- limited per-share, per-IP, and per-burst; lockout state is tracked
116
- in `share_password_attempts` (Task 7).
112
+ - **Brute force.** Verification is rate limited per-share, per-IP, and
113
+ per-burst; lockout state is tracked in `share_password_attempts`
114
+ (Task 7). The brute-force defense wraps the verifier comparison the
115
+ same way it previously wrapped the bcrypt comparison.
117
116
 
118
117
  The recipient is a trusted third party for the single file shared; the
119
118
  system makes no confidentiality guarantee against that recipient.
120
- Confidentiality against A1 is unaffected (the server doesn't have the
121
- share password in cleartext; it has only the bcrypt hash).
119
+
120
+ New shares use a **client-derived blind verifier**: the client derives
121
+ `verifier = Argon2id(share password, share_verifier_salt)` and the
122
+ creator stores only `SHA-256(verifier)` (`share_verifier_hash`). At
123
+ download the recipient re-derives the verifier locally and sends it; the
124
+ server `SHA-256`s the received verifier and constant-time compares it to
125
+ `share_verifier_hash`. The share password is never transmitted to the
126
+ server, so the server never holds the cleartext password alongside
127
+ `csk_pw_wrapped` and cannot decrypt the shared file. The verifier salt
128
+ is independent of `csk_pw_salt`, and the key-wrapping path (`csk_pw_*`)
129
+ is unchanged. Confidentiality against A1 is therefore unaffected (the
130
+ server has only `share_verifier_hash`, never the password).
131
+
132
+ The legacy bcrypt path (`share_password_hash`) is retained only for
133
+ shares created before the blind verifier; those shares still send the
134
+ cleartext password for the server-side bcrypt compare. New shares are
135
+ always verifier mode.
122
136
 
123
137
  ## Out of scope
124
138
 
@@ -199,9 +213,10 @@ a security claim about which product is "best" — each makes different
199
213
  tradeoffs — but it documents the design positions ShieldFive v1 takes
200
214
  deliberately.
201
215
 
202
- † Suite `0x03` is available in the crypto library (1.0.0-alpha.3) and
203
- is the format-v1 spec default; the production web client has not yet
204
- been updated to emit it. See § "Current deployment status" under A3.
216
+ † As of 2026-05-17, the production web client emits Suite `0x03`
217
+ (`pq-hybrid-xchacha-mlkem1024-v1`) for new uploads; files written
218
+ under the previous default (v0, Suite `0x01`) remain readable
219
+ indefinitely. See § "Current deployment status" under A3.
205
220
 
206
221
  ## Known limitations of v1
207
222
 
@@ -236,7 +251,7 @@ These are intentional tradeoffs documented for transparency:
236
251
  ## Reporting
237
252
 
238
253
  Cryptographic vulnerabilities should be reported to
239
- `security@shieldfive.com` encrypted with the PGP key in `SECURITY.md`. We
240
- acknowledge within 72 hours and target a patch within 30 days for
241
- high-severity issues. Researchers acting in good faith are protected under
254
+ `security@shieldfive.com`; see `SECURITY.md` for the encrypted-channel
255
+ process. We acknowledge within 72 hours and target a patch within 30 days
256
+ for high-severity issues. Researchers acting in good faith are protected under
242
257
  the safe-harbor clause in `SECURITY.md`.
@@ -26,8 +26,21 @@ import {
26
26
  encapsulateForRecipient,
27
27
  decapsulateFromHeader,
28
28
  parsePqHybridV1SuitePayload,
29
- buildPqHybridV1SuitePayload,
30
29
  } from '../suites/pq-hybrid-v1/index.js'
30
+ export {
31
+ SIGNATURE_ALGO_ED25519,
32
+ SIGNATURE_ALGO_ML_DSA_65,
33
+ ED25519_PUBLIC_KEY_LENGTH,
34
+ ED25519_SECRET_KEY_LENGTH,
35
+ ED25519_SIGNATURE_LENGTH,
36
+ SignatureError,
37
+ buildSignatureBlock,
38
+ deriveEd25519PublicKey,
39
+ parseSignatureBlock,
40
+ signHeaderAndMacs,
41
+ verifyHeaderAndMacs,
42
+ } from './sign.js'
43
+ export type { SignatureBlock } from './sign.js'
31
44
  import {
32
45
  bytesToBase64,
33
46
  base64ToBytes,
@@ -39,9 +52,18 @@ import {
39
52
  } from '../internal/encoding.js'
40
53
  import { randomBytes } from '../internal/runtime.js'
41
54
  import { getSodium } from '../internal/sodium.js'
55
+ import { hkdfSha256 } from '../internal/hkdf.js'
56
+ import { HKDF_INFO } from '../internal/types.js'
42
57
 
43
58
  const IDENTITY_BUNDLE_MAGIC = new Uint8Array([0x53, 0x46, 0x35, 0x49, 0x01]) // "SF5I" + version
44
- const IDENTITY_BUNDLE_VERSION = 0x01
59
+
60
+ // Share-bundle magic: "SF5S" + version byte. Version 0x02 marks the hardened
61
+ // share construction (audit H2/M6): a domain-separated transport key and an
62
+ // AEAD wrap whose AAD authenticates the whole bundle. Earlier alpha builds
63
+ // emitted an unversioned bundle (an AAD-less secretbox under the raw transport
64
+ // secret); those are NOT readable here and are rejected by the magic check.
65
+ // No real shares exist pre-launch, so this is a deliberate clean break.
66
+ const SHARE_BUNDLE_MAGIC = new Uint8Array([0x53, 0x46, 0x35, 0x53, 0x02]) // "SF5S" + version 2
45
67
 
46
68
  export interface PublicIdentityBundle {
47
69
  /** ML-KEM-1024 public key (1568 bytes) */
@@ -255,40 +277,73 @@ export interface ShareFileInput {
255
277
  * key. To make sharing work, we use a different construction: we wrap
256
278
  * the existing combined key under (recipient PQ KEM ⊕ recipient
257
279
  * envelope) and emit only the wrapped form.
280
+ *
281
+ * Two properties this construction must hold (audit H2/M6):
282
+ * 1. The wrapping key is NOT the raw KEM/envelope combined key (which
283
+ * reuses the file-combiner HKDF label). We run it through one more
284
+ * HKDF under the dedicated `SHARE_TRANSPORT` label so a share key is
285
+ * never equal to a file's combined key.
286
+ * 2. The wrap authenticates the WHOLE bundle, not just the 32-byte
287
+ * combined key: the XChaCha20-Poly1305 AAD covers the version marker,
288
+ * the length prefix, the entire pqPayload (including its reserved pad)
289
+ * and the nonce, so PQ material cannot be substituted or stripped
290
+ * without breaking the tag.
291
+ *
292
+ * The bundle is prefixed with `SHARE_BUNDLE_MAGIC` ("SF5S" + version) so the
293
+ * hardened format is distinguishable from the earlier unversioned one.
258
294
  */
259
295
  export async function buildShareForRecipient(
260
296
  input: ShareFileInput,
261
297
  ): Promise<{ shareBundle: Uint8Array }> {
262
298
  const sodium = await getSodium()
263
299
 
264
- // Generate a fresh transport key for this share.
300
+ // Generate a fresh transport secret for this share.
265
301
  // ML-KEM encapsulation gives us the PQ-half of the transport secret.
266
- const { suitePayload: pqPayload, combinedKey: transportKey } =
302
+ const { suitePayload: pqPayload, combinedKey: transportSecret } =
267
303
  await encapsulateForRecipient({
268
304
  recipientPublicKey: input.recipient.mlKemPublicKey,
269
305
  envelopeKey: input.recipientEnvelopeKey,
270
306
  fileId: input.fileId,
271
307
  })
272
308
 
273
- // Now wrap the original combined key under the transport key using
274
- // XSalsa20-Poly1305 secretbox.
275
- if (transportKey.length !== 32) {
309
+ if (transportSecret.length !== 32) {
276
310
  throw new Error('buildShareForRecipient: transport key length wrong')
277
311
  }
312
+ // (1) Domain-separate the share transport key from the file-combiner output.
313
+ const shareTransportKey = await hkdfSha256({
314
+ ikm: transportSecret,
315
+ salt: input.fileId,
316
+ info: HKDF_INFO.SHARE_TRANSPORT,
317
+ length: 32,
318
+ })
319
+
320
+ // (2) Wrap the original combined key with an AEAD whose AAD authenticates
321
+ // the whole bundle prefix (magic || length || pqPayload || nonce). Binding
322
+ // the magic means the version marker cannot be stripped or downgraded
323
+ // without breaking the tag.
278
324
  const wrapNonce = randomBytes(24)
279
- const wrappedCombinedKey = sodium.crypto_secretbox_easy(
325
+ const aad = concatBytes([
326
+ SHARE_BUNDLE_MAGIC,
327
+ uint32BE(pqPayload.length),
328
+ pqPayload,
329
+ wrapNonce,
330
+ ])
331
+ const wrappedCombinedKey = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
280
332
  input.combinedKey,
333
+ aad,
334
+ null,
281
335
  wrapNonce,
282
- transportKey,
336
+ shareTransportKey,
283
337
  )
284
338
  if (wrappedCombinedKey.length !== 32 + 16) {
285
- throw new Error('buildShareForRecipient: secretbox produced unexpected length')
339
+ throw new Error('buildShareForRecipient: AEAD produced unexpected length')
286
340
  }
287
341
 
288
342
  // Share record format:
289
- // [4 bytes BE length of pqPayload][pqPayload][24-byte wrap nonce][48-byte wrapped combined key]
343
+ // [5-byte magic][4 bytes BE length of pqPayload][pqPayload][24-byte wrap nonce][48-byte wrapped combined key]
290
344
  return {
291
345
  shareBundle: concatBytes([
346
+ SHARE_BUNDLE_MAGIC,
292
347
  uint32BE(pqPayload.length),
293
348
  pqPayload,
294
349
  wrapNonce,
@@ -309,10 +364,20 @@ export async function openShareAsRecipient(input: {
309
364
  recipientEnvelopeKey: Uint8Array
310
365
  }): Promise<Uint8Array> {
311
366
  const sodium = await getSodium()
312
- if (input.shareBundle.length < 4) {
367
+ if (input.shareBundle.length < SHARE_BUNDLE_MAGIC.length + 4) {
313
368
  throw new Error('openShareAsRecipient: share too short')
314
369
  }
315
- let offset = 0
370
+ // Reject anything that isn't a current ("SF5S" + version) share bundle.
371
+ // Earlier-alpha unversioned bundles fail here rather than producing an
372
+ // opaque authentication error later.
373
+ for (let i = 0; i < SHARE_BUNDLE_MAGIC.length; i += 1) {
374
+ if (input.shareBundle[i] !== SHARE_BUNDLE_MAGIC[i]) {
375
+ throw new Error(
376
+ 'openShareAsRecipient: bad magic / unsupported share bundle version',
377
+ )
378
+ }
379
+ }
380
+ let offset = SHARE_BUNDLE_MAGIC.length
316
381
  const pqLen = readUint32BE(input.shareBundle, offset)
317
382
  offset += 4
318
383
  if (offset + pqLen + 24 + 48 !== input.shareBundle.length) {
@@ -324,31 +389,45 @@ export async function openShareAsRecipient(input: {
324
389
  offset += 24
325
390
  const wrappedCombinedKey = input.shareBundle.slice(offset, offset + 48)
326
391
 
327
- // Sanity-check the suite payload structure (this throws if malformed).
392
+ // Structurally validate the suite payload. This rejects a non-zero reserved
393
+ // pad in classical_wrapped (M6) before any decapsulation work.
328
394
  parsePqHybridV1SuitePayload(pqPayload)
329
- // Round-trip through builder to ensure we got back the same bytes.
330
- const checkBytes = buildPqHybridV1SuitePayload(
331
- parsePqHybridV1SuitePayload(pqPayload),
332
- )
333
- if (checkBytes.length !== pqPayload.length) {
334
- throw new Error('openShareAsRecipient: pq payload self-check failed')
335
- }
336
395
 
337
- const { combinedKey: transportKey } = await decapsulateFromHeader({
396
+ const { combinedKey: transportSecret } = await decapsulateFromHeader({
338
397
  suitePayload: pqPayload,
339
398
  recipientSecretKey: input.recipientSecretKey,
340
399
  envelopeKey: input.recipientEnvelopeKey,
341
400
  fileId: input.fileId,
342
401
  })
402
+ const shareTransportKey = await hkdfSha256({
403
+ ikm: transportSecret,
404
+ salt: input.fileId,
405
+ info: HKDF_INFO.SHARE_TRANSPORT,
406
+ length: 32,
407
+ })
343
408
 
344
- const unwrapped = sodium.crypto_secretbox_open_easy(
345
- wrappedCombinedKey,
409
+ // The AAD must reproduce exactly what the builder authenticated: it binds
410
+ // the version marker, the length prefix, the whole pqPayload (incl. reserved
411
+ // pad) and the nonce, so any substitution/stripping of PQ material breaks
412
+ // the tag.
413
+ const aad = concatBytes([
414
+ SHARE_BUNDLE_MAGIC,
415
+ uint32BE(pqPayload.length),
416
+ pqPayload,
346
417
  wrapNonce,
347
- transportKey,
348
- )
349
- if (!unwrapped) {
418
+ ])
419
+ let unwrapped: Uint8Array
420
+ try {
421
+ unwrapped = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
422
+ null,
423
+ wrappedCombinedKey,
424
+ aad,
425
+ wrapNonce,
426
+ shareTransportKey,
427
+ )
428
+ } catch {
350
429
  throw new Error(
351
- 'openShareAsRecipient: secretbox unwrap failed — wrong recipient or tampered share',
430
+ 'openShareAsRecipient: share authentication failed — wrong recipient or tampered share',
352
431
  )
353
432
  }
354
433
  if (unwrapped.length !== 32) {