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.

162 lines
5.1 KiB

  1. -- ======================================================================
  2. -- TDES 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_tdes is
  22. end entity tb_tdes;
  23. architecture rtl of tb_tdes 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_key1 : std_logic_vector(0 to 63) := (others => '0');
  38. signal s_key2 : std_logic_vector(0 to 63) := (others => '0');
  39. signal s_key3 : std_logic_vector(0 to 63) := (others => '0');
  40. signal s_datain : std_logic_vector(0 to 63) := (others => '0');
  41. signal s_validin : std_logic := '0';
  42. signal s_acceptin : std_logic;
  43. signal s_dataout : std_logic_vector(0 to 63);
  44. signal s_validout : std_logic := '0';
  45. signal s_acceptout : std_logic := '0';
  46. begin
  47. s_reset <= '1' after 100 ns;
  48. s_clk <= not(s_clk) after 10 ns;
  49. teststimuliP : process is
  50. begin
  51. s_mode <= '0';
  52. s_validin <= '0';
  53. s_key1 <= (others => '0');
  54. s_key2 <= (others => '0');
  55. s_key3 <= (others => '0');
  56. s_datain <= (others => '0');
  57. wait until s_reset = '1';
  58. -- ENCRYPTION TESTS
  59. -- cbc known answers test
  60. for index in c_table_test_plain'range loop
  61. wait until rising_edge(s_clk);
  62. s_key1 <= x"1111111111111111";
  63. s_key2 <= x"5555555555555555";
  64. s_key3 <= x"9999999999999999";
  65. s_validin <= '1';
  66. s_datain <= c_table_test_plain(index);
  67. wait until s_acceptin = '1' and rising_edge(s_clk);
  68. s_validin <= '0';
  69. end loop;
  70. wait until rising_edge(s_clk);
  71. s_mode <= '0';
  72. s_validin <= '0';
  73. s_key1 <= (others => '0');
  74. s_key2 <= (others => '0');
  75. s_key3 <= (others => '0');
  76. s_datain <= (others => '0');
  77. wait for 1 us;
  78. -- DECRYPTION TESTS
  79. -- cbc known answer test
  80. for index in c_table_test_plain'range loop
  81. wait until rising_edge(s_clk);
  82. s_key1 <= x"1111111111111111";
  83. s_key2 <= x"5555555555555555";
  84. s_key3 <= x"9999999999999999";
  85. s_mode <= '1';
  86. s_validin <= '1';
  87. s_datain <= s_tdes_answers(index);
  88. wait until s_acceptin = '1' and rising_edge(s_clk);
  89. s_validin <= '0';
  90. s_mode <= '0';
  91. end loop;
  92. wait until rising_edge(s_clk);
  93. s_mode <= '0';
  94. s_validin <= '0';
  95. s_key1 <= (others => '0');
  96. s_key2 <= (others => '0');
  97. s_key3 <= (others => '0');
  98. s_datain <= (others => '0');
  99. wait;
  100. end process teststimuliP;
  101. testcheckerP : process is
  102. begin
  103. s_acceptout <= '1';
  104. report "# ENCRYPTION TESTS";
  105. for index in c_table_test_plain'range loop
  106. wait until rising_edge(s_clk) and s_validout = '1';
  107. s_tdes_answers(index) <= s_dataout;
  108. end loop;
  109. report "# DECRYPTION TESTS";
  110. report "# tdes known answer test";
  111. for index in c_table_test_plain'range loop
  112. wait until rising_edge(s_clk) and s_validout = '1';
  113. assert (s_dataout = c_table_test_plain(index))
  114. report "decryption error"
  115. severity error;
  116. end loop;
  117. report "# Successfully passed all tests";
  118. wait;
  119. end process testcheckerP;
  120. i_tdes : entity work.tdes
  121. port map (
  122. reset_i => s_reset,
  123. clk_i => s_clk,
  124. mode_i => s_mode,
  125. key1_i => s_key1,
  126. key2_i => s_key2,
  127. key3_i => s_key3,
  128. data_i => s_datain,
  129. valid_i => s_validin,
  130. accept_o => s_acceptin,
  131. data_o => s_dataout,
  132. valid_o => s_validout,
  133. accept_i => s_acceptout
  134. );
  135. end architecture rtl;