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.

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