CSS411 Cryptography

Hash Functions

Slide deck 13: hash definition, uses (MIC, MAC, signatures, passwords), requirements table, insecure toy hashes and the birthday attack.

22 min read

Hash function basicsSearch on YouTube

DefinitionSearch on YouTube

A hash function condenses an arbitrary message to a fixed size:

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

  • The input is a message or data block MM of variable length LL bits. The output is a hash value hh of fixed length.
  • We usually assume that the hash function is public. No secret is part of HH.
  • The hash detects changes to the message.

Cryptographic propertiesSearch on YouTube

A cryptographic hash function has two more properties:

  • One-way property: it is computationally infeasible to find data that maps to a specific hash, H(y)=hH(y) = h.
  • Collision-free property: it is computationally infeasible to find two data with the same hash, H(x)=H(y)H(x) = H(y).

A hash is like a serial number for a document. Any document, short or long, gets a short serial of fixed length. Nobody can rebuild the document from the serial. Two documents with the same serial show a design flaw.

Hash function usesSearch on YouTube

MIC, MAC and signatureSearch on YouTube

UseWhat is sentEncryption
Message Integrity Check (MIC)Hash of the message (digest)MIC always encrypted, message optionally
Message Authentication Code (MAC)Keyed hash of the messageMAC, message optionally encrypted
Digital signature (non-repudiation)Hash encrypted with the private (signing) keyVerified with the public (verification) key

A MAC uses a shared secret key. Only the two parties can check it, so neither party can prove the origin to a third party. A signature uses a private key, so a third party can check it and settle a dispute.

Symmetric key, unkeyed hashSearch on YouTube

The slides show two diagrams. Source A sends to destination B.

  • (a) Message encrypted: A computes H(M)H(M), concatenates it with MM and encrypts both with key KK. The channel carries E(K,[M∥H(M)])\mathrm{E}(K, [M \| H(M)]). B decrypts with KK, computes the hash of MM again and compares.
  • (b) Message unencrypted: A encrypts only the hash. The channel carries MM with E(K,H(M))\mathrm{E}(K, H(M)). B decrypts the hash with KK and compares it with a new hash of MM.
Source ADestination BMHE(K, ·)M ‖ E(K, H(M))D(K, ·)CompareHM also goes in clearAuthentication only: anyone can read M, but nobody can change the digest without K.
Hash use (b): A sends M in clear with E(K, H(M)). B decrypts the digest and compares it with a new hash of M.

Diagram (b) gives authentication only. The message goes in clear, so anyone can read it. The encryption E(K,H(M))\mathrm{E}(K, H(M)) stops changes to the digest. It does not keep MM secret.

Symmetric key, keyed hashSearch on YouTube

In the next two diagrams, the hash also takes a shared secret value SS:

  • (c) Message unencrypted: A computes H(M∥S)H(M \| S) and sends it with MM. B computes H(M∥S)H(M \| S) with the same SS and compares.
  • (d) Message encrypted: A encrypts MM and H(M∥S)H(M \| S) with KK. The channel carries E(K,[M∥H(M∥S)])\mathrm{E}(K, [M \| H(M \| S)]). B decrypts with KK, computes the hash with SS and compares.

The secret SS changes the plain hash into a MAC. The attacker does not know SS, so the attacker cannot compute a valid hash for a changed message.

Digital signatures (PKCS)Search on YouTube

The PKCS diagrams use a public-key pair:

  • (a) A hashes MM and encrypts the digest with the private key: E(PRa,H(M))\mathrm{E}(PR_a, H(M)). The message and the signature go together. B hashes MM, decrypts the signature with PUaPU_a and compares.
  • (b) This adds confidentiality. A encrypts M∥E(PRa,H(M))M \| \mathrm{E}(PR_a, H(M)) with a symmetric key KK. The channel carries E(K,[M∥E(PRa,H(M))])\mathrm{E}(K, [M \| \mathrm{E}(PR_a, H(M))]). B decrypts with KK, then verifies the signature with PUaPU_a.

Sign the hash, not the message:

sign=E(PRa, H(M))verify:D(PUa, sign)=H(M) ?\text{sign} = \mathrm{E}(PR_a,\, H(M)) \qquad \text{verify}: \mathrm{D}(PU_a,\, \text{sign}) = H(M)\ \text{?}

The hash keeps the signature short and fast for any length of MM.

Other usesSearch on YouTube

  • Pseudorandom function (PRF): generate session keys and nonces. Produce a key from a password. Derive keys from a master key cooperatively.
  • Pseudorandom number generator (PRNG).
  • One-way password file: store the hash of the password, not the actual password. Unix and Windows NT do this. A salt deters precomputation attacks.
  • Intrusion detection and virus detection: keep and check the hash of each file on the system. An example tool is Tripwire.

If an attacker steals the password file, the attacker gets only hashes. A precomputation attack makes a table of hashes for common passwords. A different salt for each user makes one table useless for the other users.

