Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/* Copyright (c) (2010-2023) Apple Inc. All rights reserved.
 *
 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
 * is contained in the License.txt file distributed with corecrypto) and only to
 * people who accept that license. IMPORTANT:  Any license rights granted to you by
 * Apple Inc. (if any) are limited to internal use within your organization only on
 * devices and computers you own or control, for the sole purpose of verifying the
 * security characteristics and correct functioning of the Apple Software.  You may
 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
 */

#ifndef _CORECRYPTO_CC_CONFIG_H_
#define _CORECRYPTO_CC_CONFIG_H_

/* A word about configuration macros:

    Conditional configuration macros specific to corecrypto should be named CORECRYPTO_xxx
    or CCxx_yyy and be defined to be either 0 or 1 in this file. You can add an
    #ifndef #error construct at the end of this file to make sure it's always defined.

    They should always be tested using the #if directive, never the #ifdef directive.

    No other conditional macros shall ever be used (except in this file)

    Configuration Macros that are defined outside of corecrypto (eg: KERNEL, DEBUG, ...)
    shall only be used in this file to define CCxxx macros.

    External macros should be assumed to be either undefined, defined with no value,
    or defined as true or false. We shall strive to build with -Wundef whenever possible,
    so the following construct should be used to test external macros in this file:

         #if defined(DEBUG) && (DEBUG)
         #define CORECRYPTO_DEBUG 1
         #else
         #define CORECRYPTO_DEBUG 0
         #endif


    It is acceptable to define a conditional CC_xxxx macro in an implementation file,
    to be used only in this file.

    The current code is not guaranteed to follow those rules, but should be fixed to.

    Corecrypto requires GNU and C99 compatibility.
    Typically enabled by passing --gnu --c99 to the compiler (eg. armcc)

*/

#if !defined(__has_feature)
    #define __has_feature(FEATURE) 0
#endif

#if !defined(__has_extension)
    #define __has_extension(EXT) 0
#endif

#if !defined(__has_attribute)
    #define __has_attribute(ATTR) 0
#endif

#ifndef __cc_printflike
 #define __cc_printflike(fmtarg, firstvararg) __attribute__((format(printf, fmtarg, firstvararg)))
#endif

//Do not set this macros to 1, unless you are developing/testing for Linux under macOS
#define CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT    0

//Do not set these macros to 1, unless you are developing/testing for Windows under macOS
#define CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT 0
#define CORECRYPTO_HACK_FOR_WINDOWS_DEVELOPMENT 0

#if (defined(DEBUG) && (DEBUG)) || defined(_DEBUG) //MSVC defines _DEBUG
/* CC_DEBUG is already used in CommonCrypto */
 #define CORECRYPTO_DEBUG 1
#else
 #define CORECRYPTO_DEBUG 0
#endif

// Enable specific configurations only relevant in test builds
#if defined(CORECRYPTO_BUILT_FOR_TESTING) && CORECRYPTO_BUILT_FOR_TESTING
  #define CC_BUILT_FOR_TESTING 1
#else
  #define CC_BUILT_FOR_TESTING 0
#endif

// This macro can be used to enable prints when a condition in the macro "cc_require"
// is false. This is especially useful to confirm that negative testing fails
// at the intended location
#define CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS 0

#ifndef CC_TXM
 #if defined(TXM) && (TXM)
  #define CC_TXM 1
 #else
  #define CC_TXM 0
 #endif
#endif

#ifndef CC_SPTM
 #if defined(SPTM) && (SPTM)
  #define CC_SPTM 1
 #else
  #define CC_SPTM 0
 #endif
#endif

#if defined(KERNEL) && (KERNEL)
 #define CC_KERNEL 1 // KEXT, XNU repo or kernel components such as AppleKeyStore
#else
 #define CC_KERNEL 0
#endif

#if defined(LINUX_SGX) && (LINUX_SGX)
 #define CC_SGX 1
#else
 #define CC_SGX 0
#endif

#if (defined(__linux__) && !(CC_SGX)) || CORECRYPTO_SIMULATE_POSIX_ENVIRONMENT
 #define CC_LINUX 1
#else
 #define CC_LINUX 0
