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.

246 lines
8.0 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:31] ip0 (input [0:63] data);
  21. begin
  22. ip0 = {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. end
  27. endfunction
  28. function [0:31] ip1 (input [0:63] data);
  29. begin
  30. ip1 = {data[56], data[48], data[40], data[32], data[24], data[16], data[ 8], data[0],
  31. data[58], data[50], data[42], data[34], data[26], data[18], data[10], data[2],
  32. data[60], data[52], data[44], data[36], data[28], data[20], data[12], data[4],
  33. data[62], data[54], data[46], data[38], data[30], data[22], data[14], data[6]};
  34. end
  35. endfunction
  36. function [0:63] ipn (input [0:63] data);
  37. begin
  38. ipn = {data[39], data[7], data[47], data[15], data[55], data[23], data[63], data[31],
  39. data[38], data[6], data[46], data[14], data[54], data[22], data[62], data[30],
  40. data[37], data[5], data[45], data[13], data[53], data[21], data[61], data[29],
  41. data[36], data[4], data[44], data[12], data[52], data[20], data[60], data[28],
  42. data[35], data[3], data[43], data[11], data[51], data[19], data[59], data[27],
  43. data[34], data[2], data[42], data[10], data[50], data[18], data[58], data[26],
  44. data[33], data[1], data[41], data[ 9], data[49], data[17], data[57], data[25],
  45. data[32], data[0], data[40], data[ 8], data[48], data[16], data[56], data[24]};
  46. end
  47. endfunction
  48. function [0:47] e (input [0:31] data);
  49. begin
  50. e = {data[31], data[ 0], data[ 1], data[ 2], data[ 3], data[ 4],
  51. data[ 3], data[ 4], data[ 5], data[ 6], data[ 7], data[ 8],
  52. data[ 7], data[ 8], data[ 9], data[10], data[11], data[12],
  53. data[11], data[12], data[13], data[14], data[15], data[16],
  54. data[15], data[16], data[17], data[18], data[19], data[20],
  55. data[19], data[20], data[21], data[22], data[23], data[24],
  56. data[23], data[24], data[25], data[26], data[27], data[28],
  57. data[27], data[28], data[29], data[30], data[31], data[ 0]};
  58. end
  59. endfunction
  60. function [0:3] s1 (input [0:5] data);
  61. reg [0:255] matrix;
  62. integer pos;
  63. integer index;
  64. begin
  65. matrix = 256'hE4D12FB83A6C5907_0F74E2D1A6CB9538_41E8D62BFC973A50_FC8249175B3EA06D;
  66. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  67. for(index = 0 ; index <= 3; index = index + 1)
  68. begin
  69. s1[index] = matrix[pos + index];
  70. end
  71. end
  72. endfunction
  73. function [0:3] s2 (input [0:5] data);
  74. reg [0:255] matrix;
  75. integer pos;
  76. integer index;
  77. begin
  78. matrix = 256'hF18E6B34972DC05A_3D47F28EC01A69B5_0E7BA4D158C6932F_D8A13F42B67C05E9;
  79. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  80. for(index = 0 ; index <= 3; index = index + 1)
  81. begin
  82. s2[index] = matrix[pos + index];
  83. end
  84. end
  85. endfunction
  86. function [0:3] s3 (input [0:5] data);
  87. reg [0:255] matrix;
  88. integer pos;
  89. integer index;
  90. begin
  91. matrix = 256'hA09E63F51DC7B428_D709346A285ECBF1_D6498F30B12C5AE7_1AD069874FE3B52C;
  92. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  93. for(index = 0 ; index <= 3; index = index + 1)
  94. begin
  95. s3[index] = matrix[pos + index];
  96. end
  97. end
  98. endfunction
  99. function [0:3] s4 (input [0:5] data);
  100. reg [0:255] matrix;
  101. integer pos;
  102. integer index;
  103. begin
  104. matrix = 256'h7DE3069A1285BC4F_D8B56F03472C1AE9_A690CB7DF13E5284_3F06A1D8945BC72E;
  105. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  106. for(index = 0 ; index <= 3; index = index + 1)
  107. begin
  108. s4[index] = matrix[pos + index];
  109. end
  110. end
  111. endfunction
  112. function [0:3] s5 (input [0:5] data);
  113. reg [0:255] matrix;
  114. integer pos;
  115. integer index;
  116. begin
  117. matrix = 256'h2C417AB6853FD0E9_EB2C47D150FA3986_421BAD78F9C5630E_B8C71E2D6F09A453;
  118. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  119. for(index = 0 ; index <= 3; index = index + 1)
  120. begin
  121. s5[index] = matrix[pos + index];
  122. end
  123. end
  124. endfunction
  125. function [0:3] s6 (input [0:5] data);
  126. reg [0:255] matrix;
  127. integer pos;
  128. integer index;
  129. begin
  130. matrix = 256'hC1AF92680D34E75B_AF427C9561DE0B38_9EF528C3704A1DB6_432C95FABE17608D;
  131. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  132. for(index = 0 ; index <= 3; index = index + 1)
  133. begin
  134. s6[index] = matrix[pos + index];
  135. end
  136. end
  137. endfunction
  138. function [0:3] s7 (input [0:5] data);
  139. reg [0:255] matrix;
  140. integer pos;
  141. integer index;
  142. begin
  143. matrix = 256'h4B2EF08D3C975A61_D0B7491AE35C2F86_14BDC37EAF680592_6BD814A7950FE23C;
  144. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  145. for(index = 0 ; index <= 3; index = index + 1)
  146. begin
  147. s7[index] = matrix[pos + index];
  148. end
  149. end
  150. endfunction
  151. function [0:3] s8 (input [0:5] data);
  152. reg [0:255] matrix;
  153. integer pos;
  154. integer index;
  155. begin
  156. matrix = 256'hD2846FB1A93E50C7_1FD8A374C56B0E92_7B419CE206ADF358_21E74A8DFC90356B;
  157. pos = {data[0], data[5]} * 64 + data[1:4] * 4;
  158. for(index = 0 ; index <= 3; index = index + 1)
  159. begin
  160. s8[index] = matrix[pos + index];
  161. end
  162. end
  163. endfunction
  164. function [0:31] p (input [0:31] data);
  165. begin
  166. p = {data[15], data[ 6], data[19], data[20],
  167. data[28], data[11], data[27], data[16],
  168. data[ 0], data[14], data[22], data[25],
  169. data[ 4], data[17], data[30], data[ 9],
  170. data[ 1], data[ 7], data[23], data[13],
  171. data[31], data[26], data[ 2], data[ 8],
  172. data[18], data[12], data[29], data[ 5],
  173. data[21], data[10], data[ 3], data[24]};
  174. end
  175. endfunction
  176. function [0:31] f (input [0:31] data, input [0:47] key);
  177. reg [0:47] intern;
  178. begin
  179. intern = e(data) ^ key;
  180. f = p({s1(intern[0:5]), s2(intern[6:11]), s3(intern[12:17]), s4(intern[18:23]),
  181. s5(intern[24:29]), s6(intern[30:35]), s7(intern[36:41]), s8(intern[42:47])});
  182. end
  183. endfunction
  184. function [0:27] pc1_c (input [0:63] data);
  185. begin
  186. pc1_c = {data[56], data[48], data[40], data[32], data[24], data[16], data[ 8],
  187. data[ 0], data[57], data[49], data[41], data[33], data[25], data[17],
  188. data[ 9], data[ 1], data[58], data[50], data[42], data[34], data[26],
  189. data[18], data[10], data[ 2], data[59], data[51], data[43], data[35]};
  190. end
  191. endfunction
  192. function [0:27] pc1_d (input [0:63] data);
  193. begin
  194. pc1_d = {data[62], data[54], data[46], data[38], data[30], data[22], data[14],
  195. data[ 6], data[61], data[53], data[45], data[37], data[29], data[21],
  196. data[13], data[ 5], data[60], data[52], data[44], data[36], data[28],
  197. data[20], data[12], data[ 4], data[27], data[19], data[11], data[ 3]};
  198. end
  199. endfunction
  200. function [0:47] pc2 (input [0:55] data);
  201. begin
  202. pc2 = {data[13], data[16], data[10], data[23], data[ 0], data[ 4],
  203. data[ 2], data[27], data[14], data[ 5], data[20], data[ 9],
  204. data[22], data[18], data[11], data[ 3], data[25], data[ 7],
  205. data[15], data[ 6], data[26], data[19], data[12], data[ 1],
  206. data[40], data[51], data[30], data[36], data[46], data[54],
  207. data[29], data[39], data[50], data[44], data[32], data[47],
  208. data[43], data[48], data[38], data[55], data[33], data[52],
  209. data[45], data[41], data[49], data[35], data[28], data[31]};
  210. end
  211. endfunction