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.

160 lines
4.7 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. -- synthesis off
  95. verification : block is
  96. signal s_data : std_logic_vector(0 to 127);
  97. begin
  98. s_data <= data_o when rising_edge(clk_i) else
  99. 128x"0" when reset_i = '0';
  100. default clock is rising_edge(Clk_i);
  101. cover {accept_o};
  102. assert always (accept_o -> s_round = 0);
  103. cover {valid_i and accept_o};
  104. assert always (valid_i and accept_o -> next not accept_o);
  105. cover {valid_o};
  106. assert always (valid_o -> s_round = t_dec_rounds'high);
  107. cover {valid_o and accept_i};
  108. assert always (valid_o and accept_i -> next not valid_o);
  109. cover {valid_o and not accept_i};
  110. assert always (valid_o and not accept_i -> next valid_o);
  111. assert always (valid_o and not accept_i -> next data_o = s_data);
  112. end block verification;
  113. -- synthesis on
  114. end generate IterG;
  115. end architecture rtl;