void decode_block(BYTE* counter, BYTE* dst, BYTE round_key[ROUND_KEY_SIZE][BLOCK_SIZE]){ unsigned char temp[BLOCK_SIZE]; xor_key(counter,temp,round_key[ROUNDS]); //memcpy(dst, round_key[ROUNDS], 16); shift_rows_inv(temp,dst); sub_bytes_inv(dst,temp); for(int i = ROUNDS-1; i > 0; i--){ xor_key(temp, dst, round_key[i]); mix_columns_inv(dst, temp); shift_rows_inv(temp, dst); sub_bytes_inv(dst, temp); } xor_key(temp, dst, round_key[0]); }
void AES::test(){ BYTE src[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; BYTE key[] = {0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; BYTE rk[11][16]; AES::expand_key(key, rk); BYTE dst[16]; sub_bytes_inv(src,dst); //AES::print_state(dst); decode_block(src, dst, rk); /*AES::print_state(rk[0]); AES::print_state(rk[1]); AES::print_state(rk[2]); AES::print_state(rk[3]); AES::print_state(rk[4]); AES::print_state(rk[5]); AES::print_state(rk[6]); AES::print_state(rk[7]); AES::print_state(rk[8]); AES::print_state(rk[9]);*/ AES::print_state(dst); }
void Aes256::decrypt(unsigned char* buffer) { unsigned char i, rcon = 1; copy_key(); for (i = NUM_ROUNDS / 2; i > 0; --i) expand_enc_key(&rcon); add_round_key(buffer, NUM_ROUNDS); shift_rows_inv(buffer); sub_bytes_inv(buffer); for (i = NUM_ROUNDS, rcon = 0x80; --i;) { if ((i & 1)) expand_dec_key(&rcon); add_round_key(buffer, i); mix_columns_inv(buffer); shift_rows_inv(buffer); sub_bytes_inv(buffer); } add_round_key(buffer, i); }
// Decrypt a single 128 bit block by a 128 bit key using AES // http://en.wikipedia.org/wiki/Advanced_Encryption_Standard void AES_Decrypt(unsigned char *data, unsigned char *key) { int i; // To count the rounds // Key expansion unsigned char keys[176]; expand_key(key,keys); // Reverse the final Round xor_round_key(data,keys,10); shift_rows_inv(data); sub_bytes_inv(data, 16); // Reverse the middle rounds for (i=0; i<9; i++) { xor_round_key(data,keys,9-i); mix_cols_inv(data); shift_rows_inv(data); sub_bytes_inv(data, 16); } // Reverse the first Round xor_round_key(data, keys, 0); }
// Decrypt a single 128 bit block by a 128 bit key using AES // http://en.wikipedia.org/wiki/Advanced_Encryption_Standard void DecryptAES(byte *c, byte *key, byte *m) { int i; // To count the rounds // Key expansion byte keys[176]; expand_key(key,keys); // Reverse the final Round memcpy(m,c,16); xor_round_key(m,keys,10); shift_rows_inv(m); sub_bytes_inv(m, 16); // Reverse the middle rounds for (i=0; i<9; i++) { xor_round_key(m,keys,9-i); mix_cols_inv(m); shift_rows_inv(m); sub_bytes_inv(m, 16); } // Reverse the first Round xor_round_key(m, keys, 0); }