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