# Birthday attack

__: Type of cryptographic attack__

**Short description**A **birthday attack** is a type of cryptographic attack that exploits the mathematics behind the birthday problem in probability theory. This attack can be used to abuse communication between two or more parties. The attack depends on the higher likelihood of collisions found between random attack attempts and a fixed degree of permutations (pigeonholes). With a birthday attack, it is possible to find a collision of a hash function in [math]\displaystyle{ \sqrt{2^n} = 2^{n / 2} }[/math], with [math]\displaystyle{ 2^n }[/math] being the classical preimage resistance security. There is a general (though disputed^{[1]}) result that quantum computers can perform birthday attacks, thus breaking collision resistance, in [math]\displaystyle{ \sqrt[3]{2^n} = 2^{n / 3} }[/math].^{[2]}

## Understanding the problem

As an example, consider the scenario in which a teacher with a class of 30 students (n = 30) asks for everybody's birthday (for simplicity, ignore leap years) to determine whether any two students have the same birthday (corresponding to a hash collision as described further). Intuitively, this chance may seem small. Counter-intuitively, the probability that at least one student has the same birthday as *any* other student on any day is around 70% (for n = 30), from the formula [math]\displaystyle{ 1-\frac{365!}{(365-n)!\cdot 365^n} }[/math].^{[3]}

If the teacher had picked a *specific* day (say, 16 September), then the chance that at least one student was born on that specific day is [math]\displaystyle{ 1 - (364/365)^{30} }[/math], about 7.9%.

In a birthday attack, the attacker prepares many different variants of benign and malicious contracts, each having a digital signature. A pair of benign and malicious contracts with the same signature is sought. In this fictional example, suppose that the digital signature of a string is the first byte of its SHA-256 hash. The pair found is indicated in green – note that finding a pair of benign contracts (blue) or a pair of malicious contracts (red) is useless. After the victim accepts the benign contract, the attacker substitutes it with the malicious one and claims the victim signed it, as proven by the digital signature.

## Mathematics

Given a function [math]\displaystyle{ f }[/math], the goal of the attack is to find two different inputs [math]\displaystyle{ x_{1}, x_{2} }[/math] such that [math]\displaystyle{ f(x_{1}) = f(x_{2}) }[/math]. Such a pair [math]\displaystyle{ x_{1}, x_{2} }[/math] is called a collision. The method used to find a collision is simply to evaluate the function [math]\displaystyle{ f }[/math] for different input values that may be chosen randomly or pseudorandomly until the same result is found more than once. Because of the birthday problem, this method can be rather efficient. Specifically, if a function [math]\displaystyle{ f(x) }[/math] yields any of [math]\displaystyle{ H }[/math] different outputs with equal probability and [math]\displaystyle{ H }[/math] is sufficiently large, then we expect to obtain a pair of different arguments [math]\displaystyle{ x_{1} }[/math] and [math]\displaystyle{ x_{2} }[/math] with [math]\displaystyle{ f(x_{1}) = f(x_{2}) }[/math] after evaluating the function for about [math]\displaystyle{ 1.25\sqrt{H} }[/math] different arguments on average.

We consider the following experiment. From a set of *H* values we choose *n* values uniformly at random thereby allowing repetitions. Let *p*(*n*; *H*) be the probability that during this experiment at least one value is chosen more than once. This probability can be approximated as

- [math]\displaystyle{ p(n;H) \approx 1 - e^{-n(n-1)/(2H)} \approx 1-e^{-n^2/(2H)} }[/math]
^{[4]}

Let *n*(*p*; *H*) be the smallest number of values we have to choose, such that the probability for finding a collision is at least *p*. By inverting this expression above, we find the following approximation

- [math]\displaystyle{ n(p;H)\approx \sqrt{2H\ln\frac{1}{1-p}} }[/math]

and assigning a 0.5 probability of collision we arrive at

- [math]\displaystyle{ n(0.5;H) \approx 1.1774 \sqrt H }[/math]

Let *Q*(*H*) be the expected number of values we have to choose before finding the first collision. This number can be approximated by

- [math]\displaystyle{ Q(H)\approx \sqrt{\frac{\pi}{2}H} }[/math]

As an example, if a 64-bit hash is used, there are approximately 1.8×10^{19} different outputs. If these are all equally probable (the best case), then it would take 'only' approximately 5 billion attempts (5.38×10^{9}) to generate a collision using brute force.^{[5]} This value is called **birthday bound**^{[6]} and for *n*-bit codes it could be approximated as 2^{n/2}.^{[7]} Other examples are as follows:

Bits Possible outputs (H) Desired probability of random collision

