# Encryption

> SSE-C and static-key SSE-S3, and what they protect.

---

LLMS index: [llms.txt](/llms.txt)

---

This tutorial configures static-key `SSE-S3` (`AES256`) on a gateway and bucket, then runs a round-trip write and read with a client-supplied `SSE-C` customer key. Complete [Install](/docs/s3/operations/install/) and [Use an S3 SDK](/docs/s3/tutorial/sdk/) first.

## Configure static-key SSE-S3 {#static-key-sse-s3}

Generate a 32-byte key file once on the gateway host (refusing to overwrite an existing key file):

```bash
sudo sh -c 'set -eu; umask 027; test ! -e /etc/silo/kms.key; set -C; { printf "silo-key-1:"; openssl rand -base64 32; } > /etc/silo/kms.key; chown root:silo /etc/silo/kms.key; chmod 0640 /etc/silo/kms.key'
```

Back up the exact key ID (`silo-key-1`) and raw key bytes outside the database, and copy the identical `/etc/silo/kms.key` file to every gateway that connects to this PostgreSQL database.

Add `MINIO_KMS_SECRET_KEY_FILE` to `/etc/default/silo` (do not set `MINIO_KMS_SECRET_KEY` at the same time):

```ini
MINIO_KMS_SECRET_KEY_FILE="/etc/silo/kms.key"
```

Restart the gateway, enable default `AES256` encryption on a new `encrypted-demo` bucket, and verify an encrypted upload and download:

```bash
sudo systemctl restart silo
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api create-bucket --bucket encrypted-demo
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-bucket-encryption \
  --bucket encrypted-demo \
  --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}'
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-object \
  --bucket encrypted-demo --key hello.txt --body "$SILO_TUTORIAL_DIR/hello.txt"
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api get-object \
  --bucket encrypted-demo --key hello.txt "$SILO_TUTORIAL_DIR/encrypted.download.txt"
cmp "$SILO_TUTORIAL_DIR/hello.txt" "$SILO_TUTORIAL_DIR/encrypted.download.txt"
```

If the static key is missing or wrong, encryption and decryption requests fail closed; the gateway never falls back to plaintext. Losing the external key makes encrypted objects in PostgreSQL unrecoverable. See [Backup and restore](/docs/s3/operations/backup/).

External KMS integration and `SSE-KMS` (`aws:kms`) are not supported. The `silo_api` SQL interface cannot decrypt `SSE-S3` or `SSE-C` bodies, and buckets with default encryption reject SQL `put_object` writes.

## Use a client-held SSE-C key {#sse-c}

With `SSE-C`, the client supplies the 32-byte key on every write and read over HTTPS. Save the following script as `~/.local/share/silo-tutorial/sse-c.py`:

```python
import base64
import hashlib
import os
from pathlib import Path

import boto3
from botocore.config import Config

key_path = Path.home() / ".local/share/silo-tutorial/sse-c.key"
key_path.parent.mkdir(parents=True, exist_ok=True)
if key_path.exists():
    key = key_path.read_bytes()
else:
    key = os.urandom(32)
    descriptor = os.open(key_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
    with os.fdopen(descriptor, "wb") as output:
        output.write(key)
if len(key) != 32:
    raise ValueError("SSE-C key must contain exactly 32 bytes")

session = boto3.Session(profile_name="silo-admin", region_name="us-east-1")
s3 = session.client(
    "s3",
    endpoint_url=os.environ["SILO_ENDPOINT"],
    verify=os.environ["AWS_CA_BUNDLE"],
    config=Config(signature_version="s3v4", s3={"addressing_style": "path"}),
)
customer_headers = {
    "SSECustomerAlgorithm": "AES256",
    "SSECustomerKey": base64.b64encode(key).decode("ascii"),
    "SSECustomerKeyMD5": base64.b64encode(
        hashlib.md5(key, usedforsecurity=False).digest()
    ).decode("ascii"),
}
s3.create_bucket(Bucket="customer-key-demo")
s3.put_object(
    Bucket="customer-key-demo",
    Key="private.txt",
    Body=b"protected by a client-held key\n",
    **customer_headers,
)
response = s3.get_object(
    Bucket="customer-key-demo", Key="private.txt", **customer_headers
)
try:
    assert response["Body"].read() == b"protected by a client-held key\n"
    print("SSE-C round trip succeeded")
finally:
    response["Body"].close()
```

Run `"$HOME/.local/share/silo-tutorial/venv/bin/python" "$HOME/.local/share/silo-tutorial/sse-c.py"`. It prints `SSE-C round trip succeeded`.

`SSE-C` requests require verified HTTPS; HTTP `SSE-C` requests are rejected. Back up the 32 bytes in `sse-c.key` independently — the gateway's static `SSE-S3` key cannot decrypt `SSE-C` objects.

---

Backlinks:

- [Configuration](/docs/s3/reference/configuration/)
- [Tutorials](/docs/s3/tutorial/)
