From b72579c4b1200459f959bcd6a25364e239420573 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jun 2022 15:07:29 +0700 Subject: Cleanup Exception classes --- crypto/test/src/util/test/SimpleTest.cs | 2 +- crypto/test/src/util/test/TestFailedException.cs | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) (limited to 'crypto/test') diff --git a/crypto/test/src/util/test/SimpleTest.cs b/crypto/test/src/util/test/SimpleTest.cs index fc88be103..1695164e5 100644 --- a/crypto/test/src/util/test/SimpleTest.cs +++ b/crypto/test/src/util/test/SimpleTest.cs @@ -108,7 +108,7 @@ namespace Org.BouncyCastle.Utilities.Test } catch (TestFailedException e) { - return e.GetResult(); + return e.Result; } catch (Exception e) { diff --git a/crypto/test/src/util/test/TestFailedException.cs b/crypto/test/src/util/test/TestFailedException.cs index 215e1fd20..2ea66087d 100644 --- a/crypto/test/src/util/test/TestFailedException.cs +++ b/crypto/test/src/util/test/TestFailedException.cs @@ -2,23 +2,20 @@ using System; namespace Org.BouncyCastle.Utilities.Test { -#if !PORTABLE - [Serializable] -#endif public class TestFailedException : Exception { - private ITestResult _result; + private readonly ITestResult m_result; - public TestFailedException( - ITestResult result) + public TestFailedException(ITestResult result) + : base() { - _result = result; + m_result = result; } - public ITestResult GetResult() + public ITestResult Result { - return _result; + get { return m_result; } } } } -- cgit 1.5.1 From 0fc832a4e64f4b5bd3413531979bb90117ce375f Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jun 2022 17:31:14 +0700 Subject: Remove bridging version of Curve25519 --- crypto/src/crypto/ec/CustomNamedCurves.cs | 120 +++------ crypto/src/math/ec/custom/djb/Curve25519.cs | 171 ------------ crypto/src/math/ec/custom/djb/Curve25519Field.cs | 292 --------------------- .../math/ec/custom/djb/Curve25519FieldElement.cs | 233 ---------------- crypto/src/math/ec/custom/djb/Curve25519Point.cs | 287 -------------------- crypto/test/src/crypto/test/ECTest.cs | 15 +- 6 files changed, 50 insertions(+), 1068 deletions(-) delete mode 100644 crypto/src/math/ec/custom/djb/Curve25519.cs delete mode 100644 crypto/src/math/ec/custom/djb/Curve25519Field.cs delete mode 100644 crypto/src/math/ec/custom/djb/Curve25519FieldElement.cs delete mode 100644 crypto/src/math/ec/custom/djb/Curve25519Point.cs (limited to 'crypto/test') diff --git a/crypto/src/crypto/ec/CustomNamedCurves.cs b/crypto/src/crypto/ec/CustomNamedCurves.cs index 166c0ad75..bf2b7f486 100644 --- a/crypto/src/crypto/ec/CustomNamedCurves.cs +++ b/crypto/src/crypto/ec/CustomNamedCurves.cs @@ -7,7 +7,6 @@ using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; -using Org.BouncyCastle.Math.EC.Custom.Djb; using Org.BouncyCastle.Math.EC.Custom.GM; using Org.BouncyCastle.Math.EC.Custom.Sec; using Org.BouncyCastle.Math.EC.Endo; @@ -41,42 +40,6 @@ namespace Org.BouncyCastle.Crypto.EC return c.Configure().SetEndomorphism(new GlvTypeBEndomorphism(c, p)).Create(); } - /* - * curve25519 - */ - internal class Curve25519Holder - : X9ECParametersHolder - { - 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 = Curve; - - /* - * NOTE: Curve25519 was specified in Montgomery form. Rewriting in Weierstrass form - * involves substitution of variables, so the base-point x coordinate is 9 + (486662 / 3). - * - * The Curve25519 paper doesn't say which of the two possible y values the base - * point has. The choice here is guided by language in the Ed25519 paper. - * - * (The other possible y value is 5F51E65E475F794B1FE122D388B72EB36DC2B28192839E4DD6163A5D81312C14) - */ - X9ECPoint G = ConfigureBasepoint(curve, - "042AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD245A20AE19A1B8A086B4E01EDD2C7748D14C923D4D7E6D7C61B229E9C5A27ECED3D9"); - - return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S); - } - } - /* * secp128r1 */ @@ -919,14 +882,7 @@ namespace Org.BouncyCastle.Crypto.EC private static readonly IDictionary oidToName = Platform.CreateHashtable(); private static readonly IList names = Platform.CreateArrayList(); - private static void DefineCurve(string name, X9ECParametersHolder holder) - { - names.Add(name); - name = Platform.ToUpperInvariant(name); - nameToCurve.Add(name, holder); - } - - private static void DefineCurveWithOid(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { names.Add(name); oidToName.Add(oid, name); @@ -949,44 +905,42 @@ namespace Org.BouncyCastle.Crypto.EC static CustomNamedCurves() { - DefineCurve("curve25519", Curve25519Holder.Instance); - - //DefineCurveWithOid("secp112r1", SecObjectIdentifiers.SecP112r1, SecP112R1Holder.Instance); - //DefineCurveWithOid("secp112r2", SecObjectIdentifiers.SecP112r2, SecP112R2Holder.Instance); - DefineCurveWithOid("secp128r1", SecObjectIdentifiers.SecP128r1, SecP128R1Holder.Instance); - //DefineCurveWithOid("secp128r2", SecObjectIdentifiers.SecP128r2, SecP128R2Holder.Instance); - DefineCurveWithOid("secp160k1", SecObjectIdentifiers.SecP160k1, SecP160K1Holder.Instance); - DefineCurveWithOid("secp160r1", SecObjectIdentifiers.SecP160r1, SecP160R1Holder.Instance); - DefineCurveWithOid("secp160r2", SecObjectIdentifiers.SecP160r2, SecP160R2Holder.Instance); - DefineCurveWithOid("secp192k1", SecObjectIdentifiers.SecP192k1, SecP192K1Holder.Instance); - DefineCurveWithOid("secp192r1", SecObjectIdentifiers.SecP192r1, SecP192R1Holder.Instance); - DefineCurveWithOid("secp224k1", SecObjectIdentifiers.SecP224k1, SecP224K1Holder.Instance); - DefineCurveWithOid("secp224r1", SecObjectIdentifiers.SecP224r1, SecP224R1Holder.Instance); - DefineCurveWithOid("secp256k1", SecObjectIdentifiers.SecP256k1, SecP256K1Holder.Instance); - DefineCurveWithOid("secp256r1", SecObjectIdentifiers.SecP256r1, SecP256R1Holder.Instance); - DefineCurveWithOid("secp384r1", SecObjectIdentifiers.SecP384r1, SecP384R1Holder.Instance); - DefineCurveWithOid("secp521r1", SecObjectIdentifiers.SecP521r1, SecP521R1Holder.Instance); - - DefineCurveWithOid("sect113r1", SecObjectIdentifiers.SecT113r1, SecT113R1Holder.Instance); - DefineCurveWithOid("sect113r2", SecObjectIdentifiers.SecT113r2, SecT113R2Holder.Instance); - DefineCurveWithOid("sect131r1", SecObjectIdentifiers.SecT131r1, SecT131R1Holder.Instance); - DefineCurveWithOid("sect131r2", SecObjectIdentifiers.SecT131r2, SecT131R2Holder.Instance); - DefineCurveWithOid("sect163k1", SecObjectIdentifiers.SecT163k1, SecT163K1Holder.Instance); - DefineCurveWithOid("sect163r1", SecObjectIdentifiers.SecT163r1, SecT163R1Holder.Instance); - DefineCurveWithOid("sect163r2", SecObjectIdentifiers.SecT163r2, SecT163R2Holder.Instance); - DefineCurveWithOid("sect193r1", SecObjectIdentifiers.SecT193r1, SecT193R1Holder.Instance); - DefineCurveWithOid("sect193r2", SecObjectIdentifiers.SecT193r2, SecT193R2Holder.Instance); - DefineCurveWithOid("sect233k1", SecObjectIdentifiers.SecT233k1, SecT233K1Holder.Instance); - DefineCurveWithOid("sect233r1", SecObjectIdentifiers.SecT233r1, SecT233R1Holder.Instance); - DefineCurveWithOid("sect239k1", SecObjectIdentifiers.SecT239k1, SecT239K1Holder.Instance); - DefineCurveWithOid("sect283k1", SecObjectIdentifiers.SecT283k1, SecT283K1Holder.Instance); - DefineCurveWithOid("sect283r1", SecObjectIdentifiers.SecT283r1, SecT283R1Holder.Instance); - DefineCurveWithOid("sect409k1", SecObjectIdentifiers.SecT409k1, SecT409K1Holder.Instance); - DefineCurveWithOid("sect409r1", SecObjectIdentifiers.SecT409r1, SecT409R1Holder.Instance); - DefineCurveWithOid("sect571k1", SecObjectIdentifiers.SecT571k1, SecT571K1Holder.Instance); - DefineCurveWithOid("sect571r1", SecObjectIdentifiers.SecT571r1, SecT571R1Holder.Instance); - - DefineCurveWithOid("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance); + //DefineCurve("secp112r1", SecObjectIdentifiers.SecP112r1, SecP112R1Holder.Instance); + //DefineCurve("secp112r2", SecObjectIdentifiers.SecP112r2, SecP112R2Holder.Instance); + DefineCurve("secp128r1", SecObjectIdentifiers.SecP128r1, SecP128R1Holder.Instance); + //DefineCurve("secp128r2", SecObjectIdentifiers.SecP128r2, SecP128R2Holder.Instance); + DefineCurve("secp160k1", SecObjectIdentifiers.SecP160k1, SecP160K1Holder.Instance); + DefineCurve("secp160r1", SecObjectIdentifiers.SecP160r1, SecP160R1Holder.Instance); + DefineCurve("secp160r2", SecObjectIdentifiers.SecP160r2, SecP160R2Holder.Instance); + DefineCurve("secp192k1", SecObjectIdentifiers.SecP192k1, SecP192K1Holder.Instance); + DefineCurve("secp192r1", SecObjectIdentifiers.SecP192r1, SecP192R1Holder.Instance); + DefineCurve("secp224k1", SecObjectIdentifiers.SecP224k1, SecP224K1Holder.Instance); + DefineCurve("secp224r1", SecObjectIdentifiers.SecP224r1, SecP224R1Holder.Instance); + DefineCurve("secp256k1", SecObjectIdentifiers.SecP256k1, SecP256K1Holder.Instance); + DefineCurve("secp256r1", SecObjectIdentifiers.SecP256r1, SecP256R1Holder.Instance); + DefineCurve("secp384r1", SecObjectIdentifiers.SecP384r1, SecP384R1Holder.Instance); + DefineCurve("secp521r1", SecObjectIdentifiers.SecP521r1, SecP521R1Holder.Instance); + + DefineCurve("sect113r1", SecObjectIdentifiers.SecT113r1, SecT113R1Holder.Instance); + DefineCurve("sect113r2", SecObjectIdentifiers.SecT113r2, SecT113R2Holder.Instance); + DefineCurve("sect131r1", SecObjectIdentifiers.SecT131r1, SecT131R1Holder.Instance); + DefineCurve("sect131r2", SecObjectIdentifiers.SecT131r2, SecT131R2Holder.Instance); + DefineCurve("sect163k1", SecObjectIdentifiers.SecT163k1, SecT163K1Holder.Instance); + DefineCurve("sect163r1", SecObjectIdentifiers.SecT163r1, SecT163R1Holder.Instance); + DefineCurve("sect163r2", SecObjectIdentifiers.SecT163r2, SecT163R2Holder.Instance); + DefineCurve("sect193r1", SecObjectIdentifiers.SecT193r1, SecT193R1Holder.Instance); + DefineCurve("sect193r2", SecObjectIdentifiers.SecT193r2, SecT193R2Holder.Instance); + DefineCurve("sect233k1", SecObjectIdentifiers.SecT233k1, SecT233K1Holder.Instance); + DefineCurve("sect233r1", SecObjectIdentifiers.SecT233r1, SecT233R1Holder.Instance); + DefineCurve("sect239k1", SecObjectIdentifiers.SecT239k1, SecT239K1Holder.Instance); + DefineCurve("sect283k1", SecObjectIdentifiers.SecT283k1, SecT283K1Holder.Instance); + DefineCurve("sect283r1", SecObjectIdentifiers.SecT283r1, SecT283R1Holder.Instance); + DefineCurve("sect409k1", SecObjectIdentifiers.SecT409k1, SecT409K1Holder.Instance); + DefineCurve("sect409r1", SecObjectIdentifiers.SecT409r1, SecT409R1Holder.Instance); + DefineCurve("sect571k1", SecObjectIdentifiers.SecT571k1, SecT571K1Holder.Instance); + DefineCurve("sect571r1", SecObjectIdentifiers.SecT571r1, SecT571R1Holder.Instance); + + DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance); DefineCurveAlias("B-163", SecObjectIdentifiers.SecT163r2); DefineCurveAlias("B-233", SecObjectIdentifiers.SecT233r1); diff --git a/crypto/src/math/ec/custom/djb/Curve25519.cs b/crypto/src/math/ec/custom/djb/Curve25519.cs deleted file mode 100644 index 380be7252..000000000 --- a/crypto/src/math/ec/custom/djb/Curve25519.cs +++ /dev/null @@ -1,171 +0,0 @@ -using System; - -using Org.BouncyCastle.Math.Raw; -using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities.Encoders; - -namespace Org.BouncyCastle.Math.EC.Custom.Djb -{ - internal class Curve25519 - : AbstractFpCurve - { - public static readonly BigInteger q = Curve25519FieldElement.Q; - - private static readonly BigInteger C_a = new BigInteger(1, Hex.DecodeStrict("2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA984914A144")); - private static readonly BigInteger C_b = new BigInteger(1, Hex.DecodeStrict("7B425ED097B425ED097B425ED097B425ED097B425ED097B4260B5E9C7710C864")); - - private const int CURVE25519_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED; - private const int CURVE25519_FE_INTS = 8; - private static readonly ECFieldElement[] CURVE25519_AFFINE_ZS = new ECFieldElement[] { - new Curve25519FieldElement(BigInteger.One), new Curve25519FieldElement(C_a) }; - protected readonly Curve25519Point m_infinity; - - public Curve25519() - : base(q) - { - this.m_infinity = new Curve25519Point(this, null, null); - - this.m_a = FromBigInteger(C_a); - this.m_b = FromBigInteger(C_b); - this.m_order = new BigInteger(1, Hex.DecodeStrict("1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED")); - this.m_cofactor = BigInteger.ValueOf(8); - this.m_coord = CURVE25519_DEFAULT_COORDS; - } - - protected override ECCurve CloneCurve() - { - return new Curve25519(); - } - - public override bool SupportsCoordinateSystem(int coord) - { - switch (coord) - { - case COORD_JACOBIAN_MODIFIED: - return true; - default: - return false; - } - } - - public virtual BigInteger Q - { - get { return q; } - } - - public override ECPoint Infinity - { - get { return m_infinity; } - } - - public override int FieldSize - { - get { return q.BitLength; } - } - - public override ECFieldElement FromBigInteger(BigInteger x) - { - return new Curve25519FieldElement(x); - } - - protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y) - { - return new Curve25519Point(this, x, y); - } - - protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) - { - return new Curve25519Point(this, x, y, zs); - } - - public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len) - { - uint[] table = new uint[len * CURVE25519_FE_INTS * 2]; - { - int pos = 0; - for (int i = 0; i < len; ++i) - { - ECPoint p = points[off + i]; - Nat256.Copy(((Curve25519FieldElement)p.RawXCoord).x, 0, table, pos); pos += CURVE25519_FE_INTS; - Nat256.Copy(((Curve25519FieldElement)p.RawYCoord).x, 0, table, pos); pos += CURVE25519_FE_INTS; - } - } - - return new Curve25519LookupTable(this, table, len); - } - - public override ECFieldElement RandomFieldElement(SecureRandom r) - { - uint[] x = Nat256.Create(); - Curve25519Field.Random(r, x); - return new Curve25519FieldElement(x); - } - - public override ECFieldElement RandomFieldElementMult(SecureRandom r) - { - uint[] x = Nat256.Create(); - Curve25519Field.RandomMult(r, x); - return new Curve25519FieldElement(x); - } - - private class Curve25519LookupTable - : AbstractECLookupTable - { - private readonly Curve25519 m_outer; - private readonly uint[] m_table; - private readonly int m_size; - - internal Curve25519LookupTable(Curve25519 outer, uint[] table, int size) - { - this.m_outer = outer; - this.m_table = table; - this.m_size = size; - } - - public override int Size - { - get { return m_size; } - } - - public override ECPoint Lookup(int index) - { - uint[] x = Nat256.Create(), y = Nat256.Create(); - int pos = 0; - - for (int i = 0; i < m_size; ++i) - { - uint MASK = (uint)(((i ^ index) - 1) >> 31); - - for (int j = 0; j < CURVE25519_FE_INTS; ++j) - { - x[j] ^= m_table[pos + j] & MASK; - y[j] ^= m_table[pos + CURVE25519_FE_INTS + j] & MASK; - } - - pos += (CURVE25519_FE_INTS * 2); - } - - return CreatePoint(x, y); - } - - public override ECPoint LookupVar(int index) - { - uint[] x = Nat256.Create(), y = Nat256.Create(); - int pos = index * CURVE25519_FE_INTS * 2; - - for (int j = 0; j < CURVE25519_FE_INTS; ++j) - { - x[j] = m_table[pos + j]; - y[j] = m_table[pos + CURVE25519_FE_INTS + j]; - } - - return CreatePoint(x, y); - } - - private ECPoint CreatePoint(uint[] x, uint[] y) - { - return m_outer.CreateRawPoint(new Curve25519FieldElement(x), new Curve25519FieldElement(y), CURVE25519_AFFINE_ZS); - } - } - } -} diff --git a/crypto/src/math/ec/custom/djb/Curve25519Field.cs b/crypto/src/math/ec/custom/djb/Curve25519Field.cs deleted file mode 100644 index 31416249d..000000000 --- a/crypto/src/math/ec/custom/djb/Curve25519Field.cs +++ /dev/null @@ -1,292 +0,0 @@ -using System; -using System.Diagnostics; - -using Org.BouncyCastle.Crypto.Utilities; -using Org.BouncyCastle.Math.Raw; -using Org.BouncyCastle.Security; - -namespace Org.BouncyCastle.Math.EC.Custom.Djb -{ - internal class Curve25519Field - { - // 2^255 - 19 - internal static readonly uint[] P = new uint[]{ 0xFFFFFFED, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, - 0xFFFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF }; - private const uint P7 = 0x7FFFFFFF; - private static readonly uint[] PExt = new uint[]{ 0x00000169, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFED, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, - 0xFFFFFFFF, 0x3FFFFFFF }; - private const uint PInv = 0x13; - - public static void Add(uint[] x, uint[] y, uint[] z) - { - Nat256.Add(x, y, z); - if (Nat256.Gte(z, P)) - { - SubPFrom(z); - } - } - - public static void AddExt(uint[] xx, uint[] yy, uint[] zz) - { - Nat.Add(16, xx, yy, zz); - if (Nat.Gte(16, zz, PExt)) - { - SubPExtFrom(zz); - } - } - - public static void AddOne(uint[] x, uint[] z) - { - Nat.Inc(8, x, z); - if (Nat256.Gte(z, P)) - { - SubPFrom(z); - } - } - - public static uint[] FromBigInteger(BigInteger x) - { - uint[] z = Nat.FromBigInteger(256, x); - while (Nat256.Gte(z, P)) - { - Nat256.SubFrom(P, z); - } - return z; - } - - public static void Half(uint[] x, uint[] z) - { - if ((x[0] & 1) == 0) - { - Nat.ShiftDownBit(8, x, 0, z); - } - else - { - Nat256.Add(x, P, z); - Nat.ShiftDownBit(8, z, 0); - } - } - - public static void Inv(uint[] x, uint[] z) - { - Mod.CheckedModOddInverse(P, x, z); - } - - public static int IsZero(uint[] x) - { - uint d = 0; - for (int i = 0; i < 8; ++i) - { - d |= x[i]; - } - d = (d >> 1) | (d & 1); - return ((int)d - 1) >> 31; - } - - public static void Multiply(uint[] x, uint[] y, uint[] z) - { - uint[] tt = Nat256.CreateExt(); - Nat256.Mul(x, y, tt); - Reduce(tt, z); - } - - public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz) - { - Nat256.MulAddTo(x, y, zz); - if (Nat.Gte(16, zz, PExt)) - { - SubPExtFrom(zz); - } - } - - public static void Negate(uint[] x, uint[] z) - { - if (0 != IsZero(x)) - { - Nat256.Sub(P, P, z); - } - else - { - Nat256.Sub(P, x, z); - } - } - - public static void Random(SecureRandom r, uint[] z) - { - byte[] bb = new byte[8 * 4]; - do - { - r.NextBytes(bb); - Pack.LE_To_UInt32(bb, 0, z, 0, 8); - z[7] &= P7; - } - while (0 == Nat.LessThan(8, z, P)); - } - - public static void RandomMult(SecureRandom r, uint[] z) - { - do - { - Random(r, z); - } - while (0 != IsZero(z)); - } - - public static void Reduce(uint[] xx, uint[] z) - { - Debug.Assert(xx[15] >> 30 == 0); - - uint xx07 = xx[7]; - Nat.ShiftUpBit(8, xx, 8, xx07, z, 0); - uint c = Nat256.MulByWordAddTo(PInv, xx, z) << 1; - uint z7 = z[7]; - c += (z7 >> 31) - (xx07 >> 31); - z7 &= P7; - z7 += Nat.AddWordTo(7, c * PInv, z); - z[7] = z7; - if (z7 >= P7 && Nat256.Gte(z, P)) - { - SubPFrom(z); - } - } - - public static void Reduce27(uint x, uint[] z) - { - Debug.Assert(x >> 26 == 0); - - uint z7 = z[7]; - uint c = (x << 1 | z7 >> 31); - z7 &= P7; - z7 += Nat.AddWordTo(7, c * PInv, z); - z[7] = z7; - if (z7 >= P7 && Nat256.Gte(z, P)) - { - SubPFrom(z); - } - } - - public static void Square(uint[] x, uint[] z) - { - uint[] tt = Nat256.CreateExt(); - Nat256.Square(x, tt); - Reduce(tt, z); - } - - public static void SquareN(uint[] x, int n, uint[] z) - { - Debug.Assert(n > 0); - - uint[] tt = Nat256.CreateExt(); - Nat256.Square(x, tt); - Reduce(tt, z); - - while (--n > 0) - { - Nat256.Square(z, tt); - Reduce(tt, z); - } - } - - public static void Subtract(uint[] x, uint[] y, uint[] z) - { - int c = Nat256.Sub(x, y, z); - if (c != 0) - { - AddPTo(z); - } - } - - public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz) - { - int c = Nat.Sub(16, xx, yy, zz); - if (c != 0) - { - AddPExtTo(zz); - } - } - - public static void Twice(uint[] x, uint[] z) - { - Nat.ShiftUpBit(8, x, 0, z); - if (Nat256.Gte(z, P)) - { - SubPFrom(z); - } - } - - private static uint AddPTo(uint[] z) - { - long c = (long)z[0] - PInv; - z[0] = (uint)c; - c >>= 32; - if (c != 0) - { - c = Nat.DecAt(7, z, 1); - } - c += (long)z[7] + (P7 + 1); - z[7] = (uint)c; - c >>= 32; - return (uint)c; - } - - private static uint AddPExtTo(uint[] zz) - { - long c = (long)zz[0] + PExt[0]; - zz[0] = (uint)c; - c >>= 32; - if (c != 0) - { - c = Nat.IncAt(8, zz, 1); - } - c += (long)zz[8] - PInv; - zz[8] = (uint)c; - c >>= 32; - if (c != 0) - { - c = Nat.DecAt(15, zz, 9); - } - c += (long)zz[15] + (PExt[15] + 1); - zz[15] = (uint)c; - c >>= 32; - return (uint)c; - } - - private static int SubPFrom(uint[] z) - { - long c = (long)z[0] + PInv; - z[0] = (uint)c; - c >>= 32; - if (c != 0) - { - c = Nat.IncAt(7, z, 1); - } - c += (long)z[7] - (P7 + 1); - z[7] = (uint)c; - c >>= 32; - return (int)c; - } - - private static int SubPExtFrom(uint[] zz) - { - long c = (long)zz[0] - PExt[0]; - zz[0] = (uint)c; - c >>= 32; - if (c != 0) - { - c = Nat.DecAt(8, zz, 1); - } - c += (long)zz[8] + PInv; - zz[8] = (uint)c; - c >>= 32; - if (c != 0) - { - c = Nat.IncAt(15, zz, 9); - } - c += (long)zz[15] - (PExt[15] + 1); - zz[15] = (uint)c; - c >>= 32; - return (int)c; - } - } -} diff --git a/crypto/src/math/ec/custom/djb/Curve25519FieldElement.cs b/crypto/src/math/ec/custom/djb/Curve25519FieldElement.cs deleted file mode 100644 index a5509841d..000000000 --- a/crypto/src/math/ec/custom/djb/Curve25519FieldElement.cs +++ /dev/null @@ -1,233 +0,0 @@ -using System; - -using Org.BouncyCastle.Math.Raw; -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Math.EC.Custom.Djb -{ - internal class Curve25519FieldElement - : AbstractFpFieldElement - { - public static readonly BigInteger Q = Nat256.ToBigInteger(Curve25519Field.P); - - // Calculated as ECConstants.TWO.modPow(Q.shiftRight(2), Q) - private static readonly uint[] PRECOMP_POW2 = new uint[]{ 0x4a0ea0b0, 0xc4ee1b27, 0xad2fe478, 0x2f431806, - 0x3dfbd7a7, 0x2b4d0099, 0x4fc1df0b, 0x2b832480 }; - - protected internal readonly uint[] x; - - public Curve25519FieldElement(BigInteger x) - { - if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0) - throw new ArgumentException("value invalid for Curve25519FieldElement", "x"); - - this.x = Curve25519Field.FromBigInteger(x); - } - - public Curve25519FieldElement() - { - this.x = Nat256.Create(); - } - - protected internal Curve25519FieldElement(uint[] x) - { - this.x = x; - } - - public override bool IsZero - { - get { return Nat256.IsZero(x); } - } - - public override bool IsOne - { - get { return Nat256.IsOne(x); } - } - - public override bool TestBitZero() - { - return Nat256.GetBit(x, 0) == 1; - } - - public override BigInteger ToBigInteger() - { - return Nat256.ToBigInteger(x); - } - - public override string FieldName - { - get { return "Curve25519Field"; } - } - - public override int FieldSize - { - get { return Q.BitLength; } - } - - public override ECFieldElement Add(ECFieldElement b) - { - uint[] z = Nat256.Create(); - Curve25519Field.Add(x, ((Curve25519FieldElement)b).x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement AddOne() - { - uint[] z = Nat256.Create(); - Curve25519Field.AddOne(x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement Subtract(ECFieldElement b) - { - uint[] z = Nat256.Create(); - Curve25519Field.Subtract(x, ((Curve25519FieldElement)b).x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement Multiply(ECFieldElement b) - { - uint[] z = Nat256.Create(); - Curve25519Field.Multiply(x, ((Curve25519FieldElement)b).x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement Divide(ECFieldElement b) - { - //return Multiply(b.Invert()); - uint[] z = Nat256.Create(); - Curve25519Field.Inv(((Curve25519FieldElement)b).x, z); - Curve25519Field.Multiply(z, x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement Negate() - { - uint[] z = Nat256.Create(); - Curve25519Field.Negate(x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement Square() - { - uint[] z = Nat256.Create(); - Curve25519Field.Square(x, z); - return new Curve25519FieldElement(z); - } - - public override ECFieldElement Invert() - { - //return new Curve25519FieldElement(ToBigInteger().ModInverse(Q)); - uint[] z = Nat256.Create(); - Curve25519Field.Inv(x, z); - return new Curve25519FieldElement(z); - } - - /** - * return a sqrt root - the routine verifies that the calculation returns the right value - if - * none exists it returns null. - */ - public override ECFieldElement Sqrt() - { - /* - * Q == 8m + 5, so we use Pocklington's method for this case. - * - * First, raise this element to the exponent 2^252 - 2^1 (i.e. m + 1) - * - * Breaking up the exponent's binary representation into "repunits", we get: - * { 251 1s } { 1 0s } - * - * Therefore we need an addition chain containing 251 (the lengths of the repunits) - * We use: 1, 2, 3, 4, 7, 11, 15, 30, 60, 120, 131, [251] - */ - - uint[] x1 = this.x; - if (Nat256.IsZero(x1) || Nat256.IsOne(x1)) - return this; - - uint[] x2 = Nat256.Create(); - Curve25519Field.Square(x1, x2); - Curve25519Field.Multiply(x2, x1, x2); - uint[] x3 = x2; - Curve25519Field.Square(x2, x3); - Curve25519Field.Multiply(x3, x1, x3); - uint[] x4 = Nat256.Create(); - Curve25519Field.Square(x3, x4); - Curve25519Field.Multiply(x4, x1, x4); - uint[] x7 = Nat256.Create(); - Curve25519Field.SquareN(x4, 3, x7); - Curve25519Field.Multiply(x7, x3, x7); - uint[] x11 = x3; - Curve25519Field.SquareN(x7, 4, x11); - Curve25519Field.Multiply(x11, x4, x11); - uint[] x15 = x7; - Curve25519Field.SquareN(x11, 4, x15); - Curve25519Field.Multiply(x15, x4, x15); - uint[] x30 = x4; - Curve25519Field.SquareN(x15, 15, x30); - Curve25519Field.Multiply(x30, x15, x30); - uint[] x60 = x15; - Curve25519Field.SquareN(x30, 30, x60); - Curve25519Field.Multiply(x60, x30, x60); - uint[] x120 = x30; - Curve25519Field.SquareN(x60, 60, x120); - Curve25519Field.Multiply(x120, x60, x120); - uint[] x131 = x60; - Curve25519Field.SquareN(x120, 11, x131); - Curve25519Field.Multiply(x131, x11, x131); - uint[] x251 = x11; - Curve25519Field.SquareN(x131, 120, x251); - Curve25519Field.Multiply(x251, x120, x251); - - uint[] t1 = x251; - Curve25519Field.Square(t1, t1); - - uint[] t2 = x120; - Curve25519Field.Square(t1, t2); - - if (Nat256.Eq(x1, t2)) - { - return new Curve25519FieldElement(t1); - } - - /* - * If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess, - * which is ((4x)^(m + 1))/2 mod Q - */ - Curve25519Field.Multiply(t1, PRECOMP_POW2, t1); - - Curve25519Field.Square(t1, t2); - - if (Nat256.Eq(x1, t2)) - { - return new Curve25519FieldElement(t1); - } - - return null; - } - - public override bool Equals(object obj) - { - return Equals(obj as Curve25519FieldElement); - } - - public override bool Equals(ECFieldElement other) - { - return Equals(other as Curve25519FieldElement); - } - - public virtual bool Equals(Curve25519FieldElement other) - { - if (this == other) - return true; - if (null == other) - return false; - return Nat256.Eq(x, other.x); - } - - public override int GetHashCode() - { - return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8); - } - } -} diff --git a/crypto/src/math/ec/custom/djb/Curve25519Point.cs b/crypto/src/math/ec/custom/djb/Curve25519Point.cs deleted file mode 100644 index f61e28af8..000000000 --- a/crypto/src/math/ec/custom/djb/Curve25519Point.cs +++ /dev/null @@ -1,287 +0,0 @@ -using System; - -using Org.BouncyCastle.Math.Raw; - -namespace Org.BouncyCastle.Math.EC.Custom.Djb -{ - internal class Curve25519Point - : AbstractFpPoint - { - internal Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y) - : base(curve, x, y) - { - } - - internal Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs) - : base(curve, x, y, zs) - { - } - - protected override ECPoint Detach() - { - return new Curve25519Point(null, AffineXCoord, AffineYCoord); - } - - public override ECFieldElement GetZCoord(int index) - { - if (index == 1) - { - return GetJacobianModifiedW(); - } - - return base.GetZCoord(index); - } - - public override ECPoint Add(ECPoint b) - { - if (this.IsInfinity) - return b; - if (b.IsInfinity) - return this; - if (this == b) - return Twice(); - - ECCurve curve = this.Curve; - - Curve25519FieldElement X1 = (Curve25519FieldElement)this.RawXCoord, Y1 = (Curve25519FieldElement)this.RawYCoord, - Z1 = (Curve25519FieldElement)this.RawZCoords[0]; - Curve25519FieldElement X2 = (Curve25519FieldElement)b.RawXCoord, Y2 = (Curve25519FieldElement)b.RawYCoord, - Z2 = (Curve25519FieldElement)b.RawZCoords[0]; - - uint c; - uint[] tt1 = Nat256.CreateExt(); - uint[] t2 = Nat256.Create(); - uint[] t3 = Nat256.Create(); - uint[] t4 = Nat256.Create(); - - bool Z1IsOne = Z1.IsOne; - uint[] U2, S2; - if (Z1IsOne) - { - U2 = X2.x; - S2 = Y2.x; - } - else - { - S2 = t3; - Curve25519Field.Square(Z1.x, S2); - - U2 = t2; - Curve25519Field.Multiply(S2, X2.x, U2); - - Curve25519Field.Multiply(S2, Z1.x, S2); - Curve25519Field.Multiply(S2, Y2.x, S2); - } - - bool Z2IsOne = Z2.IsOne; - uint[] U1, S1; - if (Z2IsOne) - { - U1 = X1.x; - S1 = Y1.x; - } - else - { - S1 = t4; - Curve25519Field.Square(Z2.x, S1); - - U1 = tt1; - Curve25519Field.Multiply(S1, X1.x, U1); - - Curve25519Field.Multiply(S1, Z2.x, S1); - Curve25519Field.Multiply(S1, Y1.x, S1); - } - - uint[] H = Nat256.Create(); - Curve25519Field.Subtract(U1, U2, H); - - uint[] R = t2; - Curve25519Field.Subtract(S1, S2, R); - - // Check if b == this or b == -this - if (Nat256.IsZero(H)) - { - if (Nat256.IsZero(R)) - { - // this == b, i.e. this must be doubled - return this.Twice(); - } - - // this == -b, i.e. the result is the point at infinity - return curve.Infinity; - } - - uint[] HSquared = Nat256.Create(); - Curve25519Field.Square(H, HSquared); - - uint[] G = Nat256.Create(); - Curve25519Field.Multiply(HSquared, H, G); - - uint[] V = t3; - Curve25519Field.Multiply(HSquared, U1, V); - - Curve25519Field.Negate(G, G); - Nat256.Mul(S1, G, tt1); - - c = Nat256.AddBothTo(V, V, G); - Curve25519Field.Reduce27(c, G); - - Curve25519FieldElement X3 = new Curve25519FieldElement(t4); - Curve25519Field.Square(R, X3.x); - Curve25519Field.Subtract(X3.x, G, X3.x); - - Curve25519FieldElement Y3 = new Curve25519FieldElement(G); - Curve25519Field.Subtract(V, X3.x, Y3.x); - Curve25519Field.MultiplyAddToExt(Y3.x, R, tt1); - Curve25519Field.Reduce(tt1, Y3.x); - - Curve25519FieldElement Z3 = new Curve25519FieldElement(H); - if (!Z1IsOne) - { - Curve25519Field.Multiply(Z3.x, Z1.x, Z3.x); - } - if (!Z2IsOne) - { - Curve25519Field.Multiply(Z3.x, Z2.x, Z3.x); - } - - uint[] Z3Squared = (Z1IsOne && Z2IsOne) ? HSquared : null; - - // TODO If the result will only be used in a subsequent addition, we don't need W3 - Curve25519FieldElement W3 = CalculateJacobianModifiedW((Curve25519FieldElement)Z3, Z3Squared); - - ECFieldElement[] zs = new ECFieldElement[] { Z3, W3 }; - - return new Curve25519Point(curve, X3, Y3, zs); - } - - public override ECPoint Twice() - { - if (this.IsInfinity) - return this; - - ECCurve curve = this.Curve; - - ECFieldElement Y1 = this.RawYCoord; - if (Y1.IsZero) - return curve.Infinity; - - return TwiceJacobianModified(true); - } - - public override ECPoint TwicePlus(ECPoint b) - { - if (this == b) - return ThreeTimes(); - if (this.IsInfinity) - return b; - if (b.IsInfinity) - return Twice(); - - ECFieldElement Y1 = this.RawYCoord; - if (Y1.IsZero) - return b; - - return TwiceJacobianModified(false).Add(b); - } - - public override ECPoint ThreeTimes() - { - if (this.IsInfinity || this.RawYCoord.IsZero) - return this; - - return TwiceJacobianModified(false).Add(this); - } - - public override ECPoint Negate() - { - if (IsInfinity) - return this; - - return new Curve25519Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords); - } - - protected virtual Curve25519FieldElement CalculateJacobianModifiedW(Curve25519FieldElement Z, uint[] ZSquared) - { - Curve25519FieldElement a4 = (Curve25519FieldElement)this.Curve.A; - if (Z.IsOne) - return a4; - - Curve25519FieldElement W = new Curve25519FieldElement(); - if (ZSquared == null) - { - ZSquared = W.x; - Curve25519Field.Square(Z.x, ZSquared); - } - Curve25519Field.Square(ZSquared, W.x); - Curve25519Field.Multiply(W.x, a4.x, W.x); - return W; - } - - protected virtual Curve25519FieldElement GetJacobianModifiedW() - { - ECFieldElement[] ZZ = this.RawZCoords; - Curve25519FieldElement W = (Curve25519FieldElement)ZZ[1]; - if (W == null) - { - // NOTE: Rarely, TwicePlus will result in the need for a lazy W1 calculation here - ZZ[1] = W = CalculateJacobianModifiedW((Curve25519FieldElement)ZZ[0], null); - } - return W; - } - - protected virtual Curve25519Point TwiceJacobianModified(bool calculateW) - { - Curve25519FieldElement X1 = (Curve25519FieldElement)this.RawXCoord, Y1 = (Curve25519FieldElement)this.RawYCoord, - Z1 = (Curve25519FieldElement)this.RawZCoords[0], W1 = GetJacobianModifiedW(); - - uint c; - - uint[] M = Nat256.Create(); - Curve25519Field.Square(X1.x, M); - c = Nat256.AddBothTo(M, M, M); - c += Nat256.AddTo(W1.x, M); - Curve25519Field.Reduce27(c, M); - - uint[] _2Y1 = Nat256.Create(); - Curve25519Field.Twice(Y1.x, _2Y1); - - uint[] _2Y1Squared = Nat256.Create(); - Curve25519Field.Multiply(_2Y1, Y1.x, _2Y1Squared); - - uint[] S = Nat256.Create(); - Curve25519Field.Multiply(_2Y1Squared, X1.x, S); - Curve25519Field.Twice(S, S); - - uint[] _8T = Nat256.Create(); - Curve25519Field.Square(_2Y1Squared, _8T); - Curve25519Field.Twice(_8T, _8T); - - Curve25519FieldElement X3 = new Curve25519FieldElement(_2Y1Squared); - Curve25519Field.Square(M, X3.x); - Curve25519Field.Subtract(X3.x, S, X3.x); - Curve25519Field.Subtract(X3.x, S, X3.x); - - Curve25519FieldElement Y3 = new Curve25519FieldElement(S); - Curve25519Field.Subtract(S, X3.x, Y3.x); - Curve25519Field.Multiply(Y3.x, M, Y3.x); - Curve25519Field.Subtract(Y3.x, _8T, Y3.x); - - Curve25519FieldElement Z3 = new Curve25519FieldElement(_2Y1); - if (!Nat256.IsOne(Z1.x)) - { - Curve25519Field.Multiply(Z3.x, Z1.x, Z3.x); - } - - Curve25519FieldElement W3 = null; - if (calculateW) - { - W3 = new Curve25519FieldElement(_8T); - Curve25519Field.Multiply(W3.x, W1.x, W3.x); - Curve25519Field.Twice(W3.x, W3.x); - } - - return new Curve25519Point(this.Curve, X3, Y3, new ECFieldElement[] { Z3, W3 }); - } - } -} diff --git a/crypto/test/src/crypto/test/ECTest.cs b/crypto/test/src/crypto/test/ECTest.cs index 18abb8df1..58dc75e2f 100644 --- a/crypto/test/src/crypto/test/ECTest.cs +++ b/crypto/test/src/crypto/test/ECTest.cs @@ -745,8 +745,19 @@ namespace Org.BouncyCastle.Crypto.Tests { SecureRandom random = new SecureRandom(); - X9ECParameters x9 = CustomNamedCurves.GetByName("curve25519"); - ECDomainParameters ec = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed()); + // "curve25519" in short Weierstrass form + BigInteger q = BigInteger.One.ShiftLeft(255).Subtract(BigInteger.ValueOf(19)); + BigInteger a = new BigInteger(1, Hex.DecodeStrict("2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA984914A144")); + BigInteger b = new BigInteger(1, Hex.DecodeStrict("7B425ED097B425ED097B425ED097B425ED097B425ED097B4260B5E9C7710C864")); + BigInteger n = new BigInteger(1, Hex.DecodeStrict("1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED")); + BigInteger h = BigInteger.ValueOf(8); + + ECCurve curve = new FpCurve(q, a, b, n, h); + X9ECPoint G = new X9ECPoint(curve, + Hex.DecodeStrict("042AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD245A20AE19A1B8A086B4E01EDD2C7748D14C923D4D7E6D7C61B229E9C5A27ECED3D9")); + + X9ECParameters x9 = new X9ECParameters(curve, G, n, h); + ECDomainParameters ec = new ECDomainParameters(x9); ECKeyPairGenerator kpg = new ECKeyPairGenerator(); kpg.Init(new ECKeyGenerationParameters(ec, random)); -- cgit 1.5.1 From 110f936da13a1639a83edc512c5c104d2f5694a7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jun 2022 20:37:41 +0700 Subject: Update EC curve registry classes --- crypto/src/asn1/anssi/ANSSINamedCurves.cs | 86 +++++---- crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs | 130 +++++++------- crypto/src/asn1/gm/GMNamedCurves.cs | 94 +++++----- crypto/src/asn1/nist/NISTNamedCurves.cs | 93 +++++----- crypto/src/asn1/sec/SECNamedCurves.cs | 188 +++++--------------- crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs | 97 +++++----- crypto/src/asn1/x9/ECNamedCurveTable.cs | 194 ++++++++++---------- crypto/src/asn1/x9/X962NamedCurves.cs | 94 +++++----- crypto/src/crypto/ec/CustomNamedCurves.cs | 197 +++++---------------- crypto/src/security/PrivateKeyFactory.cs | 4 +- crypto/src/security/PublicKeyFactory.cs | 4 +- crypto/src/util/collections/CollectionUtilities.cs | 11 ++ crypto/src/util/collections/EnumerableProxy.cs | 25 +++ crypto/test/src/crypto/test/ECGOST3410_2012Test.cs | 8 +- .../src/math/ec/test/ECPointPerformanceTest.cs | 18 +- crypto/test/src/math/ec/test/ECPointTest.cs | 9 +- 16 files changed, 517 insertions(+), 735 deletions(-) (limited to 'crypto/test') diff --git a/crypto/src/asn1/anssi/ANSSINamedCurves.cs b/crypto/src/asn1/anssi/ANSSINamedCurves.cs index 3a9e4dd03..ed1faa75c 100644 --- a/crypto/src/asn1/anssi/ANSSINamedCurves.cs +++ b/crypto/src/asn1/anssi/ANSSINamedCurves.cs @@ -1,17 +1,17 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; 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.Anssi { - public class AnssiNamedCurves + /// Elliptic curve registry for ANSSI. + public static class AnssiNamedCurves { private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { @@ -30,9 +30,6 @@ namespace Org.BouncyCastle.Asn1.Anssi return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * FRP256v1 - */ internal class Frp256v1Holder : X9ECParametersHolder { @@ -63,16 +60,16 @@ namespace Org.BouncyCastle.Asn1.Anssi } } - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - private static void DefineCurve( - string name, - DerObjectIdentifier oid, - X9ECParametersHolder holder) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -82,63 +79,64 @@ namespace Org.BouncyCastle.Asn1.Anssi DefineCurve("FRP256v1", AnssiObjectIdentifiers.FRP256v1, Frp256v1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. 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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs index 5ac8cadfe..ec297f7a1 100644 --- a/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs +++ b/crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs @@ -1,27 +1,19 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; 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 { - /// - /// Table of the available named parameters for GOST 3410-2001 / 2012. - /// - public sealed class ECGost3410NamedCurves + /// Elliptic curve registry for GOST 3410-2001 / 2012. + public static class ECGost3410NamedCurves { - private ECGost3410NamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, BigInteger x, BigInteger y) { ECPoint G = curve.CreatePoint(x, y); @@ -39,9 +31,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * GostR3410-2001-CryptoPro-A (and GostR3410-2001-CryptoPro-XchA) - */ internal class Holder_gostR3410_2001_CryptoPro_A : X9ECParametersHolder { @@ -74,9 +63,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * GostR3410-2001-CryptoPro-B - */ internal class Holder_gostR3410_2001_CryptoPro_B : X9ECParametersHolder { @@ -109,9 +95,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * GostR3410-2001-CryptoPro-C - */ internal class Holder_gostR3410_2001_CryptoPro_C : X9ECParametersHolder { @@ -144,9 +127,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * GostR3410-2001-CryptoPro-XchB - */ internal class Holder_gostR3410_2001_CryptoPro_XchB : X9ECParametersHolder { @@ -179,9 +159,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-256-paramSetA - */ internal class Holder_id_tc26_gost_3410_12_256_paramSetA : X9ECParametersHolder { @@ -214,9 +191,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-512-paramSetA - */ internal class Holder_id_tc26_gost_3410_12_512_paramSetA : X9ECParametersHolder { @@ -249,9 +223,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-512-paramSetB - */ internal class Holder_id_tc26_gost_3410_12_512_paramSetB : X9ECParametersHolder { @@ -284,9 +255,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - /* - * Tc26-Gost-3410-12-512-paramSetC - */ internal class Holder_id_tc26_gost_3410_12_512_paramSetC : X9ECParametersHolder { @@ -319,10 +287,12 @@ namespace Org.BouncyCastle.Asn1.CryptoPro } }; - - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary curves = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { @@ -333,71 +303,93 @@ namespace Org.BouncyCastle.Asn1.CryptoPro static ECGost3410NamedCurves() { - DefineCurve("GostR3410-2001-CryptoPro-A", CryptoProObjectIdentifiers.GostR3410x2001CryptoProA, + DefineCurve("GostR3410-2001-CryptoPro-A", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProA, Holder_gostR3410_2001_CryptoPro_A.Instance); - DefineCurve("GostR3410-2001-CryptoPro-B", CryptoProObjectIdentifiers.GostR3410x2001CryptoProB, + DefineCurve("GostR3410-2001-CryptoPro-B", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProB, Holder_gostR3410_2001_CryptoPro_B.Instance); - DefineCurve("GostR3410-2001-CryptoPro-C", CryptoProObjectIdentifiers.GostR3410x2001CryptoProC, + DefineCurve("GostR3410-2001-CryptoPro-C", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProC, Holder_gostR3410_2001_CryptoPro_C.Instance); - DefineCurve("GostR3410-2001-CryptoPro-XchA", CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA, + DefineCurve("GostR3410-2001-CryptoPro-XchA", + CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA, Holder_gostR3410_2001_CryptoPro_A.Instance); - DefineCurve("GostR3410-2001-CryptoPro-XchB", CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB, + 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, + 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, + 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, + 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, + 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) + /// Look up the for the curve with the given name. + /// The name of the curve. + public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : GetByOidX9(oid); + return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOidLazy(oid); } - public static X9ECParameters GetByOidX9(DerObjectIdentifier oid) + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. + public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[name]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/gm/GMNamedCurves.cs b/crypto/src/asn1/gm/GMNamedCurves.cs index f906a2b95..fec0c1401 100644 --- a/crypto/src/asn1/gm/GMNamedCurves.cs +++ b/crypto/src/asn1/gm/GMNamedCurves.cs @@ -1,22 +1,18 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; 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.GM { - public sealed class GMNamedCurves + /// Elliptic curve registry for GM. + public static class GMNamedCurves { - private GMNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -34,9 +30,6 @@ namespace Org.BouncyCastle.Asn1.GM return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * sm2p256v1 - */ internal class SM2P256V1Holder : X9ECParametersHolder { @@ -67,9 +60,6 @@ namespace Org.BouncyCastle.Asn1.GM } } - /* - * wapip192v1 - */ internal class WapiP192V1Holder : X9ECParametersHolder { @@ -100,17 +90,16 @@ namespace Org.BouncyCastle.Asn1.GM } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - 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) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -121,63 +110,64 @@ namespace Org.BouncyCastle.Asn1.GM DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. 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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/nist/NISTNamedCurves.cs b/crypto/src/asn1/nist/NISTNamedCurves.cs index ee256cc2b..a8bc56549 100644 --- a/crypto/src/asn1/nist/NISTNamedCurves.cs +++ b/crypto/src/asn1/nist/NISTNamedCurves.cs @@ -1,31 +1,26 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Asn1.Nist { - /** - * Utility class for fetching curves using their NIST names as published in FIPS-PUB 186-3 - */ - public sealed class NistNamedCurves + /// Elliptic curve registry for NIST curves. + public static class NistNamedCurves { - private NistNamedCurves() - { - } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary names = + new Dictionary(); - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary names = Platform.CreateHashtable(); - - private static void DefineCurveAlias( - string name, - DerObjectIdentifier oid) + private static void DefineCurveAlias(string name, DerObjectIdentifier oid) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + if (SecNamedCurves.GetByOidLazy(oid) == null) + throw new InvalidOperationException(); + + objIds.Add(name, oid); names.Add(oid, name); } @@ -50,62 +45,64 @@ namespace Org.BouncyCastle.Asn1.Nist DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : SecNamedCurves.GetByOid(oid); + return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { DerObjectIdentifier oid = GetOid(name); - return oid == null ? null : SecNamedCurves.GetByOidLazy(oid); + 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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - return names.Contains(oid) ? SecNamedCurves.GetByOid(oid) : null; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return names.Contains(oid) ? SecNamedCurves.GetByOidLazy(oid) : null; + return names.ContainsKey(oid) ? SecNamedCurves.GetByOidLazy(oid) : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier) objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string) names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/sec/SECNamedCurves.cs b/crypto/src/asn1/sec/SECNamedCurves.cs index ad2f3e333..c0a783ec6 100644 --- a/crypto/src/asn1/sec/SECNamedCurves.cs +++ b/crypto/src/asn1/sec/SECNamedCurves.cs @@ -1,24 +1,19 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.EC.Endo; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.Sec { - public sealed class SecNamedCurves + /// Elliptic curve registry for the SEC standard. + public static class SecNamedCurves { - private SecNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -41,9 +36,6 @@ namespace Org.BouncyCastle.Asn1.Sec return new BigInteger(1, Hex.DecodeStrict(hex)); } - /* - * secp112r1 - */ internal class Secp112r1Holder : X9ECParametersHolder { @@ -75,9 +67,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp112r2 - */ internal class Secp112r2Holder : X9ECParametersHolder { @@ -109,9 +98,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp128r1 - */ internal class Secp128r1Holder : X9ECParametersHolder { @@ -143,9 +129,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp128r2 - */ internal class Secp128r2Holder : X9ECParametersHolder { @@ -177,9 +160,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160k1 - */ internal class Secp160k1Holder : X9ECParametersHolder { @@ -225,9 +205,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160r1 - */ internal class Secp160r1Holder : X9ECParametersHolder { @@ -259,9 +236,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp160r2 - */ internal class Secp160r2Holder : X9ECParametersHolder { @@ -293,9 +267,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp192k1 - */ internal class Secp192k1Holder : X9ECParametersHolder { @@ -341,9 +312,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp192r1 - */ internal class Secp192r1Holder : X9ECParametersHolder { @@ -375,9 +343,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp224k1 - */ internal class Secp224k1Holder : X9ECParametersHolder { @@ -423,9 +388,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp224r1 - */ internal class Secp224r1Holder : X9ECParametersHolder { @@ -457,9 +419,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp256k1 - */ internal class Secp256k1Holder : X9ECParametersHolder { @@ -505,9 +464,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp256r1 - */ internal class Secp256r1Holder : X9ECParametersHolder { @@ -539,9 +495,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp384r1 - */ internal class Secp384r1Holder : X9ECParametersHolder { @@ -574,9 +527,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * secp521r1 - */ internal class Secp521r1Holder : X9ECParametersHolder { @@ -609,9 +559,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect113r1 - */ internal class Sect113r1Holder : X9ECParametersHolder { @@ -644,9 +591,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect113r2 - */ internal class Sect113r2Holder : X9ECParametersHolder { @@ -679,9 +623,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect131r1 - */ internal class Sect131r1Holder : X9ECParametersHolder { @@ -716,9 +657,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect131r2 - */ internal class Sect131r2Holder : X9ECParametersHolder { @@ -753,9 +691,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163k1 - */ internal class Sect163k1Holder : X9ECParametersHolder { @@ -790,9 +725,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163r1 - */ internal class Sect163r1Holder : X9ECParametersHolder { @@ -827,9 +759,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect163r2 - */ internal class Sect163r2Holder : X9ECParametersHolder { @@ -864,9 +793,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect193r1 - */ internal class Sect193r1Holder : X9ECParametersHolder { @@ -899,9 +825,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect193r2 - */ internal class Sect193r2Holder : X9ECParametersHolder { @@ -934,9 +857,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect233k1 - */ internal class Sect233k1Holder : X9ECParametersHolder { @@ -969,9 +889,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect233r1 - */ internal class Sect233r1Holder : X9ECParametersHolder { @@ -1004,9 +921,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect239k1 - */ internal class Sect239k1Holder : X9ECParametersHolder { @@ -1039,9 +953,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect283k1 - */ internal class Sect283k1Holder : X9ECParametersHolder { @@ -1077,9 +988,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect283r1 - */ internal class Sect283r1Holder : X9ECParametersHolder { @@ -1115,9 +1023,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect409k1 - */ internal class Sect409k1Holder : X9ECParametersHolder { @@ -1151,9 +1056,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect409r1 - */ internal class Sect409r1Holder : X9ECParametersHolder { @@ -1187,9 +1089,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect571k1 - */ internal class Sect571k1Holder : X9ECParametersHolder { @@ -1225,9 +1124,6 @@ namespace Org.BouncyCastle.Asn1.Sec } } - /* - * sect571r1 - */ internal class Sect571r1Holder : X9ECParametersHolder { @@ -1263,17 +1159,16 @@ namespace Org.BouncyCastle.Asn1.Sec } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - 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) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -1316,63 +1211,64 @@ namespace Org.BouncyCastle.Asn1.Sec DefineCurve("sect571r1", SecObjectIdentifiers.SecT571r1, Sect571r1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. 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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs b/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs index dcc160016..b863babce 100644 --- a/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs +++ b/crypto/src/asn1/teletrust/TeleTrusTNamedCurves.cs @@ -1,20 +1,18 @@ -using System.Collections; +using System; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; 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.TeleTrust { - /** - * elliptic curves defined in "ECC Brainpool Standard Curves and Curve Generation" - * http://www.ecc-brainpool.org/download/draft_pkix_additional_ecc_dp.txt - */ - public class TeleTrusTNamedCurves + /// Elliptic curve registry for curves defined in "ECC Brainpool Standard Curves and Curve Generation" + /// http://www.ecc-brainpool.org/download/draft_pkix_additional_ecc_dp.txt . + public static class TeleTrusTNamedCurves { private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { @@ -460,17 +458,16 @@ namespace Org.BouncyCastle.Asn1.TeleTrust } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - 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) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -493,70 +490,64 @@ namespace Org.BouncyCastle.Asn1.TeleTrust DefineCurve("brainpoolP512t1", TeleTrusTObjectIdentifiers.BrainpoolP512T1, BrainpoolP512t1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. 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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. 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. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) - { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (string)names[oid]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - get { return new EnumerableProxy(names.Values); } + return objIds.TryGetValue(name, out var oid) ? oid : null; } - public static DerObjectIdentifier GetOid( - short curvesize, - bool twisted) + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - return GetOid("brainpoolP" + curvesize + (twisted ? "t" : "r") + "1"); + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/x9/ECNamedCurveTable.cs b/crypto/src/asn1/x9/ECNamedCurveTable.cs index f0d70272b..9243c341e 100644 --- a/crypto/src/asn1/x9/ECNamedCurveTable.cs +++ b/crypto/src/asn1/x9/ECNamedCurveTable.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1.Anssi; using Org.BouncyCastle.Asn1.CryptoPro; @@ -7,23 +7,14 @@ using Org.BouncyCastle.Asn1.GM; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.TeleTrust; -using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Asn1.X9 { - /** - * A general class that reads all X9.62 style EC curve tables. - */ + /// A unified elliptic curve registry of the various standard-specific registries. public class ECNamedCurveTable { - /** - * return a X9ECParameters object representing the passed in named - * curve. The routine returns null if the curve is not present. - * - * @param name the name of the curve requested - * @return an X9ECParameters object or null if the curve is not available. - */ + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { X9ECParameters ecP = X962NamedCurves.GetByName(name); @@ -45,7 +36,7 @@ namespace Org.BouncyCastle.Asn1.X9 } if (ecP == null) { - ecP = ECGost3410NamedCurves.GetByNameX9(name); + ecP = ECGost3410NamedCurves.GetByName(name); } if (ecP == null) { @@ -54,6 +45,12 @@ namespace Org.BouncyCastle.Asn1.X9 return ecP; } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { X9ECParametersHolder holder = X962NamedCurves.GetByNameLazy(name); @@ -84,6 +81,76 @@ namespace Org.BouncyCastle.Asn1.X9 return holder; } + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. + public static X9ECParameters GetByOid(DerObjectIdentifier oid) + { + X9ECParameters ecP = X962NamedCurves.GetByOid(oid); + if (ecP == null) + { + ecP = SecNamedCurves.GetByOid(oid); + } + + // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup + + if (ecP == null) + { + ecP = TeleTrusTNamedCurves.GetByOid(oid); + } + if (ecP == null) + { + ecP = AnssiNamedCurves.GetByOid(oid); + } + if (ecP == null) + { + ecP = ECGost3410NamedCurves.GetByOid(oid); + } + if (ecP == null) + { + ecP = GMNamedCurves.GetByOid(oid); + } + return ecP; + } + + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. + 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; + } + + /// Look up the name of the curve with the given OID. + /// The OID for the curve. public static string GetName(DerObjectIdentifier oid) { string name = X962NamedCurves.GetName(oid); @@ -114,12 +181,8 @@ namespace Org.BouncyCastle.Asn1.X9 return name; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ + /// Look up the OID of the curve with the given name. + /// The name of the curve. public static DerObjectIdentifier GetOid(string name) { DerObjectIdentifier oid = X962NamedCurves.GetOid(name); @@ -150,89 +213,20 @@ namespace Org.BouncyCastle.Asn1.X9 return oid; } - /** - * return a X9ECParameters object representing the passed in named - * curve. - * - * @param oid the object id of the curve requested - * @return an X9ECParameters object or null if the curve is not available. - */ - public static X9ECParameters GetByOid(DerObjectIdentifier oid) - { - X9ECParameters ecP = X962NamedCurves.GetByOid(oid); - if (ecP == null) - { - ecP = SecNamedCurves.GetByOid(oid); - } - - // NOTE: All the NIST curves are currently from SEC, so no point in redundant OID lookup - - if (ecP == null) - { - ecP = TeleTrusTNamedCurves.GetByOid(oid); - } - if (ecP == null) - { - ecP = AnssiNamedCurves.GetByOid(oid); - } - if (ecP == null) - { - ecP = ECGost3410NamedCurves.GetByOidX9(oid); - } - if (ecP == null) - { - ecP = GMNamedCurves.GetByOid(oid); - } - 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. - * - * @return an enumeration of the names of the available curves. - */ - public static IEnumerable Names + /// Enumerate the available curve names in all the registries. + public static IEnumerable Names { get { - IList v = Platform.CreateArrayList(); - CollectionUtilities.AddRange(v, X962NamedCurves.Names); - CollectionUtilities.AddRange(v, SecNamedCurves.Names); - CollectionUtilities.AddRange(v, NistNamedCurves.Names); - CollectionUtilities.AddRange(v, TeleTrusTNamedCurves.Names); - CollectionUtilities.AddRange(v, AnssiNamedCurves.Names); - CollectionUtilities.AddRange(v, ECGost3410NamedCurves.Names); - CollectionUtilities.AddRange(v, GMNamedCurves.Names); - return v; + var result = new List(); + result.AddRange(X962NamedCurves.Names); + result.AddRange(SecNamedCurves.Names); + result.AddRange(NistNamedCurves.Names); + result.AddRange(TeleTrusTNamedCurves.Names); + result.AddRange(AnssiNamedCurves.Names); + result.AddRange(ECGost3410NamedCurves.Names); + result.AddRange(GMNamedCurves.Names); + return result; } } } diff --git a/crypto/src/asn1/x9/X962NamedCurves.cs b/crypto/src/asn1/x9/X962NamedCurves.cs index 4fd3c8d1e..e0fb625f9 100644 --- a/crypto/src/asn1/x9/X962NamedCurves.cs +++ b/crypto/src/asn1/x9/X962NamedCurves.cs @@ -1,24 +1,17 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Math; 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.X9 { - /** - * table of the current named curves defined in X.962 EC-DSA. - */ - public sealed class X962NamedCurves + /// Elliptic curve registry for the curves defined in X.962 EC-DSA. + public static class X962NamedCurves { - private X962NamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -253,9 +246,6 @@ namespace Org.BouncyCastle.Asn1.X9 } } - /* - * F2m Curves - */ internal class C2pnb163v1Holder : X9ECParametersHolder { @@ -768,17 +758,16 @@ namespace Org.BouncyCastle.Asn1.X9 } } + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); - 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) + private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - objIds.Add(Platform.ToUpperInvariant(name), oid); + objIds.Add(name, oid); names.Add(oid, name); curves.Add(oid, holder); } @@ -810,63 +799,64 @@ namespace Org.BouncyCastle.Asn1.X9 DefineCurve("c2tnb431r1", X9ObjectIdentifiers.C2Tnb431r1, C2tnb431r1Holder.Instance); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { DerObjectIdentifier oid = GetOid(name); return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. 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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)curves[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid( - string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName( - DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)names[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names.Values); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/crypto/ec/CustomNamedCurves.cs b/crypto/src/crypto/ec/CustomNamedCurves.cs index bf2b7f486..b1b97551c 100644 --- a/crypto/src/crypto/ec/CustomNamedCurves.cs +++ b/crypto/src/crypto/ec/CustomNamedCurves.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.GM; @@ -11,18 +11,14 @@ using Org.BouncyCastle.Math.EC.Custom.GM; using Org.BouncyCastle.Math.EC.Custom.Sec; using Org.BouncyCastle.Math.EC.Endo; using Org.BouncyCastle.Math.EC.Multiplier; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Crypto.EC { - public sealed class CustomNamedCurves + /// Elliptic curve registry for various customized curve implementations. + public static class CustomNamedCurves { - private CustomNamedCurves() - { - } - private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding) { X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding)); @@ -40,9 +36,6 @@ namespace Org.BouncyCastle.Crypto.EC return c.Configure().SetEndomorphism(new GlvTypeBEndomorphism(c, p)).Create(); } - /* - * secp128r1 - */ internal class SecP128R1Holder : X9ECParametersHolder { @@ -65,9 +58,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp160k1 - */ internal class SecP160K1Holder : X9ECParametersHolder { @@ -103,9 +93,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp160r1 - */ internal class SecP160R1Holder : X9ECParametersHolder { @@ -128,9 +115,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp160r2 - */ internal class SecP160R2Holder : X9ECParametersHolder { @@ -153,9 +137,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * secp192k1 - */ internal class SecP192K1Holder : X9ECParametersHolder { @@ -191,9 +172,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp192r1 - */ internal class SecP192R1Holder : X9ECParametersHolder { @@ -216,9 +194,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp224k1 - */ internal class SecP224K1Holder : X9ECParametersHolder { @@ -254,9 +229,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp224r1 - */ internal class SecP224R1Holder : X9ECParametersHolder { @@ -279,9 +251,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp256k1 - */ internal class SecP256K1Holder : X9ECParametersHolder { @@ -317,9 +286,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp256r1 - */ internal class SecP256R1Holder : X9ECParametersHolder { @@ -342,9 +308,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp384r1 - */ internal class SecP384R1Holder : X9ECParametersHolder { @@ -368,9 +331,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * secp521r1 - */ internal class SecP521R1Holder : X9ECParametersHolder { @@ -394,9 +354,6 @@ namespace Org.BouncyCastle.Crypto.EC } } - /* - * sect113r1 - */ internal class SecT113R1Holder : X9ECParametersHolder { @@ -419,9 +376,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect113r2 - */ internal class SecT113R2Holder : X9ECParametersHolder { @@ -444,9 +398,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect131r1 - */ internal class SecT131R1Holder : X9ECParametersHolder { @@ -469,9 +420,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect131r2 - */ internal class SecT131R2Holder : X9ECParametersHolder { @@ -494,9 +442,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect163k1 - */ internal class SecT163K1Holder : X9ECParametersHolder { @@ -519,9 +464,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect163r1 - */ internal class SecT163R1Holder : X9ECParametersHolder { @@ -544,9 +486,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect163r2 - */ internal class SecT163R2Holder : X9ECParametersHolder { @@ -569,9 +508,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect193r1 - */ internal class SecT193R1Holder : X9ECParametersHolder { @@ -594,9 +530,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect193r2 - */ internal class SecT193R2Holder : X9ECParametersHolder { @@ -619,9 +552,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect233k1 - */ internal class SecT233K1Holder : X9ECParametersHolder { @@ -644,9 +574,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect233r1 - */ internal class SecT233R1Holder : X9ECParametersHolder { @@ -669,9 +596,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect239k1 - */ internal class SecT239K1Holder : X9ECParametersHolder { @@ -694,9 +618,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect283k1 - */ internal class SecT283K1Holder : X9ECParametersHolder { @@ -720,9 +641,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect283r1 - */ internal class SecT283R1Holder : X9ECParametersHolder { @@ -746,9 +664,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect409k1 - */ internal class SecT409K1Holder : X9ECParametersHolder { @@ -772,9 +687,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect409r1 - */ internal class SecT409R1Holder : X9ECParametersHolder { @@ -798,9 +710,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect571k1 - */ internal class SecT571K1Holder : X9ECParametersHolder { @@ -824,9 +733,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sect571r1 - */ internal class SecT571R1Holder : X9ECParametersHolder { @@ -850,9 +756,6 @@ namespace Org.BouncyCastle.Crypto.EC } }; - /* - * sm2p256v1 - */ internal class SM2P256V1Holder : X9ECParametersHolder { @@ -875,32 +778,26 @@ namespace Org.BouncyCastle.Crypto.EC } } - - private static readonly IDictionary nameToCurve = Platform.CreateHashtable(); - private static readonly IDictionary nameToOid = Platform.CreateHashtable(); - private static readonly IDictionary oidToCurve = Platform.CreateHashtable(); - private static readonly IDictionary oidToName = Platform.CreateHashtable(); - private static readonly IList names = Platform.CreateArrayList(); + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary curves = + new Dictionary(); + private static readonly Dictionary names = + new Dictionary(); private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder) { - names.Add(name); - oidToName.Add(oid, name); - oidToCurve.Add(oid, holder); - name = Platform.ToUpperInvariant(name); - nameToOid.Add(name, oid); - nameToCurve.Add(name, holder); + objIds.Add(name, oid); + names.Add(oid, name); + curves.Add(oid, holder); } private static void DefineCurveAlias(string name, DerObjectIdentifier oid) { - object curve = oidToCurve[oid]; - if (curve == null) + if (!curves.ContainsKey(oid)) throw new InvalidOperationException(); - name = Platform.ToUpperInvariant(name); - nameToOid.Add(name, oid); - nameToCurve.Add(name, curve); + objIds.Add(name, oid); } static CustomNamedCurves() @@ -961,60 +858,64 @@ namespace Org.BouncyCastle.Crypto.EC DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1); } + /// Look up the for the curve with the given name. + /// The name of the curve. public static X9ECParameters GetByName(string name) { - X9ECParametersHolder holder = GetByNameLazy(name); - return holder == null ? null : holder.Parameters; + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOid(oid); } + /// Look up an for the curve with the given name. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The name of the curve. public static X9ECParametersHolder GetByNameLazy(string name) { - return (X9ECParametersHolder)nameToCurve[Platform.ToUpperInvariant(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. - */ + /// Look up the for the curve with the given + /// OID. + /// The OID for the curve. public static X9ECParameters GetByOid(DerObjectIdentifier oid) { - X9ECParametersHolder holder = GetByOidLazy(oid); - return holder == null ? null : holder.Parameters; + return GetByOidLazy(oid)?.Parameters; } + /// Look up an for the curve with the given + /// OID. + /// + /// Allows accessing the curve without necessarily triggering the creation of the + /// full . + /// + /// The OID for the curve. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid) { - return (X9ECParametersHolder)oidToCurve[oid]; + return curves.TryGetValue(oid, out var holder) ? holder : null; } - /** - * return the object identifier signified by the passed in name. Null - * if there is no object identifier associated with name. - * - * @return the object identifier associated with name, if present. - */ - public static DerObjectIdentifier GetOid(string name) + /// Look up the name of the curve with the given OID. + /// The OID for the curve. + public static string GetName(DerObjectIdentifier oid) { - return (DerObjectIdentifier)nameToOid[Platform.ToUpperInvariant(name)]; + return names.TryGetValue(oid, out var name) ? name : null; } - /** - * return the named curve name represented by the given object identifier. - */ - public static string GetName(DerObjectIdentifier oid) + /// Look up the OID of the curve with the given name. + /// The name of the curve. + public static DerObjectIdentifier GetOid(string name) { - return (string)oidToName[oid]; + return objIds.TryGetValue(name, out var oid) ? oid : null; } - /** - * returns an enumeration containing the name strings for curves - * contained in this structure. - */ - public static IEnumerable Names + /// Enumerate the available curve names in this registry. + public static IEnumerable Names { - get { return new EnumerableProxy(names); } + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index a8c4d94d0..93f8a2260 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -135,7 +135,7 @@ namespace Org.BouncyCastle.Security Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance( algID.Parameters.ToAsn1Object()); - X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(gostParams.PublicKeyParamSet); + X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet); if (ecP == null) throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key"); @@ -198,7 +198,7 @@ namespace Org.BouncyCastle.Security if (p is Asn1Sequence && (Asn1Sequence.GetInstance(p).Count == 2 || Asn1Sequence.GetInstance(p).Count == 3)) { - X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(gostParams.PublicKeyParamSet); + X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet); ecSpec = new ECGost3410Parameters( new ECNamedDomainParameters( diff --git a/crypto/src/security/PublicKeyFactory.cs b/crypto/src/security/PublicKeyFactory.cs index 10b2aacdc..65baf003c 100644 --- a/crypto/src/security/PublicKeyFactory.cs +++ b/crypto/src/security/PublicKeyFactory.cs @@ -162,7 +162,7 @@ namespace Org.BouncyCastle.Security Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters); DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet; - X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet); + X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(publicKeyParamSet); if (ecP == null) return null; @@ -238,7 +238,7 @@ namespace Org.BouncyCastle.Security DerObjectIdentifier publicKeyParamSet = gostParams.PublicKeyParamSet; ECGost3410Parameters ecDomainParameters =new ECGost3410Parameters( - new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet)), + new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOid(publicKeyParamSet)), publicKeyParamSet, gostParams.DigestParamSet, gostParams.EncryptionParamSet); diff --git a/crypto/src/util/collections/CollectionUtilities.cs b/crypto/src/util/collections/CollectionUtilities.cs index 35ee60e41..37551e5b3 100644 --- a/crypto/src/util/collections/CollectionUtilities.cs +++ b/crypto/src/util/collections/CollectionUtilities.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Text; namespace Org.BouncyCastle.Utilities.Collections @@ -14,6 +15,16 @@ namespace Org.BouncyCastle.Utilities.Collections } } + public static IEnumerable Proxy(IEnumerable e) + { + return new EnumerableProxy(e); + } + + public static IEnumerable Proxy(IEnumerable e) + { + return new EnumerableProxy(e); + } + public static IDictionary ReadOnly(IDictionary d) { return new UnmodifiableDictionaryProxy(d); diff --git a/crypto/src/util/collections/EnumerableProxy.cs b/crypto/src/util/collections/EnumerableProxy.cs index 9eec4af21..196b4d9df 100644 --- a/crypto/src/util/collections/EnumerableProxy.cs +++ b/crypto/src/util/collections/EnumerableProxy.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; namespace Org.BouncyCastle.Utilities.Collections { @@ -22,4 +23,28 @@ namespace Org.BouncyCastle.Utilities.Collections return inner.GetEnumerator(); } } + + internal sealed class EnumerableProxy + : IEnumerable + { + private readonly IEnumerable m_inner; + + internal EnumerableProxy(IEnumerable inner) + { + if (inner == null) + throw new ArgumentNullException("inner"); + + m_inner = inner; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return m_inner.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return m_inner.GetEnumerator(); + } + } } diff --git a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs index 0e7e90611..7e0a84cbe 100644 --- a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs +++ b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs @@ -30,7 +30,7 @@ namespace Org.BouncyCastle.Crypto.Tests public SimpleTestResult EncodeRecodePublicKey() { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); ECKeyGenerationParameters paramameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); @@ -118,7 +118,7 @@ namespace Org.BouncyCastle.Crypto.Tests private SimpleTestResult EncodeRecodePrivateKey() { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); @@ -338,7 +338,7 @@ namespace Org.BouncyCastle.Crypto.Tests public SimpleTestResult EncodeDecodePrivateLW(string oidStr, DerObjectIdentifier digest) { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); @@ -421,7 +421,7 @@ namespace Org.BouncyCastle.Crypto.Tests public SimpleTestResult EncodeDecodePublicLW(string oidStr, DerObjectIdentifier digest) { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOidX9(oid)); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); diff --git a/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs b/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs index e36947b11..059416427 100644 --- a/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs +++ b/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs @@ -1,17 +1,13 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.Text; using NUnit.Framework; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.EC; -using Org.BouncyCastle.Math; -using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Date; @@ -98,6 +94,8 @@ namespace Org.BouncyCastle.Math.EC.Tests Console.WriteLine(sb.ToString()); } } + + Console.Out.Flush(); } private double RandMult(SecureRandom random, ECPoint g, BigInteger n) @@ -175,12 +173,12 @@ namespace Org.BouncyCastle.Math.EC.Tests [Test, Explicit] public void TestMultiply() { - ArrayList nameList = new ArrayList(); - CollectionUtilities.AddRange(nameList, ECNamedCurveTable.Names); - CollectionUtilities.AddRange(nameList, CustomNamedCurves.Names); + var names = new List(); + names.AddRange(ECNamedCurveTable.Names); + names.AddRange(CustomNamedCurves.Names); + + names.Sort(); - string[] names = (string[])nameList.ToArray(typeof(string)); - Array.Sort(names); ISet oids = new HashSet(); foreach (string name in names) { diff --git a/crypto/test/src/math/ec/test/ECPointTest.cs b/crypto/test/src/math/ec/test/ECPointTest.cs index f3fc6e592..4e3fc5832 100644 --- a/crypto/test/src/math/ec/test/ECPointTest.cs +++ b/crypto/test/src/math/ec/test/ECPointTest.cs @@ -1,11 +1,10 @@ using System; -using System.Collections; +using System.Collections.Generic; using NUnit.Framework; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.EC; -using Org.BouncyCastle.Math.EC.Multiplier; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; @@ -535,9 +534,9 @@ namespace Org.BouncyCastle.Math.EC.Tests [Test] public void TestAddSubtractMultiplyTwiceEncoding() { - ArrayList names = new ArrayList(); - CollectionUtilities.AddRange(names, ECNamedCurveTable.Names); - CollectionUtilities.AddRange(names, CustomNamedCurves.Names); + var names = new List(); + names.AddRange(ECNamedCurveTable.Names); + names.AddRange(CustomNamedCurves.Names); ISet uniqNames = new HashSet(names); -- cgit 1.5.1 From c46292dac6e5cea7a6b2c01ca867ffa26ee22ae7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Jun 2022 20:39:54 +0700 Subject: Cleanup in tests --- crypto/test/src/pqc/crypto/lms/HSSTests.cs | 2 +- crypto/test/src/pqc/crypto/lms/LMSTests.cs | 16 +++------------- 2 files changed, 4 insertions(+), 14 deletions(-) (limited to 'crypto/test') diff --git a/crypto/test/src/pqc/crypto/lms/HSSTests.cs b/crypto/test/src/pqc/crypto/lms/HSSTests.cs index 6918da102..d97038d3b 100644 --- a/crypto/test/src/pqc/crypto/lms/HSSTests.cs +++ b/crypto/test/src/pqc/crypto/lms/HSSTests.cs @@ -470,7 +470,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] hssPubEnc = null; MemoryStream fixedESBuffer = new MemoryStream(); IList sigVectors = new ArrayList(); - int d = 0, j = 0; + int d = 0; string line; while ((line = sr.ReadLine()) != null) diff --git a/crypto/test/src/pqc/crypto/lms/LMSTests.cs b/crypto/test/src/pqc/crypto/lms/LMSTests.cs index 4b5d6206f..dbc541674 100644 --- a/crypto/test/src/pqc/crypto/lms/LMSTests.cs +++ b/crypto/test/src/pqc/crypto/lms/LMSTests.cs @@ -158,20 +158,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms ctx.Update((byte)1); Assert.Fail("Digest reuse after signature taken."); } - catch (NullReferenceException npe) + catch (NullReferenceException) { - Assert.True(true); + // Expected } - } - - public void Main(string[] args) - { - TestCoefFunc(); - TestPrivateKeyRound(); - TestLMS(); - TestContextSingleUse(); - } - } -} \ No newline at end of file +} -- cgit 1.5.1 From 35ad48901c94bf59499ff02a5cc9075e571f545a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 25 Jun 2022 01:32:32 +0700 Subject: Refactoring --- .../src/asn1/cryptopro/GOST3410NamedParameters.cs | 91 +++++++++------------- .../cryptopro/GOST3410PublicKeyAlgParameters.cs | 13 +--- crypto/test/src/math/ec/test/FixedPointTest.cs | 10 +-- 3 files changed, 47 insertions(+), 67 deletions(-) (limited to 'crypto/test') diff --git a/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs b/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs index 66dba51d7..2d183a4f9 100644 --- a/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs +++ b/crypto/src/asn1/cryptopro/GOST3410NamedParameters.cs @@ -1,25 +1,14 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Math; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Asn1.CryptoPro { - /** - * table of the available named parameters for GOST 3410-94. - */ - public sealed class Gost3410NamedParameters + /// Registry of available named parameters for GOST 3410-94. + public static class Gost3410NamedParameters { - private Gost3410NamedParameters() - { - } - - private static readonly IDictionary objIds = Platform.CreateHashtable(); - private static readonly IDictionary parameters = Platform.CreateHashtable(); - private static readonly Gost3410ParamSetParameters cryptoProA = new Gost3410ParamSetParameters( 1024, new BigInteger("127021248288932417465907042777176443525787653508916535812817507265705031260985098497423188333483401180925999995120988934130659205614996724254121049274349357074920312769561451689224110579311248812610229678534638401693520013288995000362260684222750813532307004517341633685004541062586971416883686778842537820383"), @@ -65,59 +54,55 @@ namespace Org.BouncyCastle.Asn1.CryptoPro new BigInteger("133531813272720673433859519948319001217942375967847486899482359599369642528734712461590403327731821410328012529253871914788598993103310567744136196364803064721377826656898686468463277710150809401182608770201615324990468332931294920912776241137878030224355746606283971659376426832674269780880061631528163475887") ); - static Gost3410NamedParameters() - { - parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProA] = cryptoProA; - parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProB] = cryptoProB; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProC] = cryptoProC; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProD] = cryptoProD; - parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProXchA] = cryptoProXchA; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProXchB] = cryptoProXchA; - //parameters[CryptoProObjectIdentifiers.GostR3410x94CryptoProXchC] = cryptoProXchA; + private static readonly Dictionary objIds = + new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary parameters = + new Dictionary(); - objIds["GostR3410-94-CryptoPro-A"] = CryptoProObjectIdentifiers.GostR3410x94CryptoProA; - objIds["GostR3410-94-CryptoPro-B"] = CryptoProObjectIdentifiers.GostR3410x94CryptoProB; - objIds["GostR3410-94-CryptoPro-XchA"] = CryptoProObjectIdentifiers.GostR3410x94CryptoProXchA; + private static void DefineParameters(string name, DerObjectIdentifier oid, + Gost3410ParamSetParameters parameterSet) + { + objIds.Add(name, oid); + parameters.Add(oid, parameterSet); } - /** - * return the GOST3410ParamSetParameters object for the given OID, null if it - * isn't present. - * - * @param oid an object identifier representing a named parameters, if present. - */ - public static Gost3410ParamSetParameters GetByOid( - DerObjectIdentifier oid) + static Gost3410NamedParameters() { - return (Gost3410ParamSetParameters) parameters[oid]; + DefineParameters("GostR3410-94-CryptoPro-A", CryptoProObjectIdentifiers.GostR3410x94CryptoProA, cryptoProA); + DefineParameters("GostR3410-94-CryptoPro-B", CryptoProObjectIdentifiers.GostR3410x94CryptoProB, cryptoProB); + DefineParameters("GostR3410-94-CryptoPro-XchA", CryptoProObjectIdentifiers.GostR3410x94CryptoProXchA, + cryptoProXchA); } - /** - * returns an enumeration containing the name strings for parameters - * contained in this structure. - */ - public static IEnumerable Names + /// Look up the for the parameter set with the given name. + /// + /// The name of the parameter set. + public static Gost3410ParamSetParameters GetByName(string name) { - get { return new EnumerableProxy(objIds.Keys); } + DerObjectIdentifier oid = GetOid(name); + return oid == null ? null : GetByOid(oid); } - public static Gost3410ParamSetParameters GetByName( - string name) + /// Look up the for the parameter set with the given + /// OID. + /// The OID for the parameter set. + public static Gost3410ParamSetParameters GetByOid(DerObjectIdentifier oid) { - DerObjectIdentifier oid = (DerObjectIdentifier) objIds[name]; - - if (oid != null) - { - return (Gost3410ParamSetParameters) parameters[oid]; - } + return parameters.TryGetValue(oid, out var parameterSet) ? parameterSet : null; + } - return null; + /// Look up the OID of the parameter set with the given name. + /// + /// The name of the parameter set. + public static DerObjectIdentifier GetOid(string name) + { + return objIds.TryGetValue(name, out var oid) ? oid : null; } - public static DerObjectIdentifier GetOid( - string name) + /// Enumerate the available parameter set names in this registry. + public static IEnumerable Names { - return (DerObjectIdentifier) objIds[name]; + get { return CollectionUtilities.Proxy(objIds.Keys); } } } } diff --git a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs index c6ea26204..40b69428d 100644 --- a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs +++ b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs @@ -1,7 +1,5 @@ using System; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.CryptoPro { public class Gost3410PublicKeyAlgParameters @@ -11,20 +9,17 @@ namespace Org.BouncyCastle.Asn1.CryptoPro private DerObjectIdentifier digestParamSet; private DerObjectIdentifier encryptionParamSet; - public static Gost3410PublicKeyAlgParameters GetInstance( - Asn1TaggedObject obj, - bool explicitly) + public static Gost3410PublicKeyAlgParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - return GetInstance(Asn1Sequence.GetInstance(obj, explicitly)); + return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } - public static Gost3410PublicKeyAlgParameters GetInstance( - object obj) + public static Gost3410PublicKeyAlgParameters GetInstance(object obj) { if (obj == null || obj is Gost3410PublicKeyAlgParameters) return (Gost3410PublicKeyAlgParameters)obj; - return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance((obj))); + return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance(obj)); } public Gost3410PublicKeyAlgParameters( diff --git a/crypto/test/src/math/ec/test/FixedPointTest.cs b/crypto/test/src/math/ec/test/FixedPointTest.cs index 83e5fab8f..433f956da 100644 --- a/crypto/test/src/math/ec/test/FixedPointTest.cs +++ b/crypto/test/src/math/ec/test/FixedPointTest.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using NUnit.Framework; @@ -23,9 +23,9 @@ namespace Org.BouncyCastle.Math.EC.Tests { FixedPointCombMultiplier M = new FixedPointCombMultiplier(); - ArrayList names = new ArrayList(); - CollectionUtilities.AddRange(names, ECNamedCurveTable.Names); - CollectionUtilities.AddRange(names, CustomNamedCurves.Names); + var names = new List(); + names.AddRange(ECNamedCurveTable.Names); + names.AddRange(CustomNamedCurves.Names); ISet uniqNames = new HashSet(names); @@ -34,7 +34,7 @@ namespace Org.BouncyCastle.Math.EC.Tests X9ECParameters x9A = ECNamedCurveTable.GetByName(name); X9ECParameters x9B = CustomNamedCurves.GetByName(name); - X9ECParameters x9 = x9B != null ? x9B : x9A; + X9ECParameters x9 = x9B ?? x9A; for (int i = 0; i < TestsPerCurve; ++i) { -- cgit 1.5.1