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

Subversion Repositories sha256_hash_core

[/] [sha256_hash_core/] [trunk/] [syn/] [sha256/] [sha256_control.vhd] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jdoin
-----------------------------------------------------------------------------------------------------------------------
2 6 jdoin
-- Author:          Jonny Doin, jdoin@opencores.org, jonnydoin@gmail.com, jonnydoin@gridvortex.com
3 2 jdoin
-- 
4 6 jdoin
-- Create Date:     09:56:30 05/06/2016  
5 2 jdoin
-- Module Name:     sha256_control - RTL
6
-- Project Name:    sha256 hash engine
7
-- Target Devices:  Spartan-6
8
-- Tool versions:   ISE 14.7
9
-- Description: 
10
--
11
--      This is the control path logic for the GV_SHA256 fast engine.
12
--
13
--      It is a fully synchronous design, with all signals synchronous to the rising edge of the system clock.
14
--      The sequencer state machine controls the hash datapath modules, generating addresses for the coefficients ROM, load/enable signals for the 
15
--      message schedule, hash core and output registers circuit blocks, and control signals for the input padding logic.
16
--
17
--      The SHA256 hash core follows the FIPS-180-4 logic description for the SHA-256 algorithm, optimized as a single-cycle per iteration engine. 
18
--                                                                                              
19
--      This implementation follows the implementation guidelines of the NIST Cryptographic Toolkit, and the NIST Approved Algorithms notes. 
20
--
21
--      RELEVANT NIST PUBLICATIONS
22
--      Link to Document                                                               | Description
23
--      ------------------------------------------------------------------------------ | ---------------------------------------------------------
24
--      http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf                  | SECURITY REQUIREMENTS FOR CRYPTOGRAPHIC MODULES           
25
--      http://csrc.nist.gov/groups/ST/toolkit/index.html                              | NIST CRYPTOGRAPHIC TOOLKIT                                
26
--      http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf                      | Secure Hash Standard (SHS) SHA-256                        
27
--      http://csrc.nist.gov/publications/fips/fips198-1/FIPS-198-1_final.pdf          | The Keyed-Hash Message Authentication Code (HMAC)         
28
--      http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf           | SHA-256 verification test vectors                         
29
--      http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA2_Additional.pdf  | Additional SHA-256 corner case verification test vectors  
30
--                                                                                             
31
--      RELEVANT RFCs
32
--      Link to PDF document                       | Description
33
--      ------------------------------------------ | ---------------------------------------------------------
34
--      https://tools.ietf.org/pdf/rfc2104.pdf     | RFC2104 - HMAC: Keyed-Hashing for Message Authentication
35
--      https://tools.ietf.org/pdf/rfc4231.pdf     | RFC4231 - Identifiers and Test Vectors for HMAC-SHA-256
36
--      https://tools.ietf.org/pdf/rfc4868.pdf     | RFC4868 - Using HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 with IPsec
37
---------------------------------------------------------------------------------------------------------------------------------------------------------------
38
--      SHA256 ENGINE
39
--      =============
40
--
41
--      The setup of all circuit blocks is performed in a single extra clock cycle, besides the 64 steps needed to compute a hash block operation, 
42
--      resulting in a 65-cycle per block hash computation processor. Heavy pipelining is implemented to suppress control path operations logic steps. 
43
--      The engine is internally implemented as a 256-bit machine, with all combinational operations performed as a single-cycle operation on each
44
--      64 steps of the hash algorithm. Wide transfers of 256-bit data are also performed as single-cycle operations. 
45
--
46 9 jdoin
--      The data input accepts 16 consecutive 32bit words for a total of 64 bytes per block, one word per clock cycle. The input signal 'wr_i' can be
47 2 jdoin
--      used as a flow control input to hold the processor to wait for slower data.
48
--
49
--      A hash computation starts with a 'start_i' pulse that resets the processor. A pulse of the 'end_i' signal marks the last input data word. The
50
--      core will pad the last block according to the SHA256 rules, and present the results of the hash computation at the output registers, raising the
51
--      'data_valid' signal to mark the end of the computation. The hash results are available at the 256-bit output port.
52
--
53
--      The following waveforms describe the detailed operation for message start, update and end, with internal signals and FSM states. 
54
--
55
--      BEGIN BLOCK (1st block) - showing lookahead Wt and Kt
56
--      ======================
57
--
58
--      The hash operation starts with a 'start' sync pulse, which causes the RESET of the processor. The processor comes out of RESET only after 'start' is
59
--      released. 
60
--      The DATA_INPUT state is signalled by the data request signal 'di_req' going HIGH. The processor will latch 16 words from the 'di' port, at every 
61
--      rising edge of the system clock. At the end of the block input, the 'di_req' signal goes LOW. 
62
--      The input data can be held by bringing the 'ack' input LOW. When the 'ack' input is held LOW, it includes a wait state in the whole processor, to
63
--      cope with slow inputs or to allow periodic fetches of input data from multiple data sources. 
64
--
65
--      STATE              |reset| data                                    |wait |                                                     | process                  
66
--                    __   |__   |__    __    __    __    __    __    __   |__   |__    __    __    __    __    __    __    __    __   |__    __    __ 
67
--      clk_i      __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \...     -- system clock
68
--                        _____                                                                                                                                      
69
--      start_i    ______/   \_\_______________________________________________________________________________________________________________________...     -- 'start_i' resets the processor and starts a new hash
70
--                                                                                                                                                       
71
--      end_i      ____________________________________________________________________________________________________________________________________...     -- 'end_i' marks end of last block data input
72
--                 __ _ _ _       _____________________________________________________________________________________________________                  
73
--      di_req_o   __ _ _ _\_____/                                                                                                     \_______________...     -- 'di_req_o' asserted during data input
74
--                            ___________________________________________       _________________________________________________________                
75 9 jdoin
--      wr_i       __________/____/                                      \_____/                                                         \_____________...     -- 'wr_i' can hold the core for slow data
76 2 jdoin
--                 __________ _________ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ ______ ______________...
77
--      di_i       __________\___\_W0__\__W1_\__W2_\__W3_\__W4_\__W5_\\\\\\\__W6_\__W7_\__W8_\__W9_\_W10_\_W11_\_W12_\_W13_\_W14_\_W15__\______X_______...     -- user words on 'di_i' are latched on 'clk_i' rising edge
78
--                 ____________________ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ _____________________...
79
--      st_cnt_reg ________/__0__/__0__/__1__/__2__/__3__/__4__/__5__/___6_______/__7__/__8__/__9__/__10_/__11_/__12_/__13_/__14_/__15_/__16_/__17_/_18...     -- internal state counter value
80
--                 __________ ___ _____ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ _____________________...
81
--      Wt_i@core  __________\___\__W0_\__W1_\__W2_\__W3_\__W4_\__W5_\\\\\\\__W6_\__W7_\__W8_\__W9_\_W10_\_W11_\_W12_\_W13_\_W14_\_W15_________________...     -- msg scheduler lookahead output for Wt_i at core
82
--                 ______________ _____ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ _____________________...
83
--      Kt_i@core  ______________/__K0_/__K1_/__K2_/__K3_/__K4_/__K5_/__K6_______/__K7_/__K8_/__K9_/_K10_/_K11_/_K12_/_K13_/_K14_/_K15_________________...     -- Kt rom synchronous with scheduler for Kt_i at core
84
--                 __ _ _ _                                                                                                                                            
85
--      error_o    __ _ _ _\___________________________________________________________________________________________________________________________...     -- 'start_i' clears any error condition
86
--                 __ _ _ _                                                                                                                                            
87
--      do_valid_o __ _ _ _\___________________________________________________________________________________________________________________________...     -- 'start_i' invalidates any previous results
88
--
89
--
90
--      UPDATE BLOCK (preload)
91
--      =====================
92
--
93
--      At the start of each block, the 'di_req' signal is raised to request new data.
94
--
95
--      STATE       ... process  |next | data                                    |wait |                                                     | process                    
96
--                    __    __    __    __    __    __    __    __    __    __   |__   |__    __    __    __    __    __    __    __    __    __ 
97
--      clk_i      __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \...        -- system clock
98
--                                                                                                                                                  
99
--      end_i      ______________________________________________________________________________________________________________________________...        -- 'end_i' marks end of last block data input
100
--                                      _____________________________________________________________________________________________________       
101
--      di_req_o   ____________________/                                                                                                     \___...        -- 'di_req_o' asserted during data input
102
--                          ___________________________________________________       _________________________________________________________     
103 9 jdoin
--      wr_i       ________/__________/                                        \_____/                                                         \_...        -- 'wr_i' can hold the core for slow data
104 2 jdoin
--                 _________________ _ ______ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ _____ ____...
105
--      di_i       _________________\\\___W0_\__W1_\__W2_\__W3_\__W4_\__W5_\\\\\\\\_W6_\__W7_\__W8_\__W9_\_W10_\_W11_\_W12_\_W13_\_W14_\_W15_\\_X_...       -- user words on 'di_i' are latched on 'clk_i' rising edge
106
--                 
107
--
108
--      UPDATE BLOCK (delayed start)
109
--      ===========================
110
--
111
--      The data for the new block can be delayed, by keeping the 'ack' signal low until the data is present at the data input port. 
112
--
113
--      STATE      ..|next | data                                                                  |wait |                                         | process                    
114
--                    __    __    __    __    __    __    __    __    __    __    __    __    __   |__   |__    __    __    __    __    __    __    __ 
115
--      clk_i      __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \...     -- system clock
116
--                                                                                                                                                       
117
--      end_i      ____________________________________________________________________________________________________________________________________...     -- 'end_i' marks end of last block data input
118
--                          _______ _ _ ___________________________________________________________________________________________________________      
119
--      di_req_o   ________/                                                                                                                       \___...     -- 'di_req_o' asserted during data input
120
--                                             __________________________________________________       _____________________________________________    
121 9 jdoin
--      wr_i       ________________ _ _ ______/                                                  \_____/                                             \_...     -- 'wr_i' valid on rising edge of 'clk_i'
122 2 jdoin
--                 ________________ _ _ ___________ _____ _____ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ ____...
123
--      di_i       ________________ _ _ ______\_W0_\__W1_\__W2_\__W3_\__W4_\__W5_\__W6_\__W7_\\\\\\\__W8_\__W9_\_W10_\_W11_\_W12_\_W13_\_W14_\_W15_\\_Z_...     -- user words on 'di_i' are latched on 'clk_i' rising edge
124
--                 
125
--
126
--      END BLOCK (success)
127
--      ==================
128
--
129
--      At the end of the last block the signal 'end' must be raised for at least one clock cycle. 
130
--      The 'bytes' input marks the number of valid bytes in the last word. 
131
--      A PADDING state completes the last data block and a BLK_PROCESS finishes the hash computation.
132
--      The 'do_valid' remains HIGH until the next RESET.
133
--
134
--      STATE      ..|next | data                              | padding         | process                     |next | valid     |reset| data     
135
--                    __    __    __    __    __    __    __    __    __          __    __    __          __    __    __    __    __    __    __  
136
--      clk_i      __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \_ _ _ __/  \__/  \__/  \_ _ _ __/  \__/  \__/  \__/  \__/  \__/  \__/  \_...     -- system clock
137
--                                                                                                                              ______                    
138
--      start_i    ____________________________________________________________________________________________________________/   \__\___________...     -- 'start_i' resets the processor and starts a new hash
139
--                                                           ______                                                                               
140
--      end_i      _________________________________________/      \______ _ _ ___________________ _ _ ___________________________________________...     -- 'end_i' marks end of last block data input
141
--                          ___________________________________                                                                         __________  
142
--      di_req_o   ________/                                   \__________ _ _ ___________________ _ _ ________________________________/          ...     -- 'di_req_o' asserted during data input
143
--                           ______________________________________                                                                      _________  
144 9 jdoin
--      wr_i       _________/                                    \\\______ _ _ ___________________ _ _ _________________________________/         ...     -- 'wr_i' can hold the core for slow data
145 2 jdoin
--                 ______________ _____ _____ _____ _____ _____ __________ _ _ ___________________ _ _ ______________________________________ ____...
146
--      di_i       _________\_W0_\__W1_\__W2_\__W3_\__W4_\__W5_\__________ _ _ ___________________ _ _ _________________________________\_W0_\__W1...     -- words after the end_i assertion are ignored
147
--                 __ _____ _____ _____ _____ _____ _____ _____ _____ ____ _ ____ _____ _____ ____ _ _ ______________________________________ ____
148
--      st_cnt_reg __/_64__/__0__/__1__/__2__/__3__/__4__/__5__/__6__/__7_ _ _15_/__16_/__17_/__18 _ _ __/__63_/__64_/______0__________/__0__/__1_...     -- internal state counter value
149
--                          _____ _____ _____ _____ _____ _____                                                                         _____ ____
150
--      bytes_i    --------<__0__\__0__\__0__\__0__\__0__\__3__>-----------------------------------------------------------------------<__0__\__0_...     -- bytes_i mark number of valid bytes in each word
151
--                                                                                                                                                   
152
--      error_o    _______________________________________________________ _ _ ___________________ _ _ ___________________________________________...     -- 'error_o' goes high on an invalid computation
153
--                                                                                                                    ___________                 
154
--      do_valid_o _______________________________________________________ _ _ ___________________ _ _ ______________/           \________________...     -- 'do_valid_o' goes high at the end of a computation
155
--                                                                                                                    ___________                 
156
--      H0_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H0______\________________...     -- H0 holds the bytes 0..3 of the output
157
--                                                                                                                    ___________                                 
158
--      H1_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H1______\________________...     -- H1 holds the bytes 4..7 of the output
159
--                                                                                                                    ___________                            
160
--      H2_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H2______\________________...     -- H2 holds the bytes 8..11 of the output
161
--                                                                                                                    ___________                            
162
--      H3_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H3______\________________...     -- H3 holds the bytes 12..15 of the output
163
--                                                                                                                    ___________                            
164
--      H4_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H4______\________________...     -- H4 holds the bytes 16..19 of the output
165
--                                                                                                                    ___________                            
166
--      H5_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H5______\________________...     -- H5 holds the bytes 20..23 of the output
167
--                                                                                                                    ___________                            
168
--      H6_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H6______\________________...     -- H6 holds the bytes 24..27 of the output
169
--                                                                                                                    ___________                            
170
--      H7_o       _______________________________________________________ _ _ ___________________ _ _ ______________/___H7______\________________...     -- H7 holds the bytes 28..31 of the output
171
--
172
--
173
--      END BLOCK (full last block)
174
--      ==================
175
--
176
--      If the last block has exactly 16 full words, the controller inserts a dummy PADDING cycle, processes the input block, and inserts a
177
--      last PADDING block followed by a last BLK_PROCESS block.
178
--
179
--      STATE      ... data         |pad  | process   |next | pad                   | process   |next | valid     |reset| data
180
--                 __    __    __   |__   |__         |__   |__    __          __   |__         |__   |__    __   |__   |__    __     
181
--      clk_i        \__/  \__/  \__/  \__/  \_ _ _ __/  \__/  \__/  \_ _ _ __/  \__/  \_ _ _ __/  \__/  \__/  \__/  \__/  \__/  \_...     -- system clock
182
--                                                                                                               ______                  
183
--      start_i    ____________________________ _ _ ____________________________________________________________/   \__\___________...     -- 'start_i' resets the processor and starts a new hash
184
--                              _____                                                                                                         
185
--      end_i      ____________/     \_________ _ _ ___________________ _ _ _____________ _ _ _____________________________________...     -- 'end_i' marks end of last block data input
186
--                 _________________                                                                                     __________  
187
--      di_req_o                    \__________ _ _ ___________________ _ _ _____________ _ _ __________________________/          ...     -- 'di_req_o' asserted on rising edge of 'clk_i'
188
--                 ____________________                                                                                   _________  
189 9 jdoin
--      wr_i                         \\\_______ _ _ ___________________ _ _ _____________ _ _ ___________________________/         ...     -- 'wr_i' valid on rising edge of 'clk_i'
190 2 jdoin
--                 _____ _____ _____ __________ _ _ ___________________ _ _ _____________ _ _ ________________________________ ____...
191
--      di_i       _W13_\_W14_\_W15_\__________ _ _ ___________________ _ _ _____________ _ _ ___________________________\_W0_\__W1...     -- words after the end_i assertion are ignored
192
--                 _____ _____ _____ _____ ____ _ ____ _____ _____ ____ _ _ ________ ____ _ ____ _____ _______________________ ____
193
--      st_cnt_reg _13__/_14__/_15__/_16__/_16_ _ _63_/__64_/__0__/__1_ _ _ __/_15__/_16_ _ _63_/__64_/_____0_____/__0__/__0__/__1_...     -- internal state counter value
194
--                 _____ _____ _____                                                                                     _____ ____
195
--      bytes_i    __0__/__0__/__0__>-----------------------------------------------------------------------------------<__0__/__0_...     -- bytes_i mark number of valid bytes in each word
196
--                                                                                                     ___________                 
197
--      do_valid_o ____________________________ _ _ ___________________ _ _ __________________________/           \________________...     -- 'do_valid_o' goes high at the end of a computation
198
--
199
--
200
------------------------------ COPYRIGHT NOTICE -----------------------------------------------------------------------
201 9 jdoin
--                                                                   
202
--      This file is part of the SHA256 HASH CORE project http://opencores.org/project,sha256_hash_core
203
--                                                                   
204
--      Author(s):      Jonny Doin, jdoin@opencores.org, jonnydoin@gridvortex.com, jonnydoin@gmail.com
205
--                                                                   
206
--      Copyright (C) 2016 Jonny Doin
207
--      -----------------------------
208
--                                                                   
209
--      This source file may be used and distributed without restriction provided that this copyright statement is not    
210
--      removed from the file and that any derivative work contains the original copyright notice and the associated 
211
--      disclaimer. 
212
--                                                                   
213
--      This source file is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
214
--      General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
215
--      (at your option) any later version.
216
--                                                                   
217
--      This source is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
218
--      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more  
219
--      details.
220
--
221
--      You should have received a copy of the GNU Lesser General Public License along with this source; if not, download 
222
--      it from http://www.gnu.org/licenses/lgpl.txt
223
--                                                                   
224 2 jdoin
------------------------------ REVISION HISTORY -----------------------------------------------------------------------
225
--
226
-- 2016/05/22   v0.01.0010  [JD]    started development. design of blocks and port interfaces.
227
-- 2016/06/05   v0.01.0090  [JD]    all modules integrated. testbench for basic test vectors verification.
228
-- 2016/06/05   v0.01.0095  [JD]    verification failed. misalignment of words in the datapath. 
229
-- 2016/06/06   v0.01.0100  [JD]    first simulation verification against NIST-FIPS-180-4 test vectors "abc" passed.
230
-- 2016/06/07   v0.01.0101  [JD]    failed 2-block test for "abcdbcdecd..." vector. Fixed padding control logic.
231
-- 2016/06/07   v0.01.0105  [JD]    verification against all NIST-FIPS-180-4 test vectors passed.
232
-- 2016/06/11   v0.01.0105  [JD]    verification against NIST-SHA2_Additional test vectors #1 to #10 passed.
233
-- 2016/06/11   v0.01.0110  [JD]    optimized controller states, reduced 2 clocks per block. 
234
-- 2016/06/18   v0.01.0120  [JD]    implemented error detection on 'bytes_i' input.
235
-- 2016/07/06   v0.01.0210  [JD]    optimized suspend logic on 'sch_ld' to supress possible glitch in 'pad_one_next'.
236 9 jdoin
-- 2016/09/25   v0.01.0220  [JD]    changed 'ack_i' name to 'wr_i', and changed semantics to 'data write'.
237 2 jdoin
--
238
-----------------------------------------------------------------------------------------------------------------------
239
--  TODO
240
--  ====
241
--
242
--
243
-----------------------------------------------------------------------------------------------------------------------
244
library ieee;
245
use ieee.std_logic_1164.all;
246
use ieee.numeric_std.all;
247
 
