28-05 — Hash Functions and Digital Signatures

Phase: Cryptography | Subject: 28-05 Prerequisites: 28-04-asymmetric-encryption.md Next subject: 28-06-elliptic-curve-cryptography.md


Learning Objectives

By the end of this subject, you will be able to:

  1. Define the three security properties of cryptographic hash functions
  2. Explain the Merkle-Damgård construction and the SHA family
  3. Describe the birthday attack and its implications for hash lengths
  4. Explain how digital signatures work using RSA-PSS and ECDSA
  5. Apply hash functions to data integrity, password storage, and blockchain

Core Content

Cryptographic Hash Functions

A hash function $H: {0,1}^* \to {0,1}^n$ maps arbitrary-length input to a fixed-length output (digest).

Three essential security properties:

Property Definition Broken if...
Preimage resistance (one-way) Given $y$, hard to find $x$ with $H(x) = y$ $2^n$ work
Second preimage resistance Given $x_1$, hard to find $x_2 \neq x_1$ with $H(x_1) = H(x_2)$ $2^n$ work
Collision resistance Hard to find ANY pair $x_1 \neq x_2$ with $H(x_1) = H(x_2)$ $2^{n/2}$ work (birthday)

⚠️ CRITICAL: Collision resistance is the strongest property. If it breaks, second preimage resistance may still hold (as with MD5 initially). But for modern use, all three must hold.

The Birthday Attack

The birthday paradox: with only 23 people, the probability of a shared birthday exceeds 50%.

For hash functions with $n$-bit output, a collision can be found with approximately $2^{n/2}$ random trials (not $2^n$).

Why: When you have $k$ hashes, the number of pairs is $\binom{k}{2} \approx k^2/2$. When $k^2/2 \approx 2^n$, i.e., $k \approx 2^{n/2}$, you expect a collision.

Implication: A hash function needs twice the bit length for collision resistance than for preimage resistance. SHA-256 has 128-bit collision resistance and 256-bit preimage resistance.

The Merkle-Damgård Construction

Most hash functions (MD5, SHA-1, SHA-256) use the Merkle-Damgård construction:

  1. Pad input to a multiple of the block size
  2. Append length encoding (prevents length extension attacks — partially)
  3. Process blocks sequentially through a compression function $f$:

$$h_0 = IV$$ $$h_i = f(h_{i-1}, M_i)$$ $$H(M) = h_k$$

Strengths: simple, collision resistance of $f$ implies collision resistance of $H$.

Weakness: length extension attacks. Given $H(M)$, you can compute $H(M | \text{pad} | X)$ without knowing $M$. SHA-3 fixes this.

The SHA Family

Function Output (bits) Block size Rounds Security (collision) Status
MD5 128 512 64 Broken ($2^{18}$) Deprecated
SHA-1 160 512 80 Broken ($2^{63}$, SHAttered) Deprecated
SHA-256 256 512 64 128 bits Secure
SHA-512 512 1024 80 256 bits Secure
SHA-3 (Keccak) 224/256/384/512 1152/1088/832/576 24 varies Secure, different structure

SHA-256 compression: based on 64 rounds of operations on 8 × 32-bit state words, using message schedule, Ch, Maj, Σ functions. The design is ARX (Add-Rotate-XOR) for efficiency.

SHA-3 uses a sponge construction instead of Merkle-Damgård: absorb input, then squeeze output. This resists length extension attacks natively.

Applications of Hash Functions

Application How it's used
Data integrity Hash files; compare digest to detect tampering
Password storage Store $H(\text{salt} | \text{password})$; use bcrypt/scrypt/Argon2 (slow hashes)
Digital signatures Sign $H(m)$ instead of $m$ (efficiency + security)
Commitment schemes Publish $H(\text{secret})$ to commit, reveal later
Proof-of-work Find nonce such that $H(\text{block} | \text{nonce}) < \text{target}$
Merkle trees Efficient verification in blockchains, Git, certificate transparency

Digital Signatures

