@shieldfive/crypto 1.0.0-alpha.11

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 (124) hide show
  1. package/CHANGELOG.md +228 -0
  2. package/LICENSE +201 -0
  3. package/README.md +275 -0
  4. package/SECURITY.md +130 -0
  5. package/dist/format/header.d.ts +73 -0
  6. package/dist/format/header.d.ts.map +1 -0
  7. package/dist/format/header.js +230 -0
  8. package/dist/format/header.js.map +1 -0
  9. package/dist/identity/index.d.ts +119 -0
  10. package/dist/identity/index.d.ts.map +1 -0
  11. package/dist/identity/index.js +217 -0
  12. package/dist/identity/index.js.map +1 -0
  13. package/dist/identity/sign.d.ts +92 -0
  14. package/dist/identity/sign.d.ts.map +1 -0
  15. package/dist/identity/sign.js +182 -0
  16. package/dist/identity/sign.js.map +1 -0
  17. package/dist/index.d.ts +58 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +146 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/internal/encoding.d.ts +33 -0
  22. package/dist/internal/encoding.d.ts.map +1 -0
  23. package/dist/internal/encoding.js +135 -0
  24. package/dist/internal/encoding.js.map +1 -0
  25. package/dist/internal/hkdf.d.ts +14 -0
  26. package/dist/internal/hkdf.d.ts.map +1 -0
  27. package/dist/internal/hkdf.js +27 -0
  28. package/dist/internal/hkdf.js.map +1 -0
  29. package/dist/internal/hmac.d.ts +9 -0
  30. package/dist/internal/hmac.d.ts.map +1 -0
  31. package/dist/internal/hmac.js +18 -0
  32. package/dist/internal/hmac.js.map +1 -0
  33. package/dist/internal/runtime.d.ts +29 -0
  34. package/dist/internal/runtime.d.ts.map +1 -0
  35. package/dist/internal/runtime.js +67 -0
  36. package/dist/internal/runtime.js.map +1 -0
  37. package/dist/internal/sodium.d.ts +12 -0
  38. package/dist/internal/sodium.d.ts.map +1 -0
  39. package/dist/internal/sodium.js +34 -0
  40. package/dist/internal/sodium.js.map +1 -0
  41. package/dist/internal/types.d.ts +98 -0
  42. package/dist/internal/types.d.ts.map +1 -0
  43. package/dist/internal/types.js +70 -0
  44. package/dist/internal/types.js.map +1 -0
  45. package/dist/kdf/argon2id.d.ts +72 -0
  46. package/dist/kdf/argon2id.d.ts.map +1 -0
  47. package/dist/kdf/argon2id.js +98 -0
  48. package/dist/kdf/argon2id.js.map +1 -0
  49. package/dist/migration/v0-bridge.d.ts +94 -0
  50. package/dist/migration/v0-bridge.d.ts.map +1 -0
  51. package/dist/migration/v0-bridge.js +203 -0
  52. package/dist/migration/v0-bridge.js.map +1 -0
  53. package/dist/streams/aes-gcm-v1.d.ts +144 -0
  54. package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
  55. package/dist/streams/aes-gcm-v1.js +456 -0
  56. package/dist/streams/aes-gcm-v1.js.map +1 -0
  57. package/dist/streams/pq-hybrid-v1.d.ts +219 -0
  58. package/dist/streams/pq-hybrid-v1.d.ts.map +1 -0
  59. package/dist/streams/pq-hybrid-v1.js +473 -0
  60. package/dist/streams/pq-hybrid-v1.js.map +1 -0
  61. package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
  62. package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
  63. package/dist/suites/aes-gcm-v0/api.js +110 -0
  64. package/dist/suites/aes-gcm-v0/api.js.map +1 -0
  65. package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
  66. package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
  67. package/dist/suites/aes-gcm-v1/api.js +181 -0
  68. package/dist/suites/aes-gcm-v1/api.js.map +1 -0
  69. package/dist/suites/aes-gcm-v1/index.d.ts +59 -0
  70. package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
  71. package/dist/suites/aes-gcm-v1/index.js +177 -0
  72. package/dist/suites/aes-gcm-v1/index.js.map +1 -0
  73. package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
  74. package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
  75. package/dist/suites/aes-gcm-v2/api.js +162 -0
  76. package/dist/suites/aes-gcm-v2/api.js.map +1 -0
  77. package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
  78. package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
  79. package/dist/suites/aes-gcm-v2/index.js +193 -0
  80. package/dist/suites/aes-gcm-v2/index.js.map +1 -0
  81. package/dist/suites/pq-hybrid-v1/api.d.ts +67 -0
  82. package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
  83. package/dist/suites/pq-hybrid-v1/api.js +190 -0
  84. package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
  85. package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
  86. package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
  87. package/dist/suites/pq-hybrid-v1/index.js +225 -0
  88. package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
  89. package/dist/suites/xchacha-v1/api.d.ts +29 -0
  90. package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
  91. package/dist/suites/xchacha-v1/api.js +154 -0
  92. package/dist/suites/xchacha-v1/api.js.map +1 -0
  93. package/dist/suites/xchacha-v1/index.d.ts +50 -0
  94. package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
  95. package/dist/suites/xchacha-v1/index.js +133 -0
  96. package/dist/suites/xchacha-v1/index.js.map +1 -0
  97. package/package.json +124 -0
  98. package/spec/format-v0.md +81 -0
  99. package/spec/format-v1.md +400 -0
  100. package/spec/key-derivation.md +167 -0
  101. package/spec/threat-model.md +242 -0
  102. package/src/format/header.ts +316 -0
  103. package/src/identity/index.ts +371 -0
  104. package/src/identity/sign.ts +221 -0
  105. package/src/index.ts +224 -0
  106. package/src/internal/encoding.ts +152 -0
  107. package/src/internal/hkdf.ts +47 -0
  108. package/src/internal/hmac.ts +33 -0
  109. package/src/internal/runtime.ts +80 -0
  110. package/src/internal/sodium.ts +38 -0
  111. package/src/internal/types.ts +134 -0
  112. package/src/kdf/argon2id.ts +165 -0
  113. package/src/migration/v0-bridge.ts +347 -0
  114. package/src/streams/aes-gcm-v1.ts +647 -0
  115. package/src/streams/pq-hybrid-v1.ts +722 -0
  116. package/src/suites/aes-gcm-v0/api.ts +155 -0
  117. package/src/suites/aes-gcm-v1/api.ts +292 -0
  118. package/src/suites/aes-gcm-v1/index.ts +269 -0
  119. package/src/suites/aes-gcm-v2/api.ts +268 -0
  120. package/src/suites/aes-gcm-v2/index.ts +289 -0
  121. package/src/suites/pq-hybrid-v1/api.ts +299 -0
  122. package/src/suites/pq-hybrid-v1/index.ts +343 -0
  123. package/src/suites/xchacha-v1/api.ts +254 -0
  124. package/src/suites/xchacha-v1/index.ts +213 -0
