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.

194 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. wait (reset);
  75. while (index < 19) begin
  76. @(posedge clk)
  77. if (ready) begin
  78. mode <= 0;
  79. validin <= 1;
  80. datain <= test_data[index];
  81. key1 <= 64'h1111111111111111;
  82. key2 <= 64'h5555555555555555;
  83. key3 <= 64'h9999999999999999;
  84. index = index + 1;
  85. @(posedge clk)
  86. validin <= 0;
  87. end
  88. end
  89. index = 0;
  90. while (index < 19) begin
  91. @(posedge clk)
  92. if (ready) begin
  93. mode <= 1;
  94. validin <= 1;
  95. datain <= test_answers[index];
  96. key1 <= 64'h1111111111111111;
  97. key2 <= 64'h5555555555555555;
  98. key3 <= 64'h9999999999999999;
  99. index = index + 1;
  100. @(posedge clk)
  101. validin <= 0;
  102. end
  103. end
  104. @(posedge clk)
  105. validin <= 0;
  106. mode <= 0;
  107. datain <= 0;
  108. key1 <= 0;
  109. key2 <= 0;
  110. key3 <= 0;
  111. end
  112. // checker process
  113. always begin : checker
  114. wait (reset);
  115. outdex = 0;
  116. // encryption tests
  117. outdex = 0;
  118. while (outdex < 19) begin
  119. @(posedge clk)
  120. if (validout) begin
  121. $display ("encrypt test pattern %d", outdex);
  122. test_answers[outdex] = dataout;
  123. outdex = outdex + 1;
  124. end
  125. end
  126. // decryption tests
  127. outdex = 0;
  128. while (outdex < 19) begin
  129. @(posedge clk)
  130. if (validout) begin
  131. $display ("decrypt test pattern %d", outdex);
  132. // detected an error -> print error message
  133. // increment error counter
  134. if (dataout != test_data[outdex]) begin
  135. $display ("error, output was %h - should have been %h", dataout, test_data[outdex]);
  136. errors = errors + 1;
  137. end
  138. outdex = outdex + 1;
  139. end
  140. end
  141. if (errors) begin
  142. $display ("simulation finished, %0d errors detected :(", errors);
  143. end else begin
  144. $display ("simulation tests finished, no errors detected :)");
  145. end
  146. $display ("#############");
  147. @(posedge clk)
  148. $finish;
  149. end
  150. // dut
  151. cbctdes i_cbctdes (
  152. .reset_i(reset),
  153. .clk_i(clk),
  154. .start_i(start),
  155. .mode_i(mode),
  156. .key1_i(key1),
  157. .key2_i(key2),
  158. .key3_i(key3),
  159. .iv_i(iv),
  160. .data_i(datain),
  161. .valid_i(validin),
  162. .data_o(dataout),
  163. .valid_o(validout),
  164. .ready_o(ready)
  165. );
  166. endmodule