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 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 be the keyed function, with keys of bits, inputs of bits and outputs of bits.
The challenger flips a fair coin in secret to choose the real world or the ideal world.
Real world. The challenger picks a key uniformly at random, meaning that each of the possible keys is equally likely, and keeps it secret. The challenger answers each query with .
Ideal world. The challenger answers each new input with an output chosen uniformly at random from the 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.
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.
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. 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 , where 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 and , XORs the two answers, and outputs 1 if the result is 1111.
Real world, with key : the answers are and , and . The test passes for every key, because the key occurs in both answers and , so the XOR of the answers is . The adversary outputs 1 with probability 1.
Ideal world: the two inputs differ, so the two answers are independent and uniform over the 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 .
The advantage is . 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 . The advantage is , which is 1 for every practical purpose.
Stated precisely
Let be a keyed function: for each key of bits, maps -bit inputs to -bit outputs. Let be the set of all functions from -bit strings to -bit strings. It has members, because each of the inputs independently takes one of outputs. For an adversary and a function , write for the event that , allowed to query , outputs 1. The PRF advantage of is where the first probability is over a key chosen uniformly from and over the random choices of , and the second is over a function chosen uniformly from and over the random choices of .
is a -secure PRF if for every adversary that runs for at most steps and makes at most queries. Textbooks also use an asymptotic form. There the key length grows with a whole number , the security parameter, and every adversary whose running time is bounded by a polynomial in must have a negligible advantage: one that, for every polynomial , is below once 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.
A uniform, secret key. is chosen uniformly at random, and nothing about 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.
Query access only. The adversary learns only for inputs that it submits, and learns no other function of .
Bounded resources. The bound holds only for adversaries within steps and queries.
No keyed function achieves against every adversary, because an adversary can guess keys. Such an adversary queries two inputs and, for each of 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 . In the ideal world one candidate reproduces both random answers with probability , 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 . The advantage is therefore at least . For and guesses this is , very close to . Every PRF bound must therefore name the adversary’s budget.
A PRF gives a message authentication code directly: the tag of a message is . Suppose an attacker obtains the tags of up to messages of the attacker’s choice and then submits one new message with a guessed tag. The attacker succeeds with probability at most , where is the PRF advantage bound for queries and the attacker’s running time. The term 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 as a mask to XOR onto a message hides the message only if 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 , where joins two byte strings end to end, anyone can compute for a suffix of their choice, where is the padding that the hash function appends and depends only on the length of . This is the length-extension attack §5.11. An adversary queries , computes the extended value, queries the extended input and compares; the test always passes in the real world and passes with probability in the ideal world. HMAC §5.8 (hash-based message authentication code) avoids this weakness: it hashes twice, with two different keys derived from . 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 bits is guessed correctly with probability 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 queries (the birthday bound §5.6). Using a block cipher as a PRF adds at most 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].