xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/test_rand.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: test_rand.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2007 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc  *    without specific prior written permission.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc  * SUCH DAMAGE.
36ebfedea0SLionel Sambuc  */
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include <config.h>
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc #include <stdio.h>
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc #include <krb5/roken.h>
43ebfedea0SLionel Sambuc #include <krb5/getarg.h>
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc #include "rand.h"
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc /*
49ebfedea0SLionel Sambuc  *
50ebfedea0SLionel Sambuc  */
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc static int version_flag;
53ebfedea0SLionel Sambuc static int help_flag;
54ebfedea0SLionel Sambuc static int len = 1024 * 1024;
55ebfedea0SLionel Sambuc static char *rand_method;
56ebfedea0SLionel Sambuc static char *filename;
57ebfedea0SLionel Sambuc 
58ebfedea0SLionel Sambuc static struct getargs args[] = {
59ebfedea0SLionel Sambuc     { "length",	0,	arg_integer,	&len,
60ebfedea0SLionel Sambuc       "length", NULL },
61ebfedea0SLionel Sambuc     { "file",	0,	arg_string,	&filename,
62ebfedea0SLionel Sambuc       "file name", NULL },
63ebfedea0SLionel Sambuc     { "method",	0,	arg_string,	&rand_method,
64ebfedea0SLionel Sambuc       "method", NULL },
65ebfedea0SLionel Sambuc     { "version",	0,	arg_flag,	&version_flag,
66ebfedea0SLionel Sambuc       "print version", NULL },
67ebfedea0SLionel Sambuc     { "help",		0,	arg_flag,	&help_flag,
68ebfedea0SLionel Sambuc       NULL, 	NULL }
69ebfedea0SLionel Sambuc };
70ebfedea0SLionel Sambuc 
71ebfedea0SLionel Sambuc /*
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  */
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc /*
76ebfedea0SLionel Sambuc  *
77ebfedea0SLionel Sambuc  */
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc static void
usage(int ret)80ebfedea0SLionel Sambuc usage (int ret)
81ebfedea0SLionel Sambuc {
82ebfedea0SLionel Sambuc     arg_printusage (args,
83ebfedea0SLionel Sambuc 		    sizeof(args)/sizeof(args[0]),
84ebfedea0SLionel Sambuc 		    NULL,
85ebfedea0SLionel Sambuc 		    "");
86ebfedea0SLionel Sambuc     exit (ret);
87ebfedea0SLionel Sambuc }
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc int
main(int argc,char ** argv)90ebfedea0SLionel Sambuc main(int argc, char **argv)
91ebfedea0SLionel Sambuc {
92ebfedea0SLionel Sambuc     int idx = 0;
93ebfedea0SLionel Sambuc     char *buffer;
94ebfedea0SLionel Sambuc     char path[MAXPATHLEN];
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc     setprogname(argv[0]);
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
99ebfedea0SLionel Sambuc 	usage(1);
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     if (help_flag)
102ebfedea0SLionel Sambuc 	usage(0);
103ebfedea0SLionel Sambuc 
104ebfedea0SLionel Sambuc     if(version_flag){
105ebfedea0SLionel Sambuc 	print_version(NULL);
106ebfedea0SLionel Sambuc 	exit(0);
107ebfedea0SLionel Sambuc     }
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc     argc -= idx;
110ebfedea0SLionel Sambuc     argv += idx;
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc     if (argc != 0)
113ebfedea0SLionel Sambuc 	usage(1);
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc     buffer = emalloc(len);
116ebfedea0SLionel Sambuc 
117ebfedea0SLionel Sambuc     if (rand_method) {
118ebfedea0SLionel Sambuc 	if (0) {
119ebfedea0SLionel Sambuc 	}
120ebfedea0SLionel Sambuc #ifndef NO_RAND_FORTUNA_METHOD
121ebfedea0SLionel Sambuc 	else if (strcasecmp(rand_method, "fortuna") == 0)
122ebfedea0SLionel Sambuc 	    RAND_set_rand_method(RAND_fortuna_method());
123ebfedea0SLionel Sambuc #endif
124ebfedea0SLionel Sambuc #ifndef NO_RAND_UNIX_METHOD
125ebfedea0SLionel Sambuc 	else if (strcasecmp(rand_method, "unix") == 0)
126ebfedea0SLionel Sambuc 	    RAND_set_rand_method(RAND_unix_method());
127ebfedea0SLionel Sambuc #endif
128ebfedea0SLionel Sambuc #ifndef NO_RAND_EGD_METHOD
129ebfedea0SLionel Sambuc 	else if (strcasecmp(rand_method, "egd") == 0)
130ebfedea0SLionel Sambuc 	    RAND_set_rand_method(RAND_egd_method());
131ebfedea0SLionel Sambuc #endif
132ebfedea0SLionel Sambuc #ifdef WIN32
133ebfedea0SLionel Sambuc 	else if (strcasecmp(rand_method, "w32crypto") == 0)
134ebfedea0SLionel Sambuc 	    RAND_set_rand_method(RAND_w32crypto_method());
135ebfedea0SLionel Sambuc #endif
136ebfedea0SLionel Sambuc 	else
137ebfedea0SLionel Sambuc 	    errx(1, "unknown method %s", rand_method);
138ebfedea0SLionel Sambuc     }
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc     if (RAND_file_name(path, sizeof(path)) == NULL)
141ebfedea0SLionel Sambuc 	errx(1, "RAND_file_name failed");
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc     if (RAND_status() != 1)
144ebfedea0SLionel Sambuc 	errx(1, "random not ready yet");
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc     if (RAND_bytes(buffer, len) != 1)
147ebfedea0SLionel Sambuc 	errx(1, "RAND_bytes");
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc     if (filename)
150ebfedea0SLionel Sambuc 	rk_dumpdata(filename, buffer, len);
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc     /* head vs tail */
153ebfedea0SLionel Sambuc     if (len >= 100000) {
154ebfedea0SLionel Sambuc 	int bit, i;
155ebfedea0SLionel Sambuc 	double res;
156ebfedea0SLionel Sambuc 	int bits[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc 	for (i = 0; i < len; i++) {
159ebfedea0SLionel Sambuc 	    unsigned char c = ((unsigned char *)buffer)[i];
160ebfedea0SLionel Sambuc 	    for (bit = 0; bit < 8 && c; bit++) {
161ebfedea0SLionel Sambuc 		if (c & 1)
162ebfedea0SLionel Sambuc 		    bits[bit]++;
163ebfedea0SLionel Sambuc 		c = c >> 1;
164ebfedea0SLionel Sambuc 	    }
165ebfedea0SLionel Sambuc 	}
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc 	for (bit = 0; bit < 8; bit++) {
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc 	    res = ((double)abs(len - bits[bit] * 2)) / (double)len;
170ebfedea0SLionel Sambuc 	    if (res > 0.005)
171ebfedea0SLionel Sambuc 		errx(1, "head%d vs tail%d > 0.5%%%% %lf == %d vs %d",
172ebfedea0SLionel Sambuc 		     bit, bit, res, len, bits[bit]);
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc 	    printf("head vs tails bit%d: %lf\n", bit, res);
175ebfedea0SLionel Sambuc 	}
176ebfedea0SLionel Sambuc     }
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc     free(buffer);
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc     /* test write random file */
181ebfedea0SLionel Sambuc     {
182ebfedea0SLionel Sambuc 	static const char *file = "test.file";
183ebfedea0SLionel Sambuc 	if (RAND_write_file(file) != 1)
184ebfedea0SLionel Sambuc 	    errx(1, "RAND_write_file");
185ebfedea0SLionel Sambuc 	if (RAND_load_file(file, 1024) != 1)
186ebfedea0SLionel Sambuc 	    errx(1, "RAND_load_file");
187ebfedea0SLionel Sambuc 	unlink(file);
188ebfedea0SLionel Sambuc     }
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc     return 0;
191ebfedea0SLionel Sambuc }
192