# Rotation and recovery A key that protects data for years will be rotated, retired, and, one day, unavailable. This guide sets up a primary key with an offline escrow key, rotates the primary, moves to a new key, and recovers without the key service. The examples use HPKE providers in place of a key service so that they run anywhere; with AWS KMS, Google Cloud KMS, Azure Key Vault, or Vault Transit the calls are the same. ## A primary key and an escrow key Every envelope is wrapped for every recipient key: the primary key, then each additional key. An escrow key that only its custodians can use keeps data recoverable if the key service is lost, the account is locked, or the key is destroyed by mistake. ```python from cryptography_suite import EncryptionContext, KeyRef, Policy, Protector from cryptography_suite.providers.hpke import HpkeRecipientProvider, generate_private_key context = EncryptionContext({"tenant": "acme"}, purpose="payments") kms = HpkeRecipientProvider(provider_id="example.kms") # stands in for a key service kms.add_identity("payments", generate_private_key()) escrow_identity = generate_private_key() # printed, split, and locked away escrow = HpkeRecipientProvider(provider_id="example.escrow") escrow.add_recipient("break-glass", escrow_identity.public_key()) protector = Protector( providers=[kms, escrow], policy=Policy(), primary_key=KeyRef("example.kms", "payments"), additional_keys=[KeyRef("example.escrow", "break-glass")], ) envelope = protector.seal(b"wire 1,000,000 EUR to ACME", context=context) print([key.key_id for key in protector.inspect(envelope).recipients]) # ['payments', 'break-glass'] ``` The service holds only the escrow *public* key: it can seal to it but never open with it. If any recipient key cannot wrap, `seal` fails instead of writing an envelope without it. ## Rotate the primary key A key service rotates a key by creating a new version. New envelopes use it at once; existing envelopes keep opening under the version that wrapped them. `rewrap` moves an envelope to the current version by rewriting its recipient section. The ciphertext, and any escrow recipient, stay exactly as they were. ```python kms.add_identity("payments", generate_private_key()) # a new version rotated = protector.rewrap(envelope, destination_key=KeyRef("example.kms", "payments")) before = protector.inspect(envelope).recipients after = protector.inspect(rotated).recipients assert after[0].version != before[0].version # the primary moved assert after[1] == before[1] # the escrow recipient is untouched assert protector.open(rotated, context=context) == b"wire 1,000,000 EUR to ACME" ``` `rewrap` needs no context and does not decrypt the content, so it is cheap and can run in bulk as a background job. Rewrapping an envelope that already has the current version returns it unchanged, so the job can be rerun safely. For large archives, `rewrap_stream` does the same in bounded memory (see [Streaming and range reads](streaming.md)). With AWS KMS automatic rotation, the key ARN stays the same and AWS keeps old key material, so envelopes need no rewrap at all. ## Move to a new key To replace the primary key with a different key, make the new key primary and keep the old one as a *decryption key*: trusted for opening and rewrapping, never used for sealing. ```python kms.add_identity("payments-2027", generate_private_key()) protector_2027 = Protector( providers=[kms, escrow], policy=Policy(), primary_key=KeyRef("example.kms", "payments-2027"), additional_keys=[KeyRef("example.escrow", "break-glass")], decryption_keys=[KeyRef("example.kms", "payments")], ) assert protector_2027.open(rotated, context=context) == b"wire 1,000,000 EUR to ACME" migrated = protector_2027.rewrap( rotated, destination_key=KeyRef("example.kms", "payments-2027") ) print([key.key_id for key in protector_2027.inspect(migrated).recipients]) # ['payments-2027', 'break-glass'] ``` Once every envelope has been rewrapped, remove the old key from `decryption_keys` and retire it at the key service. ```{important} Only keys you configure are trusted. A recipient that names any other key is never sent to a key service, even when your credentials could use that key, because anyone controlling it could otherwise seal data you would accept as your own. When you re-point an alias to a new key, envelopes under the old key keep opening through the alias only until the five-minute resolution cache expires: list the old key in `decryption_keys` before re-pointing. ``` ## Rotate an additional key `rewrap` replaces one recipient. Unless told otherwise, it replaces the recipient with the destination's name, or else the first recipient, the primary key, whichever recipient unwrapped, so a rewrap during an outage of the primary's key service keeps the escrow recipient. To move any other recipient, such as an escrow key, name it with `replace`: ```python successor_identity = generate_private_key() successor = escrow.add_recipient("break-glass-2028", successor_identity.public_key()) escrow_rotated = protector_2027.rewrap( migrated, destination_key=successor, replace=KeyRef("example.escrow", "break-glass"), ) recipients = protector_2027.inspect(escrow_rotated).recipients assert [key.key_id for key in recipients] == ["payments-2027", "break-glass-2028"] ``` Without `replace`, this rewrap would have replaced the *primary* recipient and kept the old escrow key. `replace` is checked before any key is used: a recipient the envelope does not have is refused with `KEY_NOT_FOUND`, and an envelope that already holds the successor is returned unchanged, so the job can be rerun. A new version under the same name, as with the primary key above, needs no `replace`. ## Recover without the key service When the primary key is disabled, destroyed, or unreachable, opening falls back to the next recipient whose key is available. The escrow custodians can open everything on an isolated machine with nothing but the escrow private key: ```python console = HpkeRecipientProvider(provider_id="example.escrow") break_glass = console.add_identity("break-glass", escrow_identity) recovery = Protector(provider=console, policy=Policy(), primary_key=break_glass) assert recovery.open(migrated, context=context) == b"wire 1,000,000 EUR to ACME" ``` A single escrow key is a single point of trust. When no one person should be able to recover data, use a [quorum](quorum-recovery.md) as the escrow recipient instead: any two of three custodians, for example, and no one alone. ## Checklist - Seal to at least two keys that fail independently: a key service and an offline escrow or quorum key, or two regions. - Test recovery from the escrow key on a schedule, not only during an incident. - Keep retired keys in `decryption_keys` until a rewrap job has moved every envelope. - Rewrap after each rotation of a key whose service does not keep old versions usable.