Skip to content

Commit

Permalink
Merge pull request #633 from libtom/some-fixes
Browse files Browse the repository at this point in the history
Fix AES-NI and other minor stuff
  • Loading branch information
sjaeckel authored Oct 8, 2023
2 parents b96e96c + e4adaaf commit 91b7bbe
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 71 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ include(CheckIPOSupported)
include(CMakePackageConfigHelpers)
# for potential builds against gnump
include(FindPkgConfig)
# for potential builds with MSVC
include(CMakePushCheckState)
include(CheckSymbolExists)
# default is "No tests"
option(BUILD_TESTING "" OFF)
include(CTest)
Expand Down Expand Up @@ -297,6 +300,15 @@ else()
set(DISTRO_PACK_PATH ${CMAKE_SYSTEM_NAME}/)
endif()

# make sure untagged versions get a different package name
execute_process(COMMAND git describe --exact-match --tags ERROR_QUIET RESULT_VARIABLE REPO_HAS_TAG)
if(REPO_HAS_TAG EQUAL 0)
set(PACKAGE_NAME_SUFFIX "")
else()
set(PACKAGE_NAME_SUFFIX "-git")
message(STATUS "Use -git suffix")
endif()

# default CPack generators
set(CPACK_GENERATOR TGZ STGZ)

Expand Down
4 changes: 1 addition & 3 deletions demos/gcm-file/gcm_filehandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ int gcm_filehandle( int cipher,
* but again it's only for SSE2 anyways, so who cares?
*/
#ifdef LTC_GCM_TABLES_SSE2
if ((unsigned long)gcm & 15) {
gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15)));
}
gcm = LTC_ALIGN_BUF(gcm, 16);
#endif

if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {
Expand Down
18 changes: 9 additions & 9 deletions makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ ifndef CROSS_COMPILE
CROSS_COMPILE:=
endif

# We only need to go through this dance of determining the right compiler if we're using
# cross compilation, otherwise $(CC) is fine as-is.
H := \#
ifeq (CLANG,$(shell printf "$(H)ifdef __clang__\nCLANG\n$(H)endif\n" | $(CC) -E - | grep CLANG))
CC_IS_CLANG := 1
else
CC_IS_CLANG := 0
endif # Clang

ifneq (,$(CROSS_COMPILE))
ifeq ($(origin CC),default)
CSTR := "\#ifdef __clang__\nCLANG\n\#endif\n"
ifeq ($(PLATFORM),FreeBSD)
# XXX: FreeBSD needs extra escaping for some reason
CSTR := $$$(CSTR)
endif
ifneq (,$(shell echo $(CSTR) | $(CC) -E - | grep CLANG))
ifeq ($(CC_IS_CLANG), 1)
CC := $(CROSS_COMPILE)clang
else
CC := $(CROSS_COMPILE)gcc
Expand Down Expand Up @@ -124,7 +124,7 @@ LTC_CFLAGS += -Os -DLTC_SMALL_CODE
endif # LTC_SMALL


ifneq ($(findstring clang,$(CC)),)
ifeq ($(CC_IS_CLANG), 1)
LTC_CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header
LTC_CFLAGS += -Wno-missing-field-initializers -Wno-missing-braces -Wno-incomplete-setjmp-declaration -Wno-cast-align
LTC_CFLAGS += -Wno-declaration-after-statement
Expand Down
6 changes: 5 additions & 1 deletion src/ciphers/aes/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static ulong32 setup_mix2(ulong32 temp)
int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
{
int i;
ulong32 temp, *rk;
ulong32 temp, *rk, *K;
#ifndef ENCRYPT_ONLY
ulong32 *rrk;
#endif
Expand All @@ -112,6 +112,10 @@ int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *s
}

skey->rijndael.Nr = 10 + ((keylen/8)-2)*2;
K = LTC_ALIGN_BUF(skey->rijndael.K, 16);
skey->rijndael.eK = K;
K += 60;
skey->rijndael.dK = K;

/* setup the forward key */
i = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/ciphers/aes/aesni.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
{
int i;
__m128i temp;
ulong32 *rk;
ulong32 *rk, *K;
ulong32 *rrk;
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(skey != NULL);
Expand All @@ -60,6 +60,10 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
}

skey->rijndael.Nr = keylen / 4 + 6;
K = LTC_ALIGN_BUF(skey->rijndael.K, 16);
skey->rijndael.eK = K;
K += 60;
skey->rijndael.dK = K;

/* setup the forward key */
i = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/encauth/ccm/ccm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ int ccm_memory(int cipher,
PAD[x++] = 0;
}
for (; y < L; y++) {
if (x >= sizeof(PAD)) {
return CRYPT_INVALID_ARG;
}
PAD[x++] = (unsigned char)((len >> 24) & 255);
len <<= 8;
}
Expand Down
4 changes: 1 addition & 3 deletions src/encauth/gcm/gcm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ int gcm_memory( int cipher,
* but again it's only for SSE2 anyways, so who cares?
*/
#ifdef LTC_GCM_TABLES_SSE2
if ((unsigned long)gcm & 15) {
gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15)));
}
gcm = LTC_ALIGN_BUF(gcm, 16);
#endif

