Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_cleanuponly released the AEAD state. SoEVP_HPKE_CTX_kem,EVP_HPKE_CTX_kdfandEVP_HPKE_CTX_aeadreturned non-NULL after the failure.hpke.hsays 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.hsays it is safe to callEVP_HPKE_CTX_cleanupafter 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_cleanupnow 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 inhpke.hdoes not change. The code now does what the header already says.hpke_ctx_setup_auth_senderzeros 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.SetupSenderBufferTooSmallandHPKETest.SetupRecipientWrongLengthEncnow check that all three getters return NULL after the failed setup.HPKETest.ZeroedKeyFailsCleanlyandHPKEMLKEMTest.AuthModeNotSupportednow 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 fullssl_test, because ECH uses HPKE contexts. All tests pass.Review considerations
EVP_HPKE_CTX_cleanupnow writes zeros over the whole context. No caller reads the context after cleanup.ScopedEVP_HPKE_CTX::Resetalready 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.