A digital signature provides: authentication (who signed), integrity (content unchanged), and non-repudiation (signer cannot deny).

RSA-PSS (Probabilistic Signature Scheme):

Signing: $s = \text{PSS-Pad}(H(m))^d \bmod n$ Verification: check $\text{PSS-Pad}(H(m)) \equiv s^e \pmod{n}$

PSS padding is critical — it provides randomization per signature.

ECDSA (Elliptic Curve Digital Signature Algorithm):

Standardized curve + parameters. Private key $d$, public key $Q = dG$.

Signing: 1. Compute $e = H(m)$ (hash the message) 2. Choose random nonce $k$, compute $R = kG$, let $r = R_x \bmod q$ 3. Compute $s = k^{-1}(e + r \cdot d) \bmod q$ 4. Signature: $(r, s)$

Verification: 1. Compute $e = H(m)$, $w = s^{-1} \bmod q$ 2. Compute $u_1 = ew \bmod q$, $u_2 = rw \bmod q$ 3. Compute $X = u_1 G + u_2 Q$, verify $X_x \bmod q \equiv r$

⚠️ CRITICAL (ECDSA nonce): The nonce $k$ MUST be random and unique per signature. Sony's PS3 hack exploited ECDSA with a fixed $k$. If $k$ is reused or predictable, the private key is recoverable with two signatures.

EdDSA (Ed25519): deterministic nonce from hash, faster, simpler, no per-signature randomness needed.

Signature vs MAC


Key Terms

Worked Examples

Example 1: Birthday Attack Calculation

How many random hash computations to find an SHA-256 collision with 50% probability?

Solution:

SHA-256 output is 256 bits. Collision resistance by birthday bound: $\approx \sqrt{\pi \cdot 2^{256} / 2} \approx 1.25 \cdot 2^{128}$.

That's $\approx 4 \times 10^{38}$ hash computations. For context: if you could compute $10^{12}$ hashes/second, it would take $4 \times 10^{26}$ seconds — far longer than the age of the universe.

Click for answer ~$1.25 \cdot 2^{128} \approx 4 \times 10^{38}$ operations. This is computationally infeasible.

Example 2: RSA Signature (simple, no padding)

Sign $m = 42$ with RSA private key $(n=143, d=103)$.

Solution:

Signature $s = 42^{103} \bmod 143$.

This is the same computation as decryption in the earlier RSA example. But since $\gcd(42, 143) \neq 1$ (143=11×13), let's use the fact that $42^{103} \equiv 42 \pmod{143}$... no, that's not right.

Actually, compute: $42 \equiv 9 \pmod{11}$ and $42 \equiv 3 \pmod{13}$.

Using CRT: compute $9^{103} \bmod 11$ and $3^{103} \bmod 13$. Since $103 = 10 \cdot 10 + 3$, $9^{103} \equiv 9^3 = 729 \equiv 729 - 66 \cdot 11 = 729 - 726 = 3 \pmod{11}$.

$3^{103} \bmod 13$: $\varphi(13)=12$, $103 = 8 \cdot 12 + 7$, $3^{103} \equiv 3^7 = 2187$, $2187 \div 13 = 168 \cdot 13 = 2184$, remain $3$.

$s \equiv 3 \pmod{11}$, $s \equiv 3 \pmod{13}$. So $s = 3$.

Verify: $3^7 \bmod 143 = 3^7 = 2187 \bmod 143$. $143 \cdot 15 = 2145$, $2187 - 2145 = 42$. ✓

Click for answer $s = 3$. Verification: $3^7 \bmod 143 = 42 = m$. ✓ (Note: real signatures use padding and hash the message.)

Example 3: ECDSA Nonce Reuse

Suppose two messages $m_1, m_2$ signed with the same ECDSA nonce $k$, producing signatures $(r, s_1)$ and $(r, s_2)$ (same $r$ because same $k$). Show how the private key is recovered.

Solution:

$s_1 = k^{-1}(e_1 + r \cdot d) \bmod q$ $s_2 = k^{-1}(e_2 + r \cdot d) \bmod q$

