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.

164 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. clk_i : IN std_logic; -- clock
  43. mode_i : IN std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  44. key_i : IN std_logic_vector(0 TO 63); -- key input
  45. data_i : IN std_logic_vector(0 TO 63); -- data input
  46. valid_i : IN std_logic; -- input key/data valid flag
  47. data_o : OUT std_logic_vector(0 TO 63); -- data output
  48. valid_o : OUT std_logic -- output data valid flag
  49. );
  50. end component des;
  51. signal s_ready : std_logic;
  52. signal s_reset : 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_reset <= '0';
  77. s_mode <= '0';
  78. s_key1 <= (others => '0');
  79. s_key2 <= (others => '0');
  80. s_key3 <= (others => '0');
  81. elsif(rising_edge(clk_i)) then
  82. s_reset <= reset_i;
  83. if(valid_i = '1' and s_ready = '1') then
  84. s_mode <= mode_i;
  85. s_key1 <= key1_i;
  86. s_key2 <= key2_i;
  87. s_key3 <= key3_i;
  88. end if;
  89. end if;
  90. end process inputregister;
  91. outputregister : process(clk_i, reset_i) is
  92. begin
  93. if(reset_i = '0') then
  94. s_ready <= '0';
  95. elsif(rising_edge(clk_i)) then
  96. if(valid_i = '1' and s_ready = '1') then
  97. s_ready <= '0';
  98. end if;
  99. if(s_des3_validout = '1' or (reset_i = '1' and s_reset = '0')) then
  100. s_ready <= '1';
  101. end if;
  102. end if;
  103. end process outputregister;
  104. i1_des : des
  105. port map (
  106. clk_i => clk_i,
  107. mode_i => mode_i,
  108. key_i => s_des1_key,
  109. data_i => data_i,
  110. valid_i => s_des1_validin,
  111. data_o => s_des1_dataout,
  112. valid_o => s_des1_validout
  113. );
  114. i2_des : des
  115. port map (
  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. clk_i => clk_i,
  127. mode_i => s_mode,
  128. key_i => s_des3_key,
  129. data_i => s_des2_dataout,
  130. valid_i => s_des2_validout,
  131. data_o => data_o,
  132. valid_o => s_des3_validout
  133. );
  134. end architecture rtl;