Skip to content

Commit 467ca1e

Browse files
committed
Improve Dilithium, LMS, SPHINCS+'s constant time behavior
Signed-off-by: Alexander Scheel <[email protected]>
1 parent 7f49a54 commit 467ca1e

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/DilithiumEngine.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,7 @@ public boolean signVerify(byte[] sig, int siglen, byte[] msg, int msglen, byte[]
536536
// Helper.printByteArray(c2);
537537

538538

539-
for (int i = 0; i < DilithiumCTilde; ++i)
540-
{
541-
if (c[i] != c2[i])
542-
{
543-
return false;
544-
}
545-
}
546-
return true;
539+
return Arrays.constantTimeAreEqual(c, c2);
547540
}
548541

549542
public boolean signOpen(byte[] msg, byte[] signedMsg, int signedMsglen, byte[] rho, byte[] t1)

core/src/main/java/org/bouncycastle/pqc/crypto/lms/HSSPublicKeyParameters.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public LMSContext generateLMSContext(byte[] sigEnc)
136136

137137
public boolean verify(LMSContext context)
138138
{
139-
boolean failed = false;
139+
boolean passed = true;
140140

141141
LMSSignedPubKey[] sigKeys = context.getSignedPubKeys();
142142

@@ -151,13 +151,10 @@ public boolean verify(LMSContext context)
151151
{
152152
LMSSignature sig = sigKeys[i].getSignature();
153153
byte[] msg = sigKeys[i].getPublicKey().toByteArray();
154-
if (!LMS.verifySignature(key, sig, msg))
155-
{
156-
failed = true;
157-
}
154+
passed &= LMS.verifySignature(key, sig, msg);
158155
key = sigKeys[i].getPublicKey();
159156
}
160157

161-
return !failed & key.verify(context);
158+
return passed & key.verify(context);
162159
}
163160
}

core/src/main/java/org/bouncycastle/pqc/crypto/sphincs/SPHINCS256Signer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,12 @@ boolean verify(HashFunctions hs, byte[] m, byte[] sm, byte[] pk)
406406
smlen -= SPHINCS256Config.SUBTREE_HEIGHT * SPHINCS256Config.HASH_BYTES;
407407
}
408408

409+
// Because we use custom offsets on tpk, rather than incurring an
410+
// expensive copy, we use a manual constant time comparison.
409411
boolean verified = true;
410412
for (i = 0; i < SPHINCS256Config.HASH_BYTES; i++)
411413
{
412-
if (root[i] != tpk[i + Horst.N_MASKS * SPHINCS256Config.HASH_BYTES])
413-
{
414-
verified = false;
415-
}
414+
verified &= root[i] == tpk[i + Horst.N_MASKS * SPHINCS256Config.HASH_BYTES];
416415
}
417416

418417
return verified;

0 commit comments

Comments
 (0)