248
entity sha256_control is
249
    port (
250
        -- inputs
251
        clk_i : in std_logic := 'U';                                    -- system clock
252
        ce_i : in std_logic := 'U';                                     -- core clock enable
253
        start_i : in std_logic := 'U';                                  -- reset the processor and start a new hash
254
        end_i : in std_logic := 'U';                                    -- marks end of last block data input
255 9 jdoin
        wr_i  : in std_logic := 'U';                                    -- input word write/hold control
256 2 jdoin
        bytes_i : in std_logic_vector (1 downto 0) := (others => 'U');  -- valid bytes in input word
257
        error_i : in std_logic := 'U';                                  -- datapath error input from other modules
258
        -- output control signals
259
        bitlen_o : out std_logic_vector (63 downto 0);                  -- message bit length
260
        words_sel_o : out std_logic_vector (1 downto 0);                -- bitlen insertion control
261
        Kt_addr_o : out std_logic_vector (5 downto 0);                  -- address for the Kt coefficients ROM
262
        sch_ld_o : out std_logic;                                       -- load/recirculate words for message scheduler
263
        core_ld_o : out std_logic;                                      -- load all registers for hash core
264
        oregs_ld_o : out std_logic;                                     -- load output registers
265
        sch_ce_o : out std_logic;                                       -- clock enable for message scheduler logic block
266
        core_ce_o : out std_logic;                                      -- clock enable for hash core logic block
267
        oregs_ce_o : out std_logic;                                     -- clock enable for output regs logic block
268
        bytes_ena_o : out std_logic_vector (3 downto 0);                -- byte lane selectors for padding logic block
269
        one_insert_o : out std_logic;                                   -- insert leading '1' in the padding
270
        di_req_o : out std_logic;                                       -- external data request by the 'di_i' port
271
        data_valid_o : out std_logic;                                   -- operation finished. output data is valid
272
        error_o : out std_logic                                         -- operation aborted. output data is not valid
273
    );
