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.

190 lines
4.4 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_tdes;
  22. // set dumpfile
  23. initial begin
  24. $dumpfile ("tb_tdes.vcd");
  25. $dumpvars (0, tb_tdes);
  26. end
  27. reg reset;
  28. reg clk = 0;
  29. reg mode;
  30. reg [0:63] key1;
  31. reg [0:63] key2;
  32. reg [0:63] key3;
  33. reg [0:63] datain;
  34. reg validin;
  35. integer index;
  36. integer outdex;
  37. integer errors;
  38. wire [0:63] dataout;
  39. wire validout;
  40. wire ready;
  41. reg [0:63] test_data [0:18];
  42. reg [0:63] test_answers [0:18];
  43. // read in test data files
  44. initial begin
  45. $readmemh("test_data.txt", test_data);
  46. end
  47. // setup simulation
  48. initial begin
  49. reset = 1;
  50. #1 reset = 0;
  51. #20 reset = 1;
  52. end
  53. // generate clock with 100 mhz
  54. always #5 clk = !clk;
  55. // init the register values
  56. initial
  57. forever @(negedge reset) begin
  58. //disable stimuli;
  59. disable checker;
  60. mode <= 0;
  61. validin <= 0;
  62. key1 <= 0;
  63. key2 <= 0;
  64. key3 <= 0;
  65. datain <= 0;
  66. errors = 0;
  67. end
  68. // stimuli generator process
  69. initial
  70. forever @(negedge reset) begin
  71. index = 0;
  72. wait (reset);
  73. while (index < 19) begin
  74. @(posedge clk)
  75. if (ready) begin
  76. mode <= 0;
  77. validin <= 1;
  78. datain <= test_data[index];
  79. key1 <= 64'h1111111111111111;
  80. key2 <= 64'h5555555555555555;
  81. key3 <= 64'h9999999999999999;
  82. index = index + 1;
  83. @(posedge clk)
  84. validin <= 0;
  85. end
  86. end
  87. index = 0;
  88. while (index < 19) begin
  89. @(posedge clk)
  90. if (ready) begin
  91. mode <= 1;
  92. validin <= 1;
  93. datain <= test_answers[index];
  94. key1 <= 64'h1111111111111111;
  95. key2 <= 64'h5555555555555555;
  96. key3 <= 64'h9999999999999999;
  97. index = index + 1;
  98. @(posedge clk)
  99. validin <= 0;
  100. end
  101. end
  102. @(posedge clk)
  103. validin <= 0;
  104. mode <= 0;
  105. datain <= 0;
  106. key1 <= 0;
  107. key2 <= 0;
  108. key3 <= 0;
  109. end
  110. // checker process
  111. always begin : checker
  112. wait (reset)
  113. outdex = 0;
  114. // encryption tests
  115. outdex = 0;
  116. while (outdex < 19) begin
  117. @(posedge clk)
  118. if (validout) begin
  119. test_answers[outdex] = dataout;
  120. outdex = outdex + 1;
  121. end
  122. end
  123. // decryption tests
  124. outdex = 0;
  125. while (outdex < 19) begin
  126. @(posedge clk)
  127. if (validout) begin
  128. // detected an error -> print error message
  129. // increment error counter
  130. if (dataout != test_data[outdex]) begin
  131. $display ("error, output was %h - should have been %h", dataout, test_data[outdex]);
  132. errors = errors + 1;
  133. end
  134. outdex = outdex + 1;
  135. end
  136. end
  137. if (errors) begin
  138. $display ("simulation finished, %0d errors detected :(", errors);
  139. end else begin
  140. $display ("simulation tests finished, no errors detected :)");
  141. end
  142. $display ("#############");
  143. @(posedge clk)
  144. $finish;
  145. end
  146. // dut
  147. tdes i_tdes (
  148. .reset_i(reset),
  149. .clk_i(clk),
  150. .mode_i(mode),
  151. .key1_i(key1),
  152. .key2_i(key2),
  153. .key3_i(key3),
  154. .data_i(datain),
  155. .valid_i(validin),
  156. .data_o(dataout),
  157. .valid_o(validout),
  158. .ready_o(ready)
  159. );
  160. endmodule