Commit 381180d95010432df7fd860c3f3288f7a6f12caf

Authored by Brice Colombier
1 parent 4751d2988d
Exists in master

Generate keyed design

Showing 3 changed files with 122 additions and 43 deletions

add_frame_elements.py View file @ 381180d
... ... @@ -27,12 +27,12 @@
27 27 self.com_port_button_connect.pack(side=LEFT)
28 28 self.com_port_button_reset = Button(self.motherboard_frame,
29 29 text="Reset",
30   - state=NORMAL,
  30 + state="disabled",
31 31 command=self.reset_board)
32 32 self.com_port_button_reset.pack(side=LEFT)
33 33 self.com_port_button_disconnect = Button(self.motherboard_frame,
34 34 text="Disconnect",
35   - state=NORMAL,
  35 + state="disabled",
36 36 command=self.disconnect)
37 37 self.com_port_button_disconnect.pack(side=LEFT)
38 38 # ------------------------------------------------------
... ... @@ -94,7 +94,7 @@
94 94 self.save_as_reference_response_button=Button(self.get_PUF_response_frame,
95 95 text="Save as reference response",
96 96 command=self.save_as_reference_response,
97   - state="normal")
  97 + state="disabled")
98 98 self.save_as_reference_response_button.grid(row=1, column=0)
99 99 self.save_as_reference_response_status_label=Label(self.get_PUF_response_frame,
100 100 textvariable=self.save_as_reference_response_status)
... ... @@ -130,7 +130,7 @@
130 130 self.build_graph_button=Button(self.file_frame,
131 131 text="Build graph",
132 132 command=self.build_graph,
133   - state="normal")
  133 + state="disabled")
134 134 self.build_graph_button.grid(row=2, column=0)
135 135 self.graph_info_label=Label(self.file_frame,
136 136 textvariable=self.graph_info)
... ... @@ -161,7 +161,7 @@
161 161 state="normal")
162 162 self.masking_overhead_spinbox.grid(row=1, column=1)
163 163 self.locking_overhead_label=Label(self.modify_design_frame,
164   - text="% area overhead")
  164 + text="% area overhead at most")
165 165 self.locking_overhead_label.grid(row=0, column=2, sticky=W)
166 166 self.masking_overhead_label=Label(self.modify_design_frame,
167 167 text="% area overhead using heuristic:")
... ... @@ -259,7 +259,7 @@
259 259 sticky=W)
260 260 self.save_associated_activation_word_simple_button=Button(self.generate_save_modified_design_frame,
261 261 text="Save associated Activation word",
262   - state="normal",
  262 + state="disabled",
263 263 command=self.save_associated_AW,
264 264 width=32)
265 265 self.save_associated_activation_word_simple_button.grid(row=3,
... ... @@ -51,6 +51,8 @@
51 51  
52 52 from AW_decoder.gen_AW_decoder import gen_AW_decoder
53 53  
  54 +from keyed_design.generate_keyed_design import generate_keyed_design
  55 +
54 56 class App:
55 57  
56 58 """Main class for the demonstrator GUI app"""
... ... @@ -86,6 +88,21 @@
86 88 # Status bar
87 89 status_bar(self)
88 90  
  91 + # Init frame elements
  92 + # Logic modifier frame
  93 + self.setState(self.modify_design_frame, state = "disabled")
  94 + self.setState(self.generate_save_modified_design_frame, state = "disabled")
  95 + self.setState(self.wrap_modified_design_frame, state = "disabled")
  96 + # Activation frame
  97 + self.setState(self.CASCADE_frame, state = "disabled")
  98 + self.setState(self.key_derivation_frame, state = "disabled")
  99 + self.setState(self.activation_word_encryption_frame, state = "disabled")
  100 + self.setState(self.activation_process_frame, state = "disabled")
  101 + # Enrolment frame
  102 + self.setState(self.get_PUF_response_frame, state = "disabled")
  103 +
  104 +
  105 +
89 106 def select_file(self):
90 107 self.update_status("")
91 108  
... ... @@ -102,6 +119,22 @@
102 119 ("Structural Verilog files", ".v")]
103 120 self.filename.set(tkFileDialog.askopenfilename(initialdir = "./user_space/",
104 121 filetypes=ftypes))
  122 + if self.filename.get() == "":
  123 + self.filename.set("No design loaded")
  124 + else:
  125 + if self.filename.get().split(".")[-1] == "txt":
  126 + self.design_format.set("BENCH")
  127 + elif self.filename.get().split(".")[-1] == "blif":
  128 + self.design_format.set("BLIF")
  129 + elif self.filename.get().split(".")[-1] == "slif":
  130 + self.design_format.set("SLIF")
  131 + elif self.filename.get().split(".")[-1] == "edf":
  132 + self.design_format.set("EDIF")
  133 + elif self.filename.get().split(".")[-1] == "vhd":
  134 + self.design_format.set("VHDL Dataflow")
  135 + elif self.filename.get().split(".")[-1] == "v":
  136 + self.design_format.set("Verilog Dataflow")
  137 + self.build_graph_button.configure(state = "normal")
