1 /* $NetBSD: t_getrandom.c,v 1.1 2020/08/14 00:53:16 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __RCSID("$NetBSD: t_getrandom.c,v 1.1 2020/08/14 00:53:16 riastradh Exp $"); 34 35 #include <sys/random.h> 36 37 #include <atf-c.h> 38 #include <errno.h> 39 #include <signal.h> 40 #include <unistd.h> 41 42 static uint8_t buf[65536]; 43 static uint8_t zero24[24]; 44 45 static void 46 alarm_handler(int signo) 47 { 48 } 49 50 ATF_TC(getrandom); 51 ATF_TC_HEAD(getrandom, tc) 52 { 53 54 atf_tc_set_md_var(tc, "descr", "getrandom(2)"); 55 } 56 57 /* 58 * Probability of spurious failure is 1/2^192 for each of the memcmps. 59 * As long as there are fewer than 2^64 of them, the probability of 60 * spurious failure is at most 1/2^128, which is low enough that we 61 * don't care about it. 62 */ 63 64 ATF_TC_BODY(getrandom, tc) 65 { 66 ssize_t n; 67 68 ATF_REQUIRE(signal(SIGALRM, &alarm_handler) != SIG_ERR); 69 70 /* default */ 71 alarm(1); 72 memset(buf, 0, sizeof buf); 73 n = getrandom(buf, sizeof buf, 0); 74 if (n == -1) { 75 ATF_CHECK_EQ(errno, EINTR); 76 } else { 77 ATF_CHECK_EQ((size_t)n, sizeof buf); 78 ATF_CHECK(memcmp(buf, zero24, 24) != 0); 79 ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); 80 } 81 alarm(0); 82 83 /* default, nonblocking */ 84 memset(buf, 0, sizeof buf); 85 n = getrandom(buf, sizeof buf, GRND_NONBLOCK); 86 if (n == -1) { 87 ATF_CHECK_EQ(errno, EAGAIN); 88 } else { 89 ATF_CHECK_EQ((size_t)n, sizeof buf); 90 ATF_CHECK(memcmp(buf, zero24, 24) != 0); 91 ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); 92 } 93 94 /* insecure */ 95 memset(buf, 0, sizeof buf); 96 n = getrandom(buf, sizeof buf, GRND_INSECURE); 97 ATF_CHECK(n != -1); 98 ATF_CHECK_EQ((size_t)n, sizeof buf); 99 ATF_CHECK(memcmp(buf, zero24, 24) != 0); 100 ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); 101 102 /* insecure, nonblocking -- same as mere insecure */ 103 memset(buf, 0, sizeof buf); 104 n = getrandom(buf, sizeof buf, GRND_INSECURE|GRND_NONBLOCK); 105 ATF_CHECK(n != -1); 106 ATF_CHECK_EQ((size_t)n, sizeof buf); 107 ATF_CHECK(memcmp(buf, zero24, 24) != 0); 108 ATF_CHECK(memcmp(buf + sizeof buf - 24, zero24, 24) != 0); 109 110 /* `random' (hokey) */ 111 alarm(1); 112 memset(buf, 0, sizeof buf); 113 n = getrandom(buf, sizeof buf, GRND_RANDOM); 114 if (n == -1) { 115 ATF_CHECK_EQ(errno, EINTR); 116 } else { 117 ATF_CHECK(n != 0); 118 ATF_CHECK((size_t)n <= sizeof buf); 119 if ((size_t)n >= 24) { 120 ATF_CHECK(memcmp(buf, zero24, 24) != 0); 121 ATF_CHECK(memcmp(buf + n - 24, zero24, 24) != 0); 122 } 123 } 124 alarm(0); 125 126 /* `random' (hokey), nonblocking */ 127 memset(buf, 0, sizeof buf); 128 n = getrandom(buf, sizeof buf, GRND_RANDOM|GRND_NONBLOCK); 129 if (n == -1) { 130 ATF_CHECK_EQ(errno, EAGAIN); 131 } else { 132 ATF_CHECK(n != 0); 133 ATF_CHECK((size_t)n <= sizeof buf); 134 if ((size_t)n >= 24) { 135 ATF_CHECK(memcmp(buf, zero24, 24) != 0); 136 ATF_CHECK(memcmp(buf + n - 24, zero24, 24) != 0); 137 } 138 } 139 140 /* random and insecure -- nonsensical */ 141 n = getrandom(buf, sizeof buf, GRND_RANDOM|GRND_INSECURE); 142 ATF_CHECK_EQ(n, -1); 143 ATF_CHECK_EQ(errno, EINVAL); 144 145 /* random and insecure, nonblocking -- nonsensical */ 146 n = getrandom(buf, sizeof buf, 147 GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK); 148 ATF_CHECK_EQ(n, -1); 149 ATF_CHECK_EQ(errno, EINVAL); 150 151 /* invalid flags */ 152 __CTASSERT(~(GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK)); 153 n = getrandom(buf, sizeof buf, 154 ~(GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK)); 155 ATF_CHECK_EQ(n, -1); 156 ATF_CHECK_EQ(errno, EINVAL); 157 158 /* unmapped */ 159 n = getrandom(NULL, sizeof buf, GRND_INSECURE|GRND_NONBLOCK); 160 ATF_CHECK_EQ(n, -1); 161 ATF_CHECK_EQ(errno, EFAULT); 162 } 163 164 ATF_TP_ADD_TCS(tp) 165 { 166 167 ATF_TP_ADD_TC(tp, getrandom); 168 169 return atf_no_error(); 170 } 171