summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-23 16:01:11 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-23 16:01:11 +0700
commit584ada5e755ef20e9b2824f11b034afaf32b3fda (patch)
treecf11582bcb5990c3f74b71e074d9665378c3564b
parentVMPC refactoring (diff)
downloadBouncyCastle.NET-ed25519-584ada5e755ef20e9b2824f11b034afaf32b3fda.tar.xz
Change BigInteger arbitrary random source
-rw-r--r--crypto/src/math/BigInteger.cs4
-rw-r--r--crypto/src/security/SecureRandom.cs36
2 files changed, 26 insertions, 14 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs
index dd8b3a85b..5986f9fd8 100644
--- a/crypto/src/math/BigInteger.cs
+++ b/crypto/src/math/BigInteger.cs
@@ -7,7 +7,7 @@ using System.Runtime.Intrinsics.X86;
 #endif
 using System.Runtime.Serialization;
 using System.Text;
-
+using Org.BouncyCastle.Crypto.Prng;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 
@@ -163,7 +163,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 SecureRandom RandomSource = new SecureRandom();
+        private static readonly SecureRandom RandomSource = new SecureRandom(new VmpcRandomGenerator(), 16);
 
         /*
          * These are the threshold bit-lengths (of an exponent) where we increase the window size.
diff --git a/crypto/src/security/SecureRandom.cs b/crypto/src/security/SecureRandom.cs
index 10b837b03..53e5be069 100644
--- a/crypto/src/security/SecureRandom.cs
+++ b/crypto/src/security/SecureRandom.cs
@@ -27,18 +27,7 @@ namespace Org.BouncyCastle.Security
             DigestRandomGenerator prng = new DigestRandomGenerator(digest);
             if (autoSeed)
             {
-                prng.AddSeedMaterial(NextCounterValue());
-
-                int seedLength = digest.GetDigestSize();
-#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-                Span<byte> seed = seedLength <= 128
-                    ? stackalloc byte[seedLength]
-                    : new byte[seedLength];
-#else
-                byte[] seed = new byte[seedLength];
-#endif
-                Master.NextBytes(seed);
-                prng.AddSeedMaterial(seed);
+                AutoSeed(prng, digest.GetDigestSize());
             }
             return prng;
         }
@@ -103,6 +92,14 @@ namespace Org.BouncyCastle.Security
             this.generator = generator;
         }
 
+        public SecureRandom(IRandomGenerator generator, int autoSeedLengthInBytes)
+            : base(0)
+        {
+            AutoSeed(generator, autoSeedLengthInBytes);
+
+            this.generator = generator;
+        }
+
         public virtual byte[] GenerateSeed(int length)
         {
             return GetNextBytes(Master, length);
@@ -246,5 +243,20 @@ namespace Org.BouncyCastle.Security
             NextBytes(bytes);
             return (long)Pack.BE_To_UInt64(bytes);
         }
+
+        private static void AutoSeed(IRandomGenerator generator, int seedLength)
+        {
+            generator.AddSeedMaterial(NextCounterValue());
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> seed = seedLength <= 128
+                ? stackalloc byte[seedLength]
+                : new byte[seedLength];
+#else
+                byte[] seed = new byte[seedLength];
+#endif
+            Master.NextBytes(seed);
+            generator.AddSeedMaterial(seed);
+        }
     }
 }