Skip to content

Commit f5078e4

Browse files
committed
RFC 7748: Exclude all-zeroes agreement value
1 parent 3fb7da2 commit f5078e4

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

crypto/src/crypto/parameters/X25519PrivateKeyParameters.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public void GenerateSecret(X25519PublicKeyParameters publicKey, byte[] buf, int
5656
{
5757
byte[] encoded = new byte[X25519.PointSize];
5858
publicKey.Encode(encoded, 0);
59-
X25519.ScalarMult(data, 0, encoded, 0, buf, off);
59+
if (!X25519.CalculateAgreement(data, 0, encoded, 0, buf, off))
60+
throw new InvalidOperationException("X25519 agreement failed");
6061
}
6162
}
6263
}

crypto/src/crypto/parameters/X448PrivateKeyParameters.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public void GenerateSecret(X448PublicKeyParameters publicKey, byte[] buf, int of
5656
{
5757
byte[] encoded = new byte[X448.PointSize];
5858
publicKey.Encode(encoded, 0);
59-
X448.ScalarMult(data, 0, encoded, 0, buf, off);
59+
if (!X448.CalculateAgreement(data, 0, encoded, 0, buf, off))
60+
throw new InvalidOperationException("X448 agreement failed");
6061
}
6162
}
6263
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Diagnostics;
33
using System.Runtime.CompilerServices;
44

5+
using Org.BouncyCastle.Utilities;
6+
57
namespace Org.BouncyCastle.Math.EC.Rfc7748
68
{
79
public abstract class X25519
@@ -21,6 +23,12 @@ public abstract class X25519
2123

2224
private static int[] precompBase = null;
2325

26+
public static bool CalculateAgreement(byte[] k, int kOff, byte[] u, int uOff, byte[] r, int rOff)
27+
{
28+
ScalarMult(k, kOff, u, uOff, r, rOff);
29+
return !Arrays.AreAllZeroes(r, rOff, PointSize);
30+
}
31+
2432
private static uint Decode32(byte[] bs, int off)
2533
{
2634
uint n = bs[off];

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Diagnostics;
33
using System.Runtime.CompilerServices;
44

5+
using Org.BouncyCastle.Utilities;
6+
57
namespace Org.BouncyCastle.Math.EC.Rfc7748
68
{
79
public abstract class X448
@@ -24,6 +26,12 @@ public abstract class X448
2426

2527
private static uint[] precompBase = null;
2628

29+
public static bool CalculateAgreement(byte[] k, int kOff, byte[] u, int uOff, byte[] r, int rOff)
30+
{
31+
ScalarMult(k, kOff, u, uOff, r, rOff);
32+
return !Arrays.AreAllZeroes(r, rOff, PointSize);
33+
}
34+
2735
private static uint Decode32(byte[] bs, int off)
2836
{
2937
uint n = bs[off];

crypto/src/util/Arrays.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ public abstract class Arrays
1111
public static readonly byte[] EmptyBytes = new byte[0];
1212
public static readonly int[] EmptyInts = new int[0];
1313

14+
public static bool AreAllZeroes(byte[] buf, int off, int len)
15+
{
16+
uint bits = 0;
17+
for (int i = 0; i < len; ++i)
18+
{
19+
bits |= buf[off + i];
20+
}
21+
return bits == 0;
22+
}
23+
1424
public static bool AreEqual(
1525
bool[] a,
1626
bool[] b)

0 commit comments

Comments
 (0)