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

Subversion Repositories sha_core

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marsgod
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  SHA-160                                                    ////
4
////  Secure Hash Algorithm (SHA-160)                            ////
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) 2002-2004 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 SHA1_H0 32'h67452301
40
`define SHA1_H1 32'hefcdab89
41
`define SHA1_H2 32'h98badcfe
42
`define SHA1_H3 32'h10325476
43
`define SHA1_H4 32'hc3d2e1f0
44
 
45
`define SHA1_K0 32'h5a827999
46
`define SHA1_K1 32'h6ed9eba1
47
`define SHA1_K2 32'h8f1bbcdc
48
`define SHA1_K3 32'hca62c1d6
49
 
50
module sha1 (clk_i, rst_i, text_i, text_o, cmd_i, cmd_w_i, cmd_o);
51
 
52
        input           clk_i;  // global clock input
53
        input           rst_i;  // global reset input , active high
54
 
55
        input   [31:0]   text_i; // text input 32bit
56
        output  [31:0]   text_o; // text output 32bit
57
 
58
        input   [2:0]    cmd_i;  // command input
59
        input           cmd_w_i;// command input write enable
60
        output  [3:0]    cmd_o;  // command output(status)
61
 
62
        /*
63
                cmd
64
                Busy Round W R
65
 
66
                bit3 bit2  bit1 bit0
67
                Busy Round W    R
68
 
69
                Busy:
70
 
71
                1       busy
72
 
73
                Round:
74
 
75
                1       internal round
76
 
77
                W:
78
 
79
                1       write data
80
 
81
                R:
82
 
83
                1       read data
84
 
85
        */
86
 
87
 
88
        reg     [3:0]    cmd;
89
        wire    [3:0]    cmd_o;
90
 
91
        reg     [31:0]   text_o;
92
 
93
        reg     [6:0]    round;
94
        wire    [6:0]    round_plus_1;
95
 
96
        reg     [2:0]    read_counter;
97
 
98
        reg     [31:0]   H0,H1,H2,H3,H4;
99
        reg     [31:0]   W0,W1,W2,W3,W4,W5,W6,W7,W8,W9,W10,W11,W12,W13,W14;
100
        reg     [31:0]   Wt,Kt;
101
        reg     [31:0]   A,B,C,D,E;
102
 
103
        reg             busy;
104
 
105
        assign cmd_o = cmd;
106
        always @ (posedge clk_i)
107
        begin
108
                if (rst_i)
109
                        cmd <= 'b0;
110
                else
111
                if (cmd_w_i)
112
                        cmd[2:0] <= cmd_i[2:0];           // busy bit can't write
113
                else
114
                begin
115
                        cmd[3] <= busy;                 // update busy bit
116
                        if (~busy)
117
                                cmd[1:0] <= 2'b00;       // hardware auto clean R/W bits
118
                end
119
        end
120
 
121
        // Hash functions
122
        wire [31:0] SHA1_f1_BCD,SHA1_f2_BCD,SHA1_f3_BCD,SHA1_Wt_1;
123
        wire [31:0] SHA1_ft_BCD;
124
        wire [31:0] next_Wt,next_A,next_C;
125
        wire [159:0] SHA1_result;
126
 
127
        assign SHA1_f1_BCD = (B & C) ^ (~B & D);
128
        assign SHA1_f2_BCD = B ^ C ^ D;
129
        assign SHA1_f3_BCD = (B & C) ^ (C & D) ^ (B & D);
130
 
