xref: /xnu-10002.81.5/tests/prng.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <dispatch/dispatch.h>
2*5e3eaea3SApple OSS Distributions #include <darwintest.h>
3*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
4*5e3eaea3SApple OSS Distributions #include <sys/random.h>
5*5e3eaea3SApple OSS Distributions 
6*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
7*5e3eaea3SApple OSS Distributions 
8*5e3eaea3SApple OSS Distributions #define BUF_SIZE ((size_t)(1 << 25))
9*5e3eaea3SApple OSS Distributions #define BLOCK_SIZE ((size_t)16)
10*5e3eaea3SApple OSS Distributions 
11*5e3eaea3SApple OSS Distributions static int
cmp(const void * a,const void * b)12*5e3eaea3SApple OSS Distributions cmp(const void *a, const void *b)
13*5e3eaea3SApple OSS Distributions {
14*5e3eaea3SApple OSS Distributions 	return memcmp(a, b, 16);
15*5e3eaea3SApple OSS Distributions }
16*5e3eaea3SApple OSS Distributions 
17*5e3eaea3SApple OSS Distributions static void
prng_sanitycheck(uint8_t * buf,size_t buf_size)18*5e3eaea3SApple OSS Distributions prng_sanitycheck(uint8_t *buf, size_t buf_size)
19*5e3eaea3SApple OSS Distributions {
20*5e3eaea3SApple OSS Distributions 	size_t nblocks = buf_size / BLOCK_SIZE;
21*5e3eaea3SApple OSS Distributions 	qsort(buf, nblocks, BLOCK_SIZE, cmp);
22*5e3eaea3SApple OSS Distributions 
23*5e3eaea3SApple OSS Distributions 	for (size_t i = 0; i < nblocks - 1; i += 1) {
24*5e3eaea3SApple OSS Distributions 		T_QUIET;
25*5e3eaea3SApple OSS Distributions 		T_ASSERT_NE(memcmp(buf, buf + BLOCK_SIZE, BLOCK_SIZE), 0, "duplicate block");
26*5e3eaea3SApple OSS Distributions 		buf += BLOCK_SIZE;
27*5e3eaea3SApple OSS Distributions 	}
28*5e3eaea3SApple OSS Distributions }
29*5e3eaea3SApple OSS Distributions 
30*5e3eaea3SApple OSS Distributions static void
prng_getentropy(void * ctx,size_t i)31*5e3eaea3SApple OSS Distributions prng_getentropy(void *ctx, size_t i)
32*5e3eaea3SApple OSS Distributions {
33*5e3eaea3SApple OSS Distributions 	uint8_t *buf = ((uint8_t *)ctx) + (BUF_SIZE * i);
34*5e3eaea3SApple OSS Distributions 
35*5e3eaea3SApple OSS Distributions 	for (size_t j = 0; j < BUF_SIZE; j += 256) {
36*5e3eaea3SApple OSS Distributions 		T_QUIET;
37*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(getentropy(&buf[j], 256), "getentropy");
38*5e3eaea3SApple OSS Distributions 	}
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions 	prng_sanitycheck(buf, BUF_SIZE);
41*5e3eaea3SApple OSS Distributions }
42*5e3eaea3SApple OSS Distributions 
43*5e3eaea3SApple OSS Distributions static void
prng_devrandom(void * ctx,size_t i)44*5e3eaea3SApple OSS Distributions prng_devrandom(void *ctx, size_t i)
45*5e3eaea3SApple OSS Distributions {
46*5e3eaea3SApple OSS Distributions 	uint8_t *buf = ((uint8_t *)ctx) + (BUF_SIZE * i);
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions 	int fd = open("/dev/random", O_RDONLY);
49*5e3eaea3SApple OSS Distributions 	T_QUIET;
50*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "open");
51*5e3eaea3SApple OSS Distributions 
52*5e3eaea3SApple OSS Distributions 	size_t n = BUF_SIZE;
53*5e3eaea3SApple OSS Distributions 	while (n > 0) {
54*5e3eaea3SApple OSS Distributions 		ssize_t m = read(fd, buf, n);
55*5e3eaea3SApple OSS Distributions 		T_QUIET;
56*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(m, "read");
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions 		n -= (size_t)m;
59*5e3eaea3SApple OSS Distributions 		buf += m;
60*5e3eaea3SApple OSS Distributions 	}
61*5e3eaea3SApple OSS Distributions 
62*5e3eaea3SApple OSS Distributions 	buf = ((uint8_t *)ctx) + (BUF_SIZE * i);
63*5e3eaea3SApple OSS Distributions 	prng_sanitycheck(buf, BUF_SIZE);
64*5e3eaea3SApple OSS Distributions }
65*5e3eaea3SApple OSS Distributions 
66*5e3eaea3SApple OSS Distributions T_DECL(prng, "prng test")
67*5e3eaea3SApple OSS Distributions {
68*5e3eaea3SApple OSS Distributions 	size_t ncpu = (size_t)dt_ncpu();
69*5e3eaea3SApple OSS Distributions 
70*5e3eaea3SApple OSS Distributions 	uint8_t *buf = malloc(BUF_SIZE * ncpu);
71*5e3eaea3SApple OSS Distributions 	T_QUIET;
72*5e3eaea3SApple OSS Distributions 	T_ASSERT_NOTNULL(buf, "malloc");
73*5e3eaea3SApple OSS Distributions 
74*5e3eaea3SApple OSS Distributions 	dispatch_apply_f(ncpu, DISPATCH_APPLY_AUTO, buf, prng_getentropy);
75*5e3eaea3SApple OSS Distributions 
76*5e3eaea3SApple OSS Distributions 	dispatch_apply_f(ncpu, DISPATCH_APPLY_AUTO, buf, prng_devrandom);
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions 	prng_sanitycheck(buf, BUF_SIZE * ncpu);
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	free(buf);
81*5e3eaea3SApple OSS Distributions }
82