Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed possible integer overflows and implicit casting #813

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ private void createInitialState()
}

// Process configuration block
ubiComplete(PARAM_TYPE_CONFIG, new Configuration(outputSizeBytes * 8).getBytes());
ubiComplete(PARAM_TYPE_CONFIG, new Configuration(outputSizeBytes * 8L).getBytes());
}

// Process additional pre-message parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ private long procedure_Aa(long x0, long c, BigInteger[] pq, int size)
//Verify and perform condition: 0<x<2^32; 0<c<2^32; c - odd.
while(x0<0 || x0>4294967296L)
{
x0 = init_random.nextInt()*2;
x0 = init_random.nextInt()*2L;
}

while((c<0 || c>4294967296L) || (c/2==0))
{
c = init_random.nextInt()*2+1;
c = init_random.nextInt()*2L+1L;
}

BigInteger C = new BigInteger(Long.toString(c));
Expand Down Expand Up @@ -371,12 +371,12 @@ private void procedure_Bb(long x0, long c, BigInteger[] pq)
//Verify and perform condition: 0<x<2^32; 0<c<2^32; c - odd.
while(x0<0 || x0>4294967296L)
{
x0 = init_random.nextInt()*2;
x0 = init_random.nextInt()*2L;
}

while((c<0 || c>4294967296L) || (c/2==0))
{
c = init_random.nextInt()*2+1;
c = init_random.nextInt()*2L+1;
}

BigInteger [] qp = new BigInteger[2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ public long skip(
int avail = available();
if (n <= avail)
{
bufOff += n;
if (bufOff > Integer.MAX_VALUE - n)
{
throw new IOException("Unable to skip: integer overflow");
}
bufOff += n; // lgtm [java/implicit-cast-in-compound-assignment]

return n;
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/bouncycastle/crypto/macs/KMAC.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public int doFinal(byte[] out, int outOff)
throw new IllegalStateException("KMAC not initialized");
}

byte[] encOut = XofUtils.rightEncode(getMacSize() * 8);
byte[] encOut = XofUtils.rightEncode(getMacSize() * 8L);

cshake.update(encOut, 0, encOut.length);
}
Expand All @@ -126,7 +126,7 @@ public int doFinal(byte[] out, int outOff, int outLen)
throw new IllegalStateException("KMAC not initialized");
}

byte[] encOut = XofUtils.rightEncode(outLen * 8);
byte[] encOut = XofUtils.rightEncode(outLen * 8L);

cshake.update(encOut, 0, encOut.length);
}
Expand Down Expand Up @@ -199,6 +199,6 @@ private void bytePad(byte[] X, int w)

