☕
Pause for minutes until .
Remaining:
Pius Walter
T4INF1202: Kryptologie
Part 2: Hash functions
2026-04-07
Recap
  • Last week we spoke about simple/historic ciphers and how they can be attacked
  • We mentioned Shannon's One-Time-Pad
  • Today we will discuss one of the most fundamental primitives in cryptography, hash functions
Hash function
  • A hash function takes an input and, from that creates some deterministic output
  • It has many applications in computer science generally, but hash functions are particularly interesting for cryptographic purposes
  • Note that, generally speaking, hash functions do not need to have the properties you might typically associate with them
Hash function flavors
  • A hash function can produce constant or variable length input or output (e.g., compare forensic hash functions)
  • It can compress to varying degree or not at all (non-compressing hash function)
  • It can be possible or impossible to reverse (i.e., going from the output back to the input)
  • It needs not to operate on byte level (can also be on bit level – actually, all “popular” hash functions are defined at bit level)
Hash function schematic
Hash function flavors
  • For many purposes in computer science, hash functions are used
  • An example are ADTAbstract data types like a dictionary (aka hash map)
  • In those applications, we usually want the input to be chopped up in a way that the output values are uniformly distributed
Introduction
Cryptographic use
8
Hash function rigidity
  • For cryptographic purposes, uniform distribution is not the only property we are looking for
  • We also want the “chopping up” to occur in a manner that is not easily predictable unless the hash function is actually executed
  • I.e., there should be no simple correlation between changes in input to changes in output
Cryptographic hash function
  • Such primitives have different synonyms: cryptographic hash function, compression functions, message digests, one-way functions or trapdoor functions
  • They take variable-length input and produce fixed-length output
  • We will now examine their properties more formally
Avalanche property and uniformity
  • Avalanche property: Error propagation (minimal disturbance in input leads to maximal change in output)
  • Statistically, 50% of bits flip as a result to any change of the input
  • Uniform output: For random inputs, every output occurs with the same probability
  • Uncorrelated output: The dependence of the output form the input is quasi-random
Hash function definition
  • Let Q4mta4Ck5NLHHZf1qH71g5gJY be a cryptographic hash function
  • For this function Q4mta4Ck5NLHHZf1qH71g5gJY and a message Q4mta4Ck5NLHmZf1qH71g5gJY, let the output hash value be Q4mta4Ck5NLHh = H(m)Zf1qH71g5gJY
  • Computation of Q4mta4Ck5NLHhZf1qH71g5gJY given Q4mta4Ck5NLHmZf1qH71g5gJY is efficiently possible
  • There are typically three security guarantees associated with a cryptographically strong hash function, which we will cover next
Pre-image resistance
  • Pre-image resistance: Given Q4mta4Ck5NLHhZf1qH71g5gJY, an attacker cannot efficiently compute any Q4mta4Ck5NLHmZf1qH71g5gJY so that Q4mta4Ck5NLHH(m) = hZf1qH71g5gJY
  • In other words: no message can be crafted that matches a specific hash and the function is not “reversible”
  • Only attack is to exhaustively walk through all messages Q4mta4Ck5NLHmZf1qH71g5gJY and try hashing each one
Second pre-image resistance
  • Second pre-image resistance: Given Q4mta4Ck5NLHmZf1qH71g5gJY, an attacker cannot efficiently compute any Q4mta4Ck5NLHm' \neq mZf1qH71g5gJY so that Q4mta4Ck5NLHH(m') = H(m)Zf1qH71g5gJY
  • In other words: An attacker cannot modify a given message Q4mta4Ck5NLHmZf1qH71g5gJY so that it becomes a different message that still produces the same hash value
Collision resistance
  • Collision resistance: an attacker cannot efficiently create two arbitrary messages Q4mta4Ck5NLHm_1Zf1qH71g5gJY and Q4mta4Ck5NLHm_2Zf1qH71g5gJY so that Q4mta4Ck5NLHH(m_1) = H(m_2)Zf1qH71g5gJY with Q4mta4Ck5NLHm_1 \neq m_2Zf1qH71g5gJY
  • This is by far the easiest attacker model – nothing is fixed, the attacker needs to find just two different messages that need to produce the same hash value
