app.py 20.6 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
# -*- coding: utf-8 -*-

# Author: Brice Colombier
# Laboratoire Hubert Curien
# 42000 Saint-Etienne - France
# Contact: b.colombier@univ-st-etienne.fr
# Project: Demonstrator
# File: app.py
# Date: 2016-10-13

import random
import sys
import time

import tkFileDialog
from Tkinter import *
from ttk import *

from add_frame_elements import add_frame_elements
from build_menu import build_menu
from build_tabs import build_tabs
from declare_initialize_variables import declare_initialize_variables
from declare_pack_frames import declare_pack_frames
from status_bar import status_bar

from key_derivation.blake2 import BLAKE2s

from locking.locking import locking

from masking.masking import masking

from parsers.build_bench import build as build_bench
from parsers.build_blif import build as build_blif
from parsers.build_edif import build as build_edif
from parsers.build_slif import build as build_slif
from parsers.build_verilog_df import build as build_verilog_df
from parsers.build_verilog_struct import build as build_verilog_struct
from parsers.build_vhd_df import build as build_vhd_df
from parsers.build_vhd_struct import build as build_vhd_struct
from parsers.build_xilinx import build as build_xilinx

from generate_modified_netlist.convert_back_bench import convert_back as convert_back_bench
from generate_modified_netlist.convert_back_vhd import convert_back as convert_back_vhd

from boards_management import board_commands

from CASCADE.cascade import cascade

from response_converter.response_converter import response_converter_to_hex
from response_converter.response_converter import response_converter_to_bin_list

from AW_decoder.gen_AW_decoder import gen_AW_decoder

class App:

"""Main class for the demonstrator GUI app"""

def __init__(self, master):

self.master = master

# self.master.maxsize(width=800, height=560)
# self.master.minsize(width=800, height=560)
self.master.resizable(width=False, height=False)
self.master.title("SALWARE IP protection tool")
self.master.iconbitmap(default='./contents/icon.ico')

self.tcl_obj = Tcl()
self.board_manager = board_commands.Board_manager()

# Tabs
build_tabs(self)

# Menu bar
build_menu(self)

# Variables
declare_initialize_variables(self)

# Frames
declare_pack_frames(self)

# Frame elements
add_frame_elements(self)

# Status bar
status_bar(self)

def select_file(self):
self.update_status("")


ftypes = [("All netlist files", ".txt; .blif; .slif; .edf; .vhd; .v"),
("BENCH files", ".txt"),
("BLIF files", ".blif"),
("SLIF files", ".slif"),
("EDIF files", ".edf"),
("Xilinx EDIF files", ".edf"),
("Dataflow VHDL files", ".vhd"),
("Structural VHDL files", ".vhd"),
("Dataflow Verilog files", ".v"),
("Structural Verilog files", ".v")]
self.filename.set(tkFileDialog.askopenfilename(initialdir = "./user_space/",
filetypes=ftypes))

def select_modified_design_to_wrap(self):
self.update_status("")

ftypes = [("VHDL files", ".vhd")]
self.modified_design_to_wrap.set(tkFileDialog.askopenfilename(initialdir = "./user_space/",
filetypes=ftypes))

def build_graph(self):
self.update_status("")
self.update_status("Building the graph from file "+self.filename.get().split("/")[-1]+"...")
self.master.config(cursor="wait")
self.graph_info_label.configure(foreground="black")
self.master.update()
try:
if self.design_format.get() == "BENCH":
self.g, self.prim_in, self.prim_out, self.nodes = build_bench(self.filename.get())
elif self.design_format.get() == "BLIF":
self.g, self.prim_in, self.prim_out, self.nodes = build_blif(self.filename.get())
elif self.design_format.get() == "SLIF":
self.g, self.prim_in, self.prim_out, self.nodes = build_slif(self.filename.get())
elif self.design_format.get() == "EDIF":
self.g, self.prim_in, self.prim_out, self.nodes = build_edif(self.filename.get())
elif self.design_format.get() == "Xilinx EDIF":
self.g, self.prim_in, self.prim_out, self.nodes = build_xilinx(self.filename.get())
elif self.design_format.get() == "VHDL Dataflow":
self.g, self.prim_in, self.prim_out, self.nodes = build_vhd_df(self.filename.get())
elif self.design_format.get() == "VHDL Structural":
self.g, self.prim_in, self.prim_out, self.nodes = build_vhd_struct(self.filename.get())
elif self.design_format.get() == "Verilog Dataflow":
self.g, self.prim_in, self.prim_out, self.nodes = build_verilog_df(self.filename.get())
elif self.design_format.get() == "Verilog Structural":
self.g, self.prim_in, self.prim_out, self.nodes = build_verilog_struct(self.filename.get())
self.graph_info.set(str(str(len(self.nodes))+" nodes, "+
str(len(self.prim_in))+" inputs, "+
str(len(self.prim_out))+" outputs."))
self.update_status("Graph built")
except:
self.update_status("Error in building the graph")