#endif

#if defined(USE_L4) && (USE_L4)
 #define CC_USE_L4 1
#else
 #define CC_USE_L4 0
#endif

#if defined(RTKIT) && (RTKIT)
 #define CC_RTKIT 1
#else
 #define CC_RTKIT 0
#endif

#if defined(RTKITROM) && (RTKITROM)
#define CC_RTKITROM 1
#else
#define CC_RTKITROM 0
#endif

#if defined(USE_SEPROM) && (USE_SEPROM)
 #define CC_USE_SEPROM 1
#else
 #define CC_USE_SEPROM 0
#endif

#if (defined(ICE_FEATURES_ENABLED)) || (defined(MAVERICK) && (MAVERICK))
 #define CC_BASEBAND 1
#else
 #define CC_BASEBAND 0
#endif

#if defined(EFI) && (EFI)
 #define CC_EFI 1
#else
 #define CC_EFI 0
#endif

#if defined(IBOOT) && (IBOOT)
 #define CC_IBOOT 1
#else
 #define CC_IBOOT 0
#endif

// Include target conditionals if available.
#if defined(__has_include)     /* portability */
#if __has_include(<TargetConditionals.h>)
#include <TargetConditionals.h>
#endif /* __has_include(<TargetConditionals.h>) */
#endif /* defined(__has_include) */

#if defined(TARGET_OS_DRIVERKIT)
 #define CC_DRIVERKIT TARGET_OS_DRIVERKIT
#else
 #define CC_DRIVERKIT 0
#endif

#if defined(TARGET_OS_BRIDGE)
 #define CC_BRIDGE TARGET_OS_BRIDGE
#else
 #define CC_BRIDGE 0
#endif

// TARGET_OS_EXCLAVECORE && TARGET_OS_EXCLAVEKIT
#if defined(TARGET_OS_EXCLAVECORE) && TARGET_OS_EXCLAVECORE
 #define CC_EXCLAVE 1
 #define CC_EXCLAVECORE 1
 #define CC_EXCLAVEKIT 0
#elif defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT
 #define CC_EXCLAVE 1
 #define CC_EXCLAVECORE 0
 #define CC_EXCLAVEKIT 1
#else
 #define CC_EXCLAVE 0
 #define CC_EXCLAVECORE 0
 #define CC_EXCLAVEKIT 0
#endif

// ARMv6-M
#if defined(__arm__) && (defined(__ARM_ARCH_6M__) || defined(__TARGET_ARCH_6S_M) || defined (__armv6m__))
 #define CC_ARM_ARCH_6M 1
#else
 #define CC_ARM_ARCH_6M 0
#endif

// ARMv7
#if defined(__arm__) && (defined (__ARM_ARCH_7A__) || defined (__ARM_ARCH_7S__) || defined (__ARM_ARCH_7F__) || defined (__ARM_ARCH_7K__) || defined(__ARM_ARCH_7EM__))
    #define CC_ARM_ARCH_7 1
#else
    #define CC_ARM_ARCH_7 0
#endif

// DSP is only available on aarch32
#if CC_ARM_ARCH_7 && defined(__ARM_FEATURE_DSP) && __ARM_FEATURE_DSP
    #define CC_ARM_ARCH_7_DSP 1
#else
    #define CC_ARM_ARCH_7_DSP 0
#endif

#if defined(__ARM_NEON) && __ARM_NEON
    #define CC_ARM_NEON 1
#else
    #define CC_ARM_NEON 0
#endif

#if defined(__ARM_FEATURE_AES) && __ARM_FEATURE_AES
    #define CC_ARM_FEATURE_AES 1
#else
    #define CC_ARM_FEATURE_AES 0
#endif

#if defined(__ARM_FEATURE_SHA2) && __ARM_FEATURE_SHA2
    #define CC_ARM_FEATURE_SHA2 1
#else
    #define CC_ARM_FEATURE_SHA2 0
#endif

#if defined(__ARM_FEATURE_SHA512) && __ARM_FEATURE_SHA512
    #define CC_ARM_FEATURE_SHA512 1
#else
    #define CC_ARM_FEATURE_SHA512 0
