randtest.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/rand.h>
  12. #include "../e_os.h"
  13. /* some FIPS 140-1 random number test */
  14. /* some simple tests */
  15. int main(int argc, char **argv)
  16. {
  17. unsigned char buf[2500];
  18. int i, j, k, s, sign, nsign, err = 0;
  19. unsigned long n1;
  20. unsigned long n2[16];
  21. unsigned long runs[2][34];
  22. /*
  23. * double d;
  24. */
  25. long d;
  26. i = RAND_bytes(buf, 2500);
  27. if (i <= 0) {
  28. printf("init failed, the rand method is not properly installed\n");
  29. err++;
  30. goto err;
  31. }
  32. n1 = 0;
  33. for (i = 0; i < 16; i++)
  34. n2[i] = 0;
  35. for (i = 0; i < 34; i++)
  36. runs[0][i] = runs[1][i] = 0;
  37. /* test 1 and 2 */
  38. sign = 0;
  39. nsign = 0;
  40. for (i = 0; i < 2500; i++) {
  41. j = buf[i];
  42. n2[j & 0x0f]++;
  43. n2[(j >> 4) & 0x0f]++;
  44. for (k = 0; k < 8; k++) {
  45. s = (j & 0x01);
  46. if (s == sign)
  47. nsign++;
  48. else {
  49. if (nsign > 34)
  50. nsign = 34;
  51. if (nsign != 0) {
  52. runs[sign][nsign - 1]++;
  53. if (nsign > 6)
  54. runs[sign][5]++;
  55. }
  56. sign = s;
  57. nsign = 1;
  58. }
  59. if (s)
  60. n1++;
  61. j >>= 1;
  62. }
  63. }
  64. if (nsign > 34)
  65. nsign = 34;
  66. if (nsign != 0)
  67. runs[sign][nsign - 1]++;
  68. /* test 1 */
  69. if (!((9654 < n1) && (n1 < 10346))) {
  70. printf("test 1 failed, X=%lu\n", n1);
  71. err++;
  72. }
  73. printf("test 1 done\n");
  74. /* test 2 */
  75. d = 0;
  76. for (i = 0; i < 16; i++)
  77. d += n2[i] * n2[i];
  78. d = (d * 8) / 25 - 500000;
  79. if (!((103 < d) && (d < 5740))) {
  80. printf("test 2 failed, X=%ld.%02ld\n", d / 100L, d % 100L);
  81. err++;
  82. }
  83. printf("test 2 done\n");
  84. /* test 3 */
  85. for (i = 0; i < 2; i++) {
  86. if (!((2267 < runs[i][0]) && (runs[i][0] < 2733))) {
  87. printf("test 3 failed, bit=%d run=%d num=%lu\n",
  88. i, 1, runs[i][0]);
  89. err++;
  90. }
  91. if (!((1079 < runs[i][1]) && (runs[i][1] < 1421))) {
  92. printf("test 3 failed, bit=%d run=%d num=%lu\n",
  93. i, 2, runs[i][1]);
  94. err++;
  95. }
  96. if (!((502 < runs[i][2]) && (runs[i][2] < 748))) {
  97. printf("test 3 failed, bit=%d run=%d num=%lu\n",
  98. i, 3, runs[i][2]);
  99. err++;
  100. }
  101. if (!((223 < runs[i][3]) && (runs[i][3] < 402))) {
  102. printf("test 3 failed, bit=%d run=%d num=%lu\n",
  103. i, 4, runs[i][3]);
  104. err++;
  105. }
  106. if (!((90 < runs[i][4]) && (runs[i][4] < 223))) {
  107. printf("test 3 failed, bit=%d run=%d num=%lu\n",
  108. i, 5, runs[i][4]);
  109. err++;
  110. }
  111. if (!((90 < runs[i][5]) && (runs[i][5] < 223))) {
  112. printf("test 3 failed, bit=%d run=%d num=%lu\n",
  113. i, 6, runs[i][5]);
  114. err++;
  115. }
  116. }
  117. printf("test 3 done\n");
  118. /* test 4 */
  119. if (runs[0][33] != 0) {
  120. printf("test 4 failed, bit=%d run=%d num=%lu\n", 0, 34, runs[0][33]);
  121. err++;
  122. }
  123. if (runs[1][33] != 0) {
  124. printf("test 4 failed, bit=%d run=%d num=%lu\n", 1, 34, runs[1][33]);
  125. err++;
  126. }
  127. printf("test 4 done\n");
  128. err:
  129. err = ((err) ? 1 : 0);
  130. EXIT(err);
  131. }