From a75cfaf73ecb62448c0344e2842c61b178d84b7b Mon Sep 17 00:00:00 2001 From: bg Date: Fri, 9 Jan 2009 16:40:47 +0000 Subject: [PATCH] improving AES --- aes.c | 9 ------- aes.h | 2 -- aes128_dec.c | 5 +--- aes128_enc.c | 5 +--- aes192_dec.c | 5 +--- aes192_enc.c | 5 +--- aes256_dec.c | 5 +--- aes256_enc.c | 5 +--- aes_dec.c | 63 +++++++++++++++++++++++++------------------ aes_enc.c | 68 ++++++++++++++++++++++++----------------------- aes_keyschedule.c | 6 ----- 11 files changed, 78 insertions(+), 100 deletions(-) diff --git a/aes.c b/aes.c index 3fda3be..5f68a5d 100644 --- a/aes.c +++ b/aes.c @@ -28,13 +28,4 @@ #include #include "aes.h" -void aes_buffer2state(void* dest, void* src){ - uint8_t i,j; - for(i=0;i<4;++i){ - for(j=0;j<4;++j){ - ((uint8_t*)dest)[i*4+j] = ((uint8_t*)src)[j*4+i]; - } - } -} - diff --git a/aes.h b/aes.h index 5a6ee3e..260ca34 100644 --- a/aes.h +++ b/aes.h @@ -53,6 +53,4 @@ typedef struct{ uint8_t s[16]; } aes_cipher_state_t; -void aes_buffer2state(void* dest, void* src); - #endif diff --git a/aes128_dec.c b/aes128_dec.c index 8f6c49f..142ac3e 100644 --- a/aes128_dec.c +++ b/aes128_dec.c @@ -29,9 +29,6 @@ #include "aes_dec.h" void aes128_dec(void* buffer, aes128_ctx_t* ctx){ - aes_cipher_state_t state; - aes_buffer2state(state.s, buffer); - aes_decrypt_core(&state, (aes_genctx_t*)ctx, 10); - aes_buffer2state(buffer, state.s); + aes_decrypt_core(buffer, (aes_genctx_t*)ctx, 10); } diff --git a/aes128_enc.c b/aes128_enc.c index c0c900b..1e8ee31 100644 --- a/aes128_enc.c +++ b/aes128_enc.c @@ -29,9 +29,6 @@ #include "aes_enc.h" void aes128_enc(void* buffer, aes128_ctx_t* ctx){ - aes_cipher_state_t state; - aes_buffer2state(state.s, buffer); - aes_encrypt_core(&state, (aes_genctx_t*)ctx, 10); - aes_buffer2state(buffer, state.s); + aes_encrypt_core(buffer, (aes_genctx_t*)ctx, 10); } diff --git a/aes192_dec.c b/aes192_dec.c index 29d5aa9..1fabc03 100644 --- a/aes192_dec.c +++ b/aes192_dec.c @@ -29,9 +29,6 @@ #include "aes_dec.h" void aes192_dec(void* buffer, aes192_ctx_t* ctx){ - aes_cipher_state_t state; - aes_buffer2state(state.s, buffer); - aes_decrypt_core(&state, (aes_genctx_t*)ctx, 12); - aes_buffer2state(buffer, state.s); + aes_decrypt_core(buffer, (aes_genctx_t*)ctx, 12); } diff --git a/aes192_enc.c b/aes192_enc.c index a5d0830..f7e566f 100644 --- a/aes192_enc.c +++ b/aes192_enc.c @@ -29,9 +29,6 @@ #include "aes_enc.h" void aes192_enc(void* buffer, aes192_ctx_t* ctx){ - aes_cipher_state_t state; - aes_buffer2state(state.s, buffer); - aes_encrypt_core(&state, (aes_genctx_t*)ctx, 12); - aes_buffer2state(buffer, state.s); + aes_encrypt_core(buffer, (aes_genctx_t*)ctx, 12); } diff --git a/aes256_dec.c b/aes256_dec.c index eb80943..08cb481 100644 --- a/aes256_dec.c +++ b/aes256_dec.c @@ -29,9 +29,6 @@ #include "aes_dec.h" void aes256_dec(void* buffer, aes256_ctx_t* ctx){ - aes_cipher_state_t state; - aes_buffer2state(state.s, buffer); - aes_decrypt_core(&state, (aes_genctx_t*)ctx, 14); - aes_buffer2state(buffer, state.s); + aes_decrypt_core(buffer, (aes_genctx_t*)ctx, 14); } diff --git a/aes256_enc.c b/aes256_enc.c index 4d89602..7d71a0f 100644 --- a/aes256_enc.c +++ b/aes256_enc.c @@ -29,9 +29,6 @@ #include "aes_enc.h" void aes256_enc(void* buffer, aes256_ctx_t* ctx){ - aes_cipher_state_t state; - aes_buffer2state(state.s, buffer); - aes_encrypt_core(&state, (aes_genctx_t*)ctx, 14); - aes_buffer2state(buffer, state.s); + aes_encrypt_core(buffer, (aes_genctx_t*)ctx, 14); } diff --git a/aes_dec.c b/aes_dec.c index ebfdda9..48ad3b4 100644 --- a/aes_dec.c +++ b/aes_dec.c @@ -34,6 +34,17 @@ void aes_invshiftrow(void* data, uint8_t shift){ memcpy(data, tmp, 4); } +void aes_invshiftcol(void* data, uint8_t shift){ + uint8_t tmp[4]; + tmp[0] = ((uint8_t*)data)[ 0]; + tmp[1] = ((uint8_t*)data)[ 4]; + tmp[2] = ((uint8_t*)data)[ 8]; + tmp[3] = ((uint8_t*)data)[12]; + ((uint8_t*)data)[ 0] = tmp[(4-shift+0)&3]; + ((uint8_t*)data)[ 4] = tmp[(4-shift+1)&3]; + ((uint8_t*)data)[ 8] = tmp[(4-shift+2)&3]; + ((uint8_t*)data)[12] = tmp[(4-shift+3)&3]; +} static void aes_dec_round(aes_cipher_state_t* state, const aes_roundkey_t* k){ uint8_t tmp[16]; @@ -44,31 +55,31 @@ void aes_dec_round(aes_cipher_state_t* state, const aes_roundkey_t* k){ } /* mixColums */ for(i=0; i<4; ++i){ - state->s[4*0+i] = - gf256mul(0xe, tmp[4*0+i], 0x1b) - ^ gf256mul(0xb, tmp[4*1+i], 0x1b) - ^ gf256mul(0xd, tmp[4*2+i], 0x1b) - ^ gf256mul(0x9, tmp[4*3+i], 0x1b); - state->s[4*1+i] = - gf256mul(0x9, tmp[4*0+i], 0x1b) - ^ gf256mul(0xe, tmp[4*1+i], 0x1b) - ^ gf256mul(0xb, tmp[4*2+i], 0x1b) - ^ gf256mul(0xd, tmp[4*3+i], 0x1b); - state->s[4*2+i] = - gf256mul(0xd, tmp[4*0+i], 0x1b) - ^ gf256mul(0x9, tmp[4*1+i], 0x1b) - ^ gf256mul(0xe, tmp[4*2+i], 0x1b) - ^ gf256mul(0xb, tmp[4*3+i], 0x1b); - state->s[4*3+i] = - gf256mul(0xb, tmp[4*0+i], 0x1b) - ^ gf256mul(0xd, tmp[4*1+i], 0x1b) - ^ gf256mul(0x9, tmp[4*2+i], 0x1b) - ^ gf256mul(0xe, tmp[4*3+i], 0x1b); + state->s[4*i+0] = + gf256mul(0xe, tmp[4*i+0], 0x1b) + ^ gf256mul(0xb, tmp[4*i+1], 0x1b) + ^ gf256mul(0xd, tmp[4*i+2], 0x1b) + ^ gf256mul(0x9, tmp[4*i+3], 0x1b); + state->s[4*i+1] = + gf256mul(0x9, tmp[4*i+0], 0x1b) + ^ gf256mul(0xe, tmp[4*i+1], 0x1b) + ^ gf256mul(0xb, tmp[4*i+2], 0x1b) + ^ gf256mul(0xd, tmp[4*i+3], 0x1b); + state->s[4*i+2] = + gf256mul(0xd, tmp[4*i+0], 0x1b) + ^ gf256mul(0x9, tmp[4*i+1], 0x1b) + ^ gf256mul(0xe, tmp[4*i+2], 0x1b) + ^ gf256mul(0xb, tmp[4*i+3], 0x1b); + state->s[4*i+3] = + gf256mul(0xb, tmp[4*i+0], 0x1b) + ^ gf256mul(0xd, tmp[4*i+1], 0x1b) + ^ gf256mul(0x9, tmp[4*i+2], 0x1b) + ^ gf256mul(0xe, tmp[4*i+3], 0x1b); } /* shiftRows */ - aes_invshiftrow(state->s+4, 1); - aes_invshiftrow(state->s+8, 2); - aes_invshiftrow(state->s+12, 3); + aes_invshiftcol(state->s+1, 1); + aes_invshiftcol(state->s+2, 2); + aes_invshiftcol(state->s+3, 3); /* subBytes */ for(i=0; i<16; ++i){ state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]); @@ -84,9 +95,9 @@ void aes_dec_firstround(aes_cipher_state_t* state, const aes_roundkey_t* k){ state->s[i] ^= k->ks[i]; } /* shiftRows */ - aes_invshiftrow(state->s+4, 1); - aes_invshiftrow(state->s+8, 2); - aes_invshiftrow(state->s+12, 3); + aes_invshiftcol(state->s+1, 1); + aes_invshiftcol(state->s+2, 2); + aes_invshiftcol(state->s+3, 3); /* subBytes */ for(i=0; i<16; ++i){ state->s[i] = pgm_read_byte(aes_invsbox+state->s[i]); diff --git a/aes_enc.c b/aes_enc.c index 7932e52..244dcff 100644 --- a/aes_enc.c +++ b/aes_enc.c @@ -33,14 +33,16 @@ #include "aes_enc.h" #include - -void aes_shiftrow(void* data, uint8_t shift){ +void aes_shiftcol(void* data, uint8_t shift){ uint8_t tmp[4]; - tmp[0] = ((uint8_t*)data)[(0+shift)&3]; - tmp[1] = ((uint8_t*)data)[(1+shift)&3]; - tmp[2] = ((uint8_t*)data)[(2+shift)&3]; - tmp[3] = ((uint8_t*)data)[(3+shift)&3]; - memcpy(data, tmp, 4); + tmp[0] = ((uint8_t*)data)[ 0]; + tmp[1] = ((uint8_t*)data)[ 4]; + tmp[2] = ((uint8_t*)data)[ 8]; + tmp[3] = ((uint8_t*)data)[12]; + ((uint8_t*)data)[ 0] = tmp[(shift+0)&3]; + ((uint8_t*)data)[ 4] = tmp[(shift+1)&3]; + ((uint8_t*)data)[ 8] = tmp[(shift+2)&3]; + ((uint8_t*)data)[12] = tmp[(shift+3)&3]; } #define GF256MUL_1(a) (a) @@ -56,31 +58,31 @@ void aes_enc_round(aes_cipher_state_t* state, const aes_roundkey_t* k){ tmp[i] = pgm_read_byte(aes_sbox+state->s[i]); } /* shiftRows */ - aes_shiftrow(tmp+4, 1); - aes_shiftrow(tmp+8, 2); - aes_shiftrow(tmp+12, 3); + aes_shiftcol(tmp+1, 1); + aes_shiftcol(tmp+2, 2); + aes_shiftcol(tmp+3, 3); /* mixColums */ for(i=0; i<4; ++i){ - state->s[4*0+i] = - GF256MUL_2(tmp[4*0+i]) - ^ GF256MUL_3(tmp[4*1+i]) - ^ GF256MUL_1(tmp[4*2+i]) - ^ GF256MUL_1(tmp[4*3+i]); - state->s[4*1+i] = - GF256MUL_1(tmp[4*0+i]) - ^ GF256MUL_2(tmp[4*1+i]) - ^ GF256MUL_3(tmp[4*2+i]) - ^ GF256MUL_1(tmp[4*3+i]); - state->s[4*2+i] = - GF256MUL_1(tmp[4*0+i]) - ^ GF256MUL_1(tmp[4*1+i]) - ^ GF256MUL_2(tmp[4*2+i]) - ^ GF256MUL_3(tmp[4*3+i]); - state->s[4*3+i] = - GF256MUL_3(tmp[4*0+i]) - ^ GF256MUL_1(tmp[4*1+i]) - ^ GF256MUL_1(tmp[4*2+i]) - ^ GF256MUL_2(tmp[4*3+i]); + state->s[4*i+0] = + GF256MUL_2(tmp[4*i+0]) + ^ GF256MUL_3(tmp[4*i+1]) + ^ GF256MUL_1(tmp[4*i+2]) + ^ GF256MUL_1(tmp[4*i+3]); + state->s[4*i+1] = + GF256MUL_1(tmp[4*i+0]) + ^ GF256MUL_2(tmp[4*i+1]) + ^ GF256MUL_3(tmp[4*i+2]) + ^ GF256MUL_1(tmp[4*i+3]); + state->s[4*i+2] = + GF256MUL_1(tmp[4*i+0]) + ^ GF256MUL_1(tmp[4*i+1]) + ^ GF256MUL_2(tmp[4*i+2]) + ^ GF256MUL_3(tmp[4*i+3]); + state->s[4*i+3] = + GF256MUL_3(tmp[4*i+0]) + ^ GF256MUL_1(tmp[4*i+1]) + ^ GF256MUL_1(tmp[4*i+2]) + ^ GF256MUL_2(tmp[4*i+3]); } /* addKey */ @@ -98,9 +100,9 @@ void aes_enc_lastround(aes_cipher_state_t* state,const aes_roundkey_t* k){ state->s[i] = pgm_read_byte(aes_sbox+state->s[i]); } /* shiftRows */ - aes_shiftrow(state->s+4, 1); - aes_shiftrow(state->s+8, 2); - aes_shiftrow(state->s+12, 3); + aes_shiftcol(state->s+1, 1); + aes_shiftcol(state->s+2, 2); + aes_shiftcol(state->s+3, 3); /* keyAdd */ for(i=0; i<16; ++i){ state->s[i] ^= k->ks[i]; diff --git a/aes_keyschedule.c b/aes_keyschedule.c index 72fd679..d53c406 100644 --- a/aes_keyschedule.c +++ b/aes_keyschedule.c @@ -73,12 +73,6 @@ void aes_init(const void* key, uint16_t keysize_b, aes_genctx_t* ctx){ ((uint32_t*)(ctx->key[0].ks))[i] = ((uint32_t*)(ctx->key[0].ks))[i-nk] ^ *((uint32_t*)tmp); } - - uint8_t buffer[16]; - for(i=0; ikey[i].ks, 16); - aes_buffer2state(ctx->key[i].ks, buffer); - } } void aes128_init(const void* key, aes128_ctx_t* ctx){ -- 2.39.5