7#ifndef EXCEPTION_EXECUTE_HANDLER
8# define EXCEPTION_EXECUTE_HANDLER 1
11#ifndef CRYPTOPP_IMPORTS
19#if (CRYPTOPP_MSC_VERSION >= 1600) && (defined(_M_IX86) || defined(_M_X64))
20# include <immintrin.h>
25#if defined(_WIN32) && defined(_M_ARM64)
27# include <processthreadsapi.h>
31# include <sys/systemcfg.h>
40#if (((__GLIBC__ * 100) + __GLIBC_MINOR__) >= 216)
41# define CRYPTOPP_GETAUXV_AVAILABLE 1
44#if CRYPTOPP_GETAUXV_AVAILABLE
53unsigned long int getauxval(
unsigned long int) {
return 0; }
57# include <sys/utsname.h>
58# include <sys/sysctl.h>
63#if defined(__FreeBSD__)
65# include <sys/elf_common.h>
72#if defined(__ANDROID__)
73# include "cpu-features.h"
76#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
84#if defined(_M_X64) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
85extern "C" unsigned long long __fastcall XGETBV64(
unsigned int);
86extern "C" unsigned long long __fastcall CPUID64(
unsigned int,
unsigned int,
unsigned int*);
89#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
91 typedef void (*SigHandler)(int);
96 static jmp_buf s_jmpNoCPUID;
97 static void SigIllHandler(
int)
99 longjmp(s_jmpNoCPUID, 1);
104ANONYMOUS_NAMESPACE_BEGIN
106#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
108using CryptoPP::word32;
110inline bool IsIntel(
const word32 output[4])
113 return (output[1] == 0x756e6547) &&
114 (output[2] == 0x6c65746e) &&
115 (output[3] == 0x49656e69);
118inline bool IsAMD(
const word32 output[4])
121 return ((output[1] == 0x68747541) &&
122 (output[2] == 0x444D4163) &&
123 (output[3] == 0x69746E65)) ||
125 ((output[1] == 0x69444d41) &&
126 (output[2] == 0x74656273) &&
127 (output[3] == 0x21726574));
130inline bool IsHygon(
const word32 output[4])
133 return (output[1] == 0x6f677948) &&
134 (output[2] == 0x656e6975) &&
135 (output[3] == 0x6e65476e);
138inline bool IsVIA(
const word32 output[4])
141 return ((output[1] == 0x746e6543) &&
142 (output[2] == 0x736c7561) &&
143 (output[3] == 0x48727561)) ||
145 ((output[1] == 0x32414956) &&
146 (output[2] == 0x32414956) &&
147 (output[3] == 0x32414956));
152#if defined(__APPLE__)
155class AppleMachineInfo
158 enum { PowerMac=1, Mac, iPhone, iPod, iPad, AppleTV, AppleWatch };
159 enum { PowerPC=1, I386, I686, X86_64, ARM32, ARMV8, ARMV82, ARMV83 };
161 AppleMachineInfo() : m_device(0), m_version(0), m_arch(0)
163 struct utsname systemInfo;
164 systemInfo.machine[0] =
'\0';
167 std::string machine(systemInfo.machine);
169 std::string::size_type pos = machine.find_first_of(
"0123456789");
170 if (pos != std::string::npos)
171 m_version = std::atoi(machine.substr(pos).c_str());
173 if (machine.find(
"iPhone") != std::string::npos)
176 if (m_version >= 6) { m_arch = ARMV8; }
177 else { m_arch = ARM32; }
179 else if (machine.find(
"iPod") != std::string::npos)
182 if (m_version >= 6) { m_arch = ARMV8; }
183 else { m_arch = ARM32; }
185 else if (machine.find(
"iPad") != std::string::npos)
188 if (m_version >= 5) { m_arch = ARMV8; }
189 else { m_arch = ARM32; }
191 else if (machine.find(
"PowerMac") != std::string::npos ||
192 machine.find(
"Power Macintosh") != std::string::npos)
197 else if (machine.find(
"Mac") != std::string::npos ||
198 machine.find(
"Macintosh") != std::string::npos)
200#if defined(__x86_64) || defined(__amd64)
215 else if (machine.find(
"AppleTV") != std::string::npos)
218 if (m_version >= 4) { m_arch = ARMV8; }
219 else { m_arch = ARM32; }
221 else if (machine.find(
"AppleWatch") != std::string::npos)
223 m_device = AppleWatch;
224 if (m_version >= 4) { m_arch = ARMV8; }
225 else { m_arch = ARM32; }
227 else if (machine.find(
"arm64") != std::string::npos)
236 if (sysctlbyname(
"machdep.cpu.brand_string", &brand[0], &size, NULL, 0) == 0 && size > 0)
238 if (brand[size-1] ==
'\0')
243 if (brand ==
"Apple M1")
261 unsigned int Device()
const {
269 unsigned int Arch()
const {
273 bool IsARM32()
const {
274 return m_arch == ARM32;
277 bool IsARMv8()
const {
278 return m_arch >= ARMV8;
281 bool IsARMv82()
const {
282 return m_arch >= ARMV82;
285 bool IsARMv83()
const {
286 return m_arch >= ARMV83;
290 unsigned int m_device, m_version, m_arch;
293void GetAppleMachineInfo(
unsigned int& device,
unsigned int& version,
unsigned int& arch)
295#if CRYPTOPP_CXX11_STATIC_INIT
296 static const AppleMachineInfo info;
298 using CryptoPP::Singleton;
302 device = info.Device();
303 version = info.Version();
307inline bool IsAppleMachineARM32()
309 static unsigned int arch;
313 GetAppleMachineInfo(unused, unused, arch);
315 return arch == AppleMachineInfo::ARM32;
318inline bool IsAppleMachineARMv8()
320 static unsigned int arch;
324 GetAppleMachineInfo(unused, unused, arch);
326 return arch >= AppleMachineInfo::ARMV8;
329inline bool IsAppleMachineARMv82()
331 static unsigned int arch;
335 GetAppleMachineInfo(unused, unused, arch);
337 return arch >= AppleMachineInfo::ARMV82;
340inline bool IsAppleMachineARMv83()
342 static unsigned int arch;
346 GetAppleMachineInfo(unused, unused, arch);
348 return arch >= AppleMachineInfo::ARMV83;
353ANONYMOUS_NAMESPACE_END
359#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
384extern bool CPU_ProbeSSE2();
393#if defined(CRYPTOPP_DISABLE_ASM)
398#elif defined(_M_X64) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
400 return XGETBV64(num);
404#elif defined(_M_IX86) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
421 return (
static_cast<word64>(d) << 32) | a;
424#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
429 "xgetbv" :
"=a"(a),
"=d"(d) :
"c"(num) :
"cc"
431 return (
static_cast<word64>(d) << 32) | a;
434#elif defined(__GNUC__) || defined(__clang__) || defined(__SUNPRO_CC)
441 ".byte 0x0f, 0x01, 0xd0" "\n\t"
442 :
"=a"(a),
"=d"(d) :
"c"(num) :
"cc"
444 return (
static_cast<word64>(d) << 32) | a;
446 # error "Need an xgetbv function"
456#if defined(CRYPTOPP_DISABLE_ASM)
457 output[0] = output[1] = output[2] = output[3] = 0;
462#elif defined(_M_X64) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
464 CPUID64(func, subfunc, output);
469#elif defined(_M_IX86) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
493 __except (EXCEPTION_EXECUTE_HANDLER)
505 volatile bool result =
true;
507 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
508 if (oldHandler == SIG_ERR)
512 volatile sigset_t oldMask;
513 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask) != 0)
515 signal(SIGILL, oldHandler);
520 if (setjmp(s_jmpNoCPUID))
527# if CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
528 "pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"
530 "push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"
532 :
"=a" (output[0]),
"=D" (output[1]),
"=c" (output[2]),
"=d" (output[3])
533 :
"a" (func),
"c" (subfunc)
539 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
542 signal(SIGILL, oldHandler);
547void DetectX86Features()
551 word32 cpuid0[4]={0}, cpuid1[4]={0}, cpuid2[4]={0};
553#if defined(CRYPTOPP_DISABLE_ASM)
557 if (!CpuId(0, 0, cpuid0))
559 if (!CpuId(1, 0, cpuid1))
563 CRYPTOPP_CONSTANT(EAX_REG = 0);
564 CRYPTOPP_CONSTANT(EBX_REG = 1);
565 CRYPTOPP_CONSTANT(ECX_REG = 2);
566 CRYPTOPP_CONSTANT(EDX_REG = 3);
568 CRYPTOPP_CONSTANT(MMX_FLAG = (1 << 24));
569 CRYPTOPP_CONSTANT(SSE_FLAG = (1 << 25));
570 CRYPTOPP_CONSTANT(SSE2_FLAG = (1 << 26));
572 CRYPTOPP_CONSTANT(SSE3_FLAG = (1 << 0));
573 CRYPTOPP_CONSTANT(SSSE3_FLAG = (1 << 9));
574 CRYPTOPP_CONSTANT(SSE41_FLAG = (1 << 19));
575 CRYPTOPP_CONSTANT(SSE42_FLAG = (1 << 20));
576 CRYPTOPP_CONSTANT(MOVBE_FLAG = (1 << 22));
577 CRYPTOPP_CONSTANT(AESNI_FLAG = (1 << 25));
578 CRYPTOPP_CONSTANT(CLMUL_FLAG = (1 << 1));
580 CRYPTOPP_CONSTANT(XSAVE_FLAG = (1 << 26));
581 CRYPTOPP_CONSTANT(OSXSAVE_FLAG = (1 << 27));
583 CRYPTOPP_CONSTANT(AVX_FLAG = (3 << 27));
584 CRYPTOPP_CONSTANT(YMM_FLAG = (3 << 1));
588 CRYPTOPP_UNUSED(MMX_FLAG); CRYPTOPP_UNUSED(SSE_FLAG);
589 CRYPTOPP_UNUSED(SSE2_FLAG); CRYPTOPP_UNUSED(SSE3_FLAG);
590 CRYPTOPP_UNUSED(XSAVE_FLAG);
592#if (CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
595 g_hasSSE2 = (cpuid1[ECX_REG] & OSXSAVE_FLAG) != 0;
601 if ((cpuid1[EDX_REG] & SSE2_FLAG) == SSE2_FLAG)
602 g_hasSSE2 = (cpuid1[ECX_REG] & XSAVE_FLAG) != 0 &&
603 (cpuid1[ECX_REG] & OSXSAVE_FLAG) != 0;
610 if (g_hasSSE2 ==
false)
612 g_hasSSE2 = CPU_ProbeSSE2();
613 if (g_hasSSE2 ==
false)
617 g_hasSSSE3 = (cpuid1[ECX_REG] & SSSE3_FLAG) != 0;
618 g_hasSSE41 = (cpuid1[ECX_REG] & SSE41_FLAG) != 0;
619 g_hasSSE42 = (cpuid1[ECX_REG] & SSE42_FLAG) != 0;
620 g_hasMOVBE = (cpuid1[ECX_REG] & MOVBE_FLAG) != 0;
621 g_hasAESNI = (cpuid1[ECX_REG] & AESNI_FLAG) != 0;
622 g_hasCLMUL = (cpuid1[ECX_REG] & CLMUL_FLAG) != 0;
627 if ((cpuid1[ECX_REG] & AVX_FLAG) == AVX_FLAG)
630 g_hasAVX = (xcr0 & YMM_FLAG) == YMM_FLAG;
635 CRYPTOPP_CONSTANT(RDRAND_FLAG = (1 << 30));
636 CRYPTOPP_CONSTANT(RDSEED_FLAG = (1 << 18));
637 CRYPTOPP_CONSTANT( ADX_FLAG = (1 << 19));
638 CRYPTOPP_CONSTANT( SHA_FLAG = (1 << 29));
639 CRYPTOPP_CONSTANT( AVX2_FLAG = (1 << 5));
641 g_isP4 = ((cpuid1[0] >> 8) & 0xf) == 0xf;
642 g_cacheLineSize = 8 * GETBYTE(cpuid1[1], 1);
643 g_hasRDRAND = (cpuid1[ECX_REG] & RDRAND_FLAG) != 0;
645 if (cpuid0[EAX_REG] >= 7)
647 if (CpuId(7, 0, cpuid2))
649 g_hasRDSEED = (cpuid2[EBX_REG] & RDSEED_FLAG) != 0;
650 g_hasADX = (cpuid2[EBX_REG] & ADX_FLAG) != 0;
651 g_hasSHA = (cpuid2[EBX_REG] & SHA_FLAG) != 0;
652 g_hasAVX2 = (cpuid2[EBX_REG] & AVX2_FLAG) != 0;
656 else if (IsAMD(cpuid0) || IsHygon(cpuid0))
658 CRYPTOPP_CONSTANT(RDRAND_FLAG = (1 << 30));
659 CRYPTOPP_CONSTANT(RDSEED_FLAG = (1 << 18));
660 CRYPTOPP_CONSTANT( ADX_FLAG = (1 << 19));
661 CRYPTOPP_CONSTANT( SHA_FLAG = (1 << 29));
662 CRYPTOPP_CONSTANT( AVX2_FLAG = (1 << 5));
664 CpuId(0x80000005, 0, cpuid2);
665 g_cacheLineSize = GETBYTE(cpuid2[ECX_REG], 0);
666 g_hasRDRAND = (cpuid1[ECX_REG] & RDRAND_FLAG) != 0;
668 if (cpuid0[EAX_REG] >= 7)
670 if (CpuId(7, 0, cpuid2))
672 g_hasRDSEED = (cpuid2[EBX_REG] & RDSEED_FLAG) != 0;
673 g_hasADX = (cpuid2[EBX_REG] & ADX_FLAG) != 0;
674 g_hasSHA = (cpuid2[EBX_REG] & SHA_FLAG) != 0;
675 g_hasAVX2 = (cpuid2[EBX_REG] & AVX2_FLAG) != 0;
684 CRYPTOPP_CONSTANT(FAMILY_BASE_FLAG = (0x0f << 8));
685 CRYPTOPP_CONSTANT(FAMILY_EXT_FLAG = (0xff << 20));
687 word32 family = (cpuid1[0] & FAMILY_BASE_FLAG) >> 8;
689 family += (cpuid1[0] & FAMILY_EXT_FLAG) >> 20;
690 if (family == 0x15 || family == 0x16)
697 else if (IsVIA(cpuid0))
700 CRYPTOPP_CONSTANT( RNG_FLAGS = (0x3 << 2));
701 CRYPTOPP_CONSTANT( ACE_FLAGS = (0x3 << 6));
702 CRYPTOPP_CONSTANT(ACE2_FLAGS = (0x3 << 8));
703 CRYPTOPP_CONSTANT( PHE_FLAGS = (0x3 << 10));
704 CRYPTOPP_CONSTANT( PMM_FLAGS = (0x3 << 12));
706 CpuId(0xC0000000, 0, cpuid2);
707 word32 extendedFeatures = cpuid2[0];
709 if (extendedFeatures >= 0xC0000001)
711 CpuId(0xC0000001, 0, cpuid2);
712 g_hasPadlockRNG = (cpuid2[EDX_REG] & RNG_FLAGS) != 0;
713 g_hasPadlockACE = (cpuid2[EDX_REG] & ACE_FLAGS) != 0;
714 g_hasPadlockACE2 = (cpuid2[EDX_REG] & ACE2_FLAGS) != 0;
715 g_hasPadlockPHE = (cpuid2[EDX_REG] & PHE_FLAGS) != 0;
716 g_hasPadlockPMM = (cpuid2[EDX_REG] & PMM_FLAGS) != 0;
719 if (extendedFeatures >= 0xC0000005)
721 CpuId(0xC0000005, 0, cpuid2);
722 g_cacheLineSize = GETBYTE(cpuid2[ECX_REG], 0);
728 g_hasAVX2 &= g_hasAVX;
732#if defined(_SC_LEVEL1_DCACHE_LINESIZE)
735 int cacheLineSize = (int)sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
736 if (g_cacheLineSize == 0 && cacheLineSize > 0)
737 g_cacheLineSize = cacheLineSize;
740 if (g_cacheLineSize == 0)
743 *
const_cast<volatile bool*
>(&g_x86DetectionDone) =
true;
748#elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8)
778extern bool CPU_ProbeARMv7();
779extern bool CPU_ProbeNEON();
780extern bool CPU_ProbeCRC32();
781extern bool CPU_ProbeAES();
782extern bool CPU_ProbeSHA1();
783extern bool CPU_ProbeSHA256();
784extern bool CPU_ProbeSHA512();
785extern bool CPU_ProbeSHA3();
786extern bool CPU_ProbeSM3();
787extern bool CPU_ProbeSM4();
788extern bool CPU_ProbePMULL();
793# define HWCAP_ARMv7 (1 << 29)
796# define HWCAP_ASIMD (1 << 1)
799# define HWCAP_NEON (1 << 12)
802# define HWCAP_CRC32 (1 << 7)
805# define HWCAP2_CRC32 (1 << 4)
808# define HWCAP_PMULL (1 << 4)
811# define HWCAP2_PMULL (1 << 1)
814# define HWCAP_AES (1 << 3)
817# define HWCAP2_AES (1 << 0)
820# define HWCAP_SHA1 (1 << 5)
823# define HWCAP_SHA2 (1 << 6)
826# define HWCAP2_SHA1 (1 << 2)
829# define HWCAP2_SHA2 (1 << 3)
832# define HWCAP_SHA3 (1 << 17)
835# define HWCAP_SM3 (1 << 18)
838# define HWCAP_SM4 (1 << 19)
841# define HWCAP_SHA512 (1 << 21)
844inline bool CPU_QueryARMv7()
846#if defined(__ANDROID__) && defined(__arm__)
847 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
848 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0))
850#elif defined(__linux__) && defined(__arm__)
851 if ((getauxval(AT_HWCAP) & HWCAP_ARMv7) != 0 ||
852 (getauxval(AT_HWCAP) & HWCAP_NEON) != 0)
854#elif defined(__APPLE__) && defined(__arm__)
857#elif defined(_WIN32) && defined(_M_ARM64)
864inline bool CPU_QueryNEON()
866#if defined(__ANDROID__) && defined(__aarch64__)
867 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
868 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_ASIMD) != 0))
870#elif defined(__ANDROID__) && defined(__arm__)
871 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
872 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0))
874#elif defined(__linux__) && defined(__aarch64__)
875 if ((getauxval(AT_HWCAP) & HWCAP_ASIMD) != 0)
877#elif defined(__linux__) && defined(__aarch32__)
878 if ((getauxval(AT_HWCAP2) & HWCAP2_ASIMD) != 0)
880#elif defined(__linux__) && defined(__arm__)
881 if ((getauxval(AT_HWCAP) & HWCAP_NEON) != 0)
883#elif defined(__APPLE__) && defined(__aarch64__)
885 if (IsAppleMachineARMv8())
887#elif defined(_WIN32) && defined(_M_ARM64)
889 if (IsProcessorFeaturePresent(PF_ARM_V8_INSTRUCTIONS_AVAILABLE) != 0)
895inline bool CPU_QueryCRC32()
897#if defined(__ANDROID__) && defined(__aarch64__)
898 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
899 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_CRC32) != 0))
901#elif defined(__ANDROID__) && defined(__aarch32__)
902 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
903 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_CRC32) != 0))
905#elif defined(__linux__) && defined(__aarch64__)
906 if ((getauxval(AT_HWCAP) & HWCAP_CRC32) != 0)
908#elif defined(__linux__) && defined(__aarch32__)
909 if ((getauxval(AT_HWCAP2) & HWCAP2_CRC32) != 0)
911#elif defined(__APPLE__) && defined(__aarch64__)
913 if (IsAppleMachineARMv82())
915#elif defined(_WIN32) && defined(_M_ARM64)
916 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) != 0)
922inline bool CPU_QueryPMULL()
924#if defined(__ANDROID__) && defined(__aarch64__)
925 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
926 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_PMULL) != 0))
928#elif defined(__ANDROID__) && defined(__aarch32__)
929 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
930 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_PMULL) != 0))
932#elif defined(__linux__) && defined(__aarch64__)
933 if ((getauxval(AT_HWCAP) & HWCAP_PMULL) != 0)
935#elif defined(__linux__) && defined(__aarch32__)
936 if ((getauxval(AT_HWCAP2) & HWCAP2_PMULL) != 0)
938#elif defined(__APPLE__) && defined(__aarch64__)
940 if (IsAppleMachineARMv82())
942#elif defined(_WIN32) && defined(_M_ARM64)
943 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0)
949inline bool CPU_QueryAES()
951#if defined(__ANDROID__) && defined(__aarch64__)
952 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
953 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_AES) != 0))
955#elif defined(__ANDROID__) && defined(__aarch32__)
956 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
957 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_AES) != 0))
959#elif defined(__linux__) && defined(__aarch64__)
960 if ((getauxval(AT_HWCAP) & HWCAP_AES) != 0)
962#elif defined(__linux__) && defined(__aarch32__)
963 if ((getauxval(AT_HWCAP2) & HWCAP2_AES) != 0)
965#elif defined(__APPLE__) && defined(__aarch64__)
967 if (IsAppleMachineARMv82())
969#elif defined(_WIN32) && defined(_M_ARM64)
970 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0)
976inline bool CPU_QuerySHA1()
978#if defined(__ANDROID__) && defined(__aarch64__)
979 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
980 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA1) != 0))
982#elif defined(__ANDROID__) && defined(__aarch32__)
983 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
984 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA1) != 0))
986#elif defined(__linux__) && defined(__aarch64__)
987 if ((getauxval(AT_HWCAP) & HWCAP_SHA1) != 0)
989#elif defined(__linux__) && defined(__aarch32__)
990 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA1) != 0)
992#elif defined(__APPLE__) && defined(__aarch64__)
994 if (IsAppleMachineARMv82())
996#elif defined(_WIN32) && defined(_M_ARM64)
997 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0)
1003inline bool CPU_QuerySHA256()
1005#if defined(__ANDROID__) && defined(__aarch64__)
1006 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
1007 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA2) != 0))
1009#elif defined(__ANDROID__) && defined(__aarch32__)
1010 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
1011 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA2) != 0))
1013#elif defined(__linux__) && defined(__aarch64__)
1014 if ((getauxval(AT_HWCAP) & HWCAP_SHA2) != 0)
1016#elif defined(__linux__) && defined(__aarch32__)
1017 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA2) != 0)
1019#elif defined(__APPLE__) && defined(__aarch64__)
1021 if (IsAppleMachineARMv82())
1023#elif defined(_WIN32) && defined(_M_ARM64)
1024 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0)
1031inline bool CPU_QuerySHA3()
1037 if (!g_hasSHA1 || !g_hasSHA2) {
return false; }
1039#if defined(__ANDROID__) && defined(__aarch64__) && 0
1040 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
1041 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA3) != 0))
1043#elif defined(__ANDROID__) && defined(__aarch32__) && 0
1044 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
1045 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA3) != 0))
1047#elif defined(__linux__) && defined(__aarch64__)
1048 if ((getauxval(AT_HWCAP) & HWCAP_SHA3) != 0)
1050#elif defined(__linux__) && defined(__aarch32__)
1051 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA3) != 0)
1053#elif defined(__APPLE__) && defined(__aarch64__)
1055 if (IsAppleMachineARMv82())
1062inline bool CPU_QuerySHA512()
1068 if (!g_hasSHA1 || !g_hasSHA2) {
return false; }
1070#if defined(__ANDROID__) && defined(__aarch64__) && 0
1071 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
1072 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA512) != 0))
1074#elif defined(__ANDROID__) && defined(__aarch32__) && 0
1075 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
1076 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA512) != 0))
1078#elif defined(__linux__) && defined(__aarch64__)
1079 if ((getauxval(AT_HWCAP) & HWCAP_SHA512) != 0)
1081#elif defined(__linux__) && defined(__aarch32__)
1082 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA512) != 0)
1084#elif defined(__APPLE__) && defined(__aarch64__)
1086 if (IsAppleMachineARMv82())
1093inline bool CPU_QuerySM3()
1095#if defined(__ANDROID__) && defined(__aarch64__) && 0
1096 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
1097 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SM3) != 0))
1099#elif defined(__ANDROID__) && defined(__aarch32__) && 0
1100 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
1101 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SM3) != 0))
1103#elif defined(__linux__) && defined(__aarch64__)
1104 if ((getauxval(AT_HWCAP) & HWCAP_SM3) != 0)
1106#elif defined(__linux__) && defined(__aarch32__)
1107 if ((getauxval(AT_HWCAP2) & HWCAP2_SM3) != 0)
1109#elif defined(__APPLE__) && defined(__aarch64__) && 0
1116inline bool CPU_QuerySM4()
1118#if defined(__ANDROID__) && defined(__aarch64__) && 0
1119 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
1120 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SM4) != 0))
1122#elif defined(__ANDROID__) && defined(__aarch32__) && 0
1123 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
1124 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SM4) != 0))
1126#elif defined(__linux__) && defined(__aarch64__)
1127 if ((getauxval(AT_HWCAP) & HWCAP_SM4) != 0)
1129#elif defined(__linux__) && defined(__aarch32__)
1130 if ((getauxval(AT_HWCAP2) & HWCAP2_SM4) != 0)
1132#elif defined(__APPLE__) && defined(__aarch64__) && 0
1138void DetectArmFeatures()
1140#ifndef CRYPTOPP_DISABLE_ASM
1144 g_hasARMv7 = CPU_QueryARMv7() || CPU_ProbeARMv7();
1145 g_hasNEON = CPU_QueryNEON() || CPU_ProbeNEON();
1146 g_hasCRC32 = CPU_QueryCRC32() || CPU_ProbeCRC32();
1147 g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
1148 g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
1149 g_hasSHA1 = CPU_QuerySHA1() || CPU_ProbeSHA1();
1150 g_hasSHA2 = CPU_QuerySHA256() || CPU_ProbeSHA256();
1151 g_hasSHA512 = CPU_QuerySHA512();
1152 g_hasSHA3 = CPU_QuerySHA3();
1153 g_hasSM3 = CPU_QuerySM3();
1154 g_hasSM4 = CPU_QuerySM4();
1156#if defined(_SC_LEVEL1_DCACHE_LINESIZE)
1159 int cacheLineSize = (int)sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
1160 if (cacheLineSize > 0)
1161 g_cacheLineSize = cacheLineSize;
1164 if (g_cacheLineSize == 0)
1169 *
const_cast<volatile bool*
>(&g_ArmDetectionDone) =
true;
1174#elif (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
1188extern bool CPU_ProbeAltivec();
1189extern bool CPU_ProbePower7();
1190extern bool CPU_ProbePower8();
1191extern bool CPU_ProbePower9();
1192extern bool CPU_ProbeAES();
1193extern bool CPU_ProbePMULL();
1194extern bool CPU_ProbeSHA256();
1195extern bool CPU_ProbeSHA512();
1196extern bool CPU_ProbeDARN();
1202#ifndef __power_6_andup
1203# define __power_6_andup() __power_set(0xffffffffU<<14)
1205#ifndef __power_7_andup
1206# define __power_7_andup() __power_set(0xffffffffU<<15)
1208#ifndef __power_8_andup
1209# define __power_8_andup() __power_set(0xffffffffU<<16)
1211#ifndef __power_9_andup
1212# define __power_9_andup() __power_set(0xffffffffU<<17)
1217inline bool CPU_QueryAltivec()
1219#if defined(__linux__) && defined(PPC_FEATURE_HAS_ALTIVEC)
1220 if ((getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC) != 0)
1223 if (__power_6_andup() != 0)
1225#elif defined(__APPLE__) && defined(__POWERPC__)
1226 unsigned int unused, arch;
1227 GetAppleMachineInfo(unused, unused, arch);
1228 return arch == AppleMachineInfo::PowerMac;
1229#elif defined(__FreeBSD__) && defined(PPC_FEATURE_HAS_ALTIVEC)
1230 unsigned long cpufeatures;
1231 if (elf_aux_info(AT_HWCAP, &cpufeatures,
sizeof(cpufeatures)) == 0)
1232 if ((cpufeatures & PPC_FEATURE_HAS_ALTIVEC) != 0)
1238inline bool CPU_QueryPower7()
1241#if defined(__linux__) && defined(PPC_FEATURE_ARCH_2_06)
1242 if ((getauxval(AT_HWCAP) & PPC_FEATURE_ARCH_2_06) != 0)
1245 if (__power_7_andup() != 0)
1247#elif defined(__FreeBSD__) && defined(PPC_FEATURE_ARCH_2_06)
1248 unsigned long cpufeatures;
1249 if (elf_aux_info(AT_HWCAP, &cpufeatures,
sizeof(cpufeatures)) == 0)
1250 if ((cpufeatures & PPC_FEATURE_ARCH_2_06) != 0)
1256inline bool CPU_QueryPower8()
1259#if defined(__linux__) && defined(PPC_FEATURE2_ARCH_2_07)
1260 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_2_07) != 0)
1263 if (__power_8_andup() != 0)
1265#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_ARCH_2_07)
1266 unsigned long cpufeatures;
1267 if (elf_aux_info(AT_HWCAP, &cpufeatures,
sizeof(cpufeatures)) == 0)
1268 if ((cpufeatures & PPC_FEATURE_ARCH_2_07) != 0)
1274inline bool CPU_QueryPower9()
1277#if defined(__linux__) && defined(PPC_FEATURE2_ARCH_3_00)
1278 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_3_00) != 0)
1281 if (__power_9_andup() != 0)
1283#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_ARCH_3_00)
1284 unsigned long cpufeatures;
1285 if (elf_aux_info(AT_HWCAP, &cpufeatures,
sizeof(cpufeatures)) == 0)
1286 if ((cpufeatures & PPC_FEATURE_ARCH2_3_00) != 0)
1292inline bool CPU_QueryAES()
1296#if defined(__linux__) && defined(PPC_FEATURE2_VEC_CRYPTO)
1297 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
1300 if (__power_8_andup() != 0)
1302#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_HAS_VEC_CRYPTO)
1303 unsigned long cpufeatures;
1304 if (elf_aux_info(AT_HWCAP2, &cpufeatures,
sizeof(cpufeatures)) == 0)
1305 if ((cpufeatures & PPC_FEATURE2_HAS_VEC_CRYPTO != 0)
1311inline bool CPU_QueryPMULL()
1315#if defined(__linux__) && defined(PPC_FEATURE2_VEC_CRYPTO)
1316 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
1319 if (__power_8_andup() != 0)
1321#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_HAS_VEC_CRYPTO)
1322 unsigned long cpufeatures;
1323 if (elf_aux_info(AT_HWCAP2, &cpufeatures,
sizeof(cpufeatures)) == 0)
1324 if ((cpufeatures & PPC_FEATURE2_HAS_VEC_CRYPTO != 0)
1330inline bool CPU_QuerySHA256()
1334#if defined(__linux__) && defined(PPC_FEATURE2_VEC_CRYPTO)
1335 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
1338 if (__power_8_andup() != 0)
1340#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_HAS_VEC_CRYPTO)
1341 unsigned long cpufeatures;
1342 if (elf_aux_info(AT_HWCAP2, &cpufeatures,
sizeof(cpufeatures)) == 0)
1343 if ((cpufeatures & PPC_FEATURE2_HAS_VEC_CRYPTO != 0)
1348inline bool CPU_QuerySHA512()
1352#if defined(__linux__) && defined(PPC_FEATURE2_VEC_CRYPTO)
1353 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
1356 if (__power_8_andup() != 0)
1358#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_HAS_VEC_CRYPTO)
1359 unsigned long cpufeatures;
1360 if (elf_aux_info(AT_HWCAP2, &cpufeatures,
sizeof(cpufeatures)) == 0)
1361 if ((cpufeatures & PPC_FEATURE2_HAS_VEC_CRYPTO != 0)
1368inline bool CPU_QueryDARN()
1372#if defined(__linux__) && defined(PPC_FEATURE2_ARCH_3_00)
1373 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_3_00) != 0)
1376 if (__power_9_andup() != 0)
1378#elif defined(__FreeBSD__) && defined(PPC_FEATURE2_ARCH_3_00)
1379 unsigned long cpufeatures;
1380 if (elf_aux_info(AT_HWCAP2, &cpufeatures,
sizeof(cpufeatures)) == 0)
1381 if ((cpufeatures & PPC_FEATURE2_ARCH_3_00) != 0)
1387void DetectPowerPcFeatures()
1395#ifndef CRYPTOPP_DISABLE_ASM
1399 g_hasAltivec = CPU_QueryAltivec() || CPU_ProbeAltivec();
1400 g_hasPower7 = CPU_QueryPower7() || CPU_ProbePower7();
1401 g_hasPower8 = CPU_QueryPower8() || CPU_ProbePower8();
1402 g_hasPower9 = CPU_QueryPower9() || CPU_ProbePower9();
1403 g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
1404 g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
1405 g_hasSHA256 = CPU_QuerySHA256() || CPU_ProbeSHA256();
1406 g_hasSHA512 = CPU_QuerySHA512() || CPU_ProbeSHA512();
1407 g_hasDARN = CPU_QueryDARN() || CPU_ProbeDARN();
1409#if defined(_AIX) && defined(SC_L1C_DLS)
1411 int cacheLineSize = getsystemcfg(SC_L1C_DLS);
1412 if (cacheLineSize > 0)
1413 g_cacheLineSize = cacheLineSize;
1414#elif defined(_SC_LEVEL1_DCACHE_LINESIZE)
1417 int cacheLineSize = (int)sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
1418 if (cacheLineSize > 0)
1419 g_cacheLineSize = cacheLineSize;
1422 if (g_cacheLineSize == 0)
1427 *
const_cast<volatile bool*
>(&g_PowerPcDetectionDone) =
true;
1435ANONYMOUS_NAMESPACE_BEGIN
1442#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
1443 CryptoPP::DetectX86Features();
1444#elif CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8
1445 CryptoPP::DetectArmFeatures();
1446#elif CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
1447 CryptoPP::DetectPowerPcFeatures();
1456#if HAVE_GCC_INIT_PRIORITY
1457 const InitCpu s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = InitCpu();
1458#elif HAVE_MSC_INIT_PRIORITY
1459 #pragma warning(disable: 4075)
1460 #pragma init_seg(".CRT$XCU")
1461 const InitCpu s_init;
1462 #pragma warning(default: 4075)
1463#elif HAVE_XLC_INIT_PRIORITY
1465 #pragma priority(270)
1466 const InitCpu s_init;
1468 const InitCpu s_init;
1471ANONYMOUS_NAMESPACE_END
Restricts the instantiation of a class to one static object without locks.
const T & Ref(...) const
Return a reference to the inner Singleton object.
Library configuration file.
#define CRYPTOPP_L1_CACHE_LINE_SIZE
L1 data cache line size.
#define CRYPTOPP_SECTION_INIT
Initialized data section.
unsigned int word32
32-bit unsigned datatype
unsigned long long word64
64-bit unsigned datatype
Functions for CPU features and intrinsics.
Utility functions for the Crypto++ library.
Crypto++ library namespace.
const char * Version()
int
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.