Skip to content

feat(fips): encrypt stored secrets with the FIPS provider's AES-256-GCM in fips_mode - #51429

Merged
yucheng-berri merged 10 commits into
mainfrom
litellm_fips_at_rest_aes_gcm
Nov 18, 2026
Merged

yucheng-berri merged 10 commits into
mainfrom
litellm_fips_at_rest_aes_gcm

Conversation

@yucheng-berri

@yucheng-berri yucheng-berri commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

<make-me-human-readable>
With general_settings.fips_mode: true, the proxy still encrypted every stored secret (model keys, credentials, MCP tokens) with XSalsa20-Poly1305 from libsodium, which is not a FIPS algorithm and runs outside the validated OpenSSL module. In fips_mode those secrets are now written with AES-256-GCM from the OpenSSL FIPS provider, under a key the provider derives from LITELLM_SALT_KEY, and values in any other format are refused on read. Without fips_mode nothing changes
</make-me-human-readable>

Details

TLDR

Builds on #52099, now merged. This PR's at-rest key comes from the salt key check that #52099 added.

Problem this solves:

  • encrypt_value_helper defaults to PyNaCl SecretBox, and fips_mode did not change that. On the Chainguard FIPS image a fips_mode proxy stored secrets with libsodium and said nothing
  • The existing opt-in, encryption_algorithm: aes-256-gcm, is not an approved service on that module either: the app passes its own nonce (os.urandom), which the module's security policy lists as "AES-GCM encryption with external IV: Non-Approved", and the key is a bare SHA-256 of the salt key

How it solves it:

  • In fips_mode the write algorithm is always AES-256-GCM, whatever encryption_algorithm says
  • The nonce is drawn by the FIPS provider itself (EVP_EncryptInit_ex2 with no IV), as FIPS 140-3 IG C.H asks. cryptography's AESGCM always takes a caller nonce, so this one call goes to libcrypto through ctypes, the same way derive_key_from_salt_key already reaches the provider's KBKDF
  • The key comes from that existing KBKDF helper (SP 800-108, HMAC-SHA256) under its own label, over the UTF-8 bytes of the salt key, the same bytes feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 checks at startup
  • The stored format is unchanged: v2:gcm: + base64url(nonce || ciphertext || tag). Reads use cryptography's AESGCM on the FIPS provider
  • In fips_mode a value without the v2:gcm: prefix is refused, so libsodium is never called

User Flow

Before: a fips_mode proxy stores a model key with libsodium

  1. The proxy admin runs the FIPS image with general_settings.fips_mode: true
  2. The admin sends POST https://litellm-domain/model/new with an api_key
  3. The proxy encrypts the key with XSalsa20-Poly1305 from the libsodium bundled in PyNaCl
  4. The row holds a value with no prefix, and no call reached the FIPS module

After: the same request stores the key with the FIPS provider

  1. Same image, same config, same request
  2. The proxy derives the key with the provider's KBKDF and encrypts with the provider's AES-256-GCM, which draws the nonce
  3. The row holds v2:gcm:..., and libsodium is never loaded

Linear ticket

Resolves LIT-8427

Behavior changes

Removed / renamed

  • None. _KBKDF_PROPERTIES (private) is now _FIPS_PROPERTIES, since the cipher fetch uses it too

Silent regressions to watch

  • None without fips_mode. The default write is still PyNaCl, encryption_algorithm: aes-256-gcm still writes the same v2:gcm: values with the same key, and both are read as before. A live base-versus-head run on one Postgres confirmed each side reads the other's rows
  • With fips_mode: true:
    • Stored secrets are encrypted under a key derived from LITELLM_SALT_KEY. feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 already refuses to start a fips_mode proxy without a LITELLM_SALT_KEY of at least 112 bits, and the master key never stands in for it, so this PR adds no new startup rule. A write that still cannot be encrypted, such as one under a too-short new_encryption_key, raises: POST /credentials returns 500 with the message and POST /model/new returns 500 Failed to add model to db. Main stored those values with libsodium
    • Team and key callback settings, guardrail params and search tool params used to be stored as written when encryption failed. In fips_mode that write now raises, so a secret is never stored in plaintext because encryption failed
    • encryption_algorithm: xsalsa20-poly1305 is ignored
    • Values written outside fips_mode (PyNaCl values, and v2:gcm: values written in standard mode) are not decrypted. The models and credentials behind them do not load, and the log says why. Main read them
    • Values written in fips_mode cannot be read after fips_mode is turned off, because the key differs
    • Tokens that go through the same helper change shape the way they already do under encryption_algorithm: aes-256-gcm: a response id becomes resp_v2:gcm:...
    • fips_mode is only in v1.110.0 dev and rc tags, no stable release

