]> git.saurik.com Git - apple/xnu.git/blame - EXTERNAL_HEADERS/corecrypto/cc_config.h
xnu-4903.241.1.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cc_config.h
CommitLineData
316670eb
A
1/*
2 * cc_config.h
3 * corecrypto
4 *
3e170ce0
A
5 * Created on 11/16/2010
6 *
7 * Copyright (c) 2010,2011,2012,2013,2014,2015 Apple Inc. All rights reserved.
316670eb
A
8 *
9 */
fe8ab488 10
316670eb
A
11#ifndef _CORECRYPTO_CC_CONFIG_H_
12#define _CORECRYPTO_CC_CONFIG_H_
13
fe8ab488 14/* A word about configuration macros:
d9a64523 15
fe8ab488 16 Conditional configuration macros specific to corecrypto should be named CORECRYPTO_xxx
d9a64523 17 or CCxx_yyy and be defined to be either 0 or 1 in this file. You can add an
fe8ab488
A
18 #ifndef #error construct at the end of this file to make sure it's always defined.
19
20 They should always be tested using the #if directive, never the #ifdef directive.
21
22 No other conditional macros shall ever be used (except in this file)
23
24 Configuration Macros that are defined outside of corecrypto (eg: KERNEL, DEBUG, ...)
25 shall only be used in this file to define CCxxx macros.
d9a64523 26
fe8ab488
A
27 External macros should be assumed to be either undefined, defined with no value,
28 or defined as true or false. We shall strive to build with -Wundef whenever possible,
29 so the following construct should be used to test external macros in this file:
d9a64523 30
fe8ab488
A
31 #if defined(DEBUG) && (DEBUG)
32 #define CORECRYPTO_DEBUG 1
33 #else
34 #define CORECRYPTO_DEBUG 0
35 #endif
d9a64523 36
fe8ab488
A
37
38 It is acceptable to define a conditional CC_xxxx macro in an implementation file,
39 to be used only in this file.
d9a64523 40
fe8ab488 41 The current code is not guaranteed to follow those rules, but should be fixed to.
d9a64523 42
fe8ab488
A
43 Corecrypto requires GNU and C99 compatibility.
44 Typically enabled by passing --gnu --c99 to the compiler (eg. armcc)
45
46*/
47
5ba3f43e
A
48//Do not set this macros to 1, unless you are developing/testing for Linux under macOS
49#define CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT 0
50
51//Do not set these macros to 1, unless you are developing/testing for Windows under macOS
39037602 52#define CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT 0
5ba3f43e 53#define CORECRYPTO_HACK_FOR_WINDOWS_DEVELOPMENT 0 //to be removed after <rdar://problem/27304763> port corecrypto to Windows
39037602 54
39037602 55#if (defined(DEBUG) && (DEBUG)) || defined(_DEBUG) //MSVC defines _DEBUG
fe8ab488 56/* CC_DEBUG is already used in CommonCrypto */
39037602 57 #define CORECRYPTO_DEBUG 1
fe8ab488 58#else
39037602 59 #define CORECRYPTO_DEBUG 0
fe8ab488
A
60#endif
61
39037602
A
62// This macro can be used to enable prints when a condition in the macro "cc_require"
63// is false. This is especially useful to confirm that negative testing fails
64// at the intended location
65#define CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS 0
66
fe8ab488 67#if defined(KERNEL) && (KERNEL)
39037602 68 #define CC_KERNEL 1 // KEXT, XNU repo or kernel components such as AppleKeyStore
fe8ab488 69#else
39037602 70 #define CC_KERNEL 0
fe8ab488
A
71#endif
72
5ba3f43e 73#if defined(__linux__) || CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT
39037602 74 #define CC_LINUX 1
3e170ce0 75#else
39037602 76 #define CC_LINUX 0
3e170ce0
A
77#endif
78
fe8ab488 79#if defined(USE_L4) && (USE_L4)
39037602 80 #define CC_USE_L4 1
fe8ab488 81#else
39037602 82 #define CC_USE_L4 0
fe8ab488
A
83#endif
84
5ba3f43e
A
85#if defined(RTKIT) && (RTKIT)
86 #define CC_RTKIT 1
87#else
88 #define CC_RTKIT 0
89#endif
90
d9a64523
A
91#if defined(RTKITROM) && (RTKITROM)
92#define CC_RTKITROM 1
93#else
94#define CC_RTKITROM 0
95#endif
96
3e170ce0 97#if defined(USE_SEPROM) && (USE_SEPROM)
39037602 98 #define CC_USE_SEPROM 1
3e170ce0 99#else
39037602 100 #define CC_USE_SEPROM 0
3e170ce0
A
101#endif
102
103#if defined(USE_S3) && (USE_S3)
39037602 104 #define CC_USE_S3 1
3e170ce0 105#else
39037602 106 #define CC_USE_S3 0
3e170ce0
A
107#endif
108
39037602
A
109#if (defined(ICE_FEATURES_ENABLED)) || (defined(MAVERICK) && (MAVERICK))
110 #define CC_BASEBAND 1
fe8ab488 111#else
39037602
A
112 #define CC_BASEBAND 0
113#endif
114
115#if defined(EFI) && (EFI)
116 #define CC_EFI 1
117#else
118 #define CC_EFI 0
fe8ab488
A
119#endif
120
121#if defined(IBOOT) && (IBOOT)
39037602 122 #define CC_IBOOT 1
fe8ab488 123#else
39037602 124 #define CC_IBOOT 0
fe8ab488
A
125#endif
126
5ba3f43e
A
127// Defined by the XNU build scripts
128// Applies to code embedded in XNU but NOT to the kext
129#if defined(XNU_KERNEL_PRIVATE)
130 #define CC_XNU_KERNEL_PRIVATE 1
131#else
132 #define CC_XNU_KERNEL_PRIVATE 0
133#endif
134
135// handle unaligned data, if the cpu cannot. Currently for gladman AES and the C version of the SHA256
136#define CC_HANDLE_UNALIGNED_DATA CC_BASEBAND
137
138// BaseBand configuration
39037602 139#if CC_BASEBAND
fe8ab488
A
140
141// -- ENDIANESS
5ba3f43e 142#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
39037602
A
143 #if defined(ENDIAN_LITTLE) || (defined(__arm__) && !defined(__BIG_ENDIAN))
144 #define __LITTLE_ENDIAN__
145 #elif !defined(ENDIAN_BIG) && !defined(__BIG_ENDIAN)
146 #error Baseband endianess not defined.
147 #endif
148 #define AESOPT_ENDIAN_NO_FILE
5ba3f43e 149#endif
fe8ab488
A
150
151// -- Architecture
39037602 152 #define CCN_UNIT_SIZE 4 // 32 bits
fe8ab488
A
153
154// -- External function
39037602 155 #define assert ASSERT // sanity
fe8ab488
A
156
157// -- Warnings
158// Ignore irrelevant warnings after verification
159// #1254-D: arithmetic on pointer to void or function type
160// #186-D: pointless comparison of unsigned integer with zero
161// #546-D: transfer of control bypasses initialization of
5ba3f43e
A
162 #ifdef __arm__
163 #pragma diag_suppress 186, 1254,546
164 #elif defined(__GNUC__)
fe8ab488 165// warning: pointer of type 'void *' used in arithmetic
39037602 166 #pragma GCC diagnostic ignored "-Wpointer-arith"
5ba3f43e 167 #endif // __arm__
d9a64523
A
168#define CC_SMALL_CODE 1
169
39037602 170#endif // CC_BASEBAND
fe8ab488 171
d9a64523
A
172#if CC_RTKIT || CC_RTKITROM
173#define CC_SMALL_CODE 1
174#endif
175
176
177#ifndef CC_SMALL_CODE
178#define CC_SMALL_CODE 0
179#endif
180
39037602
A
181//CC_XNU_KERNEL_AVAILABLE indicates the availibity of XNU kernel functions,
182//like what we have on OSX, iOS, tvOS, Watch OS
d9a64523 183#if defined(__APPLE__) && defined(__MACH__)
39037602 184 #define CC_XNU_KERNEL_AVAILABLE 1
316670eb 185#else
39037602 186 #define CC_XNU_KERNEL_AVAILABLE 0
316670eb 187#endif
39037602 188
5ba3f43e
A
189//arm arch64 definition for gcc
190#if defined(__GNUC__) && defined(__aarch64__) && !defined(__arm64__)
191 #define __arm64__
192#endif
193
39037602 194#if !defined(CCN_UNIT_SIZE)
d9a64523 195 #if defined(__arm64__) || defined(__x86_64__) || defined(_WIN64)
39037602
A
196 #define CCN_UNIT_SIZE 8
197 #elif defined(__arm__) || defined(__i386__) || defined(_WIN32)
198 #define CCN_UNIT_SIZE 4
199 #else
200 #error undefined architecture
201 #endif
316670eb
A
202#endif /* !defined(CCN_UNIT_SIZE) */
203
39037602
A
204
205//this allows corecrypto Windows development using xcode
206#if defined(CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT)
207 #if CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT && CC_XNU_KERNEL_AVAILABLE && CORECRYPTO_DEBUG
208 #define CC_USE_ASM 0
209 #define CC_USE_HEAP_FOR_WORKSPACE 1
210 #if (CCN_UNIT_SIZE==8)
211 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 0
212 #else
213 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1
214 #endif
215 #endif
216#endif
217
218#if !defined(CCN_UINT128_SUPPORT_FOR_64BIT_ARCH)
219 #if defined(_WIN64) && defined(_WIN32) && (CCN_UNIT_SIZE==8)
220 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 0
221 #elif defined(_WIN32)
222 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1//should not be a problem
223 #else
224 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1
225 #endif
226#endif
227
5ba3f43e
A
228#if defined(_MSC_VER)
229 #if defined(__clang__)
d9a64523 230 #define CC_ALIGNED(x) __attribute__ ((aligned(x))) //clang compiler
5ba3f43e
A
231 #else
232 #define CC_ALIGNED(x) __declspec(align(x)) //MS complier
233 #endif
39037602 234#else
5ba3f43e
A
235 #if __clang__ || CCN_UNIT_SIZE==8
236 #define CC_ALIGNED(x) __attribute__ ((aligned(x)))
237 #else
238 #define CC_ALIGNED(x) __attribute__ ((aligned((x)>8?8:(x))))
239 #endif
39037602
A
240#endif
241
5ba3f43e
A
242#if defined(__arm__)
243//this is copied from <arm/arch.h>, because <arm/arch.h> is not available on SEPROM environment
d9a64523 244#if defined (__ARM_ARCH_7A__) || defined (__ARM_ARCH_7S__) || defined (__ARM_ARCH_7F__) || defined (__ARM_ARCH_7K__) || defined(__ARM_ARCH_7EM__)
5ba3f43e
A
245 #define _ARM_ARCH_7
246 #endif
247
248 #if defined(__ARM_ARCH_6M__) || defined(__TARGET_ARCH_6S_M) || defined (__armv6m__)
249 #define _ARM_ARCH_6M
250 #endif
251#endif
39037602 252
5ba3f43e
A
253#if defined(__arm64__) || defined(__arm__)
254 #define CCN_IOS 1
255 #define CCN_OSX 0
256#elif defined(__x86_64__) || defined(__i386__)
39037602
A
257 #define CCN_IOS 0
258 #define CCN_OSX 1
d9a64523 259#endif
fe8ab488 260
3e170ce0 261#if CC_USE_L4 || CC_USE_S3
316670eb 262/* No dynamic linking allowed in L4, e.g. avoid nonlazy symbols */
3e170ce0 263/* For corecrypto kext, CC_STATIC should be undefined */
39037602 264 #define CC_STATIC 1
fe8ab488 265#endif
316670eb 266
39037602 267#if !defined(CC_USE_HEAP_FOR_WORKSPACE)
d9a64523 268 #if CC_USE_S3 || CC_USE_SEPROM || CC_RTKITROM
39037602 269 #define CC_USE_HEAP_FOR_WORKSPACE 0
5ba3f43e
A
270 #else
271 #define CC_USE_HEAP_FOR_WORKSPACE 1
39037602 272 #endif
fe8ab488
A
273#endif
274
3e170ce0 275/* memset_s is only available in few target */
5ba3f43e 276#if CC_USE_SEPROM || defined(__CC_ARM) \
39037602
A
277 || defined(__hexagon__) || CC_EFI
278 #define CC_HAS_MEMSET_S 0
3e170ce0 279#else
39037602 280 #define CC_HAS_MEMSET_S 1
3e170ce0
A
281#endif
282
39037602
A
283// Include target conditionals if available.
284#if defined(__has_include) /* portability */
285#if __has_include(<TargetConditionals.h>)
286#include <TargetConditionals.h>
287#endif /* __has_include(<TargetConditionals.h>) */
288#endif /* defined(__has_include) */
289
5ba3f43e 290// Disable RSA Keygen on iBridge
743345f9
A
291#if defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE && CC_KERNEL
292#define CC_DISABLE_RSAKEYGEN 1 /* for iBridge */
293#else
294#define CC_DISABLE_RSAKEYGEN 0 /* default */
295#endif
296
d9a64523
A
297// see rdar://problem/26636018
298#if (CCN_UNIT_SIZE == 8) && !( defined(_MSC_VER) && defined(__clang__))
299#define CCEC25519_CURVE25519DONNA_64BIT 1
300#else
301#define CCEC25519_CURVE25519DONNA_64BIT 0
302#endif
303
39037602
A
304//- functions implemented in assembly ------------------------------------------
305//this the list of corecrypto clients that use assembly and the clang compiler
d9a64523 306#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
39037602 307 #warning "You are using the default corecrypto configuration, assembly optimizations may not be available for your platform"
316670eb
A
308#endif
309
5ba3f43e
A
310// Use this macro to strictly disable assembly regardless of cpu/os/compiler/etc.
311// Our assembly code is not gcc compatible. Clang defines the __GNUC__ macro as well.
39037602 312#if !defined(CC_USE_ASM)
d9a64523 313 #if defined(_WIN32) || CC_EFI || CC_BASEBAND || CC_XNU_KERNEL_PRIVATE || (defined(__GNUC__) && !defined(__clang__)) || defined(__ANDROID_API__) || CC_RTKIT || CC_RTKITROM
39037602
A
314 #define CC_USE_ASM 0
315 #else
316 #define CC_USE_ASM 1
317 #endif
3e170ce0
A
318#endif
319
39037602
A
320//-(1) ARM V7
321#if defined(_ARM_ARCH_7) && __clang__ && CC_USE_ASM
d9a64523 322 #define CCN_DEDICATED_SQR CC_SMALL_CODE
39037602
A
323 #define CCN_MUL_KARATSUBA 0 // no performance improvement
324 #define CCN_ADD_ASM 1
325 #define CCN_SUB_ASM 1
326 #define CCN_MUL_ASM 0
327 #define CCN_ADDMUL1_ASM 1
328 #define CCN_MUL1_ASM 1
329 #define CCN_CMP_ASM 1
330 #define CCN_ADD1_ASM 0
331 #define CCN_SUB1_ASM 0
332 #define CCN_N_ASM 1
333 #define CCN_SET_ASM 1
334 #define CCN_SHIFT_RIGHT_ASM 1
335 #define CCAES_ARM_ASM 1
336 #define CCAES_INTEL_ASM 0
d9a64523 337 #if CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || CC_USE_S3
39037602
A
338 #define CCAES_MUX 0
339 #else
340 #define CCAES_MUX 1
341 #endif
342 #define CCN_USE_BUILTIN_CLZ 1
343 #define CCSHA1_VNG_INTEL 0
344 #define CCSHA2_VNG_INTEL 0
345
346 #if defined(__ARM_NEON__) || CC_KERNEL
347 #define CCSHA1_VNG_ARMV7NEON 1
348 #define CCSHA2_VNG_ARMV7NEON 1
349 #else /* !defined(__ARM_NEON__) */
350 #define CCSHA1_VNG_ARMV7NEON 0
351 #define CCSHA2_VNG_ARMV7NEON 0
352 #endif /* !defined(__ARM_NEON__) */
353 #define CCSHA256_ARMV6M_ASM 0
354
355//-(2) ARM 64
5ba3f43e 356#elif defined(__arm64__) && __clang__ && CC_USE_ASM
d9a64523 357 #define CCN_DEDICATED_SQR CC_SMALL_CODE
5ba3f43e
A
358 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
359 #define CCN_ADD_ASM 1
360 #define CCN_SUB_ASM 1
361 #define CCN_MUL_ASM 1
362 #define CCN_ADDMUL1_ASM 0
363 #define CCN_MUL1_ASM 0
364 #define CCN_CMP_ASM 1
365 #define CCN_ADD1_ASM 0
366 #define CCN_SUB1_ASM 0
367 #define CCN_N_ASM 1
368 #define CCN_SET_ASM 0
369 #define CCN_SHIFT_RIGHT_ASM 1
370 #define CCAES_ARM_ASM 1
371 #define CCAES_INTEL_ASM 0
372 #define CCAES_MUX 0 // On 64bit SoC, asm is much faster than HW
373 #define CCN_USE_BUILTIN_CLZ 1
374 #define CCSHA1_VNG_INTEL 0
375 #define CCSHA2_VNG_INTEL 0
376 #define CCSHA1_VNG_ARMV7NEON 1 // reused this to avoid making change to xcode project, put arm64 assembly code with armv7 code
377 #define CCSHA2_VNG_ARMV7NEON 1
378 #define CCSHA256_ARMV6M_ASM 0
379
380//-(3) Intel 32/64
39037602
A
381#elif (defined(__x86_64__) || defined(__i386__)) && __clang__ && CC_USE_ASM
382 #define CCN_DEDICATED_SQR 1
383 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
384 /* These assembly routines only work for a single CCN_UNIT_SIZE. */
385 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8) || (defined(__i386__) && CCN_UNIT_SIZE == 4)
386 #define CCN_ADD_ASM 1
387 #define CCN_SUB_ASM 1
388 #define CCN_MUL_ASM 1
389 #else
390 #define CCN_ADD_ASM 0
391 #define CCN_SUB_ASM 0
392 #define CCN_MUL_ASM 0
393 #endif
394
395 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8)
396 #define CCN_CMP_ASM 1
397 #define CCN_N_ASM 1
398 #define CCN_SHIFT_RIGHT_ASM 1
399 #else
400 #define CCN_CMP_ASM 0
401 #define CCN_N_ASM 0
402 #define CCN_SHIFT_RIGHT_ASM 0
403 #endif
404
405 #define CCN_ADDMUL1_ASM 0
406 #define CCN_MUL1_ASM 0
407 #define CCN_ADD1_ASM 0
408 #define CCN_SUB1_ASM 0
409 #define CCN_SET_ASM 0
410 #define CCAES_ARM_ASM 0
411 #define CCAES_INTEL_ASM 1
412 #define CCAES_MUX 0
413 #define CCN_USE_BUILTIN_CLZ 0
414 #define CCSHA1_VNG_INTEL 1
415 #define CCSHA2_VNG_INTEL 1
416 #define CCSHA1_VNG_ARMV7NEON 0
417 #define CCSHA2_VNG_ARMV7NEON 0
418 #define CCSHA256_ARMV6M_ASM 0
419
d9a64523 420//-(4) disable assembly
316670eb 421#else
39037602
A
422 #if CCN_UINT128_SUPPORT_FOR_64BIT_ARCH
423 #define CCN_DEDICATED_SQR 1
424 #else
425 #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
426 #endif
427 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
428 #define CCN_ADD_ASM 0
429 #define CCN_SUB_ASM 0
430 #define CCN_MUL_ASM 0
431 #define CCN_ADDMUL1_ASM 0
432 #define CCN_MUL1_ASM 0
433 #define CCN_CMP_ASM 0
434 #define CCN_ADD1_ASM 0
435 #define CCN_SUB1_ASM 0
436 #define CCN_N_ASM 0
437 #define CCN_SET_ASM 0
438 #define CCN_SHIFT_RIGHT_ASM 0
439 #define CCAES_ARM_ASM 0
440 #define CCAES_INTEL_ASM 0
441 #define CCAES_MUX 0
442 #define CCN_USE_BUILTIN_CLZ 0
443 #define CCSHA1_VNG_INTEL 0
444 #define CCSHA2_VNG_INTEL 0
445 #define CCSHA1_VNG_ARMV7NEON 0
446 #define CCSHA2_VNG_ARMV7NEON 0
447 #define CCSHA256_ARMV6M_ASM 0
448
449#endif
316670eb 450
316670eb
A
451#define CC_INLINE static inline
452
39037602
A
453#ifdef __GNUC__
454 #define CC_NORETURN __attribute__((__noreturn__))
455 #define CC_NOTHROW __attribute__((__nothrow__))
456 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
d9a64523 457 #define CC_NONNULL4 CC_NONNULL((4))
39037602
A
458 #define CC_NONNULL_ALL __attribute__((__nonnull__))
459 #define CC_SENTINEL __attribute__((__sentinel__))
460 #define CC_CONST __attribute__((__const__))
461 #define CC_PURE __attribute__((__pure__))
462 #define CC_WARN_RESULT __attribute__((__warn_unused_result__))
463 #define CC_MALLOC __attribute__((__malloc__))
464 #define CC_UNUSED __attribute__((unused))
316670eb
A
465#else /* !__GNUC__ */
466/*! @parseOnly */
39037602 467 #define CC_UNUSED
fe8ab488 468/*! @parseOnly */
39037602 469 #define CC_NONNULL(N)
316670eb 470/*! @parseOnly */
39037602 471 #define CC_NONNULL4
316670eb 472/*! @parseOnly */
d9a64523 473 #define CC_NORETURN
316670eb 474/*! @parseOnly */
d9a64523 475 #define CC_NOTHROW
316670eb 476/*! @parseOnly */
39037602 477 #define CC_NONNULL_ALL
316670eb 478/*! @parseOnly */
39037602 479 #define CC_SENTINEL
316670eb 480/*! @parseOnly */
39037602 481 #define CC_CONST
316670eb 482/*! @parseOnly */
39037602 483 #define CC_PURE
316670eb 484/*! @parseOnly */
39037602 485 #define CC_WARN_RESULT
316670eb 486/*! @parseOnly */
39037602 487 #define CC_MALLOC
316670eb
A
488#endif /* !__GNUC__ */
489
5ba3f43e
A
490// Enable FIPSPOST function tracing only when supported. */
491#ifdef CORECRYPTO_POST_TRACE
492#define CC_FIPSPOST_TRACE 1
493#else
494#define CC_FIPSPOST_TRACE 0
495#endif
3e170ce0 496
316670eb 497#endif /* _CORECRYPTO_CC_CONFIG_H_ */