2 * Copyright (c) 2013 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_RANDOM_H_
30 #define _PRNG_RANDOM_H_
34 #ifdef XNU_KERNEL_PRIVATE
36 #define ENTROPY_BUFFER_BYTE_SIZE 64
38 #define ENTROPY_BUFFER_SIZE (ENTROPY_BUFFER_BYTE_SIZE / sizeof(uint32_t))
40 typedef struct entropy_data
{
42 * TODO: Should index_ptr be volatile? Are we exposed to any races that
43 * we care about if it is not?
46 uint32_t buffer
[ENTROPY_BUFFER_SIZE
];
49 extern entropy_data_t EntropyData
;
51 /* Trace codes for DBG_SEC_KERNEL: */
52 #define ENTROPY_READ(n) SECURITYDBG_CODE(DBG_SEC_KERNEL, n) /* n: 0 .. 3 */
55 * Early_random implementation params: */
56 #define EARLY_RANDOM_SEED_SIZE (16)
57 #define EARLY_RANDOM_STATE_STATIC_SIZE (264)
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)
67 #error architecture unknown
70 #include <corecrypto/ccdrbg.h>
71 #include <corecrypto/ccsha1.h>
73 typedef void (*ccdrbg_factory_t
)(struct ccdrbg_info
*info
, const void *custom
);
75 extern void ccdrbg_factory_yarrow(struct ccdrbg_info
*info
, const void *custom
);
77 void prng_factory_register(ccdrbg_factory_t factory
);
78 void prng_cpu_init(int cpu
);
80 void entropy_buffer_read(void *buffer
, size_t *count
);
81 void entropy_boot_trace(void);
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.
92 #define PRNG_CCDRBG(op) \
97 #define PRNG_CCDRBG(op) \
101 disable_preemption(); \
102 start = mach_absolute_time(); \
104 stop = mach_absolute_time(); \
105 enable_preemption(); \
106 assert(stop - start < 10*NSEC_PER_MSEC || \
107 machine_timeout_suspended()); \
113 #endif /* XNU_KERNEL_PRIVATE */
115 /* /dev/random's PRNG is reseeded after generating this many bytes: */
116 #define RESEED_BYTES (17597)
120 #endif /* _PRNG_RANDOM_H_ */