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.

756 lines
19 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. `timescale 1ns/1ps
  21. module des
  22. (
  23. input reset_i, // async reset
  24. input clk_i, // clock
  25. input mode_i, // des-mode: 0 = encrypt, 1 = decrypt
  26. input [0:63] key_i, // key input
  27. input [0:63] data_i, // data input
  28. input valid_i, // input key/data valid flag
  29. output reg accept_o,
  30. output reg [0:63] data_o, // data output
  31. output valid_o, // output data valid flag
  32. input accept_i
  33. );
  34. `include "../../rtl/verilog/des_pkg.v"
  35. `ifdef PIPE
  36. // valid, mode register
  37. reg [0:18] valid;
  38. reg [0:17] mode;
  39. // algorithm pipeline register
  40. // key calculation register
  41. reg [0:27] c0;
  42. reg [0:27] c1;
  43. reg [0:27] c2;
  44. reg [0:27] c3;
  45. reg [0:27] c4;
  46. reg [0:27] c5;
  47. reg [0:27] c6;
  48. reg [0:27] c7;
  49. reg [0:27] c8;
  50. reg [0:27] c9;
  51. reg [0:27] c10;
  52. reg [0:27] c11;
  53. reg [0:27] c12;
  54. reg [0:27] c13;
  55. reg [0:27] c14;
  56. reg [0:27] c15;
  57. reg [0:27] c16;
  58. reg [0:27] d0;
  59. reg [0:27] d1;
  60. reg [0:27] d2;
  61. reg [0:27] d3;
  62. reg [0:27] d4;
  63. reg [0:27] d5;
  64. reg [0:27] d6;
  65. reg [0:27] d7;
  66. reg [0:27] d8;
  67. reg [0:27] d9;
  68. reg [0:27] d10;
  69. reg [0:27] d11;
  70. reg [0:27] d12;
  71. reg [0:27] d13;
  72. reg [0:27] d14;
  73. reg [0:27] d15;
  74. reg [0:27] d16;
  75. // key register
  76. wire [0:47] key1;
  77. wire [0:47] key2;
  78. wire [0:47] key3;
  79. wire [0:47] key4;
  80. wire [0:47] key5;
  81. wire [0:47] key6;
  82. wire [0:47] key7;
  83. wire [0:47] key8;
  84. wire [0:47] key9;
  85. wire [0:47] key10;
  86. wire [0:47] key11;
  87. wire [0:47] key12;
  88. wire [0:47] key13;
  89. wire [0:47] key14;
  90. wire [0:47] key15;
  91. wire [0:47] key16;
  92. // register for left, right data blocks
  93. reg [0:31] l;
  94. reg [0:31] l0;
  95. reg [0:31] l1;
  96. reg [0:31] l2;
  97. reg [0:31] l3;
  98. reg [0:31] l4;
  99. reg [0:31] l5;
  100. reg [0:31] l6;
  101. reg [0:31] l7;
  102. reg [0:31] l8;
  103. reg [0:31] l9;
  104. reg [0:31] l10;
  105. reg [0:31] l11;
  106. reg [0:31] l12;
  107. reg [0:31] l13;
  108. reg [0:31] l14;
  109. reg [0:31] l15;
  110. reg [0:31] l16;
  111. reg [0:31] r;
  112. reg [0:31] r0;
  113. reg [0:31] r1;
  114. reg [0:31] r2;
  115. reg [0:31] r3;
  116. reg [0:31] r4;
  117. reg [0:31] r5;
  118. reg [0:31] r6;
  119. reg [0:31] r7;
  120. reg [0:31] r8;
  121. reg [0:31] r9;
  122. reg [0:31] r10;
  123. reg [0:31] r11;
  124. reg [0:31] r12;
  125. reg [0:31] r13;
  126. reg [0:31] r14;
  127. reg [0:31] r15;
  128. reg [0:31] r16;
  129. wire valid_o = valid[18];
  130. // valid, mode register
  131. always @(posedge clk_i, negedge reset_i) begin
  132. if(~reset_i) begin
  133. valid <= 0;
  134. mode <= 0;
  135. accept_o <= 0;
  136. end
  137. else begin
  138. // shift registers
  139. valid[1:18] <= valid[0:17];
  140. valid[0] <= valid_i;
  141. mode[1:17] <= mode[0:16];
  142. mode[0] <= mode_i;
  143. accept_o <= 1;
  144. end
  145. end
  146. // des algorithm pipeline
  147. always @(posedge clk_i, negedge reset_i) begin
  148. if(~reset_i) begin
  149. l <= 0;
  150. r <= 0;
  151. l0 <= 0;
  152. l1 <= 0;
  153. l2 <= 0;
  154. l3 <= 0;
  155. l4 <= 0;
  156. l5 <= 0;
  157. l6 <= 0;
  158. l7 <= 0;
  159. l8 <= 0;
  160. l9 <= 0;
  161. l10 <= 0;
  162. l11 <= 0;
  163. l12 <= 0;
  164. l13 <= 0;
  165. l14 <= 0;
  166. l15 <= 0;
  167. l16 <= 0;
  168. r0 <= 0;
  169. r1 <= 0;
  170. r2 <= 0;
  171. r3 <= 0;
  172. r4 <= 0;
  173. r5 <= 0;
  174. r6 <= 0;
  175. r7 <= 0;
  176. r8 <= 0;
  177. r9 <= 0;
  178. r10 <= 0;
  179. r11 <= 0;
  180. r12 <= 0;
  181. r13 <= 0;
  182. r14 <= 0;
  183. r15 <= 0;
  184. r16 <= 0;
  185. data_o <= 0;
  186. end
  187. else begin
  188. // output stage
  189. data_o <= ipn({r16, l16});
  190. // 16. stage
  191. l16 <= r15;
  192. r16 <= l15 ^ (f(r15, key16));
  193. // 15. stage
  194. l15 <= r14;
  195. r15 <= l14 ^ (f(r14, key15));
  196. // 14. stage
  197. l14 <= r13;
  198. r14 <= l13 ^ (f(r13, key14));
  199. // 13. stage
  200. l13 <= r12;
  201. r13 <= l12 ^ (f(r12, key13));
  202. // 12. stage
  203. l12 <= r11;
  204. r12 <= l11 ^ (f(r11, key12));
  205. // 11. stage
  206. l11 <= r10;
  207. r11 <= l10 ^ (f(r10, key11));
  208. // 10. stage
  209. l10 <= r9;
  210. r10 <= l9 ^ (f(r9, key10));
  211. // 9. stage
  212. l9 <= r8;
  213. r9 <= l8 ^ (f(r8, key9));
  214. // 8. stage
  215. l8 <= r7;
  216. r8 <= l7 ^ (f(r7, key8));
  217. // 7. stage
  218. l7 <= r6;
  219. r7 <= l6 ^ (f(r6, key7));
  220. // 6. stage
  221. l6 <= r5;
  222. r6 <= l5 ^ (f(r5, key6));
  223. // 5. stage
  224. l5 <= r4;
  225. r5 <= l4 ^ (f(r4, key5));
  226. // 4. stage
  227. l4 <= r3;
  228. r4 <= l3 ^ (f(r3, key4));
  229. // 3. stage
  230. l3 <= r2;
  231. r3 <= l2 ^ (f(r2, key3));
  232. // 2. stage
  233. l2 <= r1;
  234. r2 <= l1 ^ (f(r1, key2));
  235. // 1. stage
  236. l1 <= r0;
  237. r1 <= l0 ^ (f(r0, key1));
  238. // 1. state
  239. l0 <= l;
  240. r0 <= r;
  241. // input stage
  242. l <= ip0(data_i);
  243. r <= ip1(data_i);
  244. end
  245. end
  246. // des key pipeline
  247. always @(posedge clk_i, negedge reset_i) begin
  248. if(~reset_i) begin
  249. c0 <= 0;
  250. c1 <= 0;
  251. c2 <= 0;
  252. c3 <= 0;
  253. c4 <= 0;
  254. c5 <= 0;
  255. c6 <= 0;
  256. c7 <= 0;
  257. c8 <= 0;
  258. c9 <= 0;
  259. c10 <= 0;
  260. c11 <= 0;
  261. c12 <= 0;
  262. c13 <= 0;
  263. c14 <= 0;
  264. c15 <= 0;
  265. c16 <= 0;
  266. d0 <= 0;
  267. d1 <= 0;
  268. d2 <= 0;
  269. d3 <= 0;
  270. d4 <= 0;
  271. d5 <= 0;
  272. d6 <= 0;
  273. d7 <= 0;
  274. d8 <= 0;
  275. d9 <= 0;
  276. d10 <= 0;
  277. d11 <= 0;
  278. d12 <= 0;
  279. d13 <= 0;
  280. d14 <= 0;
  281. d15 <= 0;
  282. d16 <= 0;
  283. end
  284. else begin
  285. // input stage
  286. c0 <= pc1_c(key_i);
  287. d0 <= pc1_d(key_i);
  288. // 1st stage
  289. if (~mode[0]) begin
  290. c1 <= {c0[1:27], c0[0]};
  291. d1 <= {d0[1:27], d0[0]};
  292. end
  293. else begin
  294. c1 <= c0;
  295. d1 <= d0;
  296. end
  297. // 2nd stage
  298. if (~mode[1]) begin
  299. c2 <= {c1[1:27], c1[0]};
  300. d2 <= {d1[1:27], d1[0]};
  301. end
  302. else begin
  303. c2 <= {c1[27], c1[0:26]};
  304. d2 <= {d1[27], d1[0:26]};
  305. end
  306. // 3rd stage
  307. if (~mode[2]) begin
  308. c3 <= {c2[2:27], c2[0:1]};
  309. d3 <= {d2[2:27], d2[0:1]};
  310. end
  311. else begin
  312. c3 <= {c2[26:27], c2[0:25]};
  313. d3 <= {d2[26:27], d2[0:25]};
  314. end
  315. // 4th stage
  316. if (~mode[3]) begin
  317. c4 <= {c3[2:27], c3[0:1]};
  318. d4 <= {d3[2:27], d3[0:1]};
  319. end
  320. else begin
  321. c4 <= {c3[26:27], c3[0:25]};
  322. d4 <= {d3[26:27], d3[0:25]};
  323. end
  324. // 5th stage
  325. if (~mode[4]) begin
  326. c5 <= {c4[2:27], c4[0:1]};
  327. d5 <= {d4[2:27], d4[0:1]};
  328. end
  329. else begin
  330. c5 <= {c4[26:27], c4[0:25]};
  331. d5 <= {d4[26:27], d4[0:25]};
  332. end
  333. // 6. stage
  334. if (~mode[5]) begin
  335. c6 <= {c5[2:27], c5[0:1]};
  336. d6 <= {d5[2:27], d5[0:1]};
  337. end
  338. else begin
  339. c6 <= {c5[26:27], c5[0:25]};
  340. d6 <= {d5[26:27], d5[0:25]};
  341. end
  342. // 7. stage
  343. if (~mode[6]) begin
  344. c7 <= {c6[2:27], c6[0:1]};
  345. d7 <= {d6[2:27], d6[0:1]};
  346. end
  347. else begin
  348. c7 <= {c6[26:27], c6[0:25]};
  349. d7 <= {d6[26:27], d6[0:25]};
  350. end
  351. // 8. stage
  352. if (~mode[7]) begin
  353. c8 <= {c7[2:27], c7[0:1]};
  354. d8 <= {d7[2:27], d7[0:1]};
  355. end
  356. else begin
  357. c8 <= {c7[26:27], c7[0:25]};
  358. d8 <= {d7[26:27], d7[0:25]};
  359. end
  360. // 9. stage
  361. if (~mode[8]) begin
  362. c9 <= {c8[1:27], c8[0]};
  363. d9 <= {d8[1:27], d8[0]};
  364. end
  365. else begin
  366. c9 <= {c8[27], c8[0:26]};
  367. d9 <= {d8[27], d8[0:26]};
  368. end
  369. // 10. stage
  370. if (~mode[9]) begin
  371. c10 <= {c9[2:27], c9[0:1]};
  372. d10 <= {d9[2:27], d9[0:1]};
  373. end
  374. else begin
  375. c10 <= {c9[26:27], c9[0:25]};
  376. d10 <= {d9[26:27], d9[0:25]};
  377. end
  378. // 6. stage
  379. if (~mode[10]) begin
  380. c11 <= {c10[2:27], c10[0:1]};
  381. d11 <= {d10[2:27], d10[0:1]};
  382. end
  383. else begin
  384. c11 <= {c10[26:27], c10[0:25]};
  385. d11 <= {d10[26:27], d10[0:25]};
  386. end
  387. // 6. stage
  388. if (~mode[11]) begin
  389. c12 <= {c11[2:27], c11[0:1]};
  390. d12 <= {d11[2:27], d11[0:1]};
  391. end
  392. else begin
  393. c12 <= {c11[26:27], c11[0:25]};
  394. d12 <= {d11[26:27], d11[0:25]};
  395. end
  396. // 6. stage
  397. if (~mode[12]) begin
  398. c13 <= {c12[2:27], c12[0:1]};
  399. d13 <= {d12[2:27], d12[0:1]};
  400. end
  401. else begin
  402. c13 <= {c12[26:27], c12[0:25]};
  403. d13 <= {d12[26:27], d12[0:25]};
  404. end
  405. // 6. stage
  406. if (~mode[13]) begin
  407. c14 <= {c13[2:27], c13[0:1]};
  408. d14 <= {d13[2:27], d13[0:1]};
  409. end
  410. else begin
  411. c14 <= {c13[26:27], c13[0:25]};
  412. d14 <= {d13[26:27], d13[0:25]};
  413. end
  414. // 6. stage
  415. if (~mode[14]) begin
  416. c15 <= {c14[2:27], c14[0:1]};
  417. d15 <= {d14[2:27], d14[0:1]};
  418. end
  419. else begin
  420. c15 <= {c14[26:27], c14[0:25]};
  421. d15 <= {d14[26:27], d14[0:25]};
  422. end
  423. // 6. stage
  424. if (~mode[15]) begin
  425. c16 <= {c15[1:27], c15[0]};
  426. d16 <= {d15[1:27], d15[0]};
  427. end
  428. else begin
  429. c16 <= {c15[27], c15[0:26]};
  430. d16 <= {d15[27], d15[0:26]};
  431. end
  432. end
  433. end
  434. // key assignments
  435. assign key1 = pc2({c1, d1});
  436. assign key2 = pc2({c2, d2});
  437. assign key3 = pc2({c3, d3});
  438. assign key4 = pc2({c4, d4});
  439. assign key5 = pc2({c5, d5});
  440. assign key6 = pc2({c6, d6});
  441. assign key7 = pc2({c7, d7});
  442. assign key8 = pc2({c8, d8});
  443. assign key9 = pc2({c9, d9});
  444. assign key10 = pc2({c10, d10});
  445. assign key11 = pc2({c11, d11});
  446. assign key12 = pc2({c12, d12});
  447. assign key13 = pc2({c13, d13});
  448. assign key14 = pc2({c14, d14});
  449. assign key15 = pc2({c15, d15});
  450. assign key16 = pc2({c16, d16});
  451. `endif
  452. `ifdef ITER
  453. // mode register
  454. reg valid;
  455. reg mode;
  456. integer state;
  457. // algorithm pipeline register
  458. // key calculation register
  459. reg [0:27] c;
  460. reg [0:27] d;
  461. // key register
  462. reg [0:47] key;
  463. // register for left, right data blocks
  464. reg [0:31] l;
  465. reg [0:31] r;
  466. wire valid_o = valid;
  467. always @(posedge clk_i, negedge reset_i) begin
  468. if (~reset_i) begin
  469. c <= 0;
  470. d <= 0;
  471. end else begin
  472. case (state)
  473. 3, 4, 5, 6, 7, 8 , 10 , 11, 12, 13, 14, 15 : begin
  474. if (mode) begin
  475. c <= {c[26:27], c[0:25]};
  476. d <= {d[26:27], d[0:25]};
  477. end else begin
  478. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  479. c <= {c[2:27], c[0:1]};
  480. d <= {d[2:27], d[0:1]};
  481. end
  482. end
  483. 1 : begin
  484. if (~mode) begin
  485. c <= {c[1:27], c[0]};
  486. d <= {c[0], d[1:27], d[0]};
  487. end
  488. end
  489. 2, 9, 16 : begin
  490. if (mode) begin
  491. c <= {c[27], c[0:26]};
  492. d <= {d[27], d[0:26]};
  493. end else begin
  494. r <= l ^ f(r, pc2({c[1:27], c[0], d[1:27], d[0]}));
  495. c <= {c[1:27], c[0]};
  496. d <= {d[1:27], d[0]};
  497. end
  498. end
  499. endcase
  500. end
  501. end
  502. always @(posedge clk_i, negedge reset_i) begin
  503. if(~reset_i) begin
  504. l <= 0;
  505. r <= 0;
  506. key <= 0;
  507. c <= 0;
  508. d <= 0;
  509. state <= 0;
  510. mode <= 0;
  511. valid <= 0;
  512. accept_o <= 0;
  513. data_o <= 0;
  514. end
  515. else begin
  516. case (state)
  517. 0 : begin
  518. l <= 0;
  519. r <= 0;
  520. key <= 0;
  521. c <= 0;
  522. d <= 0;
  523. mode <= 0;
  524. valid <= 0;
  525. accept_o <= 1;
  526. if (valid_i && accept_o) begin
  527. accept_o <= 0;
  528. mode <= mode_i;
  529. l <= ip0(data_i);
  530. r <= ip1(data_i);
  531. c <= pc1_c(key_i);
  532. d <= pc1_d(key_i);
  533. state <= state + 1;
  534. end
  535. end
  536. 1 : begin
  537. if (mode) begin
  538. r <= l ^ f(r, pc2({c, d}));
  539. end else begin
  540. r <= l ^ f(r, pc2({c[1:27], c[0], d[1:27], d[0]}));
  541. end
  542. l <= r;
  543. state <= state + 1;
  544. end
  545. 2 : begin
  546. if (mode) begin
  547. r <= l ^ f(r, pc2({c[27], c[0:26], d[27], d[0:26]}));
  548. end else begin
  549. r <= l ^ f(r, pc2({c[1:27], c[0], d[1:27], d[0]}));
  550. end
  551. l <= r;
  552. state <= state + 1;
  553. end
  554. 3 : begin
  555. if (mode) begin
  556. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  557. end else begin
  558. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  559. end
  560. l <= r;
  561. state <= state + 1;
  562. end
  563. 4 : begin
  564. if (mode) begin
  565. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  566. end else begin
  567. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  568. end
  569. l <= r;
  570. state <= state + 1;
  571. end
  572. 5 : begin
  573. if (mode) begin
  574. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  575. end else begin
  576. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  577. end
  578. l <= r;
  579. state <= state + 1;
  580. end
  581. 6 : begin
  582. if (mode) begin
  583. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  584. end else begin
  585. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  586. end
  587. l <= r;
  588. state <= state + 1;
  589. end
  590. 7 : begin
  591. if (mode) begin
  592. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  593. end else begin
  594. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  595. end
  596. l <= r;
  597. state <= state + 1;
  598. end
  599. 8 : begin
  600. if (mode) begin
  601. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  602. end else begin
  603. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  604. end
  605. l <= r;
  606. state <= state + 1;
  607. end
  608. 9 : begin
  609. if (mode) begin
  610. r <= l ^ f(r, pc2({c[27], c[0:26], d[27], d[0:26]}));
  611. end else begin
  612. r <= l ^ f(r, pc2({c[1:27], c[0], d[1:27], d[0]}));
  613. end
  614. l <= r;
  615. state <= state + 1;
  616. end
  617. 10 : begin
  618. if (mode) begin
  619. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  620. end else begin
  621. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  622. end
  623. l <= r;
  624. state <= state + 1;
  625. end
  626. 11 : begin
  627. if (mode) begin
  628. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  629. end else begin
  630. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  631. end
  632. l <= r;
  633. state <= state + 1;
  634. end
  635. 12 : begin
  636. if (mode) begin
  637. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  638. end else begin
  639. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  640. end
  641. l <= r;
  642. state <= state + 1;
  643. end
  644. 13 : begin
  645. if (mode) begin
  646. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  647. end else begin
  648. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  649. end
  650. l <= r;
  651. state <= state + 1;
  652. end
  653. 14 : begin
  654. if (mode) begin
  655. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  656. end else begin
  657. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  658. end
  659. l <= r;
  660. state <= state + 1;
  661. end
  662. 15 : begin
  663. if (mode) begin
  664. r <= l ^ f(r, pc2({c[26:27], c[0:25], d[26:27], d[0:25]}));
  665. end else begin
  666. r <= l ^ f(r, pc2({c[2:27], c[0:1], d[2:27], d[0:1]}));
  667. end
  668. l <= r;
  669. state <= state + 1;
  670. end
  671. 16 : begin
  672. if (mode) begin
  673. r <= l ^ f(r, pc2({c[27], c[0:26], d[27], d[0:26]}));
  674. end else begin
  675. r <= l ^ f(r, pc2({c[1:27], c[0], d[1:27], d[0]}));
  676. end
  677. l <= r;
  678. state <= state + 1;
  679. end
  680. 17 : begin
  681. valid <= 1;
  682. data_o <= ipn({r, l});
  683. if (valid&& accept_i) begin
  684. valid <= 0;
  685. state <= 0;
  686. end
  687. end
  688. default :
  689. state <= 0;
  690. endcase
  691. end
  692. end
  693. assign valid_o = valid;
  694. `endif
  695. endmodule