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

Subversion Repositories sha256_hash_core

[/] [sha256_hash_core/] [trunk/] [syn/] [sha256/] [gv_sha256.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:     01:21:32 06/30/2011 
5
-- Design Name:     gv_sha256
6
-- Module Name:     GV_SHA256 toplevel
7
-- Project Name:    GV_SHA256 engine
8
-- Target Devices:  Spartan-6 LX45
9
-- Tool versions:   ISE 14.7
10
-- Description: 
11
--
12
--      This is the gv_sha256 engine top level.
13
--      The gv_sha256 is a stream hash engine, i.e., the data words are hashed as a stream of words read from an input bus, with
14
--      control inputs for BEGIN/END of the message data stream. The input bus is a 32bit word bus, with a byte lane selector to signalize
15
--      how many bytes are valid in the last word. 
16
--
17
--      It is a structural integration of the logic blocks for the SHA256 engine, with the internal datapath and controlpath wires.
18
--
19
--      Written in synthesizable VHDL, the hash engine is a low resource, area-efficient implementation of the FIPS-180-4 SHA256 hash algorithm.
20
--      Designed around the core registers and combinational hash functions as a 768bit-wide engine, the engine takes 64+1 clocks to 
21
--      compute a hash block.
22
--
23
--      It is designed for stand-alone ASIC functions and 32-bit bus interfaces for generic processor integration.
24
--
25
--      The data input port is organized as a 32bit word write register, with flow control and begin/end signals.
26
--      The 256bit result register is organized as 8 x 32bit registers that can be read simultaneously.
27
--
28
--      This implementation is a conservative implementation of the approved FIPS-180-4 algorithm, with a fair compromise of resources, 
29
--      comprising of only 32 registers of 32bit words for the hash engine, with a single-cycle combinational logic for each algorithm step.
30
--      The combinational logic depth of the engine is 10 logic layers. For a process with 650ps of average (Tpd + Tsu), this core can
31
--      be synthesized to 75MHz system clock. 
32
--
33
--      The GV_SHA256 is a basic cryptographic block, used by almost all encryption and digital signature schemes. 
34
--
35
--      Applications include low-cost CyberPhysical Systems and also fast backend crypto functions for realtime hashing of packet data.
36
--      It is used in the GridVortex CyberSec IP, as a base for the fused HMAC-SHA256, HKDF, HMAC-SHA256-DRBG, and the SP-800 TRNG Entropy Source.
37
--
38
------------------------------ COPYRIGHT NOTICE -----------------------------------------------------------------------
39
--                                                                   
40
--                                                                   
41
--      Author(s):      Jonny Doin, jonnydoin@gridvortex.com, jonnydoin@gmail.com
42
--                                                                   
43
--      Copyright (C) 2016 GridVortex, All Rights Reserved
44
--      --------------------------------------------------
45
--                                                                   
46
------------------------------ REVISION HISTORY -----------------------------------------------------------------------
47
--
48
-- 2016/05/22   v0.01.0010  [JD]    started development. design of blocks and port interfaces.
49
-- 2016/06/05   v0.01.0090  [JD]    all modules integrated. testbench for basic test vectors verification.
50
-- 2016/06/05   v0.01.0095  [JD]    verification failed. misalignment of words in the datapath. 
51
-- 2016/06/06   v0.01.0100  [JD]    first simulation verification against NIST-FIPS-180-4 test vectors "abc" passed.
52
-- 2016/06/07   v0.01.0105  [JD]    verification against all NIST-FIPS-180-4 test vectors passed.
53
-- 2016/06/11   v0.01.0105  [JD]    verification against NIST-SHA2_Additional test vectors #1 to #10 passed.
54
-- 2016/06/11   v0.01.0110  [JD]    optimized controller states, reduced 2 clocks per block, added lookahead register feedback.
55
--
56
--
57
-----------------------------------------------------------------------------------------------------------------------
58
library ieee;
59
use ieee.std_logic_1164.all;
60
use ieee.numeric_std.all;
61
 
62
 
63
entity gv_sha256 is
64
    port (
65
        -- clock and core enable
66
        clk_i : in std_logic := 'U';                                    -- system clock
67
        ce_i : in std_logic := 'U';                                     -- core clock enable
68
        -- input data
69
        di_i : in std_logic_vector (31 downto 0) := (others => 'U');    -- big endian input message words
70
        bytes_i : in std_logic_vector (1 downto 0) := (others => 'U');  -- valid bytes in input word
71
        -- start/end commands
72
        start_i : in std_logic := 'U';                                  -- reset the engine and start a new hash
73
        end_i : in std_logic := 'U';                                    -- marks end of last block data input
74
        -- handshake
75
        di_req_o : out std_logic;                                       -- requests data input for next word
76
        di_ack_i : in std_logic := 'U';                                 -- high for di_i valid, low for hold
77
        error_o : out std_logic;                                        -- signalizes error. output data is invalid
78
        do_valid_o : out std_logic;                                     -- when high, the output is valid
79
        -- 256bit output registers
80
        H0_o : out std_logic_vector (31 downto 0);
81
        H1_o : out std_logic_vector (31 downto 0);
82
        H2_o : out std_logic_vector (31 downto 0);
83
        H3_o : out std_logic_vector (31 downto 0);
84
        H4_o : out std_logic_vector (31 downto 0);
85
        H5_o : out std_logic_vector (31 downto 0);
86
        H6_o : out std_logic_vector (31 downto 0);
87
        H7_o : out std_logic_vector (31 downto 0)
88
    );
89
end gv_sha256;
90
 
91
architecture rtl of gv_sha256 is
92
    -- internal register data values
93
    signal R0_data : std_logic_vector (31 downto 0);
94
    signal R1_data : std_logic_vector (31 downto 0);
95
    signal R2_data : std_logic_vector (31 downto 0);
96
    signal R3_data : std_logic_vector (31 downto 0);
97
    signal R4_data : std_logic_vector (31 downto 0);
98
    signal R5_data : std_logic_vector (31 downto 0);
99
    signal R6_data : std_logic_vector (31 downto 0);
100
    signal R7_data : std_logic_vector (31 downto 0);
101
    -- initial hash data values
102
    signal K0_data : std_logic_vector (31 downto 0);
103
    signal K1_data : std_logic_vector (31 downto 0);
104
    signal K2_data : std_logic_vector (31 downto 0);
105
    signal K3_data : std_logic_vector (31 downto 0);
106
    signal K4_data : std_logic_vector (31 downto 0);
107
    signal K5_data : std_logic_vector (31 downto 0);
108
    signal K6_data : std_logic_vector (31 downto 0);
109
    signal K7_data : std_logic_vector (31 downto 0);
110
    -- hash result lookahead port
111
    signal N0_data : std_logic_vector (31 downto 0);
112
    signal N1_data : std_logic_vector (31 downto 0);
113
    signal N2_data : std_logic_vector (31 downto 0);
114
    signal N3_data : std_logic_vector (31 downto 0);
115
    signal N4_data : std_logic_vector (31 downto 0);
116
    signal N5_data : std_logic_vector (31 downto 0);
117
    signal N6_data : std_logic_vector (31 downto 0);
118
    signal N7_data : std_logic_vector (31 downto 0);
119
    -- hash result data
120
    signal H0_data : std_logic_vector (31 downto 0);
121
    signal H1_data : std_logic_vector (31 downto 0);
122
    signal H2_data : std_logic_vector (31 downto 0);
123
    signal H3_data : std_logic_vector (31 downto 0);
124
    signal H4_data : std_logic_vector (31 downto 0);
125
    signal H5_data : std_logic_vector (31 downto 0);
126
    signal H6_data : std_logic_vector (31 downto 0);
127
    signal H7_data : std_logic_vector (31 downto 0);
128
    -- message schedule word datapath
129
    signal Mi_data : std_logic_vector (31 downto 0);
130
    signal Wt_data : std_logic_vector (31 downto 0);
131
    -- coefficients ROMs
132
    signal Kt_data : std_logic_vector (31 downto 0);
133
    signal Kt_addr : std_logic_vector (5 downto 0);
134
    -- padding control
135
    signal words_sel : std_logic_vector (1 downto 0);
136
    signal bytes_ena : std_logic_vector (3 downto 0);
137
    signal one_insert : std_logic;
138
    signal msg_bitlen : std_logic_vector (63 downto 0);
139
    signal pad_data : std_logic_vector (31 downto 0);
140
    -- block mux selectors
141
    signal sch_ld : std_logic;
142
    signal core_ld : std_logic;
143
    signal oregs_ld : std_logic;
144
    -- block clock enables
145
    signal sch_ce : std_logic;
146
    signal core_ce : std_logic;
147
    signal oregs_ce : std_logic;
148
    -- output data valid / error
149
    signal data_valid : std_logic;
150
    signal error_pad : std_logic;
151
    signal error_ctrl : std_logic;
152
begin
153
    --=============================================================================================
154
    -- INTERNAL COMPONENT INSTANTIATIONS AND CONNECTIONS
155
    --=============================================================================================
156
 
157
    -- control path core logic
158
    Inst_sha256_control: entity work.sha256_control(rtl)
159
        port map(
160
            -- inputs
161
            clk_i           => clk_i,
162
            ce_i            => ce_i,
163
            bytes_i         => bytes_i,
164
            ack_i           => di_ack_i,
165
            start_i         => start_i,
166
            end_i           => end_i,
167
            error_i         => error_pad,
168
            -- output control signals
169
            bitlen_o        => msg_bitlen,
170
            words_sel_o     => words_sel,
171
            Kt_addr_o       => Kt_addr,
172
            sch_ld_o        => sch_ld,
173
            core_ld_o       => core_ld,
174
            oregs_ld_o      => oregs_ld,
175
            sch_ce_o        => sch_ce,
176
            core_ce_o       => core_ce,
177
            oregs_ce_o      => oregs_ce,
178
            one_insert_o    => one_insert,
179
            bytes_ena_o     => bytes_ena,
180
            di_req_o        => di_req_o,
181
            data_valid_o    => data_valid,
182
            error_o         => error_ctrl
183
        );
184
 
185
    -- datapath: sha256 byte padding
186
    Inst_sha256_padding: entity work.sha256_padding(rtl)
187
        port map(
188
            words_sel_i     => words_sel,
189
            one_insert_i    => one_insert,
190
            bytes_ena_i     => bytes_ena,
191
            bitlen_i        => msg_bitlen,
192
            di_i            => di_i,
193
            do_o            => Mi_data,
194
            error_o         => error_pad
195
        );
196
 
197
    -- datapath: sha256 message schedule
198
    Inst_sha256_msg_sch: entity work.sha256_msg_sch(rtl)
199
        port map(
200
            clk_i => clk_i,
201
            ce_i  => sch_ce,
202
            ld_i  => sch_ld,
203
            M_i   => Mi_data,
204
            Wt_o  => Wt_data
205
        );
206
 
207
    -- datapath: sha256 core logic
208
    Inst_sha256_hash_core: entity work.sha256_hash_core(rtl)
209
        port map(
210
            clk_i => clk_i,
211
            ce_i  => core_ce,
212
            ld_i  => core_ld,
213
            -- initial hash data values 
214
            A_i => N0_data,
215
            B_i => N1_data,
216
            C_i => N2_data,
217
            D_i => N3_data,
218
            E_i => N4_data,
219
            F_i => N5_data,
220
            G_i => N6_data,
221
            H_i => N7_data,
222
            -- block hash values
223
            A_o => R0_data,
224
            B_o => R1_data,
225
            C_o => R2_data,
226
            D_o => R3_data,
227
            E_o => R4_data,
228
            F_o => R5_data,
229
            G_o => R6_data,
230
            H_o => R7_data,
231
            -- key coefficients 
232
            Kt_i => Kt_data,
233
            -- message schedule word input
234
            Wt_i => Wt_data
235
        );
236
 
237
    -- datapath: sha256 output registers
238
    Inst_sha256_regs: entity work.sha256_regs(rtl)
239
        port map(
240
            clk_i => clk_i,
241
            ce_i  => oregs_ce,
242
            ld_i =>  oregs_ld,
243
            -- register data from the core logic
244
            A_i => R0_data,
245
            B_i => R1_data,
246
            C_i => R2_data,
247
            D_i => R3_data,
248
            E_i => R4_data,
249
            F_i => R5_data,
250
            G_i => R6_data,
251
            H_i => R7_data,
252
            -- initial hash values
253
            K0_i => K0_data,
254
            K1_i => K1_data,
255
            K2_i => K2_data,
256
            K3_i => K3_data,
257
            K4_i => K4_data,
258
            K5_i => K5_data,
259
            K6_i => K6_data,
260
            K7_i => K7_data,
261
            -- lookahead output hash values, one pipeline advanced
262
            N0_o => N0_data,
263
            N1_o => N1_data,
264
            N2_o => N2_data,
265
            N3_o => N3_data,
266
            N4_o => N4_data,
267
            N5_o => N5_data,
268
            N6_o => N6_data,
269
            N7_o => N7_data,
270
            -- output hash values
271
            H0_o => H0_data,
272
            H1_o => H1_data,
273
            H2_o => H2_data,
274
            H3_o => H3_data,
275
            H4_o => H4_data,
276
            H5_o => H5_data,
277
            H6_o => H6_data,
278
            H7_o => H7_data
279
        );
280
 
281
    -- coefficients ROM: modelled as an asynchronously addressable ROM
282
    Inst_sha256_kt_rom: entity work.sha256_kt_rom(behavioral)
283
        port map(
284
            addr_i => Kt_addr,
285
            dout_o => Kt_data
286
        );
287
 
288
    -- init output data ROM: modelled as a statically defined constant
289
    Inst_sha256_ki_rom: entity work.sha256_ki_rom(behavioral)
290
        port map(
291
            K0_o => K0_data,
292
            K1_o => K1_data,
293
            K2_o => K2_data,
294
            K3_o => K3_data,
295
            K4_o => K4_data,
296
            K5_o => K5_data,
297
            K6_o => K6_data,
298
            K7_o => K7_data
299
        );
300
 
301
    --=============================================================================================
302
    --  OUTPUTS LOGIC
303
    --=============================================================================================
304
 
305
    error_o_proc:       error_o <= error_ctrl;
306
    do_valid_o_proc:    do_valid_o <= data_valid;
307
    H0_o_proc:          H0_o <= H0_data;
308
    H1_o_proc:          H1_o <= H1_data;
309
    H2_o_proc:          H2_o <= H2_data;
310
    H3_o_proc:          H3_o <= H3_data;
311
    H4_o_proc:          H4_o <= H4_data;
312
    H5_o_proc:          H5_o <= H5_data;
313
    H6_o_proc:          H6_o <= H6_data;
314
    H7_o_proc:          H7_o <= H7_data;
315
 
316
end rtl;
317
 

powered by: WebSVN 2.1.0

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