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