Simplified aes_set_encrypt_key.
authorNiels Möller <nisse@lysator.liu.se>
Fri, 13 Apr 2012 15:00:50 +0000 (17:00 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Fri, 13 Apr 2012 15:01:33 +0000 (17:01 +0200)
ChangeLog
aes-set-encrypt-key.c

index ff613329aa3e90253fd51c35f7c3fb26b34ba9c4..2b4a4c32b30f855f117ac27dd442e8dba6146cf7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2012-04-13  Niels Möller  <nisse@lysator.liu.se>
 
+       * aes-set-encrypt-key.c (aes_set_encrypt_key): Use LE_READ_UINT32.
+       Tabulate the needed "round constants".
+       (xtime): Deleted function.
+
        * aes-internal.h (SUBBYTE): Cast to uint32_t. Use B0, ..., B3
        macros.
 
index dfd102f722f664f01ddbc321b5ae7de54053c045..0f7ac25a6e067c09992fa8af7046be2deb3cac5d 100644 (file)
 #include "aes-internal.h"
 #include "macros.h"
 
-static unsigned
-xtime(unsigned x)
-{
-  assert (x < 0x100);
-
-  x <<= 1;
-  if (x & 0x100)
-    x ^= 0x11b;
-
-  assert (x < 0x100);
-
-  return x;
-}
-
 void
 aes_set_encrypt_key(struct aes_ctx *ctx,
                    unsigned keysize, const uint8_t *key)
 {
+  static const uint8_t rcon[10] = {
+    0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36,
+  };
   unsigned nk, nr, i, lastkey;
-  uint32_t temp, rcon;
+  uint32_t temp;
+  const uint8_t *rp;
 
   assert(keysize >= AES_MIN_KEY_SIZE);
   assert(keysize <= AES_MAX_KEY_SIZE);
@@ -72,25 +62,19 @@ aes_set_encrypt_key(struct aes_ctx *ctx,
 
   lastkey = (AES_BLOCK_SIZE/4) * (nr + 1);
   ctx->nrounds = nr;
-  rcon = 1;
-  for (i=0; i<nk; i++)
-    {
-      ctx->keys[i] = key[i*4] + (key[i*4+1]<<8) + (key[i*4+2]<<16) +
-       (key[i*4+3]<<24);
-    }
+
+  for (i=0, rp = rcon; i<nk; i++)
+    ctx->keys[i] = LE_READ_UINT32(key + i*4);
 
   for (i=nk; i<lastkey; i++)
     {
       temp = ctx->keys[i-1];
       if (i % nk == 0)
-       {
-         temp = SUBBYTE(ROTL32(24, temp), aes_sbox) ^ rcon;
-         rcon = (uint32_t)xtime((uint8_t)rcon&0xff);
-       }
+       temp = SUBBYTE(ROTL32(24, temp), aes_sbox) ^ *rp++;
+
       else if (nk > 6 && (i%nk) == 4)
-       {
-         temp = SUBBYTE(temp, aes_sbox);
-       }
+       temp = SUBBYTE(temp, aes_sbox);
+
       ctx->keys[i] = ctx->keys[i-nk] ^ temp;
     }
 }