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.

188 lines
5.4 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_enc is
  22. generic (
  23. design_type : string := "ITER";
  24. formal : boolean := false;
  25. simulation : boolean := false
  26. );
  27. port (
  28. reset_i : in std_logic; -- async reset
  29. clk_i : in std_logic; -- clock
  30. key_i : in std_logic_vector(0 to 127); -- key input
  31. data_i : in std_logic_vector(0 to 127); -- data input
  32. valid_i : in std_logic; -- input key/data valid flag
  33. accept_o : out std_logic;
  34. data_o : out std_logic_vector(0 to 127); -- data output
  35. valid_o : out std_logic; -- output data valid flag
  36. accept_i : in std_logic
  37. );
  38. end entity aes_enc;
  39. architecture rtl of aes_enc is
  40. begin
  41. IterG : if design_type = "ITER" generate
  42. signal s_round : t_enc_rounds;
  43. begin
  44. CryptP : process (reset_i, clk_i) is
  45. variable v_state : t_datatable2d;
  46. variable v_key : t_key;
  47. begin
  48. if (reset_i = '0') then
  49. v_state := (others => (others => (others => '0')));
  50. v_key := (others => (others => '0'));
  51. s_round <= 0;
  52. accept_o <= '0';
  53. data_o <= (others => '0');
  54. valid_o <= '0';
  55. elsif (rising_edge(clk_i)) then
  56. case s_round is
  57. when 0 =>
  58. accept_o <= '1';
  59. if (accept_o = '1' and valid_i = '1') then
  60. accept_o <= '0';
  61. v_state := set_state(data_i);
  62. v_key := set_key(key_i);
  63. s_round <= s_round + 1;
  64. end if;
  65. when 1 =>
  66. v_state := addroundkey(v_state, v_key);
  67. v_key := key_round(v_key, s_round-1);
  68. s_round <= s_round + 1;
  69. when t_enc_rounds'high-1 =>
  70. v_state := subbytes(v_state);
  71. v_state := shiftrow(v_state);
  72. v_state := addroundkey(v_state, v_key);
  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_enc_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 := subbytes(v_state);
  87. v_state := shiftrow(v_state);
  88. v_state := mixcolumns(v_state);
  89. v_state := addroundkey(v_state, v_key);
  90. v_key := key_round(v_key, s_round-1);
  91. s_round <= s_round + 1;
  92. end case;
  93. end if;
  94. end process CryptP;
  95. formalG : if formal generate
  96. begin
  97. default clock is rising_edge(Clk_i);
  98. -- initial reset
  99. restrict {not reset_i; reset_i[+]}[*1];
  100. -- constraints
  101. assume always (valid_i and not accept_o -> next stable(valid_i));
  102. assume always (valid_i and not accept_o -> next stable(key_i));
  103. assume always (valid_i and not accept_o -> next stable(data_i));
  104. -- interface asserts
  105. assert always (accept_o -> s_round = 0);
  106. assert always (valid_i and accept_o -> next not accept_o);
  107. assert always (valid_o -> s_round = t_enc_rounds'high);
  108. assert always (valid_o and accept_i -> next not valid_o);
  109. assert always (valid_o and not accept_i -> next stable(valid_o));
  110. assert always (valid_o and not accept_i -> next stable(data_o));
  111. end generate formalG;
  112. simulationG : if simulation generate
  113. signal s_data : std_logic_vector(0 to 127);
  114. begin
  115. s_data <= data_o when rising_edge(clk_i) else
  116. 128x"0" when reset_i = '0';
  117. default clock is rising_edge(Clk_i);
  118. cover {accept_o};
  119. assert always (accept_o -> s_round = 0);
  120. cover {valid_i and accept_o};
  121. assert always (valid_i and accept_o -> next not accept_o);
  122. cover {valid_o};
  123. assert always (valid_o -> s_round = t_enc_rounds'high);
  124. cover {valid_o and accept_i};
  125. assert always (valid_o and accept_i -> next not valid_o);
  126. cover {valid_o and not accept_i};
  127. assert always (valid_o and not accept_i -> next valid_o);
  128. assert always (valid_o and not accept_i -> next data_o = s_data);
  129. end generate simulationG;
  130. end generate IterG;
  131. end architecture rtl;