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.

145 lines
3.6 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 cbcdes
  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] key_i, // key input
  28. input [0:63] iv_i, // iv input
  29. input [0:63] data_i, // data input
  30. input valid_i, // input key/data valid flag
  31. output reg ready_o, // ready to encrypt/decrypt
  32. output reg [0:63] data_o, // data output
  33. output valid_o // output data valid flag
  34. );
  35. reg mode;
  36. wire des_mode;
  37. reg start;
  38. reg [0:63] key;
  39. wire [0:63] des_key;
  40. reg [0:63] iv;
  41. reg [0:63] datain;
  42. reg [0:63] datain_d;
  43. reg [0:63] des_datain;
  44. wire validin;
  45. wire [0:63] des_dataout;
  46. reg reset;
  47. reg [0:63] dataout;
  48. assign des_key = (start_i) ? key_i : key;
  49. assign des_mode = (start_i) ? mode_i : mode;
  50. assign validin = valid_i & ready_o;
  51. always @(*) begin
  52. if (~mode_i && start_i) begin
  53. des_datain = iv_i ^ data_i;
  54. end
  55. else if (~mode && ~start_i) begin
  56. des_datain = dataout ^ data_i;
  57. end
  58. else begin
  59. des_datain = data_i;
  60. end
  61. end
  62. always @(*) begin
  63. if (mode && start) begin
  64. data_o = iv ^ des_dataout;
  65. end
  66. else if (mode && ~start) begin
  67. data_o = datain_d ^ des_dataout;
  68. end
  69. else begin
  70. data_o = des_dataout;
  71. end
  72. end
  73. // input register
  74. always @(posedge clk_i, negedge reset_i) begin
  75. if (~reset_i) begin
  76. reset <= 0;
  77. mode <= 0;
  78. start <= 0;
  79. key <= 0;
  80. iv <= 0;
  81. datain <= 0;
  82. datain_d <= 0;
  83. end
  84. else begin
  85. reset <= reset_i;
  86. if (valid_i && ready_o) begin
  87. start <= start_i;
  88. datain <= data_i;
  89. datain_d <= datain;
  90. end
  91. else if (valid_i && ready_o && start_i) begin
  92. mode <= mode_i;
  93. key <= key_i;
  94. iv <= iv_i;
  95. end
  96. end
  97. end
  98. // output register
  99. always @(posedge clk_i, negedge reset_i) begin
  100. if (~reset_i) begin
  101. ready_o <= 0;
  102. dataout <= 0;
  103. end
  104. else begin
  105. if (valid_i && ready_o) begin
  106. ready_o <= 0;
  107. end
  108. else if (valid_o || (reset_i && ~reset)) begin
  109. ready_o <= 1;
  110. dataout <= des_dataout;
  111. end
  112. end
  113. end
  114. // des instance
  115. des i_des (
  116. .reset_i(reset),
  117. .clk_i(clk_i),
  118. .mode_i(des_mode),
  119. .key_i(des_key),
  120. .data_i(des_datain),
  121. .valid_i(validin),
  122. .data_o(des_dataout),
  123. .valid_o(valid_o)
  124. );
  125. endmodule