- Introduction
- Preconditions
- Timing Attack on RSA
- Differential Power Analysis on AES
- Differential Fault Attack on AES
This repository contains some very simple, rather theoretical physical attacks, including a Timing Attack on RSA, Differential Power Analysis on AES and Differential Fault Attack also on AES. The attacks are implemented in Python and the code is well documented. The attacks are not optimized for speed, but are just my first attempts at implementing attacks like this. The attacks are not meant to be used in real life, but rather to illustrate the principles of the attacks, e.g. the timing attack on RSA only recovers 64b of the key (which is normally 1024b-4096b).
In the following sections I will briefly explain the theory behind the attacks and describe how they can be executed.
There are a couple of standard python libraries in use. Install them by running the command: pip install -r requirements.txt
.
This timing attack on RSA is based on the data dependent reduction in the (original) Montgomery Multiplication, which is used for modular multiplication in RSA:
def MontgomeryMul(self, a: int, b: int, n: int, n1: int):
"""
Montgomery Multiplication
Returns: f=a*b and er=1 if reduction is done, er=0 otherwise
"""
# Do some stuff
if f >= n:
er = 1
f = f - n
return f, er
Here, f is the result of the modular multiplication & as can be seen in the code snippet above, depending on the size of f, an extra reduction step is done. This reduction step is data dependent and takes a longer amount of time to compute. This can be exploited to recover the private key d
in RSA by measuring the timing of the RSA computation. As already mentioned above, in this example, we only recover 64b of a key to prove the concept.
The attack works as follows:
- Compute the RSA ciphertext
c
for a lot of different messages and public keye
andn
. Measure the timings for all of these computations. In our example this has already been done, you can find the CSV files for the ciphertexts & timings underrsa_dta/inputs
. - The first bit of the private key
d
will always be1
, we can discard any leading0
s. - Starting from the second bit of
d
, do the following:- Propose 2 hypotheses for the currently attacked bit of
d
, which are0
and1
, resulting ind0
andd1
. - For both hypotheses, compute the exponentiation
m^d0/1
using theLeft-to-Right-Square-And-Multiply
algorithm with the Montgomery Multiplication. This algorithm works by always squaring the message & multiplying it if the current bit is1
. For the last Montgomery Multiplication, store, whether an extra reduction step was done or not. This step is revered to as Look-Ahead. We can improve this by storing the result of each exponentiation and reusing it for the next bit. This is not necessary but greatly improves the speed of the attack. - After doing this for all ciphertexts, we have 2 lists for the extra reductions for both hypotheses.
- Finally we use the Pearson Correlation Coefficient to calculate the correlation of the extra reductions for both hypotheses with the timing data. The hypothesis with the highest correlation is the correct one. We set the bit of
d
to the correct hypothesis and continue with the next bit.
- Propose 2 hypotheses for the currently attacked bit of
- For the last bit we cannot do a Look-Ahead. Therefore we just need to manually test both possibilities to get the final bit of
d
.
You can run all of the attacks easily from one script.
- Make sure you install the required libraries by running
pip install -r requirements.txt
. - Run the attack by executing:
python3 attacks.py dta
.
Differential Power Analysis (DPA) is a side channel attack usually used on cryptographic algorithms. It works by measuring the power consumption of the algorithm and correlating this power consumption with intermediate values calculated during the computation. With this correlation secret information can be extracted. In principle, the attack works on any algorithm, without specific knowledge of the algorithm. However, the number of required traces can be greatly decreased by cleverly using knowledge about the attacked algorithm. In this example, we will attack an AES algorithm in the last round, to extract the last round key, which can be used to calculate the AES master key.
For each byte of the last round key do the following:
- Create a list of all 256 possible values for the byte.
- Create the V-Matrix by calculating the inverse Sub-Bytes transformation for all key hypotheses & each column of our ciphertext:
- From this V-Matrix we compute the H-Matrix which contain the power model values for all of our key hypotheses & inputs. There are a number of possibilities for power models, in this attack I used the Hamming-Weight model, which simply counts the numbers of
1
s in a data word. For example:hw(0x12) = hw(0b0001 0010) = 2
. The H-Matrix has the same dimensions as the V-Matrix:
- Finally, we use a correlation function to correlate our H-Matrix with the power traces in the trace-matrix T. This leads to the R-Matrix.
- The absolute maximum value now lies at the index for the correct key byte.
You can run all of the attacks easily from one script.
- Make sure you install the required libraries by running
pip install -r requirements.txt
. - Run the attack by executing:
python3 attacks.py dpa
.
Fault Attacks are fundamentally different from the 2 attacks above, both of which are Side-Channel Attacks. Side-Channel Attacks measure attributes of an attacked system, while Fault Attacks directly inject a fault. This can be done in various ways, e.g. by temporarily spiking the supply voltage of the device, or by using a focused Laser beam to change certain bytes.
In this implementation, we again use a simplified way of implementing a Fault attack. The faulty-ciphertext pairs are generated with a program & not actually on hardware. By doing this, we can exactly define where to inject the fault, which is quite complicated in real life.
You can run all of the attacks easily from one script.
- Make sure you install the required libraries by running
pip install -r requirements.txt
. - Run the attack by executing:
python3 attacks.py dfa
.