Skip to content

Commit

Permalink
limtomcrypt patch: Ensure that AES key is always correctly aligned
Browse files Browse the repository at this point in the history
  • Loading branch information
karel-m committed Oct 2, 2023
1 parent 6f17270 commit dc2dc8e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/ltc/ciphers/aes/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ static ulong32 setup_mix2(ulong32 temp)
int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
{
int i;
unsigned char *K;
ulong32 temp, *rk;
#ifndef ENCRYPT_ONLY
ulong32 *rrk;
Expand All @@ -112,6 +113,10 @@ int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *s
}

skey->rijndael.Nr = 10 + ((keylen/8)-2)*2;
K = (void*)((unsigned long)&skey->rijndael.K[15] & (~0xFuL));
skey->rijndael.eK = (ulong32*)K;
K += (60 * sizeof(ulong32));
skey->rijndael.dK = (ulong32*)K;

/* setup the forward key */
i = 0;
Expand Down Expand Up @@ -723,4 +728,3 @@ int ECB_KS(int *keysize)
}

#endif

6 changes: 5 additions & 1 deletion src/ltc/ciphers/aes/aesni.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static const ulong32 rcon[] = {
int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
{
int i;
unsigned char *K;
__m128i temp;
ulong32 *rk;
ulong32 *rrk;
Expand All @@ -60,6 +61,10 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
}

skey->rijndael.Nr = keylen / 4 + 6;
K = (void*)((unsigned long)&skey->rijndael.K[15] & (~0xFuL));
skey->rijndael.eK = (ulong32*)K;
K += (60 * sizeof(ulong32));
skey->rijndael.dK = (ulong32*)K;

/* setup the forward key */
i = 0;
Expand Down Expand Up @@ -364,4 +369,3 @@ int aesni_keysize(int *keysize)
}

#endif

5 changes: 3 additions & 2 deletions src/ltc/headers/tomcrypt_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ struct saferp_key {

#ifdef LTC_RIJNDAEL
struct rijndael_key {
ulong32 eK[60] LTC_ALIGN(16);
ulong32 dK[60] LTC_ALIGN(16);
ulong32 *eK;
ulong32 *dK;
int Nr;
unsigned char K[(60 + 60 + 4) * sizeof(ulong32)];
};
#endif

Expand Down

0 comments on commit dc2dc8e

Please sign in to comment.