Skip to content

Commit cd76e74

Browse files
committed
Add constant time equals
1 parent 2884b0b commit cd76e74

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* rename `toObjectArray()` to `toBoxedArray()` (will be removed in 1.0)
88
* add appendNullSafe and append string with encoding
99
* add proguard optimized version (can be used with classifier 'optimized')
10+
* add constant time equals
1011

1112
## v0.4.6
1213

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,18 @@ public boolean equals(byte[] anotherArray) {
16221622
return anotherArray != null && Arrays.equals(internalArray(), anotherArray);
16231623
}
16241624

1625+
/**
1626+
* Compares the inner array with given array. The comparison is done in constant time, therefore
1627+
* will not break on the first mismatch. This method is useful to prevent some side-channel attacks,
1628+
* but is slower on average.
1629+
*
1630+
* @param anotherArray to compare with
1631+
* @return true if {@link Arrays#equals(byte[], byte[])} returns true on given and internal array
1632+
*/
1633+
public boolean equalsConstantTime(byte[] anotherArray) {
1634+
return anotherArray != null && Util.constantTimeEquals(internalArray(), anotherArray);
1635+
}
1636+
16251637
/**
16261638
* Compares the inner array with given array.
16271639
* Note: a <code>null</code> Byte will not be equal to a <code>0</code> byte

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,15 @@ static boolean equals(byte[] obj, Byte[] anotherArray) {
433433
return true;
434434
}
435435

436+
static boolean constantTimeEquals(byte[] obj, byte[] anotherArray) {
437+
if (anotherArray == null || obj.length != anotherArray.length) return false;
438+
boolean result = true;
439+
for (int i = 0; i < obj.length; i++) {
440+
result &= obj[i] == anotherArray[i];
441+
}
442+
return result;
443+
}
444+
436445
/*
437446
=================================================================================================
438447
Copyright 2011 Twitter, Inc.

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public void testEqualsWithArray() {
7777
assertFalse(Bytes.random(16).equals(new byte[16]));
7878
}
7979

80+
@Test
81+
public void testEqualsWithConstantTime() {
82+
assertTrue(Bytes.allocate(4).equalsConstantTime(new byte[4]));
83+
assertFalse(Bytes.allocate(4).equalsConstantTime(new byte[3]));
84+
assertFalse(Bytes.random(16).equalsConstantTime(new byte[16]));
85+
}
86+
8087
@Test
8188
public void testEqualsWithObjectArray() {
8289
assertFalse(Bytes.allocate(4).equals(new Byte[4]));

0 commit comments

Comments
 (0)