(2 s.f.) (p)10 ^{−18}10 ^{−15}10 ^{−12}10 ^{−9}10 ^{−6}0.1% 1% 25% 50% 75% 16 2 ^{16}(~6.5 x 10^{4})<2 <2 <2 <2 <2 11 36 190 300 430 32 2 ^{32}(~4.3×10^{9})<2 <2 <2 3 93 2900 9300 50,000 77,000 110,000 64 2 ^{64}(~1.8×10^{19})6 190 6100 190,000 6,100,000 1.9×10 ^{8}6.1×10 ^{8}3.3×10 ^{9}5.1×10 ^{9}7.2×10 ^{9}128 2 ^{128}(~3.4×10^{38})2.6×10 ^{10}8.2×10 ^{11}2.6×10 ^{13}8.2×10 ^{14}2.6×10 ^{16}8.3×10 ^{17}2.6×10 ^{18}1.4×10 ^{19}2.2×10 ^{19}3.1×10 ^{19}256 2 ^{256}(~1.2×10^{77})4.8×10 ^{29}1.5×10 ^{31}4.8×10 ^{32}1.5×10 ^{34}4.8×10 ^{35}1.5×10 ^{37}4.8×10 ^{37}2.6×10 ^{38}4.0×10 ^{38}5.7×10 ^{38}384 2 ^{384}(~3.9×10^{115})8.9×10 ^{48}2.8×10 ^{50}8.9×10 ^{51}2.8×10 ^{53}8.9×10 ^{54}2.8×10 ^{56}8.9×10 ^{56}4.8×10 ^{57}7.4×10 ^{57}1.0×10 ^{58}512 2 ^{512}(~1.3×10^{154})1.6×10 ^{68}5.2×10 ^{69}1.6×10 ^{71}5.2×10 ^{72}1.6×10 ^{74}5.2×10 ^{75}1.6×10 ^{76}8.8×10 ^{76}1.4×10 ^{77}1.9×10 ^{77}

*Table shows number of hashes n*(*p*)*needed to achieve the given probability of success, assuming all hashes are equally likely. For comparison,*10^{−18}*to*10^{−15}*is the uncorrectable bit error rate of a typical hard disk.*^{[8]}In theory, MD5 hashes or UUIDs, being roughly 128 bits, should stay within that range until about 820 billion documents, even if its possible outputs are many more.

It is easy to see that if the outputs of the function are distributed unevenly, then a collision could be found even faster. The notion of 'balance' of a hash function quantifies the resistance of the function to birthday attacks (exploiting uneven key distribution.) However, determining the balance of a hash function will typically require all possible inputs to be calculated and thus is infeasible for popular hash functions such as the MD and SHA families.^{[9]}
The subexpression [math]\displaystyle{ \ln\frac{1}{1-p} }[/math] in the equation for [math]\displaystyle{ n(p;H) }[/math] is not computed accurately for small [math]\displaystyle{ p }[/math] when directly translated into common programming languages as `log(1/(1-p))`

due to loss of significance. When `log1p`

is available (as it is in C99) for example, the equivalent expression `-log1p(-p)`

should be used instead.^{[10]} If this is not done, the first column of the above table is computed as zero, and several items in the second column do not have even one correct significant digit.

### Simple approximation

A good rule of thumb which can be used for mental calculation is the relation

- [math]\displaystyle{ p(n) \approx {n^2 \over 2H} }[/math]

which can also be written as

- [math]\displaystyle{ H \approx {n^2 \over 2p(n)} }[/math].

or

- [math]\displaystyle{ n \approx \sqrt { 2H \times p(n)} }[/math].

This works well for probabilities less than or equal to 0.5.

This approximation scheme is especially easy to use when working with exponents. For instance, suppose you are building 32-bit hashes ([math]\displaystyle{ H = 2^{32} }[/math]) and want the chance of a collision to be at most one in a million ([math]\displaystyle{ p \approx 2^{-20} }[/math]), how many documents could we have at the most?

- [math]\displaystyle{ n \approx \sqrt { 2 \times 2^{32} \times 2^{-20}} = \sqrt { 2^{1+32-20} } = \sqrt { 2^{13} } = 2^{6.5} \approx 90.5 }[/math]

which is close to the correct answer of 93.

## Digital signature susceptibility

Digital signatures can be susceptible to a birthday attack. A message [math]\displaystyle{ m }[/math] is typically signed by first computing [math]\displaystyle{ f(m) }[/math], where [math]\displaystyle{ f }[/math] is a cryptographic hash function, and then using some secret key to sign [math]\displaystyle{ f(m) }[/math]. Suppose Mallory wants to trick Bob into signing a fraudulent contract. Mallory prepares a fair contract [math]\displaystyle{ m }[/math] and a fraudulent one [math]\displaystyle{ m' }[/math]. She then finds a number of positions where [math]\displaystyle{ m }[/math] can be changed without changing the meaning, such as inserting commas, empty lines, one versus two spaces after a sentence, replacing synonyms, etc. By combining these changes, she can create a huge number of variations on [math]\displaystyle{ m }[/math] which are all fair contracts.

In a similar manner, Mallory also creates a huge number of variations on the fraudulent contract [math]\displaystyle{ m' }[/math]. She then applies the hash function to all these variations until she finds a version of the fair contract and a version of the fraudulent contract which have the same hash value, [math]\displaystyle{ f(m) = f(m') }[/math]. She presents the fair version to Bob for signing. After Bob has signed, Mallory takes the signature and attaches it to the fraudulent contract. This signature then "proves" that Bob signed the fraudulent contract.