#endif

#if defined(__ARM_FEATURE_SHA3) && __ARM_FEATURE_SHA3
    #define CC_ARM_FEATURE_SHA3 1
#else
    #define CC_ARM_FEATURE_SHA3 0
#endif

// Check for open source builds

// Defined by the XNU build scripts
// Applies to code embedded in XNU but NOT to the kext
#if defined(XNU_KERNEL_PRIVATE)
 #define CC_XNU_KERNEL_PRIVATE 1
#else
 #define CC_XNU_KERNEL_PRIVATE 0
#endif

// Handle unaligned data, if the CPU cannot. (For Gladman AES.)
#define CC_HANDLE_UNALIGNED_DATA CC_BASEBAND

// BaseBand configuration
#if CC_BASEBAND

// -- ENDIANESS
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
 #if defined(ENDIAN_LITTLE) || (defined(__arm__) && !defined(__BIG_ENDIAN))
  #define __LITTLE_ENDIAN__
 #elif !defined(ENDIAN_BIG) && !defined(__BIG_ENDIAN)
  #error Baseband endianess not defined.
 #endif
 #define AESOPT_ENDIAN_NO_FILE
#endif

#if !defined(__x86_64__) && !defined(__arm64__)
// -- Architecture
 #define CCN_UNIT_SIZE  4 // 32 bits
#endif

// -- Warnings
// Ignore irrelevant warnings after verification
// #186-D: pointless comparison of unsigned integer with zero
// #546-D: transfer of control bypasses initialization of
 #ifdef __arm__
  #pragma diag_suppress 186, 546
 #endif // __arm__
#define CC_SMALL_CODE 1

#endif // CC_BASEBAND

#if CC_RTKIT || CC_RTKITROM
#define CC_SMALL_CODE 1
#endif


#ifndef CC_SMALL_CODE
#define CC_SMALL_CODE 0
#endif

#ifndef CC_DARWIN
 //CC_DARWIN indicates the availability of XNU kernel functions,
 //like what we have on OSX, iOS, tvOS, Watch OS
 #if (CC_USE_L4 || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || CC_EFI || CC_LINUX || defined(_WIN32) || CC_BASEBAND || CC_SGX || CC_ARM_ARCH_6M)
  #define CC_DARWIN 0
 #elif CC_TXM
  #define CC_DARWIN 0
 #elif CC_SPTM
  #define CC_DARWIN 0
 #else
  #define CC_DARWIN 1
 #endif
#endif

//arm arch64 definition for gcc
#if defined(__GNUC__) && defined(__aarch64__) && !defined(__arm64__)
    #define __arm64__
#endif

#if !defined(CCN_UNIT_SIZE)
 #if defined(__arm64__) || defined(__x86_64__)  || defined(_WIN64)
  #define CCN_UNIT_SIZE  8
 #elif defined(__arm__) || defined(__i386__) || defined(_WIN32)
  #define CCN_UNIT_SIZE  4
 #else
  #error undefined architecture
 #endif
#endif /* !defined(CCN_UNIT_SIZE) */


//this allows corecrypto Windows development using xcode
#if defined(CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT)
 #if CORECRYPTO_SIMULATE_WINDOWS_ENVIRONMENT && CC_DARWIN && CORECRYPTO_DEBUG
  #define CC_USE_ASM 0
  #define CC_USE_HEAP_FOR_WORKSPACE 1
   #if (CCN_UNIT_SIZE == 8)
    #define CC_DUNIT_SUPPORTED 0
   #else
    #define CC_DUNIT_SUPPORTED 1
   #endif
 #endif
#endif

#if !defined(CC_DUNIT_SUPPORTED)
 #if defined(_WIN64) && defined(_WIN32) && (CCN_UNIT_SIZE == 8)
  #define CC_DUNIT_SUPPORTED 0
 #else
  #define CC_DUNIT_SUPPORTED 1
 #endif
#endif

#if defined(_MSC_VER)
    #if defined(__clang__)
        #define CC_ALIGNED(x) __attribute__ ((aligned(x))) //clang compiler
    #else
        #define CC_ALIGNED(x) __declspec(align(x)) //MS complier
    #endif
