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