forked from WhiteboxFiltering/WhiteboxFiltering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transposeTraces.py
361 lines (318 loc) · 15.3 KB
/
transposeTraces.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
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
import argparse
import pathlib
import os
import io
import sys
from time import time
TRACE_HEADER = b"WboxTrac"
TRACE_FOOTER = b"TraceEnd"
def to_bytes(n, length=1, byteorder='big', signed=False):
if byteorder == 'little':
order = range(length)
elif byteorder == 'big':
order = reversed(range(length))
else:
raise ValueError("byteorder must be either 'little' or 'big'")
return bytes((n >> i*8) & 0xff for i in order)
def transposeTraces(pathToTraces):
try:
open(pathToTraces / "nodeVectors.bin", "rb")
print("The traces are already transposed and contained in the file \"nodeVectors.bin\".")
except IOError:
#OPENING TRACE FILE(S) FROM EITHER BU OR SEL OFFICIAL IMPLEMENTATIONS
SELorBU=0 #SEL traces = 1, BU traces = 2, not found = 0
try:
f = open(pathToTraces / "trace.txt", "rt").readlines()
SELorBU=1
except IOError:
pass
try:
numOfBytes = os.path.getsize(pathToTraces / "0000.bin")
if SELorBU==1:
print("/!\\ Traces of SEL masking scheme and BU masking scheme are located in the same folder /!\\")
print("Please separate \"traces.txt\" into a different one and relaunch the program.")
sys.exit()
else:
SELorBU=2
except IOError:
pass
if not SELorBU:
print("/!\\ The given path to the traces directory does not contain the file \"0000.bin\" or \"traces.txt\" /!\\")
print(" Either the path is not correct, or the traces are not in the correct format")
sys.exit()
#READING TRACE FILE(S), TRANSPOSING THEM AND WRITING THEM INTO nodeVectors.bin
try:
#Processing SEL traces
if SELorBU==1:
print()
print()
with open(pathToTraces / "nodeVectors.bin", "wb") as outputfile:
numOfNodes=len(f[0])-1
T=len(f)
#WRITING HEADER
outputfile.write(TRACE_HEADER)
outputfile.write(to_bytes(numOfNodes, 4, 'big'))
outputfile.write(to_bytes(T, 4, 'big'))
#WRITING NODE VECTORS
for node in range(numOfNodes):
nodeByte=0
for traceNumber in range(T):
try:
nodeByte^=int(f[traceNumber][node]) << (7-traceNumber%8)
if traceNumber%8==7:
outputfile.write(to_bytes(nodeByte, 1, 'big'))
nodeByte=0
except IndexError:
print('\033[1A', end='\x1b[2K')
print("/!\\ The trace %d does not have the size as the first one /!\\" % traceNumber)
print("Exiting the program.")
print("The file \"nodeVectors.txt\" has still been created for the first %d nodes (%.2f%%)" % (node,100*node/numOfNodes))
sys.exit()
if T % 8:
try:
outputfile.write(to_bytes(nodeByte, 1, 'big'))
except IndexError:
print('\033[1A', end='\x1b[2K')
print("/!\\ The trace %d does not have the size as the first one /!\\" % traceNumber)
print("Exiting the program.")
print("The file \"nodeVectors.txt\" has still been created for the first %d nodes (%.2f%%)" % (node,100*node/numOfNodes))
sys.exit()
if node % 128==0:
print('\033[1A', end='\x1b[2K')
print("%d/%d (%.2f%%)" % (node, numOfNodes, 100*node/numOfNodes))
outputfile.write(TRACE_FOOTER)
print('\033[1A', end='\x1b[2K')
print('\033[1A', end='\x1b[2K')
print("The file \"nodeVectors.bin\" containing all the node vectors has been created.")
else:
#Processing BU traces
numOfNodes = numOfBytes*8
TRACES=[]
T = 0
while True:
ftrace = pathToTraces / ("%04d.bin" % T)
try:
with open(ftrace, "rb") as file:
try:
TRACES.append(file.read(numOfBytes))
except IOError as err:
print("/!\\ The trace %04d.bin does not have the size as the trace 0000.bin /!\\" % len(TRACES))
print("Exiting the program.")
sys.exit()
if len(TRACES[-1]) != numOfBytes:
print("/!\\ The trace %04d.bin does not have the size as the trace 0000.bin /!\\" % len(TRACES))
print("Exiting the program.")
sys.exit()
T+=1
file.close()
except IOError as err:
break
#TRANSPOSITION OF TRACES AND WRITING THE NODE VECTORS
print("Transposing traces...")
with open(pathToTraces / "nodeVectors.bin", "wb") as outputfile:
#WRITING HEADER
outputfile.write(TRACE_HEADER)
outputfile.write(to_bytes(numOfNodes, 4, 'big'))
outputfile.write(to_bytes(T, 4, 'big'))
#WRITING NODE VECTORS
SUMtime=0
numberOfTimes=0
for node in range(numOfNodes):
t1=time()
nodeByte=0
for traceNumber in range(T):
try:
nodeByte ^= ((TRACES[traceNumber][node//8] >> (7-node%8)) & 1) << (7-traceNumber%8)
if traceNumber%8==7:
outputfile.write(to_bytes(nodeByte, 1, 'big'))
nodeByte=0
except IndexError:
print('\033[1A', end='\x1b[2K')
print("/!\\ The trace %04d.bin does not have the size as the the trace 0000.bin /!\\" % traceNumber)
print("Exiting the program.")
print("The file \"nodeVectors.txt\" has still been created for the first %d nodes (%.2f%%)" % (node,100*node/numOfNodes))
sys.exit()
if T % 8:
try:
outputfile.write(to_bytes(nodeByte, 1, 'big'))
except IndexError:
print('\033[1A', end='\x1b[2K')
print("/!\\ The trace %04d.bin does not have the size as the the trace 0000.bin /!\\" % traceNumber)
print("Exiting the program.")
print("The file \"nodeVectors.txt\" has still been created for the first %d nodes (%.2f%%)" % (node,100*node/numOfNodes))
sys.exit()
t2=time()
SUMtime+=t2-t1
if node % 128==0:
print("node %d/%d (%.2f%%)," % (node, numOfNodes, 100*node/numOfNodes), end="")
timeRemaining=SUMtime/(node+1)*(numOfNodes-node)
print(" Estimated remaining time: %dh%dm%.2fs " % (timeRemaining//3600, (timeRemaining//60)%60, timeRemaining%60), end = "\r")
outputfile.write(TRACE_FOOTER)
except Exception as err:
if os.path.exists(pathToTraces / "nodeVectors.bin"):
os.remove(pathToTraces / "nodeVectors.bin")
print(err)
sys.exit()
print()
print(" ", end="\r")
print('\033[1A', end='\x1b[2K')
print(" ", end="\r")
print('\033[1A', end='\x1b[2K')
print("The file \"nodeVectors.bin\" containing all the node vectors has been created.")
def getHeader(pathToTraces):
#OPENING THE FILE
try:
with open(pathToTraces / "nodeVectors.bin", "rb") as ftrace:
header=ftrace.read(16)
assert header[:8] == TRACE_HEADER
ftrace.close()
except IOError as err:
print("Impossible to open the file \"NodeVectors.bin\"")
print("Please execute first \"sage prepareTraces.sage ./yourPath/toTraces/\"")
print(err)
print("Exiting the program.")
sys.exit()
#READING THE HEADER
numOfNodes=int.from_bytes(header[8:12], byteorder='big')
T=int.from_bytes(header[12:16], byteorder='big')
return(numOfNodes, T)
#Function openning nodeVectors.bin and returning a list of selction vectors
def getNodeVectors(pathToTraces, requiredT, begining=0, ending=0, NonRedundantNodes=None, silent=0, mode='vect'):
from sage.all import Matrix, GF, vector
(numOfNodes, T)=getHeader(pathToTraces)
#Opens only from the begining index to the ending one
if ending<=0:
ending=numOfNodes-ending
else:
ending=min(ending, numOfNodes)
if begining>numOfNodes:
print("the parameter cannot be chose greater then the number of available nodes (here:%d)" % numOfNodes)
print("Exiting the program.")
sys.exit()
if begining>=ending:
print("We have b=%d and e=%d:" % (begining,ending))
print("The parameter \"b\" cannot be chosen greater than the parameter \"e\"")
print("Exiting the program.")
sys.exit()
if requiredT>T:
print("Only %d traces available, cannot get T=%d traces" % (T, requiredT))
print("Exiting the program.")
sys.exit()
Tbytes=(T+7)//8
requiredTbytes=(requiredT)//8
assert requiredT <= T
assert requiredTbytes <= Tbytes
#OPENING THE FILE
try:
with open(pathToTraces / "nodeVectors.bin", "rb") as ftrace:
assert ftrace.read(16).startswith(TRACE_HEADER)
fileBytes=ftrace.read()
assert fileBytes.endswith(TRACE_FOOTER)
fileBytes = fileBytes[:-8]
assert len(fileBytes) == numOfNodes * Tbytes
ftrace.close()
except IOError as err:
print("Impossible to open the file \"NodeVectors.bin\"")
print("Please execute first \"sage prepareTraces.sage ./yourPath/toTraces/\"")
print(err)
print("Exiting the program.")
sys.exit()
#If an NRN list is given, it opens only non redudant vectors
nodesToGoThrough=[]
if NonRedundantNodes is not None:
newBegining=0
while NonRedundantNodes[newBegining]<begining:
newBegining+=1
newBegining=max(newBegining-1,0)
newEnding=newBegining
while newEnding<len(NonRedundantNodes) and NonRedundantNodes[newEnding]<ending :
newEnding+=1
nodesToGoThrough=NonRedundantNodes[newBegining:newEnding]
else:
nodesToGoThrough=range(begining,ending)
newBegining=begining
newEnding=ending
#RECOVERING THE NODE VECTORS
if not silent:
print("Opening NodeVectors.bin")
SUMtime=0
t0 = time()
NODEVECTORS=[]
for nodePos in range(len(nodesToGoThrough)):
node=nodesToGoThrough[nodePos]
if not silent:
t1=time()
nodeVector=[]
for nodeVecByte in fileBytes[node*Tbytes:node*Tbytes+requiredTbytes]:
for i in range(8):
nodeVector.append((nodeVecByte >> (7-i)) & 1)
if requiredT%8:
for i in range(requiredT%8):
nodeVector.append((fileBytes[node*Tbytes+requiredTbytes] >> (7-i)) & 1)
if mode=='vect':
# NODEVECTORS.append(vector(GF(2),nodeVector))
# NODEVECTORS.append(Matrix(GF(2), 1, requiredT, nodeVector))
NODEVECTORS.append(nodeVector)
elif mode=='mat':
NODEVECTORS.append(Matrix(GF(2), 1, requiredT, nodeVector))
if not silent:
t2=time()
SUMtime+=t2-t1
if node%128==0:
print("[.] node %d/%d (%.2f%%)," % (nodePos, len(nodesToGoThrough), 100*nodePos/len(nodesToGoThrough)), end="")
timeRemaining=SUMtime/(nodePos+1)*(len(nodesToGoThrough)-nodePos)
print(" Estimated remaining time: %dh%02dm%.2fs " % (timeRemaining//3600, (timeRemaining//60)%60, timeRemaining%60), end = "\r")
if not silent:
print(" ", end="")
print('\033[1A', end='\x1b[2K')
TOTALtime = time() - t0
print(
f"\r[✓] Opened {len(nodesToGoThrough):d} nodes in"
f" {int(TOTALtime)//3600:d}h{(int(TOTALtime)//60)%60:02d}m{TOTALtime%60:.2f}s"
)
print("[.] Converting the node vectors into a matrix")
t1=time()
if mode=='fulmat':
output=(Matrix(GF(2),NODEVECTORS).transpose(), nodesToGoThrough, newBegining, newEnding)
if mode=='mat':
output=(NODEVECTORS, nodesToGoThrough, newBegining, newEnding)
else:
output=(Matrix(GF(2),NODEVECTORS), nodesToGoThrough, newBegining, newEnding)
if not silent:
TOTALtime = time() - t0
print('\033[1A', end='\x1b[2K')
print(
f"\r[✓] Converted the node vectors into a matrix in"
f" {int(TOTALtime)//3600:d}h{(int(TOTALtime)//60)%60:02d}m{TOTALtime%60:.2f}s"
)
return(output)
#@TODO
def writeNodeVectors(vecs, file):
with open(file, "wb") as f:
f.write(TRACE_HEADER)
f.write(to_bytes(len(vecs), 4, 'big'))
f.write(to_bytes(len(vecs[0]), 4, 'big'))
T = len(vecs[0])
for vec in vecs:
assert len(vec) == T
nodeByte=0
for i in range(T):
nodeByte ^= int(vec[i]) << (7-i%8)
if i%8==7:
f.write(to_bytes(nodeByte, 1, 'big'))
nodeByte = 0
if T % 8:
f.write(to_bytes(nodeByte, 1, 'big'))
nodeByte = 0
f.write(TRACE_FOOTER)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='description to do later',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'trace_dir', type=pathlib.Path,
help="path to directory with trace/plaintext/ciphertext files"
)
args = parser.parse_args()
transposeTraces(args.trace_dir)