Sub topic 11
What cryptographic hashes add
This chapter assumes you know what a hash function is, why collisions are unavoidable, and the birthday bound from the earlier chapters. You should also be comfortable reading short Python.
Different threat model
A hash for a hash table only has to spread honest keys evenly and run fast. A cryptographic hash has to hold up against an attacker who chooses inputs on purpose. It keeps the basics, which are determinism, a fixed-size output, and a strong avalanche effect. It adds three resistance properties.
The three resistance properties
Let H be a hash with an n-bit output.
| Property | Attacker's goal | Best generic cost |
|---|---|---|
| Preimage resistance | Given a digest `d`, find any `m` with `H(m) = d` | about 2^n |
| Second-preimage resistance | Given `m1`, find a different `m2` with `H(m2) = H(m1)` | about 2^n |
| Collision resistance | Find any pair `m1 != m2` with `H(m1) = H(m2)` | about 2^(n/2) |
The collision cost is the birthday bound at work. The attacker may pick both inputs, so the search needs only about the square root of the output space. For SHA-256 that is 2^128 work for collisions and 2^256 for the other two.
Collision resistance is the strongest requirement in practice. If you can find a collision, you can usually build two documents with the same digest. If a hash is collision resistant, it is also second-preimage resistant in practice, but the reverse is not true.
Seeing the bound with a short hash
Truncating SHA-256 to 32 bits removes the security margin. The following program finds a collision by brute force.
import hashlib
def h32(data: bytes) -> bytes:
return hashlib.sha256(data).digest()[:4] # keep only 32 bits
seen = {}
i = 0
while True:
msg = str(i).encode()
tag = h32(msg)
if tag in seen:
print("collision:", seen[tag], msg, tag.hex())
print("after", i + 1, "inputs")
break
seen[tag] = msg
i += 1It stops after roughly 2^16 to 2^17 inputs, which matches the birthday estimate and takes well under a second. Finding a second input for one specific 32-bit digest would take about four billion tries. The collision is cheap because you may choose both inputs.
Where the properties come from
The design goal is that the only known attack is brute force. To get there, a cryptographic hash must also be one-way: no known shortcut inverts it, because the internal rounds mix and discard information. The avalanche effect is necessary for this but not sufficient. FNV-1a avalanches reasonably well, yet anyone can construct inputs that collide, because its structure is simple and invertible.
Common mistakes
- Using a fast non-cryptographic hash, such as FNV-1a, to verify downloads or sign data. An attacker can craft a collision on purpose.
- Truncating a digest to save space without recomputing the security. A 64-bit tag gives only about 32 bits of collision resistance.
- Assuming a cryptographic hash is a good password hash. It resists inversion in the abstract, but it is also fast, which helps an attacker guess passwords.
Exercise
A hash outputs 160 bits. Roughly how much work does a generic attack need to find a collision, and how much for a preimage?
Show answer
A collision needs about 2^80 work, from the birthday bound (2^(160/2)). A preimage needs about 2^160 work.
Exercise
In the truncation program, why does the search succeed after about 100,000 inputs instead of billions?
Show answer
The program is looking for any two inputs with the same 32-bit tag, not a match for a fixed target. By the birthday bound, that takes about 2^16 to 2^17 inputs.