-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivities.py
More file actions
376 lines (300 loc) · 13.8 KB
/
activities.py
File metadata and controls
376 lines (300 loc) · 13.8 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# ----------------------------------------------------
# Electromagnetic Mining Array (EMMA)
# Copyright 2018, Pieter Robyns
# ----------------------------------------------------
import time
import emutils
import ops
import numpy as np
import visualizations
import saliency
from celery import group, chord
from celery.result import AsyncResult, GroupResult
from celery.utils.log import get_task_logger
from registry import activity
from emutils import EMMAException, conf_has_op
logger = get_task_logger(__name__) # Logger
def submit_task(task, *args, remote=True, message="Working", **kwargs):
if remote:
async_result = task.si(*args, **kwargs).delay()
em_result = wait_until_completion(async_result, message=message + " (remote)")
else:
logger.info(message)
em_result = task(*args, **kwargs)
return em_result
def wait_until_completion(async_result, message="Task"):
"""
Wait for a Celery async_result to complete and measure the time taken.
:param async_result:
:param message:
:return:
"""
count = 0
while not async_result.ready():
print("\r%s: elapsed: %ds" % (message, count), end='')
count += 1
time.sleep(1)
print("")
if isinstance(async_result, AsyncResult):
return async_result.result
elif isinstance(async_result, GroupResult):
return async_result.results
else:
raise TypeError
def parallel_work(trace_set_paths, conf, merge_results=False):
"""
Divide the trace set paths into `conf.max_subtasks` partitions that are distributed to available workers. The
actions are performed in parallel on these partitions. Optionally, after processing is completed by all workers,
the results can be merged.
:param trace_set_paths:
:param conf:
:param merge_results:
:return:
"""
num_partitions = min(conf.max_subtasks, len(trace_set_paths))
result = []
for part in emutils.partition(trace_set_paths, num_partitions):
result.append(ops.work.si(part, conf))
if merge_results: # Merge subresults from all workers into one final result
return chord(result, body=ops.merge.s(conf))()
else:
return group(result)()
@activity('spattack')
@activity('attack')
def __perform_cpa_attack(emma):
"""
Attack that predicts the best subkey guess using the maximum correlation with the key value.
:param emma:
:return:
"""
def update_correlations(max_correlations, em_result, subkey_index):
corr_result = em_result.correlations
print("Num correlation entries: %d" % corr_result._n[0][0])
# Get maximum correlations over all points
for subkey_guess in range(0, 256):
max_correlations[subkey_index, subkey_guess] = np.max(np.abs(corr_result[subkey_guess, :]))
print("{:02x}".format(np.argmax(max_correlations[subkey_index])))
def print_results(max_correlations):
emutils.pretty_print_subkey_scores(max_correlations, limit_rows=20)
most_likely_bytes = np.argmax(max_correlations, axis=1)
print(emutils.numpy_to_hex(most_likely_bytes))
__attack_subkeys(emma, update_correlations, print_results)
# TODO: Duplicate code, fix me
@activity('pattack')
def __perform_prob_cpa_attack(emma):
"""
Attack that predicts the best subkey guess using the maximum probability of the key value.
:param emma:
:return:
"""
def update_probabilities(max_probs, em_result, subkey_index):
prob_result = em_result.probabilities
# Get maximum correlations over all points
for subkey_guess in range(0, 256):
max_probs[subkey_index, subkey_guess] = np.max(prob_result[subkey_guess, :])
print("{:02x}".format(np.argmax(max_probs[subkey_index])))
def print_results(max_probs):
emutils.pretty_print_subkey_scores(max_probs, limit_rows=20)
most_likely_bytes = np.argmax(max_probs, axis=1)
print(emutils.numpy_to_hex(most_likely_bytes))
__attack_subkeys(emma, update_probabilities, print_results)
def __attack_subkeys(emma, subkey_score_cb, final_score_cb):
score = np.zeros([emma.conf.key_high, 256])
# Determine dataset to attack
if emma.dataset_val is None:
emma.dataset_val = emma.dataset
logger.info("Attacking traces: %s" % str(emma.dataset_val.trace_set_paths))
# Attack each subkey separately
for subkey in range(emma.conf.key_low, emma.conf.key_high):
emma.conf.subkey = subkey # Set in conf, so the workers know which subkey to attack
# Execute task
async_result = parallel_work(emma.dataset_val.trace_set_paths, emma.conf, merge_results=True)
em_result = wait_until_completion(async_result, message="Attacking subkey %d" % emma.conf.subkey)
# Parse results
if em_result is not None:
subkey_score_cb(score, em_result, subkey)
# Print results to stdout
final_score_cb(score)
@activity('dattack')
def __perform_dis_attack(emma):
"""
Attack that predicts the best subkey guess using the minimum absolute distance to the key value.
:param emma:
:return:
"""
# Define callbacks
def update_distances(min_distances, em_result, subkey_index):
# Get score for this subkey determined by the workers
dis_result = em_result.distances
print("Num distance entries: %d" % dis_result._n[0][0])
# Get minimum distances over all points in the trace or encoding
for subkey_guess in range(0, 256):
min_distances[subkey_index, subkey_guess] = np.min(dis_result[subkey_guess, :])
# Print best subkey guess for this subkey index
print("{:02x}".format(np.argmin(min_distances[subkey_index])))
def print_results(min_distances):
emutils.pretty_print_subkey_scores(min_distances, limit_rows=20, descending=False)
most_likely_bytes = np.argmin(min_distances, axis=1)
print(emutils.numpy_to_hex(most_likely_bytes))
__attack_subkeys(emma, update_distances, print_results)
@activity('corrtrain')
@activity('ascadtrain')
@activity('shacputrain')
@activity('shacctrain')
@activity('autoenctrain')
def __perform_ml_attack(emma):
"""
Trains a machine learning algorithm on the training samples from a dataset.
"""
if emma.dataset is None:
raise EMMAException("No dataset provided")
if emma.dataset_val is None: # No validation dataset provided, so split training data in two parts
if emma.dataset.format == "ascad": # ASCAD uses different formatting
validation_split = [x for x in emma.dataset.trace_set_paths if x.endswith('-val')]
training_split = [x for x in emma.dataset.trace_set_paths if x.endswith('-train')]
else:
validation_split = emma.dataset.trace_set_paths[0:emma.conf.num_valsets]
training_split = emma.dataset.trace_set_paths[emma.conf.num_valsets:]
else:
validation_split = emma.dataset_val.trace_set_paths[0:emma.conf.num_valsets]
training_split = emma.dataset.trace_set_paths
logger.info("Training set: %s" % str(training_split))
logger.info("Validation set: %s" % str(validation_split))
submit_task(ops.aitrain,
training_split, validation_split, emma.conf,
remote=emma.conf.remote,
message="Training neural network")
@activity('plot')
def __perform_plot(emma, *params):
trace_sets_to_get = max(int(emma.conf.plot_num_traces / emma.dataset.traces_per_set), 1)
em_result = submit_task(ops.work, # Op
emma.dataset.trace_set_paths[0:trace_sets_to_get], emma.conf, keep_trace_sets=True, keep_scores=False, # Op parameters
remote=emma.conf.remote,
message="Performing actions")
visualizations.plot_trace_sets(
em_result.reference_signal,
em_result.trace_sets,
params=params,
no_reference_plot=emma.conf.plot_no_reference,
num_traces=emma.conf.plot_num_traces,
title=emma.conf.plot_title,
xlabel=emma.conf.plot_xlabel,
ylabel=emma.conf.plot_ylabel,
colorbar_label=emma.conf.plot_colorbar_label,
time_domain=(not (conf_has_op(emma.conf, 'spec') or conf_has_op(emma.conf, 'fft'))) or emma.conf.plot_force_timedomain,
sample_rate=1.0)
@activity('specgram')
def __perform_specgram(emma, *params):
em_result = submit_task(ops.work,
emma.dataset.trace_set_paths[0:1], emma.conf, keep_trace_sets=True, keep_scores=False, # Op parameters
remote=emma.conf.remote,
message="Performing actions")
for trace_set in em_result.trace_sets:
visualizations.plot_spectogram(trace_set,
emma.conf.specgram_samprate,
params=params,
num_traces=emma.conf.plot_num_traces)
@activity('basetest')
def __perform_base_test(emma):
async_result = ops.basetest.si(emma.dataset.trace_set_paths, emma.conf).delay()
wait_until_completion(async_result, message="Performing base test")
@activity('default')
def __perform_actions(emma, message="Performing actions"):
"""
Default activity: split trace_set_paths in partitions and let each node execute the actions on its assigned partition.
:param emma:
:param message:
:return:
"""
if emma.conf.remote:
async_result = parallel_work(emma.dataset.trace_set_paths, emma.conf)
return wait_until_completion(async_result, message=message)
else:
ops.work(emma.dataset.trace_set_paths, emma.conf)
@activity('keyplot')
def __perform_keyplot(emma, message="Grouping keys..."):
if emma.conf.remote:
async_result = parallel_work(emma.dataset.trace_set_paths, emma.conf)
em_result = wait_until_completion(async_result, message=message)
else:
em_result = ops.work(emma.dataset.trace_set_paths, emma.conf)
em_result = ops.merge(em_result, emma.conf)
visualizations.plot_keyplot(em_result.means,
time_domain=(not (conf_has_op(emma.conf, 'spec') or conf_has_op(emma.conf, 'fft'))) or emma.conf.plot_force_timedomain,
sample_rate=1.0,
show=True)
@activity('classify')
def __perform_classification_attack(emma):
for subkey in range(emma.conf.key_low, emma.conf.key_high):
emma.conf.subkey = subkey # Set in conf, so the workers know which subkey to attack
async_result = parallel_work(emma.dataset_val.trace_set_paths, emma.conf, merge_results=False)
celery_results = wait_until_completion(async_result, message="Classifying")
if 'hw' in emma.conf.leakage_model: # Leakage model uses Hamming Weight instead of byte values
predict_count = np.zeros(9, dtype=int)
label_count = np.zeros(9, dtype=int)
logprobs = np.zeros(9, dtype=float)
else:
predict_count = np.zeros(256, dtype=int)
label_count = np.zeros(256, dtype=int)
logprobs = np.zeros(256, dtype=float)
accuracy = 0
num_samples = 0
# Get results from all workers and store in prediction dictionary
for celery_result in celery_results:
em_result = celery_result.get()
assert(len(em_result.labels) == len(em_result.predictions))
for i in range(0, len(em_result.labels)):
label = em_result.labels[i]
prediction = em_result.predictions[i]
logprob = em_result.logprobs[i]
if label == prediction:
accuracy += 1
predict_count[prediction] += 1
label_count[label] += 1
num_samples += 1
logprobs += np.array(logprob)
accuracy /= float(num_samples)
print("Labels")
print(label_count)
print("Predictions")
print(predict_count)
print("Best argmax prediction: %02x" % np.argmax(predict_count))
print("Argmax accuracy: %.4f" % accuracy)
if np.sum(label_count) == np.max(label_count):
print("Best logprob prediction: %02x" % np.argmax(logprobs))
print("True key : %02x" % np.argmax(label_count))
else:
print("WARNING: logprob prediction not available because there is more than 1 true key label.")
@activity('salvis')
def __visualize_model(emma, model_type, vis_type='2doverlay', *args, **kwargs):
vis_type = vis_type.lower()
if emma.dataset_val is not None:
trace_sets = emma.dataset_val.trace_set_paths
else:
trace_sets = emma.dataset.trace_set_paths
salvis_result = submit_task(ops.salvis,
trace_sets,
model_type,
vis_type,
emma.conf,
remote=emma.conf.remote,
message="Getting trace set gradients %s" % str(trace_sets))
logger.info("Getting saliency of %d traces" % salvis_result.examples_batch.shape[0])
if vis_type == '1d':
saliency.plot_saliency_1d(emma.conf, salvis_result)
elif vis_type == '2d':
saliency.plot_saliency_2d(emma.conf, salvis_result)
elif vis_type == '2doverlay':
saliency.plot_saliency_2d_overlay(emma.conf, salvis_result)
elif vis_type == 'kerasvis':
saliency.plot_saliency_kerasvis(emma.conf, salvis_result)
elif vis_type == '2doverlayold':
saliency.plot_saliency_2d_overlayold(emma.conf, salvis_result)
else:
logger.error("Unknown visualization type: %s" % vis_type)
@activity('optimize_capture')
def __optimize_capture(emma):
submit_task(ops.optimize_capture,
emma.dataset.trace_set_paths, emma.conf,
remote=emma.conf.remote,
message="Fitting PCA")