xref: /minix3/minix/lib/libsys/arch/i386/get_randomness.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include <lib.h>
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc #include <stdlib.h>
4*433d6423SLionel Sambuc #include <string.h>
5*433d6423SLionel Sambuc #include <minix/profile.h>
6*433d6423SLionel Sambuc #include <minix/syslib.h>
7*433d6423SLionel Sambuc #include <minix/type.h>
8*433d6423SLionel Sambuc #include <minix/minlib.h>
9*433d6423SLionel Sambuc #include <minix/sysutil.h>
10*433d6423SLionel Sambuc 
11*433d6423SLionel Sambuc /*===========================================================================*
12*433d6423SLionel Sambuc  *                              get_randomness                               *
13*433d6423SLionel Sambuc  *===========================================================================*/
get_randomness(rand,source)14*433d6423SLionel Sambuc void get_randomness(rand, source)
15*433d6423SLionel Sambuc struct k_randomness *rand;
16*433d6423SLionel Sambuc int source;
17*433d6423SLionel Sambuc {
18*433d6423SLionel Sambuc /* Use architecture-dependent high-resolution clock for
19*433d6423SLionel Sambuc  * raw entropy gathering.
20*433d6423SLionel Sambuc  */
21*433d6423SLionel Sambuc   int r_next;
22*433d6423SLionel Sambuc   unsigned long tsc_high, tsc_low;
23*433d6423SLionel Sambuc 
24*433d6423SLionel Sambuc   source %= RANDOM_SOURCES;
25*433d6423SLionel Sambuc   if (rand->bin[source].r_size >= RANDOM_ELEMENTS) return;
26*433d6423SLionel Sambuc   r_next= rand->bin[source].r_next;
27*433d6423SLionel Sambuc   read_tsc((u32_t *) &tsc_high, (u32_t *) &tsc_low);
28*433d6423SLionel Sambuc   rand->bin[source].r_buf[r_next] = tsc_low;
29*433d6423SLionel Sambuc   if (rand->bin[source].r_size < RANDOM_ELEMENTS) {
30*433d6423SLionel Sambuc         rand->bin[source].r_size ++;
31*433d6423SLionel Sambuc   }
32*433d6423SLionel Sambuc   rand->bin[source].r_next = (r_next + 1 ) % RANDOM_ELEMENTS;
33*433d6423SLionel Sambuc }
34*433d6423SLionel Sambuc 
35*433d6423SLionel Sambuc 
36