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.

192 lines
6.9 KiB

  1. // ======================================================================
  2. // DES encryption/decryption
  3. // package file with functions
  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 to the Free Software
  18. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. // ======================================================================
  20. function [0:63] ip (input [0:63] data);
  21. begin
  22. ip = {data[57], data[49], data[41], data[33], data[25], data[17], data[ 9], data[1],
  23. data[59], data[51], data[43], data[35], data[27], data[19], data[11], data[3],
  24. data[61], data[53], data[45], data[37], data[29], data[21], data[13], data[5],
  25. data[63], data[55], data[47], data[39], data[31], data[23], data[15], data[7],
  26. data[56], data[48], data[40], data[32], data[24], data[16], data[ 8], data[0],
  27. data[58], data[50], data[42], data[34], data[26], data[18], data[10], data[2],
  28. data[60], data[52], data[44], data[36], data[28], data[20], data[12], data[4],
  29. data[62], data[54], data[46], data[38], data[30], data[22], data[14], data[6]};
  30. end
  31. endfunction
  32. function [0:63] ipn (input [0:63] data);
  33. begin
  34. ipn = {data[39], data[7], data[47], data[15], data[55], data[23], data[63], data[31],
  35. data[38], data[6], data[46], data[14], data[54], data[22], data[62], data[30],
  36. data[37], data[5], data[45], data[13], data[53], data[21], data[61], data[29],
  37. data[36], data[4], data[44], data[12], data[52], data[20], data[60], data[28],
  38. data[35], data[3], data[43], data[11], data[51], data[19], data[59], data[27],
  39. data[34], data[2], data[42], data[10], data[50], data[18], data[58], data[26],
  40. data[33], data[1], data[41], data[ 9], data[49], data[17], data[57], data[25],
  41. data[32], data[0], data[40], data[ 8], data[48], data[16], data[56], data[24]};
  42. end
  43. endfunction
  44. function [0:47] e (input [0:31] data);
  45. begin
  46. e = {data[31], data[ 0], data[ 1], data[ 2], data[ 3], data[ 4],
  47. data[ 3], data[ 4], data[ 5], data[ 6], data[ 7], data[ 8],
  48. data[ 7], data[ 8], data[ 9], data[10], data[11], data[12],
  49. data[11], data[12], data[13], data[14], data[15], data[16],
  50. data[15], data[16], data[17], data[18], data[19], data[20],
  51. data[19], data[20], data[21], data[22], data[23], data[24],
  52. data[23], data[24], data[25], data[26], data[27], data[28],
  53. data[27], data[28], data[29], data[30], data[31], data[ 0]};
  54. end
  55. endfunction
  56. function [0:3] s1 (input [0:5] data);
  57. reg [0:255] matrix;
  58. begin
  59. matrix = 256'hE4D12FB83A6C5907_0F74E2D1A6CB9538_41E8D62BFC973A50_FC8249175B3EA06D;
  60. s1 = matrix[{data[0],data[5]}*16 + data[1:4]];
  61. end
  62. endfunction
  63. function [0:3] s2 (input [0:5] data);
  64. reg [0:255] matrix;
  65. begin
  66. matrix = 256'hF18E6B34972DC05A_3D47F28DC01A69B5_0E7BA4D158C6932F_D8A13F42B67C05E9;
  67. s2 = matrix[{data[0],data[5]}*16 + data[1:4]];
  68. end
  69. endfunction
  70. function [0:3] s3 (input [0:5] data);
  71. reg [0:255] matrix;
  72. begin
  73. matrix = 256'hA09E63F51DC7B428_D709346A285ECBF1_D6498F30B12C5AE7_1AD069874FE3B52C;
  74. s3 = matrix[{data[0],data[5]}*16 + data[1:4]];
  75. end
  76. endfunction
  77. function [0:3] s4 (input [0:5] data);
  78. reg [0:255] matrix;
  79. begin
  80. matrix = 256'h7DE3069A1285BC4F_D8B56F03472C1AE9_A690CB7DF13E5284_3F06A1D8945BC72E;
  81. s4 = matrix[{data[0],data[5]}*16 + data[1:4]];
  82. end
  83. endfunction
  84. function [0:3] s5 (input [0:5] data);
  85. reg [0:255] matrix;
  86. begin
  87. matrix = 256'h2C417AB6853FD0E9_EB2C47D150FA3986_421BAD78F9C5630E_B8C71E2D6F09A453;
  88. s5 = matrix[{data[0],data[5]}*16 + data[1:4]];
  89. end
  90. endfunction
  91. function [0:3] s6 (input [0:5] data);
  92. reg [0:255] matrix;
  93. begin
  94. matrix = 256'hC1AF92680D34E75B_AF427C9561DE0B38_9EF528C3704A1DB6_432C95FABE17608D;
  95. s6 = matrix[{data[0],data[5]}*16 + data[1:4]];
  96. end
  97. endfunction
  98. function [0:3] s7 (input [0:5] data);
  99. reg [0:255] matrix;
  100. begin
  101. matrix = 256'h4B2EF08D3C975A61_D0B7491AE35C2F86_14BDC37EAF680592_6BD814A7950FE23C;
  102. s7 = matrix[{data[0],data[5]}*16 + data[1:4]];
  103. end
  104. endfunction
  105. function [0:3] s8 (input [0:5] data);
  106. reg [0:255] matrix;
  107. begin
  108. matrix = 256'hD2846FB1A93E50C7_1FD8A374C56B0E92_7B419CE206ADF358_21E74A8DFC90356B;
  109. s8 = matrix[{data[0],data[5]}*16 + data[1:4]];
  110. end
  111. endfunction
  112. function [0:31] p (input [0:31] data);
  113. begin
  114. p = {data[15], data[ 6], data[19], data[20],
  115. data[28], data[11], data[27], data[16],
  116. data[ 0], data[14], data[22], data[25],
  117. data[ 4], data[17], data[30], data[ 9],
  118. data[ 1], data[ 7], data[23], data[13],
  119. data[31], data[26], data[ 2], data[ 8],
  120. data[18], data[12], data[29], data[ 5],
  121. data[21], data[10], data[ 3], data[24]};
  122. end
  123. endfunction
  124. function [0:31] f (input [0:31] data, input [0:47] key);
  125. reg [0:47] intern;
  126. begin
  127. intern = e(data) ^ key;
  128. f = p({s1(intern[0:5]), s2(intern[6:11]), s3(intern[12:17]), s4(intern[18:23]),
  129. s5(intern[24:29]), s6(intern[30:35]), s7(intern[36:41]), s8(intern[42:47])});
  130. end
  131. endfunction
  132. function [0:27] pc1_c (input [0:63] data);
  133. begin
  134. pc1_c = {data[56], data[48], data[40], data[32], data[24], data[16], data[ 8],
  135. data[ 0], data[57], data[49], data[41], data[33], data[25], data[17],
  136. data[ 9], data[ 1], data[58], data[50], data[42], data[34], data[26],
  137. data[18], data[10], data[ 2], data[59], data[51], data[43], data[35]};
  138. end
  139. endfunction
  140. function [0:27] pc1_d (input [0:63] data);
  141. begin
  142. pc1_d = {data[62], data[54], data[46], data[38], data[30], data[22], data[14],
  143. data[ 6], data[61], data[53], data[45], data[37], data[29], data[21],
  144. data[13], data[ 5], data[60], data[52], data[44], data[36], data[28],
  145. data[20], data[12], data[ 4], data[27], data[19], data[11], data[ 3]};
  146. end
  147. endfunction
  148. function [0:47] pc2 (input [0:55] data);
  149. begin
  150. pc2 = {data[13], data[16], data[10], data[23], data[ 0], data[ 4],
  151. data[ 2], data[27], data[14], data[ 5], data[20], data[ 9],
  152. data[22], data[18], data[11], data[ 3], data[25], data[ 7],
  153. data[15], data[ 6], data[26], data[19], data[12], data[ 1],
  154. data[40], data[51], data[30], data[36], data[46], data[54],
  155. data[29], data[39], data[50], data[44], data[32], data[47],
  156. data[43], data[48], data[38], data[55], data[33], data[52],
  157. data[45], data[41], data[49], data[35], data[28], data[31]};
  158. end
  159. endfunction