From 551bd07fd737f2460b015f7b8300056fa012baff Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 30 Jul 2019 00:39:23 +0700 Subject: Work on EC parameters classes - make fields private/readonly where possible - add public validation methods to ECDomainParameters - add validation to ECPrivateKeyParameters - ECDomainParameters equality/GetHashCode ignore (optional) cofactor --- crypto/src/crypto/generators/ECKeyPairGenerator.cs | 2 +- crypto/src/crypto/parameters/ECDomainParameters.cs | 51 ++++++++++++++-------- .../crypto/parameters/ECNamedDomainParameters.cs | 2 +- .../crypto/parameters/ECPrivateKeyParameters.cs | 15 ++----- .../src/crypto/parameters/ECPublicKeyParameters.cs | 15 ++----- 5 files changed, 42 insertions(+), 43 deletions(-) (limited to 'crypto') diff --git a/crypto/src/crypto/generators/ECKeyPairGenerator.cs b/crypto/src/crypto/generators/ECKeyPairGenerator.cs index 26bc06e14..6a710c62e 100644 --- a/crypto/src/crypto/generators/ECKeyPairGenerator.cs +++ b/crypto/src/crypto/generators/ECKeyPairGenerator.cs @@ -105,7 +105,7 @@ namespace Org.BouncyCastle.Crypto.Generators { d = new BigInteger(n.BitLength, random); - if (d.CompareTo(BigInteger.Two) < 0 || d.CompareTo(n) >= 0) + if (d.CompareTo(BigInteger.One) < 0 || d.CompareTo(n) >= 0) continue; if (WNafUtilities.GetNafWeight(d) < minWeight) diff --git a/crypto/src/crypto/parameters/ECDomainParameters.cs b/crypto/src/crypto/parameters/ECDomainParameters.cs index e377f7760..3ff7d809f 100644 --- a/crypto/src/crypto/parameters/ECDomainParameters.cs +++ b/crypto/src/crypto/parameters/ECDomainParameters.cs @@ -8,12 +8,13 @@ namespace Org.BouncyCastle.Crypto.Parameters { public class ECDomainParameters { - internal ECCurve curve; - internal byte[] seed; - internal ECPoint g; - internal BigInteger n; - internal BigInteger h; - internal BigInteger hInv; + private readonly ECCurve curve; + private readonly byte[] seed; + private readonly ECPoint g; + private readonly BigInteger n; + private readonly BigInteger h; + + private BigInteger hInv; public ECDomainParameters( ECCurve curve, @@ -48,7 +49,7 @@ namespace Org.BouncyCastle.Crypto.Parameters // we can't check for h == null here as h is optional in X9.62 as it is not required for ECDSA this.curve = curve; - this.g = Validate(curve, g); + this.g = ValidatePublicPoint(curve, g); this.n = n; this.h = h; this.seed = Arrays.Clone(seed); @@ -113,26 +114,42 @@ namespace Org.BouncyCastle.Crypto.Parameters { return curve.Equals(other.curve) && g.Equals(other.g) - && n.Equals(other.n) - && h.Equals(other.h); + && n.Equals(other.n); } public override int GetHashCode() { - int hc = curve.GetHashCode(); - hc *= 37; + //return Arrays.GetHashCode(new object[]{ curve, g, n }); + int hc = 4; + hc *= 257; + hc ^= curve.GetHashCode(); + hc *= 257; hc ^= g.GetHashCode(); - hc *= 37; + hc *= 257; hc ^= n.GetHashCode(); - hc *= 37; - hc ^= h.GetHashCode(); return hc; } - internal static ECPoint Validate(ECCurve c, ECPoint q) + public BigInteger ValidatePrivateScalar(BigInteger d) + { + if (null == d) + throw new ArgumentNullException("d", "Scalar cannot be null"); + + if (d.CompareTo(BigInteger.One) < 0 || (d.CompareTo(N) >= 0)) + throw new ArgumentException("Scalar is not in the interval [1, n - 1]", "d"); + + return d; + } + + public ECPoint ValidatePublicPoint(ECPoint q) + { + return ValidatePublicPoint(Curve, q); + } + + internal static ECPoint ValidatePublicPoint(ECCurve c, ECPoint q) { - if (q == null) - throw new ArgumentException("Point has null value", "q"); + if (null == q) + throw new ArgumentNullException("q", "Point cannot be null"); q = ECAlgorithms.ImportPoint(c, q).Normalize(); diff --git a/crypto/src/crypto/parameters/ECNamedDomainParameters.cs b/crypto/src/crypto/parameters/ECNamedDomainParameters.cs index 4b8e2558f..2279c7dcc 100644 --- a/crypto/src/crypto/parameters/ECNamedDomainParameters.cs +++ b/crypto/src/crypto/parameters/ECNamedDomainParameters.cs @@ -17,7 +17,7 @@ namespace Org.BouncyCastle.Crypto.Parameters } public ECNamedDomainParameters(DerObjectIdentifier name, ECDomainParameters dp) - : this(name, dp.curve, dp.g, dp.n, dp.h, dp.seed) + : this(name, dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed()) { } diff --git a/crypto/src/crypto/parameters/ECPrivateKeyParameters.cs b/crypto/src/crypto/parameters/ECPrivateKeyParameters.cs index 4d0fa1fc6..47e53ef2d 100644 --- a/crypto/src/crypto/parameters/ECPrivateKeyParameters.cs +++ b/crypto/src/crypto/parameters/ECPrivateKeyParameters.cs @@ -24,10 +24,7 @@ namespace Org.BouncyCastle.Crypto.Parameters DerObjectIdentifier publicKeyParamSet) : base("ECGOST3410", true, publicKeyParamSet) { - if (d == null) - throw new ArgumentNullException("d"); - - this.d = d; + this.d = Parameters.ValidatePrivateScalar(d); } public ECPrivateKeyParameters( @@ -36,10 +33,7 @@ namespace Org.BouncyCastle.Crypto.Parameters ECDomainParameters parameters) : base(algorithm, true, parameters) { - if (d == null) - throw new ArgumentNullException("d"); - - this.d = d; + this.d = Parameters.ValidatePrivateScalar(d); } public ECPrivateKeyParameters( @@ -48,10 +42,7 @@ namespace Org.BouncyCastle.Crypto.Parameters DerObjectIdentifier publicKeyParamSet) : base(algorithm, true, publicKeyParamSet) { - if (d == null) - throw new ArgumentNullException("d"); - - this.d = d; + this.d = Parameters.ValidatePrivateScalar(d); } public BigInteger D diff --git a/crypto/src/crypto/parameters/ECPublicKeyParameters.cs b/crypto/src/crypto/parameters/ECPublicKeyParameters.cs index 69916e525..d43ac7e0e 100644 --- a/crypto/src/crypto/parameters/ECPublicKeyParameters.cs +++ b/crypto/src/crypto/parameters/ECPublicKeyParameters.cs @@ -24,10 +24,7 @@ namespace Org.BouncyCastle.Crypto.Parameters DerObjectIdentifier publicKeyParamSet) : base("ECGOST3410", false, publicKeyParamSet) { - if (q == null) - throw new ArgumentNullException("q"); - - this.q = ECDomainParameters.Validate(Parameters.Curve, q); + this.q = ECDomainParameters.ValidatePublicPoint(Parameters.Curve, q); } public ECPublicKeyParameters( @@ -36,10 +33,7 @@ namespace Org.BouncyCastle.Crypto.Parameters ECDomainParameters parameters) : base(algorithm, false, parameters) { - if (q == null) - throw new ArgumentNullException("q"); - - this.q = ECDomainParameters.Validate(Parameters.Curve, q); + this.q = ECDomainParameters.ValidatePublicPoint(Parameters.Curve, q); } public ECPublicKeyParameters( @@ -48,10 +42,7 @@ namespace Org.BouncyCastle.Crypto.Parameters DerObjectIdentifier publicKeyParamSet) : base(algorithm, false, publicKeyParamSet) { - if (q == null) - throw new ArgumentNullException("q"); - - this.q = ECDomainParameters.Validate(Parameters.Curve, q); + this.q = ECDomainParameters.ValidatePublicPoint(Parameters.Curve, q); } public ECPoint Q -- cgit 1.4.1