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.

168 lines
5.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. -- Revision 0.1 2011/10/08
  19. -- Initial release
  20. library ieee;
  21. use ieee.std_logic_1164.all;
  22. use ieee.numeric_std.all;
  23. use work.des_pkg.all;
  24. entity tdes is
  25. port (
  26. reset_i : in std_logic; -- async reset
  27. clk_i : in std_logic; -- clock
  28. mode_i : in std_logic; -- tdes-modus: 0 = encrypt, 1 = decrypt
  29. key1_i : in std_logic_vector(0 TO 63); -- key input
  30. key2_i : in std_logic_vector(0 TO 63); -- key input
  31. key3_i : in std_logic_vector(0 TO 63); -- key input
  32. data_i : in std_logic_vector(0 TO 63); -- data input
  33. valid_i : in std_logic; -- input key/data valid flag
  34. data_o : out std_logic_vector(0 TO 63); -- data output
  35. valid_o : out std_logic; -- output data valid flag
  36. ready_o : out std_logic
  37. );
  38. end entity tdes;
  39. architecture rtl of tdes is
  40. component des is
  41. port (
  42. reset_i : in std_logic;
  43. clk_i : IN std_logic; -- clock
  44. mode_i : IN std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  45. key_i : IN std_logic_vector(0 TO 63); -- key input
  46. data_i : IN std_logic_vector(0 TO 63); -- data input
  47. valid_i : IN std_logic; -- input key/data valid flag
  48. data_o : OUT std_logic_vector(0 TO 63); -- data output
  49. valid_o : OUT std_logic -- output data valid flag
  50. );
  51. end component des;
  52. signal s_ready : std_logic;
  53. signal s_reset : std_logic;
  54. signal s_mode : std_logic;
  55. signal s_des2_mode : std_logic;
  56. signal s_des1_validin : std_logic := '0';
  57. signal s_des1_validout : std_logic;
  58. signal s_des2_validout : std_logic;
  59. signal s_des3_validout : std_logic;
  60. signal s_key1 : std_logic_vector(0 to 63);
  61. signal s_key2 : std_logic_vector(0 to 63);
  62. signal s_key3 : std_logic_vector(0 to 63);
  63. signal s_des1_key : std_logic_vector(0 to 63);
  64. signal s_des3_key : std_logic_vector(0 to 63);
  65. signal s_des1_dataout : std_logic_vector(0 to 63);
  66. signal s_des2_dataout : std_logic_vector(0 to 63);
  67. begin
  68. ready_o <= s_ready;
  69. valid_o <= s_des3_validout;
  70. s_des2_mode <= not(s_mode);
  71. s_des1_validin <= valid_i and s_ready;
  72. s_des1_key <= key1_i when mode_i = '0' else key3_i;
  73. s_des3_key <= s_key3 when s_mode = '0' else s_key1;
  74. inputregister : process(clk_i, reset_i) is
  75. begin
  76. if(reset_i = '0') then
  77. s_reset <= '0';
  78. s_mode <= '0';
  79. s_key1 <= (others => '0');
  80. s_key2 <= (others => '0');
  81. s_key3 <= (others => '0');
  82. elsif(rising_edge(clk_i)) then
  83. s_reset <= reset_i;
  84. if(valid_i = '1' and s_ready = '1') then
  85. s_mode <= mode_i;
  86. s_key1 <= key1_i;
  87. s_key2 <= key2_i;
  88. s_key3 <= key3_i;
  89. end if;
  90. end if;
  91. end process inputregister;
  92. outputregister : process(clk_i, reset_i) is
  93. begin
  94. if(reset_i = '0') then
  95. s_ready <= '0';
  96. elsif(rising_edge(clk_i)) then
  97. if(valid_i = '1' and s_ready = '1') then
  98. s_ready <= '0';
  99. end if;
  100. if(s_des3_validout = '1' or (reset_i = '1' and s_reset = '0')) then
  101. s_ready <= '1';
  102. end if;
  103. end if;
  104. end process outputregister;
  105. i1_des : des
  106. port map (
  107. reset_i => reset_i,
  108. clk_i => clk_i,
  109. mode_i => mode_i,
  110. key_i => s_des1_key,
  111. data_i => data_i,
  112. valid_i => s_des1_validin,
  113. data_o => s_des1_dataout,
  114. valid_o => s_des1_validout
  115. );
  116. i2_des : des
  117. port map (
  118. reset_i => reset_i,
  119. clk_i => clk_i,
  120. mode_i => s_des2_mode,
  121. key_i => s_key2,
  122. data_i => s_des1_dataout,
  123. valid_i => s_des1_validout,
  124. data_o => s_des2_dataout,
  125. valid_o => s_des2_validout
  126. );
  127. i3_des : des
  128. port map (
  129. reset_i => reset_i,
  130. clk_i => clk_i,
  131. mode_i => s_mode,
  132. key_i => s_des3_key,
  133. data_i => s_des2_dataout,
  134. valid_i => s_des2_validout,
  135. data_o => data_o,
  136. valid_o => s_des3_validout
  137. );
  138. end architecture rtl;