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 2

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

Line No. Rev Author Line
1 2 jdoin
-----------------------------------------------------------------------------------------------------------------------
2
-- Author:          Jonny Doin, jdoin@opencores.org, jonnydoin@gmail.com
3
-- 
4
-- Create Date:     09:56:30 07/06/2011  
5
-- 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
--      The data input accepts 16 consecutive 32bit words for a total of 64 bytes per block, one word per clock cycle. The input signal 'ack_i' can be
47
--      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
--      ack_i      __________/____/                                      \_____/                                                         \_____________...     -- 'ack_i' can hold the core for slow data
76
--                 __________ _________ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ ______ ______________...
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
--      ack_i      ________/__________/                                        \_____/                                                         \_...        -- 'ack_i' can hold the core for slow data
104
--                 _________________ _ ______ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ _____ _____ ____...
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
--      ack_i      ________________ _ _ ______/                                                  \_____/                                             \_...     -- 'ack_i' valid on rising edge of 'clk_i'
122
--                 ________________ _ _ ___________ _____ _____ _____ _____ _____ _____ _____ ___________ _____ _____ _____ _____ _____ _____ _____ ____...
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
--      ack_i      _________/                                    \\\______ _ _ ___________________ _ _ _________________________________/         ...     -- 'ack_i' can hold the core for slow data
145
--                 ______________ _____ _____ _____ _____ _____ __________ _ _ ___________________ _ _ ______________________________________ ____...
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
--      ack_i                        \\\_______ _ _ ___________________ _ _ _____________ _ _ ___________________________/         ...     -- 'ack_i' valid on rising edge of 'clk_i'
190
--                 _____ _____ _____ __________ _ _ ___________________ _ _ _____________ _ _ ________________________________ ____...
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
--                                                                   
202
--                                                                   
203
--      Author(s):      Jonny Doin, jonnydoin@gridvortex.com, jonnydoin@gmail.com
204
--                                                                   
205
--      Copyright (C) 2016 GridVortex, All Rights Reserved
206
--      --------------------------------------------------
207
--                                                                   
208
------------------------------ REVISION HISTORY -----------------------------------------------------------------------
209
--
210
-- 2016/05/22   v0.01.0010  [JD]    started development. design of blocks and port interfaces.
211
-- 2016/06/05   v0.01.0090  [JD]    all modules integrated. testbench for basic test vectors verification.
212
-- 2016/06/05   v0.01.0095  [JD]    verification failed. misalignment of words in the datapath. 
213
-- 2016/06/06   v0.01.0100  [JD]    first simulation verification against NIST-FIPS-180-4 test vectors "abc" passed.
214
-- 2016/06/07   v0.01.0101  [JD]    failed 2-block test for "abcdbcdecd..." vector. Fixed padding control logic.
215
-- 2016/06/07   v0.01.0105  [JD]    verification against all NIST-FIPS-180-4 test vectors passed.
216
-- 2016/06/11   v0.01.0105  [JD]    verification against NIST-SHA2_Additional test vectors #1 to #10 passed.
217
-- 2016/06/11   v0.01.0110  [JD]    optimized controller states, reduced 2 clocks per block. 
218
-- 2016/06/18   v0.01.0120  [JD]    implemented error detection on 'bytes_i' input.
219
-- 2016/07/06   v0.01.0210  [JD]    optimized suspend logic on 'sch_ld' to supress possible glitch in 'pad_one_next'.
220
--
221
-----------------------------------------------------------------------------------------------------------------------
222
--  TODO
223
--  ====
224
--
225
--
226
-----------------------------------------------------------------------------------------------------------------------
227
library ieee;
228
use ieee.std_logic_1164.all;
229
use ieee.numeric_std.all;
230
 
