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.

134 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. `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 errors;
  36. wire [0:63] dataout;
  37. wire validout;
  38. reg [0:63] data_input [0:127];
  39. reg [0:63] key_input [0:127];
  40. reg [0:63] data_output [0:127];
  41. // read in test data files
  42. initial begin
  43. $readmemh("data_input.txt", data_input);
  44. $readmemh("key_input.txt", key_input);
  45. $readmemh("data_output.txt", data_output);
  46. end
  47. // setup simulation
  48. initial begin
  49. reset = 1;
  50. #1 reset = 0;
  51. #20 reset = 1;
  52. #2000 $finish;
  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. errors = 0;
  66. end
  67. // stimuli generator process
  68. always begin : stimuli
  69. wait (reset)
  70. @(posedge clk)
  71. // Variable plaintext known answer test
  72. for(index = 0; index < 128; 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. validin <= 0;
  81. end
  82. // checker process
  83. always begin : checker
  84. wait (reset)
  85. // Variable plaintext known answer test
  86. wait (validout)
  87. for(outdex = 0; outdex < 128; outdex = outdex + 1)
  88. begin
  89. @(posedge clk)
  90. // detected an error -> print error message
  91. // increent error counter
  92. if (dataout != data_output[outdex]) begin
  93. $display ("error, output was %h - should have been %h", dataout, data_output[outdex]);
  94. errors = errors + 1;
  95. end
  96. end
  97. // simulation finished -> print messages and if an error was detected
  98. $display ("#############");
  99. if (errors) begin
  100. $display ("test finished, %0d errors detected :(", errors);
  101. end else begin
  102. $display ("test finished, no errors detected :)");
  103. end
  104. $display ("#############");
  105. @(posedge clk)
  106. $finish;
  107. end
  108. // dut
  109. des i_des (
  110. .reset_i(reset),
  111. .clk_i(clk),
  112. .mode_i(mode),
  113. .key_i(key),
  114. .data_i(datain),
  115. .valid_i(validin),
  116. .data_o(dataout),
  117. .valid_o(validout)
  118. );
  119. endmodule