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.

132 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. // increent error counter
  91. if (dataout != data_output[outdex]) begin
  92. $display ("error, output was %h - should have been %h", dataout, data_output[outdex]);
  93. errors = errors + 1;
  94. end
  95. end
  96. // simulation finished -> print messages and if an error was detected
  97. $display ("#############");
  98. if (errors) begin
  99. $display ("test finished, %0d errors detected :(", errors);
  100. end else begin
  101. $display ("test finished, no errors detected :)");
  102. end
  103. $display ("#############");
  104. @(posedge clk)
  105. $finish;
  106. end
  107. // dut
  108. des i_des (
  109. .reset_i(reset),
  110. .clk_i(clk),
  111. .mode_i(mode),
  112. .key_i(key),
  113. .data_i(datain),
  114. .valid_i(validin),
  115. .data_o(dataout),
  116. .valid_o(validout)
  117. );
  118. endmodule