Subtract: $s_1 - s_2 = k^{-1}(e_1 - e_2) \bmod q$ Thus: $k = (e_1 - e_2)(s_1 - s_2)^{-1} \bmod q$

Recover $k$, then: $d = r^{-1}(s_1 k - e_1) \bmod q$.

Private key exposed from just two signatures with the same nonce.

Click for answer $k = (e_1 - e_2)(s_1 - s_2)^{-1} \bmod q$, then $d = r^{-1}(s_1 k - e_1) \bmod q$. This is why nonce reuse is fatal.


Quiz

Q1: What does the concept of Preimage resistance primarily refer to in this subject?

A) The definition and application of Preimage resistance B) A historical anecdote about Preimage resistance C) A computational error related to Preimage resistance D) A visual representation of Preimage resistance

Correct: A)

Q2: Which of the following is the key formula discussed in this subject?

A) H: \{0,1\}^ \to \{0,1\}^n B) The inverse operation of the formula in question C) A simplified version of H: \{0,1\}^ \to \{0,1\}^... D) An unrelated formula from a different topic

Correct: A)

Q3: What is the primary purpose of Second preimage resistance?

A) It replaces all other methods in this domain B) It is used to second preimage resistance in mathematical analysis C) It is primarily a historical notation system D) It is used only in advanced research contexts

Correct: B)

Q4: Which statement about Collision resistance is TRUE?

A) Collision resistance is not related to this subject B) Collision resistance is a fundamental concept covered in this subject C) Collision resistance is mentioned only as a historical footnote D) Collision resistance is an advanced topic beyond this subject's scope

Correct: B)

Q5: Based on the worked examples in this subject, what is the correct result?

A) The inverse of the correct answer B) A different result from a common mistake C) An unrelated numerical value D) RSA Signature (simple, no padding)

Correct: D)

Q6: How are Collision resistance and Data integrity related?

A) Collision resistance and Data integrity are closely related concepts B) Collision resistance is the inverse of Data integrity C) Collision resistance and Data integrity are completely unrelated topics D) Collision resistance is a special case of Data integrity

Correct: A)

Q7: What is a common pitfall when working with Password storage?

A) A common mistake is confusing Password storage with a similar concept B) Password storage is always computed the same way in all contexts C) The main error with Password storage is using it when it is not needed D) Password storage has no common misconceptions

Correct: A)

Q8: When should you apply Digital signatures?

A) Use Digital signatures only in pure mathematics contexts B) Avoid Digital signatures unless explicitly instructed C) Digital signatures is not practically useful D) Apply Digital signatures to solve problems in this subject's domain

Correct: D)

Practice Problems

  1. How many trials to find a preimage for SHA-256?

    Click for answer $2^{256}$ trials expected (brute force). Preimage resistance is 256 bits (full output length).

  2. If $H$ is 64-bit output, how many random hashes for a birthday collision (~50%)?

    Click for answer ~$2^{32} \approx 4.3 \times 10^9$ hashes. This is very feasible — 64-bit hashes are insecure against collisions.

  3. In RSA signing, why do we sign $H(m)$ rather than $m$ directly?

    Click for answer (1) Efficiency — hash is fixed-size, fast; (2) Security — prevents existential forgery attacks on raw RSA; (3) The hash reduces the message to the modulus size.

  4. What problem does a salt solve in password hashing?

    Click for answer Without salt, identical passwords produce identical hashes (rainbow table attack). A salt makes each hash unique, forcing an attacker to crack each password individually.

  5. Why is HMAC secure even when built on MD5 (which has collisions)?

    Click for answer HMAC security relies on the compression function's pseudorandomness, not on collision resistance directly. HMAC-MD5 remained secure long after MD5 collisions were found (though it's still discouraged today).


Summary

Key takeaways:


Pitfalls



Next Steps

Next up: 28-06-elliptic-curve-cryptography.md — smaller keys, same security, the future of asymmetric crypto.