rndctl.c revision 1.35 1 1.35 riastrad /* $NetBSD: rndctl.c,v 1.35 2020/05/07 19:12:45 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.35 riastrad __RCSID("$NetBSD: rndctl.c,v 1.35 2020/05/07 19:12:45 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.34 riastrad static int iflag;
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.34 riastrad fprintf(stderr, " %s [-i] -L save-file\n", getprogname());
93 1.34 riastrad fprintf(stderr, " %s -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.34 riastrad static int
132 1.35 riastrad update_seed(const char *filename, int fd_seed, const char *tmp,
133 1.35 riastrad const void *extra, size_t nextra, uint32_t extraentropy)
134 1.21 tls {
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.35 riastrad int fd_random;
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.35 riastrad /* Open /dev/urandom to read data from the system. */
147 1.35 riastrad if ((fd_random = open(_PATH_URANDOM, O_RDONLY)) == -1) {
148 1.35 riastrad warn("open /dev/urandom");
149 1.34 riastrad return -1;
150 1.34 riastrad }
151 1.21 tls
152 1.31 riastrad /* Find how much entropy is in the pool. */
153 1.35 riastrad if (ioctl(fd_random, RNDGETENTCNT, &systementropy) == -1) {
154 1.34 riastrad warn("ioctl(RNDGETENTCNT)");
155 1.34 riastrad systementropy = 0;
156 1.34 riastrad }
157 1.31 riastrad
158 1.31 riastrad /* Read some data from /dev/urandom. */
159 1.35 riastrad if ((size_t)(nread = read(fd_random, buf, sizeof buf)) != sizeof buf) {
160 1.31 riastrad if (nread == -1)
161 1.34 riastrad warn("read");
162 1.31 riastrad else
163 1.34 riastrad warnx("truncated read");
164 1.34 riastrad return -1;
165 1.31 riastrad }
166 1.31 riastrad
167 1.31 riastrad /* Close /dev/urandom; we're done with it. */
168 1.35 riastrad if (close(fd_random) == -1)
169 1.31 riastrad warn("close");
170 1.35 riastrad fd_random = -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.35 riastrad if ((size_t)(nwrit = write(fd_seed, &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.34 riastrad warnc(error, "write");
223 1.31 riastrad else
224 1.34 riastrad warnx("truncated write");
225 1.34 riastrad return -1;
226 1.31 riastrad }
227 1.31 riastrad explicit_memset(&rs, 0, sizeof rs); /* paranoia */
228 1.35 riastrad if (fsync_range(fd_seed, FDATASYNC|FDISKSYNC, 0, 0) == -1) {
229 1.31 riastrad int error = errno;
230 1.31 riastrad if (unlink(tmp) == -1)
231 1.31 riastrad warn("unlink");
232 1.34 riastrad warnc(error, "fsync_range");
233 1.34 riastrad return -1;
234 1.31 riastrad }
235 1.35 riastrad if (close(fd_seed) == -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.34 riastrad if (rename(tmp, filename) == -1) {
240 1.34 riastrad warn("rename");
241 1.34 riastrad return -1;
242 1.34 riastrad }
243 1.34 riastrad
244 1.34 riastrad /* Success! */
245 1.34 riastrad return 0;
246 1.34 riastrad }
247 1.34 riastrad
248 1.34 riastrad static void
249 1.34 riastrad do_save(const char *filename)
250 1.34 riastrad {
251 1.35 riastrad char tmp[PATH_MAX];
252 1.35 riastrad int fd_seed;
253 1.35 riastrad
254 1.35 riastrad /* Format the temporary file name. */
255 1.35 riastrad if (snprintf(tmp, sizeof tmp, "%s.tmp", filename) >= PATH_MAX)
256 1.35 riastrad errx(1, "path too long");
257 1.34 riastrad
258 1.35 riastrad /* Create a temporary seed file. */
259 1.35 riastrad if ((fd_seed = open(tmp, O_CREAT|O_TRUNC|O_WRONLY, 0600)) == -1)
260 1.35 riastrad err(1, "open seed file to save");
261 1.35 riastrad
262 1.35 riastrad /* Update the seed. Abort on failure. */
263 1.35 riastrad if (update_seed(filename, fd_seed, tmp, NULL, 0, 0) == -1)
264 1.34 riastrad exit(1);
265 1.21 tls }
266 1.21 tls
267 1.21 tls static void
268 1.31 riastrad do_load(const char *filename)
269 1.21 tls {
270 1.31 riastrad char tmp[PATH_MAX];
271 1.35 riastrad int fd_new, fd_old, fd_random;
272 1.31 riastrad rndsave_t rs;
273 1.21 tls rnddata_t rd;
274 1.31 riastrad ssize_t nread, nwrit;
275 1.21 tls SHA1_CTX s;
276 1.21 tls uint8_t digest[SHA1_DIGEST_LENGTH];
277 1.34 riastrad int ro = 0, fail = 0;
278 1.34 riastrad int error;
279 1.21 tls
280 1.31 riastrad /*
281 1.31 riastrad * 1. Load the old seed.
282 1.35 riastrad * 2. Feed the old seed into the kernel.
283 1.35 riastrad * 3. Generate and write a new seed.
284 1.34 riastrad * 4. Erase the old seed if we can.
285 1.31 riastrad *
286 1.35 riastrad * We follow the procedure in
287 1.31 riastrad *
288 1.31 riastrad * Niels Ferguson, Bruce Schneier, and Tadayoshi Kohno,
289 1.31 riastrad * _Cryptography Engineering_, Wiley, 2010, Sec. 9.6.2
290 1.35 riastrad * `Update Seed File'.
291 1.31 riastrad *
292 1.35 riastrad * Additionally, we zero the seed's stored entropy estimate if
293 1.35 riastrad * it appears to be on a read-only medium.
294 1.31 riastrad */
295 1.21 tls
296 1.31 riastrad /* Format the temporary file name. */
297 1.31 riastrad if (snprintf(tmp, sizeof tmp, "%s.tmp", filename) >= PATH_MAX)
298 1.31 riastrad errx(1, "path too long");
299 1.31 riastrad
300 1.35 riastrad /* Create a new seed file or determine the medium is read-only. */
301 1.35 riastrad if ((fd_new = open(tmp, O_CREAT|O_TRUNC|O_WRONLY, 0600)) == -1) {
302 1.35 riastrad warn("update seed file");
303 1.35 riastrad ro = 1;
304 1.35 riastrad }
305 1.35 riastrad
306 1.34 riastrad /*
307 1.34 riastrad * 1. Load the old seed.
308 1.34 riastrad */
309 1.35 riastrad if ((fd_old = open(filename, O_RDWR)) == -1) {
310 1.34 riastrad error = errno;
311 1.34 riastrad if ((error != EPERM && error != EROFS) ||
312 1.35 riastrad (fd_old = open(filename, O_RDONLY)) == -1)
313 1.34 riastrad err(1, "open seed file to load");
314 1.35 riastrad if (fd_new != -1)
315 1.35 riastrad warnc(error, "can't overwrite old seed file");
316 1.34 riastrad ro = 1;
317 1.34 riastrad }
318 1.35 riastrad if ((size_t)(nread = read(fd_old, &rs, sizeof rs)) != sizeof rs) {
319 1.31 riastrad if (nread == -1)
320 1.31 riastrad err(1, "read seed");
321 1.31 riastrad else
322 1.31 riastrad errx(1, "seed too short");
323 1.21 tls }
324 1.21 tls
325 1.31 riastrad /* Verify its checksum. */
326 1.21 tls SHA1Init(&s);
327 1.31 riastrad SHA1Update(&s, (const uint8_t *)&rs.entropy, sizeof(rs.entropy));
328 1.21 tls SHA1Update(&s, rs.data, sizeof(rs.data));
329 1.21 tls SHA1Final(digest, &s);
330 1.31 riastrad if (!consttime_memequal(digest, rs.digest, sizeof(digest))) {
331 1.31 riastrad /*
332 1.31 riastrad * If the checksum doesn't match, doesn't hurt to feed
333 1.31 riastrad * the seed in anyway, but act as though it has zero
334 1.31 riastrad * entropy in case it was corrupted with predictable
335 1.31 riastrad * garbage.
336 1.31 riastrad */
337 1.31 riastrad warnx("bad checksum");
338 1.31 riastrad rs.entropy = 0;
339 1.21 tls }
340 1.21 tls
341 1.32 riastrad /*
342 1.32 riastrad * If the entropy is insensibly large, try byte-swapping.
343 1.32 riastrad * Otherwise assume the file is corrupted and act as though it
344 1.32 riastrad * has zero entropy.
345 1.32 riastrad */
346 1.32 riastrad if (howmany(rs.entropy, NBBY) > sizeof(rs.data)) {
347 1.32 riastrad rs.entropy = bswap32(rs.entropy);
348 1.35 riastrad if (howmany(rs.entropy, NBBY) > sizeof(rs.data)) {
349 1.35 riastrad warnx("bad entropy estimate");
350 1.32 riastrad rs.entropy = 0;
351 1.35 riastrad }
352 1.32 riastrad }
353 1.32 riastrad
354 1.35 riastrad /* If the medium can't be updated, zero the entropy estimate. */
355 1.35 riastrad if (ro)
356 1.34 riastrad rs.entropy = 0;
357 1.34 riastrad
358 1.35 riastrad /* Fail later on if there's no entropy in the seed. */
359 1.35 riastrad if (rs.entropy == 0) {
360 1.35 riastrad warnx("no entropy in seed");
361 1.35 riastrad fail = 1;
362 1.35 riastrad }
363 1.35 riastrad
364 1.35 riastrad /* If the user asked, zero the entropy estimate, but succeed. */
365 1.35 riastrad if (iflag)
366 1.34 riastrad rs.entropy = 0;
367 1.34 riastrad
368 1.34 riastrad /*
369 1.35 riastrad * 2. Feed the old seed into the kernel.
370 1.34 riastrad */
371 1.21 tls rd.len = MIN(sizeof(rd.data), sizeof(rs.data));
372 1.21 tls rd.entropy = rs.entropy;
373 1.31 riastrad memcpy(rd.data, rs.data, rd.len);
374 1.31 riastrad explicit_memset(&rs, 0, sizeof rs); /* paranoia */
375 1.31 riastrad if ((fd_random = open(_PATH_URANDOM, O_WRONLY)) == -1)
376 1.31 riastrad err(1, "open /dev/urandom");
377 1.31 riastrad if (ioctl(fd_random, RNDADDDATA, &rd) == -1)
378 1.31 riastrad err(1, "RNDADDDATA");
379 1.34 riastrad explicit_memset(&rd, 0, sizeof rd); /* paranoia */
380 1.31 riastrad if (close(fd_random) == -1)
381 1.31 riastrad warn("close /dev/urandom");
382 1.31 riastrad fd_random = -1; /* paranoia */
383 1.21 tls
384 1.31 riastrad /*
385 1.35 riastrad * 3. Generate and write a new seed.
386 1.35 riastrad */
387 1.35 riastrad if (fd_new == -1 ||
388 1.35 riastrad update_seed(filename, fd_new, tmp, rs.data, sizeof(rs.data),
389 1.35 riastrad rs.entropy) == -1)
390 1.35 riastrad fail = 1;
391 1.35 riastrad
392 1.35 riastrad /*
393 1.35 riastrad * 4. Erase the old seed.
394 1.35 riastrad *
395 1.35 riastrad * Only effective if we're on a fixed-address file system like
396 1.35 riastrad * ffs -- doesn't help to erase the data on lfs, but doesn't
397 1.35 riastrad * hurt either. No need to unlink because update_seed will
398 1.35 riastrad * have already renamed over it.
399 1.31 riastrad */
400 1.34 riastrad if (!ro) {
401 1.34 riastrad memset(&rs, 0, sizeof rs);
402 1.35 riastrad if ((size_t)(nwrit = pwrite(fd_old, &rs, sizeof rs, 0)) !=
403 1.34 riastrad sizeof rs) {
404 1.34 riastrad if (nwrit == -1)
405 1.34 riastrad err(1, "overwrite old seed");
406 1.34 riastrad else
407 1.34 riastrad errx(1, "truncated overwrite");
408 1.34 riastrad }
409 1.35 riastrad if (fsync_range(fd_old, FDATASYNC|FDISKSYNC, 0, 0) == -1)
410 1.34 riastrad err(1, "fsync_range");
411 1.21 tls }
412 1.34 riastrad
413 1.35 riastrad /* Fail noisily if anything went wrong. */
414 1.34 riastrad if (fail)
415 1.35 riastrad exit(1);
416 1.21 tls }
417 1.21 tls
418 1.21 tls static void
419 1.1 explorer do_ioctl(rndctl_t *rctl)
420 1.1 explorer {
421 1.1 explorer int fd;
422 1.1 explorer int res;
423 1.1 explorer
424 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
425 1.1 explorer if (fd < 0)
426 1.1 explorer err(1, "open");
427 1.1 explorer
428 1.1 explorer res = ioctl(fd, RNDCTL, rctl);
429 1.1 explorer if (res < 0)
430 1.1 explorer err(1, "ioctl(RNDCTL)");
431 1.1 explorer
432 1.1 explorer close(fd);
433 1.1 explorer }
434 1.1 explorer
435 1.20 joerg static char *
436 1.1 explorer strflags(u_int32_t fl)
437 1.1 explorer {
438 1.1 explorer static char str[512];
439 1.1 explorer
440 1.28 tls str[0] = '\0';
441 1.1 explorer if (fl & RND_FLAG_NO_ESTIMATE)
442 1.6 sommerfe ;
443 1.9 enami else
444 1.28 tls strlcat(str, "estimate, ", sizeof(str));
445 1.9 enami
446 1.1 explorer if (fl & RND_FLAG_NO_COLLECT)
447 1.6 sommerfe ;
448 1.28 tls else
449 1.28 tls strlcat(str, "collect, ", sizeof(str));
450 1.28 tls
451 1.28 tls if (fl & RND_FLAG_COLLECT_VALUE)
452 1.28 tls strlcat(str, "v, ", sizeof(str));
453 1.28 tls if (fl & RND_FLAG_COLLECT_TIME)
454 1.28 tls strlcat(str, "t, ", sizeof(str));
455 1.28 tls if (fl & RND_FLAG_ESTIMATE_VALUE)
456 1.28 tls strlcat(str, "dv, ", sizeof(str));
457 1.28 tls if (fl & RND_FLAG_ESTIMATE_TIME)
458 1.28 tls strlcat(str, "dt, ", sizeof(str));
459 1.28 tls
460 1.28 tls if (str[strlen(str) - 2] == ',')
461 1.28 tls str[strlen(str) - 2] = '\0';
462 1.9 enami
463 1.9 enami return (str);
464 1.1 explorer }
465 1.1 explorer
466 1.6 sommerfe #define HEADER "Source Bits Type Flags\n"
467 1.1 explorer
468 1.20 joerg static void
469 1.1 explorer do_list(int all, u_int32_t type, char *name)
470 1.1 explorer {
471 1.28 tls rndstat_est_t rstat;
472 1.28 tls rndstat_est_name_t rstat_name;
473 1.9 enami int fd;
474 1.9 enami int res;
475 1.19 lukem uint32_t i;
476 1.9 enami u_int32_t start;
477 1.1 explorer
478 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
479 1.1 explorer if (fd < 0)
480 1.1 explorer err(1, "open");
481 1.1 explorer
482 1.1 explorer if (all == 0 && type == 0xff) {
483 1.14 itojun strncpy(rstat_name.name, name, sizeof(rstat_name.name));
484 1.28 tls res = ioctl(fd, RNDGETESTNAME, &rstat_name);
485 1.1 explorer if (res < 0)
486 1.28 tls err(1, "ioctl(RNDGETESTNAME)");
487 1.1 explorer printf(HEADER);
488 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
489 1.28 tls rstat_name.source.rt.name,
490 1.28 tls rstat_name.source.rt.total,
491 1.28 tls find_name(rstat_name.source.rt.type),
492 1.28 tls strflags(rstat_name.source.rt.flags));
493 1.28 tls if (vflag) {
494 1.28 tls printf("\tDt samples = %d\n",
495 1.28 tls rstat_name.source.dt_samples);
496 1.28 tls printf("\tDt bits = %d\n",
497 1.28 tls rstat_name.source.dt_total);
498 1.28 tls printf("\tDv samples = %d\n",
499 1.28 tls rstat_name.source.dv_samples);
500 1.28 tls printf("\tDv bits = %d\n",
501 1.28 tls rstat_name.source.dv_total);
502 1.28 tls }
503 1.1 explorer close(fd);
504 1.1 explorer return;
505 1.1 explorer }
506 1.1 explorer
507 1.1 explorer /*
508 1.9 enami * Run through all the devices present in the system, and either
509 1.1 explorer * print out ones that match, or print out all of them.
510 1.1 explorer */
511 1.1 explorer printf(HEADER);
512 1.1 explorer start = 0;
513 1.1 explorer for (;;) {
514 1.1 explorer rstat.count = RND_MAXSTATCOUNT;
515 1.1 explorer rstat.start = start;
516 1.28 tls res = ioctl(fd, RNDGETESTNUM, &rstat);
517 1.1 explorer if (res < 0)
518 1.28 tls err(1, "ioctl(RNDGETESTNUM)");
519 1.9 enami
520 1.1 explorer if (rstat.count == 0)
521 1.1 explorer break;
522 1.9 enami
523 1.19 lukem for (i = 0; i < rstat.count; i++) {
524 1.9 enami if (all != 0 ||
525 1.28 tls type == rstat.source[i].rt.type)
526 1.6 sommerfe printf("%-16s %10u %-4s %s\n",
527 1.28 tls rstat.source[i].rt.name,
528 1.28 tls rstat.source[i].rt.total,
529 1.28 tls find_name(rstat.source[i].rt.type),
530 1.28 tls strflags(rstat.source[i].rt.flags));
531 1.28 tls if (vflag) {
532 1.28 tls printf("\tDt samples = %d\n",
533 1.28 tls rstat.source[i].dt_samples);
534 1.28 tls printf("\tDt bits = %d\n",
535 1.28 tls rstat.source[i].dt_total);
536 1.28 tls printf("\tDv samples = %d\n",
537 1.28 tls rstat.source[i].dv_samples);
538 1.28 tls printf("\tDv bits = %d\n",
539 1.28 tls rstat.source[i].dv_total);
540 1.28 tls }
541 1.28 tls }
542 1.1 explorer start += rstat.count;
543 1.1 explorer }
544 1.1 explorer
545 1.1 explorer close(fd);
546 1.1 explorer }
547 1.1 explorer
548 1.20 joerg static void
549 1.20 joerg do_stats(void)
550 1.6 sommerfe {
551 1.6 sommerfe rndpoolstat_t rs;
552 1.6 sommerfe int fd;
553 1.9 enami
554 1.25 jruoho fd = open(_PATH_URANDOM, O_RDONLY, 0644);
555 1.6 sommerfe if (fd < 0)
556 1.6 sommerfe err(1, "open");
557 1.9 enami
558 1.6 sommerfe if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
559 1.6 sommerfe err(1, "ioctl(RNDGETPOOLSTAT)");
560 1.6 sommerfe
561 1.12 enami printf("\t%9u bits mixed into pool\n", rs.added);
562 1.12 enami printf("\t%9u bits currently stored in pool (max %u)\n",
563 1.6 sommerfe rs.curentropy, rs.maxentropy);
564 1.12 enami printf("\t%9u bits of entropy discarded due to full pool\n",
565 1.6 sommerfe rs.discarded);
566 1.12 enami printf("\t%9u hard-random bits generated\n", rs.removed);
567 1.12 enami printf("\t%9u pseudo-random bits generated\n", rs.generated);
568 1.6 sommerfe
569 1.6 sommerfe close(fd);
570 1.6 sommerfe }
571 1.6 sommerfe
572 1.1 explorer int
573 1.1 explorer main(int argc, char **argv)
574 1.1 explorer {
575 1.9 enami rndctl_t rctl;
576 1.9 enami int ch, cmd, lflag, mflag, sflag;
577 1.1 explorer u_int32_t type;
578 1.9 enami char name[16];
579 1.21 tls const char *filename = NULL;
580 1.1 explorer
581 1.31 riastrad if (SHA3_Selftest() != 0)
582 1.31 riastrad errx(1, "SHA-3 self-test failed");
583 1.31 riastrad
584 1.1 explorer rctl.mask = 0;
585 1.1 explorer rctl.flags = 0;
586 1.1 explorer
587 1.1 explorer cmd = 0;
588 1.1 explorer lflag = 0;
589 1.1 explorer mflag = 0;
590 1.7 joda sflag = 0;
591 1.2 explorer type = 0xff;
592 1.1 explorer
593 1.34 riastrad while ((ch = getopt(argc, argv, "CES:L:celit:d:sv")) != -1) {
594 1.9 enami switch (ch) {
595 1.1 explorer case 'C':
596 1.1 explorer rctl.flags |= RND_FLAG_NO_COLLECT;
597 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
598 1.1 explorer mflag++;
599 1.1 explorer break;
600 1.1 explorer case 'E':
601 1.1 explorer rctl.flags |= RND_FLAG_NO_ESTIMATE;
602 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
603 1.1 explorer mflag++;
604 1.1 explorer break;
605 1.21 tls case 'L':
606 1.21 tls if (cmd != 0)
607 1.21 tls usage();
608 1.21 tls cmd = 'L';
609 1.21 tls filename = optarg;
610 1.21 tls break;
611 1.21 tls case 'S':
612 1.21 tls if (cmd != 0)
613 1.21 tls usage();
614 1.21 tls cmd = 'S';
615 1.21 tls filename = optarg;
616 1.21 tls break;
617 1.1 explorer case 'c':
618 1.1 explorer rctl.flags &= ~RND_FLAG_NO_COLLECT;
619 1.1 explorer rctl.mask |= RND_FLAG_NO_COLLECT;
620 1.1 explorer mflag++;
621 1.1 explorer break;
622 1.1 explorer case 'e':
623 1.1 explorer rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
624 1.1 explorer rctl.mask |= RND_FLAG_NO_ESTIMATE;
625 1.1 explorer mflag++;
626 1.1 explorer break;
627 1.34 riastrad case 'i':
628 1.34 riastrad iflag = 1;
629 1.34 riastrad break;
630 1.1 explorer case 'l':
631 1.1 explorer lflag++;
632 1.1 explorer break;
633 1.1 explorer case 't':
634 1.1 explorer if (cmd != 0)
635 1.1 explorer usage();
636 1.1 explorer cmd = 't';
637 1.1 explorer
638 1.1 explorer type = find_type(optarg);
639 1.1 explorer break;
640 1.1 explorer case 'd':
641 1.1 explorer if (cmd != 0)
642 1.1 explorer usage();
643 1.1 explorer cmd = 'd';
644 1.1 explorer
645 1.1 explorer type = 0xff;
646 1.14 itojun strlcpy(name, optarg, sizeof(name));
647 1.1 explorer break;
648 1.6 sommerfe case 's':
649 1.6 sommerfe sflag++;
650 1.6 sommerfe break;
651 1.28 tls case 'v':
652 1.28 tls vflag++;
653 1.28 tls break;
654 1.1 explorer case '?':
655 1.1 explorer default:
656 1.1 explorer usage();
657 1.1 explorer }
658 1.18 apb }
659 1.18 apb argc -= optind;
660 1.18 apb argv += optind;
661 1.18 apb
662 1.18 apb /*
663 1.18 apb * No leftover non-option arguments.
664 1.18 apb */
665 1.18 apb if (argc > 0)
666 1.18 apb usage();
667 1.1 explorer
668 1.1 explorer /*
669 1.34 riastrad * -i makes sense only with -L.
670 1.34 riastrad */
671 1.34 riastrad if (iflag && cmd != 'L')
672 1.34 riastrad usage();
673 1.34 riastrad
674 1.34 riastrad /*
675 1.21 tls * Save.
676 1.21 tls */
677 1.21 tls if (cmd == 'S') {
678 1.34 riastrad do_save(filename);
679 1.21 tls exit(0);
680 1.21 tls }
681 1.21 tls
682 1.21 tls /*
683 1.21 tls * Load.
684 1.21 tls */
685 1.21 tls if (cmd == 'L') {
686 1.21 tls do_load(filename);
687 1.21 tls exit(0);
688 1.21 tls }
689 1.21 tls
690 1.21 tls /*
691 1.9 enami * Cannot list and modify at the same time.
692 1.1 explorer */
693 1.6 sommerfe if ((lflag != 0 || sflag != 0) && mflag != 0)
694 1.1 explorer usage();
695 1.1 explorer
696 1.1 explorer /*
697 1.9 enami * Bomb out on no-ops.
698 1.1 explorer */
699 1.6 sommerfe if (lflag == 0 && mflag == 0 && sflag == 0)
700 1.1 explorer usage();
701 1.1 explorer
702 1.1 explorer /*
703 1.9 enami * If not listing, we need a device name or a type.
704 1.1 explorer */
705 1.6 sommerfe if (lflag == 0 && cmd == 0 && sflag == 0)
706 1.1 explorer usage();
707 1.1 explorer
708 1.1 explorer /*
709 1.9 enami * Modify request.
710 1.1 explorer */
711 1.1 explorer if (mflag != 0) {
712 1.1 explorer rctl.type = type;
713 1.14 itojun strncpy(rctl.name, name, sizeof(rctl.name));
714 1.1 explorer do_ioctl(&rctl);
715 1.1 explorer
716 1.1 explorer exit(0);
717 1.1 explorer }
718 1.1 explorer
719 1.1 explorer /*
720 1.9 enami * List sources.
721 1.1 explorer */
722 1.1 explorer if (lflag != 0)
723 1.1 explorer do_list(cmd == 0, type, name);
724 1.1 explorer
725 1.6 sommerfe if (sflag != 0)
726 1.6 sommerfe do_stats();
727 1.9 enami
728 1.9 enami exit(0);
729 1.1 explorer }
730