-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add ByteString.equals(other, constantTime) overload
#1812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SAY-5
wants to merge
6
commits into
square:master
Choose a base branch
from
SAY-5:bytestring-constant-time-equals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd9f614
feat: add ByteString.equals(other, constantTime) overload
SAY-5 d9deb8c
test(jvm): add statistical timing test for constantTime equals
SAY-5 9445141
test(jvm): use org.junit imports and ktlint-friendly assertTrue form
SAY-5 4bfb638
fix(test): add missing toByteString import in ConstantTimeEqualsTimin…
SAY-5 a59f221
fix(timing-test): drop flaky CT-vs-normal-match ratio assertion
SAY-5 a4e8c40
fix: dispatch to standard equals when constantTime is false
SAY-5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
82 changes: 82 additions & 0 deletions
82
okio/src/jvmTest/kotlin/okio/ConstantTimeEqualsTimingTest.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright (C) 2026 Square, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package okio | ||
|
|
||
| import okio.ByteString.Companion.toByteString | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| /** | ||
| * Statistical timing test for [ByteString.equals] with [constantTime]=true. | ||
| * | ||
| * Given two very large byte strings: | ||
| * - When not equal, constant-time equals takes the same time as when equal (no short-circuit). | ||
| * - When not equal, normal equals takes statistically significantly less time than when equal | ||
| * (short-circuits at the first differing byte). | ||
| */ | ||
| class ConstantTimeEqualsTimingTest { | ||
|
|
||
| @Test | ||
| fun constantTimeEqualsDoesNotShortCircuit() { | ||
| val n = 1_000_000 | ||
| val aBytes = ByteArray(n) { 0 } | ||
| val bBytes = ByteArray(n) { 0 } // identical to a | ||
| val cBytes = ByteArray(n) { 0 }.also { it[0] = 1 } // differs at byte 0 | ||
|
|
||
| val a = aBytes.toByteString() | ||
| val b = bBytes.toByteString() | ||
| val c = cBytes.toByteString() | ||
|
|
||
| // Warm up the JIT. | ||
| repeat(200) { | ||
| a.equals(b, constantTime = true) | ||
| a.equals(c, constantTime = true) | ||
| a == b | ||
| a == c | ||
| } | ||
|
|
||
| val iterations = 500 | ||
|
|
||
| val ctMatch = median(iterations) { a.equals(b, constantTime = true) } | ||
| val ctMismatch = median(iterations) { a.equals(c, constantTime = true) } | ||
| val normalMismatch = median(iterations) { a == c } | ||
|
|
||
| // CT(match) and CT(mismatch) should be within 3x of each other: neither short-circuits. | ||
| val ctRatio = | ||
| if (ctMatch > ctMismatch) ctMatch.toDouble() / ctMismatch else ctMismatch.toDouble() / ctMatch | ||
| assertTrue( | ||
| "CT(match)=$ctMatch ns and CT(mismatch)=$ctMismatch ns differ by ${ctRatio}x (expected <3x)", | ||
| ctRatio < 3.0, | ||
| ) | ||
|
|
||
| // normal(mismatch) must be significantly faster than CT(mismatch): normal short-circuits at byte 0. | ||
| assertTrue( | ||
| "normal(mismatch)=$normalMismatch ns should be <2% of CT(mismatch)=$ctMismatch ns (short-circuit at byte 0)", | ||
| normalMismatch * 50L < ctMismatch, | ||
| ) | ||
| } | ||
|
|
||
| private inline fun median(n: Int, block: () -> Unit): Long { | ||
| val times = LongArray(n) | ||
| repeat(n) { i -> | ||
| val t0 = System.nanoTime() | ||
| block() | ||
| times[i] = System.nanoTime() - t0 | ||
| } | ||
| times.sort() | ||
| return times[n / 2] | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason you chose to use 0 as the true-y value and
orthe comparisons as opposed to using 1 andand? The result is the same, but this version is unintuitive to me because it's not how I think about the comparisons being done.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the standard constant-time idiom: XOR each byte pair (0 when equal) and OR the results, so result stays 0 only if every pair matched and becomes non-zero the moment any byte differs, with a single comparison at the very end. Going with 1 and AND would need a per-byte equality test (this[i] == other[i]), which reintroduces a branch/comparison inside the loop that the OR-of-XOR form avoids. It mirrors crypto/subtle.ConstantTimeCompare and CRYPTO_memcmp. Happy to add a short comment to that effect if it helps readability.