274
end sha256_control;
275
 
276
architecture rtl of sha256_control is
277
    --=============================================================================================
278
    -- Type definitions
279
    --=============================================================================================
280
    -- controller states
281
    type hash_toplevel_control is
282
            (   st_reset,                   -- core reset, initial state
283
                st_sha_data_input,          -- sha data input
284
                st_sha_blk_process,         -- sha block process
285
                st_sha_blk_nxt,             -- sha block next
286
                st_sha_padding,             -- sha padding    
287
                st_sha_data_valid,          -- sha data valid
288
                st_error                    -- fsm locks on error, exit only by reset
289
            );
290
 
291
    --=============================================================================================
292
    -- Signals for state machine control
293
    --=============================================================================================
294
    signal hash_control_st_reg  : hash_toplevel_control := st_reset;
295
    signal hash_control_st_next : hash_toplevel_control := st_reset;
296
 
297
    --=============================================================================================
298
    -- Signals for internal operation
299
    --=============================================================================================
300
    -- combinational flags: message data input / padding control / block internal process selection
301
    signal reset : std_logic;
302
    signal sha_reset : std_logic;
303
    signal sha_init : std_logic;
304
    signal wait_run_ce : std_logic;
305
    -- registered flags: last block, padding control and hmac processing
306
    signal sha_last_blk_reg : std_logic;