The probabilities differ slightly from the original birthday problem, as Mallory gains nothing by finding two fair or two fraudulent contracts with the same hash. Mallory's strategy is to generate pairs of one fair and one fraudulent contract. The birthday problem equations apply where [math]\displaystyle{ n }[/math] is the number of pairs. The number of hashes Mallory actually generates is [math]\displaystyle{ 2n }[/math].

To avoid this attack, the output length of the hash function used for a signature scheme can be chosen large enough so that the birthday attack becomes computationally infeasible, i.e. about twice as many bits as are needed to prevent an ordinary brute-force attack.

Besides using a larger bit length, the signer (Bob) can protect himself by making some random, inoffensive changes to the document before signing it, and by keeping a copy of the contract he signed in his own possession, so that he can at least demonstrate in court that his signature matches that contract, not just the fraudulent one.

Pollard's rho algorithm for logarithms is an example for an algorithm using a birthday attack for the computation of discrete logarithms.

### Reverse Attack

The same fraud is possible if the signer is Mallory, not Bob. Bob could suggest a contract to Mallory for a signature. Mallory could find both an inoffensively-modified version of this fair contract that has the same signature as a fraudulent contract, and Mallory could provide the modified fair contract and signature to Bob. Later, Mallory could produce the fraudulent copy. If Bob doesn't have the inoffensively-modified version contract (perhaps only finding their original proposal), Mallory's fraud is perfect. If Bob does have it, Mallory can at least claim that it is Bob who is the fraudster.

## See also

## Notes

- ↑ Daniel J. Bernstein. "Cost analysis of hash collisions : Will quantum computers make SHARCS obsolete?". http://cr.yp.to/hash/collisioncost-20090823.pdf.
- ↑ Brassard, Gilles; HØyer, Peter; Tapp, Alain (20 April 1998).
*LATIN'98: Theoretical Informatics*. Lecture Notes in Computer Science.**1380**. Springer, Berlin, Heidelberg. pp. 163–169. doi:10.1007/BFb0054319. ISBN 978-3-540-64275-6. - ↑ "Math Forum: Ask Dr. Math FAQ: The Birthday Problem". http://mathforum.org/dr.math/faq/faq.birthdayprob.html.
- ↑ Gupta, Ganesh (2015) (in en).
*What is Birthday attack??*. doi:10.13140/2.1.4915.7443. http://rgdoi.net/10.13140/2.1.4915.7443. - ↑ Flajolet, Philippe; Odlyzko, Andrew M. (1990). Quisquater, Jean-Jacques; Vandewalle, Joos. eds. "Random Mapping Statistics" (in en).
*Advances in Cryptology — EUROCRYPT '89*. Lecture Notes in Computer Science (Berlin, Heidelberg: Springer)**434**: 329–354. doi:10.1007/3-540-46885-4_34. ISBN 978-3-540-46885-1. https://link.springer.com/chapter/10.1007%2F3-540-46885-4_34. - ↑ See upper and lower bounds.
- ↑ Jacques Patarin, Audrey Montreuil (2005) (PostScript, PDF).
*Benes and Butterfly schemes revisited*. Université de Versailles. http://eprint.iacr.org/2005/004. Retrieved 2007-03-15. - ↑ Gray, Jim; van Ingen, Catharine (25 January 2007). "Empirical Measurements of Disk Failure Rates and Error Rates". arXiv:cs/0701166.
- ↑ "CiteSeerX". http://citeseer.ist.psu.edu/bellare02hash.html.
- ↑ "Compute log(1+x) accurately for small values of x". http://www.mathworks.com/help/techdoc/ref/log1p.html.

## References

- Mihir Bellare, Tadayoshi Kohno: Hash Function Balance and Its Impact on Birthday Attacks. EUROCRYPT 2004: pp401–418
*Applied Cryptography, 2nd ed.*by Bruce Schneier

## External links

- "What is a digital signature and what is authentication?" from RSA Security's crypto FAQ.
- "Birthday Attack" X5 Networks Crypto FAQs

de:Kollisionsangriff#Geburtstagsangriff

Original source: https://en.wikipedia.org/wiki/Birthday attack.
Read more |