]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
improving AES
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Fri, 9 Jan 2009 16:40:47 +0000 (16:40 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Fri, 9 Jan 2009 16:40:47 +0000 (16:40 +0000)
aes.c
aes.h
aes128_dec.c
aes128_enc.c
aes192_dec.c
aes192_enc.c
aes256_dec.c
aes256_enc.c
aes_dec.c
aes_enc.c
aes_keyschedule.c

diff --git a/aes.c b/aes.c
index 3fda3beaf7013c6ff712369c76140061f55c9adb..5f68a5de00cfb966266887f761f0e1a14121b145 100644 (file)
--- a/aes.c
+++ b/aes.c
 #include <stdint.h>
 #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 5a6ee3e3764d517db9027e907b3a097ec8dfa3e4..260ca34c4f618c4f75dc4f9700f41092057ac53e 100644 (file)
--- 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
index 8f6c49f59e9f9315c3a2ab54c4ac3b364af645b9..142ac3e724c4d12f8460a8b69a0b1be91f952b42 100644 (file)
@@ -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);
 }
 
index c0c900bf94daedd497fddfeba4be9cb1ebe32da6..1e8ee3137e32a7823b8bb1721093cf316d7f9130 100644 (file)
@@ -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);
 }
 
index 29d5aa9dcbe50168de258164b6c4b862fa628630..1fabc03f3ec0109beb04f7080c9987d0889664b3 100644 (file)
@@ -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);
 }
 
index a5d083064fb6ca0f833e12b9d0acffc52fc49ca0..f7e566f2c744b59494632faafd2f311d53a7fc0b 100644 (file)
@@ -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);
 }
 
index eb809434cccbeb0ddb4adf491266dcbdc026bca0..08cb4818fa10dbfc180b5405dc112f95ccb8ab84 100644 (file)
@@ -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);
 }
 
index 4d89602af816c262e28db0292bad5cef169988ea..7d71a0f3d0e163b10100e46b997b6c890e41ac38 100644 (file)
@@ -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);
 }
 
index ebfdda9fb25d1ef46cf2ea2c0c23de99b47a8df8..48ad3b47986b61a1f6b764b3f40d136f20767d7f 100644 (file)
--- 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]);
index 7932e528d48218511191ee51cbf024a9b8c19798..244dcff5e2da381eb1945479af70cd12a1717c9b 100644 (file)
--- a/aes_enc.c
+++ b/aes_enc.c
 #include "aes_enc.h"
 #include <avr/pgmspace.h>
 
-
-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];
index 72fd6798501f2f79803cdfdef649a3f7a49d70c6..d53c406cf27e48e798ea940f2d0da56f1a00ccc8 100644 (file)
@@ -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; i<nk+7; ++i){
-               memcpy(buffer, ctx->key[i].ks, 16);
-               aes_buffer2state(ctx->key[i].ks, buffer);
-       }
 }
 
 void aes128_init(const void* key, aes128_ctx_t* ctx){