X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=aes_dec.c;h=8c476de0dbe70d3105eda6412befb75f334e78ba;hb=402f8b98e8bd4ca12bbac8fe6ccd07e594015a63;hp=3b546411aba18aa7ae082a8aad7565ee9939213c;hpb=d6a35f05fd2b5ee79d5ad8424434ad1a068be453;p=avr-crypto-lib.git diff --git a/aes_dec.c b/aes_dec.c index 3b54641..8c476de 100644 --- a/aes_dec.c +++ b/aes_dec.c @@ -1,7 +1,7 @@ /* aes.c */ /* - This file is part of the Crypto-avr-lib/microcrypt-lib. - Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de) + This file is part of the AVR-Crypto-Lib. + Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,41 +34,66 @@ 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]; uint8_t i; + uint8_t t,u,v,w; /* keyAdd */ for(i=0; i<16; ++i){ tmp[i] = state->s[i] ^ k->ks[i]; } /* 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); + t = tmp[4*i+3] ^ tmp[4*i+2]; + u = tmp[4*i+1] ^ tmp[4*i+0]; + v = t ^ u; + v = gf256mul(0x09, v, 0x1b); + w = v ^ gf256mul(0x04, tmp[4*i+2] ^ tmp[4*i+0], 0x1b); + v = v ^ gf256mul(0x04, tmp[4*i+3] ^ tmp[4*i+1], 0x1b); + state->s[4*i+3] = tmp[4*i+3] ^ v ^ gf256mul(0x02, tmp[4*i+0] ^ tmp[4*i+3], 0x1b); + state->s[4*i+2] = tmp[4*i+2] ^ w ^ gf256mul(0x02, t, 0x1b); + state->s[4*i+1] = tmp[4*i+1] ^ v ^ gf256mul(0x02, tmp[4*i+2] ^ tmp[4*i+1], 0x1b); + state->s[4*i+0] = tmp[4*i+0] ^ w ^ gf256mul(0x02, u, 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 +109,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]);