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.

158 lines
5.0 KiB

  1. -- ======================================================================
  2. -- DES 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. -- the testvectors in this file are taken from the project
  18. -- "DES/Triple DES IP Cores" from Rudolf Usselmann, to find under
  19. -- http://opencores.org/project,des
  20. -- Copyright (C) 2001 Rudolf Usselmann (rudi@asics.ws)
  21. -- ======================================================================
  22. -- Revision 1.0 2011/09/17
  23. -- Initial release
  24. library ieee;
  25. use ieee.std_logic_1164.all;
  26. use ieee.numeric_std.ALL;
  27. entity tb_des is
  28. end entity tb_des;
  29. architecture rtl of tb_des is
  30. type t_array is array (0 to 18) of std_logic_vector(0 to 63);
  31. signal s_key_values : t_array :=
  32. (x"7CA110454A1A6E57", x"0131D9619DC1376E", x"07A1133E4A0B2686",
  33. x"3849674C2602319E", x"04B915BA43FEB5B6", x"0113B970FD34F2CE",
  34. x"0170F175468FB5E6", x"43297FAD38E373FE", x"07A7137045DA2A16",
  35. x"04689104C2FD3B2F", x"37D06BB516CB7546", x"1F08260D1AC2465E",
  36. x"584023641ABA6176", x"025816164629B007", x"49793EBC79B3258F",
  37. x"4FB05E1515AB73A7", x"49E95D6D4CA229BF", x"018310DC409B26D6",
  38. x"1C587F1C13924FEF");
  39. signal s_plain_values : t_array :=
  40. (x"01A1D6D039776742", x"5CD54CA83DEF57DA", x"0248D43806F67172",
  41. x"51454B582DDF440A", x"42FD443059577FA2", x"059B5E0851CF143A",
  42. x"0756D8E0774761D2", x"762514B829BF486A", x"3BDD119049372802",
  43. x"26955F6835AF609A", x"164D5E404F275232", x"6B056E18759F5CCA",
  44. x"004BD6EF09176062", x"480D39006EE762F2", x"437540C8698F3CFA",
  45. x"072D43A077075292", x"02FE55778117F12A", x"1D9D5C5018F728C2",
  46. x"305532286D6F295A");
  47. signal s_crypt_values : t_array :=
  48. (x"690F5B0D9A26939B", x"7A389D10354BD271", x"868EBB51CAB4599A",
  49. x"7178876E01F19B2A", x"AF37FB421F8C4095", x"86A560F10EC6D85B",
  50. x"0CD3DA020021DC09", x"EA676B2CB7DB2B7A", x"DFD64A815CAF1A0F",
  51. x"5C513C9C4886C088", x"0A2AEEAE3FF4AB77", x"EF1BF03E5DFA575A",
  52. x"88BF0DB6D70DEE56", x"A1F9915541020B56", x"6FBF1CAFCFFD0556",
  53. x"2F22E49BAB7CA1AC", x"5A6B612CC26CCE4A", x"5F4C038ED12B2E41",
  54. x"63FAC0D034D9F793");
  55. signal s_clk : std_logic := '0';
  56. signal s_mode : std_logic := '0';
  57. signal s_key : std_logic_vector(0 to 63) := (others => '0');
  58. signal s_datain : std_logic_vector(0 to 63) := (others => '0');
  59. signal s_validin : std_logic := '0';
  60. signal s_dataout : std_logic_vector(0 to 63);
  61. signal s_validout : std_logic;
  62. component des is
  63. port (
  64. clk_i : in std_logic;
  65. mode_i : in std_logic;
  66. key_i : in std_logic_vector(0 TO 63);
  67. data_i : in std_logic_vector(0 TO 63);
  68. valid_i : in std_logic;
  69. data_o : out std_logic_vector(0 TO 63);
  70. valid_o : out std_logic
  71. );
  72. end component des;
  73. begin
  74. s_clk <= not(s_clk) after 10 ns;
  75. teststimuliP : process is
  76. begin
  77. report "# encryption test";
  78. for index in 0 to 18 loop
  79. wait until rising_edge(s_clk);
  80. s_mode <= '0';
  81. s_validin <= '1';
  82. s_key <= s_key_values(index);
  83. s_datain <= s_plain_values(index);
  84. end loop;
  85. wait until rising_edge(s_clk);
  86. s_validin <= '0';
  87. wait for 100 ns;
  88. report "# decryption test";
  89. for index in 0 to 18 loop
  90. wait until rising_edge(s_clk);
  91. s_mode <= '1';
  92. s_validin <= '1';
  93. s_key <= s_key_values(index);
  94. s_datain <= s_crypt_values(index);
  95. end loop;
  96. wait until rising_edge(s_clk);
  97. s_mode <= '0';
  98. s_validin <= '0';
  99. wait;
  100. end process teststimuliP;
  101. testcheckerP : process is
  102. begin
  103. for index in 0 to 18 loop
  104. wait until rising_edge(s_clk) and s_validout = '1';
  105. if(s_dataout /= s_crypt_values(index)) then
  106. report "encryption error";
  107. end if;
  108. end loop;
  109. for index in 0 to 18 loop
  110. wait until rising_edge(s_clk) and s_validout = '1';
  111. if(s_dataout /= s_plain_values(index)) then
  112. report "decryption error";
  113. end if;
  114. end loop;
  115. wait;
  116. end process testcheckerP;
  117. i_des : des
  118. port map (
  119. clk_i => s_clk,
  120. mode_i => s_mode,
  121. key_i => s_key,
  122. data_i => s_datain,
  123. valid_i => s_validin,
  124. data_o => s_dataout,
  125. valid_o => s_validout
  126. );
  127. end architecture rtl;