
keywhiz.auth.Subtles Maven / Gradle / Ivy
/*
* Copyright (C) 2015 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 keywhiz.auth;
/**
* Cryptographic utility methods that are somewhat subtle. Named in honor of Go's 'subtle'
* package.
*/
public final class Subtles {
private Subtles() { }
/**
* Compare two byte arrays in constant time, so as to protect against timing attacks. (NB:
* "Constant time" means that the method execution time does not depend on the contents of the
* buffer — it's not a comment about the time complexity. (The method is obviously O(n).)
*
* This is interesting because normal comparisons (Arrays.equals(), eg) short-circuit as soon as a
* difference is found. This leaks information about the underlying data, and so is inappropriate
* for comparing secrets. For more information, see:
*
*
* - this Square Security Awareness
* note
* - a discussion of Java 6's
* MessageDigest.isEqual()
* - a paper on the
* practical exploitability of network-based timing attacks
*
*
* @param a first value
* @param b second value
* @return true iff a == b
*/
public static boolean secureCompare(final byte[] a, final byte[] b) {
if (a.length != b.length) {
return false;
}
int match = 0;
for (int i = 0; i < a.length; i++) {
match = match | a[i] ^ b[i];
}
return (match == 0); // true if match
}
/**
* Compare two char arrays in constant time, so as to protect against timing attacks. (NB:
* "Constant time" means that the method execution time does not depend on the contents of the
* buffer — it's not a comment about the time complexity. (The method is obviously O(n).)
*
* This is interesting because normal comparisons (Arrays.equals(), eg) short-circuit as soon as a
* difference is found. This leaks information about the underlying data, and so is inappropriate
* for comparing secrets. For more information, see:
*
*
*
* @param a first value
* @param b second value
* @return true iff a == b
*/
public static boolean secureCompare(final char[] a, final char[] b) {
if (a.length != b.length) {
return false;
}
int match = 0;
for (int i = 0; i < a.length; i++) {
match = match | a[i] ^ b[i];
}
return (match == 0); // true if match
}
/**
* Compare two Strings in constant time, so as to protect against timing attacks. (NB:
* "Constant time" means that the method execution time does not depend on the contents of the
* buffer — it's not a comment about the time complexity. (The method is obviously O(n).)
*
* This is interesting because normal comparisons (Arrays.equals(), eg) short-circuit as soon as a
* difference is found. This leaks information about the underlying data, and so is inappropriate
* for comparing secrets. For more information, see:
*
*
*
* @param a first value
* @param b second value
* @return true iff a == b
*/
public static boolean secureCompare(final String a, final String b) {
return secureCompare(a.toCharArray(), b.toCharArray());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy