2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/rand.h>
16 /* some FIPS 140-1 random number test */
17 /* some simple tests */
19 int main(int argc, char **argv)
21 unsigned char buf[2500];
22 int i, j, k, s, sign, nsign, err = 0;
25 unsigned long runs[2][34];
31 i = RAND_bytes(buf, 2500);
33 printf("init failed, the rand method is not properly installed\n");
39 for (i = 0; i < 16; i++)
41 for (i = 0; i < 34; i++)
42 runs[0][i] = runs[1][i] = 0;
47 for (i = 0; i < 2500; i++) {
51 n2[(j >> 4) & 0x0f]++;
53 for (k = 0; k < 8; k++) {
61 runs[sign][nsign - 1]++;
77 runs[sign][nsign - 1]++;
80 if (!((9654 < n1) && (n1 < 10346))) {
81 printf("test 1 failed, X=%lu\n", n1);
84 printf("test 1 done\n");
88 for (i = 0; i < 16; i++)
90 d = (d * 8) / 25 - 500000;
91 if (!((103 < d) && (d < 5740))) {
92 printf("test 2 failed, X=%ld.%02ld\n", d / 100L, d % 100L);
95 printf("test 2 done\n");
98 for (i = 0; i < 2; i++) {
99 if (!((2267 < runs[i][0]) && (runs[i][0] < 2733))) {
100 printf("test 3 failed, bit=%d run=%d num=%lu\n",
104 if (!((1079 < runs[i][1]) && (runs[i][1] < 1421))) {
105 printf("test 3 failed, bit=%d run=%d num=%lu\n",
109 if (!((502 < runs[i][2]) && (runs[i][2] < 748))) {
110 printf("test 3 failed, bit=%d run=%d num=%lu\n",
114 if (!((223 < runs[i][3]) && (runs[i][3] < 402))) {
115 printf("test 3 failed, bit=%d run=%d num=%lu\n",
119 if (!((90 < runs[i][4]) && (runs[i][4] < 223))) {
120 printf("test 3 failed, bit=%d run=%d num=%lu\n",
124 if (!((90 < runs[i][5]) && (runs[i][5] < 223))) {
125 printf("test 3 failed, bit=%d run=%d num=%lu\n",
130 printf("test 3 done\n");
133 if (runs[0][33] != 0) {
134 printf("test 4 failed, bit=%d run=%d num=%lu\n", 0, 34, runs[0][33]);
137 if (runs[1][33] != 0) {
138 printf("test 4 failed, bit=%d run=%d num=%lu\n", 1, 34, runs[1][33]);
141 printf("test 4 done\n");
143 err = ((err) ? 1 : 0);