Skip to content
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

Adding a time-constant comparison method #8

Merged
merged 2 commits into from
Nov 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public boolean verify(final byte[] data, final byte[] signature) throws MslCrypt
final Mac mac = CryptoCache.getMac(HMAC_SHA256_ALGO);
mac.init(hmacKey);
final byte[] hmac = mac.doFinal(data);
return Arrays.equals(hmac, envelope.getSignature());
return MslUtils.safeEquals(hmac, envelope.getSignature());
} catch (final MslEncodingException e) {
throw new MslCryptoException(MslError.SIGNATURE_ENVELOPE_PARSE_ERROR, e);
} catch (final NoSuchAlgorithmException e) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/netflix/msl/util/MslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,24 @@ public static byte[] uncompress(final CompressionAlgorithm compressionAlgo, fina
throw new MslException(MslError.UNCOMPRESSION_ERROR, "algo " + compressionAlgo.name() + " data " + dataB64, e);
}
}

/**
* Safely compares two byte arrays to prevent timing attacks.
* https://github.com/Netflix/msl/issues/7
*
* @param a first array for the comparison.
* @param b second array for the comparison.
* @return the result of the comparison.
*/
public static boolean safeEquals(byte[] a, byte[] b) {
if (a.length != b.length) {
return false;
}

int result = 0;
for (int i = 0; i < a.length; i++) {
result |= a[i] ^ b[i]
}
return result == 0;
}
}