Fix non-constant-time prefix comparison in LegacyFullMac - #77
Closed
LuisCastellanos-dev wants to merge 2 commits into
Closed
Fix non-constant-time prefix comparison in LegacyFullMac#77LuisCastellanos-dev wants to merge 2 commits into
LuisCastellanos-dev wants to merge 2 commits into
Conversation
tholenst
reviewed
Aug 6, 2026
| } | ||
|
|
||
| if (!Arrays.equals(identifier, prefix)) { | ||
| if (!MessageDigest.isEqual(identifier, prefix)) { |
Contributor
There was a problem hiding this comment.
Both "identifier" and "prefix" are already public information, hence Array.equals is fine.
Author
There was a problem hiding this comment.
Thanks for the clarification, @tholenst — closing this as the fix is already in place.
LuisCastellanos-dev
deleted the
fix/legacy-full-mac-constant-time-prefix
branch
August 6, 2026 13:19
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.
Fix non-constant-time prefix comparison in LegacyFullMac
Closes #75
What
Replace
Arrays.equals()withMessageDigest.isEqual()when comparing theoutput prefix in
LegacyFullMac.verifyMac().Why
Arrays.equals()short-circuits on the first differing byte. In a contextwhere an attacker can measure response latency, this allows them to determine
how many bytes of the tag prefix are correct — leaking information about the
key identifier byte by byte.
MessageDigest.isEqual()is the standard constant-time comparison forbyte arrays in Java's security APIs. It always iterates over both arrays
in full, regardless of content.
The
ChunkedMacVerificationFromComputationpath already usesMessageDigest.isEqual()for the MAC tag comparison. This fix bringsLegacyFullMacinto alignment with that approach for its prefix comparison.Change
LegacyFullMac.javaBefore:
if (!Arrays.equals(identifier, prefix)) {
After:
if (!MessageDigest.isEqual(identifier, prefix)) {
Tests
Added
LegacyFullMacTimingSafeTestwith three cases:Notes
import java.security.MessageDigestaddedimport java.util.Arraysretained (still used forcopyOfRange)