CSS411 Cryptography

Hash Function Notes

Supplementary notes on hash function requirements, birthday attacks, SHA-1 and signature schemes: ElGamal and DSA.

26 min read

Hash functionsSearch on YouTube

DefinitionSearch on YouTube

A function HH makes a hash value:

h=H(M)h = H(M)

MM is a message of variable length. hh is a fixed-length hash value. Other names are message digest and hash code. A footnote also calls it a cryptographic checksum.

The source appends the hash value to the message. At that time, the message is known to be correct. The receiver calculates the hash again to authenticate the message.

The hash function is not secret. Thus something must protect the hash value. Figures 10.1 and 10.2 show the basic uses:

  • Encrypt the hash value with a secret key.
  • Sign the hash value with a private key.
  • Hash a shared secret value together with the message.

The purpose of a hash function is to make a "fingerprint" of a file, message or other data block.

Six propertiesSearch on YouTube

HH must have these properties for message authentication:

#PropertyName
1HH works on a data block of any size.Practical
2HH gives a fixed-length output.Practical
3H(x)H(x) is easy to calculate for any xx, in hardware and software.Practical
4For a given hh, it is infeasible to find xx with H(x)=hH(x) = h.One-way
5For a given xx, it is infeasible to find y≠xy \neq x with H(y)=H(x)H(y) = H(x).Weak collision
6It is infeasible to find any pair (x,y)(x, y) with H(x)=H(y)H(x) = H(y).Strong collision
  • A function that satisfies properties 1 to 5 is a weak hash function.
  • A function that also satisfies property 6 is a strong hash function.

Why properties 4 to 6 matterSearch on YouTube

Property 4 (one-way). It is important when the sender hashes a secret value SABS_{AB} with the message. The sender does not send SABS_{AB}. If HH is not one-way, an attacker can find it:

  1. Intercept MM and C=H(SAB∥M)C = H(S_{AB} \Vert M).
  2. Invert the hash: SAB∥M=H−1(C)S_{AB} \Vert M = H^{-1}(C).
  3. Remove the known MM to get SABS_{AB}.

Property 5 (weak collision). It prevents forgery when the hash code is encrypted. The opponent can read the message and calculate its hash. But the opponent does not have the secret key. Without property 5, an attacker can do this:

  1. Intercept a message and its encrypted hash code.
  2. Calculate the unencrypted hash code of the message.
  3. Make a different message with the same hash code.

Property 6 (strong collision). It protects against the birthday attack.

General structureSearch on YouTube

Figure 10.3 shows the general structure of a secure hash code. SHA-1 uses this structure.

  • The function divides the message into blocks.
  • A compression function takes one block and a chaining variable CVCV. It gives the next CVCV.
  • The last CVCV is the hash value.

This structure is similar to CBC mode for symmetric algorithms.

Birthday attackSearch on YouTube

The ideaSearch on YouTube

Suppose that a 64-bit hash code is used. A sender transmits the message MM and an encrypted hash code CC. To replace the message, an opponent must find M′M' with H(M′)=H(M)H(M') = H(M). On average, this needs about 2632^{63} tries.

The birthday attack needs much less work. The source A signs a message. A appends an mm-bit hash code and encrypts the hash code with its private key.

Attack stepsSearch on YouTube

  1. The opponent makes 2m/22^{m/2} variations x′x' of the valid message xx. All variations have the same meaning.
  2. The opponent makes 2m/22^{m/2} variations y′y' of a fraudulent message yy.
  3. The opponent compares the two sets to find a pair with the same hash code. By the birthday paradox, the probability of success is more than 0.5. If there is no match, the opponent makes more variations.
  4. The opponent gives the valid variation to A for signature.
  5. The opponent attaches this signature to the fraudulent variation and sends it.

The two variations have the same hash code. Thus they have the same signature. The attack works without the encryption key.

For an mm-bit hash code, the birthday attack needs about 2m/22^{m/2} operations. For a 64-bit hash code, this is only about 2322^{32}, not 2632^{63}.

In a room of only 23 people, two people probably share a birthday. It is much easier to find any matching pair than to match one given value.

Secure Hash AlgorithmSearch on YouTube

HistorySearch on YouTube

AlgorithmOriginDigest sizeNote
MD4Ron Rivest, MIT128 bitsSHA is based on MD4
MD5Ron Rivest128 bitsReplaced MD4. Less popular now, because of cryptanalysis.
SHA (FIPS 180)NIST, 1993160 bitsThe Secure Hash Standard
SHA-1 (FIPS 180-1)NIST, 1995160 bitsRevised version
SHA-256, SHA-384, SHA-512 (FIPS 180-2)NIST, 2002256, 384, 512 bitsSame structure as SHA-1