Collision difficulty
  • Imagine you are filling an empty room with people, one at a time
  • How many people do you need to send in there to have more than a 50% probability that at least two of those people have birthday on the same day?
  • What do you think?
Birthday paradox
  • The number of people you need is unintuitively low.
  • For 365 days in a year, you only need 23 people to have a 50.7% chance of this event happening.
  • This is because we are not looking for someone having birth on a specific day (there, the answer would be 183 days), but we just are interested in collisions of any day
  • This phenomenon is called the birthday paradox
Birthday paradox
  • The chance increases rapidly depending on the number of people
  • At 41 people, this probability is already 90.3%
  • For 58, it is 99.2%
Birthday paradox
  • More generally, we have Q4mta4Ck5NLHnZf1qH71g5gJY categories and Q4mta4Ck5NLHkZf1qH71g5gJY candidates that occupy one category each
  • What is the probability Q4mta4Ck5NLHpZf1qH71g5gJY that two candidates fall into the same category?
  • For the example before, Q4mta4Ck5NLHn = 365Zf1qH71g5gJY and for Q4mta4Ck5NLHk = 23Zf1qH71g5gJY it follows Q4mta4Ck5NLHp \approx 50.7\%Zf1qH71g5gJY
Birthday paradox
QBEntEozXWiop = 1 - \frac{n!}{n^k \cdot (n - k)!}kIt7msRdzcHK
QBEntEozXWiop \approx 1 - e^{-\frac{k(k-1)}{2n}}kIt7msRdzcHK
Birthday paradox probability
Sock drawer
  • You can visually this easily by imagining a drawer full of 20 (unpaired) socks
  • Imagine there are five different colors of sock: red, green, yellow, blue, and purple, 4 of each
  • You are getting dressed. You need a pair of socks. A sock collision.
Sock finding strategy 1
  • First strategy: You decide that you want a red sock beforehand.
  • We then pull out socks one by one. If it is not the one we are looking for, we put it back.
  • Note that for large enough sock drawers (Q4mta4Ck5NLH2^{256}Zf1qH71g5gJY “socks”), it practically does not make a difference if we put it back or keep it
Sock finding strategy 1
  • The probability of finding one by randomly grabbing inside the drawer is Q4mta4Ck5NLH\frac{4}{20}Zf1qH71g5gJY
  • This means an 80% chance of grabbing a non-red sock. In other words, you need to try 20 times (!) to have a 99% chance of finding a red sock: Q4mta4Ck5NLHp = 1 - \left(\frac{20 - 4}{20} \right)^{20} \approx 99\%Zf1qH71g5gJY
Sock finding strategy 1
  • Note that using this method you have only now found the first red sock. You even need a second one.
  • The average case performs a little bit better, using the expected value of the negative binominal distribution, we find that Q4mta4Ck5NLHE = \frac{k}{p} = \frac{2}{0.2} = 10Zf1qH71g5gJY
  • However, you still need 10 attempts to find the desired pair on average
Sock finding strategy 2
  • Now imagine that you do not care what sock color you wear on that day
  • Instead, you randomly pull out socks one by one until you have a matching pair
  • With one sock pulled out, you have a collision with Q4mta4Ck5NLHp = 0Zf1qH71g5gJY
  • For the second one you either have a collision with Q4mta4Ck5NLHp = 0.2Zf1qH71g5gJY or you now have two different colors to match
Sock finding strategy 2
  • 3rd: Q4mta4Ck5NLHp = 0.4Zf1qH71g5gJY (or 3 possibilities on non-match)
  • 4th: Q4mta4Ck5NLHp = 0.6Zf1qH71g5gJY (or 4 possibilities on non-match)
  • 5th: Q4mta4Ck5NLHp = 0.8Zf1qH71g5gJY (or 5 possibilities on non-match)
  • 6th: Q4mta4Ck5NLHp = 1Zf1qH71g5gJY → guaranteed match!
  • I.e., you need almost half of the tries for this approach just because you do not care about the color (= hash value)
Consequence
  • Therefore, an efficient attack on cryptographic hash functions will always be looking for a collision rather than a preimage
  • Collision attacks are typically also the first to be published before a hash function is fully broken
  • There is an implication on the effective security level of a hash function further below
