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.

141 lines
3.9 KiB

  1. -- ======================================================================
  2. -- CBC-MAC-DES testbench
  3. -- Copyright (C) 2015 Torsten Meissner
  4. -------------------------------------------------------------------------
  5. -- This program is free software; you can redistribute it and/or modify
  6. -- it under the terms of the GNU General Public License as published by
  7. -- the Free Software Foundation; either version 2 of the License, or
  8. -- (at your option) any later version.
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. -- GNU General Public License for more details.
  13. -- You should have received a copy of the GNU General Public License
  14. -- along with this program; if not, write to the Free Software
  15. -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. -- ======================================================================
  17. library ieee;
  18. use ieee.std_logic_1164.all;
  19. use ieee.numeric_std.all;
  20. entity tb_cbcmac_des is
  21. end entity tb_cbcmac_des;
  22. architecture sim of tb_cbcmac_des is
  23. type t_array is array (natural range <>) of std_logic_vector(0 to 63);
  24. signal s_reset : std_logic := '0';
  25. signal s_clk : std_logic := '0';
  26. signal s_start : std_logic := '0';
  27. signal s_key : std_logic_vector(0 to 63) := (others => '0');
  28. signal s_datain : std_logic_vector(0 to 63) := (others => '0');
  29. signal s_validin : std_logic := '0';
  30. signal s_acceptout : std_logic;
  31. signal s_dataout : std_logic_vector(0 to 63);
  32. signal s_validout : std_logic;
  33. signal s_acceptin : std_logic;
  34. component cbcmac_des is
  35. port (
  36. reset_i : in std_logic;
  37. clk_i : in std_logic;
  38. start_i : in std_logic;
  39. key_i : in std_logic_vector(0 to 63);
  40. data_i : in std_logic_vector(0 to 63);
  41. valid_i : in std_logic;
  42. accept_o : out std_logic;
  43. data_o : out std_logic_vector(0 to 63);
  44. valid_o : out std_logic;
  45. accept_i : in std_logic
  46. );
  47. end component cbcmac_des;
  48. -- key, plain & crypto stimuli values
  49. -- taken from NIST website:
  50. -- http://csrc.nist.gov/publications/fips/fips113/fips113.html
  51. constant C_KEY : std_logic_vector(0 to 63) := x"0123456789abcdef";
  52. constant C_PLAIN : t_array := (
  53. x"3736353433323120", x"4e6f772069732074",
  54. x"68652074696d6520", x"666f722000000000");
  55. constant C_CRYPT : t_array := (
  56. x"21fb193693a16c28", x"6c463f0cb7167a6f",
  57. x"956ee891e889d91e", x"f1d30f6849312ca4");
  58. begin
  59. s_clk <= not(s_clk) after 10 ns;
  60. s_reset <= '1' after 100 ns;
  61. StimuliP : process is
  62. begin
  63. s_start <= '0';
  64. s_key <= (others => '0');
  65. s_datain <= (others => '0');
  66. s_validin <= '0';
  67. wait until s_reset = '1';
  68. s_start <= '1';
  69. for i in C_PLAIN'range loop
  70. wait until rising_edge(s_clk);
  71. s_validin <= '1';
  72. s_key <= C_KEY;
  73. s_datain <= C_PLAIN(i);
  74. wait until rising_edge(s_clk) and s_acceptout = '1';
  75. s_start <= '0';
  76. s_validin <= '0';
  77. end loop;
  78. wait;
  79. end process StimuliP;
  80. CheckerP : process is
  81. begin
  82. s_acceptin <= '0';
  83. wait until s_reset = '1';
  84. for i in C_CRYPT'range loop
  85. wait until rising_edge(s_clk);
  86. s_acceptin <= '1';
  87. wait until rising_edge(s_clk) and s_validout = '1';
  88. assert s_dataout = C_CRYPT(i)
  89. report "Encryption error"
  90. severity failure;
  91. s_acceptin <= '0';
  92. end loop;
  93. report "CBCMAC test successful :)";
  94. wait;
  95. end process CheckerP;
  96. i_cbcmac_des : cbcmac_des
  97. port map (
  98. reset_i => s_reset,
  99. clk_i => s_clk,
  100. start_i => s_start,
  101. key_i => s_key,
  102. data_i => s_datain,
  103. valid_i => s_validin,
  104. accept_o => s_acceptout,
  105. data_o => s_dataout,
  106. valid_o => s_validout,
  107. accept_i => s_acceptin
  108. );
  109. end architecture sim;