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.

165 lines
5.3 KiB

  1. -- ======================================================================
  2. -- AES Counter mode testbench
  3. -- Copyright (C) 2020 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. library osvvm;
  21. use osvvm.RandomPkg.all;
  22. use std.env.all;
  23. entity tb_cbcaes is
  24. end entity tb_cbcaes;
  25. architecture sim of tb_cbcaes is
  26. signal s_reset : std_logic := '0';
  27. signal s_clk : std_logic := '0';
  28. signal s_start : std_logic := '0';
  29. signal s_mode : std_logic := '0';
  30. signal s_iv : std_logic_vector(0 to 127) := (others => '0');
  31. signal s_key : std_logic_vector(0 to 127) := (others => '0');
  32. signal s_datain : std_logic_vector(0 to 127) := (others => '0');
  33. signal s_validin : std_logic := '0';
  34. signal s_acceptin : std_logic;
  35. signal s_dataout : std_logic_vector(0 to 127);
  36. signal s_validout : std_logic := '0';
  37. signal s_acceptout : std_logic := '0';
  38. procedure cryptData(datain : in std_logic_vector(0 to 127);
  39. key : in std_logic_vector(0 to 127);
  40. iv : in std_logic_vector(0 to 127);
  41. mode : in boolean;
  42. start : in boolean;
  43. final : in boolean;
  44. dataout : out std_logic_vector(0 to 127);
  45. bytelen : in integer) is
  46. begin
  47. report "VHPIDIRECT cryptData" severity failure;
  48. end procedure;
  49. attribute foreign of cryptData: procedure is "VHPIDIRECT cryptData";
  50. function swap (datain : std_logic_vector(0 to 127)) return std_logic_vector is
  51. variable v_data : std_logic_vector(0 to 127);
  52. begin
  53. for i in 0 to 15 loop
  54. for y in 0 to 7 loop
  55. v_data((i*8)+y) := datain((i*8)+7-y);
  56. end loop;
  57. end loop;
  58. return v_data;
  59. end function;
  60. begin
  61. i_cbcaes : entity work.cbcaes
  62. port map (
  63. reset_i => s_reset,
  64. clk_i => s_clk,
  65. start_i => s_start,
  66. mode_i => s_mode,
  67. key_i => s_key,
  68. iv_i => s_iv,
  69. data_i => s_datain,
  70. valid_i => s_validin,
  71. accept_o => s_acceptin,
  72. data_o => s_dataout,
  73. valid_o => s_validout,
  74. accept_i => s_acceptout
  75. );
  76. s_clk <= not(s_clk) after 10 ns;
  77. s_reset <= '1' after 100 ns;
  78. process is
  79. variable v_key : std_logic_vector(0 to 127);
  80. variable v_iv : std_logic_vector(0 to 127);
  81. variable v_datain : std_logic_vector(0 to 127);
  82. variable v_dataout : std_logic_vector(0 to 127);
  83. variable v_random : RandomPType;
  84. begin
  85. v_random.InitSeed(v_random'instance_name);
  86. wait until s_reset = '1' and rising_edge(s_clk);
  87. -- ENCRYPTION TESTs
  88. report "Test CBC-AES encryption";
  89. s_start <= '1';
  90. s_mode <= '0';
  91. v_iv := v_random.RandSlv(128);
  92. v_key := v_random.RandSlv(128);
  93. for i in 0 to 31 loop
  94. v_datain := v_random.RandSlv(128);
  95. s_validin <= '1';
  96. s_key <= v_key;
  97. s_iv <= v_iv;
  98. s_datain <= v_datain;
  99. cryptData(swap(v_datain), swap(v_key), swap(v_iv), false, i = 0, i = 31, v_dataout, v_datain'length/8);
  100. wait until s_acceptin = '1' and rising_edge(s_clk);
  101. s_validin <= '0';
  102. s_start <= '0';
  103. wait until s_validout = '1' and rising_edge(s_clk);
  104. s_acceptout <= '1';
  105. assert s_dataout = swap(v_dataout)
  106. report "Encryption error: Expected 0x" & to_hstring(swap(v_dataout)) & ", got 0x" & to_hstring(s_dataout)
  107. severity failure;
  108. wait until rising_edge(s_clk);
  109. s_acceptout <= '0';
  110. end loop;
  111. -- DECRYPTION TESTs
  112. report "Test CBC-AES decryption";
  113. s_start <= '1';
  114. s_mode <= '1';
  115. v_iv := v_random.RandSlv(128);
  116. v_key := v_random.RandSlv(128);
  117. for i in 0 to 31 loop
  118. v_datain := v_random.RandSlv(128);
  119. s_validin <= '1';
  120. s_key <= v_key;
  121. s_iv <= v_iv;
  122. s_datain <= v_datain;
  123. cryptData(swap(v_datain), swap(v_key), swap(v_iv), true, i = 0, i = 31, v_dataout, v_datain'length/8);
  124. wait until s_acceptin = '1' and rising_edge(s_clk);
  125. s_validin <= '0';
  126. s_start <= '0';
  127. wait until s_validout = '1' and rising_edge(s_clk);
  128. s_acceptout <= '1';
  129. assert s_dataout = swap(v_dataout)
  130. report "Decryption error: Expected 0x" & to_hstring(swap(v_dataout)) & ", got 0x" & to_hstring(s_dataout)
  131. severity failure;
  132. wait until rising_edge(s_clk);
  133. s_acceptout <= '0';
  134. end loop;
  135. wait for 100 ns;
  136. report "Simulation finished without errors";
  137. finish(0);
  138. end process;
  139. end architecture sim;