-
Notifications
You must be signed in to change notification settings - Fork 16
/
attackdesroundxor.py
266 lines (210 loc) · 9.43 KB
/
attackdesroundxor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'''
This file is part of pysca toolbox, license is GPLv3, see https://www.gnu.org/licenses/gpl-3.0.en.html
Author: Ilya Kizhvatov
Version: 1.0, 2017-05-14
CPA and LRA attacks on DES round in XOR out
The code should be self-explanatory (especially if you look into lracpa.py module)
In the plots:
- red trace is for known correct candidate
- blue trace is for the winning candidate (e.g. the one with maximum peak)
- grey traces are for all other candidates
'''
import numpy as np
import matplotlib.pyplot as plt
import struct
import time
from desutils import * # my DES utilities
from lracpa import * # my LRA-CPA toolbox
from condaverdes import * # incremental conditional averaging
##################################################
### 0. Configurable parameters
## Traceset, number of traces, and S-box to attack
tracesetFilename = "traces/hwdes_card8_power.npz"
sampleRange = (0, 50) # range of smaples to attack
N = 10000 # number of traces to attack (less or equal to the amount of traces in the file)
offset = 0 # trace number to start from
evolutionStep = 500 # step for intermediate reports
SboxNum = 1 # S-box to attack, counting from 0
## Leakage model
## (these parameters correspond to function names in lracpa module)
averagingFunction = roundXOR_valueForAveraging # for CPA and LRA
intermediateFunction = roundXOR_targetVariable # for CPA and LRA
leakageFunction = leakageModelHW # for CPA
basisFunctionsModel = basisModelSingleBits # for LRA
## Known key for ranking
knownKey = 0x8A7400A03230DA28
encrypt = True
# get the known key
roundKeyNum = 1
if (encrypt == False):
roundKeyNum = 16
roundKey = computeRoundKeys(knownKey, roundKeyNum)[roundKeyNum-1]
knownKeyChunk = roundKeyChunk(roundKey, SboxNum)
##################################################
### 1. Log the parameters
print "---\nAttack parameters"
print "Averaging function :", averagingFunction.__name__
print "Intermediate function :", intermediateFunction.__name__
print "CPA leakage function :", leakageFunction.__name__
print "LRA basis functions :", basisFunctionsModel.__name__
print "Encryption :", encrypt
print "S-box number :", SboxNum
print "Known key : " + format(knownKey, "#018x")
print "Known round key : " + format(roundKey, '#014x'),
print '[',
for i in range(8):
print format(roundKeyChunk(roundKey, i), '#04x'),
print ']'
#################################################
### 2. Load samples and data
# Readout
print "---\nLoading " + tracesetFilename
t0 = time.clock()
npzfile = np.load(tracesetFilename)
data = npzfile['data'][0:N]
traces = npzfile['traces'][0:N,sampleRange[0]:sampleRange[1]]
t1 = time.clock()
timeLoad = t1 - t0
# convert data byte arrays to integers (more convenient for DES)
print "Converting data..."
datanew = []
for i in range(0, len(data)):
datanew.append(struct.unpack('!Q', data[i][0:8].tostring())[0])
data = datanew # old data will be garbage-collected
# Log traceset parameters
(numTraces, traceLength) = traces.shape
print "Number of traces loaded :", numTraces
print "Trace length :", traceLength
print "Loading time : %0.2f s" % timeLoad
#################################################
### 3. Attack with fixed amount of traces
'''
print "---\nAttack"
# perform conditional averaging
CondAver = ConditionalAveragerDes(1024, traceLength)
for i in range(N):
CondAver.addTrace(data[i], traces[i], averagingFunction, SboxNum)
(avdata, avtraces) = CondAver.getSnapshot()
# CPA
CorrTraces = cpaDES(avdata, avtraces, intermediateFunction, SboxNum, leakageFunction)
# LRA
R2, coefs = lraDES(avdata, avtraces, intermediateFunction, SboxNum, basisFunctionsModel)
### visualize results
fig = plt.figure()
# allocate grid
axCPA = plt.subplot2grid((3, 1), (0, 0))
axLRA = plt.subplot2grid((3, 1), (1, 0))
axLRAcoefs = plt.subplot2grid((3, 1), (2, 0))
# CPA
axCPA.plot(CorrTraces.T, color = 'grey')
axCPA.plot(CorrTraces[knownKeyChunk, :], 'r')
axCPA.set_xlim([0, traceLength])
# LRA
axLRA.plot(R2.T, color = 'grey')
axLRA.plot(R2[knownKeyChunk, :], 'r')
axLRA.set_xlim([0, traceLength])
# LRA coefs
coefsKnownKey = np.array(coefs[knownKeyChunk])
axLRAcoefs.pcolormesh(coefsKnownKey[:,:-1].T, cmap="jet")
axLRAcoefs.set_xlim([0, traceLength])
# labels
fig.suptitle("CPA and LRA on %d traces" % N)
axCPA.set_ylabel('Correlation')
axLRA.set_ylabel('R2')
axLRAcoefs.set_ylabel('Basis function (bit)')
axLRAcoefs.set_xlabel('Time sample')
plt.show()
'''
#################################################
### 4. Attack with evolving amount of traces
print "---\nAttack"
t0 = time.clock()
# initialize the incremental averager
CondAver = ConditionalAveragerDes(1024, traceLength)
# allocate arrays for storing key rank evolution
numSteps = int(np.ceil(N / np.double(evolutionStep)))
keyRankEvolutionCPA = np.zeros(numSteps)
keyRankEvolutionLRA = np.zeros(numSteps)
# the incremental loop
tracesToSkip = 20 # warm-up to avoid numerical problems for small evolution step
for i in range (0, tracesToSkip - 1):
CondAver.addTrace(data[i], traces[i], averagingFunction, SboxNum)
for i in range(tracesToSkip - 1, N):
CondAver.addTrace(data[i], traces[i], averagingFunction, SboxNum)
if (((i + 1) % evolutionStep == 0) or ((i + 1) == N)):
(avdata, avtraces) = CondAver.getSnapshot()
CorrTraces = cpaDES(avdata, avtraces, intermediateFunction, SboxNum, leakageFunction)
R2, coefs = lraDES(avdata, avtraces, intermediateFunction, SboxNum, basisFunctionsModel)
#R2 = normalizeR2Traces(R2)
print "---\nResults after %d traces" % (i + 1)
print "CPA"
CorrPeaks = np.max(np.abs(CorrTraces), axis=1) # global maximization, absolute value!
CpaWinningCandidate = np.argmax(CorrPeaks)
CpaWinningCandidatePeak = np.max(CorrPeaks)
CpaCorrectCandidateRank = np.count_nonzero(CorrPeaks >= CorrPeaks[knownKeyChunk])
CpaCorrectCandidatePeak = CorrPeaks[knownKeyChunk]
print "Winning candidate: 0x%02x, peak magnitude %f" % (CpaWinningCandidate, CpaWinningCandidatePeak)
print "Correct candidate: 0x%02x, peak magnitude %f, rank %d" % (knownKeyChunk, CpaCorrectCandidatePeak, CpaCorrectCandidateRank)
print "LRA"
R2Peaks = np.max(R2, axis=1) # global maximization
LraWinningCandidate = np.argmax(R2Peaks)
LraWinningCandidatePeak = np.max(R2Peaks)
LraCorrectCandidateRank = np.count_nonzero(R2Peaks >= R2Peaks[knownKeyChunk])
LraCorrectCandidatePeak = R2Peaks[knownKeyChunk]
print "Winning candidate: 0x%02x, peak magnitude %f" % (LraWinningCandidate, LraWinningCandidatePeak)
print "Correct candidate: 0x%02x, peak magnitude %f, rank %d" % (knownKeyChunk, LraCorrectCandidatePeak, LraCorrectCandidateRank)
stepCount = int(np.floor(i / np.double(evolutionStep)))
keyRankEvolutionCPA[stepCount] = CpaCorrectCandidateRank
keyRankEvolutionLRA[stepCount] = LraCorrectCandidateRank
t1 = time.clock()
timeAll = t1 - t0
print "---\nCumulative timing"
print "%0.2f s" % timeAll
# save the rank evolution for later processing
#np.savez("results/keyRankEvolutionSbox%02d" % SboxNum, kreCPA=keyRankEvolutionCPA, kreLRA=keyRankEvolutionLRA, step=evolutionStep)
#################################################
### 5. Visualize results
print "---\nPlotting..."
fig = plt.figure()
# allocate grid
axCPA = plt.subplot2grid((3, 2), (0, 0))
axLRA = plt.subplot2grid((3, 2), (1, 0))
axLRAcoefs = plt.subplot2grid((3, 2), (2, 0))
axRankEvolution = plt.subplot2grid((2, 2), (0, 1), rowspan = 3)
# compute trace nubmers for x axis (TODO: move into block 3)
traceNumbers = np.arange(evolutionStep, N + 1, evolutionStep)
# CPA
axCPA.plot(CorrTraces.T, color = 'grey')
if CpaWinningCandidate != knownKeyChunk:
axCPA.plot(CorrTraces[CpaWinningCandidate, :], 'blue')
axCPA.plot(CorrTraces[knownKeyChunk, :], 'r')
axRankEvolution.plot(traceNumbers, keyRankEvolutionCPA, color = 'green')
axCPA.set_xlim([0, traceLength])
# LRA
axLRA.plot(R2.T, color = 'grey')
if LraWinningCandidate != knownKeyChunk:
axLRA.plot(R2[LraWinningCandidate, :], 'blue')
axLRA.plot(R2[knownKeyChunk, :], 'r')
axRankEvolution.plot(traceNumbers, keyRankEvolutionLRA, color = 'magenta')
axLRA.set_xlim([0, traceLength])
# LRA coefs
coefsKnownKey = np.array(coefs[knownKeyChunk])
axLRAcoefs.pcolormesh(coefsKnownKey[:,:-1].T, cmap="jet")
axLRAcoefs.set_xlim([0, traceLength])
# labels
fig.suptitle("CPA and LRA on %d traces" % N)
axCPA.set_ylabel('Correlation')
axLRA.set_ylabel('R2')
axLRAcoefs.set_ylabel('Basis function (bit)')
axLRAcoefs.set_xlabel('Time sample')
axRankEvolution.set_ylabel('Correct key candidate rank')
axRankEvolution.set_xlabel('Number of traces')
axRankEvolution.set_title('Correct key rank evolution (global maximisation)')
# Limits and tick labels for key rand evolution plot
axRankEvolution.set_xlim([traceNumbers[int(np.ceil(tracesToSkip / np.double(evolutionStep))) - 1], N])
axRankEvolution.set_ylim([0, 64])
axRankEvolution.grid(b=True, which='both', color='0.65',linestyle='-')
#axRankEvolution.ticklabel_format(style='sci', axis='x', scilimits=(0,0), useOffset=True)
# Legend for rank evolution plot
axRankEvolution.legend(['CPA', 'LRA'], loc='upper right')
plt.show()