From b7039a1a635e7b74765d44d2caf8cc2c5f66a6bc Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 14 Oct 2014 14:50:29 -0400 Subject: Added Visual Studio 2010 project/solution files for .NET 2.0 and Xamarin Android/iOS --- crypto/test/src/util/test/SimpleTest.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'crypto/test/src/util') diff --git a/crypto/test/src/util/test/SimpleTest.cs b/crypto/test/src/util/test/SimpleTest.cs index be846e20f..bcbfa2a5b 100644 --- a/crypto/test/src/util/test/SimpleTest.cs +++ b/crypto/test/src/util/test/SimpleTest.cs @@ -115,23 +115,13 @@ namespace Org.BouncyCastle.Utilities.Test private static string GetFullName( string name) { -// TODO MonoDevelop/Visual Studio embedded resource ids still inconsistent -#if BC_BUILD_MONODEVELOP - return "test.data." + name; -#else - return "crypto.test.data." + name; -#endif + return "UnitTests.data." + name; } private static string GetShortName( string fullName) { -// TODO MonoDevelop/Visual Studio embedded resource ids still inconsistent -#if BC_BUILD_MONODEVELOP - return fullName.Substring("test.data.".Length); -#else - return fullName.Substring("crypto.test.data.".Length); -#endif + return fullName.Substring("UnitTests.data.".Length); } #if NETCF_1_0 || NETCF_2_0 -- cgit 1.5.1 From 36057f66032c5e2942a8c3797be39f20693318f3 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 9 Nov 2015 12:10:58 +0700 Subject: Add DOTNET and LIB preprocessor flags as per BouncyCastle-PCL --- crypto/src/cms/CMSProcessableFile.cs | 2 +- crypto/src/openpgp/PgpLiteralDataGenerator.cs | 4 ++-- crypto/src/openpgp/PgpUtilities.cs | 2 +- crypto/test/src/asn1/test/AllTests.cs | 2 ++ crypto/test/src/cms/test/AllTests.cs | 2 ++ crypto/test/src/crypto/agreement/test/AllTests.cs | 4 +++- crypto/test/src/crypto/io/test/AllTests.cs | 2 ++ crypto/test/src/crypto/test/AllTests.cs | 4 ++++ crypto/test/src/math/ec/test/AllTests.cs | 2 ++ crypto/test/src/math/test/AllTests.cs | 2 ++ crypto/test/src/ocsp/test/AllTests.cs | 2 ++ crypto/test/src/openpgp/examples/test/AllTests.cs | 2 ++ crypto/test/src/openssl/test/AllTests.cs | 4 ++++ crypto/test/src/tsp/test/AllTests.cs | 2 ++ crypto/test/src/util/io/pem/test/AllTests.cs | 4 ++++ 15 files changed, 35 insertions(+), 5 deletions(-) (limited to 'crypto/test/src/util') diff --git a/crypto/src/cms/CMSProcessableFile.cs b/crypto/src/cms/CMSProcessableFile.cs index b514bdbda..c74d2a846 100644 --- a/crypto/src/cms/CMSProcessableFile.cs +++ b/crypto/src/cms/CMSProcessableFile.cs @@ -1,4 +1,4 @@ -#if !PORTABLE +#if !PORTABLE || DOTNET using System; using System.IO; diff --git a/crypto/src/openpgp/PgpLiteralDataGenerator.cs b/crypto/src/openpgp/PgpLiteralDataGenerator.cs index 17a6eeef2..7672659ca 100644 --- a/crypto/src/openpgp/PgpLiteralDataGenerator.cs +++ b/crypto/src/openpgp/PgpLiteralDataGenerator.cs @@ -141,8 +141,8 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return new WrappedGeneratorStream(this, pkOut); } -#if !PORTABLE - /// +#if !PORTABLE || DOTNET + /// ///

/// Open a literal data packet for the passed in FileInfo object, returning /// an output stream for saving the file contents. diff --git a/crypto/src/openpgp/PgpUtilities.cs b/crypto/src/openpgp/PgpUtilities.cs index 9238edcfc..055f99636 100644 --- a/crypto/src/openpgp/PgpUtilities.cs +++ b/crypto/src/openpgp/PgpUtilities.cs @@ -347,7 +347,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp return MakeKey(algorithm, keyBytes); } -#if !PORTABLE +#if !PORTABLE || DOTNET ///

Write out the passed in file as a literal data packet. public static void WriteFileToLiteralData( Stream output, diff --git a/crypto/test/src/asn1/test/AllTests.cs b/crypto/test/src/asn1/test/AllTests.cs index ad2f90362..981b5a4df 100644 --- a/crypto/test/src/asn1/test/AllTests.cs +++ b/crypto/test/src/asn1/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -28,3 +29,4 @@ namespace Org.BouncyCastle.Asn1.Tests } } } +#endif diff --git a/crypto/test/src/cms/test/AllTests.cs b/crypto/test/src/cms/test/AllTests.cs index b7ac7644b..b47374914 100644 --- a/crypto/test/src/cms/test/AllTests.cs +++ b/crypto/test/src/cms/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -32,3 +33,4 @@ namespace Org.BouncyCastle.Cms.Tests } } } +#endif diff --git a/crypto/test/src/crypto/agreement/test/AllTests.cs b/crypto/test/src/crypto/agreement/test/AllTests.cs index ea8f438e5..66853b285 100644 --- a/crypto/test/src/crypto/agreement/test/AllTests.cs +++ b/crypto/test/src/crypto/agreement/test/AllTests.cs @@ -1,4 +1,5 @@ -using System; +#if !LIB +using System; using NUnit.Core; using NUnit.Framework; @@ -29,3 +30,4 @@ namespace Org.BouncyCastle.Crypto.Agreement.Tests } } } +#endif diff --git a/crypto/test/src/crypto/io/test/AllTests.cs b/crypto/test/src/crypto/io/test/AllTests.cs index 5c8c759f9..2634b4ab1 100644 --- a/crypto/test/src/crypto/io/test/AllTests.cs +++ b/crypto/test/src/crypto/io/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -24,3 +25,4 @@ namespace Org.BouncyCastle.Crypto.IO.Tests } } } +#endif diff --git a/crypto/test/src/crypto/test/AllTests.cs b/crypto/test/src/crypto/test/AllTests.cs index 3d8ef5602..e7035a663 100644 --- a/crypto/test/src/crypto/test/AllTests.cs +++ b/crypto/test/src/crypto/test/AllTests.cs @@ -1,6 +1,8 @@ using System; +#if !LIB using NUnit.Core; +#endif using NUnit.Framework; using Org.BouncyCastle.Utilities.Test; @@ -10,6 +12,7 @@ namespace Org.BouncyCastle.Crypto.Tests [TestFixture] public class AllTests { +#if !LIB public static void Main(string[] args) { Suite.Run(new NullListener(), NUnit.Core.TestFilter.Empty); @@ -26,6 +29,7 @@ namespace Org.BouncyCastle.Crypto.Tests return suite; } } +#endif [Test] public void TestCrypto() diff --git a/crypto/test/src/math/ec/test/AllTests.cs b/crypto/test/src/math/ec/test/AllTests.cs index 3e014ffd2..0517ac713 100644 --- a/crypto/test/src/math/ec/test/AllTests.cs +++ b/crypto/test/src/math/ec/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -25,3 +26,4 @@ namespace Org.BouncyCastle.Math.EC.Tests } } } +#endif diff --git a/crypto/test/src/math/test/AllTests.cs b/crypto/test/src/math/test/AllTests.cs index 2bcc129ea..53feff954 100644 --- a/crypto/test/src/math/test/AllTests.cs +++ b/crypto/test/src/math/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -25,3 +26,4 @@ namespace Org.BouncyCastle.Math.Tests } } } +#endif diff --git a/crypto/test/src/ocsp/test/AllTests.cs b/crypto/test/src/ocsp/test/AllTests.cs index 5e919fd91..5e799cd09 100644 --- a/crypto/test/src/ocsp/test/AllTests.cs +++ b/crypto/test/src/ocsp/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -26,3 +27,4 @@ namespace Org.BouncyCastle.Ocsp.Tests } } } +#endif diff --git a/crypto/test/src/openpgp/examples/test/AllTests.cs b/crypto/test/src/openpgp/examples/test/AllTests.cs index 9a515ea15..3403a4fdc 100644 --- a/crypto/test/src/openpgp/examples/test/AllTests.cs +++ b/crypto/test/src/openpgp/examples/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using System.IO; @@ -434,3 +435,4 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Examples.Tests } } } +#endif diff --git a/crypto/test/src/openssl/test/AllTests.cs b/crypto/test/src/openssl/test/AllTests.cs index f843d0479..0cc2dcd85 100644 --- a/crypto/test/src/openssl/test/AllTests.cs +++ b/crypto/test/src/openssl/test/AllTests.cs @@ -2,7 +2,9 @@ using System; using System.IO; using System.Text; +#if !LIB using NUnit.Core; +#endif using NUnit.Framework; using Org.BouncyCastle.Crypto; @@ -33,6 +35,7 @@ namespace Org.BouncyCastle.OpenSsl.Tests } } +#if !LIB public static void Main(string[] args) { Suite.Run(new NullListener(), NUnit.Core.TestFilter.Empty); @@ -48,6 +51,7 @@ namespace Org.BouncyCastle.OpenSsl.Tests return suite; } } +#endif [Test] public void TestOpenSsl() diff --git a/crypto/test/src/tsp/test/AllTests.cs b/crypto/test/src/tsp/test/AllTests.cs index 3e8b0cd5e..58d096d54 100644 --- a/crypto/test/src/tsp/test/AllTests.cs +++ b/crypto/test/src/tsp/test/AllTests.cs @@ -1,3 +1,4 @@ +#if !LIB using System; using NUnit.Core; @@ -29,3 +30,4 @@ namespace Org.BouncyCastle.Tsp.Tests } } } +#endif diff --git a/crypto/test/src/util/io/pem/test/AllTests.cs b/crypto/test/src/util/io/pem/test/AllTests.cs index c36f79304..921c40cb8 100644 --- a/crypto/test/src/util/io/pem/test/AllTests.cs +++ b/crypto/test/src/util/io/pem/test/AllTests.cs @@ -4,7 +4,9 @@ using System.Collections.Specialized; using System.IO; using System.Text; +#if !LIB using NUnit.Core; +#endif using NUnit.Framework; using Org.BouncyCastle.Crypto; @@ -19,6 +21,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem.Tests [TestFixture] public class AllTests { +#if !LIB public static void Main(string[] args) { Suite.Run(new NullListener(), NUnit.Core.TestFilter.Empty); @@ -34,6 +37,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem.Tests return suite; } } +#endif [Test] public void TestPemLength() -- cgit 1.5.1 From ced92a086526c85e31842aaa60ede4c4b8e0fdb9 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 17 Nov 2015 18:57:14 +0700 Subject: Improve usage/behaviour of SecureRandom.GenerateSeed --- crypto/crypto.csproj | 10 ++++ crypto/src/crypto/CipherKeyGenerator.cs | 4 +- crypto/src/crypto/encodings/OaepEncoding.cs | 2 +- .../crypto/prng/CryptoApiEntropySourceProvider.cs | 70 ++++++++++++++++++++++ crypto/src/crypto/prng/DigestRandomGenerator.cs | 12 ++-- crypto/src/crypto/prng/EntropyUtilities.cs | 30 ++++++++++ crypto/src/crypto/prng/SP800SecureRandom.cs | 4 +- crypto/src/crypto/prng/X931Rng.cs | 2 +- crypto/src/crypto/prng/X931SecureRandom.cs | 4 +- crypto/src/security/SecureRandom.cs | 21 +++---- crypto/test/src/security/test/SecureRandomTest.cs | 41 ++++++++++++- crypto/test/src/test/BlockCipherTest.cs | 2 +- crypto/test/src/util/test/FixedSecureRandom.cs | 7 ++- 13 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 crypto/src/crypto/prng/CryptoApiEntropySourceProvider.cs create mode 100644 crypto/src/crypto/prng/EntropyUtilities.cs (limited to 'crypto/test/src/util') diff --git a/crypto/crypto.csproj b/crypto/crypto.csproj index 395ea08df..ecfdc7311 100644 --- a/crypto/crypto.csproj +++ b/crypto/crypto.csproj @@ -4318,6 +4318,11 @@ SubType = "Code" BuildAction = "Compile" /> + + >= 8; - } + byte[] bytes = new byte[8]; + Pack.UInt64_To_LE((ulong)seedVal, bytes); + digest.BlockUpdate(bytes, 0, bytes.Length); } - private void DigestUpdate(byte[] inSeed) + private void DigestUpdate(byte[] inSeed) { digest.BlockUpdate(inSeed, 0, inSeed.Length); } diff --git a/crypto/src/crypto/prng/EntropyUtilities.cs b/crypto/src/crypto/prng/EntropyUtilities.cs new file mode 100644 index 000000000..58c8703f4 --- /dev/null +++ b/crypto/src/crypto/prng/EntropyUtilities.cs @@ -0,0 +1,30 @@ +using System; + +using Org.BouncyCastle.Security; + +namespace Org.BouncyCastle.Crypto.Prng +{ + public abstract class EntropyUtilities + { + /** + * Generate numBytes worth of entropy from the passed in entropy source. + * + * @param entropySource the entropy source to request the data from. + * @param numBytes the number of bytes of entropy requested. + * @return a byte array populated with the random data. + */ + public static byte[] GenerateSeed(IEntropySource entropySource, int numBytes) + { + byte[] bytes = new byte[numBytes]; + int count = 0; + while (count < numBytes) + { + byte[] entropy = entropySource.GetEntropy(); + int toCopy = System.Math.Min(bytes.Length, numBytes - count); + Array.Copy(entropy, 0, bytes, count, toCopy); + count += toCopy; + } + return bytes; + } + } +} diff --git a/crypto/src/crypto/prng/SP800SecureRandom.cs b/crypto/src/crypto/prng/SP800SecureRandom.cs index 996ba0846..5c5bda399 100644 --- a/crypto/src/crypto/prng/SP800SecureRandom.cs +++ b/crypto/src/crypto/prng/SP800SecureRandom.cs @@ -74,9 +74,7 @@ namespace Org.BouncyCastle.Crypto.Prng public override byte[] GenerateSeed(int numBytes) { - byte[] bytes = new byte[numBytes]; - NextBytes(bytes); - return bytes; + return EntropyUtilities.GenerateSeed(mEntropySource, numBytes); } } } diff --git a/crypto/src/crypto/prng/X931Rng.cs b/crypto/src/crypto/prng/X931Rng.cs index d09cc6618..987379d4b 100644 --- a/crypto/src/crypto/prng/X931Rng.cs +++ b/crypto/src/crypto/prng/X931Rng.cs @@ -10,7 +10,7 @@ namespace Org.BouncyCastle.Crypto.Prng private const int BLOCK128_MAX_BITS_REQUEST = 1 << (19 - 1); private readonly IBlockCipher mEngine; - private readonly IEntropySource mEntropySource; + internal readonly IEntropySource mEntropySource; private readonly byte[] mDT; private readonly byte[] mI; diff --git a/crypto/src/crypto/prng/X931SecureRandom.cs b/crypto/src/crypto/prng/X931SecureRandom.cs index a87bf1567..bce8d2cf1 100644 --- a/crypto/src/crypto/prng/X931SecureRandom.cs +++ b/crypto/src/crypto/prng/X931SecureRandom.cs @@ -64,9 +64,7 @@ namespace Org.BouncyCastle.Crypto.Prng public override byte[] GenerateSeed(int numBytes) { - byte[] bytes = new byte[numBytes]; - NextBytes(bytes); - return bytes; + return EntropyUtilities.GenerateSeed(mDrbg.mEntropySource, numBytes); } } } diff --git a/crypto/src/security/SecureRandom.cs b/crypto/src/security/SecureRandom.cs index 8683b4729..cb831acc2 100644 --- a/crypto/src/security/SecureRandom.cs +++ b/crypto/src/security/SecureRandom.cs @@ -68,11 +68,18 @@ namespace Org.BouncyCastle.Security if (autoSeed) { prng.AddSeedMaterial(NextCounterValue()); - prng.AddSeedMaterial(GetSeed(digest.GetDigestSize())); + prng.AddSeedMaterial(GetNextBytes(Master, digest.GetDigestSize())); } return prng; } + public static byte[] GetNextBytes(SecureRandom secureRandom, int length) + { + byte[] result = new byte[length]; + secureRandom.NextBytes(result); + return result; + } + /// /// Create and auto-seed an instance based on the given algorithm. /// @@ -104,12 +111,10 @@ namespace Org.BouncyCastle.Security throw new ArgumentException("Unrecognised PRNG algorithm: " + algorithm, "algorithm"); } + [Obsolete("Call GenerateSeed() on a SecureRandom instance instead")] public static byte[] GetSeed(int length) { -#if NETCF_1_0 || PORTABLE - lock (master) -#endif - return Master.GenerateSeed(length); + return GetNextBytes(Master, length); } protected readonly IRandomGenerator generator; @@ -145,11 +150,7 @@ namespace Org.BouncyCastle.Security public virtual byte[] GenerateSeed(int length) { - SetSeed(DateTime.Now.Ticks); - - byte[] rv = new byte[length]; - NextBytes(rv); - return rv; + return GetNextBytes(Master, length); } public virtual void SetSeed(byte[] seed) diff --git a/crypto/test/src/security/test/SecureRandomTest.cs b/crypto/test/src/security/test/SecureRandomTest.cs index f93afc0aa..f1d83b29c 100644 --- a/crypto/test/src/security/test/SecureRandomTest.cs +++ b/crypto/test/src/security/test/SecureRandomTest.cs @@ -3,7 +3,12 @@ using System.Text; using NUnit.Framework; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Digests; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Macs; using Org.BouncyCastle.Crypto.Prng; +using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Security.Tests @@ -60,6 +65,30 @@ namespace Org.BouncyCastle.Security.Tests CheckSecureRandom(random); } + [Test] + public void TestSP800Ctr() + { + SecureRandom random = new SP800SecureRandomBuilder().BuildCtr(new AesFastEngine(), 256, new byte[32], false); + + CheckSecureRandom(random); + } + + [Test] + public void TestSP800Hash() + { + SecureRandom random = new SP800SecureRandomBuilder().BuildHash(new Sha256Digest(), new byte[32], false); + + CheckSecureRandom(random); + } + + [Test] + public void TestSP800HMac() + { + SecureRandom random = new SP800SecureRandomBuilder().BuildHMac(new HMac(new Sha256Digest()), new byte[32], false); + + CheckSecureRandom(random); + } + [Test] public void TestThreadedSeed() { @@ -73,7 +102,15 @@ namespace Org.BouncyCastle.Security.Tests public void TestVmpcPrng() { SecureRandom random = new SecureRandom(new VmpcRandomGenerator()); - random.SetSeed(SecureRandom.GetSeed(32)); + random.SetSeed(random.GenerateSeed(32)); + + CheckSecureRandom(random); + } + + [Test] + public void TestX931() + { + SecureRandom random = new X931SecureRandomBuilder().Build(new AesFastEngine(), new KeyParameter(new byte[16]), false); CheckSecureRandom(random); } @@ -106,7 +143,7 @@ namespace Org.BouncyCastle.Security.Tests private static double MeasureChiSquared(SecureRandom random, int rounds) { - byte[] opts = SecureRandom.GetSeed(2); + byte[] opts = random.GenerateSeed(2); int[] counts = new int[256]; byte[] bs = new byte[256]; diff --git a/crypto/test/src/test/BlockCipherTest.cs b/crypto/test/src/test/BlockCipherTest.cs index 93cf2b0a5..fc3a99f4e 100644 --- a/crypto/test/src/test/BlockCipherTest.cs +++ b/crypto/test/src/test/BlockCipherTest.cs @@ -410,7 +410,7 @@ namespace Org.BouncyCastle.Tests else { // NB: rand always generates same values each test run - iv = rand.GenerateSeed(ivLength); + iv = SecureRandom.GetNextBytes(rand, ivLength); } parameters = new ParametersWithIV(key, iv); diff --git a/crypto/test/src/util/test/FixedSecureRandom.cs b/crypto/test/src/util/test/FixedSecureRandom.cs index 15a2e9bb3..d8598ac24 100644 --- a/crypto/test/src/util/test/FixedSecureRandom.cs +++ b/crypto/test/src/util/test/FixedSecureRandom.cs @@ -38,7 +38,12 @@ namespace Org.BouncyCastle.Utilities.Test return new FixedSecureRandom(bOut.ToArray()); } - public override void NextBytes( + public override byte[] GenerateSeed(int numBytes) + { + return SecureRandom.GetNextBytes(this, numBytes); + } + + public override void NextBytes( byte[] buf) { Array.Copy(_data, _index, buf, 0, buf.Length); -- cgit 1.5.1