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