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.

138 lines
3.3 KiB

  1. // ======================================================================
  2. // TDES encryption/decryption
  3. // algorithm according: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 tdes
  22. (
  23. input reset_i, // async reset
  24. input clk_i, // clock
  25. input mode_i, // des-mode: 0 = encrypt, 1 = decrypt
  26. input [0:63] key1_i, // key input
  27. input [0:63] key2_i, // key input
  28. input [0:63] key3_i, // key input
  29. input [0:63] data_i, // data input
  30. input valid_i, // input key/data valid flag
  31. output [0:63] data_o, // data output
  32. output valid_o, // output data valid flag
  33. output reg ready_o // ready for new data
  34. );
  35. reg mode;
  36. reg [0:63] key1;
  37. reg [0:63] key2;
  38. reg [0:63] key3;
  39. wire des2_mode;
  40. wire des1_validin;
  41. wire [0:63] des1_key;
  42. wire [0:63] des3_key;
  43. wire [0:63] des1_dataout;
  44. wire [0:63] des2_dataout;
  45. wire des1_validout;
  46. wire des2_validout;
  47. assign des2_mode = ~mode;
  48. assign des1_validin = valid_i & ready_o;
  49. assign des1_key = (~mode_i) ? key1_i : key3_i;
  50. assign des3_key = (~mode) ? key3 : key1;
  51. // input register
  52. always @(posedge clk_i, negedge reset_i) begin
  53. if (~reset_i) begin
  54. mode <= 0;
  55. key1 <= 0;
  56. key2 <= 0;
  57. key3 <= 0;
  58. end
  59. else begin
  60. if (valid_i && ready_o) begin
  61. mode <= mode_i;
  62. key1 <= key1_i;
  63. key2 <= key2_i;
  64. key3 <= key3_i;
  65. end
  66. end
  67. end
  68. // output register
  69. always @(posedge clk_i, negedge reset_i) begin
  70. if (~reset_i) begin
  71. ready_o <= 1;
  72. end
  73. else begin
  74. if (valid_i && ready_o) begin
  75. ready_o <= 0;
  76. end
  77. if (valid_o) begin
  78. ready_o <= 1;
  79. end
  80. end
  81. end
  82. des i1_des
  83. (
  84. .reset_i(reset_i),
  85. .clk_i(clk_i),
  86. .mode_i(mode_i),
  87. .key_i(des1_key),
  88. .data_i(data_i),
  89. .valid_i(des1_validin),
  90. .data_o(des1_dataout),
  91. .valid_o(des1_validout)
  92. );
  93. des i2_des
  94. (
  95. .reset_i(reset_i),
  96. .clk_i(clk_i),
  97. .mode_i(des2_mode),
  98. .key_i(key2),
  99. .data_i(des1_dataout),
  100. .valid_i(des1_validout),
  101. .data_o(des2_dataout),
  102. .valid_o(des2_validout)
  103. );
  104. des i3_des
  105. (
  106. .reset_i(reset_i),
  107. .clk_i(clk_i),
  108. .mode_i(mode),
  109. .key_i(des3_key),
  110. .data_i(des2_dataout),
  111. .valid_i(des2_validout),
  112. .data_o(data_o),
  113. .valid_o(valid_o)
  114. );
  115. endmodule