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