Two insecure hash functionsSearch on YouTube

Block XORSearch on YouTube

The first toy hash is a bit-by-bit exclusive-OR (XOR) of every block:

Ci=bi1xorbi2xor⋯xorbimC_i = b_{i1} \mathbin{\mathrm{xor}} b_{i2} \mathbin{\mathrm{xor}} \dots \mathbin{\mathrm{xor}} b_{im}

Bit ii of the hash is the XOR of bit ii of all mm blocks. This is a longitudinal redundancy check. It is reasonably effective as a data integrity check. One changed message bit changes exactly one hash bit.

Rotate and XORSearch on YouTube

The second toy hash adds a one-bit circular shift on the hash value:

  1. Take each successive nn-bit block.
  2. Rotate the current hash value to the left by 1 bit.
  3. XOR the block into the hash value.

The slides say that this is good for data integrity but useless for security.

Both toy hashes are linear and easy to invert. For any target digest, an attacker can make a message with that digest. Thus they have no preimage resistance and no collision resistance.

Requirements and attacksSearch on YouTube

Requirements tableSearch on YouTube

RequirementDescription
Variable input sizeHH can be applied to a block of data of any size
Fixed output sizeHH produces a fixed-length output
EfficiencyH(x)H(x) is relatively easy to compute for any given xx, making both hardware and software implementations practical
Preimage resistant (one-way property)For any given hash value hh, it is computationally infeasible to find yy such that H(y)=hH(y) = h
Second preimage resistant (weak collision resistant)For any given block xx, it is computationally infeasible to find y≠xy \ne x with H(y)=H(x)H(y) = H(x)
Collision resistant (strong collision resistant)It is computationally infeasible to find any pair (x,y)(x, y) such that H(x)=H(y)H(x) = H(y)
PseudorandomnessOutput of HH meets standard tests for pseudorandomness

Seven requirements in order: V, F, E, P, S, C, P. They are Variable in, Fixed out, Efficient, Preimage, Second preimage, Collision, Pseudorandom. Weak resistance fixes one xx. Strong resistance lets the attacker choose both messages.

Attack typesSearch on YouTube

The attacks are brute-force attacks and cryptanalysis.

  • Preimage or second preimage attack: find yy such that H(y)H(y) equals a given hash value.
  • Collision attack (against collision resistance): find two messages xx and yy with the same hash, H(x)=H(y)H(x) = H(y).

Brute-force effortSearch on YouTube

For an mm-bit hash, a brute-force preimage attack needs about 2m2^m trials. A collision attack needs only about 2m/22^{m/2} trials, because of the birthday paradox.

Hash size mmPreimage work ≈2m\approx 2^{m}Collision work ≈2m/2\approx 2^{m/2}
642642^{64}2322^{32}
12821282^{128}2642^{64}
16021602^{160}2802^{80}
25622562^{256}21282^{128}

For the exam: an mm-bit hash gives only m/2m/2 bits of collision resistance. For a 64-bit hash, the birthday attack needs about 2322^{32} variations of each message, not 2642^{64}.

Birthday attacksSearch on YouTube

Birthday paradoxSearch on YouTube

A 64-bit hash can look secure. Because of the birthday paradox, it is not. With about 2m/22^{m/2} random messages, the probability of a match is more than 0.50.5.

Birthday paradox core, for NN hash values from a set of 2m2^m:

P(at least one match) ≈ 1−e−N2/2m+1 ≈ 0.5 when N≈2m/2P(\text{at least one match}) \ \approx \ 1 - e^{-N^2/2^{m+1}} \ \approx \ 0.5 \text{ when } N \approx 2^{m/2}

Attack stepsSearch on YouTube

  1. A user is prepared to sign a valid message xx.
  2. The opponent generates 2m/22^{m/2} variations x′x' of xx, all with essentially the same meaning, and saves them.
  3. The opponent generates 2m/22^{m/2} variations y′y' of a desired fraudulent message yy.
  4. The opponent compares the two sets to find a pair with the same hash. The probability is more than 0.5 because of the birthday paradox.
  5. The user signs the valid message. The opponent then puts the forgery in its place, and the forgery has a valid signature.

The conclusion of the slides: use a larger MAC or hash.

Comparison tableSearch on YouTube

Find ii and jj such that H(yj′)=H(xi′)H(y'_j) = H(x'_i):

yyy1′y'_1y2′y'_2...yj′y'_j...yN′y'_N
xx≠≠≠≠≠
x1′x'_1≠≠≠≠≠
x2′x'_2≠≠≠≠≠
...
xi′x'_i≠≠≠=≠
...
xN′x'_N≠≠≠≠≠

Each cell compares H(xi′)H(x'_i) with H(yj′)H(y'_j). One equal cell is sufficient. The forged yj′y'_j gets the signature of the signed xi′x'_i. A full table takes O(N2)O(N^2) time. Sorted lists are faster and take O(Nlog⁡N)O(N \log N) time.

