entropy_test.c revision 3759f10fc543747668b1ca4b4671f35b0dea8445
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004, 2005, 2007, 2015 Internet Systems Consortium, Inc. ("ISC")
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * Copyright (C) 2000, 2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * copyright notice and this permission notice appear in all copies.
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt *
5e3affc6a0b155ee1cadac735c1a71f4d418ad69Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt * PERFORMANCE OF THIS SOFTWARE.
2c089bf6d24936de631a57b4958ba6b8b5e3b23dMark Andrews */
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt/* $Id: entropy_test.c,v 1.23 2007/06/19 23:46:59 tbox Exp $ */
2c089bf6d24936de631a57b4958ba6b8b5e3b23dMark Andrews
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt/*! \file */
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt#include <config.h>
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt#include <stdio.h>
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt#include <stdlib.h>
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
c298583db573a329f37d43301d8c3c812500ac85Mark Andrews#include <isc/entropy.h>
c298583db573a329f37d43301d8c3c812500ac85Mark Andrews#include <isc/mem.h>
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt#include <isc/print.h>
c298583db573a329f37d43301d8c3c812500ac85Mark Andrews#include <isc/string.h>
c298583db573a329f37d43301d8c3c812500ac85Mark Andrews#include <isc/util.h>
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
c298583db573a329f37d43301d8c3c812500ac85Mark Andrewsstatic void
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunthex_dump(const char *msg, void *data, unsigned int length) {
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt unsigned int len;
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt unsigned char *base;
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt isc_boolean_t first = ISC_TRUE;
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt base = data;
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt printf("DUMP of %d bytes: %s\n\t", length, msg);
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt for (len = 0; len < length; len++) {
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt if (len % 16 == 0 && !first)
2c089bf6d24936de631a57b4958ba6b8b5e3b23dMark Andrews printf("\n\t");
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt printf("%02x ", base[len]);
2c089bf6d24936de631a57b4958ba6b8b5e3b23dMark Andrews first = ISC_FALSE;
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt }
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt printf("\n");
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt}
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
2c089bf6d24936de631a57b4958ba6b8b5e3b23dMark Andrewsstatic void
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan HuntCHECK(const char *msg, isc_result_t result) {
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt if (result != ISC_R_SUCCESS) {
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt printf("FAILURE: %s: %s\n", msg, isc_result_totext(result));
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt exit(1);
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt }
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt}
cf0d508b1eb836150fd2fc9c3d497525ed8fbe66Evan Hunt
0949306cb96f9ccbb7d0205584ed2db293a5aad2Evan Huntint
0949306cb96f9ccbb7d0205584ed2db293a5aad2Evan Huntmain(int argc, char **argv) {
0949306cb96f9ccbb7d0205584ed2db293a5aad2Evan Hunt isc_mem_t *mctx;
0949306cb96f9ccbb7d0205584ed2db293a5aad2Evan Hunt unsigned char buffer[512];
0949306cb96f9ccbb7d0205584ed2db293a5aad2Evan Hunt isc_entropy_t *ent;
unsigned int returned;
unsigned int flags;
isc_result_t result;
UNUSED(argc);
UNUSED(argv);
mctx = NULL;
CHECK("isc_mem_create()",
isc_mem_create(0, 0, &mctx));
ent = NULL;
CHECK("isc_entropy_create()",
isc_entropy_create(mctx, &ent));
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
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;
}
hex_dump("good data only:", buffer, returned);
any:
isc_entropy_stats(ent, stderr);
CHECK("isc_entropy_getdata() pseudorandom",
isc_entropy_getdata(ent, buffer, 128, NULL, 0));
hex_dump("pseudorandom data", buffer, 128);
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));
{
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);
}
isc_entropy_detach(&ent);
isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx);
return (0);
}