231
entity sha256_control is
232
    port (
233
        -- inputs
234
        clk_i : in std_logic := 'U';                                    -- system clock
235
        ce_i : in std_logic := 'U';                                     -- core clock enable
236
        start_i : in std_logic := 'U';                                  -- reset the processor and start a new hash
237
        end_i : in std_logic := 'U';                                    -- marks end of last block data input
238
        ack_i : in std_logic := 'U';                                    -- input word hold control
239
        bytes_i : in std_logic_vector (1 downto 0) := (others => 'U');  -- valid bytes in input word
240
        error_i : in std_logic := 'U';                                  -- datapath error input from other modules
241
        -- output control signals
242
        bitlen_o : out std_logic_vector (63 downto 0);                  -- message bit length
243
        words_sel_o : out std_logic_vector (1 downto 0);                -- bitlen insertion control
244
        Kt_addr_o : out std_logic_vector (5 downto 0);                  -- address for the Kt coefficients ROM
245
        sch_ld_o : out std_logic;                                       -- load/recirculate words for message scheduler
246
        core_ld_o : out std_logic;                                      -- load all registers for hash core
247
        oregs_ld_o : out std_logic;                                     -- load output registers
248
        sch_ce_o : out std_logic;                                       -- clock enable for message scheduler logic block
249
        core_ce_o : out std_logic;                                      -- clock enable for hash core logic block
250
        oregs_ce_o : out std_logic;                                     -- clock enable for output regs logic block
251
        bytes_ena_o : out std_logic_vector (3 downto 0);                -- byte lane selectors for padding logic block
252
        one_insert_o : out std_logic;                                   -- insert leading '1' in the padding
253
        di_req_o : out std_logic;                                       -- external data request by the 'di_i' port
254
        data_valid_o : out std_logic;                                   -- operation finished. output data is valid
255
        error_o : out std_logic                                         -- operation aborted. output data is not valid
256
    );
257
end sha256_control;
258
 
259
architecture rtl of sha256_control is
260
    --=============================================================================================
261
    -- Type definitions
262
    --=============================================================================================
263
    -- controller states
264
    type hash_toplevel_control is
265
            (   st_reset,                   -- core reset, initial state
266
                st_sha_data_input,          -- sha data input
267
                st_sha_blk_process,         -- sha block process
268
                st_sha_blk_nxt,             -- sha block next
269
                st_sha_padding,             -- sha padding    
270
                st_sha_data_valid,          -- sha data valid
271
                st_error                    -- fsm locks on error, exit only by reset
272
            );
273
 
274
    --=============================================================================================
275
    -- Signals for state machine control
276
    --=============================================================================================
277
    signal hash_control_st_reg  : hash_toplevel_control := st_reset;
278
    signal hash_control_st_next : hash_toplevel_control := st_reset;
279
 
280
    --=============================================================================================
281
    -- Signals for internal operation
282
    --=============================================================================================
283
    -- combinational flags: message data input / padding control / block internal process selection
284
    signal reset : std_logic;
285
    signal sha_reset : std_logic;
286
    signal sha_init : std_logic;
287
    signal wait_run_ce : std_logic;
288
    -- registered flags: last block, padding control and hmac processing
289
    signal sha_last_blk_reg : std_logic;
290
    signal sha_last_blk_next : std_logic;
291
    signal padding_reg : std_logic;
292
    signal padding_next : std_logic;
293
    signal pad_one_reg : std_logic;
294
    signal pad_one_next : std_logic;
295
    signal bytes_error_reg : std_logic;
296
    signal bytes_error_next : std_logic;
297
    -- 64 bit message bit counter
298
    signal msg_bit_cnt_reg : unsigned (63 downto 0);
299
    signal msg_bit_cnt_next : unsigned (63 downto 0);
300
    signal bits_to_add : unsigned (5 downto 0);
301
    signal msg_bit_cnt_ce : std_logic;
302
    -- sequencer state counter
303
    signal st_cnt_reg : unsigned (6 downto 0);
304
    signal st_cnt_next : unsigned (6 downto 0);
305
    signal st_cnt_ce : std_logic;
306
    signal st_cnt_clr : std_logic;
307
 
308
    --=============================================================================================
309
    -- Output Control Signals
310
    --=============================================================================================
