-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16sbox_skinny.py
237 lines (200 loc) · 11.3 KB
/
16sbox_skinny.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
import numpy as np
import scipy.stats
import sys
import os
from numpy import random as rd
np.set_printoptions(formatter={'float': '{: 0.5f}'.format})
# value of noise for which we want to run experiments
SIGMA = np.sqrt(float(sys.argv[1]))
# number of trace to process for each value of noise
N_TRACES = int(sys.argv[2])
# number of experiment to perform for the success rate
N_EXPERIMENTS = int(sys.argv[3])
if len(sys.argv) == 5:
seed = int(sys.argv[4])
else:
seed = int.from_bytes(os.urandom(4), sys.byteorder)
HAMMING_WEIGTH_TABLE = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
3, 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76, 0x73, 0xf3, 0xf6, 0x71, 0x24, 0xf1, 0xb4, 0x21, 0x74, 0xb1, 0xf4,
0x2c, 0x78, 0xf8, 0xbc, 0x7c, 0x28, 0xb8, 0xfc, 0x2a, 0x2e, 0xba, 0xbe, 0x7a, 0x7e, 0xfa, 0xfe,
0x25, 0x70, 0xf0, 0xb5, 0x75, 0x20, 0xb0, 0xf5, 0x22, 0x27, 0xb2, 0xb7, 0x72, 0x77, 0xf2, 0xf7,
0x2d, 0x79, 0xf9, 0xbd, 0x7d, 0x29, 0xb9, 0xfd, 0x2b, 0x2f, 0xbb, 0xbf, 0x7b, 0x7f, 0xfb, 0xff]
# KEY BYTES MODELS
def leakage_k1(tweakeys_byte, plaintext):
c0 = 1
l_5 = SBOX_TABLE[plaintext[0]] ^ tweakeys_byte ^ c0
l_13 = l_5 ^ SBOX_TABLE[plaintext[10]]
l_1 = l_13 ^ SBOX_TABLE[plaintext[13]]
return HAMMING_WEIGTH_TABLE[SBOX_TABLE[l_1]]
def leakage_k9(tweakeys_byte, ciphertext):
l_56 = INV_SBOX_TABLE[ciphertext[6] ^
ciphertext[10] ^ ciphertext[14] ^ tweakeys_byte]
return HAMMING_WEIGTH_TABLE[l_56]
# SKINNY FUNCTIONS
def skinny_lfsr_3(byte, n_application):
for _ in range(n_application):
x6 = (byte >> 6) & 0x01
x0 = byte & 0x01
top = (x0 ^ x6) << 7
byte = (byte >> 1) ^ top
return byte
def compute_round_tweakeys(tk1, tk2, tk3):
permutation = np.array(
[9, 15, 8, 13, 10, 14, 12, 11, 0, 1, 2, 3, 4, 5, 6, 7])
round_tweakeys = [[], [], []]
for _ in range(56):
round_tweakeys[0].append(tk1)
tk1 = tk1[permutation]
round_tweakeys[1].append(tk2)
tk2 = tk2[permutation]
for i in range(8):
x5 = (tk2[i] >> 5) & 0x01
x7 = (tk2[i] >> 7) & 0x01
bottom = x7 ^ x5
tk2[i] = ((tk2[i] << 1) ^ bottom) % 256
round_tweakeys[2].append(tk3)
tk3 = tk3[permutation]
for i in range(8):
x6 = (tk3[i] >> 6) & 0x01
x0 = tk3[i] & 0x01
top = (x0 ^ x6) << 7
tk3[i] = (tk3[i] >> 1) ^ top
return round_tweakeys
def mix_column(state):
new_state = np.array([0 for _ in range(16)], dtype=int)
new_state[4:8] = state[0:4]
new_state[8:12] = np.bitwise_xor(state[4:8], state[8:12])
new_state[12:16] = np.bitwise_xor(state[0:4], state[8:12])
new_state[0:4] = np.bitwise_xor(new_state[12:16], state[12:16])
return new_state
def encrypt_skinny_with_leakages(round_tweakeys, plaintext):
c0 = [1, 3, 7, 15, 15, 14, 13, 11, 7, 15, 14, 12, 9, 3, 7, 14, 13, 10, 5, 11, 6, 12, 8, 0, 1, 2, 5, 11, 7, 14, 12, 8,
1, 3, 6, 13, 11, 6, 13, 10, 4, 9, 2, 4, 8, 1, 2, 4, 9, 3, 6, 12, 9, 2, 5, 10]
c1 = [0, 0, 0, 0, 1, 3, 3, 3, 3, 2, 1, 3, 3, 3, 2, 0, 1, 3, 3, 2, 1, 2, 1, 3, 2, 0, 0, 0, 1, 2, 1, 3, 3, 2, 0, 0, 1,
3, 2, 1, 3, 2, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 1, 3, 2, 0]
c2 = 0x2
shift = np.array([0, 1, 2, 3, 7, 4, 5, 6, 10, 11, 8, 9, 13, 14, 15, 12])
for round in range(56):
# leak on the SBOX input for last rounds
if round == 55:
round_56 = [HAMMING_WEIGTH_TABLE[x] for x in plaintext]
plaintext = np.array([SBOX_TABLE[p] for p in plaintext])
# leak on the SBOX output for first rounds
if round == 1:
round_2 = [HAMMING_WEIGTH_TABLE[x] for x in plaintext]
plaintext[0] = plaintext[0] ^ c0[round]
plaintext[4] = plaintext[4] ^ c1[round]
plaintext[8] = plaintext[8] ^ c2
plaintext[0:8] = np.bitwise_xor(
np.bitwise_xor(
np.bitwise_xor(plaintext[0:8], round_tweakeys[0][round][0:8]), round_tweakeys[1][round][0:8]),
round_tweakeys[2][round][0:8])
plaintext = plaintext[shift]
plaintext = mix_column(plaintext)
return (round_2, round_56, plaintext)
# CONSTANTS
pdfs = [scipy.stats.norm(
loc=i, scale=SIGMA) for i in range(0, 9)]
last_round_k = np.arange(start=0, stop=256, step=1, dtype=int)
last_round_k = [skinny_lfsr_3(k, 28) for k in last_round_k]
# MAIN LOOP
success_rate_k1 = np.zeros(N_TRACES, dtype=float)
success_rate_k9 = np.zeros(N_TRACES, dtype=float)
for experiment in range(N_EXPERIMENTS):
# the PRNG is reseeded with seed + experiment to allow for
# easy inspection of individual experiment
rd_state = rd.default_rng(seed+experiment)
secret = rd_state.integers(0, 256, 16)
# fixed tweaks, random, known by the attack
tk1 = rd_state.integers(0, 256, 16)
tk2 = rd_state.integers(0, 256, 16)
round_tweakeys = compute_round_tweakeys(tk1, tk2, secret)
# holder for the ranks
rank_k1 = []
rank_k9 = []
# holder for the scores
scores_k1 = np.array([(i, 0) for i in range(256)], dtype=[
('key', int), ('score', float)])
scores_k9 = np.array([(i, 0) for i in range(256)], dtype=[
('key', int), ('score', float)])
for t in range(N_TRACES):
plaintext = rd_state.integers(0, 256, 16)
(round_2, round_56, ciphertext) = encrypt_skinny_with_leakages(
round_tweakeys, plaintext)
# K1
trace_k1 = rd_state.normal(
loc=round_2[0], scale=SIGMA, size=1)
eval_k1 = [pdf.pdf(trace_k1[0]) for pdf in pdfs]
predicted_k1s = np.arange(start=0, stop=256, step=1, dtype=int)
predicted_k1s = [leakage_k1(
round_tweakeys[0][0][0] ^ round_tweakeys[1][0][0] ^ k, plaintext) for k in predicted_k1s]
k1s = np.array([eval_k1[k] for k in predicted_k1s], dtype=float)
k1s = k1s.flatten(order="C")
k1s = np.log(k1s)
scores_k1['score'] += k1s
tmp_scores_k1 = np.sort(scores_k1, order='score')[::-1]
rank_k1.append(next(x for x, y in enumerate(
tmp_scores_k1) if y['key'] == secret[0]))
# K9
trace_k9 = rd_state.normal(
loc=round_56[5], scale=SIGMA, size=1)
eval_k9 = [pdf.pdf(trace_k9[0]) for pdf in pdfs]
predicted_k9s = np.arange(
start=0, stop=256, step=1, dtype=int)
predicted_k9s = [leakage_k9(
round_tweakeys[0][55][5] ^ round_tweakeys[1][55][5] ^ last_round_k[k], ciphertext) for k in predicted_k9s]
k9s = np.array([eval_k9[k] for k in predicted_k9s], dtype=float)
k9s = k9s.flatten(order="C")
k9s = np.log(k9s)
scores_k9['score'] += k9s
tmp_scores_k9 = np.sort(scores_k9, order='score')[::-1]
rank_k9.append(next(x for x, y in enumerate(
tmp_scores_k9) if y['key'] == secret[8]))
success_rate_k1 += (np.array(rank_k1) == 0).astype(float)
success_rate_k9 += (np.array(rank_k9) == 0).astype(float)
success_rate_k1 = success_rate_k1/ N_EXPERIMENTS
success_rate_k1 = np.log2(success_rate_k1) * -1
success_rate_k9 = success_rate_k9/ N_EXPERIMENTS
success_rate_k9 = np.log2(success_rate_k9) * -1
success_rate = np.zeros(N_TRACES, dtype=float)
success_rate += success_rate_k1 *8
success_rate += success_rate_k9 *8
print(success_rate)