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.

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