aboutsummaryrefslogtreecommitdiff
path: root/src/rtl/sha256.v
blob: d28a9147c6c8e248c9e0df4b63cc46f2714ad67c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//======================================================================
//
// sha256.v
// --------
// Top level wrapper for the SHA-256 hash function providing
// a simple write()/read() interface with 8 bit data access.
//
//
// Authors: Joachim Strombergson, Paul Selkirk
// Copyright (c) 2014, SUNET
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or 
// without modification, are permitted provided that the following 
// conditions are met: 
// 
// 1. Redistributions of source code must retain the above copyright 
//    notice, this list of conditions and the following disclaimer. 
// 
// 2. Redistributions in binary form must reproduce the above copyright 
//    notice, this list of conditions and the following disclaimer in 
//    the documentation and/or other materials provided with the 
//    distribution. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//======================================================================

module sha256(
              // Clock and reset.
              input wire          clk,
              input wire          reset_n,

              // Interface to communication core
              input wire          rx_syn,
              input wire [7 : 0]  rx_data,
              output wire         rx_ack,

              output wire         tx_syn,
              output wire [7 : 0] tx_data,
              input wire          tx_ack
              );

   //----------------------------------------------------------------
   // Internal constant and parameter definitions.
   //----------------------------------------------------------------
   parameter RX_INIT = 0;
   parameter RX_SYN  = 1;
   parameter RX_ACK  = 2;
   parameter RX_WAIT = 3;

   parameter TX_INIT = 0;
   parameter TX_SYN  = 1;
   parameter TX_ACK  = 2;

   parameter BLOCK_BITS   = 512;
   parameter BLOCK_BYTES  = BLOCK_BITS / 8;
   parameter DIGEST_BITS  = 256;
   parameter DIGEST_BYTES = DIGEST_BITS / 8;

   //----------------------------------------------------------------
   // Registers including update variables and write enable.
   //----------------------------------------------------------------
   reg                          rx_ack_reg;
   reg                          rx_ack_new;
   reg                          rx_ack_we;

   reg [6 : 0]                  rx_ptr_reg;
   reg [7 : 0]                  rx_ptr_new;
   reg                          rx_ptr_we;

   reg [1 : 0]                  rx_ctrl_reg;
   reg [1 : 0]                  rx_ctrl_new;
   reg                          rx_ctrl_we;

   reg [7 : 0]                  block_reg [0 : BLOCK_BYTES - 1];
   reg [7 : 0]                  block_new;
   reg                          block_we;

   reg                          init_reg;
   reg                          init_new;
   reg                          init_we;

   reg                          next_reg;
   reg                          next_new;
   reg                          next_we;

   reg                          initnext_reg;
   reg                          initnext_new;
   reg                          initnext_we;

   reg                          tx_syn_reg;
   reg                          tx_syn_new;
   reg                          tx_syn_we;

   reg [6 : 0]                  tx_ptr_reg;
   reg [7 : 0]                  tx_ptr_new;
   reg                          tx_ptr_we;

   reg [1 : 0]                  tx_ctrl_reg;
   reg [1 : 0]                  tx_ctrl_new;
   reg                          tx_ctrl_we;

   reg                          tx_active_reg;
   reg                          tx_active_new;
   reg                          tx_active_we;

   wire [7 : 0]                 digest_reg [0 : DIGEST_BYTES - 1];

   //----------------------------------------------------------------
   // Wires.
   //----------------------------------------------------------------
   wire                         core_init;
   wire                         core_next;
   wire                         core_ready;
   wire [BLOCK_BITS - 1 : 0]    core_block;
   wire [DIGEST_BITS - 1 : 0]   core_digest;
   wire                         core_digest_valid;

   //----------------------------------------------------------------
   // Concurrent connectivity for ports etc.
   //----------------------------------------------------------------
   assign core_init  = init_reg;
   assign core_next  = next_reg;
   assign core_block = {block_reg[0], block_reg[1], block_reg[2], block_reg[3],
                        block_reg[4], block_reg[5], block_reg[6], block_reg[7],
                        block_reg[8], block_reg[9], block_reg[10], block_reg[11],
                        block_reg[12], block_reg[13], block_reg[14], block_reg[15],
                        block_reg[16], block_reg[17], block_reg[18], block_reg[19],
                        block_reg[20], block_reg[21], block_reg[22], block_reg[23],
                        block_reg[24], block_reg[25], block_reg[26], block_reg[27],
                        block_reg[28], block_reg[29], block_reg[30], block_reg[31],
                        block_reg[32], block_reg[33], block_reg[34], block_reg[35],
                        block_reg[36], block_reg[37], block_reg[38], block_reg[39],
                        block_reg[40], block_reg[41], block_reg[42], block_reg[43],
                        block_reg[44], block_reg[45], block_reg[46], block_reg[47],
                        block_reg[48], block_reg[49], block_reg[50], block_reg[51],
                        block_reg[52], block_reg[53], block_reg[54], block_reg[55],
                        block_reg[56], block_reg[57], block_reg[58], block_reg[59],
                        block_reg[60], block_reg[61], block_reg[62], block_reg[63]};

   assign rx_ack     = rx_ack_reg;
   assign tx_syn     = tx_syn_reg;
   assign tx_data    = digest_reg[tx_ptr_reg];

   assign digest_reg[0]  = core_digest[255 : 248];
   assign digest_reg[1]  = core_digest[247 : 240];
   assign digest_reg[2]  = core_digest[239 : 232];
   assign digest_reg[3]  = core_digest[231 : 224];
   assign digest_reg[4]  = core_digest[223 : 216];
   assign digest_reg[5]  = core_digest[215 : 208];
   assign digest_reg[6]  = core_digest[207 : 200];
   assign digest_reg[7]  = core_digest[199 : 192];
   assign digest_reg[8]  = core_digest[191 : 184];
   assign digest_reg[9]  = core_digest[183 : 176];
   assign digest_reg[10] = core_digest[175 : 168];
   assign digest_reg[11] = core_digest[167 : 160];
   assign digest_reg[12] = core_digest[159 : 152];
   assign digest_reg[13] = core_digest[151 : 144];
   assign digest_reg[14] = core_digest[143 : 136];
   assign digest_reg[15] = core_digest[135 : 128];
   assign digest_reg[16] = core_digest[127 : 120];
   assign digest_reg[17] = core_digest[119 : 112];
   assign digest_reg[18] = core_digest[111 : 104];
   assign digest_reg[19] = core_digest[103 : 96];
   assign digest_reg[20] = core_digest[95 : 88];
   assign digest_reg[21] = core_digest[87 : 80];
   assign digest_reg[22] = core_digest[79 : 72];
   assign digest_reg[23] = core_digest[71 : 64];
   assign digest_reg[24] = core_digest[63 : 56];
   assign digest_reg[25] = core_digest[55 : 48];
   assign digest_reg[26] = core_digest[47 : 40];
   assign digest_reg[27] = core_digest[39 : 32];
   assign digest_reg[28] = core_digest[31 : 24];
   assign digest_reg[29] = core_digest[23 : 16];
   assign digest_reg[30] = core_digest[15 : 8];
   assign digest_reg[31] = core_digest[7 : 0];

   //----------------------------------------------------------------
   // core instantiation.
   //----------------------------------------------------------------
   sha256_core core(
                    .clk(clk),
                    .reset_n(reset_n),

                    .init(core_init),
                    .next(core_next),

                    .block(core_block),

                    .ready(core_ready),

                    .digest(core_digest),
                    .digest_valid(core_digest_valid)
                    );

   //----------------------------------------------------------------
   // reg_update
   // Update functionality for all registers in the core.
   // All registers are positive edge triggered with synchronous
   // active low reset. All registers have write enable.
   //----------------------------------------------------------------

   always @ (posedge clk)
     begin: reg_update
        if (!reset_n)
          begin: reset_reg
             reg[7:0] i;
             for (i = 0; i < BLOCK_BYTES; i = i + 1)
               block_reg[i] <= 0;
             rx_ack_reg   <= 0;
             rx_ptr_reg   <= 0;
             rx_ctrl_reg  <= RX_INIT;
             tx_syn_reg   <= 0;
             tx_ptr_reg   <= 0;
             tx_ctrl_reg  <= TX_INIT;
             initnext_reg <= 0;
          end
        else
          begin
             if (rx_ack_we)
               begin
                  rx_ack_reg <= rx_ack_new;
               end

             if (rx_ptr_we)
               begin
                  rx_ptr_reg <= rx_ptr_new[6:0];
               end

             if (rx_ctrl_we)
               begin
                  rx_ctrl_reg <= rx_ctrl_new;
               end

             if (block_we)
               begin
                  block_reg[rx_ptr_reg] <= block_new;
               end

             if (init_we)
               begin
                  init_reg <= init_new;
               end

             if (next_we)
               begin
                  next_reg <= next_new;
               end

             if (initnext_we)
               begin
                  initnext_reg <= initnext_new;
               end

             if (tx_syn_we)
               begin
                  tx_syn_reg <= tx_syn_new;
               end

             if (tx_ptr_we)
               begin
                  tx_ptr_reg <= tx_ptr_new[6:0];
               end

             if (tx_ctrl_we)
               begin
                  tx_ctrl_reg <= tx_ctrl_new;
               end

             if (tx_active_we)
               begin
                  tx_active_reg <= tx_active_new;
               end
          end
     end // reg_update

   //----------------------------------------------------------------
   // rx_engine
   //----------------------------------------------------------------
   always @*
     begin: rx_engine
        rx_ack_new = 0;
        rx_ack_we = 0;
        rx_ptr_new = 0;
        rx_ptr_we = 0;
        rx_ctrl_new = 0;
        rx_ctrl_we = 0;
        block_new = 0;
        block_we = 0;
        init_new = 0;
        init_we = 0;
        next_new = 0;
        next_we = 0;
        initnext_new = 0;
        initnext_we = 0;

        if (tx_active_reg)
          begin
             initnext_we = 1;
          end

        case (rx_ctrl_reg)
          RX_INIT:
            if (core_ready)
              begin
                 rx_ctrl_new = RX_SYN;
                 rx_ctrl_we = 1;
              end
          RX_SYN:
            if (rx_syn)
              begin
                 rx_ack_new = 1;
                 rx_ack_we = 1;
                 block_new = rx_data;
                 block_we = 1;
                 rx_ctrl_new = RX_ACK;
                 rx_ctrl_we = 1;
              end
          RX_ACK:
            if (!rx_syn)
              begin
                 rx_ack_new = 0;
                 rx_ack_we = 1;
                 rx_ptr_new = rx_ptr_reg + 1;
                 rx_ptr_we = 1;
                 rx_ctrl_new = RX_SYN;
                 rx_ctrl_we = 1;
                 if (rx_ptr_new == BLOCK_BYTES)
                   begin
                      rx_ptr_new = 0;
                      rx_ctrl_new = RX_WAIT;
                      if (initnext_reg == 0)
                        begin
                           init_new = 1;
                           init_we = 1;
                           initnext_new = 1;
                           initnext_we = 1;
                        end
                      else
                        begin
                           next_new = 1;
                           next_we = 1;
                        end
                   end
              end
          RX_WAIT:
            if (!core_ready)
              begin
                 init_new = 0;
                 init_we = 1;
                 next_new = 0;
                 next_we = 1;
                 rx_ctrl_new = RX_INIT;
                 rx_ctrl_we = 1;
              end
        endcase // case (rx_ctrl_reg)
     end // rx_engine

   //----------------------------------------------------------------
   // tx_engine
   //----------------------------------------------------------------
   always @*
     begin: tx_engine
        tx_syn_new = 0;
        tx_syn_we = 0;
        tx_ptr_new = 0;
        tx_ptr_we = 0;
        tx_ctrl_new = 0;
        tx_ctrl_we = 0;
        tx_active_new = 0;
        tx_active_we = 0;

        case (tx_ctrl_reg)
          TX_INIT:
            if (core_digest_valid)
              begin
                 tx_ctrl_new = TX_SYN;
                 tx_ctrl_we = 1;
              end
          TX_SYN:
            begin
               tx_syn_new = 1;
               tx_syn_we = 1;
               tx_ctrl_new = TX_ACK;
               tx_ctrl_we = 1;
            end
          TX_ACK:
            if (!core_digest_valid)
              begin
                 tx_syn_new = 0;
                 tx_syn_we = 1;
                 tx_ptr_new = 0;
                 tx_ptr_we = 1;
                 tx_ctrl_new = TX_INIT;
                 tx_ctrl_we = 1;
              end
            else if (tx_ack)
              begin
                 tx_active_new = 1;
                 tx_active_we = 1;
                 tx_syn_new = 0;
                 tx_syn_we = 1;
                 tx_ptr_new = tx_ptr_reg + 1;
                 tx_ptr_we = 1;
                 tx_ctrl_new = TX_SYN;
                 tx_ctrl_we = 1;
                 if (tx_ptr_new == DIGEST_BYTES)
                   begin
                      tx_active_new = 0;
                      tx_ptr_new = 0;
                      tx_ctrl_new = TX_INIT;
                   end
              end
        endcase // case (tx_ctrl_reg)
     end // tx_engine

endmodule // sha256

//======================================================================
// EOF sha256.v
//======================================================================