In 2005, NIST announced a plan to stop approval of SHA-1 by 2010. Then a research team described a collision attack on SHA-1. It needs 2692^{69} operations. Before, people thought that a collision needed 2802^{80} operations.

SHA-1 stepsSearch on YouTube

SHA-1 takes a message of less than 2642^{64} bits. It gives a 160-bit digest. It processes the input in 512-bit blocks.

Step 1: append padding bits. Pad the message so that its length is 448 mod 512448 \bmod 512. The padding is one 1 bit, then the necessary 0 bits. Always add padding, also when the length is already correct. Thus the padding is 1 to 512 bits.

Step 2: append length. Append a 64-bit block. It contains the length of the original message, as an unsigned integer, most significant byte first.

Step 3: initialize the MD buffer. A 160-bit buffer holds the intermediate and final results. It has five 32-bit registers:

RegisterValue (hex)
A67452301
BEFCDAB89
C98BADCFE
D10325476
EC3D2E1F0

Step 4: process the message in 512-bit (16-word) blocks. The compression function has four rounds of 20 steps each.

  • Each round uses a different logical function: f1f_1, f2f_2, f3f_3 or f4f_4.
  • Each round takes the current block YqY_q and the buffer ABCDEABCDE, and updates the buffer.
  • Each step tt (0≤t≤790 \le t \le 79) uses an additive constant KtK_t. There are only four different constants, one for each round.
  • The output of round 4 is added to CVqCV_q modulo 2322^{32}. The result is CVq+1CV_{q+1}.

Step 5: output. After all LL blocks, the output of the last stage is the 160-bit digest.

The standard values of KtK_t and ftf_t (from the SHA lecture) are:

RoundStepsKtK_t (hex)ft(B,C,D)f_t(B, C, D)
10≤t≤190 \le t \le 195A827999(B∧C)∨(¬B∧D)(B \wedge C) \vee (\neg B \wedge D)
220≤t≤3920 \le t \le 396ED9EBA1B⊕C⊕DB \oplus C \oplus D
340≤t≤5940 \le t \le 598F1BBCDC(B∧C)∨(B∧D)∨(C∧D)(B \wedge C) \vee (B \wedge D) \vee (C \wedge D)
460≤t≤7960 \le t \le 79CA62C1D6B⊕C⊕DB \oplus C \oplus D

SHA-1 summarySearch on YouTube

CV0=IVCV_0 = IV

CVq+1=SUM32(CVq,ABCDEq)CV_{q+1} = SUM_{32}(CV_q, ABCDE_q)

MD=CVLMD = CV_L

SymbolMeaning
IVIVInitial value of the ABCDE buffer, from step 3
ABCDEqABCDE_qOutput of the last round for block qq
LLNumber of blocks, with the padding and length fields
SUM32SUM_{32}Addition modulo 2322^{32} on each word pair
MDMDFinal message digest

Pad to 448, append the 64-bit length, load ABCDE. Then do 4 rounds of 20 steps per block. Add the result to CVCV modulo 2322^{32}.

Digital signaturesSearch on YouTube

DisputesSearch on YouTube

Message authentication protects two parties from a third party. It does not protect the two parties from each other. Suppose that John sends an authenticated message to Mary. Two disputes are possible:

  • Forgery by the receiver. Mary makes a different message and says that it came from John. She uses the key that she shares with John to make the authentication code.
  • Denial by the sender. John says that he did not send the message. Mary can forge messages, so nobody can prove that John sent it.

When sender and receiver do not fully trust each other, use a digital signature.

PropertiesSearch on YouTube

A digital signature is similar to a handwritten signature. It must:

  • verify the author and the date and time of the signature,
  • authenticate the contents at the time of the signature,
  • be verifiable by third parties, to resolve disputes.

RequirementsSearch on YouTube

  1. The signature is a bit pattern that depends on the signed message.
  2. It uses information unique to the sender, to prevent forgery and denial.
  3. It is easy to make.
  4. It is easy to recognize and verify.
  5. It is infeasible to forge. This means a new message for an existing signature, or a false signature for a given message.
  6. It is practical to keep a copy of the signature in storage.

ElGamal signaturesSearch on YouTube

KeysSearch on YouTube