311
    -- unregistered control signals
312
    signal words_sel : std_logic_vector (1 downto 0);   -- bitlen insertion control
313
    signal sch_ld : std_logic;                          -- input data load into message scheduler control
314
    signal core_ld : std_logic;                         -- hash core load data registers control
315
    signal oregs_ld : std_logic;                        -- load initial value into output regs control
316
    signal sch_ce : std_logic;                          -- clock enable for message scheduler logic block
317
    signal core_ce : std_logic;                         -- clock enable for hash core logic block
318
    signal oregs_ce : std_logic;                        -- clock enable for output regs logic block
319
    signal bytes_ena : std_logic_vector (3 downto 0);   -- byte lane selectors for padding logic block
320
    signal one_insert : std_logic;                      -- insert leading one in the padding
321
    signal di_req : std_logic;                          -- data request
322
    signal data_valid : std_logic;                      -- operation finished. output data is valid
323
    signal core_error : std_logic;                      -- operation aborted. output data is not valid
324
    signal out_error : std_logic;                       -- operation aborted. output data is not valid
325
 
326
begin
327
    --=============================================================================================
328
    --  REGISTER TRANSFER PROCESSES
329
    --=============================================================================================
330
    -- control fsm register transfer logic
331
    control_fsm_proc: process (clk_i) is
332
    begin
333
        -- FSM state register: sync RESET on 'reset', and sync PRESET on error_i
334
        if clk_i'event and clk_i = '1' then
335
            if reset = '1' then
336
                -- all registered values are reset on master clear
337
                hash_control_st_reg <= st_reset;
338
            elsif out_error = '1' then
339
                hash_control_st_reg <= st_error;
340
            elsif ce_i = '1' then
341
                -- all registered values are held on master clock enable
342
                hash_control_st_reg <= hash_control_st_next;
343
            end if;
344
        end if;
345
        -- SHA256 registers, RESET on 'sha_init'
346
        if clk_i'event and clk_i = '1' then
347
            if sha_init = '1' then
348
                -- all SHA256 registered values are reset on SHA master clear
349
                sha_last_blk_reg <= '0';
350
                padding_reg <= '0';
351
            elsif ce_i = '1' then
352
                -- all registered values are held on master clock enable
353
                sha_last_blk_reg <= sha_last_blk_next;
354
                padding_reg <= padding_next;
355
            end if;
356
        end if;
357
        -- bytes_i error register: sync RESET on 'reset'
358
        if clk_i'event and clk_i = '1' then
359
            if reset = '1' then
360
                -- all registered values are reset on master clear
361
                bytes_error_reg <= '0';
362
            else
363
                -- all registered values are held on master clock enable
364
                bytes_error_reg <= bytes_error_next;
365
            end if;
366
        end if;
367
    end process control_fsm_proc;
368
 
369
    -- bit counter register transfer logic
370
    bit_counter_proc: process (clk_i) is
371
    begin
372
        -- bit counter
373
        if clk_i'event and clk_i = '1' then
374
            if sha_init = '1' then
375
                msg_bit_cnt_reg <= (others => '0');
376
            elsif ce_i = '1' and msg_bit_cnt_ce = '1' then
377
                msg_bit_cnt_reg <= msg_bit_cnt_next;
378
            end if;
379
        end if;
380
    end process bit_counter_proc;
381
 
382
    -- state counter register transfer process
383
    state_counter_proc: process (clk_i) is
384
    begin
385
        -- core state counter
386
        if clk_i'event and clk_i = '1' then
387
            if (sha_init = '1') or (st_cnt_clr = '1') then
388
                st_cnt_reg <= (others => '0');
389
            elsif (ce_i = '1') and (st_cnt_ce = '1') then
390
                st_cnt_reg <= st_cnt_next;
391
            end if;
392
        end if;
393
    end process state_counter_proc;
394
 
395
    -- one-padding register transfer logic
396
    pad_one_fsm_proc: process (clk_i) is
397
    begin
398
        if clk_i'event and clk_i = '1' then
