Skip to content

Commit 85e2cdd

Browse files
committed
Constant-time checks for DES weak keys
1 parent c94981f commit 85e2cdd

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

core/src/main/java/org/bouncycastle/crypto/params/DESParameters.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.crypto.params;
22

3+
import org.bouncycastle.util.Arrays;
4+
35
public class DESParameters
46
extends KeyParameter
57
{
@@ -58,27 +60,21 @@ public DESParameters(
5860
* @return true if the given DES key material is weak or semi-weak,
5961
* false otherwise.
6062
*/
61-
public static boolean isWeakKey(
62-
byte[] key,
63-
int offset)
63+
public static boolean isWeakKey(byte[] key, int offset)
6464
{
65-
if (key.length - offset < DES_KEY_LENGTH)
65+
if (offset > (key.length - DES_KEY_LENGTH))
6666
{
6767
throw new IllegalArgumentException("key material too short.");
6868
}
6969

70-
nextkey: for (int i = 0; i < N_DES_WEAK_KEYS; i++)
70+
for (int i = 0; i < N_DES_WEAK_KEYS; i++)
7171
{
72-
for (int j = 0; j < DES_KEY_LENGTH; j++)
72+
if (Arrays.constantTimeAreEqual(DES_KEY_LENGTH, key, offset, DES_weak_keys, i * DES_KEY_LENGTH))
7373
{
74-
if (key[j + offset] != DES_weak_keys[i * DES_KEY_LENGTH + j])
75-
{
76-
continue nextkey;
77-
}
74+
return true;
7875
}
79-
80-
return true;
8176
}
77+
8278
return false;
8379
}
8480

0 commit comments

Comments
 (0)