From 41125f9453ae8ac75f6314dbd6f77e3284e7912b Mon Sep 17 00:00:00 2001 From: Eric Vannier Date: Thu, 22 Mar 2018 21:32:56 -0700 Subject: [PATCH] Allow AESNI to be used on all processor supporting AESNI, not just Intel's This improves performance/reduced CPU usage. Tests performed: - Ran on Ivy Bridge and Ryzen and verified that AESNI is detected (crypto tests) - Ran on Ryzen, and observed 50% increased speed. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13302 Signed-off-by: Eric Vannier --- lib/crypto/aes.c | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c index c226ac1b3df..f521a4d11bf 100644 --- a/lib/crypto/aes.c +++ b/lib/crypto/aes.c @@ -54,10 +54,10 @@ static inline void __cpuid(unsigned int where[4], unsigned int leaf) } /* - * has_intel_aes_instructions() + * has_aes_instructions() * return true if supports AES-NI and false if doesn't */ -static bool has_intel_aes_instructions(void) +static bool has_aes_instructions(void) { static int has_aes_instructions = -1; unsigned int cpuid_results[4]; @@ -66,22 +66,6 @@ static bool has_intel_aes_instructions(void) return (bool)has_aes_instructions; } - __cpuid(cpuid_results, 0); - /* - * MSB LSB - * EBX = 'u' 'n' 'e' 'G' - * EDX = 'I' 'e' 'n' 'i' - * ECX = 'l' 'e' 't' 'n' - */ - if (memcmp((unsigned char *)&cpuid_results[1], "Genu", 4) != 0 || - memcmp((unsigned char *)&cpuid_results[3], - "ineI", 4) != 0 || - memcmp((unsigned char *)&cpuid_results[2], - "ntel", 4) != 0) { - has_aes_instructions = 0; - return (bool)has_aes_instructions; - } - __cpuid(cpuid_results, 1); has_aes_instructions = !!(cpuid_results[2] & (1 << 25)); return (bool)has_aes_instructions; @@ -140,10 +124,10 @@ static void AES_decrypt_aesni(const unsigned char *in, /* * Dummy implementations if no Intel AES instructions present. - * Only has_intel_aes_instructions() will ever be called. + * Only has_aes_instructions() will ever be called. */ -static bool has_intel_aes_instructions(void) +static bool has_aes_instructions(void) { return false; } @@ -233,7 +217,7 @@ AES_decrypt_rj(const unsigned char *in, unsigned char *out, const AES_KEY *key) int AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key) { - if (has_intel_aes_instructions()) { + if (has_aes_instructions()) { return AES_set_encrypt_key_aesni(userkey, bits, key); } return AES_set_encrypt_key_rj(userkey, bits, key); @@ -242,7 +226,7 @@ AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key) int AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key) { - if (has_intel_aes_instructions()) { + if (has_aes_instructions()) { return AES_set_decrypt_key_aesni(userkey, bits, key); } return AES_set_decrypt_key_rj(userkey, bits, key); @@ -251,7 +235,7 @@ AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key) void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { - if (has_intel_aes_instructions()) { + if (has_aes_instructions()) { return AES_encrypt_aesni(in, out, key); } return AES_encrypt_rj(in, out, key); @@ -260,7 +244,7 @@ AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { - if (has_intel_aes_instructions()) { + if (has_aes_instructions()) { return AES_decrypt_aesni(in, out, key); } return AES_decrypt_rj(in, out, key); -- 2.14.3