Skip to content

Commit 33d1c81

Browse files
Tim van der Molensjaeckel
authored andcommitted
Improve SSE4.1/AES-NI support
1 parent e8eb4a0 commit 33d1c81

File tree

9 files changed

+23
-22
lines changed

9 files changed

+23
-22
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ If you have `libtommath` in a non-standard location:
150150

151151
make CFLAGS="-DUSE_LTM -DLTM_DESC -I/opt/devel/ltm" EXTRALIBS="/opt/devel/ltm/libtommath.a" all
152152

153+
You want to enable AES-NI support:
154+
155+
make CFLAGS=-DLTC_AES_NI
156+
153157
## Installation
154158

155159
There exist several _install_ make-targets which are described in the table above.

src/ciphers/aes/aes_desc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ const struct ltc_cipher_descriptor aes_enc_desc =
4949
#endif
5050

5151
/* Code partially borrowed from https://software.intel.com/content/www/us/en/develop/articles/intel-sha-extensions.html */
52-
#if defined(LTC_HAS_AES_NI)
52+
#if defined(LTC_AES_NI)
5353
static LTC_INLINE int s_aesni_is_supported(void)
5454
{
5555
static int initialized = 0, is_supported = 0;
5656

5757
if (initialized == 0) {
5858
int a, b, c, d;
5959

60-
/* Look for CPUID.1.0.ECX[25]
60+
/* Look for CPUID.1.0.ECX[19] (SSE4.1) and CPUID.1.0.ECX[25] (AES-NI)
6161
* EAX = 1, ECX = 0
6262
*/
6363
a = 1;
@@ -68,7 +68,7 @@ static LTC_INLINE int s_aesni_is_supported(void)
6868
:"a"(a), "c"(c)
6969
);
7070

71-
is_supported = ((c >> 25) & 1);
71+
is_supported = ((c >> 19) & 1) && ((c >> 25) & 1);
7272
initialized = 1;
7373
}
7474

@@ -93,7 +93,7 @@ int aesni_is_supported(void)
9393
*/
9494
int AES_SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
9595
{
96-
#ifdef LTC_HAS_AES_NI
96+
#ifdef LTC_AES_NI
9797
if (s_aesni_is_supported()) {
9898
return aesni_setup(key, keylen, num_rounds, skey);
9999
}
@@ -111,7 +111,7 @@ int AES_SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_ke
111111
*/
112112
int AES_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
113113
{
114-
#ifdef LTC_HAS_AES_NI
114+
#ifdef LTC_AES_NI
115115
if (s_aesni_is_supported()) {
116116
return aesni_ecb_encrypt(pt, ct, skey);
117117
}
@@ -130,7 +130,7 @@ int AES_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *ske
130130
*/
131131
int AES_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
132132
{
133-
#ifdef LTC_HAS_AES_NI
133+
#ifdef LTC_AES_NI
134134
if (s_aesni_is_supported()) {
135135
return aesni_ecb_decrypt(ct, pt, skey);
136136
}

src/ciphers/aes/aesni.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include "tomcrypt_private.h"
1111

12-
#if defined(LTC_HAS_AES_NI)
12+
#if defined(LTC_AES_NI)
1313

1414
const struct ltc_cipher_descriptor aesni_desc =
1515
{
@@ -42,6 +42,7 @@ static const ulong32 rcon[] = {
4242
@param skey The key in as scheduled by this function.
4343
@return CRYPT_OK if successful
4444
*/
45+
LTC_ATTRIBUTE((__target__("aes,sse4.1")))
4546
int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
4647
{
4748
int i;
@@ -168,6 +169,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
168169
@param skey The key as scheduled
169170
@return CRYPT_OK if successful
170171
*/
172+
LTC_ATTRIBUTE((__target__("aes")))
171173
#ifdef LTC_CLEAN_STACK
172174
static int s_aesni_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
173175
#else
@@ -219,6 +221,7 @@ int aesni_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetri
219221
@param skey The key as scheduled
220222
@return CRYPT_OK if successful
221223
*/
224+
LTC_ATTRIBUTE((__target__("aes")))
222225
#ifdef LTC_CLEAN_STACK
223226
static int s_aesni_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
224227
#else

src/headers/tomcrypt_cfg.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
9191
#define ENDIAN_LITTLE
9292
#define ENDIAN_64BITWORD
9393
#define LTC_FAST
94-
#if defined(__SSE4_1__)
95-
#if __SSE4_1__ == 1
96-
#define LTC_AMD64_SSE4_1
97-
#endif
98-
#endif
9994
#endif
10095

10196
/* detect PPC32 */
@@ -337,4 +332,10 @@ typedef unsigned long ltc_mp_digit;
337332
# define LTC_DEPRECATED_PRAGMA(s)
338333
#endif
339334

335+
#if defined(__GNUC__) || defined(__clang__)
336+
# define LTC_ATTRIBUTE(x) __attribute__(x)
337+
#else
338+
# define LTC_ATTRIBUTE(x)
339+
#endif
340+
340341
#endif /* TOMCRYPT_CFG_H */

src/headers/tomcrypt_cipher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ extern const struct ltc_cipher_descriptor rijndael_desc;
736736
extern const struct ltc_cipher_descriptor rijndael_enc_desc;
737737
#endif
738738

739-
#if defined(LTC_AES_NI) && defined(LTC_AMD64_SSE4_1)
739+
#if defined(LTC_AES_NI)
740740
int aesni_is_supported(void);
741741
int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
742742
int aesni_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey);

src/headers/tomcrypt_custom.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@
182182
#define LTC_RC6
183183
#define LTC_SAFERP
184184
#define LTC_RIJNDAEL
185-
#ifndef LTC_NO_AES_NI
186-
#define LTC_AES_NI
187-
#endif
188185
#define LTC_XTEA
189186
/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format
190187
* (saves 4KB of ram), _ALL_TABLES enables all tables during setup */

src/headers/tomcrypt_private.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ typedef struct
110110

111111
/* tomcrypt_cipher.h */
112112

113-
#if defined(LTC_AES_NI) && defined(LTC_AMD64_SSE4_1)
114-
#define LTC_HAS_AES_NI
115-
#endif
116-
117113
void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
118114
int blowfish_expand(const unsigned char *key, int keylen,
119115
const unsigned char *data, int datalen,

src/misc/crypt/crypt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ const char *crypt_build_settings =
427427
#if defined(LTC_ADLER32)
428428
" ADLER32 "
429429
#endif
430-
#if defined(LTC_AES_NI) && defined(LTC_AMD64_SSE4_1)
430+
#if defined(LTC_AES_NI)
431431
" AES-NI "
432432
#endif
433433
#if defined(LTC_BASE64)

tests/cipher_hash_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int cipher_hash_test(void)
1414
}
1515

1616
/* explicit AES-NI test */
17-
#if defined(LTC_HAS_AES_NI)
17+
#if defined(LTC_AES_NI)
1818
if (aesni_is_supported()) {
1919
DO(aesni_test());
2020
}

0 commit comments

Comments
 (0)