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.

446 lines
18 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. -- Revision 1.0 2011/09/23
  19. -- Initial release, variable plaintext known answer test integrated
  20. library ieee;
  21. use ieee.std_logic_1164.all;
  22. use ieee.numeric_std.all;
  23. entity tb_cbcdes is
  24. end entity tb_cbcdes;
  25. architecture rtl of tb_cbcdes is
  26. type t_array is array (natural range <>) of std_logic_vector(0 to 63);
  27. constant c_variable_plaintext_known_answers : t_array(0 to 63) :=
  28. (x"95F8A5E5DD31D900", x"DD7F121CA5015619", x"2E8653104F3834EA",
  29. x"4BD388FF6CD81D4F", x"20B9E767B2FB1456", x"55579380D77138EF",
  30. x"6CC5DEFAAF04512F", x"0D9F279BA5D87260", x"D9031B0271BD5A0A",
  31. x"424250B37C3DD951", x"B8061B7ECD9A21E5", x"F15D0F286B65BD28",
  32. x"ADD0CC8D6E5DEBA1", x"E6D5F82752AD63D1", x"ECBFE3BD3F591A5E",
  33. x"F356834379D165CD", x"2B9F982F20037FA9", x"889DE068A16F0BE6",
  34. x"E19E275D846A1298", x"329A8ED523D71AEC", x"E7FCE22557D23C97",
  35. x"12A9F5817FF2D65D", x"A484C3AD38DC9C19", x"FBE00A8A1EF8AD72",
  36. x"750D079407521363", x"64FEED9C724C2FAF", x"F02B263B328E2B60",
  37. x"9D64555A9A10B852", x"D106FF0BED5255D7", x"E1652C6B138C64A5",
  38. x"E428581186EC8F46", x"AEB5F5EDE22D1A36", x"E943D7568AEC0C5C",
  39. x"DF98C8276F54B04B", x"B160E4680F6C696F", x"FA0752B07D9C4AB8",
  40. x"CA3A2B036DBC8502", x"5E0905517BB59BCF", x"814EEB3B91D90726",
  41. x"4D49DB1532919C9F", x"25EB5FC3F8CF0621", x"AB6A20C0620D1C6F",
  42. x"79E90DBC98F92CCA", x"866ECEDD8072BB0E", x"8B54536F2F3E64A8",
  43. x"EA51D3975595B86B", x"CAFFC6AC4542DE31", x"8DD45A2DDF90796C",
  44. x"1029D55E880EC2D0", x"5D86CB23639DBEA9", x"1D1CA853AE7C0C5F",
  45. x"CE332329248F3228", x"8405D1ABE24FB942", x"E643D78090CA4207",
  46. x"48221B9937748A23", x"DD7C0BBD61FAFD54", x"2FBC291A570DB5C4",
  47. x"E07C30D7E4E26E12", x"0953E2258E8E90A1", x"5B711BC4CEEBF2EE",
  48. x"CC083F1E6D9E85F6", x"D2FD8867D50D2DFE", x"06E7EA22CE92708F",
  49. x"166B40B44ABA4BD6");
  50. constant c_variable_key_known_answers : t_array(0 to 55) :=
  51. (x"95A8D72813DAA94D", x"0EEC1487DD8C26D5", x"7AD16FFB79C45926",
  52. x"D3746294CA6A6CF3", x"809F5F873C1FD761", x"C02FAFFEC989D1FC",
  53. x"4615AA1D33E72F10", x"2055123350C00858", x"DF3B99D6577397C8",
  54. x"31FE17369B5288C9", x"DFDD3CC64DAE1642", x"178C83CE2B399D94",
  55. x"50F636324A9B7F80", x"A8468EE3BC18F06D", x"A2DC9E92FD3CDE92",
  56. x"CAC09F797D031287", x"90BA680B22AEB525", x"CE7A24F350E280B6",
  57. x"882BFF0AA01A0B87", x"25610288924511C2", x"C71516C29C75D170",
  58. x"5199C29A52C9F059", x"C22F0A294A71F29F", x"EE371483714C02EA",
  59. x"A81FBD448F9E522F", x"4F644C92E192DFED", x"1AFA9A66A6DF92AE",
  60. x"B3C1CC715CB879D8", x"19D032E64AB0BD8B", x"3CFAA7A7DC8720DC",
  61. x"B7265F7F447AC6F3", x"9DB73B3C0D163F54", x"8181B65BABF4A975",
  62. x"93C9B64042EAA240", x"5570530829705592", x"8638809E878787A0",
  63. x"41B9A79AF79AC208", x"7A9BE42F2009A892", x"29038D56BA6D2745",
  64. x"5495C6ABF1E5DF51", x"AE13DBD561488933", x"024D1FFA8904E389",
  65. x"D1399712F99BF02E", x"14C1D7C1CFFEC79E", x"1DE5279DAE3BED6F",
  66. x"E941A33F85501303", x"DA99DBBC9A03F379", x"B7FC92F91D8E92E9",
  67. x"AE8E5CAA3CA04E85", x"9CC62DF43B6EED74", x"D863DBB5C59A91A0",
  68. x"A1AB2190545B91D7", x"0875041E64C570F7", x"5A594528BEBEF1CC",
  69. x"FCDB3291DE21F0C0", x"869EFD7F9F265A09");
  70. constant c_permutation_operation_known_answers_keys : t_array(0 to 31) :=
  71. (x"1046913489980131", x"1007103489988020", x"10071034C8980120",
  72. x"1046103489988020", x"1086911519190101", x"1086911519580101",
  73. x"5107B01519580101", x"1007B01519190101", x"3107915498080101",
  74. x"3107919498080101", x"10079115B9080140", x"3107911598080140",
  75. x"1007D01589980101", x"9107911589980101", x"9107D01589190101",
  76. x"1007D01598980120", x"1007940498190101", x"0107910491190401",
  77. x"0107910491190101", x"0107940491190401", x"19079210981A0101",
  78. x"1007911998190801", x"10079119981A0801", x"1007921098190101",
  79. x"100791159819010B", x"1004801598190101", x"1004801598190102",
  80. x"1004801598190108", x"1002911598100104", x"1002911598190104",
  81. x"1002911598100201", x"1002911698100101");
  82. constant c_permutation_operation_known_answers_cipher : t_array(0 to 31) :=
  83. (x"88D55E54F54C97B4", x"0C0CC00C83EA48FD", x"83BC8EF3A6570183",
  84. x"DF725DCAD94EA2E9", x"E652B53B550BE8B0", x"AF527120C485CBB0",
  85. x"0F04CE393DB926D5", x"C9F00FFC74079067", x"7CFD82A593252B4E",
  86. x"CB49A2F9E91363E3", x"00B588BE70D23F56", x"406A9A6AB43399AE",
  87. x"6CB773611DCA9ADA", x"67FD21C17DBB5D70", x"9592CB4110430787",
  88. x"A6B7FF68A318DDD3", x"4D102196C914CA16", x"2DFA9F4573594965",
  89. x"B46604816C0E0774", x"6E7E6221A4F34E87", x"AA85E74643233199",
  90. x"2E5A19DB4D1962D6", x"23A866A809D30894", x"D812D961F017D320",
  91. x"055605816E58608F", x"ABD88E8B1B7716F1", x"537AC95BE69DA1E1",
  92. x"AED0F6AE3C25CDD8", x"B3E35A5EE53E7B8D", x"61C79C71921A2EF8",
  93. x"E2F5728F0995013C", x"1AEAC39A61F0A464");
  94. constant c_substitution_table_test_keys : t_array(0 to 18) :=
  95. (x"7CA110454A1A6E57", x"0131D9619DC1376E", x"07A1133E4A0B2686",
  96. x"3849674C2602319E", x"04B915BA43FEB5B6", x"0113B970FD34F2CE",
  97. x"0170F175468FB5E6", x"43297FAD38E373FE", x"07A7137045DA2A16",
  98. x"04689104C2FD3B2F", x"37D06BB516CB7546", x"1F08260D1AC2465E",
  99. x"584023641ABA6176", x"025816164629B007", x"49793EBC79B3258F",
  100. x"4FB05E1515AB73A7", x"49E95D6D4CA229BF", x"018310DC409B26D6",
  101. x"1C587F1C13924FEF");
  102. constant c_substitution_table_test_plain : t_array(0 to 18) :=
  103. (x"01A1D6D039776742", x"5CD54CA83DEF57DA", x"0248D43806F67172",
  104. x"51454B582DDF440A", x"42FD443059577FA2", x"059B5E0851CF143A",
  105. x"0756D8E0774761D2", x"762514B829BF486A", x"3BDD119049372802",
  106. x"26955F6835AF609A", x"164D5E404F275232", x"6B056E18759F5CCA",
  107. x"004BD6EF09176062", x"480D39006EE762F2", x"437540C8698F3CFA",
  108. x"072D43A077075292", x"02FE55778117F12A", x"1D9D5C5018F728C2",
  109. x"305532286D6F295A");
  110. constant c_substitution_table_test_cipher : t_array(0 to 18) :=
  111. (x"690F5B0D9A26939B", x"7A389D10354BD271", x"868EBB51CAB4599A",
  112. x"7178876E01F19B2A", x"AF37FB421F8C4095", x"86A560F10EC6D85B",
  113. x"0CD3DA020021DC09", x"EA676B2CB7DB2B7A", x"DFD64A815CAF1A0F",
  114. x"5C513C9C4886C088", x"0A2AEEAE3FF4AB77", x"EF1BF03E5DFA575A",
  115. x"88BF0DB6D70DEE56", x"A1F9915541020B56", x"6FBF1CAFCFFD0556",
  116. x"2F22E49BAB7CA1AC", x"5A6B612CC26CCE4A", x"5F4C038ED12B2E41",
  117. x"63FAC0D034D9F793");
  118. signal s_reset : std_logic := '0';
  119. signal s_clk : std_logic := '0';
  120. signal s_start : std_logic := '0';
  121. signal s_mode : std_logic := '0';
  122. signal s_key : std_logic_vector(0 to 63) := (others => '0');
  123. signal s_iv : std_logic_vector(0 to 63) := (others => '0');
  124. signal s_datain : std_logic_vector(0 to 63) := (others => '0');
  125. signal s_validin : std_logic := '0';
  126. signal s_ready : std_logic := '0';
  127. signal s_dataout : std_logic_vector(0 to 63);
  128. signal s_validout : std_logic;
  129. component cbcdes is
  130. port (
  131. reset_i : in std_logic;
  132. clk_i : in std_logic;
  133. mode_i : in std_logic;
  134. start_i : in std_logic;
  135. iv_i : in std_logic_vector(0 to 63);
  136. key_i : in std_logic_vector(0 TO 63);
  137. data_i : in std_logic_vector(0 TO 63);
  138. valid_i : in std_logic;
  139. ready_o : out std_logic;
  140. data_o : out std_logic_vector(0 TO 63);
  141. valid_o : out std_logic
  142. );
  143. end component cbcdes;
  144. begin
  145. s_reset <= '1' after 100 ns;
  146. s_clk <= not(s_clk) after 10 ns;
  147. teststimuliP : process is
  148. begin
  149. -- ENCRYPTION TESTS
  150. s_start <= '0';
  151. s_mode <= '0';
  152. s_validin <= '0';
  153. s_iv <= (others => '0');
  154. s_key <= x"0101010101010101";
  155. s_datain <= x"8000000000000000";
  156. -- Variable plaintext known answer test
  157. for index in c_variable_plaintext_known_answers'range loop
  158. wait until rising_edge(s_clk) and s_ready = '1';
  159. s_validin <= '1';
  160. s_start <= '1';
  161. if(index /= 0) then
  162. s_datain <= '0' & s_datain(0 to 62);
  163. end if;
  164. wait until rising_edge(s_clk);
  165. s_validin <= '0';
  166. s_start <= '0';
  167. end loop;
  168. wait until rising_edge(s_clk);
  169. s_start <= '0';
  170. s_mode <= '0';
  171. s_validin <= '0';
  172. s_iv <= (others => '0');
  173. s_key <= (others => '0');
  174. s_datain <= (others => '0');
  175. -- wait for 100 ns;
  176. -- -- Inverse permutation known answer test
  177. -- s_key <= x"0101010101010101";
  178. -- for index in c_variable_plaintext_known_answers'range loop
  179. -- wait until rising_edge(s_clk);
  180. -- s_validin <= '1';
  181. -- s_datain <= c_variable_plaintext_known_answers(index);
  182. -- end loop;
  183. -- wait until rising_edge(s_clk);
  184. -- s_mode <= '0';
  185. -- s_validin <= '0';
  186. -- s_key <= (others => '0');
  187. -- s_datain <= (others => '0');
  188. -- wait for 100 ns;
  189. -- -- Variable key known answer test
  190. -- s_key <= x"8000000000000000";
  191. -- for index in c_variable_key_known_answers'range loop
  192. -- wait until rising_edge(s_clk);
  193. -- s_validin <= '1';
  194. -- if(index /= 0) then
  195. -- if(index = 7 or index = 14 or index = 21 or index = 28 or index = 35 or
  196. -- index = 42 or index = 49) then
  197. -- s_key <= "00" & s_key(0 to 61);
  198. -- else
  199. -- s_key <= '0' & s_key(0 to 62);
  200. -- end if;
  201. -- end if;
  202. -- end loop;
  203. -- wait until rising_edge(s_clk);
  204. -- s_mode <= '0';
  205. -- s_validin <= '0';
  206. -- s_key <= (others => '0');
  207. -- s_datain <= (others => '0');
  208. -- wait for 100 ns;
  209. -- -- Permutation operation known answer test
  210. -- s_datain <= x"0000000000000000";
  211. -- for index in c_permutation_operation_known_answers_keys'range loop
  212. -- wait until rising_edge(s_clk);
  213. -- s_validin <= '1';
  214. -- s_key <= c_permutation_operation_known_answers_keys(index);
  215. -- end loop;
  216. -- wait until rising_edge(s_clk);
  217. -- s_mode <= '0';
  218. -- s_validin <= '0';
  219. -- s_key <= (others => '0');
  220. -- s_datain <= (others => '0');
  221. -- wait for 100 ns;
  222. -- -- Substitution table known answer test
  223. -- for index in c_substitution_table_test_keys'range loop
  224. -- wait until rising_edge(s_clk);
  225. -- s_validin <= '1';
  226. -- s_key <= c_substitution_table_test_keys(index);
  227. -- s_datain <= c_substitution_table_test_plain(index);
  228. -- end loop;
  229. -- wait until rising_edge(s_clk);
  230. -- -- DECRYPTION TESTS
  231. -- s_mode <= '0';
  232. -- s_validin <= '0';
  233. -- s_key <= (others => '0');
  234. -- s_datain <= (others => '0');
  235. -- wait for 100 ns;
  236. -- -- Variable ciphertext known answer test
  237. -- s_key <= x"0101010101010101";
  238. -- for index in c_variable_plaintext_known_answers'range loop
  239. -- wait until rising_edge(s_clk);
  240. -- s_mode <= '1';
  241. -- s_validin <= '1';
  242. -- s_datain <= c_variable_plaintext_known_answers(index);
  243. -- end loop;
  244. -- wait until rising_edge(s_clk);
  245. -- s_mode <= '0';
  246. -- s_validin <= '0';
  247. -- s_key <= (others => '0');
  248. -- s_datain <= (others => '0');
  249. -- wait for 100 ns;
  250. -- -- Initial permutation known answer test
  251. -- s_key <= x"0101010101010101";
  252. -- s_datain <= x"8000000000000000";
  253. -- for index in c_variable_plaintext_known_answers'range loop
  254. -- wait until rising_edge(s_clk);
  255. -- s_mode <= '1';
  256. -- s_validin <= '1';
  257. -- if(index /= 0) then
  258. -- s_datain <= '0' & s_datain(0 to 62);
  259. -- end if;
  260. -- end loop;
  261. -- wait until rising_edge(s_clk);
  262. -- s_mode <= '0';
  263. -- s_validin <= '0';
  264. -- s_key <= (others => '0');
  265. -- s_datain <= (others => '0');
  266. -- -- Variable key known answer test
  267. -- s_key <= x"8000000000000000";
  268. -- for index in c_variable_key_known_answers'range loop
  269. -- wait until rising_edge(s_clk);
  270. -- s_mode <= '1';
  271. -- s_validin <= '1';
  272. -- s_datain <= c_variable_key_known_answers(index);
  273. -- if(index /= 0) then
  274. -- if(index = 7 or index = 14 or index = 21 or index = 28 or index = 35 or
  275. -- index = 42 or index = 49) then
  276. -- s_key <= "00" & s_key(0 to 61);
  277. -- else
  278. -- s_key <= '0' & s_key(0 to 62);
  279. -- end if;
  280. -- end if;
  281. -- end loop;
  282. -- wait until rising_edge(s_clk);
  283. -- s_mode <= '0';
  284. -- s_validin <= '0';
  285. -- s_key <= (others => '0');
  286. -- s_datain <= (others => '0');
  287. -- wait for 100 ns;
  288. -- -- Permutation operation known answer test
  289. -- for index in c_permutation_operation_known_answers_keys'range loop
  290. -- wait until rising_edge(s_clk);
  291. -- s_mode <= '1';
  292. -- s_validin <= '1';
  293. -- s_datain <= c_permutation_operation_known_answers_cipher(index);
  294. -- s_key <= c_permutation_operation_known_answers_keys(index);
  295. -- end loop;
  296. -- wait until rising_edge(s_clk);
  297. -- s_mode <= '0';
  298. -- s_validin <= '0';
  299. -- s_key <= (others => '0');
  300. -- s_datain <= (others => '0');
  301. -- wait for 100 ns;
  302. -- -- Substitution table known answer test
  303. -- for index in c_substitution_table_test_keys'range loop
  304. -- wait until rising_edge(s_clk);
  305. -- s_mode <= '1';
  306. -- s_validin <= '1';
  307. -- s_key <= c_substitution_table_test_keys(index);
  308. -- s_datain <= c_substitution_table_test_cipher(index);
  309. -- end loop;
  310. -- wait until rising_edge(s_clk);
  311. -- s_mode <= '0';
  312. -- s_validin <= '0';
  313. -- s_key <= (others => '0');
  314. -- s_datain <= (others => '0');
  315. wait;
  316. end process teststimuliP;
  317. testcheckerP : process is
  318. variable v_plaintext : std_logic_vector(0 to 63) := x"8000000000000000";
  319. begin
  320. report "# ENCRYPTION TESTS";
  321. report "# Variable plaintext known answer test";
  322. for index in c_variable_plaintext_known_answers'range loop
  323. wait until rising_edge(s_clk) and s_validout = '1';
  324. assert (s_dataout = c_variable_plaintext_known_answers(index))
  325. report "encryption error"
  326. severity error;
  327. end loop;
  328. -- report "# Inverse permutation known answer test";
  329. -- for index in c_variable_plaintext_known_answers'range loop
  330. -- wait until rising_edge(s_clk) and s_validout = '1';
  331. -- assert (s_dataout = v_plaintext)
  332. -- report "encryption error"
  333. -- severity error;
  334. -- v_plaintext := '0' & v_plaintext(0 to 62);
  335. -- end loop;
  336. -- report "# Variable key known answer test";
  337. -- for index in c_variable_key_known_answers'range loop
  338. -- wait until rising_edge(s_clk) and s_validout = '1';
  339. -- assert (s_dataout = c_variable_key_known_answers(index))
  340. -- report "encryption error"
  341. -- severity error;
  342. -- end loop;
  343. -- report "# Permutation operation known answer test";
  344. -- for index in c_permutation_operation_known_answers_cipher'range loop
  345. -- wait until rising_edge(s_clk) and s_validout = '1';
  346. -- assert (s_dataout = c_permutation_operation_known_answers_cipher(index))
  347. -- report "encryption error"
  348. -- severity error;
  349. -- end loop;
  350. -- report "# Substitution table known answer test";
  351. -- for index in c_substitution_table_test_cipher'range loop
  352. -- wait until rising_edge(s_clk) and s_validout = '1';
  353. -- assert (s_dataout = c_substitution_table_test_cipher(index))
  354. -- report "encryption error"
  355. -- severity error;
  356. -- end loop;
  357. -- report "# DECRYPTION TESTS";
  358. -- report "# Variable ciphertext known answer test";
  359. -- v_plaintext := x"8000000000000000";
  360. -- for index in c_variable_plaintext_known_answers'range loop
  361. -- wait until rising_edge(s_clk) and s_validout = '1';
  362. -- assert (s_dataout = v_plaintext)
  363. -- report "decryption error"
  364. -- severity error;
  365. -- v_plaintext := '0' & v_plaintext(0 to 62);
  366. -- end loop;
  367. -- report "# Initial permutation known answer test";
  368. -- for index in c_variable_plaintext_known_answers'range loop
  369. -- wait until rising_edge(s_clk) and s_validout = '1';
  370. -- assert (s_dataout = c_variable_plaintext_known_answers(index))
  371. -- report "decryption error"
  372. -- severity error;
  373. -- end loop;
  374. -- report "# Variable key known answer test";
  375. -- for index in c_variable_key_known_answers'range loop
  376. -- wait until rising_edge(s_clk) and s_validout = '1';
  377. -- assert (s_dataout = x"0000000000000000")
  378. -- report "decryption error"
  379. -- severity error;
  380. -- end loop;
  381. -- report "# Permutation operation known answer test";
  382. -- for index in c_permutation_operation_known_answers_keys'range loop
  383. -- wait until rising_edge(s_clk) and s_validout = '1';
  384. -- assert (s_dataout = x"0000000000000000")
  385. -- report "decryption error"
  386. -- severity error;
  387. -- end loop;
  388. -- report "# Substitution table known answer test";
  389. -- for index in c_substitution_table_test_cipher'range loop
  390. -- wait until rising_edge(s_clk) and s_validout = '1';
  391. -- assert (s_dataout = c_substitution_table_test_plain(index))
  392. -- report "decryption error"
  393. -- severity error;
  394. -- end loop;
  395. report "# Successfully passed all tests";
  396. wait;
  397. end process testcheckerP;
  398. i_cbcdes : cbcdes
  399. port map (
  400. reset_i => s_reset,
  401. clk_i => s_clk,
  402. start_i => s_start,
  403. mode_i => s_mode,
  404. key_i => s_key,
  405. iv_i => s_iv,
  406. data_i => s_datain,
  407. valid_i => s_validin,
  408. ready_o => s_ready,
  409. data_o => s_dataout,
  410. valid_o => s_validout
  411. );
  412. end architecture rtl;