@shieldfive/crypto 1.0.0-rc.4 → 1.0.0-rc.6

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.
@@ -0,0 +1,101 @@
1
+ # Vault key chain, name envelopes and agent grants
2
+
3
+ Implemented in `src/vault/index.ts` (`@shieldfive/crypto/vault`). The first
4
+ two formats already exist in the ShieldFive web app's database. This file
5
+ specifies them so that any client can interoperate. The third, agent grants,
6
+ is new.
7
+
8
+ ## 1. Key chain
9
+
10
+ Each folder has a 32-byte folder key (FK). A top-level folder's FK is
11
+ wrapped under the vault root key (RK). Every other FK is wrapped under its
12
+ parent's FK. A file's content key is wrapped under the key of the folder that
13
+ contains it, or under RK for a file at the vault root.
14
+
15
+ ```
16
+ wrapped = AES-256-GCM(key = parent key, iv = 12 random bytes, aad = none, pt = child key)
17
+ stored = base64(ciphertext || 16-byte tag), base64(iv)
18
+ ```
19
+
20
+ Columns: `folders.fk_wrapped/fk_iv`, `files.csk_wrapped/csk_iv`,
21
+ `files.pqk_fk_wrapped/pqk_fk_iv`.
22
+
23
+ For suite 0x03 (PQ-hybrid) files, `csk_wrapped` holds only the classical
24
+ envelope key. The key that decrypts the file is the combined key, which also
25
+ needs the owner's ML-KEM secret key. `pqk_fk_wrapped` is the combined key
26
+ wrapped under the parent FK. The owner's client writes it only when a share
27
+ or grant needs it.
28
+
29
+ ## 2. Name envelopes
30
+
31
+ ```json
32
+ {"v": 4 | 6, "ct": b64, "iv": b64(12), "tag": b64(16), "salt": b64(16), "kdf": "interactive" | "moderate"}
33
+ ```
34
+
35
+ ```
36
+ name key = Argon2id13(password = UTF-8 of base64(parent key), salt,
37
+ interactive: ops=2 mem=64 MiB | moderate: ops=3 mem=256 MiB, 32 bytes)
38
+ v4: AES-256-GCM(name key, iv, aad = none, pt = UTF-8 name)
39
+ v6: AES-256-GCM(name key, iv, aad = UTF-8 of row UUID, pt = UTF-8 name)
40
+ ```
41
+
42
+ If `kdf` is missing, a reader tries interactive first and then moderate.
43
+ Writers always emit v6 with `kdf: "interactive"`. The password already
44
+ carries 256 bits of entropy, so the cost level only exists for compatibility.
45
+
46
+ v3 (fragment-keyed) and the server-keyed `v2|…` shape are outside this
47
+ module.
48
+
49
+ ## 3. Agent grants
50
+
51
+ A grant lets a local agent open a subtree of the vault without the root key.
52
+
53
+ ```
54
+ connection string = "sf-grant-v1:" grant_id "." b64url(token) "." b64url(secret) "." hex(sha256(ml_kem_pk))
55
+ token = 32 random bytes. It is the bearer credential, and the server stores hex(sha256(token)).
56
+ secret = 32 random bytes. It never leaves a client.
57
+ GK = HKDF-SHA-256(ikm = secret, salt = UTF-8(grant_id), info = "shieldfive/v1/agent-grant/wrap", L = 32)
58
+ wrap = AES-256-GCM(GK, iv = 12 random bytes,
59
+ aad = UTF-8("sf-grant-v1|" grant_id "|" kind "|" object_id), pt = key)
60
+ kind = "folder" a folder key
61
+ | "file" the key csk_wrapped holds (the classical envelope key for suite 0x03)
62
+ | "file_pq" the combined content key of a suite-0x03 file
63
+ | "name" the derived key of ONE name envelope
64
+ ```
65
+
66
+ `file`, `file_pq` and `name` are issued only for objects whose parent key
67
+ the grant does not hold: the scope roots' own names, and files at the vault
68
+ root when the grant covers the whole vault. A `name` wrap opens one envelope.
69
+ It is useless for writing a new name under the same parent.
70
+
71
+ The AAD stops a server from presenting one object's wrap as another's. The
72
+ public-key fingerprint lets a grant that writes files detect a substituted
73
+ ML-KEM public key.
74
+
75
+ The owner keeps a copy of the secret so their browser can extend the grant
76
+ later (new top-level folders, renamed roots, post-quantum aux wraps):
77
+
78
+ ```
79
+ scope = "sf-grant-scope-v1|" grant_id "|all=" 0|1 "|media=" 0|1 "|roots=" sorted,unique ids
80
+ "|trash=" id or "-" "|excluded=" sorted,unique ids
81
+ "|excludedKeys=" sorted,unique hex(sha256(key)) of the excluded folders' keys
82
+ SK = HKDF-SHA-256(ikm = RK, salt = UTF-8(grant_id), info = "shieldfive/v1/agent-grant/secret", L = 32)
83
+ owner = AES-256-GCM(SK, iv = 12 random bytes, aad = UTF-8(scope), pt = secret)
84
+ ```
85
+
86
+ The scope is authenticated rather than trusted. A server that edits a grant's
87
+ scope makes the owner's copy stop opening, so the owner's browser never
88
+ extends a grant beyond what the owner chose. The separate label means no
89
+ other key wrapped under RK can stand in for the secret.
90
+
91
+ Security properties and limits:
92
+
93
+ - Leaking the secret without the token gives nothing. The server returns no
94
+ wraps without the token.
95
+ - Leaking both gives the grant's subtree only. It never gives RK, the master
96
+ secret or the ML-KEM secret key.
97
+ - Revocation is enforced by the server. It does not re-key, so wraps and
98
+ ciphertext fetched before revocation stay decryptable by whoever holds
99
+ the secret.
100
+
101
+ Vectors: `tests/vectors/vault-vectors.json`.
package/src/index.ts CHANGED
@@ -99,6 +99,7 @@ export * as kdfArgon2id from './kdf/argon2id.js'
99
99
  export * as identity from './identity/index.js'
