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.

135 lines
4.1 KiB

  1. -- ======================================================================
  2. -- TDES encryption/decryption
  3. -- algorithm according to FIPS 46-3 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.des_pkg.all;
  22. entity tdes is
  23. port (
  24. reset_i : in std_logic; -- async reset
  25. clk_i : in std_logic; -- clock
  26. mode_i : in std_logic; -- tdes-modus: 0 = encrypt, 1 = decrypt
  27. key1_i : in std_logic_vector(0 to 63); -- key input
  28. key2_i : in std_logic_vector(0 to 63); -- key input
  29. key3_i : in std_logic_vector(0 to 63); -- key input
  30. data_i : in std_logic_vector(0 to 63); -- 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 63); -- data output
  34. valid_o : out std_logic; -- output data valid flag
  35. accept_i : in std_logic
  36. );
  37. end entity tdes;
  38. architecture rtl of tdes is
  39. signal s_mode : std_logic;
  40. signal s_des1_validout : std_logic;
  41. signal s_des2_validout : std_logic;
  42. signal s_des2_acceptout : std_logic;
  43. signal s_des3_acceptout : std_logic;
  44. signal s_key1 : std_logic_vector(0 to 63);
  45. signal s_key2 : std_logic_vector(0 to 63);
  46. signal s_key3 : std_logic_vector(0 to 63);
  47. signal s_des1_key : std_logic_vector(0 to 63);
  48. signal s_des3_key : std_logic_vector(0 to 63);
  49. signal s_des1_dataout : std_logic_vector(0 to 63);
  50. signal s_des2_dataout : std_logic_vector(0 to 63);
  51. begin
  52. s_des1_key <= key1_i when mode_i = '0' else key3_i;
  53. s_des3_key <= s_key3 when s_mode = '0' else s_key1;
  54. inputregister : process (clk_i, reset_i) is
  55. begin
  56. if (reset_i = '0') then
  57. s_mode <= '0';
  58. s_key1 <= (others => '0');
  59. s_key2 <= (others => '0');
  60. s_key3 <= (others => '0');
  61. elsif(rising_edge(clk_i)) then
  62. if (valid_i = '1' and accept_o = '1') then
  63. s_mode <= mode_i;
  64. s_key1 <= key1_i;
  65. s_key2 <= key2_i;
  66. s_key3 <= key3_i;
  67. end if;
  68. end if;
  69. end process inputregister;
  70. i1_des : des
  71. port map (
  72. reset_i => reset_i,
  73. clk_i => clk_i,
  74. mode_i => mode_i,
  75. key_i => s_des1_key,
  76. data_i => data_i,
  77. valid_i => valid_i,
  78. accept_o => accept_o,
  79. data_o => s_des1_dataout,
  80. valid_o => s_des1_validout,
  81. accept_i => s_des2_acceptout
  82. );
  83. i2_des : des
  84. port map (
  85. reset_i => reset_i,
  86. clk_i => clk_i,
  87. mode_i => not s_mode,
  88. key_i => s_key2,
  89. data_i => s_des1_dataout,
  90. valid_i => s_des1_validout,
  91. accept_o => s_des2_acceptout,
  92. data_o => s_des2_dataout,
  93. valid_o => s_des2_validout,
  94. accept_i => s_des3_acceptout
  95. );
  96. i3_des : des
  97. port map (
  98. reset_i => reset_i,
  99. clk_i => clk_i,
  100. mode_i => s_mode,
  101. key_i => s_des3_key,
  102. data_i => s_des2_dataout,
  103. valid_i => s_des2_validout,
  104. accept_o => s_des3_acceptout,
  105. data_o => data_o,
  106. valid_o => valid_o,
  107. accept_i => accept_i
  108. );
  109. end architecture rtl;