From c1d609b35a28a5d517ac47ead9b50876a378d94a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jan 2014 19:18:01 +0700 Subject: Some more EC point normalization --- crypto/src/asn1/x9/X9ECParameters.cs | 2 +- crypto/src/asn1/x9/X9ECPoint.cs | 12 +-- crypto/src/crypto/parameters/ECDomainParameters.cs | 82 ++++++++-------- .../src/crypto/parameters/ECPublicKeyParameters.cs | 106 ++++++++++----------- crypto/src/crypto/signers/ECNRSigner.cs | 3 +- 5 files changed, 102 insertions(+), 103 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/asn1/x9/X9ECParameters.cs b/crypto/src/asn1/x9/X9ECParameters.cs index 6389defa8..a192e4c52 100644 --- a/crypto/src/asn1/x9/X9ECParameters.cs +++ b/crypto/src/asn1/x9/X9ECParameters.cs @@ -87,7 +87,7 @@ namespace Org.BouncyCastle.Asn1.X9 byte[] seed) { this.curve = curve; - this.g = g; + this.g = g.Normalize(); this.n = n; this.h = h; this.seed = seed; diff --git a/crypto/src/asn1/x9/X9ECPoint.cs b/crypto/src/asn1/x9/X9ECPoint.cs index ba2b2bcbf..75d58cd38 100644 --- a/crypto/src/asn1/x9/X9ECPoint.cs +++ b/crypto/src/asn1/x9/X9ECPoint.cs @@ -10,25 +10,25 @@ namespace Org.BouncyCastle.Asn1.X9 { private readonly ECPoint p; - public X9ECPoint( + public X9ECPoint( ECPoint p) { - this.p = p; + this.p = p.Normalize(); } - public X9ECPoint( + public X9ECPoint( ECCurve c, Asn1OctetString s) { this.p = c.DecodePoint(s.GetOctets()); } - public ECPoint Point + public ECPoint Point { - get { return p; } + get { return p; } } - /** + /** * Produce an object suitable for an Asn1OutputStream. *
          *  ECPoint ::= OCTET STRING
diff --git a/crypto/src/crypto/parameters/ECDomainParameters.cs b/crypto/src/crypto/parameters/ECDomainParameters.cs
index c6a3e4e72..619971a6c 100644
--- a/crypto/src/crypto/parameters/ECDomainParameters.cs
+++ b/crypto/src/crypto/parameters/ECDomainParameters.cs
@@ -14,11 +14,11 @@ namespace Org.BouncyCastle.Crypto.Parameters
         internal BigInteger  n;
         internal BigInteger  h;
 
-		public ECDomainParameters(
+        public ECDomainParameters(
             ECCurve     curve,
             ECPoint     g,
             BigInteger  n)
-			: this(curve, g, n, BigInteger.One)
+            : this(curve, g, n, BigInteger.One)
         {
         }
 
@@ -27,34 +27,34 @@ namespace Org.BouncyCastle.Crypto.Parameters
             ECPoint     g,
             BigInteger  n,
             BigInteger  h)
-			: this(curve, g, n, h, null)
-		{
+            : this(curve, g, n, h, null)
+        {
         }
 
-		public ECDomainParameters(
+        public ECDomainParameters(
             ECCurve     curve,
             ECPoint     g,
             BigInteger  n,
             BigInteger  h,
             byte[]      seed)
         {
-			if (curve == null)
-				throw new ArgumentNullException("curve");
-			if (g == null)
-				throw new ArgumentNullException("g");
-			if (n == null)
-				throw new ArgumentNullException("n");
-			if (h == null)
-				throw new ArgumentNullException("h");
-
-			this.curve = curve;
-            this.g = g;
+            if (curve == null)
+                throw new ArgumentNullException("curve");
+            if (g == null)
+                throw new ArgumentNullException("g");
+            if (n == null)
+                throw new ArgumentNullException("n");
+            if (h == null)
+                throw new ArgumentNullException("h");
+
+            this.curve = curve;
+            this.g = g.Normalize();
             this.n = n;
             this.h = h;
             this.seed = Arrays.Clone(seed);
         }
 
-		public ECCurve Curve
+        public ECCurve Curve
         {
             get { return curve; }
         }
@@ -76,40 +76,40 @@ namespace Org.BouncyCastle.Crypto.Parameters
 
         public byte[] GetSeed()
         {
-			return Arrays.Clone(seed);
+            return Arrays.Clone(seed);
         }
 
-		public override bool Equals(
-			object obj)
+        public override bool Equals(
+            object obj)
         {
-			if (obj == this)
-				return true;
+            if (obj == this)
+                return true;
 
-			ECDomainParameters other = obj as ECDomainParameters;
+            ECDomainParameters other = obj as ECDomainParameters;
 
-			if (other == null)
-				return false;
+            if (other == null)
+                return false;
 
-			return Equals(other);
+            return Equals(other);
+        }
+
+        protected bool Equals(
+            ECDomainParameters other)
+        {
+            return curve.Equals(other.curve)
+                &&	g.Equals(other.g)
+                &&	n.Equals(other.n)
+                &&	h.Equals(other.h)
+                &&	Arrays.AreEqual(seed, other.seed);
         }
 
-		protected bool Equals(
-			ECDomainParameters other)
-		{
-			return curve.Equals(other.curve)
-				&&	g.Equals(other.g)
-				&&	n.Equals(other.n)
-				&&	h.Equals(other.h)
-				&&	Arrays.AreEqual(seed, other.seed);
-		}
-
-		public override int GetHashCode()
+        public override int GetHashCode()
         {
             return curve.GetHashCode()
-				^	g.GetHashCode()
-				^	n.GetHashCode()
-				^	h.GetHashCode()
-				^	Arrays.GetHashCode(seed);
+                ^	g.GetHashCode()
+                ^	n.GetHashCode()
+                ^	h.GetHashCode()
+                ^	Arrays.GetHashCode(seed);
         }
     }
 
diff --git a/crypto/src/crypto/parameters/ECPublicKeyParameters.cs b/crypto/src/crypto/parameters/ECPublicKeyParameters.cs
index 9e71c2a25..1eb665da9 100644
--- a/crypto/src/crypto/parameters/ECPublicKeyParameters.cs
+++ b/crypto/src/crypto/parameters/ECPublicKeyParameters.cs
@@ -7,78 +7,78 @@ using Org.BouncyCastle.Math.EC;
 namespace Org.BouncyCastle.Crypto.Parameters
 {
     public class ECPublicKeyParameters
-		: ECKeyParameters
+        : ECKeyParameters
     {
         private readonly ECPoint q;
 
-		public ECPublicKeyParameters(
-			ECPoint				q,
-			ECDomainParameters	parameters)
-			: this("EC", q, parameters)
-		{
-		}
-
-		[Obsolete("Use version with explicit 'algorithm' parameter")]
-		public ECPublicKeyParameters(
-			ECPoint				q,
-			DerObjectIdentifier publicKeyParamSet)
-			: base("ECGOST3410", false, publicKeyParamSet)
-		{
-			if (q == null)
-				throw new ArgumentNullException("q");
-
-			this.q = q;
-		}
-
-		public ECPublicKeyParameters(
-			string				algorithm,
-			ECPoint				q,
-			ECDomainParameters	parameters)
-			: base(algorithm, false, parameters)
+        public ECPublicKeyParameters(
+            ECPoint				q,
+            ECDomainParameters	parameters)
+            : this("EC", q, parameters)
         {
-			if (q == null)
-				throw new ArgumentNullException("q");
+        }
+
+        [Obsolete("Use version with explicit 'algorithm' parameter")]
+        public ECPublicKeyParameters(
+            ECPoint				q,
+            DerObjectIdentifier publicKeyParamSet)
+            : base("ECGOST3410", false, publicKeyParamSet)
+        {
+            if (q == null)
+                throw new ArgumentNullException("q");
 
-			this.q = q;
-		}
+            this.q = q.Normalize();
+        }
 
-		public ECPublicKeyParameters(
-			string				algorithm,
-			ECPoint				q,
-			DerObjectIdentifier publicKeyParamSet)
-			: base(algorithm, false, publicKeyParamSet)
+        public ECPublicKeyParameters(
+            string				algorithm,
+            ECPoint				q,
+            ECDomainParameters	parameters)
+            : base(algorithm, false, parameters)
         {
-			if (q == null)
-				throw new ArgumentNullException("q");
+            if (q == null)
+                throw new ArgumentNullException("q");
 
-			this.q = q;
-		}
+            this.q = q.Normalize();
+        }
 
-		public ECPoint Q
+        public ECPublicKeyParameters(
+            string				algorithm,
+            ECPoint				q,
+            DerObjectIdentifier publicKeyParamSet)
+            : base(algorithm, false, publicKeyParamSet)
         {
-			get { return q; }
+            if (q == null)
+                throw new ArgumentNullException("q");
+
+            this.q = q.Normalize();
         }
 
-		public override bool Equals(object obj)
+        public ECPoint Q
         {
-			if (obj == this)
-				return true;
+            get { return q; }
+        }
+
+        public override bool Equals(object obj)
+        {
+            if (obj == this)
+                return true;
 
-			ECPublicKeyParameters other = obj as ECPublicKeyParameters;
+            ECPublicKeyParameters other = obj as ECPublicKeyParameters;
 
-			if (other == null)
-				return false;
+            if (other == null)
+                return false;
 
-			return Equals(other);
+            return Equals(other);
         }
 
-		protected bool Equals(
-			ECPublicKeyParameters other)
-		{
-			return q.Equals(other.q) && base.Equals(other);
-		}
+        protected bool Equals(
+            ECPublicKeyParameters other)
+        {
+            return q.Equals(other.q) && base.Equals(other);
+        }
 
-		public override int GetHashCode()
+        public override int GetHashCode()
         {
             return q.GetHashCode() ^ base.GetHashCode();
         }
diff --git a/crypto/src/crypto/signers/ECNRSigner.cs b/crypto/src/crypto/signers/ECNRSigner.cs
index ba953aca4..cae15bdbf 100644
--- a/crypto/src/crypto/signers/ECNRSigner.cs
+++ b/crypto/src/crypto/signers/ECNRSigner.cs
@@ -106,8 +106,7 @@ namespace Org.BouncyCastle.Crypto.Signers
 
                 //    BigInteger Vx = tempPair.getPublic().getW().getAffineX();
                 ECPublicKeyParameters V = (ECPublicKeyParameters) tempPair.Public; // get temp's public key
-                ECPoint vq = V.Q.Normalize();
-                BigInteger Vx = vq.AffineXCoord.ToBigInteger(); // get the point's x coordinate
+                BigInteger Vx = V.Q.AffineXCoord.ToBigInteger(); // get the point's x coordinate
 
                 r = Vx.Add(e).Mod(n);
             }
-- 
cgit 1.5.1