Anatomy of Hash Functions
29
Iterated construction
  • Internally, hash functions have a state which has a static size
  • With each iteration (and supplying of new data), this state is updated
  • Sometimes this update only happens once a buffered internal block is full
  • This allows processing of infinite input (length does not have to be known in advance either)
Data streaming
  • Data is streamed through the cryptographic hash function
  • Imagine a car wash through which you drive a long car (like a stretch limousine) – this does not require a larger car wash, just takes more time
  • Similarly, a hash function does not require more storage, just more time to process the data piecewise
Iterated construction
  • Generally, a hash function therefore has three different internal sizes
    • Size of the hash state
    • Block size (internal data buffer)
    • Output size
  • For example, MD5 has 128 bit state, 512 bit block buffer and 128 bit output
Merkle-Damgård
  • A typical design that is frequently used by hash functions is the Merkle–Damgård construction
  • Basic build block: collision-resistant one way compression function with static input length
  • Derived result: collision-resistant cryptographic hash function with dynamic input length
  • MD5, SHA-1, SHA-2 all use this
Sponge construction
  • A relatively new alternative to the Merkle–Damgård construction is the sponge construction
  • Using this, there are two phases, one where the sponge is fed data that runs through it (absorption phase)
  • Once enough absorption has happened, there is a phase in which data is then extracted from that internal state (squeezing phase)
  • SHA-3 uses this construction
Examples
  • MD5 (1992): 128 bit output, 128 bit state, Merkle-Damgård, broken 1996/2005
  • SHA-1 (1993): 160 bit output, 192 bit state, Merkle-Damgård, broken 2005
  • SHA-256 (2001): 256 bit output, 512 bit state, Merkle-Damgård
  • SHA-3-256 (2016): 256 bit output, 1600 bit state, Sponge
Considerations
  • Typically, the internal state size should be at least double that of the output size
  • This is the case up to SHA-256 in the SHA-2 family, but not for SHA-384 or SHA-512 anymore (512 bits throughout)
  • Still, there are no practical attacks known against SHA-256 or better in that family
Hash function security
  • When evaluating the security of a hash function theoretically, we can idealize the function
  • I.e., assume there are no algorithmic/cryptographic weaknesses to the function that can be used as a “shortcut” for the adversary
  • In this case, one might naïvely expect that a hash function with Q4mta4Ck5NLHbZf1qH71g5gJY bit output size needs on average Q4mta4Ck5NLH2^{b-1}Zf1qH71g5gJY attempts to find a preimage
Hash function security
  • However, because of the birthday paradox it is much easier to find collisions than it is to find preimages
  • This is the (more difficult) bar that we apply when judging the security level of a hash function
  • Therefore, a hash function with output of Q4mta4Ck5NLHbZf1qH71g5gJY bits provides only Q4mta4Ck5NLH\frac{b}{2}Zf1qH71g5gJY bits of security; the average number of attempts is assumed to be Q4mta4Ck5NLH2^{\frac{b}{2}}Zf1qH71g5gJY
Hash function security
  • Concretely, what this means is that if you want to achieve a security level of 128 bits, you need at least a hash function output of 256 bits
  • In a compound, the weakest point in cryptographic constructions is that which is going to be attacked first
  • Therefore, any compound construction is judged by its weakest link
Usage
40
Ubiquity
  • Cryptographic hash functions are ubiquitous throughout applied cryptography
  • They are immensely useful and there are few primitives that can do without
  • We will only highly two uses here (password storage and digital signatures, but there are dozens more): proof-of-work, cipher construction, ADTAbstract data types, etc.
Password storage
  • One area where hash functions are routinely used is for password storage
  • E.g., imagine a backend that needs to store the password to a given username
  • If you store these passwords in clear text, any leak/hack of said database immediately reveals all passwords of every user
Password storage
  • Therefore, one of the solutions to this is to hash the password when it is set initially and every time a user authenticates
  • Then, only the hash values are compared
  • We can assume with reasonable probability that the original passwords are identical if their hash values are identical
  • During authentication, the password needs to be present in clear text, however
Password storage drawbacks
  • Hash functions are one-way functions, i.e., from a hash value the password cannot be efficiently inferred
  • Why is such a naïve implementation still problematic?
  • What could be possible attacks?
