Repository navigation
feat(fips): encrypt stored secrets with the FIPS provider's AES-256-GCM in fips_mode - #51429
Conversation
Merging this PR will not alter performance1 warning
Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
bugbot run |
[Medium impact] The update appears safe to merge.
|
|
bugbot run |
1 similar comment
|
bugbot run |
d659ad2 to
c60c014
Compare
|
bugbot run |
070d1b1 to
c4caf10
Compare
|
bugbot run |
… 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.
… it in encrypt_value_helper
c4caf10 to
e1110ed
Compare
|
bugbot run |
|
bugbot run |
There was a problem hiding this comment.
✅ 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.
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. Infips_modethose secrets are now written with AES-256-GCM from the OpenSSL FIPS provider, under a key the provider derives fromLITELLM_SALT_KEY, and values in any other format are refused on read. Withoutfips_modenothing 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_helperdefaults to PyNaCl SecretBox, andfips_modedid not change that. On the Chainguard FIPS image afips_modeproxy stored secrets with libsodium and said nothingencryption_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 keyHow it solves it:
fips_modethe write algorithm is always AES-256-GCM, whateverencryption_algorithmsaysEVP_EncryptInit_ex2with no IV), as FIPS 140-3 IG C.H asks.cryptography'sAESGCMalways takes a caller nonce, so this one call goes to libcrypto throughctypes, the same wayderive_key_from_salt_keyalready reaches the provider's KBKDFv2:gcm:+ base64url(nonce || ciphertext || tag). Reads usecryptography'sAESGCMon the FIPS providerfips_modea value without thev2:gcm:prefix is refused, so libsodium is never calledUser Flow
Before: a
fips_modeproxy stores a model key with libsodiumgeneral_settings.fips_mode: trueapi_keyAfter: the same request stores the key with the FIPS provider
v2:gcm:..., and libsodium is never loadedLinear ticket
Resolves LIT-8427
Behavior changes
Removed / renamed
_KBKDF_PROPERTIES(private) is now_FIPS_PROPERTIES, since the cipher fetch uses it tooSilent regressions to watch
fips_mode. The default write is still PyNaCl,encryption_algorithm: aes-256-gcmstill writes the samev2: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 rowsfips_mode: true: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 afips_modeproxy without aLITELLM_SALT_KEYof 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-shortnew_encryption_key, raises:POST /credentialsreturns 500 with the message andPOST /model/newreturns 500Failed to add model to db. Main stored those values with libsodiumfips_modethat write now raises, so a secret is never stored in plaintext because encryption failedencryption_algorithm: xsalsa20-poly1305is ignoredfips_mode(PyNaCl values, andv2: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 themfips_modecannot be read afterfips_modeis turned off, because the key differsencryption_algorithm: aes-256-gcm: a response id becomesresp_v2:gcm:...fips_modeis only in v1.110.0 dev and rc tags, no stable releaseMigration for existing customers
fips_modeofffips_modeis for new deployments. A database with secrets written beforefips_modewas on has to be re-created or have those secrets entered againRegression tests
tests/unit/proxy/common_utils/test_encrypt_decrypt_utils.pytest_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 anxsalsa20-poly1305setting does not change ittest_fips_mode_refuses_values_it_did_not_writetest_fips_mode_encrypts_under_an_explicit_new_keytest_fips_mode_refuses_to_encrypt_under_a_salt_key_under_112_bitstest_fips_mode_never_stores_a_secret_as_written_when_it_cannot_be_encrypted: callback vars, guardrail params and search tool paramstest_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 ittest_fips_mode_merge_user_env_vars_takes_its_lock_without_blake2bnow writes through the FIPS provider, so it is skipped on a Python that links OpenSSL statically, wherectypescannot reach itctypestests run: 17979 tests intests/unit/proxy/common_utils,mcp_server,guardrails,search_endpoints, thefips_modeauth and password files, and the plugin and startup files pass.mcp_server/test_rest_endpoints.py::TestListToolsRestAPI::test_single_server_response_includes_paginated_upstream_toolsandguardrail_hooks/test_cato_networks.py::test_streaming_connect_uses_verifying_context_for_ca_bundlefailed under-n 12and pass alone, and this PR does not touch either file.ruff_strict_gate,type_discipline_gate,test_quality_gateandtype_check_gatepasstest_encrypt_decrypt_utils.py,test_fips.py,test_mcp_env_vars.py,guardrails,search_endpoints,test_callback_utils.py,test_plugin_routes.pyandmcp_server/outbound_credentialspass on the same Linux setup, with thectypestests runningtests/unit/proxy/common_utils,test_credential_migration.py,guardrails,search_endpointsandtest_mcp_env_vars.pypass on Linux, andtest_encrypt_decrypt_utils.pypasses with thectypestests runningWhy 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-generatedindicator stays 0 becausecryptographyhands 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 theAuthorizationheader. The commits after 1ee6ac4 change one test, make three callers re-raise infips_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.fips_mode: true, hex salt keyapi_keyv2:gcm:+ 12 + n + 16 bytesencryption_algorithm: xsalsa20-poly1305v2:gcm:previous_response_id, /v2/logingdb on
encrypt_value_helperinfips_modeon this branch:The read runs KBKDF again and
EVP_DecryptFinal_exreturns 1 on the FIPS provider's AES-256-GCM, with 0 libsodium hits. On main the same write and read hitcrypto_secretbox_easyandcrypto_secretbox_open_easyand make no libcrypto call.Other cells on the image:
fips_modeononly v2:gcm: values can be decrypted, new writes workfips_moderows, thenfips_modeoffInvalidTag), login worksThe Models page of the
fips_modeproxy on this branch, on the Chainguard image, listing both models after the restart (their keys are stored asv2:gcm:):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/responsesp50 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.fipsin #50330 (taggedlitellm-fipsbelow), with this branch'slitellm/mounted over the installed package. Use main'slitellm/for the base column.For the other cells: add
encryption_algorithm: xsalsa20-poly1305to the config; write rows withfips_moderemoved and then turn it on; turnfips_modeoff 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 thenencrypt_value_helper("...")anddecrypt_value_helper(...), with breakpoints onEVP_KDF_fetch,EVP_KDF_derive,EVP_CIPHER_fetch,EVP_EncryptInit_ex2,EVP_EncryptUpdate,EVP_EncryptFinal_ex,EVP_DecryptFinal_exand libsodium'scrypto_secretbox_*, reading theiv-generatedparameter withEVP_CIPHER_CTX_get_paramsafter each call.Follow-ups, not in this PR
envelope.py) call SecretBox directly, also infips_modeencrypt_bearer_token) use AES-GCM with an app-made nonce and the SHA-256 keyPOST /model/newwhen encryption failsencryption_algorithm: xsalsa20-poly1305at boot infips_mode(today it is ignored)credential_migration.pyreads the raw setting, so the migration endpoint does not knowfips_modepynaclout of the FIPS imageNote
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_modeis on, new at-rest secrets are written asv2:gcm:using the OpenSSL FIPS provider’s AES-256-GCM (viactypes), with keys from KBKDF overLITELLM_SALT_KEYand provider-generated nonces—not PyNaCl or the non-FIPScryptographyencrypt path. The write algorithm is forced to AES-GCM regardless ofencryption_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.