]>
Commit | Line | Data |
---|---|---|
440e5d80 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
440e5d80 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
ec577822 | 12 | #include <openssl/rand.h> |
d02b48c6 | 13 | |
55f78baf RL |
14 | #include "../e_os.h" |
15 | ||
d02b48c6 RE |
16 | /* some FIPS 140-1 random number test */ |
17 | /* some simple tests */ | |
18 | ||
0f113f3e MC |
19 | int main(int argc, char **argv) |
20 | { | |
21 | unsigned char buf[2500]; | |
22 | int i, j, k, s, sign, nsign, err = 0; | |
23 | unsigned long n1; | |
24 | unsigned long n2[16]; | |
25 | unsigned long runs[2][34]; | |
26 | /* | |
27 | * double d; | |
28 | */ | |
29 | long d; | |
d02b48c6 | 30 | |
266483d2 MC |
31 | i = RAND_bytes(buf, 2500); |
32 | if (i <= 0) { | |
0f113f3e MC |
33 | printf("init failed, the rand method is not properly installed\n"); |
34 | err++; | |
35 | goto err; | |
36 | } | |
d02b48c6 | 37 | |
0f113f3e MC |
38 | n1 = 0; |
39 | for (i = 0; i < 16; i++) | |
40 | n2[i] = 0; | |
41 | for (i = 0; i < 34; i++) | |
42 | runs[0][i] = runs[1][i] = 0; | |
d02b48c6 | 43 | |
0f113f3e MC |
44 | /* test 1 and 2 */ |
45 | sign = 0; | |
46 | nsign = 0; | |
47 | for (i = 0; i < 2500; i++) { | |
48 | j = buf[i]; | |
d02b48c6 | 49 | |
0f113f3e MC |
50 | n2[j & 0x0f]++; |
51 | n2[(j >> 4) & 0x0f]++; | |
d02b48c6 | 52 | |
0f113f3e MC |
53 | for (k = 0; k < 8; k++) { |
54 | s = (j & 0x01); | |
55 | if (s == sign) | |
56 | nsign++; | |
57 | else { | |
58 | if (nsign > 34) | |
59 | nsign = 34; | |
60 | if (nsign != 0) { | |
61 | runs[sign][nsign - 1]++; | |
62 | if (nsign > 6) | |
63 | runs[sign][5]++; | |
64 | } | |
65 | sign = s; | |
66 | nsign = 1; | |
67 | } | |
d02b48c6 | 68 | |
0f113f3e MC |
69 | if (s) |
70 | n1++; | |
71 | j >>= 1; | |
72 | } | |
73 | } | |
74 | if (nsign > 34) | |
75 | nsign = 34; | |
76 | if (nsign != 0) | |
77 | runs[sign][nsign - 1]++; | |
d02b48c6 | 78 | |
0f113f3e MC |
79 | /* test 1 */ |
80 | if (!((9654 < n1) && (n1 < 10346))) { | |
81 | printf("test 1 failed, X=%lu\n", n1); | |
82 | err++; | |
83 | } | |
84 | printf("test 1 done\n"); | |
d02b48c6 | 85 | |
0f113f3e | 86 | /* test 2 */ |
0f113f3e MC |
87 | d = 0; |
88 | for (i = 0; i < 16; i++) | |
89 | d += n2[i] * n2[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); | |
93 | err++; | |
94 | } | |
95 | printf("test 2 done\n"); | |
96 | ||
97 | /* test 3 */ | |
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", | |
101 | i, 1, runs[i][0]); | |
102 | err++; | |
103 | } | |
104 | if (!((1079 < runs[i][1]) && (runs[i][1] < 1421))) { | |
105 | printf("test 3 failed, bit=%d run=%d num=%lu\n", | |
106 | i, 2, runs[i][1]); | |
107 | err++; | |
108 | } | |
109 | if (!((502 < runs[i][2]) && (runs[i][2] < 748))) { | |
110 | printf("test 3 failed, bit=%d run=%d num=%lu\n", | |
111 | i, 3, runs[i][2]); | |
112 | err++; | |
113 | } | |
114 | if (!((223 < runs[i][3]) && (runs[i][3] < 402))) { | |
115 | printf("test 3 failed, bit=%d run=%d num=%lu\n", | |
116 | i, 4, runs[i][3]); | |
117 | err++; | |
118 | } | |
119 | if (!((90 < runs[i][4]) && (runs[i][4] < 223))) { | |
120 | printf("test 3 failed, bit=%d run=%d num=%lu\n", | |
121 | i, 5, runs[i][4]); | |
122 | err++; | |
123 | } | |
124 | if (!((90 < runs[i][5]) && (runs[i][5] < 223))) { | |
125 | printf("test 3 failed, bit=%d run=%d num=%lu\n", | |
126 | i, 6, runs[i][5]); | |
127 | err++; | |
128 | } | |
129 | } | |
130 | printf("test 3 done\n"); | |
d02b48c6 | 131 | |
0f113f3e MC |
132 | /* test 4 */ |
133 | if (runs[0][33] != 0) { | |
134 | printf("test 4 failed, bit=%d run=%d num=%lu\n", 0, 34, runs[0][33]); | |
135 | err++; | |
136 | } | |
137 | if (runs[1][33] != 0) { | |
138 | printf("test 4 failed, bit=%d run=%d num=%lu\n", 1, 34, runs[1][33]); | |
139 | err++; | |
140 | } | |
141 | printf("test 4 done\n"); | |
5ef67042 | 142 | err: |
0f113f3e | 143 | err = ((err) ? 1 : 0); |
0f113f3e | 144 | EXIT(err); |
0f113f3e | 145 | } |