5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt/*
8688e7005afed694ca129d00d3cd73b32828804bTinderbox User * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt */
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt/* $Id: entropy2_test.c,v 1.16 2007/06/19 23:46:59 tbox Exp $ */
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt/*! \file */
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <config.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <stdio.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <stdlib.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <isc/entropy.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <isc/keyboard.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <isc/mem.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <isc/print.h>
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <isc/string.h>
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt#include <isc/time.h>
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt#include <isc/util.h>
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Huntstatic void
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunthex_dump(const char *msg, void *data, unsigned int length) {
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt unsigned int len;
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt unsigned char *base;
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt isc_boolean_t first = ISC_TRUE;
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt base = data;
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt printf("DUMP of %d bytes: %s\n\t", length, msg);
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt for (len = 0; len < length; len++) {
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt if (len % 16 == 0 && !first)
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt printf("\n\t");
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt printf("%02x ", base[len]);
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt first = ISC_FALSE;
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt }
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt printf("\n");
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt}
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
420a43c8d8028992a4e9c170022f97bfac689025Evan Huntstatic void
420a43c8d8028992a4e9c170022f97bfac689025Evan HuntCHECK(const char *msg, isc_result_t result) {
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt if (result != ISC_R_SUCCESS) {
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt printf("FAILURE: %s: %s\n", msg, isc_result_totext(result));
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt exit(1);
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt }
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt}
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
420a43c8d8028992a4e9c170022f97bfac689025Evan Huntstatic isc_result_t
420a43c8d8028992a4e9c170022f97bfac689025Evan Huntstart(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt UNUSED(source);
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman if (blocking)
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman printf("start called, blocking mode.\n");
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman else
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman printf("start called, non-blocking mode.\n");
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman return (isc_keyboard_open(kbd));
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman}
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaramanstatic void
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaramanstop(isc_entropysource_t *source, void *arg) {
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman UNUSED(source);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman printf("ENOUGH! Stop typing, please.\r\n");
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman (void)isc_keyboard_close(kbd, 3);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman printf("stop called\n");
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman}
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaramanstatic isc_result_t
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaramanget(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_result_t result;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_time_t t;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_uint32_t sample;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman isc_uint32_t extra;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman unsigned char c;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman if (!blocking)
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman return (ISC_R_NOENTROPY);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman result = isc_keyboard_getchar(kbd, &c);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman if (result != ISC_R_SUCCESS)
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman return (result);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman TIME_NOW(&t);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman sample = isc_time_nanoseconds(&t);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman extra = c;
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman result = isc_entropy_addcallbacksample(source, sample, extra);
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman if (result != ISC_R_SUCCESS) {
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman printf("\r\n");
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt return (result);
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt }
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt printf(".");
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt fflush(stdout);
420a43c8d8028992a4e9c170022f97bfac689025Evan Hunt
b5252fcde512405a68dd4becfe683d9763bd0feaMukund Sivaraman return (result);
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt}
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt
int
main(int argc, char **argv) {
isc_mem_t *mctx;
unsigned char buffer[512];
isc_entropy_t *ent;
isc_entropysource_t *source;
unsigned int returned;
unsigned int flags;
isc_result_t result;
isc_keyboard_t kbd;
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);
source = NULL;
result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd,
&source);
CHECK("isc_entropy_createcallbacksource()", result);
fprintf(stderr,
"Reading 32 bytes of GOOD random data only, partial OK\n");
flags = 0;
flags |= ISC_ENTROPY_GOODONLY;
flags |= ISC_ENTROPY_PARTIAL;
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);
}