From 4f6c8a6b2618fa491b602da4ee5573593359249e Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Mon, 23 Dec 2024 11:07:47 +1000 Subject: [PATCH] Aarch64 CPU id: fix for privilege instruction detection AES/PMULL is in four bits 4-7. When value is 0b0010, this indicates both AES and PMULL. Fix code to set both. Add setting of CPU flags as a command line option to benchmark. Added XSTRTOUL for Intel x64 and Aarch64 for CPU flagss command line option in benchmark. --- wolfcrypt/benchmark/benchmark.c | 11 +++++++++++ wolfcrypt/src/cpuid.c | 8 +++++--- wolfssl/wolfcrypt/types.h | 7 +++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index fa18aa0bbd..bb976c7918 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -15104,6 +15104,17 @@ int wolfcrypt_benchmark_main(int argc, char** argv) minimum_runs = XATOI(argv[1]); } } +#endif +#if defined(HAVE_CPUID) && (defined(HAVE_CPUID_INTEL) || \ + defined(HAVE_CPUID_AARCH64) + else if (string_matches(argv[1], "-cpuid")) { + argc--; + argv++; + if (argc > 1) { + (void)cpuid_get_flags(); + cpuid_select_flags((word32)XSTRTOUL(argv[1], NULL, 16)); + } + } #endif else if (argv[1][0] == '-') { optMatched = 0; diff --git a/wolfcrypt/src/cpuid.c b/wolfcrypt/src/cpuid.c index 2e63a092bf..a9f15338cf 100644 --- a/wolfcrypt/src/cpuid.c +++ b/wolfcrypt/src/cpuid.c @@ -105,7 +105,7 @@ #elif defined(HAVE_CPUID_AARCH64) #define CPUID_AARCH64_FEAT_AES ((word64)1 << 4) -#define CPUID_AARCH64_FEAT_PMULL ((word64)1 << 5) +#define CPUID_AARCH64_FEAT_AES_PMULL ((word64)1 << 5) #define CPUID_AARCH64_FEAT_SHA256 ((word64)1 << 12) #define CPUID_AARCH64_FEAT_SHA256_512 ((word64)1 << 13) #define CPUID_AARCH64_FEAT_RDM ((word64)1 << 28) @@ -131,8 +131,10 @@ if (features & CPUID_AARCH64_FEAT_AES) cpuid_flags |= CPUID_AES; - if (features & CPUID_AARCH64_FEAT_PMULL) + if (features & CPUID_AARCH64_FEAT_AES_PMULL) { + cpuid_flags |= CPUID_AES; cpuid_flags |= CPUID_PMULL; + } if (features & CPUID_AARCH64_FEAT_SHA256) cpuid_flags |= CPUID_SHA256; if (features & CPUID_AARCH64_FEAT_SHA256_512) @@ -279,7 +281,6 @@ void cpuid_set_flags(void) { if (!cpuid_check) { - #ifndef WOLFSSL_ARMASM_NO_HW_CRYPTO cpuid_flags |= CPUID_AES; cpuid_flags |= CPUID_PMULL; @@ -300,6 +301,7 @@ #ifdef WOLFSSL_ARMASM_CRYPTO_SM4 cpuid_flags |= CPUID_SM4; #endif + cpuid_check = 1; } } diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index f8042cf827..529af66e49 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -930,6 +930,13 @@ typedef struct w64wrapper { #define XATOI(s) atoi((s)) #endif #endif + + #if defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_AARCH64_BUILD) + #ifndef XSTR + #include + #define XSTRTOUL(s,e,b) strtoul((s),(e),(b)) + #endif + #endif #endif #ifdef USE_WOLF_STRTOK