diff options
Diffstat (limited to 'crypto/src')
36 files changed, 330 insertions, 487 deletions
diff --git a/crypto/src/crmf/CertificateRequestMessageBuilder.cs b/crypto/src/crmf/CertificateRequestMessageBuilder.cs index 88d1d87bd..9a33fd964 100644 --- a/crypto/src/crmf/CertificateRequestMessageBuilder.cs +++ b/crypto/src/crmf/CertificateRequestMessageBuilder.cs @@ -1,13 +1,11 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Crmf; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Operators; using Org.BouncyCastle.Math; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crmf { @@ -16,7 +14,7 @@ namespace Org.BouncyCastle.Crmf private readonly BigInteger _certReqId; private X509ExtensionsGenerator _extGenerator; private CertTemplateBuilder _templateBuilder; - private IList _controls = Platform.CreateArrayList(); + private IList<IControl> m_controls = new List<IControl>(); private ISignatureFactory _popSigner; private PKMacBuilder _pkMacBuilder; private char[] _password; @@ -95,7 +93,7 @@ namespace Org.BouncyCastle.Crmf public CertificateRequestMessageBuilder AddControl(IControl control) { - _controls.Add(control); + m_controls.Add(control); return this; } @@ -195,13 +193,12 @@ namespace Org.BouncyCastle.Crmf v.Add(_templateBuilder.Build()); - if (_controls.Count > 0) + if (m_controls.Count > 0) { Asn1EncodableVector controlV = new Asn1EncodableVector(); - foreach (object item in _controls) + foreach (var control in m_controls) { - IControl control = (IControl)item; controlV.Add(new AttributeTypeAndValue(control.Type, control.Value)); } diff --git a/crypto/src/crmf/EncryptedValueBuilder.cs b/crypto/src/crmf/EncryptedValueBuilder.cs index d95896959..0154be58c 100644 --- a/crypto/src/crmf/EncryptedValueBuilder.cs +++ b/crypto/src/crmf/EncryptedValueBuilder.cs @@ -1,15 +1,11 @@ using System; -using System.Collections; using System.IO; -using System.Text; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Crmf; -using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.Utilities; diff --git a/crypto/src/crypto/agreement/ECDHWithKdfBasicAgreement.cs b/crypto/src/crypto/agreement/ECDHWithKdfBasicAgreement.cs index 1de80d1e5..a73e3f438 100644 --- a/crypto/src/crypto/agreement/ECDHWithKdfBasicAgreement.cs +++ b/crypto/src/crypto/agreement/ECDHWithKdfBasicAgreement.cs @@ -1,12 +1,8 @@ using System; -using System.Collections; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Nist; -using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.Agreement.Kdf; -using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; diff --git a/crypto/src/crypto/agreement/ECMqvWithKdfBasicAgreement.cs b/crypto/src/crypto/agreement/ECMqvWithKdfBasicAgreement.cs index 7d79fc468..248e04e27 100644 --- a/crypto/src/crypto/agreement/ECMqvWithKdfBasicAgreement.cs +++ b/crypto/src/crypto/agreement/ECMqvWithKdfBasicAgreement.cs @@ -1,12 +1,8 @@ using System; -using System.Collections; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Nist; -using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.Agreement.Kdf; -using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; diff --git a/crypto/src/crypto/digests/SkeinDigest.cs b/crypto/src/crypto/digests/SkeinDigest.cs index 7fae9c630..394f0acd5 100644 --- a/crypto/src/crypto/digests/SkeinDigest.cs +++ b/crypto/src/crypto/digests/SkeinDigest.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; -using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; diff --git a/crypto/src/crypto/digests/SkeinEngine.cs b/crypto/src/crypto/digests/SkeinEngine.cs index e93ec601c..2f38115d2 100644 --- a/crypto/src/crypto/digests/SkeinEngine.cs +++ b/crypto/src/crypto/digests/SkeinEngine.cs @@ -1,9 +1,10 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Crypto.Digests { @@ -131,7 +132,7 @@ namespace Org.BouncyCastle.Crypto.Digests * Precalculated UBI(CFG) states for common state/output combinations without key or other * pre-message params. */ - private static readonly IDictionary INITIAL_STATES = Platform.CreateHashtable(); + private static readonly IDictionary<int, ulong[]> InitialStates = new Dictionary<int, ulong[]>(); static SkeinEngine() { @@ -213,7 +214,7 @@ namespace Org.BouncyCastle.Crypto.Digests private static void InitialState(int blockSize, int outputSize, ulong[] state) { - INITIAL_STATES.Add(VariantIdentifier(blockSize / 8, outputSize / 8), state); + InitialStates.Add(VariantIdentifier(blockSize / 8, outputSize / 8), state); } private static int VariantIdentifier(int blockSizeBytes, int outputSizeBytes) @@ -617,16 +618,17 @@ namespace Org.BouncyCastle.Crypto.Digests UbiInit(PARAM_TYPE_MESSAGE); } - private void InitParams(IDictionary parameters) + private void InitParams(IDictionary<int, byte[]> parameters) { - IEnumerator keys = parameters.Keys.GetEnumerator(); - IList pre = Platform.CreateArrayList(); - IList post = Platform.CreateArrayList(); + //IEnumerator keys = parameters.Keys.GetEnumerator(); + var pre = new List<Parameter>(); + var post = new List<Parameter>(); - while (keys.MoveNext()) + //while (keys.MoveNext()) + foreach (var parameter in parameters) { - int type = (int)keys.Current; - byte[] value = (byte[])parameters[type]; + int type = parameter.Key; + byte[] value = parameter.Value; if (type == PARAM_TYPE_KEY) { @@ -655,7 +657,7 @@ namespace Org.BouncyCastle.Crypto.Digests */ private void CreateInitialState() { - ulong[] precalc = (ulong[])INITIAL_STATES[VariantIdentifier(BlockSize, OutputSize)]; + var precalc = CollectionUtilities.GetValueOrNull(InitialStates, VariantIdentifier(BlockSize, OutputSize)); if ((key == null) && (precalc != null)) { // Precalculated UBI(CFG) diff --git a/crypto/src/crypto/engines/Dstu7624Engine.cs b/crypto/src/crypto/engines/Dstu7624Engine.cs index 4242d3a7a..844a873a8 100644 --- a/crypto/src/crypto/engines/Dstu7624Engine.cs +++ b/crypto/src/crypto/engines/Dstu7624Engine.cs @@ -1,9 +1,7 @@ using System; -using System.Collections; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Utilities; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Engines { diff --git a/crypto/src/crypto/engines/Dstu7624WrapEngine.cs b/crypto/src/crypto/engines/Dstu7624WrapEngine.cs index 9cb98245f..823390187 100644 --- a/crypto/src/crypto/engines/Dstu7624WrapEngine.cs +++ b/crypto/src/crypto/engines/Dstu7624WrapEngine.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Utilities; @@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Crypto.Engines Array.Copy(buffer, 0, B, 0, blockSize / 2); //Console.WriteLine("B0: "+ Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B)); - IList bTemp = Platform.CreateArrayList(); + var bTemp = new List<byte[]>(); int bHalfBlocksLen = buffer.Length - blockSize / 2; int bufOff = blockSize / 2; while (bHalfBlocksLen != 0) @@ -83,7 +83,7 @@ namespace Org.BouncyCastle.Crypto.Engines for (int j = 0; j < V; j++) { Array.Copy(B, 0, buffer, 0, blockSize / 2); - Array.Copy((byte[])bTemp[0], 0, buffer, blockSize / 2, blockSize / 2); + Array.Copy(bTemp[0], 0, buffer, blockSize / 2, blockSize / 2); engine.ProcessBlock(buffer, 0, buffer, 0); @@ -97,10 +97,10 @@ namespace Org.BouncyCastle.Crypto.Engines for (int i = 2; i < n; i++) { - Array.Copy((byte[])bTemp[i - 1], 0, (byte[])bTemp[i - 2], 0, blockSize / 2); + Array.Copy(bTemp[i - 1], 0, bTemp[i - 2], 0, blockSize / 2); } - Array.Copy(buffer, 0, (byte[])bTemp[n - 2], 0, blockSize / 2); + Array.Copy(buffer, 0, bTemp[n - 2], 0, blockSize / 2); //Console.WriteLine("B" + j.ToString() + ": " + Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B)); //Console.WriteLine("b: " + Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[0])); @@ -140,7 +140,7 @@ namespace Org.BouncyCastle.Crypto.Engines Array.Copy(buffer, 0, B, 0, blockSize / 2); //Console.WriteLine("B18: " + Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B)); - IList bTemp = Platform.CreateArrayList(); + var bTemp = new List<byte[]>(); int bHalfBlocksLen = buffer.Length - blockSize / 2; int bufOff = blockSize / 2; @@ -160,7 +160,7 @@ namespace Org.BouncyCastle.Crypto.Engines for (int j = 0; j < V; j++) { - Array.Copy((byte[])bTemp[n - 2], 0, buffer, 0, blockSize / 2); + Array.Copy(bTemp[n - 2], 0, buffer, 0, blockSize / 2); Array.Copy(B, 0, buffer, blockSize / 2, blockSize / 2); byte[] intArray = Pack.UInt32_To_LE((uint)(V - j)); @@ -179,10 +179,10 @@ namespace Org.BouncyCastle.Crypto.Engines for (int i = 2; i < n; i++) { - Array.Copy((byte[])bTemp[n - i - 1], 0, (byte[])bTemp[n - i], 0, blockSize / 2); + Array.Copy(bTemp[n - i - 1], 0, bTemp[n - i], 0, blockSize / 2); } - Array.Copy(buffer, blockSize / 2, (byte[])bTemp[0], 0, blockSize / 2); + Array.Copy(buffer, blockSize / 2, bTemp[0], 0, blockSize / 2); //Console.WriteLine("B" + (V - j - 1).ToString() + ": " + Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(B)); //Console.WriteLine("b: " + Org.BouncyCastle.Utilities.Encoders.Hex.ToHexString(bTemp[0])); @@ -197,7 +197,7 @@ namespace Org.BouncyCastle.Crypto.Engines for (int i = 0; i < n - 1; i++) { - Array.Copy((byte[])bTemp[i], 0, buffer, bufOff, blockSize / 2); + Array.Copy(bTemp[i], 0, buffer, bufOff, blockSize / 2); bufOff += blockSize / 2; } diff --git a/crypto/src/crypto/engines/GOST28147Engine.cs b/crypto/src/crypto/engines/GOST28147Engine.cs index acf0be27b..8ef8aeb13 100644 --- a/crypto/src/crypto/engines/GOST28147Engine.cs +++ b/crypto/src/crypto/engines/GOST28147Engine.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; @@ -117,7 +117,8 @@ namespace Org.BouncyCastle.Crypto.Engines // // pre-defined sbox table // - private static readonly IDictionary sBoxes = Platform.CreateHashtable(); + private static readonly Dictionary<string, byte[]> m_sBoxes = + new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase); static Gost28147Engine() { @@ -133,7 +134,7 @@ namespace Org.BouncyCastle.Crypto.Engines private static void AddSBox(string sBoxName, byte[] sBox) { - sBoxes.Add(Platform.ToUpperInvariant(sBoxName), sBox); + m_sBoxes.Add(sBoxName, sBox); } /** @@ -351,12 +352,9 @@ namespace Org.BouncyCastle.Crypto.Engines * @param sBoxName name of the S-Box * @return byte array representing the S-Box */ - public static byte[] GetSBox( - string sBoxName) + public static byte[] GetSBox(string sBoxName) { - byte[] sBox = (byte[])sBoxes[Platform.ToUpperInvariant(sBoxName)]; - - if (sBox == null) + if (!m_sBoxes.TryGetValue(sBoxName, out var sBox)) { throw new ArgumentException("Unknown S-Box - possible types: " + "\"Default\", \"E-Test\", \"E-A\", \"E-B\", \"E-C\", \"E-D\", \"D-Test\", \"D-A\"."); @@ -367,13 +365,10 @@ namespace Org.BouncyCastle.Crypto.Engines public static string GetSBoxName(byte[] sBox) { - foreach (string name in sBoxes.Keys) + foreach (var entry in m_sBoxes) { - byte[] sb = (byte[])sBoxes[name]; - if (Arrays.AreEqual(sb, sBox)) - { - return name; - } + if (Arrays.AreEqual(entry.Value, sBox)) + return entry.Key; } throw new ArgumentException("SBOX provided did not map to a known one"); diff --git a/crypto/src/crypto/engines/NaccacheSternEngine.cs b/crypto/src/crypto/engines/NaccacheSternEngine.cs index 713ed9472..39fb7c9ec 100644 --- a/crypto/src/crypto/engines/NaccacheSternEngine.cs +++ b/crypto/src/crypto/engines/NaccacheSternEngine.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; @@ -18,7 +18,7 @@ namespace Org.BouncyCastle.Crypto.Engines private NaccacheSternKeyParameters key; - private IList[] lookup = null; + private IList<BigInteger>[] lookup = null; public string AlgorithmName { @@ -48,22 +48,19 @@ namespace Org.BouncyCastle.Crypto.Engines if (!this.forEncryption) { NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key; - IList primes = priv.SmallPrimesList; - lookup = new IList[primes.Count]; + var primes = priv.SmallPrimesList; + lookup = new IList<BigInteger>[primes.Count]; for (int i = 0; i < primes.Count; i++) { - BigInteger actualPrime = (BigInteger) primes[i]; + BigInteger actualPrime = primes[i]; int actualPrimeValue = actualPrime.IntValue; - lookup[i] = Platform.CreateArrayList(actualPrimeValue); + lookup[i] = new List<BigInteger>(actualPrimeValue); lookup[i].Add(BigInteger.One); BigInteger accJ = BigInteger.Zero; - for (int j = 1; j < actualPrimeValue; j++) { -// BigInteger bigJ = BigInteger.ValueOf(j); -// accJ = priv.PhiN.Multiply(bigJ); accJ = accJ.Add(priv.PhiN); BigInteger comp = accJ.Divide(actualPrime); lookup[i].Add(priv.G.ModPow(comp, priv.Modulus)); @@ -147,31 +144,30 @@ namespace Org.BouncyCastle.Crypto.Engines } else { - IList plain = Platform.CreateArrayList(); + var plain = new List<BigInteger>(); NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key; - IList primes = priv.SmallPrimesList; + var primes = priv.SmallPrimesList; // Get Chinese Remainders of CipherText for (int i = 0; i < primes.Count; i++) { BigInteger exp = input.ModPow(priv.PhiN.Divide((BigInteger)primes[i]), priv.Modulus); - IList al = lookup[i]; - if (lookup[i].Count != ((BigInteger)primes[i]).IntValue) + var al = lookup[i]; + if (lookup[i].Count != primes[i].IntValue) { throw new InvalidCipherTextException("Error in lookup Array for " - + ((BigInteger)primes[i]).IntValue + + primes[i].IntValue + ": Size mismatch. Expected ArrayList with length " - + ((BigInteger)primes[i]).IntValue + " but found ArrayList of length " + + primes[i].IntValue + " but found ArrayList of length " + lookup[i].Count); } int lookedup = al.IndexOf(exp); if (lookedup == -1) - { throw new InvalidCipherTextException("Lookup failed"); - } + plain.Add(BigInteger.ValueOf(lookedup)); } - BigInteger test = chineseRemainder(plain, primes); + BigInteger test = ChineseRemainder(plain, primes); // Should not be used as an oracle, so reencrypt output to see // if it corresponds to input @@ -328,21 +324,21 @@ namespace Org.BouncyCastle.Crypto.Engines * the primes p_i * @return an integer x for that x % p_i == c_i */ - private static BigInteger chineseRemainder(IList congruences, IList primes) + private static BigInteger ChineseRemainder(IList<BigInteger> congruences, IList<BigInteger> primes) { BigInteger retval = BigInteger.Zero; BigInteger all = BigInteger.One; for (int i = 0; i < primes.Count; i++) { - all = all.Multiply((BigInteger)primes[i]); + all = all.Multiply(primes[i]); } for (int i = 0; i < primes.Count; i++) { - BigInteger a = (BigInteger)primes[i]; + BigInteger a = primes[i]; BigInteger b = all.Divide(a); BigInteger b2 = b.ModInverse(a); BigInteger tmp = b.Multiply(b2); - tmp = tmp.Multiply((BigInteger)congruences[i]); + tmp = tmp.Multiply(congruences[i]); retval = retval.Add(tmp); } diff --git a/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs b/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs index d68106844..7a047cd26 100644 --- a/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs +++ b/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs @@ -1,12 +1,9 @@ using System; -using System.Collections; +using System.Collections.Generic; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Crypto.Generators { @@ -52,9 +49,9 @@ namespace Org.BouncyCastle.Crypto.Generators SecureRandom rand = param.Random; int certainty = param.Certainty; - IList smallPrimes = findFirstPrimes(param.CountSmallPrimes); + var smallPrimes = FindFirstPrimes(param.CountSmallPrimes); - smallPrimes = permuteList(smallPrimes, rand); + smallPrimes = PermuteList(smallPrimes, rand); BigInteger u = BigInteger.One; BigInteger v = BigInteger.One; @@ -77,8 +74,8 @@ namespace Org.BouncyCastle.Crypto.Generators // remainingStrength = strength - sigma.bitLength() - _p.bitLength() - // _q.bitLength() - 1 -1 int remainingStrength = strength - sigma.BitLength - 48; - BigInteger a = generatePrime(remainingStrength / 2 + 1, certainty, rand); - BigInteger b = generatePrime(remainingStrength / 2 + 1, certainty, rand); + BigInteger a = GeneratePrime(remainingStrength / 2 + 1, certainty, rand); + BigInteger b = GeneratePrime(remainingStrength / 2 + 1, certainty, rand); BigInteger _p; BigInteger _q; @@ -94,7 +91,7 @@ namespace Org.BouncyCastle.Crypto.Generators { tries++; - _p = generatePrime(24, certainty, rand); + _p = GeneratePrime(24, certainty, rand); p = _p.Multiply(_2au).Add(BigInteger.One); @@ -103,7 +100,7 @@ namespace Org.BouncyCastle.Crypto.Generators for (;;) { - _q = generatePrime(24, certainty, rand); + _q = GeneratePrime(24, certainty, rand); if (_p.Equals(_q)) continue; @@ -135,17 +132,17 @@ namespace Org.BouncyCastle.Crypto.Generators for (;;) { // TODO After the first loop, just regenerate one randomly-selected gPart each time? - IList gParts = Platform.CreateArrayList(); + var gParts = new List<BigInteger>(); for (int ind = 0; ind != smallPrimes.Count; ind++) { - BigInteger i = (BigInteger)smallPrimes[ind]; + BigInteger i = smallPrimes[ind]; BigInteger e = phi_n.Divide(i); for (;;) { tries++; - g = generatePrime(strength, certainty, rand); + g = GeneratePrime(strength, certainty, rand); if (!g.ModPow(e, n).Equals(BigInteger.One)) { @@ -209,10 +206,7 @@ namespace Org.BouncyCastle.Crypto.Generators new NaccacheSternPrivateKeyParameters(g, n, sigma.BitLength, smallPrimes, phi_n)); } - private static BigInteger generatePrime( - int bitLength, - int certainty, - SecureRandom rand) + private static BigInteger GeneratePrime(int bitLength, int certainty, SecureRandom rand) { return new BigInteger(bitLength, certainty, rand); } @@ -227,15 +221,13 @@ namespace Org.BouncyCastle.Crypto.Generators * the source of Randomness for permutation * @return a new IList with the permuted elements. */ - private static IList permuteList( - IList arr, - SecureRandom rand) + private static IList<T> PermuteList<T>(IList<T> arr, SecureRandom rand) { // TODO Create a utility method for generating permutation of first 'n' integers - IList retval = Platform.CreateArrayList(arr.Count); + var retval = new List<T>(arr.Count); - foreach (object element in arr) + foreach (var element in arr) { int index = rand.Next(retval.Count + 1); retval.Insert(index, element); @@ -251,10 +243,9 @@ namespace Org.BouncyCastle.Crypto.Generators * the number of primes to find * @return a vector containing the found primes as Integer */ - private static IList findFirstPrimes( - int count) + private static IList<BigInteger> FindFirstPrimes(int count) { - IList primes = Platform.CreateArrayList(count); + var primes = new List<BigInteger>(count); for (int i = 0; i != count; i++) { diff --git a/crypto/src/crypto/macs/GMac.cs b/crypto/src/crypto/macs/GMac.cs index f2c3990c6..0554c44f0 100644 --- a/crypto/src/crypto/macs/GMac.cs +++ b/crypto/src/crypto/macs/GMac.cs @@ -1,10 +1,7 @@ using System; -using System.Collections; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Parameters; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Macs { diff --git a/crypto/src/crypto/macs/HMac.cs b/crypto/src/crypto/macs/HMac.cs index 3d42aec0f..a717ce4f7 100644 --- a/crypto/src/crypto/macs/HMac.cs +++ b/crypto/src/crypto/macs/HMac.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; diff --git a/crypto/src/crypto/modes/OCBBlockCipher.cs b/crypto/src/crypto/modes/OCBBlockCipher.cs index 91fdfff18..28e88a6c9 100644 --- a/crypto/src/crypto/modes/OCBBlockCipher.cs +++ b/crypto/src/crypto/modes/OCBBlockCipher.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; @@ -38,7 +38,7 @@ namespace Org.BouncyCastle.Crypto.Modes * KEY-DEPENDENT */ // NOTE: elements are lazily calculated - private IList L; + private IList<byte[]> L; private byte[] L_Asterisk, L_Dollar; /* @@ -161,7 +161,7 @@ namespace Org.BouncyCastle.Crypto.Modes this.L_Dollar = OCB_double(L_Asterisk); - this.L = Platform.CreateArrayList(); + this.L = new List<byte[]>(); this.L.Add(OCB_double(L_Dollar)); /* @@ -420,9 +420,9 @@ namespace Org.BouncyCastle.Crypto.Modes { while (n >= L.Count) { - L.Add(OCB_double((byte[]) L[L.Count - 1])); + L.Add(OCB_double(L[L.Count - 1])); } - return (byte[])L[n]; + return L[n]; } protected virtual void ProcessHashBlock() diff --git a/crypto/src/crypto/modes/gcm/Tables1kGcmExponentiator.cs b/crypto/src/crypto/modes/gcm/Tables1kGcmExponentiator.cs index 4a15712c2..15f8e058e 100644 --- a/crypto/src/crypto/modes/gcm/Tables1kGcmExponentiator.cs +++ b/crypto/src/crypto/modes/gcm/Tables1kGcmExponentiator.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; - -using Org.BouncyCastle.Utilities; +using System.Collections.Generic; namespace Org.BouncyCastle.Crypto.Modes.Gcm { @@ -10,7 +8,7 @@ namespace Org.BouncyCastle.Crypto.Modes.Gcm { // A lookup table of the power-of-two powers of 'x' // - lookupPowX2[i] = x^(2^i) - private IList lookupPowX2; + private IList<GcmUtilities.FieldElement> lookupPowX2; public void Init(byte[] x) { @@ -19,7 +17,7 @@ namespace Org.BouncyCastle.Crypto.Modes.Gcm if (lookupPowX2 != null && y.Equals(lookupPowX2[0])) return; - lookupPowX2 = Platform.CreateArrayList(8); + lookupPowX2 = new List<GcmUtilities.FieldElement>(8); lookupPowX2.Add(y); } diff --git a/crypto/src/crypto/operators/Asn1CipherBuilder.cs b/crypto/src/crypto/operators/Asn1CipherBuilder.cs index d5840746f..0561bee4f 100644 --- a/crypto/src/crypto/operators/Asn1CipherBuilder.cs +++ b/crypto/src/crypto/operators/Asn1CipherBuilder.cs @@ -1,17 +1,13 @@ using System; -using System.Collections; using System.IO; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Nist; -using Org.BouncyCastle.Asn1.Ntt; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Cms; using Org.BouncyCastle.Crypto.IO; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Operators { diff --git a/crypto/src/crypto/operators/Asn1KeyWrapper.cs b/crypto/src/crypto/operators/Asn1KeyWrapper.cs index a34d93d54..9e7bffae7 100644 --- a/crypto/src/crypto/operators/Asn1KeyWrapper.cs +++ b/crypto/src/crypto/operators/Asn1KeyWrapper.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Nist; @@ -9,7 +9,6 @@ using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto.Encodings; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.X509; namespace Org.BouncyCastle.Crypto.Operators @@ -155,26 +154,24 @@ namespace Org.BouncyCastle.Crypto.Operators // // Provider // - private static readonly IDictionary providerMap = Platform.CreateHashtable(); + private static readonly Dictionary<string, WrapperProvider> m_providerMap = + new Dictionary<string, WrapperProvider>(StringComparer.OrdinalIgnoreCase); static KeyWrapperUtil() - { - providerMap.Add("RSA/ECB/PKCS1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1)); - providerMap.Add("RSA/NONE/PKCS1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1)); - providerMap.Add("RSA/NONE/OAEPWITHSHA1ANDMGF1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1)); - providerMap.Add("RSA/NONE/OAEPWITHSHA224ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha224)); - providerMap.Add("RSA/NONE/OAEPWITHSHA256ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha256)); - providerMap.Add("RSA/NONE/OAEPWITHSHA384ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha384)); - providerMap.Add("RSA/NONE/OAEPWITHSHA512ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha512)); - providerMap.Add("RSA/NONE/OAEPWITHSHA256ANDMGF1WITHSHA1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha256, OiwObjectIdentifiers.IdSha1)); + m_providerMap.Add("RSA/ECB/PKCS1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1)); + m_providerMap.Add("RSA/NONE/PKCS1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1)); + m_providerMap.Add("RSA/NONE/OAEPWITHSHA1ANDMGF1PADDING", new RsaOaepWrapperProvider(OiwObjectIdentifiers.IdSha1)); + m_providerMap.Add("RSA/NONE/OAEPWITHSHA224ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha224)); + m_providerMap.Add("RSA/NONE/OAEPWITHSHA256ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha256)); + m_providerMap.Add("RSA/NONE/OAEPWITHSHA384ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha384)); + m_providerMap.Add("RSA/NONE/OAEPWITHSHA512ANDMGF1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha512)); + m_providerMap.Add("RSA/NONE/OAEPWITHSHA256ANDMGF1WITHSHA1PADDING", new RsaOaepWrapperProvider(NistObjectIdentifiers.IdSha256, OiwObjectIdentifiers.IdSha1)); } public static IKeyWrapper WrapperForName(string algorithm, ICipherParameters parameters) { - WrapperProvider provider = (WrapperProvider)providerMap[Strings.ToUpperCase(algorithm)]; - - if (provider == null) + if (!m_providerMap.TryGetValue(algorithm, out var provider)) throw new ArgumentException("could not resolve " + algorithm + " to a KeyWrapper"); return (IKeyWrapper)provider.CreateWrapper(true, parameters); @@ -182,8 +179,7 @@ namespace Org.BouncyCastle.Crypto.Operators public static IKeyUnwrapper UnwrapperForName(string algorithm, ICipherParameters parameters) { - WrapperProvider provider = (WrapperProvider)providerMap[Strings.ToUpperCase(algorithm)]; - if (provider == null) + if (!m_providerMap.TryGetValue(algorithm, out var provider)) throw new ArgumentException("could not resolve " + algorithm + " to a KeyUnwrapper"); return (IKeyUnwrapper)provider.CreateWrapper(false, parameters); diff --git a/crypto/src/crypto/operators/Asn1Signature.cs b/crypto/src/crypto/operators/Asn1Signature.cs index e079bf8ef..674e717b1 100644 --- a/crypto/src/crypto/operators/Asn1Signature.cs +++ b/crypto/src/crypto/operators/Asn1Signature.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using Org.BouncyCastle.Asn1; @@ -11,87 +10,88 @@ using Org.BouncyCastle.Asn1.TeleTrust; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Crypto.Operators { internal class X509Utilities { - private static readonly IDictionary algorithms = Platform.CreateHashtable(); - private static readonly IDictionary exParams = Platform.CreateHashtable(); - private static readonly HashSet<DerObjectIdentifier> noParams = new HashSet<DerObjectIdentifier>(); + private static readonly IDictionary<string, DerObjectIdentifier> m_algorithms = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); + private static readonly IDictionary<string, Asn1Encodable> m_exParams = + new Dictionary<string, Asn1Encodable>(StringComparer.OrdinalIgnoreCase); + private static readonly HashSet<DerObjectIdentifier> noParams = new HashSet<DerObjectIdentifier>(); static X509Utilities() { - algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption); - algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption); - algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption); - algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption); - algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); - algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); - algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); - algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); - algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); - algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); - algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); - algorithms.Add("SHA3-224WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224); - algorithms.Add("SHA3-256WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256); - algorithms.Add("SHA3-384WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384); - algorithms.Add("SHA3-512WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512); - algorithms.Add("SHA3-224WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224); - algorithms.Add("SHA3-256WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256); - algorithms.Add("SHA3-384WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384); - algorithms.Add("SHA3-512WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512); - algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); - algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); - algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); - algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); - algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); - algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); - algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); - algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); - algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); - algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); - algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); - algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1); - algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1); - algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224); - algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256); - algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384); - algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512); - algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1); - algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1); - algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224); - algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256); - algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384); - algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512); - algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); - algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); - algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001); - algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001); - algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001); + m_algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption); + m_algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption); + m_algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption); + m_algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption); + m_algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + m_algorithms.Add("SHA-1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + m_algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + m_algorithms.Add("SHA-1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption); + m_algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + m_algorithms.Add("SHA-224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + m_algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + m_algorithms.Add("SHA-224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption); + m_algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + m_algorithms.Add("SHA-256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + m_algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + m_algorithms.Add("SHA-256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption); + m_algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + m_algorithms.Add("SHA-384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + m_algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + m_algorithms.Add("SHA-384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption); + m_algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + m_algorithms.Add("SHA-512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + m_algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + m_algorithms.Add("SHA-512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption); + m_algorithms.Add("SHA512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + m_algorithms.Add("SHA-512(224)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + m_algorithms.Add("SHA512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + m_algorithms.Add("SHA-512(224)WITHRSA", PkcsObjectIdentifiers.Sha512_224WithRSAEncryption); + m_algorithms.Add("SHA512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + m_algorithms.Add("SHA-512(256)WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + m_algorithms.Add("SHA512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + m_algorithms.Add("SHA-512(256)WITHRSA", PkcsObjectIdentifiers.Sha512_256WithRSAEncryption); + m_algorithms.Add("SHA3-224WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224); + m_algorithms.Add("SHA3-256WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256); + m_algorithms.Add("SHA3-384WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384); + m_algorithms.Add("SHA3-512WITHRSAENCRYPTION", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512); + m_algorithms.Add("SHA3-224WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224); + m_algorithms.Add("SHA3-256WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256); + m_algorithms.Add("SHA3-384WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384); + m_algorithms.Add("SHA3-512WITHRSA", NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512); + m_algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); + m_algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); + m_algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); + m_algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); + m_algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss); + m_algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); + m_algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160); + m_algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); + m_algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128); + m_algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); + m_algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256); + m_algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1); + m_algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1); + m_algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224); + m_algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256); + m_algorithms.Add("SHA384WITHDSA", NistObjectIdentifiers.DsaWithSha384); + m_algorithms.Add("SHA512WITHDSA", NistObjectIdentifiers.DsaWithSha512); + m_algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1); + m_algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1); + m_algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224); + m_algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256); + m_algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384); + m_algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512); + m_algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); + m_algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94); + m_algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001); + m_algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001); + m_algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001); // // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field. @@ -119,19 +119,19 @@ namespace Org.BouncyCastle.Crypto.Operators // explicit params // AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance); - exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20)); + m_exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20)); AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance); - exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28)); + m_exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28)); AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance); - exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32)); + m_exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32)); AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance); - exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48)); + m_exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48)); AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance); - exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64)); + m_exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64)); } /** @@ -229,41 +229,28 @@ namespace Org.BouncyCastle.Crypto.Operators new DerInteger(1)); } - internal static DerObjectIdentifier GetAlgorithmOid( - string algorithmName) + internal static DerObjectIdentifier GetAlgorithmOid(string algorithmName) { - algorithmName = Platform.ToUpperInvariant(algorithmName); - - if (algorithms.Contains(algorithmName)) - { - return (DerObjectIdentifier) algorithms[algorithmName]; - } + if (m_algorithms.TryGetValue(algorithmName, out var oid)) + return oid; return new DerObjectIdentifier(algorithmName); } - internal static AlgorithmIdentifier GetSigAlgID( - DerObjectIdentifier sigOid, - string algorithmName) + internal static AlgorithmIdentifier GetSigAlgID(DerObjectIdentifier sigOid, string algorithmName) { if (noParams.Contains(sigOid)) - { return new AlgorithmIdentifier(sigOid); - } - - algorithmName = Platform.ToUpperInvariant(algorithmName); - if (exParams.Contains(algorithmName)) - { - return new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]); - } + if (m_exParams.TryGetValue(algorithmName, out var explicitParameters)) + return new AlgorithmIdentifier(sigOid, explicitParameters); return new AlgorithmIdentifier(sigOid, DerNull.Instance); } - internal static IEnumerable GetAlgNames() + internal static IEnumerable<string> GetAlgNames() { - return new EnumerableProxy(algorithms.Keys); + return CollectionUtilities.Proxy(m_algorithms.Keys); } } @@ -329,7 +316,7 @@ namespace Org.BouncyCastle.Crypto.Operators /// <summary> /// Allows enumeration of the signature names supported by the verifier provider. /// </summary> - public static IEnumerable SignatureAlgNames + public static IEnumerable<string> SignatureAlgNames { get { return X509Utilities.GetAlgNames(); } } @@ -409,7 +396,7 @@ namespace Org.BouncyCastle.Crypto.Operators /// <summary> /// Allows enumeration of the signature names supported by the verifier provider. /// </summary> - public IEnumerable SignatureAlgNames + public IEnumerable<string> SignatureAlgNames { get { return X509Utilities.GetAlgNames(); } } diff --git a/crypto/src/crypto/operators/CmsContentEncryptorBuilder.cs b/crypto/src/crypto/operators/CmsContentEncryptorBuilder.cs index 690e970cb..1dd9edf1c 100644 --- a/crypto/src/crypto/operators/CmsContentEncryptorBuilder.cs +++ b/crypto/src/crypto/operators/CmsContentEncryptorBuilder.cs @@ -1,16 +1,10 @@ using System; -using System.Collections; -using System.IO; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Ntt; -using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Cms; -using Org.BouncyCastle.Crypto.IO; -using Org.BouncyCastle.Crypto.Parameters; -using Org.BouncyCastle.Security; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Operators; @@ -18,7 +12,8 @@ namespace Org.BouncyCastle.Operators { public class CmsContentEncryptorBuilder { - private static readonly IDictionary KeySizes = Platform.CreateHashtable(); + private static readonly IDictionary<DerObjectIdentifier, int> KeySizes = + new Dictionary<DerObjectIdentifier, int>(); static CmsContentEncryptorBuilder() { @@ -33,12 +28,7 @@ namespace Org.BouncyCastle.Operators private static int GetKeySize(DerObjectIdentifier oid) { - if (KeySizes.Contains(oid)) - { - return (int)KeySizes[oid]; - } - - return -1; + return KeySizes.TryGetValue(oid, out var keySize) ? keySize : -1; } private readonly DerObjectIdentifier encryptionOID; diff --git a/crypto/src/crypto/parameters/ECKeyParameters.cs b/crypto/src/crypto/parameters/ECKeyParameters.cs index 49bd4fe31..07165ead1 100644 --- a/crypto/src/crypto/parameters/ECKeyParameters.cs +++ b/crypto/src/crypto/parameters/ECKeyParameters.cs @@ -1,13 +1,10 @@ using System; -using System.Collections; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Crypto.Parameters { diff --git a/crypto/src/crypto/parameters/NaccacheSternPrivateKeyParameters.cs b/crypto/src/crypto/parameters/NaccacheSternPrivateKeyParameters.cs index 5b0917bd2..0444f3702 100644 --- a/crypto/src/crypto/parameters/NaccacheSternPrivateKeyParameters.cs +++ b/crypto/src/crypto/parameters/NaccacheSternPrivateKeyParameters.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Math; @@ -14,7 +14,7 @@ namespace Org.BouncyCastle.Crypto.Parameters public class NaccacheSternPrivateKeyParameters : NaccacheSternKeyParameters { private readonly BigInteger phiN; - private readonly IList smallPrimes; + private readonly IList<BigInteger> smallPrimes; /** * Constructs a NaccacheSternPrivateKey @@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Crypto.Parameters BigInteger g, BigInteger n, int lowerSigmaBound, - IList smallPrimes, + IList<BigInteger> smallPrimes, BigInteger phiN) : base(true, g, n, lowerSigmaBound) { @@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Crypto.Parameters get { return phiN; } } - public IList SmallPrimesList + public IList<BigInteger> SmallPrimesList { get { return smallPrimes; } } diff --git a/crypto/src/crypto/parameters/SkeinParameters.cs b/crypto/src/crypto/parameters/SkeinParameters.cs index cc57ef5ff..f32ce0532 100644 --- a/crypto/src/crypto/parameters/SkeinParameters.cs +++ b/crypto/src/crypto/parameters/SkeinParameters.cs @@ -1,9 +1,10 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.Globalization; using System.IO; using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Crypto.Parameters { @@ -71,25 +72,24 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public const int PARAM_TYPE_OUTPUT = 63; - private IDictionary parameters; + private IDictionary<int, byte[]> m_parameters; public SkeinParameters() - : this(Platform.CreateHashtable()) - + : this(new Dictionary<int, byte[]>()) { } - private SkeinParameters(IDictionary parameters) + private SkeinParameters(IDictionary<int, byte[]> parameters) { - this.parameters = parameters; + this.m_parameters = parameters; } /// <summary> /// Obtains a map of type (int) to value (byte[]) for the parameters tracked in this object. /// </summary> - public IDictionary GetParameters() + public IDictionary<int, byte[]> GetParameters() { - return parameters; + return m_parameters; } /// <summary> @@ -99,7 +99,7 @@ namespace Org.BouncyCastle.Crypto.Parameters /// <returns>The key.</returns> public byte[] GetKey() { - return (byte[])parameters[PARAM_TYPE_KEY]; + return CollectionUtilities.GetValueOrNull(m_parameters, PARAM_TYPE_KEY); } /// <summary> @@ -108,7 +108,7 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public byte[] GetPersonalisation() { - return (byte[])parameters[PARAM_TYPE_PERSONALISATION]; + return CollectionUtilities.GetValueOrNull(m_parameters, PARAM_TYPE_PERSONALISATION); } /// <summary> @@ -117,7 +117,7 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public byte[] GetPublicKey() { - return (byte[])parameters[PARAM_TYPE_PUBLIC_KEY]; + return CollectionUtilities.GetValueOrNull(m_parameters, PARAM_TYPE_PUBLIC_KEY); } /// <summary> @@ -126,7 +126,7 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public byte[] GetKeyIdentifier() { - return (byte[])parameters[PARAM_TYPE_KEY_IDENTIFIER]; + return CollectionUtilities.GetValueOrNull(m_parameters, PARAM_TYPE_KEY_IDENTIFIER); } /// <summary> @@ -135,7 +135,7 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public byte[] GetNonce() { - return (byte[])parameters[PARAM_TYPE_NONCE]; + return CollectionUtilities.GetValueOrNull(m_parameters, PARAM_TYPE_NONCE); } /// <summary> @@ -143,30 +143,21 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public class Builder { - private IDictionary parameters = Platform.CreateHashtable(); + private Dictionary<int, byte[]> m_parameters; public Builder() { + m_parameters = new Dictionary<int, byte[]>(); } - public Builder(IDictionary paramsMap) + public Builder(IDictionary<int, byte[]> paramsMap) { - IEnumerator keys = paramsMap.Keys.GetEnumerator(); - while (keys.MoveNext()) - { - int key = (int)keys.Current; - parameters.Add(key, paramsMap[key]); - } + m_parameters = new Dictionary<int, byte[]>(paramsMap); } public Builder(SkeinParameters parameters) + : this(parameters.m_parameters) { - IEnumerator keys = parameters.parameters.Keys.GetEnumerator(); - while (keys.MoveNext()) - { - int key = (int)keys.Current; - this.parameters.Add(key, parameters.parameters[key]); - } } /// <summary> @@ -198,7 +189,7 @@ namespace Org.BouncyCastle.Crypto.Parameters throw new ArgumentException("Parameter type " + PARAM_TYPE_CONFIG + " is reserved for internal use."); } - this.parameters.Add(type, value); + m_parameters.Add(type, value); return this; } @@ -279,8 +270,8 @@ namespace Org.BouncyCastle.Crypto.Parameters /// </summary> public SkeinParameters Build() { - return new SkeinParameters(parameters); + return new SkeinParameters(m_parameters); } } } -} \ No newline at end of file +} diff --git a/crypto/src/crypto/prng/drbg/DrbgUtilities.cs b/crypto/src/crypto/prng/drbg/DrbgUtilities.cs index d9a1c439c..b1f2f29be 100644 --- a/crypto/src/crypto/prng/drbg/DrbgUtilities.cs +++ b/crypto/src/crypto/prng/drbg/DrbgUtilities.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Utilities; @@ -7,31 +7,32 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg { internal class DrbgUtilities { - private static readonly IDictionary maxSecurityStrengths = Platform.CreateHashtable(); + private static readonly IDictionary<string, int> MaxSecurityStrengths = + new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); static DrbgUtilities() { - maxSecurityStrengths.Add("SHA-1", 128); + MaxSecurityStrengths.Add("SHA-1", 128); - maxSecurityStrengths.Add("SHA-224", 192); - maxSecurityStrengths.Add("SHA-256", 256); - maxSecurityStrengths.Add("SHA-384", 256); - maxSecurityStrengths.Add("SHA-512", 256); + MaxSecurityStrengths.Add("SHA-224", 192); + MaxSecurityStrengths.Add("SHA-256", 256); + MaxSecurityStrengths.Add("SHA-384", 256); + MaxSecurityStrengths.Add("SHA-512", 256); - maxSecurityStrengths.Add("SHA-512/224", 192); - maxSecurityStrengths.Add("SHA-512/256", 256); + MaxSecurityStrengths.Add("SHA-512/224", 192); + MaxSecurityStrengths.Add("SHA-512/256", 256); } internal static int GetMaxSecurityStrength(IDigest d) { - return (int)maxSecurityStrengths[d.AlgorithmName]; + return MaxSecurityStrengths[d.AlgorithmName]; } internal static int GetMaxSecurityStrength(IMac m) { string name = m.AlgorithmName; - return (int)maxSecurityStrengths[name.Substring(0, name.IndexOf("/"))]; + return MaxSecurityStrengths[name.Substring(0, name.IndexOf("/"))]; } /** diff --git a/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs b/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs index 493da5a75..506517aae 100644 --- a/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs +++ b/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Utilities; @@ -16,17 +16,18 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg private readonly static long RESEED_MAX = 1L << (48 - 1); private readonly static int MAX_BITS_REQUEST = 1 << (19 - 1); - private static readonly IDictionary seedlens = Platform.CreateHashtable(); + private static readonly IDictionary<string, int> SeedLens = + new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); static HashSP800Drbg() { - seedlens.Add("SHA-1", 440); - seedlens.Add("SHA-224", 440); - seedlens.Add("SHA-256", 440); - seedlens.Add("SHA-512/256", 440); - seedlens.Add("SHA-512/224", 440); - seedlens.Add("SHA-384", 888); - seedlens.Add("SHA-512", 888); + SeedLens.Add("SHA-1", 440); + SeedLens.Add("SHA-224", 440); + SeedLens.Add("SHA-256", 440); + SeedLens.Add("SHA-512/256", 440); + SeedLens.Add("SHA-512/224", 440); + SeedLens.Add("SHA-384", 888); + SeedLens.Add("SHA-512", 888); } private readonly IDigest mDigest; @@ -59,7 +60,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg mDigest = digest; mEntropySource = entropySource; mSecurityStrength = securityStrength; - mSeedLength = (int)seedlens[digest.AlgorithmName]; + mSeedLength = SeedLens[digest.AlgorithmName]; // 1. seed_material = entropy_input || nonce || personalization_string. // 2. seed = Hash_df (seed_material, seedlen). diff --git a/crypto/src/crypto/signers/GOST3410DigestSigner.cs b/crypto/src/crypto/signers/GOST3410DigestSigner.cs index bff35869b..81a5ff1cc 100644 --- a/crypto/src/crypto/signers/GOST3410DigestSigner.cs +++ b/crypto/src/crypto/signers/GOST3410DigestSigner.cs @@ -1,10 +1,5 @@ using System; -using System.Collections; -using System.IO; -using System.Text; -using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Crypto.Signers; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; diff --git a/crypto/src/crypto/signers/Iso9796d2PssSigner.cs b/crypto/src/crypto/signers/Iso9796d2PssSigner.cs index 9a2ec4fa9..ad2718280 100644 --- a/crypto/src/crypto/signers/Iso9796d2PssSigner.cs +++ b/crypto/src/crypto/signers/Iso9796d2PssSigner.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; -using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; diff --git a/crypto/src/crypto/signers/Iso9796d2Signer.cs b/crypto/src/crypto/signers/Iso9796d2Signer.cs index fc3c275c5..f28c4ac71 100644 --- a/crypto/src/crypto/signers/Iso9796d2Signer.cs +++ b/crypto/src/crypto/signers/Iso9796d2Signer.cs @@ -1,8 +1,5 @@ using System; -using System.Collections; -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; diff --git a/crypto/src/crypto/signers/IsoTrailers.cs b/crypto/src/crypto/signers/IsoTrailers.cs index 497ffaf78..61006b848 100644 --- a/crypto/src/crypto/signers/IsoTrailers.cs +++ b/crypto/src/crypto/signers/IsoTrailers.cs @@ -1,9 +1,5 @@ using System; -using System.Collections; - -using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; +using System.Collections.Generic; namespace Org.BouncyCastle.Crypto.Signers { @@ -21,9 +17,9 @@ namespace Org.BouncyCastle.Crypto.Signers public const int TRAILER_SHA512_224 = 0x39CC; public const int TRAILER_SHA512_256 = 0x40CC; - private static IDictionary CreateTrailerMap() + private static IDictionary<string, int> CreateTrailerMap() { - IDictionary trailers = Platform.CreateHashtable(); + var trailers = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); trailers.Add("RIPEMD128", TRAILER_RIPEMD128); trailers.Add("RIPEMD160", TRAILER_RIPEMD160); @@ -38,20 +34,20 @@ namespace Org.BouncyCastle.Crypto.Signers trailers.Add("Whirlpool", TRAILER_WHIRLPOOL); - return CollectionUtilities.ReadOnly(trailers); + return trailers; } // IDictionary is (string -> Int32) - private static readonly IDictionary trailerMap = CreateTrailerMap(); + private static readonly IDictionary<string, int> TrailerMap = CreateTrailerMap(); public static int GetTrailer(IDigest digest) { - return (int)trailerMap[digest.AlgorithmName]; + return TrailerMap[digest.AlgorithmName]; } public static bool NoTrailerAvailable(IDigest digest) { - return !trailerMap.Contains(digest.AlgorithmName); + return !TrailerMap.ContainsKey(digest.AlgorithmName); } } } diff --git a/crypto/src/crypto/signers/RsaDigestSigner.cs b/crypto/src/crypto/signers/RsaDigestSigner.cs index ce6bcb2d6..25bd4af4e 100644 --- a/crypto/src/crypto/signers/RsaDigestSigner.cs +++ b/crypto/src/crypto/signers/RsaDigestSigner.cs @@ -1,20 +1,17 @@ using System; -using System.Collections; -using System.IO; -using System.Text; +using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.TeleTrust; -using Org.BouncyCastle.Asn1.Utilities; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Encodings; using Org.BouncyCastle.Crypto.Engines; -using Org.BouncyCastle.Crypto.Signers; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Crypto.Signers { @@ -26,36 +23,37 @@ namespace Org.BouncyCastle.Crypto.Signers private readonly IDigest digest; private bool forSigning; - private static readonly IDictionary oidMap = Platform.CreateHashtable(); + private static readonly IDictionary<string, DerObjectIdentifier> OidMap = + new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase); /// <summary> /// Load oid table. /// </summary> static RsaDigestSigner() { - oidMap["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128; - oidMap["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160; - oidMap["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256; - - oidMap["SHA-1"] = X509ObjectIdentifiers.IdSha1; - oidMap["SHA-224"] = NistObjectIdentifiers.IdSha224; - oidMap["SHA-256"] = NistObjectIdentifiers.IdSha256; - oidMap["SHA-384"] = NistObjectIdentifiers.IdSha384; - oidMap["SHA-512"] = NistObjectIdentifiers.IdSha512; - oidMap["SHA-512/224"] = NistObjectIdentifiers.IdSha512_224; - oidMap["SHA-512/256"] = NistObjectIdentifiers.IdSha512_256; - oidMap["SHA3-224"] = NistObjectIdentifiers.IdSha3_224; - oidMap["SHA3-256"] = NistObjectIdentifiers.IdSha3_256; - oidMap["SHA3-384"] = NistObjectIdentifiers.IdSha3_384; - oidMap["SHA3-512"] = NistObjectIdentifiers.IdSha3_512; - - oidMap["MD2"] = PkcsObjectIdentifiers.MD2; - oidMap["MD4"] = PkcsObjectIdentifiers.MD4; - oidMap["MD5"] = PkcsObjectIdentifiers.MD5; + OidMap["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128; + OidMap["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160; + OidMap["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256; + + OidMap["SHA-1"] = X509ObjectIdentifiers.IdSha1; + OidMap["SHA-224"] = NistObjectIdentifiers.IdSha224; + OidMap["SHA-256"] = NistObjectIdentifiers.IdSha256; + OidMap["SHA-384"] = NistObjectIdentifiers.IdSha384; + OidMap["SHA-512"] = NistObjectIdentifiers.IdSha512; + OidMap["SHA-512/224"] = NistObjectIdentifiers.IdSha512_224; + OidMap["SHA-512/256"] = NistObjectIdentifiers.IdSha512_256; + OidMap["SHA3-224"] = NistObjectIdentifiers.IdSha3_224; + OidMap["SHA3-256"] = NistObjectIdentifiers.IdSha3_256; + OidMap["SHA3-384"] = NistObjectIdentifiers.IdSha3_384; + OidMap["SHA3-512"] = NistObjectIdentifiers.IdSha3_512; + + OidMap["MD2"] = PkcsObjectIdentifiers.MD2; + OidMap["MD4"] = PkcsObjectIdentifiers.MD4; + OidMap["MD5"] = PkcsObjectIdentifiers.MD5; } public RsaDigestSigner(IDigest digest) - : this(digest, (DerObjectIdentifier)oidMap[digest.AlgorithmName]) + : this(digest, CollectionUtilities.GetValueOrNull(OidMap, digest.AlgorithmName)) { } diff --git a/crypto/src/crypto/signers/X931Signer.cs b/crypto/src/crypto/signers/X931Signer.cs index 411d3b9e8..0907403a8 100644 --- a/crypto/src/crypto/signers/X931Signer.cs +++ b/crypto/src/crypto/signers/X931Signer.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; diff --git a/crypto/src/crypto/util/BasicAlphabetMapper.cs b/crypto/src/crypto/util/BasicAlphabetMapper.cs index b60733fc0..79e93fa5c 100644 --- a/crypto/src/crypto/util/BasicAlphabetMapper.cs +++ b/crypto/src/crypto/util/BasicAlphabetMapper.cs @@ -1,7 +1,5 @@ using System; -using System.Collections; - -using Org.BouncyCastle.Utilities; +using System.Collections.Generic; namespace Org.BouncyCastle.Crypto.Utilities { @@ -12,8 +10,8 @@ namespace Org.BouncyCastle.Crypto.Utilities public class BasicAlphabetMapper : IAlphabetMapper { - private readonly IDictionary indexMap = Platform.CreateHashtable(); - private readonly IDictionary charMap = Platform.CreateHashtable(); + private readonly IDictionary<char, int> m_indexMap = new Dictionary<char, int>(); + private readonly IList<char> m_charMap = new List<char>(); /** * Base constructor. @@ -34,30 +32,32 @@ namespace Org.BouncyCastle.Crypto.Utilities { for (int i = 0; i != alphabet.Length; i++) { - if (indexMap.Contains(alphabet[i])) - { + if (m_indexMap.ContainsKey(alphabet[i])) throw new ArgumentException("duplicate key detected in alphabet: " + alphabet[i]); - } - indexMap.Add(alphabet[i], i); - charMap.Add(i, alphabet[i]); + + m_indexMap.Add(alphabet[i], i); + m_charMap.Add(alphabet[i]); } } public int Radix { - get { return indexMap.Count; } + get { return m_charMap.Count; } } public byte[] ConvertToIndexes(char[] input) { byte[] outBuf; - if (indexMap.Count <= 256) + if (m_charMap.Count <= 256) { outBuf = new byte[input.Length]; for (int i = 0; i != input.Length; i++) { - outBuf[i] = (byte)(int)indexMap[input[i]]; + if (!m_indexMap.TryGetValue(input[i], out var idx)) + throw new InvalidOperationException(); + + outBuf[i] = (byte)idx; } } else @@ -65,9 +65,11 @@ namespace Org.BouncyCastle.Crypto.Utilities outBuf = new byte[input.Length * 2]; for (int i = 0; i != input.Length; i++) { - int idx = (int)indexMap[input[i]]; - outBuf[i * 2] = (byte)((idx >> 8) & 0xff); - outBuf[i * 2 + 1] = (byte)(idx & 0xff); + if (!m_indexMap.TryGetValue(input[i], out var idx)) + throw new InvalidOperationException(); + + outBuf[i * 2 + 0] = (byte)(idx >> 8); + outBuf[i * 2 + 1] = (byte)idx; } } @@ -78,12 +80,12 @@ namespace Org.BouncyCastle.Crypto.Utilities { char[] outBuf; - if (charMap.Count <= 256) + if (m_charMap.Count <= 256) { outBuf = new char[input.Length]; for (int i = 0; i != input.Length; i++) { - outBuf[i] = (char)charMap[input[i] & 0xff]; + outBuf[i] = m_charMap[input[i]]; } } else @@ -96,7 +98,7 @@ namespace Org.BouncyCastle.Crypto.Utilities outBuf = new char[input.Length / 2]; for (int i = 0; i != input.Length; i += 2) { - outBuf[i / 2] = (char)charMap[((input[i] << 8) & 0xff00) | (input[i + 1] & 0xff)]; + outBuf[i / 2] = m_charMap[(input[i] << 8) | input[i + 1]]; } } diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs index c6e760db9..98d1fcb1d 100644 --- a/crypto/src/math/BigInteger.cs +++ b/crypto/src/math/BigInteger.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Text; @@ -3258,7 +3258,7 @@ namespace Org.BouncyCastle.Math int mask = (1 << 30) - 1; BigInteger u = this.Abs(); int bits = u.BitLength; - IList S = Platform.CreateArrayList(); + var S = new List<string>(); while (bits > 30) { S.Add(Convert.ToString(u.IntValue & mask, 8)); @@ -3268,7 +3268,7 @@ namespace Org.BouncyCastle.Math sb.Append(Convert.ToString(u.IntValue, 8)); for (int i = S.Count - 1; i >= 0; --i) { - AppendZeroExtendedString(sb, (string)S[i], 10); + AppendZeroExtendedString(sb, S[i], 10); } break; } @@ -3294,8 +3294,8 @@ namespace Org.BouncyCastle.Math } // TODO Could cache the moduli for each radix (soft reference?) - IList moduli = Platform.CreateArrayList(); - BigInteger R = BigInteger.ValueOf(radix); + var moduli = new List<BigInteger>(); + BigInteger R = ValueOf(radix); while (R.CompareTo(q) <= 0) { moduli.Add(R); @@ -3314,7 +3314,7 @@ namespace Org.BouncyCastle.Math return sb.ToString(); } - private static void ToString(StringBuilder sb, int radix, IList moduli, int scale, BigInteger pos) + private static void ToString(StringBuilder sb, int radix, IList<BigInteger> moduli, int scale, BigInteger pos) { if (pos.BitLength < 64) { @@ -3330,7 +3330,7 @@ namespace Org.BouncyCastle.Math return; } - BigInteger[] qr = pos.DivideAndRemainder((BigInteger)moduli[--scale]); + BigInteger[] qr = pos.DivideAndRemainder(moduli[--scale]); ToString(sb, radix, moduli, scale, qr[0]); ToString(sb, radix, moduli, scale, qr[1]); diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs index 838e407e3..8b078c2a8 100644 --- a/crypto/src/math/ec/ECCurve.cs +++ b/crypto/src/math/ec/ECCurve.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Math.EC.Abc; using Org.BouncyCastle.Math.EC.Endo; @@ -148,7 +148,7 @@ namespace Org.BouncyCastle.Math.EC { CheckPoint(point); - IDictionary table; + IDictionary<string, PreCompInfo> table; lock (point) { table = point.m_preCompTable; @@ -159,7 +159,7 @@ namespace Org.BouncyCastle.Math.EC lock (table) { - return (PreCompInfo)table[name]; + return table.TryGetValue(name, out var preCompInfo) ? preCompInfo : null; } } @@ -179,19 +179,19 @@ namespace Org.BouncyCastle.Math.EC { CheckPoint(point); - IDictionary table; + IDictionary<string, PreCompInfo> table; lock (point) { table = point.m_preCompTable; if (null == table) { - point.m_preCompTable = table = Platform.CreateHashtable(4); + point.m_preCompTable = table = new Dictionary<string, PreCompInfo>(); } } lock (table) { - PreCompInfo existing = (PreCompInfo)table[name]; + PreCompInfo existing = table.TryGetValue(name, out var preCompInfo) ? preCompInfo : null; PreCompInfo result = callback.Precompute(existing); if (result != existing) @@ -659,7 +659,7 @@ namespace Org.BouncyCastle.Math.EC { private const int FP_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED; - private static readonly IDictionary knownQs = Platform.CreateHashtable(); + private static readonly HashSet<BigInteger> KnownQs = new HashSet<BigInteger>(); private static readonly SecureRandom random = new SecureRandom(); protected readonly BigInteger m_q, m_r; @@ -679,38 +679,31 @@ namespace Org.BouncyCastle.Math.EC internal FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor, bool isInternal) : base(q) { - if (isInternal) + if (!isInternal) { - this.m_q = q; - if (!knownQs.Contains(q)) - { - knownQs.Add(q, q); - } - } - else if (knownQs.Contains(q)) - { - this.m_q = q; - } - else - { - int maxBitLength = AsInteger("Org.BouncyCastle.EC.Fp_MaxSize", 1042); // 2 * 521 - int certainty = AsInteger("Org.BouncyCastle.EC.Fp_Certainty", 100); + bool unknownQ; + lock (KnownQs) unknownQ = !KnownQs.Contains(q); - int qBitLength = q.BitLength; - if (maxBitLength < qBitLength) + if (unknownQ) { - throw new ArgumentException("Fp q value out of range"); - } + int maxBitLength = AsInteger("Org.BouncyCastle.EC.Fp_MaxSize", 1042); // 2 * 521 + int certainty = AsInteger("Org.BouncyCastle.EC.Fp_Certainty", 100); - if (Primes.HasAnySmallFactors(q) || !Primes.IsMRProbablePrime( - q, random, GetNumberOfIterations(qBitLength, certainty))) - { - throw new ArgumentException("Fp q value not prime"); - } + int qBitLength = q.BitLength; + if (maxBitLength < qBitLength) + throw new ArgumentException("Fp q value out of range"); - this.m_q = q; + if (Primes.HasAnySmallFactors(q) || + !Primes.IsMRProbablePrime(q, random, GetNumberOfIterations(qBitLength, certainty))) + { + throw new ArgumentException("Fp q value not prime"); + } + } } + lock (KnownQs) KnownQs.Add(q); + this.m_q = q; + this.m_r = FpFieldElement.CalculateResidue(q); this.m_infinity = new FpPoint(this, null, null); diff --git a/crypto/src/math/ec/ECPoint.cs b/crypto/src/math/ec/ECPoint.cs index f32376455..dcda5abfc 100644 --- a/crypto/src/math/ec/ECPoint.cs +++ b/crypto/src/math/ec/ECPoint.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.Text; using Org.BouncyCastle.Math.EC.Multiplier; @@ -51,8 +51,7 @@ namespace Org.BouncyCastle.Math.EC protected internal readonly ECFieldElement m_x, m_y; protected internal readonly ECFieldElement[] m_zs; - // Dictionary is (string -> PreCompInfo) - protected internal IDictionary m_preCompTable = null; + protected internal IDictionary<string, PreCompInfo> m_preCompTable = null; protected ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y) : this(curve, x, y, GetInitialZCoords(curve)) diff --git a/crypto/src/openssl/MiscPemGenerator.cs b/crypto/src/openssl/MiscPemGenerator.cs index 3db299569..294cf0afb 100644 --- a/crypto/src/openssl/MiscPemGenerator.cs +++ b/crypto/src/openssl/MiscPemGenerator.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Asn1; @@ -195,11 +195,9 @@ namespace Org.BouncyCastle.OpenSsl byte[] encData = PemUtilities.Crypt(true, keyData, password, dekAlgName, iv); - IList headers = Platform.CreateArrayList(2); - + var headers = new List<PemHeader>(2); headers.Add(new PemHeader("Proc-Type", "4,ENCRYPTED")); - headers.Add(new PemHeader("DEK-Info", dekAlgName + "," - + Strings.ToUpperCase(Hex.ToHexString(iv)))); + headers.Add(new PemHeader("DEK-Info", dekAlgName + "," + Hex.ToHexString(iv).ToUpperInvariant())); return new PemObject(type, headers, encData); } diff --git a/crypto/src/util/Strings.cs b/crypto/src/util/Strings.cs index 1a94d2bff..1d0c94611 100644 --- a/crypto/src/util/Strings.cs +++ b/crypto/src/util/Strings.cs @@ -6,31 +6,6 @@ namespace Org.BouncyCastle.Utilities /// <summary> General string utilities.</summary> public abstract class Strings { - - public static string ToUpperCase(string original) - { - bool changed = false; - char[] chars = original.ToCharArray(); - - for (int i = 0; i != chars.Length; i++) - { - char ch = chars[i]; - if ('a' <= ch && 'z' >= ch) - { - changed = true; - chars[i] = (char)(ch - 'a' + 'A'); - } - } - - if (changed) - { - return new string(chars); - } - - return original; - } - - internal static bool IsOneOf(string s, params string[] candidates) { foreach (string candidate in candidates) @@ -41,8 +16,7 @@ namespace Org.BouncyCastle.Utilities return false; } - public static string FromByteArray( - byte[] bs) + public static string FromByteArray(byte[] bs) { char[] cs = new char[bs.Length]; for (int i = 0; i < cs.Length; ++i) @@ -52,8 +26,7 @@ namespace Org.BouncyCastle.Utilities return new string(cs); } - public static byte[] ToByteArray( - char[] cs) + public static byte[] ToByteArray(char[] cs) { byte[] bs = new byte[cs.Length]; for (int i = 0; i < bs.Length; ++i) @@ -63,8 +36,7 @@ namespace Org.BouncyCastle.Utilities return bs; } - public static byte[] ToByteArray( - string s) + public static byte[] ToByteArray(string s) { byte[] bs = new byte[s.Length]; for (int i = 0; i < bs.Length; ++i) @@ -74,53 +46,32 @@ namespace Org.BouncyCastle.Utilities return bs; } - public static string FromAsciiByteArray( - byte[] bytes) + public static string FromAsciiByteArray(byte[] bytes) { -#if PORTABLE - // TODO Check for non-ASCII bytes in input? - return Encoding.UTF8.GetString(bytes, 0, bytes.Length); -#else return Encoding.ASCII.GetString(bytes, 0, bytes.Length); -#endif } - public static byte[] ToAsciiByteArray( - char[] cs) + public static byte[] ToAsciiByteArray(char[] cs) { -#if PORTABLE - // TODO Check for non-ASCII characters in input? - return Encoding.UTF8.GetBytes(cs); -#else return Encoding.ASCII.GetBytes(cs); -#endif } - public static byte[] ToAsciiByteArray( - string s) + public static byte[] ToAsciiByteArray(string s) { -#if PORTABLE - // TODO Check for non-ASCII characters in input? - return Encoding.UTF8.GetBytes(s); -#else return Encoding.ASCII.GetBytes(s); -#endif } - public static string FromUtf8ByteArray( - byte[] bytes) + public static string FromUtf8ByteArray(byte[] bytes) { return Encoding.UTF8.GetString(bytes, 0, bytes.Length); } - public static byte[] ToUtf8ByteArray( - char[] cs) + public static byte[] ToUtf8ByteArray(char[] cs) { return Encoding.UTF8.GetBytes(cs); } - public static byte[] ToUtf8ByteArray( - string s) + public static byte[] ToUtf8ByteArray(string s) { return Encoding.UTF8.GetBytes(s); } |