2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-26 20:17:35 +00:00
bind/bin/tests/entropy_test.c

133 lines
3.1 KiB
C
Raw Normal View History

2000-06-08 23:41:37 +00:00
/*
* Copyright (C) 2000, 2001, 2004, 2005, 2007, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2000-06-08 23:41:37 +00:00
*/
2007-06-19 23:47:24 +00:00
/* $Id: entropy_test.c,v 1.23 2007/06/19 23:46:59 tbox Exp $ */
/*! \file */
2000-06-23 16:19:01 +00:00
#include <config.h>
#include <stdio.h>
2000-07-26 22:44:51 +00:00
#include <stdlib.h>
2000-06-22 22:00:42 +00:00
2000-06-08 23:41:37 +00:00
#include <isc/entropy.h>
2000-06-09 00:11:39 +00:00
#include <isc/mem.h>
#include <isc/print.h>
2000-06-08 23:41:37 +00:00
#include <isc/string.h>
#include <isc/util.h>
2000-06-08 23:41:37 +00:00
static void
hex_dump(const char *msg, void *data, unsigned int length) {
2015-05-23 23:45:25 +00:00
unsigned int len;
2000-06-09 00:11:39 +00:00
unsigned char *base;
isc_boolean_t first = ISC_TRUE;
2000-06-08 23:41:37 +00:00
2000-06-09 00:11:39 +00:00
base = data;
2015-05-23 23:45:25 +00:00
printf("DUMP of %d bytes: %s\n\t", length, msg);
for (len = 0; len < length; len++) {
if (len % 16 == 0 && !first)
printf("\n\t");
2015-05-23 23:45:25 +00:00
printf("%02x ", base[len]);
first = ISC_FALSE;
2015-05-23 23:45:25 +00:00
}
printf("\n");
2000-06-08 23:41:37 +00:00
}
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: %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;
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);
#if 1
CHECK("isc_entropy_createfilesource() 1",
isc_entropy_createfilesource(ent, "/dev/random"));
CHECK("isc_entropy_createfilesource() 2",
isc_entropy_createfilesource(ent, "/dev/random"));
#else
CHECK("isc_entropy_createfilesource() 3",
isc_entropy_createfilesource(ent, "/tmp/foo"));
#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");
goto any;
2000-06-09 02:00:22 +00:00
}
hex_dump("good data only:", buffer, returned);
any:
2000-06-09 17:48:08 +00:00
isc_entropy_stats(ent, stderr);
CHECK("isc_entropy_getdata() pseudorandom",
2000-06-09 02:00:22 +00:00
isc_entropy_getdata(ent, buffer, 128, NULL, 0));
hex_dump("pseudorandom data", buffer, 128);
2000-06-09 02:00:22 +00:00
isc_entropy_stats(ent, stderr);
flags = 0;
flags |= ISC_ENTROPY_GOODONLY;
flags |= ISC_ENTROPY_BLOCKING;
2001-11-27 01:56:32 +00:00
result = isc_entropy_getdata(ent, buffer, sizeof(buffer), &returned,
flags);
CHECK("good data only, blocking mode", result);
2001-11-27 01:56:32 +00:00
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
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);
}