RandomNumberGeneratorTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // RandomNumberGeneratorTest.cs - NUnit Test Cases for RNG
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.IO;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. namespace MonoTests.System.Security.Cryptography {
  15. // References:
  16. // a. NIST FIPS PUB 140-2: Security requirements for Cryptographic Modules
  17. // http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf
  18. // b. NIST SP 800-22: A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications
  19. // not implemented
  20. // http://csrc.nist.gov/publications/nistpubs/800-22/sp-800-22-051501.pdf
  21. // c. IETF RFC1750: Randomness Recommendations for Security
  22. // not implemented
  23. // http://www.ietf.org/rfc/rfc1750.txt
  24. public class RandomNumberGeneratorTest : TestCase {
  25. protected RandomNumberGenerator rng;
  26. protected override void SetUp ()
  27. {
  28. rng = RandomNumberGenerator.Create();
  29. }
  30. protected override void TearDown () {}
  31. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  32. {
  33. AllTests.AssertEquals (msg, array1, array2);
  34. }
  35. // count the number of 1
  36. protected void Monobit (string rngName, byte[] sample)
  37. {
  38. int x = 0;
  39. for (int i=0; i < sample.Length; i++) {
  40. byte b = sample[i];
  41. for (int j = 0; j < 8; j++) {
  42. if ((b & 0x01) == 0x01)
  43. x++;
  44. // next bit
  45. b >>= 1;
  46. }
  47. }
  48. Assert (rngName + " Monobit x=" + x, ((9725 < x) && (x < 10275)));
  49. }
  50. // 16 patterns (nibbles)
  51. protected void Poker (string rngName, byte[] sample)
  52. {
  53. int[] pattern = new int[16];
  54. for (int i = 0; i < sample.Length; i++) {
  55. byte b = sample[i];
  56. int n = (b & 0x0F);
  57. pattern[n]++;
  58. b >>= 4;
  59. n = b;
  60. pattern[n]++;
  61. }
  62. double result = 0;
  63. for (int i = 0; i < 16; i++)
  64. result += (pattern[i] * pattern[i]);
  65. result = ((16 * result) / 5000) - 5000;
  66. Assert (rngName + " Poker: " + result, ((result > 2.16) && (result < 46.17)));
  67. }
  68. // runs of 1 (or 0)
  69. protected void Runs (string rngName, byte[] sample)
  70. {
  71. int[] runs = new int[6];
  72. int x = 0;
  73. bool one = false;
  74. bool zero = false;
  75. for (int i = sample.Length - 1; i >= 0 ; i--) {
  76. byte b = sample[i];
  77. for (int j = 0; j < 8; j++) {
  78. if ((b & 0x01) == 0x01) {
  79. if (!one) {
  80. one = true;
  81. zero = false;
  82. int p = Math.Min (x, 6) - 1;
  83. if (p >= 0)
  84. runs[p]++;
  85. x = 0;
  86. }
  87. }
  88. else {
  89. if (!zero) {
  90. one = false;
  91. zero = true;
  92. /*int p = Math.Min (x, 6) - 1;
  93. if (p >= 0)
  94. runs[p]++;*/
  95. x = 0;
  96. }
  97. }
  98. x++;
  99. // next bit
  100. b >>= 1;
  101. }
  102. }
  103. Assert (rngName + " Runs length=1: " + runs[0], ((runs[0] >= 2343) && (runs[0] <= 2657)));
  104. Assert (rngName + " Runs length=2: " + runs[1], ((runs[1] >= 1135) && (runs[1] <= 1365)));
  105. Assert (rngName + " Runs length=3: " + runs[2], ((runs[2] >= 542) && (runs[2] <= 708)));
  106. Assert (rngName + " Runs length=4: " + runs[3], ((runs[3] >= 251) && (runs[3] <= 373)));
  107. Assert (rngName + " Runs length=5: " + runs[4], ((runs[4] >= 111) && (runs[4] <= 201)));
  108. Assert (rngName + " Runs length=6+ " + runs[5], ((runs[5] >= 111) && (runs[5] <= 201)));
  109. }
  110. // no long runs of 26 or more (0 or 1)
  111. protected void LongRuns (string rngName, byte[] sample)
  112. {
  113. int longestRun = 0;
  114. int currentRun = 0;
  115. bool one = false;
  116. bool zero = false;
  117. for (int i = sample.Length - 1; i >= 0 ; i--) {
  118. byte b = sample[i];
  119. for (int j = 0; j < 8; j++) {
  120. if ((b & 0x01) == 0x01) {
  121. if (!one) {
  122. one = true;
  123. zero = false;
  124. longestRun = Math.Max (longestRun, currentRun);
  125. currentRun = 0;
  126. }
  127. currentRun++;
  128. }
  129. else {
  130. if (!zero) {
  131. one = false;
  132. zero = true;
  133. longestRun = Math.Max (longestRun, currentRun);
  134. currentRun = 0;
  135. }
  136. currentRun++;
  137. }
  138. // next bit
  139. b >>= 1;
  140. }
  141. }
  142. Assert (rngName + " Long Runs max = " + longestRun, (longestRun < 26));
  143. }
  144. // all tests should be done on the same random sample
  145. public void TestFIPS140()
  146. {
  147. string name = rng.ToString();
  148. // 20,000 bits
  149. byte[] sample = new byte[2500];
  150. rng.GetBytes (sample);
  151. Monobit (name, sample);
  152. Poker (name, sample);
  153. Runs (name, sample);
  154. LongRuns (name, sample);
  155. }
  156. }
  157. }