#else
    #if defined(__clang__) || CCN_UNIT_SIZE==8
        #define CC_ALIGNED(x) __attribute__ ((aligned(x)))
    #else
        #define CC_ALIGNED(x) __attribute__ ((aligned((x)>8?8:(x))))
    #endif
#endif

#if !defined(CC_USE_HEAP_FOR_WORKSPACE)
 #if CC_USE_SEPROM || CC_RTKITROM || CC_EFI
  #define CC_USE_HEAP_FOR_WORKSPACE 0
 #else
  #define CC_USE_HEAP_FOR_WORKSPACE 1
 #endif
#endif

// Secure memory zeroization functions
#if !defined(__APPLE__) || CC_RTKIT || CC_RTKITROM || CC_USE_SEPROM || defined(__CC_ARM) || defined(__hexagon__) || CC_EFI
 #define CC_HAS_MEMSET_S 0
#else
 #define CC_HAS_MEMSET_S 1
#endif

#if defined(_WIN32) && !defined(__clang__)
 // Clang with Microsoft CodeGen doesn't support SecureZeroMemory.
 #define CC_HAS_SECUREZEROMEMORY 1
#else
 #define CC_HAS_SECUREZEROMEMORY 0
#endif

#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
 #define CC_HAS_EXPLICIT_BZERO 1
#else
 #define CC_HAS_EXPLICIT_BZERO 0
#endif

// Disable RSA keygen on iBridge.
#if !defined(CC_DISABLE_RSAKEYGEN)
 #if CC_BRIDGE && CC_KERNEL
  #define CC_DISABLE_RSAKEYGEN 1
 #else
  #define CC_DISABLE_RSAKEYGEN 0
 #endif
#endif

// Enable assembler in Linux if CC_LINUX_ASM is defined
#if (CC_LINUX || CC_SGX) && defined(CC_LINUX_ASM) && CC_LINUX_ASM
#define CC_USE_ASM 1
#endif

// Use this macro to strictly disable assembly regardless of cpu/os/compiler/etc.
// Our assembly code is not gcc compatible. Clang defines the __GNUC__ macro as well.
#if !defined(CC_USE_ASM)
 #if defined(_WIN32) || CC_EFI || CC_BASEBAND || CC_XNU_KERNEL_PRIVATE || (defined(__GNUC__) && !defined(__clang__)) || CC_LINUX
  #define CC_USE_ASM 0
 #else
  #define CC_USE_ASM 1
 #endif
#endif

#ifndef CC_LOG
#define CC_LOG (CC_DARWIN && !CC_KERNEL && !CC_IBOOT && !CC_DRIVERKIT && !CC_EXCLAVE)
#endif

#ifndef CC_EXTERN_MALLOC
 #if CC_TXM
  #define CC_EXTERN_MALLOC 1
 #else
  #define CC_EXTERN_MALLOC 0
 #endif
#endif

#ifndef CC_MALLOC_ABORT
 #if CC_SPTM
  #define CC_MALLOC_ABORT 1
 #else
  #define CC_MALLOC_ABORT 0
 #endif
#endif

#define CC_CACHE_DESCRIPTORS CC_KERNEL

