Skip to content

Fix non-constant-time prefix comparison in LegacyFullMac - #77

Closed
LuisCastellanos-dev wants to merge 2 commits into
tink-crypto:mainfrom
LuisCastellanos-dev:fix/legacy-full-mac-constant-time-prefix
Closed

Fix non-constant-time prefix comparison in LegacyFullMac#77
LuisCastellanos-dev wants to merge 2 commits into
tink-crypto:mainfrom
LuisCastellanos-dev:fix/legacy-full-mac-constant-time-prefix

Conversation

@LuisCastellanos-dev

Copy link
Copy Markdown

Fix non-constant-time prefix comparison in LegacyFullMac

Closes #75

What

Replace Arrays.equals() with MessageDigest.isEqual() when comparing the
output prefix in LegacyFullMac.verifyMac().

Why

Arrays.equals() short-circuits on the first differing byte. In a context
where 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 for
byte arrays in Java's security APIs. It always iterates over both arrays
in full, regardless of content.

The ChunkedMacVerificationFromComputation path already uses
MessageDigest.isEqual() for the MAC tag comparison. This fix brings
LegacyFullMac into alignment with that approach for its prefix comparison.

Change

LegacyFullMac.java

Before:
if (!Arrays.equals(identifier, prefix)) {

After:
if (!MessageDigest.isEqual(identifier, prefix)) {

Tests

Added LegacyFullMacTimingSafeTest with three cases:

  • Tag with correct first prefix byte but corrupted last prefix byte (TINK)
  • Same for CRUNCHY variant
  • Tag with all prefix bytes corrupted

Notes

  • import java.security.MessageDigest added
  • import java.util.Arrays retained (still used for copyOfRange)

@LuisCastellanos-dev LuisCastellanos-dev changed the title Fix/legacy full mac constant time prefix Fix non-constant-time prefix comparison in LegacyFullMac Aug 5, 2026
}

if (!Arrays.equals(identifier, prefix)) {
if (!MessageDigest.isEqual(identifier, prefix)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both "identifier" and "prefix" are already public information, hence Array.equals is fine.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification, @tholenst — closing this as the fix is already in place.

@LuisCastellanos-dev
LuisCastellanos-dev deleted the fix/legacy-full-mac-constant-time-prefix branch August 6, 2026 13:19
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.

ChunkedMacVerification does non constant time comparison on Mac

2 participants