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.

463 lines
11 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:18] valid;
  34. reg [0:17] 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. wire [0:47] key1;
  73. wire [0:47] key2;
  74. wire [0:47] key3;
  75. wire [0:47] key4;
  76. wire [0:47] key5;
  77. wire [0:47] key6;
  78. wire [0:47] key7;
  79. wire [0:47] key8;
  80. wire [0:47] key9;
  81. wire [0:47] key10;
  82. wire [0:47] key11;
  83. wire [0:47] key12;
  84. wire [0:47] key13;
  85. wire [0:47] key14;
  86. wire [0:47] key15;
  87. wire [0:47] key16;
  88. // register for left, right data blocks
  89. reg [0:31] l;
  90. reg [0:31] l0;
  91. reg [0:31] l1;
  92. reg [0:31] l2;
  93. reg [0:31] l3;
  94. reg [0:31] l4;
  95. reg [0:31] l5;
  96. reg [0:31] l6;
  97. reg [0:31] l7;
  98. reg [0:31] l8;
  99. reg [0:31] l9;
  100. reg [0:31] l10;
  101. reg [0:31] l11;
  102. reg [0:31] l12;
  103. reg [0:31] l13;
  104. reg [0:31] l14;
  105. reg [0:31] l15;
  106. reg [0:31] l16;
  107. reg [0:31] r;
  108. reg [0:31] r0;
  109. reg [0:31] r1;
  110. reg [0:31] r2;
  111. reg [0:31] r3;
  112. reg [0:31] r4;
  113. reg [0:31] r5;
  114. reg [0:31] r6;
  115. reg [0:31] r7;
  116. reg [0:31] r8;
  117. reg [0:31] r9;
  118. reg [0:31] r10;
  119. reg [0:31] r11;
  120. reg [0:31] r12;
  121. reg [0:31] r13;
  122. reg [0:31] r14;
  123. reg [0:31] r15;
  124. reg [0:31] r16;
  125. wire valid_o = valid[18];
  126. // valid, mode register
  127. always @(posedge clk_i, negedge reset_i) begin
  128. if(~reset_i) begin
  129. valid <= 0;
  130. mode <= 0;
  131. end
  132. else begin
  133. // shift registers
  134. valid[1:18] <= valid[0:17];
  135. valid[0] <= valid_i;
  136. mode[1:17] <= mode[0:16];
  137. mode[0] <= mode_i;
  138. end
  139. end
  140. // des algorithm pipeline
  141. always @(posedge clk_i, negedge reset_i) begin
  142. if(~reset_i) begin
  143. l <= 0;
  144. r <= 0;
  145. l0 <= 0;
  146. l1 <= 0;
  147. l2 <= 0;
  148. l3 <= 0;
  149. l4 <= 0;
  150. l5 <= 0;
  151. l6 <= 0;
  152. l7 <= 0;
  153. l8 <= 0;
  154. l9 <= 0;
  155. l10 <= 0;
  156. l11 <= 0;
  157. l12 <= 0;
  158. l13 <= 0;
  159. l14 <= 0;
  160. l15 <= 0;
  161. l16 <= 0;
  162. r0 <= 0;
  163. r1 <= 0;
  164. r2 <= 0;
  165. r3 <= 0;
  166. r4 <= 0;
  167. r5 <= 0;
  168. r6 <= 0;
  169. r7 <= 0;
  170. r8 <= 0;
  171. r9 <= 0;
  172. r10 <= 0;
  173. r11 <= 0;
  174. r12 <= 0;
  175. r13 <= 0;
  176. r14 <= 0;
  177. r15 <= 0;
  178. r16 <= 0;
  179. data_o <= 0;
  180. end
  181. else begin
  182. // output stage
  183. data_o <= ipn({r16, l16});
  184. // 16. stage
  185. l16 <= r15;
  186. r16 <= l15 ^ (f(r15, key16));
  187. // 15. stage
  188. l15 <= r14;
  189. r15 <= l14 ^ (f(r14, key15));
  190. // 14. stage
  191. l14 <= r13;
  192. r14 <= l13 ^ (f(r13, key14));
  193. // 13. stage
  194. l13 <= r12;
  195. r13 <= l12 ^ (f(r12, key13));
  196. // 12. stage
  197. l12 <= r11;
  198. r12 <= l11 ^ (f(r11, key12));
  199. // 11. stage
  200. l11 <= r10;
  201. r11 <= l10 ^ (f(r10, key11));
  202. // 10. stage
  203. l10 <= r9;
  204. r10 <= l9 ^ (f(r9, key10));
  205. // 9. stage
  206. l9 <= r8;
  207. r9 <= l8 ^ (f(r8, key9));
  208. // 8. stage
  209. l8 <= r7;
  210. r8 <= l7 ^ (f(r7, key8));
  211. // 7. stage
  212. l7 <= r6;
  213. r7 <= l6 ^ (f(r6, key7));
  214. // 6. stage
  215. l6 <= r5;
  216. r6 <= l5 ^ (f(r5, key6));
  217. // 5. stage
  218. l5 <= r4;
  219. r5 <= l4 ^ (f(r4, key5));
  220. // 4. stage
  221. l4 <= r3;
  222. r4 <= l3 ^ (f(r3, key4));
  223. // 3. stage
  224. l3 <= r2;
  225. r3 <= l2 ^ (f(r2, key3));
  226. // 2. stage
  227. l2 <= r1;
  228. r2 <= l1 ^ (f(r1, key2));
  229. // 1. stage
  230. l1 <= r0;
  231. r1 <= l0 ^ (f(r0, key1));
  232. // 1. state
  233. l0 <= l;
  234. r0 <= r;
  235. // input stage
  236. l <= ip0(data_i);
  237. r <= ip1(data_i);
  238. end
  239. end
  240. // des key pipeline
  241. always @(posedge clk_i, negedge reset_i) begin
  242. if(~reset_i) begin
  243. c0 <= 0;
  244. c1 <= 0;
  245. c2 <= 0;
  246. c3 <= 0;
  247. c4 <= 0;
  248. c5 <= 0;
  249. c6 <= 0;
  250. c7 <= 0;
  251. c8 <= 0;
  252. c9 <= 0;
  253. c10 <= 0;
  254. c11 <= 0;
  255. c12 <= 0;
  256. c13 <= 0;
  257. c14 <= 0;
  258. c15 <= 0;
  259. c16 <= 0;
  260. d0 <= 0;
  261. d1 <= 0;
  262. d2 <= 0;
  263. d3 <= 0;
  264. d4 <= 0;
  265. d5 <= 0;
  266. d6 <= 0;
  267. d7 <= 0;
  268. d8 <= 0;
  269. d9 <= 0;
  270. d10 <= 0;
  271. d11 <= 0;
  272. d12 <= 0;
  273. d13 <= 0;
  274. d14 <= 0;
  275. d15 <= 0;
  276. d16 <= 0;
  277. end
  278. else begin
  279. // input stage
  280. c0 <= pc1_c(key_i);
  281. d0 <= pc1_d(key_i);
  282. // 1st stage
  283. if (~mode[0]) begin
  284. c1 <= {c0[1:27], c0[0]};
  285. d1 <= {c0[1:27], c0[0]};
  286. end
  287. else begin
  288. c1 <= c0;
  289. d1 <= d0;
  290. end
  291. // 2nd stage
  292. if (~mode[1]) begin
  293. c2 <= {c1[1:27], c1[0]};
  294. d2 <= {d1[1:27], d1[0]};
  295. end
  296. else begin
  297. c2 <= {c1[27], c1[0:26]};
  298. d2 <= {d1[27], d1[0:26]};
  299. end
  300. // 3rd stage
  301. if (~mode[2]) begin
  302. c3 <= {c2[2:27], c2[0:1]};
  303. d3 <= {d2[2:27], d2[0:1]};
  304. end
  305. else begin
  306. c3 <= {c2[26:27], c2[0:25]};
  307. d3 <= {d2[26:27], d2[0:25]};
  308. end
  309. // 4th stage
  310. if (~mode[3]) begin
  311. c4 <= {c3[2:27], c3[0:1]};
  312. d4 <= {d3[2:27], d3[0:1]};
  313. end
  314. else begin
  315. c4 <= {c3[26:27], c3[0:25]};
  316. d4 <= {d3[26:27], d3[0:25]};
  317. end
  318. // 5th stage
  319. if (~mode[4]) begin
  320. c5 <= {c4[2:27], c4[0:1]};
  321. d5 <= {d4[2:27], d4[0:1]};
  322. end
  323. else begin
  324. c5 <= {c4[26:27], c4[0:25]};
  325. d5 <= {d4[26:27], d4[0:25]};
  326. end
  327. // 6. stage
  328. if (~mode[5]) begin
  329. c6 <= {c5[2:27], c5[0:1]};
  330. d6 <= {d5[2:27], d5[0:1]};
  331. end
  332. else begin
  333. c6 <= {c5[26:27], c5[0:25]};
  334. d6 <= {d5[26:27], d5[0:25]};
  335. end
  336. // 7. stage
  337. if (~mode[6]) begin
  338. c7 <= {c6[2:27], c6[0:1]};
  339. d7 <= {d6[2:27], d6[0:1]};
  340. end
  341. else begin
  342. c7 <= {c6[26:27], c6[0:25]};
  343. d7 <= {d6[26:27], d6[0:25]};
  344. end
  345. // 8. stage
  346. if (~mode[7]) begin
  347. c8 <= {c7[2:27], c7[0:1]};
  348. d8 <= {d7[2:27], d7[0:1]};
  349. end
  350. else begin
  351. c8 <= {c7[26:27], c7[0:25]};
  352. d8 <= {d7[26:27], d7[0:25]};
  353. end
  354. // 9. stage
  355. if (~mode[8]) begin
  356. c9 <= {c8[2:27], c8[0:1]};
  357. d9 <= {d8[2:27], d8[0:1]};
  358. end
  359. else begin
  360. c9 <= {c8[26:27], c8[0:25]};
  361. d9 <= {d8[26:27], d8[0:25]};
  362. end
  363. // 10. stage
  364. if (~mode[9]) begin
  365. c10 <= {c9[2:27], c9[0:1]};
  366. d10 <= {d9[2:27], d9[0:1]};
  367. end
  368. else begin
  369. c10 <= {c9[26:27], c9[0:25]};
  370. d10 <= {d9[26:27], d9[0:25]};
  371. end
  372. // 6. stage
  373. if (~mode[10]) begin
  374. c11 <= {c10[2:27], c10[0:1]};
  375. d11 <= {d10[2:27], d10[0:1]};
  376. end
  377. else begin
  378. c11 <= {c10[26:27], c10[0:25]};
  379. d11 <= {d10[26:27], d10[0:25]};
  380. end
  381. // 6. stage
  382. if (~mode[11]) begin
  383. c12 <= {c11[2:27], c11[0:1]};
  384. d12 <= {d11[2:27], d11[0:1]};
  385. end
  386. else begin
  387. c12 <= {c11[26:27], c11[0:25]};
  388. d12 <= {d11[26:27], d11[0:25]};
  389. end
  390. // 6. stage
  391. if (~mode[12]) begin
  392. c13 <= {c12[2:27], c12[0:1]};
  393. d13 <= {d12[2:27], d12[0:1]};
  394. end
  395. else begin
  396. c13 <= {c12[26:27], c12[0:25]};
  397. d13 <= {d12[26:27], d12[0:25]};
  398. end
  399. // 6. stage
  400. if (~mode[13]) begin
  401. c14 <= {c13[2:27], c13[0:1]};
  402. d14 <= {d13[2:27], d13[0:1]};
  403. end
  404. else begin
  405. c14 <= {c13[26:27], c13[0:25]};
  406. d14 <= {d13[26:27], d13[0:25]};
  407. end
  408. // 6. stage
  409. if (~mode[14]) begin
  410. c15 <= {c14[2:27], c14[0:1]};
  411. d15 <= {d14[2:27], d14[0:1]};
  412. end
  413. else begin
  414. c15 <= {c14[26:27], c14[0:25]};
  415. d15 <= {d14[26:27], d14[0:25]};
  416. end
  417. // 6. stage
  418. if (~mode[15]) begin
  419. c16 <= {c15[1:27], c15[0]};
  420. d16 <= {d15[1:27], d15[0]};
  421. end
  422. else begin
  423. c16 <= {c15[27], c15[0:26]};
  424. d16 <= {d15[27], d15[0:26]};
  425. end
  426. end
  427. end
  428. // key assignments
  429. assign key1 = pc2({c1, d1});
  430. assign key2 = pc2({c2, d2});
  431. assign key3 = pc2({c3, d3});
  432. assign key4 = pc2({c4, d4});
  433. assign key5 = pc2({c5, d5});
  434. assign key6 = pc2({c6, d6});
  435. assign key7 = pc2({c7, d7});
  436. assign key8 = pc2({c8, d8});
  437. assign key9 = pc2({c9, d9});
  438. assign key10 = pc2({c10, d10});
  439. assign key11 = pc2({c11, d11});
  440. assign key12 = pc2({c12, d12});
  441. assign key13 = pc2({c13, d13});
  442. assign key14 = pc2({c14, d14});
  443. assign key15 = pc2({c15, d15});
  444. assign key16 = pc2({c16, d16});
  445. endmodule