399
            if sha_init = '1' then
400
                -- all registered values are reset on master clear
401
                pad_one_reg <= '1';
402
            elsif (ce_i = '1') and (sch_ce = '1') then
403
                -- one-padding register is clocked synchronous with the message schedule
404
                pad_one_reg <= pad_one_next;
405
            end if;
406
        end if;
407
    end process pad_one_fsm_proc;
408
 
409
    --=============================================================================================
410
    --  COMBINATIONAL NEXT-STATE LOGIC
411
    --=============================================================================================
412
    -- State and control path combinational logic
413
    -- The hash_control_st_reg state register controls the SHA256 algorithm.
414
    control_combi_proc : process (  hash_control_st_reg, sha_last_blk_reg, padding_reg, wait_run_ce,
415
                                    end_i, st_cnt_reg, sha_last_blk_next, one_insert, sha_reset ) is
416
    begin
417
        -- default logic that applies to all states at each fsm clock --
418
 
419
        -- assign default values to all unchanging combinational outputs (avoid latches)
420
        hash_control_st_next <= hash_control_st_reg;
421
        sha_last_blk_next <= sha_last_blk_reg;
422
        padding_next <= padding_reg;
423
        -- handshaking
424
        sha_init <= '0';
425
        core_error <= '0';
426
        words_sel <= b"00";
427
        data_valid <= '0';
428
        di_req <= '0';              -- data request only during data input
429
        -- state counter
430
        st_cnt_clr <= '0';          -- only clear the state counter at the beginning of each block
431
        st_cnt_ce <= '0';
432
        -- message scheduler
433
        sch_ld <= '1';              -- enable pass-thru input through message schedule
434
        sch_ce <= '0';              -- stop message schedule clock
435
        -- hash core
436
        core_ld <= '0';             -- enable internal hash core logic
437
        core_ce <= '0';             -- core computation enabled only for data input and processing
438
        -- output registers
439
        oregs_ld <= '0';            -- defaults for accumulate blk hash
440
        oregs_ce <= '0';            -- only register init values and end of computation
441
        case hash_control_st_reg is
442
 
443
            when st_reset =>                -- master reset: starts a new hash/hmac processing
444
                -- moore outputs
445
                sha_init <= '1';            -- reset SHA256 engine
446
                oregs_ld <= '1';            -- load initial hash values
447
                oregs_ce <= '1';            -- latch initial hash values into output registers
448
                core_ld <= '1';             -- load initial value into core registers
449
                core_ce <= '1';             -- latch initial value into core registers
450
                st_cnt_clr <= '1';          -- reset state counter
451
                -- next state
452
                hash_control_st_next <= st_sha_data_input;
453
 
454
            when st_sha_data_input =>       -- message data words are clocked into the processor
455
                -- moore outputs
456
                di_req <= '1';              -- request message data
457
                sch_ce <= wait_run_ce;      -- hold the message scheduler with data hold
458
                st_cnt_ce <= wait_run_ce;   -- hold state count with data hold
459
                core_ce <= wait_run_ce;     -- hold processing clock with data hold
460
                -- next state
461
                if wait_run_ce = '1' then
462
                    if end_i = '1' then
463
                        hash_control_st_next <= st_sha_padding;     -- pad incomplete blocks
464
                    elsif st_cnt_reg = 15 then
465
                        hash_control_st_next <= st_sha_blk_process; -- process one more block
466
                    end if;
467
                end if;
468
 
469
            when st_sha_blk_process =>      -- internal block hash processing
470
                -- moore outputs
471
                st_cnt_ce <= '1';           -- enable state counter
472
                sch_ld <= '0';              -- recirculate scheduler data
473
                sch_ce <= '1';              -- enable message scheduler clock
474
                core_ce <= '1';             -- enable processing clock
475
                -- next state
476
                if st_cnt_reg = 63 then
477
                    hash_control_st_next <= st_sha_blk_nxt;
478
                end if;
479
 
