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.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. clk_i : in std_logic; -- clock
  27. mode_i : in std_logic; -- tdes-modus: 0 = encrypt, 1 = decrypt
  28. key1_i : in std_logic_vector(0 TO 63); -- key input
  29. key2_i : in std_logic_vector(0 TO 63); -- key input
  30. key3_i : in std_logic_vector(0 TO 63); -- key input
  31. data_i : in std_logic_vector(0 TO 63); -- data input
  32. valid_i : in std_logic; -- input key/data valid flag
  33. data_o : out std_logic_vector(0 TO 63); -- data output
  34. valid_o : out std_logic; -- output data valid flag
  35. ready_o : out std_logic
  36. );
  37. end entity tdes;
  38. architecture rtl of tdes is
  39. component des is
  40. port (
  41. clk_i : IN std_logic; -- clock
  42. mode_i : IN std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  43. key_i : IN std_logic_vector(0 TO 63); -- key input
  44. data_i : IN std_logic_vector(0 TO 63); -- data input
  45. valid_i : IN std_logic; -- input key/data valid flag
  46. data_o : OUT std_logic_vector(0 TO 63); -- data output
  47. valid_o : OUT std_logic -- output data valid flag
  48. );
  49. end component des;
  50. begin
  51. s_des2_mode <= not(s_mode);
  52. s_des1_validin <= valid_i and s_ready;
  53. inputregister : process(clk_i, reset_i) is
  54. begin
  55. if(reset_i = '0') then
  56. s_reset <= '0';
  57. s_mode <= '0';
  58. s_des2_key <= (others => '0');
  59. s_des3_key <= (others => '0');
  60. elsif(rising_edge(clk_i)) then
  61. s_reset <= reset_i;
  62. if(valid_i = '1' and s_ready = '1' and start_i = '1') then
  63. s_mode <= mode_i;
  64. s_des2_key <= key2_i;
  65. s_des3_key <= key3_i;
  66. end if;
  67. end if;
  68. end process inputregister;
  69. outputregister : process(clk_i, reset_i) is
  70. begin
  71. if(reset_i = '0') then
  72. s_ready <= '0';
  73. elsif(rising_edge(clk_i)) then
  74. if(valid_i = '1' and s_ready = '1') then
  75. s_ready <= '0';
  76. end if;
  77. if(s_validout = '1' or (reset_i = '1' and s_reset = '0')) then
  78. s_ready <= '1';
  79. end if;
  80. end if;
  81. end process outputregister;
  82. i1_des : des
  83. port map (
  84. clk_i => clk_i,
  85. mode_i => mode_i,
  86. key_i => key1_i,
  87. data_i => data_i,
  88. valid_i => s_des1_valid,
  89. data_o => s_des1_dataout,
  90. valid_o => s_des1_validout
  91. );
  92. i2_des : des
  93. port map (
  94. clk_i => clk_i,
  95. mode_i => s_des2_mode,
  96. key_i => s_des2_key,
  97. data_i => s_des1_dataout,
  98. valid_i => s_des1_validout,
  99. data_o => s_des2_dataout,
  100. valid_o => s_des2_validout
  101. );
  102. i3_des : des
  103. port map (
  104. clk_i => clk_i,
  105. mode_i => s_mode,
  106. key_i => s_des3_key,
  107. data_i => s_des2_dataout,
  108. valid_i => s_des2_validout,
  109. data_o => data_o,
  110. valid_o => s_des3_validout
  111. );
  112. end architecture rtl;