# Cryptography

**Last updated:** 2026-11-27T14:16:11-03:00 (UTC-3)

[Security and trust](/docs/security/trust/) · [Data inventory](/docs/security/data-inventory/) · [Key rotation](/docs/features/key-rotation/)

Where m7gtw uses cryptography, with which algorithm and where each key comes from. Checked against the code on 2026-10-27. All primitives come from the Go standard library (`crypto/*`), with no custom implementation.

## Operator keys

The gateway neither generates nor stores the master keys: the operator supplies them through an environment variable, a file (`<NAME>_FILE`) or a vault.

| Key | Configuration | Format | What it protects |
|---|---|---|---|
| Providers key | `storage.provider_encryption_key_env` (`GATEWAY_PROVIDER_ENCRYPTION_KEY`), optional `storage.provider_encryption_previous_key_env` | 32 bytes in hex | Provider, MCP and alert channel secrets, and eval captures |
| Content key | `storage.content_encryption_key_env` (`GATEWAY_CONTENT_ENCRYPTION_KEY`), optional `storage.content_encryption_previous_key_env` for reads during a rotation | 32 bytes in hex | The data keys of [content captures](/docs/features/content-capture/) |
| Session key | `admin.session_key_env` (`GATEWAY_UI_SESSION_KEY`), optional paired `session_previous_key_env` and absolute `session_previous_key_valid_until` | 32 bytes or more | Console session and derived keys (below) |
| Cache key | `cache_backend` (`key_env` or managed secret) | 32 bytes in base64 | Exact cache content in Redis or Memcached |

## Uses

| Use | Algorithm | Key |
|---|---|---|
| Provider, MCP and alert secrets in PostgreSQL | AES-256-GCM | Providers key |
| Eval captures | AES-256-GCM, with the `request_id` as associated data | Providers key |
| Content captures | Envelope: a random 256-bit data key per record seals the content with AES-256-GCM; the content key seals that data key with AES-256-GCM. Both bind the record's ID, request ID, team, key, model, operation, status, sides, sizes, truncation, `output_blocked`, time and expiry as associated data, and the wrapped data key also binds the key reference. A record written with `content_capture_evaluation` binds, under a second label, its request view, its purposes, the size of its sent view and the end of its evaluation purpose too, so a stored record cannot be given another purpose or a longer evaluation life. The record keeps a 16-character key reference (HMAC-SHA256 of a fixed label under the key). A captured image or audio (`content_capture_media`) is sealed with AES-256-GCM under its record's data key, with its own nonce and associated data that bind its ID, its record, request, team, source, kind, media type and size, so an attachment cannot be moved to another record or relabelled; it has no wrapped key of its own, so a content key rotation rewraps one key per record. Its SHA-256 is kept inside the encrypted record, not in a clear column | Content key |
| Semantic cache in PostgreSQL | AES-256-GCM | SHA-256 of label + session key |
| Exact cache in Redis or Memcached | AES-256-GCM and HMAC-SHA256 on the keys | HMAC-SHA256 of the cache key with one label per use |
| Semantic cache in Redis (`distributed_semantic_cache`) | AES-256-GCM with the key name as associated data, HMAC-SHA256 on the keys and the buckets | HMAC-SHA256 of the cache key with its own labels; each key name gets its own AES subkey, so random nonces never accumulate on one key |
| OIDC login state (cookie) | AES-256-GCM | SHA-256 of label + session key |
| Console session cookie | HMAC-SHA256 | Session key |
| Responses and Conversations IDs | HMAC-SHA256 | SHA-256 of label + session key |
| Realtime ephemeral tokens | HMAC-SHA256 | SHA-256 of label + session key |
| Alert webhook signature | HMAC-SHA256 over `timestamp.body` | Channel secret |
| Client keys, console tokens and invitations | SHA-256 of the token; lookup is by hash, and the bootstrap token is compared in constant time | The token comes from `crypto/rand` and exists only with whoever received it |
| Requests to AWS (Bedrock, SageMaker, vaults) | SigV4, HMAC-SHA256 | Operator's AWS credential |
| Requests to OCI | RSA PKCS#1 v1.5 signature with SHA-256 | OCI API key, which never leaves the file |
| Inference JWT and OIDC logout | RS256 and ES256 verification against the issuer's JWKS | IdP public keys |

Each derivation uses its own label (`gateway-oidc-state:`, `gateway-semantic-cache:`, `gateway-response-id:`, `gateway-realtime-ephemeral:`). That is why a key derived for one use is not valid for another.

## Transport

- **TLS 1.2 at minimum** on the connections the gateway opens: PostgreSQL, Redis, SMTP, vaults and configured proxies ([network](/docs/operate/network-configuration/)).
- Providers and MCP use HTTPS. HTTP without TLS is only accepted for local targets.
- Inbound TLS is handled by the operator's ingress or load balancer, as in [operations](/docs/operate/operations/).

## Rotation

- **Client keys:** rotation with overlap, without downtime ([rotation](/docs/features/key-rotation/)).
- **Session key:** direct replacement invalidates old signatures. The default-off `session_key_rotation` profile permits one previous verifier before an absolute cutoff, retaining original expiry and live authority checks. Current alone signs. Durable Responses/Conversations IDs have no expiry and become unusable when their previous key is retired. The PostgreSQL semantic cache deliberately starts cold. See [overlap and retirement](/docs/security/keyring-rotation/#session-overlap-and-retirement).
- **Providers key:** use the explicit `provider_key_rotation` profile and offline `gateway reencrypt` check/apply procedure. One previous reader preserves access while all twelve ciphertext columns are migrated atomically; keep matching keys with the backup/recovery policy. No automatic rewrite runs at serving boot. See [maintenance and recovery](/docs/security/keyring-rotation/#provider-key-maintenance).
- **Cache key:** rotating it invalidates the exact cache, and the semantic cache in Redis, which fill up again.
- **Content key:** set the new key as current and the old one as `content_encryption_previous_key_env`; once the longest capture retention has passed, remove the old one ([steps](/docs/features/content-capture/#rotating-the-content-key)). There is no rewrap tool yet. Destroying the content key makes every capture unreadable.

## Limits

- Derived keys use SHA-256 of label + secret, not HKDF. It works to separate uses, but it is not the standard construction.
- Without a configured session key, Responses IDs and ephemeral tokens use a random per-process key: they are valid only on that replica and until a restart.
- Provider re-encryption is deliberate offline maintenance, not automatic online key-epoch enforcement. A scan certifies its database snapshot, not the absence of future old writers.
