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.

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