mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-02 23:55:27 +00:00
use the keyboard as a source
This commit is contained in:
@@ -16,9 +16,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <isc/entropy.h>
|
#include <isc/entropy.h>
|
||||||
|
#include <isc/keyboard.h>
|
||||||
#include <isc/mem.h>
|
#include <isc/mem.h>
|
||||||
#include <isc/util.h>
|
#include <isc/util.h>
|
||||||
#include <isc/string.h>
|
#include <isc/string.h>
|
||||||
|
#include <isc/time.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -49,15 +51,28 @@ CHECK(const char *msg, isc_result_t result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static isc_result_t
|
static isc_result_t
|
||||||
start(isc_entropysource_t *source, void *arg, isc_boolean_t blocking)
|
start(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
|
||||||
{
|
isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
|
||||||
printf("start called, non-blocking mode.\n");
|
|
||||||
|
|
||||||
return (ISC_R_SUCCESS);
|
UNUSED(source);
|
||||||
|
|
||||||
|
if (blocking)
|
||||||
|
printf("start called, blocking mode.\n");
|
||||||
|
else
|
||||||
|
printf("start called, non-blocking mode.\n");
|
||||||
|
|
||||||
|
return (isc_keyboard_open(kbd));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
stop(isc_entropysource_t *source, void *arg) {
|
stop(isc_entropysource_t *source, void *arg) {
|
||||||
|
isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
|
||||||
|
|
||||||
|
UNUSED(source);
|
||||||
|
|
||||||
|
printf("ENOUGH! Stop typing, please.\r\n");
|
||||||
|
|
||||||
|
(void)isc_keyboard_close(kbd, 3);
|
||||||
printf("stop called\n");
|
printf("stop called\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,29 +83,40 @@ stop(isc_entropysource_t *source, void *arg) {
|
|||||||
*/
|
*/
|
||||||
static isc_result_t
|
static isc_result_t
|
||||||
get(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
|
get(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
|
||||||
|
isc_keyboard_t *kbd = (isc_keyboard_t *)arg;
|
||||||
isc_result_t result;
|
isc_result_t result;
|
||||||
static isc_uint32_t val = 1;
|
isc_time_t t;
|
||||||
static int count = 0;
|
isc_uint32_t sample;
|
||||||
|
isc_uint32_t extra;
|
||||||
|
unsigned char c;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Here, we should check to see if we are in blocking mode or not.
|
* Here, we should check to see if we are in blocking mode or not.
|
||||||
* If we will block and the application asked us not to,
|
* If we will block and the application asked us not to,
|
||||||
* we should return an error instead, rather than block.
|
* we should return an error instead, rather than block.
|
||||||
*/
|
*/
|
||||||
if (!blocking) {
|
if (!blocking)
|
||||||
count++;
|
return (ISC_R_NOENTROPY);
|
||||||
if (count > 6)
|
|
||||||
return (ISC_R_NOENTROPY);
|
result = isc_keyboard_getchar(kbd, &c);
|
||||||
|
if (result != ISC_R_SUCCESS)
|
||||||
|
return (result);
|
||||||
|
|
||||||
|
result = isc_time_now(&t);
|
||||||
|
if (result != ISC_R_SUCCESS)
|
||||||
|
return (result);
|
||||||
|
|
||||||
|
sample = isc_time_nanoseconds(&t);
|
||||||
|
extra = c;
|
||||||
|
|
||||||
|
result = isc_entropy_addcallbacksample(source, sample, extra);
|
||||||
|
if (result != ISC_R_SUCCESS) {
|
||||||
|
printf("\r\n");
|
||||||
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
printf(".");
|
||||||
if (val == 0)
|
fflush(stdout);
|
||||||
val = 0x12345678;
|
|
||||||
val <<= 3;
|
|
||||||
val %= 100000;
|
|
||||||
|
|
||||||
result = isc_entropy_addcallbacksample(source, val, 0);
|
|
||||||
} while (result == ISC_R_SUCCESS);
|
|
||||||
|
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
@@ -104,6 +130,7 @@ main(int argc, char **argv) {
|
|||||||
unsigned int returned;
|
unsigned int returned;
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
isc_result_t result;
|
isc_result_t result;
|
||||||
|
isc_keyboard_t kbd;
|
||||||
|
|
||||||
UNUSED(argc);
|
UNUSED(argc);
|
||||||
UNUSED(argv);
|
UNUSED(argv);
|
||||||
@@ -119,7 +146,7 @@ main(int argc, char **argv) {
|
|||||||
isc_entropy_stats(ent, stderr);
|
isc_entropy_stats(ent, stderr);
|
||||||
|
|
||||||
source = NULL;
|
source = NULL;
|
||||||
result = isc_entropy_createcallbacksource(ent, start, get, stop, NULL,
|
result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd,
|
||||||
&source);
|
&source);
|
||||||
CHECK("isc_entropy_createcallbacksource()", result);
|
CHECK("isc_entropy_createcallbacksource()", result);
|
||||||
|
|
||||||
@@ -129,14 +156,15 @@ main(int argc, char **argv) {
|
|||||||
flags = 0;
|
flags = 0;
|
||||||
flags |= ISC_ENTROPY_GOODONLY;
|
flags |= ISC_ENTROPY_GOODONLY;
|
||||||
flags |= ISC_ENTROPY_PARTIAL;
|
flags |= ISC_ENTROPY_PARTIAL;
|
||||||
#if 0
|
|
||||||
flags |= ISC_ENTROPY_BLOCKING;
|
flags |= ISC_ENTROPY_BLOCKING;
|
||||||
#endif
|
|
||||||
returned = 0;
|
returned = 0;
|
||||||
result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
|
result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
|
||||||
if (result == ISC_R_NOENTROPY) {
|
if (result == ISC_R_NOENTROPY) {
|
||||||
fprintf(stderr, "No entropy.\n");
|
fprintf(stderr, "No entropy.\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isc_entropy_stopcallbacksources(ent);
|
||||||
|
|
||||||
hex_dump("good data only:", buffer, returned);
|
hex_dump("good data only:", buffer, returned);
|
||||||
|
|
||||||
isc_entropy_stats(ent, stderr);
|
isc_entropy_stats(ent, stderr);
|
||||||
|
Reference in New Issue
Block a user