Cryptography Reference
In-Depth Information
tmp_hash[ a ] += tmp_hash[ b ];
}
hash[ 0 ] += tmp_hash[ 0 ];
hash[ 1 ] += tmp_hash[ 1 ];
hash[ 2 ] += tmp_hash[ 2 ];
hash[ 3 ] += tmp_hash[ 3 ];
}
The longer implementation in Listing 4-5 follows the specifi cation more closely;
the shorter implementation is a bit diffi cult to read, but it yields the same results.
NOTE Actually, the specifi cation includes C code! The implementation there
is a bit different than this one, though. The reason is covered later.
This produces a 128-bit hash on a 512-bit block. If the input is greater than
512 bits, just call the function again, this time passing the output of the previ-
ous call as the initializer. If this is the fi rst call, initialize the hash code to the
cryptographically meaningless initializer in Listing 4-6.
Listing 4-6: “md5.c” md5 initial hash
unsigned int md5_initial_hash[ ] = {
0x67452301,
0xefcdab89,
0x98badcfe,
0x10325476
};
Notice that this initializer doesn't have any quasi-mystical cryptographic
security properties; it's just the byte sequence 0123456789abcdef (in little-endian
form), followed by the same thing backward. It doesn't much matter what you
initialize the hash to — although 0's would be a bad choice — as long as every
implementation agrees on the starting value.
Securely Hashing Multiple Blocks of Data
If you need to encrypt less than 512 bits, or a bit string that's not an even multiple
of 512 bits, you pad the last block. However, you can't just pad with 0's or just
with 1's. Remember, 512 0's is a legitimate input to MD5. So is one 0. You need
some way to ensure that 512 0's hashes to a different value than one 0. Therefore,
MD5 requires that the last eight bytes of the input be set to the length, in bits
(remember that you may want to hash a value that's not an even multiple of eight
bits) of the input preceding it. This means that MD5 is essentially undefi ned for
lengths greater than 2 64 bits, and that if the input happens to be between 448
(512 - 64) and 512 bits, you need to add an extra 512-bit block of padding just to
 
Search WWH ::




Custom Search