xref: /freebsd/tests/sys/kern/sys_getrandom.c (revision 4f4b548b1d21ae16955b09f9b717524167a7d97e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/random.h>
34 #include <errno.h>
35 
36 #include <atf-c.h>
37 
38 #include <zstd.h>
39 
40 static const unsigned valid_flags[] = { 0, GRND_NONBLOCK, GRND_RANDOM,
41     GRND_NONBLOCK | GRND_RANDOM };
42 
43 ATF_TC_WITHOUT_HEAD(getrandom_randomness);
44 ATF_TC_BODY(getrandom_randomness, tc)
45 {
46 	char randomb[4096], compressed[5000];
47 	ssize_t ret;
48 	size_t i, j, c;
49 	unsigned mode;
50 
51 	for (i = 0; i < nitems(valid_flags); i++) {
52 		mode = valid_flags[i];
53 
54 		/* Get new random data, filling randomb. */
55 
56 		memset(randomb, 0, sizeof(randomb));
57 
58 		for (j = 0; j < sizeof(randomb);) {
59 			ret = getrandom(&randomb[j], sizeof(randomb) - j, mode);
60 			if (ret < 0 && (mode & GRND_NONBLOCK) != 0 &&
61 			    errno == EAGAIN)
62 				continue;
63 
64 			ATF_REQUIRE_MSG(ret >= 0, "other error: %d", errno);
65 			ATF_REQUIRE_MSG(ret > 0, "bogus zero return");
66 
67 			j += (size_t)ret;
68 		}
69 
70 		/* Perform compressibility test */
71 		c = ZSTD_compress(compressed, sizeof(compressed), randomb,
72 		    sizeof(randomb), ZSTD_maxCLevel());
73 		ATF_REQUIRE_MSG(!ZSTD_isError(c), "zstd compress: %s",
74 		    ZSTD_getErrorName(c));
75 
76 		/*
77 		 * If the output is very compressible, it's probably not random
78 		 */
79 		ATF_REQUIRE_MSG(c > (sizeof(randomb) * 4 / 5),
80 		    "purportedly random data was compressible: %zu/%zu or %f%%",
81 		    c, sizeof(randomb), (double)c / (double)sizeof(randomb));
82 	}
83 }
84 
85 ATF_TC_WITHOUT_HEAD(getrandom_fault);
86 ATF_TC_BODY(getrandom_fault, tc)
87 {
88 	ssize_t ret;
89 
90 	ret = getrandom(NULL, 1, 0);
91 	ATF_REQUIRE_EQ(ret, -1);
92 	ATF_REQUIRE_EQ(errno, EFAULT);
93 }
94 
95 ATF_TC_WITHOUT_HEAD(getrandom_count);
96 ATF_TC_BODY(getrandom_count, tc)
97 {
98 	char buf[4096], reference[4096];
99 	ssize_t ret;
100 
101 	/* getrandom(2) does not modify buf past the requested length */
102 	_Static_assert(sizeof(reference) == sizeof(buf), "must match");
103 	memset(reference, 0x7C, sizeof(reference));
104 
105 	memset(buf, 0x7C, sizeof(buf));
106 	ret = getrandom(buf, 1, 0);
107 	ATF_REQUIRE_EQ(ret, 1);
108 	ATF_REQUIRE_EQ(memcmp(&buf[1], reference, sizeof(reference) - 1), 0);
109 
110 	memset(buf, 0x7C, sizeof(buf));
111 	ATF_REQUIRE_EQ(getrandom(buf, 15, 0), 15);
112 	ATF_REQUIRE_EQ(memcmp(&buf[15], reference, sizeof(reference) - 15), 0);
113 
114 	memset(buf, 0x7C, sizeof(buf));
115 	ATF_REQUIRE_EQ(getrandom(buf, 255, 0), 255);
116 	ATF_REQUIRE_EQ(memcmp(&buf[255], reference, sizeof(reference) - 255), 0);
117 
118 	memset(buf, 0x7C, sizeof(buf));
119 	ATF_REQUIRE_EQ(getrandom(buf, 4095, 0), 4095);
120 	ATF_REQUIRE_EQ(memcmp(&buf[4095], reference, sizeof(reference) - 4095), 0);
121 }
122 
123 ATF_TP_ADD_TCS(tp)
124 {
125 
126 	ATF_TP_ADD_TC(tp, getrandom_count);
127 	ATF_TP_ADD_TC(tp, getrandom_fault);
128 	ATF_TP_ADD_TC(tp, getrandom_randomness);
129 	return (atf_no_error());
130 }
131