mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Removed hashes from NotConstantTimeCryptoComparison.ql
This commit is contained in:
committed by
Fosstars
parent
8a69b7b3ac
commit
f245dc3ac8
@@ -3,7 +3,7 @@
|
||||
|
||||
<overview>
|
||||
<p>
|
||||
When comparing results of cryptographic operations, such as MAC or cryptographic hash,
|
||||
When comparing results of cryptographic operations, such as MAC or digital signature,
|
||||
a constant time algorithm should be used. In other words, the comparison time should not depend on
|
||||
the content of the input. Otherwise, an attacker may be able to implement a timing attack.
|
||||
A successful timing attack may result in leaking secrets or authentication bypass.
|
||||
@@ -20,15 +20,15 @@ and does not depend on the contents of the arrays.
|
||||
|
||||
<example>
|
||||
<p>
|
||||
The following example uses <code>Arrays.equals()</code> method for comparing cryptographic hashes.
|
||||
The following example uses <code>Arrays.equals()</code> method for comparing MAC.
|
||||
This method implements a not-constant time algorithm:
|
||||
</p>
|
||||
<sample src="UnsafeCryptoHashComparison.java" />
|
||||
<sample src="UnsafeMacComparison.java" />
|
||||
|
||||
<p>
|
||||
The next example example uses a safe not-constant time algorithm for comparing cryptographic hashes:
|
||||
The next example example uses a safe not-constant time algorithm for comparing MAC:
|
||||
</p>
|
||||
<sample src="SafeCryptoHashComparison.java" />
|
||||
<sample src="SafeMacComparison.java" />
|
||||
|
||||
</example>
|
||||
|
||||
|
||||
@@ -26,9 +26,6 @@ private class ReturnCryptoOperatinoResultMethod extends Method {
|
||||
or
|
||||
getDeclaringType().hasQualifiedName("java.security", "Signature") and
|
||||
hasName("sign")
|
||||
or
|
||||
getDeclaringType().hasQualifiedName("java.security", "MessageDigest") and
|
||||
hasName("digest")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
public boolean checkHash(byte[] expectedHash, byte[] data) throws Exception {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] actualHash = md.digest(data);
|
||||
return MessageDigest.isEqual(expectedHash, actualHash);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public boolean check(byte[] expected, byte[] data, SecretKey key) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(key.getEncoded(), "HmacSHA256"));
|
||||
byte[] actual = mac.doFinal(data);
|
||||
return MessageDigest.isEqual(expected, actual);
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
public boolean checkHash(byte[] expectedHash, byte[] data) throws Exception {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] actualHash = md.digest(data);
|
||||
return Arrays.equals(expectedHash, actualHash);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public boolean check(byte[] expected, byte[] data, SecretKey key) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(key.getEncoded(), "HmacSHA256"));
|
||||
byte[] actual = mac.doFinal(data);
|
||||
return Arrays.equals(expected, actual);
|
||||
}
|
||||
Reference in New Issue
Block a user