//-(1) ARM V7
#if CC_ARM_ARCH_7 && defined(__clang__) && CC_USE_ASM
 #define CCN_ADD_ASM            1
 #define CCN_SUB_ASM            1
 #define CCN_MUL_ASM            0
 #define CCN_ADDMUL1_ASM        1
 #define CCN_MUL1_ASM           1
 #define CCN_CMP_ASM            1
 #define CCN_ADD1_ASM           1
 #define CCN_SUB1_ASM           1
 #define CCN_N_ASM              1
 #define CCN_SET_ASM            1
 #define CCN_SHIFT_RIGHT_ASM    1
 #if defined(__ARM_NEON__) 
 #define CCN_SHIFT_LEFT_ASM     1
 #else
 #define CCN_SHIFT_LEFT_ASM     0
 #endif
 #define CCN_MULMOD_224_ASM     CC_ARM_ARCH_7_DSP
 #define CCN_MULMOD_256_ASM     CC_ARM_ARCH_7_DSP
 #define CCN_MULMOD_384_ASM     CC_ARM_ARCH_7_DSP
 #define CCN_MULMOD_25519_ASM   0
 #define CCN_MULMOD_448_ASM     0

 // Clang cross-compiling for ARMv7/Linux can't handle the indirect symbol
 // directives we use to reference AES tables. Skip AES assembly for now.
 #if CC_LINUX && CC_BUILT_FOR_TESTING
  #define CCAES_ARM_ASM         0
 #else
  #define CCAES_ARM_ASM         1
 #endif

 #define CCAES_INTEL_ASM        0
 #define CCSHA1_VNG_INTEL       0
 #define CCSHA2_VNG_INTEL       0
 #define CCSHA3_VNG_INTEL       0
 #define CCSHA3_VNG_ARM         0
 #define CCSHA256_ARMV6M_ASM    0

 #if defined(__ARM_NEON__) || CC_KERNEL
  #define CCSHA1_VNG_ARM        1
  #define CCSHA2_VNG_ARM        1
 #else /* !defined(__ARM_NEON__) */
  #define CCSHA1_VNG_ARM        0
  #define CCSHA2_VNG_ARM        0
 #endif /* !defined(__ARM_NEON__) */

//-(2) ARM 64
#elif defined(__arm64__) && defined(__clang__) && CC_USE_ASM
 #define CCN_ADD_ASM            1
 #define CCN_SUB_ASM            1
 #define CCN_MUL_ASM            1
 #define CCN_ADDMUL1_ASM        1
 #define CCN_MUL1_ASM           1
 #define CCN_CMP_ASM            1
 #define CCN_ADD1_ASM           1
 #define CCN_SUB1_ASM           0
 #define CCN_N_ASM              1
 #define CCN_SET_ASM            0
 #define CCN_SHIFT_RIGHT_ASM    CC_ARM_NEON
 #define CCN_SHIFT_LEFT_ASM     CC_ARM_NEON
 #define CCN_MULMOD_224_ASM     1
 #define CCN_MULMOD_256_ASM     1
 #define CCN_MULMOD_384_ASM     1
 #define CCN_MULMOD_25519_ASM   1
 #define CCN_MULMOD_448_ASM     1
 #define CCAES_ARM_ASM          (CC_ARM_NEON && CC_ARM_FEATURE_AES)
 #define CCAES_INTEL_ASM        0
 #define CCSHA1_VNG_INTEL       0
 #define CCSHA2_VNG_INTEL       0
 #define CCSHA3_VNG_INTEL       0
 #define CCSHA1_VNG_ARM         1
 #define CCSHA2_VNG_ARM         (CC_ARM_NEON && CC_ARM_FEATURE_SHA2)
 #define CCSHA256_ARMV6M_ASM    0
 #if CC_USE_L4 || CC_KERNEL
  #define CCSHA3_VNG_ARM        0
 #else
  #define CCSHA3_VNG_ARM        1
 #endif
