2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-27 04:28:33 +00:00
bind/bin/tests/entropy_test.c

121 lines
3.0 KiB
C
Raw Normal View History

2000-06-08 23:41:37 +00:00
/*
* Copyright (C) 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <isc/entropy.h>
2000-06-09 00:11:39 +00:00
#include <isc/mem.h>
2000-06-08 23:41:37 +00:00
#include <isc/util.h>
#include <isc/string.h>
#include <stdio.h>
static void
2000-06-09 00:11:39 +00:00
hex_dump(char *msg, void *data, unsigned int length) {
2000-06-08 23:41:37 +00:00
unsigned int len;
2000-06-09 00:11:39 +00:00
unsigned char *base;
2000-06-08 23:41:37 +00:00
2000-06-09 00:11:39 +00:00
base = data;
printf("DUMP of %d bytes: %s\n", length, msg);
for (len = 0 ; len < length ; len++) {
2000-06-08 23:41:37 +00:00
if (len % 16 == 0)
printf("\n");
2000-06-09 00:11:39 +00:00
printf("%02x ", base[len]);
2000-06-08 23:41:37 +00:00
}
printf("\n");
}
2000-06-09 00:11:39 +00:00
static void
CHECK(const char *msg, isc_result_t result) {
if (result != ISC_R_SUCCESS) {
printf("FAILURE: %s\n", msg);
exit(1);
}
}
2000-06-08 23:41:37 +00:00
int
main(int argc, char **argv) {
2000-06-09 00:11:39 +00:00
isc_mem_t *mctx;
2000-06-09 02:00:22 +00:00
unsigned char buffer[512];
2000-06-09 00:11:39 +00:00
isc_entropy_t *ent;
isc_entropysource_t *devrandom;
2000-06-09 02:00:22 +00:00
unsigned int returned;
unsigned int flags;
isc_result_t result;
2000-06-08 23:41:37 +00:00
UNUSED(argc);
UNUSED(argv);
2000-06-09 00:11:39 +00:00
mctx = NULL;
CHECK("isc_mem_create()",
isc_mem_create(0, 0, &mctx));
ent = NULL;
CHECK("isc_entropy_create()",
isc_entropy_create(mctx, &ent));
2000-06-09 02:00:22 +00:00
isc_entropy_stats(ent, stderr);
2000-06-09 00:11:39 +00:00
devrandom = NULL;
CHECK("isc_entropy_createfilesource()",
isc_entropy_createfilesource(ent, "/dev/random",
0, &devrandom));
2000-06-09 02:00:22 +00:00
fprintf(stderr,
"Reading 32 bytes of GOOD random data only, partial OK\n");
flags = 0;
flags |= ISC_ENTROPY_GOODONLY;
flags |= ISC_ENTROPY_PARTIAL;
result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
if (result == ISC_R_NOENTROPY) {
fprintf(stderr, "No entropy.\n");
goto out;
}
hex_dump("good data only:", buffer, returned);
2000-06-09 17:48:08 +00:00
isc_entropy_stats(ent, stderr);
2000-06-09 02:00:22 +00:00
CHECK("isc_entropy_getdata()",
isc_entropy_getdata(ent, buffer, 128, NULL, 0));
hex_dump("entropy data", buffer, 128);
out:
2000-06-09 19:18:56 +00:00
{
isc_entropy_t *entcopy1 = NULL;
isc_entropy_t *entcopy2 = NULL;
isc_entropy_t *entcopy3 = NULL;
isc_entropy_attach(ent, &entcopy1);
isc_entropy_attach(ent, &entcopy2);
isc_entropy_attach(ent, &entcopy3);
isc_entropy_stats(ent, stderr);
isc_entropy_detach(&entcopy1);
isc_entropy_detach(&entcopy2);
isc_entropy_detach(&entcopy3);
}
2000-06-09 02:00:22 +00:00
isc_entropy_destroysource(&devrandom);
2000-06-09 19:18:56 +00:00
isc_entropy_detach(&ent);
2000-06-09 02:00:22 +00:00
isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx);
2000-06-08 23:41:37 +00:00
return (0);
}