kernel, random: adaptive entropy gathering
[minix3.git] / drivers / random / main.c
blob18ed36d34dcf5977fc1f4fe26d714a39b5acf08f
1 /* This file contains the device dependent part of the drivers for the
2 * following special files:
3 * /dev/random - random number generator
4 */
6 #include <minix/drivers.h>
7 #include <minix/chardriver.h>
8 #include <minix/type.h>
10 #include "assert.h"
11 #include "random.h"
13 #define NR_DEVS 1 /* number of minor devices */
14 # define RANDOM_DEV 0 /* minor device for /dev/random */
16 #define KRANDOM_PERIOD 1 /* ticks between krandom calls */
18 static struct device m_geom[NR_DEVS]; /* base and size of each device */
19 static dev_t m_device; /* current device */
21 extern int errno; /* error number for PM calls */
23 static struct device *r_prepare(dev_t device);
24 static int r_transfer(endpoint_t endpt, int opcode, u64_t position,
25 iovec_t *iov, unsigned int nr_req, endpoint_t user_endpt, unsigned int
26 flags);
27 static int r_do_open(message *m_ptr);
28 static void r_random(message *m_ptr);
29 static void r_updatebin(int source, struct k_randomness_bin *rb);
31 /* Entry points to this driver. */
32 static struct chardriver r_dtab = {
33 r_do_open, /* open or mount */
34 do_nop, /* nothing on a close */
35 nop_ioctl, /* no I/O controls supported */
36 r_prepare, /* prepare for I/O on a given minor device */
37 r_transfer, /* do the I/O */
38 nop_cleanup, /* no need to clean up */
39 r_random, /* get randomness from kernel (alarm) */
40 nop_cancel, /* cancel not supported */
41 nop_select, /* select not supported */
42 NULL, /* other messages not supported */
45 /* Buffer for the /dev/random number generator. */
46 #define RANDOM_BUF_SIZE 1024
47 static char random_buf[RANDOM_BUF_SIZE];
49 /* SEF functions and variables. */
50 static void sef_local_startup(void);
51 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
53 /*===========================================================================*
54 * main *
55 *===========================================================================*/
56 int main(void)
58 /* SEF local startup. */
59 sef_local_startup();
61 /* Call the generic receive loop. */
62 chardriver_task(&r_dtab, CHARDRIVER_ASYNC);
64 return(OK);
67 /*===========================================================================*
68 * sef_local_startup *
69 *===========================================================================*/
70 static void sef_local_startup()
72 /* Register init callbacks. */
73 sef_setcb_init_fresh(sef_cb_init_fresh);
74 sef_setcb_init_lu(sef_cb_init_fresh);
75 sef_setcb_init_restart(sef_cb_init_fresh);
77 /* Register live update callbacks. */
78 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
79 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard);
81 /* Let SEF perform startup. */
82 sef_startup();
85 /*===========================================================================*
86 * sef_cb_init_fresh *
87 *===========================================================================*/
88 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
90 /* Initialize the random driver. */
91 static struct k_randomness krandom;
92 int i, s;
94 random_init();
95 r_random(NULL); /* also set periodic timer */
97 /* Retrieve first randomness buffer with parameters. */
98 if (OK != (s=sys_getrandomness(&krandom))) {
99 printf("RANDOM: sys_getrandomness failed: %d\n", s);
100 exit(1);
103 /* Do sanity check on parameters. */
104 if(krandom.random_sources != RANDOM_SOURCES ||
105 krandom.random_elements != RANDOM_ELEMENTS) {
106 printf("random: parameters (%d, %d) don't match kernel's (%d, %d)\n",
107 RANDOM_SOURCES, RANDOM_ELEMENTS,
108 krandom.random_sources, krandom.random_elements);
109 exit(1);
112 /* Feed initial batch. */
113 for(i = 0; i < RANDOM_SOURCES; i++)
114 r_updatebin(i, &krandom.bin[i]);
116 /* Announce we are up! */
117 chardriver_announce();
119 return(OK);
122 /*===========================================================================*
123 * r_prepare *
124 *===========================================================================*/
125 static struct device *r_prepare(dev_t device)
127 /* Prepare for I/O on a device: check if the minor device number is ok. */
129 if (device >= NR_DEVS) return(NULL);
130 m_device = device;
132 return(&m_geom[device]);
135 /*===========================================================================*
136 * r_transfer *
137 *===========================================================================*/
138 static int r_transfer(
139 endpoint_t endpt, /* endpoint of grant owner */
140 int opcode, /* DEV_GATHER or DEV_SCATTER */
141 u64_t position, /* offset on device to read or write */
142 iovec_t *iov, /* pointer to read or write request vector */
143 unsigned int nr_req, /* length of request vector */
144 endpoint_t UNUSED(user_endpt),/* endpoint of user process */
145 unsigned int UNUSED(flags)
148 /* Read or write one the driver's minor devices. */
149 unsigned count, left, chunk;
150 cp_grant_id_t grant;
151 struct device *dv;
152 int r;
153 size_t vir_offset = 0;
155 /* Get minor device number and check for /dev/null. */
156 dv = &m_geom[m_device];
158 while (nr_req > 0) {
160 /* How much to transfer and where to / from. */
161 count = iov->iov_size;
162 grant = (cp_grant_id_t) iov->iov_addr;
164 switch (m_device) {
166 /* Random number generator. Character instead of block device. */
167 case RANDOM_DEV:
168 if (opcode == DEV_GATHER_S && !random_isseeded())
169 return(EAGAIN);
170 left = count;
171 while (left > 0) {
172 chunk = (left > RANDOM_BUF_SIZE) ? RANDOM_BUF_SIZE : left;
173 if (opcode == DEV_GATHER_S) {
174 random_getbytes(random_buf, chunk);
175 r= sys_safecopyto(endpt, grant, vir_offset,
176 (vir_bytes) random_buf, chunk);
177 if (r != OK)
179 printf("random: sys_safecopyto failed for proc %d, "
180 "grant %d\n", endpt, grant);
181 return r;
183 } else if (opcode == DEV_SCATTER_S) {
184 r= sys_safecopyfrom(endpt, grant, vir_offset,
185 (vir_bytes) random_buf, chunk);
186 if (r != OK)
188 printf("random: sys_safecopyfrom failed for proc %d, "
189 "grant %d\n", endpt, grant);
190 return r;
192 random_putbytes(random_buf, chunk);
194 vir_offset += chunk;
195 left -= chunk;
197 break;
199 /* Unknown (illegal) minor device. */
200 default:
201 return(EINVAL);
204 /* Book the number of bytes transferred. */
205 position= add64u(position, count);
206 if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
209 return(OK);
212 /*===========================================================================*
213 * r_do_open *
214 *===========================================================================*/
215 static int r_do_open(message *m_ptr)
217 /* Check device number on open.
219 if (r_prepare(m_ptr->DEVICE) == NULL) return(ENXIO);
221 return(OK);
224 #define UPDATE(binnumber, bp, startitem, elems) { \
225 rand_t *r; \
226 int n = elems, item = startitem;\
227 int high; \
228 assert(binnumber >= 0 && binnumber < RANDOM_SOURCES); \
229 assert(item >= 0 && item < RANDOM_ELEMENTS); \
230 if(n > 0) { \
231 high = item+n-1; \
232 assert(high >= item); \
233 assert(high >= 0 && high < RANDOM_ELEMENTS); \
234 r = &bp->r_buf[item]; \
235 random_update(binnumber, r, n); \
239 /*===========================================================================*
240 * r_updatebin *
241 *===========================================================================*/
242 static void r_updatebin(int source, struct k_randomness_bin *rb)
244 int r_next, r_size, r_high;
246 r_next= rb->r_next;
247 r_size= rb->r_size;
249 assert(r_next >= 0 && r_next < RANDOM_ELEMENTS);
250 assert(r_size >= 0 && r_size <= RANDOM_ELEMENTS);
252 r_high= r_next+r_size;
254 if (r_high <= RANDOM_ELEMENTS) {
255 UPDATE(source, rb, r_next, r_size);
256 } else {
257 assert(r_next < RANDOM_ELEMENTS);
258 UPDATE(source, rb, r_next, RANDOM_ELEMENTS-r_next);
259 UPDATE(source, rb, 0, r_high-RANDOM_ELEMENTS);
262 return;
265 /*===========================================================================*
266 * r_random *
267 *===========================================================================*/
268 static void r_random(message *UNUSED(m_ptr))
270 /* Fetch random information from the kernel to update /dev/random. */
271 int s;
272 static int bin = 0;
273 static struct k_randomness_bin krandom_bin;
274 u32_t hi, lo;
275 rand_t r;
276 int nextperiod = random_isseeded() ? KRANDOM_PERIOD*500 : KRANDOM_PERIOD;
278 bin = (bin+1) % RANDOM_SOURCES;
280 if(sys_getrandom_bin(&krandom_bin, bin) == OK)
281 r_updatebin(bin, &krandom_bin);
283 /* Add our own timing source. */
284 read_tsc(&hi, &lo);
285 r = lo;
286 random_update(RND_TIMING, &r, 1);
288 /* Schedule new alarm for next m_random call. */
289 if (OK != (s=sys_setalarm(nextperiod, 0)))
290 printf("RANDOM: sys_setalarm failed: %d\n", s);