HAL  v4.5.0-124-g47ab54673
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
cipher_candidate.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
31 #pragma once
32 
36 #include "hawkeye/sbox_database.h"
37 
38 #include <map>
39 #include <memory>
40 #include <set>
41 #include <string>
42 #include <vector>
43 
44 namespace hal
45 {
46  class Gate;
47  class Module;
48  class Net;
49  class Netlist;
50 
51  namespace graph_algorithm
52  {
53  class NetlistGraph;
54  }
55 
56  namespace hawkeye
57  {
61  enum class SBoxStatus
62  {
63  unidentified,
64  identified,
65  superseded,
66  };
67 
78  struct SBox
79  {
83  std::vector<Gate*> component;
84 
93  std::vector<Gate*> input_gates;
94 
98  std::vector<Gate*> output_gates;
99 
103  std::string identified_as;
104 
112 
121  std::vector<Gate*> get_combinational_gates() const;
122  };
123 
136  {
137  public:
138  CipherCandidate() = default;
139  ~CipherCandidate() = default;
140 
146  explicit CipherCandidate(const std::set<Gate*>& round_reg);
147 
155  CipherCandidate(const std::set<Gate*>& in_reg, const std::set<Gate*>& out_reg);
156 
159 
166 
188  detect(Netlist* nl, const std::vector<DetectionConfiguration>& configs, u32 min_state_size = 40, const std::vector<Gate*>& start_ffs = {});
189 
204 
219 
225  void clear_sboxes();
226 
243 
257  Result<std::string> identify_sbox(const SBox* sbox, const SBoxDatabase& db) const;
258 
271  static Result<std::string> identify_sbox(const std::vector<BooleanFunction>& output_functions, const SBoxDatabase& db);
272 
284 
290  Netlist* get_netlist() const;
291 
297  u32 get_size() const;
298 
304  bool is_round_based() const;
305 
311  bool has_round_function() const;
312 
318  const std::vector<Gate*>& get_input_reg() const;
319 
326  const std::vector<Gate*>& get_output_reg() const;
327 
333  const std::vector<Gate*>& get_round_logic() const;
334 
340  std::vector<Gate*> get_gates() const;
341 
347  std::vector<SBox*> get_sboxes() const;
348 
356 
362  const std::set<Net*>& get_state_inputs() const;
363 
369  const std::set<Net*>& get_control_inputs() const;
370 
376  const std::set<Net*>& get_other_inputs() const;
377 
383  const std::set<Net*>& get_state_outputs() const;
384 
390  const std::map<Gate*, std::set<Gate*>>& get_input_ffs_of_gate() const;
391 
397  const std::map<u32, std::set<Gate*>>& get_longest_distance_to_gate() const;
398 
409  bool operator<(const CipherCandidate& rhs) const;
410 
417  bool operator==(const CipherCandidate& rhs) const;
418 
419  private:
421  Netlist* m_netlist = nullptr;
422 
424  u32 m_size = 0;
425 
427  bool m_is_round_based = false;
428 
430  std::vector<Gate*> m_in_reg;
431 
433  std::vector<Gate*> m_out_reg;
434 
442  std::vector<u32> m_in_reg_ids, m_out_reg_ids;
443 
445  std::vector<Gate*> m_round_logic;
446 
448  std::unique_ptr<graph_algorithm::NetlistGraph> m_graph;
449 
451  std::set<Net*> m_state_inputs, m_state_outputs, m_control_inputs, m_other_inputs;
452 
462  mutable std::map<Gate*, std::set<Gate*>> m_input_ffs_of_gate;
463  mutable std::map<u32, std::set<Gate*>> m_longest_distance_to_gate;
464 
466  mutable bool m_has_gate_dependencies = false;
467 
472  void compute_gate_dependencies() const;
473 
480  std::vector<std::unique_ptr<SBox>> m_sboxes;
481 
483  bool m_has_round_function = false;
484  };
485  } // namespace hawkeye
486 } // namespace hal
std::set< u32 > out_reg
std::set< u32 > in_reg
This file contains the struct for configuring HAWKEYE's candidate search, see CipherCandidate::detect...
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:60
A candidate for a symmetric cryptographic implementation within a netlist.
const std::map< u32, std::set< Gate * > > & get_longest_distance_to_gate() const
Get a map from a distance to all gates reachable within at most that distance from any input flip-flo...
std::vector< Gate * > get_gates() const
Get all gates of the candidate, i.e., its registers together with its round function,...
CipherCandidate & operator=(CipherCandidate &&)=default
const std::set< Net * > & get_control_inputs() const
Get the control inputs of the round function.
std::vector< SBox * > get_sboxes() const
Get the S-boxes located within the round function of the candidate.
Result< u32 > identify_sboxes(const SBoxDatabase &db)
Try to identify all S-boxes of the candidate by matching them against a database of known S-boxes.
graph_algorithm::NetlistGraph * get_graph() const
Get the graph of the round function, in which the gates of the state register are represented by a pr...
Result< std::vector< SBox * > > locate_sboxes()
Try to locate S-boxes within the round function of the candidate.
void clear_sboxes()
Discard the S-boxes located so far.
const std::set< Net * > & get_state_outputs() const
Get the state outputs of the round function.
const std::vector< Gate * > & get_input_reg() const
Get the input register of the candidate, ordered by gate ID.
const std::vector< Gate * > & get_round_logic() const
Get the combinational logic computing the next state, ordered by gate ID.
Result< Module * > create_modules()
Write the candidate back into the netlist as a module hierarchy.
Result< std::monostate > build_round_function()
Determine the round function of the candidate, i.e., the combinational logic computing the next state...
const std::set< Net * > & get_state_inputs() const
Get the state inputs of the round function.
CipherCandidate & operator=(const CipherCandidate &)=delete
bool operator==(const CipherCandidate &rhs) const
Check whether two candidates have the same size and the same registers.
Netlist * get_netlist() const
Get the netlist that the candidate belongs to.
CipherCandidate(const CipherCandidate &)=delete
const std::set< Net * > & get_other_inputs() const
Get the remaining inputs of the round function.
bool is_round_based() const
Check whether the candidate is round-based, i.e., whether its input and output register are the same.
static Result< std::vector< CipherCandidate > > detect(Netlist *nl, const std::vector< DetectionConfiguration > &configs, u32 min_state_size=40, const std::vector< Gate * > &start_ffs={})
Attempt to locate candidates for symmetric cryptographic SPN, Feistel, and ARX implementations within...
Result< std::string > identify_sbox(const SBox *sbox, const SBoxDatabase &db) const
Try to identify a single S-box of this candidate by matching it against a database of known S-boxes u...
u32 get_size() const
Get the size of the candidate, i.e., the width of its state register.
bool operator<(const CipherCandidate &rhs) const
Compare two candidates.
const std::vector< Gate * > & get_output_reg() const
Get the output register of the candidate, ordered by gate ID. Equal to the input register for a round...
bool has_round_function() const
Check whether the round function of the candidate has been computed, see build_round_function.
CipherCandidate(CipherCandidate &&)=default
const std::map< Gate *, std::set< Gate * > > & get_input_ffs_of_gate() const
Get a map from each gate of the round function to the input flip-flops it depends on.
Database of known S-boxes.
Definition: sbox_database.h:50
uint32_t u32
Definition: defines.h:41
SBoxStatus
The outcome of trying to identify an S-box, see CipherCandidate::identify_sboxes.
Definition: defines.h:45
This file contains the S-box database class that holds and manages known cryptographic S-boxes up to ...
An S-box located within the round function of a CipherCandidate.
std::string identified_as
The name of the S-box in the database it was identified as, empty unless status is identified.
std::vector< Gate * > output_gates
The output gates of the S-box, ordered by gate ID. Usually combinational gates feeding the linear lay...
std::vector< Gate * > component
The gates of the connected component that the S-box was located in, including its input flip-flops.
std::vector< Gate * > input_gates
The input flip-flops of the S-box, ordered by gate ID.
SBoxStatus status
The outcome of trying to identify the S-box, unidentified until identify_sboxes ran.
std::vector< Gate * > get_combinational_gates() const
Get the combinational gates computing the outputs of the S-box from its input flip-flops.