Пример #1
0
Файл: aes.cpp Проект: iyu9/mylib
void cipher(uint8_t *in, uint8_t *out, uint8_t *w) {

	uint8_t state[4*Nb];
	uint8_t r, i, j;

	for (i = 0; i < 4; i++) {
		for (j = 0; j < Nb; j++) {
			state[Nb*i+j] = in[i+4*j];
		}
	}

	add_round_key(state, w, 0);

	for (r = 1; r < Nr; r++) {
		sub_bytes(state);
		shift_rows(state);
		mix_columns(state);
		add_round_key(state, w, r);
	}

	sub_bytes(state);
	shift_rows(state);
	add_round_key(state, w, Nr);

	for (i = 0; i < 4; i++) {
		for (j = 0; j < Nb; j++) {
			out[i+4*j] = state[Nb*i+j];
		}
	}
}
Пример #2
0
void cipher(uint8_t *in, uint8_t **key_schedule, uint8_t n_k, uint8_t n_r) {
  uint8_t *state;

  state = malloc(BLOCK_LENGTH_IN_BYTES * sizeof(uint8_t));
  if(state == NULL) {
    printf("malloc returned NULL in cipher");
    exit(1);
  }
  memcpy(state, in, BLOCK_LENGTH_IN_BYTES * sizeof(uint8_t));

  if (DEBUG) {
    printf("\nRound 0\n");
    debug_print_block(state, "  Start: ");
  }

  add_round_key(state, key_schedule, 0);
  if (DEBUG) {
    debug_print_key_schedule(key_schedule, 0);
  }

  for (int rnd = 1; rnd < n_r; rnd++) {
    if (DEBUG) printf("\n\nRound %2d\n", rnd);

    debug_print_block(state, "  Start: ");

    sub_bytes(state);
    debug_print_block(state, "  Subst: ");

    shift_rows(state);
    debug_print_block(state, "  Shift: ");

    mix_columns(state);
    debug_print_block(state, "  Mxcol: ");

    add_round_key(state, key_schedule, rnd);
    debug_print_key_schedule(key_schedule, rnd);

  }

  if (DEBUG) printf("\n\nRound 10\n");
  debug_print_block(state, "  Start: ");

  sub_bytes(state);
  debug_print_block(state, "  Subst: ");

  shift_rows(state);
  debug_print_block(state, "  Shift: ");

  add_round_key(state, key_schedule, n_r);

  debug_print_key_schedule(key_schedule, 10);
  debug_print_block(state, "\n\nCIPHER\n");
}
Пример #3
0
// Cipher is the main function that encrypts the PlainText.
void cipher(int offset)
{
	int i, j, round = 0;

	//Copy the input PlainText to state array.
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			state[i][j] = temp_buffer[i][j];
		}
	}

#ifdef SHIFT_ROWS
	for (round = 0; round <= nr; round++)
		shift_rows();
#elif defined(MIX_COLUMNS)
	for (round = 0; round <= nr; round++)
		mix_columns();
#elif defined(ADD_ROUND_KEY)
	for (round = 0; round <= nr; round++)
		add_round_key(nr);
#elif defined(SUB_BYTES)
	for (round = 0; round <= nr; round++)
		sub_bytes();
#else
	// Add the First round key to the state before starting the rounds.
	add_round_key(0);

	// There will be nr rounds.
	// The first nr-1 rounds are identical.
	// These nr-1 rounds are executed in the loop below.
	for (round = 1; round < nr; round++) {
		sub_bytes();
		shift_rows();
		mix_columns();
		add_round_key(round);
	}

	// The last round is given below.
	// The MixColumns function is not here in the last round.
	sub_bytes();
	shift_rows();
	add_round_key(nr);
