Cryptography Reference
In-Depth Information
const unsigned char *key )
{
unsigned char nonce[ AES_BLOCK_SIZE ];
unsigned char input_block[ AES_BLOCK_SIZE ];
unsigned char zeros[ AES_BLOCK_SIZE ];
unsigned char H[ AES_BLOCK_SIZE ];
unsigned char mac_block[ AES_BLOCK_SIZE ];
unsigned int next_nonce;
int original_input_len;
int block_size;
memset( zeros, '\0', AES_BLOCK_SIZE );
aes_block_encrypt( zeros, H, key, 16 );
memcpy( nonce, iv, 12 );
memset( nonce + 12, '\0', sizeof( unsigned int ) );
// MAC initialization
memset( mac_block, '\0', AES_BLOCK_SIZE );
original_input_len = htonl( input_len << 3 ); // remember this for final block
next_nonce = htonl( 1 );
while ( input_len )
{
next_nonce = ntohl( next_nonce );
next_nonce++;
next_nonce = htonl( next_nonce );
memcpy( ( void * ) ( nonce + 12 ), ( void * ) &next_nonce,
sizeof( unsigned int ) );
block_size = ( input_len < AES_BLOCK_SIZE ) ? input_len : AES_BLOCK_SIZE;
aes_block_encrypt( nonce, input_block, key, 16 );
xor( input_block, input, block_size ); // implement CTR
memcpy( ( void * ) output, ( void * ) input_block, block_size );
// Update the MAC; input_block contains encrypted value
memset( ( input_block + AES_BLOCK_SIZE ) -
( AES_BLOCK_SIZE - block_size ), '\0',
AES_BLOCK_SIZE - block_size );
xor( input_block, mac_block, AES_BLOCK_SIZE );
gf_multiply( input_block, H, mac_block );
input += block_size;
output += block_size;
input_len -= block_size;
}
memset( input_block, '\0', AES_BLOCK_SIZE );
memcpy( input_block + 12, ( void * ) &original_input_len,
sizeof( unsigned int ) );
xor( input_block, mac_block, AES_BLOCK_SIZE );
gf_multiply( input_block, H, output );
Search WWH ::




Custom Search