Migration for existing customers

  • None for deployments that leave fips_mode off
  • fips_mode is for new deployments. A database with secrets written before fips_mode was on has to be re-created or have those secrets entered again

Regression tests

  • tests/unit/proxy/common_utils/test_encrypt_decrypt_utils.py
    • test_fips_mode_writes_aes_gcm_under_the_kbkdf_key_with_a_nonce_from_openssl: the value opens with AES-256-GCM under the SP 800-108 key computed by hand, two writes have different nonces, and an xsalsa20-poly1305 setting does not change it
    • test_fips_mode_refuses_values_it_did_not_write
    • test_fips_mode_encrypts_under_an_explicit_new_key
    • test_fips_mode_refuses_to_encrypt_under_a_salt_key_under_112_bits
    • test_fips_mode_never_stores_a_secret_as_written_when_it_cannot_be_encrypted: callback vars, guardrail params and search tool params
    • test_fips_aes_gcm_refuses_a_key_that_is_not_32_bytes: the seal raises for a 0, 16, 24, 31 or 33 byte key instead of letting OpenSSL read 32 bytes from it
  • The standard-mode tests in that file are untouched and pass
  • test_fips_mode_merge_user_env_vars_takes_its_lock_without_blake2b now writes through the FIPS provider, so it is skipped on a Python that links OpenSSL statically, where ctypes cannot reach it
  • Seven mutations of the new branches each turn one of these tests red. After the rebase onto feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099, three more on the at-rest key do too: decoding a hex salt key again, a different KBKDF context, and no refusal of a short key. Dropping the 32-byte key check turns all five cases of its test red
  • At c4caf10, rebased onto feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 at 74f2d29, on Linux with a shared-library OpenSSL so the ctypes tests run: 17979 tests in tests/unit/proxy/common_utils, mcp_server, guardrails, search_endpoints, the fips_mode auth and password files, and the plugin and startup files pass. mcp_server/test_rest_endpoints.py::TestListToolsRestAPI::test_single_server_response_includes_paginated_upstream_tools and guardrail_hooks/test_cato_networks.py::test_streaming_connect_uses_verifying_context_for_ca_bundle failed under -n 12 and pass alone, and this PR does not touch either file. ruff_strict_gate, type_discipline_gate, test_quality_gate and type_check_gate pass
  • At e1110ed, the same patches rebased onto main 88da5e8 after feat(proxy)!: require a LITELLM_SALT_KEY of at least 112 bits to start in fips_mode #52099 merged, 7264 tests in test_encrypt_decrypt_utils.py, test_fips.py, test_mcp_env_vars.py, guardrails, search_endpoints, test_callback_utils.py, test_plugin_routes.py and mcp_server/outbound_credentials pass on the same Linux setup, with the ctypes tests running
  • At eb5eaba, which adds the 32-byte key check, 8950 tests in tests/unit/proxy/common_utils, test_credential_migration.py, guardrails, search_endpoints and test_mcp_env_vars.py pass on Linux, and test_encrypt_decrypt_utils.py passes with the ctypes tests running

Why not just default to the existing AES-256-GCM path