//-(3) Intel 32/64
#elif (defined(__x86_64__) || defined(__i386__)) && defined(__clang__) && CC_USE_ASM
 /* These assembly routines only work for a single CCN_UNIT_SIZE. */
 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8) || (defined(__i386__) && CCN_UNIT_SIZE == 4)
  #define CCN_ADD_ASM            1
  #define CCN_SUB_ASM            1
  #define CCN_MUL_ASM            1
 #else
  #define CCN_ADD_ASM            0
  #define CCN_SUB_ASM            0
  #define CCN_MUL_ASM            0
 #endif

 #if (defined(__x86_64__) && CCN_UNIT_SIZE == 8)
  #define CCN_CMP_ASM            1
  #define CCN_N_ASM              1
  #define CCN_SHIFT_RIGHT_ASM    1
  #define CCN_SHIFT_LEFT_ASM     1
 #else
  #define CCN_CMP_ASM            0
  #define CCN_N_ASM              0
  #define CCN_SHIFT_RIGHT_ASM    0
  #define CCN_SHIFT_LEFT_ASM     0
 #endif

 #define CCN_MULMOD_384_ASM     0
 #define CCN_MULMOD_448_ASM     0
 #if defined(__x86_64__) && CCN_UNIT_SIZE == 8 && !CC_SGX
  #define CCN_MULMOD_224_ASM    1
  #define CCN_MULMOD_256_ASM    1
  #define CCN_MULMOD_25519_ASM  1
  #define CCN_ADDMUL1_ASM       1
  #define CCN_MUL1_ASM          1
 #else
  #define CCN_MULMOD_224_ASM    0
  #define CCN_MULMOD_256_ASM    0
  #define CCN_MULMOD_25519_ASM  0
  #define CCN_ADDMUL1_ASM       0
  #define CCN_MUL1_ASM          0
 #endif
 #define CCN_ADD1_ASM           0
 #define CCN_SUB1_ASM           0
 #define CCN_SET_ASM            0
 #define CCAES_ARM_ASM          0
 #define CCAES_INTEL_ASM        1
 #define CCSHA1_VNG_INTEL       1
 #define CCSHA2_VNG_INTEL       1
 #define CCSHA3_VNG_INTEL       1
 #define CCSHA1_VNG_ARM         0
 #define CCSHA2_VNG_ARM         0
 #define CCSHA3_VNG_ARM         0
 #define CCSHA256_ARMV6M_ASM    0
//-(4) ARMv6-M
#elif CC_ARM_ARCH_6M
 #define CCN_ADD_ASM            0
 #define CCN_SUB_ASM            0
 #define CCN_MUL_ASM            0
 #define CCN_ADDMUL1_ASM        0
 #define CCN_MUL1_ASM           0
 #define CCN_CMP_ASM            0
 #define CCN_ADD1_ASM           0
 #define CCN_SUB1_ASM           0
 #define CCN_N_ASM              0
 #define CCN_SET_ASM            0
 #define CCN_SHIFT_RIGHT_ASM    0
 #define CCN_SHIFT_LEFT_ASM     0
 #define CCN_MULMOD_224_ASM     0
 #define CCN_MULMOD_256_ASM     0
 #define CCN_MULMOD_384_ASM     0
 #define CCN_MULMOD_25519_ASM   0
 #define CCN_MULMOD_448_ASM     0
 #define CCAES_ARM_ASM          0
 #define CCAES_INTEL_ASM        0
 #define CCSHA1_VNG_INTEL       0
 #define CCSHA2_VNG_INTEL       0
 #define CCSHA3_VNG_INTEL       0
 #define CCSHA1_VNG_ARM         0
 #define CCSHA2_VNG_ARM         0
 #define CCSHA3_VNG_ARM         0
 #define CCSHA256_ARMV6M_ASM    1
//-(5) disable assembly
#else
 #define CCN_ADD_ASM            0
 #define CCN_SUB_ASM            0
 #define CCN_MUL_ASM            0
 #define CCN_ADDMUL1_ASM        0
 #define CCN_MUL1_ASM           0
 #define CCN_CMP_ASM            0
 #define CCN_ADD1_ASM           0
 #define CCN_SUB1_ASM           0
 #define CCN_N_ASM              0
 #define CCN_SET_ASM            0
 #define CCN_SHIFT_RIGHT_ASM    0
 #define CCN_SHIFT_LEFT_ASM     0
 #define CCN_MULMOD_224_ASM     0
 #define CCN_MULMOD_256_ASM     0
 #define CCN_MULMOD_384_ASM     0
 #define CCN_MULMOD_25519_ASM   0
 #define CCN_MULMOD_448_ASM     0
 #define CCAES_ARM_ASM          0
 #define CCAES_INTEL_ASM        0
 #define CCSHA1_VNG_INTEL       0
 #define CCSHA2_VNG_INTEL       0
 #define CCSHA3_VNG_INTEL       0
 #define CCSHA1_VNG_ARM         0
 #define CCSHA2_VNG_ARM         0
 #define CCSHA3_VNG_ARM         0
 #define CCSHA256_ARMV6M_ASM    0
#endif

#define CC_INLINE static inline
#define CC_NONNULL4 CC_NONNULL((4))

