Skip to content

Commit fe659c4

Browse files
committed
Move XDH/EdDSA key generation into low-level
- Clamp X25519, X448 private keys during generation
1 parent d4f3d50 commit fe659c4

File tree

9 files changed

+36
-5
lines changed

9 files changed

+36
-5
lines changed

crypto/src/crypto/parameters/Ed25519PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class Ed25519PrivateKeyParameters
1919
public Ed25519PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
Ed25519.GeneratePrivateKey(random, data);
2323
}
2424

2525
public Ed25519PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/parameters/Ed448PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class Ed448PrivateKeyParameters
1919
public Ed448PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
Ed448.GeneratePrivateKey(random, data);
2323
}
2424

2525
public Ed448PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/parameters/X25519KeyGenerationParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class X25519KeyGenerationParameters
88
: KeyGenerationParameters
99
{
1010
public X25519KeyGenerationParameters(SecureRandom random)
11-
: base(random, 256)
11+
: base(random, 255)
1212
{
1313
}
1414
}

crypto/src/crypto/parameters/X25519PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class X25519PrivateKeyParameters
1919
public X25519PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
X25519.GeneratePrivateKey(random, data);
2323
}
2424

2525
public X25519PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/parameters/X448PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class X448PrivateKeyParameters
1919
public X448PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
X448.GeneratePrivateKey(random, data);
2323
}
2424

2525
public X448PrivateKeyParameters(byte[] buf, int off)

crypto/src/math/ec/rfc7748/X25519.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33

4+
using Org.BouncyCastle.Security;
45
using Org.BouncyCastle.Utilities;
56

67
namespace Org.BouncyCastle.Math.EC.Rfc7748
@@ -50,6 +51,15 @@ private static void DecodeScalar(byte[] k, int kOff, uint[] n)
5051
n[7] |= 0x40000000U;
5152
}
5253

54+
public static void GeneratePrivateKey(SecureRandom random, byte[] k)
55+
{
56+
random.NextBytes(k);
57+
58+
k[0] &= 0xF8;
59+
k[ScalarSize - 1] &= 0x7F;
60+
k[ScalarSize - 1] |= 0x40;
61+
}
62+
5363
private static void PointDouble(int[] x, int[] z)
5464
{
5565
int[] A = X25519Field.Create();

crypto/src/math/ec/rfc7748/X448.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33

4+
using Org.BouncyCastle.Security;
45
using Org.BouncyCastle.Utilities;
56

67
namespace Org.BouncyCastle.Math.EC.Rfc7748
@@ -52,6 +53,14 @@ private static void DecodeScalar(byte[] k, int kOff, uint[] n)
5253
n[13] |= 0x80000000U;
5354
}
5455

56+
public static void GeneratePrivateKey(SecureRandom random, byte[] k)
57+
{
58+
random.NextBytes(k);
59+
60+
k[0] &= 0xFC;
61+
k[ScalarSize - 1] |= 0x80;
62+
}
63+
5564
private static void PointDouble(uint[] x, uint[] z)
5665
{
5766
uint[] A = X448Field.Create();

crypto/src/math/ec/rfc8032/Ed25519.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Org.BouncyCastle.Crypto.Digests;
66
using Org.BouncyCastle.Math.EC.Rfc7748;
77
using Org.BouncyCastle.Math.Raw;
8+
using Org.BouncyCastle.Security;
89
using Org.BouncyCastle.Utilities;
910

1011
namespace Org.BouncyCastle.Math.EC.Rfc8032
@@ -248,6 +249,11 @@ private static void EncodePoint(PointAccum p, byte[] r, int rOff)
248249
r[rOff + PointBytes - 1] |= (byte)((x[0] & 1) << 7);
249250
}
250251

252+
public static void GeneratePrivateKey(SecureRandom random, byte[] k)
253+
{
254+
random.NextBytes(k);
255+
}
256+
251257
public static void GeneratePublicKey(byte[] sk, int skOff, byte[] pk, int pkOff)
252258
{
253259
IDigest d = CreateDigest();

crypto/src/math/ec/rfc8032/Ed448.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Org.BouncyCastle.Crypto.Digests;
66
using Org.BouncyCastle.Math.EC.Rfc7748;
77
using Org.BouncyCastle.Math.Raw;
8+
using Org.BouncyCastle.Security;
89
using Org.BouncyCastle.Utilities;
910

1011
namespace Org.BouncyCastle.Math.EC.Rfc8032
@@ -257,6 +258,11 @@ private static void EncodePoint(PointExt p, byte[] r, int rOff)
257258
r[rOff + PointBytes - 1] = (byte)((x[0] & 1) << 7);
258259
}
259260

261+
public static void GeneratePrivateKey(SecureRandom random, byte[] k)
262+
{
263+
random.NextBytes(k);
264+
}
265+
260266
public static void GeneratePublicKey(byte[] sk, int skOff, byte[] pk, int pkOff)
261267
{
262268
IXof d = CreateXof();

0 commit comments

Comments
 (0)