105 138  
106 139 def select_modified_design_to_wrap(self):
107 140 self.update_status("")
108 141  
... ... @@ -139,9 +172,11 @@
139 172 str(len(self.prim_in))+" inputs, "+
140 173 str(len(self.prim_out))+" outputs."))
141 174 self.update_status("Graph built")
  175 + self.setState(self.modify_design_frame, state = "normal")
142 176 except:
143 177 self.update_status("Error in building the graph")
144   -
  178 + self.setState(self.generate_save_modified_design_frame, state = "disabled")
  179 + self.setState(self.wrap_modified_design_frame, state = "disabled")
145 180 self.master.config(cursor="")
146 181 self.master.update()
147 182  
... ... @@ -159,6 +194,8 @@
159 194 self.server_reference_response_displayed.set(self.server_reference_response)
160 195 self.server_reference_response = response_converter_to_bin_list(self.server_reference_response)
161 196 self.update_status("")
  197 + if self.connected.get() == True:
  198 + self.setState(self.CASCADE_frame, state = "normal")
162 199  
163 200 def open_activation_word(self):
164 201 self.update_status("")
... ... @@ -186,6 +223,8 @@
186 223 self.update_status("The file could not be parsed as an Activation Word file")
187 224 else:
188 225 self.associated_activation_word.set(line)
  226 + if self.modified_design_to_wrap.get() != "No design loaded":
  227 + self.generate_wrapped_design_button.configure(state = "normal")
189 228  
190 229 def round_masking(self, _): # Dummy second argument
191 230 self.masking_overhead.set(int(round(self.masking_overhead.get())))
... ... @@ -203,7 +242,7 @@
203 242 self.PUF_response = list(reversed(response_converter_to_bin_list(temp_hex_response)))
204 243 self.PUF_response_displayed.set(response_converter_to_hex(self.PUF_response))
205 244 except:
206   - print("Error")
  245 + self.server_reference_response_displayed.set("")
207 246 self.update_status("Response could not be obtained")
208 247  
209 248 def save_as_reference_response(self):
... ... @@ -260,6 +299,7 @@
260 299 tcl_return = self.tcl_obj.eval(self.board_manager.connect(self.com_port.get()))
261 300 if tcl_return == "-1": # Error handling
262 301 self.update_status("Could not connect to the board")
  302 + self.connected.set(False)
263 303 else: # Connection successful
264 304 self.com_port_button_connect.configure(state=DISABLED)
265 305 self.com_port_button_disconnect.configure(state=NORMAL)
... ... @@ -267,6 +307,7 @@
267 307 self.board_status.set("Board Connected")
268 308 self.board_status_label.configure(foreground="darkgreen")
269 309 self.status.set("")
  310 + self.setState(self.get_PUF_response_frame, state = "normal")
270 311  
271 312 def disconnect(self):
272 313 self.update_status("")
... ... @@ -314,6 +355,9 @@
314 355 self.nodes,
315 356 self.masking_overhead.get(),
316 357 self.masking_heuristic.get())
  358 + self.setState(self.generate_save_modified_design_frame, state = "normal")
  359 + self.save_associated_activation_word_simple_button.configure(state = "disabled")
  360 + self.setState(self.modify_design_frame, state = "disabled")
