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: entropy_test.c,v 1.23 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/mem.h>
29#include <isc/util.h>
30#include <isc/string.h>
31
32static void
33hex_dump(const char *msg, void *data, unsigned int length) {
34        unsigned int len;
35	unsigned char *base;
36	isc_boolean_t first = ISC_TRUE;
37
38	base = data;
39
40        printf("DUMP of %d bytes:  %s\n\t", length, msg);
41        for (len = 0; len < length; len++) {
42                if (len % 16 == 0 && !first)
43			printf("\n\t");
44                printf("%02x ", base[len]);
45		first = ISC_FALSE;
46        }
47        printf("\n");
48}
49
50static void
51CHECK(const char *msg, isc_result_t result) {
52	if (result != ISC_R_SUCCESS) {
53		printf("FAILURE:  %s:  %s\n", msg, isc_result_totext(result));
54		exit(1);
55	}
56}
57
58int
59main(int argc, char **argv) {
60	isc_mem_t *mctx;
61	unsigned char buffer[512];
62	isc_entropy_t *ent;
63	unsigned int returned;
64	unsigned int flags;
65	isc_result_t result;
66
67	UNUSED(argc);
68	UNUSED(argv);
69
70	mctx = NULL;
71	CHECK("isc_mem_create()",
72	      isc_mem_create(0, 0, &mctx));
73
74	ent = NULL;
75	CHECK("isc_entropy_create()",
76	      isc_entropy_create(mctx, &ent));
77
78	isc_entropy_stats(ent, stderr);
79
80#if 1
81	CHECK("isc_entropy_createfilesource() 1",
82	      isc_entropy_createfilesource(ent, "/dev/random"));
83	CHECK("isc_entropy_createfilesource() 2",
84	      isc_entropy_createfilesource(ent, "/dev/random"));
85#else
86	CHECK("isc_entropy_createfilesource() 3",
87	      isc_entropy_createfilesource(ent, "/tmp/foo"));
88#endif
89
90	fprintf(stderr,
91		"Reading 32 bytes of GOOD random data only, partial OK\n");
92
93	flags = 0;
94	flags |= ISC_ENTROPY_GOODONLY;
95	flags |= ISC_ENTROPY_PARTIAL;
96	result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
97	if (result == ISC_R_NOENTROPY) {
98		fprintf(stderr, "No entropy.\n");
99		goto any;
100	}
101	hex_dump("good data only:", buffer, returned);
102
103 any:
104	isc_entropy_stats(ent, stderr);
105	CHECK("isc_entropy_getdata() pseudorandom",
106	      isc_entropy_getdata(ent, buffer, 128, NULL, 0));
107	hex_dump("pseudorandom data", buffer, 128);
108
109	isc_entropy_stats(ent, stderr);
110	flags = 0;
111	flags |= ISC_ENTROPY_GOODONLY;
112	flags |= ISC_ENTROPY_BLOCKING;
113	result = isc_entropy_getdata(ent, buffer, sizeof(buffer), &returned,
114				     flags);
115	CHECK("good data only, blocking mode", result);
116	hex_dump("blocking mode data", buffer, sizeof(buffer));
117
118	{
119		isc_entropy_t *entcopy1 = NULL;
120		isc_entropy_t *entcopy2 = NULL;
121		isc_entropy_t *entcopy3 = NULL;
122
123		isc_entropy_attach(ent, &entcopy1);
124		isc_entropy_attach(ent, &entcopy2);
125		isc_entropy_attach(ent, &entcopy3);
126
127		isc_entropy_stats(ent, stderr);
128
129		isc_entropy_detach(&entcopy1);
130		isc_entropy_detach(&entcopy2);
131		isc_entropy_detach(&entcopy3);
132	}
133
134	isc_entropy_detach(&ent);
135	isc_mem_stats(mctx, stderr);
136	isc_mem_destroy(&mctx);
137
138	return (0);
139}
140
141