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.

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