Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / bin / tests / entropy2_test.c
blobad59e763e5926538e01770e4fc98140f6d36cd69
1 /* $NetBSD: entropy2_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
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.
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.
20 /* Id: entropy2_test.c,v 1.16 2007/06/19 23:46:59 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
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>
36 static void
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;
42 base = data;
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;
51 printf("\n");
54 static void
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);
62 static isc_result_t
63 start(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
64 isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
66 UNUSED(source);
68 if (blocking)
69 printf("start called, blocking mode.\n");
70 else
71 printf("start called, non-blocking mode.\n");
73 return (isc_keyboard_open(kbd));
76 static void
77 stop(isc_entropysource_t *source, void *arg) {
78 isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
80 UNUSED(source);
82 printf("ENOUGH! Stop typing, please.\r\n");
84 (void)isc_keyboard_close(kbd, 3);
85 printf("stop called\n");
88 static isc_result_t
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;
97 if (!blocking)
98 return (ISC_R_NOENTROPY);
100 result = isc_keyboard_getchar(kbd, &c);
101 if (result != ISC_R_SUCCESS)
102 return (result);
104 TIME_NOW(&t);
106 sample = isc_time_nanoseconds(&t);
107 extra = c;
109 result = isc_entropy_addcallbacksample(source, sample, extra);
110 if (result != ISC_R_SUCCESS) {
111 printf("\r\n");
112 return (result);
115 printf(".");
116 fflush(stdout);
118 return (result);
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;
132 UNUSED(argc);
133 UNUSED(argv);
135 mctx = NULL;
136 CHECK("isc_mem_create()",
137 isc_mem_create(0, 0, &mctx));
139 ent = NULL;
140 CHECK("isc_entropy_create()",
141 isc_entropy_create(mctx, &ent));
143 isc_entropy_stats(ent, stderr);
145 source = NULL;
146 result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd,
147 &source);
148 CHECK("isc_entropy_createcallbacksource()", result);
150 fprintf(stderr,
151 "Reading 32 bytes of GOOD random data only, partial OK\n");
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");
163 isc_entropy_stopcallbacksources(ent);
165 hex_dump("good data only:", buffer, returned);
167 isc_entropy_stats(ent, stderr);
169 isc_entropy_destroysource(&source);
170 isc_entropy_detach(&ent);
172 isc_mem_stats(mctx, stderr);
173 isc_mem_destroy(&mctx);
175 return (0);