cryptography ip-cores in vhdl / verilog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.3 KiB

  1. -- ======================================================================
  2. -- AES encryption/decryption
  3. -- Copyright (C) 2019 Torsten Meissner
  4. -------------------------------------------------------------------------
  5. -- This program is free software; you can redistribute it and/or modify
  6. -- it under the terms of the GNU General Public License as published by
  7. -- the Free Software Foundation; either version 2 of the License, or
  8. -- (at your option) any later version.
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. -- GNU General Public License for more details.
  13. -- You should have received a copy of the GNU General Public License
  14. -- along with this program; if not, write to the Free Software
  15. -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. -- ======================================================================
  17. library ieee;
  18. use ieee.std_logic_1164.all;
  19. use ieee.numeric_std.all;
  20. use work.aes_pkg.all;
  21. entity aes_dec is
  22. generic (
  23. design_type : string := "ITER"
  24. );
  25. port (
  26. reset_i : in std_logic; -- async reset
  27. clk_i : in std_logic; -- clock
  28. key_i : in std_logic_vector(0 to 127); -- key input
  29. data_i : in std_logic_vector(0 to 127); -- data input
  30. valid_i : in std_logic; -- input key/data valid flag
  31. accept_o : out std_logic;
  32. data_o : out std_logic_vector(0 to 127); -- data output
  33. valid_o : out std_logic; -- output data valid flag
  34. accept_i : in std_logic
  35. );
  36. end entity aes_dec;
  37. architecture rtl of aes_dec is
  38. begin
  39. IterG : if design_type = "ITER" generate
  40. signal s_round : t_dec_rounds;
  41. begin
  42. DeCryptP : process (reset_i, clk_i) is
  43. variable v_state : t_datatable2d;
  44. type t_key_array is array (0 to 10) of t_key;
  45. variable v_round_keys : t_key_array;
  46. begin
  47. if (reset_i = '0') then
  48. v_state := (others => (others => (others => '0')));
  49. s_round <= 0;
  50. accept_o <= '0';
  51. data_o <= (others => '0');
  52. valid_o <= '0';
  53. elsif (rising_edge(clk_i)) then
  54. case s_round is
  55. when 0 =>
  56. accept_o <= '1';
  57. if (accept_o = '1' and valid_i = '1') then
  58. accept_o <= '0';
  59. v_state := set_state(data_i);
  60. v_round_keys(0) := set_key(key_i);
  61. for i in t_key_rounds'low to t_key_rounds'high loop
  62. v_round_keys(i+1) := key_round(v_round_keys(i), i);
  63. end loop;
  64. s_round <= s_round + 1;
  65. end if;
  66. when 1 =>
  67. v_state := addroundkey(v_state, v_round_keys(v_round_keys'length-s_round));
  68. s_round <= s_round + 1;
  69. when t_dec_rounds'high-1 =>
  70. v_state := invshiftrow(v_state);
  71. v_state := invsubbytes(v_state);
  72. v_state := addroundkey(v_state, v_round_keys(v_round_keys'length-s_round));
  73. s_round <= s_round + 1;
  74. -- set data & valid to save one cycle
  75. valid_o <= '1';
  76. data_o <= get_state(v_state);
  77. when t_dec_rounds'high =>
  78. if (valid_o = '1' and accept_i = '1') then
  79. valid_o <= '0';
  80. data_o <= (others => '0');
  81. s_round <= 0;
  82. -- Set accept to save one cycle
  83. accept_o <= '1';
  84. end if;
  85. when others =>
  86. v_state := invshiftrow(v_state);
  87. v_state := invsubbytes(v_state);
  88. v_state := addroundkey(v_state, v_round_keys(v_round_keys'length-s_round));
  89. v_state := invmixcolumns(v_state);
  90. s_round <= s_round + 1;
  91. end case;
  92. end if;
  93. end process DeCryptP;
  94. psl : block is
  95. signal s_key , s_din, s_dout : std_logic_vector(0 to 127) := (others => '0');
  96. begin
  97. process (clk_i) is
  98. begin
  99. if (rising_edge(clk_i)) then
  100. s_key <= key_i;
  101. s_din <= data_i;
  102. s_dout <= data_o;
  103. end if;
  104. end process;
  105. default clock is rising_edge(clk_i);
  106. -- initial reset
  107. restrict {not reset_i; reset_i[+]}[*1];
  108. -- constraints
  109. assume always (valid_i and not accept_o -> next valid_i);
  110. assume always (valid_i and not accept_o -> next key_i = s_key);
  111. assume always (valid_i and not accept_o -> next data_i = s_din);
  112. ACCEPTO_c : cover {accept_o};
  113. ACCEPT_IN_ROUND_0_ONLY_a : assert always (accept_o -> s_round = 0);
  114. VALIDI_AND_ACCEPTO_c : cover {valid_i and accept_o};
  115. ACCEPT_OFF_WHEN_VALID_a : assert always (valid_i and accept_o -> next not accept_o);
  116. VALIDO_c : cover {valid_o};
  117. VALID_IN_LAST_ROUND_ONLY_a : assert always (valid_o -> s_round = t_enc_rounds'high);
  118. VALIDO_AND_ACCEPTI_c : cover {valid_o and accept_i};
  119. VALID_OFF_WHEN_ACCEPTED_a : assert always (valid_o and accept_i -> next not valid_o);
  120. VALIDO_AND_NOT_ACCEPTI_c : cover {valid_o and not accept_i};
  121. VALID_STABLE_WHEN_NOT_ACCEPTED_a : assert always (valid_o and not accept_i -> next valid_o);
  122. DATA_STABLE_WHEN_NOT_ACCEPTED_a : assert always (valid_o and not accept_i -> next data_o = s_dout);
  123. end block psl;
  124. end generate IterG;
  125. end architecture rtl;