2017-01-27 12:05:45 +00:00
|
|
|
// cpu.cpp - originally written and placed in the public domain by Wei Dai
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifndef EXCEPTION_EXECUTE_HANDLER
|
|
|
|
# define EXCEPTION_EXECUTE_HANDLER 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CRYPTOPP_IMPORTS
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "misc.h"
|
2017-08-17 06:15:42 +00:00
|
|
|
#include "stdcpp.h"
|
|
|
|
|
2017-09-11 07:05:04 +00:00
|
|
|
#ifdef _AIX
|
|
|
|
# include <sys/systemcfg.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-17 06:39:41 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-13 00:14:21 +00:00
|
|
|
// Capability queries, requires Glibc 2.16, http://lwn.net/Articles/519085/
|
2017-09-17 04:41:48 +00:00
|
|
|
// CRYPTOPP_GLIBC_VERSION not used because config.h is missing <feature.h>
|
|
|
|
#if (((__GLIBC__ * 100) + __GLIBC_MINOR__) >= 216)
|
|
|
|
# define CRYPTOPP_GETAUXV_AVAILABLE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CRYPTOPP_GETAUXV_AVAILABLE
|
2017-09-13 11:16:41 +00:00
|
|
|
# include <sys/auxv.h>
|
2017-09-17 04:41:48 +00:00
|
|
|
#else
|
2018-03-04 14:17:03 +00:00
|
|
|
#ifndef AT_HWCAP
|
2018-03-21 20:19:43 +00:00
|
|
|
# define AT_HWCAP 16
|
2018-03-04 14:17:03 +00:00
|
|
|
#endif
|
|
|
|
#ifndef AT_HWCAP2
|
2018-03-21 20:19:43 +00:00
|
|
|
# define AT_HWCAP2 26
|
2018-03-04 14:17:03 +00:00
|
|
|
#endif
|
2017-09-17 04:41:48 +00:00
|
|
|
unsigned long int getauxval(unsigned long int) { return 0; }
|
2017-09-13 11:16:41 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-13 16:49:35 +00:00
|
|
|
#if defined(__APPLE__) && (defined(__aarch64__) || defined(__POWERPC__))
|
2017-09-13 11:16:41 +00:00
|
|
|
# include <sys/utsname.h>
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-17 06:39:41 +00:00
|
|
|
// The cpu-features header and source file are located in $ANDROID_NDK_ROOT/sources/android/cpufeatures
|
2017-09-13 11:16:41 +00:00
|
|
|
// setenv-android.sh will copy the header and source file into PWD and the makefile will build it in place.
|
2017-09-08 19:43:06 +00:00
|
|
|
#if defined(__ANDROID__)
|
2017-09-13 11:16:41 +00:00
|
|
|
# include "cpu-features.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
|
|
|
|
# include <signal.h>
|
|
|
|
# include <setjmp.h>
|
2017-09-08 19:43:06 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2016-05-16 22:47:43 +00:00
|
|
|
#ifndef CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
|
|
|
|
extern "C" {
|
|
|
|
typedef void (*SigHandler)(int);
|
2018-03-31 17:10:11 +00:00
|
|
|
}
|
2017-11-17 00:37:29 +00:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
static jmp_buf s_jmpNoCPUID;
|
2018-01-25 01:04:16 +00:00
|
|
|
static void SigIllHandlerCPUID(int unused)
|
2017-11-17 00:37:29 +00:00
|
|
|
{
|
2018-01-25 01:04:16 +00:00
|
|
|
CRYPTOPP_UNUSED(unused);
|
2017-11-17 00:37:29 +00:00
|
|
|
longjmp(s_jmpNoCPUID, 1);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 22:47:43 +00:00
|
|
|
#endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
|
|
|
|
|
2017-08-18 07:03:42 +00:00
|
|
|
// *************************** IA-32 CPUs ***************************
|
2017-08-17 16:33:43 +00:00
|
|
|
|
2017-11-16 20:11:51 +00:00
|
|
|
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
|
|
|
|
|
|
|
|
extern bool CPU_ProbeSSE2();
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2018-03-28 00:38:26 +00:00
|
|
|
#if _MSC_VER >= 1600
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CpuId(word32 func, word32 subfunc, word32 output[4])
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-08-17 16:33:43 +00:00
|
|
|
__cpuidex((int *)output, func, subfunc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif _MSC_VER >= 1400 && CRYPTOPP_BOOL_X64
|
|
|
|
|
|
|
|
inline bool CpuId(word32 func, word32 subfunc, word32 output[4])
|
|
|
|
{
|
|
|
|
if (subfunc != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
__cpuid((int *)output, func);
|
2015-11-05 06:59:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-09-16 22:03:24 +00:00
|
|
|
// Borland/Embarcadero and Issue 498
|
2017-09-15 16:42:50 +00:00
|
|
|
// cpu.cpp (131): E2211 Inline assembly not allowed in inline and template functions
|
|
|
|
bool CpuId(word32 func, word32 subfunc, word32 output[4])
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-10-13 00:14:21 +00:00
|
|
|
#if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) || defined(__BORLANDC__)
|
2015-11-05 06:59:46 +00:00
|
|
|
__try
|
|
|
|
{
|
2017-09-16 22:03:24 +00:00
|
|
|
// Borland/Embarcadero and Issue 500
|
|
|
|
// Local variables for cpuid output
|
|
|
|
word32 a, b, c, d;
|
2015-11-05 06:59:46 +00:00
|
|
|
__asm
|
|
|
|
{
|
2017-08-17 16:33:43 +00:00
|
|
|
mov eax, func
|
|
|
|
mov ecx, subfunc
|
2015-11-05 06:59:46 +00:00
|
|
|
cpuid
|
2017-09-16 22:03:24 +00:00
|
|
|
mov [a], eax
|
|
|
|
mov [b], ebx
|
|
|
|
mov [c], ecx
|
|
|
|
mov [d], edx
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
2017-09-16 22:03:24 +00:00
|
|
|
output[0] = a;
|
|
|
|
output[1] = b;
|
|
|
|
output[2] = c;
|
|
|
|
output[3] = d;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
// GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:55:57 +00:00
|
|
|
// func = 0 returns the highest basic function understood in EAX. If the CPU does
|
|
|
|
// not return non-0, then it is mostly useless. The code below converts basic
|
|
|
|
// function value to a true/false return value.
|
2017-09-16 04:19:14 +00:00
|
|
|
if(func == 0)
|
2018-01-25 01:04:16 +00:00
|
|
|
return output[0] != 0;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
2015-11-18 20:32:28 +00:00
|
|
|
// longjmp and clobber warnings. Volatile is required.
|
2016-05-19 08:45:35 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
|
2015-11-18 20:32:28 +00:00
|
|
|
volatile bool result = true;
|
|
|
|
|
2016-05-30 07:08:53 +00:00
|
|
|
volatile SigHandler oldHandler = signal(SIGILL, SigIllHandlerCPUID);
|
2015-11-05 06:59:46 +00:00
|
|
|
if (oldHandler == SIG_ERR)
|
2016-06-01 05:27:53 +00:00
|
|
|
return false;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2016-08-01 19:56:43 +00:00
|
|
|
# ifndef __MINGW32__
|
2016-05-30 07:08:53 +00:00
|
|
|
volatile sigset_t oldMask;
|
2018-01-25 01:04:16 +00:00
|
|
|
if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask) != 0)
|
2016-06-01 05:27:53 +00:00
|
|
|
return false;
|
2016-08-01 19:56:43 +00:00
|
|
|
# endif
|
2016-05-30 07:08:53 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if (setjmp(s_jmpNoCPUID))
|
|
|
|
result = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asm volatile
|
|
|
|
(
|
|
|
|
// save ebx in case -fPIC is being used
|
|
|
|
// TODO: this might need an early clobber on EDI.
|
|
|
|
# if CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
|
|
|
|
"pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"
|
|
|
|
# else
|
|
|
|
"push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"
|
|
|
|
# endif
|
|
|
|
: "=a" (output[0]), "=D" (output[1]), "=c" (output[2]), "=d" (output[3])
|
2017-08-17 16:33:43 +00:00
|
|
|
: "a" (func), "c" (subfunc)
|
2016-11-13 16:14:03 +00:00
|
|
|
: "cc"
|
2015-11-05 06:59:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-01 19:56:43 +00:00
|
|
|
# ifndef __MINGW32__
|
2017-03-01 11:10:06 +00:00
|
|
|
sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
|
2016-08-01 19:56:43 +00:00
|
|
|
# endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
signal(SIGILL, oldHandler);
|
|
|
|
return result;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-07-13 03:05:00 +00:00
|
|
|
bool CRYPTOPP_SECTION_INIT g_x86DetectionDone = false;
|
2018-03-28 03:19:26 +00:00
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSSE2 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSSSE3 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSSE41 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSSE42 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasAESNI = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasCLMUL = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasADX = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSHA = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasRDRAND = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasRDSEED = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_isP4 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPadlockRNG = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPadlockACE = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPadlockACE2 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPadlockPHE = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPadlockPMM = false;
|
2016-07-13 03:05:00 +00:00
|
|
|
word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2015-11-18 20:32:28 +00:00
|
|
|
static inline bool IsIntel(const word32 output[4])
|
|
|
|
{
|
|
|
|
// This is the "GenuineIntel" string
|
|
|
|
return (output[1] /*EBX*/ == 0x756e6547) &&
|
|
|
|
(output[2] /*ECX*/ == 0x6c65746e) &&
|
|
|
|
(output[3] /*EDX*/ == 0x49656e69);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsAMD(const word32 output[4])
|
|
|
|
{
|
2016-05-19 12:32:51 +00:00
|
|
|
// This is the "AuthenticAMD" string. Some early K5's can return "AMDisbetter!"
|
2015-11-18 20:32:28 +00:00
|
|
|
return (output[1] /*EBX*/ == 0x68747541) &&
|
2016-05-19 12:32:51 +00:00
|
|
|
(output[2] /*ECX*/ == 0x444D4163) &&
|
|
|
|
(output[3] /*EDX*/ == 0x69746E65);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsVIA(const word32 output[4])
|
|
|
|
{
|
2016-07-13 03:05:00 +00:00
|
|
|
// This is the "CentaurHauls" string. Some non-PadLock's can return "VIA VIA VIA "
|
2016-05-19 12:32:51 +00:00
|
|
|
return (output[1] /*EBX*/ == 0x746e6543) &&
|
|
|
|
(output[2] /*ECX*/ == 0x736c7561) &&
|
|
|
|
(output[3] /*EDX*/ == 0x48727561);
|
2015-11-18 20:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
void DetectX86Features()
|
|
|
|
{
|
2017-03-18 01:29:15 +00:00
|
|
|
// Coverity finding CID 171239...
|
2017-08-11 21:13:15 +00:00
|
|
|
word32 cpuid0[4]={0}, cpuid1[4]={0}, cpuid2[4]={0};
|
2017-08-17 16:33:43 +00:00
|
|
|
if (!CpuId(0, 0, cpuid0))
|
2015-11-05 06:59:46 +00:00
|
|
|
return;
|
2017-08-17 16:33:43 +00:00
|
|
|
if (!CpuId(1, 0, cpuid1))
|
2015-11-05 06:59:46 +00:00
|
|
|
return;
|
|
|
|
|
2017-10-13 00:14:21 +00:00
|
|
|
// cpuid1[2] & (1 << 27) is XSAVE/XRESTORE and signals OS support for SSE; use it to avoid probes.
|
|
|
|
// See http://github.com/weidai11/cryptopp/issues/511 and http://stackoverflow.com/a/22521619/608639
|
2017-08-11 21:13:15 +00:00
|
|
|
if ((cpuid1[3] & (1 << 26)) != 0)
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasSSE2 = ((cpuid1[2] & (1 << 27)) != 0) || CPU_ProbeSSE2();
|
2017-10-13 00:14:21 +00:00
|
|
|
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasSSSE3 = g_hasSSE2 && ((cpuid1[2] & (1<< 9)) != 0);
|
|
|
|
g_hasSSE41 = g_hasSSE2 && ((cpuid1[2] & (1<<19)) != 0);
|
|
|
|
g_hasSSE42 = g_hasSSE2 && ((cpuid1[2] & (1<<20)) != 0);
|
|
|
|
g_hasAESNI = g_hasSSE2 && ((cpuid1[2] & (1<<25)) != 0);
|
|
|
|
g_hasCLMUL = g_hasSSE2 && ((cpuid1[2] & (1<< 1)) != 0);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-08-11 21:13:15 +00:00
|
|
|
if (IsIntel(cpuid0))
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-09-16 05:21:14 +00:00
|
|
|
CRYPTOPP_CONSTANT(RDRAND_FLAG = (1 << 30))
|
|
|
|
CRYPTOPP_CONSTANT(RDSEED_FLAG = (1 << 18))
|
2017-11-16 07:38:53 +00:00
|
|
|
CRYPTOPP_CONSTANT( ADX_FLAG = (1 << 19))
|
2017-09-16 05:21:14 +00:00
|
|
|
CRYPTOPP_CONSTANT( SHA_FLAG = (1 << 29))
|
2016-05-19 12:32:51 +00:00
|
|
|
|
2017-08-11 21:13:15 +00:00
|
|
|
g_isP4 = ((cpuid1[0] >> 8) & 0xf) == 0xf;
|
|
|
|
g_cacheLineSize = 8 * GETBYTE(cpuid1[1], 1);
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasRDRAND = (cpuid1[2] /*ECX*/ & RDRAND_FLAG) != 0;
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2018-01-11 22:59:24 +00:00
|
|
|
if (cpuid0[0] /*EAX*/ >= 7)
|
2015-11-18 20:32:28 +00:00
|
|
|
{
|
2017-08-17 16:33:43 +00:00
|
|
|
if (CpuId(7, 0, cpuid2))
|
2016-10-27 02:35:24 +00:00
|
|
|
{
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasRDSEED = (cpuid2[1] /*EBX*/ & RDSEED_FLAG) != 0;
|
|
|
|
g_hasADX = (cpuid2[1] /*EBX*/ & ADX_FLAG) != 0;
|
|
|
|
g_hasSHA = (cpuid2[1] /*EBX*/ & SHA_FLAG) != 0;
|
2016-10-27 02:35:24 +00:00
|
|
|
}
|
2015-11-18 20:32:28 +00:00
|
|
|
}
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
2017-08-11 21:13:15 +00:00
|
|
|
else if (IsAMD(cpuid0))
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-09-16 05:21:14 +00:00
|
|
|
CRYPTOPP_CONSTANT(RDRAND_FLAG = (1 << 30))
|
|
|
|
CRYPTOPP_CONSTANT(RDSEED_FLAG = (1 << 18))
|
2017-11-16 07:38:53 +00:00
|
|
|
CRYPTOPP_CONSTANT( ADX_FLAG = (1 << 19))
|
2017-09-16 05:21:14 +00:00
|
|
|
CRYPTOPP_CONSTANT( SHA_FLAG = (1 << 29))
|
2016-05-19 12:32:51 +00:00
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
CpuId(0x80000005, 0, cpuid2);
|
2017-08-11 21:13:15 +00:00
|
|
|
g_cacheLineSize = GETBYTE(cpuid2[2], 0);
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasRDRAND = (cpuid1[2] /*ECX*/ & RDRAND_FLAG) != 0;
|
2016-05-19 12:32:51 +00:00
|
|
|
|
2018-01-11 22:59:24 +00:00
|
|
|
if (cpuid0[0] /*EAX*/ >= 7)
|
2017-05-21 15:38:56 +00:00
|
|
|
{
|
2017-08-17 16:33:43 +00:00
|
|
|
if (CpuId(7, 0, cpuid2))
|
2017-05-21 15:38:56 +00:00
|
|
|
{
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasRDSEED = (cpuid2[1] /*EBX*/ & RDSEED_FLAG) != 0;
|
|
|
|
g_hasADX = (cpuid2[1] /*EBX*/ & ADX_FLAG) != 0;
|
|
|
|
g_hasSHA = (cpuid2[1] /*EBX*/ & SHA_FLAG) != 0;
|
2017-05-21 15:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-19 12:32:51 +00:00
|
|
|
}
|
2017-08-11 21:13:15 +00:00
|
|
|
else if (IsVIA(cpuid0))
|
2016-05-19 12:32:51 +00:00
|
|
|
{
|
2017-09-16 05:21:14 +00:00
|
|
|
CRYPTOPP_CONSTANT( RNG_FLAGS = (0x3 << 2))
|
|
|
|
CRYPTOPP_CONSTANT( ACE_FLAGS = (0x3 << 6))
|
|
|
|
CRYPTOPP_CONSTANT(ACE2_FLAGS = (0x3 << 8))
|
|
|
|
CRYPTOPP_CONSTANT( PHE_FLAGS = (0x3 << 10))
|
|
|
|
CRYPTOPP_CONSTANT( PMM_FLAGS = (0x3 << 12))
|
2017-08-11 21:13:15 +00:00
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
CpuId(0xC0000000, 0, cpuid2);
|
|
|
|
if (cpuid2[0] >= 0xC0000001)
|
2016-05-19 12:32:51 +00:00
|
|
|
{
|
|
|
|
// Extended features available
|
2017-08-17 16:33:43 +00:00
|
|
|
CpuId(0xC0000001, 0, cpuid2);
|
2018-01-25 01:04:16 +00:00
|
|
|
g_hasPadlockRNG = (cpuid2[3] /*EDX*/ & RNG_FLAGS) != 0;
|
|
|
|
g_hasPadlockACE = (cpuid2[3] /*EDX*/ & ACE_FLAGS) != 0;
|
|
|
|
g_hasPadlockACE2 = (cpuid2[3] /*EDX*/ & ACE2_FLAGS) != 0;
|
|
|
|
g_hasPadlockPHE = (cpuid2[3] /*EDX*/ & PHE_FLAGS) != 0;
|
|
|
|
g_hasPadlockPMM = (cpuid2[3] /*EDX*/ & PMM_FLAGS) != 0;
|
2016-05-19 12:32:51 +00:00
|
|
|
}
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 20:19:43 +00:00
|
|
|
if (g_cacheLineSize == 0)
|
2015-11-05 06:59:46 +00:00
|
|
|
g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
|
|
|
|
|
2017-11-17 00:37:29 +00:00
|
|
|
*const_cast<volatile bool*>(&g_x86DetectionDone) = true;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 07:05:04 +00:00
|
|
|
// *************************** ARM-32, Aarch32 and Aarch64 ***************************
|
2017-08-17 16:33:43 +00:00
|
|
|
|
2016-05-15 06:30:08 +00:00
|
|
|
#elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64)
|
|
|
|
|
2016-07-13 03:05:00 +00:00
|
|
|
bool CRYPTOPP_SECTION_INIT g_ArmDetectionDone = false;
|
2018-03-28 03:19:26 +00:00
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasNEON = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPMULL = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasCRC32 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasAES = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSHA1 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSHA2 = false;
|
2016-07-13 03:05:00 +00:00
|
|
|
word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
|
2016-05-15 06:30:08 +00:00
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
// ARM does not have an unprivliged equivalent to CPUID on IA-32. We have to jump through some
|
|
|
|
// hoops to detect features on a wide array of platforms. Our strategy is two part. First,
|
|
|
|
// attempt to *Query* the OS for a feature, like using getauxval on Linux. If that fails,
|
|
|
|
// then *Probe* the cpu executing an instruction and an observe a SIGILL if unsupported.
|
|
|
|
// The probes are in source files where compilation options like -march=armv8-a+crc make
|
|
|
|
// intrinsics available. They are expensive when compared to a standard OS feature query.
|
|
|
|
// Always perform the feature quesry first. For Linux see
|
|
|
|
// http://sourceware.org/ml/libc-help/2017-08/msg00012.html
|
|
|
|
// Avoid probes on Apple platforms because Apple's signal handling for SIGILLs appears broken.
|
|
|
|
// We are trying to figure out a way to feature test without probes. Also see
|
|
|
|
// http://stackoverflow.com/a/11197770/608639 and
|
|
|
|
// http://gist.github.com/erkanyildiz/390a480f27e86f8cd6ba
|
|
|
|
|
|
|
|
extern bool CPU_ProbeNEON();
|
|
|
|
extern bool CPU_ProbeCRC32();
|
|
|
|
extern bool CPU_ProbeAES();
|
|
|
|
extern bool CPU_ProbeSHA1();
|
|
|
|
extern bool CPU_ProbeSHA2();
|
|
|
|
extern bool CPU_ProbePMULL();
|
|
|
|
|
2017-09-17 04:41:48 +00:00
|
|
|
#ifndef HWCAP_ASIMD
|
|
|
|
# define HWCAP_ASIMD (1 << 1)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP_ARM_NEON
|
|
|
|
# define HWCAP_ARM_NEON 4096
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP_CRC32
|
|
|
|
# define HWCAP_CRC32 (1 << 7)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP2_CRC32
|
|
|
|
# define HWCAP2_CRC32 (1 << 4)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP_PMULL
|
|
|
|
# define HWCAP_PMULL (1 << 4)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP2_PMULL
|
|
|
|
# define HWCAP2_PMULL (1 << 1)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP_AES
|
|
|
|
# define HWCAP_AES (1 << 3)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP2_AES
|
|
|
|
# define HWCAP2_AES (1 << 0)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP_SHA1
|
|
|
|
# define HWCAP_SHA1 (1 << 5)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP_SHA2
|
|
|
|
# define HWCAP_SHA2 (1 << 6)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP2_SHA1
|
|
|
|
# define HWCAP2_SHA1 (1 << 2)
|
|
|
|
#endif
|
|
|
|
#ifndef HWCAP2_SHA2
|
|
|
|
# define HWCAP2_SHA2 (1 << 3)
|
|
|
|
#endif
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CPU_QueryNEON()
|
2016-05-15 06:30:08 +00:00
|
|
|
{
|
2017-09-13 11:16:41 +00:00
|
|
|
#if defined(__ANDROID__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_ASIMD) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__ANDROID__) && defined(__arm__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_ASIMD) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & HWCAP2_ASIMD) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__arm__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_ARM_NEON) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
2017-08-18 07:03:42 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__aarch64__)
|
|
|
|
// Core feature set for Aarch32 and Aarch64.
|
|
|
|
return true;
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
2016-05-16 22:47:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CPU_QueryCRC32()
|
2016-07-24 13:48:28 +00:00
|
|
|
{
|
2017-09-13 11:16:41 +00:00
|
|
|
#if defined(__ANDROID__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_CRC32) != 0))
|
2017-09-13 11:16:41 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__ANDROID__) && defined(__aarch32__)
|
2018-03-22 04:20:45 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
|
2018-03-21 20:19:43 +00:00
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_CRC32) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_CRC32) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & HWCAP2_CRC32) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
2017-08-18 07:03:42 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__aarch64__)
|
|
|
|
// No compiler support. CRC intrinsics result in a failed compiled.
|
|
|
|
return false;
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
2016-07-24 13:48:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CPU_QueryPMULL()
|
2016-05-16 22:47:43 +00:00
|
|
|
{
|
2017-09-13 11:16:41 +00:00
|
|
|
#if defined(__ANDROID__) && defined(__aarch64__)
|
2018-03-22 04:20:45 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
|
2018-03-21 20:19:43 +00:00
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_PMULL) != 0))
|
2017-09-13 11:16:41 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__ANDROID__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_PMULL) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_PMULL) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & HWCAP2_PMULL) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
2017-08-18 07:03:42 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__aarch64__)
|
|
|
|
// No compiler support. PMULL intrinsics result in a failed compiled.
|
|
|
|
return false;
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
2016-05-16 22:47:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CPU_QueryAES()
|
2016-05-16 22:47:43 +00:00
|
|
|
{
|
2017-09-13 11:16:41 +00:00
|
|
|
#if defined(__ANDROID__) && defined(__aarch64__)
|
2018-03-22 04:20:45 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
|
2018-03-21 20:19:43 +00:00
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_AES) != 0))
|
2017-09-13 11:16:41 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__ANDROID__) && defined(__aarch32__)
|
2018-03-22 04:20:45 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
|
2018-03-21 20:19:43 +00:00
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_AES) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_AES) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & HWCAP2_AES) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
2017-08-17 17:52:26 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__aarch64__)
|
2017-10-13 00:14:21 +00:00
|
|
|
// http://stackoverflow.com/questions/45637888/how-to-determine-armv8-features-at-runtime-on-ios
|
2017-08-17 16:33:43 +00:00
|
|
|
struct utsname systemInfo;
|
|
|
|
systemInfo.machine[0] = '\0';
|
|
|
|
uname(&systemInfo);
|
|
|
|
|
2017-08-18 07:03:42 +00:00
|
|
|
// The machine strings below are known ARM8 devices
|
2017-08-17 16:33:43 +00:00
|
|
|
std::string machine(systemInfo.machine);
|
|
|
|
if (machine.substr(0, 7) == "iPhone6" || machine.substr(0, 7) == "iPhone7" ||
|
|
|
|
machine.substr(0, 7) == "iPhone8" || machine.substr(0, 7) == "iPhone9" ||
|
|
|
|
machine.substr(0, 5) == "iPad4" || machine.substr(0, 5) == "iPad5" ||
|
|
|
|
machine.substr(0, 5) == "iPad6" || machine.substr(0, 5) == "iPad7")
|
|
|
|
{
|
|
|
|
return true;
|
2016-05-16 22:47:43 +00:00
|
|
|
}
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
2016-05-19 08:45:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CPU_QuerySHA1()
|
2016-05-19 08:45:35 +00:00
|
|
|
{
|
2017-09-13 11:16:41 +00:00
|
|
|
#if defined(__ANDROID__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA1) != 0))
|
2017-09-13 11:16:41 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__ANDROID__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA1) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_SHA1) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & HWCAP2_SHA1) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
2017-08-18 07:03:42 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__aarch64__)
|
2017-10-13 00:14:21 +00:00
|
|
|
// http://stackoverflow.com/questions/45637888/how-to-determine-armv8-features-at-runtime-on-ios
|
2017-08-18 07:03:42 +00:00
|
|
|
struct utsname systemInfo;
|
|
|
|
systemInfo.machine[0] = '\0';
|
|
|
|
uname(&systemInfo);
|
|
|
|
|
|
|
|
// The machine strings below are known ARM8 devices
|
|
|
|
std::string machine(systemInfo.machine);
|
|
|
|
if (machine.substr(0, 7) == "iPhone6" || machine.substr(0, 7) == "iPhone7" ||
|
|
|
|
machine.substr(0, 7) == "iPhone8" || machine.substr(0, 7) == "iPhone9" ||
|
|
|
|
machine.substr(0, 5) == "iPad4" || machine.substr(0, 5) == "iPad5" ||
|
|
|
|
machine.substr(0, 5) == "iPad6" || machine.substr(0, 5) == "iPad7")
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
2016-05-19 08:45:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
inline bool CPU_QuerySHA2()
|
2016-05-19 08:45:35 +00:00
|
|
|
{
|
2017-09-13 11:16:41 +00:00
|
|
|
#if defined(__ANDROID__) && defined(__aarch64__)
|
2018-03-22 04:20:45 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
|
2018-03-21 20:19:43 +00:00
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA2) != 0))
|
2017-09-13 11:16:41 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__ANDROID__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
|
|
|
|
((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA2) != 0))
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch64__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & HWCAP_SHA2) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(__linux__) && defined(__aarch32__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & HWCAP2_SHA2) != 0)
|
2017-08-17 16:33:43 +00:00
|
|
|
return true;
|
2017-08-18 07:03:42 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__aarch64__)
|
2017-10-13 00:14:21 +00:00
|
|
|
// http://stackoverflow.com/questions/45637888/how-to-determine-armv8-features-at-runtime-on-ios
|
2017-08-18 07:03:42 +00:00
|
|
|
struct utsname systemInfo;
|
|
|
|
systemInfo.machine[0] = '\0';
|
|
|
|
uname(&systemInfo);
|
|
|
|
|
|
|
|
// The machine strings below are known ARM8 devices
|
|
|
|
std::string machine(systemInfo.machine);
|
|
|
|
if (machine.substr(0, 7) == "iPhone6" || machine.substr(0, 7) == "iPhone7" ||
|
|
|
|
machine.substr(0, 7) == "iPhone8" || machine.substr(0, 7) == "iPhone9" ||
|
|
|
|
machine.substr(0, 5) == "iPad4" || machine.substr(0, 5) == "iPad5" ||
|
|
|
|
machine.substr(0, 5) == "iPad6" || machine.substr(0, 5) == "iPad7")
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
2016-05-16 22:47:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DetectArmFeatures()
|
|
|
|
{
|
2017-08-18 07:03:42 +00:00
|
|
|
// The CPU_ProbeXXX's return false for OSes which
|
|
|
|
// can't tolerate SIGILL-based probes
|
2017-08-17 16:33:43 +00:00
|
|
|
g_hasNEON = CPU_QueryNEON() || CPU_ProbeNEON();
|
|
|
|
g_hasCRC32 = CPU_QueryCRC32() || CPU_ProbeCRC32();
|
|
|
|
g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
|
|
|
|
g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
|
|
|
|
g_hasSHA1 = CPU_QuerySHA1() || CPU_ProbeSHA1();
|
|
|
|
g_hasSHA2 = CPU_QuerySHA2() || CPU_ProbeSHA2();
|
2016-05-16 22:47:43 +00:00
|
|
|
|
2017-09-17 18:34:05 +00:00
|
|
|
#if defined(__linux__) && defined(_SC_LEVEL1_DCACHE_LINESIZE)
|
2018-03-21 20:19:43 +00:00
|
|
|
// Glibc does not implement on some platforms. The runtime returns 0 instead of error.
|
|
|
|
// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/sysconf.c
|
|
|
|
int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
|
|
|
|
if (cacheLineSize > 0)
|
|
|
|
g_cacheLineSize = cacheLineSize;
|
2017-09-17 06:39:41 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-17 00:37:29 +00:00
|
|
|
*const_cast<volatile bool*>(&g_ArmDetectionDone) = true;
|
2016-05-15 06:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 07:05:04 +00:00
|
|
|
// *************************** PowerPC and PowerPC64 ***************************
|
|
|
|
|
|
|
|
#elif (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
|
|
|
|
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_PowerpcDetectionDone = false;
|
2018-03-28 03:19:26 +00:00
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasAltivec = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPower7 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasPower8 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasAES = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSHA256 = false;
|
|
|
|
bool CRYPTOPP_SECTION_INIT g_hasSHA512 = false;
|
2017-09-11 07:05:04 +00:00
|
|
|
word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
|
|
|
|
|
2017-09-12 02:39:59 +00:00
|
|
|
extern bool CPU_ProbeAltivec();
|
2017-10-18 02:50:45 +00:00
|
|
|
extern bool CPU_ProbePower7();
|
2017-09-12 02:39:59 +00:00
|
|
|
extern bool CPU_ProbePower8();
|
|
|
|
extern bool CPU_ProbeAES();
|
2017-09-22 12:58:50 +00:00
|
|
|
extern bool CPU_ProbeSHA256();
|
|
|
|
extern bool CPU_ProbeSHA512();
|
2017-09-11 07:05:04 +00:00
|
|
|
|
2017-09-17 04:41:48 +00:00
|
|
|
#ifndef PPC_FEATURE_HAS_ALTIVEC
|
|
|
|
# define PPC_FEATURE_HAS_ALTIVEC 0x10000000
|
|
|
|
#endif
|
|
|
|
#ifndef PPC_FEATURE_ARCH_2_06
|
|
|
|
# define PPC_FEATURE_ARCH_2_06 0x00000100
|
|
|
|
#endif
|
|
|
|
#ifndef PPC_FEATURE2_ARCH_2_07
|
|
|
|
# define PPC_FEATURE2_ARCH_2_07 0x80000000
|
|
|
|
#endif
|
|
|
|
#ifndef PPC_FEATURE2_VEC_CRYPTO
|
|
|
|
# define PPC_FEATURE2_VEC_CRYPTO 0x02000000
|
|
|
|
#endif
|
|
|
|
|
2017-09-11 07:05:04 +00:00
|
|
|
inline bool CPU_QueryAltivec()
|
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC) != 0)
|
2017-09-11 07:05:04 +00:00
|
|
|
return true;
|
2018-03-21 04:35:56 +00:00
|
|
|
#elif defined(_AIX)
|
|
|
|
if (__power_vmx() != 0)
|
|
|
|
return true;
|
2017-12-13 16:49:35 +00:00
|
|
|
#elif defined(__APPLE__) && defined(__POWERPC__)
|
|
|
|
// http://stackoverflow.com/questions/45637888/how-to-determine-armv8-features-at-runtime-on-ios
|
|
|
|
struct utsname systemInfo;
|
|
|
|
systemInfo.machine[0] = '\0';
|
|
|
|
uname(&systemInfo);
|
|
|
|
|
|
|
|
// The machine strings below are known PPC machines
|
|
|
|
std::string machine(systemInfo.machine);
|
|
|
|
if (machine.substr(0, 15) == "Power Macintosh")
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool CPU_QueryPower7()
|
|
|
|
{
|
|
|
|
// Power7 and ISA 2.06
|
|
|
|
#if defined(__linux__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP) & PPC_FEATURE_ARCH_2_06) != 0)
|
2018-03-21 03:52:10 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(_AIX)
|
2018-03-21 04:35:56 +00:00
|
|
|
if (__power_7_andup() != 0)
|
2017-09-11 07:05:04 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool CPU_QueryPower8()
|
|
|
|
{
|
2017-09-12 02:39:59 +00:00
|
|
|
// Power8 and ISA 2.07 provide in-core crypto.
|
2017-09-11 07:05:04 +00:00
|
|
|
#if defined(__linux__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_2_07) != 0)
|
2018-03-21 03:52:10 +00:00
|
|
|
return true;
|
|
|
|
#elif defined(_AIX)
|
|
|
|
if (__power_8_andup() != 0)
|
2017-09-11 07:05:04 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool CPU_QueryAES()
|
|
|
|
{
|
2017-09-12 02:39:59 +00:00
|
|
|
// Power8 and ISA 2.07 provide in-core crypto. Glibc
|
|
|
|
// 2.24 or higher is required for PPC_FEATURE2_VEC_CRYPTO.
|
2017-09-11 07:05:04 +00:00
|
|
|
#if defined(__linux__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
|
2017-09-11 07:05:04 +00:00
|
|
|
return true;
|
2018-03-21 03:52:10 +00:00
|
|
|
#elif defined(_AIX)
|
|
|
|
if (__power_8_andup() != 0)
|
|
|
|
return true;
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-22 12:58:50 +00:00
|
|
|
inline bool CPU_QuerySHA256()
|
2017-09-11 07:05:04 +00:00
|
|
|
{
|
2017-09-12 02:39:59 +00:00
|
|
|
// Power8 and ISA 2.07 provide in-core crypto. Glibc
|
|
|
|
// 2.24 or higher is required for PPC_FEATURE2_VEC_CRYPTO.
|
2017-09-11 07:05:04 +00:00
|
|
|
#if defined(__linux__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
|
2017-09-11 07:05:04 +00:00
|
|
|
return true;
|
2018-03-21 03:52:10 +00:00
|
|
|
#elif defined(_AIX)
|
|
|
|
if (__power_8_andup() != 0)
|
|
|
|
return true;
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-22 12:58:50 +00:00
|
|
|
inline bool CPU_QuerySHA512()
|
2017-09-11 07:05:04 +00:00
|
|
|
{
|
2017-09-12 02:39:59 +00:00
|
|
|
// Power8 and ISA 2.07 provide in-core crypto. Glibc
|
|
|
|
// 2.24 or higher is required for PPC_FEATURE2_VEC_CRYPTO.
|
2017-09-11 07:05:04 +00:00
|
|
|
#if defined(__linux__)
|
2018-03-21 20:19:43 +00:00
|
|
|
if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
|
2017-09-11 07:05:04 +00:00
|
|
|
return true;
|
2018-03-21 03:52:10 +00:00
|
|
|
#elif defined(_AIX)
|
|
|
|
if (__power_8_andup() != 0)
|
|
|
|
return true;
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DetectPowerpcFeatures()
|
|
|
|
{
|
|
|
|
// The CPU_ProbeXXX's return false for OSes which
|
|
|
|
// can't tolerate SIGILL-based probes, like Apple
|
|
|
|
g_hasAltivec = CPU_QueryAltivec() || CPU_ProbeAltivec();
|
2017-10-18 02:50:45 +00:00
|
|
|
g_hasPower7 = CPU_QueryPower7() || CPU_ProbePower7();
|
2017-09-11 07:05:04 +00:00
|
|
|
g_hasPower8 = CPU_QueryPower8() || CPU_ProbePower8();
|
|
|
|
//g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
|
|
|
|
g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
|
2017-09-22 12:58:50 +00:00
|
|
|
g_hasSHA256 = CPU_QuerySHA256() || CPU_ProbeSHA256();
|
|
|
|
g_hasSHA512 = CPU_QuerySHA512() || CPU_ProbeSHA512();
|
2017-09-11 07:05:04 +00:00
|
|
|
|
2018-03-21 20:19:43 +00:00
|
|
|
#if defined(_AIX) && defined(SC_L1C_DLS)
|
2017-09-11 07:05:04 +00:00
|
|
|
// /usr/include/sys/systemcfg.h
|
2018-03-21 20:19:43 +00:00
|
|
|
int cacheLineSize = getsystemcfg(SC_L1C_DLS);
|
|
|
|
if (cacheLineSize > 0)
|
|
|
|
g_cacheLineSize = cacheLineSize;
|
2017-09-17 18:34:05 +00:00
|
|
|
#elif defined(__linux__) && defined(_SC_LEVEL1_DCACHE_LINESIZE)
|
2018-03-21 20:19:43 +00:00
|
|
|
// Glibc does not implement on some platforms. The runtime returns 0 instead of error.
|
|
|
|
// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/sysconf.c
|
|
|
|
int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
|
|
|
|
if (cacheLineSize > 0)
|
|
|
|
g_cacheLineSize = cacheLineSize;
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-17 00:37:29 +00:00
|
|
|
*const_cast<volatile bool*>(&g_PowerpcDetectionDone) = true;
|
2017-09-11 07:05:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2017-08-18 07:03:42 +00:00
|
|
|
// *************************** C++ Static Initialization ***************************
|
2017-03-20 12:51:10 +00:00
|
|
|
|
|
|
|
ANONYMOUS_NAMESPACE_BEGIN
|
2017-11-12 16:55:57 +00:00
|
|
|
|
|
|
|
class InitCpu
|
2017-03-20 12:51:10 +00:00
|
|
|
{
|
2017-11-12 16:55:57 +00:00
|
|
|
public:
|
|
|
|
InitCpu()
|
2017-03-20 12:51:10 +00:00
|
|
|
{
|
|
|
|
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
|
|
|
|
CryptoPP::DetectX86Features();
|
|
|
|
#elif CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64
|
|
|
|
CryptoPP::DetectArmFeatures();
|
2017-09-11 07:05:04 +00:00
|
|
|
#elif CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
|
|
|
|
CryptoPP::DetectPowerpcFeatures();
|
2017-03-20 12:51:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-12 16:55:57 +00:00
|
|
|
// This is not really needed because HasSSE() and friends can dynamically initialize.
|
|
|
|
// Everything depends on CPU features so we initialize it once at load time.
|
|
|
|
// Dynamic initialization will be used if init priorities are not available.
|
|
|
|
|
2017-03-20 12:51:10 +00:00
|
|
|
#if HAVE_GCC_INIT_PRIORITY
|
2017-11-12 16:55:57 +00:00
|
|
|
const InitCpu s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = InitCpu();
|
2017-03-20 12:51:10 +00:00
|
|
|
#elif HAVE_MSC_INIT_PRIORITY
|
2017-11-12 16:55:57 +00:00
|
|
|
#pragma warning(disable: 4075)
|
|
|
|
#pragma init_seg(".CRT$XCU")
|
|
|
|
const InitCpu s_init;
|
|
|
|
#pragma warning(default: 4075)
|
2017-03-20 12:51:10 +00:00
|
|
|
#else
|
2017-11-12 16:55:57 +00:00
|
|
|
const InitCpu s_init;
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|
2017-11-12 16:55:57 +00:00
|
|
|
|
2017-03-20 12:51:10 +00:00
|
|
|
ANONYMOUS_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif // CRYPTOPP_IMPORTS
|