Home | History | Annotate | Line # | Download | only in gen
t_arc4random.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: t_arc4random.c,v 1.1 2024/08/27 13:43:02 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #define	_REENTRANT
     30  1.1  riastrad 
     31  1.1  riastrad #include <sys/cdefs.h>
     32  1.1  riastrad __RCSID("$NetBSD: t_arc4random.c,v 1.1 2024/08/27 13:43:02 riastradh Exp $");
     33  1.1  riastrad 
     34  1.1  riastrad #include <sys/resource.h>
     35  1.1  riastrad #include <sys/sysctl.h>
     36  1.1  riastrad #include <sys/wait.h>
     37  1.1  riastrad 
     38  1.1  riastrad #include <atf-c.h>
     39  1.1  riastrad #include <stdio.h>
     40  1.1  riastrad #include <string.h>
     41  1.1  riastrad #include <unistd.h>
     42  1.1  riastrad 
     43  1.1  riastrad #include "arc4random.h"
     44  1.1  riastrad #include "reentrant.h"
     45  1.1  riastrad #include "h_macros.h"
     46  1.1  riastrad 
     47  1.1  riastrad /*
     48  1.1  riastrad  * iszero(buf, len)
     49  1.1  riastrad  *
     50  1.1  riastrad  *	True if len bytes at buf are all zero, false if any one of them
     51  1.1  riastrad  *	is nonzero.
     52  1.1  riastrad  */
     53  1.1  riastrad static bool
     54  1.1  riastrad iszero(const void *buf, size_t len)
     55  1.1  riastrad {
     56  1.1  riastrad 	const unsigned char *p = buf;
     57  1.1  riastrad 	size_t i;
     58  1.1  riastrad 
     59  1.1  riastrad 	for (i = 0; i < len; i++) {
     60  1.1  riastrad 		if (p[i] != 0)
     61  1.1  riastrad 			return false;
     62  1.1  riastrad 	}
     63  1.1  riastrad 	return true;
     64  1.1  riastrad }
     65  1.1  riastrad 
     66  1.1  riastrad /*
     67  1.1  riastrad  * arc4random_prng()
     68  1.1  riastrad  *
     69  1.1  riastrad  *	Get a pointer to the current arc4random state, without updating
     70  1.1  riastrad  *	any of the state, not even lazy initialization.
     71  1.1  riastrad  */
     72  1.1  riastrad static struct arc4random_prng *
     73  1.1  riastrad arc4random_prng(void)
     74  1.1  riastrad {
     75  1.1  riastrad 	struct arc4random_prng *prng = NULL;
     76  1.1  riastrad 
     77  1.1  riastrad 	/*
     78  1.1  riastrad 	 * If arc4random has been initialized and there is a thread key
     79  1.1  riastrad 	 * (i.e., libc was built with _REENTRANT), get the thread-local
     80  1.1  riastrad 	 * arc4random state if there is one.
     81  1.1  riastrad 	 */
     82  1.1  riastrad 	if (arc4random_global.initialized)
     83  1.1  riastrad 		prng = thr_getspecific(arc4random_global.thread_key);
     84  1.1  riastrad 
     85  1.1  riastrad 	/*
     86  1.1  riastrad 	 * If we couldn't get the thread-local state, get the global
     87  1.1  riastrad 	 * state instead.
     88  1.1  riastrad 	 */
     89  1.1  riastrad 	if (prng == NULL)
     90  1.1  riastrad 		prng = &arc4random_global.prng;
     91  1.1  riastrad 
     92  1.1  riastrad 	return prng;
     93  1.1  riastrad }
     94  1.1  riastrad 
     95  1.1  riastrad /*
     96  1.1  riastrad  * arc4random_global_buf(buf, len)
     97  1.1  riastrad  *
     98  1.1  riastrad  *	Same as arc4random_buf, but force use of the global state.
     99  1.1  riastrad  *	Must happen before any other use of arc4random.
    100  1.1  riastrad  */
    101  1.1  riastrad static void
    102  1.1  riastrad arc4random_global_buf(void *buf, size_t len)
    103  1.1  riastrad {
    104  1.1  riastrad 	struct rlimit rlim, orlim;
    105  1.1  riastrad 	struct arc4random_prng *prng;
    106  1.1  riastrad 
    107  1.1  riastrad 	/*
    108  1.1  riastrad 	 * Save the address space limit.
    109  1.1  riastrad 	 */
    110  1.1  riastrad 	RL(getrlimit(RLIMIT_AS, &orlim));
    111  1.1  riastrad 	memcpy(&rlim, &orlim, sizeof(rlim));
    112  1.1  riastrad 
    113  1.1  riastrad 	/*
    114  1.1  riastrad 	 * Get a sample while the address space limit is zero.  This
    115  1.1  riastrad 	 * should try, and fail, to allocate a thread-local arc4random
    116  1.1  riastrad 	 * state with mmap(2).
    117  1.1  riastrad 	 */
    118  1.1  riastrad 	rlim.rlim_cur = 0;
    119  1.1  riastrad 	RL(setrlimit(RLIMIT_AS, &rlim));
    120  1.1  riastrad 	arc4random_buf(buf, len);
    121  1.1  riastrad 	RL(setrlimit(RLIMIT_AS, &orlim));
    122  1.1  riastrad 
    123  1.1  riastrad 	/*
    124  1.1  riastrad 	 * Restore the address space limit.
    125  1.1  riastrad 	 */
    126  1.1  riastrad 	RL(setrlimit(RLIMIT_AS, &orlim));
    127  1.1  riastrad 
    128  1.1  riastrad 	/*
    129  1.1  riastrad 	 * Verify the PRNG is the global one, not the thread-local one,
    130  1.1  riastrad 	 * and that it was initialized.
    131  1.1  riastrad 	 */
    132  1.1  riastrad 	prng = arc4random_prng();
    133  1.1  riastrad 	ATF_CHECK_EQ(prng, &arc4random_global.prng);
    134  1.1  riastrad 	ATF_CHECK(!iszero(&prng->arc4_prng, sizeof(prng->arc4_prng)));
    135  1.1  riastrad 	ATF_CHECK(prng->arc4_epoch != 0);
    136  1.1  riastrad }
    137  1.1  riastrad 
    138  1.1  riastrad /*
    139  1.1  riastrad  * arc4random_global_thread(cookie)
    140  1.1  riastrad  *
    141  1.1  riastrad  *	Start routine for a thread that just grabs an output from the
    142  1.1  riastrad  *	global state.
    143  1.1  riastrad  */
    144  1.1  riastrad static void *
    145  1.1  riastrad arc4random_global_thread(void *cookie)
    146  1.1  riastrad {
    147  1.1  riastrad 	unsigned char buf[32];
    148  1.1  riastrad 
    149  1.1  riastrad 	arc4random_global_buf(buf, sizeof(buf));
    150  1.1  riastrad 
    151  1.1  riastrad 	return NULL;
    152  1.1  riastrad }
    153  1.1  riastrad 
    154  1.1  riastrad ATF_TC(addrandom);
    155  1.1  riastrad ATF_TC_HEAD(addrandom, tc)
    156  1.1  riastrad {
    157  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    158  1.1  riastrad 	    "Test arc4random_addrandom updates the state");
    159  1.1  riastrad }
    160  1.1  riastrad ATF_TC_BODY(addrandom, tc)
    161  1.1  riastrad {
    162  1.1  riastrad 	unsigned char buf[32], zero32[32] = {0};
    163  1.1  riastrad 	struct arc4random_prng *prng, copy;
    164  1.1  riastrad 
    165  1.1  riastrad 	/*
    166  1.1  riastrad 	 * Get a sample to start things off.
    167  1.1  riastrad 	 */
    168  1.1  riastrad 	arc4random_buf(buf, sizeof(buf));
    169  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    170  1.1  riastrad 
    171  1.1  riastrad 	/*
    172  1.1  riastrad 	 * By this point, the global state must be initialized -- if
    173  1.1  riastrad 	 * not, the process should have aborted.
    174  1.1  riastrad 	 */
    175  1.1  riastrad 	ATF_CHECK(arc4random_global.initialized);
    176  1.1  riastrad 
    177  1.1  riastrad 	/*
    178  1.1  riastrad 	 * Get the PRNG, global or local.  By this point, the PRNG
    179  1.1  riastrad 	 * state should be nonzero (with overwhelmingly high
    180  1.1  riastrad 	 * probability) and the epoch should also be nonzero.
    181  1.1  riastrad 	 */
    182  1.1  riastrad 	prng = arc4random_prng();
    183  1.1  riastrad 	ATF_CHECK(!iszero(&prng->arc4_prng, sizeof(prng->arc4_prng)));
    184  1.1  riastrad 	ATF_CHECK(prng->arc4_epoch != 0);
    185  1.1  riastrad 
    186  1.1  riastrad 	/*
    187  1.1  riastrad 	 * Save a copy and update the state with arc4random_addrandom.
    188  1.1  riastrad 	 */
    189  1.1  riastrad 	copy = *prng;
    190  1.1  riastrad 	arc4random_addrandom(zero32, sizeof(zero32));
    191  1.1  riastrad 
    192  1.1  riastrad 	/*
    193  1.1  riastrad 	 * The state should have changed.  (The epoch may or may not.)
    194  1.1  riastrad 	 */
    195  1.1  riastrad 	ATF_CHECK(memcmp(&prng->arc4_prng, &copy.arc4_prng,
    196  1.1  riastrad 		sizeof(copy.arc4_prng)) != 0);
    197  1.1  riastrad 
    198  1.1  riastrad 	/*
    199  1.1  riastrad 	 * Save a copy and update the state with arc4random_stir.
    200  1.1  riastrad 	 */
    201  1.1  riastrad 	copy = *prng;
    202  1.1  riastrad 	arc4random_stir();
    203  1.1  riastrad 
    204  1.1  riastrad 	/*
    205  1.1  riastrad 	 * The state should have changed.  (The epoch may or may not.)
    206  1.1  riastrad 	 */
    207  1.1  riastrad 	ATF_CHECK(memcmp(&prng->arc4_prng, &copy.arc4_prng,
    208  1.1  riastrad 		sizeof(copy.arc4_prng)) != 0);
    209  1.1  riastrad }
    210  1.1  riastrad 
    211  1.1  riastrad ATF_TC(consolidate);
    212  1.1  riastrad ATF_TC_HEAD(consolidate, tc)
    213  1.1  riastrad {
    214  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    215  1.1  riastrad 	    "Test consolidating entropy resets the epoch");
    216  1.1  riastrad }
    217  1.1  riastrad ATF_TC_BODY(consolidate, tc)
    218  1.1  riastrad {
    219  1.1  riastrad 	unsigned char buf[32];
    220  1.1  riastrad 	struct arc4random_prng *local, *global = &arc4random_global.prng;
    221  1.1  riastrad 	unsigned localepoch, globalepoch;
    222  1.1  riastrad 	const int consolidate = 1;
    223  1.1  riastrad 	pthread_t thread;
    224  1.1  riastrad 
    225  1.1  riastrad 	/*
    226  1.1  riastrad 	 * Get a sample from the global state to make sure the global
    227  1.1  riastrad 	 * state is initialized.  Remember the epoch.
    228  1.1  riastrad 	 */
    229  1.1  riastrad 	arc4random_global_buf(buf, sizeof(buf));
    230  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    231  1.1  riastrad 	ATF_CHECK(!iszero(&global->arc4_prng, sizeof(global->arc4_prng)));
    232  1.1  riastrad 	ATF_CHECK((globalepoch = global->arc4_epoch) != 0);
    233  1.1  riastrad 
    234  1.1  riastrad 	/*
    235  1.1  riastrad 	 * Get a sample from the local state too to make sure the local
    236  1.1  riastrad 	 * state is initialized.  Remember the epoch.
    237  1.1  riastrad 	 */
    238  1.1  riastrad 	arc4random_buf(buf, sizeof(buf));
    239  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    240  1.1  riastrad 	local = arc4random_prng();
    241  1.1  riastrad 	ATF_CHECK(!iszero(&local->arc4_prng, sizeof(local->arc4_prng)));
    242  1.1  riastrad 	ATF_CHECK((localepoch = local->arc4_epoch) != 0);
    243  1.1  riastrad 
    244  1.1  riastrad 	/*
    245  1.1  riastrad 	 * Trigger entropy consolidation.
    246  1.1  riastrad 	 */
    247  1.1  riastrad 	RL(sysctlbyname("kern.entropy.consolidate", /*oldp*/NULL, /*oldlen*/0,
    248  1.1  riastrad 		&consolidate, sizeof(consolidate)));
    249  1.1  riastrad 
    250  1.1  riastrad 	/*
    251  1.1  riastrad 	 * Verify the epoch cache isn't changed yet until we ask for
    252  1.1  riastrad 	 * more data.
    253  1.1  riastrad 	 */
    254  1.1  riastrad 	ATF_CHECK_EQ_MSG(globalepoch, global->arc4_epoch,
    255  1.1  riastrad 	    "global epoch was %u, now %u", globalepoch, global->arc4_epoch);
    256  1.1  riastrad 	ATF_CHECK_EQ_MSG(localepoch, local->arc4_epoch,
    257  1.1  riastrad 	    "local epoch was %u, now %u", localepoch, local->arc4_epoch);
    258  1.1  riastrad 
    259  1.1  riastrad 	/*
    260  1.1  riastrad 	 * Request new output and verify the local epoch cache has
    261  1.1  riastrad 	 * changed.
    262  1.1  riastrad 	 */
    263  1.1  riastrad 	arc4random_buf(buf, sizeof(buf));
    264  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    265  1.1  riastrad 	ATF_CHECK_MSG(localepoch != local->arc4_epoch,
    266  1.1  riastrad 	    "local epoch unchanged from %u", localepoch);
    267  1.1  riastrad 
    268  1.1  riastrad 	/*
    269  1.1  riastrad 	 * Create a new thread to grab output from the global state,
    270  1.1  riastrad 	 * wait for it to complete, and verify the global epoch cache
    271  1.1  riastrad 	 * has changed.  (Now that we have already used the local state
    272  1.1  riastrad 	 * in this thread, we can't use the global state any more.)
    273  1.1  riastrad 	 */
    274  1.1  riastrad 	RZ(pthread_create(&thread, NULL, &arc4random_global_thread, NULL));
    275  1.1  riastrad 	RZ(pthread_join(thread, NULL));
    276  1.1  riastrad 	ATF_CHECK_MSG(globalepoch != global->arc4_epoch,
    277  1.1  riastrad 	    "global epoch unchanged from %u", globalepoch);
    278  1.1  riastrad }
    279  1.1  riastrad 
    280  1.1  riastrad ATF_TC(fork);
    281  1.1  riastrad ATF_TC_HEAD(fork, tc)
    282  1.1  riastrad {
    283  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    284  1.1  riastrad 	    "Test fork zeros the state and gets independent state");
    285  1.1  riastrad }
    286  1.1  riastrad ATF_TC_BODY(fork, tc)
    287  1.1  riastrad {
    288  1.1  riastrad 	unsigned char buf[32];
    289  1.1  riastrad 	struct arc4random_prng *local, *global = &arc4random_global.prng;
    290  1.1  riastrad 	struct arc4random_prng childstate;
    291  1.1  riastrad 	int fd[2];
    292  1.1  riastrad 	pid_t child, pid;
    293  1.1  riastrad 	ssize_t nread;
    294  1.1  riastrad 	int status;
    295  1.1  riastrad 
    296  1.1  riastrad 	/*
    297  1.1  riastrad 	 * Get a sample from the global state to make sure the global
    298  1.1  riastrad 	 * state is initialized.
    299  1.1  riastrad 	 */
    300  1.1  riastrad 	arc4random_global_buf(buf, sizeof(buf));
    301  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    302  1.1  riastrad 	ATF_CHECK(!iszero(&global->arc4_prng, sizeof(global->arc4_prng)));
    303  1.1  riastrad 	ATF_CHECK(global->arc4_epoch != 0);
    304  1.1  riastrad 
    305  1.1  riastrad 	/*
    306  1.1  riastrad 	 * Get a sample from the local state too to make sure the local
    307  1.1  riastrad 	 * state is initialized.
    308  1.1  riastrad 	 */
    309  1.1  riastrad 	arc4random_buf(buf, sizeof(buf));
    310  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    311  1.1  riastrad 	local = arc4random_prng();
    312  1.1  riastrad 	ATF_CHECK(!iszero(&local->arc4_prng, sizeof(local->arc4_prng)));
    313  1.1  riastrad 	ATF_CHECK(local->arc4_epoch != 0);
    314  1.1  riastrad 
    315  1.1  riastrad 	/*
    316  1.1  riastrad 	 * Create a pipe to transfer the state from child to parent.
    317  1.1  riastrad 	 */
    318  1.1  riastrad 	RL(pipe(fd));
    319  1.1  riastrad 
    320  1.1  riastrad 	/*
    321  1.1  riastrad 	 * Fork a child.
    322  1.1  riastrad 	 */
    323  1.1  riastrad 	RL(child = fork());
    324  1.1  riastrad 	if (child == 0) {
    325  1.1  riastrad 		status = 0;
    326  1.1  riastrad 
    327  1.1  riastrad 		/*
    328  1.1  riastrad 		 * Verify the states have been zero'd on fork.
    329  1.1  riastrad 		 */
    330  1.1  riastrad 		if (!iszero(local, sizeof(*local))) {
    331  1.1  riastrad 			fprintf(stderr, "failed to zero local state\n");
    332  1.1  riastrad 			status = 1;
    333  1.1  riastrad 		}
    334  1.1  riastrad 		if (!iszero(global, sizeof(*global))) {
    335  1.1  riastrad 			fprintf(stderr, "failed to zero global state\n");
    336  1.1  riastrad 			status = 1;
    337  1.1  riastrad 		}
    338  1.1  riastrad 
    339  1.1  riastrad 		/*
    340  1.1  riastrad 		 * Verify we generate nonzero output.
    341  1.1  riastrad 		 */
    342  1.1  riastrad 		arc4random_buf(buf, sizeof(buf));
    343  1.1  riastrad 		if (iszero(buf, sizeof(buf))) {
    344  1.1  riastrad 			fprintf(stderr, "failed to generate nonzero output\n");
    345  1.1  riastrad 			status = 1;
    346  1.1  riastrad 		}
    347  1.1  riastrad 
    348  1.1  riastrad 		/*
    349  1.1  riastrad 		 * Share the state to compare with parent.
    350  1.1  riastrad 		 */
    351  1.1  riastrad 		if ((size_t)write(fd[1], local, sizeof(*local)) !=
    352  1.1  riastrad 		    sizeof(*local)) {
    353  1.1  riastrad 			fprintf(stderr, "failed to share local state\n");
    354  1.1  riastrad 			status = 1;
    355  1.1  riastrad 		}
    356  1.1  riastrad 		_exit(status);
    357  1.1  riastrad 	}
    358  1.1  riastrad 
    359  1.1  riastrad 	/*
    360  1.1  riastrad 	 * Verify the global state has been zeroed as expected.  (This
    361  1.1  riastrad 	 * way it is never available to the child, even shortly after
    362  1.1  riastrad 	 * the fork syscall returns before the atfork handler is
    363  1.1  riastrad 	 * called.)
    364  1.1  riastrad 	 */
    365  1.1  riastrad 	ATF_CHECK(iszero(global, sizeof(*global)));
    366  1.1  riastrad 
    367  1.1  riastrad 	/*
    368  1.1  riastrad 	 * Read the state from the child.
    369  1.1  riastrad 	 */
    370  1.1  riastrad 	RL(nread = read(fd[0], &childstate, sizeof(childstate)));
    371  1.1  riastrad 	ATF_CHECK_EQ_MSG(nread, sizeof(childstate),
    372  1.1  riastrad 	    "nread=%zu sizeof(childstate)=%zu", nread, sizeof(childstate));
    373  1.1  riastrad 
    374  1.1  riastrad 	/*
    375  1.1  riastrad 	 * Verify the child state is distinct.  (The global state has
    376  1.1  riastrad 	 * been zero'd so it's OK it if coincides.)  Check again after
    377  1.1  riastrad 	 * we grab another output.
    378  1.1  riastrad 	 */
    379  1.1  riastrad 	ATF_CHECK(memcmp(local, &childstate, sizeof(*local)) != 0);
    380  1.1  riastrad 	arc4random_buf(buf, sizeof(buf));
    381  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    382  1.1  riastrad 	ATF_CHECK(memcmp(local, &childstate, sizeof(*local)) != 0);
    383  1.1  riastrad 
    384  1.1  riastrad 	/*
    385  1.1  riastrad 	 * Wait for the child to complete and verify it passed.
    386  1.1  riastrad 	 */
    387  1.1  riastrad 	RL(pid = waitpid(child, &status, 0));
    388  1.1  riastrad 	ATF_CHECK_EQ_MSG(status, 0, "child exited with nonzero status=%d",
    389  1.1  riastrad 	    status);
    390  1.1  riastrad }
    391  1.1  riastrad 
    392  1.1  riastrad ATF_TC(global);
    393  1.1  riastrad ATF_TC_HEAD(global, tc)
    394  1.1  riastrad {
    395  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    396  1.1  riastrad 	    "Test the global state is used when address space limit is hit");
    397  1.1  riastrad }
    398  1.1  riastrad ATF_TC_BODY(global, tc)
    399  1.1  riastrad {
    400  1.1  riastrad 	unsigned char buf[32], buf1[32];
    401  1.1  riastrad 
    402  1.1  riastrad 	/*
    403  1.1  riastrad 	 * Get a sample from the global state (and verify it was using
    404  1.1  riastrad 	 * the global state).
    405  1.1  riastrad 	 */
    406  1.1  riastrad 	arc4random_global_buf(buf, sizeof(buf));
    407  1.1  riastrad 
    408  1.1  riastrad 	/*
    409  1.1  riastrad 	 * Verify we got a sample.
    410  1.1  riastrad 	 */
    411  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    412  1.1  riastrad 
    413  1.1  riastrad 	/*
    414  1.1  riastrad 	 * Get a sample from whatever state and make sure it wasn't
    415  1.1  riastrad 	 * repeated, which happens only with probability 1/2^256.
    416  1.1  riastrad 	 */
    417  1.1  riastrad 	arc4random_buf(buf1, sizeof(buf1));
    418  1.1  riastrad 	ATF_CHECK(!iszero(buf1, sizeof(buf1)));	/* Pr[fail] = 1/2^256 */
    419  1.1  riastrad 	ATF_CHECK(memcmp(buf, buf1, sizeof(buf)) != 0);
    420  1.1  riastrad }
    421  1.1  riastrad 
    422  1.1  riastrad ATF_TC(local);
    423  1.1  riastrad ATF_TC_HEAD(local, tc)
    424  1.1  riastrad {
    425  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    426  1.1  riastrad 	    "Test arc4random uses thread-local state");
    427  1.1  riastrad 	/* XXX skip if libc was built without _REENTRANT */
    428  1.1  riastrad }
    429  1.1  riastrad ATF_TC_BODY(local, tc)
    430  1.1  riastrad {
    431  1.1  riastrad 	unsigned char buf[32], buf1[32];
    432  1.1  riastrad 	struct arc4random_prng *prng;
    433  1.1  riastrad 
    434  1.1  riastrad 	/*
    435  1.1  riastrad 	 * Get a sample to start things off.
    436  1.1  riastrad 	 */
    437  1.1  riastrad 	arc4random_buf(buf, sizeof(buf));
    438  1.1  riastrad 	ATF_CHECK(!iszero(buf, sizeof(buf)));	/* Pr[fail] = 1/2^256 */
    439  1.1  riastrad 
    440  1.1  riastrad 	/*
    441  1.1  riastrad 	 * Verify the arc4random state is _not_ the global state.
    442  1.1  riastrad 	 */
    443  1.1  riastrad 	prng = arc4random_prng();
    444  1.1  riastrad 	ATF_CHECK(prng != &arc4random_global.prng);
    445  1.1  riastrad 	ATF_CHECK(!iszero(&prng->arc4_prng, sizeof(prng->arc4_prng)));
    446  1.1  riastrad 	ATF_CHECK(prng->arc4_epoch != 0);
    447  1.1  riastrad 
    448  1.1  riastrad 	/*
    449  1.1  riastrad 	 * Get another sample and make sure it wasn't repeated, which
    450  1.1  riastrad 	 * happens only with probability 1/2^256.
    451  1.1  riastrad 	 */
    452  1.1  riastrad 	arc4random_buf(buf1, sizeof(buf1));
    453  1.1  riastrad 	ATF_CHECK(!iszero(buf1, sizeof(buf1)));	/* Pr[fail] = 1/2^256 */
    454  1.1  riastrad 	ATF_CHECK(memcmp(buf, buf1, sizeof(buf)) != 0);
    455  1.1  riastrad }
    456  1.1  riastrad 
    457  1.1  riastrad ATF_TP_ADD_TCS(tp)
    458  1.1  riastrad {
    459  1.1  riastrad 
    460  1.1  riastrad 	ATF_TP_ADD_TC(tp, addrandom);
    461  1.1  riastrad 	ATF_TP_ADD_TC(tp, consolidate);
    462  1.1  riastrad 	ATF_TP_ADD_TC(tp, fork);
    463  1.1  riastrad 	ATF_TP_ADD_TC(tp, global);
    464  1.1  riastrad 	ATF_TP_ADD_TC(tp, local);
    465  1.1  riastrad 
    466  1.1  riastrad 	return atf_no_error();
    467  1.1  riastrad }
    468