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