diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-12 18:07:11 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-12 18:07:11 +0700 |
commit | 03818ccafe4464791f8fe60de70ed5b4615d8f2e (patch) | |
tree | 56c8845a575641f2cefc161a73adc4fac911ca0c /crypto | |
parent | Make explicitly UTC (diff) | |
download | BouncyCastle.NET-ed25519-03818ccafe4464791f8fe60de70ed5b4615d8f2e.tar.xz |
Refactoring around SecureRandom
Diffstat (limited to 'crypto')
25 files changed, 203 insertions, 162 deletions
diff --git a/crypto/src/crypto/prng/SP800SecureRandom.cs b/crypto/src/crypto/prng/SP800SecureRandom.cs index 30c838c1b..2e1484125 100644 --- a/crypto/src/crypto/prng/SP800SecureRandom.cs +++ b/crypto/src/crypto/prng/SP800SecureRandom.cs @@ -49,6 +49,11 @@ namespace Org.BouncyCastle.Crypto.Prng public override void NextBytes(byte[] bytes) { + NextBytes(bytes, 0, bytes.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { lock (this) { if (mDrbg == null) @@ -57,21 +62,14 @@ namespace Org.BouncyCastle.Crypto.Prng } // check if a reseed is required... - if (mDrbg.Generate(bytes, null, mPredictionResistant) < 0) + if (mDrbg.Generate(buf, off, len, null, mPredictionResistant) < 0) { mDrbg.Reseed(null); - mDrbg.Generate(bytes, null, mPredictionResistant); + mDrbg.Generate(buf, off, len, null, mPredictionResistant); } } } - public override void NextBytes(byte[] buf, int off, int len) - { - byte[] bytes = new byte[len]; - NextBytes(bytes); - Array.Copy(bytes, 0, buf, off, len); - } - public override byte[] GenerateSeed(int numBytes) { return EntropyUtilities.GenerateSeed(mEntropySource, numBytes); diff --git a/crypto/src/crypto/prng/X931Rng.cs b/crypto/src/crypto/prng/X931Rng.cs index 2bd8e0c6b..53c982c25 100644 --- a/crypto/src/crypto/prng/X931Rng.cs +++ b/crypto/src/crypto/prng/X931Rng.cs @@ -46,14 +46,14 @@ namespace Org.BouncyCastle.Crypto.Prng * * @return number of bits generated, -1 if a reseed required. */ - internal int Generate(byte[] output, bool predictionResistant) + internal int Generate(byte[] output, int outputOff, int outputLen, bool predictionResistant) { if (mR.Length == 8) // 64 bit block size { if (mReseedCounter > BLOCK64_RESEED_MAX) return -1; - if (IsTooLarge(output, BLOCK64_MAX_BITS_REQUEST / 8)) + if (outputLen > BLOCK64_MAX_BITS_REQUEST / 8) throw new ArgumentException("Number of bits per request limited to " + BLOCK64_MAX_BITS_REQUEST, "output"); } else @@ -61,7 +61,7 @@ namespace Org.BouncyCastle.Crypto.Prng if (mReseedCounter > BLOCK128_RESEED_MAX) return -1; - if (IsTooLarge(output, BLOCK128_MAX_BITS_REQUEST / 8)) + if (outputLen > BLOCK128_MAX_BITS_REQUEST / 8) throw new ArgumentException("Number of bits per request limited to " + BLOCK128_MAX_BITS_REQUEST, "output"); } @@ -72,7 +72,7 @@ namespace Org.BouncyCastle.Crypto.Prng throw new InvalidOperationException("Insufficient entropy returned"); } - int m = output.Length / mR.Length; + int m = outputLen / mR.Length; for (int i = 0; i < m; i++) { @@ -80,12 +80,12 @@ namespace Org.BouncyCastle.Crypto.Prng Process(mR, mI, mV); Process(mV, mR, mI); - Array.Copy(mR, 0, output, i * mR.Length, mR.Length); + Array.Copy(mR, 0, output, outputOff + i * mR.Length, mR.Length); Increment(mDT); } - int bytesToCopy = (output.Length - m * mR.Length); + int bytesToCopy = (outputLen - m * mR.Length); if (bytesToCopy > 0) { @@ -93,14 +93,14 @@ namespace Org.BouncyCastle.Crypto.Prng Process(mR, mI, mV); Process(mV, mR, mI); - Array.Copy(mR, 0, output, m * mR.Length, bytesToCopy); + Array.Copy(mR, 0, output, outputOff + m * mR.Length, bytesToCopy); Increment(mDT); } mReseedCounter++; - return output.Length; + return outputLen * 8; } /** @@ -137,10 +137,5 @@ namespace Org.BouncyCastle.Crypto.Prng break; } } - - private static bool IsTooLarge(byte[] bytes, int maxBytes) - { - return bytes != null && bytes.Length > maxBytes; - } } } diff --git a/crypto/src/crypto/prng/X931SecureRandom.cs b/crypto/src/crypto/prng/X931SecureRandom.cs index d2e4849c5..1402e5c31 100644 --- a/crypto/src/crypto/prng/X931SecureRandom.cs +++ b/crypto/src/crypto/prng/X931SecureRandom.cs @@ -44,24 +44,22 @@ namespace Org.BouncyCastle.Crypto.Prng public override void NextBytes(byte[] bytes) { + NextBytes(bytes, 0, bytes.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { lock (this) { // check if a reseed is required... - if (mDrbg.Generate(bytes, mPredictionResistant) < 0) + if (mDrbg.Generate(buf, off, len, mPredictionResistant) < 0) { mDrbg.Reseed(); - mDrbg.Generate(bytes, mPredictionResistant); + mDrbg.Generate(buf, off, len, mPredictionResistant); } } } - public override void NextBytes(byte[] buf, int off, int len) - { - byte[] bytes = new byte[len]; - NextBytes(bytes); - Array.Copy(bytes, 0, buf, off, len); - } - public override byte[] GenerateSeed(int numBytes) { return EntropyUtilities.GenerateSeed(mDrbg.EntropySource, numBytes); diff --git a/crypto/src/crypto/prng/drbg/CtrSP800Drbg.cs b/crypto/src/crypto/prng/drbg/CtrSP800Drbg.cs index 5715a915e..a7b1326c3 100644 --- a/crypto/src/crypto/prng/drbg/CtrSP800Drbg.cs +++ b/crypto/src/crypto/prng/drbg/CtrSP800Drbg.cs @@ -331,14 +331,15 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg * * @return number of bits generated, -1 if a reseed required. */ - public int Generate(byte[] output, byte[] additionalInput, bool predictionResistant) + public int Generate(byte[] output, int outputOff, int outputLen, byte[] additionalInput, + bool predictionResistant) { if (mIsTdea) { if (mReseedCounter > TDEA_RESEED_MAX) return -1; - if (DrbgUtilities.IsTooLarge(output, TDEA_MAX_BITS_REQUEST / 8)) + if (outputLen > TDEA_MAX_BITS_REQUEST / 8) throw new ArgumentException("Number of bits per request limited to " + TDEA_MAX_BITS_REQUEST, "output"); } else @@ -346,7 +347,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg if (mReseedCounter > AES_RESEED_MAX) return -1; - if (DrbgUtilities.IsTooLarge(output, AES_MAX_BITS_REQUEST / 8)) + if (outputLen > AES_MAX_BITS_REQUEST / 8) throw new ArgumentException("Number of bits per request limited to " + AES_MAX_BITS_REQUEST, "output"); } @@ -370,11 +371,9 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg mEngine.Init(true, new KeyParameter(ExpandKey(mKey))); - for (int i = 0; i <= output.Length / tmp.Length; i++) + for (int i = 0, limit = outputLen / tmp.Length; i <= limit; i++) { - int bytesToCopy = ((output.Length - i * tmp.Length) > tmp.Length) - ? tmp.Length - : (output.Length - i * mV.Length); + int bytesToCopy = System.Math.Min(tmp.Length, outputLen - i * tmp.Length); if (bytesToCopy != 0) { @@ -382,7 +381,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg mEngine.ProcessBlock(mV, 0, tmp, 0); - Array.Copy(tmp, 0, output, i * tmp.Length, bytesToCopy); + Array.Copy(tmp, 0, output, outputOff + i * tmp.Length, bytesToCopy); } } @@ -390,7 +389,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg mReseedCounter++; - return output.Length * 8; + return outputLen * 8; } /** diff --git a/crypto/src/crypto/prng/drbg/DrbgUtilities.cs b/crypto/src/crypto/prng/drbg/DrbgUtilities.cs index b1f2f29be..58baaf5d9 100644 --- a/crypto/src/crypto/prng/drbg/DrbgUtilities.cs +++ b/crypto/src/crypto/prng/drbg/DrbgUtilities.cs @@ -95,10 +95,5 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg return temp; } - - internal static bool IsTooLarge(byte[] bytes, int maxBytes) - { - return bytes != null && bytes.Length > maxBytes; - } } } diff --git a/crypto/src/crypto/prng/drbg/HMacSP800Drbg.cs b/crypto/src/crypto/prng/drbg/HMacSP800Drbg.cs index 78331705e..0ec0e8b71 100644 --- a/crypto/src/crypto/prng/drbg/HMacSP800Drbg.cs +++ b/crypto/src/crypto/prng/drbg/HMacSP800Drbg.cs @@ -104,9 +104,10 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg * * @return number of bits generated, -1 if a reseed required. */ - public int Generate(byte[] output, byte[] additionalInput, bool predictionResistant) + public int Generate(byte[] output, int outputOff, int outputLen, byte[] additionalInput, + bool predictionResistant) { - int numberOfBits = output.Length * 8; + int numberOfBits = outputLen * 8; if (numberOfBits > MAX_BITS_REQUEST) throw new ArgumentException("Number of bits per request limited to " + MAX_BITS_REQUEST, "output"); @@ -129,9 +130,9 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg } // 3. - byte[] rv = new byte[output.Length]; + byte[] rv = new byte[outputLen]; - int m = output.Length / mV.Length; + int m = outputLen / mV.Length; mHMac.Init(new KeyParameter(mK)); @@ -155,7 +156,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg mReseedCounter++; - Array.Copy(rv, 0, output, 0, output.Length); + Array.Copy(rv, 0, output, outputOff, outputLen); return numberOfBits; } diff --git a/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs b/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs index 506517aae..accc65ec3 100644 --- a/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs +++ b/crypto/src/crypto/prng/drbg/HashSP800Drbg.cs @@ -101,7 +101,8 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg * * @return number of bits generated, -1 if a reseed required. */ - public int Generate(byte[] output, byte[] additionalInput, bool predictionResistant) + public int Generate(byte[] output, int outputOff, int outputLen, byte[] additionalInput, + bool predictionResistant) { // 1. If reseed_counter > reseed_interval, then return an indication that a // reseed is required. @@ -116,7 +117,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg // 6. reseed_counter = reseed_counter + 1. // 7. Return SUCCESS, returned_bits, and the new values of V, C, and // reseed_counter for the new_working_state. - int numberOfBits = output.Length * 8; + int numberOfBits = outputLen * 8; if (numberOfBits > MAX_BITS_REQUEST) throw new ArgumentException("Number of bits per request limited to " + MAX_BITS_REQUEST, "output"); @@ -166,7 +167,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg mReseedCounter++; - Array.Copy(rv, 0, output, 0, output.Length); + Array.Copy(rv, 0, output, outputOff, outputLen); return numberOfBits; } diff --git a/crypto/src/crypto/prng/drbg/ISP80090Drbg.cs b/crypto/src/crypto/prng/drbg/ISP80090Drbg.cs index 0e398209e..78cbcd92f 100644 --- a/crypto/src/crypto/prng/drbg/ISP80090Drbg.cs +++ b/crypto/src/crypto/prng/drbg/ISP80090Drbg.cs @@ -23,7 +23,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Drbg * * @return number of bits generated, -1 if a reseed required. */ - int Generate(byte[] output, byte[] additionalInput, bool predictionResistant); + int Generate(byte[] output, int outputOff, int outputLen, byte[] additionalInput, bool predictionResistant); /** * Reseed the DRBG. diff --git a/crypto/test/src/crypto/prng/test/CtrDrbgTest.cs b/crypto/test/src/crypto/prng/test/CtrDrbgTest.cs index 1fd051294..2471bba8c 100644 --- a/crypto/test/src/crypto/prng/test/CtrDrbgTest.cs +++ b/crypto/test/src/crypto/prng/test/CtrDrbgTest.cs @@ -340,7 +340,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test output = new byte[tv.GetExpectedValue(0).Length]; - d.Generate(output, tv.GetAdditionalInput(0), tv.PredictionResistance); + d.Generate(output, 0, output.Length, tv.GetAdditionalInput(0), tv.PredictionResistance); byte[] expected = tv.GetExpectedValue(0); @@ -351,7 +351,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test output = new byte[tv.GetExpectedValue(0).Length]; - d.Generate(output, tv.GetAdditionalInput(1), tv.PredictionResistance); + d.Generate(output, 0, output.Length, tv.GetAdditionalInput(1), tv.PredictionResistance); expected = tv.GetExpectedValue(1); if (!AreEqual(expected, output)) @@ -368,7 +368,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test output = new byte[tv.GetExpectedValue(0).Length]; - drbg.Generate(output, tv.GetAdditionalInput(0), tv.PredictionResistance); + drbg.Generate(output, 0, output.Length, tv.GetAdditionalInput(0), tv.PredictionResistance); // Exception tests try diff --git a/crypto/test/src/crypto/prng/test/HMacDrbgTest.cs b/crypto/test/src/crypto/prng/test/HMacDrbgTest.cs index f523cc4d7..9e2ae7b90 100644 --- a/crypto/test/src/crypto/prng/test/HMacDrbgTest.cs +++ b/crypto/test/src/crypto/prng/test/HMacDrbgTest.cs @@ -414,7 +414,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test byte[] output = new byte[tv.GetExpectedValue(0).Length]; - d.Generate(output, tv.GetAdditionalInput(0), tv.PredictionResistance); + d.Generate(output, 0, output.Length, tv.GetAdditionalInput(0), tv.PredictionResistance); byte[] expected = tv.GetExpectedValue(0); @@ -425,7 +425,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test output = new byte[tv.GetExpectedValue(0).Length]; - d.Generate(output, tv.GetAdditionalInput(1), tv.PredictionResistance); + d.Generate(output, 0, output.Length, tv.GetAdditionalInput(1), tv.PredictionResistance); expected = tv.GetExpectedValue(1); if (!AreEqual(expected, output)) diff --git a/crypto/test/src/crypto/prng/test/HashDrbgTest.cs b/crypto/test/src/crypto/prng/test/HashDrbgTest.cs index e043f03da..29929c613 100644 --- a/crypto/test/src/crypto/prng/test/HashDrbgTest.cs +++ b/crypto/test/src/crypto/prng/test/HashDrbgTest.cs @@ -356,7 +356,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test byte[] output = new byte[tv.GetExpectedValue(0).Length]; - d.Generate(output, tv.GetAdditionalInput(0), tv.PredictionResistance); + d.Generate(output, 0, output.Length, tv.GetAdditionalInput(0), tv.PredictionResistance); byte[] expected = tv.GetExpectedValue(0); @@ -367,7 +367,7 @@ namespace Org.BouncyCastle.Crypto.Prng.Test output = new byte[tv.GetExpectedValue(0).Length]; - d.Generate(output, tv.GetAdditionalInput(1), tv.PredictionResistance); + d.Generate(output, 0, output.Length, tv.GetAdditionalInput(1), tv.PredictionResistance); expected = tv.GetExpectedValue(1); if (!AreEqual(expected, output)) diff --git a/crypto/test/src/crypto/test/DSATest.cs b/crypto/test/src/crypto/test/DSATest.cs index b81ef511f..f9f601ed8 100644 --- a/crypto/test/src/crypto/test/DSATest.cs +++ b/crypto/test/src/crypto/test/DSATest.cs @@ -592,16 +592,16 @@ namespace Org.BouncyCastle.Crypto.Tests { } - public override void NextBytes(byte[] bytes) + public override void NextBytes(byte[] buf, int off, int len) { if (first) { - base.NextBytes(bytes); + base.NextBytes(buf, off, len); first = false; } else { - bytes[bytes.Length - 1] = 2; + buf[off + len - 1] = 2; } } } diff --git a/crypto/test/src/crypto/test/GOST3410Test.cs b/crypto/test/src/crypto/test/GOST3410Test.cs index 93c234ca1..130fdaf92 100644 --- a/crypto/test/src/crypto/test/GOST3410Test.cs +++ b/crypto/test/src/crypto/test/GOST3410Test.cs @@ -239,7 +239,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-TEST1-1024"; } } - private class SecureRandomImpl1 : SecureRandom + private class SecureRandomImpl1 : SecureRandomImpl { bool firstInt = true; @@ -266,7 +266,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl1(); - private class SecureRandomImpl2 : SecureRandom + private class SecureRandomImpl2 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -291,7 +291,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl2(); - private class SecureRandomImpl3 : SecureRandom + private class SecureRandomImpl3 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -386,7 +386,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-TEST2-1024"; } } - private class SecureRandomImpl4 : SecureRandom + private class SecureRandomImpl4 : SecureRandomImpl { bool firstLong = true; @@ -413,7 +413,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl4(); - private class SecureRandomImpl5 : SecureRandom + private class SecureRandomImpl5 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -438,7 +438,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl5(); - private class SecureRandomImpl6 : SecureRandom + private class SecureRandomImpl6 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -533,7 +533,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-AParam"; } } - private class SecureRandomImpl7 : SecureRandom + private class SecureRandomImpl7 : SecureRandomImpl { bool firstLong = true; @@ -560,7 +560,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl7(); - private class SecureRandomImpl8 : SecureRandom + private class SecureRandomImpl8 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -585,7 +585,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl8(); - private class SecureRandomImpl9 : SecureRandom + private class SecureRandomImpl9 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -680,7 +680,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-BParam"; } } - private class SecureRandomImpl10 : SecureRandom + private class SecureRandomImpl10 : SecureRandomImpl { bool firstLong = true; @@ -706,7 +706,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl10(); - private class SecureRandomImpl11 : SecureRandom + private class SecureRandomImpl11 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -731,7 +731,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl11(); - private class SecureRandomImpl12 : SecureRandom + private class SecureRandomImpl12 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -826,7 +826,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-CParam"; } } - private class SecureRandomImpl13 : SecureRandom + private class SecureRandomImpl13 : SecureRandomImpl { bool firstLong = true; @@ -852,7 +852,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl13(); - private class SecureRandomImpl14 : SecureRandom + private class SecureRandomImpl14 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -877,7 +877,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl14(); - private class SecureRandomImpl15 : SecureRandom + private class SecureRandomImpl15 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -972,7 +972,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-DParam"; } } - private class SecureRandomImpl16 : SecureRandom + private class SecureRandomImpl16 : SecureRandomImpl { bool firstLong = true; @@ -999,7 +999,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl16(); - private class SecureRandomImpl17 : SecureRandom + private class SecureRandomImpl17 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1024,7 +1024,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl17(); - private class SecureRandomImpl18 : SecureRandom + private class SecureRandomImpl18 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1119,7 +1119,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-AExParam"; } } - private class SecureRandomImpl19 : SecureRandom + private class SecureRandomImpl19 : SecureRandomImpl { bool firstLong = true; @@ -1145,7 +1145,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl19(); - private class SecureRandomImpl20 : SecureRandom + private class SecureRandomImpl20 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1170,7 +1170,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl20(); - private class SecureRandomImpl21 : SecureRandom + private class SecureRandomImpl21 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1265,7 +1265,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-BExParam"; } } - private class SecureRandomImpl22 : SecureRandom + private class SecureRandomImpl22 : SecureRandomImpl { bool firstLong = true; @@ -1291,7 +1291,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl22(); - private class SecureRandomImpl23 : SecureRandom + private class SecureRandomImpl23 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1316,7 +1316,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl23(); - private class SecureRandomImpl24 : SecureRandom + private class SecureRandomImpl24 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1411,7 +1411,7 @@ namespace Org.BouncyCastle.Crypto.Tests get { return "Gost3410-CExParam"; } } - private class SecureRandomImpl25 : SecureRandom + private class SecureRandomImpl25 : SecureRandomImpl { bool firstLong = true; @@ -1437,7 +1437,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom init_random = new SecureRandomImpl25(); - private class SecureRandomImpl26 : SecureRandom + private class SecureRandomImpl26 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1462,7 +1462,7 @@ namespace Org.BouncyCastle.Crypto.Tests }; SecureRandom random = new SecureRandomImpl26(); - private class SecureRandomImpl27 : SecureRandom + private class SecureRandomImpl27 : SecureRandomImpl { public override void NextBytes(byte[] bytes) { @@ -1591,5 +1591,15 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.AreEqual(Name + ": Okay", resultText); } + + internal class SecureRandomImpl : SecureRandom + { + public override void NextBytes(byte[] buf, int off, int len) + { + byte[] bytes = new byte[len]; + NextBytes(bytes); + bytes.CopyTo(buf, off); + } + } } } diff --git a/crypto/test/src/crypto/test/OAEPTest.cs b/crypto/test/src/crypto/test/OAEPTest.cs index 37faff740..b2cbde8b3 100644 --- a/crypto/test/src/crypto/test/OAEPTest.cs +++ b/crypto/test/src/crypto/test/OAEPTest.cs @@ -293,10 +293,14 @@ namespace Org.BouncyCastle.Crypto.Tests this.seed = seed; } - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - Array.Copy(seed, 0, bytes, 0, bytes.Length); + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { + Array.Copy(seed, 0, buf, off, len); } } diff --git a/crypto/test/src/crypto/test/PSSBlindTest.cs b/crypto/test/src/crypto/test/PSSBlindTest.cs index 0abb8d87d..1cd74bb70 100644 --- a/crypto/test/src/crypto/test/PSSBlindTest.cs +++ b/crypto/test/src/crypto/test/PSSBlindTest.cs @@ -36,10 +36,14 @@ namespace Org.BouncyCastle.Crypto.Tests this.vals = vals; } - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - Array.Copy(vals, 0, bytes, 0, vals.Length); + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { + Array.Copy(vals, 0, buf, off, len); } } diff --git a/crypto/test/src/crypto/test/PSSTest.cs b/crypto/test/src/crypto/test/PSSTest.cs index 6375269a4..aeaf85a53 100644 --- a/crypto/test/src/crypto/test/PSSTest.cs +++ b/crypto/test/src/crypto/test/PSSTest.cs @@ -32,10 +32,14 @@ namespace Org.BouncyCastle.Crypto.Tests this.vals = vals; } - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - Array.Copy(vals, 0, bytes, 0, vals.Length); + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { + Array.Copy(vals, 0, buf, off, len); } } diff --git a/crypto/test/src/crypto/test/RC2WrapTest.cs b/crypto/test/src/crypto/test/RC2WrapTest.cs index 9471ba6c9..7d98ee9af 100644 --- a/crypto/test/src/crypto/test/RC2WrapTest.cs +++ b/crypto/test/src/crypto/test/RC2WrapTest.cs @@ -22,10 +22,14 @@ namespace Org.BouncyCastle.Crypto.Tests private class RFCRandom : SecureRandom { - public override void NextBytes( - byte[] nextBytes) + public override void NextBytes(byte[] buf) { - Array.Copy(Hex.Decode("4845cce7fd1250"), 0, nextBytes, 0, nextBytes.Length); + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { + Array.Copy(Hex.Decode("4845cce7fd1250"), 0, buf, off, len); } } diff --git a/crypto/test/src/pqc/crypto/lms/HSSTests.cs b/crypto/test/src/pqc/crypto/lms/HSSTests.cs index 0d01e5d1f..2045f7693 100644 --- a/crypto/test/src/pqc/crypto/lms/HSSTests.cs +++ b/crypto/test/src/pqc/crypto/lms/HSSTests.cs @@ -731,15 +731,20 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms class HSSSecureRandom : SecureRandom { - public override void NextBytes(byte[] bytes) + public override void NextBytes(byte[] buf) { - for (int t = 0; t < bytes.Length; t++) + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { + for (int t = 0; t < len; t++) { - bytes[t] = 1; + buf[off + t] = 1; } } } - + [Test] public void TestSignUnitExhaustion() { diff --git a/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs b/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs index 0ca8dd990..aac73c4ee 100644 --- a/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs +++ b/crypto/test/src/pqc/crypto/test/NistSecureRandom.cs @@ -69,14 +69,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests reseed_counuter = 1; } - public override void NextBytes(byte[] x) + public override void NextBytes(byte[] buf) + { + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) { byte[] block = new byte[16]; int i = 0; - int xlen = x.Length; - - while (xlen > 0) + while (len > 0) { for (int j = 15; j >= 0; j--) { @@ -93,16 +96,16 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests AES256_ECB(key, v, block, 0); - if (xlen > 15) + if (len > 15) { - Array.Copy(block, 0, x, i, block.Length); + Array.Copy(block, 0, buf, off + i, block.Length); i += 16; - xlen -= 16; + len -= 16; } else { - Array.Copy(block, 0, x, i, xlen); - xlen = 0; + Array.Copy(block, 0, buf, off + i, len); + len = 0; } } diff --git a/crypto/test/src/test/BlockCipherTest.cs b/crypto/test/src/test/BlockCipherTest.cs index b57d62d94..d0e5e20b3 100644 --- a/crypto/test/src/test/BlockCipherTest.cs +++ b/crypto/test/src/test/BlockCipherTest.cs @@ -402,18 +402,21 @@ namespace Org.BouncyCastle.Tests (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f }; - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - int offset = 0; + NextBytes(buf, 0, buf.Length); + } - while ((offset + seed.Length) < bytes.Length) + public override void NextBytes(byte[] buf, int off, int len) + { + int pos = 0; + while ((pos + seed.Length) < len) { - Array.Copy(seed, 0, bytes, offset, seed.Length); - offset += seed.Length; + Array.Copy(seed, 0, buf, off + pos, seed.Length); + pos += seed.Length; } - Array.Copy(seed, 0, bytes, offset, bytes.Length- offset); + Array.Copy(seed, 0, buf, off + pos, len - pos); } } diff --git a/crypto/test/src/test/DESedeTest.cs b/crypto/test/src/test/DESedeTest.cs index 04d8725ae..39d55e424 100644 --- a/crypto/test/src/test/DESedeTest.cs +++ b/crypto/test/src/test/DESedeTest.cs @@ -46,25 +46,28 @@ namespace Org.BouncyCastle.Tests : SecureRandom { private byte[] seed = - { - (byte)0xaa, (byte)0xfd, (byte)0x12, (byte)0xf6, (byte)0x59, - (byte)0xca, (byte)0xe6, (byte)0x34, (byte)0x89, (byte)0xb4, - (byte)0x79, (byte)0xe5, (byte)0x07, (byte)0x6d, (byte)0xde, - (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f - }; + { + (byte)0xaa, (byte)0xfd, (byte)0x12, (byte)0xf6, (byte)0x59, + (byte)0xca, (byte)0xe6, (byte)0x34, (byte)0x89, (byte)0xb4, + (byte)0x79, (byte)0xe5, (byte)0x07, (byte)0x6d, (byte)0xde, + (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f + }; - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - int offset = 0; + NextBytes(buf, 0, buf.Length); + } - while ((offset + seed.Length) < bytes.Length) + public override void NextBytes(byte[] buf, int off, int len) + { + int pos = 0; + while ((pos + seed.Length) < len) { - Array.Copy(seed, 0, bytes, offset, seed.Length); - offset += seed.Length; + Array.Copy(seed, 0, buf, off + pos, seed.Length); + pos += seed.Length; } - Array.Copy(seed, 0, bytes, offset, bytes.Length - offset); + Array.Copy(seed, 0, buf, off + pos, len - pos); } } diff --git a/crypto/test/src/test/DSATest.cs b/crypto/test/src/test/DSATest.cs index b4868c52f..95b46c2bf 100644 --- a/crypto/test/src/test/DSATest.cs +++ b/crypto/test/src/test/DSATest.cs @@ -829,16 +829,21 @@ namespace Org.BouncyCastle.Tests { } - public override void NextBytes(byte[] bytes) + public override void NextBytes(byte[] buf) + { + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) { if (first) { - base.NextBytes(bytes); + base.NextBytes(buf, off, len); first = false; } else { - bytes[bytes.Length - 1] = 2; + buf[off + len - 1] = 2; } } } diff --git a/crypto/test/src/test/PSSTest.cs b/crypto/test/src/test/PSSTest.cs index ab8f0f690..fa6f54e31 100644 --- a/crypto/test/src/test/PSSTest.cs +++ b/crypto/test/src/test/PSSTest.cs @@ -33,10 +33,17 @@ namespace Org.BouncyCastle.Tests this.vals = vals; } - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - vals.CopyTo(bytes, 0); + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) + { + if (vals.Length > len) + throw new InvalidOperationException(); + + vals.CopyTo(buf, off); } } diff --git a/crypto/test/src/test/RSATest.cs b/crypto/test/src/test/RSATest.cs index 267472d91..94c1fb816 100644 --- a/crypto/test/src/test/RSATest.cs +++ b/crypto/test/src/test/RSATest.cs @@ -39,18 +39,21 @@ namespace Org.BouncyCastle.Tests (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f }; - public override void NextBytes( - byte[] bytes) + public override void NextBytes(byte[] buf) { - int offset = 0; + NextBytes(buf, 0, buf.Length); + } - while ((offset + seed.Length) < bytes.Length) + public override void NextBytes(byte[] buf, int off, int len) + { + int pos = 0; + while ((pos + seed.Length) < len) { - seed.CopyTo(bytes, offset); - offset += seed.Length; + seed.CopyTo(buf, off + pos); + pos += seed.Length; } - Array.Copy(seed, 0, bytes, offset, bytes.Length - offset); + Array.Copy(seed, 0, buf, off + pos, len - pos); } } diff --git a/crypto/test/src/util/test/FixedSecureRandom.cs b/crypto/test/src/util/test/FixedSecureRandom.cs index 682b3eefa..be5b25347 100644 --- a/crypto/test/src/util/test/FixedSecureRandom.cs +++ b/crypto/test/src/util/test/FixedSecureRandom.cs @@ -211,21 +211,15 @@ namespace Org.BouncyCastle.Utilities.Test public override byte[] GenerateSeed(int numBytes) { - return SecureRandom.GetNextBytes(this, numBytes); + return GetNextBytes(this, numBytes); } - public override void NextBytes( - byte[] buf) + public override void NextBytes(byte[] buf) { - Array.Copy(_data, _index, buf, 0, buf.Length); - - _index += buf.Length; + NextBytes(buf, 0, buf.Length); } - public override void NextBytes( - byte[] buf, - int off, - int len) + public override void NextBytes(byte[] buf, int off, int len) { Array.Copy(_data, _index, buf, off, len); @@ -243,11 +237,16 @@ namespace Org.BouncyCastle.Utilities.Test byte[] data = Hex.Decode("01020304ffffffff0506070811111111"); int index = 0; - public override void NextBytes(byte[] bytes) + public override void NextBytes(byte[] buf) + { + NextBytes(buf, 0, buf.Length); + } + + public override void NextBytes(byte[] buf, int off, int len) { - Array.Copy(data, index, bytes, 0, bytes.Length); + Array.Copy(data, index, buf, off, len); - index += bytes.Length; + index += len; } } |