cryptography ip-cores in vhdl / verilog
vhdl
osvvm
fpga
ghdl
testbenches
verilog
cryptography

des_pkg.vhd 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. type pc_matrix IS ARRAY (0 TO 27) OF natural RANGE 0 TO 63;
  100. constant pc1c_table : pc_matrix := (56, 48, 40, 32, 24, 16, 8,
  101. 0, 57, 49, 41, 33, 25, 17,
  102. 9, 1, 58, 50, 42, 34, 26,
  103. 18, 10, 2, 59, 51, 43, 35);
  104. constant pc1d_table : pc_matrix := (62, 54, 46, 38, 30, 22, 14,
  105. 6, 61, 53, 45, 37, 29, 21,
  106. 13, 5, 60, 52, 44, 36, 28,
  107. 20, 12, 4, 27, 19, 11, 3);
  108. type p_matrix IS ARRAY (0 TO 31) OF natural RANGE 0 TO 31;
  109. constant p_table : p_matrix := (15, 6, 19, 20,
  110. 28, 11, 27, 16,
  111. 0, 14, 22, 25,
  112. 4, 17, 30, 9,
  113. 1, 7, 23, 13,
  114. 31, 26, 2, 8,
  115. 18, 12, 29, 5,
  116. 21, 10, 3, 24);
  117. type pc2_matrix IS ARRAY (0 TO 47) OF natural RANGE 0 TO 63;
  118. constant pc2_table : pc2_matrix := (13, 16, 10, 23, 0, 4,
  119. 2, 27, 14, 5, 20, 9,
  120. 22, 18, 11, 3, 25, 7,
  121. 15, 6, 26, 19, 12, 1,
  122. 40, 51, 30, 36, 46, 54,
  123. 29, 39, 50, 44, 32, 47,
  124. 43, 48, 38, 55, 33, 52,
  125. 45, 41, 49, 35, 28, 31);
  126. END PACKAGE des_pkg;
  127. PACKAGE BODY des_pkg IS
  128. FUNCTION ip ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  129. VARIABLE result : std_logic_vector(0 TO 63);
  130. BEGIN
  131. FOR index IN 0 TO 63 LOOP
  132. result( index ) := input_vector( ip_table( index ) );
  133. END LOOP;
  134. RETURN result;
  135. END FUNCTION ip;
  136. FUNCTION ipn ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  137. VARIABLE result : std_logic_vector(0 TO 63);
  138. BEGIN
  139. FOR index IN 0 TO 63 LOOP
  140. result( index ) := input_vector( ipn_table( index ) );
  141. END LOOP;
  142. RETURN result;
  143. END FUNCTION ipn;
  144. FUNCTION e (input_vector : std_logic_vector(0 TO 31) ) RETURN std_logic_vector IS
  145. VARIABLE result : std_logic_vector(0 TO 47);
  146. BEGIN
  147. FOR index IN 0 TO 47 LOOP
  148. result( index ) := input_vector( e_table( index ) );
  149. END LOOP;
  150. RETURN result;
  151. END FUNCTION e;
  152. FUNCTION s1 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  153. VARIABLE int : std_logic_vector(0 TO 1);
  154. VARIABLE i : integer RANGE 0 TO 3;
  155. VARIABLE j : integer RANGE 0 TO 15;
  156. VARIABLE result : std_logic_vector(0 TO 3);
  157. BEGIN
  158. int := input_vector( 0 ) & input_vector( 5 );
  159. i := to_integer( unsigned( int ) );
  160. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  161. result := std_logic_vector( to_unsigned( s1_table( i, j ), 4 ) );
  162. RETURN result;
  163. END FUNCTION s1;
  164. FUNCTION s2 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  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( s2_table( i, j ), 4 ) );
  174. RETURN result;
  175. END FUNCTION s2;
  176. FUNCTION s3 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  177. VARIABLE int : std_logic_vector(0 TO 1);
  178. VARIABLE i : integer RANGE 0 TO 3;
  179. VARIABLE j : integer RANGE 0 TO 15;
  180. VARIABLE result : std_logic_vector(0 TO 3);
  181. BEGIN
  182. int := input_vector( 0 ) & input_vector( 5 );
  183. i := to_integer( unsigned( int ) );
  184. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  185. result := std_logic_vector( to_unsigned( s3_table( i, j ), 4 ) );
  186. RETURN result;
  187. END FUNCTION s3;
  188. FUNCTION s4 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  189. VARIABLE int : std_logic_vector(0 TO 1);
  190. VARIABLE i : integer RANGE 0 TO 3;
  191. VARIABLE j : integer RANGE 0 TO 15;
  192. VARIABLE result : std_logic_vector(0 TO 3);
  193. BEGIN
  194. int := input_vector( 0 ) & input_vector( 5 );
  195. i := to_integer( unsigned( int ) );
  196. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  197. result := std_logic_vector( to_unsigned( s4_table( i, j ), 4 ) );
  198. RETURN result;
  199. END FUNCTION s4;
  200. FUNCTION s5 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  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( s5_table( i, j ), 4 ) );
  210. RETURN result;
  211. END FUNCTION s5;
  212. FUNCTION s6 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  213. VARIABLE int : std_logic_vector(0 TO 1);
  214. VARIABLE i : integer RANGE 0 TO 3;
  215. VARIABLE j : integer RANGE 0 TO 15;
  216. VARIABLE result : std_logic_vector(0 TO 3);
  217. BEGIN
  218. int := input_vector( 0 ) & input_vector( 5 );
  219. i := to_integer( unsigned( int ) );
  220. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  221. result := std_logic_vector( to_unsigned( s6_table( i, j ), 4 ) );
  222. RETURN result;
  223. END FUNCTION s6;
  224. FUNCTION s7 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  225. VARIABLE int : std_logic_vector(0 TO 1);
  226. VARIABLE i : integer RANGE 0 TO 3;
  227. VARIABLE j : integer RANGE 0 TO 15;
  228. VARIABLE result : std_logic_vector(0 TO 3);
  229. BEGIN
  230. int := input_vector( 0 ) & input_vector( 5 );
  231. i := to_integer( unsigned( int ) );
  232. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  233. result := std_logic_vector( to_unsigned( s7_table( i, j ), 4 ) );
  234. RETURN result;
  235. END FUNCTION s7;
  236. FUNCTION s8 ( input_vector : std_logic_vector(0 TO 5) ) RETURN std_logic_vector IS
  237. VARIABLE int : std_logic_vector(0 TO 1);
  238. VARIABLE i : integer RANGE 0 TO 3;
  239. VARIABLE j : integer RANGE 0 TO 15;
  240. VARIABLE result : std_logic_vector(0 TO 3);
  241. BEGIN
  242. int := input_vector( 0 ) & input_vector( 5 );
  243. i := to_integer( unsigned( int ) );
  244. j := to_integer( unsigned( input_vector( 1 TO 4) ) );
  245. result := std_logic_vector( to_unsigned( s8_table( i, j ), 4 ) );
  246. RETURN result;
  247. END FUNCTION s8;
  248. FUNCTION p (input_vector : std_logic_vector(0 TO 31) ) RETURN std_logic_vector IS
  249. VARIABLE result : std_logic_vector(0 TO 31);
  250. BEGIN
  251. FOR index IN 0 TO 31 LOOP
  252. result( index ) := input_vector( p_table( index ) );
  253. END LOOP;
  254. RETURN result;
  255. END FUNCTION p;
  256. FUNCTION f (input_r : std_logic_vector(0 TO 31); input_key : std_logic_vector(0 TO 47) ) RETURN std_logic_vector IS
  257. VARIABLE intern : std_logic_vector(0 TO 47);
  258. VARIABLE result : std_logic_vector(0 TO 31);
  259. BEGIN
  260. intern := e( input_r ) xor input_key;
  261. result := p( s1( intern(0 TO 5) ) & s2( intern(6 TO 11) ) & s3( intern(12 TO 17) ) & s4( intern(18 TO 23) ) &
  262. s5( intern(24 TO 29) ) & s6( intern(30 TO 35) ) & s7( intern(36 TO 41) ) & s8( intern(42 TO 47) ) );
  263. RETURN result;
  264. END FUNCTION f;
  265. FUNCTION pc1_c ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  266. VARIABLE result : std_logic_vector(0 TO 27);
  267. BEGIN
  268. FOR index IN 0 TO 27 LOOP
  269. result( index ) := input_vector( pc1c_table( index ) );
  270. END LOOP;
  271. RETURN result;
  272. END FUNCTION pc1_c;
  273. FUNCTION pc1_d ( input_vector : std_logic_vector(0 TO 63) ) RETURN std_logic_vector IS
  274. VARIABLE result : std_logic_vector(0 TO 27);
  275. BEGIN
  276. FOR index IN 0 TO 27 LOOP
  277. result( index ) := input_vector( pc1d_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. VARIABLE result : std_logic_vector(0 TO 47);
  283. BEGIN
  284. FOR index IN 0 TO 47 LOOP
  285. result( index ) := input_vector( pc2_table( index ) );
  286. END LOOP;
  287. RETURN result;
  288. END FUNCTION pc2;
  289. END PACKAGE BODY des_pkg;