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