]> git.saurik.com Git - apple/xnu.git/blob - osfmk/prng/random.h
xnu-3789.60.24.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 uint32_t *index_ptr;
42 uint32_t buffer[ENTROPY_BUFFER_SIZE];
43 } entropy_data_t;
44
45 extern entropy_data_t EntropyData;
46
47 /* Trace codes for DBG_SEC_KERNEL: */
48 #define ENTROPY_READ(n) SECURITYDBG_CODE(DBG_SEC_KERNEL, n) /* n: 0 .. 3 */
49
50 /*
51 * Early_random implementation params: */
52 #define EARLY_RANDOM_SEED_SIZE (16)
53 #define EARLY_RANDOM_STATE_STATIC_SIZE (264)
54
55 #if defined (__x86_64__)
56 #define current_prng_context() (current_cpu_datap()->cpu_prng)
57 #define master_prng_context() (cpu_datap(master_cpu)->cpu_prng)
58 #else
59 #error architecture unknown
60 #endif
61
62 #include <corecrypto/ccdrbg.h>
63 #include <corecrypto/ccsha1.h>
64
65 typedef struct ccdrbg_info ccdrbg_info_t;
66 typedef void (*ccdrbg_factory_t)(ccdrbg_info_t *info, const void *custom);
67
68 extern void ccdrbg_factory_yarrow(ccdrbg_info_t *info, const void *custom);
69
70 void prng_factory_register(ccdrbg_factory_t factory);
71 void prng_cpu_init(int cpu);
72
73 void entropy_buffer_read(char *buffer, unsigned int *count);
74 void entropy_boot_trace(void);
75
76 /*
77 * Wrapper for requesting a CCDRBG operation.
78 * This macro makes the DRBG call with pre-emption disabled to ensure that
79 * any attempt to block will cause a panic. And the operation is timed and
80 * cannot exceed 10msec (for development kernels).
81 * But skip this while we retain Yarrow.
82 */
83 #define YARROW 1
84 #if YARROW
85 #define PRNG_CCDRBG(op) \
86 MACRO_BEGIN \
87 op; \
88 MACRO_END
89 #else
90 #define PRNG_CCDRBG(op) \
91 MACRO_BEGIN \
92 uint64_t start; \
93 uint64_t stop; \
94 disable_preemption(); \
95 start = mach_absolute_time(); \
96 op; \
97 stop = mach_absolute_time(); \
98 enable_preemption(); \
99 assert(stop - start < 10*NSEC_PER_MSEC || \
100 machine_timeout_suspended()); \
101 (void) start; \
102 (void) stop; \
103 MACRO_END
104 #endif
105
106 #endif /* XNU_KERNEL_PRIVATE */
107
108 /* /dev/random's PRNG is reseeded after generating this many bytes: */
109 #define RESEED_BYTES (17597)
110
111 __END_DECLS
112
113 #endif /* _PRNG_RANDOM_H_ */