entropy2_test.c 3.47 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2015, 2016, 2018  Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * 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/.
7 8
 */

Automatic Updater's avatar
Automatic Updater committed
9
/* $Id: entropy2_test.c,v 1.16 2007/06/19 23:46:59 tbox Exp $ */
10 11

/*! \file */
David Lawrence's avatar
David Lawrence committed
12 13 14 15

#include <config.h>

#include <stdio.h>
16
#include <stdlib.h>
David Lawrence's avatar
David Lawrence committed
17

18
#include <isc/entropy.h>
Michael Graff's avatar
Michael Graff committed
19
#include <isc/keyboard.h>
20
#include <isc/mem.h>
21
#include <isc/print.h>
22
#include <isc/string.h>
Michael Graff's avatar
Michael Graff committed
23
#include <isc/time.h>
David Lawrence's avatar
David Lawrence committed
24
#include <isc/util.h>
25 26 27

static void
hex_dump(const char *msg, void *data, unsigned int length) {
28
	unsigned int len;
29 30 31 32 33
	unsigned char *base;
	isc_boolean_t first = ISC_TRUE;

	base = data;

Mark Andrews's avatar
Mark Andrews committed
34
	printf("DUMP of %u bytes:  %s\n\t", length, msg);
35 36
	for (len = 0; len < length; len++) {
		if (len % 16 == 0 && !first)
37
			printf("\n\t");
38
		printf("%02x ", base[len]);
39
		first = ISC_FALSE;
40 41
	}
	printf("\n");
42 43 44 45 46 47 48 49 50 51 52
}

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));
		exit(1);
	}
}

static isc_result_t
Michael Graff's avatar
Michael Graff committed
53 54
start(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
	isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
55

Michael Graff's avatar
Michael Graff committed
56 57 58 59 60 61 62 63
	UNUSED(source);

	if (blocking)
		printf("start called, blocking mode.\n");
	else
		printf("start called, non-blocking mode.\n");

	return (isc_keyboard_open(kbd));
64 65 66 67
}

static void
stop(isc_entropysource_t *source, void *arg) {
Michael Graff's avatar
Michael Graff committed
68 69 70 71 72 73 74
	isc_keyboard_t *kbd = (isc_keyboard_t *)arg;

	UNUSED(source);

	printf("ENOUGH!  Stop typing, please.\r\n");

	(void)isc_keyboard_close(kbd, 3);
75 76 77 78 79
	printf("stop called\n");
}

static isc_result_t
get(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
Michael Graff's avatar
Michael Graff committed
80
	isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
81
	isc_result_t result;
Michael Graff's avatar
Michael Graff committed
82 83 84 85
	isc_time_t t;
	isc_uint32_t sample;
	isc_uint32_t extra;
	unsigned char c;
86

Michael Graff's avatar
Michael Graff committed
87 88 89 90 91 92
	if (!blocking)
		return (ISC_R_NOENTROPY);

	result = isc_keyboard_getchar(kbd, &c);
	if (result != ISC_R_SUCCESS)
		return (result);
93

94
	TIME_NOW(&t);
95

Michael Graff's avatar
Michael Graff committed
96 97 98 99 100 101 102 103 104 105 106
	sample = isc_time_nanoseconds(&t);
	extra = c;

	result = isc_entropy_addcallbacksample(source, sample, extra);
	if (result != ISC_R_SUCCESS) {
		printf("\r\n");
		return (result);
	}

	printf(".");
	fflush(stdout);
107 108 109 110 111 112 113 114 115 116 117 118 119

	return (result);
}

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;
Michael Graff's avatar
Michael Graff committed
120
	isc_keyboard_t kbd;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

	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;
Michael Graff's avatar
Michael Graff committed
136
	result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd,
137 138 139 140 141 142 143 144 145 146 147 148 149
						  &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) {
Michael Graff's avatar
Michael Graff committed
150
		fprintf(stderr, "No entropy.\r\n");
151
	}
Michael Graff's avatar
Michael Graff committed
152 153 154

	isc_entropy_stopcallbacksources(ent);

155 156 157 158 159 160 161 162 163 164 165 166 167
	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);
}