# Your own encryption key (BYOK)
Source: https://sovaria.in/docs/encryption-keys

Encrypt your organization's storage files and database backups with a key you hold in AWS KMS. Revoke it and that data can no longer be read — by anyone, Sovaria included.

On Enterprise, an organization can bring its own **AWS KMS key**. Sovaria then encrypts the organization's storage files and database backups under that key, and can only use it with your organization's encryption context — so no other organization's requests can. You stay in control in AWS: every use of the key shows in your CloudTrail, and disabling or revoking it makes that data unreadable.

## How it works

(Diagram: Where your key is used — Your key never leaves AWS KMS. Sovaria asks KMS to wrap and unwrap, always with your organization's encryption context.)

- **Files** are written with S3 server-side encryption under your key (SSE-KMS); reading one needs your key.
- **Backups and pause archives** are encrypted with AES-256-GCM using an organization data key. Sovaria stores that data key only wrapped by your KMS key and keeps the unwrapped copy in memory for a few minutes at most.
- Every file and backup is bound to your organization: the encryption context `sovaria:org_id` must match, and the key policy only allows that context.

### What it covers — and what it doesn't

| Data | Under your key? | How it is protected |
|---|---|---|
| Storage files | Yes | S3 SSE-KMS with your key. Files from before you added the key stay under Sovaria's key until you re-encrypt them. |
| Database backups and pause archives | Yes | AES-256-GCM with the organization data key, wrapped by your key. |
| Database files of your running Postgres | No | Encrypted at rest by the server's disk encryption (AWS-managed key). Dedicated Enterprise instances can use your key for the volume — ask your account team. |
| Function secrets, project secrets, database passwords | No | Encrypted with Sovaria's keys (AES-256-GCM / Fernet), never stored in plain text. |

## Set it up

1. **Create a key in AWS KMS** — A **symmetric** key (key spec `SYMMETRIC_DEFAULT`, usage `ENCRYPT_DECRYPT`) in any region and account. Turn on automatic key rotation if you want it — that is transparent to Sovaria.
2. **Add Sovaria to the key policy** — Open **Organization settings › Security › Your own encryption key**, click **Show key policy statement**, and add that statement to the key's policy. It names Sovaria's AWS role and only allows your organization's encryption context. Optionally also allow `kms:DescribeKey` (without the condition) so Sovaria can show the key's state.
3. **Paste the key ARN** — Paste `arn:aws:kms:<region>:<account>:key/<id>` and confirm. Sovaria checks it end to end first — wraps and unwraps a data key, and writes and reads a file under it. Nothing changes if any check fails.
4. **Re-encrypt existing files** — New files and backups use your key immediately. Click **Re-encrypt files** to rewrite files stored before; progress shows on the card.

Key policy statement:
```json
{
  "Sid": "SovariaOrg<your-org-id>",
  "Effect": "Allow",
  "Principal": { "AWS": "<Sovaria's role ARN, shown in the console>" },
  "Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey"],
  "Resource": "*",
  "Condition": { "StringEquals": { "kms:EncryptionContext:sovaria:org_id": "<your-org-id>" } }
}
```

CLI:
```bash
sov org key status --policy          # the statement to add, with your org id filled in
sov org key set arn:aws:kms:ap-south-1:123456789012:key/1f2e3d4c-...
sov org key check                    # exit code 1 if Sovaria can't use the key now
sov org key reencrypt                # rewrite existing files under your key
sov org key rotate                   # a new data key version
```

JavaScript:
```ts
const status = await sovaria.api.byok.get()
console.log(status.setup.key_policy_statement)

await sovaria.api.byok.set({ provider: 'aws-kms', keyRef: 'arn:aws:kms:ap-south-1:123456789012:key/1f2e3d4c-...' })
await sovaria.api.byok.reencryptStorage()
```

HTTP:
```http
GET  /api/v1/byok                         # status, coverage, setup
PUT  /api/v1/byok                         { "provider": "aws-kms", "key_ref": "arn:aws:kms:..." }
POST /api/v1/byok/check
POST /api/v1/byok/rotate
POST /api/v1/byok/reencrypt-storage
X-Sovaria-Org-ID: <org-id>
```

Only organization **owners** can set, rotate or re-encrypt; owners and admins can view the status and run a check. App users and API keys of your projects can't reach these endpoints.

## Disabling or revoking the key

Disable the key, schedule its deletion, or remove the policy statement in AWS and Sovaria can no longer use it — within a few minutes, once its in-memory copy of the data key expires. From then on:

- Uploading, downloading and copying files in the organization's projects fail with `403 ENCRYPTION_KEY_UNAVAILABLE`.
- New backups can't be taken and existing ones can't be restored.
- Your databases, functions and auth keep running — they don't depend on the key.
- The console card shows **Not usable** with the reason from KMS (for example `KEY_DISABLED` or `KEY_ACCESS_DENIED`).

Re-enable the key or restore the statement and click **Check now**; everything works again. If the key is deleted in AWS, data under it is gone for good — Sovaria can't recover it.

## Rotation and changing keys

- **AWS automatic rotation** needs nothing from you or Sovaria; KMS keeps old key material for decryption.
- **Rotate data key** makes a new organization data key version for new backups. Older versions stay, wrapped by your key, to decrypt older backups.
- **Use a different key** (a new ARN): new files and backups go under the new key. Keep the old key enabled until you have re-encrypted files and no longer need older backups — they still need it.

## Signed upload URLs

For organizations with their own key, signed upload URLs always point at `api.sovaria.in/api/v1/store/signed/…` instead of directly at S3 (even when you pass a file size), so every write is encrypted under your key. The URLs work the same way for your app; download URLs are unchanged.

## Errors

| Code | Meaning |
|---|---|
| `PLAN_FEATURE_REQUIRED` | Your plan doesn't include your own key (Enterprise only). |
| `KEY_REF_INVALID` | Not a KMS key ARN. Use the key's full ARN, `arn:aws:kms:<region>:<account>:key/<id>` — an alias isn't accepted. |
| `KEY_ACCESS_DENIED` | The key policy doesn't let Sovaria use the key with your encryption context — add the statement. |
| `KEY_DISABLED` / `KEY_NOT_FOUND` | The key is disabled, pending deletion or doesn't exist. |
| `STORAGE_KEY_DENIED` | KMS allowed Sovaria but S3 couldn't use the key — usually a key in a different region without a multi-region replica, or a policy that excludes S3. |
| `ENCRYPTION_KEY_UNAVAILABLE` | A storage or backup call was refused because the key isn't usable right now. |

## What Sovaria staff see

Staff see which organizations use their own key, the key ARN, its data key version and whether it was usable at the last check (**Control Center › Compliance › Customer keys**). They can ask KMS to check it again — audited with a reason — but can't change, rotate or remove your key, and never see key material.

## Plans

|  | Free | Pro | Ultra | Team | Enterprise |
|---|---|---|---|---|---|
| Bring your own key (your KMS key encrypts files and backups) | — | — | — | — | ✓ |
