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.

132 lines
4.4 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. accept_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. accept_i : in std_logic
  40. );
  41. end entity cbcdes;
  42. architecture rtl of cbcdes is
  43. signal s_mode : std_logic;
  44. signal s_des_mode : std_logic;
  45. signal s_start : std_logic;
  46. signal s_key : std_logic_vector(0 to 63);
  47. signal s_des_key : std_logic_vector(0 to 63);
  48. signal s_iv : std_logic_vector(0 to 63);
  49. signal s_datain : std_logic_vector(0 to 63);
  50. signal s_datain_d : std_logic_vector(0 to 63);
  51. signal s_des_datain : std_logic_vector(0 to 63);
  52. signal s_des_dataout : std_logic_vector(0 to 63);
  53. signal s_dataout : std_logic_vector(0 to 63);
  54. begin
  55. s_des_datain <= iv_i xor data_i when mode_i = '0' and start_i = '1' else
  56. s_dataout xor data_i when s_mode = '0' and start_i = '0' else
  57. data_i;
  58. data_o <= s_iv xor s_des_dataout when s_mode = '1' and s_start = '1' else
  59. s_datain_d xor s_des_dataout when s_mode = '1' and s_start = '0' else
  60. s_des_dataout;
  61. s_des_key <= key_i when start_i = '1' else s_key;
  62. s_des_mode <= mode_i when start_i = '1' else s_mode;
  63. inputregister : process (clk_i, reset_i) is
  64. begin
  65. if (reset_i = '0') then
  66. s_mode <= '0';
  67. s_start <= '0';
  68. s_key <= (others => '0');
  69. s_iv <= (others => '0');
  70. s_datain <= (others => '0');
  71. s_datain_d <= (others => '0');
  72. elsif (rising_edge(clk_i)) then
  73. if (valid_i = '1' and accept_o = '1') then
  74. s_start <= start_i;
  75. s_datain <= data_i;
  76. s_datain_d <= s_datain;
  77. if (start_i = '1') then
  78. s_mode <= mode_i;
  79. s_key <= key_i;
  80. s_iv <= iv_i;
  81. end if;
  82. end if;
  83. end if;
  84. end process inputregister;
  85. outputregister : process (clk_i, reset_i) is
  86. begin
  87. if (reset_i = '0') then
  88. s_dataout <= (others => '0');
  89. elsif (rising_edge(clk_i)) then
  90. if (valid_o = '1' and accept_i = '1') then
  91. s_dataout <= s_des_dataout;
  92. end if;
  93. end if;
  94. end process outputregister;
  95. i_des : entity work.des
  96. port map (
  97. reset_i => reset_i,
  98. clk_i => clk_i,
  99. mode_i => s_des_mode,
  100. key_i => s_des_key,
  101. data_i => s_des_datain,
  102. valid_i => valid_i,
  103. accept_o => accept_o,
  104. data_o => s_des_dataout,
  105. valid_o => valid_o,
  106. accept_i => accept_i
  107. );
  108. end architecture rtl;