Skip to content

Commit 00eb352

Browse files
committed
constant time/side-channel improvements
1 parent 65bf633 commit 00eb352

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

core/src/main/java/org/bouncycastle/pqc/crypto/bike/BIKEEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void decaps(byte[] k, byte[] h0, byte[] h1, byte[] sigma, byte[] c0, byte
212212

213213
// 3. Compute K
214214
byte[] wlist = functionH(mPrime);
215-
if (Arrays.areEqual(ePrimeBytes, 0, R2_BYTE, wlist, 0, R2_BYTE))
215+
if (Arrays.constantTimeAreEqual(R2_BYTE, ePrimeBytes, 0, wlist, 0))
216216
{
217217
functionK(mPrime, c0, c1, k);
218218
}

core/src/main/java/org/bouncycastle/pqc/crypto/ntruplus/NTRUPlusEngine.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -930,13 +930,18 @@ public void crypto_kem_dec(byte[] ss, int ssPos, byte[] ct, int ctPos, byte[] sk
930930
fail |= verify(buf1, buf2, polyBytes);
931931

932932
// Copy shared secret, zeroing on failure
933-
if (fail != 0)
934-
{
935-
Arrays.fill(ss, (byte)0);
936-
}
937-
else
933+
cmov(ss, buf3, ssPos, SSBytes, fail);
934+
}
935+
936+
/* b = 0 means mov, b = 1 means don't mov*/
937+
static void cmov(byte[] r, byte[] x, int x_offset, int len, int b)
938+
{
939+
int i;
940+
941+
b = (b - 1) & 0xff;
942+
for (i = 0; i < len; i++)
938943
{
939-
System.arraycopy(buf3, 0, ss, ssPos, SSBytes);
944+
r[i] ^= b & (x[i + x_offset] ^ r[i]);
940945
}
941946
}
942947
}

core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/NTRULPRimeKEMExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public byte[] extractSecret(byte[] encapsulation)
150150
* Match Ciphertext ct with input encapsulation
151151
* Update encR accordingly
152152
*/
153-
int mask = (Arrays.areEqual(encapsulation, ct)) ? 0 : -1;
153+
int mask = (Arrays.constantTimeAreEqual(encapsulation, ct)) ? 0 : -1;
154154

155155
/*
156156
* Update encR with Ciphertext diff mask

core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/SNTRUPrimeKEMExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public byte[] extractSecret(byte[] encapsulation)
125125
* Match Ciphertext ct with input encapsulation
126126
* Update encR accordingly
127127
*/
128-
int mask = (Arrays.areEqual(encapsulation, ct)) ? 0 : -1;
128+
int mask = (Arrays.constantTimeAreEqual(encapsulation, ct)) ? 0 : -1;
129129

130130
/*
131131
* Update encR with Ciphertext diff mask

0 commit comments

Comments
 (0)