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.
- Step 1: a requested length
maskLen > 2^32 * hLenis rejected asmask too long. - Step 3: for
counterfrom0to⌈maskLen / hLen⌉ - 1, the digest ofmgfSeed || I2OSP(counter, 4)is appended to an initially empty stringT. Section 4.1 definesI2OSP(counter, 4)as the four-byte big-endian encoding ofcounter. - Step 4: the result is the leading
maskLenbytes ofT.
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 #
Cslib.Crypto.MGF1.mgf1SHA256,mgf1SHA512: masks of the requested length.
Main results #
Cslib.Crypto.MGF1.mgf1SHA256_prefix,mgf1SHA512_prefix: a shorter mask is the corresponding prefix of a longer one.
References #
- K. Moriarty, B. Kaliski, J. Jonsson and A. Rusch, PKCS #1: RSA Cryptography Specifications Version 2.2, RFC 8017, November 2016, Section 4.1 and Appendix B.2.1.
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.
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.
A shorter SHA-256 MGF1 mask is the corresponding prefix of any longer mask from the same seed.
A shorter SHA-512 MGF1 mask is the corresponding prefix of any longer mask from the same seed.