-
Notifications
You must be signed in to change notification settings - Fork 1
/
attack.py
159 lines (133 loc) · 4.7 KB
/
attack.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
from util import *
import numpy as np
import time
from functools import partial
import concurrent.futures
import subprocess
import tqdm
import csv
import os
def cpa(traces, power_models):
# Calculate the correlation matrix for each added trace.
# Based on Version 6 in Computational aspects of correlation power analysis
# but with even more precomputation to compute for all added traces.
hyp = np.empty((len(power_models), len(traces), 256))
for m, model in enumerate(power_models):
hyp[m] = np.fromfunction(np.vectorize(model), (len(traces), 256), dtype=int)
s1 = np.cumsum(traces, axis=0)[:, np.newaxis, :]
s2 = np.cumsum(np.square(traces), axis=0)[:, np.newaxis, :]
s3 = np.cumsum(hyp, axis=1)[:, :, :, np.newaxis]
s4 = np.cumsum(np.square(hyp), axis=1)[:, :, :, np.newaxis]
s5 = np.cumsum(traces[:, np.newaxis, :] * hyp[:, :, :, np.newaxis], axis=1)
n = np.arange(1, len(traces) + 1)[:, np.newaxis, np.newaxis]
# We ignore divide by zero warning, those calulations become NaN.
with np.errstate(divide="ignore", invalid="ignore"):
return np.abs(
(n * s5 - s1 * s3)
/ np.sqrt((n * s2 - np.square(s1)) * (n * s4 - np.square(s3)))
)
def scores(corr):
"""Compute the scores for each key from a matrix of all correlations"""
return np.max(corr, axis=len(corr.shape) - 1)
def keyrank(g, key):
"""Compute the rank of the correct key from a list of scores"""
return np.where((-g).argsort() == key)[0][0]
def experiment(numtraces, key, byte, model, experiment):
corr = scores(
cpa(
traces[numtraces * experiment : numtraces * (experiment + 1)],
[
model(
plaintexts[numtraces * experiment : numtraces * (experiment + 1)],
byte,
b,
)
for b in range(8)
],
)
)
summ = np.sum(corr, axis=0)
prodd = np.prod(corr, axis=0)
maxx = np.max(corr, axis=0)
results = {}
for i in range(numtraces):
for b in range(8):
results[f"Bit {b}, {i}"] = keyrank(corr[b, i], key[byte])
results[f"Sum, {i}"] = keyrank(summ[i], key[byte])
results[f"Prod, {i}"] = keyrank(prodd[i], key[byte])
results[f"Max, {i}"] = keyrank(maxx[i], key[byte])
return results
def run_experiments(
path,
experiments,
threads,
num_traces,
window_start,
window_end,
byte=0,
model=two_bit_model,
seed=None,
):
key, plaintexts_original, _, traces_full = get_traces(path)
assert traces_full.shape[0] >= num_traces * experiments
seed_sequence = np.random.SeedSequence(seed)
permutation = np.random.default_rng(seed_sequence).permutation(traces_full.shape[0])
global traces
traces = traces_full[permutation, window_start:window_end].copy()
del traces_full
global plaintexts
plaintexts = plaintexts_original[permutation].copy()
del plaintexts_original
columns = []
for i in range(num_traces):
for b in range(8):
columns.append(f"Bit {b}, {i}")
columns.append(f"Sum, {i}")
columns.append(f"Prod, {i}")
columns.append(f"Max, {i}")
with open(f"results/experiments-{int(time.time())}.csv", "w", newline="") as f:
commit = (
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
.decode("ascii")
.strip()
)
f.write(
f"Commit: {commit}, Trace file: {os.path.basename(path)}, Seed: {seed_sequence.entropy}, Experiments: {experiments}, Number of traces: {num_traces}, Window start: {window_start}, Window end: {window_end}, Byte: {str(byte)}, Power Model: {model.__name__}\n"
)
writer = csv.DictWriter(f, columns, delimiter=";")
writer.writeheader()
with concurrent.futures.ProcessPoolExecutor(threads) as executor:
for results in tqdm.tqdm(
executor.map(
partial(
experiment,
num_traces,
key,
byte,
model,
),
range(experiments),
),
total=experiments,
):
writer.writerow(results)
def main():
threads = 10
run_experiments(
"traces/2b7e151628aed2a6abf7158809cf4f3c-fix-500000-equal.csv",
1000,
threads,
500,
1100,
1500,
)
run_experiments(
"traces/2b7e151628aed2a6abf7158809cf4f3c-fix-500000-diff.csv",
1000,
threads,
500,
1100,
1500,
)
if __name__ == "__main__":
main()