Content deleted Content added
Atropos235 (talk | contribs) m →Definition: format |
m Reverted edits by 73.167.116.198 (talk) (HG) (3.4.12) |
||
(27 intermediate revisions by 17 users not shown) | |||
Line 1:
{{Short description|Cryptographic tool}}
{{Use American English|date = April 2019}}
A '''mask generation function''' ('''MGF''') is a cryptographic primitive similar to a [[cryptographic hash function]] except that while a hash function's output has a fixed size, a MGF supports output of a variable length. In this respect, a MGF can be viewed as a [[extendable-output function]] (XOF): it can accept input of any length and process it to produce output of any length. Mask generation functions are completely deterministic: for any given input and any desired output length the output is always the same.
== Definition ==
Line 9 ⟶ 11:
== Applications ==
Mask
=== Padding
Mask
===
NIST Special Publication 800-90A<ref>{{cite journal |url=http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf |title=Recommendation for Random Number Generation Using Deterministic Random Bit Generators |author=National Institute of Standards and Technology|year=2012 |doi=10.6028/NIST.SP.800-90A }}</ref> defines a class of cryptographically secure random number generators, one of which is the "Hash DRBG", which uses a hash function with a counter to produce a requested sequence of random bits equal in size to the requested number of random bits.
== Examples ==
Perhaps the most common and
=== MGF1 ===
MGF1 is a
<blockquote>
====Options====
<math>\mathsf{Hash}</math>
: hash function (<math>\mathsf{hLen}</math> denotes the length in octets of the hash function output)
====Input====
<math>Z</math>
:seed from which mask is generated, an octet string
<math>l</math>
:intended length in octets of the mask, at most <math>2^{32}(\mathsf{hLen})</math>
====Output====
<math>\mathsf{mask}</math>
:mask, an octet string of length <math>l</math>; or "mask too long"
====Steps====
{{Ordered list | list_style_type=decimal
| If <math>l > 2^{32}(\mathsf{hLen})</math>, output "mask too long" and stop.
| Let <math>T</math> be the empty octet string.
| For <math>\mathsf{counter}</math> from <math>0</math> to <math>\left\lceil{\tfrac{l}{\mathsf{hLen}}}\right\rceil-1</math>, do the following:
{{Ordered list | list_style_type=lower-alpha
| Convert <math>\mathsf{counter}</math> to an octet string <math>C</math> of length <math>4</math> with the primitive <math>\mathsf{I2OSP}</math>:
: <math>C = \mathsf{I2OSP} (\mathsf{counter}, 4)</math>
| Concatenate the hash of the seed <math>Z</math> and <math>C</math> to the octet string <math>T</math>:
: <math>T = T \shortparallel \mathsf{Hash} (Z \shortparallel C)</math>
}}
| Output the leading <math>l</math> octets of <math>T</math> as the octet string mask.
}}
</blockquote>
=== Example code ===
Below is Python code implementing MGF1:
<syntaxhighlight lang="python">
import hashlib
def mgf1(seed: bytes, length: int, hash_func=hashlib.sha1) -> bytes:
"""Mask generation function."""
hLen = hash_func().digest_size
# https://www.ietf.org/rfc/rfc2437.txt
# 1. If l > 2^32(hLen), output "mask too long" and stop.
if length > (hLen << 32):
raise ValueError("mask too long")
# 2. Let T be the empty octet string.
T = b""
# 3. For counter from 0 to \lceil{l / hLen}\rceil-1, do the following:
# Note: \lceil{l / hLen}\rceil-1 is the number of iterations needed,
# but it's easier to check if we have reached the desired length.
counter = 0
while len(T) < length:
# a. Convert counter to an octet string C of length 4 with the primitive I2OSP: C = I2OSP (counter, 4)
C = int.to_bytes(counter, 4, "big")
# b. Concatenate the hash of the seed Z and C to the octet string T: T = T || Hash (Z || C)
T += hash_func(seed + C).digest()
counter += 1
# 4. Output the leading l octets of T as the octet string mask.
return T[:length]
</syntaxhighlight>
Example outputs of MGF1:
<
Python
Type "help", "copyright", "credits" or "license" for more information.
>>> from mgf1 import mgf1
>>> from hashlib import sha256
>>>
'1ac907'
>>>
'1ac9075cd4'
>>>
'bc0c655e01'
>>>
'bc0c655e016bc2931d85a2e675181adcef7f581f76df2739da74faac41627be2f7f415c89e983fd0ce80ced9878641cb4876'
>>>
'382576a7841021cc28fc4c0948753fb8312090cea942ea4c4e735d10dc724b155f9f6069f289d61daca0cb814502ef04eae1'
</syntaxhighlight>
== References ==
{{reflist}}
[[Category:Articles with example Python (programming language) code]]
[[Category:Cryptographic primitives]]
[[Category:Cryptographic hash functions]]
|