Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  1. //======================================================================
  2. //
  3. // sha256_core.v
  4. // -------------
  5. // Verilog 2001 implementation of the SHA-256 hash function.
  6. // This is the internal core with wide interfaces.
  7. //
  8. //
  9. // Author: Joachim Strombergson
  10. // Copyright (c) 2013, Secworks Sweden AB
  11. // All rights reserved.
  12. //
  13. // Redistribution and use in source and binary forms, with or
  14. // without modification, are permitted provided that the following
  15. // conditions are met:
  16. //
  17. // 1. Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // 2. Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in
  22. // the documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  36. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. //======================================================================
  39.  
  40. module sha256_core(
  41. input wire clk,
  42. input wire reset_n,
  43.  
  44. input wire init,
  45. input wire next,
  46. input wire mode,
  47.  
  48. input wire [511 : 0] block,
  49.  
  50. output wire ready,
  51. output wire [255 : 0] digest,
  52. output wire digest_valid
  53. );
  54.  
  55.  
  56. //----------------------------------------------------------------
  57. // Internal constant and parameter definitions.
  58. //----------------------------------------------------------------
  59. parameter SHA224_H0_0 = 32'hc1059ed8;
  60. parameter SHA224_H0_1 = 32'h367cd507;
  61. parameter SHA224_H0_2 = 32'h3070dd17;
  62. parameter SHA224_H0_3 = 32'hf70e5939;
  63. parameter SHA224_H0_4 = 32'hffc00b31;
  64. parameter SHA224_H0_5 = 32'h68581511;
  65. parameter SHA224_H0_6 = 32'h64f98fa7;
  66. parameter SHA224_H0_7 = 32'hbefa4fa4;
  67.  
  68. parameter SHA256_H0_0 = 32'h6a09e667;
  69. parameter SHA256_H0_1 = 32'hbb67ae85;
  70. parameter SHA256_H0_2 = 32'h3c6ef372;
  71. parameter SHA256_H0_3 = 32'ha54ff53a;
  72. parameter SHA256_H0_4 = 32'h510e527f;
  73. parameter SHA256_H0_5 = 32'h9b05688c;
  74. parameter SHA256_H0_6 = 32'h1f83d9ab;
  75. parameter SHA256_H0_7 = 32'h5be0cd19;
  76.  
  77. parameter SHA256_ROUNDS = 63;
  78.  
  79. parameter CTRL_IDLE = 0;
  80. parameter CTRL_ROUNDS = 1;
  81. parameter CTRL_DONE = 2;
  82.  
  83.  
  84. //----------------------------------------------------------------
  85. // Registers including update variables and write enable.
  86. //----------------------------------------------------------------
  87. reg [31 : 0] a_reg;
  88. reg [31 : 0] a_new;
  89. reg [31 : 0] b_reg;
  90. reg [31 : 0] b_new;
  91. reg [31 : 0] c_reg;
  92. reg [31 : 0] c_new;
  93. reg [31 : 0] d_reg;
  94. reg [31 : 0] d_new;
  95. reg [31 : 0] e_reg;
  96. reg [31 : 0] e_new;
  97. reg [31 : 0] f_reg;
  98. reg [31 : 0] f_new;
  99. reg [31 : 0] g_reg;
  100. reg [31 : 0] g_new;
  101. reg [31 : 0] h_reg;
  102. reg [31 : 0] h_new;
  103. reg a_h_we;
  104.  
  105. reg [31 : 0] H0_reg;
  106. reg [31 : 0] H0_new;
  107. reg [31 : 0] H1_reg;
  108. reg [31 : 0] H1_new;
  109. reg [31 : 0] H2_reg;
  110. reg [31 : 0] H2_new;
  111. reg [31 : 0] H3_reg;
  112. reg [31 : 0] H3_new;
  113. reg [31 : 0] H4_reg;
  114. reg [31 : 0] H4_new;
  115. reg [31 : 0] H5_reg;
  116. reg [31 : 0] H5_new;
  117. reg [31 : 0] H6_reg;
  118. reg [31 : 0] H6_new;
  119. reg [31 : 0] H7_reg;
  120. reg [31 : 0] H7_new;
  121. reg H_we;
  122.  
  123. reg [5 : 0] t_ctr_reg;
  124. reg [5 : 0] t_ctr_new;
  125. reg t_ctr_we;
  126. reg t_ctr_inc;
  127. reg t_ctr_rst;
  128.  
  129. reg digest_valid_reg;
  130. reg digest_valid_new;
  131. reg digest_valid_we;
  132.  
  133. reg [1 : 0] sha256_ctrl_reg;
  134. reg [1 : 0] sha256_ctrl_new;
  135. reg sha256_ctrl_we;
  136.  
  137.  
  138. //----------------------------------------------------------------
  139. // Wires.
  140. //----------------------------------------------------------------
  141. reg digest_init;
  142. reg digest_update;
  143.  
  144. reg state_init;
  145. reg state_update;
  146.  
  147. reg first_block;
  148.  
  149. reg ready_flag;
  150.  
  151. reg [31 : 0] t1;
  152. reg [31 : 0] t2;
  153.  
  154. wire [31 : 0] k_data;
  155.  
  156. reg w_init;
  157. reg w_next;
  158. wire [31 : 0] w_data;
  159.  
  160.  
  161. //----------------------------------------------------------------
  162. // Module instantiantions.
  163. //----------------------------------------------------------------
  164. sha256_k_constants k_constants_inst(
  165. .addr(t_ctr_reg),
  166. .K(k_data)
  167. );
  168.  
  169.  
  170. sha256_w_mem w_mem_inst(
  171. .clk(clk),
  172. .reset_n(reset_n),
  173.  
  174. .block(block),
  175.  
  176. .init(w_init),
  177. .next(w_next),
  178. .w(w_data)
  179. );
  180.  
  181.  
  182. //----------------------------------------------------------------
  183. // Concurrent connectivity for ports etc.
  184. //----------------------------------------------------------------
  185. assign ready = ready_flag;
  186.  
  187. assign digest = {H0_reg, H1_reg, H2_reg, H3_reg,
  188. H4_reg, H5_reg, H6_reg, H7_reg};
  189.  
  190. assign digest_valid = digest_valid_reg;
  191.  
  192.  
  193. //----------------------------------------------------------------
  194. // reg_update
  195. // Update functionality for all registers in the core.
  196. // All registers are positive edge triggered with asynchronous
  197. // active low reset. All registers have write enable.
  198. //----------------------------------------------------------------
  199. always @ (posedge clk or negedge reset_n)
  200. begin : reg_update
  201. if (!reset_n)
  202. begin
  203. a_reg <= 32'h0;
  204. b_reg <= 32'h0;
  205. c_reg <= 32'h0;
  206. d_reg <= 32'h0;
  207. e_reg <= 32'h0;
  208. f_reg <= 32'h0;
  209. g_reg <= 32'h0;
  210. h_reg <= 32'h0;
  211. H0_reg <= 32'h0;
  212. H1_reg <= 32'h0;
  213. H2_reg <= 32'h0;
  214. H3_reg <= 32'h0;
  215. H4_reg <= 32'h0;
  216. H5_reg <= 32'h0;
  217. H6_reg <= 32'h0;
  218. H7_reg <= 32'h0;
  219. digest_valid_reg <= 0;
  220. t_ctr_reg <= 6'h0;
  221. sha256_ctrl_reg <= CTRL_IDLE;
  222. end
  223. else
  224. begin
  225.  
  226. if (a_h_we)
  227. begin
  228. a_reg <= a_new;
  229. b_reg <= b_new;
  230. c_reg <= c_new;
  231. d_reg <= d_new;
  232. e_reg <= e_new;
  233. f_reg <= f_new;
  234. g_reg <= g_new;
  235. h_reg <= h_new;
  236. end
  237.  
  238. if (H_we)
  239. begin
  240. H0_reg <= H0_new;
  241. H1_reg <= H1_new;
  242. H2_reg <= H2_new;
  243. H3_reg <= H3_new;
  244. H4_reg <= H4_new;
  245. H5_reg <= H5_new;
  246. H6_reg <= H6_new;
  247. H7_reg <= H7_new;
  248. end
  249.  
  250. if (t_ctr_we)
  251. t_ctr_reg <= t_ctr_new;
  252.  
  253. if (digest_valid_we)
  254. digest_valid_reg <= digest_valid_new;
  255.  
  256. if (sha256_ctrl_we)
  257. sha256_ctrl_reg <= sha256_ctrl_new;
  258. end
  259. end // reg_update
  260.  
  261.  
  262. //----------------------------------------------------------------
  263. // digest_logic
  264. //
  265. // The logic needed to init as well as update the digest.
  266. //----------------------------------------------------------------
  267. always @*
  268. begin : digest_logic
  269. H0_new = 32'h0;
  270. H1_new = 32'h0;
  271. H2_new = 32'h0;
  272. H3_new = 32'h0;
  273. H4_new = 32'h0;
  274. H5_new = 32'h0;
  275. H6_new = 32'h0;
  276. H7_new = 32'h0;
  277. H_we = 0;
  278.  
  279. if (digest_init)
  280. begin
  281. H_we = 1;
  282. if (mode)
  283. begin
  284. H0_new = SHA256_H0_0;
  285. H1_new = SHA256_H0_1;
  286. H2_new = SHA256_H0_2;
  287. H3_new = SHA256_H0_3;
  288. H4_new = SHA256_H0_4;
  289. H5_new = SHA256_H0_5;
  290. H6_new = SHA256_H0_6;
  291. H7_new = SHA256_H0_7;
  292. end
  293. else
  294. begin
  295. H0_new = SHA224_H0_0;
  296. H1_new = SHA224_H0_1;
  297. H2_new = SHA224_H0_2;
  298. H3_new = SHA224_H0_3;
  299. H4_new = SHA224_H0_4;
  300. H5_new = SHA224_H0_5;
  301. H6_new = SHA224_H0_6;
  302. H7_new = SHA224_H0_7;
  303. end
  304. end
  305.  
  306. if (digest_update)
  307. begin
  308. H0_new = H0_reg + a_reg;
  309. H1_new = H1_reg + b_reg;
  310. H2_new = H2_reg + c_reg;
  311. H3_new = H3_reg + d_reg;
  312. H4_new = H4_reg + e_reg;
  313. H5_new = H5_reg + f_reg;
  314. H6_new = H6_reg + g_reg;
  315. H7_new = H7_reg + h_reg;
  316. H_we = 1;
  317. end
  318. end // digest_logic
  319.  
  320.  
  321. //----------------------------------------------------------------
  322. // t1_logic
  323. //
  324. // The logic for the T1 function.
  325. //----------------------------------------------------------------
  326. always @*
  327. begin : t1_logic
  328. reg [31 : 0] sum1;
  329. reg [31 : 0] ch;
  330.  
  331. sum1 = {e_reg[5 : 0], e_reg[31 : 6]} ^
  332. {e_reg[10 : 0], e_reg[31 : 11]} ^
  333. {e_reg[24 : 0], e_reg[31 : 25]};
  334.  
  335. ch = (e_reg & f_reg) ^ ((~e_reg) & g_reg);
  336.  
  337. t1 = h_reg + sum1 + ch + w_data + k_data;
  338. end // t1_logic
  339.  
  340.  
  341. //----------------------------------------------------------------
  342. // t2_logic
  343. //
  344. // The logic for the T2 function
  345. //----------------------------------------------------------------
  346. always @*
  347. begin : t2_logic
  348. reg [31 : 0] sum0;
  349. reg [31 : 0] maj;
  350.  
  351. sum0 = {a_reg[1 : 0], a_reg[31 : 2]} ^
  352. {a_reg[12 : 0], a_reg[31 : 13]} ^
  353. {a_reg[21 : 0], a_reg[31 : 22]};
  354.  
  355. maj = (a_reg & b_reg) ^ (a_reg & c_reg) ^ (b_reg & c_reg);
  356.  
  357. t2 = sum0 + maj;
  358. end // t2_logic
  359.  
  360.  
  361. //----------------------------------------------------------------
  362. // state_logic
  363. //
  364. // The logic needed to init as well as update the state during
  365. // round processing.
  366. //----------------------------------------------------------------
  367. always @*
  368. begin : state_logic
  369. a_new = 32'h0;
  370. b_new = 32'h0;
  371. c_new = 32'h0;
  372. d_new = 32'h0;
  373. e_new = 32'h0;
  374. f_new = 32'h0;
  375. g_new = 32'h0;
  376. h_new = 32'h0;
  377. a_h_we = 0;
  378.  
  379. if (state_init)
  380. begin
  381. a_h_we = 1;
  382. if (first_block)
  383. begin
  384. if (mode)
  385. begin
  386. a_new = SHA256_H0_0;
  387. b_new = SHA256_H0_1;
  388. c_new = SHA256_H0_2;
  389. d_new = SHA256_H0_3;
  390. e_new = SHA256_H0_4;
  391. f_new = SHA256_H0_5;
  392. g_new = SHA256_H0_6;
  393. h_new = SHA256_H0_7;
  394. end
  395. else
  396. begin
  397. a_new = SHA224_H0_0;
  398. b_new = SHA224_H0_1;
  399. c_new = SHA224_H0_2;
  400. d_new = SHA224_H0_3;
  401. e_new = SHA224_H0_4;
  402. f_new = SHA224_H0_5;
  403. g_new = SHA224_H0_6;
  404. h_new = SHA224_H0_7;
  405. end
  406. end
  407. else
  408. begin
  409. a_new = H0_reg;
  410. b_new = H1_reg;
  411. c_new = H2_reg;
  412. d_new = H3_reg;
  413. e_new = H4_reg;
  414. f_new = H5_reg;
  415. g_new = H6_reg;
  416. h_new = H7_reg;
  417. end
  418. end
  419.  
  420. if (state_update)
  421. begin
  422. a_new = t1 + t2;
  423. b_new = a_reg;
  424. c_new = b_reg;
  425. d_new = c_reg;
  426. e_new = d_reg + t1;
  427. f_new = e_reg;
  428. g_new = f_reg;
  429. h_new = g_reg;
  430. a_h_we = 1;
  431. end
  432. end // state_logic
  433.  
  434.  
  435. //----------------------------------------------------------------
  436. // t_ctr
  437. //
  438. // Update logic for the round counter, a monotonically
  439. // increasing counter with reset.
  440. //----------------------------------------------------------------
  441. always @*
  442. begin : t_ctr
  443. t_ctr_new = 0;
  444. t_ctr_we = 0;
  445.  
  446. if (t_ctr_rst)
  447. begin
  448. t_ctr_new = 0;
  449. t_ctr_we = 1;
  450. end
  451.  
  452. if (t_ctr_inc)
  453. begin
  454. t_ctr_new = t_ctr_reg + 1'b1;
  455. t_ctr_we = 1;
  456. end
  457. end // t_ctr
  458.  
  459.  
  460. //----------------------------------------------------------------
  461. // sha256_ctrl_fsm
  462. //
  463. // Logic for the state machine controlling the core behaviour.
  464. //----------------------------------------------------------------
  465. always @*
  466. begin : sha256_ctrl_fsm
  467. digest_init = 0;
  468. digest_update = 0;
  469.  
  470. state_init = 0;
  471. state_update = 0;
  472.  
  473. first_block = 0;
  474. ready_flag = 0;
  475.  
  476. w_init = 0;
  477. w_next = 0;
  478.  
  479. t_ctr_inc = 0;
  480. t_ctr_rst = 0;
  481.  
  482. digest_valid_new = 0;
  483. digest_valid_we = 0;
  484.  
  485. sha256_ctrl_new = CTRL_IDLE;
  486. sha256_ctrl_we = 0;
  487.  
  488.  
  489. case (sha256_ctrl_reg)
  490. CTRL_IDLE:
  491. begin
  492. ready_flag = 1;
  493.  
  494. if (init)
  495. begin
  496. digest_init = 1;
  497. w_init = 1;
  498. state_init = 1;
  499. first_block = 1;
  500. t_ctr_rst = 1;
  501. digest_valid_new = 0;
  502. digest_valid_we = 1;
  503. sha256_ctrl_new = CTRL_ROUNDS;
  504. sha256_ctrl_we = 1;
  505. end
  506.  
  507. if (next)
  508. begin
  509. t_ctr_rst = 1;
  510. w_init = 1;
  511. state_init = 1;
  512. digest_valid_new = 0;
  513. digest_valid_we = 1;
  514. sha256_ctrl_new = CTRL_ROUNDS;
  515. sha256_ctrl_we = 1;
  516. end
  517. end
  518.  
  519.  
  520. CTRL_ROUNDS:
  521. begin
  522. w_next = 1;
  523. state_update = 1;
  524. t_ctr_inc = 1;
  525.  
  526. if (t_ctr_reg == SHA256_ROUNDS)
  527. begin
  528. sha256_ctrl_new = CTRL_DONE;
  529. sha256_ctrl_we = 1;
  530. end
  531. end
  532.  
  533.  
  534. CTRL_DONE:
  535. begin
  536. digest_update = 1;
  537. digest_valid_new = 1;
  538. digest_valid_we = 1;
  539.  
  540. sha256_ctrl_new = CTRL_IDLE;
  541. sha256_ctrl_we = 1;
  542. end
  543. endcase // case (sha256_ctrl_reg)
  544. end // sha256_ctrl_fsm
  545.  
  546. endmodule // sha256_core
  547.  
  548. //======================================================================
  549. // EOF sha256_core.v
  550. //======================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement