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.

154 lines
3.6 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_cbcmac_des;
  22. // set dumpfile
  23. initial begin
  24. $dumpfile ("tb_cbcmac_des.vcd");
  25. $dumpvars (0, tb_cbcmac_des);
  26. end
  27. reg reset;
  28. reg clk = 0;
  29. reg start;
  30. reg [0:63] key;
  31. reg [0:63] datain;
  32. reg validin;
  33. reg acceptin;
  34. integer index;
  35. integer outdex;
  36. integer errors;
  37. wire [0:63] dataout;
  38. wire validout;
  39. wire acceptout;
  40. reg [0:63] data_input [0:3];
  41. reg [0:63] key_input = 64'h0123456789abcdef;
  42. reg [0:63] data_output [0:3];
  43. // read in test data files
  44. initial begin
  45. $readmemh("data_input.txt", data_input);
  46. $readmemh("data_output.txt", data_output);
  47. end
  48. // setup simulation
  49. initial begin
  50. reset = 1;
  51. #1 reset = 0;
  52. #20 reset = 1;
  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. start <= 0;
  62. validin <= 0;
  63. key <= 0;
  64. datain <= 0;
  65. errors = 0;
  66. end
  67. // stimuli generator process
  68. initial
  69. forever @(posedge reset) begin
  70. @(posedge clk)
  71. for (index = 0; index < 4; index = index + 1)
  72. begin
  73. @(posedge acceptout)
  74. validin <= 1;
  75. datain <= data_input[index];
  76. if (index == 0) begin
  77. key <= key_input;
  78. start <= 1;
  79. end
  80. @(negedge acceptout)
  81. validin <= 0;
  82. start <= 0;
  83. key <= 0;
  84. end
  85. end
  86. // checker process
  87. always begin : checker
  88. wait (reset)
  89. acceptin <= 1;
  90. // encryption tests
  91. @(posedge clk)
  92. for(outdex = 0; outdex < 4; outdex = outdex + 1)
  93. begin
  94. @(posedge validout)
  95. // detected an error -> print error message
  96. // increment error counter
  97. if (dataout != data_output[outdex]) begin
  98. $display ("error, output was %h - should have been %h", dataout, data_output[outdex]);
  99. errors = errors + 1;
  100. end
  101. end
  102. // simulation finished -> print messages and if an error was detected
  103. $display ("#############");
  104. if (errors) begin
  105. $display ("Tests finished, %0d errors detected :(", errors);
  106. end else begin
  107. $display ("Tests finished, no errors detected :)");
  108. end
  109. $display ("#############");
  110. @(posedge clk)
  111. $finish;
  112. end
  113. // dut
  114. cbcmac_des i_cbcmac_des (
  115. .reset_i(reset),
  116. .clk_i(clk),
  117. .start_i(start),
  118. .key_i(key),
  119. .data_i(datain),
  120. .valid_i(validin),
  121. .accept_o(acceptout),
  122. .data_o(dataout),
  123. .valid_o(validout),
  124. .accept_i(acceptin)
  125. );
  126. endmodule