self.master.config(cursor="")
self.master.update()

def open_server_reference_response(self):
self.update_status("")
rrtypes = [("Reference response file", ".txt")]
self.server_reference_response_file.set(tkFileDialog.askopenfilename(initialdir = "./../User_space/",
filetypes=rrtypes))
with open(self.server_reference_response_file.get(), "r") as rrfile:
line = rrfile.readline()
if line[:20] != "Reference response: ":
self.update_status("The file could not be parsed as a Reference Response file")
else:
self.server_reference_response = str(line[20:])
self.server_reference_response_displayed.set(self.server_reference_response)
self.server_reference_response = response_converter_to_bin_list(self.server_reference_response)
self.update_status("")
def open_activation_word(self):
self.update_status("")
awtypes = [("Activation word", ".txt")]
self.activation_word_file = tkFileDialog.askopenfilename(initialdir = "./../User_space/",
filetypes=awtypes)
with open(self.activation_word_file, "r") as awfile:
line = awfile.readline()
line = awfile.readline() # Get the second line
if not set(line) <= {"0", "1"}:
self.update_status("The file could not be parsed as an Activation Word file")
else:
self.activation_word.set(line)
print self.activation_word.get()

def open_associated_activation_word(self):
self.update_status("")
awtypes = [("Activation word file", ".txt")]
self.associated_activation_word_file = tkFileDialog.askopenfilename(initialdir = "./../User_space/",
filetypes=awtypes)
with open(self.associated_activation_word_file, "r") as awfile:
line = awfile.readline() # First line of the file
line = awfile.readline() # Get the second line
if not set(line) <= {"0", "1", " ", "\n"}:
self.update_status("The file could not be parsed as an Activation Word file")
else:
self.associated_activation_word.set(line)

def round_masking(self, _): # Dummy second argument
self.masking_overhead.set(int(round(self.masking_overhead.get())))

def round_locking(self, _): # Dummy second argument
self.locking_overhead.set(int(round(self.locking_overhead.get())))

def get_PUF_response(self):
self.update_status("")
self.PUF_response_displayed.set("")
try:
self.tcl_obj.eval(self.board_manager.reset_boards(port=2))
self.tcl_obj.eval(self.board_manager.generate_response())
temp_hex_response = self.tcl_obj.eval(self.board_manager.offload_response())
self.PUF_response = list(reversed(response_converter_to_bin_list(temp_hex_response)))
self.PUF_response_displayed.set(response_converter_to_hex(self.PUF_response))
except:
print("Error")
self.update_status("Response could not be obtained")

def save_as_reference_response(self):
self.update_status("")
with open("./user_space/rr.txt", "w") as rrfile:
rrfile.write("Reference response: "+self.PUF_response_displayed.get())
self.update_status("Reference response saved under ./user_space/rr.txt")

def pop_up_about(self):
self.update_status("")
self.top = Toplevel(background="White")
self.top.title("About")

self.msg = Label(self.top, text="This software was realised in the frame of\n\
the SALWARE project number ANR-13-JS03-0003\n\
supported by the French \"Agence Nationale de\n\
la Recherche\" and by the French \"Fondation de\n\
Recherche pour l'Aéronautique et l'Espace\",\n\
funding for this project was also provided by\n\
a grant from \"La Région Rhône-Alpes\".", background="white")
self.msg.pack()
self.salware_logo = PhotoImage(file="./contents/Logo_SALWARE.gif")

