Cryptography Reference
In-Depth Information
NOTE This update_digest/fi nalize_digest/digest_ctx all but invents C++.
(Don't worry, I promise it won't happen again.) In fact, if C and C++ weren't so
diffi cult to interoperate, I'd just turn digest into a new superclass with three
subclasses, md5digest, sha1digest, and sha256digest.
This new updateable hash operation simplifi es HMAC a bit because you
don't need to do so much copying to get the buffers set up correctly as shown
in Listing 4-27.
Listing 4-27: “hmac.c” modifi ed HMAC function to use updateable digest functions
void hmac( unsigned char *key,
int key_length,
unsigned char *text,
int text_length,
digest_ctx *digest )
{
unsigned char ipad[ DIGEST_BLOCK_SIZE ];
unsigned char opad[ DIGEST_BLOCK_SIZE ];
digest_ctx hash1;
int i;
// TODO if key_length > hash_block_length, should hash it using
// “hash_function” first and then use that as the key.
assert( key_length < DIGEST_BLOCK_SIZE );
// “cheating”; copy the supplied digest context in here, since we don't
// know which digest algorithm is being used
memcpy( &hash1, digest, sizeof( digest_ctx ) );
hash1.hash = ( unsigned int * ) malloc(
hash1.hash_len * sizeof( unsigned int ) );
memcpy( hash1.hash, digest->hash, hash1.hash_len * sizeof( unsigned int ) );
memset( ipad, 0x36, DIGEST_BLOCK_SIZE );
for ( i = 0; i < key_length; i++ )
{
ipad[ i ] ^= key[ i ];
}
update_digest( &hash1, ipad, DIGEST_BLOCK_SIZE );
update_digest( &hash1, text, text_length );
finalize_digest( &hash1 );
memset( opad, 0x5C, DIGEST_BLOCK_SIZE );
for ( i = 0; i < key_length; i++ )
 
Search WWH ::




Custom Search