Skip to content

Zero the HPKE context on every setup failure - #3886

Closed
prasden wants to merge 1 commit into
aws:mainfrom
prasden:hpke-setup-zero
Closed

prasden wants to merge 1 commit into
aws:mainfrom
prasden:hpke-setup-zero

Conversation

@prasden

@prasden prasden commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Context and motivation

The HPKE setup functions did not always leave the context in a clean state when they failed.

A failed setup could leave the KEM, KDF and AEAD fields set. Each setup function zeroed the context, set those fields, and then ran encapsulation or decapsulation and the key schedule. If that work failed, EVP_HPKE_CTX_cleanup only released the AEAD state. So EVP_HPKE_CTX_kem, EVP_HPKE_CTX_kdf and EVP_HPKE_CTX_aead returned non-NULL after the failure. hpke.h says they return NULL if the context has not been set up.

Some setup paths also returned before they zeroed the context. This happened when the key had no KEM, and when the KEM does not support auth mode, which is true for the ML-KEM KEMs. hpke.h says it is safe to call EVP_HPKE_CTX_cleanup after a failed setup. If the caller had not zeroed the context first, cleanup read an uninitialized AEAD pointer and crashed.

Description of changes

Every setup function now zeros the context before any check. EVP_HPKE_CTX_cleanup now also zeros the context after it releases the AEAD state. So every failure path ends in the zero state, and a cleaned context is safe to clean up again or to set up again. The documented contract in hpke.h does not change. The code now does what the header already says.

hpke_ctx_setup_auth_sender zeros the context and then calls the seeded version, which zeros it again. The outer function has its own early returns before that call, so it needs its own zero.

Testing

I amended existing tests and did not add new ones. HPKETest.SetupSenderBufferTooSmall and HPKETest.SetupRecipientWrongLengthEnc now check that all three getters return NULL after the failed setup. HPKETest.ZeroedKeyFailsCleanly and HPKEMLKEMTest.AuthModeNotSupported now fill the context with garbage before each setup call, so the cleanup that follows runs on whatever state the failed setup left behind. Without the fix, the getter checks fail and the other two tests crash under ASan.

I built with clang 14, Debug and ASan. I ran crypto_test --gtest_filter='HPKETest.*:*HPKEMLKEMTest*' (111 tests) and the full ssl_test, because ECH uses HPKE contexts. All tests pass.

Review considerations

EVP_HPKE_CTX_cleanup now writes zeros over the whole context. No caller reads the context after cleanup. ScopedEVP_HPKE_CTX::Reset already called cleanup and then zero, so its behavior does not change.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@prasden prasden closed this Dec 3, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.80%. Comparing base (aa8f2e3) to head (d4b8fca).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3886      +/-   ##
==========================================
- Coverage   79.02%   78.80%   -0.23%     
==========================================
  Files         716      716              
  Lines      135498   135520      +22     
  Branches    19535    19533       -2     
==========================================
- Hits       107079   106790     -289     
- Misses      27373    27686     +313     
+ Partials     1046     1044       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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