Hash Functions
Slide deck 13: hash definition, uses (MIC, MAC, signatures, passwords), requirements table, insecure toy hashes and the birthday attack.
Hashing Algorithms and Security - ComputerphileComputerphile · hash functions, collisions and security, 8 min
Cryptographic Hash Functions: Part 1myAcademic-Scholartica · hash uses, the birthday problem and SHA, 15 min
Birthday Paradox & Birthday Attack on Hash Functions | IS - 5.3Ayan Memon · birthday paradox and the birthday attack on hashes, 12 minHash function basicsSearch on YouTube
DefinitionSearch on YouTube
A hash function condenses an arbitrary message to a fixed size:
- The input is a message or data block of variable length bits. The output is a hash value of fixed length.
- We usually assume that the hash function is public. No secret is part of .
- 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, .
- Collision-free property: it is computationally infeasible to find two data with the same hash, .
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
| Use | What is sent | Encryption |
|---|---|---|
| Message Integrity Check (MIC) | Hash of the message (digest) | MIC always encrypted, message optionally |
| Message Authentication Code (MAC) | Keyed hash of the message | MAC, message optionally encrypted |
| Digital signature (non-repudiation) | Hash encrypted with the private (signing) key | Verified 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 , concatenates it with and encrypts both with key . The channel carries . B decrypts with , computes the hash of again and compares.
- (b) Message unencrypted: A encrypts only the hash. The channel carries with . B decrypts the hash with and compares it with a new hash of .
Diagram (b) gives authentication only. The message goes in clear, so anyone can read it. The encryption stops changes to the digest. It does not keep secret.
Symmetric key, keyed hashSearch on YouTube
In the next two diagrams, the hash also takes a shared secret value :
- (c) Message unencrypted: A computes and sends it with . B computes with the same and compares.
- (d) Message encrypted: A encrypts and with . The channel carries . B decrypts with , computes the hash with and compares.
The secret changes the plain hash into a MAC. The attacker does not know , 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 and encrypts the digest with the private key: . The message and the signature go together. B hashes , decrypts the signature with and compares.
- (b) This adds confidentiality. A encrypts with a symmetric key . The channel carries . B decrypts with , then verifies the signature with .
Sign the hash, not the message:
The hash keeps the signature short and fast for any length of .
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:
Bit of the hash is the XOR of bit of all 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:
- Take each successive -bit block.
- Rotate the current hash value to the left by 1 bit.
- 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
| Requirement | Description |
|---|---|
| Variable input size | can be applied to a block of data of any size |
| Fixed output size | produces a fixed-length output |
| Efficiency | is relatively easy to compute for any given , making both hardware and software implementations practical |
| Preimage resistant (one-way property) | For any given hash value , it is computationally infeasible to find such that |
| Second preimage resistant (weak collision resistant) | For any given block , it is computationally infeasible to find with |
| Collision resistant (strong collision resistant) | It is computationally infeasible to find any pair such that |
| Pseudorandomness | Output of 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 . 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 such that equals a given hash value.
- Collision attack (against collision resistance): find two messages and with the same hash, .
Brute-force effortSearch on YouTube
For an -bit hash, a brute-force preimage attack needs about trials. A collision attack needs only about trials, because of the birthday paradox.
| Hash size | Preimage work | Collision work |
|---|---|---|
| 64 | ||
| 128 | ||
| 160 | ||
| 256 |
For the exam: an -bit hash gives only bits of collision resistance. For a 64-bit hash, the birthday attack needs about variations of each message, not .
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 random messages, the probability of a match is more than .
Birthday paradox core, for hash values from a set of :
Attack stepsSearch on YouTube
- A user is prepared to sign a valid message .
- The opponent generates variations of , all with essentially the same meaning, and saves them.
- The opponent generates variations of a desired fraudulent message .
- 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.
- 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 and such that :
| ... | ... | ||||||
|---|---|---|---|---|---|---|---|
| ≠ | ≠ | ≠ | ≠ | ≠ | |||
| ≠ | ≠ | ≠ | ≠ | ≠ | |||
| ≠ | ≠ | ≠ | ≠ | ≠ | |||
| ... | |||||||
| ≠ | ≠ | ≠ | = | ≠ | |||
| ... | |||||||
| ≠ | ≠ | ≠ | ≠ | ≠ |
Each cell compares with . One equal cell is sufficient. The forged gets the signature of the signed . A full table takes time. Sorted lists are faster and take time.
A 64-bit hash needs about variations in each set, which is cheap today. A 128-bit hash needs , a 160-bit hash needs and a 256-bit hash needs . Each extra hash bit doubles the brute-force preimage work and multiplies the collision work by .
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, . Collision-free property: it is computationally infeasible to find two messages with .
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 ; (b) send clear with . Keyed hash: (c) send with ; (d) send . In each case, the destination computes the hash again and compares.
Show the PKCS digital signature flow with its formulas.
(a) The source sends with . The destination hashes , decrypts the signature with and compares. (b) For confidentiality, the source sends . The destination decrypts with , then verifies with .
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: , 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 such that equals a given hash value, about trials. Second preimage: for a given , find with , also about trials. Collision: find any pair with equal hashes, about 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 . The opponent generates variations with the same meaning. The opponent generates variations of a fraudulent message . A comparison finds with probability more than 0.5. The user signs , and the opponent puts in its place.
Which search times does the birthday slide give, and why is a 64-bit hash unsafe?
A full table comparison takes time. Sorted lists take time. With , each set needs about variations, which is affordable. The slide concludes: use a larger MAC or hash.
- A hash condenses any message to a fixed size, . 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 , with or without message encryption. PKCS signs the hash, .
- 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 for a preimage and for a collision. The birthday attack makes variations of each message and finds a match. The answer is a larger hash.