fix(crypto): constant-time MAC compare#2530
Closed
swaits wants to merge 1 commit into
Closed
Conversation
Contributor
|
This is #1656 but worse. |
Author
|
You're right on both counts — apologies, and thanks for the pointer.
Closing in favor of #1656. |
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.
Summary
Utils::MACThenDecryptinsrc/Utils.cppcompares the computed HMAC to the packet's MAC bytes withmemcmp, which is the textbook timing-oracle pattern — it returns at the first mismatched byte. This change replaces it with an XOR-OR constant-time loop.Background
memcmpis allowed to return as soon as it finds a mismatch. Across many MAC-validation attempts, an attacker can in principle observe the byte-of-first-mismatch through the (very small) timing difference and reduce a 16-bit forgery problem toward 2×8-bit problems.The practical exploitability here is low —
CIPHER_MAC_SIZE = 2so there are only ~2 bytes of timing signal to leak, and the LoRa receive-path noise dwarfs sub-microsecond timing differences. But:CIPHER_MAC_SIZEwidens in any future protocol revision, having constant-time compare already in place is one less migration step.Change
src/Utils.cpp— replace thememcmpMAC check with an XOR-OR accumulator:Every byte is always touched; loop length depends only on the public
CIPHER_MAC_SIZE, never on the input data.Why this is the minimal fix
This is the standard constant-time-equality pattern (e.g. NaCl
crypto_verify, OpenSSLCRYPTO_memcmp, Gocrypto/subtle.ConstantTimeCompare). Adding a dependency on a vendor library for two bytes would be over-engineering on an embedded target.Risk / compatibility
References
crypto/subtledocumentation, "Functions in this package … all execute in constant time" — reference pattern.