Section 15.1

Pseudorandom function (PRF)

Definition

A pseudorandom function (PRF) is a keyed function: an efficient algorithm §14.1 that maps a secret key and an input to an output of fixed length. Nobody who lacks the key and has limited computing power can tell a PRF apart from a truly random function, which gives each input its own independent output with all values equally likely, even after choosing inputs and seeing their outputs. Unlike a public hash function, a PRF is secure only while its key is secret.

The idea

Two devices share a secret key. With each message it sends, one device wants to attach a value that the other device can recompute and that an outsider cannot predict, even an outsider who has seen thousands of earlier messages with their values and chose some of those messages. The ideal tool would be a shared secret table with an independent random entry for every possible message. For 16-byte messages the table would need 21282^{128} rows, far more than any memory holds.

A PRF replaces the table by a short key and a public algorithm that computes any entry from the key on demand. The promise is that the computed entries look like the entries of a random table to every observer who does not hold the key and has feasible resources. The observer may submit any inputs, in any order, and sees only the outputs. A repeated input gets the same output, exactly as in a table: random-looking does not mean that the output changes from one query to the next.

Picture a sealed booth with a slot for questions and answers. Behind the slot sits either a clerk, who answers each new question by flipping coins and records the answer so that a repeated question gets the same answer, or a machine that computes the answer from a secret key. The machine is a PRF if no visitor with a feasible amount of effort can tell which of the two is behind the slot. The picture is inaccurate in one respect: the machine’s design is public, and only its key is hidden.

Much of symmetric-key cryptography §5.15, in which the communicating parties share one secret key, rests on this one property. A message authentication code §5.7 (a short tag that shows a message came from a key holder), a key derivation function §5.20 (an algorithm that turns one secret into several keys) and the keystream of counter mode §15.5 (pseudorandom bits that are combined with a message to encrypt it) can all be built from a PRF, and their security proofs assume one.

How it works

The definition is an experiment: a game between an adversary, the party that tries to tell two worlds apart, and a challenger, who sets up one of the two worlds. Let FF be the keyed function, with keys of kk bits, inputs of mm bits and outputs of nn bits.

  1. The challenger flips a fair coin in secret to choose the real world or the ideal world.

  2. Real world. The challenger picks a key KK uniformly at random, meaning that each of the 2k2^k possible keys is equally likely, and keeps it secret. The challenger answers each query xx with F(K,x)F(K, x).

  3. Ideal world. The challenger answers each new input with an output chosen uniformly at random from the 2n2^n possible outputs, records the pair, and answers a repeated input with the recorded output. A table filled in this way, on demand, behaves exactly like a random function chosen in full at the start.

  4. The adversary sends inputs one at a time and may choose each input after seeing the earlier answers. At the end the adversary outputs one bit: 1 for the real world, 0 for the ideal world.

  5. The adversary’s advantage is the probability that the adversary outputs 1 in the real world minus the probability that the adversary outputs 1 in the ideal world, taken as a positive number.

An adversary that ignores the answers and guesses has advantage 0. An adversary with advantage near 1 tells the worlds apart almost every time. FF is a secure PRF when every adversary within a stated budget of computing time and number of queries has an advantage no larger than a stated small number. In the ideal world two different inputs may get equal outputs, because each output is drawn independently.

Worked example

A keyed function that fails. Take 4-bit keys, inputs and outputs, and F(K,x)=x⊕KF(K, x) = x \oplus K, where ⊕\oplus is bitwise XOR (exclusive or): each output bit is 1 where the two input bits differ and 0 where they are equal. The key is secret and uniformly random. The adversary queries x1=𝟶𝟶𝟶𝟶x_1 = \texttt{0000} and x2=𝟷𝟷𝟷𝟷x_2 = \texttt{1111}, XORs the two answers, and outputs 1 if the result is 1111.

Real world, with key K=𝟷𝟶𝟷𝟶K = \texttt{1010}: the answers are 𝟶𝟶𝟶𝟶⊕𝟷𝟶𝟷𝟶=𝟷𝟶𝟷𝟶\texttt{0000} \oplus \texttt{1010} = \texttt{1010} and 𝟷𝟷𝟷𝟷⊕𝟷𝟶𝟷𝟶=𝟶𝟷𝟶𝟷\texttt{1111} \oplus \texttt{1010} = \texttt{0101}, and 𝟷𝟶𝟷𝟶⊕𝟶𝟷𝟶𝟷=𝟷𝟷𝟷𝟷\texttt{1010} \oplus \texttt{0101} = \texttt{1111}. The test passes for every key, because the key occurs in both answers and K⊕K=𝟶𝟶𝟶𝟶K \oplus K = \texttt{0000}, so the XOR of the answers is x1⊕x2=𝟷𝟷𝟷𝟷x_1 \oplus x_2 = \texttt{1111}. The adversary outputs 1 with probability 1.