307
    signal sha_last_blk_next : std_logic;
308
    signal padding_reg : std_logic;
309
    signal padding_next : std_logic;
310
    signal pad_one_reg : std_logic;
311
    signal pad_one_next : std_logic;
312
    signal bytes_error_reg : std_logic;
313
    signal bytes_error_next : std_logic;
314
    -- 64 bit message bit counter
315
    signal msg_bit_cnt_reg : unsigned (63 downto 0);
316
    signal msg_bit_cnt_next : unsigned (63 downto 0);
317
    signal bits_to_add : unsigned (5 downto 0);
318
    signal msg_bit_cnt_ce : std_logic;
319
    -- sequencer state counter
320
    signal st_cnt_reg : unsigned (6 downto 0);
321
    signal st_cnt_next : unsigned (6 downto 0);
322
    signal st_cnt_ce : std_logic;
323
    signal st_cnt_clr : std_logic;
324
 
325
    --=============================================================================================
326
    -- Output Control Signals
327
    --=============================================================================================
328
    -- unregistered control signals
329
    signal words_sel : std_logic_vector (1 downto 0);   -- bitlen insertion control
330
    signal sch_ld : std_logic;                          -- input data load into message scheduler control
331
    signal core_ld : std_logic;                         -- hash core load data registers control
