# Backup & disaster recovery

The vault protects key material through storage encryption and encrypted exports for disaster recovery. Keep private recovery keys outside the running deployment and separately from exported archives.

## Backup encryption-key entries

From Crypto Vault 0.7 (chart `0.1.24`), backup recipients are **named entries** configured on the Policy Server with `policyServer.backupEncryptionKeys`. Each entry holds one public key for each Key Store shard. Users then assign an entry, in the Vault UI or through the API, to each vault and to `offchain` before exporting those scopes. All scopes in one export must use the same entry. See [Assign recipients and export a backup](/develop/backup-export).

```yaml
policyServer:
  backupEncryptionKeys:
    - entryName: primary
      keys:
        mpc-ks-0: { encryptionScheme: hpke, publicKey: "<shard-0 64-hex X25519 public key>" }
        mpc-ks-1: { encryptionScheme: hpke, publicKey: "<shard-1 64-hex X25519 public key>" }
        mpc-ks-2: { encryptionScheme: hpke, publicKey: "<shard-2 64-hex X25519 public key>" }
    - entryName: eu-recovery
      keys:
        mpc-ks-0:
          encryptionScheme: rsa_oaep_sha256_chacha20_poly1305
          publicKey: |
            -----BEGIN PUBLIC KEY-----
            ...
            -----END PUBLIC KEY-----
        mpc-ks-1: { encryptionScheme: hpke, publicKey: "<64-hex X25519 public key>" }
        mpc-ks-2: { encryptionScheme: hpke, publicKey: "<64-hex X25519 public key>" }
```