#ifdef __GNUC__
 #define CC_NORETURN __attribute__((__noreturn__))
 #define CC_NOTHROW __attribute__((__nothrow__))
 #define CC_NONNULL(N) __attribute__((__nonnull__ N))
 #define CC_NONNULL_ALL __attribute__((__nonnull__))
 #define CC_SENTINEL __attribute__((__sentinel__))
 // 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.
 // Specifically, do not apply CC_CONST if the function has any arguments that are pointers (directly, or indirectly)
 #define CC_CONST __attribute__((__const__))
 #define CC_PURE __attribute__((__pure__))
 #define CC_NODISCARD __attribute__((warn_unused_result))
 #define CC_WARN_RESULT __attribute__((__warn_unused_result__))
 #define CC_MALLOC_CLEAR __attribute__((__malloc__))
 #define CC_UNUSED __attribute__((unused))
 #define CC_WEAK __attribute__((weak))
#elif defined(__KEIL__)
 #define CC_NORETURN __attribute__((noreturn))
 #define CC_NOTHROW __attribute__((nothrow))
 #define CC_NONNULL(N) __attribute__((nonnull N))
 #define CC_NONNULL_ALL __attribute__((nonnull))
 #define CC_SENTINEL __attribute__((sentinel))
 #define CC_CONST __attribute__((const))
 #define CC_PURE __attribute__((pure))
 #define CC_NODISCARD __attribute__((warn_unused_result))
 #define CC_WARN_RESULT __attribute__((warn_unused_result))
 #define CC_MALLOC_CLEAR __attribute__((malloc))
 #define CC_UNUSED __attribute__((unused))
 #define CC_WEAK __attribute__((weak))
#else /* !__GNUC__ */
/*! @parseOnly */
 #define CC_UNUSED
/*! @parseOnly */
 #define CC_NONNULL(N)
/*! @parseOnly */
 #define CC_NORETURN
/*! @parseOnly */
 #define CC_NOTHROW
/*! @parseOnly */
 #define CC_NONNULL_ALL
/*! @parseOnly */
 #define CC_SENTINEL
/*! @parseOnly */
 #define CC_CONST
/*! @parseOnly */
 #define CC_PURE
/*! @parseOnly */
 #define CC_NODISCARD
/*! @parseOnly */
 #define CC_WARN_RESULT
/*! @parseOnly */
 #define CC_MALLOC_CLEAR
/*! @parseOnly */
 #define CC_WEAK
#endif /* !__GNUC__ */

// Use CC_WEAK_IF_SMALL_CODE to mark symbols as weak when compiling with
// CC_SMALL_CODE=1. This allows replacing faster but bigger code with smaller
// versions at link time.
#if CC_SMALL_CODE && !CC_BUILT_FOR_TESTING
 #define CC_WEAK_IF_SMALL_CODE CC_WEAK
#else
 #define CC_WEAK_IF_SMALL_CODE
#endif

// Bridge differences between MachO and ELF compiler/assemblers. */
#if CC_LINUX || CC_SGX
#define CC_ASM_SECTION_CONST .rodata
#define CC_ASM_PRIVATE_EXTERN .hidden
#define CC_ASM_SUBSECTIONS_VIA_SYMBOLS
#if CC_LINUX
// We need to be sure that assembler can access relocated C
// symbols. Sad but this is the quickest way to do that, at least with
// our current linux compiler (clang-3.4).
#define CC_C_LABEL(_sym) _sym@PLT
#else /* CC_SGX */
#define CC_C_LABEL(_sym) _sym
#endif
#define _IMM(x) $(x)
#else /* !CC_LINUX && !CC_SGX */
#define CC_ASM_SECTION_CONST .const
#define CC_ASM_PRIVATE_EXTERN .private_extern
#define CC_ASM_SUBSECTIONS_VIA_SYMBOLS .subsections_via_symbols
#define CC_C_LABEL(_sym) _##_sym
#define _IMM(x) $$(x)
#endif /* !CC_LINUX && !CC_SGX */

// Enable FIPSPOST function tracing only when supported. */
#ifdef CORECRYPTO_POST_TRACE
#define CC_FIPSPOST_TRACE 1
#else
#define CC_FIPSPOST_TRACE 0
#endif

