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.

193 lines
4.5 KiB

  1. // ======================================================================
  2. // DES encryption/decryption testbench
  3. // tests according to NIST 800-17 special publication
  4. // Copyright (C) 2012 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. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. // ======================================================================
  20. `timescale 1ns/1ps
  21. module tb_cbctdes;
  22. // set dumpfile
  23. initial begin
  24. $dumpfile ("tb_cbctdes.vcd");
  25. $dumpvars (0, tb_cbctdes);
  26. end
  27. reg reset;
  28. reg clk = 0;
  29. reg start;
  30. reg mode;
  31. reg [0:63] key1;
  32. reg [0:63] key2;
  33. reg [0:63] key3;
  34. reg [0:63] iv;
  35. reg [0:63] datain;
  36. reg validin;
  37. integer index;
  38. integer outdex;
  39. integer errors;
  40. wire [0:63] dataout;
  41. wire validout;
  42. wire ready;
  43. reg [0:63] test_data [0:18];
  44. reg [0:63] test_answers [0:18];
  45. // read in test data files
  46. initial begin
  47. $readmemh("test_data.txt", test_data);
  48. end
  49. // setup simulation
  50. initial begin
  51. reset = 1;
  52. #1 reset = 0;
  53. #100 reset = 1;
  54. end
  55. // generate clock with 50 mhz
  56. always #10 clk = !clk;
  57. // init the register values
  58. initial
  59. forever @(negedge reset) begin
  60. //disable stimuli;
  61. disable checker;
  62. mode <= 0;
  63. validin <= 0;
  64. key1 <= 0;
  65. key2 <= 0;
  66. key3 <= 0;
  67. datain <= 0;
  68. errors = 0;
  69. end
  70. // stimuli generator process
  71. initial
  72. forever @(negedge reset) begin
  73. index = 0;
  74. while (index < 19) begin
  75. @(posedge clk)
  76. if (ready) begin
  77. mode <= 0;
  78. validin <= 1;
  79. datain <= test_data[index];
  80. key1 <= 64'h1111111111111111;
  81. key2 <= 64'h5555555555555555;
  82. key3 <= 64'h9999999999999999;
  83. index = index + 1;
  84. @(posedge clk)
  85. validin <= 0;
  86. end
  87. end
  88. index = 0;
  89. while (index < 19) begin
  90. @(posedge clk)
  91. if (ready) begin
  92. mode <= 1;
  93. validin <= 1;
  94. datain <= test_answers[index];
  95. key1 <= 64'h1111111111111111;
  96. key2 <= 64'h5555555555555555;
  97. key3 <= 64'h9999999999999999;
  98. index = index + 1;
  99. @(posedge clk)
  100. validin <= 0;
  101. end
  102. end
  103. @(posedge clk)
  104. validin <= 0;
  105. mode <= 0;
  106. datain <= 0;
  107. key1 <= 0;
  108. key2 <= 0;
  109. key3 <= 0;
  110. end
  111. // checker process
  112. always begin : checker
  113. wait (reset)
  114. outdex = 0;
  115. // encryption tests
  116. outdex = 0;
  117. while (outdex < 19) begin
  118. @(posedge clk)
  119. if (validout) begin
  120. $display ("encrypt test pattern %d", outdex);
  121. test_answers[outdex] = dataout;
  122. outdex = outdex + 1;
  123. end
  124. end
  125. // decryption tests
  126. outdex = 0;
  127. while (outdex < 19) begin
  128. @(posedge clk)
  129. if (validout) begin
  130. $display ("decrypt test pattern %d", outdex);
  131. // detected an error -> print error message
  132. // increment error counter
  133. if (dataout != test_data[outdex]) begin
  134. $display ("error, output was %h - should have been %h", dataout, test_data[outdex]);
  135. errors = errors + 1;
  136. end
  137. outdex = outdex + 1;
  138. end
  139. end
  140. if (errors) begin
  141. $display ("simulation finished, %0d errors detected :(", errors);
  142. end else begin
  143. $display ("simulation tests finished, no errors detected :)");
  144. end
  145. $display ("#############");
  146. @(posedge clk)
  147. $finish;
  148. end
  149. // dut
  150. cbctdes i_cbctdes (
  151. .reset_i(reset),
  152. .clk_i(clk),
  153. .start_i(start),
  154. .mode_i(mode),
  155. .key1_i(key1),
  156. .key2_i(key2),
  157. .key3_i(key3),
  158. .iv_i(iv),
  159. .data_i(datain),
  160. .valid_i(validin),
  161. .data_o(dataout),
  162. .valid_o(validout),
  163. .ready_o(ready)
  164. );
  165. endmodule