#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];
- }
- }
-}
-
uint8_t s[16];
} aes_cipher_state_t;
-void aes_buffer2state(void* dest, void* src);
-
#endif
#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);
}
#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);
}
#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);
}
#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);
}
#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);
}
#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);
}
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];
}
/* 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]);
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]);
#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)
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 */
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];
((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){