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.

328 lines
16 KiB

  1. -- ======================================================================
  2. -- DES encryption/decryption
  3. -- package file with functions
  4. -- Copyright (C) 2007 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. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. -- GNU General Public License for more details.
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program; if not, write to the Free Software
  16. -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. -- ======================================================================
  18. LIBRARY ieee;
  19. USE ieee.std_logic_1164.all;
  20. USE ieee.numeric_std.ALL;
  21. PACKAGE des_pkg IS
  22. FUNCTION ip ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector;
  23. FUNCTION ipn ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector;
  24. FUNCTION e (input_vector : std_logic_vector(0 TO 31) ) RETURN std_logic_vector;
  25. FUNCTION p (input_vector : std_logic_vector(0 TO 31) ) RETURN std_logic_vector;
  26. FUNCTION s1 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  27. FUNCTION s2 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  28. FUNCTION s3 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  29. FUNCTION s4 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  30. FUNCTION s5 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  31. FUNCTION s6 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  32. FUNCTION s7 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  33. FUNCTION s8 (input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector;
  34. FUNCTION f (input_r : std_logic_vector(0 TO 31); input_key : std_logic_vector(0 TO 47) ) RETURN std_logic_vector;
  35. FUNCTION pc1_c ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector;
  36. FUNCTION pc1_d ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector;
  37. FUNCTION pc2 ( input_vector : std_logic_vector(0 TO 55) ) RETURN std_logic_vector;
  38. END PACKAGE des_pkg;
  39. PACKAGE BODY des_pkg IS
  40. FUNCTION ip ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  41. TYPE matrix IS ARRAY (0 TO 63) OF natural RANGE 0 TO 63;
  42. VARIABLE table : matrix := (57, 49, 41, 33, 25, 17, 9, 1,
  43. 59, 51, 43, 35, 27, 19, 11, 3,
  44. 61, 53, 45, 37, 29, 21, 13, 5,
  45. 63, 55, 47, 39, 31, 23, 15, 7,
  46. 56, 48, 40, 32, 24, 16, 8, 0,
  47. 58, 50, 42, 34, 26, 18, 10, 2,
  48. 60, 52, 44, 36, 28, 20, 12, 4,
  49. 62, 54, 46, 38, 30, 22, 14, 6);
  50. VARIABLE result : std_logic_vector(0 TO 63);
  51. BEGIN
  52. FOR index IN 0 TO 63 LOOP
  53. result( index ) := input_vector( table( index ) );
  54. END LOOP;
  55. RETURN result;
  56. END FUNCTION ip;
  57. FUNCTION ipn ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  58. TYPE matrix IS ARRAY (0 TO 63) OF natural RANGE 0 TO 63;
  59. VARIABLE table : matrix := (39, 7, 47, 15, 55, 23, 63, 31,
  60. 38, 6, 46, 14, 54, 22, 62, 30,
  61. 37, 5, 45, 13, 53, 21, 61, 29,
  62. 36, 4, 44, 12, 52, 20, 60, 28,
  63. 35, 3, 43, 11, 51, 19, 59, 27,
  64. 34, 2, 42, 10, 50, 18, 58, 26,
  65. 33, 1, 41, 9, 49, 17, 57, 25,
  66. 32, 0, 40, 8, 48, 16, 56, 24);
  67. VARIABLE result : std_logic_vector(0 TO 63);
  68. BEGIN
  69. FOR index IN 0 TO 63 LOOP
  70. result( index ) := input_vector( table( index ) );
  71. END LOOP;
  72. RETURN result;
  73. END FUNCTION ipn;
  74. FUNCTION e (input_vector : std_logic_vector(0 TO 31) ) RETURN std_logic_vector IS
  75. TYPE matrix IS ARRAY (0 TO 47) OF natural RANGE 0 TO 31;
  76. VARIABLE table : matrix := (31, 0, 1, 2, 3, 4,
  77. 3, 4, 5, 6, 7, 8,
  78. 7, 8, 9, 10, 11, 12,
  79. 11, 12, 13, 14, 15, 16,
  80. 15, 16, 17, 18, 19, 20,
  81. 19, 20, 21, 22, 23, 24,
  82. 23, 24, 25, 26, 27, 28,
  83. 27, 28, 29, 30, 31, 0);
  84. VARIABLE result : std_logic_vector(0 TO 47);
  85. BEGIN
  86. FOR index IN 0 TO 47 LOOP
  87. result( index ) := input_vector( table( index ) );
  88. END LOOP;
  89. RETURN result;
  90. END FUNCTION e;
  91. FUNCTION s1 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  92. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  93. VARIABLE table : matrix := (0 => (14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7),
  94. 1 => ( 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8),
  95. 2 => ( 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0),
  96. 3 => (15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13));
  97. VARIABLE int : std_logic_vector(0 TO 1);
  98. VARIABLE i : integer RANGE 0 TO 3;
  99. VARIABLE j : integer RANGE 0 TO 15;
  100. VARIABLE result : std_logic_vector(0 TO 3);
  101. BEGIN
  102. int := input_vector( 0 ) & input_vector( 5 );
  103. i := to_integer( unsigned( int ) );
  104. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  105. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  106. RETURN result;
  107. END FUNCTION s1;
  108. FUNCTION s2 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  109. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  110. VARIABLE table : matrix := (0 => (15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10),
  111. 1 => ( 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5),
  112. 2 => ( 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15),
  113. 3 => (13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9));
  114. VARIABLE int : std_logic_vector(0 TO 1);
  115. VARIABLE i : integer RANGE 0 TO 3;
  116. VARIABLE j : integer RANGE 0 TO 15;
  117. VARIABLE result : std_logic_vector(0 TO 3);
  118. BEGIN
  119. int := input_vector( 0 ) & input_vector( 5 );
  120. i := to_integer( unsigned( int ) );
  121. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  122. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  123. RETURN result;
  124. END FUNCTION s2;
  125. FUNCTION s3 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  126. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  127. VARIABLE table : matrix := (0 => (10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8),
  128. 1 => (13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1),
  129. 2 => (13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7),
  130. 3 => ( 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12));
  131. VARIABLE int : std_logic_vector(0 TO 1);
  132. VARIABLE i : integer RANGE 0 TO 3;
  133. VARIABLE j : integer RANGE 0 TO 15;
  134. VARIABLE result : std_logic_vector(0 TO 3);
  135. BEGIN
  136. int := input_vector( 0 ) & input_vector( 5 );
  137. i := to_integer( unsigned( int ) );
  138. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  139. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  140. RETURN result;
  141. END FUNCTION s3;
  142. FUNCTION s4 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  143. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  144. VARIABLE table : matrix := (0 => ( 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15),
  145. 1 => (13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9),
  146. 2 => (10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4),
  147. 3 => ( 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14));
  148. VARIABLE int : std_logic_vector(0 TO 1);
  149. VARIABLE i : integer RANGE 0 TO 3;
  150. VARIABLE j : integer RANGE 0 TO 15;
  151. VARIABLE result : std_logic_vector(0 TO 3);
  152. BEGIN
  153. int := input_vector( 0 ) & input_vector( 5 );
  154. i := to_integer( unsigned( int ) );
  155. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  156. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  157. RETURN result;
  158. END FUNCTION s4;
  159. FUNCTION s5 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  160. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  161. VARIABLE table : matrix := (0 => ( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9),
  162. 1 => (14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6),
  163. 2 => ( 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14),
  164. 3 => (11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3));
  165. VARIABLE int : std_logic_vector(0 TO 1);
  166. VARIABLE i : integer RANGE 0 TO 3;
  167. VARIABLE j : integer RANGE 0 TO 15;
  168. VARIABLE result : std_logic_vector(0 TO 3);
  169. BEGIN
  170. int := input_vector( 0 ) & input_vector( 5 );
  171. i := to_integer( unsigned( int ) );
  172. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  173. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  174. RETURN result;
  175. END FUNCTION s5;
  176. FUNCTION s6 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  177. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  178. VARIABLE table : matrix := (0 => (12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11),
  179. 1 => (10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8),
  180. 2 => ( 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6),
  181. 3 => ( 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13));
  182. VARIABLE int : std_logic_vector(0 TO 1);
  183. VARIABLE i : integer RANGE 0 TO 3;
  184. VARIABLE j : integer RANGE 0 TO 15;
  185. VARIABLE result : std_logic_vector(0 TO 3);
  186. BEGIN
  187. int := input_vector( 0 ) & input_vector( 5 );
  188. i := to_integer( unsigned( int ) );
  189. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  190. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  191. RETURN result;
  192. END FUNCTION s6;
  193. FUNCTION s7 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  194. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  195. VARIABLE table : matrix := (0 => ( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1),
  196. 1 => (13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6),
  197. 2 => ( 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2),
  198. 3 => ( 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12));
  199. VARIABLE int : std_logic_vector(0 TO 1);
  200. VARIABLE i : integer RANGE 0 TO 3;
  201. VARIABLE j : integer RANGE 0 TO 15;
  202. VARIABLE result : std_logic_vector(0 TO 3);
  203. BEGIN
  204. int := input_vector( 0 ) & input_vector( 5 );
  205. i := to_integer( unsigned( int ) );
  206. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  207. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  208. RETURN result;
  209. END FUNCTION s7;
  210. FUNCTION s8 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  211. TYPE matrix IS ARRAY (0 TO 3, 0 TO 15) OF integer RANGE 0 TO 15;
  212. VARIABLE table : matrix := (0 => (13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7),
  213. 1 => ( 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2),
  214. 2 => ( 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8),
  215. 3 => ( 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11));
  216. VARIABLE int : std_logic_vector(0 TO 1);
  217. VARIABLE i : integer RANGE 0 TO 3;
  218. VARIABLE j : integer RANGE 0 TO 15;
  219. VARIABLE result : std_logic_vector(0 TO 3);
  220. BEGIN
  221. int := input_vector( 0 ) & input_vector( 5 );
  222. i := to_integer( unsigned( int ) );
  223. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  224. result := std_logic_vector( to_unsigned( table( i, j ), 4 ) );
  225. RETURN result;
  226. END FUNCTION s8;
  227. FUNCTION p (input_vector : std_logic_vector(0 TO 31) ) RETURN std_logic_vector IS
  228. TYPE matrix IS ARRAY (0 TO 31) OF natural RANGE 0 TO 31;
  229. VARIABLE table : matrix := (15, 6, 19, 20,
  230. 28, 11, 27, 16,
  231. 0, 14, 22, 25,
  232. 4, 17, 30, 9,
  233. 1, 7, 23, 13,
  234. 31, 26, 2, 8,
  235. 18, 12, 29, 5,
  236. 21, 10, 3, 24);
  237. VARIABLE result : std_logic_vector(0 TO 31);
  238. BEGIN
  239. FOR index IN 0 TO 31 LOOP
  240. result( index ) := input_vector( table( index ) );
  241. END LOOP;
  242. RETURN result;
  243. END FUNCTION p;
  244. FUNCTION f (input_r : std_logic_vector(0 TO 31); input_key : std_logic_vector(0 TO 47) ) RETURN std_logic_vector IS
  245. VARIABLE intern : std_logic_vector(0 TO 47);
  246. VARIABLE result : std_logic_vector(0 TO 31);
  247. BEGIN
  248. intern := e( input_r ) xor input_key;
  249. result := p( s1( intern(0 TO 5) ) & s2( intern(6 TO 11) ) & s3( intern(12 TO 17) ) & s4( intern(18 TO 23) ) &
  250. s5( intern(24 TO 29) ) & s6( intern(30 TO 35) ) & s7( intern(36 TO 41) ) & s8( intern(42 TO 47) ) );
  251. RETURN result;
  252. END FUNCTION f;
  253. FUNCTION pc1_c ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  254. TYPE matrix IS ARRAY (0 TO 27) OF natural RANGE 0 TO 63;
  255. VARIABLE table : matrix := (56, 48, 40, 32, 24, 16, 8,
  256. 0, 57, 49, 41, 33, 25, 17,
  257. 9, 1, 58, 50, 42, 34, 26,
  258. 18, 10, 2, 59, 51, 43, 35);
  259. VARIABLE result : std_logic_vector(0 TO 27);
  260. BEGIN
  261. FOR index IN 0 TO 27 LOOP
  262. result( index ) := input_vector( table( index ) );
  263. END LOOP;
  264. RETURN result;
  265. END FUNCTION pc1_c;
  266. FUNCTION pc1_d ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  267. TYPE matrix IS ARRAY (0 TO 27) OF natural RANGE 0 TO 63;
  268. VARIABLE table : matrix := (62, 54, 46, 38, 30, 22, 14,
  269. 6, 61, 53, 45, 37, 29, 21,
  270. 13, 5, 60, 52, 44, 36, 28,
  271. 20, 12, 4, 27, 19, 11, 3);
  272. VARIABLE result : std_logic_vector(0 TO 27);
  273. BEGIN
  274. FOR index IN 0 TO 27 LOOP
  275. result( index ) := input_vector( table( index ) );
  276. END LOOP;
  277. RETURN result;
  278. END FUNCTION pc1_d;
  279. FUNCTION pc2 ( input_vector : std_logic_vector(0 TO 55) ) RETURN std_logic_vector IS
  280. TYPE matrix IS ARRAY (0 TO 47) OF natural RANGE 0 TO 63;
  281. VARIABLE table : matrix := (13, 16, 10, 23, 0, 4,
  282. 2, 27, 14, 5, 20, 9,
  283. 22, 18, 11, 3, 25, 7,
  284. 15, 6, 26, 19, 12, 1,
  285. 40, 51, 30, 36, 46, 54,
  286. 29, 39, 50, 44, 32, 47,
  287. 43, 48, 38, 55, 33, 52,
  288. 45, 41, 49, 35, 28, 31);
  289. VARIABLE result : std_logic_vector(0 TO 47);
  290. BEGIN
  291. FOR index IN 0 TO 47 LOOP
  292. result( index ) := input_vector( table( index ) );
  293. END LOOP;
  294. RETURN result;
  295. END FUNCTION pc2;
  296. END PACKAGE BODY des_pkg;