Cryptography Reference
In-Depth Information
context->block_len );
context->block[ context->block_len ] = 0x80;
// special handling if the last block is < 64 but > 56
if ( context->block_len >= INPUT_BLOCK_SIZE )
{
context->block_operate( context->block, context->hash );
context->block_len = 0;
memset( context->block + context->block_len, 0, DIGEST_BLOCK_SIZE -
context->block_len );
}
// Only append the length for the very last block
// Technically, this allows for 64 bits of length, but since we can only
// process 32 bits worth, we leave the upper four bytes empty
context->block_finalize( context->block, context->input_len * 8 );
context->block_operate( context->block, context->hash );
}
This logic was described when it was originally presented in the context
of MD5.
Listing 4-24 shows how you initialize the MD5 digest context.
Listing 4-24: “md5.c” MD5 digest initialization
void new_md5_digest( digest_ctx *context )
{
context->hash_len = 4;
context->input_len = 0;
context->block_len = 0;
context->hash = ( unsigned int * )
malloc( context->hash_len * sizeof( unsigned int ) );
memcpy( context->hash, md5_initial_hash,
context->hash_len * sizeof( unsigned int ) );
memset( context->block, '\0', DIGEST_BLOCK_SIZE );
context->block_operate = md5_block_operate;
context->block_finalize = md5_finalize;
}
Listing 4-25 shows how you initialize the SHA-1 context.
Listing 4-25: “sha.c” SHA-1 digest initialization
void new_sha1_digest( digest_ctx *context )
{
context->hash_len = 5;
context->input_len = 0;
context->block_len = 0;
context->hash = ( unsigned int * )
malloc( context->hash_len * sizeof( unsigned int ) );
memcpy( context->hash, sha1_initial_hash,
context->hash_len * sizeof( unsigned int ) );
(Continued)
Search WWH ::




Custom Search