From f940880a6756ebe1b12ea32cb526e5221735c0f1 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 30 Apr 2026 08:55:54 +0200 Subject: [PATCH] Create `ENCRYPT_ONLY` descriptors in amalgamated build. Signed-off-by: Steffen Jaeckel --- makefile | 4 ++++ src/ciphers/aes/aes.c | 10 +++++++--- src/ciphers/aes/aes_desc.c | 4 ++-- src/ciphers/aes/aesni.c | 14 +++++++++----- src/prngs/fortuna.c | 7 ++++++- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/makefile b/makefile index 72437220..d62bc62d 100644 --- a/makefile +++ b/makefile @@ -156,6 +156,10 @@ pre_gen/tomcrypt_amalgam.c: $(TAB_SOURCES) $(SOURCES) printf "#define LTC_WHIRLTAB_C\n\n" >> $@ printf "#include \"tomcrypt_private.h\"\n\n" >> $@ cat $^ >> $@ + printf "#define ENCRYPT_ONLY\n\n" >> $@ + cat src/ciphers/aes/aes.c >> $@ + cat src/ciphers/aes/aes_desc.c >> $@ + printf "#undef ENCRYPT_ONLY\n\n" >> $@ pre_gen: pre_gen/tomcrypt_amalgam.c diff --git a/src/ciphers/aes/aes.c b/src/ciphers/aes/aes.c index 33882ccc..103fb1c0 100644 --- a/src/ciphers/aes/aes.c +++ b/src/ciphers/aes/aes.c @@ -67,6 +67,10 @@ const struct ltc_cipher_descriptor rijndael_enc_desc = #include "aes_tab.c" #endif +#define setup_mix rijndael_setup_mix + +#ifndef RIJNDAEL_SETUP_MIX +#define RIJNDAEL_SETUP_MIX static ulong32 setup_mix(ulong32 temp) { return (Te4_3[LTC_BYTE(temp, 2)]) ^ @@ -74,9 +78,9 @@ static ulong32 setup_mix(ulong32 temp) (Te4_1[LTC_BYTE(temp, 0)]) ^ (Te4_0[LTC_BYTE(temp, 3)]); } +#endif /* RIJNDAEL_SETUP_MIX */ -#ifndef ENCRYPT_ONLY -#ifdef LTC_SMALL_CODE +#if !defined(ENCRYPT_ONLY) && defined(LTC_SMALL_CODE) static ulong32 setup_mix2(ulong32 temp) { return Td0(255 & Te4[LTC_BYTE(temp, 3)]) ^ @@ -84,7 +88,6 @@ static ulong32 setup_mix2(ulong32 temp) Td2(255 & Te4[LTC_BYTE(temp, 1)]) ^ Td3(255 & Te4[LTC_BYTE(temp, 0)]); } -#endif #endif /** @@ -728,6 +731,7 @@ int ECB_KS(int *keysize) return CRYPT_OK; } +#undef setup_mix #undef SETUP #undef ECB_ENC #undef ECB_DEC diff --git a/src/ciphers/aes/aes_desc.c b/src/ciphers/aes/aes_desc.c index 62b30d8b..b9d1d508 100644 --- a/src/ciphers/aes/aes_desc.c +++ b/src/ciphers/aes/aes_desc.c @@ -272,6 +272,8 @@ int AES_KS(int *keysize) return CRYPT_OK; } +#endif + #undef AES_SETUP #undef AES_ENC #undef AES_DEC @@ -279,5 +281,3 @@ int AES_KS(int *keysize) #undef AES_TEST #undef AES_KS -#endif - diff --git a/src/ciphers/aes/aesni.c b/src/ciphers/aes/aesni.c index c4b981dd..beb51ccb 100644 --- a/src/ciphers/aes/aesni.c +++ b/src/ciphers/aes/aesni.c @@ -32,7 +32,7 @@ const struct ltc_cipher_descriptor aesni_desc = #pragma GCC diagnostic pop #endif -#define setup_mix(t, c) _mm_extract_epi32(_mm_aeskeygenassist_si128(t, 0), c) +#define aesni_setup_mix(t, c) _mm_extract_epi32(_mm_aeskeygenassist_si128(t, 0), c) #define temp_load(k) _mm_loadu_si128((__m128i*)(k)) #define temp_update(t, k) _mm_insert_epi32(t, k, 3) #define temp_invert(k) _mm_aesimc_si128(*((__m128i*)(k))) @@ -84,7 +84,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ if (keylen == 16) { temp = temp_load(key); for (;;) { - rk[4] = rk[0] ^ setup_mix(temp, 3) ^ rcon[i]; + rk[4] = rk[0] ^ aesni_setup_mix(temp, 3) ^ rcon[i]; rk[5] = rk[1] ^ rk[4]; rk[6] = rk[2] ^ rk[5]; rk[7] = rk[3] ^ rk[6]; @@ -99,7 +99,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ LOAD32L(rk[5], key + 20); temp = temp_load(key + 8); for (;;) { - rk[6] = rk[0] ^ setup_mix(temp, 3) ^ rcon[i]; + rk[6] = rk[0] ^ aesni_setup_mix(temp, 3) ^ rcon[i]; rk[7] = rk[1] ^ rk[6]; rk[8] = rk[2] ^ rk[7]; rk[9] = rk[3] ^ rk[8]; @@ -118,7 +118,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ LOAD32L(rk[7], key + 28); temp = temp_load(key + 16); for (;;) { - rk[8] = rk[0] ^ setup_mix(temp, 3) ^ rcon[i]; + rk[8] = rk[0] ^ aesni_setup_mix(temp, 3) ^ rcon[i]; rk[9] = rk[1] ^ rk[8]; rk[10] = rk[2] ^ rk[9]; rk[11] = rk[3] ^ rk[10]; @@ -126,7 +126,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ break; } temp = temp_update(temp, rk[11]); - rk[12] = rk[4] ^ setup_mix(temp, 2); + rk[12] = rk[4] ^ aesni_setup_mix(temp, 2); rk[13] = rk[5] ^ rk[12]; rk[14] = rk[6] ^ rk[13]; rk[15] = rk[7] ^ rk[14]; @@ -377,6 +377,10 @@ int aesni_keysize(int *keysize) return CRYPT_OK; } +#undef aesni_setup_mix +#undef temp_load +#undef temp_update +#undef temp_invert #undef rcon #endif diff --git a/src/prngs/fortuna.c b/src/prngs/fortuna.c index 9c92595e..af55fed4 100644 --- a/src/prngs/fortuna.c +++ b/src/prngs/fortuna.c @@ -528,5 +528,10 @@ int fortuna_test(void) #endif } -#endif +#undef AES_SETUP +#undef AES_ENC +#undef AES_DONE +#undef AES_TEST + +#endif /* LTC_FORTUNA */