Skip to content

Commit

Permalink
another pass at constant time comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed May 5, 2019
1 parent bf6c20d commit 8696331
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/src/main/java/org/bouncycastle/util/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,23 @@ public static boolean constantTimeAreEqual(
return false;
}

if (expected.length != supplied.length)
if (expected == supplied)
{
return !Arrays.constantTimeAreEqual(expected, expected);
return true;
}

int nonEqual = 0;
int len = (expected.length < supplied.length) ? expected.length : supplied.length;

int nonEqual = expected.length ^ supplied.length;

for (int i = 0; i != expected.length; i++)
for (int i = 0; i != len; i++)
{
nonEqual |= (expected[i] ^ supplied[i]);
}
for (int i = len; i < supplied.length; i++)
{
nonEqual |= (supplied[i] ^ ~supplied[i]);
}

return nonEqual == 0;
}
Expand Down

0 comments on commit 8696331

Please sign in to comment.