1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-23 20:15:19 -07:00

Add ARM NEON and AES runtime checks

This commit is contained in:
Frank Denis 2019-10-22 22:34:09 +02:00
parent c9e95c59bd
commit acaed459ce
6 changed files with 62 additions and 15 deletions

View File

@ -554,7 +554,7 @@ AC_SUBST(CFLAGS_AESNI)
AC_SUBST(CFLAGS_PCLMUL)
AC_SUBST(CFLAGS_RDRAND)
AC_CHECK_HEADERS([sys/mman.h sys/random.h intrin.h])
AC_CHECK_HEADERS([sys/mman.h sys/random.h intrin.h sys/auxv.h])
AC_MSG_CHECKING([if _xgetbv() is available])
AC_LINK_IFELSE(
@ -818,6 +818,7 @@ unsigned char buf;
])
AC_CHECK_FUNCS([posix_memalign getpid nanosleep])
AC_CHECK_FUNCS([memset_s explicit_bzero explicit_memset])
AC_CHECK_FUNCS([getauxval])
AC_SUBST([LIBTOOL_EXTRA_FLAGS])

View File

@ -606,6 +606,7 @@ _sodium_runtime_has_avx 0 0
_sodium_runtime_has_avx2 0 0
_sodium_runtime_has_avx512f 0 0
_sodium_runtime_has_neon 0 0
_sodium_runtime_has_armcrypto_aes 0 0
_sodium_runtime_has_pclmul 0 0
_sodium_runtime_has_rdrand 0 0
_sodium_runtime_has_sse2 0 0

View File

@ -11,6 +11,9 @@ extern "C" {
SODIUM_EXPORT_WEAK
int sodium_runtime_has_neon(void);
SODIUM_EXPORT_WEAK
int sodium_runtime_has_armcrypto_aes(void);
SODIUM_EXPORT_WEAK
int sodium_runtime_has_sse2(void);

View File

@ -3,6 +3,14 @@
#ifdef HAVE_ANDROID_GETCPUFEATURES
# include <cpu-features.h>
#endif
#ifdef __APPLE__
# include <sys/types.h>
# include <sys/sysctl.h>
# include <mach/machine.h>
#endif
#ifdef HAVE_SYS_AUXV_H
# include <sys/auxv.h>
#endif
#include "private/common.h"
#include "runtime.h"
@ -10,6 +18,7 @@
typedef struct CPUFeatures_ {
int initialized;
int has_neon;
int has_armcrypto_aes;
int has_sse2;
int has_sse3;
int has_ssse3;
@ -48,25 +57,50 @@ static CPUFeatures _cpu_features;
static int
_sodium_runtime_arm_cpu_features(CPUFeatures * const cpu_features)
{
cpu_features->has_neon = 0;
cpu_features->has_armcrypto_aes = 0;
#ifndef __arm__
cpu_features->has_neon = 0;
return -1;
#else
# ifdef __APPLE__
# ifdef __ARM_NEON__
return -1; /* LCOV_EXCL_LINE */
#endif
#if defined(__ARM_NEON) || defined(__aarch64__)
cpu_features->has_neon = 1;
# else
cpu_features->has_neon = 0;
# endif
# elif defined(HAVE_ANDROID_GETCPUFEATURES) && \
defined(ANDROID_CPU_ARM_FEATURE_NEON)
#elif defined(HAVE_ANDROID_GETCPUFEATURES) && defined(ANDROID_CPU_ARM_FEATURE_NEON)
cpu_features->has_neon =
(android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0x0;
# else
cpu_features->has_neon = 0;
# endif
return 0;
#elif defined(HAVE_GETAUXVAL) && defined(AT_HWCAP) && defined(__aarch64__)
cpu_features->has_neon = (getauxval(AT_HWCAP) & (1L << 1)) != 0;
#endif
if (cpu_features->has_neon == 0) {
return 0;
}
#if __ARM_FEATURE_CRYPTO
cpu_features->has_armcrypto_aes = 1;
#elif defined(__APPLE__) && defined(CPU_TYPE_ARM64) && defined(CPU_SUBTYPE_ARM64E)
{
cpu_type_t cpu_type;
cpu_subtype_t cpu_subtype;
size_t cpu_type_len = sizeof cpu_type;
size_t cpu_subtype_len = sizeof cpu_subtype;
if (sysctlbyname("hw.cputype", &cpu_type, &cpu_type_len,
NULL, 0) == 0 && cpu_type == CPU_TYPE_ARM64 &&
sysctlbyname("hw.cpusubtype", &cpu_subtype, &cpu_subtype_len,
NULL, 0) == 0 && cpu_type == CPU_SUBTYPE_ARM64E) {
cpu_features->has_armcrypto_aes = 1;
}
}
#elif defined(HAVE_ANDROID_GETCPUFEATURES) && defined(ANDROID_CPU_ARM_FEATURE_AES)
cpu_features->has_armcrypto_aes =
(android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_AES) != 0x0;
#elif defined(HAVE_GETAUXVAL) && defined(AT_HWCAP) && defined(__aarch64__)
cpu_features->has_armcrypto_aes = (getauxval(AT_HWCAP) & (1L << 3)) != 0;
#endif
return 0;
}
static void
@ -248,6 +282,12 @@ sodium_runtime_has_neon(void)
return _cpu_features.has_neon;
}
int
sodium_runtime_has_armcrypto_aes(void)
{
return _cpu_features.has_armcrypto_aes;
}
int
sodium_runtime_has_sse2(void)
{

View File

@ -19,6 +19,7 @@ main(void)
assert(sodium_init() == 1);
(void) sodium_runtime_has_neon();
(void) sodium_runtime_has_armcrypto_aes();
(void) sodium_runtime_has_sse2();
(void) sodium_runtime_has_sse3();
(void) sodium_runtime_has_ssse3();

View File

@ -680,6 +680,7 @@ sodium_munlock
sodium_munshield
sodium_pad
sodium_runtime_has_aesni
sodium_runtime_has_armcrypto_aes
sodium_runtime_has_avx
sodium_runtime_has_avx2
sodium_runtime_has_avx512f