diff --git a/README.md b/README.md index 3088bdcac..661fdbcce 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,10 @@ If you have `libtommath` in a non-standard location: make CFLAGS="-DUSE_LTM -DLTM_DESC -I/opt/devel/ltm" EXTRALIBS="/opt/devel/ltm/libtommath.a" all +You want to enable AES-NI support: + + make CFLAGS=-DLTC_AES_NI + ## Installation There exist several _install_ make-targets which are described in the table above. diff --git a/src/ciphers/aes/aes_desc.c b/src/ciphers/aes/aes_desc.c index a09f94389..7c537df75 100644 --- a/src/ciphers/aes/aes_desc.c +++ b/src/ciphers/aes/aes_desc.c @@ -49,7 +49,7 @@ const struct ltc_cipher_descriptor aes_enc_desc = #endif /* Code partially borrowed from https://software.intel.com/content/www/us/en/develop/articles/intel-sha-extensions.html */ -#if defined(LTC_HAS_AES_NI) +#if defined(LTC_AES_NI) static LTC_INLINE int s_aesni_is_supported(void) { static int initialized = 0, is_supported = 0; @@ -57,7 +57,7 @@ static LTC_INLINE int s_aesni_is_supported(void) if (initialized == 0) { int a, b, c, d; - /* Look for CPUID.1.0.ECX[25] + /* Look for CPUID.1.0.ECX[19] (SSE4.1) and CPUID.1.0.ECX[25] (AES-NI) * EAX = 1, ECX = 0 */ a = 1; @@ -68,7 +68,7 @@ static LTC_INLINE int s_aesni_is_supported(void) :"a"(a), "c"(c) ); - is_supported = ((c >> 25) & 1); + is_supported = ((c >> 19) & 1) && ((c >> 25) & 1); initialized = 1; } @@ -93,7 +93,7 @@ int aesni_is_supported(void) */ int AES_SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { -#ifdef LTC_HAS_AES_NI +#ifdef LTC_AES_NI if (s_aesni_is_supported()) { return aesni_setup(key, keylen, num_rounds, skey); } @@ -111,7 +111,7 @@ int AES_SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_ke */ int AES_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) { -#ifdef LTC_HAS_AES_NI +#ifdef LTC_AES_NI if (s_aesni_is_supported()) { return aesni_ecb_encrypt(pt, ct, skey); } @@ -130,7 +130,7 @@ int AES_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *ske */ int AES_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) { -#ifdef LTC_HAS_AES_NI +#ifdef LTC_AES_NI if (s_aesni_is_supported()) { return aesni_ecb_decrypt(ct, pt, skey); } diff --git a/src/ciphers/aes/aesni.c b/src/ciphers/aes/aesni.c index 113aaf676..c490503d1 100644 --- a/src/ciphers/aes/aesni.c +++ b/src/ciphers/aes/aesni.c @@ -9,7 +9,13 @@ #include "tomcrypt_private.h" -#if defined(LTC_HAS_AES_NI) +#if defined(LTC_AES_NI) + +#if defined(__GNUC__) || defined(__clang__) +#define LTC_ATTRIBUTE(x) __attribute__(x) +#else +#define LTC_ATTRIBUTE(x) +#endif const struct ltc_cipher_descriptor aesni_desc = { @@ -42,6 +48,7 @@ static const ulong32 rcon[] = { @param skey The key in as scheduled by this function. @return CRYPT_OK if successful */ +LTC_ATTRIBUTE((__target__("aes,sse4.1"))) int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { int i; @@ -168,6 +175,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ @param skey The key as scheduled @return CRYPT_OK if successful */ +LTC_ATTRIBUTE((__target__("aes"))) #ifdef LTC_CLEAN_STACK static int s_aesni_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) #else @@ -219,6 +227,7 @@ int aesni_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetri @param skey The key as scheduled @return CRYPT_OK if successful */ +LTC_ATTRIBUTE((__target__("aes"))) #ifdef LTC_CLEAN_STACK static int s_aesni_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) #else diff --git a/src/headers/tomcrypt_cfg.h b/src/headers/tomcrypt_cfg.h index a0209e23c..23e3520d8 100644 --- a/src/headers/tomcrypt_cfg.h +++ b/src/headers/tomcrypt_cfg.h @@ -91,11 +91,6 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2); #define ENDIAN_LITTLE #define ENDIAN_64BITWORD #define LTC_FAST - #if defined(__SSE4_1__) - #if __SSE4_1__ == 1 - #define LTC_AMD64_SSE4_1 - #endif - #endif #endif /* detect PPC32 */ diff --git a/src/headers/tomcrypt_cipher.h b/src/headers/tomcrypt_cipher.h index 074e55900..7599ecd46 100644 --- a/src/headers/tomcrypt_cipher.h +++ b/src/headers/tomcrypt_cipher.h @@ -727,7 +727,7 @@ extern const struct ltc_cipher_descriptor rijndael_desc; extern const struct ltc_cipher_descriptor rijndael_enc_desc; #endif -#if defined(LTC_AES_NI) && defined(LTC_AMD64_SSE4_1) +#if defined(LTC_AES_NI) int aesni_is_supported(void); int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int aesni_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey); diff --git a/src/headers/tomcrypt_custom.h b/src/headers/tomcrypt_custom.h index bea0ea501..6558889d0 100644 --- a/src/headers/tomcrypt_custom.h +++ b/src/headers/tomcrypt_custom.h @@ -181,9 +181,6 @@ #define LTC_RC6 #define LTC_SAFERP #define LTC_RIJNDAEL -#ifndef LTC_NO_AES_NI - #define LTC_AES_NI -#endif #define LTC_XTEA /* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format * (saves 4KB of ram), _ALL_TABLES enables all tables during setup */ diff --git a/src/headers/tomcrypt_private.h b/src/headers/tomcrypt_private.h index 61a8b2ae8..de2028822 100644 --- a/src/headers/tomcrypt_private.h +++ b/src/headers/tomcrypt_private.h @@ -110,10 +110,6 @@ typedef struct /* tomcrypt_cipher.h */ -#if defined(LTC_AES_NI) && defined(LTC_AMD64_SSE4_1) -#define LTC_HAS_AES_NI -#endif - void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey); int blowfish_expand(const unsigned char *key, int keylen, const unsigned char *data, int datalen, diff --git a/src/misc/crypt/crypt.c b/src/misc/crypt/crypt.c index a215b6a1f..ef58fd672 100644 --- a/src/misc/crypt/crypt.c +++ b/src/misc/crypt/crypt.c @@ -424,7 +424,7 @@ const char *crypt_build_settings = #if defined(LTC_ADLER32) " ADLER32 " #endif -#if defined(LTC_AES_NI) && defined(LTC_AMD64_SSE4_1) +#if defined(LTC_AES_NI) " AES-NI " #endif #if defined(LTC_BASE64) diff --git a/tests/cipher_hash_test.c b/tests/cipher_hash_test.c index 37bde7696..258780d30 100644 --- a/tests/cipher_hash_test.c +++ b/tests/cipher_hash_test.c @@ -14,7 +14,7 @@ int cipher_hash_test(void) } /* explicit AES-NI test */ -#if defined(LTC_HAS_AES_NI) +#if defined(LTC_AES_NI) if (aesni_is_supported()) { DO(aesni_test()); }