xref: /minix3/external/bsd/bind/dist/bin/tests/entropy_test.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: entropy_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: entropy_test.c,v 1.23 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/mem.h>
31 #include <isc/util.h>
32 #include <isc/string.h>
33 
34 static void
hex_dump(const char * msg,void * data,unsigned int length)35 hex_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 
52 static void
CHECK(const char * msg,isc_result_t result)53 CHECK(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 
60 int
main(int argc,char ** argv)61 main(int argc, char **argv) {
62 	isc_mem_t *mctx;
63 	unsigned char buffer[512];
64 	isc_entropy_t *ent;
65 	unsigned int returned;
66 	unsigned int flags;
67 	isc_result_t result;
68 
69 	UNUSED(argc);
70 	UNUSED(argv);
71 
72 	mctx = NULL;
73 	CHECK("isc_mem_create()",
74 	      isc_mem_create(0, 0, &mctx));
75 
76 	ent = NULL;
77 	CHECK("isc_entropy_create()",
78 	      isc_entropy_create(mctx, &ent));
79 
80 	isc_entropy_stats(ent, stderr);
81 
82 #if 1
83 	CHECK("isc_entropy_createfilesource() 1",
84 	      isc_entropy_createfilesource(ent, "/dev/random"));
85 	CHECK("isc_entropy_createfilesource() 2",
86 	      isc_entropy_createfilesource(ent, "/dev/random"));
87 #else
88 	CHECK("isc_entropy_createfilesource() 3",
89 	      isc_entropy_createfilesource(ent, "/tmp/foo"));
90 #endif
91 
92 	fprintf(stderr,
93 		"Reading 32 bytes of GOOD random data only, partial OK\n");
94 
95 	flags = 0;
96 	flags |= ISC_ENTROPY_GOODONLY;
97 	flags |= ISC_ENTROPY_PARTIAL;
98 	result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
99 	if (result == ISC_R_NOENTROPY) {
100 		fprintf(stderr, "No entropy.\n");
101 		goto any;
102 	}
103 	hex_dump("good data only:", buffer, returned);
104 
105  any:
106 	isc_entropy_stats(ent, stderr);
107 	CHECK("isc_entropy_getdata() pseudorandom",
108 	      isc_entropy_getdata(ent, buffer, 128, NULL, 0));
109 	hex_dump("pseudorandom data", buffer, 128);
110 
111 	isc_entropy_stats(ent, stderr);
112 	flags = 0;
113 	flags |= ISC_ENTROPY_GOODONLY;
114 	flags |= ISC_ENTROPY_BLOCKING;
115 	result = isc_entropy_getdata(ent, buffer, sizeof(buffer), &returned,
116 				     flags);
117 	CHECK("good data only, blocking mode", result);
118 	hex_dump("blocking mode data", buffer, sizeof(buffer));
119 
120 	{
121 		isc_entropy_t *entcopy1 = NULL;
122 		isc_entropy_t *entcopy2 = NULL;
123 		isc_entropy_t *entcopy3 = NULL;
124 
125 		isc_entropy_attach(ent, &entcopy1);
126 		isc_entropy_attach(ent, &entcopy2);
127 		isc_entropy_attach(ent, &entcopy3);
128 
129 		isc_entropy_stats(ent, stderr);
130 
131 		isc_entropy_detach(&entcopy1);
132 		isc_entropy_detach(&entcopy2);
133 		isc_entropy_detach(&entcopy3);
134 	}
135 
136 	isc_entropy_detach(&ent);
137 	isc_mem_stats(mctx, stderr);
138 	isc_mem_destroy(&mctx);
139 
140 	return (0);
141 }
142 
143