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-12 22:28:54 +00:00
|
|
|
hex_dump(const 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-09 21:36:51 +00:00
|
|
|
isc_boolean_t first = ISC_TRUE;
|
2000-06-08 23:41:37 +00:00
|
|
|
|
2000-06-09 00:11:39 +00:00
|
|
|
base = data;
|
|
|
|
|
2000-06-09 21:36:51 +00:00
|
|
|
printf("DUMP of %d bytes: %s\n\t", length, msg);
|
2000-06-09 00:11:39 +00:00
|
|
|
for (len = 0 ; len < length ; len++) {
|
2000-06-09 21:36:51 +00:00
|
|
|
if (len % 16 == 0 && !first)
|
|
|
|
printf("\n\t");
|
2000-06-09 00:11:39 +00:00
|
|
|
printf("%02x ", base[len]);
|
2000-06-09 21:36:51 +00:00
|
|
|
first = ISC_FALSE;
|
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) {
|
2000-06-12 22:28:54 +00:00
|
|
|
printf("FAILURE: %s: %s\n", msg, isc_result_totext(result));
|
2000-06-09 00:11:39 +00:00
|
|
|
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-12 22:28:54 +00:00
|
|
|
#if 1
|
2000-06-09 00:11:39 +00:00
|
|
|
devrandom = NULL;
|
2000-06-09 21:36:51 +00:00
|
|
|
flags = 0;
|
2000-06-09 00:11:39 +00:00
|
|
|
CHECK("isc_entropy_createfilesource()",
|
|
|
|
isc_entropy_createfilesource(ent, "/dev/random",
|
2000-06-09 21:36:51 +00:00
|
|
|
flags, &devrandom));
|
|
|
|
#else
|
|
|
|
devrandom = NULL;
|
|
|
|
flags = 0;
|
|
|
|
CHECK("isc_entropy_createfilesource()",
|
|
|
|
isc_entropy_createfilesource(ent, "/tmp/foo",
|
|
|
|
flags, &devrandom));
|
|
|
|
#endif
|
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");
|
2000-06-09 21:36:51 +00:00
|
|
|
goto any;
|
2000-06-09 02:00:22 +00:00
|
|
|
}
|
|
|
|
hex_dump("good data only:", buffer, returned);
|
|
|
|
|
2000-06-09 21:36:51 +00:00
|
|
|
any:
|
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));
|
2000-06-09 21:36:51 +00:00
|
|
|
hex_dump("pseudorandom data", buffer, 128);
|
2000-06-09 02:00:22 +00:00
|
|
|
|
2000-06-09 21:36:51 +00:00
|
|
|
isc_entropy_stats(ent, stderr);
|
|
|
|
flags = 0;
|
|
|
|
flags |= ISC_ENTROPY_GOODONLY;
|
|
|
|
flags |= ISC_ENTROPY_BLOCKING;
|
|
|
|
result = isc_entropy_getdata(ent, buffer, sizeof buffer, &returned,
|
|
|
|
flags);
|
|
|
|
CHECK("good data only, blocking mode", result);
|
|
|
|
hex_dump("blocking mode data", buffer, sizeof buffer);
|
2000-06-09 02:00:22 +00:00
|
|
|
|
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);
|
|
|
|
}
|