On the Chainguard module (CMVP #5132) the existing path was traced with gdb: AES-256-GCM is served by the FIPS provider, but the iv-generated indicator stays 0 because cryptography hands the nonce in, and the security policy lists that as a non-approved service. With no nonce passed, the indicator is 1. The key is the other half: SP 800-133r2 names SP 800-108 as the way to derive a key from an existing key, and a bare hash is not on the list.

Evidence

Real proxies on the real Chainguard FIPS image (Python 3.13, OpenSSL 4.0.3, Chainguard FIPS Provider 3.4.0-r5), this branch's litellm/ at 1ee6ac4, base main 52d1c4c, real Postgres, a local upstream that records the Authorization header. The commits after 1ee6ac4 change one test, make three callers re-raise in fips_mode (unit tested, not rerun live) and move where the seal failure is raised; the write and read path is the same code. The live run used a hex salt key and the key derivation from before the rebase onto #52099, which decoded hex. After the rebase the key comes from the salt key's UTF-8 bytes, checked by the unit tests against SP 800-108 computed by hand, and the live run was not repeated.

Step, fips_mode: true, hex salt key main this branch
POST /model/new, POST /credentials 200 200
Stored api_key no prefix, 24 + 16 + n bytes (SecretBox) v2:gcm: + 12 + n + 16 bytes
Same with encryption_algorithm: xsalsa20-poly1305 SecretBox v2:gcm:
After restart: GET /v1/models, chat, /v1/responses with previous_response_id, /v2/login 200 200
Key the upstream received the original the original
libsodium mapped in the proxy process yes no

gdb on encrypt_value_helper in fips_mode on this branch:

EVP_KDF_fetch(algorithm=KBKDF, properties=fips=yes) returned provider=fips ("Chainguard FIPS Provider for OpenSSL" 3.4.0-r5)
EVP_KDF_derive returned 1; fips-indicator=1
EVP_CIPHER_fetch(algorithm=AES-256-GCM, properties=fips=yes) returned provider=fips
EVP_EncryptInit_ex2(key=set, iv=NULL)
EVP_EncryptUpdate returned 1; provider=fips | iv-generated=1
EVP_EncryptFinal_ex returned 1; provider=fips | iv-generated=1
libsodium breakpoint hits: 0 (10 armed)

The read runs KBKDF again and EVP_DecryptFinal_ex returns 1 on the FIPS provider's AES-256-GCM, with 0 libsodium hits. On main the same write and read hit crypto_secretbox_easy and crypto_secretbox_open_easy and make no libcrypto call.

Other cells on the image:

Case Result on this branch
Rows written in standard mode, then fips_mode on proxy boots, login works, models list is empty, log says only v2:gcm: values can be decrypted, new writes work
fips_mode rows, then fips_mode off rows unreadable (InvalidTag), login works
Standard mode, base and head on one Postgres same layouts, each side serves the other's rows

The Models page of the fips_mode proxy on this branch, on the Chainguard image, listing both models after the restart (their keys are stored as v2:gcm:):

Models page on the FIPS image

Cost of one 60-byte value in the image: 93 µs to encrypt and 52 µs to decrypt in fips_mode, against 8 µs for SecretBox. /v1/responses p50 over 200 calls is the same on main and this branch (about 75 ms).

Steps to rerun the live check

Proxy command, config, env vars and client calls

The image is the FIPS image built from docker/Dockerfile.fips in #50330 (tagged litellm-fips below), with this branch's litellm/ mounted over the installed package. Use main's litellm/ for the base column.

docker network create b3
docker run -d --name b3-pg --network b3 -e POSTGRES_PASSWORD=pw postgres:16
# upstream double: any OpenAI-compatible server that logs the Authorization header, listening on b3-up:8080

cat > cfg.yaml <<'EOF'
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  store_model_in_db: true
  fips_mode: true
EOF

export LITELLM_MASTER_KEY=sk-$(openssl rand -hex 16)
export LITELLM_SALT_KEY=sk-$(openssl rand -hex 32)
SP=/app/.venv/lib/python3.13/site-packages

docker run -d --name b3-proxy --network b3 -p 127.0.0.1:4000:4000 \
  -e CHAINGUARD_LEGACY_ALLOWED=0 -e LITELLM_MASTER_KEY -e LITELLM_SALT_KEY \
  -e DATABASE_URL=postgresql://postgres:pw@b3-pg:5432/postgres \
  -v $PWD/litellm:$SP/litellm:ro -v $PWD/cfg.yaml:/cfg.yaml:ro \
  --entrypoint /app/.venv/bin/litellm litellm-fips --config /cfg.yaml --port 4000
H="Authorization: Bearer $LITELLM_MASTER_KEY"
curl -s localhost:4000/credentials -H "$H" -H content-type:application/json -d '{
  "credential_name": "b3-cred-1", "credential_info": {"custom_llm_provider": "openai"},
  "credential_values": {"api_key": "sk-b3-CRED-PLAINTEXT-1", "api_base": "http://b3-up:8080/v1"}}'
curl -s localhost:4000/model/new -H "$H" -H content-type:application/json -d '{
  "model_name": "b3-model-1",
  "litellm_params": {"model": "openai/fake-gpt", "api_base": "http://b3-up:8080/v1", "api_key": "sk-b3-MODEL-PLAINTEXT-1"}}'

# what is stored
docker exec b3-pg psql -U postgres -At -c \
  "select model_name, litellm_params->>'api_key' from \"LiteLLM_ProxyModelTable\""
docker exec b3-pg psql -U postgres -At -c \
  "select credential_name, credential_values->>'api_key' from \"LiteLLM_CredentialsTable\""

# read path: restart, then call through the stored model and check the upstream log
docker restart b3-proxy
curl -s localhost:4000/v1/chat/completions -H "$H" -H content-type:application/json \
  -d '{"model": "b3-model-1", "messages": [{"role": "user", "content": "ping"}]}'

# is libsodium loaded in the proxy
docker exec b3-proxy sh -c 'grep -l _sodium /proc/[0-9]*/maps'

For the other cells: add encryption_algorithm: xsalsa20-poly1305 to the config; write rows with fips_mode removed and then turn it on; turn fips_mode off on rows written with it on.

