Content deleted Content added
short desc |
m Reverted edits by 73.167.116.198 (talk) (HG) (3.4.12) |
||
(24 intermediate revisions by 15 users not shown) | |||
Line 1:
{{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
▲{{Short description|cryptographic tool}}
▲A '''mask generation function''' ('''MGF''') is a cryptographic primitive similar to a [[cryptographic hash function]] except that while a hash function's output is a fixed size, a MGF supports output of a variable length. In this respect, a MGF can be viewed as a single-use [[sponge function]]: it can absorb any length of input and process it to produce any length of output. Mask generation functions are completely deterministic: for any given input and desired output length the output is always the same.
== Definition ==
Line 11:
== Applications ==
Mask generation functions, as generalizations of hash functions, are useful wherever hash functions are. However, use of a MGF is desirable in cases where a fixed-size hash would be inadequate. Examples include generating [[Padding (cryptography)|padding]], producing [[
=== Padding schemes ===
Mask generation functions were first proposed as part of the specification for padding in the [[Optimal_asymmetric_encryption_padding|RSA-OAEP]] algorithm. The OAEP algorithm required a cryptographic hash function that could generate an output equal in size to a "data block" whose length was proportional to arbitrarily sized input message.<ref name="rsa"/>
=== Random number generators ===
NIST Special Publication 800-90A<ref>{{cite
== Examples ==
Line 35 ⟶ 27:
=== MGF1 ===
MGF1 is a mask generation function defined in the Public Key Cryptography Standard #1 published by RSA Laboratories:<ref name="rsa"/>
<blockquote>
====Options====
Line 66 ⟶ 58:
</blockquote>
=== Example
Below is
<
import hashlib
"""Mask generation function."""
hLen = hash_func().digest_size
# https://www.ietf.org/rfc/rfc2437.txt
▲def mgf1(input, length, hash=hashlib.sha1):
# 1. If l > 2^32(hLen), output "mask too long" and stop.
counter = 0▼
if length > (hLen << 32):
raise ValueError("mask too long")
while (len(output) < length):▼
# 2. Let T be the empty octet string.
C = i2osp(counter, 4)▼
T = b""
output += hash(input + C).digest()▼
# 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,
return output[:length]▼
# but it's easier to check if we have reached the desired length.
▲ counter = 0
# 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)
# 4. Output the leading l octets of T as the octet string mask.
</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]]
|