X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=aes%2Faes_keyschedule.c;h=fff0700150c63f190c5c6a0ce6042aa744d5426a;hb=701cee0d98aab48dd3192c8cc7c77eb42581bc56;hp=9e58eb578320de6ae738aa34cad0b521d487fb3a;hpb=4f50c75ee5a6cc88bf7ea71957ed509e298e6c25;p=avr-crypto-lib.git diff --git a/aes/aes_keyschedule.c b/aes/aes_keyschedule.c index 9e58eb5..fff0700 100644 --- a/aes/aes_keyschedule.c +++ b/aes/aes_keyschedule.c @@ -19,10 +19,10 @@ /** * \file aes_keyschedule.c * \email daniel.otte@rub.de - * \author Daniel Otte + * \author Daniel Otte * \date 2008-12-30 * \license GPLv3 or later - * + * */ #include @@ -42,39 +42,42 @@ void aes_rotword(void* a){ ((uint8_t*)a)[3] = t; } -uint8_t rc_tab[] PROGMEM = { 0x01, 0x02, 0x04, 0x08, +const uint8_t rc_tab[] PROGMEM = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; void aes_init(const void* key, uint16_t keysize_b, aes_genctx_t* ctx){ uint8_t hi,i,nk, next_nk; uint8_t rc=0; - uint8_t tmp[4]; + union { + uint32_t v32; + uint8_t v8[4]; + } tmp; nk=keysize_b>>5; /* 4, 6, 8 */ hi=4*(nk+6+1); memcpy(ctx, key, keysize_b/8); next_nk = nk; for(i=nk;ikey[0].ks))[i-1]; + tmp.v32 = ((uint32_t*)(ctx->key[0].ks))[i-1]; if(i!=next_nk){ if(nk==8 && i%8==4){ - tmp[0] = pgm_read_byte(aes_sbox+tmp[0]); - tmp[1] = pgm_read_byte(aes_sbox+tmp[1]); - tmp[2] = pgm_read_byte(aes_sbox+tmp[2]); - tmp[3] = pgm_read_byte(aes_sbox+tmp[3]); + tmp.v8[0] = pgm_read_byte(aes_sbox+tmp.v8[0]); + tmp.v8[1] = pgm_read_byte(aes_sbox+tmp.v8[1]); + tmp.v8[2] = pgm_read_byte(aes_sbox+tmp.v8[2]); + tmp.v8[3] = pgm_read_byte(aes_sbox+tmp.v8[3]); } } else { next_nk += nk; - aes_rotword(tmp); - tmp[0] = pgm_read_byte(aes_sbox+tmp[0]); - tmp[1] = pgm_read_byte(aes_sbox+tmp[1]); - tmp[2] = pgm_read_byte(aes_sbox+tmp[2]); - tmp[3] = pgm_read_byte(aes_sbox+tmp[3]); - tmp[0] ^= pgm_read_byte(rc_tab+rc); + aes_rotword(&(tmp.v32)); + tmp.v8[0] = pgm_read_byte(aes_sbox+tmp.v8[0]); + tmp.v8[1] = pgm_read_byte(aes_sbox+tmp.v8[1]); + tmp.v8[2] = pgm_read_byte(aes_sbox+tmp.v8[2]); + tmp.v8[3] = pgm_read_byte(aes_sbox+tmp.v8[3]); + tmp.v8[0] ^= pgm_read_byte(rc_tab+rc); rc++; } ((uint32_t*)(ctx->key[0].ks))[i] = ((uint32_t*)(ctx->key[0].ks))[i-nk] - ^ *((uint32_t*)tmp); + ^ tmp.v32; } }