you will implement HMAC (Keyed-hash Message Authentication Code) using SHA-512 as the building block. HMAC with key K of a input message M can be expressed as:
where is the key padded out to block size, which is 1024 bits when we use SHA-512. Here, opad, ipad are specified padding constants, which is specified as follows:
ipad:00110110 (36 in hexidecimal) repeated by 1024/8=128 times;
opad:01011100 (5C in hexidecimal) repeated by 1024/8=128 times.
We can describe the HMAC algorithm as follows:
1. Append zeros to the end of K to create a b-bit string (e.g., if K is of length 160 bits, and b = 1024 bits, then K will be appended with 864 bits of zeros to the end of K).
2. XOR (bitwise exclusive-or ) with ipad to produce b-bit block Si.
3. Append input message M to Si.
4. Apply hash to the stream generated in step 3.
5. XOR with opad to produce the b-bit block So.
6. Append the hash output from step 4 to So.
7. Apply hash to the stream generated in step 6 to produce the output of HMAC.
The following figure illustrates the structure of HMAC.
After computing the HMAC with key K of an input message M, print it on the screen in hexadecimal format, also store the HMAC output in the output file. Your program should take three arguments: an input file name, an output HMAC file, and a key. For example, you may use the following command to compute an HMAC for the file "text1", with the HMAC output stored in the file text1-hmac, using the key hmac123456: HMAC_gen text1 text1-hmac hmac123456.
If you want to use a longer key, like a key more than 512 bits, it is recommended that you put the key in a file. (Optional: you can use SHA-512 to hash the key entered in the command line to get a longer key to be used in the HMAC algorithm).
You do not need to implement SHA-512 yourself. For an implementation of SHA-512, you can use an existing crypto library, Crypto++ (C++) in this project, which is installed on the virtual machine. While you are using the built-in SHA-512 functions provided by Crypto++ library, you are required to implement HMAC yourself in this program assignment.
1. In order to use the crypto library, in your C++ source program (e.g., test1.cpp), you need to include the right library files, and use the right namespace as follows.
#include "cryptopp/cryptlib.h"
#include "cryptopp/hex.h"
#include "cryptopp/filters.h"
#include "cryptopp/sha.h"
#include "cryptopp/des.h"
#include "cryptopp/aes.h"
#include "cryptopp/modes.h"
2. How to compile your source program:
g++ -o test1 -L. test1.cpp -lcryptopp
3. The tool function to perform SHA-512 is illustrated as follows.
string sha_digest(string & plain)
{
string sha512digest;
SHA512 hash;
StringSource(plain, true, new HashFilter(hash, new StringSink(sha512digest) ) );
return sha512digest;
}
4. Using the following function call to compute the hash output of the input message:
string output512digest = sha_digest(plain)
Here, plain is the input message stored in a string.