1 /*- 2 * Copyright (c) 2011 David Schultz 3 * Copyright (c) 2024 Robert Clausecker <fuz@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/mman.h> 30 #include <sys/wait.h> 31 #include <errno.h> 32 #include <stdio.h> 33 #include <stdint.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #include <atf-c.h> 39 40 /* 41 * BUFSIZE is the number of bytes of rc4 output to compare. The probability 42 * that this test fails spuriously is 2**(-BUFSIZE * 8). 43 */ 44 #define BUFSIZE 8 45 46 /* 47 * Test whether arc4random_buf() returns the same sequence of bytes in both 48 * parent and child processes. (Hint: It shouldn't.) 49 */ 50 ATF_TC_WITHOUT_HEAD(test_arc4random); 51 ATF_TC_BODY(test_arc4random, tc) 52 { 53 struct shared_page { 54 char parentbuf[BUFSIZE]; 55 char childbuf[BUFSIZE]; 56 } *page; 57 pid_t pid; 58 char c; 59 60 page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE, 61 MAP_ANON | MAP_SHARED, -1, 0); 62 ATF_REQUIRE_MSG(page != MAP_FAILED, "mmap failed; errno=%d", errno); 63 64 arc4random_buf(&c, 1); 65 66 pid = fork(); 67 ATF_REQUIRE(0 <= pid); 68 if (pid == 0) { 69 /* child */ 70 arc4random_buf(page->childbuf, BUFSIZE); 71 exit(0); 72 } else { 73 /* parent */ 74 int status; 75 arc4random_buf(page->parentbuf, BUFSIZE); 76 wait(&status); 77 } 78 ATF_CHECK_MSG(memcmp(page->parentbuf, page->childbuf, BUFSIZE) != 0, 79 "sequences are the same"); 80 } 81 82 /* 83 * Test whether arc4random_uniform() returns a number below the given threshold. 84 * Test with various thresholds. 85 */ 86 ATF_TC_WITHOUT_HEAD(test_arc4random_uniform); 87 ATF_TC_BODY(test_arc4random_uniform, tc) 88 { 89 size_t i, j; 90 static const uint32_t thresholds[] = { 91 1, 2, 3, 4, 5, 10, 100, 1000, 92 INT32_MAX, (uint32_t)INT32_MAX + 1, 93 UINT32_MAX - 1000000000, UINT32_MAX - 1000000, UINT32_MAX - 1, 0 94 }; 95 96 for (i = 0; thresholds[i] != 0; i++) 97 for (j = 0; j < 10000; j++) 98 ATF_CHECK(arc4random_uniform(thresholds[i]) < thresholds[i]); 99 100 /* for a threshold of zero, just check that we get zero every time */ 101 for (j = 0; j < 1000; j++) 102 ATF_CHECK_EQ(0, arc4random_uniform(0)); 103 } 104 105 ATF_TP_ADD_TCS(tp) 106 { 107 108 ATF_TP_ADD_TC(tp, test_arc4random); 109 ATF_TP_ADD_TC(tp, test_arc4random_uniform); 110 111 return (atf_no_error()); 112 } 113