Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SSE4.1/AES-NI support #644

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You want to enable AES-NI support:
You want to enable AES-NI support, but compile the rest of the library without the requirement for SSE4.1:


make CFLAGS=-DLTC_AES_NI CFLAGS_AES_NI="-maes -msse4.1"

## Installation

There exist several _install_ make-targets which are described in the table above.
Expand Down
7 changes: 7 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ ifneq ($V,1)
endif
${silent} ${CC} ${LTC_CFLAGS} -DENCRYPT_ONLY -c $< -o $@

#AES-NI support requires special compiler flags
src/ciphers/aes/aesni.o: src/ciphers/aes/aesni.c
ifneq ($V,1)
@echo " * ${CC} $@" ${silent_echo}
endif
${silent} ${CC} ${LTC_CFLAGS} ${CFLAGS_AES_NI} -c $< -o $@

.c.o:
ifneq ($V,1)
@echo " * ${CC} $@" ${silent_echo}
Expand Down
4 changes: 4 additions & 0 deletions makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes.c
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o

#SPECIAL: AES-NI support requires special compiler flags
src/ciphers/aes/aesni.o: src/ciphers/aes/aesni.c
$(CC) $(LTC_CFLAGS) $(CFLAGS_AES_NI) -c src/ciphers/aes/aesni.c -o src/ciphers/aes/aesni.o

#SPECIAL: these are the rules to make certain object files
src/ciphers/aes/aes.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/twofish/twofish.o: src/ciphers/twofish/twofish.c src/ciphers/twofish/twofish_tab.c
Expand Down
4 changes: 4 additions & 0 deletions makefile.msvc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ src/ciphers/aes/aes_enc.obj: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/aes/aes_enc_desc.obj: src/ciphers/aes/aes_desc.c
$(CC) $(LTC_CFLAGS) /DENCRYPT_ONLY /c src/ciphers/aes/aes_desc.c /Fosrc/ciphers/aes/aes_enc_desc.obj

#SPECIAL: AES-NI support requires special compiler flags
src/ciphers/aes/aesni.obj: src/ciphers/aes/aesni.c
$(CC) $(LTC_CFLAGS) $(CFLAGS_AES_NI) /c src/ciphers/aes/aesni.c /Fosrc/ciphers/aes/aesni.obj

#SPECIAL: these are the rules to make certain object files
src/ciphers/aes/aes.obj: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/twofish/twofish.obj: src/ciphers/twofish/twofish.c src/ciphers/twofish/twofish_tab.c
Expand Down
4 changes: 4 additions & 0 deletions makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes_desc.c
$(LTCOMPILE) $(LTC_CFLAGS) $(CPPFLAGS) $(LTC_LDFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o

#AES-NI support requires special compiler flags
src/ciphers/aes/aesni.o: src/ciphers/aes/aesni.c
$(LTCOMPILE) $(LTC_CFLAGS) $(CFLAGS_AES_NI) $(CPPFLAGS) $(LTC_LDFLAGS) -c src/ciphers/aes/aesni.c -o src/ciphers/aes/aesni.o

.c.o:
$(LTCOMPILE) $(LTC_CFLAGS) $(CPPFLAGS) $(LTC_LDFLAGS) -o $@ -c $<

Expand Down
4 changes: 4 additions & 0 deletions makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes_desc.c
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o

#SPECIAL: AES-NI support requires special compiler flags
src/ciphers/aes/aesni.o: src/ciphers/aes/aesni.c
$(CC) $(LTC_CFLAGS) $(CFLAGS_AES_NI) -c src/ciphers/aes/aesni.c -o src/ciphers/aes/aesni.o

#SPECIAL: these are the rules to make certain object files
src/ciphers/aes/aes.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
src/ciphers/twofish/twofish.o: src/ciphers/twofish/twofish.c src/ciphers/twofish/twofish_tab.c
Expand Down
12 changes: 6 additions & 6 deletions src/ciphers/aes/aes_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ 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;

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;
Expand All @@ -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;
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ciphers/aes/aesni.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "tomcrypt_private.h"

#if defined(LTC_HAS_AES_NI)
#if defined(LTC_AES_NI)

const struct ltc_cipher_descriptor aesni_desc =
{
Expand Down
5 changes: 0 additions & 5 deletions src/headers/tomcrypt_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/headers/tomcrypt_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,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);
Expand Down
3 changes: 0 additions & 3 deletions src/headers/tomcrypt_custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,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 */
Expand Down
4 changes: 0 additions & 4 deletions src/headers/tomcrypt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,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,
Expand Down
2 changes: 1 addition & 1 deletion src/misc/crypt/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,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)
Expand Down
2 changes: 1 addition & 1 deletion tests/cipher_hash_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down