xref: /minix3/external/bsd/bind/dist/bin/tests/entropy2_test.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: entropy2_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: entropy2_test.c,v 1.16 2007/06/19 23:46:59 tbox Exp  */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 #include <isc/entropy.h>
30 #include <isc/keyboard.h>
31 #include <isc/mem.h>
32 #include <isc/string.h>
33 #include <isc/time.h>
34 #include <isc/util.h>
35 
36 static void
hex_dump(const char * msg,void * data,unsigned int length)37 hex_dump(const char *msg, void *data, unsigned int length) {
38         unsigned int len;
39 	unsigned char *base;
40 	isc_boolean_t first = ISC_TRUE;
41 
42 	base = data;
43 
44         printf("DUMP of %d bytes:  %s\n\t", length, msg);
45         for (len = 0; len < length; len++) {
46                 if (len % 16 == 0 && !first)
47 			printf("\n\t");
48                 printf("%02x ", base[len]);
49 		first = ISC_FALSE;
50         }
51         printf("\n");
52 }
53 
54 static void
CHECK(const char * msg,isc_result_t result)55 CHECK(const char *msg, isc_result_t result) {
56 	if (result != ISC_R_SUCCESS) {
57 		printf("FAILURE:  %s:  %s\n", msg, isc_result_totext(result));
58 		exit(1);
59 	}
60 }
61 
62 static isc_result_t
start(isc_entropysource_t * source,void * arg,isc_boolean_t blocking)63 start(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
64 	isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
65 
66 	UNUSED(source);
67 
68 	if (blocking)
69 		printf("start called, blocking mode.\n");
70 	else
71 		printf("start called, non-blocking mode.\n");
72 
73 	return (isc_keyboard_open(kbd));
74 }
75 
76 static void
stop(isc_entropysource_t * source,void * arg)77 stop(isc_entropysource_t *source, void *arg) {
78 	isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
79 
80 	UNUSED(source);
81 
82 	printf("ENOUGH!  Stop typing, please.\r\n");
83 
84 	(void)isc_keyboard_close(kbd, 3);
85 	printf("stop called\n");
86 }
87 
88 static isc_result_t
get(isc_entropysource_t * source,void * arg,isc_boolean_t blocking)89 get(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
90 	isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
91 	isc_result_t result;
92 	isc_time_t t;
93 	isc_uint32_t sample;
94 	isc_uint32_t extra;
95 	unsigned char c;
96 
97 	if (!blocking)
98 		return (ISC_R_NOENTROPY);
99 
100 	result = isc_keyboard_getchar(kbd, &c);
101 	if (result != ISC_R_SUCCESS)
102 		return (result);
103 
104 	TIME_NOW(&t);
105 
106 	sample = isc_time_nanoseconds(&t);
107 	extra = c;
108 
109 	result = isc_entropy_addcallbacksample(source, sample, extra);
110 	if (result != ISC_R_SUCCESS) {
111 		printf("\r\n");
112 		return (result);
113 	}
114 
115 	printf(".");
116 	fflush(stdout);
117 
118 	return (result);
119 }
120 
121 int
main(int argc,char ** argv)122 main(int argc, char **argv) {
123 	isc_mem_t *mctx;
124 	unsigned char buffer[512];
125 	isc_entropy_t *ent;
126 	isc_entropysource_t *source;
127 	unsigned int returned;
128 	unsigned int flags;
129 	isc_result_t result;
130 	isc_keyboard_t kbd;
131 
132 	UNUSED(argc);
133 	UNUSED(argv);
134 
135 	mctx = NULL;
136 	CHECK("isc_mem_create()",
137 	      isc_mem_create(0, 0, &mctx));
138 
139 	ent = NULL;
140 	CHECK("isc_entropy_create()",
141 	      isc_entropy_create(mctx, &ent));
142 
143 	isc_entropy_stats(ent, stderr);
144 
145 	source = NULL;
146 	result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd,
147 						  &source);
148 	CHECK("isc_entropy_createcallbacksource()", result);
149 
150 	fprintf(stderr,
151 		"Reading 32 bytes of GOOD random data only, partial OK\n");
152 
153 	flags = 0;
154 	flags |= ISC_ENTROPY_GOODONLY;
155 	flags |= ISC_ENTROPY_PARTIAL;
156 	flags |= ISC_ENTROPY_BLOCKING;
157 	returned = 0;
158 	result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
159 	if (result == ISC_R_NOENTROPY) {
160 		fprintf(stderr, "No entropy.\r\n");
161 	}
162 
163 	isc_entropy_stopcallbacksources(ent);
164 
165 	hex_dump("good data only:", buffer, returned);
166 
167 	isc_entropy_stats(ent, stderr);
168 
169 	isc_entropy_destroysource(&source);
170 	isc_entropy_detach(&ent);
171 
172 	isc_mem_stats(mctx, stderr);
173 	isc_mem_destroy(&mctx);
174 
175 	return (0);
176 }
177 
178