rndctl.c revision 1.31 1 1.31 riastrad /* $NetBSD: rndctl.c,v 1.31 2019/12/06 14:43:18 riastradh Exp $ */
2 1.3 perry
3 1.1 explorer /*-
4 1.1 explorer * Copyright (c) 1997 Michael Graff.
5 1.1 explorer * All rights reserved.
6 1.1 explorer *
7 1.1 explorer * Redistribution and use in source and binary forms, with or without
8 1.1 explorer * modification, are permitted provided that the following conditions
9 1.1 explorer * are met:
10 1.1 explorer * 1. Redistributions of source code must retain the above copyright
11 1.1 explorer * notice, this list of conditions and the following disclaimer.
12 1.1 explorer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 explorer * notice, this list of conditions and the following disclaimer in the
14 1.1 explorer * documentation and/or other materials provided with the distribution.
15 1.1 explorer * 3. Neither the name of the author nor the names of other contributors
16 1.1 explorer * may be used to endorse or promote products derived from this software
17 1.1 explorer * without specific prior written permission.
18 1.1 explorer *
19 1.1 explorer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 explorer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 explorer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 explorer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 explorer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.1 explorer * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 explorer * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.1 explorer * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.1 explorer * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 explorer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 explorer * SUCH DAMAGE.
30 1.1 explorer */
31 1.15 agc #include <sys/cdefs.h>
32 1.21 tls #include <sys/types.h>
33 1.21 tls #include <sha1.h>
34 1.15 agc
35 1.15 agc #ifndef lint
36 1.31 riastrad __RCSID("$NetBSD: rndctl.c,v 1.31 2019/12/06 14:43:18 riastradh Exp $");
37 1.15 agc #endif
38 1.15 agc
39 1.1 explorer
40 1.11 enami #include <sys/types.h>
41 1.11 enami #include <sys/ioctl.h>
42 1.21 tls #include <sys/param.h>
43 1.30 riastrad #include <sys/rndio.h>
44 1.31 riastrad #include <sys/sha3.h>
45 1.11 enami
46 1.1 explorer #include <stdio.h>
47 1.1 explorer #include <stdlib.h>
48 1.1 explorer #include <unistd.h>
49 1.1 explorer #include <fcntl.h>
50 1.1 explorer #include <errno.h>
51 1.1 explorer #include <err.h>
52 1.25 jruoho #include <paths.h>
53 1.2 explorer #include <string.h>
54 1.1 explorer
55 1.1 explorer typedef struct {
56 1.17 christos const char *a_name;
57 1.9 enami u_int32_t a_type;
58 1.1 explorer } arg_t;
59 1.1 explorer
60 1.20 joerg static const arg_t source_types[] = {
61 1.6 sommerfe { "???", RND_TYPE_UNKNOWN },
62 1.1 explorer { "disk", RND_TYPE_DISK },
63 1.1 explorer { "net", RND_TYPE_NET },
64 1.1 explorer { "tape", RND_TYPE_TAPE },
65 1.1 explorer { "tty", RND_TYPE_TTY },
66 1.11 enami { "rng", RND_TYPE_RNG },
67 1.24 tls { "skew", RND_TYPE_SKEW },
68 1.24 tls { "env", RND_TYPE_ENV },
69 1.24 tls { "vm", RND_TYPE_VM },
70 1.24 tls { "power", RND_TYPE_POWER },
71 1.1 explorer { NULL, 0 }
72 1.1 explorer };
73 1.1 explorer
74 1.20 joerg __dead static void usage(void);
75 1.20 joerg static u_int32_t find_type(const char *name);
76 1.20 joerg static const char *find_name(u_int32_t);
77 1.20 joerg static void do_ioctl(rndctl_t *);
78 1.20 joerg static char * strflags(u_int32_t);
79 1.20 joerg static void do_list(int, u_int32_t, char *);
80 1.20 joerg static void do_stats(void);
81 1.2 explorer
82 1.28 tls static int vflag;
83 1.28 tls
84 1.2 explorer static void
85 1.1 explorer usage(void)
86 1.1 explorer {
87 1.9 enami
88 1.29 wiz fprintf(stderr, "usage: %s [-CEce] [-d devname | -t devtype]\n",
89 1.11 enami getprogname());
90 1.29 wiz fprintf(stderr, " %s [-lsv] [-d devname | -t devtype]\n",
91 1.11 enami getprogname());
92 1.21 tls fprintf(stderr, " %s -[L|S] save-file\n", getprogname());
93 1.5 mycroft exit(1);
94 1.1 explorer }
95 1.1 explorer
96 1.20 joerg static u_int32_t
97 1.20 joerg find_type(const char *name)
98 1.1 explorer {
99 1.20 joerg const arg_t *a;
100 1.1 explorer
101 1.1 explorer a = source_types;
102 1.9 enami
103 1.9 enami while (a->a_name != NULL) {
104 1.9 enami if (strcmp(a->a_name, name) == 0)
105 1.9 enami return (a->a_type);
106 1.1 explorer a++;
107 1.1 explorer }
108 1.1 explorer
109 1.10 enami errx(1, "device name %s unknown", name);
110 1.9 enami return (0);
111 1.1 explorer }
112 1.1 explorer
113 1.20 joerg static const char *
114 1.1 explorer find_name(u_int32_t type)
115 1.1 explorer {
116 1.20 joerg const arg_t *a;
117 1.1 explorer
118 1.1 explorer a = source_types;
119 1.9 enami
120 1.9 enami while (a->a_name != NULL) {
121 1.9 enami if (type == a->a_type)
122 1.9 enami return (a->a_name);
123 1.1 explorer a++;
124 1.1 explorer }
125 1.1 explorer
126 1.10 enami warnx("device type %u unknown", type);
127 1.10 enami return ("???");
128 1.1 explorer }
129 1.1 explorer
130 1.20 joerg static void
131 1.31 riastrad do_save(const char *filename, const void *extra, size_t nextra,
132 1.31 riastrad uint32_t extraentropy)
133 1.21 tls {
134 1.31 riastrad char tmp[PATH_MAX];
135 1.31 riastrad uint32_t systementropy;
136 1.31 riastrad uint8_t buf[32];
137 1.31 riastrad SHAKE128_CTX shake128;
138 1.21 tls rndsave_t rs;
139 1.21 tls SHA1_CTX s;
140 1.31 riastrad ssize_t nread, nwrit;
141 1.21 tls int fd;
142 1.21 tls
143 1.31 riastrad /* Paranoia: Avoid stack memory disclosure. */
144 1.31 riastrad memset(&rs, 0, sizeof rs);
145 1.25 jruoho
146 1.31 riastrad /* Format the temporary file name. */
147 1.31 riastrad if (snprintf(tmp, sizeof tmp, "%s.tmp", filename) >= PATH_MAX)
148 1.31 riastrad errx(1, "path too long");
149 1.21 tls
150 1.31 riastrad /* Open /dev/urandom. */
151 1.31 riastrad if ((fd = open(_PATH_URANDOM, O_RDONLY)) == -1)
152 1.31 riastrad err(1, "device open");
153 1.21 tls
154 1.31 riastrad /* Find how much entropy is in the pool. */
155 1.31 riastrad if (ioctl(fd, RNDGETENTCNT, &systementropy) == -1)
156 1.31 riastrad err(1, "ioctl(RNDGETENTCNT)");
157 1.31 riastrad
158 1.31 riastrad /* Read some data from /dev/urandom. */
159 1.31 riastrad if ((size_t)(nread = read(fd, buf, sizeof buf)) != sizeof buf) {
160 1.31 riastrad if (nread == -1)
161 1.31 riastrad err(1, "read");
162 1.31 riastrad else
163 1.31 riastrad errx(1, "truncated read");
164 1.31 riastrad }
165 1.31 riastrad
166 1.31 riastrad /* Close /dev/urandom; we're done with it. */
167 1.31 riastrad if (close(fd) == -1)
168 1.31 riastrad warn("close");
169 1.31 riastrad fd = -1; /* paranoia */
170 1.21 tls
171 1.31 riastrad /*
172 1.31 riastrad * Hash what we read together with the extra input to generate
173 1.31 riastrad * the seed data.
174 1.31 riastrad */
175 1.31 riastrad SHAKE128_Init(&shake128);
176 1.31 riastrad SHAKE128_Update(&shake128, buf, sizeof buf);
177 1.31 riastrad SHAKE128_Update(&shake128, extra, nextra);
178 1.31 riastrad SHAKE128_Final(rs.data, sizeof(rs.data), &shake128);
179 1.31 riastrad explicit_memset(&shake128, 0, sizeof shake128); /* paranoia */
180 1.21 tls
181 1.31 riastrad /*
182 1.31 riastrad * Report an upper bound on the min-entropy of the seed data.
183 1.31 riastrad * We take the larger of the system entropy and the extra
184 1.31 riastrad * entropy -- the system state and the extra input may or may
185 1.31 riastrad * not be independent, so we can't add them -- and clamp to the
186 1.31 riastrad * size of the data.
187 1.31 riastrad */
188 1.31 riastrad systementropy = MIN(systementropy,
189 1.31 riastrad MIN(sizeof(buf), UINT32_MAX/NBBY)*NBBY);
190 1.31 riastrad extraentropy = MIN(extraentropy, MIN(nextra, UINT32_MAX/NBBY)*NBBY);
191 1.31 riastrad rs.entropy = MIN(MAX(systementropy, extraentropy),
192 1.31 riastrad MIN(sizeof(rs.data), UINT32_MAX/NBBY)*NBBY);
193 1.21 tls
194 1.31 riastrad /*
195 1.31 riastrad * Compute the checksum on the 32-bit entropy count, in host
196 1.31 riastrad * byte order (XXX this means it is not portable across
197 1.31 riastrad * different-endian platforms!), followed by the seed data.
198 1.31 riastrad */
199 1.21 tls SHA1Init(&s);
200 1.31 riastrad SHA1Update(&s, (const uint8_t *)&rs.entropy, sizeof(rs.entropy));
201 1.21 tls SHA1Update(&s, rs.data, sizeof(rs.data));
202 1.21 tls SHA1Final(rs.digest, &s);
203 1.31 riastrad explicit_memset(&s, 0, sizeof s); /* paranoia */
204 1.21 tls
205 1.31 riastrad /*
206 1.31 riastrad * Write it to a temporary file and sync it before we commit.
207 1.31 riastrad * This way either the old seed or the new seed is completely
208 1.31 riastrad * written in the expected location on disk even if the system
209 1.31 riastrad * crashes as long as the file system doesn't get corrupted too
210 1.31 riastrad * badly.
211 1.31 riastrad *
212 1.31 riastrad * If interrupted after this point and the temporary file is
213 1.31 riastrad * disclosed, no big deal -- either the pool was predictable to
214 1.31 riastrad * begin with in which case we're hosed either way, or we've
215 1.31 riastrad * just revealed some output which is not a problem.
216 1.31 riastrad */
217 1.31 riastrad if ((fd = open(tmp, O_CREAT|O_TRUNC|O_WRONLY, 0600)) == -1)
218 1.31 riastrad err(1, "open seed file to save");
219 1.31 riastrad if ((size_t)(nwrit = write(fd, &rs, sizeof rs)) != sizeof rs) {
220 1.31 riastrad int error = errno;
221 1.31 riastrad if (unlink(tmp) == -1)
222 1.31 riastrad warn("unlink");
223 1.31 riastrad if (nwrit == -1)
224 1.31 riastrad errc(1, error, "write");
225 1.31 riastrad else
226 1.31 riastrad errx(1, "truncated write");
227 1.31 riastrad }
228 1.31 riastrad explicit_memset(&rs, 0, sizeof rs); /* paranoia */
229 1.31 riastrad if (fsync_range(fd, FDATASYNC|FDISKSYNC, 0, 0) == -1) {
230 1.31 riastrad int error = errno;
231 1.31 riastrad if (unlink(tmp) == -1)
232 1.31 riastrad warn("unlink");
233 1.31 riastrad errc(1, error, "fsync_range");
234 1.31 riastrad }
235 1.31 riastrad if (close(fd) == -1)
236 1.31 riastrad warn("close");
237 1.31 riastrad
238 1.31 riastrad /* Rename it over the original file to commit. */
239 1.31 riastrad if (rename(tmp, filename) == -1)
240 1.31 riastrad err(1, "rename");
241 1.21 tls }
242 1.21 tls
243 1.21 tls static void
244 1.31 riastrad do_load(const char *filename)
245 1.21 tls {
246 1.31 riastrad char tmp[PATH_MAX];
247 1.31 riastrad int fd_seed, fd_random;
248 1.31 riastrad rndsave_t rs;
249 1.21 tls rnddata_t rd;
250 1.31 riastrad ssize_t nread, nwrit;
251 1.21 tls SHA1_CTX s;
252 1.21 tls uint8_t digest[SHA1_DIGEST_LENGTH];
253 1.21 tls
254 1.31 riastrad /*
255 1.31 riastrad * The order of operations is important here:
256 1.31 riastrad *
257 1.31 riastrad * 1. Load the old seed.
258 1.31 riastrad * 2. Feed the old seed into the kernel.
259 1.31 riastrad * 3. Generate and write a new seed.
260 1.31 riastrad * 4. Erase the old seed.
261 1.31 riastrad *
262 1.31 riastrad * This follows the procedure in
263 1.31 riastrad *
264 1.31 riastrad * Niels Ferguson, Bruce Schneier, and Tadayoshi Kohno,
265 1.31 riastrad * _Cryptography Engineering_, Wiley, 2010, Sec. 9.6.2
266 1.31 riastrad * `Update Seed File'.
267 1.31 riastrad *
268 1.31 riastrad * There is a race condition: If another process generates a
269 1.31 riastrad * key from /dev/urandom after step (2) but before step (3),
270 1.31 riastrad * and if the machine crashes before step (3), an adversary who
271 1.31 riastrad * can read the disk after the crash can probably guess the
272 1.31 riastrad * complete state of the entropy pool and thereby predict the
273 1.31 riastrad * key.
274 1.31 riastrad *
275 1.31 riastrad * There's not much we can do here without some kind of
276 1.31 riastrad * systemwide lock on /dev/urandom and without introducing an
277 1.31 riastrad * opportunity for a crash to wipe out the entropy altogether.
278 1.31 riastrad * To avoid this race, you should ensure that any key
279 1.31 riastrad * generation happens _after_ `rndctl -L' has completed.
280 1.31 riastrad */
281 1.21 tls
282 1.31 riastrad /* Format the temporary file name. */
283 1.31 riastrad if (snprintf(tmp, sizeof tmp, "%s.tmp", filename) >= PATH_MAX)
284 1.31 riastrad errx(1, "path too long");
285 1.31 riastrad
286 1.31 riastrad /* 1. Load the old seed. */
287 1.31 riastrad if ((fd_seed = open(filename, O_RDWR)) == -1)
288 1.31 riastrad err(1, "open seed file to load");
289 1.31 riastrad if ((size_t)(nread = read(fd_seed, &rs, sizeof rs)) != sizeof rs) {
290 1.31 riastrad if (nread == -1)
291 1.31 riastrad err(1, "read seed");
292 1.31 riastrad else
293 1.31 riastrad errx(1, "seed too short");
294 1.21 tls }
295 1.21 tls
296 1.31 riastrad /* Verify its checksum. */
297 1.21 tls SHA1Init(&s);
298 1.31 riastrad SHA1Update(&s, (const uint8_t *)&rs.entropy, sizeof(rs.entropy));
299 1.21 tls SHA1Update(&s, rs.data, sizeof(rs.data));
300 1.21 tls SHA1Final(digest, &s);
301 1.31 riastrad if (!consttime_memequal(digest, rs.digest, sizeof(digest))) {
302 1.31 riastrad /*
303 1.31 riastrad * If the checksum doesn't match, doesn't hurt to feed
304 1.31 riastrad * the seed in anyway, but act as though it has zero
305 1.31 riastrad * entropy in case it was corrupted with predictable
306 1.31 riastrad * garbage.
307 1.31 riastrad */
308 1.31 riastrad warnx("bad checksum");
309 1.31 riastrad rs.entropy = 0;
310 1.21 tls }
311 1.21 tls
312 1.31 riastrad /* Format the ioctl request. */
313 1.21 tls rd.len = MIN(sizeof(rd.data), sizeof(rs.data));
314 1.21 tls rd.entropy = rs.entropy;
315 1.31 riastrad memcpy(rd.data, rs.data, rd.len);
316 1.31 riastrad explicit_memset(&rs, 0, sizeof rs); /* paranoia */
317 1.31 riastrad
318 1.31 riastrad /* 2. Feed the old seed into the kernel. */
319 1.31 riastrad if ((fd_random = open(_PATH_URANDOM, O_WRONLY)) == -1)
320 1.31 riastrad err(1, "open /dev/urandom");
321 1.31 riastrad if (ioctl(fd_random, RNDADDDATA, &rd) == -1)
322 1.31 riastrad err(1, "RNDADDDATA");
323 1.31 riastrad if (close(fd_random) == -1)
324 1.31 riastrad warn("close /dev/urandom");
325 1.31 riastrad fd_random = -1; /* paranoia */
326 1.21 tls
327 1.31 riastrad /*
328 1.31 riastrad * 3. Generate and write a new seed. Note that we hash the old
329 1.31 riastrad * seed together with whatever /dev/urandom returns in do_save.
330 1.31 riastrad * Why? After RNDADDDATA, the input may not be distributed
331 1.31 riastrad * immediately to /dev/urandom.
332 1.31 riastrad */
333 1.31 riastrad do_save(filename, rd.data, rd.len, rd.entropy);
334 1.31 riastrad explicit_memset(&rd, 0, sizeof rd); /* paranoia */
335 1.21 tls
336 1.31 riastrad /*
337 1.31 riastrad * 4. Erase the old seed. Only effective if we're on a
338 1.31 riastrad * fixed-address file system like ffs -- doesn't help to erase
339 1.31 riastrad * the data on lfs, but doesn't hurt either. No need to unlink
340 1.31 riastrad * because do_save will have already overwritten it.
341 1.31 riastrad */
342 1.31 riastrad memset(&rs, 0, sizeof rs);
343 1.31 riastrad if ((size_t)(nwrit = pwrite(fd_seed, &rs, sizeof rs, 0)) !=
344 1.31 riastrad sizeof rs) {
345 1.31 riastrad if (nwrit == -1)
346 1.31 riastrad err(1, "overwrite old seed");
347 1.31 riastrad else
348 1.31 riastrad errx(1, "truncated overwrite");
349 1.21 tls }
350 1.31 riastrad if (fsync_range(fd_seed, FDATASYNC|FDISKSYNC, 0, 0) == -1)
351 1.31 riastrad err(1, "fsync_range");
352 1.21 tls }
353 1.21 tls
354 1.21 tls static void
355 1.1 explorer do_ioctl(rndctl_t *rctl)
356 1.1 explorer {
357 1.1 explorer int fd;
358 1.1 explorer int res;
359 1.1 explorer
360 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
361 1.1 explorer if (fd < 0)
362 1.1 explorer err(1, "open");
363 1.1 explorer
364 1.1 explorer res = ioctl(fd, RNDCTL, rctl);
365 1.1 explorer if (res < 0)
366 1.1 explorer err(1, "ioctl(RNDCTL)");
367 1.1 explorer
368 1.1 explorer close(fd);
369 1.1 explorer }
370 1.1 explorer
371 1.20 joerg static char *
372 1.1 explorer strflags(u_int32_t fl)
373 1.1 explorer {
374 1.1 explorer static char str[512];
375 1.1 explorer
376 1.28 tls str[0] = '\0';
377 1.1 explorer if (fl & RND_FLAG_NO_ESTIMATE)
378 1.6 sommerfe ;
379 1.9 enami else
380 1.28 tls strlcat(str, "estimate, ", sizeof(str));
381 1.9 enami
382 1.1 explorer if (fl & RND_FLAG_NO_COLLECT)
383 1.6 sommerfe ;
384 1.28 tls else
385 1.28 tls strlcat(str, "collect, ", sizeof(str));
386 1.28 tls
387 1.28 tls if (fl & RND_FLAG_COLLECT_VALUE)
388 1.28 tls strlcat(str, "v, ", sizeof(str));
389 1.28 tls if (fl & RND_FLAG_COLLECT_TIME)
390 1.28 tls strlcat(str, "t, ", sizeof(str));
391 1.28 tls if (fl & RND_FLAG_ESTIMATE_VALUE)
392 1.28 tls strlcat(str, "dv, ", sizeof(str));
393 1.28 tls if (fl & RND_FLAG_ESTIMATE_TIME)
394 1.28 tls strlcat(str, "dt, ", sizeof(str));
395 1.28 tls
396 1.28 tls if (str[strlen(str) - 2] == ',')
397 1.28 tls str[strlen(str) - 2] = '\0';
398 1.9 enami
399 1.9 enami return (str);
400 1.1 explorer }
401 1.1 explorer
402 1.6 sommerfe #define HEADER "Source Bits Type Flags\n"
403 1.1 explorer
404 1.20 joerg static void
405 1.1 explorer do_list(int all, u_int32_t type, char *name)
406 1.1 explorer {
407 1.28 tls rndstat_est_t rstat;
408 1.28 tls rndstat_est_name_t rstat_name;
409 1.9 enami int fd;
410 1.9 enami int res;
411 1.19 lukem uint32_t i;
412 1.9 enami u_int32_t start;
413 1.1 explorer
414 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
415 1.1 explorer if (fd < 0)
416 1.1 explorer err(1, "open");
417 1.1 explorer
418 1.1 explorer if (all == 0 && type == 0xff) {
419 1.14 itojun strncpy(rstat_name.name, name, sizeof(rstat_name.name));
420 1.28 tls res = ioctl(fd, RNDGETESTNAME, &rstat_name);
421 1.1 explorer if (res < 0)
422 1.28 tls err(1, "ioctl(RNDGETESTNAME)");
423 1.1 explorer printf(HEADER);
424 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
425 1.28 tls rstat_name.source.rt.name,
426 1.28 tls rstat_name.source.rt.total,
427 1.28 tls find_name(rstat_name.source.rt.type),
428 1.28 tls strflags(rstat_name.source.rt.flags));
429 1.28 tls if (vflag) {
430 1.28 tls printf("\tDt samples = %d\n",
431 1.28 tls rstat_name.source.dt_samples);
432 1.28 tls printf("\tDt bits = %d\n",
433 1.28 tls rstat_name.source.dt_total);
434 1.28 tls printf("\tDv samples = %d\n",
435 1.28 tls rstat_name.source.dv_samples);
436 1.28 tls printf("\tDv bits = %d\n",
437 1.28 tls rstat_name.source.dv_total);
438 1.28 tls }
439 1.1 explorer close(fd);
440 1.1 explorer return;
441 1.1 explorer }
442 1.1 explorer
443 1.1 explorer /*
444 1.9 enami * Run through all the devices present in the system, and either
445 1.1 explorer * print out ones that match, or print out all of them.
446 1.1 explorer */
447 1.1 explorer printf(HEADER);
448 1.1 explorer start = 0;
449 1.1 explorer for (;;) {
450 1.1 explorer rstat.count = RND_MAXSTATCOUNT;
451 1.1 explorer rstat.start = start;
452 1.28 tls res = ioctl(fd, RNDGETESTNUM, &rstat);
453 1.1 explorer if (res < 0)
454 1.28 tls err(1, "ioctl(RNDGETESTNUM)");
455 1.9 enami
456 1.1 explorer if (rstat.count == 0)
457 1.1 explorer break;
458 1.9 enami
459 1.19 lukem for (i = 0; i < rstat.count; i++) {
460 1.9 enami if (all != 0 ||
461 1.28 tls type == rstat.source[i].rt.type)
462 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
463 1.28 tls rstat.source[i].rt.name,
464 1.28 tls rstat.source[i].rt.total,
465 1.28 tls find_name(rstat.source[i].rt.type),
466 1.28 tls strflags(rstat.source[i].rt.flags));
467 1.28 tls if (vflag) {
468 1.28 tls printf("\tDt samples = %d\n",
469 1.28 tls rstat.source[i].dt_samples);
470 1.28 tls printf("\tDt bits = %d\n",
471 1.28 tls rstat.source[i].dt_total);
472 1.28 tls printf("\tDv samples = %d\n",
473 1.28 tls rstat.source[i].dv_samples);
474 1.28 tls printf("\tDv bits = %d\n",
475 1.28 tls rstat.source[i].dv_total);
476 1.28 tls }
477 1.28 tls }
478 1.1 explorer start += rstat.count;
479 1.1 explorer }
480 1.1 explorer
481 1.1 explorer close(fd);
482 1.1 explorer }
483 1.1 explorer
484 1.20 joerg static void
485 1.20 joerg do_stats(void)
486 1.6 sommerfe {
487 1.6 sommerfe rndpoolstat_t rs;
488 1.6 sommerfe int fd;
489 1.9 enami
490 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
491 1.6 sommerfe if (fd < 0)
492 1.6 sommerfe err(1, "open");
493 1.9 enami
494 1.6 sommerfe if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
495 1.6 sommerfe err(1, "ioctl(RNDGETPOOLSTAT)");
496 1.6 sommerfe
497 1.12 enami printf("\t%9u bits mixed into pool\n", rs.added);
498 1.12 enami printf("\t%9u bits currently stored in pool (max %u)\n",
499 1.6 sommerfe rs.curentropy, rs.maxentropy);
500 1.12 enami printf("\t%9u bits of entropy discarded due to full pool\n",
501 1.6 sommerfe rs.discarded);
502 1.12 enami printf("\t%9u hard-random bits generated\n", rs.removed);
503 1.12 enami printf("\t%9u pseudo-random bits generated\n", rs.generated);
504 1.6 sommerfe
505 1.6 sommerfe close(fd);
506 1.6 sommerfe }
507 1.6 sommerfe
508 1.1 explorer int
509 1.1 explorer main(int argc, char **argv)
510 1.1 explorer {
511 1.9 enami rndctl_t rctl;
512 1.9 enami int ch, cmd, lflag, mflag, sflag;
513 1.1 explorer u_int32_t type;
514 1.9 enami char name[16];
515 1.21 tls const char *filename = NULL;
516 1.1 explorer
517 1.31 riastrad if (SHA3_Selftest() != 0)
518 1.31 riastrad errx(1, "SHA-3 self-test failed");
519 1.31 riastrad
520 1.1 explorer rctl.mask = 0;
521 1.1 explorer rctl.flags = 0;
522 1.1 explorer
523 1.1 explorer cmd = 0;
524 1.1 explorer lflag = 0;
525 1.1 explorer mflag = 0;
526 1.7 joda sflag = 0;
527 1.2 explorer type = 0xff;
528 1.1 explorer
529 1.28 tls while ((ch = getopt(argc, argv, "CES:L:celt:d:sv")) != -1) {
530 1.9 enami switch (ch) {
531 1.1 explorer case 'C':
532 1.1 explorer rctl.flags |= RND_FLAG_NO_COLLECT;
533 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
534 1.1 explorer mflag++;
535 1.1 explorer break;
536 1.1 explorer case 'E':
537 1.1 explorer rctl.flags |= RND_FLAG_NO_ESTIMATE;
538 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
539 1.1 explorer mflag++;
540 1.1 explorer break;
541 1.21 tls case 'L':
542 1.21 tls if (cmd != 0)
543 1.21 tls usage();
544 1.21 tls cmd = 'L';
545 1.21 tls filename = optarg;
546 1.21 tls break;
547 1.21 tls case 'S':
548 1.21 tls if (cmd != 0)
549 1.21 tls usage();
550 1.21 tls cmd = 'S';
551 1.21 tls filename = optarg;
552 1.21 tls break;
553 1.1 explorer case 'c':
554 1.1 explorer rctl.flags &= ~RND_FLAG_NO_COLLECT;
555 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
556 1.1 explorer mflag++;
557 1.1 explorer break;
558 1.1 explorer case 'e':
559 1.1 explorer rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
560 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
561 1.1 explorer mflag++;
562 1.1 explorer break;
563 1.1 explorer case 'l':
564 1.1 explorer lflag++;
565 1.1 explorer break;
566 1.1 explorer case 't':
567 1.1 explorer if (cmd != 0)
568 1.1 explorer usage();
569 1.1 explorer cmd = 't';
570 1.1 explorer
571 1.1 explorer type = find_type(optarg);
572 1.1 explorer break;
573 1.1 explorer case 'd':
574 1.1 explorer if (cmd != 0)
575 1.1 explorer usage();
576 1.1 explorer cmd = 'd';
577 1.1 explorer
578 1.1 explorer type = 0xff;
579 1.14 itojun strlcpy(name, optarg, sizeof(name));
580 1.1 explorer break;
581 1.6 sommerfe case 's':
582 1.6 sommerfe sflag++;
583 1.6 sommerfe break;
584 1.28 tls case 'v':
585 1.28 tls vflag++;
586 1.28 tls break;
587 1.1 explorer case '?':
588 1.1 explorer default:
589 1.1 explorer usage();
590 1.1 explorer }
591 1.18 apb }
592 1.18 apb argc -= optind;
593 1.18 apb argv += optind;
594 1.18 apb
595 1.18 apb /*
596 1.18 apb * No leftover non-option arguments.
597 1.18 apb */
598 1.18 apb if (argc > 0)
599 1.18 apb usage();
600 1.1 explorer
601 1.1 explorer /*
602 1.21 tls * Save.
603 1.21 tls */
604 1.21 tls if (cmd == 'S') {
605 1.31 riastrad do_save(filename, NULL, 0, 0);
606 1.21 tls exit(0);
607 1.21 tls }
608 1.21 tls
609 1.21 tls /*
610 1.21 tls * Load.
611 1.21 tls */
612 1.21 tls if (cmd == 'L') {
613 1.21 tls do_load(filename);
614 1.21 tls exit(0);
615 1.21 tls }
616 1.21 tls
617 1.21 tls /*
618 1.9 enami * Cannot list and modify at the same time.
619 1.1 explorer */
620 1.6 sommerfe if ((lflag != 0 || sflag != 0) && mflag != 0)
621 1.1 explorer usage();
622 1.1 explorer
623 1.1 explorer /*
624 1.9 enami * Bomb out on no-ops.
625 1.1 explorer */
626 1.6 sommerfe if (lflag == 0 && mflag == 0 && sflag == 0)
627 1.1 explorer usage();
628 1.1 explorer
629 1.1 explorer /*
630 1.9 enami * If not listing, we need a device name or a type.
631 1.1 explorer */
632 1.6 sommerfe if (lflag == 0 && cmd == 0 && sflag == 0)
633 1.1 explorer usage();
634 1.1 explorer
635 1.1 explorer /*
636 1.9 enami * Modify request.
637 1.1 explorer */
638 1.1 explorer if (mflag != 0) {
639 1.1 explorer rctl.type = type;
640 1.14 itojun strncpy(rctl.name, name, sizeof(rctl.name));
641 1.1 explorer do_ioctl(&rctl);
642 1.1 explorer
643 1.1 explorer exit(0);
644 1.1 explorer }
645 1.1 explorer
646 1.1 explorer /*
647 1.9 enami * List sources.
648 1.1 explorer */
649 1.1 explorer if (lflag != 0)
650 1.1 explorer do_list(cmd == 0, type, name);
651 1.1 explorer
652 1.6 sommerfe if (sflag != 0)
653 1.6 sommerfe do_stats();
654 1.9 enami
655 1.9 enami exit(0);
656 1.1 explorer }
657