Hash Function Notes
Supplementary notes on hash function requirements, birthday attacks, SHA-1 and signature schemes: ElGamal and DSA.
Hashing Algorithms and Security - ComputerphileComputerphile · hash functions, collisions and security, 8 min
Birthday Paradox & Birthday Attack on Hash Functions | IS - 5.3Ayan Memon · birthday paradox and the birthday attack on hashes, 12 min
How Does SHA-1 Work - Intro to Cryptographic Hash Functions and SHA-1Fullstack Academy · SHA-1 padding, words and rounds, 12 min
Digital Signature Algorithm (DSA) explained with example - Daniel's Security AcademyDaniel's Security Academy · DSA key generation, signing and verification with an example, 25 minHash functionsSearch on YouTube
DefinitionSearch on YouTube
A function makes a hash value:
is a message of variable length. 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
must have these properties for message authentication:
| # | Property | Name |
|---|---|---|
| 1 | works on a data block of any size. | Practical |
| 2 | gives a fixed-length output. | Practical |
| 3 | is easy to calculate for any , in hardware and software. | Practical |
| 4 | For a given , it is infeasible to find with . | One-way |
| 5 | For a given , it is infeasible to find with . | Weak collision |
| 6 | It is infeasible to find any pair with . | 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 with the message. The sender does not send . If is not one-way, an attacker can find it:
- Intercept and .
- Invert the hash: .
- Remove the known to get .
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:
- Intercept a message and its encrypted hash code.
- Calculate the unencrypted hash code of the message.
- 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 . It gives the next .
- The last 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 and an encrypted hash code . To replace the message, an opponent must find with . On average, this needs about tries.
The birthday attack needs much less work. The source A signs a message. A appends an -bit hash code and encrypts the hash code with its private key.
Attack stepsSearch on YouTube
- The opponent makes variations of the valid message . All variations have the same meaning.
- The opponent makes variations of a fraudulent message .
- 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.
- The opponent gives the valid variation to A for signature.
- 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 -bit hash code, the birthday attack needs about operations. For a 64-bit hash code, this is only about , not .
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
| Algorithm | Origin | Digest size | Note |
|---|---|---|---|
| MD4 | Ron Rivest, MIT | 128 bits | SHA is based on MD4 |
| MD5 | Ron Rivest | 128 bits | Replaced MD4. Less popular now, because of cryptanalysis. |
| SHA (FIPS 180) | NIST, 1993 | 160 bits | The Secure Hash Standard |
| SHA-1 (FIPS 180-1) | NIST, 1995 | 160 bits | Revised version |
| SHA-256, SHA-384, SHA-512 (FIPS 180-2) | NIST, 2002 | 256, 384, 512 bits | Same 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 operations. Before, people thought that a collision needed operations.
SHA-1 stepsSearch on YouTube
SHA-1 takes a message of less than 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 . 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:
| Register | Value (hex) |
|---|---|
| A | 67452301 |
| B | EFCDAB89 |
| C | 98BADCFE |
| D | 10325476 |
| E | C3D2E1F0 |
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: , , or .
- Each round takes the current block and the buffer , and updates the buffer.
- Each step () uses an additive constant . There are only four different constants, one for each round.
- The output of round 4 is added to modulo . The result is .
Step 5: output. After all blocks, the output of the last stage is the 160-bit digest.
The standard values of and (from the SHA lecture) are:
| Round | Steps | (hex) | |
|---|---|---|---|
| 1 | 5A827999 | ||
| 2 | 6ED9EBA1 | ||
| 3 | 8F1BBCDC | ||
| 4 | CA62C1D6 |
SHA-1 summarySearch on YouTube
| Symbol | Meaning |
|---|---|
| Initial value of the ABCDE buffer, from step 3 | |
| Output of the last round for block | |
| Number of blocks, with the padding and length fields | |
| Addition modulo on each word pair | |
| Final 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 modulo .
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
- The signature is a bit pattern that depends on the signed message.
- It uses information unique to the sender, to prevent forgery and denial.
- It is easy to make.
- It is easy to recognize and verify.
- It is infeasible to forge. This means a new message for an existing signature, or a false signature for a given message.
- 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 and , a primitive root of .
- The private key signs. The public key verifies.
Each user A makes a key pair:
- Select a secret with .
- Calculate the public key .
Signing and verificationSearch on YouTube
Alice signs a message for Bob:
- Calculate the hash , with .
- Select a random with and .
- Calculate the temporary key .
- Calculate .
- Calculate .
- The signature is .
Any user B verifies the signature:
The signature is valid if .
Why it works: . Also . Thus the exponent is , and .
Worked exampleSearch on YouTube
Use : and .
Keys. Alice selects . Then .
Signing. The message hash is .
- Select . .
- .
- , because .
- .
The signature is .
Verification.
, so the signature is valid.
Reduce the line in two small steps. , and . Then .
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).
| Year | Event |
|---|---|
| 1991 | First proposal |
| 1993 | Revision after public comments on security |
| 1996 | Minor revision |
| 2000 | FIPS 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:
- : a 160-bit prime.
- : a prime of 512 to 1024 bits, with .
- , with and .
Each user selects a private key randomly or pseudorandomly. The standard uses . (The notes write "1 to ".) The public key is . It is easy to calculate from . It is infeasible to find from .
Signing and verificationSearch on YouTube
To sign, the user calculates and . They use , the private key , the hash and a random . Use a new for each signature.
The receiver calculates from the public parameters, the public key of the sender and the hash of the message:
The signature is valid if .
The final test is on . The value does not depend on the message. It depends only on and the three global parameters. The receiver can calculate again from the message, the signature and the public keys. Discrete logarithms are difficult. Thus an opponent cannot find from or from .
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 in ?
If an attacker can invert , it gets from . It knows , so it gets . A one-way prevents this.
Describe the birthday attack and its effort for a 64-bit hash.
Make valid and 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 , not .
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 . Append the 64-bit length. Initialize ABCDE. Process each 512-bit block in four rounds of 20 steps. Output the 160-bit .
What collision attack on SHA-1 do the notes give?
A research team found collisions with operations. Before, people thought that this needed 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 ElGamal example, give the signature and the check.
, , , . and . The signature is valid.
Give the DSA signing and verification equations.
Sign: and . Verify: , , , . Valid if .
- 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 operations: 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: , . Valid if .
- DSA signs only. Valid if .