This project implements a generic second preimage attack for long messages on a toy hash function called hs48, which is based on the Speck48/96 block cipher using a Davies-Meyer construction.
The attack exploits the Merkle-Damgård structure of the hash function to find a second preimage for a given long message. It consists of two main parts:
- Finding an expandable message
- Finding a collision with an intermediate chaining value of the target message
- C compiler (GCC recommended)
- xoshiro256** PRNG (header file provided)
Compile the code with optimizations enabled:
make
Run the compiled program:
make run
The program will attempt to find a second preimage for a predefined message of 2^18 blocks, whose hash is 0x7CA651E182DBULL.
- Uses a meet-in-the-middle approach
- Generates N random first-block messages and their chaining values
- Computes fixed-points for random second-block messages until a collision is found
- Returns two one-block messages m1 and m2 that form the expandable message
- Generates the original message (2^18 blocks)
- Finds an expandable message using find_exp_mess
- Searches for a collision block that connects the expandable message to an intermediate chaining value of the original message
- Constructs the second preimage by:
- Expanding the expandable message to the appropriate length
- Adding the collision block
- Copying the remaining blocks from the original message
- Computes and verifies the hash of the second preimage
- Finding an expandable message should take less than one minute on average
- The full attack should complete in less than ten minutes on average with optimizations enabled
The program provides verbose output detailing:
- The expandable message found
- The collision point and block
- The final hash of the second preimage
- This implementation is for educational purposes and demonstrates the attack on a toy hash function
- The hs48 hash function is intentionally weak (48-bit output) to make the attack feasible
- Real-world hash functions use various techniques to prevent such attacks
- SIMON and SPECK Families of Lightweight Block Ciphers
- Kelsey and Schneier, "Second Preimages on n-Bit Hash Functions for Much Less than 2^n Work", EUROCRYPT 2005