]> git.saurik.com Git - apple/xnu.git/blob - osfmk/prng/random.h
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / prng / random.h
1 /*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _PRNG_RANDOM_H_
30 #define _PRNG_RANDOM_H_
31
32 __BEGIN_DECLS
33
34 #ifdef XNU_KERNEL_PRIVATE
35
36 #define ENTROPY_BUFFER_BYTE_SIZE 64
37
38 #define ENTROPY_BUFFER_SIZE (ENTROPY_BUFFER_BYTE_SIZE / sizeof(uint32_t))
39
40 typedef struct entropy_data {
41 /*
42 * TODO: Should index_ptr be volatile? Are we exposed to any races that
43 * we care about if it is not?
44 */
45 uint32_t *index_ptr;
46 uint32_t buffer[ENTROPY_BUFFER_SIZE];
47 } entropy_data_t;
48
49 extern entropy_data_t EntropyData;
50
51 /* Trace codes for DBG_SEC_KERNEL: */
52 #define ENTROPY_READ(n) SECURITYDBG_CODE(DBG_SEC_KERNEL, n) /* n: 0 .. 3 */
53
54 /*
55 * Early_random implementation params: */
56 #define EARLY_RANDOM_SEED_SIZE (16)
57 #define EARLY_RANDOM_STATE_STATIC_SIZE (264)
58
59 #if defined (__x86_64__)
60 #define current_prng_context() (current_cpu_datap()->cpu_prng)
61 #define master_prng_context() (cpu_datap(master_cpu)->cpu_prng)
62 #elif defined (__arm__) || defined(__arm64__)
63 #include <arm/cpu_data_internal.h> // For MAX_CPUS
64 #define current_prng_context() (getCpuDatap()->cpu_prng)
65 #define master_prng_context() (cpu_datap(master_cpu)->cpu_prng)
66 #else
67 #error architecture unknown
68 #endif
69
70 #include <corecrypto/ccdrbg.h>
71 #include <corecrypto/ccsha1.h>
72
73 typedef void (*ccdrbg_factory_t)(struct ccdrbg_info *info, const void *custom);
74
75 extern void ccdrbg_factory_yarrow(struct ccdrbg_info *info, const void *custom);
76
77 void prng_factory_register(ccdrbg_factory_t factory);
78 void prng_cpu_init(int cpu);
79
80 void entropy_buffer_read(void *buffer, size_t *count);
81 void entropy_boot_trace(void);
82
83 /*
84 * Wrapper for requesting a CCDRBG operation.
85 * This macro makes the DRBG call with pre-emption disabled to ensure that
86 * any attempt to block will cause a panic. And the operation is timed and
87 * cannot exceed 10msec (for development kernels).
88 * But skip this while we retain Yarrow.
89 */
90 #define YARROW 1
91 #if YARROW
92 #define PRNG_CCDRBG(op) \
93 MACRO_BEGIN \
94 op; \
95 MACRO_END
96 #else
97 #define PRNG_CCDRBG(op) \
98 MACRO_BEGIN \
99 uint64_t start; \
100 uint64_t stop; \
101 disable_preemption(); \
102 start = mach_absolute_time(); \
103 op; \
104 stop = mach_absolute_time(); \
105 enable_preemption(); \
106 assert(stop - start < 10*NSEC_PER_MSEC || \
107 machine_timeout_suspended()); \
108 (void) start; \
109 (void) stop; \
110 MACRO_END
111 #endif
112
113 #endif /* XNU_KERNEL_PRIVATE */
114
115 /* /dev/random's PRNG is reseeded after generating this many bytes: */
116 #define RESEED_BYTES (17597)
117
118 __END_DECLS
119
120 #endif /* _PRNG_RANDOM_H_ */