OpenCores
URL https://opencores.org/ocsvn/sha_core/sha_core/trunk

Subversion Repositories sha_core

[/] [sha_core/] [trunk/] [rtl/] [sha512.v] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marsgod
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  SHA-512/384                                                ////
4
////  Secure Hash Algorithm (SHA-512 SHA-384)                    ////
5
////                                                             ////
6
////  Author: marsgod                                            ////
7
////          marsgod@opencores.org                              ////
8
////                                                             ////
9
////                                                             ////
10
////  Downloaded from: http://www.opencores.org/cores/sha_core/  ////
11
////                                                             ////
12
/////////////////////////////////////////////////////////////////////
13
////                                                             ////
14
//// Copyright (C) 2000-2002 marsgod                             ////
15
////                         marsgod@opencores.org               ////
16
////                                                             ////
17
////                                                             ////
18
//// This source file may be used and distributed without        ////
19
//// restriction provided that this copyright statement is not   ////
20
//// removed from the file and that any derivative work contains ////
21
//// the original copyright notice and the associated disclaimer.////
22
////                                                             ////
23
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
24
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
25
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
26
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
27
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
28
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
29
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
30
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
31
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
32
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
33
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
34
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
35
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
36
////                                                             ////
37
/////////////////////////////////////////////////////////////////////
38
 
39
`define SHA512_H0 64'h6a09e667_f3bcc908
40
`define SHA512_H1 64'hbb67ae85_84caa73b
41
`define SHA512_H2 64'h3c6ef372_fe94f82b
42
`define SHA512_H3 64'ha54ff53a_5f1d36f1
43
`define SHA512_H4 64'h510e527f_ade682d1
44
`define SHA512_H5 64'h9b05688c_2b3e6c1f
45
`define SHA512_H6 64'h1f83d9ab_fb41bd6b
46
`define SHA512_H7 64'h5be0cd19_137e2179
47
 
48
`define SHA384_H0 64'hcbbb9d5d_c1059ed8
49
`define SHA384_H1 64'h629a292a_367cd507
50
`define SHA384_H2 64'h9159015a_3070dd17
51
`define SHA384_H3 64'h152fecd8_f70e5939
52
`define SHA384_H4 64'h67332667_ffc00b31
53
`define SHA384_H5 64'h8eb44a87_68581511
54
`define SHA384_H6 64'hdb0c2e0d_64f98fa7
55
`define SHA384_H7 64'h47b5481d_befa4fa4
56
 