self.label_salware_logo = Label(self.top,
image=self.salware_logo,
background="white")
self.label_salware_logo.image = self.salware_logo # keep a reference!
self.label_salware_logo.pack()
self.logo_RA = PhotoImage(file="./contents/Logo_RA.gif")
self.label_logo_RA = Label(self.top,
image=self.logo_RA,
background="white")
self.label_logo_RA.image = self.logo_RA # keep a reference!
self.label_logo_RA.pack()
def pop_up_license(self):
self.update_status("")
self.top = Toplevel()
self.top.title("License")
try:
self.License_file = open("LICENSE", 'r')
self.License_text = self.License_file.read()
self.License_file.close()
except:
self.update_status("Cannot find LICENSE.txt")
self.msg = Label(self.top,
text=self.License_text,
background="white")
self.msg.pack()

def connect(self):
self.update_status("")
self.tcl_obj.eval(self.board_manager.source_tcl_package())
tcl_return = self.tcl_obj.eval(self.board_manager.connect(self.com_port.get()))
if tcl_return == "-1": # Error handling
self.update_status("Could not connect to the board")
else: # Connection successful
self.com_port_button_connect.configure(state=DISABLED)
self.com_port_button_disconnect.configure(state=NORMAL)
self.connected.set(True)
self.board_status.set("Board Connected")
self.board_status_label.configure(foreground="darkgreen")
self.status.set("")

def disconnect(self):
self.update_status("")
try:
self.tcl_obj.eval(self.board_manager.disconnect())
self.com_port_button_disconnect.configure(state=DISABLED)
self.com_port_button_connect.configure(state=NORMAL)
self.connected.set(False)
self.board_status.set("Board not connected")
self.board_status_label.configure(foreground="red")
except:
pass

def modify_design(self):
self.update_status("")
self.unlocking_word = ""
self.unmasking_word = ""
self.update_status("Modifying the design...")
self.master.config(cursor="wait")
self.graph_info_label.configure(foreground="black")
self.master.update()
self.locking_inputs = []
self.masking_inputs = []

if self.locking.get():
self.graph_modified, self.unlocking_word, self.locking_inputs = locking(self.g,
self.prim_in,
self.prim_out,
self.nodes,
self.locking_overhead.get())
if self.masking.get():
self.graph_modified, self.unmasking_word, self.masking_inputs = masking(self.graph_modified,
self.prim_in,
self.prim_out,
self.nodes,
self.locking_overhead.get(),
self.masking_heuristic.get())
elif self.masking.get():
self.graph_modified, self.unmasking_word, self.masking_inputs = masking(self.g,
self.prim_in,
self.prim_out,
self.nodes,
self.masking_overhead.get(),
self.masking_heuristic.get())
self.update_status("Modifications done")
self.master.config(cursor="")
self.master.update()

print self.locking_inputs
print self.masking_inputs

def derive_key_from_response(self):
self.update_status("")
self.salt = bytes(''.join(random.SystemRandom().choice(["0", "1"]) for _ in range(32)))
PRK_f = BLAKE2s(digest_size=32, key=self.salt)
PRK_f.update(self.PUF_response_displayed)
self.key = ''.join('{0:08b}'.format(ord(x), 'b') for x in PRK_f.final()).replace("0b", "")
key_file_name = "./user_space/key_"+""+".txt"
with open(key_file_name, "w") as key_file:
key_file.write("Salt = "+self.salt+"\n")
key_file.write("Key = "+self.key)
self.update_status("Key saved under "+key_file_name)

def generate_save_modified_design(self):
self.update_status("")
filename = self.filename.get().split("/")[-1]
filename = filename.split(".")
filename[0]+="_mod"
if self.generated_design_format.get() == "vhd":
filename[1] = "vhd"
filename = ".".join(filename)
filename = "/".join(self.filename.get().split("/")[:-1])+"/"+filename
convert_back_vhd(self.graph_modified, filename)
elif self.generated_design_format.get() == "bench":
filename[1] = "txt"
filename = ".".join(filename)
filename = "/".join(self.filename.get().split("/")[:-1])+"/"+filename
convert_back_bench(self.graph_modified, filename)
self.update_status(str("Modified design saved under "+filename))

