1*d3273b5bSchristos /* $NetBSD: test_rand.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2007 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ca1c9b0cSelric *
10ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
11ca1c9b0cSelric * modification, are permitted provided that the following conditions
12ca1c9b0cSelric * are met:
13ca1c9b0cSelric *
14ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
15ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
16ca1c9b0cSelric *
17ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
18ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
19ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
20ca1c9b0cSelric *
21ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
22ca1c9b0cSelric * may be used to endorse or promote products derived from this software
23ca1c9b0cSelric * without specific prior written permission.
24ca1c9b0cSelric *
25ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ca1c9b0cSelric * SUCH DAMAGE.
36ca1c9b0cSelric */
37ca1c9b0cSelric
38ca1c9b0cSelric #include <config.h>
39ca1c9b0cSelric #include <krb5/roken.h>
40b9d004c6Schristos #include <math.h>
41b9d004c6Schristos
42ca1c9b0cSelric #include <krb5/getarg.h>
43ca1c9b0cSelric
44ca1c9b0cSelric #include "rand.h"
45ca1c9b0cSelric
46ca1c9b0cSelric
47ca1c9b0cSelric /*
48ca1c9b0cSelric *
49ca1c9b0cSelric */
50ca1c9b0cSelric
51ca1c9b0cSelric static int version_flag;
52ca1c9b0cSelric static int help_flag;
53ca1c9b0cSelric static int len = 1024 * 1024;
54ca1c9b0cSelric static char *rand_method;
55ca1c9b0cSelric static char *filename;
56ca1c9b0cSelric
57ca1c9b0cSelric static struct getargs args[] = {
58ca1c9b0cSelric { "length", 0, arg_integer, &len,
59ca1c9b0cSelric "length", NULL },
60ca1c9b0cSelric { "file", 0, arg_string, &filename,
61ca1c9b0cSelric "file name", NULL },
62ca1c9b0cSelric { "method", 0, arg_string, &rand_method,
63ca1c9b0cSelric "method", NULL },
64ca1c9b0cSelric { "version", 0, arg_flag, &version_flag,
65ca1c9b0cSelric "print version", NULL },
66ca1c9b0cSelric { "help", 0, arg_flag, &help_flag,
67ca1c9b0cSelric NULL, NULL }
68ca1c9b0cSelric };
69ca1c9b0cSelric
70ca1c9b0cSelric /*
71ca1c9b0cSelric *
72ca1c9b0cSelric */
73ca1c9b0cSelric
74ca1c9b0cSelric /*
75ca1c9b0cSelric *
76ca1c9b0cSelric */
77ca1c9b0cSelric
78ca1c9b0cSelric static void
usage(int ret)79ca1c9b0cSelric usage (int ret)
80ca1c9b0cSelric {
81ca1c9b0cSelric arg_printusage (args,
82ca1c9b0cSelric sizeof(args)/sizeof(args[0]),
83ca1c9b0cSelric NULL,
84ca1c9b0cSelric "");
85ca1c9b0cSelric exit (ret);
86ca1c9b0cSelric }
87ca1c9b0cSelric
88ca1c9b0cSelric int
main(int argc,char ** argv)89ca1c9b0cSelric main(int argc, char **argv)
90ca1c9b0cSelric {
91ca1c9b0cSelric int idx = 0;
92ca1c9b0cSelric char *buffer;
93ca1c9b0cSelric char path[MAXPATHLEN];
94ca1c9b0cSelric
95ca1c9b0cSelric setprogname(argv[0]);
96ca1c9b0cSelric
97ca1c9b0cSelric if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
98ca1c9b0cSelric usage(1);
99ca1c9b0cSelric
100ca1c9b0cSelric if (help_flag)
101ca1c9b0cSelric usage(0);
102ca1c9b0cSelric
103ca1c9b0cSelric if(version_flag){
104ca1c9b0cSelric print_version(NULL);
105ca1c9b0cSelric exit(0);
106ca1c9b0cSelric }
107ca1c9b0cSelric
108b9d004c6Schristos if (argc != idx)
109ca1c9b0cSelric usage(1);
110ca1c9b0cSelric
111ca1c9b0cSelric buffer = emalloc(len);
112ca1c9b0cSelric
113ca1c9b0cSelric if (rand_method) {
114ca1c9b0cSelric if (0) {
115ca1c9b0cSelric }
116ca1c9b0cSelric #ifndef NO_RAND_FORTUNA_METHOD
117ca1c9b0cSelric else if (strcasecmp(rand_method, "fortuna") == 0)
118ca1c9b0cSelric RAND_set_rand_method(RAND_fortuna_method());
119ca1c9b0cSelric #endif
120ca1c9b0cSelric #ifndef NO_RAND_UNIX_METHOD
121ca1c9b0cSelric else if (strcasecmp(rand_method, "unix") == 0)
122ca1c9b0cSelric RAND_set_rand_method(RAND_unix_method());
123ca1c9b0cSelric #endif
124ca1c9b0cSelric #ifdef WIN32
125ca1c9b0cSelric else if (strcasecmp(rand_method, "w32crypto") == 0)
126ca1c9b0cSelric RAND_set_rand_method(RAND_w32crypto_method());
127ca1c9b0cSelric #endif
128ca1c9b0cSelric else
129ca1c9b0cSelric errx(1, "unknown method %s", rand_method);
130ca1c9b0cSelric }
131ca1c9b0cSelric
132ca1c9b0cSelric if (RAND_file_name(path, sizeof(path)) == NULL)
133ca1c9b0cSelric errx(1, "RAND_file_name failed");
134ca1c9b0cSelric
135ca1c9b0cSelric if (RAND_status() != 1)
136ca1c9b0cSelric errx(1, "random not ready yet");
137ca1c9b0cSelric
138ca1c9b0cSelric if (RAND_bytes(buffer, len) != 1)
139ca1c9b0cSelric errx(1, "RAND_bytes");
140ca1c9b0cSelric
141ca1c9b0cSelric if (filename)
142ca1c9b0cSelric rk_dumpdata(filename, buffer, len);
143ca1c9b0cSelric
144ca1c9b0cSelric /* head vs tail */
145ca1c9b0cSelric if (len >= 100000) {
146b9d004c6Schristos unsigned bytes[256];
147b9d004c6Schristos unsigned bits[8];
148b9d004c6Schristos size_t bit, i;
149ca1c9b0cSelric double res;
150b9d004c6Schristos double slen = sqrt((double)len);
151b9d004c6Schristos
152b9d004c6Schristos memset(bits, 0, sizeof(bits));
153b9d004c6Schristos memset(bytes, 0, sizeof(bytes));
154ca1c9b0cSelric
155ca1c9b0cSelric for (i = 0; i < len; i++) {
156ca1c9b0cSelric unsigned char c = ((unsigned char *)buffer)[i];
157b9d004c6Schristos
158b9d004c6Schristos bytes[c]++;
159b9d004c6Schristos
160ca1c9b0cSelric for (bit = 0; bit < 8 && c; bit++) {
161ca1c9b0cSelric if (c & 1)
162ca1c9b0cSelric bits[bit]++;
163ca1c9b0cSelric c = c >> 1;
164ca1c9b0cSelric }
165ca1c9b0cSelric }
166ca1c9b0cSelric
167b9d004c6Schristos /*
168b9d004c6Schristos * The count for each bit value has a mean of n*p = len/2,
169b9d004c6Schristos * and a standard deviation of sqrt(n*p*q) ~ sqrt(len/4).
170b9d004c6Schristos * Normalizing by dividing by "n*p", we get a mean of 1 and
171b9d004c6Schristos * a standard deviation of sqrt(q/n*p) = 1/sqrt(len).
172b9d004c6Schristos *
173b9d004c6Schristos * A 5.33-sigma event happens 1 time in 10 million.
174b9d004c6Schristos * A 5.73-sigma event happens 1 time in 100 million.
175b9d004c6Schristos * A 6.11-sigma event happens 1 time in 1000 million.
176b9d004c6Schristos *
177b9d004c6Schristos * We tolerate 5.33-sigma events (we have 8 not entirely
178b9d004c6Schristos * independent chances of skewed results) and want to fail
179b9d004c6Schristos * with a good RNG less often than 1 time in million.
180b9d004c6Schristos */
181ca1c9b0cSelric for (bit = 0; bit < 8; bit++) {
182b9d004c6Schristos res = slen * fabs(1.0 - 2 * (double)bits[bit] / len);
183b9d004c6Schristos if (res > 5.33)
184b9d004c6Schristos errx(1, "head%d vs tail%d: %.1f-sigma (%d of %d)",
185b9d004c6Schristos (int)bit, (int)bit, res, bits[bit], len);
186b9d004c6Schristos printf("head vs tails bit%d: %f-sigma\n", (int)bit, res);
187b9d004c6Schristos }
188ca1c9b0cSelric
189b9d004c6Schristos /*
190b9d004c6Schristos * The count of each byte value has a mean of n*p = len/256,
191b9d004c6Schristos * and a standard deviation of sqrt(n*p*q) ~ sqrt(len/256).
192b9d004c6Schristos * Normalizing by dividing by "n*p", we get a mean of 1 and
193b9d004c6Schristos * a standard deviation of sqrt(q/n*p) ~ 16/sqrt(len).
194b9d004c6Schristos *
195b9d004c6Schristos * We tolerate 5.73-sigma events (we have 256 not entirely
196b9d004c6Schristos * independent chances of skewed results). Note, for example,
197b9d004c6Schristos * a 5.2-sigma event was observed in ~5,000 runs.
198b9d004c6Schristos */
199b9d004c6Schristos for (i = 0; i < 256; i++) {
200b9d004c6Schristos res = (slen / 16) * fabs(1.0 - 256 * (double)bytes[i] / len);
201b9d004c6Schristos if (res > 5.73)
202b9d004c6Schristos errx(1, "byte %d: %.1f-sigma (%d of %d)",
203b9d004c6Schristos (int) i, res, bytes[i], len);
204b9d004c6Schristos printf("byte %d: %f-sigma\n", (int)i, res);
205ca1c9b0cSelric }
206ca1c9b0cSelric }
207ca1c9b0cSelric
208ca1c9b0cSelric free(buffer);
209ca1c9b0cSelric
210ca1c9b0cSelric /* test write random file */
211ca1c9b0cSelric {
212ca1c9b0cSelric static const char *file = "test.file";
213ca1c9b0cSelric if (RAND_write_file(file) != 1)
214ca1c9b0cSelric errx(1, "RAND_write_file");
215ca1c9b0cSelric if (RAND_load_file(file, 1024) != 1)
216ca1c9b0cSelric errx(1, "RAND_load_file");
217ca1c9b0cSelric unlink(file);
218ca1c9b0cSelric }
219ca1c9b0cSelric
220ca1c9b0cSelric return 0;
221ca1c9b0cSelric }
222