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.

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