OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [hdl/] [sha1/] [sha1_exec.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
///////////////////////////////////////////////////////////////
2
// sha1_exec.v  version 0.1
3
//
4
// Core Data Path Model for SHA1 Hash Function
5
//  includes state machine model 
6
// 
7
// Described in Stalling, page 284
8
// (The variable names come from that section)
9
// 
10
// After start is raised, the output is available 81 cycles later
11
// The inputs are NOT latched but the outputs are.
12
// It is assumed that this module will be used with a wrapper
13
// probably providing HMAC.
14
//
15
// Paul Hartke, phartke@stanford.edu,  Copyright (c)2002
16
//
17
// The information and description contained herein is the
18
// property of Paul Hartke.
19
//
20
// Permission is granted for any reuse of this information
21
// and description as long as this copyright notice is
22
// preserved.  Modifications may be made as long as this
23
// notice is preserved.
24
// This code is made available "as is".  There is no warranty,
25
// so use it at your own risk.
26
// Documentation? "Use the source, Luke!"
27
///////////////////////////////////////////////////////////////
28
 
29
module sha1_exec(clk, reset, start, data_in, load_in, cv,
30
  use_prev_cv, busy, out_valid, cv_next);
31
 
32
   input clk, reset;    // Global clock and synchronous reset
33
 
34
   input start;
35
   input [31:0] data_in;
36
   input load_in;
37
   input [159:0] cv;
38
   input use_prev_cv;
39
   output        busy;
40
   output        out_valid;
41
   output [159:0] cv_next;
42
 
43
   reg            busy;        // Output control signals set
44
   reg            out_valid;   //   in FSM
45
   reg [6:0]      rnd_cnt_d;   // Counts the sha1 rounds.
46
   wire [6:0]     rnd_cnt_q;   //   -- 80 of them (perform 1 rnd/clk)
47
   wire [159:0]   rnd_d;       // round register for main pipeline
48
   wire [159:0]   rnd_q;
49
   wire [159:0]   sha1_round_wire;  // outputs of each round
50
 
51
   wire [159:0]   cv_d;        // Save msg input for addition
52
   wire [159:0]   cv_q;        //   in last cycle
53
   wire [159:0]   cv_next_d;   // Register the output.
54
   wire [159:0]   cv_next_q;
55
 
56
   // FSM states.. Not one hot since it is a small state machine...
57
   parameter      IDLE = 2'b00;
58
   parameter      CALC = 2'b01;
59
   parameter      VALID_OUT = 2'b10;
60
   parameter      VALID_OUT2 = 2'b11;
61
   wire [1:0]     state;
62
   reg [1:0]      next_state;
63
 
64
   // The W round inputs are derived from the incoming message
65
   wire [511:0]   w_d;
66
   wire [511:0]   w_q;
67
   wire [511:0]   w_temp;
68
   wire [31:0]    w;
69
 
70
   // Generate the W words for each round
71
   wire [31:0]    w_gen_temp = w_temp[511:480] ^ w_temp[447:416] ^
72
                  w_temp[255:224] ^ w_temp[95:64];
73
   wire [31:0]    w_gen = {w_gen_temp[30:0], w_gen_temp[31]};
74
 
75
   // Select the incoming msg values or the computed ones
76
   assign         w_d = ((load_in == 1'b1) ? w_temp :
77
                  ((rnd_cnt_q < 7'd15) ? w_temp :
78
                  {w_gen, w_temp[479:0]}));
79
 
80
   // Msg shift register.
81
   // Left Circular shift by 1 word 
82
   assign w_temp = (state == CALC) ? {w_q[479:0], w_q[511:480]} :
83
     ((load_in == 1'b1) ? {w_q[479:0], data_in} : w_q);
84
 
85
   // W inputs come straight from a register output
86
   assign w = w_q[511:480];
87
 
88
   sha1_round sha1_round(.cv_in(rnd_q), .w(w), .round(rnd_cnt_q),
89
                           .cv_out(sha1_round_wire));
90
 
91
   // Accept the message input
92
   assign         rnd_d = ((state == CALC) ? sha1_round_wire :
93
     (use_prev_cv == 1'b1) ? cv_next_q : cv);
94
   assign         cv_d = ((state == CALC) ? cv_q :
95
     (use_prev_cv == 1'b1) ? cv_next_q : cv);
96
 
97
   // Calculate the final round...
98
   assign         cv_next_d = (state == VALID_OUT) ?
99
                                ({cv_q[159:128] + rnd_q[159:128],
100
                                  cv_q[127:96] + rnd_q[127:96],
101
                                  cv_q[95:64] + rnd_q[95:64],
102
                                  cv_q[63:32] + rnd_q[63:32],
103
                                  cv_q[31:0] + rnd_q[31:0]}) :
104
                                  (cv_next_q);
105
 
106
   // Output the hash
107
   assign         cv_next = cv_next_q;
108
 
109
   // FSM start at IDLE from reset...
110
   // Only thing to note is that start going high restarts the state machine.
111
   always @(state or start or rnd_cnt_q)
112
     begin
113
        out_valid = 1'b0;
114
        busy = 1'b0;
115
        rnd_cnt_d = 7'b0000000;
116
        case (state)
117
          IDLE : begin
118
             out_valid = 1'b0;
119
             rnd_cnt_d = 7'b0000000;
120
             if (start) begin
121
                busy = 1'b1;
122
                next_state = CALC;
123
             end
124
             else begin
125
                busy = 1'b1;
126
                next_state = IDLE;
127
             end
128
          end // case: IDLE
129
 
130
          CALC : begin
131
             busy = 1'b1;
132
             out_valid = 1'b0;
133
             if (start) begin
134
                next_state = IDLE;
135
                rnd_cnt_d = 7'd0000000;
136
             end
137
             else if (rnd_cnt_q == 7'd79) begin
138
                next_state = VALID_OUT;
139
                rnd_cnt_d = 7'd0000000;
140
             end
141
             else begin
142
                next_state = CALC;
143
                rnd_cnt_d = rnd_cnt_q + 7'd0000001;
144
             end
145
          end // case: CALC
146
 
147
          VALID_OUT : begin
148
             busy = 1'b1;
149
             out_valid = 1'b0;
150
             // Allow cycle to latch output
151
             if (start) begin
152
                next_state = IDLE;
153
                rnd_cnt_d = 7'd0000000;
154
             end else begin
155
                next_state = VALID_OUT2;
156
             end
157
          end
158
 
159
          VALID_OUT2 : begin
160
             busy = 1'b0;
161
             out_valid = 1'b1;
162
             if (start) begin
163
                next_state = CALC;
164
             end
165
             else begin
166
                next_state = IDLE;
167
             end
168
          end
169
 
170
          default : begin
171
             next_state = IDLE;
172
          end
173
 
174
        endcase
175
     end
176
 
177
   // State Elements...
178
   dffhr #(7) rnd_cnt_reg (.d(rnd_cnt_d), .q(rnd_cnt_q),
179
                           .clk(clk), .r(reset));
180
   dffhr #(2) state_reg (.d(next_state), .q(state),
181
                         .clk(clk), .r(reset));
182
   dffhr #(512) w_reg (.d(w_d), .q(w_q), .clk(clk), .r(reset));
183
   dffhr #(160) cv_reg (.d(cv_d), .q(cv_q), .clk(clk), .r(reset));
184
   dffhr #(160) rnd_reg (.d(rnd_d), .q(rnd_q), .clk(clk), .r(reset));
185
   dffhr #(160) cv_next_reg (.d(cv_next_d), .q(cv_next_q),
186
                             .clk(clk), .r(reset));
187
 
188
endmodule // sha1_exec
189
 
190
 

powered by: WebSVN 2.1.0

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