Skip to content

Commit dd133cb

Browse files
committed
Add length guards
1 parent f4ee6b4 commit dd133cb

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

core/src/main/java/org/bouncycastle/math/ec/rfc7748/X25519.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ private static void decodeScalar(byte[] k, int kOff, int[] n)
5353

5454
public static void generatePrivateKey(SecureRandom random, byte[] k)
5555
{
56+
if (k.length != SCALAR_SIZE)
57+
{
58+
throw new IllegalArgumentException("k");
59+
}
60+
5661
random.nextBytes(k);
5762

5863
k[0] &= 0xF8;

core/src/main/java/org/bouncycastle/math/ec/rfc7748/X448.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ private static void decodeScalar(byte[] k, int kOff, int[] n)
5353

5454
public static void generatePrivateKey(SecureRandom random, byte[] k)
5555
{
56+
if (k.length != SCALAR_SIZE)
57+
{
58+
throw new IllegalArgumentException("k");
59+
}
60+
5661
random.nextBytes(k);
5762

5863
k[0] &= 0xFC;

core/src/main/java/org/bouncycastle/math/ec/rfc8032/Ed25519.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ private static int encodePoint(PointAccum p, byte[] r, int rOff)
358358

359359
public static void generatePrivateKey(SecureRandom random, byte[] k)
360360
{
361+
if (k.length != SECRET_KEY_SIZE)
362+
{
363+
throw new IllegalArgumentException("k");
364+
}
365+
361366
random.nextBytes(k);
362367
}
363368

core/src/main/java/org/bouncycastle/math/ec/rfc8032/Ed448.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ private static int encodePoint(PointProjective p, byte[] r, int rOff)
340340

341341
public static void generatePrivateKey(SecureRandom random, byte[] k)
342342
{
343+
if (k.length != SECRET_KEY_SIZE)
344+
{
345+
throw new IllegalArgumentException("k");
346+
}
347+
343348
random.nextBytes(k);
344349
}
345350

0 commit comments

Comments
 (0)