1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Add supports_sha_ni(void) function

It executes CPUID instruction to check whether
SHA extensions are supported by hosting CPU.
This commit is contained in:
Pavel I. Kryukov 2018-02-19 00:06:14 +03:00 committed by Simon Tatham
parent 59e2334029
commit f51a5c9235
2 changed files with 51 additions and 0 deletions

2
ssh.h
View File

@ -258,6 +258,8 @@ void hmacmd5_key(void *handle, void const *key, int len);
void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
unsigned char *hmac);
int supports_sha_ni(void);
typedef struct SHA_State {
uint32 h[5];
unsigned char block[64];

View File

@ -461,3 +461,52 @@ const struct ssh_mac ssh_hmac_sha1_96_buggy = {
12, 16,
"bug-compatible HMAC-SHA1-96"
};
#ifdef COMPILER_SUPPORTS_SHA_NI
/*
* Set target architecture for Clang and GCC
*/
#if !defined(__clang__) && defined(__GNUC__)
# pragma GCC target("sha")
# pragma GCC target("sse4.1")
#endif
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 5))
# define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
#else
# define FUNC_ISA
#endif
/*
* Determinators of CPU type
*/
#if defined(__clang__) || defined(__GNUC__)
#include <cpuid.h>
int supports_sha_ni(void)
{
unsigned int CPUInfo[4];
__cpuid_count(7, 0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
return CPUInfo[1] & (1 << 29); /* SHA */
}
#else /* defined(__clang__) || defined(__GNUC__) */
int supports_sha_ni(void)
{
unsigned int CPUInfo[4];
__cpuidex(CPUInfo, 7, 0);
return CPUInfo[1] & (1 << 29); /* Check SHA */
}
#endif /* defined(__clang__) || defined(__GNUC__) */
#else /* COMPILER_SUPPORTS_AES_NI */
int supports_sha_ni(void)
{
return 0;
}
#endif /* COMPILER_SUPPORTS_AES_NI */