317 361 self.update_status("Modifications done")
318 362 self.master.config(cursor="")
319 363 self.master.update()
... ... @@ -349,6 +393,8 @@
349 393 filename = "/".join(self.filename.get().split("/")[:-1])+"/"+filename
350 394 convert_back_bench(self.graph_modified, filename)
351 395 self.update_status(str("Modified design saved under "+filename))
  396 + self.save_associated_activation_word_simple_button.configure(state = "normal")
  397 + self.generate_modified_design_button.configure(state = "disabled")
352 398  
353 399 def save_associated_AW(self):
354 400 self.update_status("")
... ... @@ -361,6 +407,9 @@
361 407 with open(filename, "w") as aw_file:
362 408 aw_file.write("Unlocking word concatenated with unmasking word\n"+self.unlocking_word+" "+self.unmasking_word)
363 409 self.update_status(str("Associated activation word saved under "+filename))
  410 + self.setState(self.wrap_modified_design_frame, state = "normal")
  411 + self.setState(self.generate_save_modified_design_frame, state = "disabled")
  412 +
364 413  
365 414 def save_formatted_AW(self):
366 415 self.update_status("")
... ... @@ -379,6 +428,8 @@
379 428 # Inputs:
380 429 # -> modified_design_to_wrap
381 430 # -> associated_activation_word
  431 +
  432 +
382 433 self.update_status("")
383 434 self.master.config(cursor="wait")
384 435 self.graph_info_label.configure(foreground="black")
... ... @@ -395,8 +446,6 @@
395 446 elif len(self.associated_activation_word.get().split(" ")) == 2:
396 447 masking = True
397 448 locking = True
398   - print locking
399   - print masking
400 449 try:
401 450 print self.associated_activation_word.get()
402 451 self.formatted_AW = gen_AW_decoder(self.associated_activation_word.get(),
403 452  
... ... @@ -404,9 +453,17 @@
404 453 locking = locking,
405 454 masking = masking,
406 455 path = "./user_space/")
407   - self.update_status("AW decoder generated")
408 456 except:
409 457 self.update_status("An error occured while generating the AW decoder")
  458 + try:
  459 + generate_keyed_design("./user_space/"+self.filename.get().split("/")[-1].split(".")[0]+"_mod.vhd",
  460 + "./user_space/AW_decoder.vhd",
  461 + (locking, self.locking_inputs),
  462 + (masking, self.masking_inputs))
  463 + self.update_status("Keyed design generated")
  464 + except Exception as e:
  465 + print e
  466 + self.update_status("An error occured while generating the keyed design")
410 467 self.master.config(cursor="")
411 468 self.master.update()
412 469  
413 470  
... ... @@ -439,8 +496,26 @@
439 496 self.tcl_obj.eval(self.board_manager.reset_boards(port=2))
440 497  
441 498 def reset(self):
  499 + declare_initialize_variables(self)
442 500 self.update_status("")
443   - print ("To be implemented soon")
  501 + self.setState(self.modify_design_frame, state = "disabled")
  502 + self.setState(self.generate_save_modified_design_frame, state = "disabled")
  503 + self.setState(self.wrap_modified_design_frame, state = "disabled")
  504 + # Activation frame
  505 + self.setState(self.CASCADE_frame, state = "disabled")
  506 + self.setState(self.key_derivation_frame, state = "disabled")
  507 + self.setState(self.activation_word_encryption_frame, state = "disabled")
  508 + self.setState(self.activation_process_frame, state = "disabled")
  509 + # Enrolment frame
  510 + self.setState(self.get_PUF_response_frame, state = "disabled")
  511 +
  512 + def setState(self, widget, state='disabled'):
  513 + try:
  514 + widget.configure(state=state)
  515 + except TclError:
  516 + pass
  517 + for child in widget.winfo_children():
  518 + self.setState(child, state=state)
444 519  
445 520 if __name__ == "__main__":
446 521  
keyed_design/generate_keyed_design.py View file @ 381180d
1 1 import re
2 2  
3   -def get_entity(design_file_name):
  3 +def get_entity(modified_design_file_name):
4 4  
5   - with open(design_file_name, "r") as design_file:
  5 + with open(modified_design_file_name, "r") as design_file:
