summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-07-17 22:07:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-07-17 22:07:12 +0700
commit2db28a5b63aadd34fb3ef350d69eb4db4e4580b4 (patch)
tree119f0a21af644d1e5e2e2ba9ab4658f2fd69dcaf
parentSpelling (diff)
downloadBouncyCastle.NET-ed25519-2db28a5b63aadd34fb3ef350d69eb4db4e4580b4.tar.xz
Straighten out ECGOST3410NamedCurves
- deprecate/replace methods returning ECDomainParameters
-rw-r--r--crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs27
-rw-r--r--crypto/src/asn1/x9/ECNamedCurveTable.cs10
-rw-r--r--crypto/src/crypto/parameters/ECDomainParameters.cs6
-rw-r--r--crypto/src/crypto/parameters/ECKeyParameters.cs17
-rw-r--r--crypto/src/crypto/parameters/ECNamedDomainParameters.cs7
-rw-r--r--crypto/src/security/PrivateKeyFactory.cs42
-rw-r--r--crypto/src/security/PublicKeyFactory.cs6
-rw-r--r--crypto/test/src/crypto/test/ECGOST3410_2012Test.cs8
-rw-r--r--crypto/test/src/test/NamedCurveTest.cs5
9 files changed, 51 insertions, 77 deletions
diff --git a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs
index 92c9312c7..99fb34bf7 100644
--- a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs
+++ b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs
@@ -2,6 +2,7 @@ using System;
 using System.Collections;
 
 using Org.BouncyCastle.Asn1.Rosstandart;
+using Org.BouncyCastle.Asn1.X9;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Math.EC;
@@ -228,12 +229,18 @@ namespace Org.BouncyCastle.Asn1.CryptoPro
         *
         * @param oid an object identifier representing a named parameters, if present.
         */
-        public static ECDomainParameters GetByOid(
-            DerObjectIdentifier oid)
+        [Obsolete("Use 'GetByOidX9' instead")]
+        public static ECDomainParameters GetByOid(DerObjectIdentifier oid)
         {
             return (ECDomainParameters)parameters[oid];
         }
 