#endif
	// The encryption process is over.
	// Copy the state array to output array.
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			output_file_buffer[offset + i * 4 + j] = state[i][j];
		}
	}
}
Пример #4
0
void aes_enc(aes_ctx_t *ctx) {
	int i;

	add_roundkey(ctx->state, ctx->expkey, 0);
	for(i = 1; i < NR; i++) {
		sub_bytes(ctx->state);
		shift_rows(ctx->state);
		mix_columns(ctx->state, FOR_MIX);
		add_roundkey(ctx->state, ctx->expkey, i);
	}
	sub_bytes(ctx->state);
	shift_rows(ctx->state);
	add_roundkey(ctx->state, ctx->expkey, NR);
}
Пример #5
0
void encode_block(BYTE* counter, BYTE* dst, BYTE round_key[ROUND_KEY_SIZE][BLOCK_SIZE]){
    unsigned char temp[BLOCK_SIZE];

    xor_key(counter, temp, round_key[0]);
    for(int i = 1; i < ROUNDS; i++){
        sub_bytes(temp, dst);
        shift_rows(dst, temp);
        mix_columns(temp, dst);
        xor_key(dst, temp, round_key[i]);
    }
    sub_bytes(temp,dst);
    shift_rows(dst,temp);
    xor_key(temp,dst,round_key[ROUNDS]);
}
Пример #6
0
Файл: aes.c Проект: behemot/pm
void aes_encrypt(aes_ctx *ctx, const byte *in, byte *out)
{
	int i;
	byte state[16];
	memcpy(state, in, 16);
	add_round_key(state, ctx->sub_key);
	for (i = 1; i < 10; i++) {
		sub_bytes(state);
		shift_rows(state);
		mix_columns(state);
		add_round_key(state, ctx->sub_key + (i * 16));
	}
	sub_bytes(state);
	shift_rows(state);
	add_round_key(state, ctx->sub_key + (i * 16));
	memcpy(out, state, 16);
}
Пример #7
0
void lnc_aes_enc(void *context) {
	lnc_aes_ctx_t *ctx = context;
	int i;
	uint32_t *expkey = ctx->expkey;
	uint8_t *state = ctx->state;

	add_roundkey(state, expkey, 0);

	for(i = 1; i < Nr; i++) {
		sub_bytes(state);
		shift_rows(state);
		mix_columns(state, FOR_MIX);
		add_roundkey(state, expkey, i);
	}
	sub_bytes(state);
	shift_rows(state);
	add_roundkey(state, expkey, Nr);
}
Пример #8
0
static void aes_main(aes_key *key, uint8_t *state)
{
	int i = 0;
	uint8_t rk[16];

	create_round_key(key->data, rk);
	add_round_key(state, rk);

	for (i = 1; i < key->nbr; i++) {
		create_round_key(key->data + 16 * i, rk);
		shift_rows(state);
		mix_columns(state);
		add_round_key(state, rk);
	}

	create_round_key(key->data + 16 * key->nbr, rk);
	shift_rows(state);
	add_round_key(state, rk);
}
Пример #9
0
/**
 * This function is the overall application of the AES cipher in the context of
 * a single state block matrix.
 *
 * For each state, we perform the four AES functions several times, in what are
 * referred to as "rounds". The number of rounds we do depends on the size of 
 * the encryption key. 
 */
