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.

165 lines
5.0 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_mode : std_logic;
  54. signal s_des2_mode : std_logic;
  55. signal s_des1_validin : std_logic := '0';
  56. signal s_des1_validout : std_logic;
  57. signal s_des2_validout : std_logic;
  58. signal s_des3_validout : std_logic;
  59. signal s_key1 : std_logic_vector(0 to 63);
  60. signal s_key2 : std_logic_vector(0 to 63);
  61. signal s_key3 : std_logic_vector(0 to 63);
  62. signal s_des1_key : std_logic_vector(0 to 63);
  63. signal s_des3_key : std_logic_vector(0 to 63);
  64. signal s_des1_dataout : std_logic_vector(0 to 63);
  65. signal s_des2_dataout : std_logic_vector(0 to 63);
  66. begin
  67. ready_o <= s_ready;
  68. valid_o <= s_des3_validout;
  69. s_des2_mode <= not(s_mode);
  70. s_des1_validin <= valid_i and s_ready;
  71. s_des1_key <= key1_i when mode_i = '0' else key3_i;
  72. s_des3_key <= s_key3 when s_mode = '0' else s_key1;
  73. inputregister : process(clk_i, reset_i) is
  74. begin
  75. if(reset_i = '0') then
  76. s_mode <= '0';
  77. s_key1 <= (others => '0');
  78. s_key2 <= (others => '0');
  79. s_key3 <= (others => '0');
  80. elsif(rising_edge(clk_i)) then
  81. if(valid_i = '1' and s_ready = '1') then
  82. s_mode <= mode_i;
  83. s_key1 <= key1_i;
  84. s_key2 <= key2_i;
  85. s_key3 <= key3_i;
  86. end if;
  87. end if;
  88. end process inputregister;
  89. outputregister : process(clk_i, reset_i) is
  90. begin
  91. if(reset_i = '0') then
  92. s_ready <= '1';
  93. elsif(rising_edge(clk_i)) then
  94. if(valid_i = '1' and s_ready = '1') then
  95. s_ready <= '0';
  96. end if;
  97. if(s_des3_validout = '1') then
  98. s_ready <= '1';
  99. end if;
  100. end if;
  101. end process outputregister;
  102. i1_des : des
  103. port map (
  104. reset_i => reset_i,
  105. clk_i => clk_i,
  106. mode_i => mode_i,
  107. key_i => s_des1_key,
  108. data_i => data_i,
  109. valid_i => s_des1_validin,
  110. data_o => s_des1_dataout,
  111. valid_o => s_des1_validout
  112. );
  113. i2_des : des
  114. port map (
  115. reset_i => reset_i,
  116. clk_i => clk_i,
  117. mode_i => s_des2_mode,
  118. key_i => s_key2,
  119. data_i => s_des1_dataout,
  120. valid_i => s_des1_validout,
  121. data_o => s_des2_dataout,
  122. valid_o => s_des2_validout
  123. );
  124. i3_des : des
  125. port map (
  126. reset_i => reset_i,
  127. clk_i => clk_i,
  128. mode_i => s_mode,
  129. key_i => s_des3_key,
  130. data_i => s_des2_dataout,
  131. valid_i => s_des2_validout,
  132. data_o => data_o,
  133. valid_o => s_des3_validout
  134. );
  135. end architecture rtl;