# RFC-0026: Hierarchical Keys and Bounded Branch-Key Caching - **Status:** Proposed; adds a core recipient type. - **Owner:** Provider and security leads. - **Last updated:** 2026-11-30 ## Context Every RFC-0011 recipient wraps each envelope's data key with one key-service call. At high volume that is the dominant cost: latency, money, and quota (cloud KMS request limits are per account and region). The AWS Encryption SDK's hierarchical keyring answers the same problem by caching a branch key fetched from a key store, so that one KMS call serves many encryptions. ## Binding decision `cryptography_suite.providers.hierarchical.HierarchicalKeyProvider` is a `KeyProvider` that sits between a root `KeyProvider` and the envelopes. - **Branch keys.** `create_branch_key(branch_id)` draws 32 bytes from the operating-system CSPRNG, wraps them with the root provider (one call) under the binding `"cryptography-suite/v4 branch key" || 0x00 ||` the length-prefixed provider id, branch id, and version, and stores the root-wrapped record in a `BranchKeyStore` as the branch's active version. Versions are `bk-` plus 32 random hex digits and are immutable; creating a new version is rotation. The root key is resolved to an exact version, which the record pins. - **Store.** `BranchKeyStore` is a protocol (`put`, `get`, `active`); records hold only root-wrapped material and serialize as `cs-branch-key/1` JSON for any database. `InMemoryBranchKeyStore` is the reference implementation. A store returning a record for a different branch or version is `KEY_NOT_FOUND`; a record relabelled from another branch fails the root's binding check (`AUTHENTICATION_FAILED`). - **Cache.** A branch key is unwrapped through the root on first use and cached in memory, keyed by branch and version. An entry expires after `cache_ttl` seconds (at most 3,600), optionally after `max_uses` operations, and the least recently used entries beyond `max_cache_entries` (at most 65,536) are evicted. Expired and evicted material is overwritten. Concurrent misses for the same branch key wait on one root call. `root_calls` reports how many unwraps reached the root; `clear_cache()` drops everything. - **Wrapping.** For each data key: a fresh 16-byte salt and 12-byte nonce; `k = HKDF-SHA256(branch key, salt, "cryptography-suite/v4 branch wrap" || 0x00 || provider id || branch id || version)`; `AES-256-GCM(k, nonce, data key, aad = "cryptography-suite/v4 branch binding" || 0x00 || SHA-256(protected bytes))`. The wrapped key is `salt || nonce || ciphertext` and the wrapping algorithm identifier is `cs4-hierarchical-hkdf-sha256-aes-256-gcm`. Every wrap uses its own derived key, so AES-GCM nonce limits never accumulate under one key. - **Identity.** Envelopes record the branch id as `key_id` and the branch key version, so rotation never strands an envelope; `rewrap` moves envelopes to the active version. ## Security consequences - One root call per branch key per cache lifetime, instead of one per envelope: the test seals and opens 500 envelopes with exactly one root unwrap. - A process compromise exposes the cached branch keys. Their number and lifetime are bounded by the cache settings; the root service still authorizes and logs each branch-key unwrap, and revoking the root key stops new branch-key unwraps. - The branch key never leaves the provider; stores hold only root-wrapped records. ## Test and validation requirements `tests/integration/test_hierarchical_keys.py` covers the one-call property across 1,000 operations and a cold second process, expiry with material overwritten, use limits, LRU eviction, sixteen concurrent misses sharing one root call, rotation and rewrap, the conformance kit, binding and tampering, cross-branch substitution, tampered stores, a root returning the wrong key length, a root without the wrap capability, record serialization, and construction limits.