private static byte[] encode(byte[] X)
{
return Arrays.concatenate(XofUtils.leftEncode(X.length * 8), X);
return Arrays.concatenate(XofUtils.leftEncode(X.length * 8L), X);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public byte[] generateSignature()
digest.doFinal(m2Hash, 0);

byte[] C = new byte[8];
LtoOSP(messageLength * 8, C);
LtoOSP(messageLength * 8L, C);

digest.update(C, 0, C.length);

Expand Down Expand Up @@ -514,7 +514,7 @@ public boolean verifySignature(
// check the hashes
//
byte[] C = new byte[8];
LtoOSP(recoveredMessage.length * 8, C);
LtoOSP(recoveredMessage.length * 8L, C);

digest.update(C, 0, C.length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ private static void reduce(int[] z, int x)
int t = z[9], z9 = t & M24;
t = (t >> 24) + x;

long cc = t * 19;
long cc = t * 19L;
cc += z[0]; z[0] = (int)cc & M26; cc >>= 26;
cc += z[1]; z[1] = (int)cc & M26; cc >>= 26;
cc += z[2]; z[2] = (int)cc & M25; cc >>= 25;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public byte[] Verify(byte[] message, byte[] signature)

byte[] testKey = new byte[testKeySize];

int c = 0;
long c = 0;
int counter = 0;
int test;
long test;

if (8 % w == 0)
{
Expand Down Expand Up @@ -279,7 +279,7 @@ public int getLog(int intValue)
return log;
}

private void hashSignatureBlock(byte[] sig, int sigOff, int rounds, byte[] buf, int bufOff)
private void hashSignatureBlock(byte[] sig, int sigOff, long rounds, byte[] buf, int bufOff)
{
if (rounds < 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public byte[] getSignature(byte[] message)
// byte [] message; // message m as input
byte[] hash = new byte[mdsize]; // hash of message m
int counter = 0;
int c = 0;
int test = 0;
long c = 0;
long test = 0;
// create hash of message m
messDigestOTS.update(message, 0, message.length);
messDigestOTS.doFinal(hash, 0);
Expand Down Expand Up @@ -326,7 +326,7 @@ public int getLog(int intValue)
return log;
}

private void hashPrivateKeyBlock(int index, int rounds, byte[] buf, int off)
private void hashPrivateKeyBlock(int index, long rounds, byte[] buf, int off)
{
if (rounds < 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1194,11 +1194,11 @@ static void sparse_mul32(int[] prod, int ppos, int[] pk, int pkPos, int[] pos_li
pos = pos_list[i];
for (j = 0; j < pos; j++)
{
temp[j] = temp[j] - sign_list[i] * pk[pkPos + j + PARAM_N - pos];
temp[j] = temp[j] - (long) sign_list[i] * pk[pkPos + j + PARAM_N - pos];
}
for (j = pos; j < PARAM_N; j++)
{
temp[j] = temp[j] + sign_list[i] * pk[pkPos + j - pos];
temp[j] = temp[j] + (long) sign_list[i] * pk[pkPos + j - pos];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1392,11 +1392,11 @@ static void sparse_mul8(long[] prod, int ppos, byte[] s, int spos, int[] pos_lis
pos = pos_list[i];
for (j = 0; j < pos; j++)
{
prod[ppos + j] = prod[ppos + j] - sign_list[i] * s[spos + j + PARAM_N - pos];
prod[ppos + j] = prod[ppos + j] - (long) sign_list[i] * s[spos + j + PARAM_N - pos];
}
for (j = pos; j < PARAM_N; j++)
{
prod[ppos + j] = prod[ppos + j] + sign_list[i] * s[spos + j - pos];
prod[ppos + j] = prod[ppos + j] + (long) sign_list[i] * s[spos + j - pos];
}
}
}
Expand All @@ -1417,11 +1417,11 @@ static void sparse_mul8(long[] prod, byte[] s, int[] pos_list, short[] sign_list
pos = pos_list[i];
for (j = 0; j < pos; j++)
{
prod[j] = prod[j] - sign_list[i] * t[j + PARAM_N - pos];
prod[j] = prod[j] - (long) sign_list[i] * t[j + PARAM_N - pos];
}
for (j = pos; j < PARAM_N; j++)
{
prod[j] = prod[j] + sign_list[i] * t[j - pos];
prod[j] = prod[j] + (long) sign_list[i] * t[j - pos];
}
}
}
Expand Down Expand Up @@ -1489,11 +1489,11 @@ static void sparse_mul32(long[] prod, int ppos, int[] pk, int pkPos, int[] pos_l
pos = pos_list[i];
for (j = 0; j < pos; j++)
{
prod[ppos + j] = prod[ppos + j] - sign_list[i] * pk[pkPos + j + PARAM_N - pos];
prod[ppos + j] = prod[ppos + j] - (long) sign_list[i] * pk[pkPos + j + PARAM_N - pos];
}
for (j = pos; j < PARAM_N; j++)
{
prod[ppos + j] = prod[ppos + j] + sign_list[i] * pk[pkPos + j - pos];
prod[ppos + j] = prod[ppos + j] + (long) sign_list[i] * pk[pkPos + j - pos];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static int horst_sign(HashFunctions hs,
offset_out = (1 << (HORST_LOGT - i - 1)) - 1;
for (j = 0; j < (1 << (HORST_LOGT - i - 1)); j++)
{
hs.hash_2n_n_mask(tree, (int)((offset_out + j) * SPHINCS256Config.HASH_BYTES), tree, (int)((offset_in + 2 * j) * SPHINCS256Config.HASH_BYTES), masks, 2 * i * SPHINCS256Config.HASH_BYTES);
hs.hash_2n_n_mask(tree, (int)((offset_out + j) * SPHINCS256Config.HASH_BYTES), tree, (int)((offset_in + 2L * j) * SPHINCS256Config.HASH_BYTES), masks, 2 * i * SPHINCS256Config.HASH_BYTES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ private BigInteger squareSum()
BigInteger sum = Constants.BIGINT_ZERO;
for (int i = 0; i < coeffs.length; i++)
{
sum = sum.add(BigInteger.valueOf(coeffs[i] * coeffs[i]));
sum = sum.add(BigInteger.valueOf((long) coeffs[i] * (long) coeffs[i]));
}
return sum;
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ public long centeredNormSq(int q)
{
int c = p.coeffs[i];
sum += c;
sqSum += c * c;
sqSum += (long) c * (long) c;
}

long centeredNormSq = sqSum - sum * sum / N;
Expand Down