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.

146 lines
3.7 KiB

  1. // ======================================================================
  2. // CBC-DES encryption/decryption
  3. // algorithm according to FIPS 46-3 specification
  4. // Copyright (C) 2013 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:the Free Software
  18. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. // ======================================================================
  20. `timescale 1ns/1ps
  21. module cbctdes
  22. (
  23. input reset_i, // async reset
  24. input clk_i, // clock
  25. input start_i, // start cbc
  26. input mode_i, // des-mode: 0 = encrypt, 1 = decrypt
  27. input [0:63] key1_i, // key input
  28. input [0:63] key2_i, // key input
  29. input [0:63] key3_i, // key input
  30. input [0:63] iv_i, // iv input
  31. input [0:63] data_i, // data input
  32. input valid_i, // input key/data valid flag
  33. output reg ready_o, // ready to encrypt/decrypt
  34. output reg [0:63] data_o, // data output
  35. output valid_o // output data valid flag
  36. );
  37. reg mode;
  38. wire tdes_mode;
  39. reg start;
  40. reg [0:63] key;
  41. wire [0:63] tdes_key1;
  42. wire [0:63] tdes_key2;
  43. wire [0:63] tdes_key3;
  44. reg [0:63] iv;
  45. reg [0:63] datain;
  46. reg [0:63] datain_d;
  47. reg [0:63] tdes_datain;
  48. wire validin;
  49. wire [0:63] tdes_dataout;
  50. reg reset;
  51. reg [0:63] dataout;
  52. always @(*) begin
  53. if (~mode_i && start_i) begin
  54. tdes_datain = iv_i ^ data_i;
  55. end
  56. else if (~mode && ~start_i) begin
  57. tdes_datain = dataout ^ data_i;
  58. end
  59. else begin
  60. tdes_datain = data_i;
  61. end
  62. end
  63. always @(*) begin
  64. if (mode && start) begin
  65. data_o = iv ^ tdes_dataout;
  66. end
  67. else if (mode && ~start) begin
  68. data_o = datain_d ^ tdes_dataout;
  69. end
  70. else begin
  71. data_o = tdes_dataout;
  72. end
  73. end
  74. // input register
  75. always @(posedge clk_i, negedge reset_i) begin
  76. if (~reset_i) begin
  77. reset <= 0;
  78. mode <= 0;
  79. start <= 0;
  80. key <= 0;
  81. iv <= 0;
  82. datain <= 0;
  83. datain_d <= 0;
  84. end
  85. else begin
  86. reset <= reset_i;
  87. if (valid_i && ready_o) begin
  88. start <= start_i;
  89. datain <= data_i;
  90. datain_d <= datain;
  91. end
  92. else if (valid_i && ready_o && start_i) begin
  93. mode <= mode_i;
  94. key <= key_i;
  95. iv <= iv_i;
  96. end
  97. end
  98. end
  99. // output register
  100. always @(posedge clk_i, negedge reset_i) begin
  101. if (~reset_i) begin
  102. ready_o <= 0;
  103. dataout <= 0;
  104. end
  105. else begin
  106. if (valid_i && ready_o) begin
  107. ready_o <= 0;
  108. end
  109. else if (valid_o || (reset_i && ~reset)) begin
  110. ready_o <= 1;
  111. dataout <= tdes_dataout;
  112. end
  113. end
  114. end
  115. // des instance
  116. tdes i_tdes (
  117. .reset_i(reset),
  118. .clk_i(clk_i),
  119. .mode_i(tdes_mode),
  120. .key1_i(tdes_key1),
  121. .key2_i(tdes_key2),
  122. .key3_i(tdes_key3),
  123. .data_i(tdes_datain),
  124. .valid_i(validin),
  125. .data_o(tdes_dataout),
  126. .valid_o(valid_o),
  127. .ready_o(tdesready)
  128. );
  129. endmodule