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.

131 lines
3.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. module tb_des;
  21. // set dumpfile
  22. initial begin
  23. $dumpfile ("tb_des.vcd");
  24. $dumpvars (0, tb_des);
  25. end
  26. reg reset;
  27. reg clk = 0;
  28. reg mode;
  29. reg [0:63] key;
  30. reg [0:63] datain;
  31. reg validin;
  32. integer index;
  33. integer outdex;
  34. integer errors;
  35. wire [0:63] dataout;
  36. wire validout;
  37. reg [0:63] data_input [0:127];
  38. reg [0:63] key_input [0:127];
  39. reg [0:63] data_output [0:127];
  40. // read in test data files
  41. initial begin
  42. $readmemh("data_input.txt", data_input);
  43. $readmemh("key_input.txt", key_input);
  44. $readmemh("data_output.txt", data_output);
  45. end
  46. // setup simulation
  47. initial begin
  48. reset = 1;
  49. #1 reset = 0;
  50. #20 reset = 1;
  51. #2000 $finish;
  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. key <= 0;
  63. datain <= 0;
  64. errors = 0;
  65. end
  66. // stimuli generator process
  67. always begin : stimuli
  68. wait (reset)
  69. @(posedge clk)
  70. // Variable plaintext known answer test
  71. for(index = 0; index < 128; index = index + 1)
  72. begin
  73. @(posedge clk)
  74. mode <= 0;
  75. validin <= 1;
  76. datain <= data_input[index];
  77. key <= key_input[index];
  78. end
  79. validin <= 0;
  80. end
  81. // checker process
  82. always begin : checker
  83. wait (reset)
  84. // Variable plaintext known answer test
  85. wait (validout)
  86. for(outdex = 0; outdex < 128; outdex = outdex + 1)
  87. begin
  88. @(posedge clk)
  89. // detected an error -> print error message
  90. if (dataout != data_output[outdex]) begin
  91. $display ("error, output was %h - should have been %h", dataout, data_output[outdex]);
  92. errors = errors + 1;
  93. end
  94. end
  95. // simulation finished -> print messages and if an error was detected
  96. $display ("#############");
  97. if (errors) begin
  98. $display ("test finished, there were %0d errors detected :(", errors);
  99. end else begin
  100. $display ("test finished, no errors detected :)");
  101. end
  102. $display ("#############");
  103. @(posedge clk)
  104. $finish;
  105. end
  106. // dut
  107. des i_des (
  108. .reset_i(reset),
  109. .clk_i(clk),
  110. .mode_i(mode),
  111. .key_i(key),
  112. .data_i(datain),
  113. .valid_i(validin),
  114. .data_o(dataout),
  115. .valid_o(validout)
  116. );
  117. endmodule