From 1ec8a84cf69a53e3c02d54280ff48d22ae571abb Mon Sep 17 00:00:00 2001 From: "Pavel I. Kryukov" Date: Thu, 22 Mar 2018 21:57:38 +0300 Subject: [PATCH] Add CPUID leaf checks prior to SHA checks Some old CPUs do not support CPUID to be called with eax=7 To prevent failures, call CPUID with eax=0 to get the highest possible eax value (leaf) and compare it to 7. GCC does this check internally with __get_cpuid_count function Thanks to Jeffrey Walton for noticing. --- sshsha.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sshsha.c b/sshsha.c index b116afc0..c3ad9574 100644 --- a/sshsha.c +++ b/sshsha.c @@ -501,6 +501,10 @@ const struct ssh_mac ssh_hmac_sha1_96_buggy = { int supports_sha_ni(void) { unsigned int CPUInfo[4]; + __cpuid(0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]); + if (CPUInfo[0] < 7) + return 0; + __cpuid_count(7, 0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]); return CPUInfo[1] & (1 << 29); /* SHA */ } @@ -510,6 +514,10 @@ int supports_sha_ni(void) int supports_sha_ni(void) { unsigned int CPUInfo[4]; + __cpuid(CPUInfo, 0); + if (CPUInfo[0] < 7) + return 0; + __cpuidex(CPUInfo, 7, 0); return CPUInfo[1] & (1 << 29); /* Check SHA */ }