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.

129 lines
3.1 KiB

  1. // ======================================================================
  2. // TDES encryption/decryption
  3. // algorithm according:FIPS 46-3 specification
  4. // Copyright (C) 2012 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 reg [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. `include "../../rtl/verilog/des.v"
  36. reg reset;
  37. reg mode;
  38. reg [0:63] key1;
  39. reg [0:63] key2;
  40. reg [0:63] key3;
  41. reg ready_o;
  42. // input register
  43. always @(posedge clk_i, negedge reset_i) begin
  44. if (~reset_i) begin
  45. reset <= 0;
  46. mode <= 0;
  47. key1 <= 0;
  48. key2 <= 0;
  49. key3 <= 0;
  50. end
  51. else begin
  52. reset <= reset_i;
  53. if (valid_i && ready_o) begin
  54. mode <= mode_i;
  55. key1 <= key1_i;
  56. key2 <= key2_i;
  57. key3 <= key3_i;
  58. end
  59. end
  60. end
  61. // output register
  62. always @(posedge clk_i, negedge reset_i) begin
  63. if (~reset_i) begin
  64. ready_o <= 0;
  65. end
  66. else begin
  67. if (valid_i && ready_o) begin
  68. ready_o <= 0;
  69. end
  70. if (valid_o || (reset_i && ~reset)) begin
  71. ready_o <= 1;
  72. end
  73. end
  74. end
  75. des : i1_des
  76. (
  77. .reset_i(reset_i),
  78. .clk_i(clk_i),
  79. .mode_i(mode_i),
  80. .key_i(des1_key),
  81. .data_i(data_i),
  82. .valid_i(des1_validin),
  83. .data_o(des1_dataout),
  84. .valid_o(des1_validout)
  85. );
  86. des : i2_des
  87. (
  88. .reset_i(reset_i),
  89. .clk_i(clk_i),
  90. .mode_i(des2_mode),
  91. .key_i(key2),
  92. .data_i(des1_dataout),
  93. .valid_i(des1_validout),
  94. .data_o(des2_dataout),
  95. .valid_o(des2_validout)
  96. );
  97. des : i3_des
  98. (
  99. .reset_i(reset_i),
  100. .clk_i(clk_i),
  101. .mode_i(mode_i),
  102. .key_i(des1_key),
  103. .data_i(des2_dataout),
  104. .valid_i(des2_validout),
  105. .data_o(data_o),
  106. .valid_o(valid_o)
  107. );
  108. endmodule