minix/lib/libsys/arch/i386/get_randomness.c
Ben Gras 717425320f kernel, random: adaptive entropy gathering
kernel: stop gathering timestamps once the bin is full per interrupt

random: once seeded, retrieve new entropy at a lower rate

Change-Id: I4ce6081d39274728d82c6889686d1650cfd5fc2e
2013-05-07 17:19:06 +00:00

35 lines
1 KiB
C

#include <lib.h>
#include <stdlib.h>
#include <string.h>
#include <minix/profile.h>
#include <minix/syslib.h>
#include <minix/type.h>
#include <minix/minlib.h>
#include <minix/sysutil.h>
/*===========================================================================*
* get_randomness *
*===========================================================================*/
void get_randomness(rand, source)
struct k_randomness *rand;
int source;
{
/* Use architecture-dependent high-resolution clock for
* raw entropy gathering.
*/
int r_next;
unsigned long tsc_high, tsc_low;
source %= RANDOM_SOURCES;
if (rand->bin[source].r_size >= RANDOM_ELEMENTS) return;
r_next= rand->bin[source].r_next;
read_tsc((u32_t *) &tsc_high, (u32_t *) &tsc_low);
rand->bin[source].r_buf[r_next] = tsc_low;
if (rand->bin[source].r_size < RANDOM_ELEMENTS) {
rand->bin[source].r_size ++;
}
rand->bin[source].r_next = (r_next + 1 ) % RANDOM_ELEMENTS;
}