]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc_config.h
xnu-6153.41.3.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cc_config.h
1 /*
2 * cc_config.h
3 * corecrypto
4 *
5 * Created on 11/16/2010
6 *
7 * Copyright (c) 2010,2011,2012,2013,2014,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11 #ifndef _CORECRYPTO_CC_CONFIG_H_
12 #define _CORECRYPTO_CC_CONFIG_H_
13
14 /* A word about configuration macros:
15
16 Conditional configuration macros specific to corecrypto should be named CORECRYPTO_xxx
17 or CCxx_yyy and be defined to be either 0 or 1 in this file. You can add an
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.
26
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:
30
31 #if defined(DEBUG) && (DEBUG)
32 #define CORECRYPTO_DEBUG 1
33 #else
34 #define CORECRYPTO_DEBUG 0
35 #endif
36
37
38 It is acceptable to define a conditional CC_xxxx macro in an implementation file,
39 to be used only in this file.
40
41 The current code is not guaranteed to follow those rules, but should be fixed to.
42
43 Corecrypto requires GNU and C99 compatibility.
44 Typically enabled by passing --gnu --c99 to the compiler (eg. armcc)
45
46 */
47
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
52 #define CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT 0
53 #define CORECRYPTO_HACK_FOR_WINDOWS_DEVELOPMENT 0 //to be removed after <rdar://problem/27304763> port corecrypto to Windows
54
55 #if (defined(DEBUG) && (DEBUG)) || defined(_DEBUG) //MSVC defines _DEBUG
56 /* CC_DEBUG is already used in CommonCrypto */
57 #define CORECRYPTO_DEBUG 1
58 #else
59 #define CORECRYPTO_DEBUG 0
60 #endif
61
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
67 #if defined(KERNEL) && (KERNEL)
68 #define CC_KERNEL 1 // KEXT, XNU repo or kernel components such as AppleKeyStore
69 #else
70 #define CC_KERNEL 0
71 #endif
72
73 #if defined(__linux__) || CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT
74 #define CC_LINUX 1
75 #else
76 #define CC_LINUX 0
77 #endif
78
79 #if defined(USE_L4) && (USE_L4)
80 #define CC_USE_L4 1
81 #else
82 #define CC_USE_L4 0
83 #endif
84
85 #if defined(RTKIT) && (RTKIT)
86 #define CC_RTKIT 1
87 #else
88 #define CC_RTKIT 0
89 #endif
90
91 #if defined(RTKITROM) && (RTKITROM)
92 #define CC_RTKITROM 1
93 #else
94 #define CC_RTKITROM 0
95 #endif
96
97 #if defined(USE_SEPROM) && (USE_SEPROM)
98 #define CC_USE_SEPROM 1
99 #else
100 #define CC_USE_SEPROM 0
101 #endif
102
103 #if defined(USE_S3) && (USE_S3)
104 #define CC_USE_S3 1
105 #else
106 #define CC_USE_S3 0
107 #endif
108
109 #if (defined(ICE_FEATURES_ENABLED)) || (defined(MAVERICK) && (MAVERICK))
110 #define CC_BASEBAND 1
111 #else
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
119 #endif
120
121 #if defined(IBOOT) && (IBOOT)
122 #define CC_IBOOT 1
123 #else
124 #define CC_IBOOT 0
125 #endif
126
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
139 #if CC_BASEBAND
140
141 // -- ENDIANESS
142 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
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
149 #endif
150
151 // -- Architecture
152 #define CCN_UNIT_SIZE 4 // 32 bits
153
154 // -- External function
155 #define assert ASSERT // sanity
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
162 #ifdef __arm__
163 #pragma diag_suppress 186, 1254,546
164 #elif defined(__GNUC__)
165 // warning: pointer of type 'void *' used in arithmetic
166 #pragma GCC diagnostic ignored "-Wpointer-arith"
167 #endif // __arm__
168 #define CC_SMALL_CODE 1
169
170 #endif // CC_BASEBAND
171
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
181 //CC_XNU_KERNEL_AVAILABLE indicates the availibity of XNU kernel functions,
182 //like what we have on OSX, iOS, tvOS, Watch OS
183 #if defined(__APPLE__) && defined(__MACH__)
184 #define CC_XNU_KERNEL_AVAILABLE 1
185 #else
186 #define CC_XNU_KERNEL_AVAILABLE 0
187 #endif
188
189 //arm arch64 definition for gcc
190 #if defined(__GNUC__) && defined(__aarch64__) && !defined(__arm64__)
191 #define __arm64__
192 #endif
193
194 #if !defined(CCN_UNIT_SIZE)
195 #if defined(__arm64__) || defined(__x86_64__) || defined(_WIN64)
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
202 #endif /* !defined(CCN_UNIT_SIZE) */
203
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
228 #if defined(_MSC_VER)
229 #if defined(__clang__)
230 #define CC_ALIGNED(x) __attribute__ ((aligned(x))) //clang compiler
231 #else
232 #define CC_ALIGNED(x) __declspec(align(x)) //MS complier
233 #endif
234 #else
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
240 #endif
241
242 #if defined(__arm__)
243 //this is copied from <arm/arch.h>, because <arm/arch.h> is not available on SEPROM environment
244 #if defined (__ARM_ARCH_7A__) || defined (__ARM_ARCH_7S__) || defined (__ARM_ARCH_7F__) || defined (__ARM_ARCH_7K__) || defined(__ARM_ARCH_7EM__)
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
252
253 #if defined(__arm64__) || defined(__arm__)
254 #define CCN_IOS 1
255 #define CCN_OSX 0
256 #elif defined(__x86_64__) || defined(__i386__)
257 #define CCN_IOS 0
258 #define CCN_OSX 1
259 #endif
260
261 #if CC_USE_S3
262 /* For corecrypto kext, CC_STATIC should be undefined */
263 #define CC_STATIC 1
264 #endif
265
266 #if !defined(CC_USE_HEAP_FOR_WORKSPACE)
267 #if CC_USE_S3 || CC_USE_SEPROM || CC_RTKITROM
268 #define CC_USE_HEAP_FOR_WORKSPACE 0
269 #else
270 #define CC_USE_HEAP_FOR_WORKSPACE 1
271 #endif
272 #endif
273
274 /* memset_s is only available in few target */
275 #if CC_USE_SEPROM || defined(__CC_ARM) \
276 || defined(__hexagon__) || CC_EFI
277 #define CC_HAS_MEMSET_S 0
278 #else
279 #define CC_HAS_MEMSET_S 1
280 #endif
281
282 // Include target conditionals if available.
283 #if defined(__has_include) /* portability */
284 #if __has_include(<TargetConditionals.h>)
285 #include <TargetConditionals.h>
286 #endif /* __has_include(<TargetConditionals.h>) */
287 #endif /* defined(__has_include) */
288
289 // Disable RSA Keygen on iBridge
290 #if defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE && CC_KERNEL
291 #define CC_DISABLE_RSAKEYGEN 1 /* for iBridge */
292 #else
293 #define CC_DISABLE_RSAKEYGEN 0 /* default */
294 #endif
295
296 // see rdar://problem/26636018
297 #if (CCN_UNIT_SIZE == 8) && !( defined(_MSC_VER) && defined(__clang__))
298 #define CCEC25519_CURVE25519_64BIT 1
299 #else
300 #define CCEC25519_CURVE25519_64BIT 0
301 #endif
302
303 //- functions implemented in assembly ------------------------------------------
304 //this the list of corecrypto clients that use assembly and the clang compiler
305 #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
306 #warning "You are using the default corecrypto configuration, assembly optimizations may not be available for your platform"
307 #endif
308
309 // Enable assembler in Linux if CC_LINUX_ASM is defined
310 #if CC_LINUX && defined(CC_LINUX_ASM) && CC_LINUX_ASM
311 #define CC_USE_ASM 1
312 #endif
313
314 // Use this macro to strictly disable assembly regardless of cpu/os/compiler/etc.
315 // Our assembly code is not gcc compatible. Clang defines the __GNUC__ macro as well.
316 #if !defined(CC_USE_ASM)
317 #if defined(_WIN32) || CC_EFI || CC_BASEBAND || CC_XNU_KERNEL_PRIVATE || (defined(__GNUC__) && !defined(__clang__)) || defined(__ANDROID_API__) || CC_LINUX
318 #define CC_USE_ASM 0
319 #else
320 #define CC_USE_ASM 1
321 #endif
322 #endif
323
324 //-(1) ARM V7
325 #if defined(_ARM_ARCH_7) && __clang__ && CC_USE_ASM
326 #define CCN_DEDICATED_SQR CC_SMALL_CODE
327 #define CCN_MUL_KARATSUBA 0 // no performance improvement
328 #define CCN_ADD_ASM 1
329 #define CCN_SUB_ASM 1
330 #define CCN_MUL_ASM 0
331 #define CCN_ADDMUL1_ASM 1
332 #define CCN_MUL1_ASM 1
333 #define CCN_CMP_ASM 1
334 #define CCN_ADD1_ASM 1
335 #define CCN_SUB1_ASM 1
336 #define CCN_N_ASM 1
337 #define CCN_SET_ASM 1
338 #define CCN_SHIFT_RIGHT_ASM 1
339 #if defined(__ARM_NEON__)
340 #define CCN_SHIFT_LEFT_ASM 1
341 #else
342 #define CCN_SHIFT_LEFT_ASM 0
343 #endif
344 #define CCN_MOD_224_ASM 1
345 #define CCN_MULMOD_256_ASM 1
346 #define CCAES_ARM_ASM 1
347 #define CCAES_INTEL_ASM 0
348 #if CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || CC_USE_S3
349 #define CCAES_MUX 0
350 #else
351 #define CCAES_MUX 1
352 #endif
353 #define CCN_USE_BUILTIN_CLZ 1
354 #define CCSHA1_VNG_INTEL 0
355 #define CCSHA2_VNG_INTEL 0
356
357 #if defined(__ARM_NEON__) || CC_KERNEL
358 #define CCSHA1_VNG_ARM 1
359 #define CCSHA2_VNG_ARM 1
360 #else /* !defined(__ARM_NEON__) */
361 #define CCSHA1_VNG_ARM 0
362 #define CCSHA2_VNG_ARM 0
363 #endif /* !defined(__ARM_NEON__) */
364 #define CCSHA256_ARMV6M_ASM 0
365
366 #define CC_ACCELERATECRYPTO 1
367
368 //-(2) ARM 64
369 #elif defined(__arm64__) && __clang__ && CC_USE_ASM
370 #define CCN_DEDICATED_SQR CC_SMALL_CODE
371 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
372 #define CCN_ADD_ASM 1
373 #define CCN_SUB_ASM 1
374 #define CCN_MUL_ASM 1
375 #define CCN_ADDMUL1_ASM 0
376 #define CCN_MUL1_ASM 0
377 #define CCN_CMP_ASM 1
378 #define CCN_ADD1_ASM 0
379 #define CCN_SUB1_ASM 0
380 #define CCN_N_ASM 1
381 #define CCN_SET_ASM 0
382 #define CCN_SHIFT_RIGHT_ASM 1
383 #define CCN_SHIFT_LEFT_ASM 1
384 #define CCN_MOD_224_ASM 0
385 #define CCN_MULMOD_256_ASM 1
386 #define CCAES_ARM_ASM 1
387 #define CCAES_INTEL_ASM 0
388 #define CCAES_MUX 0 // On 64bit SoC, asm is much faster than HW
389 #define CCN_USE_BUILTIN_CLZ 1
390 #define CCSHA1_VNG_INTEL 0
391 #define CCSHA2_VNG_INTEL 0
392 #define CCSHA1_VNG_ARM 1
393 #define CCSHA2_VNG_ARM 1
394 #define CCSHA256_ARMV6M_ASM 0
395
396 #define CC_ACCELERATECRYPTO 1
397
398 //-(3) Intel 32/64
399 #elif (defined(__x86_64__) || defined(__i386__)) && __clang__ && CC_USE_ASM
400 #define CCN_DEDICATED_SQR 1
401 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
402 /* These assembly routines only work for a single CCN_UNIT_SIZE. */
403 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8) || (defined(__i386__) && CCN_UNIT_SIZE == 4)
404 #define CCN_ADD_ASM 1
405 #define CCN_SUB_ASM 1
406 #define CCN_MUL_ASM 1
407 #else
408 #define CCN_ADD_ASM 0
409 #define CCN_SUB_ASM 0
410 #define CCN_MUL_ASM 0
411 #endif
412
413 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8)
414 #define CCN_CMP_ASM 1
415 #define CCN_N_ASM 1
416 #define CCN_SHIFT_RIGHT_ASM 1
417 #define CCN_SHIFT_LEFT_ASM 1
418 #else
419 #define CCN_CMP_ASM 0
420 #define CCN_N_ASM 0
421 #define CCN_SHIFT_RIGHT_ASM 0
422 #define CCN_SHIFT_LEFT_ASM 0
423 #endif
424
425 #define CCN_MOD_224_ASM 0
426 #define CCN_MULMOD_256_ASM 0
427 #define CCN_ADDMUL1_ASM 0
428 #define CCN_MUL1_ASM 0
429 #define CCN_ADD1_ASM 0
430 #define CCN_SUB1_ASM 0
431 #define CCN_SET_ASM 0
432 #define CCAES_ARM_ASM 0
433 #define CCAES_INTEL_ASM 1
434 #define CCAES_MUX 0
435 #define CCN_USE_BUILTIN_CLZ 0
436 #define CCSHA1_VNG_INTEL 1
437 #define CCSHA2_VNG_INTEL 1
438 #define CCSHA1_VNG_ARM 0
439 #define CCSHA2_VNG_ARM 0
440 #define CCSHA256_ARMV6M_ASM 0
441
442 #define CC_ACCELERATECRYPTO 1
443
444 //-(4) disable assembly
445 #else
446 #if CCN_UINT128_SUPPORT_FOR_64BIT_ARCH
447 #define CCN_DEDICATED_SQR 1
448 #else
449 #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
450 #endif
451 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
452 #define CCN_ADD_ASM 0
453 #define CCN_SUB_ASM 0
454 #define CCN_MUL_ASM 0
455 #define CCN_ADDMUL1_ASM 0
456 #define CCN_MUL1_ASM 0
457 #define CCN_CMP_ASM 0
458 #define CCN_ADD1_ASM 0
459 #define CCN_SUB1_ASM 0
460 #define CCN_N_ASM 0
461 #define CCN_SET_ASM 0
462 #define CCN_SHIFT_RIGHT_ASM 0
463 #define CCN_SHIFT_LEFT_ASM 0
464 #define CCN_MOD_224_ASM 0
465 #define CCN_MULMOD_256_ASM 0
466 #define CCAES_ARM_ASM 0
467 #define CCAES_INTEL_ASM 0
468 #define CCAES_MUX 0
469 #define CCN_USE_BUILTIN_CLZ 0
470 #define CCSHA1_VNG_INTEL 0
471 #define CCSHA2_VNG_INTEL 0
472 #define CCSHA1_VNG_ARM 0
473 #define CCSHA2_VNG_ARM 0
474 #define CCSHA256_ARMV6M_ASM 0
475
476 #define CC_ACCELERATECRYPTO 0
477
478 #endif
479
480 #define CC_INLINE static inline
481
482 #ifdef __GNUC__
483 #define CC_NORETURN __attribute__((__noreturn__))
484 #define CC_NOTHROW __attribute__((__nothrow__))
485 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
486 #define CC_NONNULL4 CC_NONNULL((4))
487 #define CC_NONNULL_ALL __attribute__((__nonnull__))
488 #define CC_SENTINEL __attribute__((__sentinel__))
489 // 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.
490 // Specifically, do not apply CC_CONST if the function has any arguments that are pointers (directly, or indirectly)
491 #define CC_CONST __attribute__((__const__))
492 #define CC_PURE __attribute__((__pure__))
493 #define CC_WARN_RESULT __attribute__((__warn_unused_result__))
494 #define CC_MALLOC_CLEAR __attribute__((__malloc__))
495 #define CC_UNUSED __attribute__((unused))
496 #else /* !__GNUC__ */
497 /*! @parseOnly */
498 #define CC_UNUSED
499 /*! @parseOnly */
500 #define CC_NONNULL(N)
501 /*! @parseOnly */
502 #define CC_NONNULL4
503 /*! @parseOnly */
504 #define CC_NORETURN
505 /*! @parseOnly */
506 #define CC_NOTHROW
507 /*! @parseOnly */
508 #define CC_NONNULL_ALL
509 /*! @parseOnly */
510 #define CC_SENTINEL
511 /*! @parseOnly */
512 #define CC_CONST
513 /*! @parseOnly */
514 #define CC_PURE
515 /*! @parseOnly */
516 #define CC_WARN_RESULT
517 /*! @parseOnly */
518 #define CC_MALLOC_CLEAR
519 #endif /* !__GNUC__ */
520
521
522 // Bridge differences between MachO and ELF compiler/assemblers. */
523 #if CC_USE_ASM
524 #if CC_LINUX
525 #define CC_ASM_SECTION_CONST .rodata
526 #define CC_ASM_PRIVATE_EXTERN .hidden
527 #define CC_C_LABEL(_sym) _sym
528 #else /* !CC_LINUX */
529 #define CC_ASM_SECTION_CONST .const
530 #define CC_ASM_PRIVATE_EXTERN .private_extern
531 #define CC_C_LABEL(_sym) _##_sym
532 #endif /* !CC_LINUX */
533 #endif /* CC_USE_ASM */
534
535
536 // Enable FIPSPOST function tracing only when supported. */
537 #ifdef CORECRYPTO_POST_TRACE
538 #define CC_FIPSPOST_TRACE 1
539 #else
540 #define CC_FIPSPOST_TRACE 0
541 #endif
542
543 #endif /* _CORECRYPTO_CC_CONFIG_H_ */