xref: /xnu-8796.141.3/tests/entropy.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdlib.h>
2*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
3*1b191cb5SApple OSS Distributions #include <darwintest.h>
4*1b191cb5SApple OSS Distributions #include <perfdata/perfdata.h>
5*1b191cb5SApple OSS Distributions 
6*1b191cb5SApple OSS Distributions typedef uint32_t entropy_sample_t;
7*1b191cb5SApple OSS Distributions 
8*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.crypto"),
9*1b191cb5SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
10*1b191cb5SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("crypto"));
11*1b191cb5SApple OSS Distributions 
12*1b191cb5SApple OSS Distributions T_DECL(entropy_collect, "Collect entropy for offline analysis",
13*1b191cb5SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1),
14*1b191cb5SApple OSS Distributions     T_META_BOOTARGS_SET("entropy-analysis-sample-count=1000"))
15*1b191cb5SApple OSS Distributions {
16*1b191cb5SApple OSS Distributions 	int ret;
17*1b191cb5SApple OSS Distributions 	uint32_t entropy_size = 0;
18*1b191cb5SApple OSS Distributions 	size_t size = sizeof(entropy_size);
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions 	ret = sysctlbyname("kern.entropy.analysis.buffer_size", &entropy_size, &size, NULL, 0);
21*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname kern.entropy.analysis.buffer_size");
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions 	uint32_t entropy_count = entropy_size / sizeof(entropy_sample_t);
24*1b191cb5SApple OSS Distributions 	entropy_sample_t *entropy = calloc(entropy_count, sizeof(entropy_sample_t));
25*1b191cb5SApple OSS Distributions 	size = entropy_size;
26*1b191cb5SApple OSS Distributions 
27*1b191cb5SApple OSS Distributions 	ret = sysctlbyname("kern.entropy.analysis.buffer", entropy, &size, NULL, 0);
28*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname kern.entropy.analysis.buffer");
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions 	// This test is not an entropy assessment. We're just checking to
31*1b191cb5SApple OSS Distributions 	// make sure the machinery of the entropy collection sysctl seems
32*1b191cb5SApple OSS Distributions 	// to be working.
33*1b191cb5SApple OSS Distributions 	for (uint32_t i = 0; i < entropy_count; i += 1) {
34*1b191cb5SApple OSS Distributions 		T_QUIET; T_EXPECT_NE(entropy[i], 0, "entropy buffer null sample %u", i);
35*1b191cb5SApple OSS Distributions 	}
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions 	free(entropy);
38*1b191cb5SApple OSS Distributions }
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions T_DECL(entropy_filter_rate, "Sample entropy filter rate")
41*1b191cb5SApple OSS Distributions {
42*1b191cb5SApple OSS Distributions 	int ret;
43*1b191cb5SApple OSS Distributions 	uint64_t total_sample_count = 0;
44*1b191cb5SApple OSS Distributions 	uint64_t rejected_sample_count = 0;
45*1b191cb5SApple OSS Distributions 	size_t size = sizeof(total_sample_count);
46*1b191cb5SApple OSS Distributions 
47*1b191cb5SApple OSS Distributions 	ret = sysctlbyname("kern.entropy.filter.total_sample_count", &total_sample_count, &size, NULL, 0);
48*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "kern.entropy.filter.total_sample_count");
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions 	size = sizeof(rejected_sample_count);
51*1b191cb5SApple OSS Distributions 	ret = sysctlbyname("kern.entropy.filter.rejected_sample_count", &rejected_sample_count, &size, NULL, 0);
52*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "kern.entropy.filter.rejected_sample_count");
53*1b191cb5SApple OSS Distributions 
54*1b191cb5SApple OSS Distributions 	double rejection_rate = (double) rejected_sample_count / (double) total_sample_count;
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions 	pdwriter_t writer = pdwriter_open_tmp("xnu", "entropy_filter_rate", 0, 0, NULL, 0);
57*1b191cb5SApple OSS Distributions 	T_ASSERT_NOTNULL(writer, "pdwriter_open_tmp");
58*1b191cb5SApple OSS Distributions 
59*1b191cb5SApple OSS Distributions 	pdwriter_new_value(writer, "Rejection Rate", PDUNIT_CUSTOM(rejectrate), rejection_rate);
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions 	pdwriter_close(writer);
62*1b191cb5SApple OSS Distributions }
63