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.

155 lines
5.3 KiB

  1. -- ======================================================================
  2. -- CBC-DES encryption/decryption
  3. -- algorithm according to FIPS 46-3 specification
  4. -- Copyright (C) 2007 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/09/23
  19. -- Initial release, incomplete and may contain bugs
  20. -- Revision 0.2 2011/10/06
  21. -- corrected some bugs which were found while testing cbc ability
  22. library ieee;
  23. use ieee.std_logic_1164.all;
  24. use ieee.numeric_std.all;
  25. use work.des_pkg.all;
  26. entity cbcdes is
  27. port (
  28. reset_i : in std_logic; -- low active async reset
  29. clk_i : in std_logic; -- clock
  30. start_i : in std_logic; -- start cbc
  31. mode_i : in std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  32. key_i : in std_logic_vector(0 TO 63); -- key input
  33. iv_i : in std_logic_vector(0 to 63); -- iv input
  34. data_i : in std_logic_vector(0 TO 63); -- data input
  35. valid_i : in std_logic; -- input key/data valid flag
  36. ready_o : out std_logic; -- ready to encrypt/decrypt
  37. data_o : out std_logic_vector(0 TO 63); -- data output
  38. valid_o : out std_logic -- output data valid flag
  39. );
  40. end entity cbcdes;
  41. architecture rtl of cbcdes is
  42. component des is
  43. port (
  44. clk_i : IN std_logic; -- clock
  45. mode_i : IN std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  46. key_i : IN std_logic_vector(0 TO 63); -- key input
  47. data_i : IN std_logic_vector(0 TO 63); -- data input
  48. valid_i : IN std_logic; -- input key/data valid flag
  49. data_o : OUT std_logic_vector(0 TO 63); -- data output
  50. valid_o : OUT std_logic -- output data valid flag
  51. );
  52. end component des;
  53. signal s_mode : std_logic;
  54. signal s_des_mode : std_logic;
  55. signal s_start : std_logic;
  56. signal s_key : std_logic_vector(0 to 63);
  57. signal s_des_key : std_logic_vector(0 to 63);
  58. signal s_iv : std_logic_vector(0 to 63);
  59. signal s_datain : std_logic_vector(0 to 63);
  60. signal s_datain_d : std_logic_vector(0 to 63);
  61. signal s_des_datain : std_logic_vector(0 to 63);
  62. signal s_validin : std_logic;
  63. signal s_des_dataout : std_logic_vector(0 to 63);
  64. signal s_dataout : std_logic_vector(0 to 63);
  65. signal s_validout : std_logic;
  66. signal s_ready : std_logic;
  67. signal s_reset : std_logic;
  68. begin
  69. s_des_datain <= iv_i xor data_i when mode_i = '0' and start_i = '1' else
  70. s_dataout xor data_i when s_mode = '0' and start_i = '0' else
  71. data_i;
  72. data_o <= s_iv xor s_des_dataout when s_mode = '1' and s_start = '1' else
  73. s_datain_d xor s_des_dataout when s_mode = '1' and s_start = '0' else
  74. s_des_dataout;
  75. s_des_key <= key_i when start_i = '1' else s_key;
  76. s_des_mode <= mode_i when start_i = '1' else s_mode;
  77. ready_o <= s_ready;
  78. s_validin <= valid_i and s_ready;
  79. valid_o <= s_validout;
  80. inputregister : process(clk_i, reset_i) is
  81. begin
  82. if(reset_i = '0') then
  83. s_reset <= '0';
  84. s_mode <= '0';
  85. s_start <= '0';
  86. s_key <= (others => '0');
  87. s_iv <= (others => '0');
  88. s_datain <= (others => '0');
  89. s_datain_d <= (others => '0');
  90. elsif(rising_edge(clk_i)) then
  91. s_reset <= reset_i;
  92. if(valid_i = '1' and s_ready = '1') then
  93. s_start <= start_i;
  94. s_datain <= data_i;
  95. s_datain_d <= s_datain;
  96. end if;
  97. if(valid_i = '1' and s_ready = '1' and start_i = '1') then
  98. s_mode <= mode_i;
  99. s_key <= key_i;
  100. s_iv <= iv_i;
  101. end if;
  102. end if;
  103. end process inputregister;
  104. outputregister : process(clk_i, reset_i) is
  105. begin
  106. if(reset_i = '0') then
  107. s_ready <= '0';
  108. s_dataout <= (others => '0');
  109. elsif(rising_edge(clk_i)) then
  110. if(valid_i = '1' and s_ready = '1') then
  111. s_ready <= '0';
  112. end if;
  113. if(s_validout = '1' or (reset_i = '1' and s_reset = '0')) then
  114. s_ready <= '1';
  115. s_dataout <= s_des_dataout;
  116. end if;
  117. end if;
  118. end process outputregister;
  119. i_des : des
  120. port map (
  121. clk_i => clk_i,
  122. mode_i => s_des_mode,
  123. key_i => s_des_key,
  124. data_i => s_des_datain,
  125. valid_i => s_validin,
  126. data_o => s_des_dataout,
  127. valid_o => s_validout
  128. );
  129. end architecture rtl;