Repository navigation
refactor(proxy): derive fips_mode keys with cryptography KBKDF - #51822
yucheng-berri wants to merge 2 commits into
Conversation
Replace the ctypes binding to OpenSSL EVP_KDF with cryptography's KBKDFHMAC (SP 800-108r1 counter mode, HMAC-SHA256, 32-bit counter before the fixed input, 32-bit length after). The derived bytes are unchanged; a known-answer test pins keys produced by the ctypes implementation.
PR overviewThe PR refactors proxy key derivation in FIPS mode to use cryptography’s KBKDF implementation for MCP/plugin encryption keys. One issue remains open: the replacement KBKDF implementation can run outside the FIPS provider’s validated derivation service even when startup FIPS checks pass. This creates a FIPS assurance gap, rather than a demonstrated key compromise; matching known-answer vectors does not establish validation of the implementation. No issues have been addressed yet. Open issues (1)
Fixed/addressed: 0 · PR risk: 4/10 Reviews |
|
bugbot run |
[Medium impact] The PR appears safe to merge, with no new actionable findings. SummaryReplaces the hand-written OpenSSL binding with yucheng-berri accepted the previously raised FIPS caveat as intentional because the maintainer chose library-based derivation knowing it leaves the module's KBKDF service. Human approval remains the stated sign-off.
Reviews (3) · Last reviewed commit: "test(proxy): pin the production-label KB..." · Reviewed by Greptile |
| from cryptography.hazmat.primitives import hashes | ||
| from cryptography.hazmat.primitives.kdf.kbkdf import KBKDFHMAC, CounterLocation, Mode | ||
|
|
||
| return KBKDFHMAC( |
There was a problem hiding this comment.
Medium: Preserve provider-backed key derivation in FIPS mode
In cryptography 50.0.0, KBKDFHMAC constructs counter-mode KBKDF in Rust over HMAC rather than invoking OpenSSL's EVP_KDF, so the startup FIPS checks can pass while MCP/plugin key derivation runs outside the provider's validated KBKDF service. (raw.githubusercontent.com) Keep an explicitly FIPS-provider-backed KBKDF for this path; matching known-answer vectors establishes compatibility, not validation of the replacement implementation. (csrc.nist.gov)
There was a problem hiding this comment.
This is the change the PR makes on purpose, and it is declared in the description under Behavior changes, items 1 to 3: in fips_mode the SP 800-108 counter loop is now computed by cryptography over HMAC-SHA256 and is no longer the provider's EVP_KDF KBKDF service. The known-answer vectors are there to show the keys do not change, not to claim validation of the replacement.
Keeping a provider-backed KBKDF means keeping the hand-written ctypes binding to EVP_KDF, since cryptography exposes no binding to OpenSSL's KBKDF. That binding is what this PR removes: a review of open source projects with FIPS builds found each one derives keys through its crypto library and none maintains its own binding to the module's KDF service. What remains in place: the boot gate still refuses fips_mode unless both Python's and cryptography's OpenSSL enforce FIPS, the HMAC digest is still served by the active provider, and derive_key_from_salt_key still refuses a LITELLM_SALT_KEY under 32 bytes of hex.
Not changing the code for this. Whether a deployment's compliance statement needs the KDF itself to be a module service is listed as an open caveat in the description for the maintainers to decide.
Merging this PR will not alter performance1 warning
Comparing |
|
bugbot run |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
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 d289a07. Configure here.
Handwritten 1-3 sentence description
<make-me-human-readable>With
general_settings.fips_mode: true, the proxy derives its MCP token keys and plugin claim keys fromLITELLM_SALT_KEYwith KBKDF (SP 800-108, HMAC-SHA256). That derivation was a hand-writtenctypesbinding into OpenSSL. It is now one call to thecryptographylibrary'sKBKDFHMAC, and every derived key is byte-for-byte the same, so tokens already issued keep working. Withoutfips_modenothing changes</make-me-human-readable>Details
TLDR
Problem this solves:
_kbkdf_hmac_sha256reached OpenSSL'sEVP_KDFthrough about 45 lines ofctypes: a hand-declaredOSSL_PARAMstruct, five function prototypes and manual frees. litellm owned a C binding that no library maintains for usHow it solves it:
_kbkdf_hmac_sha256is nowKBKDFHMAC(SHA256, CounterMode, length=32, rlen=4, llen=4, BeforeFixed, label, context).derive(key)fromcryptography, which the proxy already depends onHMAC-SHA256(key, [1]_32 || label || 0x00 || context || [256]_32)ctypes,_OsslParam,_param, the_OSSL_PARAM_*constants and_KBKDF_PROPERTIESare removedderive_key_from_salt_keykeeps its signature, its 32-byte hex check onLITELLM_SALT_KEYand its error text. The three callers (bridge_credentials.py,session_credentials.py,plugin_routes.py) are not touchedLinear ticket
Relates to LIT-8426
Relation to #50032: that PR added
derive_key_from_salt_keyand its ctypes KBKDF. This PR replaces only the function body that computes KBKDF. The keys, labels, contexts, salt key rule and thefips_mode_active()switch are as #50032 left themBehavior changes
Removed / renamed
_OsslParam,_param,_OSSL_PARAM_UTF8_STRING,_OSSL_PARAM_OCTET_STRING,_OSSL_PARAM_UNMODIFIEDand_KBKDF_PROPERTIESare gone fromencrypt_decrypt_utils.py. Nothing else in the repo used them except the sharedfips_openssltest fixture, which is updatedOpenSSL could not run KBKDFno longer exists.git grepat base finds it in two places only: the line that raised it and the one test that matched it, both removed hereSilent regressions to watch
fips_mode:derive_key_from_salt_keyis only called whenfips_mode_active()is truefips_mode: truethe derived keys are unchanged, so session tokens, bridge envelopes and plugin claims minted before this PR open after it (proof below). What does change:cryptographycomputes the SP 800-108 counter loop in its own Rust code and calls OpenSSL only for HMAC-SHA256. On the Chainguard FIPS image that HMAC goes through libcrypto'sHMAC_*functions, not through the provider'sEVP_KDFKBKDF and not through itsEVP_MACservice (traced on 11-06 for fix(mcp): derive MCP bridge and session keys with the FIPS provider's KBKDF #50032, not re-run here: anLD_PRELOADtrace ofKBKDFHMAC.deriveshowed onlyHMAC_Init_ex,HMAC_UpdateandHMAC_Final, andcryptography50.0.0'skdf.rshas no call to OpenSSL's KBKDF). The digest underneath is still fetched from the active provider. This is how the peer FIPS builds we reviewed derive keys: through their crypto library, not through a binding to the module's KDF servicederive_key_from_salt_keystill refuses anyLITELLM_SALT_KEYthat is not at least 32 bytes of hex before the KDF is called, so no litellm caller can pass a short keyfips=yespin is gone. Base fetched KBKDF withfips=yes, so it raisedValueError(a 500 on MCP token routes) when no FIPS provider could serve the fetch. Whether a proxy can be in that state:fips_mode_active()is true only afterenforce_fips_modeacceptedfips_mode: true, and that requires Python's OpenSSL to have FIPS default properties and refuse scrypt, andcryptography's OpenSSL to have FIPS enabled and hash SHA-256 under it. Otherwise the proxy does not startEVP_set_default_properties(NULL, "provider=default")). No litellm code does that. Re-measured on the image: in that state base raisesValueError: OpenSSL could not run KBKDFand head returns the same key bytes, computed with the default provider's HMACcryptographyload two different libcrypto builds, and Python's hasfips=yesdefault properties but no FIPS provider. The boot gate accepts that host. Base then raised on the first key derivation; head derives the key incryptography's OpenSSL, the one the boot gate checked for a working FIPS providerEVP_KDF, so base raised and four tests skipped. Head runs there: the same test files give 0 skips on CPython 3.13.18 with static OpenSSL 3.5.9Migration for existing customers
Regression tests
test_derive_key_from_salt_key_keeps_the_keys_fips_mode_deployments_already_issued(new, 10 cases) pins keys produced by the base ctypes implementation, as hex: both salt key spellings, empty context, empty label, a 256-byte binary context, a 100-byte salt key, and the four production labels (litellm-mcp-bridge:envelope-signing:,litellm-mcp-bridge:envelope-encryption:,litellm-mcp-gateway:session-signing:,litellm-plugin:session-claim:)test_derive_key_from_salt_key_is_sp800_108_counter_mode_hmac_sha256(existing) checks the KDF against the SP 800-108 formula computed by hand. It no longer needs thefips_opensslfixture or a shared libcryptotest_derive_key_from_salt_key_refuses_an_openssl_without_a_fips_provideris removed with the pin it tested (item 3)test_encrypt_decrypt_utils.py,test_session_credentials.pyandtest_plugin_routes.pytests/unit/proxy/mcp_server/outbound_credentialsare unchanged and passMutations of the new implementation at 2152227, each run on
test_encrypt_decrypt_utils.py,tests/unit/proxy/mcp_server/outbound_credentialsandtest_plugin_routes.py(907 tests, all green before), then restored:rlen4 -> 2: 14 failed (all 10 pinned keys, both formula cases, 2 caller tests)llen4 -> 2: 14 failedfixed=label + context): 14 failedsk-prefix handling broken (wrong hex slice): 14 failedPre-Submission checklist
uv run pytest tests/unit/<your_test_file>.py -vScreenshots / Proof of Fix
E2E exception at d289a07. Scope: one private function body swapped for a library call with the same inputs and outputs. Rationale: the only observable of this function is the 32 bytes it returns, and those are compared byte for byte against base below, including inside the Chainguard FIPS image. Callers, routes, config and token formats are untouched
Head returns the same bytes as base
Base is
encrypt_decrypt_utils.pyfrom main 3ad1643 (ctypesEVP_KDF, with_KBKDF_PROPERTIESset toNonebecause this host has no FIPS provider). Both run in one process on CPython 3.12.3, wherehashlibuses the system OpenSSL 3.0.13 andcryptography50.0.0 uses its bundled OpenSSL 4.0.1The 10 saved vectors are the ones hard-coded in the new test. They were written by base before the change:
The first value is also what the FIPS provider's KBKDF returned for the same inputs on the Chainguard image in #50032's check
The three MCP label rows were regenerated at d289a07 with the context
sk-test-master-key, because the repo'scheck_no_publicly_known_master_keycheck refuses the literal used first. They come from the same base ctypes function, run on CPython 3.14.6 with OpenSSL 3.6.5 on a Mac, and that run reproduced the first row's value as a control. Theequiv.pyand Chainguard runs on this page used the earlier 10 vectors, which differ only in that contextChainguard FIPS image
litellm-fips-b2cg:head(OpenSSL 4.0.3 with the Chainguard FIPS provider, CPython 3.13.18,cryptography50.0.0 built against that OpenSSL), with base's or head'sencrypt_decrypt_utils.pymounted over the image's copy. Same output under the image defaults andCHAINGUARD_LEGACY_ALLOWED=0So on the real FIPS image the provider's KBKDF (base) and
cryptography(head) return the same 10 keys, and the two differences are the ones declared above: the short-key refusal and the behavior after the default properties are repointedTests and gates
On the GCP dev VM at 2152227 (d289a07 changes only three test vectors;
test_encrypt_decrypt_utils.pyrerun there on a Mac: 49 passed)The 2 failures on 3.12 are
test_sso_assertion_store.pyrotation tests failing withModuleNotFoundError: No module named 'prisma.models': that borrowed venv has no generated Prisma client. They pass in the 3.13 run above, where it is generatedWider sweep, run on a Mac (the VM login had expired), CPython 3.12.13 with statically linked OpenSSL 3.5.7:
openapi_docs_gatewas left to CIType
🧹 Refactoring
Caveats (if any)
Medium
fips_modethe key derivation is computed bycryptographyover HMAC-SHA256 and is no longer the FIPS module's KBKDF service (Behavior changes, items 1 to 3). Whether a deployment's compliance statement needs the KDF itself to be a module service is a policy question this PR does not answer; it follows what peer FIPS builds doLow
LD_PRELOADtrace behind item 1 is from 11-06 on the same image andcryptographyversion, and was not re-run for this PRFinal Attestation
Note
Medium Risk
Touches FIPS key derivation for MCP tokens and plugin session claims; outputs are pinned in tests but compliance may differ from OpenSSL’s FIPS KBKDF service path.
Overview
When
general_settings.fips_modeis on, MCP and plugin keys still come fromLITELLM_SALT_KEYvia SP 800-108 counter-mode HMAC-SHA256 KBKDF, but_kbkdf_hmac_sha256no longer uses a hand-rolledctypesbinding to OpenSSL’sEVP_KDF. It now delegates tocryptography’sKBKDFHMAC, droppingctypes,_OsslParam, and thefips=yesfetch path.derive_key_from_salt_keykeeps the same contract and salt-key validation; callers are unchanged. Tests add 10 pinned output vectors so existing FIPS deployments keep the same derived keys, drop skips for statically linked OpenSSL, and remove the test that expectedOpenSSL could not run KBKDFwhen no FIPS KBKDF provider is available.Reviewed by Cursor Bugbot for commit d289a07. Bugbot is set up for automated code reviews on this repo. Configure here.