]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc_config.h
807d58cea6bad74591f0c3443e89ba78449160c6
[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 these macros to 1, unless you are developing/testing for Windows
49 #define CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT 0
50 #define CORECRYPTO_HACK_FOR_WINDOWS_DEVELOPMENT 0 //to be removed after <rdar://problem/26585938> port corecrypto to Windows
51
52 //this macro is used to turn on/off usage of transparent union in corecrypto
53 //it should be commented out in corecrypto and be used only by the software that use corecrypto
54 //#define CORECRYPTO_DONOT_USE_TRANSPARENT_UNION
55 #ifdef CORECRYPTO_DONOT_USE_TRANSPARENT_UNION
56 #define CORECRYPTO_USE_TRANSPARENT_UNION 0
57 #else
58 #define CORECRYPTO_USE_TRANSPARENT_UNION 1
59 #endif
60
61 #if (defined(DEBUG) && (DEBUG)) || defined(_DEBUG) //MSVC defines _DEBUG
62 /* CC_DEBUG is already used in CommonCrypto */
63 #define CORECRYPTO_DEBUG 1
64 #else
65 #define CORECRYPTO_DEBUG 0
66 #endif
67
68 // This macro can be used to enable prints when a condition in the macro "cc_require"
69 // is false. This is especially useful to confirm that negative testing fails
70 // at the intended location
71 #define CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS 0
72
73 #if defined(KERNEL) && (KERNEL)
74 #define CC_KERNEL 1 // KEXT, XNU repo or kernel components such as AppleKeyStore
75 #else
76 #define CC_KERNEL 0
77 #endif
78
79 // LINUX_BUILD_TEST is for sanity check of the configuration
80 // > xcodebuild -scheme "corecrypto_test" OTHER_CFLAGS="$(values) -DLINUX_BUILD_TEST"
81 #if defined(__linux__) || defined(LINUX_BUILD_TEST)
82 #define CC_LINUX 1
83 #else
84 #define CC_LINUX 0
85 #endif
86
87 #if defined(USE_L4) && (USE_L4)
88 #define CC_USE_L4 1
89 #else
90 #define CC_USE_L4 0
91 #endif
92
93 #if defined(USE_SEPROM) && (USE_SEPROM)
94 #define CC_USE_SEPROM 1
95 #else
96 #define CC_USE_SEPROM 0
97 #endif
98
99 #if defined(USE_S3) && (USE_S3)
100 #define CC_USE_S3 1
101 #else
102 #define CC_USE_S3 0
103 #endif
104
105 #if (defined(ICE_FEATURES_ENABLED)) || (defined(MAVERICK) && (MAVERICK))
106 #define CC_BASEBAND 1
107 #else
108 #define CC_BASEBAND 0
109 #endif
110
111 #if defined(EFI) && (EFI)
112 #define CC_EFI 1
113 #else
114 #define CC_EFI 0
115 #endif
116
117 #if defined(IBOOT) && (IBOOT)
118 #define CC_IBOOT 1
119 #else
120 #define CC_IBOOT 0
121 #endif
122
123 // BB configuration
124 #if CC_BASEBAND
125
126 // -- ENDIANESS
127 #if defined(ENDIAN_LITTLE) || (defined(__arm__) && !defined(__BIG_ENDIAN))
128 #define __LITTLE_ENDIAN__
129 #elif !defined(ENDIAN_BIG) && !defined(__BIG_ENDIAN)
130 #error Baseband endianess not defined.
131 #endif
132 #define AESOPT_ENDIAN_NO_FILE
133
134 // -- Architecture
135 #define CCN_UNIT_SIZE 4 // 32 bits
136 #define SAFE_IO // AES support for unaligned Input/Output
137
138 // -- External function
139 #define assert ASSERT // sanity
140
141 // -- Warnings
142 // Ignore irrelevant warnings after verification
143 // #1254-D: arithmetic on pointer to void or function type
144 // #186-D: pointless comparison of unsigned integer with zero
145 // #546-D: transfer of control bypasses initialization of
146 #if defined(__GNUC__)
147 // warning: pointer of type 'void *' used in arithmetic
148 #pragma GCC diagnostic ignored "-Wpointer-arith"
149 #endif // arm or gnuc
150
151 #endif // CC_BASEBAND
152
153 //CC_XNU_KERNEL_AVAILABLE indicates the availibity of XNU kernel functions,
154 //like what we have on OSX, iOS, tvOS, Watch OS
155 #if defined(__APPLE__) && defined(__MACH__)
156 #define CC_XNU_KERNEL_AVAILABLE 1
157 #else
158 #define CC_XNU_KERNEL_AVAILABLE 0
159 #endif
160
161 #if !defined(CCN_UNIT_SIZE)
162 #if defined(__arm64__) || defined(__x86_64__) || defined(_WIN64)
163 #define CCN_UNIT_SIZE 8
164 #elif defined(__arm__) || defined(__i386__) || defined(_WIN32)
165 #define CCN_UNIT_SIZE 4
166 #else
167 #error undefined architecture
168 #endif
169 #endif /* !defined(CCN_UNIT_SIZE) */
170
171
172 //this allows corecrypto Windows development using xcode
173 #if defined(CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT)
174 #if CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT && CC_XNU_KERNEL_AVAILABLE && CORECRYPTO_DEBUG
175 #define CC_USE_ASM 0
176 #define CC_USE_HEAP_FOR_WORKSPACE 1
177 #if (CCN_UNIT_SIZE==8)
178 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 0
179 #else
180 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1
181 #endif
182 #endif
183 #endif
184
185 #if !defined(CCN_UINT128_SUPPORT_FOR_64BIT_ARCH)
186 #if defined(_WIN64) && defined(_WIN32) && (CCN_UNIT_SIZE==8)
187 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 0
188 #elif defined(_WIN32)
189 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1//should not be a problem
190 #else
191 #define CCN_UINT128_SUPPORT_FOR_64BIT_ARCH 1
192 #endif
193 #endif
194
195 #if __clang__ || CCN_UNIT_SIZE==8
196 #define CC_ALIGNED(x) __attribute__ ((aligned(x)))
197 #elif _MSC_VER
198 #define CC_ALIGNED(x) __declspec(align(x))
199 #else
200 #define CC_ALIGNED(x) __attribute__ ((aligned((x)>8?8:(x))))
201 #endif
202
203
204 #if defined(__x86_64__) || defined(__i386__)
205 #define CCN_IOS 0
206 #define CCN_OSX 1
207 #endif
208
209 #if CC_USE_L4 || CC_USE_S3
210 /* No dynamic linking allowed in L4, e.g. avoid nonlazy symbols */
211 /* For corecrypto kext, CC_STATIC should be undefined */
212 #define CC_STATIC 1
213 #endif
214
215 #if !defined(CC_USE_HEAP_FOR_WORKSPACE)
216 #if CC_USE_L4 || CC_IBOOT || CC_BASEBAND || defined(_MSC_VER)
217 /* For L4, stack is too short, need to use HEAP for some computations */
218 /* CC_USE_HEAP_FOR_WORKSPACE not supported for KERNEL! */
219 #define CC_USE_HEAP_FOR_WORKSPACE 1
220 #else
221 #define CC_USE_HEAP_FOR_WORKSPACE 0
222 #endif
223 #endif
224
225 /* memset_s is only available in few target */
226 #if CC_KERNEL || CC_USE_SEPROM || defined(__CC_ARM) \
227 || defined(__hexagon__) || CC_EFI
228 #define CC_HAS_MEMSET_S 0
229 #else
230 #define CC_HAS_MEMSET_S 1
231 #endif
232
233 // Include target conditionals if available.
234 #if defined(__has_include) /* portability */
235 #if __has_include(<TargetConditionals.h>)
236 #include <TargetConditionals.h>
237 #endif /* __has_include(<TargetConditionals.h>) */
238 #endif /* defined(__has_include) */
239
240 // Disable FIPS key gen algorithm on userland and kext so that related POST
241 // is skipped and boot time is reduced
242 #if defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE && CC_KERNEL
243 #define CC_DISABLE_RSAKEYGEN 1 /* for iBridge */
244 #else
245 #define CC_DISABLE_RSAKEYGEN 0 /* default */
246 #endif
247
248 //- functions implemented in assembly ------------------------------------------
249 //this the list of corecrypto clients that use assembly and the clang compiler
250 #if !(CC_XNU_KERNEL_AVAILABLE || CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_USE_SEPROM || CC_USE_S3) && !defined(_WIN32) && CORECRYPTO_DEBUG
251 #warning "You are using the default corecrypto configuration, assembly optimizations may not be available for your platform"
252 #endif
253
254 // use this macro to strictly disable assembly regardless of cpu/os/compiler/etc
255 #if !defined(CC_USE_ASM)
256 #if defined(_MSC_VER) || CC_LINUX || CC_EFI || CC_BASEBAND
257 #define CC_USE_ASM 0
258 #else
259 #define CC_USE_ASM 1
260 #endif
261 #endif
262
263 //-(1) ARM V7
264 #if defined(_ARM_ARCH_7) && __clang__ && CC_USE_ASM
265 #define CCN_DEDICATED_SQR 1
266 #define CCN_MUL_KARATSUBA 0 // no performance improvement
267 #define CCN_ADD_ASM 1
268 #define CCN_SUB_ASM 1
269 #define CCN_MUL_ASM 0
270 #define CCN_ADDMUL1_ASM 1
271 #define CCN_MUL1_ASM 1
272 #define CCN_CMP_ASM 1
273 #define CCN_ADD1_ASM 0
274 #define CCN_SUB1_ASM 0
275 #define CCN_N_ASM 1
276 #define CCN_SET_ASM 1
277 #define CCN_SHIFT_RIGHT_ASM 1
278 #define CCAES_ARM_ASM 1
279 #define CCAES_INTEL_ASM 0
280 #if CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_USE_SEPROM || CC_USE_S3
281 #define CCAES_MUX 0
282 #else
283 #define CCAES_MUX 1
284 #endif
285 #define CCN_USE_BUILTIN_CLZ 1
286 #define CCSHA1_VNG_INTEL 0
287 #define CCSHA2_VNG_INTEL 0
288
289 #if defined(__ARM_NEON__) || CC_KERNEL
290 #define CCSHA1_VNG_ARMV7NEON 1
291 #define CCSHA2_VNG_ARMV7NEON 1
292 #else /* !defined(__ARM_NEON__) */
293 #define CCSHA1_VNG_ARMV7NEON 0
294 #define CCSHA2_VNG_ARMV7NEON 0
295 #endif /* !defined(__ARM_NEON__) */
296 #define CCSHA256_ARMV6M_ASM 0
297
298 //-(2) ARM 64
299 #elif (defined(__x86_64__) || defined(__i386__)) && __clang__ && CC_USE_ASM
300 #define CCN_DEDICATED_SQR 1
301 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
302 /* These assembly routines only work for a single CCN_UNIT_SIZE. */
303 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8) || (defined(__i386__) && CCN_UNIT_SIZE == 4)
304 #define CCN_ADD_ASM 1
305 #define CCN_SUB_ASM 1
306 #define CCN_MUL_ASM 1
307 #else
308 #define CCN_ADD_ASM 0
309 #define CCN_SUB_ASM 0
310 #define CCN_MUL_ASM 0
311 #endif
312
313 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8)
314 #define CCN_CMP_ASM 1
315 #define CCN_N_ASM 1
316 #define CCN_SHIFT_RIGHT_ASM 1
317 #else
318 #define CCN_CMP_ASM 0
319 #define CCN_N_ASM 0
320 #define CCN_SHIFT_RIGHT_ASM 0
321 #endif
322
323 #define CCN_ADDMUL1_ASM 0
324 #define CCN_MUL1_ASM 0
325 #define CCN_ADD1_ASM 0
326 #define CCN_SUB1_ASM 0
327 #define CCN_SET_ASM 0
328 #define CCAES_ARM_ASM 0
329 #define CCAES_INTEL_ASM 1
330 #define CCAES_MUX 0
331 #define CCN_USE_BUILTIN_CLZ 0
332 #define CCSHA1_VNG_INTEL 1
333 #define CCSHA2_VNG_INTEL 1
334 #define CCSHA1_VNG_ARMV7NEON 0
335 #define CCSHA2_VNG_ARMV7NEON 0
336 #define CCSHA256_ARMV6M_ASM 0
337
338 //-(4) disable assembly
339 #else
340 #if CCN_UINT128_SUPPORT_FOR_64BIT_ARCH
341 #define CCN_DEDICATED_SQR 1
342 #else
343 #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
344 #endif
345 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
346 #define CCN_ADD_ASM 0
347 #define CCN_SUB_ASM 0
348 #define CCN_MUL_ASM 0
349 #define CCN_ADDMUL1_ASM 0
350 #define CCN_MUL1_ASM 0
351 #define CCN_CMP_ASM 0
352 #define CCN_ADD1_ASM 0
353 #define CCN_SUB1_ASM 0
354 #define CCN_N_ASM 0
355 #define CCN_SET_ASM 0
356 #define CCN_SHIFT_RIGHT_ASM 0
357 #define CCAES_ARM_ASM 0
358 #define CCAES_INTEL_ASM 0
359 #define CCAES_MUX 0
360 #define CCN_USE_BUILTIN_CLZ 0
361 #define CCSHA1_VNG_INTEL 0
362 #define CCSHA2_VNG_INTEL 0
363 #define CCSHA1_VNG_ARMV7NEON 0
364 #define CCSHA2_VNG_ARMV7NEON 0
365 #define CCSHA256_ARMV6M_ASM 0
366
367 #endif
368
369 #define CC_INLINE static inline
370
371 #if CORECRYPTO_USE_TRANSPARENT_UNION
372 // Non null for transparent unions is ambiguous and cause problems
373 // for most tools (GCC and others: 23919290).
374 #define CC_NONNULL_TU(N)
375 #else
376 #define CC_NONNULL_TU(N) CC_NONNULL(N)
377 #endif
378
379 #ifdef __GNUC__
380 #define CC_NORETURN __attribute__((__noreturn__))
381 #define CC_NOTHROW __attribute__((__nothrow__))
382 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
383 #define CC_NONNULL1 __attribute__((__nonnull__(1)))
384 #define CC_NONNULL2 __attribute__((__nonnull__(2)))
385 #define CC_NONNULL3 __attribute__((__nonnull__(3)))
386 #define CC_NONNULL4 __attribute__((__nonnull__(4)))
387 #define CC_NONNULL5 __attribute__((__nonnull__(5)))
388 #define CC_NONNULL6 __attribute__((__nonnull__(6)))
389 #define CC_NONNULL7 __attribute__((__nonnull__(7)))
390 #define CC_NONNULL_ALL __attribute__((__nonnull__))
391 #define CC_SENTINEL __attribute__((__sentinel__))
392 #define CC_CONST __attribute__((__const__))
393 #define CC_PURE __attribute__((__pure__))
394 #define CC_WARN_RESULT __attribute__((__warn_unused_result__))
395 #define CC_MALLOC __attribute__((__malloc__))
396 #define CC_UNUSED __attribute__((unused))
397 #else /* !__GNUC__ */
398 /*! @parseOnly */
399 #define CC_UNUSED
400 /*! @parseOnly */
401 #define CC_NONNULL(N)
402 /*! @parseOnly */
403 #define CC_NORETURN
404 /*! @parseOnly */
405 #define CC_NOTHROW
406 /*! @parseOnly */
407 #define CC_NONNULL1
408 /*! @parseOnly */
409 #define CC_NONNULL2
410 /*! @parseOnly */
411 #define CC_NONNULL3
412 /*! @parseOnly */
413 #define CC_NONNULL4
414 /*! @parseOnly */
415 #define CC_NONNULL5
416 /*! @parseOnly */
417 #define CC_NONNULL6
418 /*! @parseOnly */
419 #define CC_NONNULL7
420 /*! @parseOnly */
421 #define CC_NONNULL_ALL
422 /*! @parseOnly */
423 #define CC_SENTINEL
424 /*! @parseOnly */
425 #define CC_CONST
426 /*! @parseOnly */
427 #define CC_PURE
428 /*! @parseOnly */
429 #define CC_WARN_RESULT
430 /*! @parseOnly */
431 #define CC_MALLOC
432 #endif /* !__GNUC__ */
433
434
435 #endif /* _CORECRYPTO_CC_CONFIG_H_ */