6 6 for line in design_file:
7 7 if "ENTITY" in line and "IS" in line:
8   - entity_name = re.search('ENTITY [A-Za-z_]* IS', line.upper()).group(0).replace("ENTITY", "").replace("IS", "").strip().lower()
  8 + entity_name = re.search('ENTITY [A-Za-z_0-9]* IS', line.upper()).group(0).replace("ENTITY", "").replace("IS", "").strip().lower().replace("_mod", "")
9 9 break
10 10 return entity_name
11 11  
12 12  
13 13 def write_entity(target_file,
14   - design_file_name,
15   - entity_name):
  14 + modified_design_file_name,
  15 + entity_name,
  16 + locking_inputs,
  17 + masking_inputs):
16 18 target_file.write("LIBRARY ieee;\n")
17 19 target_file.write("USE ieee.std_logic_1164.ALL;\n")
18 20 target_file.write("\n")
19   - target_file.write("ENTITY keyed_"+entity_name+" IS\n")
  21 + target_file.write("ENTITY "+entity_name+"_activable IS\n")
20 22 target_file.write("\n")
21 23 target_file.write(" PORT (\n")
22 24 inputs = []
23 25 outputs = []
24   - with open(design_file_name, "r") as design_file:
  26 + with open(modified_design_file_name, "r") as design_file:
25 27 for line in design_file:
26 28 if "IN STD_LOGIC" in line.upper():
27   - if "IN STD_LOGIC_VECTOR" in line.upper():
28   - if "DOWNTO" in line:
29   - vector_range = re.search('\([0-9]* DOWNTO [0-9]*\)', line.upper()).group(0)[1:-1].replace(" ", "").split("DOWNTO")
30   - target_file.write(" "++line.split(":")[0].strip()+" : IN STD_LOGIC_VECTOR("+vector_range[0]+" DOWNTO "+vector_range[1]+");\n")
31   - elif "TO" in line:
32   - vector_range = re.search('\([0-9]* TO [0-9]*\)', line.upper()).group(0)[1:-1].replace(" ", "").split("TO")
33   - target_file.write(" "++line.split(":")[0].strip()+" : IN STD_LOGIC_VECTOR("+vector_range[0]+" TO "+vector_range[1]+");\n")
34   - else:
35   - target_file.write(" "+line.split(":")[0].strip()+" : IN STD_LOGIC;\n")
36   - inputs.append(line.split(":")[0].strip())
  29 + input_name = line.split(":")[0].strip()
  30 + if input_name not in locking_inputs and input_name not in masking_inputs:
  31 + if "IN STD_LOGIC_VECTOR" in line.upper():
  32 + if "DOWNTO" in line:
  33 + vector_range = re.search('\([0-9]* DOWNTO [0-9]*\)', line.upper()).group(0)[1:-1].replace(" ", "").split("DOWNTO")
  34 + target_file.write(" "+input_name+" : IN STD_LOGIC_VECTOR("+vector_range[0]+" DOWNTO "+vector_range[1]+");\n")
  35 + elif "TO" in line:
  36 + vector_range = re.search('\([0-9]* TO [0-9]*\)', line.upper()).group(0)[1:-1].replace(" ", "").split("TO")
  37 + target_file.write(" "+input_name+" : IN STD_LOGIC_VECTOR("+vector_range[0]+" TO "+vector_range[1]+");\n")
  38 + else:
  39 + target_file.write(" "+line.split(":")[0].strip()+" : IN STD_LOGIC;\n")
  40 + inputs.append(line.split(":")[0].strip())
37 41 elif "OUT STD_LOGIC" in line.upper():
38 42 if "OUT STD_LOGIC_VECTOR" in line.upper():
39 43 if "DOWNTO" in line:
40 44 vector_range = re.search('\([0-9]* DOWNTO [0-9]*\)', line.upper()).group(0)[1:-1].replace(" ", "").split("DOWNTO")
41   - target_file.write(" "++line.split(":")[0].strip()+" : OUT STD_LOGIC_VECTOR("+vector_range[0]+" DOWNTO "+vector_range[1]+");\n")
  45 + target_file.write(" "+line.split(":")[0].strip()+" : OUT STD_LOGIC_VECTOR("+vector_range[0]+" DOWNTO "+vector_range[1]+");\n")
