void aes_enc(uint8_t state[16],uint8_t cipher[16],uint8_t ekey[240])

{

	uint8_t iteration = 0;
	uint8_t x,y;
	uint8_t sub[4][4];
	uint8_t shift[4][4];
	uint8_t mix[4][4];
	uint8_t round[4][4];
	uint8_t state_grid[4][4];
	uint8_t result[4][4];

	state_grid[0][0] = state[0];
	state_grid[0][1] = state[1];
	state_grid[0][2] = state[2];
	state_grid[0][3] = state[3];
	state_grid[1][0] = state[4];
	state_grid[1][1] = state[5];
	state_grid[1][2] = state[6];
	state_grid[1][3] = state[7];
	state_grid[2][0] = state[8];
	state_grid[2][1] = state[9];
	state_grid[2][2] = state[10];
	state_grid[2][3] = state[11];
	state_grid[3][0] = state[12];
	state_grid[3][1] = state[13];
	state_grid[3][2] = state[14];
	state_grid[3][3] = state[15];

	addroundkey(state_grid,0,sub,ekey);
	loop_main : for(iteration = 1; iteration < nr; iteration++)
	  {

	   subbytes(sub,shift);
	   shift_row_enc(shift,mix);
	   mixcolumn(mix,round);
	   addroundkey(round,iteration,sub,ekey);
	  }
	  subbytes(sub,shift);
	  shift_row_enc(shift,round);
	  addroundkey(round,nr,result,ekey);

	  cipher[0] = result[0][0];
	  cipher[1] = result[0][1];
	  cipher[2] = result[0][2];
	  cipher[3] = result[0][3];
	  cipher[4] = result[1][0];
	  cipher[5] = result[1][1];
	  cipher[6] = result[1][2];
	  cipher[7] = result[1][3];
	  cipher[8] = result[2][0];
	  cipher[9] = result[2][1];
	  cipher[10] = result[2][2];
	  cipher[11] = result[2][3];
	  cipher[12] = result[3][0];
	  cipher[13] = result[3][1];
	  cipher[14] = result[3][2];
	  cipher[15] = result[3][3];

}
Beispiel #2
0
static Aes aes_encrypt(OE oe, Aes key, Aes in) {
  Aes plx = Aes_new(oe);
  uint round = 0;
  Aes tmp = 0;
  *plx = *in;

  /*
    Add the round key to the state to obtain the final state in
    {plx} of this round.
  */
  plx=add_round_key(oe,tmp=plx,key);
  //oe->p("Final state from this round:");
  //print_aes(oe,plx);
  Aes_destroy(oe,&tmp);


  for(round = 1;round < 11;++round) {
    byte b[32] = {0};
    /*
      Run the key schedule getting the new key in {key1} from the
      round number and the old key previously (e.g. before this line)
      stored in {key1}.
    */
    key = key_schedule(oe,tmp=key,round);
    Aes_destroy(oe,&tmp);

    /*
      Print what round we are at
    */
    //osal_sprintf(b,"Round %u:",round);
    //oe->p(b);

    /*
      Perform the S-Box on the state i {plx} which is out AES state.
    */
    plx=subbytes(oe,tmp=plx);
    //oe->p("After SubBytes:");
    //print_aes(oe,plx);
    Aes_destroy(oe,&tmp);

    /*
      Perform the shift rows step from {plx} assigning the new state
      to {plx}.

    plx = shift_rows(oe,plx);
    oe->p("After Shift rows:");
    print_aes(oe,plx);
    */

    /*
      Perform the mix columns step from {plx} assignment the new state
      to {plx} again.
     */
    if (round < 10)  {
      //plx = mix_columns(oe,plx);
      plx = shift_row_mix_cols(oe,tmp=plx);
      Aes_destroy(oe,&tmp);
    } else {
      plx = shift_rows(oe, tmp=plx);
      Aes_destroy(oe,&tmp);
    }
    //oe->p("After linear transform: ");
    //print_aes(oe,plx);

    /*
      Add the round key to the state to obtain the final state in
      {plx} of this round.
     */
    plx=add_round_key(oe,tmp=plx,key);
    //oe->p("Final state from this round:");
    //print_aes(oe,plx);
    Aes_destroy(oe,&tmp);

    /*
      Print the round key.
     */
    //oe->p("Key");
    //print_aes(oe,key);
  }

  return plx;
}