Documentation

CslibFrontier.Crypto.Primitives.Hash.MGF1

MGF1 with SHA-256 and SHA-512 #

This module transcribes the mask generation function MGF1 of RFC 8017 (PKCS #1 v2.2, November 2016), Appendix B.2.1 [MKJR16], instantiated with the SHA-256 and SHA-512 functions of CslibFrontier.Crypto.Primitives.Hash.SHA2. With hLen the digest length in bytes (32 for SHA-256 and 64 for SHA-512), the source reads as follows.

mgf1SHA256 and mgf1SHA512 are total functions of the seed and the requested length, and the length is recorded in the type of the result. For maskLen ≤ 2^32 * hLen, the bound of step 1, every counter is below 2^32, so natToFixedBytesBE 4 is I2OSP(·, 4) and the functions are the source's MGF1. Beyond the bound, where the source has no output, they reduce the counter modulo 2^32.

The first property beyond the transcription is prefix consistency: a mask is the corresponding prefix of every longer mask from the same seed. Nothing else is proved about these functions. In particular there is no proof of agreement with an independent model of RFC 8017 and no pseudorandomness statement. The definitions are executable specifications, not constant-time or machine-costed implementations, and evaluating them at a large length is not efficient.

RFC 8017 gives no test vectors for MGF1. The test module CslibFrontierTests.Crypto.HMACMGF1 evaluates both functions at the lengths 0, hLen - 1, hLen and hLen + 1 against values computed outside Lean from the steps above.

Main definitions #

Main results #

References #

def Cslib.Crypto.MGF1.mgf1SHA256 (seed : List Byte) (maskLen : ℕ) :
FixedBytes maskLen

MGF1 of RFC 8017, Appendix B.2.1, with SHA-256: the leading maskLen bytes of the concatenated SHA-256 digests of seed || C for the counters 0, 1, …, where C is the counter as four big-endian bytes (the source's I2OSP(counter, 4)). For maskLen ≤ 2^32 * 32 this is the source's function. For larger maskLen, where the source outputs mask too long, the counter is reduced modulo 2^32.

def Cslib.Crypto.MGF1.mgf1SHA512 (seed : List Byte) (maskLen : ℕ) :
FixedBytes maskLen

MGF1 of RFC 8017, Appendix B.2.1, with SHA-512: the leading maskLen bytes of the concatenated SHA-512 digests of seed || C for the counters 0, 1, …, where C is the counter as four big-endian bytes (the source's I2OSP(counter, 4)). For maskLen ≤ 2^32 * 64 this is the source's function. For larger maskLen, where the source outputs mask too long, the counter is reduced modulo 2^32.

theorem Cslib.Crypto.MGF1.mgf1SHA256_prefix (seed : List Byte) {shorter longer : ℕ} (h : shorter ≤ longer) :
Fin.take shorter h (mgf1SHA256 seed longer) = mgf1SHA256 seed shorter

A shorter SHA-256 MGF1 mask is the corresponding prefix of any longer mask from the same seed.

theorem Cslib.Crypto.MGF1.mgf1SHA512_prefix (seed : List Byte) {shorter longer : ℕ} (h : shorter ≤ longer) :
Fin.take shorter h (mgf1SHA512 seed longer) = mgf1SHA512 seed shorter

A shorter SHA-512 MGF1 mask is the corresponding prefix of any longer mask from the same seed.