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>
12 #include "test_main.h"
14 /* some FIPS 140-1 random number test */
15 /* some simple tests */
17 static int fips_random_tests(void)
19 unsigned char buf[2500];
20 int i, j, k, s, sign, nsign, ret = 1;
23 unsigned long runs[2][34];
26 if (!TEST_int_ge(RAND_bytes(buf, sizeof(buf)), 0))
30 for (i = 0; i < 16; i++)
32 for (i = 0; i < 34; i++)
33 runs[0][i] = runs[1][i] = 0;
38 for (i = 0; i < 2500; i++) {
42 n2[(j >> 4) & 0x0f]++;
44 for (k = 0; k < 8; k++) {
52 runs[sign][nsign - 1]++;
68 runs[sign][nsign - 1]++;
71 if (!TEST_true(9654 < n1 && n1 < 10346)) {
72 TEST_info("test 1 failed, X=%lu", n1);
78 for (i = 0; i < 16; i++)
80 d = (d * 8) / 25 - 500000;
81 if (!TEST_true(103 < d && d < 5740)) {
82 TEST_info("test 2 failed, X=%ld.%02ld", d / 100L, d % 100L);
87 for (i = 0; i < 2; i++) {
88 if (!TEST_true(2267 < runs[i][0] && runs[i][0] < 2733)
89 || !TEST_true(1079 < runs[i][1] && runs[i][1] < 1421)
90 || !TEST_true(502 < runs[i][2] && runs[i][2] < 748)
91 || !TEST_true(223 < runs[i][3] && runs[i][3] < 402)
92 || !TEST_true(90 < runs[i][4] && runs[i][4] < 223)
93 || !TEST_true(90 < runs[i][5] && runs[i][5] < 223)) {
94 TEST_info("During run %d", i);
100 if (!TEST_int_eq(runs[0][33], 0)
101 || !TEST_int_eq(runs[1][33], 0))
107 void register_tests(void)
109 ADD_TEST(fips_random_tests);