Browse Source

Ensure that AES key is always correctly aligned

Aligning a `struct` member via `attribute(align(<n>))` is not guaranteed
to work.
Change the approach to use an opaque buffer and always manually align
the start pointers of the keys.

c.f. https://github.com/DCIT/perl-CryptX/issues/95

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel 2 years ago
parent
commit
1c36997bd0
3 changed files with 13 additions and 4 deletions
  1. 5 1
      src/ciphers/aes/aes.c
  2. 5 1
      src/ciphers/aes/aesni.c
  3. 3 2
      src/headers/tomcrypt_cipher.h

+ 5 - 1
src/ciphers/aes/aes.c

@@ -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 SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
 {
 {
     int i;
     int i;
-    ulong32 temp, *rk;
+    ulong32 temp, *rk, *K;
 #ifndef ENCRYPT_ONLY
 #ifndef ENCRYPT_ONLY
     ulong32 *rrk;
     ulong32 *rrk;
 #endif
 #endif
@@ -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;
     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 */
     /* setup the forward key */
     i                 = 0;
     i                 = 0;

+ 5 - 1
src/ciphers/aes/aesni.c

@@ -46,7 +46,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
 {
 {
    int i;
    int i;
    __m128i temp;
    __m128i temp;
-   ulong32 *rk;
+   ulong32 *rk, *K;
    ulong32 *rrk;
    ulong32 *rrk;
    LTC_ARGCHK(key != NULL);
    LTC_ARGCHK(key != NULL);
    LTC_ARGCHK(skey != NULL);
    LTC_ARGCHK(skey != NULL);
@@ -60,6 +60,10 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
    }
    }
 
 
    skey->rijndael.Nr = keylen / 4 + 6;
    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 */
    /* setup the forward key */
    i = 0;
    i = 0;

+ 3 - 2
src/headers/tomcrypt_cipher.h

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