CO6GC: Hash Functions

This is a post in COSIC’s Guide to Crypto! In this series, researchers from COSIC will be informally writing about different concepts in cryptography. Our aim is to introduce the reader to these concepts with explanations, illustrations and examples; so let’s get started!

HASH FUNCTIONS VS CRYPTOGRAPHIC HASH FUNCTIONS:

Before going any further in this blog, let us first clarify the terminology and distinguish between Hash Functions, and the topic of this blog Cryptographic Hash Functions.
A hash function is any function that takes as input a string of arbitrary length, and maps it to a string of fixed length. This general notion is more related to computer science, we can mention for instance hash tables, which consist of a data structure that implements dictionaries, which is efficient when aiming at speeding up data retrieval. Besides, given the usecases where in general hash functions are employed, they are required to have a fast evaluation algorithm. On the other hand, in an informal definition, Cryptographic Hash Functions as the name implies are hash functions as defined above, but expected to satisfy some “crypto-related” properties which we will see next. To name a few, we can mention the main cryptographic hash functions which are the families of SHA  derived functions.

 

PROPERTIES OF CRYPTOGRAPHIC HASH FUNCTIONS:

As previously stated, we expect a cryptographic hash function to guarantee some properties, we list the main ones below:

Let \(H\) be a hash function, with domain \(D\) and co-domain \(C\).

(1) Pre-image resistance:

\(H\) is said to be pre-image resistant, if for any \(y\) from \(C\), it is hard to find \(x\) in \(D\), such that \(H(x) = y\)

This property is also called one-wayness, basically we want \(H\) to be easy to compute from one way (domain to co-domain) but hard to invert, i.e., to compute from the inverse way (co-domain to domain).

(2) Second pre-image resistance:

\(H\) is second pre-image resistant, if given \(x\), it is hard to find \(x \neq x’\), such that \(H(x) = H(x’)\)

Basically, unlike the first property, thanks to which an attacker given \(y\) cannot find \(x\) such that \(H(x) = y\), here we are giving the attacker not only \(y\), but also one of its pre-images \(x\), and we are asking him to find another pre-image \(x’\). Note that the domain of \(H\) is bigger than the co-domain, therefore \(H\) is surjective and \(D\) contains points that map to the same image \(y\), that is, what we call collisions. Besides, in contrast to any intuition that comes to mind regarding a relationship between these two properties, in fact, there is no implication between (1) and (2).

To see why, we can take similar examples as in [1]:
Take a hash function \(H_1\) that satisfies (1), and define \(G\) as \(H_1 \circ F(x)\), where \(F\) takes \(x\) of n bits as input \(x= x_1||x_2||\dots||x_n\) and outputs, \(x_1||\dots||x_{n-2}||x_{n-1} \oplus x_n\).
It is clear that \(G\) satisfies (1) as otherwise, \(H_1\) will not satisfy (1), however, if \(y = G(x)\), then also \(x’ = x_1||\dots||x_{n-1}\oplus 1||x_n \oplus 1\) maps to the same point \(y\) as \(x\), and therefore \(G\) is not second pre-image resistant.

On the other hand, take a hash function \(H_2(x)\) that satisfies (2), and define \(K(x)\) as \(0 || x\) if \(x\) is less than \(n\) bits, and \(1 || H_2(x)\) otherwise. It is straightforward to show that \(K\) is second pre-image resistant, that is, if an attacker is given \(x\) and \(y = K(x) = b||u\): If \(b = 1\), the attacker needs to find another pre-image from the the sub domain that generates images of the form \(1||H_2(x)\),  which means that the attacker needs to break property (2) of \(H_2\). If \(b\) is \(0\), there is in fact no other point \(x’\) that yields the same \(y\) as \(x\).
However, \(K\) is not pre-image resistant, as one can trivially construct the pre-image of \(y\) if it is of the form \(0||u\), namely \(u\).

Both properties (1) and (2) are often seen as a minimal requirement expected from a cryptographic hash function, and in fact, in most usecases, we need a third property, which we describe next.

(3) Collision resistance:

\(H\) is collision resistant, if it is hard to find \(x\) and \(x’\) (\(x’ \neq x\)), such that \(H(x) = H(x’)\).

As one can see, this property is quite similar to (2), in the sense that we are also searching here for collisions, but the difference is that we are not fixing here a specific point for the attacker, but instead, we are challenging him to find any collision, and hence we have that (3) => (2). Otherwise, we refer the reader to [2], for formal definitions, properties, and relationships between them.

HOW DO WE ACTUALLY PROVE THAT THESE PROPERTIES ARE SATISFIED?

This task is actually a bit intricate, and in fact, this applies to cryptographic primitives in general. If you noticed, we deemed a hash function to enjoy one of the properties above if it is hard for an attacker to prove otherwise, and by hard we mean computationally infeasible, i.e., there exists no efficient algorithm to break the property in question.

But still, how can we know that there exists no such algorithm?

Well, roughly speaking, what happens is that when someone designs a new hash function, this hash function becomes subject to cryptanalysis, by performing attacks on it so as to see how it behaves against them. Those attacks target potential flaws in the underlying constructions of the hash function, in order to determine how much computation it is required to break a claimed property, and then if the best attack we found requires a huge amount of computation, then we can qualify the hash function in question to satisfy the claimed property. One of the tools used while cryptanalysing a hash function, is what we call the birthday paradox. Specifically, this paradox is resorted while assessing the property of collision resistance of a hash function.

-The birthday paradox:

This paradox can be illustrated as follows: In a group of 23 people, what would be the probability of finding 2 individuals having the same birthday?
One would be tempted to say for sure this probability is small, because a year is 365 days and the group of people is as few as 23 individuals. However, counter-intuitively, it turns out that this probability is more than a half.
To relate this to hash functions, the takeaway from the analogy of days of the year being the co-domain of \(H\), and people being the domain of \(H\), is that before claiming that \(H\) is collision resistant, this should be preceded by an overwhelming analysis, in order to defy any usage of this paradox that can disprove it.

SOME APPLICATIONS OF CRYPTOGRAPHIC HASH FUNCTIONS:

Cryptographic hash functions have many applications in information security. While we cannot list all of them here, we will go briefly over some of them.

-Data integrity:

Cryptographic hash functions can be used to check if some data transited was modified. For example, one way to check if a downloaded file was not altered, is by hashing it and comparing the resulting hash with the hash provided by the server hosing that file.

-Password storage:

Hash functions are a crucial tool for password protection. When you subscribe to some website and choose a username and a password, the website does not actually store your password in clear, but in fact it stores the hash of it instead (actually it is more than this but we are omitting details). So when you attempt to log in, your username is being looked up, and the password you type is hashed and then compared against the hash of your password. Note that by storing passwords as above, even if the a breach occurs and the file containing credentials is leaked, the attacker would not compromise users’ accounts, as he only gains access to usernames and the hashes of their passwords.

-Cryptographic protocols:

More generally, hash functions are used as a building block for other cryptographic protocols, such as commitment schemes, encryption and signature schemes, random generator functions etc. You can check out our previous posts to see some applications.

REFERENCES:

[1]: https://www.forth.gr/onassis/lectures/2010-06-28/presentations/The_cryptographic_hash_function_crisis_and_the_SHA_3_competition.pdf
[2]: https://web.cs.ucdavis.edu/~rogaway/papers/relates.pdf