rndctl.c revision 1.32 1 1.32 riastrad /* $NetBSD: rndctl.c,v 1.32 2020/04/30 03:24:48 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.32 riastrad __RCSID("$NetBSD: rndctl.c,v 1.32 2020/04/30 03:24:48 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.32 riastrad #include <sys/endian.h>
44 1.30 riastrad #include <sys/rndio.h>
45 1.31 riastrad #include <sys/sha3.h>
46 1.11 enami
47 1.1 explorer #include <stdio.h>
48 1.1 explorer #include <stdlib.h>
49 1.1 explorer #include <unistd.h>
50 1.1 explorer #include <fcntl.h>
51 1.1 explorer #include <errno.h>
52 1.1 explorer #include <err.h>
53 1.25 jruoho #include <paths.h>
54 1.2 explorer #include <string.h>
55 1.1 explorer
56 1.1 explorer typedef struct {
57 1.17 christos const char *a_name;
58 1.9 enami u_int32_t a_type;
59 1.1 explorer } arg_t;
60 1.1 explorer
61 1.20 joerg static const arg_t source_types[] = {
62 1.6 sommerfe { "???", RND_TYPE_UNKNOWN },
63 1.1 explorer { "disk", RND_TYPE_DISK },
64 1.1 explorer { "net", RND_TYPE_NET },
65 1.1 explorer { "tape", RND_TYPE_TAPE },
66 1.1 explorer { "tty", RND_TYPE_TTY },
67 1.11 enami { "rng", RND_TYPE_RNG },
68 1.24 tls { "skew", RND_TYPE_SKEW },
69 1.24 tls { "env", RND_TYPE_ENV },
70 1.24 tls { "vm", RND_TYPE_VM },
71 1.24 tls { "power", RND_TYPE_POWER },
72 1.1 explorer { NULL, 0 }
73 1.1 explorer };
74 1.1 explorer
75 1.20 joerg __dead static void usage(void);
76 1.20 joerg static u_int32_t find_type(const char *name);
77 1.20 joerg static const char *find_name(u_int32_t);
78 1.20 joerg static void do_ioctl(rndctl_t *);
79 1.20 joerg static char * strflags(u_int32_t);
80 1.20 joerg static void do_list(int, u_int32_t, char *);
81 1.20 joerg static void do_stats(void);
82 1.2 explorer
83 1.28 tls static int vflag;
84 1.28 tls
85 1.2 explorer static void
86 1.1 explorer usage(void)
87 1.1 explorer {
88 1.9 enami
89 1.29 wiz fprintf(stderr, "usage: %s [-CEce] [-d devname | -t devtype]\n",
90 1.11 enami getprogname());
91 1.29 wiz fprintf(stderr, " %s [-lsv] [-d devname | -t devtype]\n",
92 1.11 enami getprogname());
93 1.21 tls fprintf(stderr, " %s -[L|S] save-file\n", getprogname());
94 1.5 mycroft exit(1);
95 1.1 explorer }
96 1.1 explorer
97 1.20 joerg static u_int32_t
98 1.20 joerg find_type(const char *name)
99 1.1 explorer {
100 1.20 joerg const arg_t *a;
101 1.1 explorer
102 1.1 explorer a = source_types;
103 1.9 enami
104 1.9 enami while (a->a_name != NULL) {
105 1.9 enami if (strcmp(a->a_name, name) == 0)
106 1.9 enami return (a->a_type);
107 1.1 explorer a++;
108 1.1 explorer }
109 1.1 explorer
110 1.10 enami errx(1, "device name %s unknown", name);
111 1.9 enami return (0);
112 1.1 explorer }
113 1.1 explorer
114 1.20 joerg static const char *
115 1.1 explorer find_name(u_int32_t type)
116 1.1 explorer {
117 1.20 joerg const arg_t *a;
118 1.1 explorer
119 1.1 explorer a = source_types;
120 1.9 enami
121 1.9 enami while (a->a_name != NULL) {
122 1.9 enami if (type == a->a_type)
123 1.9 enami return (a->a_name);
124 1.1 explorer a++;
125 1.1 explorer }
126 1.1 explorer
127 1.10 enami warnx("device type %u unknown", type);
128 1.10 enami return ("???");
129 1.1 explorer }
130 1.1 explorer
131 1.20 joerg static void
132 1.31 riastrad do_save(const char *filename, const void *extra, size_t nextra,
133 1.31 riastrad uint32_t extraentropy)
134 1.21 tls {
135 1.31 riastrad char tmp[PATH_MAX];
136 1.31 riastrad uint32_t systementropy;
137 1.31 riastrad uint8_t buf[32];
138 1.31 riastrad SHAKE128_CTX shake128;
139 1.21 tls rndsave_t rs;
140 1.21 tls SHA1_CTX s;
141 1.31 riastrad ssize_t nread, nwrit;
142 1.21 tls int fd;
143 1.21 tls
144 1.31 riastrad /* Paranoia: Avoid stack memory disclosure. */
145 1.31 riastrad memset(&rs, 0, sizeof rs);
146 1.25 jruoho
147 1.31 riastrad /* Format the temporary file name. */
148 1.31 riastrad if (snprintf(tmp, sizeof tmp, "%s.tmp", filename) >= PATH_MAX)
149 1.31 riastrad errx(1, "path too long");
150 1.21 tls
151 1.31 riastrad /* Open /dev/urandom. */
152 1.31 riastrad if ((fd = open(_PATH_URANDOM, O_RDONLY)) == -1)
153 1.31 riastrad err(1, "device open");
154 1.21 tls
155 1.31 riastrad /* Find how much entropy is in the pool. */
156 1.31 riastrad if (ioctl(fd, RNDGETENTCNT, &systementropy) == -1)
157 1.31 riastrad err(1, "ioctl(RNDGETENTCNT)");
158 1.31 riastrad
159 1.31 riastrad /* Read some data from /dev/urandom. */
160 1.31 riastrad if ((size_t)(nread = read(fd, buf, sizeof buf)) != sizeof buf) {
161 1.31 riastrad if (nread == -1)
162 1.31 riastrad err(1, "read");
163 1.31 riastrad else
164 1.31 riastrad errx(1, "truncated read");
165 1.31 riastrad }
166 1.31 riastrad
167 1.31 riastrad /* Close /dev/urandom; we're done with it. */
168 1.31 riastrad if (close(fd) == -1)
169 1.31 riastrad warn("close");
170 1.31 riastrad fd = -1; /* paranoia */
171 1.21 tls
172 1.31 riastrad /*
173 1.31 riastrad * Hash what we read together with the extra input to generate
174 1.31 riastrad * the seed data.
175 1.31 riastrad */
176 1.31 riastrad SHAKE128_Init(&shake128);
177 1.31 riastrad SHAKE128_Update(&shake128, buf, sizeof buf);
178 1.31 riastrad SHAKE128_Update(&shake128, extra, nextra);
179 1.31 riastrad SHAKE128_Final(rs.data, sizeof(rs.data), &shake128);
180 1.31 riastrad explicit_memset(&shake128, 0, sizeof shake128); /* paranoia */
181 1.21 tls
182 1.31 riastrad /*
183 1.31 riastrad * Report an upper bound on the min-entropy of the seed data.
184 1.31 riastrad * We take the larger of the system entropy and the extra
185 1.31 riastrad * entropy -- the system state and the extra input may or may
186 1.31 riastrad * not be independent, so we can't add them -- and clamp to the
187 1.31 riastrad * size of the data.
188 1.31 riastrad */
189 1.31 riastrad systementropy = MIN(systementropy,
190 1.31 riastrad MIN(sizeof(buf), UINT32_MAX/NBBY)*NBBY);
191 1.31 riastrad extraentropy = MIN(extraentropy, MIN(nextra, UINT32_MAX/NBBY)*NBBY);
192 1.31 riastrad rs.entropy = MIN(MAX(systementropy, extraentropy),
193 1.31 riastrad MIN(sizeof(rs.data), UINT32_MAX/NBBY)*NBBY);
194 1.21 tls
195 1.31 riastrad /*
196 1.32 riastrad * Compute the checksum on the 32-bit entropy count, followed
197 1.32 riastrad * 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.32 riastrad /*
313 1.32 riastrad * If the entropy is insensibly large, try byte-swapping.
314 1.32 riastrad * Otherwise assume the file is corrupted and act as though it
315 1.32 riastrad * has zero entropy.
316 1.32 riastrad */
317 1.32 riastrad if (howmany(rs.entropy, NBBY) > sizeof(rs.data)) {
318 1.32 riastrad rs.entropy = bswap32(rs.entropy);
319 1.32 riastrad if (howmany(rs.entropy, NBBY) > sizeof(rs.data))
320 1.32 riastrad rs.entropy = 0;
321 1.32 riastrad }
322 1.32 riastrad
323 1.31 riastrad /* Format the ioctl request. */
324 1.21 tls rd.len = MIN(sizeof(rd.data), sizeof(rs.data));
325 1.21 tls rd.entropy = rs.entropy;
326 1.31 riastrad memcpy(rd.data, rs.data, rd.len);
327 1.31 riastrad explicit_memset(&rs, 0, sizeof rs); /* paranoia */
328 1.31 riastrad
329 1.31 riastrad /* 2. Feed the old seed into the kernel. */
330 1.31 riastrad if ((fd_random = open(_PATH_URANDOM, O_WRONLY)) == -1)
331 1.31 riastrad err(1, "open /dev/urandom");
332 1.31 riastrad if (ioctl(fd_random, RNDADDDATA, &rd) == -1)
333 1.31 riastrad err(1, "RNDADDDATA");
334 1.31 riastrad if (close(fd_random) == -1)
335 1.31 riastrad warn("close /dev/urandom");
336 1.31 riastrad fd_random = -1; /* paranoia */
337 1.21 tls
338 1.31 riastrad /*
339 1.31 riastrad * 3. Generate and write a new seed. Note that we hash the old
340 1.31 riastrad * seed together with whatever /dev/urandom returns in do_save.
341 1.31 riastrad * Why? After RNDADDDATA, the input may not be distributed
342 1.31 riastrad * immediately to /dev/urandom.
343 1.31 riastrad */
344 1.31 riastrad do_save(filename, rd.data, rd.len, rd.entropy);
345 1.31 riastrad explicit_memset(&rd, 0, sizeof rd); /* paranoia */
346 1.21 tls
347 1.31 riastrad /*
348 1.31 riastrad * 4. Erase the old seed. Only effective if we're on a
349 1.31 riastrad * fixed-address file system like ffs -- doesn't help to erase
350 1.31 riastrad * the data on lfs, but doesn't hurt either. No need to unlink
351 1.31 riastrad * because do_save will have already overwritten it.
352 1.31 riastrad */
353 1.31 riastrad memset(&rs, 0, sizeof rs);
354 1.31 riastrad if ((size_t)(nwrit = pwrite(fd_seed, &rs, sizeof rs, 0)) !=
355 1.31 riastrad sizeof rs) {
356 1.31 riastrad if (nwrit == -1)
357 1.31 riastrad err(1, "overwrite old seed");
358 1.31 riastrad else
359 1.31 riastrad errx(1, "truncated overwrite");
360 1.21 tls }
361 1.31 riastrad if (fsync_range(fd_seed, FDATASYNC|FDISKSYNC, 0, 0) == -1)
362 1.31 riastrad err(1, "fsync_range");
363 1.21 tls }
364 1.21 tls
365 1.21 tls static void
366 1.1 explorer do_ioctl(rndctl_t *rctl)
367 1.1 explorer {
368 1.1 explorer int fd;
369 1.1 explorer int res;
370 1.1 explorer
371 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
372 1.1 explorer if (fd < 0)
373 1.1 explorer err(1, "open");
374 1.1 explorer
375 1.1 explorer res = ioctl(fd, RNDCTL, rctl);
376 1.1 explorer if (res < 0)
377 1.1 explorer err(1, "ioctl(RNDCTL)");
378 1.1 explorer
379 1.1 explorer close(fd);
380 1.1 explorer }
381 1.1 explorer
382 1.20 joerg static char *
383 1.1 explorer strflags(u_int32_t fl)
384 1.1 explorer {
385 1.1 explorer static char str[512];
386 1.1 explorer
387 1.28 tls str[0] = '\0';
388 1.1 explorer if (fl & RND_FLAG_NO_ESTIMATE)
389 1.6 sommerfe ;
390 1.9 enami else
391 1.28 tls strlcat(str, "estimate, ", sizeof(str));
392 1.9 enami
393 1.1 explorer if (fl & RND_FLAG_NO_COLLECT)
394 1.6 sommerfe ;
395 1.28 tls else
396 1.28 tls strlcat(str, "collect, ", sizeof(str));
397 1.28 tls
398 1.28 tls if (fl & RND_FLAG_COLLECT_VALUE)
399 1.28 tls strlcat(str, "v, ", sizeof(str));
400 1.28 tls if (fl & RND_FLAG_COLLECT_TIME)
401 1.28 tls strlcat(str, "t, ", sizeof(str));
402 1.28 tls if (fl & RND_FLAG_ESTIMATE_VALUE)
403 1.28 tls strlcat(str, "dv, ", sizeof(str));
404 1.28 tls if (fl & RND_FLAG_ESTIMATE_TIME)
405 1.28 tls strlcat(str, "dt, ", sizeof(str));
406 1.28 tls
407 1.28 tls if (str[strlen(str) - 2] == ',')
408 1.28 tls str[strlen(str) - 2] = '\0';
409 1.9 enami
410 1.9 enami return (str);
411 1.1 explorer }
412 1.1 explorer
413 1.6 sommerfe #define HEADER "Source Bits Type Flags\n"
414 1.1 explorer
415 1.20 joerg static void
416 1.1 explorer do_list(int all, u_int32_t type, char *name)
417 1.1 explorer {
418 1.28 tls rndstat_est_t rstat;
419 1.28 tls rndstat_est_name_t rstat_name;
420 1.9 enami int fd;
421 1.9 enami int res;
422 1.19 lukem uint32_t i;
423 1.9 enami u_int32_t start;
424 1.1 explorer
425 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
426 1.1 explorer if (fd < 0)
427 1.1 explorer err(1, "open");
428 1.1 explorer
429 1.1 explorer if (all == 0 && type == 0xff) {
430 1.14 itojun strncpy(rstat_name.name, name, sizeof(rstat_name.name));
431 1.28 tls res = ioctl(fd, RNDGETESTNAME, &rstat_name);
432 1.1 explorer if (res < 0)
433 1.28 tls err(1, "ioctl(RNDGETESTNAME)");
434 1.1 explorer printf(HEADER);
435 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
436 1.28 tls rstat_name.source.rt.name,
437 1.28 tls rstat_name.source.rt.total,
438 1.28 tls find_name(rstat_name.source.rt.type),
439 1.28 tls strflags(rstat_name.source.rt.flags));
440 1.28 tls if (vflag) {
441 1.28 tls printf("\tDt samples = %d\n",
442 1.28 tls rstat_name.source.dt_samples);
443 1.28 tls printf("\tDt bits = %d\n",
444 1.28 tls rstat_name.source.dt_total);
445 1.28 tls printf("\tDv samples = %d\n",
446 1.28 tls rstat_name.source.dv_samples);
447 1.28 tls printf("\tDv bits = %d\n",
448 1.28 tls rstat_name.source.dv_total);
449 1.28 tls }
450 1.1 explorer close(fd);
451 1.1 explorer return;
452 1.1 explorer }
453 1.1 explorer
454 1.1 explorer /*
455 1.9 enami * Run through all the devices present in the system, and either
456 1.1 explorer * print out ones that match, or print out all of them.
457 1.1 explorer */
458 1.1 explorer printf(HEADER);
459 1.1 explorer start = 0;
460 1.1 explorer for (;;) {
461 1.1 explorer rstat.count = RND_MAXSTATCOUNT;
462 1.1 explorer rstat.start = start;
463 1.28 tls res = ioctl(fd, RNDGETESTNUM, &rstat);
464 1.1 explorer if (res < 0)
465 1.28 tls err(1, "ioctl(RNDGETESTNUM)");
466 1.9 enami
467 1.1 explorer if (rstat.count == 0)
468 1.1 explorer break;
469 1.9 enami
470 1.19 lukem for (i = 0; i < rstat.count; i++) {
471 1.9 enami if (all != 0 ||
472 1.28 tls type == rstat.source[i].rt.type)
473 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
474 1.28 tls rstat.source[i].rt.name,
475 1.28 tls rstat.source[i].rt.total,
476 1.28 tls find_name(rstat.source[i].rt.type),
477 1.28 tls strflags(rstat.source[i].rt.flags));
478 1.28 tls if (vflag) {
479 1.28 tls printf("\tDt samples = %d\n",
480 1.28 tls rstat.source[i].dt_samples);
481 1.28 tls printf("\tDt bits = %d\n",
482 1.28 tls rstat.source[i].dt_total);
483 1.28 tls printf("\tDv samples = %d\n",
484 1.28 tls rstat.source[i].dv_samples);
485 1.28 tls printf("\tDv bits = %d\n",
486 1.28 tls rstat.source[i].dv_total);
487 1.28 tls }
488 1.28 tls }
489 1.1 explorer start += rstat.count;
490 1.1 explorer }
491 1.1 explorer
492 1.1 explorer close(fd);
493 1.1 explorer }
494 1.1 explorer
495 1.20 joerg static void
496 1.20 joerg do_stats(void)
497 1.6 sommerfe {
498 1.6 sommerfe rndpoolstat_t rs;
499 1.6 sommerfe int fd;
500 1.9 enami
501 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
502 1.6 sommerfe if (fd < 0)
503 1.6 sommerfe err(1, "open");
504 1.9 enami
505 1.6 sommerfe if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
506 1.6 sommerfe err(1, "ioctl(RNDGETPOOLSTAT)");
507 1.6 sommerfe
508 1.12 enami printf("\t%9u bits mixed into pool\n", rs.added);
509 1.12 enami printf("\t%9u bits currently stored in pool (max %u)\n",
510 1.6 sommerfe rs.curentropy, rs.maxentropy);
511 1.12 enami printf("\t%9u bits of entropy discarded due to full pool\n",
512 1.6 sommerfe rs.discarded);
513 1.12 enami printf("\t%9u hard-random bits generated\n", rs.removed);
514 1.12 enami printf("\t%9u pseudo-random bits generated\n", rs.generated);
515 1.6 sommerfe
516 1.6 sommerfe close(fd);
517 1.6 sommerfe }
518 1.6 sommerfe
519 1.1 explorer int
520 1.1 explorer main(int argc, char **argv)
521 1.1 explorer {
522 1.9 enami rndctl_t rctl;
523 1.9 enami int ch, cmd, lflag, mflag, sflag;
524 1.1 explorer u_int32_t type;
525 1.9 enami char name[16];
526 1.21 tls const char *filename = NULL;
527 1.1 explorer
528 1.31 riastrad if (SHA3_Selftest() != 0)
529 1.31 riastrad errx(1, "SHA-3 self-test failed");
530 1.31 riastrad
531 1.1 explorer rctl.mask = 0;
532 1.1 explorer rctl.flags = 0;
533 1.1 explorer
534 1.1 explorer cmd = 0;
535 1.1 explorer lflag = 0;
536 1.1 explorer mflag = 0;
537 1.7 joda sflag = 0;
538 1.2 explorer type = 0xff;
539 1.1 explorer
540 1.28 tls while ((ch = getopt(argc, argv, "CES:L:celt:d:sv")) != -1) {
541 1.9 enami switch (ch) {
542 1.1 explorer case 'C':
543 1.1 explorer rctl.flags |= RND_FLAG_NO_COLLECT;
544 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
545 1.1 explorer mflag++;
546 1.1 explorer break;
547 1.1 explorer case 'E':
548 1.1 explorer rctl.flags |= RND_FLAG_NO_ESTIMATE;
549 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
550 1.1 explorer mflag++;
551 1.1 explorer break;
552 1.21 tls case 'L':
553 1.21 tls if (cmd != 0)
554 1.21 tls usage();
555 1.21 tls cmd = 'L';
556 1.21 tls filename = optarg;
557 1.21 tls break;
558 1.21 tls case 'S':
559 1.21 tls if (cmd != 0)
560 1.21 tls usage();
561 1.21 tls cmd = 'S';
562 1.21 tls filename = optarg;
563 1.21 tls break;
564 1.1 explorer case 'c':
565 1.1 explorer rctl.flags &= ~RND_FLAG_NO_COLLECT;
566 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
567 1.1 explorer mflag++;
568 1.1 explorer break;
569 1.1 explorer case 'e':
570 1.1 explorer rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
571 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
572 1.1 explorer mflag++;
573 1.1 explorer break;
574 1.1 explorer case 'l':
575 1.1 explorer lflag++;
576 1.1 explorer break;
577 1.1 explorer case 't':
578 1.1 explorer if (cmd != 0)
579 1.1 explorer usage();
580 1.1 explorer cmd = 't';
581 1.1 explorer
582 1.1 explorer type = find_type(optarg);
583 1.1 explorer break;
584 1.1 explorer case 'd':
585 1.1 explorer if (cmd != 0)
586 1.1 explorer usage();
587 1.1 explorer cmd = 'd';
588 1.1 explorer
589 1.1 explorer type = 0xff;
590 1.14 itojun strlcpy(name, optarg, sizeof(name));
591 1.1 explorer break;
592 1.6 sommerfe case 's':
593 1.6 sommerfe sflag++;
594 1.6 sommerfe break;
595 1.28 tls case 'v':
596 1.28 tls vflag++;
597 1.28 tls break;
598 1.1 explorer case '?':
599 1.1 explorer default:
600 1.1 explorer usage();
601 1.1 explorer }
602 1.18 apb }
603 1.18 apb argc -= optind;
604 1.18 apb argv += optind;
605 1.18 apb
606 1.18 apb /*
607 1.18 apb * No leftover non-option arguments.
608 1.18 apb */
609 1.18 apb if (argc > 0)
610 1.18 apb usage();
611 1.1 explorer
612 1.1 explorer /*
613 1.21 tls * Save.
614 1.21 tls */
615 1.21 tls if (cmd == 'S') {
616 1.31 riastrad do_save(filename, NULL, 0, 0);
617 1.21 tls exit(0);
618 1.21 tls }
619 1.21 tls
620 1.21 tls /*
621 1.21 tls * Load.
622 1.21 tls */
623 1.21 tls if (cmd == 'L') {
624 1.21 tls do_load(filename);
625 1.21 tls exit(0);
626 1.21 tls }
627 1.21 tls
628 1.21 tls /*
629 1.9 enami * Cannot list and modify at the same time.
630 1.1 explorer */
631 1.6 sommerfe if ((lflag != 0 || sflag != 0) && mflag != 0)
632 1.1 explorer usage();
633 1.1 explorer
634 1.1 explorer /*
635 1.9 enami * Bomb out on no-ops.
636 1.1 explorer */
637 1.6 sommerfe if (lflag == 0 && mflag == 0 && sflag == 0)
638 1.1 explorer usage();
639 1.1 explorer
640 1.1 explorer /*
641 1.9 enami * If not listing, we need a device name or a type.
642 1.1 explorer */
643 1.6 sommerfe if (lflag == 0 && cmd == 0 && sflag == 0)
644 1.1 explorer usage();
645 1.1 explorer
646 1.1 explorer /*
647 1.9 enami * Modify request.
648 1.1 explorer */
649 1.1 explorer if (mflag != 0) {
650 1.1 explorer rctl.type = type;
651 1.14 itojun strncpy(rctl.name, name, sizeof(rctl.name));
652 1.1 explorer do_ioctl(&rctl);
653 1.1 explorer
654 1.1 explorer exit(0);
655 1.1 explorer }
656 1.1 explorer
657 1.1 explorer /*
658 1.9 enami * List sources.
659 1.1 explorer */
660 1.1 explorer if (lflag != 0)
661 1.1 explorer do_list(cmd == 0, type, name);
662 1.1 explorer
663 1.6 sommerfe if (sflag != 0)
664 1.6 sommerfe do_stats();
665 1.9 enami
666 1.9 enami exit(0);
667 1.1 explorer }
668