xref: /freebsd/tests/sys/kern/sys_getrandom.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/random.h>
31 #include <errno.h>
32 
33 #include <atf-c.h>
34 
35 #include <zstd.h>
36 
37 static const unsigned valid_flags[] = { 0, GRND_NONBLOCK, GRND_RANDOM,
38     GRND_NONBLOCK | GRND_RANDOM };
39 
40 ATF_TC_WITHOUT_HEAD(getrandom_randomness);
41 ATF_TC_BODY(getrandom_randomness, tc)
42 {
43 	char randomb[4096], compressed[5000];
44 	ssize_t ret;
45 	size_t i, j, c;
46 	unsigned mode;
47 
48 	for (i = 0; i < nitems(valid_flags); i++) {
49 		mode = valid_flags[i];
50 
51 		/* Get new random data, filling randomb. */
52 
53 		memset(randomb, 0, sizeof(randomb));
54 
55 		for (j = 0; j < sizeof(randomb);) {
56 			ret = getrandom(&randomb[j], sizeof(randomb) - j, mode);
57 			if (ret < 0 && (mode & GRND_NONBLOCK) != 0 &&
58 			    errno == EAGAIN)
59 				continue;
60 
61 			ATF_REQUIRE_MSG(ret >= 0, "other error: %d", errno);
62 			ATF_REQUIRE_MSG(ret > 0, "bogus zero return");
63 
64 			j += (size_t)ret;
65 		}
66 
67 		/* Perform compressibility test */
68 		c = ZSTD_compress(compressed, sizeof(compressed), randomb,
69 		    sizeof(randomb), ZSTD_maxCLevel());
70 		ATF_REQUIRE_MSG(!ZSTD_isError(c), "zstd compress: %s",
71 		    ZSTD_getErrorName(c));
72 
73 		/*
74 		 * If the output is very compressible, it's probably not random
75 		 */
76 		ATF_REQUIRE_MSG(c > (sizeof(randomb) * 4 / 5),
77 		    "purportedly random data was compressible: %zu/%zu or %f%%",
78 		    c, sizeof(randomb), (double)c / (double)sizeof(randomb));
79 	}
80 }
81 
82 ATF_TC_WITHOUT_HEAD(getrandom_fault);
83 ATF_TC_BODY(getrandom_fault, tc)
84 {
85 	ssize_t ret;
86 
87 	ret = getrandom(NULL, 1, 0);
88 	ATF_REQUIRE_EQ(ret, -1);
89 	ATF_REQUIRE_EQ(errno, EFAULT);
90 }
91 
92 ATF_TC_WITHOUT_HEAD(getrandom_count);
93 ATF_TC_BODY(getrandom_count, tc)
94 {
95 	char buf[4096], reference[4096];
96 	ssize_t ret;
97 
98 	/* getrandom(2) does not modify buf past the requested length */
99 	_Static_assert(sizeof(reference) == sizeof(buf), "must match");
100 	memset(reference, 0x7C, sizeof(reference));
101 
102 	memset(buf, 0x7C, sizeof(buf));
103 	ret = getrandom(buf, 1, 0);
104 	ATF_REQUIRE_EQ(ret, 1);
105 	ATF_REQUIRE_EQ(memcmp(&buf[1], reference, sizeof(reference) - 1), 0);
106 
107 	memset(buf, 0x7C, sizeof(buf));
108 	ATF_REQUIRE_EQ(getrandom(buf, 15, 0), 15);
109 	ATF_REQUIRE_EQ(memcmp(&buf[15], reference, sizeof(reference) - 15), 0);
110 
111 	memset(buf, 0x7C, sizeof(buf));
112 	ATF_REQUIRE_EQ(getrandom(buf, 255, 0), 255);
113 	ATF_REQUIRE_EQ(memcmp(&buf[255], reference, sizeof(reference) - 255), 0);
114 
115 	memset(buf, 0x7C, sizeof(buf));
116 	ATF_REQUIRE_EQ(getrandom(buf, 4095, 0), 4095);
117 	ATF_REQUIRE_EQ(memcmp(&buf[4095], reference, sizeof(reference) - 4095), 0);
118 }
119 
120 ATF_TP_ADD_TCS(tp)
121 {
122 
123 	ATF_TP_ADD_TC(tp, getrandom_count);
124 	ATF_TP_ADD_TC(tp, getrandom_fault);
125 	ATF_TP_ADD_TC(tp, getrandom_randomness);
126 	return (atf_no_error());
127 }
128