Ideal world: the two inputs differ, so the two answers are independent and uniform over the 24=162^4 = 16 possible 4-bit values. Whatever the first answer is, exactly one of the 16 possible second answers makes the XOR equal 1111. The adversary outputs 1 with probability 1/16=0.06251/16 = 0.0625.

The advantage is 1−0.0625=0.93751 - 0.0625 = 0.9375. Two queries expose the function, although its key is secret and uniform. A PRF needs more than a secret key: its outputs must also bear no visible relation to one another.

A public hash function fails. Let the candidate be SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), which has no key. The adversary queries the 3-byte input abc. The adversary also computes SHA-256 of abc, which needs no key; the result is the published value

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

The adversary outputs 1 if the answer equals this value. In the real world the answer always equals it. In the ideal world the answer is a uniformly random 256-bit string, which equals it with probability 2−2562^{-256}. The advantage is 1−2−2561 - 2^{-256}, which is 1 for every practical purpose.

Stated precisely

Let F:{0,1}k×{0,1}m→{0,1}nF\colon \{0,1\}^k \times \{0,1\}^m \to \{0,1\}^n be a keyed function: for each key KK of kk bits, F(K,⋅)F(K, \cdot) maps mm-bit inputs to nn-bit outputs. Let Func(m,n)\mathrm{Func}(m, n) be the set of all functions from mm-bit strings to nn-bit strings. It has (2n)2m(2^n)^{2^m} members, because each of the 2m2^m inputs independently takes one of 2n2^n outputs. For an adversary AA and a function GG, write AG⇒1A^{G} \Rightarrow 1 for the event that AA, allowed to query GG, outputs 1. The PRF advantage of AA is AdvFprf(A)=|Pr[AF(K,⋅)⇒1]−Pr[AR⇒1]|,\mathrm{Adv}^{\mathrm{prf}}_F(A) = \left| \Pr\bigl[A^{F(K, \cdot)} \Rightarrow 1\bigr] - \Pr\bigl[A^{R} \Rightarrow 1\bigr] \right|, where the first probability is over a key KK chosen uniformly from {0,1}k\{0,1\}^k and over the random choices of AA, and the second is over a function RR chosen uniformly from Func(m,n)\mathrm{Func}(m, n) and over the random choices of AA.

FF is a (t,q,ε)(t, q, \varepsilon)-secure PRF if AdvFprf(A)≤ε\mathrm{Adv}^{\mathrm{prf}}_F(A) \le \varepsilon for every adversary AA that runs for at most tt steps and makes at most qq queries. Textbooks also use an asymptotic form. There the key length grows with a whole number λ\lambda, the security parameter, and every adversary whose running time is bounded by a polynomial in λ\lambda must have a negligible advantage: one that, for every polynomial pp, is below 1/p(λ)1/p(\lambda) once λ\lambda is large enough. Goldreich, Goldwasser and Micali gave the asymptotic definition in the paper “How to Construct Random Functions” (1986) [361].

The guarantee rests on three assumptions.

  1. A uniform, secret key. KK is chosen uniformly at random, and nothing about KK leaks, whether through another algorithm that uses the same key or through a side channel §1.29, an unintended source of information such as the running time of the computation.

  2. Query access only. The adversary learns F(K,x)F(K, x) only for inputs xx that it submits, and learns no other function of KK.

  3. Bounded resources. The bound ε\varepsilon holds only for adversaries within tt steps and qq queries.

No keyed function achieves ε=0\varepsilon = 0 against every adversary, because an adversary can guess keys. Such an adversary queries two inputs and, for each of gg candidate keys fixed in advance, checks whether that candidate reproduces both answers. In the real world the true key is among the candidates with probability g/2kg / 2^k. In the ideal world one candidate reproduces both random answers with probability 2−2n2^{-2n}, so by the union bound §15.42 (the probability that at least one of several events happens is at most the sum of their probabilities) some candidate does so with probability at most g⋅2−2ng \cdot 2^{-2n}. The advantage is therefore at least g/2k−g⋅2−2ng / 2^k - g \cdot 2^{-2n}. For k=n=128k = n = 128 and g=240g = 2^{40} guesses this is 2−88−2−2162^{-88} - 2^{-216}, very close to 2−882^{-88}. Every PRF bound must therefore name the adversary’s budget.