#ifndef CC_INTERNAL_SDK
#if __has_include(<System/i386/cpu_capabilities.h>)
#define CC_INTERNAL_SDK 1
#elif __has_include(<System/arm/cpu_capabilities.h>)
#define CC_INTERNAL_SDK 1
#else
#define CC_INTERNAL_SDK 0
#endif
#endif

#if __has_feature(address_sanitizer)
 #define CC_ASAN 1
#else
 #define CC_ASAN 0
#endif

#ifdef CORECRYPTO_COVERAGE
#define CC_COVERAGE 1
#else
#define CC_COVERAGE 0
#endif

// Currently thread sanitizer is only supported in local builds.
// Please edit your "corecrypto_test" scheme to build with thread
// sanitizer and then remove *all* variants of corecrypto_static
// besides "normal"

#if __has_feature(thread_sanitizer)
    #define CC_TSAN 1
#else
    #define CC_TSAN 0
#endif // __has_feature(thread_sanitizer)

#if __has_feature(bounds_attributes)
    #define CC_PTRCHECK                  1
    #define CC_PTRCHECK_CAPABLE_HEADER()   _Pragma("clang abi_ptr_attr set(single)")
    #define cc_counted_by(x)              __attribute__((counted_by(x)))
    #define cc_sized_by(x)                __attribute__((sized_by(x)))
    #define cc_bidi_indexable             __attribute__((bidi_indexable))
    #define cc_indexable                  __attribute__((indexable))
    #define cc_single                     __attribute__((single))
    #define cc_unsafe_indexable           __attribute__((unsafe_indexable))
    #define cc_unsafe_forge_bidi_indexable(P, S)     __builtin_unsafe_forge_bidi_indexable(P, S)
    #define cc_unsafe_forge_single(P)     __builtin_unsafe_forge_single(P)
    #define cc_cstring                    cc_unsafe_indexable
    #define CC_WIDE_NULL                  ((void *cc_bidi_indexable)NULL)
    #define cc_ended_by(x)                __attribute__((ended_by(x)))
    #define cc_ptrcheck_unavailable_r(r)  __ptrcheck_unavailable_r(r)
#else
    #define CC_PTRCHECK                  0
    #define CC_PTRCHECK_CAPABLE_HEADER()
    #define cc_counted_by(x)
    #define cc_sized_by(x)
    #define cc_bidi_indexable
    #define cc_indexable
    #define cc_single
    #define cc_unsafe_indexable
    #define cc_unsafe_forge_bidi_indexable(P, S) (P)
    #define cc_unsafe_forge_single(P) (P)
    #define cc_cstring
    #define CC_WIDE_NULL NULL
    #define cc_ended_by(x)
    #define cc_ptrcheck_unavailable_r(r)
#endif // __has_feature(bounds_attributes)

// Define endianess for GCC, if needed and applicable.
#if defined(__GNUC__) && !defined(__LITTLE_ENDIAN__)
    #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
        #define __LITTLE_ENDIAN__ 1
    #endif
#endif

#if defined(ENABLE_CRYPTOKIT_PRIVATE_DEFINITIONS) && ENABLE_CRYPTOKIT_PRIVATE_DEFINITIONS
#define CC_PRIVATE_CRYPTOKIT 1
#else
#define CC_PRIVATE_CRYPTOKIT 0
#endif

#if defined(__clang__)
 #define CC_WORKSPACE_OVERRIDE_PRAGMA(x) _Pragma(#x)
 #define CC_WORKSPACE_OVERRIDE(f, o) CC_WORKSPACE_OVERRIDE_PRAGMA(workspace-override f o)
#else
 #define CC_WORKSPACE_OVERRIDE(f, o)
#endif

#ifndef CC_DIT_MAYBE_SUPPORTED
 #if defined(__arm64__) && !CC_USE_L4 && !CC_USE_SEPROM && !CC_IBOOT
  #define CC_DIT_MAYBE_SUPPORTED 1
 #else
  #define CC_DIT_MAYBE_SUPPORTED 0
 #endif
#endif

#endif /* _CORECRYPTO_CC_CONFIG_H_ */