1 /* Copyright (c) (2019,2021) Apple Inc. All rights reserved.
2 *
3 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4 * is contained in the License.txt file distributed with corecrypto) and only to
5 * people who accept that license. IMPORTANT: Any license rights granted to you by
6 * Apple Inc. (if any) are limited to internal use within your organization only on
7 * devices and computers you own or control, for the sole purpose of verifying the
8 * security characteristics and correct functioning of the Apple Software. You may
9 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10 *
11 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
12 *
13 * This file contains Original Code and/or Modifications of Original Code
14 * as defined in and that are subject to the Apple Public Source License
15 * Version 2.0 (the 'License'). You may not use this file except in
16 * compliance with the License. The rights granted to you under the License
17 * may not be used to create, or enable the creation or redistribution of,
18 * unlawful or unlicensed copies of an Apple operating system, or to
19 * circumvent, violate, or enable the circumvention or violation of, any
20 * terms of an Apple operating system software license agreement.
21 *
22 * Please obtain a copy of the License at
23 * http://www.opensource.apple.com/apsl/ and read it before using this file.
24 *
25 * The Original Code and all software distributed under the License are
26 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
27 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
28 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
30 * Please see the License for the specific language governing rights and
31 * limitations under the License.
32 *
33 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34 */
35
36 #ifndef _CORECRYPTO_CC_INTERNAL_H_
37 #define _CORECRYPTO_CC_INTERNAL_H_
38
39 #include <corecrypto/cc_priv.h>
40
41 #if CC_XNU_KERNEL_PRIVATE
42
43 #elif CC_KERNEL
44 #include <libkern/libkern.h>
45 #else
46 #include <stdlib.h>
47 #include <stdio.h>
48 #endif
49
50 #include "cc_macros.h"
51
52 extern bool cc_rdrand(uint64_t *rand);
53
54 #if CC_BUILT_FOR_TESTING
55 extern bool (*cc_rdrand_mock)(uint64_t *rand);
56
57 extern void (*cc_abort_mock)(const char *msg);
58 #endif
59
60 #if CC_DIT_SUPPORTED
61
62 CC_INLINE bool
cc_is_dit_enabled(void)63 cc_is_dit_enabled(void)
64 {
65 return __builtin_arm_rsr64("DIT") == (1U << 24);
66 }
67
68 CC_INLINE bool
cc_enable_dit(void)69 cc_enable_dit(void)
70 {
71 // DIT might have already been enabled by another corecrypto function, in
72 // that case that function is responsible for disabling DIT when returning.
73 //
74 // This also covers when code _outside_ corecrypto enabled DIT before
75 // calling us. In that case we're not responsible for disabling it either.
76 if (cc_is_dit_enabled()) {
77 return false;
78 }
79
80 // Enable DIT.
81 __builtin_arm_wsr64("DIT", 1);
82
83 // Check that DIT was enabled.
84 cc_try_abort_if(!cc_is_dit_enabled(), "DIT not enabled");
85
86 // To the cleanup function, indicate that we toggled DIT and
87 // that cc_disable_dit() should actually disable it again.
88 return true;
89 }
90
91 void cc_disable_dit(volatile bool *dit_was_enabled);
92
93 #define CC_ENSURE_DIT_ENABLED \
94 volatile bool _cc_dit_auto_disable \
95 __attribute__((cleanup(cc_disable_dit))) \
96 __attribute__((unused)) = cc_enable_dit();
97
98 #else
99
100 #define CC_ENSURE_DIT_ENABLED
101
102 #endif // CC_DIT_SUPPORTED
103
104 #endif // _CORECRYPTO_CC_INTERNAL_H_
105