From 10a2318ac5f90a581a919e9d4de489c6d217f5a7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 11 Apr 2014 10:11:27 +0700 Subject: Check for low-weight numbers in DH parameter generation and RSA key generation --- crypto/src/math/ec/multiplier/WNafUtilities.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'crypto/src/math') diff --git a/crypto/src/math/ec/multiplier/WNafUtilities.cs b/crypto/src/math/ec/multiplier/WNafUtilities.cs index 98e4f545f..865b9073e 100644 --- a/crypto/src/math/ec/multiplier/WNafUtilities.cs +++ b/crypto/src/math/ec/multiplier/WNafUtilities.cs @@ -268,6 +268,17 @@ namespace Org.BouncyCastle.Math.EC.Multiplier return wnaf; } + public static int GetNafWeight(BigInteger k) + { + if (k.SignValue == 0) + return 0; + + BigInteger _3k = k.ShiftLeft(1).Add(k); + BigInteger diff = _3k.Xor(k); + + return diff.BitCount; + } + public static WNafPreCompInfo GetWNafPreCompInfo(ECPoint p) { return GetWNafPreCompInfo(p.Curve.GetPreCompInfo(p, PRECOMP_NAME)); -- cgit 1.5.1