From 8cf982222913b9765561e8dbd8a975bc3ea1134b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se> Date: Fri, 13 Apr 2012 17:00:50 +0200 Subject: [PATCH] Simplified aes_set_encrypt_key. --- ChangeLog | 4 ++++ aes-set-encrypt-key.c | 40 ++++++++++++---------------------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index ff613329..2b4a4c32 100644 --- 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. diff --git a/aes-set-encrypt-key.c b/aes-set-encrypt-key.c index dfd102f7..0f7ac25a 100644 --- a/aes-set-encrypt-key.c +++ b/aes-set-encrypt-key.c @@ -34,26 +34,16 @@ #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; } } -- GitLab