332
    signal oregs_ld : std_logic;                        -- load initial value into output regs control
333
    signal sch_ce : std_logic;                          -- clock enable for message scheduler logic block
334
    signal core_ce : std_logic;                         -- clock enable for hash core logic block
335
    signal oregs_ce : std_logic;                        -- clock enable for output regs logic block
336
    signal bytes_ena : std_logic_vector (3 downto 0);   -- byte lane selectors for padding logic block
337
    signal one_insert : std_logic;                      -- insert leading one in the padding
338
    signal di_req : std_logic;                          -- data request
339 9 jdoin
    signal di_wr_window : std_logic;                    -- valid data write window    
340 2 jdoin
    signal data_valid : std_logic;                      -- operation finished. output data is valid
341
    signal core_error : std_logic;                      -- operation aborted. output data is not valid
342 9 jdoin
    signal data_input_error : std_logic;                -- internal error signal for data write    
343 2 jdoin
    signal out_error : std_logic;                       -- operation aborted. output data is not valid
344
 
345
begin
346
    --=============================================================================================
347
    --  REGISTER TRANSFER PROCESSES
348
    --=============================================================================================
349
    -- control fsm register transfer logic
350
    control_fsm_proc: process (clk_i) is
351
    begin
352
        -- FSM state register: sync RESET on 'reset', and sync PRESET on error_i
