-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cpu.h
1089 lines (1003 loc) · 37.7 KB
/
cpu.h
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// cpu.h - originally written and placed in the public domain by Wei Dai
// updated for ARM and PowerPC by Jeffrey Walton.
// updated to split CPU_Query() and CPU_Probe() by Jeffrey Walton.
/// \file cpu.h
/// \brief Functions for CPU features and intrinsics
/// \details The CPU functions are used in IA-32, ARM and PowerPC code paths. The
/// functions provide cpu specific feature testing on IA-32, ARM and PowerPC machines.
/// \details Feature detection uses CPUID on IA-32, like Intel and AMD. On other platforms
/// a two-part strategy is used. First, the library attempts to *Query* the OS for a feature,
/// like using Linux getauxval() or android_getCpuFeatures(). If that fails, then *Probe*
/// the cpu executing an instruction and an observe a SIGILL if unsupported. The general
/// pattern used by the library is:
/// <pre>
/// g_hasCRC32 = CPU_QueryCRC32() || CPU_ProbeCRC32();
/// g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
/// g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
/// </pre>
/// \details Generally speaking, CPU_Query() is in the source file <tt>cpu.cpp</tt> because it
/// does not require special architectural flags. CPU_Probe() is in a source file that receives
/// architectural flags, like <tt>sse_simd.cpp</tt>, <tt>neon_simd.cpp</tt> and
/// <tt>ppc_simd.cpp</tt>. For example, compiling <tt>neon_simd.cpp</tt> on an ARM64 machine will
/// have <tt>-march=armv8-a</tt> applied during a compile to make the instruction set architecture
/// (ISA) available.
/// \details The cpu probes are expensive when compared to a standard OS feature query. The library
/// also avoids probes on Apple platforms because Apple's signal handling for SIGILLs appears to
/// corrupt memory. CPU_Probe() will unconditionally return false for Apple platforms. OpenSSL
/// experienced the same problem and moved away from SIGILL probes on Apple.
#ifndef CRYPTOPP_CPU_H
#define CRYPTOPP_CPU_H
#include "config.h"
// Issue 340
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
# pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
// Applies to both X86/X32/X64 and ARM32/ARM64
#if defined(CRYPTOPP_LLVM_CLANG_VERSION) || defined(CRYPTOPP_APPLE_CLANG_VERSION)
#define NEW_LINE "\n"
#define INTEL_PREFIX ".intel_syntax;"
#define INTEL_NOPREFIX ".intel_syntax;"
#define ATT_PREFIX ".att_syntax;"
#define ATT_NOPREFIX ".att_syntax;"
#elif defined(CRYPTOPP_GCC_VERSION)
#define NEW_LINE
#define INTEL_PREFIX ".intel_syntax prefix;"
#define INTEL_NOPREFIX ".intel_syntax noprefix;"
#define ATT_PREFIX ".att_syntax prefix;"
#define ATT_NOPREFIX ".att_syntax noprefix;"
#else
#define NEW_LINE
#define INTEL_PREFIX
#define INTEL_NOPREFIX
#define ATT_PREFIX
#define ATT_NOPREFIX
#endif
// Thanks to v1ne at https://github.com/weidai11/cryptopp/pull/1133
#define PERCENT_PASTE(x) "%" #x
#define PERCENT_REG(x) PERCENT_PASTE(x)
#ifdef CRYPTOPP_GENERATE_X64_MASM
#define CRYPTOPP_X86_ASM_AVAILABLE
#define CRYPTOPP_BOOL_X64 1
#define CRYPTOPP_SSE2_ASM_AVAILABLE 1
#define NAMESPACE_END
#else
NAMESPACE_BEGIN(CryptoPP)
// ***************************** IA-32 ***************************** //
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_DOXYGEN_PROCESSING
#define CRYPTOPP_CPUID_AVAILABLE 1
// Hide from Doxygen
#ifndef CRYPTOPP_DOXYGEN_PROCESSING
// These should not be used directly
extern CRYPTOPP_DLL bool g_x86DetectionDone;
extern CRYPTOPP_DLL bool g_hasSSE2;
extern CRYPTOPP_DLL bool g_hasSSSE3;
extern CRYPTOPP_DLL bool g_hasSSE41;
extern CRYPTOPP_DLL bool g_hasSSE42;
extern CRYPTOPP_DLL bool g_hasMOVBE;
extern CRYPTOPP_DLL bool g_hasAESNI;
extern CRYPTOPP_DLL bool g_hasCLMUL;
extern CRYPTOPP_DLL bool g_hasAVX;
extern CRYPTOPP_DLL bool g_hasAVX2;
extern CRYPTOPP_DLL bool g_hasSHA;
extern CRYPTOPP_DLL bool g_hasADX;
extern CRYPTOPP_DLL bool g_isP4;
extern CRYPTOPP_DLL bool g_hasRDRAND;
extern CRYPTOPP_DLL bool g_hasRDSEED;
extern CRYPTOPP_DLL bool g_hasPadlockRNG;
extern CRYPTOPP_DLL bool g_hasPadlockACE;
extern CRYPTOPP_DLL bool g_hasPadlockACE2;
extern CRYPTOPP_DLL bool g_hasPadlockPHE;
extern CRYPTOPP_DLL bool g_hasPadlockPMM;
extern CRYPTOPP_DLL word32 g_cacheLineSize;
CRYPTOPP_DLL void CRYPTOPP_API DetectX86Features();
CRYPTOPP_DLL bool CRYPTOPP_API CpuId(word32 func, word32 subfunc, word32 output[4]);
#endif // CRYPTOPP_DOXYGEN_PROCESSING
/// \name IA-32 CPU FEATURES
//@{
/// \brief Determine SSE2 availability
/// \return true if SSE2 is determined to be available, false otherwise
/// \details MMX, SSE and SSE2 are core processor features for x86_64, and
/// the function return value is based on OSXSAVE. On i386 both
/// SSE2 and OSXSAVE are used for the return value.
/// \note This function is only available on Intel IA-32 platforms
inline bool HasSSE2()
{
#if (CRYPTOPP_SSE2_ASM_AVAILABLE || CRYPTOPP_SSE2_INTRIN_AVAILABLE)
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasSSE2;
#else
return false;
#endif
}
/// \brief Determine SSSE3 availability
/// \return true if SSSE3 is determined to be available, false otherwise
/// \details HasSSSE3() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasSSSE3()
{
#if CRYPTOPP_SSSE3_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasSSSE3;
#else
return false;
#endif
}
/// \brief Determine SSE4.1 availability
/// \return true if SSE4.1 is determined to be available, false otherwise
/// \details HasSSE41() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasSSE41()
{
#if CRYPTOPP_SSE41_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasSSE41;
#else
return false;
#endif
}
/// \brief Determine SSE4.2 availability
/// \return true if SSE4.2 is determined to be available, false otherwise
/// \details HasSSE42() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasSSE42()
{
#if CRYPTOPP_SSE42_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasSSE42;
#else
return false;
#endif
}
/// \brief Determine MOVBE availability
/// \return true if MOVBE is determined to be available, false otherwise
/// \details HasMOVBE() is a runtime check performed using CPUID
/// \since Crypto++ 8.3
/// \note This function is only available on Intel IA-32 platforms
inline bool HasMOVBE()
{
#if CRYPTOPP_SSE42_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasMOVBE;
#else
return false;
#endif
}
/// \brief Determine AES-NI availability
/// \return true if AES-NI is determined to be available, false otherwise
/// \details HasAESNI() is a runtime check performed using CPUID
/// \since Crypto++ 5.6.1
/// \note This function is only available on Intel IA-32 platforms
inline bool HasAESNI()
{
#if CRYPTOPP_AESNI_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasAESNI;
#else
return false;
#endif
}
/// \brief Determine Carryless Multiply availability
/// \return true if pclmulqdq is determined to be available, false otherwise
/// \details HasCLMUL() is a runtime check performed using CPUID
/// \since Crypto++ 5.6.1
/// \note This function is only available on Intel IA-32 platforms
inline bool HasCLMUL()
{
#if CRYPTOPP_CLMUL_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasCLMUL;
#else
return false;
#endif
}
/// \brief Determine SHA availability
/// \return true if SHA is determined to be available, false otherwise
/// \details HasSHA() is a runtime check performed using CPUID
/// \since Crypto++ 6.0
/// \note This function is only available on Intel IA-32 platforms
inline bool HasSHA()
{
#if CRYPTOPP_SHANI_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasSHA;
#else
return false;
#endif
}
/// \brief Determine ADX availability
/// \return true if ADX is determined to be available, false otherwise
/// \details HasADX() is a runtime check performed using CPUID
/// \since Crypto++ 7.0
/// \note This function is only available on Intel IA-32 platforms
inline bool HasADX()
{
#if CRYPTOPP_ADX_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasADX;
#else
return false;
#endif
}
/// \brief Determine AVX availability
/// \return true if AVX is determined to be available, false otherwise
/// \details HasAVX() is a runtime check performed using CPUID
/// \since Crypto++ 8.0
/// \note This function is only available on Intel IA-32 platforms
inline bool HasAVX()
{
#if CRYPTOPP_AVX_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasAVX;
#else
return false;
#endif
}
/// \brief Determine AVX2 availability
/// \return true if AVX2 is determined to be available, false otherwise
/// \details HasAVX2() is a runtime check performed using CPUID
/// \since Crypto++ 8.0
/// \note This function is only available on Intel IA-32 platforms
inline bool HasAVX2()
{
#if CRYPTOPP_AVX2_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasAVX2;
#else
return false;
#endif
}
/// \brief Determine RDRAND availability
/// \return true if RDRAND is determined to be available, false otherwise
/// \details HasRDRAND() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasRDRAND()
{
#if CRYPTOPP_RDRAND_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasRDRAND;
#else
return false;
#endif
}
/// \brief Determine RDSEED availability
/// \return true if RDSEED is determined to be available, false otherwise
/// \details HasRDSEED() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasRDSEED()
{
#if CRYPTOPP_RDSEED_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasRDSEED;
#else
return false;
#endif
}
/// \brief Determine Padlock RNG availability
/// \return true if VIA Padlock RNG is determined to be available, false otherwise
/// \details HasPadlockRNG() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasPadlockRNG()
{
#if CRYPTOPP_PADLOCK_RNG_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasPadlockRNG;
#else
return false;
#endif
}
/// \brief Determine Padlock ACE availability
/// \return true if VIA Padlock ACE is determined to be available, false otherwise
/// \details HasPadlockACE() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasPadlockACE()
{
#if CRYPTOPP_PADLOCK_ACE_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasPadlockACE;
#else
return false;
#endif
}
/// \brief Determine Padlock ACE2 availability
/// \return true if VIA Padlock ACE2 is determined to be available, false otherwise
/// \details HasPadlockACE2() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasPadlockACE2()
{
#if CRYPTOPP_PADLOCK_ACE2_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasPadlockACE2;
#else
return false;
#endif
}
/// \brief Determine Padlock PHE availability
/// \return true if VIA Padlock PHE is determined to be available, false otherwise
/// \details HasPadlockPHE() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasPadlockPHE()
{
#if CRYPTOPP_PADLOCK_PHE_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasPadlockPHE;
#else
return false;
#endif
}
/// \brief Determine Padlock PMM availability
/// \return true if VIA Padlock PMM is determined to be available, false otherwise
/// \details HasPadlockPMM() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool HasPadlockPMM()
{
#if CRYPTOPP_PADLOCK_PMM_AVAILABLE
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasPadlockPMM;
#else
return false;
#endif
}
/// \brief Determine if the CPU is an Intel P4
/// \return true if the CPU is a P4, false otherwise
/// \details IsP4() is a runtime check performed using CPUID
/// \note This function is only available on Intel IA-32 platforms
inline bool IsP4()
{
if (!g_x86DetectionDone)
DetectX86Features();
return g_isP4;
}
/// \brief Provides the cache line size
/// \return lower bound on the size of a cache line in bytes, if available
/// \details GetCacheLineSize() returns the lower bound on the size of a cache line, if it
/// is available. If the value is not available at runtime, then 32 is returned for a 32-bit
/// processor and 64 is returned for a 64-bit processor.
/// \details x86/x32/x64 uses CPUID to determine the value and it is usually accurate. PowerPC
/// and AIX also makes the value available to user space and it is also usually accurate. The
/// ARM processor equivalent is a privileged instruction, so a compile time value is returned.
inline int GetCacheLineSize()
{
if (!g_x86DetectionDone)
DetectX86Features();
return g_cacheLineSize;
}
//@}
#endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
// ***************************** ARM-32, Aarch32 and Aarch64 ***************************** //
#if CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8 || CRYPTOPP_DOXYGEN_PROCESSING
// Hide from Doxygen
#ifndef CRYPTOPP_DOXYGEN_PROCESSING
extern bool g_ArmDetectionDone;
extern bool g_hasARMv7;
extern bool g_hasNEON;
extern bool g_hasPMULL;
extern bool g_hasCRC32;
extern bool g_hasAES;
extern bool g_hasSHA1;
extern bool g_hasSHA2;
extern bool g_hasSHA512;
extern bool g_hasSHA3;
extern bool g_hasSM3;
extern bool g_hasSM4;
void CRYPTOPP_API DetectArmFeatures();
#endif // CRYPTOPP_DOXYGEN_PROCESSING
/// \name ARM A-32, Aarch32 and AArch64 CPU FEATURES
//@{
/// \brief Determine if an ARM processor is ARMv7 or above
/// \return true if the hardware is ARMv7 or above, false otherwise.
/// \details Some AES code requires ARMv7 or above
/// \since Crypto++ 8.0
/// \note This function is only available on ARM-32, Aarch32 and Aarch64 platforms
inline bool HasARMv7()
{
// ASIMD is a core feature on Aarch32 and Aarch64 like SSE2 is a core feature on x86_64
#if defined(__aarch32__) || defined(__aarch64__)
return true;
#else
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasARMv7;
#endif
}
/// \brief Determine if an ARM processor has Advanced SIMD available
/// \return true if the hardware is capable of Advanced SIMD at runtime, false otherwise.
/// \details Advanced SIMD instructions are available under most ARMv7, Aarch32 and Aarch64.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mfpu=neon</tt> (32-bit) or <tt>-march=armv8-a</tt>
/// (64-bit). Also see ARM's <tt>__ARM_NEON</tt> preprocessor macro.
/// \since Crypto++ 5.6.4
/// \note This function is only available on ARM-32, Aarch32 and Aarch64 platforms
inline bool HasNEON()
{
// ASIMD is a core feature on Aarch32 and Aarch64 like SSE2 is a core feature on x86_64
#if defined(CRYPTOPP_ARM_ASIMD_AVAILABLE)
return true;
#elif defined(CRYPTOPP_ARM_NEON_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasNEON;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has CRC32 available
/// \return true if the hardware is capable of CRC32 at runtime, false otherwise.
/// \details CRC32 instructions provide access to the processor's CRC-32 and CRC-32C
/// instructions. They are provided by ARM C Language Extensions 2.0 (ACLE 2.0) and
/// available under Aarch32 and Aarch64.
/// \details Runtime support requires compile time support. When compiling with GCC,
/// you may need to compile with <tt>-march=armv8-a+crc</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRC32</tt> preprocessor macro.
/// \since Crypto++ 5.6.4
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasCRC32()
{
#if defined(CRYPTOPP_ARM_CRC32_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasCRC32;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has AES available
/// \return true if the hardware is capable of AES at runtime, false otherwise.
/// \details AES is part of the optional Crypto extensions on Aarch32 and Aarch64. They are
/// accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 5.6.4
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasAES()
{
#if defined(CRYPTOPP_ARM_AES_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasAES;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor provides Polynomial Multiplication
/// \return true if the hardware is capable of polynomial multiplications at runtime,
/// false otherwise.
/// \details The multiplication instructions are available under Aarch32 and Aarch64.
/// \details Runtime support requires compile time support. When compiling with GCC,
/// you may need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 5.6.4
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasPMULL()
{
#if defined(CRYPTOPP_ARM_PMULL_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasPMULL;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has SHA1 available
/// \return true if the hardware is capable of SHA1 at runtime, false otherwise.
/// \details SHA1 is part of the optional Crypto extensions on Aarch32 and Aarch64. They are
/// accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 5.6.4
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasSHA1()
{
#if defined(CRYPTOPP_ARM_SHA1_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasSHA1;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has SHA256 available
/// \return true if the hardware is capable of SHA256 at runtime, false otherwise.
/// \details SHA256 is part of the optional Crypto extensions on Aarch32 and Aarch64. They are
/// accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-march=armv8-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 5.6.4
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasSHA2()
{
#if defined(CRYPTOPP_ARM_SHA2_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasSHA2;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has SHA3 available
/// \return true if the hardware is capable of SHA3 at runtime, false otherwise.
/// \details SHA3 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
/// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you
/// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 8.0
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasSHA3()
{
#if defined(CRYPTOPP_ARM_SHA3_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasSHA3;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has SHA512 available
/// \return true if the hardware is capable of SHA512 at runtime, false otherwise.
/// \details SHA512 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
/// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you
/// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 8.0
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasSHA512()
{
#if defined(CRYPTOPP_ARM_SHA512_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasSHA512;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has SM3 available
/// \return true if the hardware is capable of SM3 at runtime, false otherwise.
/// \details SM3 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
/// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you
/// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 8.0
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasSM3()
{
#if defined(CRYPTOPP_ARM_SM3_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasSM3;
#else
return false;
#endif
}
/// \brief Determine if an ARM processor has SM4 available
/// \return true if the hardware is capable of SM4 at runtime, false otherwise.
/// \details SM4 is part of the ARMv8.2 Crypto extensions on Aarch32 and Aarch64. They
/// are accessed using ARM C Language Extensions 2.0 (ACLE 2.0).
/// \details Runtime support requires compile time support. When compiling with GCC, you
/// may need to compile with <tt>-march=armv8.2-a+crypto</tt>; while Apple requires
/// <tt>-arch arm64</tt>. Also see ARM's <tt>__ARM_FEATURE_CRYPTO</tt> preprocessor macro.
/// \since Crypto++ 8.0
/// \note This function is only available on Aarch32 and Aarch64 platforms
inline bool HasSM4()
{
#if defined(CRYPTOPP_ARM_SM4_AVAILABLE)
if (!g_ArmDetectionDone)
DetectArmFeatures();
return g_hasSM4;
#else
return false;
#endif
}
//@}
#endif // CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8
// ***************************** PowerPC ***************************** //
#if CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 || CRYPTOPP_DOXYGEN_PROCESSING
// Hide from Doxygen
#ifndef CRYPTOPP_DOXYGEN_PROCESSING
extern bool g_PowerPcDetectionDone;
extern bool g_hasAltivec;
extern bool g_hasPower7;
extern bool g_hasPower8;
extern bool g_hasPower9;
extern bool g_hasAES;
extern bool g_hasPMULL;
extern bool g_hasSHA256;
extern bool g_hasSHA512;
extern bool g_hasDARN;
extern word32 g_cacheLineSize;
void CRYPTOPP_API DetectPowerPcFeatures();
#endif // CRYPTOPP_DOXYGEN_PROCESSING
/// \name POWERPC CPU FEATURES
//@{
/// \brief Determine if a PowerPC processor has Altivec available
/// \return true if the hardware is capable of Altivec at runtime, false otherwise.
/// \details Altivec instructions are available on modern PowerPCs.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power4</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr6 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasAltivec()
{
#if CRYPTOPP_ALTIVEC_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasAltivec;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has Power7 available
/// \return true if the hardware is capable of Power7 at runtime, false otherwise.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power7</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr7 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasPower7()
{
#if CRYPTOPP_POWER7_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasPower7;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has Power8 available
/// \return true if the hardware is capable of Power8 at runtime, false otherwise.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasPower8()
{
#if CRYPTOPP_POWER8_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasPower8;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has Power9 available
/// \return true if the hardware is capable of Power9 at runtime, false otherwise.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power9</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr9 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasPower9()
{
#if CRYPTOPP_POWER9_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasPower9;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has AES available
/// \return true if the hardware is capable of AES at runtime, false otherwise.
/// \details AES is part of the in-crypto extensions on Power8 and Power9.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasAES()
{
#if CRYPTOPP_POWER8_AES_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasAES;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has Polynomial Multiply available
/// \return true if the hardware is capable of PMULL at runtime, false otherwise.
/// \details PMULL is part of the in-crypto extensions on Power8 and Power9.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasPMULL()
{
#if CRYPTOPP_POWER8_VMULL_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasPMULL;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has SHA256 available
/// \return true if the hardware is capable of SHA256 at runtime, false otherwise.
/// \details SHA is part of the in-crypto extensions on Power8 and Power9.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasSHA256()
{
#if CRYPTOPP_POWER8_SHA_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasSHA256;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has SHA512 available
/// \return true if the hardware is capable of SHA512 at runtime, false otherwise.
/// \details SHA is part of the in-crypto extensions on Power8 and Power9.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power8</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr8 -qaltivec</tt>. Also see PowerPC's <tt>__CRYPTO</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasSHA512()
{
#if CRYPTOPP_POWER8_SHA_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_hasSHA512;
#else
return false;
#endif
}
/// \brief Determine if a PowerPC processor has DARN available
/// \return true if the hardware is capable of DARN at runtime, false otherwise.
/// \details Runtime support requires compile time support. When compiling with GCC, you may
/// need to compile with <tt>-mcpu=power9</tt>; while IBM XL C/C++ compilers require
/// <tt>-qarch=pwr9 -qaltivec</tt>. Also see PowerPC's <tt>_ALTIVEC_</tt> preprocessor macro.
/// \note This function is only available on PowerPC and PowerPC-64 platforms
inline bool HasDARN()
{
#if CRYPTOPP_POWER9_AVAILABLE
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
// see comments in cpu.cpp
# if defined(__ibmxl__) && defined(__linux__)
return false;
# else
return g_hasDARN;
# endif
#else
return false;
#endif
}
/// \brief Provides the cache line size
/// \return lower bound on the size of a cache line in bytes, if available
/// \details GetCacheLineSize() returns the lower bound on the size of a cache line, if it
/// is available. If the value is not available at runtime, then 32 is returned for a 32-bit
/// processor and 64 is returned for a 64-bit processor.
/// \details x86/x32/x64 uses CPUID to determine the value and it is usually accurate. PowerPC
/// and AIX also makes the value available to user space and it is also usually accurate. The
/// ARM processor equivalent is a privileged instruction, so a compile time value is returned.
inline int GetCacheLineSize()
{
if (!g_PowerPcDetectionDone)
DetectPowerPcFeatures();
return g_cacheLineSize;
}
//@}
#endif // CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
// ***************************** L1 cache line ***************************** //
// Non-Intel systems
#if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
/// \brief Provides the cache line size
/// \return lower bound on the size of a cache line in bytes, if available
/// \details GetCacheLineSize() returns the lower bound on the size of a cache line, if it
/// is available. If the value is not available at runtime, then 32 is returned for a 32-bit
/// processor and 64 is returned for a 64-bit processor.
/// \details x86/x32/x64 uses CPUID to determine the value and it is usually accurate. PowerPC
/// and AIX also makes the value available to user space and it is also usually accurate. The
/// ARM processor equivalent is a privileged instruction, so a compile time value is returned.
inline int GetCacheLineSize()
{
return CRYPTOPP_L1_CACHE_LINE_SIZE;
}
#endif // Non-Intel systems
#endif // CRYPTOPP_GENERATE_X64_MASM
// ***************************** Inline ASM Helper ***************************** //
#ifndef CRYPTOPP_DOXYGEN_PROCESSING
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
#ifdef CRYPTOPP_GENERATE_X64_MASM
#define AS1(x) x*newline*
#define AS2(x, y) x, y*newline*
#define AS3(x, y, z) x, y, z*newline*
#define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
#define ASL(x) label##x:*newline*
#define ASJ(x, y, z) x label##y*newline*
#define ASC(x, y) x label##y*newline*
#define AS_HEX(y) 0##y##h
#elif defined(CRYPTOPP_MSC_VERSION) || defined(__BORLANDC__)
#define AS1(x) __asm {x}
#define AS2(x, y) __asm {x, y}
#define AS3(x, y, z) __asm {x, y, z}
#define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
#define ASL(x) __asm {label##x:}
#define ASJ(x, y, z) __asm {x label##y}
#define ASC(x, y) __asm {x label##y}
#define CRYPTOPP_NAKED __declspec(naked)
#define AS_HEX(y) 0x##y
#else
// define these in two steps to allow arguments to be expanded
#define GNU_AS1(x) #x ";" NEW_LINE
#define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
#define GNU_AS3(x, y, z) #x ", " #y ", " #z ";" NEW_LINE
#define GNU_ASL(x) "\n" #x ":" NEW_LINE
// clang 5.0.0 and apple clang 9.0.0 don't support numerical backward jumps
#if (CRYPTOPP_LLVM_CLANG_VERSION >= 50000) || (CRYPTOPP_APPLE_CLANG_VERSION >= 90000)
#define GNU_ASJ(x, y, z) ATT_PREFIX ";" NEW_LINE #x " " #y #z ";" NEW_LINE INTEL_PREFIX ";" NEW_LINE
#else
#define GNU_ASJ(x, y, z) #x " " #y #z ";" NEW_LINE
#endif
#define AS1(x) GNU_AS1(x)
#define AS2(x, y) GNU_AS2(x, y)
#define AS3(x, y, z) GNU_AS3(x, y, z)
#define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
#define ASL(x) GNU_ASL(x)
#define ASJ(x, y, z) GNU_ASJ(x, y, z)
#define ASC(x, y) #x " " #y ";"
#define CRYPTOPP_NAKED
#define AS_HEX(y) 0x##y
#endif
#define IF0(y)
#define IF1(y) y
#ifdef CRYPTOPP_GENERATE_X64_MASM
#define ASM_MOD(x, y) ((x) MOD (y))
#define XMMWORD_PTR XMMWORD PTR
#else
// GNU assembler doesn't seem to have mod operator
#define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
// GAS 2.15 doesn't support XMMWORD PTR. it seems necessary only for MASM
#define XMMWORD_PTR
#endif
#if CRYPTOPP_BOOL_X86
#define AS_REG_1 ecx
#define AS_REG_2 edx
#define AS_REG_3 esi
#define AS_REG_4 edi
#define AS_REG_5 eax
#define AS_REG_6 ebx
#define AS_REG_7 ebp
#define AS_REG_1d ecx
#define AS_REG_2d edx
#define AS_REG_3d esi
#define AS_REG_4d edi
#define AS_REG_5d eax
#define AS_REG_6d ebx
#define AS_REG_7d ebp
#define WORD_SZ 4
#define WORD_REG(x) e##x
#define WORD_PTR DWORD PTR
#define AS_PUSH_IF86(x) AS1(push e##x)
#define AS_POP_IF86(x) AS1(pop e##x)
#define AS_JCXZ jecxz
#elif CRYPTOPP_BOOL_X32
#define AS_REG_1 ecx
#define AS_REG_2 edx
#define AS_REG_3 r8d
#define AS_REG_4 r9d
#define AS_REG_5 eax
#define AS_REG_6 r10d
#define AS_REG_7 r11d
#define AS_REG_1d ecx
#define AS_REG_2d edx
#define AS_REG_3d r8d
#define AS_REG_4d r9d
#define AS_REG_5d eax
#define AS_REG_6d r10d
#define AS_REG_7d r11d
#define WORD_SZ 4
#define WORD_REG(x) e##x
#define WORD_PTR DWORD PTR
#define AS_PUSH_IF86(x) AS1(push r##x)
#define AS_POP_IF86(x) AS1(pop r##x)
#define AS_JCXZ jecxz
#elif CRYPTOPP_BOOL_X64