A PRF gives a message authentication code directly: the tag of a message xx is F(K,x)F(K, x). Suppose an attacker obtains the tags of up to q−1q - 1 messages of the attacker’s choice and then submits one new message with a guessed tag. The attacker succeeds with probability at most ε+2−n\varepsilon + 2^{-n}, where ε\varepsilon is the PRF advantage bound for qq queries and the attacker’s running time. The term 2−n2^{-n} is the chance of guessing the output of a truly random function on an input it has not been asked about.

Limits and common mistakes

A PRF is deterministic: the same key and input always give the same output. A PRF alone therefore does not encrypt. Using F(K,x)F(K, x) as a mask to XOR onto a message hides the message only if xx never repeats under one key, which is the rule counter mode is built on.

The definition promises nothing to someone who knows the key. A key holder may be able to find two inputs with the same output, or an input with a chosen output. A PRF is therefore no substitute for a collision-resistant §5.5 hash function, one for which nobody, with or without any secret, can feasibly find two inputs with the same output. And an unkeyed hash function is never a PRF, as the worked example shows.

Putting a secret key in front of the message and hashing the result does not automatically give a PRF. SHA-256 is built by the Merkle–Damgård construction §15.8, which processes the message in blocks and outputs its final internal state. From H(K‖x)H(K \mathbin{\Vert} x), where ‖\Vert joins two byte strings end to end, anyone can compute H(K‖x‖p‖y)H(K \mathbin{\Vert} x \mathbin{\Vert} p \mathbin{\Vert} y) for a suffix yy of their choice, where pp is the padding that the hash function appends and depends only on the length of K‖xK \mathbin{\Vert} x. This is the length-extension attack §5.11. An adversary queries xx, computes the extended value, queries the extended input and compares; the test always passes in the real world and passes with probability 2−2562^{-256} in the ideal world. HMAC §5.8 (hash-based message authentication code) avoids this weakness: it hashes twice, with two different keys derived from KK. HMAC is widely used as a PRF, and its security proofs assume properties of the hash function’s compression function, the fixed-size step that the hash function applies to each message block, not collision resistance.

The key must serve only this PRF. Reusing it in another algorithm, or deriving it from a password, breaks the first assumption. Key separation §5.23, the practice of giving each purpose its own key, prevents the first mistake; a password needs a deliberately slow password-hashing function §15.10 before its output can serve as a key.

The output length bounds what is built on top. A tag of n=32n = 32 bits is guessed correctly with probability 2−322^{-32} per attempt, however strong the key.

A block cipher §15.3 is a pseudorandom permutation §15.2, not a PRF: it never gives two inputs the same output, whereas a random function does so with probability about one half after about 2n/22^{n/2} queries (the birthday bound §5.6). Using a block cipher as a PRF adds at most q(q−1)/2n+1q(q-1)/2^{n+1} to the advantage, which is negligible for 128-bit blocks and moderate numbers of queries.

A PRF output is not fresh. A recorded message with its valid PRF tag still verifies when sent again; replay protection, authorization §1.7 and key management are separate mechanisms.

Questions answered

How does a PRF differ from a public cryptographic hash §5.1?

A pseudorandom function (PRF) has a secret key, and its security means that someone without the key, who can only submit inputs and see outputs, cannot tell it apart from a truly random function; a public cryptographic hash function has no key, anyone can compute it, and its security means that even someone who computes it freely cannot find collisions (two inputs with the same output) or preimages (an input with a given output). The two promises face different attackers. A PRF’s promise is made to outsiders and ends when the key is known. A hash’s promise holds against everyone, because nothing about the hash is secret.

An unkeyed hash can never be a PRF. An observer submits any input, computes the hash of that input, and compares it with the answer; this tells the hash apart from a random function with probability practically 1. Conversely, a PRF need not be collision resistant for someone who knows its key. A hash can be turned into a PRF by a keyed construction designed for the purpose, such as HMAC (hash-based message authentication code). Simply putting the key in front of the message is not enough for SHA-256 (Secure Hash Algorithm with a 256-bit output), because of the length-extension attack: from the hash of key and message, anyone can compute the hash of key, message and an appended suffix.

Related entries: HMAC (§5.8), Key derivation function (KDF) (§5.20), Pseudorandom permutation (PRP) (§15.2), Message authentication code (MAC) (§5.7), Length-extension attack (§5.11), Counter mode (CTR) (§15.5). Further reading: [7, 66, 361].