void aes_cipher(uint8_t **state, aes_key_t *key) {
  int round, num_rounds;

  num_rounds = (key->size / 4) + 6;
  
  add_round_key(state, 0, key->exp_block);

  for (round = 1; round < num_rounds; round++) {
	sub_bytes(state);
	shift_rows(state);
	mix_columns(state);
	add_round_key(state, round, key->exp_block);
  }

  /* Don't mix columns on the last round */
  sub_bytes(state);
  shift_rows(state);
  add_round_key(state, num_rounds, key->exp_block);
}
Пример #10
0
void Aes256::encrypt(unsigned char* buffer)
{
    unsigned char i, rcon;

    copy_key();
    add_round_key(buffer, 0);
    for (i = 1, rcon = 1; i < NUM_ROUNDS; ++i)
    {
        sub_bytes(buffer);
        shift_rows(buffer);
        mix_columns(buffer);
        if (!(i & 1))
            expand_enc_key(&rcon);
        add_round_key(buffer, i);
    }
    sub_bytes(buffer);
    shift_rows(buffer);
    expand_enc_key(&rcon);
    add_round_key(buffer, i);
}
Пример #11
0
void Aes256Encoder::Encrypt(MemoryData& rkey, const MemoryData& key,const MemoryData& salt, unsigned char* buffer)
{
	unsigned char i, rcon;

	copy_key(rkey, key, salt);
	add_round_key(rkey.MutableData(), buffer, 0);
	for (i = 1, rcon = 1; i < RoundCount; ++i)
	{
		sub_bytes(buffer);
		shift_rows(buffer);
		mix_columns(buffer);
		if (!(i & 1))
			expand_enc_key(rkey.MutableData(), &rcon);
		add_round_key(rkey.MutableData(), buffer, i);
	}
	sub_bytes(buffer);
	shift_rows(buffer);
	expand_enc_key(rkey.MutableData(), &rcon);
	add_round_key(rkey.MutableData(), buffer, i);
}
Пример #12
0
void aes128_ecb_encrypt(const uint8_t *plaintext, const uint8_t *key, uint8_t *ciphertext) {
    int round;
    
    memcpy(ciphertext, plaintext, 16);
    
    key_schedule(key, round_keys);
    add_round_key(ciphertext, round_keys);
    
    for (round = 1; round < N_ROUNDS; round++) {
        sub_bytes(ciphertext);
        shift_rows(ciphertext);
        mix_columns(ciphertext);
        add_round_key(ciphertext, round_keys + 16 * round);
        
    }
    
    sub_bytes(ciphertext);
    shift_rows(ciphertext);
    add_round_key(ciphertext, round_keys + 160);
    
}
Пример #13
0
Файл: aes.c Проект: noahp/aes_c
// Encrypt a single 128 bit block by a 128 bit key using AES
// http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
void AES_Encrypt(unsigned char *data, unsigned char *key) {
  int i; // To count the rounds

  // Key expansion
  unsigned char keys[176];
  expand_key(key,keys);

  // First Round
  xor_round_key(data,keys,0);

  // Middle rounds
  for(i=0; i<9; i++) {
    sub_bytes(data,16);
    shift_rows(data);
    mix_cols(data);
    xor_round_key(data, keys, i+1);
  }

  // Final Round
  sub_bytes(data,16);
  shift_rows(data);
  xor_round_key(data, keys, 10);
}
Пример #14
0
void test_shift_rows_back()
{
	uint8_t state[16] = {
		0x01,0x02,0x03,0x04,
		0x05,0x06,0x07,0x08,
		0x09,0x0a,0x0b,0x0c,
		0x0d,0x0e,0x0f,0x10
	};
	uint8_t tmp_state[16];
	memcpy(tmp_state, state, sizeof(tmp_state));
	shift_rows(tmp_state);
	inv_shift_rows(tmp_state);
	int ret = memcmp(tmp_state, state, sizeof(tmp_state));
	CU_ASSERT_EQUAL(ret, 0);
}
Пример #15
0
// Encrypt a single 128 bit block by a 128 bit key using AES
// http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
void EncryptAES(byte *msg, byte *key, byte *c) {
  int i; // To count the rounds
  
  // Key expansion
  byte keys[176];
  expand_key(key,keys);
  
  // First Round
  memcpy(c, msg, 16);
  xor_round_key(c,keys,0);

  // Middle rounds
  for(i=0; i<9; i++) {
    sub_bytes(c,16);
    shift_rows(c);
    mix_cols(c);
    xor_round_key(c, keys, i+1);
  }

  // Final Round
  sub_bytes(c,16);
  shift_rows(c);
  xor_round_key(c, keys, 10);
}
Пример #16
0
void test_shift_rows(){
  char msg[] = "0123456789ABCDE";
  print_block(msg, 16);

printf("\n");
  shift_rows(msg, 16);
  print_block(msg, 16);

  printf("\n");
  inv_shift_rows(msg, 16);
  print_block(msg, 16);

// TODO: cannot use strcmp because it stops on 0!
// TODO: assert inv is equal to original
  assert( strcmp(msg, "0123456789ABCDE") == 0 );
}
Пример #17
0
void test_shift_rows()
{
	uint8_t state[16] = {
		0x01,0x02,0x03,0x04,
		0x05,0x06,0x07,0x08,
		0x09,0x0a,0x0b,0x0c,
		0x0d,0x0e,0x0f,0x10
	};
	uint8_t ret_state[16] = {
		0x01,0x06,0x0b,0x10,
		0x05,0x0a,0x0f,0x04,
		0x09,0x0e,0x03,0x08,
		0x0d,0x02,0x07,0x0c
	};
	shift_rows(state);
	int ret = memcmp(ret_state, state, sizeof(ret_state));
	CU_ASSERT_EQUAL(ret, 0);
}
Пример #18
0
void shift_rows_time()	{
	// runs of 100000 to get mean for shift_rows_time function
	uint32_t state[64] = {0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff};
	int bit;
	clock_t start, end, result = 0;
	mach_timebase_info_data_t info;
	mach_timebase_info(&info);
	for(int run = 0; run < 100000; run++)	{
		start = mach_absolute_time();
		shift_rows(state, 1);
		end = mach_absolute_time();
		result += (end - start);
		for(bit = 0; bit < 64; bit++)	{
			state[bit] = 0xffffffff;
		}
	}
	printf("shift_rows time: %lu\n", (result / 100000) * info.numer / info.denom);
}
Пример #19
0
/* encrypt one 128 bit block */
void
nv_aes_encrypt(u_int8_t *in, u_int8_t *expkey, u_int8_t *out)
{
	u_int8_t  state[NVAES_STATECOLS * 4];
	u_int32_t round;

	memcpy(state, in, NVAES_STATECOLS * 4);
	add_round_key((u_int32_t *)state, (u_int32_t *)expkey);

	for (round = 1; round < NVAES_ROUNDS + 1; round++) {
		if (round < NVAES_ROUNDS)
			mix_sub_columns (state);
		else
			shift_rows (state);

		add_round_key((u_int32_t *)state, (u_int32_t *)expkey +
					round * NVAES_STATECOLS);
	}

	memcpy(out, state, sizeof(state));
}
Пример #20
0
void last_rounds(uint32_t state[64], uint32_t key[64], uint32_t RC[11][64]){ 
uint8_t r = 0;
r = 6;
for(;(r < 11);) { 
state[0] = (RC[r][0] ^ key[0]) ^ state[0];
state[1] = (RC[r][1] ^ key[1]) ^ state[1];
state[2] = (RC[r][2] ^ key[2]) ^ state[2];
state[3] = (RC[r][3] ^ key[3]) ^ state[3];
state[4] = (RC[r][4] ^ key[4]) ^ state[4];
state[5] = (RC[r][5] ^ key[5]) ^ state[5];
state[6] = (RC[r][6] ^ key[6]) ^ state[6];
state[7] = (RC[r][7] ^ key[7]) ^ state[7];
state[8] = (RC[r][8] ^ key[8]) ^ state[8];
state[9] = (RC[r][9] ^ key[9]) ^ state[9];
state[10] = (RC[r][10] ^ key[10]) ^ state[10];
state[11] = (RC[r][11] ^ key[11]) ^ state[11];
state[12] = (RC[r][12] ^ key[12]) ^ state[12];
state[13] = (RC[r][13] ^ key[13]) ^ state[13];
state[14] = (RC[r][14] ^ key[14]) ^ state[14];
state[15] = (RC[r][15] ^ key[15]) ^ state[15];
state[16] = (RC[r][16] ^ key[16]) ^ state[16];
state[17] = (RC[r][17] ^ key[17]) ^ state[17];
state[18] = (RC[r][18] ^ key[18]) ^ state[18];
state[19] = (RC[r][19] ^ key[19]) ^ state[19];
state[20] = (RC[r][20] ^ key[20]) ^ state[20];
state[21] = (RC[r][21] ^ key[21]) ^ state[21];
state[22] = (RC[r][22] ^ key[22]) ^ state[22];
state[23] = (RC[r][23] ^ key[23]) ^ state[23];
state[24] = (RC[r][24] ^ key[24]) ^ state[24];
state[25] = (RC[r][25] ^ key[25]) ^ state[25];
state[26] = (RC[r][26] ^ key[26]) ^ state[26];
state[27] = (RC[r][27] ^ key[27]) ^ state[27];
state[28] = (RC[r][28] ^ key[28]) ^ state[28];
state[29] = (RC[r][29] ^ key[29]) ^ state[29];
state[30] = (RC[r][30] ^ key[30]) ^ state[30];
state[31] = (RC[r][31] ^ key[31]) ^ state[31];
state[32] = (RC[r][32] ^ key[32]) ^ state[32];
state[33] = (RC[r][33] ^ key[33]) ^ state[33];
state[34] = (RC[r][34] ^ key[34]) ^ state[34];
state[35] = (RC[r][35] ^ key[35]) ^ state[35];
state[36] = (RC[r][36] ^ key[36]) ^ state[36];
state[37] = (RC[r][37] ^ key[37]) ^ state[37];
state[38] = (RC[r][38] ^ key[38]) ^ state[38];
state[39] = (RC[r][39] ^ key[39]) ^ state[39];
state[40] = (RC[r][40] ^ key[40]) ^ state[40];
state[41] = (RC[r][41] ^ key[41]) ^ state[41];
state[42] = (RC[r][42] ^ key[42]) ^ state[42];
state[43] = (RC[r][43] ^ key[43]) ^ state[43];
state[44] = (RC[r][44] ^ key[44]) ^ state[44];
state[45] = (RC[r][45] ^ key[45]) ^ state[45];
state[46] = (RC[r][46] ^ key[46]) ^ state[46];
state[47] = (RC[r][47] ^ key[47]) ^ state[47];
state[48] = (RC[r][48] ^ key[48]) ^ state[48];
state[49] = (RC[r][49] ^ key[49]) ^ state[49];
state[50] = (RC[r][50] ^ key[50]) ^ state[50];
state[51] = (RC[r][51] ^ key[51]) ^ state[51];
state[52] = (RC[r][52] ^ key[52]) ^ state[52];
state[53] = (RC[r][53] ^ key[53]) ^ state[53];
state[54] = (RC[r][54] ^ key[54]) ^ state[54];
state[55] = (RC[r][55] ^ key[55]) ^ state[55];
state[56] = (RC[r][56] ^ key[56]) ^ state[56];
state[57] = (RC[r][57] ^ key[57]) ^ state[57];
state[58] = (RC[r][58] ^ key[58]) ^ state[58];
state[59] = (RC[r][59] ^ key[59]) ^ state[59];
state[60] = (RC[r][60] ^ key[60]) ^ state[60];
state[61] = (RC[r][61] ^ key[61]) ^ state[61];
state[62] = (RC[r][62] ^ key[62]) ^ state[62];
state[63] = (RC[r][63] ^ key[63]) ^ state[63];
shift_rows(state, 0x1);
mPrime(state);
sBox_layer_inv(state);
r = (r + 1);
} 
}
Пример #21
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;
}