42 46 elif "TO" in line:
43 47 vector_range = re.search('\([0-9]* TO [0-9]*\)', line.upper()).group(0)[1:-1].replace(" ", "").split("TO")
44   - target_file.write(" "++line.split(":")[0].strip()+" : OUT STD_LOGIC_VECTOR("+vector_range[0]+" TO "+vector_range[1]+");\n")
  48 + target_file.write(" "+line.split(":")[0].strip()+" : OUT STD_LOGIC_VECTOR("+vector_range[0]+" TO "+vector_range[1]+");\n")
45 49 else:
46 50 target_file.write(" "+line.split(":")[0].strip()+" : OUT STD_LOGIC;\n")
47 51 outputs.append(line.split(":")[0].strip())
48 52 target_file.write(" key : IN STD_LOGIC_VECTOR(127 DOWNTO 0)\n );\n\n")
49   - target_file.write("END ENTITY keyed_"+entity_name+";\n\n")
  53 + target_file.write("END ENTITY "+entity_name+"_activable;\n\n")
50 54 return inputs, outputs
51 55  
52 56 def write_architecture(target_file,
... ... @@ -55,7 +59,7 @@
55 59 masking,
56 60 inputs,
57 61 outputs):
58   - target_file.write("ARCHITECTURE rtl OF keyed_"+entity_name+" IS\n\n")
  62 + target_file.write("ARCHITECTURE rtl OF "+entity_name+"_activable IS\n\n")
59 63 if locking[0] and masking[0]:
60 64 target_file.write(" SIGNAL AW_locking : STD_LOGIC_VECTOR("+str(len(locking[1])-1)+" DOWNTO 0);\n")
61 65 target_file.write(" SIGNAL AW_masking : STD_LOGIC_VECTOR(0 TO "+str(len(masking[1])-1)+");\n")
... ... @@ -72,7 +76,7 @@
72 76 if locking[0] and masking[0]:
73 77 target_file.write(" AW_locking <= AW(0 TO "+str(len(locking[1])-1)+");\n")
74 78 target_file.write(" AW_masking <= AW("+str(len(locking[1]))+" TO "+str(len(locking[1])+len(masking[1])-1)+");\n\n")
75   - target_file.write(" "+entity_name+"_mod_1: "+entity_name+"_mod\n")
  79 + target_file.write(" "+entity_name+"_mod_1: ENTITY work."+entity_name+"_mod\n")
76 80 target_file.write(" PORT MAP (\n")
77 81 for input_net in inputs:
78 82 target_file.write(" "+input_net+" => "+input_net+",\n")
79 83  
80 84  
81 85  
82 86  
... ... @@ -95,26 +99,26 @@
95 99 target_file.write("END ARCHITECTURE rtl;")
96 100  
97 101  
98   -def generate_keyed_design(design_file_name,
  102 +def generate_keyed_design(modified_design_file_name,
99 103 AW_decoder_file_name,
100 104 locking,
101   - masking,
102   - target_file_name):
  105 + masking):
  106 + target_file_name = modified_design_file_name.replace("mod", "activable")
  107 + print modified_design_file_name
103 108  
104 109 with open(target_file_name, "w") as target_file:
105   - entity_name = get_entity(design_file_name)
106   - inputs, outputs = write_entity(target_file, design_file_name, entity_name)
107   - print inputs
108   - print outputs
  110 + entity_name = get_entity(modified_design_file_name)
  111 + print entity_name
  112 + inputs, outputs = write_entity(target_file, modified_design_file_name, entity_name, locking[1], masking[1])
109 113 write_architecture(target_file, entity_name, locking, masking, inputs, outputs)
110 114  
111 115 if __name__ == "__main__":
112 116 locking_inputs = ["KG180gat", "KG285gat", "KG356gat", "KG416gat", "KG381gat"]
113 117 masking_inputs = ["KG309gat", "KG203gat", "KG319gat", "KG213gat", "KG296gat", "KG357gat", "KG360gat", "KG199gat"]
114   - generate_keyed_design("./../user_space/sin.vhd",
  118 + generate_keyed_design("./../user_space/c432_mod.vhd",
115 119 "./../user_space/AW_decoder.vhd",
116 120 (True, locking_inputs),
117 121 (True, masking_inputs),
118   - "./../user_space/sin_keyed.vhd")
  122 + "./../user_space/c432_keyed.vhd")
119 123