aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorToni <matzeton@googlemail.com>2022-04-30 20:46:20 +0200
committerGitHub <noreply@github.com>2022-04-30 20:46:20 +0200
commit02d0b5fe13a49ca06179f089e2bf82dedd3043f0 (patch)
tree6c2889ef341d9da8026de81ed7081303a0a622e4 /src
parente4318ffc2d3b9b7f481bf1fd9aac713218e65bd9 (diff)
Improved AES-NI check. (#1536)
* A library should not open a subshell Signed-off-by: lns <matzeton@googlemail.com>
Diffstat (limited to 'src')
-rw-r--r--src/lib/third_party/src/gcrypt/aesni.c59
1 files changed, 30 insertions, 29 deletions
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 <cpuid.h>
+#endif
+
+#if defined(WIN32) || defined(WIN64)
+#include <intrin.h>
+#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
}