void aes_decrypt_128( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const unsigned char key[N_BLOCK], unsigned char o_key[N_BLOCK] ) { uint_8t s1[N_BLOCK], r, rc = 0x6c; if(o_key != key) block16_copy( o_key, key ); copy_and_key( s1, in, o_key ); inv_shift_sub_rows( s1 ); for( r = 10 ; --r ; ) #if defined( VERSION_1 ) { update_decrypt_key_128( o_key, &rc ); add_round_key( s1, o_key ); inv_mix_sub_columns( s1 ); } #else { uint_8t s2[N_BLOCK]; update_decrypt_key_128( o_key, &rc ); copy_and_key( s2, s1, o_key ); inv_mix_sub_columns( s1, s2 ); } #endif update_decrypt_key_128( o_key, &rc ); copy_and_key( out, s1, o_key ); }
void aes_encrypt_128( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const unsigned char key[N_BLOCK], unsigned char o_key[N_BLOCK] ) { uint_8t s1[N_BLOCK], r, rc = 1; if(o_key != key) block16_copy( o_key, key ); copy_and_key( s1, in, o_key ); for( r = 1 ; r < 10 ; ++r ) #if defined( VERSION_1 ) { mix_sub_columns( s1 ); update_encrypt_key_128( o_key, &rc ); add_round_key( s1, o_key ); } #else { uint_8t s2[N_BLOCK]; mix_sub_columns( s2, s1 ); update_encrypt_key_128( o_key, &rc ); copy_and_key( s1, s2, o_key ); } #endif shift_sub_rows( s1 ); update_encrypt_key_128( o_key, &rc ); copy_and_key( out, s1, o_key ); }
return_type aes_decrypt( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1] ) { if( ctx->rnd ) { uint_8t s1[N_BLOCK], r; copy_and_key( s1, in, ctx->ksch + ctx->rnd * N_BLOCK ); inv_shift_sub_rows( s1 ); for( r = ctx->rnd ; --r ; ) #if defined( VERSION_1 ) { add_round_key( s1, ctx->ksch + r * N_BLOCK ); inv_mix_sub_columns( s1 ); } #else { uint_8t s2[N_BLOCK]; copy_and_key( s2, s1, ctx->ksch + r * N_BLOCK ); inv_mix_sub_columns( s1, s2 ); } #endif copy_and_key( out, s1, ctx->ksch ); } else return (return_type) -1; return 0; }
return_type aes_encrypt( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1] ) { if( ctx->rnd ) { uint_8t s1[N_BLOCK], r; copy_and_key( s1, in, ctx->ksch ); for( r = 1 ; r < ctx->rnd ; ++r ) #if defined( VERSION_1 ) { mix_sub_columns( s1 ); add_round_key( s1, ctx->ksch + r * N_BLOCK); } #else { uint_8t s2[N_BLOCK]; mix_sub_columns( s2, s1 ); copy_and_key( s1, s2, ctx->ksch + r * N_BLOCK); } #endif shift_sub_rows( s1 ); copy_and_key( out, s1, ctx->ksch + r * N_BLOCK ); } else return -1; return 0; }
byte AES::decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK]) { if (round) { byte s1 [N_BLOCK] ; copy_and_key (s1, cipher, (byte*) (key_sched + round * N_BLOCK)) ; inv_shift_sub_rows (s1) ; for (byte r = round ; --r ; ) { byte s2 [N_BLOCK] ; copy_and_key (s2, s1, (byte*) (key_sched + r * N_BLOCK)) ; inv_mix_sub_columns (s1, s2) ; } copy_and_key (plain, s1, (byte*) (key_sched)) ; } else { return AES_FAILURE ; } return AES_SUCCESS ; }
byte AES::encrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK]) { if (round) { byte s1 [N_BLOCK], r ; copy_and_key (s1, plain, (byte*) (key_sched)) ; for (r = 1 ; r < round ; r++) { byte s2 [N_BLOCK] ; mix_sub_columns (s2, s1) ; copy_and_key (s1, s2, (byte*) (key_sched + r * N_BLOCK)) ; } shift_sub_rows (s1) ; copy_and_key (cipher, s1, (byte*) (key_sched + r * N_BLOCK)) ; } else { return AES_FAILURE ; } return AES_SUCCESS ; }
/** * Decrypts a single block of 16 bytes * @param in Buffer holding the input data * @param out Buffer holding the output data * @param ctx AES context * @return Status from the result */ aes_result AES::aes_decrypt( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1] ) { if( ctx->rnd ) { uint_8t s1[N_BLOCK], r; copy_and_key( s1, in, ctx->ksch + ctx->rnd * N_BLOCK ); inv_shift_sub_rows( s1 ); for( r = ctx->rnd ; --r ; ) { add_round_key( s1, ctx->ksch + r * N_BLOCK ); inv_mix_sub_columns( s1 ); } copy_and_key( out, s1, ctx->ksch ); } else return -1; return 0; }
/** * Encrypts a single block of 16 bytes * @param in Buffer holding the input data * @param out Buffer holding the output data * @param ctx AES context * @return Status from the result */ aes_result AES::aes_encrypt( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1] ) { if( ctx->rnd ) { uint_8t s1[N_BLOCK], r; copy_and_key( s1, in, ctx->ksch ); for( r = 1 ; r < ctx->rnd ; ++r ) { mix_sub_columns( s1 ); add_round_key( s1, ctx->ksch + r * N_BLOCK); } shift_sub_rows( s1 ); copy_and_key( out, s1, ctx->ksch + r * N_BLOCK ); } else return -1; return 0; }
/* Decrypt a single block of 16 bytes with 'on the fly' 256 bit keying */ void aes_decrypt_256( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const unsigned char key[2 * N_BLOCK], unsigned char o_key[2 * N_BLOCK] ) { uint_8t s1[N_BLOCK], r, rc = 0x80; if(o_key != key) { block16_copy( o_key, key ); block16_copy( o_key + 16, key + 16 ); } copy_and_key( s1, in, o_key ); inv_shift_sub_rows( s1 ); for( r = 14 ; --r ; ) #if defined( VERSION_1 ) { if( ( r & 1 ) ) { update_decrypt_key_256( o_key, &rc ); add_round_key( s1, o_key + 16 ); } else add_round_key( s1, o_key ); inv_mix_sub_columns( s1 ); } #else { uint_8t s2[N_BLOCK]; if( ( r & 1 ) ) { update_decrypt_key_256( o_key, &rc ); copy_and_key( s2, s1, o_key + 16 ); } else copy_and_key( s2, s1, o_key ); inv_mix_sub_columns( s1, s2 ); } #endif copy_and_key( out, s1, o_key ); }