diff options
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/src/crypto/modes/SicBlockCipher.cs | 10 | ||||
-rw-r--r-- | crypto/src/math/BigInteger.cs | 8 | ||||
-rw-r--r-- | crypto/src/math/ec/ECCurve.cs | 3 | ||||
-rw-r--r-- | crypto/src/math/ec/ECFieldElement.cs | 3 | ||||
-rw-r--r-- | crypto/src/math/raw/Mod.cs | 6 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/GOST3411DigestTest.cs | 22 |
6 files changed, 38 insertions, 14 deletions
diff --git a/crypto/src/crypto/modes/SicBlockCipher.cs b/crypto/src/crypto/modes/SicBlockCipher.cs index 17f86ee10..239f99478 100644 --- a/crypto/src/crypto/modes/SicBlockCipher.cs +++ b/crypto/src/crypto/modes/SicBlockCipher.cs @@ -50,12 +50,14 @@ namespace Org.BouncyCastle.Crypto.Modes { ParametersWithIV ivParam = parameters as ParametersWithIV; if (ivParam == null) - throw new ArgumentException("CTR mode requires ParametersWithIV", "parameters"); + throw new ArgumentException("CTR/SIC mode requires ParametersWithIV", "parameters"); this.IV = Arrays.Clone(ivParam.GetIV()); + if (blockSize < IV.Length) + throw new ArgumentException("CTR/SIC mode requires IV no greater than: " + blockSize + " bytes."); if (blockSize - IV.Length > 8) - throw new ArgumentException("CTR mode requires IV of at least: " + (blockSize - 8) + " bytes."); + throw new ArgumentException("CTR/SIC mode requires IV of at least: " + (blockSize - 8) + " bytes."); Reset(); @@ -68,7 +70,7 @@ namespace Org.BouncyCastle.Crypto.Modes public virtual string AlgorithmName { - get { return cipher.AlgorithmName + "/CTR"; } + get { return cipher.AlgorithmName + "/SIC"; } } public virtual bool IsPartialBlockOkay @@ -109,7 +111,7 @@ namespace Org.BouncyCastle.Crypto.Modes public virtual void Reset() { Arrays.Fill(counter, (byte)0); - Array.Copy(IV, 0, counter, 0, System.Math.Min(IV.Length, counter.Length)); + Array.Copy(IV, 0, counter, 0, IV.Length); cipher.Reset(); } } diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs index f302f077e..f31e2d5f2 100644 --- a/crypto/src/math/BigInteger.cs +++ b/crypto/src/math/BigInteger.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Globalization; using System.Text; +using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Math @@ -179,7 +180,7 @@ namespace Org.BouncyCastle.Math private const int chunk2 = 1, chunk8 = 1, chunk10 = 19, chunk16 = 16; private static readonly BigInteger radix2, radix2E, radix8, radix8E, radix10, radix10E, radix16, radix16E; - private static readonly Random RandomSource = new Random(); + private static readonly SecureRandom RandomSource = new SecureRandom(); /* * These are the threshold bit-lengths (of an exponent) where we increase the window size. @@ -246,6 +247,11 @@ namespace Org.BouncyCastle.Math return (nBits + BitsPerByte - 1) / BitsPerByte; } + internal static BigInteger Arbitrary(int sizeInBits) + { + return new BigInteger(sizeInBits, RandomSource); + } + private BigInteger( int signum, int[] mag, diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs index 40b46ce72..fa2c72570 100644 --- a/crypto/src/math/ec/ECCurve.cs +++ b/crypto/src/math/ec/ECCurve.cs @@ -760,10 +760,9 @@ namespace Org.BouncyCastle.Math.EC ECFieldElement gamma, z, zeroElement = FromBigInteger(BigInteger.Zero); int m = FieldSize; - Random rand = new Random(); do { - ECFieldElement t = FromBigInteger(new BigInteger(m, rand)); + ECFieldElement t = FromBigInteger(BigInteger.Arbitrary(m)); z = zeroElement; ECFieldElement w = beta; for (int i = 1; i < m; i++) diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs index 4d4fb3e4d..d0e008aab 100644 --- a/crypto/src/math/ec/ECFieldElement.cs +++ b/crypto/src/math/ec/ECFieldElement.cs @@ -306,13 +306,12 @@ namespace Org.BouncyCastle.Math.EC BigInteger k = legendreExponent.Add(BigInteger.One), qMinusOne = q.Subtract(BigInteger.One); BigInteger U, V; - Random rand = new Random(); do { BigInteger P; do { - P = new BigInteger(q.BitLength, rand); + P = BigInteger.Arbitrary(q.BitLength); } while (P.CompareTo(q) >= 0 || !ModReduce(P.Multiply(P).Subtract(fourX)).ModPow(legendreExponent, q).Equals(qMinusOne)); diff --git a/crypto/src/math/raw/Mod.cs b/crypto/src/math/raw/Mod.cs index 63467e668..8d9e8fd21 100644 --- a/crypto/src/math/raw/Mod.cs +++ b/crypto/src/math/raw/Mod.cs @@ -2,12 +2,15 @@ using System.Diagnostics; using Org.BouncyCastle.Crypto.Utilities; +using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Math.Raw { internal abstract class Mod { + private static readonly SecureRandom RandomSource = new SecureRandom(); + public static void Invert(uint[] p, uint[] x, uint[] z) { int len = p.Length; @@ -77,7 +80,6 @@ namespace Org.BouncyCastle.Math.Raw public static uint[] Random(uint[] p) { int len = p.Length; - Random rand = new Random(); uint[] s = Nat.Create(len); uint m = p[len - 1]; @@ -90,7 +92,7 @@ namespace Org.BouncyCastle.Math.Raw do { byte[] bytes = new byte[len << 2]; - rand.NextBytes(bytes); + RandomSource.NextBytes(bytes); Pack.BE_To_UInt32(bytes, 0, s); s[len - 1] &= m; } diff --git a/crypto/test/src/crypto/test/GOST3411DigestTest.cs b/crypto/test/src/crypto/test/GOST3411DigestTest.cs index 1826b28c9..329a158d6 100644 --- a/crypto/test/src/crypto/test/GOST3411DigestTest.cs +++ b/crypto/test/src/crypto/test/GOST3411DigestTest.cs @@ -4,7 +4,13 @@ using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; +using Org.BouncyCastle.Crypto.Generators; +using Org.BouncyCastle.Crypto.Macs; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests @@ -40,7 +46,7 @@ namespace Org.BouncyCastle.Crypto.Tests // }; // 1 million 'a' - static private string million_a_digest = "8693287aa62f9478f7cb312ec0866b6c4e4a0f11160441e8f4ffcd2715dd554f"; + static private string million_a_digest = "8693287aa62f9478f7cb312ec0866b6c4e4a0f11160441e8f4ffcd2715dd554f"; public Gost3411DigestTest() : base(new Gost3411Digest(), messages, digests) @@ -52,9 +58,19 @@ namespace Org.BouncyCastle.Crypto.Tests base.PerformTest(); millionATest(million_a_digest); - } - protected override IDigest CloneDigest(IDigest digest) + byte[] data = Strings.ToUtf8ByteArray("fred"); + + KeyParameter key = new KeyParameter(Pkcs5S1ParametersGenerator.Pkcs5PasswordToUtf8Bytes("1".ToCharArray())); + byte[] mac = MacUtilities.CalculateMac("HMAC/GOST3411", key, data); + + if (!Arrays.AreEqual(Hex.Decode("e9f98610cfc80084462b175a15d2b4ec10b2ab892eae5a6179d572d9b1db6b72"), mac)) + { + Fail("mac calculation failed."); + } + } + + protected override IDigest CloneDigest(IDigest digest) { return new Gost3411Digest((Gost3411Digest)digest); } |