Calculate Intermediate value from the last Round of AES

I have tried to use this tutorial … ate_Attack in order to make my first template attack in the first round of an AES.
My first experiment doesn’t give me good results, I have a nice Point of interest, however I don’t find any correct bytes from my secret key. So I decide to modify the template attack in order to attack the last round:

This the code to calculate the intermediate value for the first round and its hamming weight:

[code]# Useful utilities
hw = [bin(x).count(“1”) for x in range(256)]

def cov(x, y):
# Find the covariance between two 1D lists (x and y).
# Note that var(x) = cov(x, x)
return np.cov(x, y)[0][1]

Start calculating template

1: load data

tempTraces = np.load(r’C:\chipwhisperer\software\temp_attack\rand_key_data\traces\2016.05.24-12.53.15_traces.npy’)
tempPText = np.load(r’C:\chipwhisperer\software\temp_attack\rand_key_data\traces\2016.05.24-12.53.15_textin.npy’)
tempKey = np.load(r’C:\chipwhisperer\software\temp_attack\rand_key_data\traces\2016.05.24-12.53.15_keylist.npy’)

2: Find HW(sbox) to go with each input

Note - we’re only working with the first byte here

tempSbox = [sbox[tempPText[i][0] ^ tempKey[i][0]] for i in range(len(tempPText))]
tempHW = [hw[s] for s in tempSbox]

But I don’t find how to modify my code in order to calculate the intermediate value of the last round. As I know that:


The intermediate value of the last round= SubBytes^(-1)[ShiftRows^(-1)[k10♁ciphertext]]

If I suppose that is correct, Mus I just change this equation in my code???

tempSbox = [sbox[Ciphertext[i][0] ^ key[i][0]] for i in range(len(Ciphertext))] 

I would be very grateful if you could help me please: How to calculate please the intermediate value of the last round in python?

Hi Marita,

You posted this question on the Assembla page as well, so I’ll just copy my responses here:

Dear @gdeon , Thank you very much for your explanation. In fact, I have 500000 traces. Traces that I put, is just an example.

Based on the key_List I have calculated the 10th Round key. So as I understand, the key_Guess is the 10th round key. Right?

Then according to your answer about calculating intermediate value from the last round, as I understand the shiftRows step is not important and we can ignore it, by that way we just calculate the intermediate value for the byte 0 by this way : states = [inv_sbox[Ciphertexts[i][0] ^ Guess_Keys[i][0]] for i in range(len(Ciphertexts))]
then I take the state table to calculate the Hamming weight for each state, and I complete the template script without any modification as it was described in the tutorial.

To summarize:


hw = [bin(x).count(“1”) for x in range(256)]

Start calculating template

1: load data

Traces = np.load(r’C:\chipwhisperer\software\temp_attack\rand_key_data\traces\2016.05.24-12.53.15_traces.npy’)
Ciphertexts = np.load(r’C:\chipwhisperer\software\temp_attack\rand_key_data\traces\2016.05.24-12.53.15_textout.npy’)
Guess_Keys = np.load(r’C:\chipwhisperer\software\temp_attack\rand_key_data\traces\2016.05.24-12.53.15_keylist.npy’)

2: Find HW(sbox) to go with each input

Note - we’re only working with the first byte here

states = [inv_sbox[Ciphertexts[i][0] ^ Guess_Keys[i][0]] for i in range(len(Ciphertexts))]
tempHW = [hw[s] for s in states]

I’m just worried about one thing - how did you calculate the 10th round keys? In your code example it looks like you’re just loading the keys from the CWCapture output file, which is the initial round key. If you’ve used the key schedule to calculate the 10th round keys then you’re good to go.

Dear @gelon
I use this tutorial to calculate my 10th key, … 1_0616.pdf

This tutorial gives me RoundKey.exe program which let me to calculate the 10th Round, I add a simple batch file which lets me to do the same traitment for many keys. It takes a lot of time, but it gives me a correct results. Do you propse another way to calculate them?
Thank you in advance.

Hmm. I’m surprised that it takes a lot of time!

There’s a key schedule calculator built into ChipWhisperer. You can use by importing

from chipwhisperer.analyzer.attacks.models.aes.key_schedule import keyScheduleRounds

and then running

key_round10 = keyScheduleRounds(key, 0, 10)

However, I’m not sure this is any faster - it’s not very optimized, so 500000 keys could take a long time! You could also try to find an implementation of AES in C and use that key schedule code, but it’s hard to say if you can easily find something fast enough for you.

Thank you very much for your help.