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 uint32_t buffer
[ENTROPY_BUFFER_SIZE
];
45 extern entropy_data_t EntropyData
;
47 /* Trace codes for DBG_SEC_KERNEL: */
48 #define ENTROPY_READ(n) SECURITYDBG_CODE(DBG_SEC_KERNEL, n) /* n: 0 .. 3 */
51 * Early_random implementation params: */
52 #define EARLY_RANDOM_SEED_SIZE (16)
53 #define EARLY_RANDOM_STATE_STATIC_SIZE (264)
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)
59 #error architecture unknown
62 #include <corecrypto/ccdrbg.h>
63 #include <corecrypto/ccsha1.h>
65 typedef struct ccdrbg_info ccdrbg_info_t
;
66 typedef void (*ccdrbg_factory_t
)(ccdrbg_info_t
*info
, const void *custom
);
68 extern void ccdrbg_factory_yarrow(ccdrbg_info_t
*info
, const void *custom
);
70 void prng_factory_register(ccdrbg_factory_t factory
);
71 void prng_cpu_init(int cpu
);
73 void entropy_buffer_read(char *buffer
, unsigned int *count
);
74 void entropy_boot_trace(void);
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.
85 #define PRNG_CCDRBG(op) \
90 #define PRNG_CCDRBG(op) \
94 disable_preemption(); \
95 start = mach_absolute_time(); \
97 stop = mach_absolute_time(); \
98 enable_preemption(); \
99 assert(stop - start < 10*NSEC_PER_MSEC || \
100 machine_timeout_suspended()); \
106 #endif /* XNU_KERNEL_PRIVATE */
108 /* /dev/random's PRNG is reseeded after generating this many bytes: */
109 #define RESEED_BYTES (17597)
113 #endif /* _PRNG_RANDOM_H_ */