Password storage drawbacks
  • If we cannot attack the hash function efficiently, we can attack it inefficiently
  • Precomputation of large dictionaries leads to generation of hash tables
  • Once this precomputation has been completed, lookup is exceptionally efficient
  • May not compromise all passwords, but certainly all bad ones in a hashed database
  • How can we improve this?
Password storage solutions
  • We can force same passwords to hash to different values by introduction of a salt value
  • It is also stored within the database and in a naïve construction simply concatenated to the password
  • Salt values are randomly generated when setting the password
  • Same passwords then have different hash values and precomputation isn't possible anymore (it needs to be salt-specific)
Password storage solutions
  • Different issue: Hashes are still reasonably efficient in their computation
  • This allows an attacker with a good setup (e.g., many GPUGraphics processing units in one system, working in parallel) to quickly brute-force a salt/hash combination
  • Therefore, we would like to deliberately slow down hashing
Password storage solutions
  • Idea: simply hash 1000, 10000, 100000 times in a row: Q4mta4Ck5NLHH(H( \ldots H(p)))Zf1qH71g5gJY
  • Iterated hashing should also not be done naïvely, more sophisticated solutions such as PBKDF2Password-Based Key Derivation Function 2, scrypt or Argon2 should be used – topic for a different lecture
Usage
Digital signatures
49
Signing data
  • Imagine we want to create a digital signature over some data, for example an email
  • The length of the data is variable
  • We want to somehow use this data as input to a mathematical function (“magic”) that performs the signing
Signing data
  • The mathematical operation has certain bounds in which it operates
  • For example, it depends on the size of the underlying field in finite field cryptography
  • Any modular arithmetic has this limitation, a finite (albeit large) number of elements
  • Our message needs to stay within these bounds – but arbitrary message sizes can become arbitrarily large
Signing data
  • Almost universally, when data is signed digitally, that data is therefore run through a cryptographic hash function as the first step
  • This ensures the values that are output generally stay within the confines of the mathematical operation that comes as step 2
Signing data
Attacking hash functions
  • One reason why the cryptographic strength of hash functions is important is that they directly relate to the strength of derived data
  • For example, in signature schemes, they directly relate to the strength of the generated signature
  • Look at the next slide and determine what kind of hash function attack is shown.
Attacking hash functions
  • Shown is a second pre-image attack
  • If the hash function is not resistant to this type of attack, an attacker can easily modify the original message into a forgery
  • This forgery Q4mta4Ck5NLHm'Zf1qH71g5gJY, because it has the same hash value (recall Q4mta4Ck5NLHH(m) = H(m')Zf1qH71g5gJY) therefore also has the same signature
  • The attacker takes the original signature and re-uses it, since it is also valid for the forgery
Attacking hash functions
  • Note that for legitimate signing of data, the correct private key is required
  • But if we are directly attacking the hash function, we do not know the key nor do we care
  • We might be able to create a forgery without knowing the key
Attacking hash functions
  • Remember how we talked about collision resistance which needs to be given as well
  • Imagine you can trick a party to sign one message Q4mta4Ck5NLHmZf1qH71g5gJY, but they will not sign any message
  • I.e., you can make them sign
    “Alice has worked 7 hours today” or
    “Alice has worked 3 hours today”
  • But you cannot make them sign “Alice has worked 99999 hours today”
Attacking hash functions
  • Imagine there is more diversity in the message (e.g., a comment field), so there are millions of possibly valid messages
  • I.e., the attacker has some control over the message that gets a legitimate signature
  • But not full control (which they would want to sign arbitrary messages)
  • Why would this collision attack be easier to pull off compared to the second pre-image attack?
Forging data by collision
Forging data by collision
Attacking hash functions
  • Note that the attacker can pre-compute a collision
  • Then have a seemingly legitimate message signed (that is within the confines of what the signer is willing to sign)
  • And instantly receive a signature that is valid for the malicious message as well
  • This is why collision attacks are the relevant measure of security for signature functions
Conclusion
61
Conclusions
  • Hash functions are one of the most important building blocks of modern cryptography
  • Their cryptographic strength and statistical distribution characteristic is paramount, as it translates directly to the security of derived algorithms
  • Play around with them and get a feeling for how they behave, they will appear pretty much anywhere
Thank you!
Any questions?