1 /* $NetBSD: entropy2_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
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 */
29 #include <isc/entropy.h>
30 #include <isc/keyboard.h>
32 #include <isc/string.h>
37 hex_dump(const char *msg
, void *data
, unsigned int length
) {
40 isc_boolean_t first
= ISC_TRUE
;
44 printf("DUMP of %d bytes: %s\n\t", length
, msg
);
45 for (len
= 0; len
< length
; len
++) {
46 if (len
% 16 == 0 && !first
)
48 printf("%02x ", base
[len
]);
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
));
63 start(isc_entropysource_t
*source
, void *arg
, isc_boolean_t blocking
) {
64 isc_keyboard_t
*kbd
= (isc_keyboard_t
*)arg
;
69 printf("start called, blocking mode.\n");
71 printf("start called, non-blocking mode.\n");
73 return (isc_keyboard_open(kbd
));
77 stop(isc_entropysource_t
*source
, void *arg
) {
78 isc_keyboard_t
*kbd
= (isc_keyboard_t
*)arg
;
82 printf("ENOUGH! Stop typing, please.\r\n");
84 (void)isc_keyboard_close(kbd
, 3);
85 printf("stop called\n");
89 get(isc_entropysource_t
*source
, void *arg
, isc_boolean_t blocking
) {
90 isc_keyboard_t
*kbd
= (isc_keyboard_t
*)arg
;
98 return (ISC_R_NOENTROPY
);
100 result
= isc_keyboard_getchar(kbd
, &c
);
101 if (result
!= ISC_R_SUCCESS
)
106 sample
= isc_time_nanoseconds(&t
);
109 result
= isc_entropy_addcallbacksample(source
, sample
, extra
);
110 if (result
!= ISC_R_SUCCESS
) {
122 main(int argc
, char **argv
) {
124 unsigned char buffer
[512];
126 isc_entropysource_t
*source
;
127 unsigned int returned
;
136 CHECK("isc_mem_create()",
137 isc_mem_create(0, 0, &mctx
));
140 CHECK("isc_entropy_create()",
141 isc_entropy_create(mctx
, &ent
));
143 isc_entropy_stats(ent
, stderr
);
146 result
= isc_entropy_createcallbacksource(ent
, start
, get
, stop
, &kbd
,
148 CHECK("isc_entropy_createcallbacksource()", result
);
151 "Reading 32 bytes of GOOD random data only, partial OK\n");
154 flags
|= ISC_ENTROPY_GOODONLY
;
155 flags
|= ISC_ENTROPY_PARTIAL
;
156 flags
|= ISC_ENTROPY_BLOCKING
;
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
);