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
10 #include <openssl/rand.h>
13 /* some FIPS 140-1 random number test */
14 /* some simple tests */
16 static int fips_random_tests(void)
18 unsigned char buf[2500];
19 int i, j, k, s, sign, nsign, ret = 1;
22 unsigned long runs[2][34];
25 if (!TEST_int_ge(RAND_bytes(buf, sizeof(buf)), 0))
29 for (i = 0; i < 16; i++)
31 for (i = 0; i < 34; i++)
32 runs[0][i] = runs[1][i] = 0;
37 for (i = 0; i < 2500; i++) {
41 n2[(j >> 4) & 0x0f]++;
43 for (k = 0; k < 8; k++) {
51 runs[sign][nsign - 1]++;
67 runs[sign][nsign - 1]++;
70 if (!TEST_true(9654 < n1 && n1 < 10346)) {
71 TEST_info("test 1 failed, X=%lu", n1);
77 for (i = 0; i < 16; i++)
79 d = (d * 8) / 25 - 500000;
80 if (!TEST_true(103 < d && d < 5740)) {
81 TEST_info("test 2 failed, X=%ld.%02ld", d / 100L, d % 100L);
86 for (i = 0; i < 2; i++) {
87 if (!TEST_true(2267 < runs[i][0] && runs[i][0] < 2733)
88 || !TEST_true(1079 < runs[i][1] && runs[i][1] < 1421)
89 || !TEST_true(502 < runs[i][2] && runs[i][2] < 748)
90 || !TEST_true(223 < runs[i][3] && runs[i][3] < 402)
91 || !TEST_true(90 < runs[i][4] && runs[i][4] < 223)
92 || !TEST_true(90 < runs[i][5] && runs[i][5] < 223)) {
93 TEST_info("During run %d", i);
99 if (!TEST_int_eq(runs[0][33], 0)
100 || !TEST_int_eq(runs[1][33], 0))
106 void register_tests(void)
108 ADD_TEST(fips_random_tests);