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