| Field | Rule |
|---|---|
| `entryName` | Unique, non-empty, with no leading or trailing whitespace. Users assign the entry by this name, which the Vault UI shows and `GET /v1/vault-backup-encryption-keys` returns as the entry `id`. |
| `keys` | Exactly one key for each Key Store shard, keyed by the shard name `mpc-ks-<mpcShardIndex>` (`mpc-ks-0`, `mpc-ks-1`, `mpc-ks-2`). A missing or extra shard stops the Policy Server writer from starting. |
| `encryptionScheme: hpke` | `publicKey` is a raw 32-byte X25519 public key, hex-encoded (64 characters). See [Generating a backup key pair](#generating-a-backup-key-pair). |
| `encryptionScheme: rsa_oaep_sha256_chacha20_poly1305` | `publicKey` is an RSA public key of at least 3072 bits, as SPKI (`BEGIN PUBLIC KEY`) or PKCS#1 (`BEGIN RSA PUBLIC KEY`) PEM. |

Set the **same list on every cluster**. Each cluster's Policy Server keeps its own copy of the entries, and all three must agree.

### How entry changes apply

The Policy Server writer applies the list when it starts. A `helm upgrade` that changes the list restarts the writer and applies the change.

* **Add an entry** to make it available for assignment. No vault uses it until someone assigns it.
* **Change a key in an existing entry** to rotate a recipient. Vaults keep their assignment, and future exports encrypt to the new key. Existing archives stay encrypted to the old key, so keep its private half for as long as you keep those archives.
* **Remove an entry** only when no vault should use it. Its vaults and `offchain`, if assigned, become unconfigured, and exports that include them fail until someone assigns another entry. Adding the entry back later does not restore those assignments.

Only public keys go into Helm values, so the list is safe to keep in Git. Keep each private key offline, separate from the clusters and from the exported archives.

Keep older archives and their corresponding private recovery keys when changing assignments. Verify format 3 archives for expected scopes, parties, shares, and empty `error_logs` before marking an export complete.

## Per-shard backup public keys (Crypto Vault 0.6 and earlier)

Chart `0.1.23` and earlier, which deploy Crypto Vault 0.6 or earlier, configure one backup encryptor public key per Key Store shard (`mpcKeyStores[].backupEncryptorPublicKey`). Every backup encrypts each shard's share to that shard's key. Key creation and import are blocked until every shard has a backup public key configured.

From Crypto Vault 0.7, the Key Store no longer reads `mpcKeyStores[].backupEncryptorPublicKey`, and key creation does not depend on it. Carry these keys into a `policyServer.backupEncryptionKeys` entry when you upgrade so existing recovery keys stay in use. See [Upgrade to Crypto Vault 0.7](/deploy/operations/upgrades-monitoring#upgrade-to-crypto-vault-07).

:::note\[When it must be set (0.6 and earlier)]
The public key must be present before the first key is created or imported. This is a hard system gate: key creation and import fail until every shard has one. You can set it at install time (recommended) or add it later by updating the Helm value. Because the Key Store reads it from config at startup, adding or changing it takes effect only after a pod restart (`kubectl rollout restart`).
:::

## Generating a backup key pair

HPKE recipients use **HPKE** ([RFC 9180](https://www.rfc-editor.org/rfc/rfc9180)) with the **X25519** KEM, HKDF-SHA256, and ChaCha20-Poly1305. The public key, whether for an `hpke` entry key or a 0.6 `mpcKeyStores[].backupEncryptorPublicKey`, is the **raw 32-byte X25519 public key, lowercase-hex-encoded (64 characters)**, with no PEM wrapper, no base64, and no `0x` prefix. Sodot does not ship a tool for this, so generate the key pair yourself with standard tooling.

:::warning\[Generate offline, one unique pair per shard]
Generate each key pair on a host **isolated from the cluster**, ideally an offline or air-gapped machine or an HSM. Give **every Key Store shard its own unique key pair**, and never reuse one key across shards, or a single node could decrypt the others' share backups. The **private key never enters the cluster**: keep it offline, geographically separated, under the same controls as your most sensitive key material. It is only needed during recovery.
:::

Only the **public key** goes into Helm values. Because it is public, it is safe to commit to Git or GitOps. Unlike the sensitive values you source from Secrets Manager, it needs no `existingSecret` indirection.

### Using OpenSSL

```bash
# 1. Generate the X25519 private key. Keep this file OFFLINE (it is the recovery key).
openssl genpkey -algorithm X25519 -out shard-0-backup.key

# 2. Derive the raw 32-byte public key as hex. This is the Helm value.
openssl pkey -in shard-0-backup.key -pubout -outform DER | tail -c 32 | xxd -p -c 64
# -> e.g. 3f1c9a...  (64 hex characters)
```

### Using Python

```python
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives import serialization

priv = X25519PrivateKey.generate()

# Public key -> the 64-char hex string for Helm values.
pub_hex = priv.public_key().public_bytes(
    serialization.Encoding.Raw, serialization.PublicFormat.Raw
).hex()

# Private key -> store OFFLINE (recovery key).
priv_hex = priv.private_bytes(
    serialization.Encoding.Raw,
    serialization.PrivateFormat.Raw,
    serialization.NoEncryption(),
).hex()

print("public  (Helm value):   ", pub_hex)
print("private (keep offline): ", priv_hex)
```

Repeat once per shard, and once per entry. Never reuse one key pair across shards, or a single recipient could decrypt more than one share of the same key.

### RSA recipients

For an `rsa_oaep_sha256_chacha20_poly1305` entry key, generate an RSA key pair of at least 3072 bits and use the PEM public key as `publicKey`:

```bash
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out shard-0-backup-rsa.key
openssl pkey -in shard-0-backup-rsa.key -pubout -out shard-0-backup-rsa.pub.pem
```

Backup keys are independent of `mpcKeyStores[].importedKeyEncryptorPem`, which is a separate **RSA private** key used only for unwrapping externally imported keys. Different purpose, configured separately.

## How backup works

When a user exports a backup, each Key Store shard encrypts its shares of the selected vaults and offchain keys to its recipient in the assigned entry. The archive can only be decrypted with the corresponding private keys, without the shards' operational credentials. Store exported archives in an immutable location, such as an encrypted S3 bucket with Object Lock or a Cloud Storage bucket with Bucket Lock.

## GCP infrastructure recovery safeguards

The GCP Terraform modules enable production-safe Cloud SQL recovery controls by default:

* `db_backup_enabled = true` enables daily automated backups and point-in-time recovery, with seven
  retained backups and seven days of transaction logs by default.
* `db_final_backup_enabled = true` creates a final backup when an instance is deleted.
* `db_final_backup_retention_days = 30` retains that final backup for 30 days.
* `db_retain_backups_on_delete = true` keeps automated and on-demand backups after deletion.
* `db_deletion_protection = true`, `gke_deletion_protection = true`, and
  `kms_deletion_protection = true` prevent accidental teardown in the per-party building block.

These database backups complement the encrypted Key Store share export and offline backup private
key. They do not replace either one.

The supported GCP recovery boundary is a Cloud SQL backup or point-in-time restore within the same
party project and the same region as the original deployment, while the original application Cloud KMS
key remains available. A
restored database whose stored CryptoKey differs from `gcp.kmsKey`, or whose original key is no longer
available, fails closed. Cross-project and cross-region recovery require a separate key-access and
rewrap design and are outside this procedure.

:::warning\[Choose the KMS protection level before first apply]
The production default `kms_key_protection_level = "HSM"` stores key material in Cloud HSM. Changing
the protection level on an existing deployment replaces the CryptoKey. Shares wrapped under the old
key then become unrecoverable. Choose the protection level before the first apply, and keep KMS
deletion protection enabled for long-lived deployments.
:::

## Operational guidance

**Verifying backup configuration at install time.** After installation or an upgrade, list the entries with `GET /v1/vault-backup-encryption-keys` and confirm each entry's `public_keys` match the keys you intended for each party. Then confirm that every vault and `offchain` has the intended assignment. For Crypto Vault 0.6 and earlier, confirm instead that each shard's backup encryptor public key is present, because key creation fails until one is configured.

**Rotating backup keys.** Change the key in the entry, or add a new entry and reassign vaults to it. Only future exports use the new key; export again after the change and keep the old private key for the archives encrypted to it.

**Storing the backup private key.** The backup private key has no role in normal vault operation. We recommend keeping it in offline, geographically separate storage, with the same physical and procedural controls you apply to your most sensitive key material.
