HMAC with SHA-256 and SHA-512 #
This module transcribes the keyed-hash message authentication code of FIPS 198-1 (July 2008)
[NIST08], instantiated with the SHA-256 and SHA-512 functions of
CslibFrontier.Crypto.Primitives.Hash.SHA2. With B the block size of the hash in bytes (64 for
SHA-256 and 128 for SHA-512), the source reads as follows.
- Section 2.3:
ipadis the byte0x36repeatedBtimes, andopadis the byte0x5crepeatedBtimes. - Section 3 and Table 1, steps 1--3: the
B-byte keyK0is the key itself when it hasBbytes, the key followed by zero bytes when it is shorter, and the digest of the key followed by zero bytes when it is longer. - Section 4 and Table 1, steps 4--9: the result is
H((K0 xor opad) || H((K0 xor ipad) || text)).
hmacSHA256 and hmacSHA512 return the full 32-byte or 64-byte digest. Section 5 of the source,
on truncation, is not transcribed: an application that prescribes truncation applies it to the
result.
The functions are total, as the SHA-2 functions are. The source's computation is defined when its
hash inputs are inside the SHA-2 message-length domain, fewer than 2^61 bytes for SHA-256 and
fewer than 2^125 bytes for SHA-512. That is two conditions: a key longer than B bytes, which
is hashed, is inside the domain, and so is the inner hash input of B + text.length bytes. The
second is the bound of Section 2.3 on the text, fewer than 2^B - 8B bits. On such inputs the
functions are the source's HMAC. On all others they are the same expression over the total SHA-2
functions, which write the bit length of their input modulo 2^64 or 2^128 into the padding.
The first property beyond the transcription is the zero-padding rule of Table 1, step 3, proved as a law of the final functions: appending zero bytes to a key does not change the result while the extended key still fits in one block, including when it fills the block exactly. Nothing else is proved about these functions. In particular there is no proof of agreement with an independent model of FIPS 198-1, and no statement that HMAC is a pseudorandom function or a secure message authentication code. The definitions are executable specifications, not constant-time or machine-costed implementations.
The test module CslibFrontierTests.Crypto.HMACMGF1 evaluates both functions on test cases 1 and
6 of RFC 4231, Sections 4.2 and 4.7 [Nys05] (a 20-byte key and a 131-byte key), and on a key of
exactly one block for each hash.
Main definitions #
Cslib.Crypto.HMAC.hmacSHA256,hmacSHA512: full-length SHA-2 HMAC functions.
Main results #
Cslib.Crypto.HMAC.hmacSHA256_append_zeros,hmacSHA512_append_zeros: adding zero bytes to a key that remains at most one hash block does not change HMAC.
References #
- National Institute of Standards and Technology, The Keyed-Hash Message Authentication Code (HMAC), FIPS PUB 198-1, July 2008, Sections 2.3, 3 and 4 and Table 1.
- M. Nystrom, Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512, RFC 4231, December 2005, Sections 4.2 and 4.7.
HMAC of FIPS 198-1 with SHA-256, returning all 32 digest bytes. This is the source's function
when both hash inputs that depend on the arguments are inside the SHA-256 domain
SHA2.ValidSHA256Input: a key longer than 64 bytes, which is hashed, has key.length < 2^61, and
the inner hash input has 64 + text.length < 2^61. On all other inputs it is the same expression
over the total SHA2.sha256, which writes the bit length of its input modulo 2^64 into the
padding. The key digest and the inner digest are the byte lists SHA2.sha256Bytes, which are the
bytes of SHA2.sha256 (SHA2.sha256Bytes_eq_toList).
HMAC of FIPS 198-1 with SHA-512, returning all 64 digest bytes. This is the source's function
when both hash inputs that depend on the arguments are inside the SHA-512 domain
SHA2.ValidSHA512Input: a key longer than 128 bytes, which is hashed, has key.length < 2^125,
and the inner hash input has 128 + text.length < 2^125. On all other inputs it is the same
expression over the total SHA2.sha512, which writes the bit length of its input modulo 2^128
into the padding. The key digest and the inner digest are the byte lists SHA2.sha512Bytes, which
are the bytes of SHA2.sha512 (SHA2.sha512Bytes_eq_toList).