1 /* $NetBSD: entropy_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: entropy_test.c,v 1.23 2007/06/19 23:46:59 tbox Exp */
29 #include <isc/entropy.h>
32 #include <isc/string.h>
35 hex_dump(const char *msg
, void *data
, unsigned int length
) {
38 isc_boolean_t first
= ISC_TRUE
;
42 printf("DUMP of %d bytes: %s\n\t", length
, msg
);
43 for (len
= 0; len
< length
; len
++) {
44 if (len
% 16 == 0 && !first
)
46 printf("%02x ", base
[len
]);
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
));
61 main(int argc
, char **argv
) {
63 unsigned char buffer
[512];
65 unsigned int returned
;
73 CHECK("isc_mem_create()",
74 isc_mem_create(0, 0, &mctx
));
77 CHECK("isc_entropy_create()",
78 isc_entropy_create(mctx
, &ent
));
80 isc_entropy_stats(ent
, stderr
);
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"));
88 CHECK("isc_entropy_createfilesource() 3",
89 isc_entropy_createfilesource(ent
, "/tmp/foo"));
93 "Reading 32 bytes of GOOD random data only, partial OK\n");
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");
103 hex_dump("good data only:", buffer
, returned
);
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);
111 isc_entropy_stats(ent
, stderr
);
113 flags
|= ISC_ENTROPY_GOODONLY
;
114 flags
|= ISC_ENTROPY_BLOCKING
;
115 result
= isc_entropy_getdata(ent
, buffer
, sizeof(buffer
), &returned
,
117 CHECK("good data only, blocking mode", result
);
118 hex_dump("blocking mode data", buffer
, sizeof(buffer
));
121 isc_entropy_t
*entcopy1
= NULL
;
122 isc_entropy_t
*entcopy2
= NULL
;
123 isc_entropy_t
*entcopy3
= NULL
;
125 isc_entropy_attach(ent
, &entcopy1
);
126 isc_entropy_attach(ent
, &entcopy2
);
127 isc_entropy_attach(ent
, &entcopy3
);
129 isc_entropy_stats(ent
, stderr
);
131 isc_entropy_detach(&entcopy1
);
132 isc_entropy_detach(&entcopy2
);
133 isc_entropy_detach(&entcopy3
);
136 isc_entropy_detach(&ent
);
137 isc_mem_stats(mctx
, stderr
);
138 isc_mem_destroy(&mctx
);