@@ -0,0 +1,400 @@
1
+ # ShieldFive Crypto File Format v1
2
+
3
+ This document is the canonical specification for the v1 ShieldFive encrypted
4
+ file format. The implementation in this repository is a reference, not the
5
+ specification — when in doubt, this document governs.
6
+
7
+ ## Goals
8
+
9
+ The v1 format is designed to satisfy these properties simultaneously:
10
+
11
+ 1. **Self-describing.** A reviewer with only the encrypted blob and the
12
+ decryption key (no out-of-band metadata) can decrypt the file.
13
+ 2. **AEAD-bound chunk position.** Truncation, reordering, and chunk-mixing
14
+ between files are detected by the AEAD authenticator, not by application
15
+ logic.
16
+ 3. **Cipher-suite agile.** A single byte selects which cipher suite the file
17
+ was encrypted with. Adding a new suite does not change the parser.
18
+ 4. **Post-quantum hybrid by default.** The default suite combines a classical
19
+ AEAD with ML-KEM-1024 key encapsulation. Files encrypted today survive a
20
+ future cryptographically-relevant quantum computer for the duration of
21
+ the AEAD's classical security.
22
+ 5. **Streaming-friendly.** A reader can decrypt chunk N without buffering
23
+ chunks 0..N-1 in memory, and a writer can emit chunk N without buffering
24
+ future chunks.
25
+
26
+ ## Wire format
27
+
28
+ All multi-byte integers are big-endian. All byte counts are in octets.
29
+
30
+ ```
31
+ encrypted_file := header || chunk_0 || chunk_1 || ... || chunk_n
32
+ || signature_block? (optional, see § "Signature block")
33
+ ```
34
+
35
+ ### Header
36
+
37
+ ```
38
+ header := magic (5 bytes) = "SF5\x01\x00"
39
+ || suite (1 byte) = cipher suite identifier
40
+ || flags (1 byte) = reserved, must be 0x00
41
+ || file_id (16 bytes) = random per-file identifier
42
+ || chunk_size (4 bytes) = uint32, plaintext bytes per chunk
43
+ || total_chunks (8 bytes) = uint64, total chunk count
44
+ || plaintext_size (8 bytes) = uint64, total plaintext bytes
45
+ || suite_payload_len (2 bytes) = uint16, length of suite_payload
46
+ || suite_payload (variable) = suite-specific bytes (see below)
47
+ || header_mac (32 bytes) = HMAC-SHA-256 over all above bytes
48
+ keyed with derived header_mac_key
49
+ ```
50
+
51
+ The first 5 bytes `53 46 35 01 00` (`"SF5\x01\x00"`) are the file magic. The
52
+ fourth byte is the format major version (0x01). The fifth is reserved for
53
+ future minor-version bumps that remain backward-compatible.
54
+
55
+ `suite` is one of:
56
+
57
+ | Value | Suite | Status |
58
+ | ------ | -------------------------------------- | -------- |
59
+ | `0x00` | (reserved, never assigned) | invalid |
60
+ | `0x01` | `aes-256-gcm-v1` | required |
61
+ | `0x02` | `xchacha20-poly1305-v1` | required |
62
+ | `0x03` | `pq-hybrid-xchacha-mlkem1024-v1` | default |
63
+ | `0x80` | (reserved, custom-suite range begins) | reserved |
64
+
65
+ `flags` is reserved; readers MUST reject any header with a non-zero flags
66
+ byte. Future versions may introduce flags such as
67
+ `0x01 = compressed-plaintext` or `0x02 = sparse-file-encoding`.
68
+
69
+ `file_id` is 16 cryptographically random bytes generated per file. It is
70
+ mixed into every chunk's key and nonce derivation (see the per-suite
71
+ definitions below) to bind ciphertext to its file. `file_id` is NOT
72
+ present in the AAD itself; the cross-file splice resistance is
73
+ structural, via the suite-specific HKDF salt. Future implementers MUST
74
+ NOT add `file_id` to the AAD bytes — doing so would break wire
75
+ compatibility with already-shipped v1 files.
76
+
77
+ `plaintext_size` is the exact number of plaintext bytes. It allows a reader
78
+ to detect truncation of the final chunk before AEAD verification, and to
79
+ allocate output buffers correctly.
80
+
81
+ `suite_payload` is suite-specific. See "Suite payloads" below.
82
+
83
+ `header_mac` authenticates the entire header (everything before the
84
+ `header_mac` field itself) under a key derived from the content key:
85
+
86
+ ```
87
+ header_mac_key := HKDF-SHA-256(
88
+ ikm = content_key,
89
+ salt = file_id,
90
+ info = "shieldfive/v1/header-mac",
91
+ L = 32
92
+ )
93
+ ```
94
+
95
+ For suites whose header MAC key is not derived from `suite_payload`
96
+ (currently `0x01` aes-gcm-v1, `0x02` xchacha-v1), readers MUST verify
97
+ `header_mac` before parsing `suite_payload` or any chunk. KEM suites
98
+ (currently `0x03` pq-hybrid-v1) verify `header_mac` after decapsulation
99
+ per their suite-specific decryption flow; see § "`0x03` — pq-hybrid"
100
+ below for the required ordering and security argument. A header_mac
101
+ failure means the wrong content key was supplied or the header has been
102
+ tampered with.
103
+
104
+ ### Chunks
105
+
106
+ ```
107
+ chunk_i := length (4 bytes) = uint32, length of ciphertext field
108
+ || ciphertext (variable) = AEAD output (includes auth tag)
109
+ ```
110
+
111
+ The AEAD inputs for chunk `i` are:
112
+
113
+ ```
114
+ nonce = chunk_nonce(suite, file_id, i)
115
+ aad = "shieldfive/v1/chunk" || uint64_be(i) || uint64_be(total_chunks)
116
+ || uint8(is_final)
117
+ key = chunk_key(suite, content_key, file_id)
118
+ ```
119
+
120
+ where:
121
+
122
+ - `is_final` is `0x01` for chunk `i = total_chunks - 1`, otherwise `0x00`.
123
+ - `chunk_nonce` and `chunk_key` are defined per suite.
124
+ - `aad` is a fixed-length 36 bytes (19 bytes domain string + 8 + 8 + 1).
125
+
126
+ Note: the AAD is exactly the 36 bytes defined above. Do not extend it.
127
+ Cross-file splice resistance is provided by the suite-specific
128
+ chunk-key and nonce-prefix derivations, which use `file_id` as HKDF
129
+ salt and IKM respectively.
130
+
131
+ The plaintext input is:
132
+
133
+ - For all chunks except the final: exactly `chunk_size` plaintext bytes.
134
+ - For the final chunk: between 1 and `chunk_size` plaintext bytes such that
135
+ the sum of all chunks' plaintext lengths equals `plaintext_size`.
136
+
137
+ A chunk with zero plaintext bytes is invalid. A file with `plaintext_size`
138
+ of zero MUST be encoded with `total_chunks = 0` and zero chunks; it is the
139
+ caller's choice whether to permit empty plaintexts at all.
140
+
141
+ ### Length-prefix vs. fixed-size chunks
142
+
143
+ The length prefix exists because some suites (notably any future
144
+ length-extending suite) may emit ciphertext that is not exactly
145
+ `chunk_size + tag_size` bytes. For the suites defined in this version,
146
+ length is always `chunk_size + suite.tag_size` for non-final chunks. Readers
147
+ SHOULD validate this invariant.
148
+
149
+ ## Suite payloads
150
+
151
+ ### `0x01` — `aes-256-gcm-v1`
152
+
153
+ ```
154
+ suite_payload := wrapped_key (60 bytes) = AES-GCM-wrapped 32-byte content key
155
+ || wrap_iv (12 bytes) = AES-GCM IV used for wrapping
156
+ ```
157
+
158
+ - `chunk_key(content_key, file_id)` =
159
+ `HKDF-SHA-256(ikm=content_key, salt=file_id, info="shieldfive/v1/aes-gcm/chunk-key", L=32)`
160
+ - `chunk_nonce(file_id, i)` =
161
+ `HKDF-SHA-256(ikm=file_id, salt=zeros(32), info="shieldfive/v1/aes-gcm/nonce-prefix", L=4)`
162
+ `|| uint64_be(i)`
163
+
164
+ The salt is the RFC 5869 "absent salt" convention: a 32-byte all-zero
165
+ string (the SHA-256 output length). Implementations MUST use this
166
+ canonical form, even though HMAC-SHA-256 happens to produce an
167
+ identical HKDF-Extract output for any salt that zero-pads to 64 bytes.
168
+
169
+ The nonce prefix is derived from `file_id` so that two files encrypted with
170
+ the same content key (which never happens under correct use, but defense in
171
+ depth) cannot collide on a `(prefix, counter)` pair.
172
+
173
+ The wrapped content key is wrapped with the *parent envelope key* (folder
174
+ key, vault key, etc.) using AES-256-GCM. This wrapping is OUT OF SCOPE for
175
+ the on-disk file format and is the responsibility of the application's
176
+ keyring module. The fields above are for files that choose to embed the
177
+ wrapped key inline (e.g. exported files). When the wrapped key is stored
178
+ out-of-band (e.g. in the ShieldFive vault database), all 72 bytes of
179
+ `suite_payload` (60-byte `wrapped_key` + 12-byte `wrap_iv`) are set to
180
+ zero. Readers MUST accept all-zero `suite_payload` bytes in this case
181
+ and obtain the wrapped key from the out-of-band channel. Whether the
182
+ field is inline or zero-filled is a deploy decision; either way the
183
+ field length is fixed at 72 bytes.
184
+
185
+ ### `0x02` — `xchacha20-poly1305-v1`
186
+
187
+ ```
188
+ suite_payload := wrapped_key (72 bytes) = secretbox-wrapped 32-byte content key
189
+ || wrap_nonce (24 bytes) = XSalsa20 nonce for wrapping
190
+ ```
191
+
192
+ - `chunk_key(content_key, file_id)` =
193
+ `HKDF-SHA-256(ikm=content_key, salt=file_id, info="shieldfive/v1/xchacha/chunk-key", L=32)`
194
+ - `chunk_nonce(file_id, i)` =
195
+ `HKDF-SHA-256(ikm=file_id, salt=zeros(32), info="shieldfive/v1/xchacha/nonce-prefix", L=16)`
196
+ `|| uint64_be(i)`
197
+
198
+ Salt convention is the RFC 5869 absent-salt zero-fill (32 zero bytes),
199
+ identical to the AES-GCM suite above.
200
+
201
+ XChaCha20's 24-byte nonce gives us a 16-byte random prefix and an 8-byte
202
+ counter, which is structurally safer than AES-GCM's 4+8 split.
203
+
204
+ As with the AES-GCM suite, when the wrapped key is stored out-of-band
205
+ (e.g. in the ShieldFive vault database), all 96 bytes of `suite_payload`
206
+ (72-byte `wrapped_key` + 24-byte `wrap_nonce`) are set to zero. Readers
207
+ MUST accept all-zero `suite_payload` bytes in this case and obtain the
208
+ wrapped key from the out-of-band channel. The field length is fixed at
209
+ 96 bytes whether inline or zero-filled.
210
+
211
+ ### `0x03` — `pq-hybrid-xchacha-mlkem1024-v1` (default)
212
+
213
+ ```
214
+ suite_payload := mlkem_ciphertext (1568 bytes) = ML-KEM-1024 ciphertext
215
+ || classical_wrapped (72 bytes) = secretbox-wrapped classical share
216
+ || classical_nonce (24 bytes) = secretbox nonce for classical wrap
217
+ ```
218
+
219
+ The recipient holds:
220
+
221
+ - A classical key `K_c` (32 bytes), wrapped at the envelope layer like the
222
+ XChaCha suite.
223
+ - An ML-KEM-1024 keypair `(pk_pq, sk_pq)` derived deterministically from
224
+ the user's master secret (see `spec/key-derivation.md`).
225
+
226
+ Encryption:
227
+
228
+ 1. Sender generates a 32-byte classical share `S_c` randomly.
229
+ 2. Sender encapsulates against `pk_pq` to get `(mlkem_ciphertext, S_pq)`.
230
+ `S_pq` is a 32-byte shared secret.
231
+ 3. Combined content key `K = HKDF-SHA-256(ikm = S_c || S_pq, salt = file_id,
232
+ info = "shieldfive/v1/pq-hybrid/combine", L = 32)`.
233
+ 4. Sender wraps `S_c` with `K_c` using XSalsa20-Poly1305 secretbox to
234
+ produce `classical_wrapped`. (The PQ share is recovered from
235
+ `mlkem_ciphertext` via decapsulation; only the classical share needs
236
+ wrapping.)
237
+ 5. Subsequent chunks use the XChaCha20-Poly1305 chunk format with `K`.
238
+
239
+ Decryption:
240
+
241
+ 1. Reader decapsulates `mlkem_ciphertext` with `sk_pq` to recover `S_pq`.
242
+ 2. Reader unwraps `classical_wrapped` with `K_c` to recover `S_c`.
243
+ 3. Reader recomputes `K = HKDF-SHA-256(ikm = S_c || S_pq, ...)`.
244
+ 4. Reader verifies `header_mac` (which uses `K`).
245
+ 5. Reader processes chunks.
246
+
247
+ #### Security argument for pre-MAC decapsulation
248
+
249
+ The decapsulation in step 1 operates on unauthenticated
250
+ `mlkem_ciphertext` bytes — the header MAC cannot yet be verified at
251
+ that point, because the MAC key is the combined key K, which requires
252
+ both decapsulation and classical unwrap to compute. Safety of this
253
+ construction rests on three properties:
254
+
255
+ 1. **ML-KEM-1024 is IND-CCA2-secure** (FIPS 203). Decapsulation of
256
+ malformed or adversarial ciphertext does not leak the encapsulated
257
+ secret `S_pq` and does not leak the secret key `sk_pq`. An attacker
258
+ who modifies `mlkem_ciphertext` bytes obtains a different `S_pq`
259
+ (computationally indistinguishable from random under IND-CCA2), but
260
+ learns nothing about the underlying key material.
261
+
262
+ 2. **The classical share is wrapped with XSalsa20-Poly1305 secretbox.**
263
+ The unwrap in step 2 is an AEAD operation over attacker-influenced
264
+ `classical_wrapped` bytes. Any modification of those bytes causes
265
+ the secretbox tag check to fail, raising before plaintext is
266
+ produced. An attacker who modifies the classical share cannot
267
+ silently substitute a different `S_c`.
268
+
269
+ 3. **The recombined key K is verified via `header_mac`.** The
270
+ encryptor's `header_mac` is a valid HMAC-SHA-256 tag under the
271
+ encryptor's K over the encryptor's header bytes. An attacker who
272
+ modifies any bytes covered by the MAC (header fields,
273
+ `suite_payload`, or both) and whose modified inputs cause
274
+ decapsulation and classical unwrap to yield some K' would then
275
+ need to forge an HMAC-SHA-256 tag under K' over the modified
276
+ bytes — which contradicts HMAC's EUF-CMA security. Equivalently:
277
+ the only way to pass MAC verification under any K' is to leave
278
+ the MAC-covered bytes unchanged, in which case the encryption
279
+ succeeds against the unmodified ciphertext (not an attack).
280
+
281
+ An attacker modifying `suite_payload` bytes therefore either (a) gets a
282
+ different combined key K and fails `header_mac` verification, or (b)
283
+ fails at secretbox tag check during classical unwrap, or (c) learns
284
+ nothing under ML-KEM IND-CCA2. In no case is plaintext revealed.
285
+ Implementations MAY surface a distinct error for case (b) (secretbox
286
+ failure) versus case (a) (MAC failure) since case (b) is reachable with
287
+ smaller attacker effort and may be useful for telemetry; they MUST NOT
288
+ leak the value of K, S_c, or S_pq in any error path.
289
+
290
+ This construction is IND-CCA2 against an adversary who breaks *either* the
291
+ classical wrap *or* the PQ KEM, but not both. As long as one primitive
292
+ remains secure, the file is secure. Under correct use, both must be broken
293
+ to recover the plaintext.
294
+
295
+ Unlike the AES-GCM and XChaCha suites, the all-zero `suite_payload`
296
+ convention does NOT apply to PQ-hybrid: `mlkem_ciphertext` is part of
297
+ decryption (the PQ shared secret is recovered from it via
298
+ decapsulation) and therefore cannot be stored out-of-band. The full
299
+ 1664-byte `suite_payload` MUST be present inline in every PQ-hybrid
300
+ file.
301
+
302
+ ## Signature block
303
+
304
+ The signature block is an OPTIONAL trailing structure that carries a
305
+ detached sender signature over `header_unauthenticated_bytes ||
306
+ concat(per-chunk MAC tags)`. It provides *sender attribution*: a
307
+ recipient who trusts a sender's public key can verify the file was
308
+ produced by that sender. It does NOT add confidentiality or chunk
309
+ integrity — those are provided by the per-suite AEAD and by
310
+ `header_mac` regardless of whether a signature block is present.
311
+
312
+ ```
313
+ signature_block := algorithm (1 byte) = signature algorithm identifier
314
+ || pubkey_len (2 bytes) = uint16 BE, length of public_key
315
+ || public_key (variable) = sender's verifying key
316
+ || signature_len (2 bytes) = uint16 BE, length of signature
317
+ || signature (variable) = detached signature bytes
318
+ ```
319
+
320
+ `algorithm` is one of:
321
+
322
+ | Value | Algorithm | Public key | Signature | Status |
323
+ | ------ | ------------- | ---------- | --------- | -------- |
324
+ | `0x00` | (reserved) | — | — | invalid |
325
+ | `0x01` | Ed25519 | 32 bytes | 64 bytes | defined |
326
+ | `0x02` | ML-DSA-65 | 1952 bytes | 3309 bytes | reserved (not yet implemented) |
327
+ | `0x80` | (reserved, custom-algorithm range begins) | — | — | reserved |
328
+
329
+ The message signed for `0x01 = Ed25519` is the byte string
330
+
331
+ ```
332
+ signed_message := header_unauthenticated_bytes
333
+ || concat(chunk_0_mac, chunk_1_mac, ..., chunk_{n-1}_mac)
334
+ ```
335
+
336
+ where `header_unauthenticated_bytes` is exactly the bytes covered by
337
+ `header_mac` (i.e. everything in the header before the `header_mac`
338
+ field itself), and each `chunk_i_mac` is the suite's AEAD authenticator
339
+ tag for chunk `i`. For all suites defined in this version the AEAD tag
340
+ is the trailing 16 bytes of the chunk's ciphertext field (AES-GCM-128
341
+ and Poly1305 both append a 16-byte tag).
342
+
343
+ The signature block is OPTIONAL. Implementations:
344
+
345
+ - **Writers MAY** append a signature block. Writers MUST NOT emit more
346
+ than one. The block, if present, MUST appear immediately after the
347
+ final chunk's ciphertext field and MUST NOT be followed by any further
348
+ bytes.
349
+ - **Readers MUST** treat a file with no bytes after the last chunk as
350
+ unsigned (signature absent). Readers MUST treat any trailing bytes as
351
+ a signature block and MUST reject the file if those bytes do not parse
352
+ as a well-formed block or are followed by trailing garbage.
353
+ - **Readers SHOULD** expose the parsed block (algorithm, public key,
354
+ signature) to callers so an application-level identity policy can
355
+ decide whether the file is acceptable.
356
+ - **Readers MAY** verify the signature against a caller-supplied public
357
+ key. A signature block whose `algorithm` is unknown to the reader is
358
+ treated as unverifiable; the reader returns the block to the caller
359
+ without raising and lets the application policy decide.
360
+
361
+ A signature block does not weaken the format's existing security
362
+ guarantees: an attacker who tampers with any byte covered by `header_mac`
363
+ fails MAC verification under the content key, and an attacker who
364
+ tampers with any chunk ciphertext fails per-chunk AEAD verification. The
365
+ signature is a separate, application-level trust layer.
366
+
367
+ ## Versioning policy
368
+
369
+ - **Format major version (4th byte of magic)** changes when the parser
370
+ changes incompatibly. Old readers MUST refuse to read newer major
371
+ versions.
372
+ - **Format minor version (5th byte of magic)** changes when fields are
373
+ added in ways that older readers can ignore. Currently 0x00. Old readers
374
+ encountering a minor version they don't recognize MUST refuse to decrypt
375
+ unless explicitly configured to ignore unknown minor versions.
376
+ - **Suite identifier** is independent of format version. New suites can be
377
+ added without changing the format version.
378
+
379
+ ## Compatibility with v0 (legacy production format)
380
+
381
+ The current ShieldFive production upload format predates this
382
+ specification. It is referred to as "v0" and is described in
383
+ `spec/format-v0.md`. v0 files do not have the SF5 magic; they are detected
384
+ by the absence of magic and by the presence of the database-stored
385
+ `cipher_version = 1` flag. ShieldFive applications MUST be able to read
386
+ v0 indefinitely; v0 writes are deprecated and SHOULD be migrated to v1.
387
+
388
+ v0 and v1 cannot be silently confused in either direction. A v1 blob fed
389
+ to the v0 reader fails AES-GCM authentication on chunk 0 (the v1
390
+ magic/suite/file_id bytes do not form a valid GCM tag). A v0 blob fed to
391
+ `parseHeader` fails the magic-byte check with probability `1 - 2^-40`
392
+ per file. Application dispatchers MUST route by an explicit version
393
+ field (e.g. a database column) and MUST NOT auto-detect across formats.
394
+ The library does not expose cross-format auto-detection.
395
+
396
+ ## Test vectors
397
+
398
+ Implementations MUST produce identical bit-for-bit output for the test
399
+ vectors in `tests/vectors/`. These vectors are computed from a fixed seed
400
+ and committed to the repository.
@@ -0,0 +1,167 @@
1
+ # ShieldFive Crypto — Key Derivation
2
+
3
+ This document specifies how application-level keys (envelope keys, ML-KEM
4
+ keypairs, header MAC keys, chunk keys) are derived from the user's master
5
+ secret. It is normative for the v1 release.
6
+
7
+ ## Master secret
8
+
9
+ The library does not generate or manage the master secret directly. The
10
+ host application is responsible for producing 32 bytes of high-entropy
11
+ secret material — typically by stretching a user passphrase through
12
+ Argon2id, by reading a hardware token, or by combining the two.
13
+
14
+ This document assumes the master secret has been derived. From there:
15
+
16
+ ```
17
+ master_secret ∈ bytes[32]
18
+ ```
19
+
20
+ Everything below is a deterministic function of this secret.
21
+
22
+ ## Derivation tree
23
+
24
+ All derivations are HKDF-SHA-256. The `info` string is a printable
25
+ domain separator. When the salt is omitted in the definitions below,
26
+ its canonical form is `zeros(32)` per RFC 5869's "absent salt"
27
+ convention (a string of `HashLen` zero octets, where `HashLen = 32` for
28
+ SHA-256). Implementations MUST use this form; under HMAC-SHA-256 a
29
+ zero-length salt yields the same HKDF-Extract output because HMAC
30
+ zero-pads short keys to its 64-byte block size, but the canonical form
31
+ is required so that cross-implementation test vectors pin a single
32
+ shape. Keys are 32 bytes unless otherwise stated. Test vectors for the
33
+ nonce-prefix absent-salt derivation are at `tests/vectors/vectors.json`
34
+ (group `09_nonce_prefix_absent_salt`).
35
+
36
+ ```
37
+ master_secret
38
+ ├── envelope_key = HKDF(master, info="shieldfive/v1/envelope-key")
39
+ │
40
+ ├── metadata_key = HKDF(master, info="shieldfive/v1/metadata-key")
41
+ │ (used to encrypt filenames, folder names — outside this library's scope)
42
+ │
43
+ └── ml_kem_seed = HKDF(master, info="shieldfive/v1/pq-hybrid/ml-kem-1024-seed", L=64)
44
+ └── (ml_kem_pk, ml_kem_sk) = ML-KEM-1024.KeyGen(ml_kem_seed)
45
+ ```
46
+
47
+ The 64-byte `ml_kem_seed` is split into `d = ml_kem_seed[0..32]` and
48
+ `z = ml_kem_seed[32..64]`, with `d` consumed as ML-KEM-1024.KeyGen's
49
+ random-coins input (FIPS 203 § Algorithm 16, parameter `d`) and `z`
50
+ stored as the implicit-rejection seed (FIPS 203 § Algorithm 17,
51
+ parameter `z`). Implementers using a library whose API exposes a
52
+ combined-seed form `KeyGen(seed)` (e.g., @noble/post-quantum) typically
53
+ follow this same convention, but MUST verify against the deterministic
54
+ test vectors below. Implementers using a library that exposes
55
+ `KeyGen(d, z)` separately MUST split the seed exactly as specified
56
+ here.
57
+
58
+ The function `deriveMlKemKeypair(masterSecret)` exported from
59
+ `@shieldfive/crypto/pq-hybrid-v1` performs the seed derivation and the
60
+ ML-KEM keygen step in one call.
61
+
62
+ ## Why deterministic ML-KEM keys?
63
+
64
+ ML-KEM-1024 secret keys are 3168 bytes. Asking the user to back up an
65
+ extra 3168 bytes alongside their master secret would create a new failure
66
+ mode: losing the PQ key while still holding the master secret would mean
67
+ losing access to PQ-hybrid files.
68
+
69
+ Instead, we derive the PQ keypair from the master secret. The user backs
70
+ up only the master secret (or, more typically, only their passphrase plus
71
+ the Argon2id parameters). Everything else regenerates deterministically.
72
+
73
+ The cost of this choice: a future cryptanalytic break in HKDF-SHA-256's
74
+ seed expansion would propagate to the PQ keypair. We accept this because
75
+ HKDF-SHA-256 is conservative, and the alternative (separate PQ key
76
+ backup) has a worse failure mode (data loss).
77
+
78
+ ## Per-file key derivation (recap from format-v1.md)
79
+
80
+ Once the envelope key is in hand, per-file keys are derived as:
81
+
82
+ ```
83
+ content_key = randomBytes(32) (fresh per file, never derived)
84
+ file_id = randomBytes(16) (fresh per file, never derived)
85
+
86
+ header_mac_key = HKDF(content_key, salt=file_id,
87
+ info="shieldfive/v1/header-mac")
88
+
89
+ chunk_key = HKDF(content_key, salt=file_id,
90
+ info="shieldfive/v1/<suite>/chunk-key")
91
+
92
+ nonce_prefix = HKDF(file_id,
93
+ info="shieldfive/v1/<suite>/nonce-prefix",
94
+ L = 4 (AES-GCM) or 16 (XChaCha))
95
+
96
+ iv / nonce_i = nonce_prefix || uint64_be(chunk_index)
97
+ ```
98
+
99
+ For the PQ-hybrid suite, `content_key` is replaced by the combined key
100
+ `K = HKDF(classical_share || pq_share, salt=file_id,
101
+ info="shieldfive/v1/pq-hybrid/combine", L=32)`. The chunk-key and
102
+ nonce-prefix derivations are then identical to the XChaCha suite's,
103
+ with `K` in the role of `content_key` and the same info strings
104
+ (`shieldfive/v1/xchacha/chunk-key` and
105
+ `shieldfive/v1/xchacha/nonce-prefix`). Reuse of those info strings
106
+ across the two suites is safe because the IKM space is partitioned:
107
+ XChaCha uses a fresh random `content_key` per file, PQ-hybrid uses the
108
+ file-bound HKDF output `K`. Both are 32 bytes of uniformly random or
109
+ pseudorandom material per file; cross-suite output collision
110
+ probability is `2^-256`.
111
+
112
+ ## Forbidden cross-context usage
113
+
114
+ These domain strings are consumed by the crypto layer. The host
115
+ application MUST NOT reuse any of them for unrelated purposes:
116
+
117
+ ```
118
+ shieldfive/v1/header-mac
119
+ shieldfive/v1/aes-gcm/chunk-key
120
+ shieldfive/v1/aes-gcm/nonce-prefix
121
+ shieldfive/v1/xchacha/chunk-key
122
+ shieldfive/v1/xchacha/nonce-prefix
123
+ shieldfive/v1/pq-hybrid/combine
124
+ shieldfive/v1/pq-hybrid/ml-kem-1024-seed
125
+ shieldfive/v1/argon2id/salt-compression
126
+ ```
127
+
128
+ ### Reserved for future use
129
+
130
+ These strings are reserved by this specification but are NOT yet
131
+ consumed by the reference implementation. They MUST NOT be reused by
132
+ host applications for unrelated purposes, since a future minor
133
+ revision of this spec may begin consuming them:
134
+
135
+ ```
136
+ shieldfive/v1/envelope-key
137
+ shieldfive/v1/metadata-key
138
+ ```
139
+
140
+ Interpretation note: the forbidden-list framing above scopes to
141
+ strings the library actually consumes today. The two entries here are
142
+ spec-only reservations carried over from the derivation tree, kept
143
+ separate so the forbidden list can be cross-checked against
144
+ `HKDF_INFO` in `src/internal/types.ts` without phantom entries.
145
+
146
+ If the host application needs a new derived key, it MUST use a fresh
147
+ domain string of the form `<application>/<version>/<purpose>` to ensure
148
+ domain separation from this library's keys.
149
+
150
+ ## Test vectors
151
+
152
+ Deterministic test vectors for each derivation step live in
153
+ `tests/vectors/key-derivation.json`. They are used by
154
+ `tests/integration/key-derivation.test.ts` to ensure cross-implementation
155
+ consistency.
156
+
157
+ A reference vector:
158
+
159
+ ```
160
+ master_secret = 0000…0000 (32 bytes of zero)
161
+ envelope_key = (computed) — see tests/vectors/key-derivation.json
162
+ ml_kem_seed = (computed)
163
+ ml_kem_pk[0..7] = (computed first 8 bytes)
164
+ ```
165
+
166
+ These vectors are committed to the repository so that any reimplementation
167
+ of the v1 spec can self-check.