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.

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