Cryptography Reference
In-Depth Information
add_round_key( state, &w[ ( round - 1 ) * 4 ] );
if ( round > 1 )
{
inv_mix_columns( state );
}
}
for ( r = 0; r < 4; r++ )
{
for ( c = 0; c < 4; c++ )
{
output_block[ r + ( 4 * c ) ] = state[ r ][ c ];
}
}
}
With the block operations defi ned, encryption and decryption are simple
enough, as shown in Listing 2-43.
Listing 2-43: “aes.c” aes_encrypt and aes_decrypt
#define AES_BLOCK_SIZE 16
static void aes_encrypt( const unsigned char *input,
int input_len,
unsigned char *output,
const unsigned char *iv,
const unsigned char *key,
int key_length )
{
unsigned char input_block[ AES_BLOCK_SIZE ];
while ( input_len >= AES_BLOCK_SIZE )
{
memcpy( input_block, input, AES_BLOCK_SIZE );
xor( input_block, iv, AES_BLOCK_SIZE ); // implement CBC
aes_block_encrypt( input_block, output, key, key_length );
memcpy( ( void * ) iv, ( void * ) output, AES_BLOCK_SIZE ); // CBC
input += AES_BLOCK_SIZE;
output += AES_BLOCK_SIZE;
input_len -= AES_BLOCK_SIZE;
}
}
static void aes_decrypt( const unsigned char *input,
int input_len,
unsigned char *output,
const unsigned char *iv,
const unsigned char *key,
int key_length )
{
while ( input_len >= AES_BLOCK_SIZE )
 
Search WWH ::




Custom Search