57
`define K00 64'h428a2f98_d728ae22
58
`define K01 64'h71374491_23ef65cd
59
`define K02 64'hb5c0fbcf_ec4d3b2f
60
`define K03 64'he9b5dba5_8189dbbc
61
`define K04 64'h3956c25b_f348b538
62
`define K05 64'h59f111f1_b605d019
63
`define K06 64'h923f82a4_af194f9b
64
`define K07 64'hab1c5ed5_da6d8118
65
`define K08 64'hd807aa98_a3030242
66
`define K09 64'h12835b01_45706fbe
67
`define K10 64'h243185be_4ee4b28c
68
`define K11 64'h550c7dc3_d5ffb4e2
69
`define K12 64'h72be5d74_f27b896f
70
`define K13 64'h80deb1fe_3b1696b1
71
`define K14 64'h9bdc06a7_25c71235
72
`define K15 64'hc19bf174_cf692694
73
`define K16 64'he49b69c1_9ef14ad2
74
`define K17 64'hefbe4786_384f25e3
75
`define K18 64'h0fc19dc6_8b8cd5b5
76
`define K19 64'h240ca1cc_77ac9c65
77
`define K20 64'h2de92c6f_592b0275
78
`define K21 64'h4a7484aa_6ea6e483
79
`define K22 64'h5cb0a9dc_bd41fbd4
80
`define K23 64'h76f988da_831153b5
81
`define K24 64'h983e5152_ee66dfab
82
`define K25 64'ha831c66d_2db43210
83
`define K26 64'hb00327c8_98fb213f
84
`define K27 64'hbf597fc7_beef0ee4
85
`define K28 64'hc6e00bf3_3da88fc2
86
`define K29 64'hd5a79147_930aa725
87
`define K30 64'h06ca6351_e003826f
88
`define K31 64'h14292967_0a0e6e70
89
`define K32 64'h27b70a85_46d22ffc
90
`define K33 64'h2e1b2138_5c26c926
91
`define K34 64'h4d2c6dfc_5ac42aed
92
`define K35 64'h53380d13_9d95b3df
93
`define K36 64'h650a7354_8baf63de
94
`define K37 64'h766a0abb_3c77b2a8
95
`define K38 64'h81c2c92e_47edaee6
96
`define K39 64'h92722c85_1482353b
97
`define K40 64'ha2bfe8a1_4cf10364
98
`define K41 64'ha81a664b_bc423001
99
`define K42 64'hc24b8b70_d0f89791
100
`define K43 64'hc76c51a3_0654be30
101
`define K44 64'hd192e819_d6ef5218
102
`define K45 64'hd6990624_5565a910
103
`define K46 64'hf40e3585_5771202a
104
`define K47 64'h106aa070_32bbd1b8
105
`define K48 64'h19a4c116_b8d2d0c8
106
`define K49 64'h1e376c08_5141ab53
107
`define K50 64'h2748774c_df8eeb99
108
`define K51 64'h34b0bcb5_e19b48a8
109
`define K52 64'h391c0cb3_c5c95a63
110
`define K53 64'h4ed8aa4a_e3418acb
111
`define K54 64'h5b9cca4f_7763e373
112
`define K55 64'h682e6ff3_d6b2b8a3
113
`define K56 64'h748f82ee_5defb2fc
114
`define K57 64'h78a5636f_43172f60
115
`define K58 64'h84c87814_a1f0ab72
116
`define K59 64'h8cc70208_1a6439ec
117
`define K60 64'h90befffa_23631e28
118
`define K61 64'ha4506ceb_de82bde9
119
`define K62 64'hbef9a3f7_b2c67915
120
`define K63 64'hc67178f2_e372532b
121
`define K64 64'hca273ece_ea26619c
122
`define K65 64'hd186b8c7_21c0c207
123
`define K66 64'heada7dd6_cde0eb1e
124
`define K67 64'hf57d4f7f_ee6ed178
125
`define K68 64'h06f067aa_72176fba
126
`define K69 64'h0a637dc5_a2c898a6
127
`define K70 64'h113f9804_bef90dae
128
`define K71 64'h1b710b35_131c471b
129
`define K72 64'h28db77f5_23047d84
130
`define K73 64'h32caab7b_40c72493
131
`define K74 64'h3c9ebe0a_15c9bebc
132
`define K75 64'h431d67c4_9c100d4c
133
`define K76 64'h4cc5d4be_cb3e42b6
134
`define K77 64'h597f299c_fc657e2a
135
`define K78 64'h5fcb6fab_3ad6faec
136
`define K79 64'h6c44198c_4a475817
137
 
138
module sha512 (clk_i, rst_i, text_i, text_o, cmd_i, cmd_w_i, cmd_o);
139
 
140
        input           clk_i;  // global clock input
141
        input           rst_i;  // global reset input , active high
142
 
143
        input   [31:0]  text_i; // text input 32bit
144
        output  [31:0]  text_o; // text output 32bit
145
 
146
        input   [3:0]   cmd_i;  // command input
147
        input           cmd_w_i;// command input write enable
148
        output  [4:0]   cmd_o;  // command output(status)
149
 
150
        /*
151
                cmd
152
                Busy S1 S0 Round W R
153
 
154
                bit4 bit3 bit2  bit1 bit0
155
                Busy S    Round W    R
156
 
157
                Busy:
158
 
159
                1       busy
160
 
161
                S:
162
 
163
                1       sha-512
164
 
165
                Round:
166
 
167
                1       internal round
168
 
169
                W:
170
 
171
                1       write data
172
 
173
                R:
174
 
175
                1       read data
176
 
177
        */
178
 
179
 
180
        reg     [4:0]   cmd;
181
        wire    [4:0]   cmd_o;
182
 
183
        reg     [31:0]  text_o;
184
 
185
        reg     [6:0]   round;
186
        wire    [6:0]   round_plus_1;
187
 
188
        reg     [4:0]   read_counter;
189
 
190
        reg     [63:0]  H0,H1,H2,H3,H4,H5,H6,H7;
191
        reg     [63:0]  W0,W1,W2,W3,W4,W5,W6,W7,W8,W9,W10,W11,W12,W13,W14;
192
        reg     [63:0]  Wt,Kt;
193
        reg     [63:0]  A,B,C,D,E,F,G,H;
194
 
195
        reg             busy;
196
 
197
        assign cmd_o = cmd;
198
        always @ (posedge clk_i)
199
        begin
200
                if (rst_i)
201
                        cmd <= 'b0;
202
                else
203
                if (cmd_w_i)
204
                        cmd[3:0] <= cmd_i[3:0];         // busy bit can't write
205
                else
206
                begin
207
                        cmd[4] <= busy;                 // update busy bit
208
                        if (~busy)
209
                                cmd[1:0] <= 2'b00;      // hardware auto clean R/W bits
210
                end
211
        end
212
 
213
        wire [63:0] f1_EFG_64,f2_ABC_64,f3_A_64,f4_E_64,f5_W1_64,f6_W14_64,T1_64,T2_64;
