Rivergate

Keys and encryption

Every secret value is encrypted before it reaches PostgreSQL, under a layered key hierarchy that keeps tenants cryptographically separate.

Envelope encryption

Each secret version gets its own random 32-byte data key (DEK). The value is encrypted with AES-256-GCM under that DEK, and the DEK is itself encrypted — “wrapped” — under a project key. No DEK is ever reused across versions.

Root key              versioned key files, held outside the database
└── Organization key  one per organization (KEK)
    └── Project key   one per project
        └── Data key  one per secret version
            └── AES-256-GCM ciphertext

Only wrapped keys are stored in PostgreSQL. The root key is never stored there, or in Redis, an image, or a plain configuration value.

Writing a version

  1. Authorize the caller and resolve the organization, project, environment and secret.
  2. Generate a fresh DEK and nonce from a cryptographically secure random source.
  3. Encrypt the value with AES-256-GCM, binding it to its context (see below).
  4. Wrap the DEK under the active project key.
  5. Persist the ciphertext, nonce, wrapped DEK and metadata, and the audit event, in one transaction.

Reading a version

  1. Authorize the caller for value access.
  2. Unwrap the DEK through the hierarchy and rebuild the associated data from trusted stored identifiers.
  3. Decrypt. A tag or context mismatch fails the read.
  4. Commit the audit event, and only then return the value. See fail-closed reads.

Bound to its context

AES-GCM authenticates both the ciphertext and some associated data. Rivergate builds that data from a versioned, unambiguous binary encoding of the product marker, organization, project, environment, secret and version identifiers, and the crypto format version. The identifiers are length-prefixed rather than joined with separators.

The consequence: ciphertext copied into another tenant, secret or version fails to decrypt. A wrapped key is bound the same way, to its parent and child key IDs, scope, purpose and wrapping format.

What is stored with a version

Reads enforce exact key, nonce and tag lengths, bounded ciphertext size, and an allowlist of supported algorithms and formats.

Root-key custody

The local key provider keeps versioned root-key files in a protected directory, ROOT_KEY_DIR.

go run ./cmd/root-keygen --key-dir /path/to/root-keys --version 1 --activate
Back up the root key — separately

Without the exact root-key versions, organization and project keys cannot be unwrapped and the ciphertext is unrecoverable, even with a perfect database backup. Keep recovery copies in a different storage and access boundary from the database backups, and never in the same archive. See Backup and recovery.

File permissions do not protect against root on the host. Use host access controls and disk encryption, and never put key material in an image, plain Helm values, logs or environment dumps.

The provider interface

Key access goes through a narrow provider interface: key references, active-version lookup, and scoped wrap and unwrap. It deliberately has no generic “give me the raw root key” call. The audit signing key is separate and uses its own interface and policy.

The local provider is the one available today. Adapters for external KMS or HSM services are not provided.

Audit signing key

Audit checkpoints are signed with a distinct Ed25519 key that is never derived from, or shared with, any data-encryption key. It is mounted only into the worker. See Audit log.

Other notes