A 64-bit hash needs about 232≈4.3×1092^{32} \approx 4.3 \times 10^9 variations in each set, which is cheap today. A 128-bit hash needs 2642^{64}, a 160-bit hash needs 2802^{80} and a 256-bit hash needs 21282^{128}. Each extra hash bit doubles the brute-force preimage work and multiplies the collision work by 2\sqrt{2}.

Quick self-testSearch on YouTube

Give the two cryptographic properties a hash function must have.

One-way property: it is computationally infeasible to find data that maps to a specific hash, H(y)=hH(y) = h. Collision-free property: it is computationally infeasible to find two messages x,yx, y with H(x)=H(y)H(x) = H(y).

Name the three main uses of hash functions and what each sends.

MIC: send the hash (digest) of the message. The MIC is always encrypted, the message optionally. MAC: send a keyed hash of the message, with the message optionally encrypted. Digital signature: encrypt the hash with the private signing key and verify with the public key. This gives non-repudiation.

Describe the four symmetric-key message authentication diagrams.

Unkeyed hash: (a) send E(K,[M∥H(M)])\mathrm{E}(K,[M \| H(M)]); (b) send clear MM with E(K,H(M))\mathrm{E}(K, H(M)). Keyed hash: (c) send MM with H(M∥S)H(M \| S); (d) send E(K,[M∥H(M∥S)])\mathrm{E}(K,[M \| H(M \| S)]). In each case, the destination computes the hash again and compares.

Show the PKCS digital signature flow with its formulas.

(a) The source sends MM with E(PRa,H(M))\mathrm{E}(PR_a, H(M)). The destination hashes MM, decrypts the signature with PUaPU_a and compares. (b) For confidentiality, the source sends E(K,[M∥E(PRa,H(M))])\mathrm{E}(K, [M \| \mathrm{E}(PR_a, H(M))]). The destination decrypts with KK, then verifies with PUaPU_a.

List the PRF, PRNG, password and intrusion-detection uses.

PRF: generate session keys and nonces, produce a key from a password, derive keys from a master key cooperatively. PRNG: generate pseudorandom numbers. Password files: store the hash with a salt, as Unix and Windows NT do. Intrusion and virus detection: keep and check the hash of each file, as Tripwire does.

Give the two simple insecure hash functions and why they fail.

Block XOR: Ci=bi1xorbi2…xorbimC_i = b_{i1} \mathrm{xor} b_{i2} \dots \mathrm{xor} b_{im}, a longitudinal redundancy check. Rotate and XOR: rotate the hash left by 1 bit for each block, then XOR the block. Both are good for data integrity but useless for security. They are easy to invert, so they have no preimage or collision resistance.

Recite the seven hash function requirements.

Variable input size. Fixed output size. Efficiency. Preimage resistant (one-way). Second preimage resistant (weak collision resistant). Collision resistant (strong collision resistant). Pseudorandomness.

Differentiate preimage, second preimage and collision attacks.

Preimage: find yy such that H(y)H(y) equals a given hash value, about 2m2^m trials. Second preimage: for a given xx, find y≠xy \ne x with H(y)=H(x)H(y) = H(x), also about 2m2^m trials. Collision: find any pair x≠yx \ne y with equal hashes, about 2m/22^{m/2} trials because of the birthday paradox.

Walk the birthday attack against a signer, step by step.

The user is prepared to sign the valid message xx. The opponent generates 2m/22^{m/2} variations x′x' with the same meaning. The opponent generates 2m/22^{m/2} variations y′y' of a fraudulent message yy. A comparison finds H(xi′)=H(yj′)H(x'_i) = H(y'_j) with probability more than 0.5. The user signs xi′x'_i, and the opponent puts yj′y'_j in its place.

Which search times does the birthday slide give, and why is a 64-bit hash unsafe?

A full table comparison takes O(N2)O(N^2) time. Sorted lists take O(Nlog⁡N)O(N \log N) time. With m=64m = 64, each set needs about 2322^{32} variations, which is affordable. The slide concludes: use a larger MAC or hash.

  • A hash condenses any message to a fixed size, h=H(M)h = H(M). It is public and detects changes. A cryptographic hash is one-way and collision-free.
  • Uses: MIC (digest, always encrypted), MAC (keyed hash) and digital signature (hash encrypted with the private key). Other uses: PRF, PRNG, password files with salt and Tripwire file checks.
  • Symmetric-key diagrams use an unkeyed hash or a keyed hash H(M∥S)H(M \| S), with or without message encryption. PKCS signs the hash, E(PRa,H(M))\mathrm{E}(PR_a, H(M)).
  • The toy hashes (block XOR, rotate and XOR) give integrity only, not security.
  • Requirements: variable input, fixed output, efficiency, preimage, second preimage and collision resistance, pseudorandomness.
  • Brute force costs 2m2^m for a preimage and 2m/22^{m/2} for a collision. The birthday attack makes 2m/22^{m/2} variations of each message and finds a match. The answer is a larger hash.