480
            when st_sha_blk_nxt =>          -- prepare for next block
481
                -- moore outputs
482
                st_cnt_clr <= '1';          -- reset state counter at the beginning of each block
483
                sch_ld <= '0';
484
                sch_ce <= '0';              -- stop the message schedule
485
                core_ld <= '1';             -- load previous result value into core registers
486
                core_ce <= '1';             -- latch result value into core registers
487
                oregs_ce <= '1';            -- latch core result into regs accumulator
488
                -- next state
489
                if sha_last_blk_reg = '1' then
490
                    hash_control_st_next <= st_sha_data_valid;  -- no hmac operation: publish data valid
491
                elsif padding_reg = '1' then
492
                    hash_control_st_next <= st_sha_padding;     -- additional padding block
493
                else
494
                    hash_control_st_next <= st_sha_data_input;  -- continue requesting input data
495
                end if;
496
 
497
            when st_sha_padding =>          -- padding of bits on the last message block
498
                -- moore outputs                
499
                padding_next <= '1';
500
                if st_cnt_reg = 16 then     -- if word 16, data block was full: proceed to process this block
501
                    -- pause processing for this cycle
502
                    sch_ld <= '0';
503
                    sch_ce <= '0';
504
                    core_ce <= '0';
505
                    st_cnt_ce <= '0';
506
                    -- next state
507
                    hash_control_st_next <= st_sha_blk_process;
508
                else                        -- incomplete block: pad words until data block completes
509
                    sch_ld <= '1';          -- load padded data into scheduler
510
                    sch_ce <= '1';          -- enable message scheduler clock
511
                    core_ce <= '1';         -- enable processing clock
512
                    st_cnt_ce <= '1';       -- enable state counter
513
                    if st_cnt_reg = 15 then -- pad up to word 15
514
                        if sha_last_blk_next = '1' then
515
                            words_sel <= b"10";  -- insert bitlen lo
516
                        end if;
517
                        -- next state
518
                        hash_control_st_next <= st_sha_blk_process;
519
                    elsif (one_insert = '0') and (st_cnt_reg = 14) then
520
                        words_sel <= b"01";     -- insert bitlen hi
521
                        sha_last_blk_next <= '1';   -- mark this as the last block
522
                    elsif st_cnt_reg = 13 then
523
                        sha_last_blk_next <= '1';   -- mark this as the last block
524
                    end if;
525
                end if;
526
 
527
            when st_sha_data_valid =>       -- process is finished, waiting for begin command
528
                -- moore outputs
529
                data_valid <= '1';          -- output results are valid
530
                -- wait for core reset with 'reset'               
531
 
532
            when st_error =>                -- processing or input error: reset with 'reset' = 1
533
                -- moore outputs
534
                core_error <= '1';
535
                st_cnt_clr <= '1';          -- clear state counter
536
                -- wait for core reset with 'reset'               
537
 
538
            when others =>                  -- internal state machine error
539
                -- next state
540
                hash_control_st_next <= st_error;
541
 
542
        end case;
543
    end process control_combi_proc;
544
 
545
 
546
    --=============================================================================================
547
    --  COMBINATIONAL CONTROL LOGIC
548
    --=============================================================================================
549
 
550
    -- controller RESET signal logic
551
    sha_reset_combi_proc:   sha_reset <= '1' when start_i = '1'   else '0';
552
    reset_combi_proc:       reset <= '1'     when sha_reset = '1' else '0';
553
 
554
    -- pad-one flag register
555
    pad_one_next_combi_proc: process (bytes_ena, sch_ld, pad_one_reg) is
556
    begin
557
        -- after one-insertion, clear the pad-one flag register 
558
        if (bytes_ena /= b"1111") and (sch_ld = '1') then
559
            pad_one_next <= '0';
560
        else
561
            pad_one_next <= pad_one_reg;
562
        end if;
563
    end process pad_one_next_combi_proc;
564
 
565
    -- padding byte lane selectors
566
    bytes_ena_combi_proc: process (bytes_i, padding_next, di_req, one_insert, end_i) is
