]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc_config.h
2f78c45a6da82aa0aa933490cb1fe40547982082
[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 || 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 //- functions implemented in assembly ------------------------------------------
241 //this the list of corecrypto clients that use assembly and the clang compiler
242 #if !(CC_XNU_KERNEL_AVAILABLE || CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_USE_SEPROM || CC_USE_S3) && !defined(_WIN32) && CORECRYPTO_DEBUG
243 #warning "You are using the default corecrypto configuration, assembly optimizations may not be available for your platform"
244 #endif
245
246 // use this macro to strictly disable assembly regardless of cpu/os/compiler/etc
247 #if !defined(CC_USE_ASM)
248 #if defined(_MSC_VER) || CC_LINUX || CC_EFI || CC_BASEBAND
249 #define CC_USE_ASM 0
250 #else
251 #define CC_USE_ASM 1
252 #endif
253 #endif
254
255 //-(1) ARM V7
256 #if defined(_ARM_ARCH_7) && __clang__ && CC_USE_ASM
257 #define CCN_DEDICATED_SQR 1
258 #define CCN_MUL_KARATSUBA 0 // no performance improvement
259 #define CCN_ADD_ASM 1
260 #define CCN_SUB_ASM 1
261 #define CCN_MUL_ASM 0
262 #define CCN_ADDMUL1_ASM 1
263 #define CCN_MUL1_ASM 1
264 #define CCN_CMP_ASM 1
265 #define CCN_ADD1_ASM 0
266 #define CCN_SUB1_ASM 0
267 #define CCN_N_ASM 1
268 #define CCN_SET_ASM 1
269 #define CCN_SHIFT_RIGHT_ASM 1
270 #define CCAES_ARM_ASM 1
271 #define CCAES_INTEL_ASM 0
272 #if CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_USE_SEPROM || CC_USE_S3
273 #define CCAES_MUX 0
274 #else
275 #define CCAES_MUX 1
276 #endif
277 #define CCN_USE_BUILTIN_CLZ 1
278 #define CCSHA1_VNG_INTEL 0
279 #define CCSHA2_VNG_INTEL 0
280
281 #if defined(__ARM_NEON__) || CC_KERNEL
282 #define CCSHA1_VNG_ARMV7NEON 1
283 #define CCSHA2_VNG_ARMV7NEON 1
284 #else /* !defined(__ARM_NEON__) */
285 #define CCSHA1_VNG_ARMV7NEON 0
286 #define CCSHA2_VNG_ARMV7NEON 0
287 #endif /* !defined(__ARM_NEON__) */
288 #define CCSHA256_ARMV6M_ASM 0
289
290 //-(2) ARM 64
291 #elif (defined(__x86_64__) || defined(__i386__)) && __clang__ && CC_USE_ASM
292 #define CCN_DEDICATED_SQR 1
293 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
294 /* These assembly routines only work for a single CCN_UNIT_SIZE. */
295 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8) || (defined(__i386__) && CCN_UNIT_SIZE == 4)
296 #define CCN_ADD_ASM 1
297 #define CCN_SUB_ASM 1
298 #define CCN_MUL_ASM 1
299 #else
300 #define CCN_ADD_ASM 0
301 #define CCN_SUB_ASM 0
302 #define CCN_MUL_ASM 0
303 #endif
304
305 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8)
306 #define CCN_CMP_ASM 1
307 #define CCN_N_ASM 1
308 #define CCN_SHIFT_RIGHT_ASM 1
309 #else
310 #define CCN_CMP_ASM 0
311 #define CCN_N_ASM 0
312 #define CCN_SHIFT_RIGHT_ASM 0
313 #endif
314
315 #define CCN_ADDMUL1_ASM 0
316 #define CCN_MUL1_ASM 0
317 #define CCN_ADD1_ASM 0
318 #define CCN_SUB1_ASM 0
319 #define CCN_SET_ASM 0
320 #define CCAES_ARM_ASM 0
321 #define CCAES_INTEL_ASM 1
322 #define CCAES_MUX 0
323 #define CCN_USE_BUILTIN_CLZ 0
324 #define CCSHA1_VNG_INTEL 1
325 #define CCSHA2_VNG_INTEL 1
326 #define CCSHA1_VNG_ARMV7NEON 0
327 #define CCSHA2_VNG_ARMV7NEON 0
328 #define CCSHA256_ARMV6M_ASM 0
329
330 //-(4) disable assembly
331 #else
332 #if CCN_UINT128_SUPPORT_FOR_64BIT_ARCH
333 #define CCN_DEDICATED_SQR 1
334 #else
335 #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
336 #endif
337 #define CCN_MUL_KARATSUBA 1 // 4*n CCN_UNIT extra memory required.
338 #define CCN_ADD_ASM 0
339 #define CCN_SUB_ASM 0
340 #define CCN_MUL_ASM 0
341 #define CCN_ADDMUL1_ASM 0
342 #define CCN_MUL1_ASM 0
343 #define CCN_CMP_ASM 0
344 #define CCN_ADD1_ASM 0
345 #define CCN_SUB1_ASM 0
346 #define CCN_N_ASM 0
347 #define CCN_SET_ASM 0
348 #define CCN_SHIFT_RIGHT_ASM 0
349 #define CCAES_ARM_ASM 0
350 #define CCAES_INTEL_ASM 0
351 #define CCAES_MUX 0
352 #define CCN_USE_BUILTIN_CLZ 0
353 #define CCSHA1_VNG_INTEL 0
354 #define CCSHA2_VNG_INTEL 0
355 #define CCSHA1_VNG_ARMV7NEON 0
356 #define CCSHA2_VNG_ARMV7NEON 0
357 #define CCSHA256_ARMV6M_ASM 0
358
359 #endif
360
361 #define CC_INLINE static inline
362
363 #if CORECRYPTO_USE_TRANSPARENT_UNION
364 // Non null for transparent unions is ambiguous and cause problems
365 // for most tools (GCC and others: 23919290).
366 #define CC_NONNULL_TU(N)
367 #else
368 #define CC_NONNULL_TU(N) CC_NONNULL(N)
369 #endif
370
371 #ifdef __GNUC__
372 #define CC_NORETURN __attribute__((__noreturn__))
373 #define CC_NOTHROW __attribute__((__nothrow__))
374 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
375 #define CC_NONNULL1 __attribute__((__nonnull__(1)))
376 #define CC_NONNULL2 __attribute__((__nonnull__(2)))
377 #define CC_NONNULL3 __attribute__((__nonnull__(3)))
378 #define CC_NONNULL4 __attribute__((__nonnull__(4)))
379 #define CC_NONNULL5 __attribute__((__nonnull__(5)))
380 #define CC_NONNULL6 __attribute__((__nonnull__(6)))
381 #define CC_NONNULL7 __attribute__((__nonnull__(7)))
382 #define CC_NONNULL_ALL __attribute__((__nonnull__))
383 #define CC_SENTINEL __attribute__((__sentinel__))
384 #define CC_CONST __attribute__((__const__))
385 #define CC_PURE __attribute__((__pure__))
386 #define CC_WARN_RESULT __attribute__((__warn_unused_result__))
387 #define CC_MALLOC __attribute__((__malloc__))
388 #define CC_UNUSED __attribute__((unused))
389 #else /* !__GNUC__ */
390 /*! @parseOnly */
391 #define CC_UNUSED
392 /*! @parseOnly */
393 #define CC_NONNULL(N)
394 /*! @parseOnly */
395 #define CC_NORETURN
396 /*! @parseOnly */
397 #define CC_NOTHROW
398 /*! @parseOnly */
399 #define CC_NONNULL1
400 /*! @parseOnly */
401 #define CC_NONNULL2
402 /*! @parseOnly */
403 #define CC_NONNULL3
404 /*! @parseOnly */
405 #define CC_NONNULL4
406 /*! @parseOnly */
407 #define CC_NONNULL5
408 /*! @parseOnly */
409 #define CC_NONNULL6
410 /*! @parseOnly */
411 #define CC_NONNULL7
412 /*! @parseOnly */
413 #define CC_NONNULL_ALL
414 /*! @parseOnly */
415 #define CC_SENTINEL
416 /*! @parseOnly */
417 #define CC_CONST
418 /*! @parseOnly */
419 #define CC_PURE
420 /*! @parseOnly */
421 #define CC_WARN_RESULT
422 /*! @parseOnly */
423 #define CC_MALLOC
424 #endif /* !__GNUC__ */
425
426
427 #endif /* _CORECRYPTO_CC_CONFIG_H_ */