From 02d0b5fe13a49ca06179f089e2bf82dedd3043f0 Mon Sep 17 00:00:00 2001 From: Toni Date: Sat, 30 Apr 2022 20:46:20 +0200 Subject: Improved AES-NI check. (#1536) * A library should not open a subshell Signed-off-by: lns --- src/lib/third_party/src/gcrypt/aesni.c | 59 +++++++++++++++++----------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/lib/third_party/src/gcrypt/aesni.c b/src/lib/third_party/src/gcrypt/aesni.c index b61c39a63..f20b0051c 100644 --- a/src/lib/third_party/src/gcrypt/aesni.c +++ b/src/lib/third_party/src/gcrypt/aesni.c @@ -34,49 +34,50 @@ #endif #if defined(MBEDTLS_HAVE_X86_64) +#if defined(linux) || defined(__linux__) +#include +#endif + +#if defined(WIN32) || defined(WIN64) +#include +#endif /* * AES-NI support detection routine */ int mbedtls_aesni_has_support( unsigned int what ) { -#if !(defined(linux) || defined(__linux__)) - static int done = 0; - static unsigned int c = 0; -#endif - #if defined(__has_feature) # if __has_feature(memory_sanitizer) - return 0; + return 0; # endif #endif #if defined(linux) || defined(__linux__) - FILE *p; - int ch; - - p = popen("sort -u /proc/crypto | grep aesni_intel | wc -l","r"); - if(p == NULL) - return(0); - - ch=fgetc(p); - pclose(p); - - /* printf("*** %d / %c\n", ch, ch); */ - - return(ch == '1' ? 1 : 0); + unsigned int eax, ebx, ecx, edx; + + if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0) + { + return 0; + } + + return ( (ecx & what) != 0 ); +#elif defined(WIN32) || defined(WIN64) + int cpuInfo[4]; + + __cpuid(cpuInfo, 1); + + return ( (cpuInfo[2] & what) != 0 ); #else - if( ! done ) - { - asm( "movl $1, %%eax \n\t" - "cpuid \n\t" - : "=c" (c) - : - : "eax", "ebx", "edx" ); - done = 1; - } + volatile unsigned int c = 0; + + asm( "movl $1, %%eax \n\t" + "cpuid \n\t" + : "=c" (c) + : + : "eax", "ebx", "edx" ); - return( ( (volatile unsigned int)c & what ) != 0 ); + return( ( c & what ) != 0 ); #endif } -- cgit v1.2.3