if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {
Expand Down
2 changes: 1 addition & 1 deletion src/headers/tomcrypt_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
typedef unsigned __int64 ulong64;
typedef __int64 long64;
#else
#define CONST64(n) n ## ULL
#define CONST64(n) n ## uLL
typedef unsigned long long ulong64;
typedef long long long64;
#endif
Expand Down
75 changes: 38 additions & 37 deletions src/headers/tomcrypt_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ struct saferp_key {

#ifdef LTC_RIJNDAEL
struct rijndael_key {
ulong32 eK[60] LTC_ALIGN(16);
ulong32 dK[60] LTC_ALIGN(16);
unsigned char K[(60 + 60 + 4) * sizeof(ulong32)];
ulong32 *eK;
ulong32 *dK;
int Nr;
};
#endif
Expand Down Expand Up @@ -128,24 +129,24 @@ struct khazad_key {

#ifdef LTC_ANUBIS
struct anubis_key {
int keyBits;
int R;
ulong32 roundKeyEnc[18 + 1][4];
ulong32 roundKeyDec[18 + 1][4];
int keyBits;
int R;
};
#endif

#ifdef LTC_MULTI2
struct multi2_key {
int N;
ulong32 uk[8];
int N;
};
#endif

#ifdef LTC_CAMELLIA
struct camellia_key {
int R;
ulong64 kw[4], k[24], kl[6];
int R;
};
#endif

Expand Down Expand Up @@ -246,67 +247,74 @@ typedef union Symmetric_key {
#ifdef LTC_ECB_MODE
/** A block cipher ECB structure */
typedef struct {
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen;
/** The scheduled key */
symmetric_key key;
} symmetric_ECB;
#endif

#ifdef LTC_CFB_MODE
/** A block cipher CFB structure */
typedef struct {
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
/** The pad used to encrypt/decrypt */
pad[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
} symmetric_CFB;
#endif

#ifdef LTC_OFB_MODE
/** A block cipher OFB structure */
typedef struct {
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
} symmetric_OFB;
#endif

#ifdef LTC_CBC_MODE
/** A block cipher CBC structure */
typedef struct {
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen;
} symmetric_CBC;
#endif


#ifdef LTC_CTR_MODE
/** A block cipher CTR structure */
typedef struct {
/** The counter */
unsigned char ctr[MAXBLOCKSIZE];
/** The pad used to encrypt/decrypt */
unsigned char pad[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;

/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
Expand All @@ -317,23 +325,13 @@ typedef struct {
mode,
/** counter width */
ctrlen;

/** The counter */
unsigned char ctr[MAXBLOCKSIZE];
/** The pad used to encrypt/decrypt */
unsigned char pad[MAXBLOCKSIZE] LTC_ALIGN(16);
/** The scheduled key */
symmetric_key key;
} symmetric_CTR;
#endif


#ifdef LTC_LRW_MODE
/** A LRW structure */
typedef struct {
/** The index of the cipher chosen (must be a 128-bit block cipher) */
int cipher;

/** The current IV */
unsigned char IV[16],

Expand All @@ -350,25 +348,28 @@ typedef struct {
/** The pre-computed multiplication table */
unsigned char PC[16][256][16];
#endif

/** The index of the cipher chosen (must be a 128-bit block cipher) */
int cipher;
} symmetric_LRW;
#endif

#ifdef LTC_F8_MODE
/** A block cipher F8 structure */
typedef struct {
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
MIV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
MIV[MAXBLOCKSIZE];
/** Current block count */
ulong32 blockcnt;
/** The scheduled key */
symmetric_key key;
} symmetric_F8;
#endif

Expand Down
4 changes: 3 additions & 1 deletion src/headers/tomcrypt_custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@
#define LTC_RC6
#define LTC_SAFERP
#define LTC_RIJNDAEL
#define LTC_AES_NI
#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
Loading

0 comments on commit 91b7bbe

Please sign in to comment.