#include <crypto/mac.h>

NTL_CLIENT

void HMAC :: PrettyPrint(ostream& tofile)
{
	tofile << "HMAC(Hash=";
	h.PrettyPrint(tofile);
	tofile << ", keylen=" << keylen << ", outlen=" << outlen;
	tofile << ")";
}


ostring HMAC :: eval(const ostring& K, const ostring& txt)
{
	int len = K.size();

	if (len != keylen)
		throw General_error();

	int i, j;
	ostring Kbar, K1, K2, IPAD, OPAD;
	ostring H1, H2, mac;

	// Append h.blocklen - len zero bits to the right of key K

	Kbar = K + ostring(h.blocklen-len, octet(0x0));

	// Expanding subkey K1
	IPAD = ostring(h.blocklen, octet(0x36));

	K1 = Xor(Kbar, IPAD);

	// Expanding subkey K2
	OPAD = ostring(h.blocklen, octet(0x5C));

	K2 = Xor(Kbar, OPAD);

	H1 = h.eval(K1 + txt);
	H2 = h.eval(K2 + H1);

	mac = H2.substr(0, outlen);

	return mac;
}