353
        if clk_i'event and clk_i = '1' then
354
            if reset = '1' then
355
                -- all registered values are reset on master clear
356
                hash_control_st_reg <= st_reset;
357
            elsif out_error = '1' then
358 9 jdoin
                -- error latch: lock on the error state            
359 2 jdoin
                hash_control_st_reg <= st_error;
360
            elsif ce_i = '1' then
361
                -- all registered values are held on master clock enable
362
                hash_control_st_reg <= hash_control_st_next;
363
            end if;
364
        end if;
365
        -- SHA256 registers, RESET on 'sha_init'
366
        if clk_i'event and clk_i = '1' then
367
            if sha_init = '1' then
368
                -- all SHA256 registered values are reset on SHA master clear
369
                sha_last_blk_reg <= '0';
370
                padding_reg <= '0';
371
            elsif ce_i = '1' then
372
                -- all registered values are held on master clock enable
373
                sha_last_blk_reg <= sha_last_blk_next;
374
                padding_reg <= padding_next;
375
            end if;
376
        end if;
377
        -- bytes_i error register: sync RESET on 'reset'
378
        if clk_i'event and clk_i = '1' then
379
            if reset = '1' then
380
                -- all registered values are reset on master clear
381
                bytes_error_reg <= '0';
382
            else
383
                -- all registered values are held on master clock enable
384
                bytes_error_reg <= bytes_error_next;
385
            end if;
386
        end if;
387
    end process control_fsm_proc;
388
 
389
    -- bit counter register transfer logic
390
    bit_counter_proc: process (clk_i) is
391
    begin
392
        -- bit counter
393
        if clk_i'event and clk_i = '1' then
394
            if sha_init = '1' then
395
                msg_bit_cnt_reg <= (others => '0');
396
            elsif ce_i = '1' and msg_bit_cnt_ce = '1' then
397
                msg_bit_cnt_reg <= msg_bit_cnt_next;
398
            end if;
399
        end if;
400
    end process bit_counter_proc;
401
 
402
    -- state counter register transfer process
403
    state_counter_proc: process (clk_i) is
404
    begin
405
        -- core state counter
406
        if clk_i'event and clk_i = '1' then
407
            if (sha_init = '1') or (st_cnt_clr = '1') then
408
                st_cnt_reg <= (others => '0');
409
            elsif (ce_i = '1') and (st_cnt_ce = '1') then
410
                st_cnt_reg <= st_cnt_next;
411
            end if;
412
        end if;
413
    end process state_counter_proc;
414
 
415
    -- one-padding register transfer logic
416
    pad_one_fsm_proc: process (clk_i) is
417
    begin
418
        if clk_i'event and clk_i = '1' then
419
            if sha_init = '1' then
420
                -- all registered values are reset on master clear
421
                pad_one_reg <= '1';
422
            elsif (ce_i = '1') and (sch_ce = '1') then
423
                -- one-padding register is clocked synchronous with the message schedule
424
                pad_one_reg <= pad_one_next;
425
            end if;
426
        end if;
427
    end process pad_one_fsm_proc;
428
 
429
    --=============================================================================================
430
    --  COMBINATIONAL NEXT-STATE LOGIC
431
    --=============================================================================================
432
    -- State and control path combinational logic
433
    -- The hash_control_st_reg state register controls the SHA256 algorithm.
434
    control_combi_proc : process (  hash_control_st_reg, sha_last_blk_reg, padding_reg, wait_run_ce,
435
                                    end_i, st_cnt_reg, sha_last_blk_next, one_insert, sha_reset ) is
436
    begin
437
        -- default logic that applies to all states at each fsm clock --
438
 
439
        -- assign default values to all unchanging combinational outputs (avoid latches)
440
        hash_control_st_next <= hash_control_st_reg;
441
        sha_last_blk_next <= sha_last_blk_reg;
442
        padding_next <= padding_reg;
443
        -- handshaking
444
        sha_init <= '0';
445
        core_error <= '0';
446 9 jdoin
        di_wr_window <= '0';
447 2 jdoin
        words_sel <= b"00";
448
        data_valid <= '0';
449
        di_req <= '0';              -- data request only during data input
450
        -- state counter
451
        st_cnt_clr <= '0';          -- only clear the state counter at the beginning of each block
452
        st_cnt_ce <= '0';
453
        -- message scheduler
454
        sch_ld <= '1';              -- enable pass-thru input through message schedule
455
        sch_ce <= '0';              -- stop message schedule clock
456
        -- hash core
457
        core_ld <= '0';             -- enable internal hash core logic
458
        core_ce <= '0';             -- core computation enabled only for data input and processing
459
        -- output registers
460
        oregs_ld <= '0';            -- defaults for accumulate blk hash
461
        oregs_ce <= '0';            -- only register init values and end of computation
