Documentation

CslibFrontier.Crypto.Primitives.Hash.HMAC

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.

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 #

Main results #

References #

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).

theorem Cslib.Crypto.HMAC.hmacSHA256_append_zeros (key text : List Byte) (padding : ℕ) (h : key.length + padding ≤ 64) :
hmacSHA256 (key ++ List.replicate padding 0) text = hmacSHA256 key text

Appending zero bytes to a SHA-256 HMAC key does not change the result while the extended key still fits in one 64-byte SHA-256 block.

theorem Cslib.Crypto.HMAC.hmacSHA512_append_zeros (key text : List Byte) (padding : ℕ) (h : key.length + padding ≤ 128) :
hmacSHA512 (key ++ List.replicate padding 0) text = hmacSHA512 key text

Appending zero bytes to a SHA-512 HMAC key does not change the result while the extended key still fits in one 128-byte SHA-512 block.