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.

139 lines
4.4 KiB

  1. -- ======================================================================
  2. -- AES decryption
  3. -- algorithm according to FIPS 197 specification
  4. -- Copyright (C) 2011 Torsten Meissner
  5. -------------------------------------------------------------------------
  6. -- This program is free software; you can redistribute it and/or modify
  7. -- it under the terms of the GNU General Public License as published by
  8. -- the Free Software Foundation; either version 2 of the License, or
  9. -- (at your option) any later version.
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. -- GNU General Public License for more details.
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program; if not, write to the Free Software
  16. -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. -- ======================================================================
  18. library ieee;
  19. use ieee.std_logic_1164.all;
  20. use ieee.numeric_std.all;
  21. use work.aes_pkg.all;
  22. entity aes_dec is
  23. generic (
  24. design_type : string := "ITER"
  25. );
  26. port (
  27. reset_i : in std_logic; -- async reset
  28. clk_i : in std_logic; -- clock
  29. key_i : in std_logic_vector(0 TO 127); -- key input
  30. data_i : in std_logic_vector(0 TO 127); -- data input
  31. valid_i : in std_logic; -- input key/data valid flag
  32. accept_o : out std_logic;
  33. data_o : out std_logic_vector(0 TO 127); -- data output
  34. valid_o : out std_logic; -- output data valid flag
  35. accept_i : in std_logic
  36. );
  37. end entity aes_dec;
  38. architecture rtl of aes_dec is
  39. -- Fixed round keys for verification until key schedule is implemented
  40. type t_key_array is array (11 downto 1) of t_key;
  41. constant c_round_keys : t_key_array := (
  42. (x"2b7e1516", x"28aed2a6", x"abf71588", x"09cf4f3c"),
  43. (x"a0fafe17", x"88542cb1", x"23a33939", x"2a6c7605"),
  44. (x"f2c295f2", x"7a96b943", x"5935807a", x"7359f67f"),
  45. (x"3d80477d", x"4716fe3e", x"1e237e44", x"6d7a883b"),
  46. (x"ef44a541", x"a8525b7f", x"b671253b", x"db0bad00"),
  47. (x"d4d1c6f8", x"7c839d87", x"caf2b8bc", x"11f915bc"),
  48. (x"6d88a37a", x"110b3efd", x"dbf98641", x"ca0093fd"),
  49. (x"4e54f70e", x"5f5fc9f3", x"84a64fb2", x"4ea6dc4f"),
  50. (x"ead27321", x"b58dbad2", x"312bf560", x"7f8d292f"),
  51. (x"ac7766f3", x"19fadc21", x"28d12941", x"575c006e"),
  52. (x"d014f9a8", x"c9ee2589", x"e13f0cc8", x"b6630ca6")
  53. );
  54. signal s_round_key : t_key := (others => (others => '0'));
  55. begin
  56. IterG : if design_type = "ITER" generate
  57. signal s_round : natural range t_rounds'low to t_rounds'high+1;
  58. begin
  59. s_round_key <= c_round_keys(s_round) when s_round >= 1 and s_round <= 11 else
  60. (others => (others => '0'));
  61. DeCryptP : process (reset_i, clk_i) is
  62. variable v_state : t_datatable2d;
  63. begin
  64. if (reset_i = '0') then
  65. v_state := (others => (others => (others => '0')));
  66. s_round <= 0;
  67. accept_o <= '0';
  68. data_o <= (others => '0');
  69. valid_o <= '0';
  70. elsif (rising_edge(clk_i)) then
  71. case s_round is
  72. when 0 =>
  73. accept_o <= '1';
  74. if (accept_o = '1' and valid_i = '1') then
  75. accept_o <= '0';
  76. v_state := set_state(data_i);
  77. s_round <= s_round + 1;
  78. end if;
  79. when 1 =>
  80. v_state := addroundkey(v_state, s_round_key);
  81. s_round <= s_round + 1;
  82. when t_rounds'high =>
  83. v_state := invshiftrow(v_state);
  84. v_state := invsubbytes(v_state);
  85. v_state := addroundkey(v_state, s_round_key);
  86. s_round <= s_round + 1;
  87. when t_rounds'high+1 =>
  88. valid_o <= '1';
  89. data_o <= get_state(v_state);
  90. if (valid_o = '1' and accept_i = '1') then
  91. valid_o <= '0';
  92. data_o <= (others => '0');
  93. s_round <= 0;
  94. end if;
  95. when others =>
  96. v_state := invshiftrow(v_state);
  97. v_state := invsubbytes(v_state);
  98. v_state := addroundkey(v_state, s_round_key);
  99. v_state := invmixcolumns(v_state);
  100. s_round <= s_round + 1;
  101. end case;
  102. end if;
  103. end process DeCryptP;
  104. end generate IterG;
  105. end architecture rtl;