> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Encryption Keys

> Manage encryption keys

# Overview

Encryption keys let you encrypt sensitive fields in logs and sessions before they reach Formal's servers. You decrypt them on demand from your browser.

Encrypted fields are stored as JWE (JSON Web Encryption) instead of plaintext. Refer to the [log schemas documentation](https://docs.formal.ai/docs/guides/observability/log-schemas.md#supported-paths) to learn which fields can be encrypted.

> [!NOTE]
>
> Log encryption keys are separate from [Token Encryption](https://docs.formal.ai/docs/guides/core-concepts/connectors/token_encryption.md). Use token encryption (and the HTTP `encrypt` / `decrypt` policy actions) to seal OAuth tokens in the data path. See [Encrypt MCP OAuth Tokens](https://docs.formal.ai/docs/guides/example-use-cases/encrypt-mcp-oauth-tokens.md).

# Configuration via Terraform

Encryption keys are managed through the `formal_encryption_key` resource. AWS KMS, GCP KMS, and Azure Key Vault are supported, all using an asymmetric RSA key. The encrypting client (Connector or Formal Endpoint) holds only the public key and needs no cloud credentials, so only the decryptor can recover the plaintext.

> [tab] AWS KMS
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> provider "aws" {
>   region = "us-east-1"
> }
>
> resource "aws_kms_key" "formal_logs_key" {
>   description              = "Asymmetric key for Formal log encryption"
>   customer_master_key_spec = "RSA_4096"
>   key_usage                = "ENCRYPT_DECRYPT"
> }
>
> data "aws_kms_public_key" "formal_logs_key" {
>   key_id = aws_kms_key.formal_logs_key.id
> }
>
> resource "formal_encryption_key" "logs_key" {
>   key_provider   = "aws-kms"
>   key_id         = aws_kms_key.formal_logs_key.arn
>   public_key_pem = data.aws_kms_public_key.formal_logs_key.public_key_pem
>   # Optional: an HTTPS endpoint the browser calls to decrypt individual fields.
>   # decryptor_uri = "<HTTPS endpoint to perform decryption>"
> }
> ```
>
> The region is derived from the key ARN, so pass the full ARN in `key_id`.

> [tab] GCP KMS
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> provider "google" {
>   project = "my-project"
> }
>
> resource "google_kms_key_ring" "formal" {
>   name     = "formal-logs"
>   location = "us-east1"
> }
>
> resource "google_kms_crypto_key" "formal_logs_key" {
>   name     = "formal-logs-key"
>   key_ring = google_kms_key_ring.formal.id
>   purpose  = "ASYMMETRIC_DECRYPT"
>
>   version_template {
>     algorithm = "RSA_DECRYPT_OAEP_4096_SHA256"
>   }
> }
>
> data "google_kms_crypto_key_version" "formal_logs_key" {
>   crypto_key = google_kms_crypto_key.formal_logs_key.id
> }
>
> resource "formal_encryption_key" "logs_key" {
>   key_provider   = "gcp-kms"
>   key_id         = data.google_kms_crypto_key_version.formal_logs_key.name
>   public_key_pem = data.google_kms_crypto_key_version.formal_logs_key.public_key[0].pem
>   # Optional: an HTTPS endpoint the browser calls to decrypt individual fields.
>   # decryptor_uri = "<HTTPS endpoint to perform decryption>"
> }
> ```
>
> For GCP, pass the crypto key version resource name (`projects/.../cryptoKeyVersions/N`) in `key_id`.

> [tab] Azure Key Vault
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "azurerm_key_vault_key" "formal_logs_key" {
>   name         = "formal-logs-key"
>   key_vault_id = azurerm_key_vault.example.id
>   key_type     = "RSA"
>   key_size     = 4096
>
>   key_opts = [
>     "decrypt",
>     "encrypt",
>   ]
> }
>
> resource "formal_encryption_key" "logs_key" {
>   key_provider   = "azure-key-vault"
>   key_id         = azurerm_key_vault_key.formal_logs_key.id
>   public_key_pem = azurerm_key_vault_key.formal_logs_key.public_key_pem
> }
> ```
>
> Use an RSA key with ENCRYPT/DECRYPT and RSA-OAEP-256. `key_id` is the Key Vault key URI, either versioned (`https://{vault}.vault.azure.net/keys/{name}/{version}`) or versionless (`https://{vault}.vault.azure.net/keys/{name}`, which resolves to the key's current version).
>
> Prefer the versioned URI. A versionless one stops decrypting fields sealed before a key rotation, because the wrapped content key only opens with the version that was current when the field was sealed. With the versioned URI, rotating the key registers a new encryption key and leaves older fields readable.

> [!NOTE]
>
> Symmetric and deterministic algorithms (`aes_random`, `aes_deterministic`) are deprecated and can no longer be registered. Encryption keys use asymmetric RSA (`rsaes_oaep_sha256`), which both the Connector and Formal Endpoint can use without cloud credentials.

# The Decryptor

Formal cannot decrypt these fields. When you set a `decryptor_uri`, the client-side JavaScript of the Control Plane calls it when a user decrypts a field. The endpoint must support CORS and accept POST requests with the encrypted payload in the body. Use [this decryptor reference implementation](https://github.com/formalco/decryptor), which unwraps AWS KMS, GCP KMS, and Azure Key Vault keys and ships Terraform examples for Lambda, Cloud Run, and Container Apps.

## Password protection

The reference decryptor can require a shared password on every decrypt request. Set `DECRYPTOR_PASSWORD_SHA256` to the hex-encoded SHA-256 of the password, not the password itself:

```sh theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
printf %s 'your-password' | shasum -a 256 | cut -d' ' -f1
```

The Terraform examples expose this as the `decryptor_password_sha256` variable. The first time a decrypt is rejected, the Control Plane asks the user for the password. It then sends the password in the `X-Decryptor-Password` header and keeps it in memory for the rest of the browser session. If you put your own CORS layer or proxy in front of the decryptor, allow that header.

Without the variable, anyone who can reach the endpoint can decrypt.

> [!WARNING]
>
> The password is shared by every user, not tied to individual users. Deploy the decryptor behind a VPN or in a private network as well.


## Related topics

- [Create encryption key](https://docs.formal.ai/api-reference/corev1logsservice/create-encryption-key.md)
- [Delete encryption key](https://docs.formal.ai/api-reference/corev1logsservice/delete-encryption-key.md)
- [Update encryption key](https://docs.formal.ai/api-reference/corev1logsservice/update-encryption-key.md)
- [Get encryption key](https://docs.formal.ai/api-reference/corev1logsservice/get-encryption-key.md)
- [List encryption keys](https://docs.formal.ai/api-reference/corev1logsservice/list-encryption-keys.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.