462
        case hash_control_st_reg is
463
 
464
            when st_reset =>                -- master reset: starts a new hash/hmac processing
465
                -- moore outputs
466
                sha_init <= '1';            -- reset SHA256 engine
467
                oregs_ld <= '1';            -- load initial hash values
468
                oregs_ce <= '1';            -- latch initial hash values into output registers
469
                core_ld <= '1';             -- load initial value into core registers
470
                core_ce <= '1';             -- latch initial value into core registers
471
                st_cnt_clr <= '1';          -- reset state counter
472 9 jdoin
                di_wr_window <= '1';        -- enable data write window                
473 2 jdoin
                -- next state
474
                hash_control_st_next <= st_sha_data_input;
475
 
476
            when st_sha_data_input =>       -- message data words are clocked into the processor
477
                -- moore outputs
478
                di_req <= '1';              -- request message data
479 9 jdoin
                di_wr_window <= '1';        -- enable data write window                
480 2 jdoin
                sch_ce <= wait_run_ce;      -- hold the message scheduler with data hold
481
                st_cnt_ce <= wait_run_ce;   -- hold state count with data hold
482
                core_ce <= wait_run_ce;     -- hold processing clock with data hold
483
                -- next state
484
                if wait_run_ce = '1' then
485
                    if end_i = '1' then
486
                        hash_control_st_next <= st_sha_padding;     -- pad incomplete blocks
487
                    elsif st_cnt_reg = 15 then
488
                        hash_control_st_next <= st_sha_blk_process; -- process one more block
489
                    end if;
490
                end if;
491
 
492
            when st_sha_blk_process =>      -- internal block hash processing
493
                -- moore outputs
494
                st_cnt_ce <= '1';           -- enable state counter
495
                sch_ld <= '0';              -- recirculate scheduler data
496
                sch_ce <= '1';              -- enable message scheduler clock
497
                core_ce <= '1';             -- enable processing clock
498
                -- next state
499
                if st_cnt_reg = 63 then
500
                    hash_control_st_next <= st_sha_blk_nxt;
501
                end if;
502
 
503
            when st_sha_blk_nxt =>          -- prepare for next block
504
                -- moore outputs
505
                st_cnt_clr <= '1';          -- reset state counter at the beginning of each block
506
                sch_ld <= '0';
507
                sch_ce <= '0';              -- stop the message schedule
508
                core_ld <= '1';             -- load previous result value into core registers
509
                core_ce <= '1';             -- latch result value into core registers
510
                oregs_ce <= '1';            -- latch core result into regs accumulator
511
                -- next state
512
                if sha_last_blk_reg = '1' then
513
                    hash_control_st_next <= st_sha_data_valid;  -- no hmac operation: publish data valid
514
                elsif padding_reg = '1' then
515
                    hash_control_st_next <= st_sha_padding;     -- additional padding block
516
                else
517
                    hash_control_st_next <= st_sha_data_input;  -- continue requesting input data
518
                end if;
519
 
520
            when st_sha_padding =>          -- padding of bits on the last message block
521
                -- moore outputs                
522
                padding_next <= '1';
523
                if st_cnt_reg = 16 then     -- if word 16, data block was full: proceed to process this block
524
                    -- pause processing for this cycle
525
                    sch_ld <= '0';
526
                    sch_ce <= '0';
527
                    core_ce <= '0';
528
                    st_cnt_ce <= '0';
529
                    -- next state
530
                    hash_control_st_next <= st_sha_blk_process;
531
                else                        -- incomplete block: pad words until data block completes
532
                    sch_ld <= '1';          -- load padded data into scheduler
533
                    sch_ce <= '1';          -- enable message scheduler clock
534
                    core_ce <= '1';         -- enable processing clock
535
                    st_cnt_ce <= '1';       -- enable state counter
536
                    if st_cnt_reg = 15 then -- pad up to word 15
537
                        if sha_last_blk_next = '1' then
538
                            words_sel <= b"10";  -- insert bitlen lo
539
                        end if;
540
                        -- next state
541
                        hash_control_st_next <= st_sha_blk_process;
542
                    elsif (one_insert = '0') and (st_cnt_reg = 14) then
543
                        words_sel <= b"01";     -- insert bitlen hi
544
                        sha_last_blk_next <= '1';   -- mark this as the last block
545
                    elsif st_cnt_reg = 13 then
546
                        sha_last_blk_next <= '1';   -- mark this as the last block
547
                    end if;
548
                end if;
549
 
550
            when st_sha_data_valid =>       -- process is finished, waiting for begin command
551
                -- moore outputs
552
                data_valid <= '1';          -- output results are valid
553
                -- wait for core reset with 'reset'               
554
 
555
            when st_error =>                -- processing or input error: reset with 'reset' = 1
556
                -- moore outputs
557
                core_error <= '1';
558
                st_cnt_clr <= '1';          -- clear state counter
559
                -- wait for core reset with 'reset'               
560
 
561
            when others =>                  -- internal state machine error
562
                -- next state
563
                hash_control_st_next <= st_error;
564
 
565
        end case;
566
    end process control_combi_proc;
567
 
568
 
569
    --=============================================================================================
