Update more tests
[openssl.git] / test / randtest.c
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
10 #include <openssl/rand.h>
11 #include "testutil.h"
12 #include "test_main.h"
13
14 /* some FIPS 140-1 random number test */
15 /* some simple tests */
16
17 static int fips_random_tests(void)
18 {
19     unsigned char buf[2500];
20     int i, j, k, s, sign, nsign, ret = 1;
21     unsigned long n1;
22     unsigned long n2[16];
23     unsigned long runs[2][34];
24     long d;
25
26     if (!TEST_int_ge(RAND_bytes(buf, sizeof(buf)), 0))
27         return 0;
28
29     n1 = 0;
30     for (i = 0; i < 16; i++)
31         n2[i] = 0;
32     for (i = 0; i < 34; i++)
33         runs[0][i] = runs[1][i] = 0;
34
35     /* test 1 and 2 */
36     sign = 0;
37     nsign = 0;
38     for (i = 0; i < 2500; i++) {
39         j = buf[i];
40
41         n2[j & 0x0f]++;
42         n2[(j >> 4) & 0x0f]++;
43
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
60             if (s)
61                 n1++;
62             j >>= 1;
63         }
64     }
65     if (nsign > 34)
66         nsign = 34;
67     if (nsign != 0)
68         runs[sign][nsign - 1]++;
69
70     /* test 1 */
71     if (!TEST_true(9654 < n1 && n1 < 10346)) {
72         TEST_info("test 1 failed, X=%lu", n1);
73         ret = 0;
74     }
75
76     /* test 2 */
77     d = 0;
78     for (i = 0; i < 16; i++)
79         d += n2[i] * n2[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);
83         ret = 0;
84     }
85
86     /* test 3 */
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);
95             ret = 0;
96         }
97     }
98
99     /* test 4 */
100     if (!TEST_int_eq(runs[0][33], 0)
101             || !TEST_int_eq(runs[1][33], 0))
102         ret = 0;
103
104     return ret;
105 }
106
107 void register_tests(void)
108 {
109     ADD_TEST(fips_random_tests);
110 }