]> git.saurik.com Git - apple/xnu.git/blame - osfmk/prng/random.h
xnu-4570.20.62.tar.gz
[apple/xnu.git] / osfmk / prng / random.h
CommitLineData
fe8ab488
A
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
5ba3f43e 36#define ENTROPY_BUFFER_BYTE_SIZE 64
fe8ab488 37
5ba3f43e 38#define ENTROPY_BUFFER_SIZE (ENTROPY_BUFFER_BYTE_SIZE / sizeof(uint32_t))
fe8ab488
A
39
40typedef struct entropy_data {
5ba3f43e
A
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];
fe8ab488
A
47} entropy_data_t;
48
49extern 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: */
5ba3f43e
A
56#define EARLY_RANDOM_SEED_SIZE (16)
57#define EARLY_RANDOM_STATE_STATIC_SIZE (264)
fe8ab488
A
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)
5ba3f43e
A
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)
fe8ab488
A
66#else
67#error architecture unknown
68#endif
69
70#include <corecrypto/ccdrbg.h>
71#include <corecrypto/ccsha1.h>
72
5ba3f43e 73typedef void (*ccdrbg_factory_t)(struct ccdrbg_info *info, const void *custom);
fe8ab488 74
5ba3f43e 75extern void ccdrbg_factory_yarrow(struct ccdrbg_info *info, const void *custom);
fe8ab488 76
5ba3f43e
A
77void prng_factory_register(ccdrbg_factory_t factory);
78void prng_cpu_init(int cpu);
fe8ab488 79
5ba3f43e
A
80void entropy_buffer_read(void *buffer, size_t *count);
81void entropy_boot_trace(void);
fe8ab488
A
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) \
93MACRO_BEGIN \
94 op; \
95MACRO_END
96#else
97#define PRNG_CCDRBG(op) \
98MACRO_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; \
110MACRO_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_ */