public static ECPublicKeyParameters PublicKeyFromBytes(byte[] key) { /* * Convert public key into a public key object * The public key comprises two co-ordinates X followed by Y, both 32 bytes */ byte[] public_key_x = new byte[32]; byte[] public_key_y = new byte[32]; Array.Copy(key, 0, public_key_x, 0, public_key_x.Length); Array.Copy(key, 32, public_key_y, 0, public_key_y.Length); BigInteger bi_x = new BigInteger(1, public_key_x); BigInteger bi_y = new BigInteger(1, public_key_y); // the key needs to relate to a specific curve X9ECParameters ecP = X962NamedCurves.GetByName("prime256v1"); var ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed()); FpCurve c = (FpCurve)ecP.Curve; var fe_x = new FpFieldElement(c.Q, bi_x); var fe_y = new FpFieldElement(c.Q, bi_y); // point q represents the x,y co-ordinate of the public key ECPoint q = new FpPoint(c, fe_x, fe_y); return(new ECPublicKeyParameters("ECDSA", q, ecSpec)); }
// private static readonly Hashtable CurveNames = new Hashtable(); // private static readonly Hashtable CurveAliases = new Hashtable(); // // static NamedCurveTest() // { // CurveNames.Add("prime192v1", "prime192v1"); // X9.62 // CurveNames.Add("sect571r1", "sect571r1"); // sec // CurveNames.Add("secp224r1", "secp224r1"); // CurveNames.Add("B-409", SecNamedCurves.GetName(NistNamedCurves.GetOid("B-409"))); // nist // CurveNames.Add("P-521", SecNamedCurves.GetName(NistNamedCurves.GetOid("P-521"))); // CurveNames.Add("brainpoolp160r1", "brainpoolp160r1"); // TeleTrusT // // CurveAliases.Add("secp192r1", "prime192v1"); // CurveAliases.Add("secp256r1", "prime256v1"); // } private static ECDomainParameters GetCurveParameters( string name) { ECDomainParameters ecdp = ECGost3410NamedCurves.GetByName(name); if (ecdp != null) { return(ecdp); } X9ECParameters ecP = X962NamedCurves.GetByName(name); if (ecP == null) { ecP = SecNamedCurves.GetByName(name); if (ecP == null) { ecP = NistNamedCurves.GetByName(name); if (ecP == null) { ecP = TeleTrusTNamedCurves.GetByName(name); if (ecP == null) { throw new Exception("unknown curve name: " + name); } } } } return(new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed())); }
/// <summary> /// Get the curve from an EC key blob /// </summary> /// <param name="magic">Magic number</param> /// <returns>Curve </returns> private static X9ECParameters getCurve(int magic, CurveNamespace nameSpace) { switch (magic) { case (int)KeyBlobMagicNumber.ECDHPublicP256: case (int)KeyBlobMagicNumber.ECDsaPublicP256: switch (nameSpace) { case CurveNamespace.X962: return(X962NamedCurves.GetByName("prime256v1")); case CurveNamespace.NIST: return(NistNamedCurves.GetByName("P-256")); case CurveNamespace.SEC: return(SecNamedCurves.GetByName("SecP256r1")); } break; case (int)KeyBlobMagicNumber.ECDHPublicP384: case (int)KeyBlobMagicNumber.ECDsaPublicP384: switch (nameSpace) { case CurveNamespace.X962: // These aren't in the BC list return(X962NamedCurves.GetByName("prime384v1")); case CurveNamespace.NIST: return(NistNamedCurves.GetByName("P-384")); case CurveNamespace.SEC: return(SecNamedCurves.GetByName("SecP384r1")); } break; case (int)KeyBlobMagicNumber.ECDHPublicP521: case (int)KeyBlobMagicNumber.ECDsaPublicP521: switch (nameSpace) { case CurveNamespace.X962: // These aren't in the BC list return(X962NamedCurves.GetByName("prime521v1")); case CurveNamespace.NIST: return(NistNamedCurves.GetByName("P-521")); case CurveNamespace.SEC: return(SecNamedCurves.GetByName("SecP521r1")); } break; } return(null); }
public static ECPrivateKeyParameters PrivateKeyFromBytes(byte[] key) { /* * Convert public_key into a public key object * The public key comprises two co-ordinates X followed by Y, both 32 bytes */ var bi_key = new BigInteger(1, key); // the key needs to relate to a specific curve X9ECParameters ecP = X962NamedCurves.GetByName("prime256v1"); var ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed()); return(new ECPrivateKeyParameters("ECDSA", bi_key, ecSpec)); }
public void TestDeserializeEcdsaFromPrivate() { var formatter = new PemKeyFormatter(); var key = formatter.Deserialize(Resources.ecdsa_1); Assert.That(key.Version, Is.EqualTo(SshVersion.SSH2)); Assert.That(key.Algorithm, Is.EqualTo(PublicKeyAlgorithm.ECDSA_SHA2_NISTP256)); var publicKey = (ECPublicKeyParameters)key.GetPublicKeyParameters(); var privateKey = (ECPrivateKeyParameters)key.GetPrivateKeyParameters(); var param_curve = X962NamedCurves.GetByName(Resources.ecdsa_1_param_curve.Trim()); var param_priv = new BigInteger(Resources.ecdsa_1_param_priv.Trim(), 16); var param_pub = new BigInteger(Resources.ecdsa_1_param_pub.Trim(), 16); Assert.That(privateKey.Parameters.Curve, Is.EqualTo(param_curve.Curve)); Assert.That(privateKey.D, Is.EqualTo(param_priv)); // TODO: figure out how to convert public key to BigInteger that matches param_pub //Assert.That(publicKey.Q, Is.EqualTo(param_pub)); }
public void TestDeserializeEcdsaFromNewForamt() { // This is actaully exactly the same as TestDeserializeEcdsaFromPrivate var formatter = new PemKeyFormatter(); var key = formatter.Deserialize(Resources.ecdsa_n); Assert.That(key.Version, Is.EqualTo(SshVersion.SSH2)); Assert.That(key.Algorithm, Is.EqualTo(PublicKeyAlgorithm.ECDSA_SHA2_NISTP256)); var publicKey = (ECPublicKeyParameters)key.GetPublicKeyParameters(); var privateKey = (ECPrivateKeyParameters)key.GetPrivateKeyParameters(); var param_curve = X962NamedCurves.GetByName(Resources.ecdsa_1_param_curve.Trim()); var param_priv = new BigInteger(Resources.ecdsa_1_param_priv.Trim(), 16); var param_pub = new BigInteger(Resources.ecdsa_1_param_pub.Trim(), 16); Assert.That(privateKey.Parameters.Curve, Is.EqualTo(param_curve.Curve)); Assert.That(privateKey.D, Is.EqualTo(param_priv)); var q = new BigInteger(publicKey.Q.GetEncoded()); Assert.That(q, Is.EqualTo(param_pub)); }
// private static ECDomainParameters GetCurveParameters( private static X9ECParameters GetCurveParameters( string name) { X9ECParameters ecP = X962NamedCurves.GetByName(name); if (ecP == null) { ecP = SecNamedCurves.GetByName(name); if (ecP == null) { ecP = NistNamedCurves.GetByName(name); if (ecP == null) { throw new Exception("unknown curve name: " + name); } } } // return new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed()); return(ecP); }
public static X9ECParameters GetByName(string name) { X9ECParameters byName = X962NamedCurves.GetByName(name); if (byName == null) { byName = SecNamedCurves.GetByName(name); } if (byName == null) { byName = NistNamedCurves.GetByName(name); } if (byName == null) { byName = TeleTrusTNamedCurves.GetByName(name); } if (byName == null) { byName = AnssiNamedCurves.GetByName(name); } return(byName); }