100
100
  export * as inbound from './identity/inbound.js'
101
101
  export * as migrationV0 from './migration/v0-bridge.js'
102
+ export * as vault from './vault/index.js'
102
103
 
103
104
  // ──────────────────────────────────────────────────────────────────────
104
105
  // Auto-routing decryptor
@@ -219,7 +220,7 @@ export async function autoDecryptBlob(
219
220
  }
220
221
 
221
222
  /** Library version, also written into format documentation. */
222
- export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-rc.4'
223
+ export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-rc.6'
223
224
 
224
225
  /** Default cipher suite for new files when not specified by the caller. */
225
226
  export const DEFAULT_SUITE = SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1
@@ -106,6 +106,15 @@ export const HKDF_INFO = Object.freeze({
106
106
  // published (ml_kem || x25519) bundle so a guest can detect a key swap.
107
107
  INBOUND_SIGNING_SEED: 'shieldfive/v1/inbound/ed25519-signing-seed',
108
108
  ARGON2ID_SALT_COMPRESSION: 'shieldfive/v1/argon2id/salt-compression',
109
+ // Agent grants (src/vault): the grant secret, generated in the owner's client
110
+ // and never sent to the server, is expanded into the key that wraps the
111
+ // grant's scope-root folder keys. Salt = the grant id.
112
+ AGENT_GRANT_WRAP: 'shieldfive/v1/agent-grant/wrap',
113
+ // The owner's copy of a grant secret is wrapped under a key derived from the
114
+ // vault root key with this label, authenticated against the grant's scope,
115
+ // so no other key wrapped under RK can stand in for it and a server that
116
+ // edits the grant's scope makes the secret stop opening.
117
+ AGENT_GRANT_SECRET: 'shieldfive/v1/agent-grant/secret',
109
118
  })
110
119
 
111
120
  /** AAD domain string mixed into every chunk's authenticator */