The ElGamal signature is related to Diffie-Hellman. It uses exponentiation in a finite (Galois) field. Its security depends on the difficulty of discrete logarithms.

  • The global elements are a prime qq and α\alpha, a primitive root of qq.
  • The private key signs. The public key verifies.

Each user A makes a key pair:

  • Select a secret xAx_A with 1<xA<q−11 < x_A < q - 1.
  • Calculate the public key yA=αxA mod qy_A = \alpha^{x_A} \bmod q.

Signing and verificationSearch on YouTube

Alice signs a message MM for Bob:

  1. Calculate the hash m=H(M)m = H(M), with 0≤m≤q−10 \le m \le q-1.
  2. Select a random KK with 1≤K≤q−11 \le K \le q-1 and gcd⁡(K,q−1)=1\gcd(K, q-1) = 1.
  3. Calculate the temporary key S1=αK mod qS_1 = \alpha^K \bmod q.
  4. Calculate K−1 mod (q−1)K^{-1} \bmod (q-1).
  5. Calculate S2=K−1(m−xAS1) mod (q−1)S_2 = K^{-1}(m - x_A S_1) \bmod (q-1).
  6. The signature is (S1,S2)(S_1, S_2).

Any user B verifies the signature:

V1=αm mod q,V2=yAS1⋅S1S2 mod qV_1 = \alpha^m \bmod q, \qquad V_2 = y_A^{S_1} \cdot S_1^{S_2} \bmod q

The signature is valid if V1=V2V_1 = V_2.

Why it works: yAS1S1S2=αxAS1+KS2y_A^{S_1} S_1^{S_2} = \alpha^{x_A S_1 + K S_2}. Also KS2≡m−xAS1(modq−1)K S_2 \equiv m - x_A S_1 \pmod{q-1}. Thus the exponent is mm, and V2=αm=V1V_2 = \alpha^m = V_1.

Worked exampleSearch on YouTube

Use GF(19)GF(19): q=19q = 19 and α=10\alpha = 10.

Keys. Alice selects xA=16x_A = 16. Then yA=1016 mod 19=4y_A = 10^{16} \bmod 19 = 4.

Signing. The message hash is m=14m = 14.

  • Select K=5K = 5. gcd⁡(18,5)=1\gcd(18, 5) = 1.
  • S1=105 mod 19=3S_1 = 10^5 \bmod 19 = 3.
  • K−1 mod 18=5−1 mod 18=11K^{-1} \bmod 18 = 5^{-1} \bmod 18 = 11, because 5×11=55=54+15 \times 11 = 55 = 54 + 1.
  • S2=11(14−16×3) mod 18=11×(−34) mod 18=4S_2 = 11(14 - 16 \times 3) \bmod 18 = 11 \times (-34) \bmod 18 = 4.

The signature is (3,4)(3, 4).

Verification.

  • V1=1014 mod 19=16V_1 = 10^{14} \bmod 19 = 16
  • V2=43⋅34 mod 19=64×81 mod 19=5184 mod 19=16V_2 = 4^3 \cdot 3^4 \bmod 19 = 64 \times 81 \bmod 19 = 5184 \bmod 19 = 16

16=1616 = 16, so the signature is valid.

Reduce the S2S_2 line in two small steps. 14−48=−3414 - 48 = -34, and −34+36=2-34 + 36 = 2. Then S2=11×2 mod 18=22 mod 18=4S_2 = 11 \times 2 \bmod 18 = 22 \bmod 18 = 4.

Digital Signature StandardSearch on YouTube

HistorySearch on YouTube

NIST published FIPS 186, the Digital Signature Standard (DSS). DSS uses SHA and a new technique, the Digital Signature Algorithm (DSA).

YearEvent
1991First proposal
1993Revision after public comments on security
1996Minor revision
2000FIPS 186-2 adds algorithms based on RSA and on elliptic curves

DSA gives only the signature function. Unlike RSA, it cannot encrypt or exchange keys. It is a public-key technique. Its security depends on the difficulty of discrete logarithms, as in Diffie-Hellman.

Parameters and keysSearch on YouTube

A group of users can share three public parameters:

  • qq: a 160-bit prime.
  • pp: a prime of 512 to 1024 bits, with q∣(p−1)q \mid (p-1).
  • g=h(p−1)/q mod pg = h^{(p-1)/q} \bmod p, with 1<h<p−11 < h < p - 1 and g>1g > 1.

