January 18, 2018 Written by

The Tutorial B5 has shown how you can break AES on a relative “easy” way. We have learned much new theoretic information for that method. But the Tutorial was limited on the Attack himself as well as the background and explanations how the Attack works. But the evaluation of the traces and the meaningful gradation on the display was realized by the software itself.

But in our own Test on real Hardware we may not use that software so easily. Because of that the Tutorial B6 shows how to evaluate the traces manually. It explains all necessary steps for interpreting and plotting the deliverables with a Python-Script.

We have scripted the program flowchart from figure 16 with Python code by ourselves:


Here ist he final code with comments:

import numpy as np
import sys

from datetime import datetime
startTime = datetime.now()


# number of bin 1's from dec 0 to dec 256
HW = [bin(n).count("1") for n in range(0,256)]

print HW


# Bitwise xor and sbox lookup
def intermediate(pt, keyguess):
    return sbox[pt ^ keyguess]

# load traces and textin from script arguments
trace_path = sys.argv[1]
pt_path = sys.argv[2]

# load traces and textin to variables
traces = np.load(trace_path) # *.npy
pt = np.load(pt_path) # *.npy

# count traces
numtraces = np.shape(traces)[0]-1
numpoint = np.shape(traces)[1]

print "Traces: ",numtraces
print "Punkte: ",numpoint
print ""
print "Calculating..."

# bestguess array mit 16 nullen fuellen
bestguess = [0]*16 

# for each subkey (bnum) run this loop
for bnum in range(0, 16):

    print "Subkey: ", bnum

    # Initialize arrays
    cpaoutput = [0]*256
    maxcpa = [0]*256 

    # For every key we assume (00, 01, ... , fc, fd, fe, ff) go through this loop
    for kguess in range(0, 256):
        print "Subkey %2d, hyp = %02x: \n"%(bnum, kguess), # example: Subkey 15, hyp = ea:

        #Initialize arrays & variables to zero
        sumnum = np.zeros(numpoint) # covar of x and y
        sumden1 = np.zeros(numpoint) # var of x
        sumden2 = np.zeros(numpoint) # var of y
        hyp = np.zeros(numtraces)

        #Get the number of ones in SBOX output
        for tnum in range(0, numtraces):
            hyp[tnum] = HW[intermediate(pt[tnum][bnum], kguess)]

        #Mean of hypothesis
        meanh = np.mean(hyp, dtype=np.float64) 

        #Mean of all points in trace
        meant = np.mean(traces, axis=0, dtype=np.float64) 

        #For each trace, do the following
        for tnum in range(0, numtraces):
            hdiff = (hyp[tnum] - meanh) # Hamming Distanz
            tdiff = traces[tnum,:] - meant           

            sumnum = sumnum + (hdiff*tdiff) # Covar of x and y
            sumden1 = sumden1 + hdiff*hdiff # var of x
            sumden2 = sumden2 + tdiff*tdiff # var of y

        cpaoutput[kguess] = sumnum / np.sqrt( sumden1 * sumden2 ) # pearson correlation coefficient
        maxcpa[kguess] = max(abs(cpaoutput[kguess]))

        #print maxcpa[kguess] # z.B. 0.579157504278 

    #Find maximum value of key
    bestguess[bnum] = np.argmax(maxcpa) 

print ""
print "Best Key Guess: "
for b in bestguess: print "%02x "%b,
print ""
print "\nExecution time: ",datetime.now() - startTime

Last modified on Wednesday, 31 January 2018 20:45