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.

340 lines
13 KiB

  1. -- ======================================================================
  2. -- DES encryption/decryption
  3. -- algorithm according to FIPS 46-3 specification
  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. -- Revision 1.1 2007/02/05
  21. -- Corrected error in use of mode register for key calculation
  22. LIBRARY ieee;
  23. USE ieee.std_logic_1164.all;
  24. USE ieee.numeric_std.ALL;
  25. USE work.des_pkg.ALL;
  26. ENTITY des IS
  27. PORT (
  28. reset_i : in std_logic; -- async reset
  29. clk_i : IN std_logic; -- clock
  30. mode_i : IN std_logic; -- des-modus: 0 = encrypt, 1 = decrypt
  31. key_i : IN std_logic_vector(0 TO 63); -- key input
  32. data_i : IN std_logic_vector(0 TO 63); -- data input
  33. valid_i : IN std_logic; -- input key/data valid flag
  34. data_o : OUT std_logic_vector(0 TO 63); -- data output
  35. valid_o : OUT std_logic -- output data valid flag
  36. );
  37. END ENTITY des;
  38. ARCHITECTURE rtl OF des IS
  39. BEGIN
  40. crypt : PROCESS ( clk_i ) IS
  41. -- variables for key calculation
  42. VARIABLE c0 : std_logic_vector(0 TO 27) := (others => '0');
  43. VARIABLE c1 : std_logic_vector(0 TO 27) := (others => '0');
  44. VARIABLE c2 : std_logic_vector(0 TO 27) := (others => '0');
  45. VARIABLE c3 : std_logic_vector(0 TO 27) := (others => '0');
  46. VARIABLE c4 : std_logic_vector(0 TO 27) := (others => '0');
  47. VARIABLE c5 : std_logic_vector(0 TO 27) := (others => '0');
  48. VARIABLE c6 : std_logic_vector(0 TO 27) := (others => '0');
  49. VARIABLE c7 : std_logic_vector(0 TO 27) := (others => '0');
  50. VARIABLE c8 : std_logic_vector(0 TO 27) := (others => '0');
  51. VARIABLE c9 : std_logic_vector(0 TO 27) := (others => '0');
  52. VARIABLE c10 : std_logic_vector(0 TO 27) := (others => '0');
  53. VARIABLE c11 : std_logic_vector(0 TO 27) := (others => '0');
  54. VARIABLE c12 : std_logic_vector(0 TO 27) := (others => '0');
  55. VARIABLE c13 : std_logic_vector(0 TO 27) := (others => '0');
  56. VARIABLE c14 : std_logic_vector(0 TO 27) := (others => '0');
  57. VARIABLE c15 : std_logic_vector(0 TO 27) := (others => '0');
  58. VARIABLE c16 : std_logic_vector(0 TO 27) := (others => '0');
  59. VARIABLE d0 : std_logic_vector(0 TO 27) := (others => '0');
  60. VARIABLE d1 : std_logic_vector(0 TO 27) := (others => '0');
  61. VARIABLE d2 : std_logic_vector(0 TO 27) := (others => '0');
  62. VARIABLE d3 : std_logic_vector(0 TO 27) := (others => '0');
  63. VARIABLE d4 : std_logic_vector(0 TO 27) := (others => '0');
  64. VARIABLE d5 : std_logic_vector(0 TO 27) := (others => '0');
  65. VARIABLE d6 : std_logic_vector(0 TO 27) := (others => '0');
  66. VARIABLE d7 : std_logic_vector(0 TO 27) := (others => '0');
  67. VARIABLE d8 : std_logic_vector(0 TO 27) := (others => '0');
  68. VARIABLE d9 : std_logic_vector(0 TO 27) := (others => '0');
  69. VARIABLE d10 : std_logic_vector(0 TO 27) := (others => '0');
  70. VARIABLE d11 : std_logic_vector(0 TO 27) := (others => '0');
  71. VARIABLE d12 : std_logic_vector(0 TO 27) := (others => '0');
  72. VARIABLE d13 : std_logic_vector(0 TO 27) := (others => '0');
  73. VARIABLE d14 : std_logic_vector(0 TO 27) := (others => '0');
  74. VARIABLE d15 : std_logic_vector(0 TO 27) := (others => '0');
  75. VARIABLE d16 : std_logic_vector(0 TO 27) := (others => '0');
  76. -- key variables
  77. VARIABLE key1 : std_logic_vector(0 TO 47) := (others => '0');
  78. VARIABLE key2 : std_logic_vector(0 TO 47) := (others => '0');
  79. VARIABLE key3 : std_logic_vector(0 TO 47) := (others => '0');
  80. VARIABLE key4 : std_logic_vector(0 TO 47) := (others => '0');
  81. VARIABLE key5 : std_logic_vector(0 TO 47) := (others => '0');
  82. VARIABLE key6 : std_logic_vector(0 TO 47) := (others => '0');
  83. VARIABLE key7 : std_logic_vector(0 TO 47) := (others => '0');
  84. VARIABLE key8 : std_logic_vector(0 TO 47) := (others => '0');
  85. VARIABLE key9 : std_logic_vector(0 TO 47) := (others => '0');
  86. VARIABLE key10 : std_logic_vector(0 TO 47) := (others => '0');
  87. VARIABLE key11 : std_logic_vector(0 TO 47) := (others => '0');
  88. VARIABLE key12 : std_logic_vector(0 TO 47) := (others => '0');
  89. VARIABLE key13 : std_logic_vector(0 TO 47) := (others => '0');
  90. VARIABLE key14 : std_logic_vector(0 TO 47) := (others => '0');
  91. VARIABLE key15 : std_logic_vector(0 TO 47) := (others => '0');
  92. VARIABLE key16 : std_logic_vector(0 TO 47) := (others => '0');
  93. -- variables for left & right data blocks
  94. VARIABLE l0 : std_logic_vector( 0 TO 31) := (others => '0');
  95. VARIABLE l1 : std_logic_vector( 0 TO 31) := (others => '0');
  96. VARIABLE l2 : std_logic_vector( 0 TO 31) := (others => '0');
  97. VARIABLE l3 : std_logic_vector( 0 TO 31) := (others => '0');
  98. VARIABLE l4 : std_logic_vector( 0 TO 31) := (others => '0');
  99. VARIABLE l5 : std_logic_vector( 0 TO 31) := (others => '0');
  100. VARIABLE l6 : std_logic_vector( 0 TO 31) := (others => '0');
  101. VARIABLE l7 : std_logic_vector( 0 TO 31) := (others => '0');
  102. VARIABLE l8 : std_logic_vector( 0 TO 31) := (others => '0');
  103. VARIABLE l9 : std_logic_vector( 0 TO 31) := (others => '0');
  104. VARIABLE l10 : std_logic_vector( 0 TO 31) := (others => '0');
  105. VARIABLE l11 : std_logic_vector( 0 TO 31) := (others => '0');
  106. VARIABLE l12 : std_logic_vector( 0 TO 31) := (others => '0');
  107. VARIABLE l13 : std_logic_vector( 0 TO 31) := (others => '0');
  108. VARIABLE l14 : std_logic_vector( 0 TO 31) := (others => '0');
  109. VARIABLE l15 : std_logic_vector( 0 TO 31) := (others => '0');
  110. VARIABLE l16 : std_logic_vector( 0 TO 31) := (others => '0');
  111. VARIABLE r0 : std_logic_vector( 0 TO 31) := (others => '0');
  112. VARIABLE r1 : std_logic_vector( 0 TO 31) := (others => '0');
  113. VARIABLE r2 : std_logic_vector( 0 TO 31) := (others => '0');
  114. VARIABLE r3 : std_logic_vector( 0 TO 31) := (others => '0');
  115. VARIABLE r4 : std_logic_vector( 0 TO 31) := (others => '0');
  116. VARIABLE r5 : std_logic_vector( 0 TO 31) := (others => '0');
  117. VARIABLE r6 : std_logic_vector( 0 TO 31) := (others => '0');
  118. VARIABLE r7 : std_logic_vector( 0 TO 31) := (others => '0');
  119. VARIABLE r8 : std_logic_vector( 0 TO 31) := (others => '0');
  120. VARIABLE r9 : std_logic_vector( 0 TO 31) := (others => '0');
  121. VARIABLE r10 : std_logic_vector( 0 TO 31) := (others => '0');
  122. VARIABLE r11 : std_logic_vector( 0 TO 31) := (others => '0');
  123. VARIABLE r12 : std_logic_vector( 0 TO 31) := (others => '0');
  124. VARIABLE r13 : std_logic_vector( 0 TO 31) := (others => '0');
  125. VARIABLE r14 : std_logic_vector( 0 TO 31) := (others => '0');
  126. VARIABLE r15 : std_logic_vector( 0 TO 31) := (others => '0');
  127. VARIABLE r16 : std_logic_vector( 0 TO 31) := (others => '0');
  128. -- variables for mode & valid shift registers
  129. VARIABLE mode : std_logic_vector(0 TO 16) := (others => '0');
  130. VARIABLE valid : std_logic_vector(0 TO 17) := (others => '0');
  131. BEGIN
  132. if(reset_i = '0') then
  133. data_o <= (others => '0');
  134. valid_o <= '0';
  135. elsif rising_edge( clk_i ) THEN
  136. -- shift registers
  137. valid(1 TO 17) := valid(0 TO 16);
  138. valid(0) := valid_i;
  139. mode(1 TO 16) := mode(0 TO 15);
  140. mode(0) := mode_i;
  141. -- output stage
  142. valid_o <= valid(17);
  143. data_o <= ipn( ( r16 & l16 ) );
  144. -- 16. stage
  145. IF mode(16) = '0' THEN
  146. c16 := c15(1 TO 27) & c15(0);
  147. d16 := d15(1 TO 27) & d15(0);
  148. ELSE
  149. c16 := c15(27) & c15(0 TO 26);
  150. d16 := d15(27) & d15(0 TO 26);
  151. END IF;
  152. key16 := pc2( ( c16 & d16 ) );
  153. l16 := r15;
  154. r16 := l15 xor ( f( r15, key16 ) );
  155. -- 15. stage
  156. IF mode(15) = '0' THEN
  157. c15 := c14(2 TO 27) & c14(0 TO 1);
  158. d15 := d14(2 TO 27) & d14(0 TO 1);
  159. ELSE
  160. c15 := c14(26 TO 27) & c14(0 TO 25);
  161. d15 := d14(26 TO 27) & d14(0 TO 25);
  162. END IF;
  163. key15 := pc2( ( c15 & d15 ) );
  164. l15 := r14;
  165. r15 := l14 xor ( f( r14, key15 ) );
  166. -- 14. stage
  167. IF mode(14) = '0' THEN
  168. c14 := c13(2 TO 27) & c13(0 TO 1);
  169. d14 := d13(2 TO 27) & d13(0 TO 1);
  170. ELSE
  171. c14 := c13(26 TO 27) & c13(0 TO 25);
  172. d14 := d13(26 TO 27) & d13(0 TO 25);
  173. END IF;
  174. key14 := pc2( ( c14 & d14 ) );
  175. l14 := r13;
  176. r14 := l13 xor ( f( r13, key14 ) );
  177. -- 13. stage
  178. IF mode(13) = '0' THEN
  179. c13 := c12(2 TO 27) & c12(0 TO 1);
  180. d13 := d12(2 TO 27) & d12(0 TO 1);
  181. ELSE
  182. c13 := c12(26 TO 27) & c12(0 TO 25);
  183. d13 := d12(26 TO 27) & d12(0 TO 25);
  184. END IF;
  185. key13 := pc2( ( c13 & d13 ) );
  186. l13 := r12;
  187. r13 := l12 xor ( f( r12, key13 ) );
  188. -- 12. stage
  189. IF mode(12) = '0' THEN
  190. c12 := c11(2 TO 27) & c11(0 TO 1);
  191. d12 := d11(2 TO 27) & d11(0 TO 1);
  192. ELSE
  193. c12 := c11(26 TO 27) & c11(0 TO 25);
  194. d12 := d11(26 TO 27) & d11(0 TO 25);
  195. END IF;
  196. key12 := pc2( ( c12 & d12 ) );
  197. l12 := r11;
  198. r12 := l11 xor ( f( r11, key12 ) );
  199. -- 11. stage
  200. IF mode(11) = '0' THEN
  201. c11 := c10(2 TO 27) & c10(0 TO 1);
  202. d11 := d10(2 TO 27) & d10(0 TO 1);
  203. ELSE
  204. c11 := c10(26 TO 27) & c10(0 TO 25);
  205. d11 := d10(26 TO 27) & d10(0 TO 25);
  206. END IF;
  207. key11 := pc2( ( c11 & d11 ) );
  208. l11 := r10;
  209. r11 := l10 xor ( f( r10, key11 ) );
  210. -- 10. stage
  211. IF mode(10) = '0' THEN
  212. c10 := c9(2 TO 27) & c9(0 TO 1);
  213. d10 := d9(2 TO 27) & d9(0 TO 1);
  214. ELSE
  215. c10 := c9(26 TO 27) & c9(0 TO 25);
  216. d10 := d9(26 TO 27) & d9(0 TO 25);
  217. END IF;
  218. key10 := pc2( ( c10 & d10 ) );
  219. l10 := r9;
  220. r10 := l9 xor ( f( r9, key10 ) );
  221. -- 9. stage
  222. IF mode(9) = '0' THEN
  223. c9 := c8(1 TO 27) & c8(0);
  224. d9 := d8(1 TO 27) & d8(0);
  225. ELSE
  226. c9 := c8(27) & c8(0 TO 26);
  227. d9 := d8(27) & d8(0 TO 26);
  228. END IF;
  229. key9 := pc2( ( c9 & d9 ) );
  230. l9 := r8;
  231. r9 := l8 xor ( f( r8, key9 ) );
  232. -- 8. stage
  233. IF mode(8) = '0' THEN
  234. c8 := c7(2 TO 27) & c7(0 TO 1);
  235. d8 := d7(2 TO 27) & d7(0 TO 1);
  236. ELSE
  237. c8 := c7(26 TO 27) & c7(0 TO 25);
  238. d8 := d7(26 TO 27) & d7(0 TO 25);
  239. END IF;
  240. key8 := pc2( ( c8 & d8 ) );
  241. l8 := r7;
  242. r8 := l7 xor ( f( r7, key8 ) );
  243. -- 7. stage
  244. IF mode(7) = '0' THEN
  245. c7 := c6(2 TO 27) & c6(0 TO 1);
  246. d7 := d6(2 TO 27) & d6(0 TO 1);
  247. ELSE
  248. c7 := c6(26 TO 27) & c6(0 TO 25);
  249. d7 := d6(26 TO 27) & d6(0 TO 25);
  250. END IF;
  251. key7 := pc2( ( c7 & d7 ) );
  252. l7 := r6;
  253. r7 := l6 xor ( f( r6, key7 ) );
  254. -- 6. stage
  255. IF mode(6) = '0' THEN
  256. c6 := c5(2 TO 27) & c5(0 TO 1);
  257. d6 := d5(2 TO 27) & d5(0 TO 1);
  258. ELSE
  259. c6 := c5(26 TO 27) & c5(0 TO 25);
  260. d6 := d5(26 TO 27) & d5(0 TO 25);
  261. END IF;
  262. key6 := pc2( ( c6 & d6 ) );
  263. l6 := r5;
  264. r6 := l5 xor ( f( r5, key6 ) );
  265. -- 5. stage
  266. IF mode(5) = '0' THEN
  267. c5 := c4(2 TO 27) & c4(0 TO 1);
  268. d5 := d4(2 TO 27) & d4(0 TO 1);
  269. ELSE
  270. c5 := c4(26 TO 27) & c4(0 TO 25);
  271. d5 := d4(26 TO 27) & d4(0 TO 25);
  272. END IF;
  273. key5 := pc2( ( c5 & d5 ) );
  274. l5 := r4;
  275. r5 := l4 xor ( f( r4, key5 ) );
  276. -- 4. stage
  277. IF mode(4) = '0' THEN
  278. c4 := c3(2 TO 27) & c3(0 TO 1);
  279. d4 := d3(2 TO 27) & d3(0 TO 1);
  280. ELSE
  281. c4 := c3(26 TO 27) & c3(0 TO 25);
  282. d4 := d3(26 TO 27) & d3(0 TO 25);
  283. END IF;
  284. key4 := pc2( ( c4 & d4 ) );
  285. l4 := r3;
  286. r4 := l3 xor ( f( r3, key4 ) );
  287. -- 3. stage
  288. IF mode(3) = '0' THEN
  289. c3 := c2(2 TO 27) & c2(0 TO 1);
  290. d3 := d2(2 TO 27) & d2(0 TO 1);
  291. ELSE
  292. c3 := c2(26 TO 27) & c2(0 TO 25);
  293. d3 := d2(26 TO 27) & d2(0 TO 25);
  294. END IF;
  295. key3 := pc2( ( c3 & d3 ) );
  296. l3 := r2;
  297. r3 := l2 xor ( f( r2, key3 ) );
  298. -- 2. stage
  299. IF mode(2) = '0' THEN
  300. c2 := c1(1 TO 27) & c1(0);
  301. d2 := d1(1 TO 27) & d1(0);
  302. ELSE
  303. c2 := c1(27) & c1(0 TO 26);
  304. d2 := d1(27) & d1(0 TO 26);
  305. END IF;
  306. key2 := pc2( ( c2 & d2 ) );
  307. l2 := r1;
  308. r2 := l1 xor ( f( r1, key2 ) );
  309. -- 1. stage
  310. IF mode(1) = '0' THEN
  311. c1 := c0(1 TO 27) & c0(0);
  312. d1 := d0(1 TO 27) & d0(0);
  313. ELSE
  314. c1 := c0;
  315. d1 := d0;
  316. END IF;
  317. key1 := pc2( ( c1 & d1 ) );
  318. l1 := r0;
  319. r1 := l0 xor ( f( r0, key1 ) );
  320. -- input stage
  321. l0 := ip( data_i )(0 TO 31);
  322. r0 := ip( data_i )(32 TO 63);
  323. c0 := pc1_c( key_i );
  324. d0 := pc1_d( key_i );
  325. END IF;
  326. END PROCESS crypt;
  327. END ARCHITECTURE rtl;