131
        assign SHA1_ft_BCD = (round < 'd21) ? SHA1_f1_BCD : (round < 'd41) ? SHA1_f2_BCD : (round < 'd61) ? SHA1_f3_BCD : SHA1_f2_BCD;
132
 
133
        assign SHA1_Wt_1 = {W13 ^ W8 ^ W2 ^ W0};
134
 
135
        assign next_Wt = {SHA1_Wt_1[30:0],SHA1_Wt_1[31]};        // NSA fix added
136
        assign next_A = {A[26:0],A[31:27]} + SHA1_ft_BCD + E + Kt + Wt;
137
        assign next_C = {B[1:0],B[31:2]};
138
 
139
        assign SHA1_result   = {A,B,C,D,E};
140
 
141
        assign round_plus_1 = round + 1;
142
 
143
        //------------------------------------------------------------------    
144
        // SHA round
145
        //------------------------------------------------------------------
146
        always @(posedge clk_i)
147
        begin
148
                if (rst_i)
149
                begin
150
                        round <= 'd0;
151
                        busy <= 'b0;
152
 
153
                        W0  <= 'b0;
154
                        W1  <= 'b0;
155
                        W2  <= 'b0;
156
                        W3  <= 'b0;
157
                        W4  <= 'b0;
158
                        W5  <= 'b0;
159
                        W6  <= 'b0;
160
                        W7  <= 'b0;
161
                        W8  <= 'b0;
162
                        W9  <= 'b0;
163
                        W10 <= 'b0;
164
                        W11 <= 'b0;
165
                        W12 <= 'b0;
166
                        W13 <= 'b0;
167
                        W14 <= 'b0;
168
                        Wt  <= 'b0;
169
 
170
                        A <= 'b0;
171
                        B <= 'b0;
172
                        C <= 'b0;
173
                        D <= 'b0;
174
                        E <= 'b0;
175
 
176
                        H0 <= 'b0;
177
                        H1 <= 'b0;
178
                        H2 <= 'b0;
179
                        H3 <= 'b0;
180
                        H4 <= 'b0;
181
 
182
                end
183
                else
184
                begin
185
                        case (round)
186
 
187
                        'd0:
188
                                begin
189
                                        if (cmd[1])
190
                                        begin
191
                                                W0 <= text_i;
192
                                                Wt <= text_i;
193
                                                busy <= 'b1;
194
                                                round <= round_plus_1;
195
 
196
                                                case (cmd[2])
197
                                                        1'b0:   // sha-1 first message
198
                                                                begin
199
                                                                        A <= `SHA1_H0;
200
                                                                        B <= `SHA1_H1;
201
                                                                        C <= `SHA1_H2;
202
                                                                        D <= `SHA1_H3;
203
                                                                        E <= `SHA1_H4;
204
 
205
                                                                        H0 <= `SHA1_H0;
206
                                                                        H1 <= `SHA1_H1;
207
                                                                        H2 <= `SHA1_H2;
208
                                                                        H3 <= `SHA1_H3;
209
                                                                        H4 <= `SHA1_H4;
210
                                                                end
211
                                                        1'b1:   // sha-1 internal message
212
                                                                begin
213
                                                                        H0 <= A;
214
                                                                        H1 <= B;
215
                                                                        H2 <= C;
216
                                                                        H3 <= D;
217
                                                                        H4 <= E;
218
                                                                end
219
                                                endcase
220
                                        end
221
                                        else
222
                                        begin   // IDLE
223
                                                round <= 'd0;
224
                                        end
225
                                end
226
                        'd1:
227
                                begin
228
                                        W1 <= text_i;
229
                                        Wt <= text_i;
230
 
231
                                        E <= D;
232
                                        D <= C;
233
                                        C <= next_C;
234
                                        B <= A;
235
                                        A <= next_A;
236
 
237
                                        round <= round_plus_1;
238
                                end
239
                        'd2:
240
                                begin
241
                                        W2 <= text_i;
242
                                        Wt <= text_i;
243
 
244
                                        E <= D;
245
                                        D <= C;
246
                                        C <= next_C;
247
                                        B <= A;
248
                                        A <= next_A;
249
 
250
                                        round <= round_plus_1;
251
                                end
252
                        'd3:
253
                                begin
254
                                        W3 <= text_i;
255
                                        Wt <= text_i;
256
 
257
                                        E <= D;
258
                                        D <= C;
259
                                        C <= next_C;
260
                                        B <= A;
261
                                        A <= next_A;
262
 
263
                                        round <= round_plus_1;
264
                                end
265
                        'd4:
266
                                begin
267
                                        W4 <= text_i;
268
                                        Wt <= text_i;
269
 
270
                                        E <= D;
271
                                        D <= C;
272
                                        C <= next_C;
273
                                        B <= A;
274
                                        A <= next_A;
275
 
276
                                        round <= round_plus_1;
277
                                end
278
                        'd5:
279
                                begin
280
                                        W5 <= text_i;
281
                                        Wt <= text_i;
282
 
283
                                        E <= D;
284
                                        D <= C;
285
                                        C <= next_C;
286
                                        B <= A;
287
                                        A <= next_A;
288
 
289
                                        round <= round_plus_1;
290
                                end
291
                        'd6:
292
                                begin
293
                                        W6 <= text_i;
294
                                        Wt <= text_i;
295
 
296
                                        E <= D;
297
                                        D <= C;
298
                                        C <= next_C;
299
                                        B <= A;
300
                                        A <= next_A;
301
 
302
                                        round <= round_plus_1;
303
                                end
304
                        'd7:
305
                                begin
306
                                        W7 <= text_i;
307
                                        Wt <= text_i;
308
 
309
                                        E <= D;
310
                                        D <= C;
311
                                        C <= next_C;
312
                                        B <= A;
313
                                        A <= next_A;
314
 
315
                                        round <= round_plus_1;
316
                                end
317
                        'd8:
318
                                begin
319
                                        W8 <= text_i;
320
                                        Wt <= text_i;
321
 
322
                                        E <= D;
323
                                        D <= C;
324
                                        C <= next_C;
325
                                        B <= A;
326
                                        A <= next_A;
327
 
328
                                        round <= round_plus_1;
329
                                end
330
                        'd9:
331
                                begin
332
                                        W9 <= text_i;
333
                                        Wt <= text_i;
334
 
335
                                        E <= D;
336
                                        D <= C;
337
                                        C <= next_C;
338
                                        B <= A;
339
                                        A <= next_A;
340
 
341
                                        round <= round_plus_1;
342
                                end
343
                        'd10:
344
                                begin
345
                                        W10 <= text_i;
346
                                        Wt <= text_i;
347
 
348
                                        E <= D;
349
                                        D <= C;
350
                                        C <= next_C;
351
                                        B <= A;
352
                                        A <= next_A;
353
 
354
                                        round <= round_plus_1;
355
                                end
356
                        'd11:
357
                                begin
358
                                        W11 <= text_i;
359
                                        Wt <= text_i;
360
 
361
                                        E <= D;
362
                                        D <= C;
363
                                        C <= next_C;
364
                                        B <= A;
365
                                        A <= next_A;
366
 
367
                                        round <= round_plus_1;
368
                                end
369
                        'd12:
370
                                begin
371
                                        W12 <= text_i;
372
                                        Wt <= text_i;
373
 
374
                                        E <= D;
375
                                        D <= C;
376
                                        C <= next_C;
377
                                        B <= A;
378
                                        A <= next_A;
379
 
380
                                        round <= round_plus_1;
381
                                end
382
                        'd13:
383
                                begin
384
                                        W13 <= text_i;
385
                                        Wt <= text_i;
386
 
387
                                        E <= D;
388
                                        D <= C;
389
                                        C <= next_C;
390
                                        B <= A;
391
                                        A <= next_A;
392
 
393
                                        round <= round_plus_1;
394
                                end
395
                        'd14:
396
                                begin
397
                                        W14 <= text_i;
398
                                        Wt <= text_i;
399
 
400
                                        E <= D;
401
                                        D <= C;
402
                                        C <= next_C;
403
                                        B <= A;
404
                                        A <= next_A;
405
 
406
                                        round <= round_plus_1;
407
                                end
408
                        'd15:
409
                                begin
410
                                        Wt <= text_i;
411
 
412
                                        E <= D;
413
                                        D <= C;
414
                                        C <= next_C;
415
                                        B <= A;
416
                                        A <= next_A;
417
 
418
                                        round <= round_plus_1;
419
                                end
420
                        'd16,
421
                        'd17,
422
                        'd18,
423
                        'd19,
424
                        'd20,
425
                        'd21,
426
                        'd22,
427
                        'd23,
428
                        'd24,
429
                        'd25,
430
                        'd26,
431
                        'd27,
432
                        'd28,
433
                        'd29,
434
                        'd30,
435
                        'd31,
436
                        'd32,
437
                        'd33,
438
                        'd34,
439
                        'd35,
440
                        'd36,
441
                        'd37,
442
                        'd38,
443
                        'd39,
444
                        'd40,
445
                        'd41,
446
                        'd42,
447
                        'd43,
448
                        'd44,
449
                        'd45,
450
                        'd46,
451
                        'd47,
452
                        'd48,
453
                        'd49,
454
                        'd50,
455
                        'd51,
456
                        'd52,
457
                        'd53,
458
                        'd54,
459
                        'd55,
460
                        'd56,
461
                        'd57,
462
                        'd58,
463
                        'd59,
464
                        'd60,
465
                        'd61,
466
                        'd62,
467
                        'd63,
468
                        'd64,
469
                        'd65,
470
                        'd66,
471
                        'd67,
472
                        'd68,
473
                        'd69,
474
                        'd70,
475
                        'd71,
476
                        'd72,
477
                        'd73,
478
                        'd74,
479
                        'd75,
480
                        'd76,
481
                        'd77,
482
                        'd78,
483
                        'd79:
484
                                begin
485
                                        W0  <= W1;
486
                                        W1  <= W2;
487
                                        W2  <= W3;
488
                                        W3  <= W4;
489
                                        W4  <= W5;
490
                                        W5  <= W6;
491
                                        W6  <= W7;
492
                                        W7  <= W8;
493
                                        W8  <= W9;
494
                                        W9  <= W10;
495
                                        W10 <= W11;
496
                                        W11 <= W12;
497
                                        W12 <= W13;
498
                                        W13 <= W14;
499
                                        W14 <= Wt;
500
                                        Wt  <= next_Wt;
501
 
502
                                        E <= D;
503
                                        D <= C;
504
                                        C <= next_C;
505
                                        B <= A;
506
                                        A <= next_A;
507
 
508
                                        round <= round_plus_1;
509
                                end
510
                        'd80:
511
                                begin
512
                                        A <= next_A + H0;
513
                                        B <= A + H1;
514
                                        C <= next_C + H2;
515
                                        D <= C + H3;
516
                                        E <= D + H4;
517
                                        round <= 'd0;
518
                                        busy <= 'b0;
519
                                end
520
                        default:
521
                                begin
522
                                        round <= 'd0;
523
                                        busy <= 'b0;
524
                                end
525
                        endcase
526
                end
527
        end
528
 
529
 
530
        //------------------------------------------------------------------    
531
        // Kt generator
532
        //------------------------------------------------------------------    
533
        always @ (posedge clk_i)
534
        begin
535
                if (rst_i)
536
                begin
537
                        Kt <= 'b0;
538
                end
539
                else
540
                begin
541
                        if (round < 'd20)
542
                                Kt <= `SHA1_K0;
543
                        else
544
                        if (round < 'd40)
545
                                Kt <= `SHA1_K1;
546
                        else
547
                        if (round < 'd60)
548
                                Kt <= `SHA1_K2;
549
                        else
550
                                Kt <= `SHA1_K3;
551
                end
552
        end
553
 
554
        //------------------------------------------------------------------    
555
        // read result 
556
        //------------------------------------------------------------------    
557
        always @ (posedge clk_i)
558
        begin
559
                if (rst_i)
560
                begin
561
                        text_o <= 'b0;
562
                        read_counter <= 'b0;
563
                end
564
                else
565
                begin
566
                        if (cmd[0])
567
                        begin
568
                                read_counter <= 'd4;    // sha-1        160/32=5
569
                        end
570
                        else
571
                        begin
572
                        if (~busy)
573
                        begin
574
                                case (read_counter)
575
                                        'd4:    text_o <= SHA1_result[5*32-1:4*32];
576
                                        'd3:    text_o <= SHA1_result[4*32-1:3*32];
577
                                        'd2:    text_o <= SHA1_result[3*32-1:2*32];
578
                                        'd1:    text_o <= SHA1_result[2*32-1:1*32];
579
                                        'd0:    text_o <= SHA1_result[1*32-1:0*32];
580
                                        default:text_o <= 'b0;
581
                                endcase
582
                                if (|read_counter)
583
                                        read_counter <= read_counter - 'd1;
584
                        end
585
                        else
586
                        begin
587
                                text_o <= 'b0;
588
                        end
589
                        end
590
                end
591
        end
592
 
593
endmodule
594
 

powered by: WebSVN 2.1.0

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