214
        wire [63:0] W1_swap,W14_swap,Wt_64_swap;
215
        wire [63:0] next_Wt,next_E,next_A;
216
        wire [383:0] SHA384_result;
217
        wire [511:0] SHA512_result;
218
 
219
        assign f1_EFG_64 = (E & F) ^ (~E & G);
220
 
221
        assign f2_ABC_64 = (A & B) ^ (B & C) ^ (A & C);
222
 
223
        assign f3_A_64 = {A[27:0],A[63:28]} ^ {A[33:0],A[63:34]} ^ {A[38:0],A[63:39]};
224
 
225
        assign f4_E_64 = {E[13:0],E[63:14]} ^ {E[17:0],E[63:18]} ^ {E[40:0],E[63:41]};
226
 
227
        assign W1_swap = {W1[31:0],W1[63:32]};
228
        assign f5_W1_64 = {W1_swap[0],W1_swap[63:1]} ^ {W1_swap[7:0],W1_swap[63:8]} ^ {7'b000_0000,W1_swap[63:7]};
229
 
230
        assign W14_swap = {W14[31:0],W14[63:32]};
231
        assign f6_W14_64 = {W14_swap[18:0],W14_swap[63:19]} ^ {W14_swap[60:0],W14_swap[63:61]} ^ {6'b00_0000,W14_swap[63:6]};
232
 
233
        assign Wt_64_swap = f6_W14_64 + {W9[31:0],W9[63:32]} + f5_W1_64 + {W0[31:0],W0[63:32]};
234
 
235
        assign T1_64 = H[63:0] + f4_E_64 + f1_EFG_64 + Kt[63:0] + {Wt[31:0],Wt[63:32]};
236
 
237
        assign T2_64 = f3_A_64 + f2_ABC_64;
238
 
239
        assign next_Wt = {Wt_64_swap[31:0],Wt_64_swap[63:32]};
240
        assign next_E = D[63:0] + T1_64;
241
        assign next_A = T1_64 + T2_64;
242
 
243
 
244
        assign SHA384_result = {A,B,C,D,E,F};
245
        assign SHA512_result = {A,B,C,D,E,F,G,H};
246
 
247
        assign round_plus_1 = round + 1;
248
 
249
        //------------------------------------------------------------------    
250
        // SHA round
251
        //------------------------------------------------------------------
252
        always @(posedge clk_i)
253
        begin
254
                if (rst_i)
255
                begin
256
                        round <= 'd0;
257
                        busy <= 'b0;
258
 
259
                        W0  <= 'b0;
260
                        W1  <= 'b0;
261
                        W2  <= 'b0;
262
                        W3  <= 'b0;
263
                        W4  <= 'b0;
264
                        W5  <= 'b0;
265
                        W6  <= 'b0;
266
                        W7  <= 'b0;
267
                        W8  <= 'b0;
268
                        W9  <= 'b0;
269
                        W10 <= 'b0;
270
                        W11 <= 'b0;
271
                        W12 <= 'b0;
272
                        W13 <= 'b0;
273
                        W14 <= 'b0;
274
                        Wt  <= 'b0;
275
 
276
                        A <= 'b0;
277
                        B <= 'b0;
278
                        C <= 'b0;
279
                        D <= 'b0;
280
                        E <= 'b0;
281
                        F <= 'b0;
282
                        G <= 'b0;
283
                        H <= 'b0;
284
 
285
                        H0 <= 'b0;
286
                        H1 <= 'b0;
287
                        H2 <= 'b0;
288
                        H3 <= 'b0;
289
                        H4 <= 'b0;
290
                        H5 <= 'b0;
291
                        H6 <= 'b0;
292
                        H7 <= 'b0;
293
                end
294
                else
295
                begin
296
                        case (round)
297
 
298
                        'd0:
299
                                begin
300
                                        if (cmd[1])
301
                                        begin
302
                                                W0[31:0] <= text_i;
303
                                                Wt[31:0] <= text_i;
304
                                                busy <= 'b1;
305
                                                round <= round_plus_1;
306
 
307
                                                case (cmd[3:2])
308
                                                        2'b00:  // sha-384 first message
309
                                                                begin
310
                                                                        A <= `SHA384_H0;
311
                                                                        B <= `SHA384_H1;
312
                                                                        C <= `SHA384_H2;
313
                                                                        D <= `SHA384_H3;
314
                                                                        E <= `SHA384_H4;
315
                                                                        F <= `SHA384_H5;
316
                                                                        G <= `SHA384_H6;
317
                                                                        H <= `SHA384_H7;
318
 
319
                                                                        H0 <= `SHA384_H0;
320
                                                                        H1 <= `SHA384_H1;
321
                                                                        H2 <= `SHA384_H2;
322
                                                                        H3 <= `SHA384_H3;
323
                                                                        H4 <= `SHA384_H4;
324
                                                                        H5 <= `SHA384_H5;
325
                                                                        H6 <= `SHA384_H6;
326
                                                                        H7 <= `SHA384_H7;
327
                                                                end
328
                                                        2'b01:  // sha-384 internal message
329
                                                                begin
330
                                                                        H0 <= A;
331
                                                                        H1 <= B;
332
                                                                        H2 <= C;
333
                                                                        H3 <= D;
334
                                                                        H4 <= E;
335
                                                                        H5 <= F;
336
                                                                        H6 <= G;
337
                                                                        H7 <= H;
338
                                                                end
339
                                                        2'b10:  // sha-512 first message
340
                                                                begin
341
                                                                        A <= `SHA512_H0;
342
                                                                        B <= `SHA512_H1;
343
                                                                        C <= `SHA512_H2;
344
                                                                        D <= `SHA512_H3;
345
                                                                        E <= `SHA512_H4;
346
                                                                        F <= `SHA512_H5;
347
                                                                        G <= `SHA512_H6;
348
                                                                        H <= `SHA512_H7;
349
 
350
                                                                        H0 <= `SHA512_H0;
351
                                                                        H1 <= `SHA512_H1;
352
                                                                        H2 <= `SHA512_H2;
353
                                                                        H3 <= `SHA512_H3;
354
                                                                        H4 <= `SHA512_H4;
355
                                                                        H5 <= `SHA512_H5;
356
                                                                        H6 <= `SHA512_H6;
357
                                                                        H7 <= `SHA512_H7;
358
                                                                end
359
                                                        2'b11:  // sha-512 internal message
360
                                                                begin
361
                                                                        H0 <= A;
362
                                                                        H1 <= B;
363
                                                                        H2 <= C;
364
                                                                        H3 <= D;
365
                                                                        H4 <= E;
366
                                                                        H5 <= F;
367
                                                                        H6 <= G;
368
                                                                        H7 <= H;
369
                                                                end
370
                                                endcase
371
                                        end
372
                                        else
373
                                        begin   // IDLE
374
                                                round <= 'd0;
375
                                        end
376
                                end
377
                        'd1:
378
                                begin
379
                                        W0[63:32] <= text_i;
380
                                        Wt[63:32] <= text_i;
381
                                        round <= round_plus_1;
382
                                end
383
                        'd2:
384
                                begin
385
                                        W1[31:0] <= text_i;
386
                                        Wt[31:0] <= text_i;
387
 
388
                                        H <= G;
389
                                        G <= F;
390
                                        F <= E;
391
                                        E <= next_E;
392
                                        D <= C;
393
                                        C <= B;
394
                                        B <= A;
395
                                        A <= next_A;
396
 
397
                                        round <= round_plus_1;
398
                                end
399
                        'd3:
400
                                begin
401
                                        W1[63:32] <= text_i;
402
                                        Wt[63:32] <= text_i;
403
                                        round <= round_plus_1;
404
                                end
405
                        'd4:
406
                                begin
407
                                        W2[31:0] <= text_i;
408
                                        Wt[31:0] <= text_i;
409
 
410
                                        H <= G;
411
                                        G <= F;
412
                                        F <= E;
413
                                        E <= next_E;
414
                                        D <= C;
415
                                        C <= B;
416
                                        B <= A;
417
                                        A <= next_A;
418
 
419
                                        round <= round_plus_1;
420
                                end
421
                        'd5:
422
                                begin
423
                                        W2[63:32] <= text_i;
424
                                        Wt[63:32] <= text_i;
425
                                        round <= round_plus_1;
426
                                end
427
                        'd6:
428
                                begin
429
                                        W3[31:0] <= text_i;
430
                                        Wt[31:0] <= text_i;
431
 
432
                                        H <= G;
433
                                        G <= F;
434
                                        F <= E;
435
                                        E <= next_E;
436
                                        D <= C;
437
                                        C <= B;
438
                                        B <= A;
439
                                        A <= next_A;
440
 
441
                                        round <= round_plus_1;
442
                                end
443
                        'd7:
444
                                begin
445
                                        W3[63:32] <= text_i;
446
                                        Wt[63:32] <= text_i;
447
                                        round <= round_plus_1;
448
                                end
449
                        'd8:
450
                                begin
451
                                        W4[31:0] <= text_i;
452
                                        Wt[31:0] <= text_i;
453
 
454
                                        H <= G;
455
                                        G <= F;
456
                                        F <= E;
457
                                        E <= next_E;
458
                                        D <= C;
459
                                        C <= B;
460
                                        B <= A;
461
                                        A <= next_A;
462
 
463
                                        round <= round_plus_1;
464
                                end
465
                        'd9:
466
                                begin
467
                                        W4[63:32] <= text_i;
468
                                        Wt[63:32] <= text_i;
469
                                        round <= round_plus_1;
470
                                end
471
                        'd10:
472
                                begin
473
                                        W5[31:0] <= text_i;
474
                                        Wt[31:0] <= text_i;
475
 
476
                                        H <= G;
477
                                        G <= F;
478
                                        F <= E;
479
                                        E <= next_E;
480
                                        D <= C;
481
                                        C <= B;
482
                                        B <= A;
483
                                        A <= next_A;
484
 
485
                                        round <= round_plus_1;
486
                                end
487
                        'd11:
488
                                begin
489
                                        W5[63:32] <= text_i;
490
                                        Wt[63:32] <= text_i;
491
                                        round <= round_plus_1;
492
                                end
493
                        'd12:
494
                                begin
495
                                        W6[31:0] <= text_i;
496
                                        Wt[31:0] <= text_i;
497
 
498
                                        H <= G;
499
                                        G <= F;
500
                                        F <= E;
501
                                        E <= next_E;
502
                                        D <= C;
503
                                        C <= B;
504
                                        B <= A;
505
                                        A <= next_A;
506
 
507
                                        round <= round_plus_1;
508
                                end
509
                        'd13:
510
                                begin
511
                                        W6[63:32] <= text_i;
512
                                        Wt[63:32] <= text_i;
513
                                        round <= round_plus_1;
514
                                end
515
                        'd14:
516
                                begin
517
                                        W7[31:0] <= text_i;
518
                                        Wt[31:0] <= text_i;
519
 
520
                                        H <= G;
521
                                        G <= F;
522
                                        F <= E;
523
                                        E <= next_E;
524
                                        D <= C;
525
                                        C <= B;
526
                                        B <= A;
527
                                        A <= next_A;
528
 
529
                                        round <= round_plus_1;
530
                                end
531
                        'd15:
532
                                begin
533
                                        W7[63:32] <= text_i;
534
                                        Wt[63:32] <= text_i;
535
                                        round <= round_plus_1;
536
                                end
537
                        'd16:
538
                                begin
539
                                        W8[31:0] <= text_i;
540
                                        Wt[31:0] <= text_i;
541
 
542
                                        H <= G;
543
                                        G <= F;
544
                                        F <= E;
545
                                        E <= next_E;
546
                                        D <= C;
547
                                        C <= B;
548
                                        B <= A;
549
                                        A <= next_A;
550
 
551
                                        round <= round_plus_1;
552
                                end
553
                        'd17:
554
                                begin
555
                                        W8[63:32] <= text_i;
556
                                        Wt[63:32] <= text_i;
557
                                        round <= round_plus_1;
558
                                end
559
                        'd18:
560
                                begin
561
                                        W9[31:0] <= text_i;
562
                                        Wt[31:0] <= text_i;
563
 
564
                                        H <= G;
565
                                        G <= F;
566
                                        F <= E;
567
                                        E <= next_E;
568
                                        D <= C;
569
                                        C <= B;
570
                                        B <= A;
571
                                        A <= next_A;
572
 
573
                                        round <= round_plus_1;
574
                                end
575
                        'd19:
576
                                begin
577
                                        W9[63:32] <= text_i;
578
                                        Wt[63:32] <= text_i;
579
                                        round <= round_plus_1;
580
                                end
581
                        'd20:
582
                                begin
583
                                        W10[31:0] <= text_i;
584
                                        Wt[31:0] <= text_i;
585
 
586
                                        H <= G;
587
                                        G <= F;
588
                                        F <= E;
589
                                        E <= next_E;
590
                                        D <= C;
591
                                        C <= B;
592
                                        B <= A;
593
                                        A <= next_A;
594
 
595
                                        round <= round_plus_1;
596
                                end
597
                        'd21:
598
                                begin
599
                                        W10[63:32] <= text_i;
600
                                        Wt[63:32] <= text_i;
601
                                        round <= round_plus_1;
602
                                end
603
                        'd22:
604
                                begin
605
                                        W11[31:0] <= text_i;
606
                                        Wt[31:0] <= text_i;
607
 
608
                                        H <= G;
609
                                        G <= F;
610
                                        F <= E;
611
                                        E <= next_E;
612
                                        D <= C;
613
                                        C <= B;
614
                                        B <= A;
615
                                        A <= next_A;
616
 
617
                                        round <= round_plus_1;
618
                                end
619
                        'd23:
620
                                begin
621
                                        W11[63:32] <= text_i;
622
                                        Wt[63:32] <= text_i;
623
                                        round <= round_plus_1;
624
                                end
625
                        'd24:
626
                                begin
627
                                        W12[31:0] <= text_i;
628
                                        Wt[31:0] <= text_i;
629
 
630
                                        H <= G;
631
                                        G <= F;
632
                                        F <= E;
633
                                        E <= next_E;
634
                                        D <= C;
635
                                        C <= B;
636
                                        B <= A;
637
                                        A <= next_A;
638
 
639
                                        round <= round_plus_1;
640
                                end
641
                        'd25:
642
                                begin
643
                                        W12[63:32] <= text_i;
644
                                        Wt[63:32] <= text_i;
645
                                        round <= round_plus_1;
646
                                end
647
                        'd26:
648
                                begin
649
                                        W13[31:0] <= text_i;
650
                                        Wt[31:0] <= text_i;
651
 
652
                                        H <= G;
653
                                        G <= F;
654
                                        F <= E;
655
                                        E <= next_E;
656
                                        D <= C;
657
                                        C <= B;
658
                                        B <= A;
659
                                        A <= next_A;
660
 
661
                                        round <= round_plus_1;
662
                                end
663
                        'd27:
664
                                begin
665
                                        W13[63:32] <= text_i;
666
                                        Wt[63:32] <= text_i;
667
                                        round <= round_plus_1;
668
                                end
669
                        'd28:
670
                                begin
671
                                        W14[31:0] <= text_i;
672
                                        Wt[31:0] <= text_i;
673
 
674
                                        H <= G;
675
                                        G <= F;
676
                                        F <= E;
677
                                        E <= next_E;
678
                                        D <= C;
679
                                        C <= B;
680
                                        B <= A;
681
                                        A <= next_A;
682
 
683
                                        round <= round_plus_1;
684
                                end
685
                        'd29:
686
                                begin
687
                                        W14[63:32] <= text_i;
688
                                        Wt[63:32] <= text_i;
689
                                        round <= round_plus_1;
690
                                end
691
                        'd30:
692
                                begin
693
                                        Wt[31:0] <= text_i;
694
 
695
                                        H <= G;
696
                                        G <= F;
697
                                        F <= E;
698
                                        E <= next_E;
699
                                        D <= C;
700
                                        C <= B;
701
                                        B <= A;
702
                                        A <= next_A;
703
 
704
                                        round <= round_plus_1;
705
                                end
706
                        'd31:
707
                                begin
708
                                        Wt[63:32] <= text_i;
709
                                        round <= round_plus_1;
710
                                end
711
                        'd32,
712
                        'd33,
713
                        'd34,
714
                        'd35,
715
                        'd36,
716
                        'd37,
717
                        'd38,
718
                        'd39,
719
                        'd40,
720
                        'd41,
721
                        'd42,
722
                        'd43,
723
                        'd44,
724
                        'd45,
725
                        'd46,
726
                        'd47,
727
                        'd48,
728
                        'd49,
729
                        'd50,
730
                        'd51,
731
                        'd52,
732
                        'd53,
733
                        'd54,
734
                        'd55,
735
                        'd56,
736
                        'd57,
737
                        'd58,
738
                        'd59,
739
                        'd60,
740
                        'd61,
741
                        'd62,
742
                        'd63,
743
                        'd64,
744
                        'd65,
745
                        'd66,
746
                        'd67,
747
                        'd68,
748
                        'd69,
749
                        'd70,
750
                        'd71,
751
                        'd72,
752
                        'd73,
753
                        'd74,
754
                        'd75,
755
                        'd76,
756
                        'd77,
757
                        'd78,
758
                        'd79,
759
                        'd80,
760
                        'd81,
761
                        'd82,
762
                        'd83,
763
                        'd84,
764
                        'd85,
765
                        'd86,
766
                        'd87,
767
                        'd88,
768
                        'd89,
769
                        'd90,
770
                        'd91,
771
                        'd92,
772
                        'd93,
773
                        'd94,
774
                        'd95:
775
                                begin
776
                                        W0  <= W1;
777
                                        W1  <= W2;
778
                                        W2  <= W3;
779
                                        W3  <= W4;
780
                                        W4  <= W5;
781
                                        W5  <= W6;
782
                                        W6  <= W7;
783
                                        W7  <= W8;
784
                                        W8  <= W9;
785
                                        W9  <= W10;
786
                                        W10 <= W11;
787
                                        W11 <= W12;
788
                                        W12 <= W13;
789
                                        W13 <= W14;
790
                                        W14 <= Wt;
791
                                        Wt  <= next_Wt;
792
 
793
                                        H <= G;
794
                                        G <= F;
795
                                        F <= E;
796
                                        E <= next_E;
797
                                        D <= C;
798
                                        C <= B;
799
                                        B <= A;
800
                                        A <= next_A;
801
 
802
                                        round <= round_plus_1;
803
                                end
804
                        'd96:
805
                                begin
806
                                        A <= next_A + H0;
807
                                        B <= A + H1;
808
                                        C <= B + H2;
809
                                        D <= C + H3;
810
                                        E <= next_E + H4;
811
                                        F <= E + H5;
812
                                        G <= F + H6;
813
                                        H <= G + H7;
814
                                        round <= 'd0;
815
                                        busy <= 'b0;
816
                                end
817
                        default:
818
                                begin
819
                                        round <= 'd0;
820
                                        busy <= 'b0;
821
                                end
822
                        endcase
823
                end
824
        end
825
 
826
 
827
        //------------------------------------------------------------------    
828
        // Kt generator
829
        //------------------------------------------------------------------    
830
        always @ (posedge clk_i)
831
        begin
832
                if (rst_i)
833
                begin
834
                        Kt <= 'b0;
835
                end
836
                else
837
                begin
838
                        case (round)
839
                                'd00:   Kt <= `K00;
840
                                'd01:   Kt <= `K00;
841
                                'd02:   Kt <= `K01;
842
                                'd03:   Kt <= `K01;
843
                                'd04:   Kt <= `K02;
844
                                'd05:   Kt <= `K02;
845
                                'd06:   Kt <= `K03;
846
                                'd07:   Kt <= `K03;
847
                                'd08:   Kt <= `K04;
848
                                'd09:   Kt <= `K04;
849
                                'd10:   Kt <= `K05;
850
                                'd11:   Kt <= `K05;
851
                                'd12:   Kt <= `K06;
852
                                'd13:   Kt <= `K06;
853
                                'd14:   Kt <= `K07;
854
                                'd15:   Kt <= `K07;
855
                                'd16:   Kt <= `K08;
856
                                'd17:   Kt <= `K08;
857
                                'd18:   Kt <= `K09;
858
                                'd19:   Kt <= `K09;
859
                                'd20:   Kt <= `K10;
860
                                'd21:   Kt <= `K10;
861
                                'd22:   Kt <= `K11;
862
                                'd23:   Kt <= `K11;
863
                                'd24:   Kt <= `K12;
864
                                'd25:   Kt <= `K12;
865
                                'd26:   Kt <= `K13;
866
                                'd27:   Kt <= `K13;
867
                                'd28:   Kt <= `K14;
868
                                'd29:   Kt <= `K14;
869
                                'd30:   Kt <= `K15;
870
                                'd31:   Kt <= `K15;
871
                                'd32:   Kt <= `K16;
872
                                'd33:   Kt <= `K17;
873
                                'd34:   Kt <= `K18;
874
                                'd35:   Kt <= `K19;
875
                                'd36:   Kt <= `K20;
876
                                'd37:   Kt <= `K21;
877
                                'd38:   Kt <= `K22;
878
                                'd39:   Kt <= `K23;
879
                                'd40:   Kt <= `K24;
880
                                'd41:   Kt <= `K25;
881
                                'd42:   Kt <= `K26;
882
                                'd43:   Kt <= `K27;
883
                                'd44:   Kt <= `K28;
884
                                'd45:   Kt <= `K29;
885
                                'd46:   Kt <= `K30;
886
                                'd47:   Kt <= `K31;
887
                                'd48:   Kt <= `K32;
888
                                'd49:   Kt <= `K33;
889
                                'd50:   Kt <= `K34;
890
                                'd51:   Kt <= `K35;
891
                                'd52:   Kt <= `K36;
892
                                'd53:   Kt <= `K37;
893
                                'd54:   Kt <= `K38;
894
                                'd55:   Kt <= `K39;
895
                                'd56:   Kt <= `K40;
896
                                'd57:   Kt <= `K41;
897
                                'd58:   Kt <= `K42;
898
                                'd59:   Kt <= `K43;
899
                                'd60:   Kt <= `K44;
900
                                'd61:   Kt <= `K45;
901
                                'd62:   Kt <= `K46;
902
                                'd63:   Kt <= `K47;
903
                                'd64:   Kt <= `K48;
904
                                'd65:   Kt <= `K49;
905
                                'd66:   Kt <= `K50;
906
                                'd67:   Kt <= `K51;
907
                                'd68:   Kt <= `K52;
908
                                'd69:   Kt <= `K53;
909
                                'd70:   Kt <= `K54;
910
                                'd71:   Kt <= `K55;
911
                                'd72:   Kt <= `K56;
912
                                'd73:   Kt <= `K57;
913
                                'd74:   Kt <= `K58;
914
                                'd75:   Kt <= `K59;
915
                                'd76:   Kt <= `K60;
916
                                'd77:   Kt <= `K61;
917
                                'd78:   Kt <= `K62;
918
                                'd79:   Kt <= `K63;
919
                                'd80:   Kt <= `K64;
920
                                'd81:   Kt <= `K65;
921
                                'd82:   Kt <= `K66;
922
                                'd83:   Kt <= `K67;
923
                                'd84:   Kt <= `K68;
924
                                'd85:   Kt <= `K69;
925
                                'd86:   Kt <= `K70;
926
                                'd87:   Kt <= `K71;
927
                                'd88:   Kt <= `K72;
928
                                'd89:   Kt <= `K73;
929
                                'd90:   Kt <= `K74;
930
                                'd91:   Kt <= `K75;
931
                                'd92:   Kt <= `K76;
932
                                'd93:   Kt <= `K77;
933
                                'd94:   Kt <= `K78;
934
                                'd95:   Kt <= `K79;
935
                                default:Kt <= 'd0;
936
                        endcase
937
                end
938
        end
939
 
940
        //------------------------------------------------------------------    
941
        // read result 
942
        //------------------------------------------------------------------    
943
        always @ (posedge clk_i)
944
        begin
945
                if (rst_i)
946
                begin
947
                        text_o <= 'b0;
948
                        read_counter <= 'b0;
949
                end
950
                else
951
                begin
952
                        if (cmd[0])
953
                        begin
954
                                case (cmd[3])
955
                                        1'b0:   read_counter <= 'd11;   // sha-384      384/32=12
956
                                        1'b1:   read_counter <= 'd15;   // sha-512      512/32=16
957
                                endcase
958
                        end
959
                        else
960
                        begin
961
                        if (~busy)
962
                        begin
963
                                case (cmd[3])
964
                                        1'b0:
965
                                                begin
966
                                                        case (read_counter)
967
                                                                'd11:   text_o <= SHA384_result[12*32-1:11*32];
968
                                                                'd10:   text_o <= SHA384_result[11*32-1:10*32];
969
                                                                'd09:   text_o <= SHA384_result[10*32-1:09*32];
970
                                                                'd08:   text_o <= SHA384_result[09*32-1:08*32];
971
                                                                'd07:   text_o <= SHA384_result[08*32-1:07*32];
972
                                                                'd06:   text_o <= SHA384_result[07*32-1:06*32];
973
                                                                'd05:   text_o <= SHA384_result[06*32-1:05*32];
974
                                                                'd04:   text_o <= SHA384_result[05*32-1:04*32];
975
                                                                'd03:   text_o <= SHA384_result[04*32-1:03*32];
976
                                                                'd02:   text_o <= SHA384_result[03*32-1:02*32];
977
                                                                'd01:   text_o <= SHA384_result[02*32-1:01*32];
978
                                                                'd00:   text_o <= SHA384_result[01*32-1:00*32];
979
                                                                default:text_o <= 'b0;
980
                                                        endcase
981
                                                end
982
                                        1'b1:
983
                                                begin
984
                                                        case (read_counter)
985
                                                                'd15:   text_o <= SHA512_result[16*32-1:15*32];
986
                                                                'd14:   text_o <= SHA512_result[15*32-1:14*32];
987
                                                                'd13:   text_o <= SHA512_result[14*32-1:13*32];
988
                                                                'd12:   text_o <= SHA512_result[13*32-1:12*32];
989
                                                                'd11:   text_o <= SHA512_result[12*32-1:11*32];
990
                                                                'd10:   text_o <= SHA512_result[11*32-1:10*32];
991
                                                                'd09:   text_o <= SHA512_result[10*32-1:09*32];
992
                                                                'd08:   text_o <= SHA512_result[09*32-1:08*32];
993
                                                                'd07:   text_o <= SHA512_result[08*32-1:07*32];
994
                                                                'd06:   text_o <= SHA512_result[07*32-1:06*32];
995
                                                                'd05:   text_o <= SHA512_result[06*32-1:05*32];
996
                                                                'd04:   text_o <= SHA512_result[05*32-1:04*32];
997
                                                                'd03:   text_o <= SHA512_result[04*32-1:03*32];
998
                                                                'd02:   text_o <= SHA512_result[03*32-1:02*32];
999
                                                                'd01:   text_o <= SHA512_result[02*32-1:01*32];
1000
                                                                'd00:   text_o <= SHA512_result[01*32-1:00*32];
1001
                                                                default:text_o <= 'b0;
1002
                                                        endcase
1003
                                                end
1004
                                endcase
1005
                                if (|read_counter)
1006
                                        read_counter <= read_counter - 'd1;
1007
                        end
1008
                        else
1009
                        begin
1010
                                text_o <= 'b0;
1011
                        end
1012
                        end
1013
                end
1014
        end
1015
 
1016
endmodule
1017
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.