def save_associated_AW(self):
self.update_status("")
filename = self.filename.get().split("/")[-1]
filename = filename.split(".")
filename[0]+="_mod_associated_AW"
filename[1] = "txt"
filename = ".".join(filename)
filename = "/".join(self.filename.get().split("/")[:-1])+"/"+filename
with open(filename, "w") as aw_file:
aw_file.write("Unlocking word concatenated with unmasking word\n"+self.unlocking_word+" "+self.unmasking_word)
self.update_status(str("Associated activation word saved under "+filename))

def save_formatted_AW(self):
self.update_status("")
filename = self.modified_design_to_wrap.get().split("/")[-1]
filename = filename.split(".")
filename[0]+="_formatted_AW"
filename[1] = "txt"
filename = ".".join(filename)
filename = "/".join(self.modified_design_to_wrap.get().split("/")[:-1])+"/"+filename
print filename
with open(filename, "w") as aw_file:
aw_file.write("Formatted Activation Word\n"+self.formatted_AW)
self.update_status(str("Formatted activation word saved under "+filename))

def generate_save_wrapped_design(self):
# Inputs:
# -> modified_design_to_wrap
# -> associated_activation_word
self.update_status("")
self.master.config(cursor="wait")
self.graph_info_label.configure(foreground="black")
self.master.update()
print self.associated_activation_word.get()[-1]
if self.associated_activation_word.get()[-1] == " ":
self.associated_activation_word.set(self.associated_activation_word.get().strip())
locking = True
masking = False
elif self.associated_activation_word.get()[0] == " ":
self.associated_activation_word.set(self.associated_activation_word.get().strip())
masking = True
locking = False
elif len(self.associated_activation_word.get().split(" ")) == 2:
masking = True
locking = True
print locking
print masking
try:
print self.associated_activation_word.get()
self.formatted_AW = gen_AW_decoder(self.associated_activation_word.get(),
128, #128-bit responses
locking = locking,
masking = masking,
path = "./user_space/")
self.update_status("AW decoder generated")
except:
self.update_status("An error occured while generating the AW decoder")
self.master.config(cursor="")
self.master.update()

def perform_reconciliation(self):
self.update_status("")
self.tcl_obj.eval(self.board_manager.reset_boards(port=2))
self.tcl_obj.eval(self.board_manager.generate_response())
server_before = self.server_reference_response
print "Server:", str.upper(hex(int(str(self.server_reference_response)[1:-1].replace(", ", ""), 2))[2:-1])
on_board = response_converter_to_bin_list(self.tcl_obj.eval(self.board_manager.offload_response()))
print "DBoard:", str.upper(hex(int(str(list(reversed(on_board)))[1:-1].replace(", ", ""), 2))[2:-1])
print "Differences before/on_board: ", [a for (a, (b, c)) in enumerate(zip(server_before, list(reversed(on_board)))) if b != c]
self.server_reference_response = cascade(self.server_reference_response,
0.02,
int(self.reconciliation_parameter_number_of_passes_spinbox.get()),
self.tcl_obj,
self.board_manager,
int(self.reconciliation_parameter_initial_block_size_spinbox.get()))
server_after = self.server_reference_response
on_board = response_converter_to_bin_list(self.tcl_obj.eval(self.board_manager.offload_response()))
print "Differences after/on_board: ", [a for (a, (b, c)) in enumerate(zip(server_after, list(reversed(on_board)))) if b != c]
print "Differences before/after: ", [a for (a, (b, c)) in enumerate(zip(server_before, server_after)) if b != c]
print ("Reconciliation done")

def update_status(self, text):
self.status.set(text)

def reset_board(self, port=2):
self.update_status("")
self.tcl_obj.eval(self.board_manager.reset_boards(port=2))

def reset(self):
self.update_status("")
print ("To be implemented soon")

if __name__ == "__main__":
fenetre = Tk()
app = App(fenetre)

fenetre.mainloop()