567
    begin
568
        if di_req = '1' and end_i /= '1' then
569
            -- accept only full words before last word
570
            bytes_ena <= b"1111";
571
        elsif di_req = '1' and end_i = '1' then
572
            -- user data: bytes controlled by 'bytes_i'
573
            case bytes_i is
574
                when b"01"  => bytes_ena <= b"0001";
575
                when b"10"  => bytes_ena <= b"0011";
576
                when b"11"  => bytes_ena <= b"0111";
577
                when others => bytes_ena <= b"1111";
578
            end case;
579
        else
580
            -- no data input: force zero bits valid
581
            bytes_ena <= b"0000";
582
        end if;
583
    end process bytes_ena_combi_proc;
584
 
585
    -- bit counter next logic
586
    msg_bit_cnt_next_combi_proc: process (bytes_ena, msg_bit_cnt_reg, bits_to_add) is
587
    begin
588
        case bytes_ena is
589
            when b"0001"    => bits_to_add <= to_unsigned( 8, 6);
590
            when b"0011"    => bits_to_add <= to_unsigned(16, 6);
591
            when b"0111"    => bits_to_add <= to_unsigned(24, 6);
592
            when b"1111"    => bits_to_add <= to_unsigned(32, 6);
593
            when others     => bits_to_add <= to_unsigned( 0, 6);
594
        end case;
595
        msg_bit_cnt_next <= msg_bit_cnt_reg + bits_to_add;
596
    end process msg_bit_cnt_next_combi_proc;
597
 
598
    -- data input wait/run: insert wait states during data input for 'ack_i' = '0'
599
    wait_run_proc:          wait_run_ce <= '1' when di_req = '1' and ack_i = '1' else '0';
600
 
601
    -- padding one-insertion control
602
    one_insert_proc:        one_insert <= '1' when pad_one_reg = '1' else '0';
603
 
604
    -- bit counter clock enable
605
    msg_bit_cnt_ce_proc :   msg_bit_cnt_ce <= '1' when wait_run_ce = '1' else '0';
606
 
607
    -- state counter next logic
608
    st_cnt_next_proc:       st_cnt_next <= st_cnt_reg + 1;
609
 
610
    -- bytes_i error logic
611
    bytes_error_proc:       bytes_error_next <= '1' when bytes_i /= b"00" and end_i /= '1' and di_req = '1' and ack_i = '1' else bytes_error_reg;
612
 
613
    -- error detection logic
614
    out_error_combi_proc:   out_error <= '1' when error_i = '1' or core_error = '1' or bytes_error_reg = '1' else '0';
615
 
616
    --=============================================================================================
617
    --  OUTPUT LOGIC PROCESSES
618
    --=============================================================================================
619
 
620
    bitlen_o_proc :         bitlen_o        <= std_logic_vector(msg_bit_cnt_reg);
621
    bytes_ena_o_proc :      bytes_ena_o     <= bytes_ena;
622
    one_insert_o_proc :     one_insert_o    <= one_insert;
623
    words_sel_o_proc :      words_sel_o     <= words_sel;
624
    sch_ce_o_proc :         sch_ce_o        <= sch_ce;
625
    sch_ld_o_proc :         sch_ld_o        <= sch_ld;
626
    core_ce_o_proc :        core_ce_o       <= core_ce;
627
    core_ld_o_proc :        core_ld_o       <= core_ld;
628
    oregs_ce_o_proc :       oregs_ce_o      <= oregs_ce;
629
    oregs_ld_o_proc :       oregs_ld_o      <= oregs_ld;
630
    Kt_addr_o_proc :        Kt_addr_o       <= std_logic_vector(st_cnt_reg(5 downto 0));
631
    di_req_o_proc :         di_req_o        <= di_req;
632
    data_valid_o_proc :     data_valid_o    <= data_valid;
633
    error_o_proc :          error_o         <= out_error;
634
 
635
end rtl;
636
 

powered by: WebSVN 2.1.0

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