From b95c776c435b56905b0e2e10ab19737abf739797 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 10 Jan 2023 16:59:51 +0800 Subject: [PATCH] Add linux runtime detection Signed-off-by: Jerry Yu --- library/aesce.c | 20 ++++++++++++++++++++ library/aesce.h | 12 ++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/library/aesce.c b/library/aesce.c index 608e5e367..f33d59342 100644 --- a/library/aesce.c +++ b/library/aesce.c @@ -45,6 +45,26 @@ #include +#if defined(__linux__) +#include +#include +#endif + +/* + * AES instruction support detection routine + */ +int mbedtls_aesce_has_support(void) +{ +#if defined(__linux__) + unsigned long auxval = getauxval(AT_HWCAP); + return (auxval & (HWCAP_ASIMD | HWCAP_AES)) == + (HWCAP_ASIMD | HWCAP_AES); +#else + /* Suppose aes instructions are supported. */ + return 1; +#endif +} + #endif /* MBEDTLS_HAVE_ARM64 */ #endif /* MBEDTLS_AESCE_C */ diff --git a/library/aesce.h b/library/aesce.h index 4968feda9..2d5dde985 100644 --- a/library/aesce.h +++ b/library/aesce.h @@ -31,8 +31,8 @@ #include "mbedtls/aes.h" -#if !defined(MBEDTLS_HAVE_ARM64) && \ - (defined(__aarch64__) || defined(_M_ARM64)) +#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ + defined(__aarch64__) && !defined(MBEDTLS_HAVE_ARM64) #define MBEDTLS_HAVE_ARM64 #endif @@ -41,6 +41,14 @@ #ifdef __cplusplus extern "C" { #endif + +/** + * \brief Internal function to detect the crypto engine in CPUs. + * + * \return 1 if CPU has support for the feature, 0 otherwise + */ +int mbedtls_aesce_has_support(void); + #ifdef __cplusplus } #endif