+        public static X9ECParameters GetByOidX9(DerObjectIdentifier oid)
+        {
+            ECDomainParameters ec = (ECDomainParameters)parameters[oid];
+            return ec == null ? null : new X9ECParameters(ec.Curve, new X9ECPoint(ec.G, false), ec.N, ec.H, ec.GetSeed());
+        }
+
         /**
          * returns an enumeration containing the name strings for curves
          * contained in this structure.
@@ -243,17 +250,17 @@ namespace Org.BouncyCastle.Asn1.CryptoPro
             get { return new EnumerableProxy(names.Values); }
         }
 
-        public static ECDomainParameters GetByName(
-            string name)
+        [Obsolete("Use 'GetByNameX9' instead")]
+        public static ECDomainParameters GetByName(string name)
         {
             DerObjectIdentifier oid = (DerObjectIdentifier)objIds[name];
+            return oid == null ? null : (ECDomainParameters)parameters[oid];
+        }
 
-            if (oid != null)
-            {
-                return (ECDomainParameters)parameters[oid];
-            }
-
-            return null;
+        public static X9ECParameters GetByNameX9(string name)
+        {
+            DerObjectIdentifier oid = (DerObjectIdentifier)objIds[name];
+            return oid == null ? null : GetByOidX9(oid);
         }
 
         /**
diff --git a/crypto/src/asn1/x9/ECNamedCurveTable.cs b/crypto/src/asn1/x9/ECNamedCurveTable.cs
index f91bded18..fcd2810e7 100644
--- a/crypto/src/asn1/x9/ECNamedCurveTable.cs
+++ b/crypto/src/asn1/x9/ECNamedCurveTable.cs
@@ -7,7 +7,6 @@ using Org.BouncyCastle.Asn1.GM;
 using Org.BouncyCastle.Asn1.Nist;
 using Org.BouncyCastle.Asn1.Sec;
 using Org.BouncyCastle.Asn1.TeleTrust;
-using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Collections;
 
@@ -46,7 +45,7 @@ namespace Org.BouncyCastle.Asn1.X9
             }
             if (ecP == null)
             {
-                ecP = FromDomainParameters(ECGost3410NamedCurves.GetByName(name));
+                ecP = ECGost3410NamedCurves.GetByNameX9(name);
             }
             if (ecP == null)
             {
@@ -148,7 +147,7 @@ namespace Org.BouncyCastle.Asn1.X9
             }
             if (ecP == null)
             {
-                ecP = FromDomainParameters(ECGost3410NamedCurves.GetByOid(oid));
+                ecP = ECGost3410NamedCurves.GetByOidX9(oid);
             }
             if (ecP == null)
             {
@@ -177,10 +176,5 @@ namespace Org.BouncyCastle.Asn1.X9
                 return v;
             }
         }
-
-        private static X9ECParameters FromDomainParameters(ECDomainParameters dp)
-        {
-            return dp == null ? null : new X9ECParameters(dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed());
-        }
     }
 }
diff --git a/crypto/src/crypto/parameters/ECDomainParameters.cs b/crypto/src/crypto/parameters/ECDomainParameters.cs
index 3ff7d809f..ce17ab6df 100644
--- a/crypto/src/crypto/parameters/ECDomainParameters.cs
+++ b/crypto/src/crypto/parameters/ECDomainParameters.cs
@@ -1,5 +1,6 @@
 using System;
 
+using Org.BouncyCastle.Asn1.X9;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Math.EC;
 using Org.BouncyCastle.Utilities;
@@ -16,6 +17,11 @@ namespace Org.BouncyCastle.Crypto.Parameters
 
         private BigInteger hInv;
 
+        public ECDomainParameters(X9ECParameters x9)
+            : this(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed())
+        {
+        }
+
         public ECDomainParameters(
             ECCurve     curve,
             ECPoint     g,
diff --git a/crypto/src/crypto/parameters/ECKeyParameters.cs b/crypto/src/crypto/parameters/ECKeyParameters.cs
index 70b3543da..49bd4fe31 100644
--- a/crypto/src/crypto/parameters/ECKeyParameters.cs
+++ b/crypto/src/crypto/parameters/ECKeyParameters.cs
@@ -116,21 +116,12 @@ namespace Org.BouncyCastle.Crypto.Parameters
             if (publicKeyParamSet == null)
                 throw new ArgumentNullException("publicKeyParamSet");
 
-            ECDomainParameters p = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
+            X9ECParameters x9 = ECKeyPairGenerator.FindECCurveByOid(publicKeyParamSet);
 
-            if (p == null)
-            {
-                X9ECParameters x9 = ECKeyPairGenerator.FindECCurveByOid(publicKeyParamSet);
-
-                if (x9 == null)
-                {
-                    throw new ArgumentException("OID is not a valid public key parameter set", "publicKeyParamSet");
-                }
-
-                p = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
-            }
+            if (x9 == null)
+                throw new ArgumentException("OID is not a valid public key parameter set", "publicKeyParamSet");
 
-            return p;
+            return new ECDomainParameters(x9);
         }
     }
 }
diff --git a/crypto/src/crypto/parameters/ECNamedDomainParameters.cs b/crypto/src/crypto/parameters/ECNamedDomainParameters.cs
index 2279c7dcc..aa20e3239 100644
--- a/crypto/src/crypto/parameters/ECNamedDomainParameters.cs
+++ b/crypto/src/crypto/parameters/ECNamedDomainParameters.cs
@@ -1,6 +1,7 @@
 using System;
 
 using Org.BouncyCastle.Asn1;
+using Org.BouncyCastle.Asn1.X9;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Math.EC;
 
@@ -21,6 +22,12 @@ namespace Org.BouncyCastle.Crypto.Parameters
         {
         }
 
+        public ECNamedDomainParameters(DerObjectIdentifier name, X9ECParameters x9)
+            : base(x9)
+        {
+            this.name = name;
+        }
+
         public ECNamedDomainParameters(DerObjectIdentifier name, ECCurve curve, ECPoint g, BigInteger n)
             : base(curve, g, n)
         {
diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs
index 4c7a91ecf..dfc73c2cd 100644
--- a/crypto/src/security/PrivateKeyFactory.cs
+++ b/crypto/src/security/PrivateKeyFactory.cs
@@ -135,7 +135,7 @@ namespace Org.BouncyCastle.Security
                 Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(
                     algID.Parameters.ToAsn1Object());
 
-                ECDomainParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);
+                X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(gostParams.PublicKeyParamSet);
 
                 if (ecP == null)
                     throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key");
@@ -198,7 +198,7 @@ namespace Org.BouncyCastle.Security
                 if (p is Asn1Sequence && (Asn1Sequence.GetInstance(p).Count == 2 || Asn1Sequence.GetInstance(p).Count == 3))
                 {
 
-                    ECDomainParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);
+                    X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(gostParams.PublicKeyParamSet);
 
                     ecSpec = new ECGost3410Parameters(
                         new ECNamedDomainParameters(
@@ -235,29 +235,10 @@ namespace Org.BouncyCastle.Security
                     {
                         DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(parameters.Parameters);
                         X9ECParameters ecP = ECNamedCurveTable.GetByOid(oid);
-                        if (ecP == null)
-                        {
-                            ECDomainParameters gParam = ECGost3410NamedCurves.GetByOid(oid);
-                            ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(
-                                    oid,
-                                    gParam.Curve,
-                                    gParam.G,
-                                    gParam.N,
-                                    gParam.H,
-                                    gParam.GetSeed()), gostParams.PublicKeyParamSet, gostParams.DigestParamSet,
-                                gostParams.EncryptionParamSet);
-                        }
-                        else
-                        {
-                            ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(
-                                    oid,
-                                    ecP.Curve,
-                                    ecP.G,
-                                    ecP.N,
-                                    ecP.H,
-                                    ecP.GetSeed()), gostParams.PublicKeyParamSet, gostParams.DigestParamSet,
-                                gostParams.EncryptionParamSet);
-                        }
+
+                        ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(oid, ecP),
+                            gostParams.PublicKeyParamSet, gostParams.DigestParamSet,
+                            gostParams.EncryptionParamSet);
                     }
                     else if (parameters.IsImplicitlyCA)
                     {
@@ -266,15 +247,8 @@ namespace Org.BouncyCastle.Security
                     else
                     {
                         X9ECParameters ecP = X9ECParameters.GetInstance(parameters.Parameters);
-                        ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(
-                                algOid,
-                                ecP.Curve,
-                                ecP.G,
-                                ecP.N,
-                                ecP.H,
-                                ecP.GetSeed()),
-                            gostParams.PublicKeyParamSet,
-                            gostParams.DigestParamSet,
+                        ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(algOid, ecP),
+                            gostParams.PublicKeyParamSet, gostParams.DigestParamSet,
                             gostParams.EncryptionParamSet);
                     }
 
diff --git a/crypto/src/security/PublicKeyFactory.cs b/crypto/src/security/PublicKeyFactory.cs
index 6813452a6..15af90f91 100644
--- a/crypto/src/security/PublicKeyFactory.cs
+++ b/crypto/src/security/PublicKeyFactory.cs
@@ -154,7 +154,7 @@ namespace Org.BouncyCastle.Security
                     return new ECPublicKeyParameters("EC", q, (DerObjectIdentifier)para.Parameters);
                 }
 
-                ECDomainParameters dParams = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
+                ECDomainParameters dParams = new ECDomainParameters(x9);
                 return new ECPublicKeyParameters(q, dParams);
             }
             else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
@@ -162,7 +162,7 @@ namespace Org.BouncyCastle.Security
                 Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
                 DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet;
 
-                ECDomainParameters ecP = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
+                X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet);
                 if (ecP == null)
                     return null;
 
@@ -238,7 +238,7 @@ namespace Org.BouncyCastle.Security
                 DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet;
 
                 ECGost3410Parameters ecDomainParameters =new ECGost3410Parameters(
-                    new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOid(publicKeyParamSet)),
+                    new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet)),
                     publicKeyParamSet,
                     gostParams.DigestParamSet,
                     gostParams.EncryptionParamSet);
diff --git a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs
index 7e0a84cbe..0e7e90611 100644
--- a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs
+++ b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs
@@ -30,7 +30,7 @@ namespace Org.BouncyCastle.Crypto.Tests
         public SimpleTestResult EncodeRecodePublicKey()
         {
             DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA");
-            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
+            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid));
             ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null);
             ECKeyGenerationParameters paramameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
             ECKeyPairGenerator engine = new ECKeyPairGenerator();
@@ -118,7 +118,7 @@ namespace Org.BouncyCastle.Crypto.Tests
         private SimpleTestResult EncodeRecodePrivateKey()
         {
             DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA");
-            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
+            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid));
             ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null);
             ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
             ECKeyPairGenerator engine = new ECKeyPairGenerator();
@@ -338,7 +338,7 @@ namespace Org.BouncyCastle.Crypto.Tests
         public SimpleTestResult EncodeDecodePrivateLW(string oidStr, DerObjectIdentifier digest)
         {
             DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr);
-            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
+            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid));
             ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null);
             ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
             ECKeyPairGenerator engine = new ECKeyPairGenerator();
@@ -421,7 +421,7 @@ namespace Org.BouncyCastle.Crypto.Tests
         public SimpleTestResult EncodeDecodePublicLW(string oidStr, DerObjectIdentifier digest)
         {
             DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr);
-            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid));
+            ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid));
             ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null);
             ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom());
             ECKeyPairGenerator engine = new ECKeyPairGenerator();
diff --git a/crypto/test/src/test/NamedCurveTest.cs b/crypto/test/src/test/NamedCurveTest.cs
index 5dec49c41..560af7563 100644
--- a/crypto/test/src/test/NamedCurveTest.cs
+++ b/crypto/test/src/test/NamedCurveTest.cs
@@ -41,11 +41,6 @@ namespace Org.BouncyCastle.Tests
         private static ECDomainParameters GetCurveParameters(
             string name)
         {
-            ECDomainParameters ecdp = ECGost3410NamedCurves.GetByName(name);
-
-            if (ecdp != null)
-                return ecdp;
-
             X9ECParameters ecP = ECNamedCurveTable.GetByName(name);
 
             if (ecP == null)