xref: /xnu-8020.121.3/osfmk/prng/entropy.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions /*
2*fdd8201dSApple OSS Distributions  * Copyright (c) 2019 Apple Inc. All rights reserved.
3*fdd8201dSApple OSS Distributions  *
4*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*fdd8201dSApple OSS Distributions  *
6*fdd8201dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*fdd8201dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*fdd8201dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*fdd8201dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*fdd8201dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*fdd8201dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*fdd8201dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*fdd8201dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*fdd8201dSApple OSS Distributions  *
15*fdd8201dSApple OSS Distributions  * Please obtain a copy of the License at
16*fdd8201dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*fdd8201dSApple OSS Distributions  *
18*fdd8201dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*fdd8201dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*fdd8201dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*fdd8201dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*fdd8201dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*fdd8201dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*fdd8201dSApple OSS Distributions  * limitations under the License.
25*fdd8201dSApple OSS Distributions  *
26*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*fdd8201dSApple OSS Distributions  */
28*fdd8201dSApple OSS Distributions 
29*fdd8201dSApple OSS Distributions #ifndef _PRNG_ENTROPY_H_
30*fdd8201dSApple OSS Distributions #define _PRNG_ENTROPY_H_
31*fdd8201dSApple OSS Distributions 
32*fdd8201dSApple OSS Distributions #include <kern/kern_types.h>
33*fdd8201dSApple OSS Distributions #include <kern/bits.h>
34*fdd8201dSApple OSS Distributions #include <sys/cdefs.h>
35*fdd8201dSApple OSS Distributions 
36*fdd8201dSApple OSS Distributions __BEGIN_DECLS
37*fdd8201dSApple OSS Distributions 
38*fdd8201dSApple OSS Distributions // This module is used to accumulate entropy from hardware interrupts
39*fdd8201dSApple OSS Distributions // for consumption by a higher-level PRNG.
40*fdd8201dSApple OSS Distributions //
41*fdd8201dSApple OSS Distributions // The raw entropy samples are collected from CPU counters during
42*fdd8201dSApple OSS Distributions // hardware interrupts. We do not perform synchronization before
43*fdd8201dSApple OSS Distributions // reading the counter (unlike ml_get_timebase and similar functions).
44*fdd8201dSApple OSS Distributions //
45*fdd8201dSApple OSS Distributions // This entropy accumulator performs continuous health tests in
46*fdd8201dSApple OSS Distributions // accordance with NIST SP 800-90B. The parameters have been chosen
47*fdd8201dSApple OSS Distributions // with the expectation that test failures should never occur except
48*fdd8201dSApple OSS Distributions // in case of catastrophic hardware failure.
49*fdd8201dSApple OSS Distributions 
50*fdd8201dSApple OSS Distributions typedef uint32_t entropy_sample_t;
51*fdd8201dSApple OSS Distributions 
52*fdd8201dSApple OSS Distributions // Called during startup to initialize internal data structures.
53*fdd8201dSApple OSS Distributions void entropy_init(void);
54*fdd8201dSApple OSS Distributions 
55*fdd8201dSApple OSS Distributions // Called during hardware interrupts to collect entropy in per-CPU
56*fdd8201dSApple OSS Distributions // structures.
57*fdd8201dSApple OSS Distributions void entropy_collect(void);
58*fdd8201dSApple OSS Distributions 
59*fdd8201dSApple OSS Distributions // Called by the higher-level PRNG. The module performs continuous
60*fdd8201dSApple OSS Distributions // health tests and decides whether to release entropy based on the
61*fdd8201dSApple OSS Distributions // values of various counters. Returns negative in case of error
62*fdd8201dSApple OSS Distributions // (e.g. health test failure).
63*fdd8201dSApple OSS Distributions int32_t entropy_provide(size_t *entropy_size, void *entropy, void *arg);
64*fdd8201dSApple OSS Distributions 
65*fdd8201dSApple OSS Distributions // Called internally when entropy_provide() is called or when exporting
66*fdd8201dSApple OSS Distributions // analysis entropy with the "kern.entropy.analysis.filter" sysctl. This
67*fdd8201dSApple OSS Distributions // function applies a filter to input entropy and returns a bitmap indicating
68*fdd8201dSApple OSS Distributions // whether a particular sample is counted. The return value is the count of
69*fdd8201dSApple OSS Distributions // samples that passed the filter. "filter" may be NULL in which case only
70*fdd8201dSApple OSS Distributions // the number of unfiltered samples that passed the filter will be calculated.
71*fdd8201dSApple OSS Distributions //
72*fdd8201dSApple OSS Distributions // Each bit in the bitmap_t "filter" indicates whether a sample
73*fdd8201dSApple OSS Distributions // is kept (when the bit is set) or filtered (when the bit is not set).
74*fdd8201dSApple OSS Distributions // In other words, "bitmap_test(filter, n)" indicates whether
75*fdd8201dSApple OSS Distributions // "sample[n]" is kept.
76*fdd8201dSApple OSS Distributions uint32_t entropy_filter(uint32_t sample_count, entropy_sample_t *samples, uint32_t filter_count, bitmap_t *filter);
77*fdd8201dSApple OSS Distributions 
78*fdd8201dSApple OSS Distributions #if (DEVELOPMENT || DEBUG)
79*fdd8201dSApple OSS Distributions #define ENTROPY_ANALYSIS_SUPPORTED 1
80*fdd8201dSApple OSS Distributions #else
81*fdd8201dSApple OSS Distributions #define ENTROPY_ANALYSIS_SUPPORTED 0
82*fdd8201dSApple OSS Distributions #endif
83*fdd8201dSApple OSS Distributions 
84*fdd8201dSApple OSS Distributions #define ENTROPY_ANALYSIS_BOOTARG "entropy-analysis-sample-count"
85*fdd8201dSApple OSS Distributions 
86*fdd8201dSApple OSS Distributions #if ENTROPY_ANALYSIS_SUPPORTED
87*fdd8201dSApple OSS Distributions // Whether analysis is enabled via the "ENTROPY_ANALYSIS_BOOTARG" boot-arg
88*fdd8201dSApple OSS Distributions extern int entropy_analysis_enabled;
89*fdd8201dSApple OSS Distributions 
90*fdd8201dSApple OSS Distributions // The size (in bytes) of the filter
91*fdd8201dSApple OSS Distributions extern uint32_t entropy_analysis_filter_size;
92*fdd8201dSApple OSS Distributions 
93*fdd8201dSApple OSS Distributions // The size (in bytes) of the entropy analysis buffer
94*fdd8201dSApple OSS Distributions extern uint32_t entropy_analysis_buffer_size;
95*fdd8201dSApple OSS Distributions // The maximum number of entropy_sample_t elements in the analysis buffer
96*fdd8201dSApple OSS Distributions extern uint32_t entropy_analysis_max_sample_count;
97*fdd8201dSApple OSS Distributions 
98*fdd8201dSApple OSS Distributions // The number of entropy_sample_t elements in the analysis buffer
99*fdd8201dSApple OSS Distributions extern uint32_t entropy_analysis_sample_count;
100*fdd8201dSApple OSS Distributions 
101*fdd8201dSApple OSS Distributions extern entropy_sample_t *entropy_analysis_buffer;
102*fdd8201dSApple OSS Distributions #endif  // ENTROPY_ANALYSIS_SUPPORTED
103*fdd8201dSApple OSS Distributions 
104*fdd8201dSApple OSS Distributions typedef struct entropy_health_stats {
105*fdd8201dSApple OSS Distributions 	// A total count of times the test has been reset with a new
106*fdd8201dSApple OSS Distributions 	// initial observation. This can be thought of as the number of
107*fdd8201dSApple OSS Distributions 	// tests, but note that a single "test" can theoretically accrue
108*fdd8201dSApple OSS Distributions 	// multiple failures.
109*fdd8201dSApple OSS Distributions 	uint32_t reset_count;
110*fdd8201dSApple OSS Distributions 
111*fdd8201dSApple OSS Distributions 	// A total count of failures of this test instance since
112*fdd8201dSApple OSS Distributions 	// boot. Since we do not expect any test failures (ever) in
113*fdd8201dSApple OSS Distributions 	// practice, this counter should always be zero.
114*fdd8201dSApple OSS Distributions 	uint32_t failure_count;
115*fdd8201dSApple OSS Distributions 
116*fdd8201dSApple OSS Distributions 	// The maximum count of times an initial observation has recurred
117*fdd8201dSApple OSS Distributions 	// across all instances of this test.
118*fdd8201dSApple OSS Distributions 	uint32_t max_observation_count;
119*fdd8201dSApple OSS Distributions } entropy_health_stats_t;
120*fdd8201dSApple OSS Distributions 
121*fdd8201dSApple OSS Distributions extern int entropy_health_startup_done;
122*fdd8201dSApple OSS Distributions extern entropy_health_stats_t entropy_health_rct_stats;
123*fdd8201dSApple OSS Distributions extern entropy_health_stats_t entropy_health_apt_stats;
124*fdd8201dSApple OSS Distributions 
125*fdd8201dSApple OSS Distributions // The total number of samples processed
126*fdd8201dSApple OSS Distributions extern uint64_t entropy_filter_total_sample_count;
127*fdd8201dSApple OSS Distributions // The number of samples that passed the filter
128*fdd8201dSApple OSS Distributions extern uint64_t entropy_filter_accepted_sample_count;
129*fdd8201dSApple OSS Distributions // The number of samples that were rejected by the filters
130*fdd8201dSApple OSS Distributions extern uint64_t entropy_filter_rejected_sample_count;
131*fdd8201dSApple OSS Distributions 
132*fdd8201dSApple OSS Distributions 
133*fdd8201dSApple OSS Distributions __END_DECLS
134*fdd8201dSApple OSS Distributions 
135*fdd8201dSApple OSS Distributions #endif /* _PRNG_ENTROPY_H_ */
136