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.

192 lines
6.0 KiB

  1. -- ======================================================================
  2. -- CBC-DES encryption/decryption testbench
  3. -- tests according to NIST 800-17 special publication
  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. library ieee;
  19. use ieee.std_logic_1164.all;
  20. use ieee.numeric_std.all;
  21. entity tb_cbctdes is
  22. end entity tb_cbctdes;
  23. architecture rtl of tb_cbctdes is
  24. type t_array is array (natural range <>) of std_logic_vector(0 to 63);
  25. constant c_table_test_plain : t_array(0 to 18) :=
  26. (x"01A1D6D039776742", x"5CD54CA83DEF57DA", x"0248D43806F67172",
  27. x"51454B582DDF440A", x"42FD443059577FA2", x"059B5E0851CF143A",
  28. x"0756D8E0774761D2", x"762514B829BF486A", x"3BDD119049372802",
  29. x"26955F6835AF609A", x"164D5E404F275232", x"6B056E18759F5CCA",
  30. x"004BD6EF09176062", x"480D39006EE762F2", x"437540C8698F3CFA",
  31. x"072D43A077075292", x"02FE55778117F12A", x"1D9D5C5018F728C2",
  32. x"305532286D6F295A");
  33. signal s_tdes_answers : t_array(0 to 19);
  34. signal s_reset : std_logic := '0';
  35. signal s_clk : std_logic := '0';
  36. signal s_mode : std_logic := '0';
  37. signal s_start : std_logic := '0';
  38. signal s_iv : std_logic_vector(0 to 63) := (others => '0');
  39. signal s_key1 : std_logic_vector(0 to 63) := (others => '0');
  40. signal s_key2 : std_logic_vector(0 to 63) := (others => '0');
  41. signal s_key3 : std_logic_vector(0 to 63) := (others => '0');
  42. signal s_datain : std_logic_vector(0 to 63) := (others => '0');
  43. signal s_validin : std_logic := '0';
  44. signal s_ready : std_logic := '0';
  45. signal s_dataout : std_logic_vector(0 to 63);
  46. signal s_validout : std_logic := '0';
  47. component cbctdes is
  48. port (
  49. reset_i : in std_logic;
  50. clk_i : in std_logic;
  51. mode_i : in std_logic;
  52. start_i : in std_logic;
  53. iv_i : in std_logic_vector(0 to 63);
  54. key1_i : in std_logic_vector(0 to 63);
  55. key2_i : in std_logic_vector(0 TO 63);
  56. key3_i : in std_logic_vector(0 TO 63);
  57. data_i : in std_logic_vector(0 TO 63);
  58. valid_i : in std_logic;
  59. data_o : out std_logic_vector(0 TO 63);
  60. valid_o : out std_logic;
  61. ready_o : out std_logic
  62. );
  63. end component cbctdes;
  64. begin
  65. s_reset <= '1' after 100 ns;
  66. s_clk <= not(s_clk) after 10 ns;
  67. teststimuliP : process is
  68. begin
  69. s_start <= '0';
  70. s_mode <= '0';
  71. s_validin <= '0';
  72. s_iv <= (others => '0');
  73. s_key1 <= (others => '0');
  74. s_key2 <= (others => '0');
  75. s_key3 <= (others => '0');
  76. s_datain <= (others => '0');
  77. wait until s_reset = '1';
  78. -- ENCRYPTION TESTS
  79. -- cbc known answers test
  80. for index in c_table_test_plain'range loop
  81. wait until rising_edge(s_clk) and s_ready = '1';
  82. s_key1 <= x"1111111111111111";
  83. s_key2 <= x"5555555555555555";
  84. s_key3 <= x"9999999999999999";
  85. s_validin <= '1';
  86. s_datain <= c_table_test_plain(index);
  87. if(index = 0) then
  88. s_start <= '1';
  89. end if;
  90. wait until rising_edge(s_clk);
  91. s_validin <= '0';
  92. s_start <= '0';
  93. end loop;
  94. wait until rising_edge(s_clk);
  95. s_mode <= '0';
  96. s_start <= '0';
  97. s_validin <= '0';
  98. s_key1 <= (others => '0');
  99. s_key2 <= (others => '0');
  100. s_key3 <= (others => '0');
  101. s_datain <= (others => '0');
  102. wait for 1 us;
  103. -- DECRYPTION TESTS
  104. -- cbc known answer test
  105. for index in c_table_test_plain'range loop
  106. wait until rising_edge(s_clk) and s_ready = '1';
  107. s_key1 <= x"1111111111111111";
  108. s_key2 <= x"5555555555555555";
  109. s_key3 <= x"9999999999999999";
  110. s_mode <= '1';
  111. s_validin <= '1';
  112. s_datain <= s_tdes_answers(index);
  113. if(index = 0) then
  114. s_start <= '1';
  115. end if;
  116. wait until rising_edge(s_clk);
  117. s_start <= '0';
  118. s_validin <= '0';
  119. s_mode <= '0';
  120. end loop;
  121. wait until rising_edge(s_clk);
  122. s_mode <= '0';
  123. s_start <= '0';
  124. s_validin <= '0';
  125. s_key1 <= (others => '0');
  126. s_key2 <= (others => '0');
  127. s_key3 <= (others => '0');
  128. s_datain <= (others => '0');
  129. wait;
  130. end process teststimuliP;
  131. testcheckerP : process is
  132. begin
  133. report "# ENCRYPTION TESTS";
  134. for index in c_table_test_plain'range loop
  135. wait until rising_edge(s_clk) and s_validout = '1';
  136. s_tdes_answers(index) <= s_dataout;
  137. end loop;
  138. report "# DECRYPTION TESTS";
  139. report "# tdes known answer test";
  140. for index in c_table_test_plain'range loop
  141. wait until rising_edge(s_clk) and s_validout = '1';
  142. assert (s_dataout = c_table_test_plain(index))
  143. report "decryption error"
  144. severity error;
  145. end loop;
  146. report "# Successfully passed all tests";
  147. wait;
  148. end process testcheckerP;
  149. i_cbctdes : cbctdes
  150. port map (
  151. reset_i => s_reset,
  152. clk_i => s_clk,
  153. start_i => s_start,
  154. mode_i => s_mode,
  155. iv_i => s_iv,
  156. key1_i => s_key1,
  157. key2_i => s_key2,
  158. key3_i => s_key3,
  159. data_i => s_datain,
  160. valid_i => s_validin,
  161. data_o => s_dataout,
  162. valid_o => s_validout,
  163. ready_o => s_ready
  164. );
  165. end architecture rtl;