From b3a8c7160ce31e8b4ef03dc862f1dc7cfa0dd480 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 3 Feb 2022 15:19:53 +0700 Subject: Improve curve registry laziness --- crypto/src/asn1/anssi/ANSSINamedCurves.cs | 36 +- crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs | 523 +++++++++++++-------- crypto/src/asn1/gm/GMNamedCurves.cs | 48 +- crypto/src/asn1/nist/NISTNamedCurves.cs | 21 +- crypto/src/asn1/sec/SECNamedCurves.cs | 448 ++++++++++++------ crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs | 173 +++++-- crypto/src/asn1/x9/ECNamedCurveTable.cs | 59 +++ crypto/src/asn1/x9/X962NamedCurves.cs | 295 +++++++++--- crypto/src/asn1/x9/X9ECParametersHolder.cs | 36 +- crypto/src/crypto/ec/CustomNamedCurves.cs | 314 ++++++++++--- crypto/src/crypto/generators/ECKeyPairGenerator.cs | 36 +- crypto/src/crypto/tls/TlsEccUtilities.cs | 8 +- crypto/src/openpgp/PgpPublicKey.cs | 25 +- crypto/src/openssl/PEMReader.cs | 14 +- crypto/src/security/PrivateKeyFactory.cs | 2 +- crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs | 8 +- 16 files changed, 1455 insertions(+), 591 deletions(-) diff --git a/crypto/src/asn1/anssi/ANSSINamedCurves.cs b/crypto/src/asn1/anssi/ANSSINamedCurves.cs index 26222581e..48017fc2a 100644 --- a/crypto/src/asn1/anssi/ANSSINamedCurves.cs +++ b/crypto/src/asn1/anssi/ANSSINamedCurves.cs @@ -40,23 +40,28 @@ namespace Org.BouncyCastle.Asn1.Anssi internal static readonly X9ECParametersHolder Instance = new Frp256v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger p = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C03"); BigInteger a = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C00"); BigInteger b = FromHex("EE353FCA5428A9300D4ABA754A44C00FDFEC0C9AE4B1A1803075ED967B7BB73F"); - byte[] S = null; BigInteger n = FromHex("F1FD178C0B3AD58F10126DE8CE42435B53DC67E140D2BF941FFDD459C6D655E1"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + X9ECPoint G = ConfigureBasepoint(curve, "04B6B3D4C356C139EB31183D4749D423958C27D2DCAF98B70164C97A2DD98F5CFF6142E0F7C8B204911F9271F0F3ECEF8C2701C307E8E4C9E183115A1554062CFB"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } - }; - + } private static readonly IDictionary objIds = Platform.CreateHashtable(); private static readonly IDictionary curves = Platform.CreateHashtable(); @@ -77,26 +82,35 @@ namespace Org.BouncyCastle.Asn1.Anssi DefineCurve("FRP256v1", AnssiObjectIdentifiers.FRP256v1, Frp256v1Holder.Instance); } - public static X9ECParameters GetByName( - string name) + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + public static X9ECParametersHolder GetByNameLazy(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOidLazy(oid); + } + /** * return the X9ECParameters object for the named curve represented by * the passed in object identifier. Null if the curve isn't present. * * @param oid an object identifier representing a named curve, if present. */ - public static X9ECParameters GetByOid( - DerObjectIdentifier oid) + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid]; + X9ECParametersHolder holder = GetByOidLazy(oid); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)curves[oid]; + } + /** * return the object identifier signified by the passed in name. Null * if there is no object identifier associated with name. diff --git a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs index 1e54cb7d1..121843ada 100644 --- a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs +++ b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs @@ -9,6 +9,7 @@ using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; +using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.CryptoPro { @@ -17,11 +18,15 @@ namespace Org.BouncyCastle.Asn1.CryptoPro /// public sealed class ECGost3410NamedCurves { - private static ECPoint ConfigureBasepoint(ECCurve curve, BigInteger x, BigInteger y) + private ECGost3410NamedCurves() + { + } + + private static X9ECPoint ConfigureBasepoint(ECCurve curve, BigInteger x, BigInteger y) { ECPoint G = curve.CreatePoint(x, y); WNafUtilities.ConfigureBasepoint(G); - return G; + return new X9ECPoint(G, false); } private static ECCurve ConfigureCurve(ECCurve curve) @@ -29,210 +34,346 @@ namespace Org.BouncyCastle.Asn1.CryptoPro return curve; } - private ECGost3410NamedCurves() + private static BigInteger FromHex(string hex) { + return new BigInteger(1, Hex.DecodeStrict(hex)); } - internal static readonly IDictionary objIds = Platform.CreateHashtable(); - internal static readonly IDictionary parameters = Platform.CreateHashtable(); - internal static readonly IDictionary names = Platform.CreateHashtable(); + /* + * GostR3410-2001-CryptoPro-A (and GostR3410-2001-CryptoPro-XchA) + */ + internal class Holder_gostR3410_2001_CryptoPro_A + : X9ECParametersHolder + { + private Holder_gostR3410_2001_CryptoPro_A() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_gostR3410_2001_CryptoPro_A(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97"); + BigInteger mod_q = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C611070995AD10045841B09B761B893"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD94"), + FromHex("A6"), + mod_q, BigInteger.One)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + BigInteger.One, + FromHex("8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * GostR3410-2001-CryptoPro-B + */ + internal class Holder_gostR3410_2001_CryptoPro_B + : X9ECParametersHolder + { + private Holder_gostR3410_2001_CryptoPro_B() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_gostR3410_2001_CryptoPro_B(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("8000000000000000000000000000000000000000000000000000000000000C99"); + BigInteger mod_q = FromHex("800000000000000000000000000000015F700CFFF1A624E5E497161BCC8A198F"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("8000000000000000000000000000000000000000000000000000000000000C96"), + FromHex("3E1AF419A269A5F866A7D3C25C3DF80AE979259373FF2B182F49D4CE7E1BBC8B"), + mod_q, BigInteger.One)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + BigInteger.One, + FromHex("3FA8124359F96680B83D1C3EB2C070E5C545C9858D03ECFB744BF8D717717EFC")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * GostR3410-2001-CryptoPro-C + */ + internal class Holder_gostR3410_2001_CryptoPro_C + : X9ECParametersHolder + { + private Holder_gostR3410_2001_CryptoPro_C() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_gostR3410_2001_CryptoPro_C(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D759B"); + BigInteger mod_q = FromHex("9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D7598"), + FromHex("805A"), + mod_q, BigInteger.One)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + BigInteger.Zero, + FromHex("41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * GostR3410-2001-CryptoPro-XchB + */ + internal class Holder_gostR3410_2001_CryptoPro_XchB + : X9ECParametersHolder + { + private Holder_gostR3410_2001_CryptoPro_XchB() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_gostR3410_2001_CryptoPro_XchB(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D759B"); + BigInteger mod_q = FromHex("9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D7598"), + FromHex("805A"), + mod_q, BigInteger.One)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + BigInteger.Zero, + FromHex("41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * Tc26-Gost-3410-12-256-paramSetA + */ + internal class Holder_id_tc26_gost_3410_12_256_paramSetA + : X9ECParametersHolder + { + private Holder_id_tc26_gost_3410_12_256_paramSetA() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_id_tc26_gost_3410_12_256_paramSetA(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97"); + BigInteger mod_q = FromHex("400000000000000000000000000000000FD8CDDFC87B6635C115AF556C360C67"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("C2173F1513981673AF4892C23035A27CE25E2013BF95AA33B22C656F277E7335"), + FromHex("295F9BAE7428ED9CCC20E7C359A9D41A22FCCD9108E17BF7BA9337A6F8AE9513"), + mod_q, BigInteger.Four)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + FromHex("91E38443A5E82C0D880923425712B2BB658B9196932E02C78B2582FE742DAA28"), + FromHex("32879423AB1A0375895786C4BB46E9565FDE0B5344766740AF268ADB32322E5C")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * Tc26-Gost-3410-12-512-paramSetA + */ + internal class Holder_id_tc26_gost_3410_12_512_paramSetA + : X9ECParametersHolder + { + private Holder_id_tc26_gost_3410_12_512_paramSetA() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_id_tc26_gost_3410_12_512_paramSetA(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7"); + BigInteger mod_q = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF27E69532F48D89116FF22B8D4E0560609B4B38ABFAD2B85DCACDB1411F10B275"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC4"), + FromHex("E8C2505DEDFC86DDC1BD0B2B6667F1DA34B82574761CB0E879BD081CFD0B6265EE3CB090F30D27614CB4574010DA90DD862EF9D4EBEE4761503190785A71C760"), + mod_q, BigInteger.One)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + BigInteger.Three, + FromHex("7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF1626BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A4")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * Tc26-Gost-3410-12-512-paramSetB + */ + internal class Holder_id_tc26_gost_3410_12_512_paramSetB + : X9ECParametersHolder + { + private Holder_id_tc26_gost_3410_12_512_paramSetB() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_id_tc26_gost_3410_12_512_paramSetB(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006F"); + BigInteger mod_q = FromHex("800000000000000000000000000000000000000000000000000000000000000149A1EC142565A545ACFDB77BD9D40CFA8B996712101BEA0EC6346C54374F25BD"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006C"), + FromHex("687D1B459DC841457E3E06CF6F5E2517B97C7D614AF138BCBF85DC806C4B289F3E965D2DB1416D217F8B276FAD1AB69C50F78BEE1FA3106EFB8CCBC7C5140116"), + mod_q, BigInteger.One)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + BigInteger.Two, + FromHex("1A8F7EDA389B094C2C071E3647A8940F3C123B697578C213BE6DD9E6C8EC7335DCB228FD1EDF4A39152CBCAAF8C0398828041055F94CEEEC7E21340780FE41BD")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + /* + * Tc26-Gost-3410-12-512-paramSetC + */ + internal class Holder_id_tc26_gost_3410_12_512_paramSetC + : X9ECParametersHolder + { + private Holder_id_tc26_gost_3410_12_512_paramSetC() {} + + internal static readonly X9ECParametersHolder Instance = new Holder_id_tc26_gost_3410_12_512_paramSetC(); + + protected override ECCurve CreateCurve() + { + BigInteger mod_p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7"); + BigInteger mod_q = FromHex("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC98CDBA46506AB004C33A9FF5147502CC8EDA9E7A769A12694623CEF47F023ED"); + + return ConfigureCurve(new FpCurve( + mod_p, + FromHex("DC9203E514A721875485A529D2C722FB187BC8980EB866644DE41C68E143064546E861C0E2C9EDD92ADE71F46FCF50FF2AD97F951FDA9F2A2EB6546F39689BD3"), + FromHex("B4C4EE28CEBC6C2C8AC12952CF37F16AC7EFB6A9F69F4B57FFDA2E4F0DE5ADE038CBC2FFF719D2C18DE0284B8BFEF3B52B8CC7A5F5BF0A3C8D2319A5312557E1"), + mod_q, BigInteger.Four)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + + X9ECPoint G = ConfigureBasepoint(curve, + FromHex("E2E31EDFC23DE7BDEBE241CE593EF5DE2295B7A9CBAEF021D385F7074CEA043AA27272A7AE602BF2A7B9033DB9ED3610C6FB85487EAE97AAC5BC7928C1950148"), + FromHex("F5CE40D95B5EB899ABBCCFF5911CB8577939804D6527378B8C108C3D2090FF9BE18E2D33E3021ED2EF32D85822423B6304F726AA854BAE07D0396E9A9ADDC40F")); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); + } + }; + + + private static readonly IDictionary objIds = Platform.CreateHashtable(); + private static readonly IDictionary curves = Platform.CreateHashtable(); + private static readonly IDictionary names = Platform.CreateHashtable(); + + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) + { + objIds.Add(name, oid); + names.Add(oid, name); + curves.Add(oid, holder); + } static ECGost3410NamedCurves() { - BigInteger mod_p = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639319"); - BigInteger mod_q = new BigInteger("115792089237316195423570985008687907853073762908499243225378155805079068850323"); - - ECCurve curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"), // a - new BigInteger("166"), // b - mod_q, BigInteger.One)); - - ECDomainParameters ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("1"), // x - new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y - mod_q, BigInteger.One); - - parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProA] = ecParams; - - mod_p = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639319"); - mod_q = new BigInteger("115792089237316195423570985008687907853073762908499243225378155805079068850323"); - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"), - new BigInteger("166"), - mod_q, BigInteger.One)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("1"), // x - new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y - mod_q, BigInteger.One); - - parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA] = ecParams; - - mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823193"); //p - mod_q = new BigInteger("57896044618658097711785492504343953927102133160255826820068844496087732066703"); //q - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823190"), // a - new BigInteger("28091019353058090096996979000309560759124368558014865957655842872397301267595"), // b - mod_q, BigInteger.One)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("1"), // x - new BigInteger("28792665814854611296992347458380284135028636778229113005756334730996303888124")), // y - mod_q, BigInteger.One); - - parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProB] = ecParams; - - mod_p = new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502619"); - mod_q = new BigInteger("70390085352083305199547718019018437840920882647164081035322601458352298396601"); - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502616"), - new BigInteger("32858"), - mod_q, BigInteger.One)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("0"), - new BigInteger("29818893917731240733471273240314769927240550812383695689146495261604565990247")), - mod_q, BigInteger.One); - - parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB] = ecParams; - - mod_p = new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502619"); //p - mod_q = new BigInteger("70390085352083305199547718019018437840920882647164081035322601458352298396601"); //q - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502616"), // a - new BigInteger("32858"), // b - mod_q, BigInteger.One)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("0"), // x - new BigInteger("29818893917731240733471273240314769927240550812383695689146495261604565990247")), // y - mod_q, BigInteger.One); - - parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProC] = ecParams; - - //GOST34.10 2012 - mod_p = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97", 16); //p - mod_q = new BigInteger("400000000000000000000000000000000FD8CDDFC87B6635C115AF556C360C67", 16); //q - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("C2173F1513981673AF4892C23035A27CE25E2013BF95AA33B22C656F277E7335", 16), // a - new BigInteger("295F9BAE7428ED9CCC20E7C359A9D41A22FCCD9108E17BF7BA9337A6F8AE9513", 16), // b - mod_q, BigInteger.Four)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("91E38443A5E82C0D880923425712B2BB658B9196932E02C78B2582FE742DAA28", 16), // x - new BigInteger("32879423AB1A0375895786C4BB46E9565FDE0B5344766740AF268ADB32322E5C", 16)), // y - mod_q, BigInteger.Four); - - parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA] = ecParams; - - mod_p = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7", 16); //p - mod_q = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF27E69532F48D89116FF22B8D4E0560609B4B38ABFAD2B85DCACDB1411F10B275", 16); //q - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC4", 16), // a - new BigInteger("E8C2505DEDFC86DDC1BD0B2B6667F1DA34B82574761CB0E879BD081CFD0B6265EE3CB090F30D27614CB4574010DA90DD862EF9D4EBEE4761503190785A71C760", 16), // b - mod_q, BigInteger.One)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003"), // x - new BigInteger("7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF1626BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A4", 16)), // y - mod_q, BigInteger.One); - - parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA] = ecParams; - - mod_p = new BigInteger("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006F", 16); //p - mod_q = new BigInteger("800000000000000000000000000000000000000000000000000000000000000149A1EC142565A545ACFDB77BD9D40CFA8B996712101BEA0EC6346C54374F25BD", 16); //q - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006C", 16), // a - new BigInteger("687D1B459DC841457E3E06CF6F5E2517B97C7D614AF138BCBF85DC806C4B289F3E965D2DB1416D217F8B276FAD1AB69C50F78BEE1FA3106EFB8CCBC7C5140116", 16), // b - mod_q, BigInteger.One)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"), // x - new BigInteger("1A8F7EDA389B094C2C071E3647A8940F3C123B697578C213BE6DD9E6C8EC7335DCB228FD1EDF4A39152CBCAAF8C0398828041055F94CEEEC7E21340780FE41BD", 16)), // y - mod_q, BigInteger.One); - - parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB] = ecParams; - - mod_p = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7", 16); //p - mod_q = new BigInteger("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC98CDBA46506AB004C33A9FF5147502CC8EDA9E7A769A12694623CEF47F023ED", 16); //q - - curve = ConfigureCurve(new FpCurve( - mod_p, // p - new BigInteger("DC9203E514A721875485A529D2C722FB187BC8980EB866644DE41C68E143064546E861C0E2C9EDD92ADE71F46FCF50FF2AD97F951FDA9F2A2EB6546F39689BD3", 16), // a - new BigInteger("B4C4EE28CEBC6C2C8AC12952CF37F16AC7EFB6A9F69F4B57FFDA2E4F0DE5ADE038CBC2FFF719D2C18DE0284B8BFEF3B52B8CC7A5F5BF0A3C8D2319A5312557E1", 16), // b - mod_q, BigInteger.Four)); - - ecParams = new ECDomainParameters( - curve, - ConfigureBasepoint(curve, - new BigInteger("E2E31EDFC23DE7BDEBE241CE593EF5DE2295B7A9CBAEF021D385F7074CEA043AA27272A7AE602BF2A7B9033DB9ED3610C6FB85487EAE97AAC5BC7928C1950148", 16), // x - new BigInteger("F5CE40D95B5EB899ABBCCFF5911CB8577939804D6527378B8C108C3D2090FF9BE18E2D33E3021ED2EF32D85822423B6304F726AA854BAE07D0396E9A9ADDC40F", 16)), // y - mod_q, BigInteger.Four); - - parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC] = ecParams; - - objIds["GostR3410-2001-CryptoPro-A"] = CryptoProObjectIdentifiers.GostR3410x2001CryptoProA; - objIds["GostR3410-2001-CryptoPro-B"] = CryptoProObjectIdentifiers.GostR3410x2001CryptoProB; - objIds["GostR3410-2001-CryptoPro-C"] = CryptoProObjectIdentifiers.GostR3410x2001CryptoProC; - objIds["GostR3410-2001-CryptoPro-XchA"] = CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA; - objIds["GostR3410-2001-CryptoPro-XchB"] = CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB; - objIds["Tc26-Gost-3410-12-256-paramSetA"] = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA; - objIds["Tc26-Gost-3410-12-512-paramSetA"] = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA; - objIds["Tc26-Gost-3410-12-512-paramSetB"] = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB; - objIds["Tc26-Gost-3410-12-512-paramSetC"] = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC; - - names[CryptoProObjectIdentifiers.GostR3410x2001CryptoProA] = "GostR3410-2001-CryptoPro-A"; - names[CryptoProObjectIdentifiers.GostR3410x2001CryptoProB] = "GostR3410-2001-CryptoPro-B"; - names[CryptoProObjectIdentifiers.GostR3410x2001CryptoProC] = "GostR3410-2001-CryptoPro-C"; - names[CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA] = "GostR3410-2001-CryptoPro-XchA"; - names[CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB] = "GostR3410-2001-CryptoPro-XchB"; - names[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA] = "Tc26-Gost-3410-12-256-paramSetA"; - names[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA] = "Tc26-Gost-3410-12-512-paramSetA"; - names[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB] = "Tc26-Gost-3410-12-512-paramSetB"; - names[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC] = "Tc26-Gost-3410-12-512-paramSetC"; + DefineCurve("GostR3410-2001-CryptoPro-A", CryptoProObjectIdentifiers.GostR3410x2001CryptoProA, + Holder_gostR3410_2001_CryptoPro_A.Instance); + DefineCurve("GostR3410-2001-CryptoPro-B", CryptoProObjectIdentifiers.GostR3410x2001CryptoProB, + Holder_gostR3410_2001_CryptoPro_B.Instance); + DefineCurve("GostR3410-2001-CryptoPro-C", CryptoProObjectIdentifiers.GostR3410x2001CryptoProC, + Holder_gostR3410_2001_CryptoPro_C.Instance); + DefineCurve("GostR3410-2001-CryptoPro-XchA", CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA, + Holder_gostR3410_2001_CryptoPro_A.Instance); + DefineCurve("GostR3410-2001-CryptoPro-XchB", CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB, + Holder_gostR3410_2001_CryptoPro_XchB.Instance); + DefineCurve("Tc26-Gost-3410-12-256-paramSetA", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA, + Holder_id_tc26_gost_3410_12_256_paramSetA.Instance); + DefineCurve("Tc26-Gost-3410-12-512-paramSetA", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA, + Holder_id_tc26_gost_3410_12_512_paramSetA.Instance); + DefineCurve("Tc26-Gost-3410-12-512-paramSetB", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB, + Holder_id_tc26_gost_3410_12_512_paramSetB.Instance); + DefineCurve("Tc26-Gost-3410-12-512-paramSetC", RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC, + Holder_id_tc26_gost_3410_12_512_paramSetC.Instance); } public static X9ECParameters GetByNameX9(string name) { - DerObjectIdentifier oid = (DerObjectIdentifier)objIds[name]; + DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidX9(oid); } + public static X9ECParametersHolder GetByNameLazy(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOidLazy(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()); + X9ECParametersHolder holder = GetByOidLazy(oid); + return holder == null ? null : holder.Parameters; + } + + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)curves[oid]; } public static DerObjectIdentifier GetOid( diff --git a/crypto/src/asn1/gm/GMNamedCurves.cs b/crypto/src/asn1/gm/GMNamedCurves.cs index 866733cc6..9cb2cef14 100644 --- a/crypto/src/asn1/gm/GMNamedCurves.cs +++ b/crypto/src/asn1/gm/GMNamedCurves.cs @@ -44,19 +44,26 @@ namespace Org.BouncyCastle.Asn1.GM internal static readonly X9ECParametersHolder Instance = new SM2P256V1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger p = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF"); BigInteger a = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC"); BigInteger b = FromHex("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93"); - byte[] S = null; BigInteger n = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + X9ECPoint G = ConfigureBasepoint(curve, "0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"); - return new X9ECParameters(curve, G, n, h, S); + + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -70,20 +77,26 @@ namespace Org.BouncyCastle.Asn1.GM internal static readonly X9ECParametersHolder Instance = new WapiP192V1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger p = FromHex("BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F"); BigInteger a = FromHex("BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985"); BigInteger b = FromHex("1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1"); - byte[] S = null; BigInteger n = FromHex("BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; + X9ECPoint G = ConfigureBasepoint(curve, "044AD5F7048DE709AD51236DE65E4D4B482C836DC6E410664002BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -108,26 +121,35 @@ namespace Org.BouncyCastle.Asn1.GM DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance); } - public static X9ECParameters GetByName( - string name) + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + public static X9ECParametersHolder GetByNameLazy(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOidLazy(oid); + } + /** * return the X9ECParameters object for the named curve represented by * the passed in object identifier. Null if the curve isn't present. * * @param oid an object identifier representing a named curve, if present. */ - public static X9ECParameters GetByOid( - DerObjectIdentifier oid) + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid]; + X9ECParametersHolder holder = GetByOidLazy(oid); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)curves[oid]; + } + /** * return the object identifier signified by the passed in name. Null * if there is no object identifier associated with name. diff --git a/crypto/src/asn1/nist/NISTNamedCurves.cs b/crypto/src/asn1/nist/NISTNamedCurves.cs index f6c1598c6..ee256cc2b 100644 --- a/crypto/src/asn1/nist/NISTNamedCurves.cs +++ b/crypto/src/asn1/nist/NISTNamedCurves.cs @@ -50,11 +50,16 @@ namespace Org.BouncyCastle.Asn1.Nist DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1); } - public static X9ECParameters GetByName( - string name) + public static X9ECParameters GetByName(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : SecNamedCurves.GetByOid(oid); + } + + public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : GetByOid(oid); + return oid == null ? null : SecNamedCurves.GetByOidLazy(oid); } /** @@ -63,10 +68,14 @@ namespace Org.BouncyCastle.Asn1.Nist * * @param oid an object identifier representing a named curve, if present. */ - public static X9ECParameters GetByOid( - DerObjectIdentifier oid) + public static X9ECParameters GetByOid(DerObjectIdentifier oid) + { + return names.Contains(oid) ? SecNamedCurves.GetByOid(oid) : null; + } + + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return SecNamedCurves.GetByOid(oid); + return names.Contains(oid) ? SecNamedCurves.GetByOidLazy(oid) : null; } /** diff --git a/crypto/src/asn1/sec/SECNamedCurves.cs b/crypto/src/asn1/sec/SECNamedCurves.cs index 8ca0a6e21..68e9b7eca 100644 --- a/crypto/src/asn1/sec/SECNamedCurves.cs +++ b/crypto/src/asn1/sec/SECNamedCurves.cs @@ -51,22 +51,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp112r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = (2^128 - 3) / 76439 BigInteger p = FromHex("DB7C2ABF62E35E668076BEAD208B"); BigInteger a = FromHex("DB7C2ABF62E35E668076BEAD2088"); BigInteger b = FromHex("659EF8BA043916EEDE8911702B22"); - byte[] S = Hex.DecodeStrict("00F50B028E4D696E676875615175290472783FB1"); BigInteger n = FromHex("DB7C2ABF62E35E7628DFAC6561C5"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("00F50B028E4D696E676875615175290472783FB1"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0409487239995A5EE76B55F9C2F098A89CE5AF8724C0A23E0E0FF77500"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -80,22 +85,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp112r2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = (2^128 - 3) / 76439 BigInteger p = FromHex("DB7C2ABF62E35E668076BEAD208B"); BigInteger a = FromHex("6127C24C05F38A0AAAF65C0EF02C"); BigInteger b = FromHex("51DEF1815DB5ED74FCC34C85D709"); - byte[] S = Hex.DecodeStrict("002757A1114D696E6768756151755316C05E0BD4"); BigInteger n = FromHex("36DF0AAFD8B8D7597CA10520D04B"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("002757A1114D696E6768756151755316C05E0BD4"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "044BA30AB5E892B4E1649DD0928643ADCD46F5882E3747DEF36E956E97"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -109,22 +119,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp128r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^128 - 2^97 - 1 BigInteger p = FromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF"); BigInteger a = FromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC"); BigInteger b = FromHex("E87579C11079F43DD824993C2CEE5ED3"); - byte[] S = Hex.DecodeStrict("000E0D4D696E6768756151750CC03A4473D03679"); BigInteger n = FromHex("FFFFFFFE0000000075A30D1B9038A115"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("000E0D4D696E6768756151750CC03A4473D03679"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04161FF7528B899B2D0C28607CA52C5B86CF5AC8395BAFEB13C02DA292DDED7A83"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -138,22 +153,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp128r2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^128 - 2^97 - 1 BigInteger p = FromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF"); BigInteger a = FromHex("D6031998D1B3BBFEBF59CC9BBFF9AEE1"); BigInteger b = FromHex("5EEEFCA380D02919DC2C6558BB6D8A5D"); - byte[] S = Hex.DecodeStrict("004D696E67687561517512D8F03431FCE63B88F4"); BigInteger n = FromHex("3FFFFFFF7FFFFFFFBE0024720613B5A3"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("004D696E67687561517512D8F03431FCE63B88F4"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "047B6AA5D85E572983E6FB32A7CDEBC14027B6916A894D3AEE7106FE805FC34B44"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -167,13 +187,12 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp160k1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73"); BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.ValueOf(7); - byte[] S = null; BigInteger n = FromHex("0100000000000000000001B8FA16DFAB9ACA16B6B3"); BigInteger h = BigInteger.One; @@ -191,12 +210,18 @@ namespace Org.BouncyCastle.Asn1.Sec new BigInteger("96341f1138933bc2f503fd44", 16), 176)); - ECCurve curve = ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + return ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "043B4C382CE37AA192A4019E763036F4F5DD4D7EBB938CF935318FDCED6BC28286531733C3F03C4FEE"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -210,22 +235,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp160r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^160 - 2^31 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"); BigInteger a = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"); BigInteger b = FromHex("1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"); - byte[] S = Hex.DecodeStrict("1053CDE42C14D696E67687561517533BF3F83345"); BigInteger n = FromHex("0100000000000000000001F4C8F927AED3CA752257"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("1053CDE42C14D696E67687561517533BF3F83345"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "044A96B5688EF573284664698968C38BB913CBFC8223A628553168947D59DCC912042351377AC5FB32"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -239,22 +269,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp160r2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73"); BigInteger a = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70"); BigInteger b = FromHex("B4E134D3FB59EB8BAB57274904664D5AF50388BA"); - byte[] S = Hex.DecodeStrict("B99B99B099B323E02709A4D696E6768756151751"); BigInteger n = FromHex("0100000000000000000000351EE786A818F3A1A16B"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("B99B99B099B323E02709A4D696E6768756151751"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0452DCB034293A117E1F4FF11B30F7199D3144CE6DFEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -268,13 +303,12 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp192k1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37"); BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.ValueOf(3); - byte[] S = null; BigInteger n = FromHex("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D"); BigInteger h = BigInteger.One; @@ -292,12 +326,18 @@ namespace Org.BouncyCastle.Asn1.Sec new BigInteger("b3fb3400dec5c4adceb8655d4c94", 16), 208)); - ECCurve curve = ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + return ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -311,22 +351,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp192r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^192 - 2^64 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"); BigInteger a = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"); BigInteger b = FromHex("64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"); - byte[] S = Hex.DecodeStrict("3045AE6FC8422F64ED579528D38120EAE12196D5"); BigInteger n = FromHex("FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("3045AE6FC8422F64ED579528D38120EAE12196D5"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF101207192B95FFC8DA78631011ED6B24CDD573F977A11E794811"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -340,13 +385,12 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp224k1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^224 - 2^32 - 2^12 - 2^11 - 2^9 - 2^7 - 2^4 - 2 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D"); BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.ValueOf(5); - byte[] S = null; BigInteger n = FromHex("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7"); BigInteger h = BigInteger.One; @@ -364,12 +408,18 @@ namespace Org.BouncyCastle.Asn1.Sec new BigInteger("b8adf1378a6eb73409fa6c9c637ba7f5", 16), 240)); - ECCurve curve = ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + return ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -383,22 +433,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp224r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^224 - 2^96 + 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"); BigInteger a = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"); BigInteger b = FromHex("B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"); - byte[] S = Hex.DecodeStrict("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5"); BigInteger n = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -412,13 +467,12 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp256k1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"); BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.ValueOf(7); - byte[] S = null; BigInteger n = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"); BigInteger h = BigInteger.One; @@ -436,12 +490,18 @@ namespace Org.BouncyCastle.Asn1.Sec new BigInteger("e4437ed6010e88286f547fa90abfe4c42212", 16), 272)); - ECCurve curve = ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + return ConfigureCurveGlv(new FpCurve(p, a, b, n, h), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -455,22 +515,27 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp256r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^224 (2^32 - 1) + 2^192 + 2^96 - 1 BigInteger p = FromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"); BigInteger a = FromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"); BigInteger b = FromHex("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"); - byte[] S = Hex.DecodeStrict("C49D360886E704936A6678E1139D26B7819F7E90"); BigInteger n = FromHex("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("C49D360886E704936A6678E1139D26B7819F7E90"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -484,23 +549,28 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp384r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^384 - 2^128 - 2^96 + 2^32 - 1 BigInteger p = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"); BigInteger a = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"); BigInteger b = FromHex("B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"); - byte[] S = Hex.DecodeStrict("A335926AA319A27A1D00896A6773A4827ACDAC73"); BigInteger n = FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("A335926AA319A27A1D00896A6773A4827ACDAC73"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7" + "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -514,23 +584,28 @@ namespace Org.BouncyCastle.Asn1.Sec internal static readonly X9ECParametersHolder Instance = new Secp521r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { // p = 2^521 - 1 BigInteger p = FromHex("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); BigInteger a = FromHex("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"); BigInteger b = FromHex("0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00"); - byte[] S = Hex.DecodeStrict("D09E8800291CB85396CC6717393284AAA0DA64BA"); BigInteger n = FromHex("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h)); + return ConfigureCurve(new FpCurve(p, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("D09E8800291CB85396CC6717393284AAA0DA64BA"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66" + "011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -547,20 +622,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 113; private const int k = 9; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("003088250CA6E7C7FE649CE85820F7"); BigInteger b = FromHex("00E8BEE4D3E2260744188BE0E9C723"); - byte[] S = Hex.DecodeStrict("10E723AB14D696E6768756151756FEBF8FCB49A9"); BigInteger n = FromHex("0100000000000000D9CCEC8A39E56F"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("10E723AB14D696E6768756151756FEBF8FCB49A9"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04009D73616F35F4AB1407D73562C10F00A52830277958EE84D1315ED31886"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -577,20 +657,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 113; private const int k = 9; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("00689918DBEC7E5A0DD6DFC0AA55C7"); BigInteger b = FromHex("0095E9A9EC9B297BD4BF36E059184F"); - byte[] S = Hex.DecodeStrict("10C0FB15760860DEF1EEF4D696E676875615175D"); BigInteger n = FromHex("010000000000000108789B2496AF93"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("10C0FB15760860DEF1EEF4D696E676875615175D"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0401A57A6A7B26CA5EF52FCDB816479700B3ADC94ED1FE674C06E695BABA1D"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -609,20 +694,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 3; private const int k3 = 8; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("07A11B09A76B562144418FF3FF8C2570B8"); BigInteger b = FromHex("0217C05610884B63B9C6C7291678F9D341"); - byte[] S = Hex.DecodeStrict("4D696E676875615175985BD3ADBADA21B43A97E2"); BigInteger n = FromHex("0400000000000000023123953A9464B54D"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("4D696E676875615175985BD3ADBADA21B43A97E2"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040081BAF91FDF9833C40F9C181343638399078C6E7EA38C001F73C8134B1B4EF9E150"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -641,20 +731,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 3; private const int k3 = 8; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("03E5A88919D7CAFCBF415F07C2176573B2"); BigInteger b = FromHex("04B8266A46C55657AC734CE38F018F2192"); - byte[] S = Hex.DecodeStrict("985BD3ADBAD4D696E676875615175A21B43A97E3"); BigInteger n = FromHex("0400000000000000016954A233049BA98F"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("985BD3ADBAD4D696E676875615175A21B43A97E3"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040356DCD8F2F95031AD652D23951BB366A80648F06D867940A5366D9E265DE9EB240F"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -673,20 +768,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 6; private const int k3 = 7; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.One; BigInteger b = BigInteger.One; - byte[] S = null; BigInteger n = FromHex("04000000000000000000020108A2E0CC0D99F8A5EF"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0402FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE80289070FB05D38FF58321F2E800536D538CCDAA3D9"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -705,20 +805,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 6; private const int k3 = 7; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("07B6882CAAEFA84F9554FF8428BD88E246D2782AE2"); BigInteger b = FromHex("0713612DCDDCB40AAB946BDA29CA91F73AF958AFD9"); - byte[] S = Hex.DecodeStrict("24B7B137C8A14D696E6768756151756FD0DA2E5C"); BigInteger n = FromHex("03FFFFFFFFFFFFFFFFFFFF48AAB689C29CA710279B"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("24B7B137C8A14D696E6768756151756FD0DA2E5C"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040369979697AB43897789566789567F787A7876A65400435EDB42EFAFB2989D51FEFCE3C80988F41FF883"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -737,20 +842,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 6; private const int k3 = 7; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.One; BigInteger b = FromHex("020A601907B8C953CA1481EB10512F78744A3205FD"); - byte[] S = Hex.DecodeStrict("85E25BFE5C86226CDB12016F7553F9D0E693A268"); BigInteger n = FromHex("040000000000000000000292FE77E70C12A4234C33"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("85E25BFE5C86226CDB12016F7553F9D0E693A268"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0403F0EBA16286A2D57EA0991168D4994637E8343E3600D51FBC6C71A0094FA2CDD545B11C5C0C797324F1"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -767,20 +877,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 193; private const int k = 15; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("0017858FEB7A98975169E171F77B4087DE098AC8A911DF7B01"); BigInteger b = FromHex("00FDFB49BFE6C3A89FACADAA7A1E5BBC7CC1C2E5D831478814"); - byte[] S = Hex.DecodeStrict("103FAEC74D696E676875615175777FC5B191EF30"); BigInteger n = FromHex("01000000000000000000000000C7F34A778F443ACC920EBA49"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("103FAEC74D696E676875615175777FC5B191EF30"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0401F481BC5F0FF84A74AD6CDF6FDEF4BF6179625372D8C0C5E10025E399F2903712CCF3EA9E3A1AD17FB0B3201B6AF7CE1B05"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -797,20 +912,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 193; private const int k = 15; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = FromHex("0163F35A5137C2CE3EA6ED8667190B0BC43ECD69977702709B"); BigInteger b = FromHex("00C9BB9E8927D4D64C377E2AB2856A5B16E3EFB7F61D4316AE"); - byte[] S = Hex.DecodeStrict("10B7B4D696E676875615175137C8A16FD0DA2211"); BigInteger n = FromHex("010000000000000000000000015AAB561B005413CCD4EE99D5"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("10B7B4D696E676875615175137C8A16FD0DA2211"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0400D9B67D192E0367C803F39E1A7E82CA14A651350AAE617E8F01CE94335607C304AC29E7DEFBD9CA01F596F927224CDECF6C"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -827,20 +947,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 233; private const int k = 74; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.One; - byte[] S = null; BigInteger n = FromHex("8000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD612601DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -857,20 +982,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 233; private const int k = 74; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.One; BigInteger b = FromHex("0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD"); - byte[] S = Hex.DecodeStrict("74D59FF07F6B413D0EA14B344B20A2DB049B50C3"); BigInteger n = FromHex("01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("74D59FF07F6B413D0EA14B344B20A2DB049B50C3"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0400FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -887,20 +1017,25 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 239; private const int k = 158; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.One; - byte[] S = null; BigInteger n = FromHex("2000000000000000000000000000005A79FEC67CB6E91F1C1DA800E478A5"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0429A0B6A887A983E9730988A68727A8B2D126C44CC2CC7B2A6555193035DC76310804F12E549BDB011C103089E73510ACB275FC312A5DC6B76553F0CA"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -919,21 +1054,26 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 7; private const int k3 = 12; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.One; - byte[] S = null; BigInteger n = FromHex("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836" + "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -952,21 +1092,26 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 7; private const int k3 = 12; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.One; BigInteger b = FromHex("027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5"); - byte[] S = Hex.DecodeStrict("77E2B07370EB0F832A6DD5B62DFC88CD06BB84BE"); BigInteger n = FromHex("03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("77E2B07370EB0F832A6DD5B62DFC88CD06BB84BE"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053" + "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -983,21 +1128,26 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 409; private const int k = 87; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.One; - byte[] S = null; BigInteger n = FromHex("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746" + "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -1014,21 +1164,26 @@ namespace Org.BouncyCastle.Asn1.Sec private const int m = 409; private const int k = 87; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.One; BigInteger b = FromHex("0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F"); - byte[] S = Hex.DecodeStrict("4099B5A457F9D69F79213D094C4BCD4D4262210B"); BigInteger n = FromHex("010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("4099B5A457F9D69F79213D094C4BCD4D4262210B"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7" + "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -1047,21 +1202,26 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 5; private const int k3 = 10; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.Zero; BigInteger b = BigInteger.One; - byte[] S = null; BigInteger n = FromHex("020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972" + "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -1080,21 +1240,26 @@ namespace Org.BouncyCastle.Asn1.Sec private const int k2 = 5; private const int k3 = 10; - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger a = BigInteger.One; BigInteger b = FromHex("02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A"); - byte[] S = Hex.DecodeStrict("2AA058F73A0E33AB486B0F610410C53A7F132310"); BigInteger n = FromHex("03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47"); BigInteger h = BigInteger.ValueOf(2); - ECCurve curve = new F2mCurve(m, k1, k2, k3, a, b, n, h); + return ConfigureCurve(new F2mCurve(m, k1, k2, k3, a, b, n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("2AA058F73A0E33AB486B0F610410C53A7F132310"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19" + "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B"); - return new X9ECParameters(curve, G, n, h, S); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -1151,26 +1316,35 @@ namespace Org.BouncyCastle.Asn1.Sec DefineCurve("sect571r1", SecObjectIdentifiers.SecT571r1, Sect571r1Holder.Instance); } - public static X9ECParameters GetByName( - string name) + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + public static X9ECParametersHolder GetByNameLazy(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOidLazy(oid); + } + /** * return the X9ECParameters object for the named curve represented by * the passed in object identifier. Null if the curve isn't present. * * @param oid an object identifier representing a named curve, if present. */ - public static X9ECParameters GetByOid( - DerObjectIdentifier oid) + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid]; + X9ECParametersHolder holder = GetByOidLazy(oid); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)curves[oid]; + } + /** * return the object identifier signified by the passed in name. Null * if there is no object identifier associated with name. diff --git a/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs b/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs index c9a1e7d76..dcc160016 100644 --- a/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs +++ b/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs @@ -40,21 +40,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP160r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("E95E4A5F737059DC60DF5991D45029409E60FC09"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("E95E4A5F737059DC60DFC7AD95B3D8139515620F"), // q FromHex("340E7BE2A280EB74E2BE61BADA745D97E8F7C300"), // a FromHex("1E589A8595423412134FAA2DBDEC95C8D8675E58"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04BED5AF16EA3F6A4F62938C4631EB5AF7BDBCDBC31667CB477A1A8EC338F94741669C976316DA6321"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -65,22 +70,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP160t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("E95E4A5F737059DC60DF5991D45029409E60FC09"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("24DBFF5DEC9B986BBFE5295A29BFBAE45E0F5D0B"), // Z FromHex("E95E4A5F737059DC60DFC7AD95B3D8139515620F"), // q FromHex("E95E4A5F737059DC60DFC7AD95B3D8139515620C"), // a FromHex("7A556B6DAE535B7B51ED2C4D7DAA7A0B5C55F380"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04B199B13B9B34EFC1397E64BAEB05ACC265FF2378ADD6718B7C7C1961F0991B842443772152C9E0AD"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -91,21 +101,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP192r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("C302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("C302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297"), // q FromHex("6A91174076B1E0E19C39C031FE8685C1CAE040E5C69A28EF"), // a FromHex("469A28EF7C28CCA3DC721D044F4496BCCA7EF4146FBF25C9"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04C0A0647EAAB6A48753B033C56CB0F0900A2F5C4853375FD614B690866ABD5BB88B5F4828C1490002E6773FA2FA299B8F"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -116,22 +131,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP192t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("C302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("1B6F5CC8DB4DC7AF19458A9CB80DC2295E5EB9C3732104CB") // Z FromHex("C302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297"), // q FromHex("C302F41D932A36CDA7A3463093D18DB78FCE476DE1A86294"), // a FromHex("13D56FFAEC78681E68F9DEB43B35BEC2FB68542E27897B79"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "043AE9E58C82F63C30282E1FE7BBF43FA72C446AF6F4618129097E2C5667C2223A902AB5CA449D0084B7E5B3DE7CCC01C9"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -142,21 +162,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP224r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF"), // q FromHex("68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43"), // a FromHex("2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -167,22 +192,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP224t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("2DF271E14427A346910CF7A2E6CFA7B3F484E5C2CCE1C8B730E28B3F") // Z FromHex("D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF"), // q FromHex("D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FC"), // a FromHex("4B337D934104CD7BEF271BF60CED1ED20DA14C08B3BB64F18A60888D"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "046AB1E344CE25FF3896424E7FFE14762ECB49F8928AC0C76029B4D5800374E9F5143E568CD23F3F4D7C0D4B1E41C8CC0D1C6ABD5F1A46DB4C"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -193,21 +223,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP256r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"), // q FromHex("7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9"), // a FromHex("26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "048BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -218,22 +253,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP256t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("3E2D4BD9597B58639AE7AA669CAB9837CF5CF20A2C852D10F655668DFC150EF0") // Z FromHex("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"), // q FromHex("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5374"), // a FromHex("662C61C430D84EA4FE66A7733D0B76B7BF93EBC4AF2F49256AE58101FEE92B04"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04A3E8EB3CC1CFE7B7732213B23A656149AFA142C47AAFBC2B79A191562E1305F42D996C823439C56D7F7B22E14644417E69BCB6DE39D027001DABE8F35B25C9BE"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -244,21 +284,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP320r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27"), // q FromHex("3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4"), // a FromHex("520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0443BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E2061114FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -269,22 +314,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP320t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("15F75CAF668077F7E85B42EB01F0A81FF56ECD6191D55CB82B7D861458A18FEFC3E5AB7496F3C7B1") // Z FromHex("D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27"), // q FromHex("D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E24"), // a FromHex("A7F561E038EB1ED560B3D147DB782013064C19F27ED27C6780AAF77FB8A547CEB5B4FEF422340353"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04925BE9FB01AFC6FB4D3E7D4990010F813408AB106C4F09CB7EE07868CC136FFF3357F624A21BED5263BA3A7A27483EBF6671DBEF7ABB30EBEE084E58A0B077AD42A5A0989D1EE71B1B9BC0455FB0D2C3"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -295,21 +345,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP384r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53"), // q FromHex("7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826"), // a FromHex("04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "041D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -320,22 +375,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP384t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("41DFE8DD399331F7166A66076734A89CD0D2BCDB7D068E44E1F378F41ECBAE97D2D63DBC87BCCDDCCC5DA39E8589291C") // Z FromHex("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53"), // q FromHex("8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC50"), // a FromHex("7F519EADA7BDA81BD826DBA647910F8C4B9346ED8CCDC64E4B1ABD11756DCE1D2074AA263B88805CED70355A33B471EE"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0418DE98B02DB9A306F2AFCD7235F72A819B80AB12EBD653172476FECD462AABFFC4FF191B946A5F54D8D0AA2F418808CC25AB056962D30651A114AFD2755AD336747F93475B7A1FCA3B88F2B6A208CCFE469408584DC2B2912675BF5B9E582928"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -346,21 +406,26 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP512r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"), // q FromHex("7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"), // a FromHex("3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0481AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F8227DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -371,22 +436,27 @@ namespace Org.BouncyCastle.Asn1.TeleTrust internal static readonly X9ECParametersHolder Instance = new BrainpoolP512t1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( //FromHex("12EE58E6764838B69782136F0F2D3BA06E27695716054092E60A80BEDB212B64E585D90BCE13761F85C3F1D2A64E3BE8FEA2220F01EBA5EEB0F35DBD29D922AB") // Z FromHex("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"), // q FromHex("AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F0"), // a FromHex("7CBBBCF9441CFAB76E1890E46884EAE321F70C0BCB4981527897504BEC3E36A62BCDFA2304976540F6450085F2DAE145C22553B465763689180EA2571867423E"), // b n, h)); + } + + protected override X9ECParameters CreateParameters() + { + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04640ECE5C12788717B9C1BA06CBC2A6FEBA85842458C56DDE9DB1758D39C0313D82BA51735CDB3EA499AA77A7D6943A64F7A3F25FE26F06B51BAA2696FA9035DA5B534BD595F5AF0FA2C892376C84ACE1BB4E3019B71634C01131159CAE03CEE9D9932184BEEF216BD71DF2DADF86A627306ECFF96DBB8BACE198B61E00F8B332"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor); } } @@ -423,26 +493,35 @@ namespace Org.BouncyCastle.Asn1.TeleTrust DefineCurve("brainpoolP512t1", TeleTrusTObjectIdentifiers.BrainpoolP512T1, BrainpoolP512t1Holder.Instance); } - public static X9ECParameters GetByName( - string name) + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + public static X9ECParametersHolder GetByNameLazy(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOidLazy(oid); + } + /** * return the X9ECParameters object for the named curve represented by * the passed in object identifier. Null if the curve isn't present. * * @param oid an object identifier representing a named curve, if present. */ - public static X9ECParameters GetByOid( - DerObjectIdentifier oid) + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid]; + X9ECParametersHolder holder = GetByOidLazy(oid); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)curves[oid]; + } + /** * return the object identifier signified by the passed in name. Null * if there is no object identifier associated with name. diff --git a/crypto/src/asn1/x9/ECNamedCurveTable.cs b/crypto/src/asn1/x9/ECNamedCurveTable.cs index fcd2810e7..f0d70272b 100644 --- a/crypto/src/asn1/x9/ECNamedCurveTable.cs +++ b/crypto/src/asn1/x9/ECNamedCurveTable.cs @@ -54,6 +54,36 @@ namespace Org.BouncyCastle.Asn1.X9 return ecP; } + public static X9ECParametersHolder GetByNameLazy(string name) + { + X9ECParametersHolder holder = X962NamedCurves.GetByNameLazy(name); + if (null == holder) + { + holder = SecNamedCurves.GetByNameLazy(name); + } + if (null == holder) + { + holder = NistNamedCurves.GetByNameLazy(name); + } + if (null == holder) + { + holder = TeleTrusTNamedCurves.GetByNameLazy(name); + } + if (null == holder) + { + holder = AnssiNamedCurves.GetByNameLazy(name); + } + if (null == holder) + { + holder = ECGost3410NamedCurves.GetByNameLazy(name); + } + if (null == holder) + { + holder = GMNamedCurves.GetByNameLazy(name); + } + return holder; + } + public static string GetName(DerObjectIdentifier oid) { string name = X962NamedCurves.GetName(oid); @@ -156,6 +186,35 @@ namespace Org.BouncyCastle.Asn1.X9 return ecP; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + X9ECParametersHolder holder = X962NamedCurves.GetByOidLazy(oid); + if (null == holder) + { + holder = SecNamedCurves.GetByOidLazy(oid); + } + + // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup + + if (null == holder) + { + holder = TeleTrusTNamedCurves.GetByOidLazy(oid); + } + if (null == holder) + { + holder = AnssiNamedCurves.GetByOidLazy(oid); + } + if (null == holder) + { + holder = ECGost3410NamedCurves.GetByOidLazy(oid); + } + if (null == holder) + { + holder = GMNamedCurves.GetByOidLazy(oid); + } + return holder; + } + /** * return an enumeration of the names of the available curves. * diff --git a/crypto/src/asn1/x9/X962NamedCurves.cs b/crypto/src/asn1/x9/X962NamedCurves.cs index 131e58ea3..04f3d335a 100644 --- a/crypto/src/asn1/x9/X962NamedCurves.cs +++ b/crypto/src/asn1/x9/X962NamedCurves.cs @@ -43,21 +43,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime192v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("ffffffffffffffffffffffff99def836146bc9b1b4d22831"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"), FromHex("fffffffffffffffffffffffffffffffefffffffffffffffc"), FromHex("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("3045AE6FC8422f64ED579528D38120EAE12196D5"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("3045AE6FC8422f64ED579528D38120EAE12196D5")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -68,21 +74,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime192v2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("fffffffffffffffffffffffe5fb1a724dc80418648d8dd31"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"), FromHex("fffffffffffffffffffffffffffffffefffffffffffffffc"), FromHex("cc22d6dfb95c6b25e49c0d6364a4e5980c393aa21668d953"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("31a92ee2029fd10d901b113e990710f0d21ac6b6"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "03eea2bae7e1497842f2de7769cfe9c989c072ad696f48034a"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("31a92ee2029fd10d901b113e990710f0d21ac6b6")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -93,21 +105,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime192v3Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("ffffffffffffffffffffffff7a62d031c83f4294f640ec13"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( FromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"), FromHex("fffffffffffffffffffffffffffffffefffffffffffffffc"), FromHex("22123dc2395a05caa7423daeccc94760a7d462256bd56916"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("c469684435deb378c4b65ca9591e2a5763059a2e"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "027d29778100c65a1da1783716588dce2b8b4aee8e228f1896"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("c469684435deb378c4b65ca9591e2a5763059a2e")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -118,21 +136,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime239v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("7fffffffffffffffffffffff7fffff9e5e9a9f5d9071fbd1522688909d0b"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), FromHex("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc"), FromHex("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("e43bb460f0b80cc0c0b075798e948060f8321b7d"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("e43bb460f0b80cc0c0b075798e948060f8321b7d")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -143,21 +167,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime239v2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("7fffffffffffffffffffffff800000cfa7e8594377d414c03821bc582063"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), FromHex("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc"), FromHex("617fab6832576cbbfed50d99f0249c3fee58b94ba0038c7ae84c8c832f2c"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("e8b4011604095303ca3b8099982be09fcb9ae616"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0238af09d98727705120c921bb5e9e26296a3cdcf2f35757a0eafd87b830e7"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("e8b4011604095303ca3b8099982be09fcb9ae616")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -168,21 +198,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime239v3Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("7fffffffffffffffffffffff7fffff975deb41b3a6057c3c432146526551"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), FromHex("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc"), FromHex("255705fa2a306654b1f4cb03d6a750a30c250102d4988717d9ba15ab6d3e"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("7d7374168ffe3471b60a857686a19475d3bfa2ff"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "036768ae8e18bb92cfcf005c949aa2c6d94853d0e660bbf854b1c9505fe95a"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("7d7374168ffe3471b60a857686a19475d3bfa2ff")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -193,21 +229,27 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new Prime256v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"); BigInteger h = BigInteger.One; - ECCurve curve = ConfigureCurve(new FpCurve( + return ConfigureCurve(new FpCurve( new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853951"), FromHex("ffffffff00000001000000000000000000000000fffffffffffffffffffffffc"), FromHex("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("c49d360886e704936a6678e1139d26b7819f7e90"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "036b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("c49d360886e704936a6678e1139d26b7819f7e90")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -221,22 +263,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb163v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("0400000000000000000001E60FC8821CC74DAEAFC1"); BigInteger h = BigInteger.Two; - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 163, 1, 2, 8, FromHex("072546B5435234A422E0789675F432C89435DE5242"), FromHex("00C9517D06D5240D3CFF38C74B20B6CD4D6F9DD4D9"), n, h)); + } + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("D2C0FB15760860DEF1EEF4D696E6768756151754"); + ECCurve curve = Curve; + X9ECPoint G = ConfigureBasepoint(curve, "0307AF69989546103D79329FCC3D74880F33BBE803CB"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("D2C0FB15760860DEF1EEF4D696E6768756151754")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -247,22 +295,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb163v2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("03FFFFFFFFFFFFFFFFFFFDF64DE1151ADBB78F10A7"); BigInteger h = BigInteger.Two; - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 163, 1, 2, 8, FromHex("0108B39E77C4B108BED981ED0E890E117C511CF072"), FromHex("0667ACEB38AF4E488C407433FFAE4F1C811638DF20"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "030024266E4EB5106D0A964D92C4860E2671DB9B6CC5"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -273,22 +327,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb163v3Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("03FFFFFFFFFFFFFFFFFFFE1AEE140F110AFF961309"); BigInteger h = BigInteger.Two; - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 163, 1, 2, 8, FromHex("07A526C63D3E25A256A007699F5447E32AE456B50E"), FromHex("03F7061798EB99E238FD6F1BF95B48FEEB4854252B"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0202F9F87B7C574D0BDECF8A22E6524775F98CDEBDCB"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -299,22 +359,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb176w1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("010092537397ECA4F6145799D62B0A19CE06FE26AD"); BigInteger h = BigInteger.ValueOf(0xFF6E); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 176, 1, 2, 43, FromHex("E4E6DB2995065C407D9D39B8D0967B96704BA8E9C90B"), FromHex("5DDA470ABE6414DE8EC133AE28E9BBD7FCEC0AE0FFF2"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "038D16C2866798B600F9F08BB4A8E860F3298CE04A5798"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -325,22 +391,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb191v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("40000000000000000000000004A20E90C39067C893BBB9A5"); BigInteger h = BigInteger.Two; - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 191, 9, FromHex("2866537B676752636A68F56554E12640276B649EF7526267"), FromHex("2E45EF571F00786F67B0081B9495A3D95462F5DE0AA185EC"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = Hex.DecodeStrict("4E13CA542744D696E67687561517552F279A8C84"); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0236B3DAF8A23206F9C4F299D7B21A9C369137F2C84AE1AA0D"); - return new X9ECParameters(curve, G, n, h, Hex.DecodeStrict("4E13CA542744D696E67687561517552F279A8C84")); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -351,22 +423,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb191v2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("20000000000000000000000050508CB89F652824E06B8173"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 191, 9, FromHex("401028774D7777C7B7666D1366EA432071274F89FF01E718"), FromHex("0620048D28BCBD03B6249C99182B7C8CD19700C362C46A01"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "023809B2B7CC1B28CC5A87926AAD83FD28789E81E2C9E3BF10"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -377,22 +455,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb191v3Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("155555555555555555555555610C0B196812BFB6288A3EA3"); BigInteger h = BigInteger.ValueOf(6); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 191, 9, FromHex("6C01074756099122221056911C77D77E77A777E7E7E77FCB"), FromHex("71FE1AF926CF847989EFEF8DB459F66394D90F32AD3F15E8"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "03375D4CE24FDE434489DE8746E71786015009E66E38A926DD"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -403,22 +487,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb208w1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("0101BAF95C9723C57B6C21DA2EFF2D5ED588BDD5717E212F9D"); BigInteger h = BigInteger.ValueOf(0xFE48); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 208, 1, 2, 83, BigInteger.Zero, FromHex("C8619ED45A62E6212E1160349E2BFA844439FAFC2A3FD1638F9E"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0289FDFBE4ABE193DF9559ECF07AC0CE78554E2784EB8C1ED1A57A"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -429,22 +519,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb239v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("2000000000000000000000000000000F4D42FFE1492A4993F1CAD666E447"); BigInteger h = BigInteger.ValueOf(4); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 239, 36, FromHex("32010857077C5431123A46B808906756F543423E8D27877578125778AC76"), FromHex("790408F2EEDAF392B012EDEFB3392F30F4327C0CA3F31FC383C422AA8C16"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0257927098FA932E7C0A96D3FD5B706EF7E5F5C156E16B7E7C86038552E91D"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -455,22 +551,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb239v2Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("1555555555555555555555555555553C6F2885259C31E3FCDF154624522D"); BigInteger h = BigInteger.ValueOf(6); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 239, 36, FromHex("4230017757A767FAE42398569B746325D45313AF0766266479B75654E65F"), FromHex("5037EA654196CFF0CD82B2C14A2FCF2E3FF8775285B545722F03EACDB74B"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0228F9D04E900069C8DC47A08534FE76D2B900B7D7EF31F5709F200C4CA205"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -481,22 +583,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb239v3Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("0CCCCCCCCCCCCCCCCCCCCCCCCCCCCCAC4912D2D9DF903EF9888B8A0E4CFF"); BigInteger h = BigInteger.ValueOf(10); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 239, 36, FromHex("01238774666A67766D6676F778E676B66999176666E687666D8766C66A9F"), FromHex("6A941977BA9F6A435199ACFC51067ED587F519C5ECB541B8E44111DE1D40"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0370F6E9D04D289C4E89913CE3530BFDE903977D42B146D539BF1BDE4E9C92"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -507,22 +615,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb272w1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("0100FAF51354E0E39E4892DF6E319C72C8161603FA45AA7B998A167B8F1E629521"); BigInteger h = BigInteger.ValueOf(0xFF06); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 272, 1, 3, 56, FromHex("91A091F03B5FBA4AB2CCF49C4EDD220FB028712D42BE752B2C40094DBACDB586FB20"), FromHex("7167EFC92BB2E3CE7C8AAAFF34E12A9C557003D7C73A6FAF003F99F6CC8482E540F7"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "026108BABB2CEEBCF787058A056CBE0CFE622D7723A289E08A07AE13EF0D10D171DD8D"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -533,22 +647,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb304w1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("0101D556572AABAC800101D556572AABAC8001022D5C91DD173F8FB561DA6899164443051D"); BigInteger h = BigInteger.ValueOf(0xFE2E); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 304, 1, 2, 11, FromHex("FD0D693149A118F651E6DCE6802085377E5F882D1B510B44160074C1288078365A0396C8E681"), FromHex("BDDB97E555A50A908E43B01C798EA5DAA6788F1EA2794EFCF57166B8C14039601E55827340BE"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "02197B07845E9BE2D96ADB0F5F3C7F2CFFBD7A3EB8B6FEC35C7FD67F26DDF6285A644F740A2614"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -559,22 +679,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb359v1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("01AF286BCA1AF286BCA1AF286BCA1AF286BCA1AF286BC9FB8F6B85C556892C20A7EB964FE7719E74F490758D3B"); BigInteger h = BigInteger.ValueOf(0x4C); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 359, 68, FromHex("5667676A654B20754F356EA92017D946567C46675556F19556A04616B567D223A5E05656FB549016A96656A557"), FromHex("2472E2D0197C49363F1FE7F5B6DB075D52B6947D135D8CA445805D39BC345626089687742B6329E70680231988"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "033C258EF3047767E7EDE0F1FDAA79DAEE3841366A132E163ACED4ED2401DF9C6BDCDE98E8E707C07A2239B1B097"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -585,22 +711,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2pnb368w1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("010090512DA9AF72B08349D98A5DD4C7B0532ECA51CE03E2D10F3B7AC579BD87E909AE40A6F131E9CFCE5BD967"); BigInteger h = BigInteger.ValueOf(0xFF70); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 368, 1, 2, 85, FromHex("E0D2EE25095206F5E2A4F9ED229F1F256E79A0E2B455970D8D0D865BD94778C576D62F0AB7519CCD2A1A906AE30D"), FromHex("FC1217D4320A90452C760A58EDCD30C8DD069B3C34453837A34ED50CB54917E1C2112D84D164F444F8F74786046A"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "021085E2755381DCCCE3C1557AFA10C2F0C0C2825646C5B34A394CBCFA8BC16B22E7E789E927BE216F02E1FB136A5F"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -611,22 +743,28 @@ namespace Org.BouncyCastle.Asn1.X9 internal static readonly X9ECParametersHolder Instance = new C2tnb431r1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { BigInteger n = FromHex("0340340340340340340340340340340340340340340340340340340323C313FAB50589703B5EC68D3587FEC60D161CC149C1AD4A91"); BigInteger h = BigInteger.ValueOf(0x2760); - ECCurve curve = ConfigureCurve(new F2mCurve( + return ConfigureCurve(new F2mCurve( 431, 120, FromHex("1A827EF00DD6FC0E234CAF046C6A5D8A85395B236CC4AD2CF32A0CADBDC9DDF620B0EB9906D0957F6C6FEACD615468DF104DE296CD8F"), FromHex("10D9B4A3D9047D8B154359ABFB1B7F5485B04CEB868237DDC9DEDA982A679A5A919B626D4E50A8DD731B107A9962381FB5D807BF2618"), n, h)); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "02120FC05D3C67A99DE161D2F4092622FECA701BE4F50F4758714E8A87BBF2A658EF8C21E7C5EFE965361F6C2999C0C247B0DBD70CE6B7"); - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); } } @@ -672,26 +810,35 @@ namespace Org.BouncyCastle.Asn1.X9 DefineCurve("c2tnb431r1", X9ObjectIdentifiers.C2Tnb431r1, C2tnb431r1Holder.Instance); } - public static X9ECParameters GetByName( - string name) + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + public static X9ECParametersHolder GetByNameLazy(string name) + { + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOidLazy(oid); + } + /** * return the X9ECParameters object for the named curve represented by * the passed in object identifier. Null if the curve isn't present. * * @param oid an object identifier representing a named curve, if present. */ - public static X9ECParameters GetByOid( - DerObjectIdentifier oid) + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid]; + X9ECParametersHolder holder = GetByOidLazy(oid); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)curves[oid]; + } + /** * return the object identifier signified by the passed in name. Null * if there is no object identifier associated with name. diff --git a/crypto/src/asn1/x9/X9ECParametersHolder.cs b/crypto/src/asn1/x9/X9ECParametersHolder.cs index e802b738c..ea72cc6ac 100644 --- a/crypto/src/asn1/x9/X9ECParametersHolder.cs +++ b/crypto/src/asn1/x9/X9ECParametersHolder.cs @@ -1,25 +1,49 @@ +using Org.BouncyCastle.Math.EC; + namespace Org.BouncyCastle.Asn1.X9 { public abstract class X9ECParametersHolder { - private X9ECParameters parameters; + private ECCurve m_curve; + private X9ECParameters m_parameters; + + public ECCurve Curve + { + get + { + lock (this) + { + if (m_curve == null) + { + m_curve = CreateCurve(); + } - public X9ECParameters Parameters + return m_curve; + } + } + } + + public X9ECParameters Parameters { get { lock (this) { - if (parameters == null) + if (m_parameters == null) { - parameters = CreateParameters(); + m_parameters = CreateParameters(); } - return parameters; + return m_parameters; } } } - protected abstract X9ECParameters CreateParameters(); + protected virtual ECCurve CreateCurve() + { + return CreateParameters().Curve; + } + + protected abstract X9ECParameters CreateParameters(); } } diff --git a/crypto/src/crypto/ec/CustomNamedCurves.cs b/crypto/src/crypto/ec/CustomNamedCurves.cs index 0eee66a33..166c0ad75 100644 --- a/crypto/src/crypto/ec/CustomNamedCurves.cs +++ b/crypto/src/crypto/ec/CustomNamedCurves.cs @@ -47,14 +47,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class Curve25519Holder : X9ECParametersHolder { - private Curve25519Holder() { } + private Curve25519Holder() {} internal static readonly X9ECParametersHolder Instance = new Curve25519Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new Curve25519()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new Curve25519()); + ECCurve curve = Curve; /* * NOTE: Curve25519 was specified in Montgomery form. Rewriting in Weierstrass form @@ -78,14 +83,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP128R1Holder : X9ECParametersHolder { - private SecP128R1Holder() { } + private SecP128R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP128R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP128R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("000E0D4D696E6768756151750CC03A4473D03679"); - ECCurve curve = ConfigureCurve(new SecP128R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04161FF7528B899B2D0C28607CA52C5B86CF5AC8395BAFEB13C02DA292DDED7A83"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -98,13 +108,12 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP160K1Holder : X9ECParametersHolder { - private SecP160K1Holder() { } + private SecP160K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP160K1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { - byte[] S = null; GlvTypeBParameters glv = new GlvTypeBParameters( new BigInteger("9ba48cba5ebcb9b6bd33b92830b2a2e0e192f10a", 16), new BigInteger("c39c6c3b3a36d7701b9c71a1f5804ae5d0003f4", 16), @@ -118,7 +127,13 @@ namespace Org.BouncyCastle.Crypto.EC new BigInteger("9162fbe73984472a0a9d0590", 16), new BigInteger("96341f1138933bc2f503fd44", 16), 176)); - ECCurve curve = ConfigureCurveGlv(new SecP160K1Curve(), glv); + return ConfigureCurveGlv(new SecP160K1Curve(), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "043B4C382CE37AA192A4019E763036F4F5DD4D7EBB938CF935318FDCED6BC28286531733C3F03C4FEE"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -131,14 +146,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP160R1Holder : X9ECParametersHolder { - private SecP160R1Holder() { } + private SecP160R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP160R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP160R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("1053CDE42C14D696E67687561517533BF3F83345"); - ECCurve curve = ConfigureCurve(new SecP160R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "044A96B5688EF573284664698968C38BB913CBFC8223A628553168947D59DCC912042351377AC5FB32"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -151,14 +171,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP160R2Holder : X9ECParametersHolder { - private SecP160R2Holder() { } + private SecP160R2Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP160R2Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP160R2Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("B99B99B099B323E02709A4D696E6768756151751"); - ECCurve curve = ConfigureCurve(new SecP160R2Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0452DCB034293A117E1F4FF11B30F7199D3144CE6DFEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -171,13 +196,12 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP192K1Holder : X9ECParametersHolder { - private SecP192K1Holder() { } + private SecP192K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP192K1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { - byte[] S = null; GlvTypeBParameters glv = new GlvTypeBParameters( new BigInteger("bb85691939b869c1d087f601554b96b80cb4f55b35f433c2", 16), new BigInteger("3d84f26c12238d7b4f3d516613c1759033b1a5800175d0b1", 16), @@ -191,7 +215,13 @@ namespace Org.BouncyCastle.Crypto.EC new BigInteger("71169be7330b3038edb025f1d0f9", 16), new BigInteger("b3fb3400dec5c4adceb8655d4c94", 16), 208)); - ECCurve curve = ConfigureCurveGlv(new SecP192K1Curve(), glv); + return ConfigureCurveGlv(new SecP192K1Curve(), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -204,14 +234,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP192R1Holder : X9ECParametersHolder { - private SecP192R1Holder() { } + private SecP192R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP192R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP192R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("3045AE6FC8422F64ED579528D38120EAE12196D5"); - ECCurve curve = ConfigureCurve(new SecP192R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF101207192B95FFC8DA78631011ED6B24CDD573F977A11E794811"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -224,13 +259,12 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP224K1Holder : X9ECParametersHolder { - private SecP224K1Holder() { } + private SecP224K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP224K1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { - byte[] S = null; GlvTypeBParameters glv = new GlvTypeBParameters( new BigInteger("fe0e87005b4e83761908c5131d552a850b3f58b749c37cf5b84d6768", 16), new BigInteger("60dcd2104c4cbc0be6eeefc2bdd610739ec34e317f9b33046c9e4788", 16), @@ -244,7 +278,13 @@ namespace Org.BouncyCastle.Crypto.EC new BigInteger("6b8cf07d4ca75c88957d9d67059037a4", 16), new BigInteger("b8adf1378a6eb73409fa6c9c637ba7f5", 16), 240)); - ECCurve curve = ConfigureCurveGlv(new SecP224K1Curve(), glv); + return ConfigureCurveGlv(new SecP224K1Curve(), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -257,14 +297,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP224R1Holder : X9ECParametersHolder { - private SecP224R1Holder() { } + private SecP224R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP224R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP224R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5"); - ECCurve curve = ConfigureCurve(new SecP224R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -281,9 +326,8 @@ namespace Org.BouncyCastle.Crypto.EC internal static readonly X9ECParametersHolder Instance = new SecP256K1Holder(); - protected override X9ECParameters CreateParameters() + protected override ECCurve CreateCurve() { - byte[] S = null; GlvTypeBParameters glv = new GlvTypeBParameters( new BigInteger("7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee", 16), new BigInteger("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72", 16), @@ -297,7 +341,13 @@ namespace Org.BouncyCastle.Crypto.EC new BigInteger("3086d221a7d46bcde86c90e49284eb153dab", 16), new BigInteger("e4437ed6010e88286f547fa90abfe4c42212", 16), 272)); - ECCurve curve = ConfigureCurveGlv(new SecP256K1Curve(), glv); + return ConfigureCurveGlv(new SecP256K1Curve(), glv); + } + + protected override X9ECParameters CreateParameters() + { + byte[] S = null; + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -314,10 +364,15 @@ namespace Org.BouncyCastle.Crypto.EC internal static readonly X9ECParametersHolder Instance = new SecP256R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP256R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("C49D360886E704936A6678E1139D26B7819F7E90"); - ECCurve curve = ConfigureCurve(new SecP256R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -330,14 +385,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP384R1Holder : X9ECParametersHolder { - private SecP384R1Holder() { } + private SecP384R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP384R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP384R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("A335926AA319A27A1D00896A6773A4827ACDAC73"); - ECCurve curve = ConfigureCurve(new SecP384R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7" + "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"); @@ -351,14 +411,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecP521R1Holder : X9ECParametersHolder { - private SecP521R1Holder() { } + private SecP521R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecP521R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecP521R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("D09E8800291CB85396CC6717393284AAA0DA64BA"); - ECCurve curve = ConfigureCurve(new SecP521R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66" + "011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650"); @@ -372,14 +437,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT113R1Holder : X9ECParametersHolder { - private SecT113R1Holder() { } + private SecT113R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT113R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT113R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("10E723AB14D696E6768756151756FEBF8FCB49A9"); - ECCurve curve = ConfigureCurve(new SecT113R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04009D73616F35F4AB1407D73562C10F00A52830277958EE84D1315ED31886"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -392,14 +462,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT113R2Holder : X9ECParametersHolder { - private SecT113R2Holder() { } + private SecT113R2Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT113R2Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT113R2Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("10C0FB15760860DEF1EEF4D696E676875615175D"); - ECCurve curve = ConfigureCurve(new SecT113R2Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0401A57A6A7B26CA5EF52FCDB816479700B3ADC94ED1FE674C06E695BABA1D"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -412,14 +487,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT131R1Holder : X9ECParametersHolder { - private SecT131R1Holder() { } + private SecT131R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT131R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT131R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("4D696E676875615175985BD3ADBADA21B43A97E2"); - ECCurve curve = ConfigureCurve(new SecT131R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040081BAF91FDF9833C40F9C181343638399078C6E7EA38C001F73C8134B1B4EF9E150"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -432,14 +512,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT131R2Holder : X9ECParametersHolder { - private SecT131R2Holder() { } + private SecT131R2Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT131R2Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT131R2Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("985BD3ADBAD4D696E676875615175A21B43A97E3"); - ECCurve curve = ConfigureCurve(new SecT131R2Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040356DCD8F2F95031AD652D23951BB366A80648F06D867940A5366D9E265DE9EB240F"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -452,14 +537,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT163K1Holder : X9ECParametersHolder { - private SecT163K1Holder() { } + private SecT163K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT163K1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT163K1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SecT163K1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0402FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE80289070FB05D38FF58321F2E800536D538CCDAA3D9"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -472,14 +562,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT163R1Holder : X9ECParametersHolder { - private SecT163R1Holder() { } + private SecT163R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT163R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT163R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("24B7B137C8A14D696E6768756151756FD0DA2E5C"); - ECCurve curve = ConfigureCurve(new SecT163R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "040369979697AB43897789566789567F787A7876A65400435EDB42EFAFB2989D51FEFCE3C80988F41FF883"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -492,14 +587,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT163R2Holder : X9ECParametersHolder { - private SecT163R2Holder() { } + private SecT163R2Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT163R2Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT163R2Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("85E25BFE5C86226CDB12016F7553F9D0E693A268"); - ECCurve curve = ConfigureCurve(new SecT163R2Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0403F0EBA16286A2D57EA0991168D4994637E8343E3600D51FBC6C71A0094FA2CDD545B11C5C0C797324F1"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -512,14 +612,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT193R1Holder : X9ECParametersHolder { - private SecT193R1Holder() { } + private SecT193R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT193R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT193R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("103FAEC74D696E676875615175777FC5B191EF30"); - ECCurve curve = ConfigureCurve(new SecT193R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0401F481BC5F0FF84A74AD6CDF6FDEF4BF6179625372D8C0C5E10025E399F2903712CCF3EA9E3A1AD17FB0B3201B6AF7CE1B05"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -532,14 +637,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT193R2Holder : X9ECParametersHolder { - private SecT193R2Holder() { } + private SecT193R2Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT193R2Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT193R2Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("10B7B4D696E676875615175137C8A16FD0DA2211"); - ECCurve curve = ConfigureCurve(new SecT193R2Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0400D9B67D192E0367C803F39E1A7E82CA14A651350AAE617E8F01CE94335607C304AC29E7DEFBD9CA01F596F927224CDECF6C"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -552,14 +662,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT233K1Holder : X9ECParametersHolder { - private SecT233K1Holder() { } + private SecT233K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT233K1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT233K1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SecT233K1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD612601DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -572,14 +687,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT233R1Holder : X9ECParametersHolder { - private SecT233R1Holder() { } + private SecT233R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT233R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT233R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("74D59FF07F6B413D0EA14B344B20A2DB049B50C3"); - ECCurve curve = ConfigureCurve(new SecT233R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0400FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -592,14 +712,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT239K1Holder : X9ECParametersHolder { - private SecT239K1Holder() { } + private SecT239K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT239K1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT239K1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SecT239K1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0429A0B6A887A983E9730988A68727A8B2D126C44CC2CC7B2A6555193035DC76310804F12E549BDB011C103089E73510ACB275FC312A5DC6B76553F0CA"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -612,14 +737,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT283K1Holder : X9ECParametersHolder { - private SecT283K1Holder() { } + private SecT283K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT283K1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT283K1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SecT283K1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836" + "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259"); @@ -633,14 +763,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT283R1Holder : X9ECParametersHolder { - private SecT283R1Holder() { } + private SecT283R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT283R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT283R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("77E2B07370EB0F832A6DD5B62DFC88CD06BB84BE"); - ECCurve curve = ConfigureCurve(new SecT283R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053" + "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4"); @@ -654,14 +789,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT409K1Holder : X9ECParametersHolder { - private SecT409K1Holder() { } + private SecT409K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT409K1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT409K1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SecT409K1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746" + "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B"); @@ -675,14 +815,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT409R1Holder : X9ECParametersHolder { - private SecT409R1Holder() { } + private SecT409R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT409R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT409R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("4099B5A457F9D69F79213D094C4BCD4D4262210B"); - ECCurve curve = ConfigureCurve(new SecT409R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7" + "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706"); @@ -696,14 +841,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT571K1Holder : X9ECParametersHolder { - private SecT571K1Holder() { } + private SecT571K1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT571K1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT571K1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SecT571K1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972" + "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3"); @@ -717,14 +867,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SecT571R1Holder : X9ECParametersHolder { - private SecT571R1Holder() { } + private SecT571R1Holder() {} internal static readonly X9ECParametersHolder Instance = new SecT571R1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SecT571R1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = Hex.DecodeStrict("2AA058F73A0E33AB486B0F610410C53A7F132310"); - ECCurve curve = ConfigureCurve(new SecT571R1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "04" + "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19" + "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B"); @@ -738,14 +893,19 @@ namespace Org.BouncyCastle.Crypto.EC internal class SM2P256V1Holder : X9ECParametersHolder { - private SM2P256V1Holder() { } + private SM2P256V1Holder() {} internal static readonly X9ECParametersHolder Instance = new SM2P256V1Holder(); + protected override ECCurve CreateCurve() + { + return ConfigureCurve(new SM2P256V1Curve()); + } + protected override X9ECParameters CreateParameters() { byte[] S = null; - ECCurve curve = ConfigureCurve(new SM2P256V1Curve()); + ECCurve curve = Curve; X9ECPoint G = ConfigureBasepoint(curve, "0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"); return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); @@ -849,10 +1009,15 @@ namespace Org.BouncyCastle.Crypto.EC public static X9ECParameters GetByName(string name) { - X9ECParametersHolder holder = (X9ECParametersHolder)nameToCurve[Platform.ToUpperInvariant(name)]; + X9ECParametersHolder holder = GetByNameLazy(name); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByNameLazy(string name) + { + return (X9ECParametersHolder)nameToCurve[Platform.ToUpperInvariant(name)]; + } + /** * return the X9ECParameters object for the named curve represented by * the passed in object identifier. Null if the curve isn't present. @@ -861,10 +1026,15 @@ namespace Org.BouncyCastle.Crypto.EC */ public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = (X9ECParametersHolder)oidToCurve[oid]; + X9ECParametersHolder holder = GetByOidLazy(oid); return holder == null ? null : holder.Parameters; } + public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) + { + return (X9ECParametersHolder)oidToCurve[oid]; + } + /** * return the object identifier signified by the passed in name. Null * if there is no object identifier associated with name. diff --git a/crypto/src/crypto/generators/ECKeyPairGenerator.cs b/crypto/src/crypto/generators/ECKeyPairGenerator.cs index 6a710c62e..7b6ee168b 100644 --- a/crypto/src/crypto/generators/ECKeyPairGenerator.cs +++ b/crypto/src/crypto/generators/ECKeyPairGenerator.cs @@ -1,18 +1,14 @@ using System; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Sec; -using Org.BouncyCastle.Asn1.TeleTrust; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.EC; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Multiplier; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Generators { @@ -133,10 +129,28 @@ namespace Org.BouncyCastle.Crypto.Generators return new FixedPointCombMultiplier(); } - internal static X9ECParameters FindECCurveByOid(DerObjectIdentifier oid) + internal static X9ECParameters FindECCurveByName(string name) { - // TODO ECGost3410NamedCurves support (returns ECDomainParameters though) + X9ECParameters ecP = CustomNamedCurves.GetByName(name); + if (ecP == null) + { + ecP = ECNamedCurveTable.GetByName(name); + } + return ecP; + } + + internal static X9ECParametersHolder FindECCurveByNameLazy(string name) + { + X9ECParametersHolder holder = CustomNamedCurves.GetByNameLazy(name); + if (holder == null) + { + holder = ECNamedCurveTable.GetByNameLazy(name); + } + return holder; + } + internal static X9ECParameters FindECCurveByOid(DerObjectIdentifier oid) + { X9ECParameters ecP = CustomNamedCurves.GetByOid(oid); if (ecP == null) { @@ -145,6 +159,16 @@ namespace Org.BouncyCastle.Crypto.Generators return ecP; } + internal static X9ECParametersHolder FindECCurveByOidLazy(DerObjectIdentifier oid) + { + X9ECParametersHolder holder = CustomNamedCurves.GetByOidLazy(oid); + if (holder == null) + { + holder = ECNamedCurveTable.GetByOidLazy(oid); + } + return holder; + } + internal static ECPublicKeyParameters GetCorrespondingPublicKey( ECPrivateKeyParameters privKey) { diff --git a/crypto/src/crypto/tls/TlsEccUtilities.cs b/crypto/src/crypto/tls/TlsEccUtilities.cs index 5ef72a74e..ec8752a62 100644 --- a/crypto/src/crypto/tls/TlsEccUtilities.cs +++ b/crypto/src/crypto/tls/TlsEccUtilities.cs @@ -115,13 +115,9 @@ namespace Org.BouncyCastle.Crypto.Tls // Parameters are lazily created the first time a particular curve is accessed - X9ECParameters ecP = CustomNamedCurves.GetByName(curveName); + X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByName(curveName); if (ecP == null) - { - ecP = ECNamedCurveTable.GetByName(curveName); - if (ecP == null) - return null; - } + return null; // It's a bit inefficient to do this conversion every time return new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed()); diff --git a/crypto/src/openpgp/PgpPublicKey.cs b/crypto/src/openpgp/PgpPublicKey.cs index 33fabc8aa..0fcffc35a 100644 --- a/crypto/src/openpgp/PgpPublicKey.cs +++ b/crypto/src/openpgp/PgpPublicKey.cs @@ -2,11 +2,11 @@ using System; using System.Collections; using System.IO; -using Org.BouncyCastle.Asn1.Sec; +using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.Gnu; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Generators; -using Org.BouncyCastle.Crypto.IO; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; @@ -126,7 +126,26 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } else if (key is ECPublicBcpgKey) { - this.keyStrength = ECKeyPairGenerator.FindECCurveByOid(((ECPublicBcpgKey)key).CurveOid).Curve.FieldSize; + DerObjectIdentifier curveOid = ((ECPublicBcpgKey)key).CurveOid; + if (GnuObjectIdentifiers.Ed25519.Equals(curveOid) + //|| CryptlibObjectIdentifiers.curvey25519.Equals(curveOid) + ) + { + this.keyStrength = 256; + } + else + { + X9ECParametersHolder ecParameters = ECKeyPairGenerator.FindECCurveByOidLazy(curveOid); + + if (ecParameters != null) + { + this.keyStrength = ecParameters.Curve.FieldSize; + } + else + { + this.keyStrength = -1; // unknown + } + } } } } diff --git a/crypto/src/openssl/PEMReader.cs b/crypto/src/openssl/PEMReader.cs index 9a5f99b1a..65d3f5ad6 100644 --- a/crypto/src/openssl/PEMReader.cs +++ b/crypto/src/openssl/PEMReader.cs @@ -379,22 +379,12 @@ namespace Org.BouncyCastle.OpenSsl // return GetCurveParameters(oid.Id); // } - //private static ECDomainParameters GetCurveParameters( - private static X9ECParameters GetCurveParameters( - string name) + private static X9ECParameters GetCurveParameters(string name) { - // TODO ECGost3410NamedCurves support (returns ECDomainParameters though) - - X9ECParameters ecP = CustomNamedCurves.GetByName(name); - if (ecP == null) - { - ecP = ECNamedCurveTable.GetByName(name); - } - + X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByName(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; } } diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index 408c8b6a0..a8c4d94d0 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -234,7 +234,7 @@ namespace Org.BouncyCastle.Security if (parameters.IsNamedCurve) { DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(parameters.Parameters); - X9ECParameters ecP = ECNamedCurveTable.GetByOid(oid); + X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByOid(oid); ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(oid, ecP), gostParams.PublicKeyParamSet, gostParams.DigestParamSet, diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs b/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs index f4c5cfc5d..61d11fb42 100644 --- a/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs +++ b/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs @@ -48,13 +48,9 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC // Parameters are lazily created the first time a particular curve is accessed string curveName = NamedGroup.GetCurveName(namedGroup); - X9ECParameters ecP = CustomNamedCurves.GetByName(curveName); + X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByName(curveName); if (ecP == null) - { - ecP = ECNamedCurveTable.GetByName(curveName); - if (ecP == null) - return null; - } + return null; // It's a bit inefficient to do this conversion every time return new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed()); -- cgit 1.4.1