-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompound_tool.py
More file actions
489 lines (422 loc) · 17.5 KB
/
compound_tool.py
File metadata and controls
489 lines (422 loc) · 17.5 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import sys
import argparse
import os
import re
import time
start_time = time.time()
parser=argparse.ArgumentParser(
description='''Python script to analysis the design regarding with the number of techniques applied''',
epilog="""This tool performs a structural analysis to discover logic locking techniques applied, differentiate them and split the design in portional for each method.""")
parser.add_argument('<nameModule>', type=str, default=42, help='Top level design name')
args=parser.parse_args()
arg1 = sys.argv[1]
file_copy = open("../script/compound_tool.tcl", "r+")
original_file = file_copy.read().splitlines(True)
file_copy.truncate(0)
file_copy.close()
file_copy = open("../script/compound_tool.tcl", "w")
file_copy.write("set DESIGN " + arg1)
file_copy.write("\n")
file_copy.write("\n")
file_copy.writelines(original_file[2:])
file_copy.close()
print("Loading the design...")
os.system("genus -files ../script/compound_tool.tcl " + ">" + "compound_tool.log")
print("Running the compound structural logic locking tool!")
# Get the output list and outputs affected by the keys
var = []
keysPSLL = []
outputs = []
classification = time.time() - start_time
with open("removal_list.log", "a") as myfile:
myfile.write("Time of classification: " + str(classification) + " seconds \n")
file1 = open('removal_list.log', 'r')
for line in reversed(list(file1)):
if (line.startswith('gate:')):
gate = re.findall(r' .+?\b', line)
var = [element.replace(' ','') for element in gate]
elif (line.startswith('keysPSLL:')):
keys = re.findall(r' .+?\b', line)
keysPSLL = [element.replace(' ','') for element in keys]
elif (line.startswith('outputs:')):
list = re.findall(r' .+?\b', line)
outputs = [element.replace(' ','') for element in list]
elif (line.startswith('techniques:')):
list = re.findall(r' .+?\b', line)
techniques = [element.replace(' ','') for element in list]
elif (line.startswith('keys:')):
keysall = re.findall(r' .+?\b', line)
keys_all = [element.replace(' ','') for element in keysall]
print(var)
print(outputs)
print(keysPSLL)
incr = 0
incr1 = 0
incr2 = 0
print("Generating the original or stripped netlist using 0 as restore output....")
append_copy = open("../script/edit_netlist_original_0.tcl", "r+")
original_text = append_copy.read().splitlines(True)
append_copy.truncate(0)
append_copy.close()
append_copy = open("../script/edit_netlist_original_0.tcl", "w")
append_copy.write("set DESIGN " + arg1)
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set var {")
for element in var:
if (incr == len(var)-1):
append_copy.write(element)
incr = incr + 1
elif (incr < len(var)):
append_copy.write(element)
append_copy.write(" ")
incr = incr + 1
append_copy.write("}")
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set list {")
for element in outputs:
if (incr1 < len(outputs)):
append_copy.write(element)
incr1 = incr1 + 1
if (incr1 == len(outputs)-1):
append_copy.write("}")
incr1 = incr1 + 1
else:
append_copy.write(" ")
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set keys_PSLL {")
for element in keysPSLL:
if (incr2 == len(keysPSLL)-1):
append_copy.write(element)
incr2 = incr2 + 1
elif (incr < len(keysPSLL)):
append_copy.write(element)
append_copy.write(" ")
incr2 = incr2 + 1
append_copy.write("}")
append_copy.write("\n")
append_copy.write("\n")
append_copy.writelines(original_text[8:])
append_copy.close()
os.system("genus -files ../script/edit_netlist_original_0.tcl" + ">>" + "compound_tool.log")
incr = 0
incr1 = 0
incr2 = 0
print("Generating the original or stripped netlist using 1 as restore output....")
append_copy = open("../script/edit_netlist_original_1.tcl", "r+")
original_text = append_copy.read().splitlines(True)
append_copy.truncate(0)
append_copy.close()
append_copy = open("../script/edit_netlist_original_1.tcl", "w")
append_copy.write("set DESIGN " + arg1)
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set var {")
for element in var:
if (incr == len(var)-1):
append_copy.write(element)
incr = incr + 1
elif (incr < len(var)):
append_copy.write(element)
append_copy.write(" ")
incr = incr + 1
append_copy.write("}")
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set list {")
for element in outputs:
if (incr1 < len(outputs)):
append_copy.write(element)
incr1 = incr1 + 1
if (incr1 == len(outputs)-1):
append_copy.write("}")
incr1 = incr1 + 1
else:
append_copy.write(" ")
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set keys_PSLL {")
for element in keysPSLL:
if (incr2 == len(keysPSLL)-1):
append_copy.write(element)
incr2 = incr2 + 1
elif (incr < len(keysPSLL)):
append_copy.write(element)
append_copy.write(" ")
incr2 = incr2 + 1
append_copy.write("}")
append_copy.write("\n")
append_copy.write("\n")
append_copy.writelines(original_text[8:])
append_copy.close()
os.system("genus -files ../script/edit_netlist_original_1.tcl" + ">>" + "compound_tool.log")
print("Generating the restore netlist....")
incr = 0
incr1 = 0
incr2 = 0
append_copy = open("../script/edit_netlist_restore.tcl", "r+")
original_text = append_copy.read().splitlines(True)
append_copy.truncate(0)
append_copy.close()
append_copy = open("../script/edit_netlist_restore.tcl", "w")
append_copy.write("set DESIGN " + arg1)
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set var {")
for element in var:
if (incr == len(var)-1):
append_copy.write(element)
incr = incr + 1
elif (incr < len(var)):
append_copy.write(element)
append_copy.write(" ")
incr = incr + 1
append_copy.write("}")
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set list {")
for element in outputs:
if (incr1 < len(outputs)):
append_copy.write(element)
incr1 = incr1 + 1
if (incr1 == len(outputs)-1):
append_copy.write("}")
incr1 = incr1 + 1
else:
append_copy.write(" ")
append_copy.write("\n")
append_copy.write("\n")
append_copy.write("set keys_PSLL {")
for element in keysPSLL:
if (incr2 == len(keysPSLL)-1):
append_copy.write(element)
incr2 = incr2 + 1
elif (incr < len(keysPSLL)):
append_copy.write(element)
append_copy.write(" ")
incr2 = incr2 + 1
append_copy.write("}")
append_copy.write("\n")
append_copy.write("\n")
append_copy.writelines(original_text[8:])
append_copy.close()
os.system("genus -files ../script/edit_netlist_restore.tcl" + ">>" + "compound_tool.log")
print ("The tool generated the restore and original/stripped netlist...")
print ("Files are in the netlist folder...")
print ("Running the LEC in two versions...")
for item in range(0,2):
append_copy = open("../script/lec_" + str(item) + ".do", "r+")
original_text = append_copy.read().splitlines(True)
append_copy.truncate(0)
append_copy.close()
append_copy = open("../script/lec_" + str(item) + ".do", "w")
append_copy.write("set parallel option -threads 4\n")
append_copy.write("read library -both -liberty /folder/file.lib\n")
append_copy.write("read lef file /folder/file.lef\n")
append_copy.write("read design -golden /home/almeida/compound/tifs_final/verilog/rll/" + "_".join(arg1.split("_", 2)[:2]) + ".v\n")
append_copy.write("read design -revised ../netlist/" + arg1 + "_original_" + str(item) + ".v\n")
append_copy.write("set system mode lec -nomap\n")
append_copy.write("analyze retiming\n")
append_copy.write("map key points\n")
append_copy.write("analyze setup\n")
append_copy.write("analyze datapath -merge\n")
append_copy.write("add compare point -all\n")
append_copy.write("compare\n")
append_copy.write("report verification > ../report/check_equivalence_" + str(item) + ".rpt\n")
append_copy.write("report statistics >> ../report/check_equivalence_" + str(item) + ".rpt\n")
append_copy.write("exit -f")
append_copy.close()
command = "lec_64 -nogui -xl -dofile ../script/lec_" + str(item) + ".do"
os.system(command)
print ("Done LEC")
print("Extract keys from ORIGINAL + RLL and RESTORE designs...")
nonEq = 0
file_rpt0 = open('../report/check_equivalence_0.rpt', 'r')
for line in file_rpt0:
if (line.startswith(' Non-equivalent')):
nonEq = nonEq + 1
file_rpt1 = open('../report/check_equivalence_1.rpt', 'r')
for line in file_rpt1:
if (line.startswith(' Non-equivalent')):
nonEq = nonEq + 1
print("Translate the verilog into bench files...")
os.system("perl ../script/ver2bench.pl -v=../netlist/" + arg1 + "_original_0.v " + "-l=../script/mcnc_tsmc65.genlib")
os.system("perl ../script/ver2bench.pl -v=../netlist/" + arg1 + "_original_1.v " + "-l=../script/mcnc_tsmc65.genlib")
os.system("perl ../script/ver2bench.pl -v=../netlist/" + arg1 + "_restore.v " + "-l=../script/mcnc_tsmc65.genlib")
result = 0
key = []
print("Extract keys from RESTORE designs...")
if (nonEq <= 1):
os.system("perl ../script/extract_key.pl -f=../netlist/" + arg1 + "_restore.bench -v=0" + " -p=../script/paths.pl > " + arg1 + "_restore_0.key")
os.system("perl ../script/extract_key.pl -f=../netlist/" + arg1 + "_restore.bench -v=1" + " -p=../script/paths.pl > " + arg1 + "_restore_1.key")
if (os.stat(arg1 + "_restore_0.key").st_size != 0):
print("The output from restore logic is 0")
result = 1
file = open(arg1 + "_restore_0.key", "r+")
for line in file:
if(line.startswith('#key=')):
key_PSLL = re.findall(r'.+?\b', line)[3]
elif(os.stat(arg1 + "_restore_1.key").st_size != 0):
print("The output from restore logic is 1")
result = 2
file = open(arg1 + "_restore_1.key", "r+")
for line in file:
if(line.startswith('#key=')):
key_PSLL = re.findall(r'.+?\b', line)[3]
elif(os.stat(arg1 + "_restore_1.key").st_size == 0 and os.stat(arg1 + "_restore_1.key").st_size == 0):
result = 3
print("Key PSLL was not found!!!")
key_PSLL = 'x'*(len(keysPSLL))
else:
result = 4
print("Key PSLL was not found!!!")
key_PSLL = 'x'*(len(keysPSLL))
print(result)
if (result == 1):
os.system("perl ../script/tempus.pl -ef=../netlist/" + arg1 + "_original_0.bench " + "-of=/home/almeida/compound/tifs_final/bench/original/" + arg1.split('_')[0] + ".bench -qt=0 -v -p=../script/paths.pl > " + arg1 + "_original_0.key")
file = open(arg1 + "_original_0.key", "r+")
for line in file:
if(line.startswith("[INFO] key=")):
key_RLL = re.findall(r'.+?\b', line)[5]
elif (result == 2):
os.system("perl ../script/tempus.pl -ef=../netlist/" + arg1 + "_original_1.bench " + "-of=/home/almeida/compound/tifs_final/bench/original/" + arg1.split('_')[0] + ".bench -qt=0 -v -p=../script/paths.pl > " + arg1 + "_original_1.key")
file = open(arg1 + "_original_1.key", "r+")
for line in file:
if(line.startswith("[INFO] key=")):
key_RLL = re.findall(r'.+?\b', line)[5]
else:
os.system("perl ../script/qatt.pl -ef=../netlist/" + arg1 + "_original_0.bench " + "-of=/home/almeida/compound/tifs_final/bench/original/" + arg1.split('_')[0] + ".bench -qt=0 -v -iter -p=../script/paths.pl > " + arg1 + "_original_0.key")
os.system("perl ../script/qatt.pl -ef=../netlist/" + arg1 + "_original_1.bench " + "-of=/home/almeida/compound/tifs_final/bench/original/" + arg1.split('_')[0] + ".bench -qt=0 -v -iter -p=../script/paths.pl > " + arg1 + "_original_1.key")
if (os.stat(arg1 + "_original_0.key").st_size > 200):
file = open(arg1 + "_original_0.key", "r+")
for line in file:
if(line.startswith('[INFO] Proven key: ')):
key_RLL = re.findall(r'.+?\b', line)[7]
elif (os.stat(arg1 + "_original_1.key").st_size > 200):
file = open(arg1 + "_original_1.key", "r+")
for line in file:
if(line.startswith('[INFO] Proven key: ')):
key_RLL = re.findall(r'.+?\b', line)[7]
else:
key_RLL = 'x'*(len(keys_all) - len(keysPSLL))
key = key_RLL + key_PSLL
# Classify the technique based on the restore verilog file
if(nonEq == 1 and result < 3):
print("The PSLL technique is Single-Flip Logic Locking")
print("The final key is: " + key)
print(key)
print ("Running the LEC between original versus compound design with the keys discovered...")
append_copy = open("../script/lec.do", "r+")
original_text = append_copy.read().splitlines(True)
append_copy.truncate(0)
append_copy.close()
append_copy = open("../script/lec.do", "w")
append_copy.write("set parallel option -threads 4\n")
append_copy.write("read library -both -liberty /folder/file.lib\n")
append_copy.write("read lef file /folder/file.lef\n")
append_copy.write("read design -golden /home/almeida/compound/tifs_final/verilog/original/" + arg1.split('_')[0] + ".v\n")
append_copy.write("read design -revised ../netlist/" + arg1 + ".v\n")
for x in range(len(keys_all)):
append_copy.write("add pin constraint " + key[x] + " " + keys_all[x] + " -revised\n")
append_copy.write("set system mode lec -nomap\n")
append_copy.write("analyze retiming\n")
append_copy.write("map key points\n")
append_copy.write("analyze setup\n")
append_copy.write("analyze datapath -merge\n")
append_copy.write("add compare point -all\n")
append_copy.write("compare\n")
append_copy.write("report verification > ../report/check_equivalence_SAT.rpt\n")
append_copy.write("report statistics >> ../report/check_equivalence_SAT.rpt\n")
append_copy.write("exit -f")
append_copy.close()
command = "lec_64 -nogui -xl -dofile ../script/lec.do"
os.system(command)
print ("Done LEC Restore")
file_rpt3 = open('../report/check_equivalence_SAT.rpt', 'r')
for line in file_rpt1:
if (line.startswith(' Non-equivalent')):
print("This single point is not SAT family")
elif(line.startswith(' Equivalent')):
print("This single flip technique is SAT family")
elif(result == 3):
print("The PSLL technique is Single-Flip Logic Locking DTL")
print("The final key is: " + key)
elif(result == 4):
print("The PSLL technique is Double-Flipe Logic Locking")
print("The final key is: " + key)
file = open("../report/" + arg1 + ".key", "w+")
file.write(key)
file.close()
inputs_restore = []
file1 = open('removal_list.log', 'r')
for line in file1:
if (line.startswith('inputs:')):
inputrestore = re.findall(r' .+?\b', line)
inputs_restore = [element.replace(' ','') for element in inputrestore]
elem = []
finish = 0
z = ""
z_tmp = ""
print(inputs_restore)
print ("Running all the attack")
for x in inputs_restore:
elem.append(x)
incr = 0
with open("../netlist/" + arg1 + "_restore.v", 'r+') as file1:
for line1 in file1:
if(incr < 2):
if(len(re.findall(x+"\)",line1)) != 0):
if(len(re.findall(r"keyinput[0-9]+", line1)) != 0):
z = re.findall(r"keyinput[0-9]+", line1)[0]
elem.append(z)
incr = incr + 1
else:
t = re.findall(r"\([a-z]_[0-9]+\)", line1)[0]
with open("../netlist/" + arg1 + "_restore.v", 'r+') as file_tmp:
for line_tmp in file_tmp:
if(len(re.findall(t+"\)", line_tmp)) != 0 and len(re.findall(r"keyinput[0-9]+", line_tmp)) != 0 and incr < 2):
z = re.findall(r"keyinput[0-9]+", line_tmp)[0]
if(z_tmp != z):
z_tmp = z
elem.append(z)
incr = incr + 1
finish = 1
file_tmp.close()
file.close()
print("Elements:")
print(elem)
print("#####################")
execution = time.time() - start_time
with open("removal_list.log", "a") as myfile:
myfile.write("Time of execution: " + str(execution) + " seconds \n")
if(elem[1].startswith('key') and elem[2].startswith('key')):
print("Circuit is from ANTISAT family with a complementary logic in the restore!!!!!")
else:
print("Circuit is not ANTISAT family!!!!")
file = open("../netlist/" + arg1 + ".bench", 'r')
for line in file:
if (line.startswith('# key=')):
key_proven = line.strip('# key=')
i = 0
dc = 0
dw = 0
count = 0
dk = len(key) - count
for key_index in key:
if (key_index == 'x'):
count = coun + 1
i = i + 1
else:
if (key_index == key_proven[i]):
dc = dc + 1
i = i + 1
else:
dw = dw + 1
i = i + 1
with open("removal_list.log", "a") as myfile:
myfile.write("The number of deciphered keys: " + str(dk) + "\n")
myfile.write("The number of correct deciphered keys: " + str(dc) + "\n")
myfile.write("The number of wrong deciphered keys: " + str(dw) + "\n" )
myfile.write("#key_discovered=" + key_proven + "\n")