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.

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