]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc_config.h
2ce76e2bd454c73b393ee443a34d651276b99dee
1 /* Copyright (c) (2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020) Apple Inc. All rights reserved.
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.
12 #ifndef _CORECRYPTO_CC_CONFIG_H_
13 #define _CORECRYPTO_CC_CONFIG_H_
15 /* A word about configuration macros:
17 Conditional configuration macros specific to corecrypto should be named CORECRYPTO_xxx
18 or CCxx_yyy and be defined to be either 0 or 1 in this file. You can add an
19 #ifndef #error construct at the end of this file to make sure it's always defined.
21 They should always be tested using the #if directive, never the #ifdef directive.
23 No other conditional macros shall ever be used (except in this file)
25 Configuration Macros that are defined outside of corecrypto (eg: KERNEL, DEBUG, ...)
26 shall only be used in this file to define CCxxx macros.
28 External macros should be assumed to be either undefined, defined with no value,
29 or defined as true or false. We shall strive to build with -Wundef whenever possible,
30 so the following construct should be used to test external macros in this file:
32 #if defined(DEBUG) && (DEBUG)
33 #define CORECRYPTO_DEBUG 1
35 #define CORECRYPTO_DEBUG 0
39 It is acceptable to define a conditional CC_xxxx macro in an implementation file,
40 to be used only in this file.
42 The current code is not guaranteed to follow those rules, but should be fixed to.
44 Corecrypto requires GNU and C99 compatibility.
45 Typically enabled by passing --gnu --c99 to the compiler (eg. armcc)
49 //Do not set this macros to 1, unless you are developing/testing for Linux under macOS
50 #define CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT 0
52 //Do not set these macros to 1, unless you are developing/testing for Windows under macOS
53 #define CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT 0
54 #define CORECRYPTO_HACK_FOR_WINDOWS_DEVELOPMENT 0
56 #if (defined(DEBUG) && (DEBUG)) || defined(_DEBUG) //MSVC defines _DEBUG
57 /* CC_DEBUG is already used in CommonCrypto */
58 #define CORECRYPTO_DEBUG 1
60 #define CORECRYPTO_DEBUG 0
63 // This macro can be used to enable prints when a condition in the macro "cc_require"
64 // is false. This is especially useful to confirm that negative testing fails
65 // at the intended location
66 #define CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS 0
68 #if defined(KERNEL) && (KERNEL)
69 #define CC_KERNEL 1 // KEXT, XNU repo or kernel components such as AppleKeyStore
74 #if defined(LINUX_SGX) && (LINUX_SGX)
80 #if (defined(__linux__) && !(CC_SGX)) || CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT
86 #if defined(__ANDROID__) && (__ANDROID__)
92 #if defined(USE_L4) && (USE_L4)
98 #if defined(RTKIT) && (RTKIT)
104 #if defined(RTKITROM) && (RTKITROM)
105 #define CC_RTKITROM 1
107 #define CC_RTKITROM 0
110 #if defined(USE_SEPROM) && (USE_SEPROM)
111 #define CC_USE_SEPROM 1
113 #define CC_USE_SEPROM 0
116 #if defined(USE_S3) && (USE_S3)
122 #if (defined(ICE_FEATURES_ENABLED)) || (defined(MAVERICK) && (MAVERICK))
123 #define CC_BASEBAND 1
125 #define CC_BASEBAND 0
128 #if defined(EFI) && (EFI)
134 #if defined(IBOOT) && (IBOOT)
140 #if defined(TARGET_OS_BRIDGE)
141 #define CC_BRIDGE TARGET_OS_BRIDGE
146 // Check if we're running on a generic, userspace platform, i.e., not in the kernel, SEP, etc.
147 #ifndef CC_GENERIC_PLATFORM
148 #define CC_GENERIC_PLATFORM \
149 (!CC_RTKIT && !CC_KERNEL && !CC_USE_L4 && \
150 !CC_RTKITROM && !CC_EFI && !CC_IBOOT && \
151 !CC_USE_SEPROM && !CC_ANDROID && !CC_LINUX && \
155 // Check for availability of internal Darwin SPIs.
156 #ifndef CC_DARWIN_SPIS_AVAILABLE
157 #if defined(__has_include)
158 #define CC_DARWIN_SPIS_AVAILABLE __has_include(<os/log_private.h>)
160 #define CC_DARWIN_SPIS_AVAILABLE 0
164 // Check for open source builds
166 // ccringbuffer availability
167 // Only enable the ccringbuffer data structure in generic, userspace builds where memory allocation is not an issue.
168 #ifndef CC_RINGBUFFER_AVAILABLE
169 #define CC_RINGBUFFER_AVAILABLE (CC_GENERIC_PLATFORM && CC_DARWIN_SPIS_AVAILABLE && !CC_OPEN_SOURCE)
172 // os_log integration
173 // Only enable logging support in generic, userspace builds with the desired Darwin SPIs.
174 #ifndef CC_LOGGING_AVAILABLE
175 #define CC_LOGGING_AVAILABLE (CC_GENERIC_PLATFORM && CC_DARWIN_SPIS_AVAILABLE && !CC_OPEN_SOURCE)
178 // FeatureFlag integration
179 // Only enable feature flag support in generic, userspace builds with the desired Darwin SPIs.
180 // This requires linking against libsystem_featureflags to function correctly.
181 #ifndef CC_FEATURE_FLAGS_AVAILABLE
182 #if defined(__has_include)
183 #define CC_FEATURE_FLAGS_AVAILABLE __has_include(<os/feature_private.h>)
185 #define CC_FEATURE_FLAGS_AVAILABLE 0
189 // Macro to determine if a specific feature is available.
190 // Turn off all features at compile time if desired and avoid the runtime check by changing this
191 // definition to 0. Limit this functionality to the same environments wherein the ringbuffer is available.
192 #ifndef CC_FEATURE_ENABLED
193 #if (CC_RINGBUFFER_AVAILABLE && CC_FEATURE_FLAGS_AVAILABLE && !defined(__i386__))
194 #define CC_FEATURE_ENABLED(FEATURE) os_feature_enabled(Cryptography, FEATURE)
196 #define CC_FEATURE_ENABLED(FEATURE) 0
200 // Trace usage of deprecated or obscure functions. For now, this is
201 // completely disabled.
203 #define CC_LOG_TRACE 0
206 // Defined by the XNU build scripts
207 // Applies to code embedded in XNU but NOT to the kext
208 #if defined(XNU_KERNEL_PRIVATE)
209 #define CC_XNU_KERNEL_PRIVATE 1
211 #define CC_XNU_KERNEL_PRIVATE 0
214 // handle unaligned data, if the cpu cannot. Currently for gladman AES and the C version of the SHA256
215 #define CC_HANDLE_UNALIGNED_DATA CC_BASEBAND
217 // BaseBand configuration
221 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
222 #if defined(ENDIAN_LITTLE) || (defined(__arm__) && !defined(__BIG_ENDIAN))
223 #define __LITTLE_ENDIAN__
224 #elif !defined(ENDIAN_BIG) && !defined(__BIG_ENDIAN)
225 #error Baseband endianess not defined.
227 #define AESOPT_ENDIAN_NO_FILE
231 #define CCN_UNIT_SIZE 4 // 32 bits
233 // -- External function
234 #define assert ASSERT // sanity
237 // Ignore irrelevant warnings after verification
238 // #1254-D: arithmetic on pointer to void or function type
239 // #186-D: pointless comparison of unsigned integer with zero
240 // #546-D: transfer of control bypasses initialization of
242 #pragma diag_suppress 186, 1254,546
243 #elif defined(__GNUC__)
244 // warning: pointer of type 'void *' used in arithmetic
245 #pragma GCC diagnostic ignored "-Wpointer-arith"
247 #define CC_SMALL_CODE 1
249 #endif // CC_BASEBAND
251 #if CC_RTKIT || CC_RTKITROM
252 #define CC_SMALL_CODE 1
256 #ifndef CC_SMALL_CODE
257 #define CC_SMALL_CODE 0
260 //CC_XNU_KERNEL_AVAILABLE indicates the availibity of XNU kernel functions,
261 //like what we have on OSX, iOS, tvOS, Watch OS
262 #if defined(__APPLE__) && defined(__MACH__)
263 #define CC_XNU_KERNEL_AVAILABLE 1
265 #define CC_XNU_KERNEL_AVAILABLE 0
268 //arm arch64 definition for gcc
269 #if defined(__GNUC__) && defined(__aarch64__) && !defined(__arm64__)
273 #if !defined(CCN_UNIT_SIZE)
274 #if defined(__arm64__) || defined(__x86_64__) || defined(_WIN64)
275 #define CCN_UNIT_SIZE 8
276 #elif defined(__arm__) || defined(__i386__) || defined(_WIN32)
277 #define CCN_UNIT_SIZE 4
279 #error undefined architecture
281 #endif /* !defined(CCN_UNIT_SIZE) */
284 //this allows corecrypto Windows development using xcode
285 #if defined(CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT)
286 #if CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT && CC_XNU_KERNEL_AVAILABLE && CORECRYPTO_DEBUG
288 #define CC_USE_HEAP_FOR_WORKSPACE 1
289 #if (CCN_UNIT_SIZE==8)
290 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 0
292 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1
297 #if !defined(CCN_UINT128_SUPPORT_FOR_64BIT_ARCH)
298 #if defined(_WIN64) && defined(_WIN32) && (CCN_UNIT_SIZE==8)
299 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 0
300 #elif defined(_WIN32)
301 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1 //should not be a problem
303 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1
307 #if defined(_MSC_VER)
308 #if defined(__clang__)
309 #define CC_ALIGNED(x) __attribute__ ((aligned(x))) //clang compiler
311 #define CC_ALIGNED(x) __declspec(align(x)) //MS complier
314 #if __clang__ || CCN_UNIT_SIZE==8
315 #define CC_ALIGNED(x) __attribute__ ((aligned(x)))
317 #define CC_ALIGNED(x) __attribute__ ((aligned((x)>8?8:(x))))
322 //this is copied from <arm/arch.h>, because <arm/arch.h> is not available on SEPROM environment
323 #if defined (__ARM_ARCH_7A__) || defined (__ARM_ARCH_7S__) || defined (__ARM_ARCH_7F__) || defined (__ARM_ARCH_7K__) || defined(__ARM_ARCH_7EM__)
327 #if defined(__ARM_ARCH_6M__) || defined(__TARGET_ARCH_6S_M) || defined (__armv6m__)
332 #if defined(__arm64__) || defined(__arm__)
335 #elif defined(__x86_64__) || defined(__i386__)
341 /* For corecrypto kext, CC_STATIC should be undefined */
345 #if !defined(CC_USE_HEAP_FOR_WORKSPACE)
346 #if CC_USE_S3 || CC_USE_SEPROM || CC_RTKITROM
347 #define CC_USE_HEAP_FOR_WORKSPACE 0
349 #define CC_USE_HEAP_FOR_WORKSPACE 1
353 /* memset_s is only available in few target */
354 #if CC_USE_SEPROM || defined(__CC_ARM) \
355 || defined(__hexagon__) || CC_EFI
356 #define CC_HAS_MEMSET_S 0
358 #define CC_HAS_MEMSET_S 1
361 // Include target conditionals if available.
362 #if defined(__has_include) /* portability */
363 #if __has_include(<TargetConditionals.h>)
364 #include <TargetConditionals.h>
365 #endif /* __has_include(<TargetConditionals.h>) */
366 #endif /* defined(__has_include) */
368 // Disable RSA Keygen on iBridge
369 #if defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE && CC_KERNEL
370 #define CC_DISABLE_RSAKEYGEN 1 /* for iBridge */
372 #define CC_DISABLE_RSAKEYGEN 0 /* default */
375 #if (CCN_UNIT_SIZE == 8) && !( defined(_MSC_VER) && defined(__clang__))
376 #define CCEC25519_CURVE25519_64BIT 1
378 #define CCEC25519_CURVE25519_64BIT 0
381 //- functions implemented in assembly ------------------------------------------
382 //this the list of corecrypto clients that use assembly and the clang compiler
383 #if !(CC_XNU_KERNEL_AVAILABLE || CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || CC_USE_S3) && !defined(_WIN32) && CORECRYPTO_DEBUG
384 #warning "You are using the default corecrypto configuration, assembly optimizations may not be available for your platform"
387 // Enable assembler in Linux if CC_LINUX_ASM is defined
388 #if (CC_LINUX || CC_SGX) && defined(CC_LINUX_ASM) && CC_LINUX_ASM
392 // Use this macro to strictly disable assembly regardless of cpu/os/compiler/etc.
393 // Our assembly code is not gcc compatible. Clang defines the __GNUC__ macro as well.
394 #if !defined(CC_USE_ASM)
395 #if defined(_WIN32) || CC_EFI || CC_BASEBAND || CC_XNU_KERNEL_PRIVATE || (defined(__GNUC__) && !defined(__clang__)) || defined(__ANDROID_API__) || CC_LINUX
402 #define CC_CACHE_DESCRIPTORS CC_KERNEL
405 #if defined(_ARM_ARCH_7) && __clang__ && CC_USE_ASM
406 #define CCN_DEDICATED_SQR CC_SMALL_CODE
407 #define CCN_MUL_KARATSUBA 0 // no performance improvement
408 #define CCN_ADD_ASM 1
409 #define CCN_SUB_ASM 1
410 #define CCN_MUL_ASM 0
411 #define CCN_ADDMUL1_ASM 1
412 #define CCN_MUL1_ASM 1
413 #define CCN_CMP_ASM 1
414 #define CCN_ADD1_ASM 1
415 #define CCN_SUB1_ASM 1
417 #define CCN_SET_ASM 1
418 #define CCN_SHIFT_RIGHT_ASM 1
419 #if defined(__ARM_NEON__)
420 #define CCN_SHIFT_LEFT_ASM 1
422 #define CCN_SHIFT_LEFT_ASM 0
424 #define CCN_MULMOD_224_ASM 1
425 #define CCN_MULMOD_256_ASM 1
426 #define CCAES_ARM_ASM 1
427 #define CCAES_INTEL_ASM 0
428 #if CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || CC_USE_S3
433 #define CCN_USE_BUILTIN_CLZ 1
434 #define CCSHA1_VNG_INTEL 0
435 #define CCSHA2_VNG_INTEL 0
437 #if defined(__ARM_NEON__) || CC_KERNEL
438 #define CCSHA1_VNG_ARM 1
439 #define CCSHA2_VNG_ARM 1
440 #else /* !defined(__ARM_NEON__) */
441 #define CCSHA1_VNG_ARM 0
442 #define CCSHA2_VNG_ARM 0
443 #endif /* !defined(__ARM_NEON__) */
444 #define CCSHA256_ARMV6M_ASM 0
446 #define CC_ACCELERATECRYPTO 1
449 #elif defined(__arm64__) && __clang__ && CC_USE_ASM
450 #define CCN_DEDICATED_SQR CC_SMALL_CODE
451 #define CCN_MUL_KARATSUBA 0 // 4*n CCN_UNIT extra memory required.
452 #define CCN_ADD_ASM 1
453 #define CCN_SUB_ASM 1
454 #define CCN_MUL_ASM 1
455 #define CCN_ADDMUL1_ASM 0
456 #define CCN_MUL1_ASM 0
457 #define CCN_CMP_ASM 1
458 #define CCN_ADD1_ASM 0
459 #define CCN_SUB1_ASM 0
461 #define CCN_SET_ASM 0
462 #define CCN_SHIFT_RIGHT_ASM 1
463 #define CCN_SHIFT_LEFT_ASM 1
464 #define CCN_MULMOD_224_ASM 1
465 #define CCN_MULMOD_256_ASM 1
466 #define CCAES_ARM_ASM 1
467 #define CCAES_INTEL_ASM 0
468 #define CCAES_MUX 0 // On 64bit SoC, asm is much faster than HW
469 #define CCN_USE_BUILTIN_CLZ 1
470 #define CCSHA1_VNG_INTEL 0
471 #define CCSHA2_VNG_INTEL 0
472 #define CCSHA1_VNG_ARM 1
473 #define CCSHA2_VNG_ARM 1
474 #define CCSHA256_ARMV6M_ASM 0
476 #define CC_ACCELERATECRYPTO 1
479 #elif (defined(__x86_64__) || defined(__i386__)) && __clang__ && CC_USE_ASM
480 #define CCN_DEDICATED_SQR 1
481 #define CCN_MUL_KARATSUBA 0 // 4*n CCN_UNIT extra memory required.
482 /* These assembly routines only work for a single CCN_UNIT_SIZE. */
483 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8) || (defined(__i386__) && CCN_UNIT_SIZE == 4)
484 #define CCN_ADD_ASM 1
485 #define CCN_SUB_ASM 1
486 #define CCN_MUL_ASM 1
488 #define CCN_ADD_ASM 0
489 #define CCN_SUB_ASM 0
490 #define CCN_MUL_ASM 0
493 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8)
494 #define CCN_CMP_ASM 1
496 #define CCN_SHIFT_RIGHT_ASM 1
497 #define CCN_SHIFT_LEFT_ASM 1
499 #define CCN_CMP_ASM 0
501 #define CCN_SHIFT_RIGHT_ASM 0
502 #define CCN_SHIFT_LEFT_ASM 0
505 #define CCN_MULMOD_224_ASM 0
506 #if defined(__x86_64__) && CCN_UNIT_SIZE == 8 && !CC_SGX
507 #define CCN_MULMOD_256_ASM 1
508 #define CCN_ADDMUL1_ASM 1
509 #define CCN_MUL1_ASM 1
511 #define CCN_MULMOD_256_ASM 0
512 #define CCN_ADDMUL1_ASM 0
513 #define CCN_MUL1_ASM 0
515 #define CCN_ADD1_ASM 0
516 #define CCN_SUB1_ASM 0
517 #define CCN_SET_ASM 0
518 #define CCAES_ARM_ASM 0
519 #define CCAES_INTEL_ASM 1
521 #define CCN_USE_BUILTIN_CLZ 0
522 #define CCSHA1_VNG_INTEL 1
523 #define CCSHA2_VNG_INTEL 1
524 #define CCSHA1_VNG_ARM 0
525 #define CCSHA2_VNG_ARM 0
526 #define CCSHA256_ARMV6M_ASM 0
528 #define CC_ACCELERATECRYPTO 1
530 //-(4) disable assembly
532 #if CCN_UINT128_SUPPORT_FOR_64BIT_ARCH
533 #define CCN_DEDICATED_SQR 1
535 #define CCN_DEDICATED_SQR 0 //when assembly is off and 128-bit integers are not supported, dedicated square is off. This is the case on Windows
537 #define CCN_MUL_KARATSUBA 0 // 4*n CCN_UNIT extra memory required.
538 #define CCN_ADD_ASM 0
539 #define CCN_SUB_ASM 0
540 #define CCN_MUL_ASM 0
541 #define CCN_ADDMUL1_ASM 0
542 #define CCN_MUL1_ASM 0
543 #define CCN_CMP_ASM 0
544 #define CCN_ADD1_ASM 0
545 #define CCN_SUB1_ASM 0
547 #define CCN_SET_ASM 0
548 #define CCN_SHIFT_RIGHT_ASM 0
549 #define CCN_SHIFT_LEFT_ASM 0
550 #define CCN_MULMOD_224_ASM 0
551 #define CCN_MULMOD_256_ASM 0
552 #define CCAES_ARM_ASM 0
553 #define CCAES_INTEL_ASM 0
555 #define CCN_USE_BUILTIN_CLZ 0
556 #define CCSHA1_VNG_INTEL 0
557 #define CCSHA2_VNG_INTEL 0
558 #define CCSHA1_VNG_ARM 0
559 #define CCSHA2_VNG_ARM 0
560 #define CCSHA256_ARMV6M_ASM 0
562 #define CC_ACCELERATECRYPTO 0
566 #define CC_INLINE static inline
569 #define CC_NORETURN __attribute__((__noreturn__))
570 #define CC_NOTHROW __attribute__((__nothrow__))
571 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
572 #define CC_NONNULL4 CC_NONNULL((4))
573 #define CC_NONNULL_ALL __attribute__((__nonnull__))
574 #define CC_SENTINEL __attribute__((__sentinel__))
575 // Only apply the `CC_CONST` attribute to functions with no side-effects where the output is a strict function of pass by value input vars with no exterior side-effects.
576 // Specifically, do not apply CC_CONST if the function has any arguments that are pointers (directly, or indirectly)
577 #define CC_CONST __attribute__((__const__))
578 #define CC_PURE __attribute__((__pure__))
579 #define CC_WARN_RESULT __attribute__((__warn_unused_result__))
580 #define CC_MALLOC_CLEAR __attribute__((__malloc__))
581 #define CC_UNUSED __attribute__((unused))
582 #else /* !__GNUC__ */
586 #define CC_NONNULL(N)
594 #define CC_NONNULL_ALL
602 #define CC_WARN_RESULT
604 #define CC_MALLOC_CLEAR
605 #endif /* !__GNUC__ */
608 // Bridge differences between MachO and ELF compiler/assemblers. */
609 #if CC_LINUX || CC_SGX
610 #define CC_ASM_SECTION_CONST .rodata
611 #define CC_ASM_PRIVATE_EXTERN .hidden
613 // We need to be sure that assembler can access relocated C
614 // symbols. Sad but this is the quickest way to do that, at least with
615 // our current linux compiler (clang-3.4).
616 #define CC_C_LABEL(_sym) _sym@PLT
618 #define CC_C_LABEL(_sym) _sym
621 #else /* !CC_LINUX && !CC_SGX */
622 #define CC_ASM_SECTION_CONST .const
623 #define CC_ASM_PRIVATE_EXTERN .private_extern
624 #define CC_C_LABEL(_sym) _##_sym
625 #define _IMM(x) $$(x)
626 #endif /* !CC_LINUX && !CC_SGX */
628 // Enable FIPSPOST function tracing only when supported. */
629 #ifdef CORECRYPTO_POST_TRACE
630 #define CC_FIPSPOST_TRACE 1
632 #define CC_FIPSPOST_TRACE 0
635 #ifndef CC_INTERNAL_SDK
636 #if __has_include(<System/i386/cpu_capabilities.h>)
637 #define CC_INTERNAL_SDK 1
638 #elif __has_include(<System/arm/cpu_capabilities.h>)
639 #define CC_INTERNAL_SDK 1
641 #define CC_INTERNAL_SDK 0
645 #endif /* _CORECRYPTO_CC_CONFIG_H_ */