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