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.

161 lines
4.1 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] key1;
  45. reg [0:63] key2;
  46. reg [0:63] key3;
  47. reg [0:63] iv;
  48. reg [0:63] datain;
  49. reg [0:63] datain_d;
  50. reg [0:63] tdes_datain;
  51. wire validin;
  52. wire [0:63] tdes_dataout;
  53. reg reset;
  54. reg [0:63] dataout;
  55. wire tdes_ready;
  56. always @(*) begin
  57. if (~mode_i && start_i) begin
  58. tdes_datain = iv_i ^ data_i;
  59. end
  60. else if (~mode && ~start_i) begin
  61. tdes_datain = dataout ^ data_i;
  62. end
  63. else begin
  64. tdes_datain = data_i;
  65. end
  66. end
  67. always @(*) begin
  68. if (mode && start) begin
  69. data_o = iv ^ tdes_dataout;
  70. end
  71. else if (mode && ~start) begin
  72. data_o = datain_d ^ tdes_dataout;
  73. end
  74. else begin
  75. data_o = tdes_dataout;
  76. end
  77. end
  78. assign tdes_key1 = start_i ? key1_i : key1;
  79. assign tdes_key2 = start_i ? key2_i : key2;
  80. assign tdes_key3 = start_i ? key3_i : key3;
  81. assign validin = valid_i & ready_o;
  82. // input register
  83. always @(posedge clk_i, negedge reset_i) begin
  84. if (~reset_i) begin
  85. reset <= 0;
  86. mode <= 0;
  87. start <= 0;
  88. key1 <= 0;
  89. key2 <= 0;
  90. key3 <= 0;
  91. iv <= 0;
  92. datain <= 0;
  93. datain_d <= 0;
  94. end
  95. else begin
  96. reset <= reset_i;
  97. if (valid_i && ready_o) begin
  98. start <= start_i;
  99. datain <= data_i;
  100. datain_d <= datain;
  101. end
  102. else if (valid_i && ready_o && start_i) begin
  103. mode <= mode_i;
  104. key1 <= key1_i;
  105. key2 <= key2_i;
  106. key3 <= key3_i;
  107. iv <= iv_i;
  108. end
  109. end
  110. end
  111. // output register
  112. always @(posedge clk_i, negedge reset_i) begin
  113. if (~reset_i) begin
  114. ready_o <= 0;
  115. dataout <= 0;
  116. end
  117. else begin
  118. if (valid_i && ready_o && tdes_ready) begin
  119. ready_o <= 0;
  120. end
  121. else if (valid_o || (reset_i && ~reset)) begin
  122. ready_o <= 1;
  123. dataout <= tdes_dataout;
  124. end
  125. end
  126. end
  127. // des instance
  128. tdes i_tdes (
  129. .reset_i(reset),
  130. .clk_i(clk_i),
  131. .mode_i(tdes_mode),
  132. .key1_i(tdes_key1),
  133. .key2_i(tdes_key2),
  134. .key3_i(tdes_key3),
  135. .data_i(tdes_datain),
  136. .valid_i(validin),
  137. .data_o(tdes_dataout),
  138. .valid_o(valid_o),
  139. .ready_o(tdes_ready)
  140. );
  141. endmodule