entropy2_test.c revision f1b68725503ff3e46001eee5a1751e29a43a09d1
5785N/A/*
5785N/A * Copyright (C) 2000, 2001 Internet Software Consortium.
5785N/A *
5785N/A * Permission to use, copy, modify, and distribute this software for any
5785N/A * purpose with or without fee is hereby granted, provided that the above
5785N/A * copyright notice and this permission notice appear in all copies.
5785N/A *
5785N/A * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
5785N/A * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
6982N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
6982N/A * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
5785N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
5785N/A * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
5785N/A * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
6982N/A * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5785N/A */
6982N/A
6982N/A/* $Id: entropy2_test.c,v 1.10 2001/11/27 00:55:40 gson Exp $ */
6982N/A
6982N/A#include <config.h>
5785N/A
5785N/A#include <stdio.h>
5785N/A#include <stdlib.h>
5785N/A
5785N/A#include <isc/entropy.h>
5785N/A#include <isc/keyboard.h>
5785N/A#include <isc/mem.h>
5785N/A#include <isc/string.h>
5785N/A#include <isc/time.h>
5785N/A#include <isc/util.h>
5785N/A
5785N/Astatic void
5785N/Ahex_dump(const char *msg, void *data, unsigned int length) {
5785N/A unsigned int len;
5785N/A unsigned char *base;
5785N/A isc_boolean_t first = ISC_TRUE;
5785N/A
5785N/A base = data;
5785N/A
5785N/A printf("DUMP of %d bytes: %s\n\t", length, msg);
5785N/A for (len = 0; len < length; len++) {
5785N/A if (len % 16 == 0 && !first)
5785N/A printf("\n\t");
5785N/A printf("%02x ", base[len]);
5785N/A first = ISC_FALSE;
5785N/A }
5785N/A printf("\n");
5785N/A}
5785N/A
5785N/Astatic void
5785N/ACHECK(const char *msg, isc_result_t result) {
5785N/A if (result != ISC_R_SUCCESS) {
5785N/A printf("FAILURE: %s: %s\n", msg, isc_result_totext(result));
5785N/A exit(1);
5785N/A }
5785N/A}
5785N/A
5785N/Astatic isc_result_t
5785N/Astart(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
5785N/A isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
5785N/A
5785N/A UNUSED(source);
5785N/A
5785N/A if (blocking)
5785N/A printf("start called, blocking mode.\n");
5785N/A else
5785N/A printf("start called, non-blocking mode.\n");
5785N/A
5785N/A return (isc_keyboard_open(kbd));
5785N/A}
5785N/A
5785N/Astatic void
5785N/Astop(isc_entropysource_t *source, void *arg) {
5785N/A isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
5785N/A
5785N/A UNUSED(source);
5785N/A
5785N/A printf("ENOUGH! Stop typing, please.\r\n");
5785N/A
5785N/A (void)isc_keyboard_close(kbd, 3);
5785N/A printf("stop called\n");
5785N/A}
5785N/A
5785N/Astatic isc_result_t
5785N/Aget(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
5785N/A isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
5785N/A isc_result_t result;
5785N/A isc_time_t t;
5785N/A isc_uint32_t sample;
5785N/A isc_uint32_t extra;
5785N/A unsigned char c;
5785N/A
5785N/A if (!blocking)
5785N/A return (ISC_R_NOENTROPY);
5785N/A
5785N/A result = isc_keyboard_getchar(kbd, &c);
5785N/A if (result != ISC_R_SUCCESS)
5785N/A return (result);
5785N/A
5785N/A result = isc_time_now(&t);
5785N/A if (result != ISC_R_SUCCESS)
5785N/A return (result);
5785N/A
5785N/A sample = isc_time_nanoseconds(&t);
5785N/A extra = c;
5785N/A
5785N/A result = isc_entropy_addcallbacksample(source, sample, extra);
5785N/A if (result != ISC_R_SUCCESS) {
5785N/A printf("\r\n");
5785N/A return (result);
5785N/A }
5785N/A
5785N/A printf(".");
5785N/A fflush(stdout);
5785N/A
5785N/A return (result);
5785N/A}
5785N/A
5785N/Aint
5785N/Amain(int argc, char **argv) {
5785N/A isc_mem_t *mctx;
5785N/A unsigned char buffer[512];
5785N/A isc_entropy_t *ent;
5785N/A isc_entropysource_t *source;
5785N/A unsigned int returned;
5785N/A unsigned int flags;
5785N/A isc_result_t result;
5785N/A isc_keyboard_t kbd;
5785N/A
5785N/A UNUSED(argc);
5785N/A UNUSED(argv);
5785N/A
5785N/A mctx = NULL;
5785N/A CHECK("isc_mem_create()",
5785N/A isc_mem_create(0, 0, &mctx));
5785N/A
5785N/A ent = NULL;
5785N/A CHECK("isc_entropy_create()",
5785N/A isc_entropy_create(mctx, &ent));
5785N/A
5785N/A isc_entropy_stats(ent, stderr);
5785N/A
5785N/A source = NULL;
5785N/A result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd,
5785N/A &source);
5785N/A CHECK("isc_entropy_createcallbacksource()", result);
5785N/A
5785N/A fprintf(stderr,
5785N/A "Reading 32 bytes of GOOD random data only, partial OK\n");
5785N/A
5785N/A flags = 0;
5785N/A flags |= ISC_ENTROPY_GOODONLY;
5785N/A flags |= ISC_ENTROPY_PARTIAL;
5785N/A flags |= ISC_ENTROPY_BLOCKING;
returned = 0;
result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
if (result == ISC_R_NOENTROPY) {
fprintf(stderr, "No entropy.\r\n");
}
isc_entropy_stopcallbacksources(ent);
hex_dump("good data only:", buffer, returned);
isc_entropy_stats(ent, stderr);
isc_entropy_destroysource(&source);
isc_entropy_detach(&ent);
isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx);
return (0);
}