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.

444 lines
10 KiB

  1. // ======================================================================
  2. // DES encryption/decryption
  3. // algorithm according:FIPS 46-3 specification
  4. // Copyright (C) 2012 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. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write:the Free Software
  18. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. // ======================================================================
  20. module des
  21. (
  22. input reset_i, // async reset
  23. input clk_i, // clock
  24. input mode_i, // des-mode: 0 = encrypt, 1 = decrypt
  25. input [0:63] key_i, // key input
  26. input [0:63] data_i, // data input
  27. input valid_i, // input key/data valid flag
  28. output reg [0:63] data_o, // data output
  29. output valid_o // output data valid flag
  30. );
  31. `include "../../rtl/verilog/des_pkg.v"
  32. // valid, mode register
  33. reg [0:17] valid;
  34. reg [0:16] mode;
  35. // algorithm pipeline register
  36. // key calculation register
  37. reg [0:27] c0;
  38. reg [0:27] c1;
  39. reg [0:27] c2;
  40. reg [0:27] c3;
  41. reg [0:27] c4;
  42. reg [0:27] c5;
  43. reg [0:27] c6;
  44. reg [0:27] c7;
  45. reg [0:27] c8;
  46. reg [0:27] c9;
  47. reg [0:27] c10;
  48. reg [0:27] c11;
  49. reg [0:27] c12;
  50. reg [0:27] c13;
  51. reg [0:27] c14;
  52. reg [0:27] c15;
  53. reg [0:27] c16;
  54. reg [0:27] d0;
  55. reg [0:27] d1;
  56. reg [0:27] d2;
  57. reg [0:27] d3;
  58. reg [0:27] d4;
  59. reg [0:27] d5;
  60. reg [0:27] d6;
  61. reg [0:27] d7;
  62. reg [0:27] d8;
  63. reg [0:27] d9;
  64. reg [0:27] d10;
  65. reg [0:27] d11;
  66. reg [0:27] d12;
  67. reg [0:27] d13;
  68. reg [0:27] d14;
  69. reg [0:27] d15;
  70. reg [0:27] d16;
  71. // key register
  72. reg [0:47] key1;
  73. reg [0:47] key2;
  74. reg [0:47] key3;
  75. reg [0:47] key4;
  76. reg [0:47] key5;
  77. reg [0:47] key6;
  78. reg [0:47] key7;
  79. reg [0:47] key8;
  80. reg [0:47] key9;
  81. reg [0:47] key10;
  82. reg [0:47] key11;
  83. reg [0:47] key12;
  84. reg [0:47] key13;
  85. reg [0:47] key14;
  86. reg [0:47] key15;
  87. reg [0:47] key16;
  88. // register for left, right data blocks
  89. reg [0:31] l0;
  90. reg [0:31] l1;
  91. reg [0:31] l2;
  92. reg [0:31] l3;
  93. reg [0:31] l4;
  94. reg [0:31] l5;
  95. reg [0:31] l6;
  96. reg [0:31] l7;
  97. reg [0:31] l8;
  98. reg [0:31] l9;
  99. reg [0:31] l10;
  100. reg [0:31] l11;
  101. reg [0:31] l12;
  102. reg [0:31] l13;
  103. reg [0:31] l14;
  104. reg [0:31] l15;
  105. reg [0:31] l16;
  106. reg [0:31] r0;
  107. reg [0:31] r1;
  108. reg [0:31] r2;
  109. reg [0:31] r3;
  110. reg [0:31] r4;
  111. reg [0:31] r5;
  112. reg [0:31] r6;
  113. reg [0:31] r7;
  114. reg [0:31] r8;
  115. reg [0:31] r9;
  116. reg [0:31] r10;
  117. reg [0:31] r11;
  118. reg [0:31] r12;
  119. reg [0:31] r13;
  120. reg [0:31] r14;
  121. reg [0:31] r15;
  122. reg [0:31] r16;
  123. wire valid_o = valid[17];
  124. // valid, mode register
  125. always @(posedge clk_i, negedge reset_i) begin
  126. if(~reset_i) begin
  127. valid <= 0;
  128. end
  129. else begin
  130. // shift registers
  131. valid[1:17] <= valid[0:16];
  132. valid[0] <= valid_i;
  133. mode[1:16] <= mode[0:15];
  134. mode[0] <= mode_i;
  135. end
  136. end
  137. // des algorithm pipeline
  138. always @(posedge clk_i, negedge reset_i) begin
  139. if(~reset_i) begin
  140. c0 <= 0;
  141. c1 <= 0;
  142. c2 <= 0;
  143. c3 <= 0;
  144. c4 <= 0;
  145. c5 <= 0;
  146. c6 <= 0;
  147. c7 <= 0;
  148. c8 <= 0;
  149. c9 <= 0;
  150. c10 <= 0;
  151. c11 <= 0;
  152. c12 <= 0;
  153. c13 <= 0;
  154. c14 <= 0;
  155. c15 <= 0;
  156. c16 <= 0;
  157. d0 <= 0;
  158. d1 <= 0;
  159. d2 <= 0;
  160. d3 <= 0;
  161. d4 <= 0;
  162. d5 <= 0;
  163. d6 <= 0;
  164. d7 <= 0;
  165. d8 <= 0;
  166. d9 <= 0;
  167. d10 <= 0;
  168. d11 <= 0;
  169. d12 <= 0;
  170. d13 <= 0;
  171. d14 <= 0;
  172. d15 <= 0;
  173. d16 <= 0;
  174. key1 <= 0;
  175. key2 <= 0;
  176. key3 <= 0;
  177. key4 <= 0;
  178. key5 <= 0;
  179. key6 <= 0;
  180. key7 <= 0;
  181. key8 <= 0;
  182. key9 <= 0;
  183. key10 <= 0;
  184. key11 <= 0;
  185. key12 <= 0;
  186. key13 <= 0;
  187. key14 <= 0;
  188. key15 <= 0;
  189. key16 <= 0;
  190. l0 <= 0;
  191. l1 <= 0;
  192. l2 <= 0;
  193. l3 <= 0;
  194. l4 <= 0;
  195. l5 <= 0;
  196. l6 <= 0;
  197. l7 <= 0;
  198. l8 <= 0;
  199. l9 <= 0;
  200. l10 <= 0;
  201. l11 <= 0;
  202. l12 <= 0;
  203. l13 <= 0;
  204. l14 <= 0;
  205. l15 <= 0;
  206. l16 <= 0;
  207. r0 <= 0;
  208. r1 <= 0;
  209. r2 <= 0;
  210. r3 <= 0;
  211. r4 <= 0;
  212. r5 <= 0;
  213. r6 <= 0;
  214. r7 <= 0;
  215. r8 <= 0;
  216. r9 <= 0;
  217. r10 <= 0;
  218. r11 <= 0;
  219. r12 <= 0;
  220. r13 <= 0;
  221. r14 <= 0;
  222. r15 <= 0;
  223. r16 <= 0;
  224. data_o <= 0;
  225. end
  226. else begin
  227. // output stage
  228. data_o = ipn({r16, l16});
  229. // 16. stage
  230. if (mode[16] == 1'b0) begin
  231. c16 = {c15[1:27], c15[0]};
  232. d16 = {d15[1:27], d15[0]};
  233. end
  234. else begin
  235. c16 = {c15[27], c15[0:26]};
  236. d16 = {d15[27], d15[0:26]};
  237. end
  238. key16 = pc2({c16, d16});
  239. l16 = r15;
  240. r16 = l15 ^ (f(r15, key16));
  241. // 15. stage
  242. if (mode[15] == 1'b0) begin
  243. c15 = {c14[2:27], c14[0:1]};
  244. d15 = {d14[2:27], d14[0:1]};
  245. end
  246. else begin
  247. c15 = {c14[26:27], c14[0:25]};
  248. d15 = {d14[26:27], d14[0:25]};
  249. end
  250. key15 = pc2({c15, d15});
  251. l15 = r14;
  252. r15 = l14 ^ (f(r14, key15));
  253. // 14. stage
  254. if (mode[14] == 1'b0) begin
  255. c14 = {c13[2:27], c13[0:1]};
  256. d14 = {d13[2:27], d13[0:1]};
  257. end
  258. else begin
  259. c14 = {c13[26:27], c13[0:25]};
  260. d14 = {d13[26:27], d13[0:25]};
  261. end
  262. key14 = pc2({c14, d14});
  263. l14 = r13;
  264. r14 = l13 ^ (f(r13, key14));
  265. // 13. stage
  266. if (mode[13] == 1'b0) begin
  267. c13 = {c12[2:27], c12[0:1]};
  268. d13 = {d12[2:27], d12[0:1]};
  269. end
  270. else begin
  271. c13 = {c12[26:27], c12[0:25]};
  272. d13 = {d12[26:27], d12[0:25]};
  273. end
  274. key13 = pc2({c13, d13});
  275. l13 = r12;
  276. r13 = l12 ^ (f(r12, key13));
  277. // 12. stage
  278. if (mode[12] == 1'b0) begin
  279. c12 = {c11[2:27], c11[0:1]};
  280. d12 = {d11[2:27], d11[0:1]};
  281. end
  282. else begin
  283. c12 = {c11[26:27], c11[0:25]};
  284. d12 = {d11[26:27], d11[0:25]};
  285. end
  286. key12 = pc2({c12, d12});
  287. l12 = r11;
  288. r12 = l11 ^ (f(r11, key12));
  289. // 11. stage
  290. if (mode[11] == 1'b0) begin
  291. c11 = {c10[2:27], c10[0:1]};
  292. d11 = {d10[2:27], d10[0:1]};
  293. end
  294. else begin
  295. c11 = {c10[26:27], c10[0:25]};
  296. d11 = {d10[26:27], d10[0:25]};
  297. end
  298. key11 = pc2({c11, d11});
  299. l11 = r10;
  300. r11 = l10 ^ (f(r10, key11));
  301. // 10. stage
  302. if (mode[10] == 1'b0) begin
  303. c10 = {c9[2:27], c9[0:1]};
  304. d10 = {d9[2:27], d9[0:1]};
  305. end
  306. else begin
  307. c10 = {c9[26:27], c9[0:25]};
  308. d10 = {d9[26:27], d9[0:25]};
  309. end
  310. key10 = pc2({c10, d10});
  311. l10 = r9;
  312. r10 = l9 ^ (f(r9, key10));
  313. // 9. stage
  314. if (mode[9] == 1'b0) begin
  315. c9 = {c8[1:27], c8[0]};
  316. d9 = {d8[1:27], d8[0]};
  317. end
  318. else begin
  319. c9 = {c8[27], c8[0:26]};
  320. d9 = {d8[27], d8[0:26]};
  321. end
  322. key9 = pc2({c9, d9});
  323. l9 = r8;
  324. r9 = l8 ^ (f(r8, key9));
  325. // 8. stage
  326. if (mode[8] == 1'b0) begin
  327. c8 = {c7[2:27], c7[0:1]};
  328. d8 = {d7[2:27], d7[0:1]};
  329. end
  330. else begin
  331. c8 = {c7[26:27], c7[0:25]};
  332. d8 = {d7[26:27], d7[0:25]};
  333. end
  334. key8 = pc2({c8, d8});
  335. l8 = r7;
  336. r8 = l7 ^ (f(r7, key8));
  337. // 7. stage
  338. if (mode[7] == 1'b0) begin
  339. c7 = {c6[2:27], c6[0:1]};
  340. d7 = {d6[2:27], d6[0:1]};
  341. end
  342. else begin
  343. c7 = {c6[26:27], c6[0:25]};
  344. d7 = {d6[26:27], d6[0:25]};
  345. end
  346. key7 = pc2({c7, d7});
  347. l7 = r6;
  348. r7 = l6 ^ (f(r6, key7));
  349. // 6. stage
  350. if (mode[6] == 1'b0) begin
  351. c6 = {c5[2:27], c5[0:1]};
  352. d6 = {d5[2:27], d5[0:1]};
  353. end
  354. else begin
  355. c6 = {c5[26:27], c5[0:25]};
  356. d6 = {d5[26:27], d5[0:25]};
  357. end
  358. key6 = pc2({c6, d6});
  359. l6 = r5;
  360. r6 = l5 ^ (f(r5, key6));
  361. // 5. stage
  362. if (mode[5] == 1'b0) begin
  363. c5 = {c4[2:27], c4[0:1]};
  364. d5 = {d4[2:27], d4[0:1]};
  365. end
  366. else begin
  367. c5 = {c4[26:27], c4[0:25]};
  368. d5 = {d4[26:27], d4[0:25]};
  369. end
  370. key5 = pc2({c5, d5});
  371. l5 = r4;
  372. r5 = l4 ^ (f(r4, key5));
  373. // 4. stage
  374. if (mode[4] == 1'b0) begin
  375. c4 = {c3[2:27], c3[0:1]};
  376. d4 = {d3[2:27], d3[0:1]};
  377. end
  378. else begin
  379. c4 = {c3[26:27], c3[0:25]};
  380. d4 = {d3[26:27], d3[0:25]};
  381. end
  382. key4 = pc2({c4, d4});
  383. l4 = r3;
  384. r4 = l3 ^ (f(r3, key4));
  385. // 3. stage
  386. if (mode[3] == 1'b0) begin
  387. c3 = {c2[2:27], c2[0:1]};
  388. d3 = {d2[2:27], d2[0:1]};
  389. end
  390. else begin
  391. c3 = {c2[26:27], c2[0:25]};
  392. d3 = {d2[26:27], d2[0:25]};
  393. end
  394. key3 = pc2({c3, d3});
  395. l3 = r2;
  396. r3 = l2 ^ (f(r2, key3));
  397. // 2. stage
  398. if (mode[2] == 1'b0) begin
  399. c2 = {c1[1:27], c1[0]};
  400. d2 = {d1[1:27], d1[0]};
  401. end
  402. else begin
  403. c2 = {c1[27], c1[0:26]};
  404. d2 = {d1[27], d1[0:26]};
  405. end
  406. key2 = pc2({c2, d2});
  407. l2 = r1;
  408. r2 = l1 ^ (f(r1, key2));
  409. // 1. stage
  410. if (mode[1] == 1'b0) begin
  411. c1 = {c0[1:27], c0[0]};
  412. d1 = {d0[1:27], d0[0]};
  413. end
  414. else begin
  415. c1 = c0;
  416. d1 = d0;
  417. end
  418. key1 = pc2({c1, d1});
  419. l1 = r0;
  420. r1 = l0 ^ (f(r0, key1));
  421. // input stage
  422. l0 = ip0(data_i);
  423. r0 = ip0(data_i);
  424. c0 = pc1_c(key_i);
  425. d0 = pc1_d(key_i);
  426. end
  427. end
  428. endmodule