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_L4 || CC_USE_S3
262/* No dynamic linking allowed in L4, e.g. avoid nonlazy symbols */
263/* For corecrypto kext, CC_STATIC should be undefined */
264 #define CC_STATIC 1
265#endif
266
267#if !defined(CC_USE_HEAP_FOR_WORKSPACE)
268 #if CC_USE_S3 || CC_USE_SEPROM || CC_RTKITROM
269 #define CC_USE_HEAP_FOR_WORKSPACE 0
270 #else
271 #define CC_USE_HEAP_FOR_WORKSPACE 1
272 #endif
273#endif
274
275/* memset_s is only available in few target */
276#if CC_USE_SEPROM || defined(__CC_ARM) \
277 || defined(__hexagon__) || CC_EFI
278 #define CC_HAS_MEMSET_S 0
279#else
280 #define CC_HAS_MEMSET_S 1
281#endif
282
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
290// Disable RSA Keygen on iBridge
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
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
304//- functions implemented in assembly ------------------------------------------
305//this the list of corecrypto clients that use assembly and the clang compiler
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
307 #warning "You are using the default corecrypto configuration, assembly optimizations may not be available for your platform"
308#endif
309
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.
312#if !defined(CC_USE_ASM)
313 #if defined(_WIN32) || CC_EFI || CC_BASEBAND || CC_XNU_KERNEL_PRIVATE || (defined(__GNUC__) && !defined(__clang__)) || defined(__ANDROID_API__) || CC_RTKIT || CC_RTKITROM
314 #define CC_USE_ASM 0
315 #else
316 #define CC_USE_ASM 1
317 #endif
318#endif
319
320//-(1) ARM V7
321#if defined(_ARM_ARCH_7) && __clang__ && CC_USE_ASM
322 #define CCN_DEDICATED_SQR CC_SMALL_CODE
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
337 #if CC_KERNEL || CC_USE_L4 || CC_IBOOT || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || CC_USE_S3
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
356#elif defined(__arm64__) && __clang__ && CC_USE_ASM
357 #define CCN_DEDICATED_SQR CC_SMALL_CODE
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
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
420//-(4) disable assembly
421#else
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
450
451#define CC_INLINE static inline
452
453#ifdef __GNUC__
454 #define CC_NORETURN __attribute__((__noreturn__))
455 #define CC_NOTHROW __attribute__((__nothrow__))
456 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
457 #define CC_NONNULL4 CC_NONNULL((4))
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))
465#else /* !__GNUC__ */
466/*! @parseOnly */
467 #define CC_UNUSED
468/*! @parseOnly */
469 #define CC_NONNULL(N)
470/*! @parseOnly */
471 #define CC_NONNULL4
472/*! @parseOnly */
473 #define CC_NORETURN
474/*! @parseOnly */
475 #define CC_NOTHROW
476/*! @parseOnly */
477 #define CC_NONNULL_ALL
478/*! @parseOnly */
479 #define CC_SENTINEL
480/*! @parseOnly */
481 #define CC_CONST
482/*! @parseOnly */
483 #define CC_PURE
484/*! @parseOnly */
485 #define CC_WARN_RESULT
486/*! @parseOnly */
487 #define CC_MALLOC
488#endif /* !__GNUC__ */
489
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
496
497#endif /* _CORECRYPTO_CC_CONFIG_H_ */
498