The gdb lines come from host gdb attached to a Python process in the same image that calls enforce_fips_mode({"fips_mode": True}) and then encrypt_value_helper("...") and decrypt_value_helper(...), with breakpoints on EVP_KDF_fetch, EVP_KDF_derive, EVP_CIPHER_fetch, EVP_EncryptInit_ex2, EVP_EncryptUpdate, EVP_EncryptFinal_ex, EVP_DecryptFinal_ex and libsodium's crypto_secretbox_*, reading the iv-generated parameter with EVP_CIPHER_CTX_get_params after each call.

Follow-ups, not in this PR

  • MCP grant envelopes (envelope.py) call SecretBox directly, also in fips_mode
  • UI session tokens and the return-to cookie (encrypt_bearer_token) use AES-GCM with an app-made nonce and the SHA-256 key
  • A clear error from POST /model/new when encryption fails
  • Refusing encryption_algorithm: xsalsa20-poly1305 at boot in fips_mode (today it is ignored)
  • Cache the derived key (the KBKDF call is most of the 52 µs read)
  • credential_migration.py reads the raw setting, so the migration endpoint does not know fips_mode
  • Leaving pynacl out of the FIPS image
  • The module's security policy says in section 2.7.9 that GCM with an internally generated IV is approved (IG C.H scenario 2) and in section 2.11 that it "shall only be used in conjunction with the TLS protocol". This PR relies on 2.7.9 and the indicator; the wording is worth confirming with Chainguard

Note

High Risk
Changes core at-rest encryption, decryption compatibility, and secret persistence when FIPS mode is enabled; misconfiguration can block loading existing secrets or prevent storing new ones.

Overview
When general_settings.fips_mode is on, new at-rest secrets are written as v2:gcm: using the OpenSSL FIPS provider’s AES-256-GCM (via ctypes), with keys from KBKDF over LITELLM_SALT_KEY and provider-generated nonces—not PyNaCl or the non-FIPS cryptography encrypt path. The write algorithm is forced to AES-GCM regardless of encryption_algorithm.

Reads in FIPS mode only accept v2:gcm: ciphertext under the FIPS-derived key; legacy XSalsa20 and standard-mode GCM values are rejected. Encrypt failures that previously stored plaintext in callback vars, guardrail params, and search tool params now raise in FIPS mode. Unit tests cover the new paths and stricter behavior.

Reviewed by Cursor Bugbot for commit eb5eaba. Bugbot is set up for automated code reviews on this repo. Configure here.

@veria-ai

veria-ai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

Reviews

yucheng-berri added a commit that referenced this pull request Nov 12, 2026
@codspeed

codspeed Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 45 untouched benchmarks


Comparing litellm_fips_at_rest_aes_gcm (d35c6ff) with main (b38c7fb)

Open in CodSpeed

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@greptile-apps

greptile-apps Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The update appears safe to merge.

Summary

The PR uses the FIPS provider to encrypt stored secrets when fips_mode is on. The latest update rejects keys that are not exactly 32 bytes before calling OpenSSL.

  • FIPS mode stores secrets with AES-GCM from the FIPS provider.
  • Callback, guardrail, and search settings stop when encryption fails in FIPS mode.
  • The MCP lock test skips Python builds that cannot reach OpenSSL.

Reviews (8) · Last reviewed commit: "fix(proxy): refuse a key that is not 32 ..." · Reviewed by Greptile

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

1 similar comment
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@yucheng-berri
yucheng-berri changed the base branch from main to litellm_fips_mode_require_hex_salt_key November 17, 2026 07:49
@yucheng-berri
yucheng-berri force-pushed the litellm_fips_at_rest_aes_gcm branch from d659ad2 to c60c014 Compare November 17, 2026 07:49
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Base automatically changed from litellm_fips_mode_require_hex_salt_key to main November 17, 2026 20:59
… fips_mode

With general_settings.fips_mode on, encrypt_value_helper writes v2:gcm: values
whose key comes from the FIPS provider's KBKDF over the salt key and whose nonce
is drawn by the provider, and values in any other format are refused on read.
Standard mode is unchanged.
@yucheng-berri
yucheng-berri force-pushed the litellm_fips_at_rest_aes_gcm branch from c4caf10 to e1110ed Compare November 17, 2026 22:11
@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit eb5eaba. Configure here.

@yucheng-berri
yucheng-berri enabled auto-merge (squash) November 18, 2026 03:09
@yucheng-berri
yucheng-berri merged commit 6462ea6 into main Nov 18, 2026
105 checks passed
@yucheng-berri
yucheng-berri deleted the litellm_fips_at_rest_aes_gcm branch November 18, 2026 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants