2 * Copyright (c) 2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _PRNG_ENTROPY_H_
30 #define _PRNG_ENTROPY_H_
32 #include <kern/kern_types.h>
33 #include <sys/cdefs.h>
37 // This module is used to accumulate entropy from hardware interrupts
38 // for consumption by a higher-level PRNG.
40 // The raw entropy samples are collected from CPU counters during
41 // hardware interrupts. We do not perform synchronization before
42 // reading the counter (unlike ml_get_timebase and similar functions).
44 // This entropy accumulator performs continuous health tests in
45 // accordance with NIST SP 800-90B. The parameters have been chosen
46 // with the expectation that test failures should never occur except
47 // in case of catastrophic hardware failure.
49 typedef uint32_t entropy_sample_t
;
51 // Called during startup to initialize internal data structures.
52 void entropy_init(void);
54 // Called during hardware interrupts to collect entropy in per-CPU
56 void entropy_collect(void);
58 // Called by the higher-level PRNG. The module performs continuous
59 // health tests and decides whether to release entropy based on the
60 // values of various counters. Returns negative in case of error
61 // (e.g. health test failure).
62 int32_t entropy_provide(size_t *entropy_size
, void *entropy
, void *arg
);
64 extern entropy_sample_t
*entropy_analysis_buffer
;
65 extern uint32_t entropy_analysis_buffer_size
;
67 typedef struct entropy_health_stats
{
68 // A total count of times the test has been reset with a new
69 // initial observation. This can be thought of as the number of
70 // tests, but note that a single "test" can theoretically accrue
74 // A total count of failures of this test instance since
75 // boot. Since we do not expect any test failures (ever) in
76 // practice, this counter should always be zero.
77 uint32_t failure_count
;
79 // The maximum count of times an initial observation has recurred
80 // across all instances of this test.
81 uint32_t max_observation_count
;
82 } entropy_health_stats_t
;
84 extern int entropy_health_startup_done
;
85 extern entropy_health_stats_t entropy_health_rct_stats
;
86 extern entropy_health_stats_t entropy_health_apt_stats
;
90 #endif /* _PRNG_ENTROPY_H_ */