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.

138 lines
4.6 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. library ieee;
  21. use ieee.std_logic_1164.all;
  22. use ieee.numeric_std.all;
  23. use work.des_pkg.all;
  24. entity cbcdes is
  25. port (
  26. reset_i : in std_logic; -- low active async reset
  27. clk_i : in std_logic; -- clock
  28. start_i : in std_logic; -- start cbc
  29. mode_i : in std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  30. key_i : in std_logic_vector(0 TO 63); -- key input
  31. iv_i : in std_logic_vector(0 to 63); -- iv 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. ready_o : out std_logic; -- ready to encrypt/decrypt
  35. data_o : out std_logic_vector(0 TO 63); -- data output
  36. valid_o : out std_logic -- output data valid flag
  37. );
  38. end entity cbcdes;
  39. architecture rtl of cbcdes 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_mode : std_logic;
  52. signal s_start : std_logic;
  53. signal s_iv : std_logic_vector(0 to 63);
  54. signal s_datain : std_logic_vector(0 to 63);
  55. signal s_des_datain : std_logic_vector(0 to 63);
  56. signal s_validin : std_logic;
  57. signal s_dataout : std_logic_vector(0 to 63);
  58. signal s_validout : std_logic;
  59. signal s_ready : std_logic;
  60. signal s_reset : std_logic;
  61. begin
  62. s_des_datain <= iv_i xor data_i when mode_i = '0' and start_i = '1' else
  63. s_dataout xor data_i when mode_i = '0' and start_i = '0' else
  64. data_i;
  65. data_o <= s_iv xor s_dataout when s_mode = '1' and s_start = '1' else
  66. s_datain xor s_dataout when s_mode = '1' and s_start = '0' else
  67. s_dataout;
  68. ready_o <= s_ready;
  69. s_validin <= valid_i and s_ready;
  70. valid_o <= s_validout;
  71. inputregister : process(clk_i, reset_i) is
  72. begin
  73. if(reset_i = '0') then
  74. s_reset <= '0';
  75. s_mode <= '0';
  76. s_start <= '0';
  77. s_iv <= (others => '0');
  78. s_datain <= (others => '0');
  79. elsif(rising_edge(clk_i)) then
  80. s_reset <= reset_i;
  81. if(valid_i = '1' and s_ready = '1') then
  82. s_mode <= mode_i;
  83. s_start <= start_i;
  84. s_iv <= iv_i;
  85. s_datain <= data_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 <= '0';
  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_validout = '1' or (reset_i = '1' and s_reset = '0')) then
  98. s_ready <= '1';
  99. end if;
  100. end if;
  101. end process outputregister;
  102. i_des : des
  103. port map (
  104. clk_i => clk_i,
  105. mode_i => mode_i,
  106. key_i => key_i,
  107. data_i => s_des_datain,
  108. valid_i => s_validin,
  109. data_o => s_dataout,
  110. valid_o => s_validout
  111. );
  112. end architecture rtl;