570
    --  COMBINATIONAL CONTROL LOGIC
571
    --=============================================================================================
572
 
573
    -- controller RESET signal logic
574
    sha_reset_combi_proc:   sha_reset <= '1' when start_i = '1'   else '0';
575
    reset_combi_proc:       reset <= '1'     when sha_reset = '1' else '0';
576
 
577
    -- pad-one flag register
578
    pad_one_next_combi_proc: process (bytes_ena, sch_ld, pad_one_reg) is
579
    begin
580
        -- after one-insertion, clear the pad-one flag register 
581
        if (bytes_ena /= b"1111") and (sch_ld = '1') then
582
            pad_one_next <= '0';
583
        else
584
            pad_one_next <= pad_one_reg;
585
        end if;
586
    end process pad_one_next_combi_proc;
587
 
588
    -- padding byte lane selectors
589
    bytes_ena_combi_proc: process (bytes_i, padding_next, di_req, one_insert, end_i) is
590
    begin
591
        if di_req = '1' and end_i /= '1' then
592
            -- accept only full words before last word
593
            bytes_ena <= b"1111";
594
        elsif di_req = '1' and end_i = '1' then
595
            -- user data: bytes controlled by 'bytes_i'
596
            case bytes_i is
597
                when b"01"  => bytes_ena <= b"0001";
598
                when b"10"  => bytes_ena <= b"0011";
599
                when b"11"  => bytes_ena <= b"0111";
600
                when others => bytes_ena <= b"1111";
601
            end case;
602
        else
603
            -- no data input: force zero bits valid
604
            bytes_ena <= b"0000";
605
        end if;
606
    end process bytes_ena_combi_proc;
607
 
608
    -- bit counter next logic
609
    msg_bit_cnt_next_combi_proc: process (bytes_ena, msg_bit_cnt_reg, bits_to_add) is
610
    begin
611
        case bytes_ena is
612
            when b"0001"    => bits_to_add <= to_unsigned( 8, 6);
613
            when b"0011"    => bits_to_add <= to_unsigned(16, 6);
614
            when b"0111"    => bits_to_add <= to_unsigned(24, 6);
615
            when b"1111"    => bits_to_add <= to_unsigned(32, 6);
616
            when others     => bits_to_add <= to_unsigned( 0, 6);
617
        end case;
618
        msg_bit_cnt_next <= msg_bit_cnt_reg + bits_to_add;
619
    end process msg_bit_cnt_next_combi_proc;
620
 
621 9 jdoin
    -- data input wait/run: insert wait states during data input for 'wr_i' = '0'
622
    wait_run_proc:          wait_run_ce <= '1' when di_req = '1' and wr_i  = '1' else '0';
623 2 jdoin
 
624
    -- padding one-insertion control
625
    one_insert_proc:        one_insert <= '1' when pad_one_reg = '1' else '0';
626
 
627
    -- bit counter clock enable
628
    msg_bit_cnt_ce_proc :   msg_bit_cnt_ce <= '1' when wait_run_ce = '1' else '0';
629
 
630
    -- state counter next logic
631
    st_cnt_next_proc:       st_cnt_next <= st_cnt_reg + 1;
632
 
633
    -- bytes_i error logic
634 9 jdoin
    bytes_error_proc:       bytes_error_next <= '1' when bytes_i /= b"00" and end_i /= '1' and di_req = '1' and wr_i  = '1' else bytes_error_reg;
635 2 jdoin
 
636 9 jdoin
    -- data input error logic
637
    data_input_error_proc:  data_input_error <= '1' when wr_i = '1' and di_wr_window /= '1' else '0';
638
 
639 2 jdoin
    -- error detection logic
640 9 jdoin
    out_error_combi_proc:   out_error <= '1' when error_i = '1' or core_error = '1' or bytes_error_reg = '1' or data_input_error = '1' else '0';
641 2 jdoin
 
642
    --=============================================================================================
643
    --  OUTPUT LOGIC PROCESSES
644
    --=============================================================================================
645
 
646
    bitlen_o_proc :         bitlen_o        <= std_logic_vector(msg_bit_cnt_reg);
647
    bytes_ena_o_proc :      bytes_ena_o     <= bytes_ena;
648
    one_insert_o_proc :     one_insert_o    <= one_insert;
649
    words_sel_o_proc :      words_sel_o     <= words_sel;
650
    sch_ce_o_proc :         sch_ce_o        <= sch_ce;
651
    sch_ld_o_proc :         sch_ld_o        <= sch_ld;
652
    core_ce_o_proc :        core_ce_o       <= core_ce;
653
    core_ld_o_proc :        core_ld_o       <= core_ld;
654
    oregs_ce_o_proc :       oregs_ce_o      <= oregs_ce;
655
    oregs_ld_o_proc :       oregs_ld_o      <= oregs_ld;
656
    Kt_addr_o_proc :        Kt_addr_o       <= std_logic_vector(st_cnt_reg(5 downto 0));
657
    di_req_o_proc :         di_req_o        <= di_req;
658
    data_valid_o_proc :     data_valid_o    <= data_valid;
659
    error_o_proc :          error_o         <= out_error;
660
 
661
end rtl;
662
 

powered by: WebSVN 2.1.0

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