Skip to content

Commit 0aa6f0a

Browse files
committed
Fix (unreachable) limit calculations
1 parent 9833781 commit 0aa6f0a

3 files changed

Lines changed: 6 additions & 21 deletions

File tree

core/src/main/java/org/bouncycastle/crypto/agreement/kdf/DHKEKGenerator.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,8 @@ public int generateBytes(byte[] out, int outOff, int len)
6161
long oBytes = len;
6262
int outLen = digest.getDigestSize();
6363

64-
//
65-
// this is at odds with the standard implementation, the
66-
// maximum value should be hBits * (2^32 - 1) where hBits
67-
// is the digest output size in bits. We can't have an
68-
// array with a long index at the moment...
69-
//
70-
if (oBytes > ((2L << 32) - 1))
64+
// NOTE: This limit isn't reachable for current array lengths
65+
if (oBytes > ((1L << 32) - 1) * outLen)
7166
{
7267
throw new IllegalArgumentException("Output length too large");
7368
}

core/src/main/java/org/bouncycastle/crypto/engines/EthereumIESEngine.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,8 @@ public int generateBytes(byte[] out, int outOff, int len)
559559
long oBytes = len;
560560
int outLen = digest.getDigestSize();
561561

562-
//
563-
// this is at odds with the standard implementation, the
564-
// maximum value should be hBits * (2^32 - 1) where hBits
565-
// is the digest output size in bits. We can't have an
566-
// array with a long index at the moment...
567-
//
568-
if (oBytes > ((2L << 32) - 1))
562+
// NOTE: This limit isn't reachable for current array lengths
563+
if (oBytes > ((1L << 32) - 1) * outLen)
569564
{
570565
throw new IllegalArgumentException("output length too large");
571566
}

core/src/main/java/org/bouncycastle/crypto/generators/BaseKDFBytesGenerator.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,8 @@ public int generateBytes(byte[] out, int outOff, int len) throws DataLengthExcep
8787
long oBytes = len;
8888
int outLen = digest.getDigestSize();
8989

90-
//
91-
// this is at odds with the standard implementation, the
92-
// maximum value should be hBits * (2^32 - 1) where hBits
93-
// is the digest output size in bits. We can't have an
94-
// array with a long index at the moment...
95-
//
96-
if (oBytes > ((2L << 32) - 1))
90+
// NOTE: This limit isn't reachable for current array lengths
91+
if (oBytes > ((1L << 32) - 1) * outLen)
9792
{
9893
throw new IllegalArgumentException("Output length too large");
9994
}

0 commit comments

Comments
 (0)