Each user selects a private key xx randomly or pseudorandomly. The standard uses 0<x<q0 < x < q. (The notes write "1 to p−1p-1".) The public key is y=gx mod py = g^x \bmod p. It is easy to calculate yy from xx. It is infeasible to find xx from yy.

Signing and verificationSearch on YouTube

To sign, the user calculates rr and ss. They use (p,q,g)(p, q, g), the private key xx, the hash H(M)H(M) and a random kk. Use a new kk for each signature.

r=(gk mod p) mod qr = \left(g^k \bmod p\right) \bmod q

s=k−1(H(M)+xr) mod qs = k^{-1}\left(H(M) + x r\right) \bmod q

The receiver calculates vv from the public parameters, the public key of the sender and the hash of the message:

w=s−1 mod qw = s^{-1} \bmod q

u1=H(M)⋅w mod q,u2=r⋅w mod qu_1 = H(M) \cdot w \bmod q, \qquad u_2 = r \cdot w \bmod q

v=(gu1⋅yu2 mod p) mod qv = \left(g^{u_1} \cdot y^{u_2} \bmod p\right) \bmod q

The signature is valid if v=rv = r.

The final test is on rr. The value rr does not depend on the message. It depends only on kk and the three global parameters. The receiver can calculate rr again from the message, the signature and the public keys. Discrete logarithms are difficult. Thus an opponent cannot find kk from rr or xx from ss.

Quick self-testSearch on YouTube

Give the six properties of a hash function for message authentication.

Any input size. Fixed-length output. Easy to calculate. One-way. Weak collision resistance. Strong collision resistance.

How does the one-way property protect SABS_{AB} in C=H(SAB∥M)C = H(S_{AB} \Vert M)?

If an attacker can invert HH, it gets SAB∥MS_{AB} \Vert M from CC. It knows MM, so it gets SABS_{AB}. A one-way HH prevents this.

Describe the birthday attack and its effort for a 64-bit hash.

Make 2322^{32} valid and 2322^{32} fraudulent variations. Find a pair with the same hash. Get A to sign the valid variation, then attach the signature to the fraudulent one. The effort is about 2322^{32}, not 2632^{63}.

What is the difference between a weak and a strong hash function?

A weak hash function satisfies properties 1 to 5. A strong hash function also satisfies property 6, so it resists the birthday attack.

Give the five SHA-1 processing steps.

Append padding to 448 mod 512448 \bmod 512. Append the 64-bit length. Initialize ABCDE. Process each 512-bit block in four rounds of 20 steps. Output the 160-bit CVLCV_L.

What collision attack on SHA-1 do the notes give?

A research team found collisions with 2692^{69} operations. Before, people thought that this needed 2802^{80} operations.

Why must a digital signature use information unique to the sender?

It prevents forgery by the receiver and denial by the sender. Only the sender can make the signature.

In the GF(19)GF(19) ElGamal example, give the signature and the check.

yA=4y_A = 4, S1=3S_1 = 3, K−1=11K^{-1} = 11, S2=4S_2 = 4. V1=1014 mod 19=16V_1 = 10^{14} \bmod 19 = 16 and V2=43⋅34 mod 19=16V_2 = 4^3 \cdot 3^4 \bmod 19 = 16. The signature is valid.

Give the DSA signing and verification equations.

Sign: r=(gk mod p) mod qr = (g^k \bmod p) \bmod q and s=k−1(H(M)+xr) mod qs = k^{-1}(H(M) + xr) \bmod q. Verify: w=s−1w = s^{-1}, u1=H(M)wu_1 = H(M) w, u2=rwu_2 = r w, v=(gu1yu2 mod p) mod qv = (g^{u_1} y^{u_2} \bmod p) \bmod q. Valid if v=rv = r.

  • h=H(M)h = H(M) is a fixed-length fingerprint. A weak hash function has properties 1 to 5. A strong one also has property 6.
  • The birthday attack needs about 2m/22^{m/2} operations: 2322^{32} for a 64-bit hash.
  • SHA-1: pad, append the length, initialize ABCDE, process 512-bit blocks in 80 steps, output 160 bits.
  • A digital signature prevents forgery by the receiver and denial by the sender.
  • ElGamal: S1=αK mod qS_1 = \alpha^K \bmod q, S2=K−1(m−xAS1) mod (q−1)S_2 = K^{-1}(m - x_A S_1) \bmod (q-1). Valid if αm=yAS1S1S2\alpha^m = y_A^{S_1} S_1^{S_2}.
  • DSA signs only. Valid if v=rv = r.