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.

168 lines
5.3 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. use std.env.all;
  22. entity tb_tdes is
  23. end entity tb_tdes;
  24. architecture rtl of tb_tdes is
  25. type t_array is array (natural range <>) of std_logic_vector(0 to 63);
  26. constant c_table_test_plain : t_array(0 to 18) :=
  27. (x"01A1D6D039776742", x"5CD54CA83DEF57DA", x"0248D43806F67172",
  28. x"51454B582DDF440A", x"42FD443059577FA2", x"059B5E0851CF143A",
  29. x"0756D8E0774761D2", x"762514B829BF486A", x"3BDD119049372802",
  30. x"26955F6835AF609A", x"164D5E404F275232", x"6B056E18759F5CCA",
  31. x"004BD6EF09176062", x"480D39006EE762F2", x"437540C8698F3CFA",
  32. x"072D43A077075292", x"02FE55778117F12A", x"1D9D5C5018F728C2",
  33. x"305532286D6F295A");
  34. signal s_tdes_answers : t_array(0 to 19);
  35. signal s_reset : std_logic := '0';
  36. signal s_clk : std_logic := '0';
  37. signal s_mode : std_logic := '0';
  38. signal s_key1 : std_logic_vector(0 to 63) := (others => '0');
  39. signal s_key2 : std_logic_vector(0 to 63) := (others => '0');
  40. signal s_key3 : std_logic_vector(0 to 63) := (others => '0');
  41. signal s_datain : std_logic_vector(0 to 63) := (others => '0');
  42. signal s_validin : std_logic := '0';
  43. signal s_acceptin : std_logic;
  44. signal s_dataout : std_logic_vector(0 to 63);
  45. signal s_validout : std_logic := '0';
  46. signal s_acceptout : std_logic := '0';
  47. begin
  48. s_reset <= '1' after 100 ns;
  49. s_clk <= not(s_clk) after 10 ns;
  50. teststimuliP : process is
  51. begin
  52. s_mode <= '0';
  53. s_validin <= '0';
  54. s_key1 <= (others => '0');
  55. s_key2 <= (others => '0');
  56. s_key3 <= (others => '0');
  57. s_datain <= (others => '0');
  58. wait until s_reset = '1';
  59. -- ENCRYPTION TESTS
  60. -- cbc known answers test
  61. for index in c_table_test_plain'range loop
  62. wait until rising_edge(s_clk);
  63. s_key1 <= x"1111111111111111";
  64. s_key2 <= x"5555555555555555";
  65. s_key3 <= x"9999999999999999";
  66. s_validin <= '1';
  67. s_datain <= c_table_test_plain(index);
  68. wait until s_acceptin = '1' and rising_edge(s_clk);
  69. s_validin <= '0';
  70. end loop;
  71. wait until rising_edge(s_clk);
  72. s_mode <= '0';
  73. s_validin <= '0';
  74. s_key1 <= (others => '0');
  75. s_key2 <= (others => '0');
  76. s_key3 <= (others => '0');
  77. s_datain <= (others => '0');
  78. wait for 1 us;
  79. -- DECRYPTION TESTS
  80. -- cbc known answer test
  81. for index in c_table_test_plain'range loop
  82. wait until rising_edge(s_clk);
  83. s_key1 <= x"1111111111111111";
  84. s_key2 <= x"5555555555555555";
  85. s_key3 <= x"9999999999999999";
  86. s_mode <= '1';
  87. s_validin <= '1';
  88. s_datain <= s_tdes_answers(index);
  89. wait until s_acceptin = '1' and rising_edge(s_clk);
  90. s_validin <= '0';
  91. s_mode <= '0';
  92. end loop;
  93. wait until rising_edge(s_clk);
  94. s_mode <= '0';
  95. s_validin <= '0';
  96. s_key1 <= (others => '0');
  97. s_key2 <= (others => '0');
  98. s_key3 <= (others => '0');
  99. s_datain <= (others => '0');
  100. wait;
  101. end process teststimuliP;
  102. testcheckerP : process is
  103. begin
  104. s_acceptout <= '1';
  105. report "# ENCRYPTION";
  106. for index in c_table_test_plain'range loop
  107. wait until rising_edge(s_clk) and s_validout = '1';
  108. s_tdes_answers(index) <= s_dataout;
  109. report "TDES enc: 0x" & to_hstring(c_table_test_plain(index)) & " -> " & to_hstring(s_dataout);
  110. end loop;
  111. report "# DECRYPTION TESTS";
  112. report "# tdes known answer test";
  113. for index in c_table_test_plain'range loop
  114. wait until rising_edge(s_clk) and s_validout = '1';
  115. report "TDES dec: 0x" & to_hstring(s_tdes_answers(index)) & " -> " & to_hstring(s_dataout);
  116. assert (s_dataout = c_table_test_plain(index))
  117. report "decryption error"
  118. severity error;
  119. end loop;
  120. report "# Successfully passed all tests";
  121. wait for 10 us;
  122. stop(0);
  123. end process testcheckerP;
  124. i_tdes : entity work.tdes
  125. port map (
  126. reset_i => s_reset,
  127. clk_i => s_clk,
  128. mode_i => s_mode,
  129. key1_i => s_key1,
  130. key2_i => s_key2,
  131. key3_i => s_key3,
  132. data_i => s_datain,
  133. valid_i => s_validin,
  134. accept_o => s_acceptin,
  135. data_o => s_dataout,
  136. valid_o => s_validout,
  137. accept_i => s_acceptout
  138. );
  139. end architecture rtl;