The analysis of the provided patches reveals several distinct vulnerabilities across the hpke-rs and hpke-rs-rust-crypto crates. The root causes are improper input validation, integer overflows, and incorrect comparison logic.
-
Integer Overflow in Sequence Counter (PR #123): In src/lib.rs, the Context struct used a u32 for its sequence_number. The increment_sequence_number function would increment this counter without checking for overflow. In a long-lived context, an attacker could force this counter to wrap around, causing nonce reuse for the AEAD cipher in Context::seal and Context::open, which is a catastrophic failure of confidentiality and integrity. The fix was to change the counter to a u64 and use checked_add to prevent overflow.
-
Improper Input Validation for X25519 Keys (PR #124): In rust_crypto_provider/src/lib.rs, the HpkeRustCrypto::dh function for DhKem25519 did not validate if the resulting shared secret was an all-zero value. This can happen when a low-order public key is used. The patch adds a check to ensure the shared secret is not all-zeros, preventing a weak key attack.
-
Improper Input Validation in seal/open and KDF (PR #128):
- In
src/lib.rs, the Context::seal and Context::open functions did not check if the ciphersuite was for 'export-only' mode. Calling these functions on such a context would lead to a panic due to an arithmetic underflow when calculating nonce length. The patch adds a check to return an error in this case.
- In
src/kdf.rs, the labeled_expand function silently truncated the requested output len because it was cast from a usize to a u16 without proper bounds checking in release builds. This could lead to a significantly shorter key than requested. The fix introduces a proper check to ensure the length does not exceed u16::MAX.
-
Incorrect Comparison in KEM Algorithm Mapping (PR #127): In traits/src/types.rs, the TryFrom<u16> implementation for KemAlgorithm incorrectly mapped the value 0x004D to XWingDraft06 instead of XWingDraft06Obsolete. This would cause a mismatch in the selected key encapsulation mechanism, breaking protocol compatibility when that specific identifier was used.
These issues highlight a pattern of missing or incomplete validation of cryptographic parameters and state, which are critical for the security of a cryptographic library.