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 6

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:     01:21:32 05/05/2016 
5 2 jdoin
-- 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 6 jdoin
--                                                                   
40
--      This file is part of the SHA256 HASH CORE project http://opencores.org/project,sha256_hash_core
41
--                                                                   
42
--      Author(s):      Jonny Doin, jdoin@opencores.org, jonnydoin@gridvortex.com, jonnydoin@gmail.com
43
--                                                                   
44
--      Copyright (C) 2016 Jonny Doin
45
--      -----------------------------
46
--                                                                   
47
--      This source file may be used and distributed without restriction provided that this copyright statement is not    
48
--      removed from the file and that any derivative work contains the original copyright notice and the associated 
49
--      disclaimer. 
50
--                                                                   
51
--      This source file is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
52
--      General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
53
--      (at your option) any later version.
54
--                                                                   
55
--      This source is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
56
--      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more  
57
--      details.
58
--
59
--      You should have received a copy of the GNU Lesser General Public License along with this source; if not, download 
60
--      it from http://www.gnu.org/licenses/lgpl.txt
61
--                                                                   
62 2 jdoin
------------------------------ REVISION HISTORY -----------------------------------------------------------------------
63
--
64
-- 2016/05/22   v0.01.0010  [JD]    started development. design of blocks and port interfaces.
65
-- 2016/06/05   v0.01.0090  [JD]    all modules integrated. testbench for basic test vectors verification.
66
-- 2016/06/05   v0.01.0095  [JD]    verification failed. misalignment of words in the datapath. 
67
-- 2016/06/06   v0.01.0100  [JD]    first simulation verification against NIST-FIPS-180-4 test vectors "abc" passed.
68
-- 2016/06/07   v0.01.0105  [JD]    verification against all NIST-FIPS-180-4 test vectors passed.
69
-- 2016/06/11   v0.01.0105  [JD]    verification against NIST-SHA2_Additional test vectors #1 to #10 passed.
70
-- 2016/06/11   v0.01.0110  [JD]    optimized controller states, reduced 2 clocks per block, added lookahead register feedback.
71
--
72
--
73
-----------------------------------------------------------------------------------------------------------------------
74
library ieee;
75
use ieee.std_logic_1164.all;
76
use ieee.numeric_std.all;
77
 
78
 
79
entity gv_sha256 is
80
    port (
81
        -- clock and core enable
82
        clk_i : in std_logic := 'U';                                    -- system clock
83
        ce_i : in std_logic := 'U';                                     -- core clock enable
84
        -- input data
85
        di_i : in std_logic_vector (31 downto 0) := (others => 'U');    -- big endian input message words
86
        bytes_i : in std_logic_vector (1 downto 0) := (others => 'U');  -- valid bytes in input word
87
        -- start/end commands
88
        start_i : in std_logic := 'U';                                  -- reset the engine and start a new hash
89
        end_i : in std_logic := 'U';                                    -- marks end of last block data input
90
        -- handshake
91
        di_req_o : out std_logic;                                       -- requests data input for next word
92
        di_ack_i : in std_logic := 'U';                                 -- high for di_i valid, low for hold
93
        error_o : out std_logic;                                        -- signalizes error. output data is invalid
94
        do_valid_o : out std_logic;                                     -- when high, the output is valid
95
        -- 256bit output registers
96
        H0_o : out std_logic_vector (31 downto 0);
97
        H1_o : out std_logic_vector (31 downto 0);
98
        H2_o : out std_logic_vector (31 downto 0);
99
        H3_o : out std_logic_vector (31 downto 0);
100
        H4_o : out std_logic_vector (31 downto 0);
101
        H5_o : out std_logic_vector (31 downto 0);
102
        H6_o : out std_logic_vector (31 downto 0);
103
        H7_o : out std_logic_vector (31 downto 0)
104
    );
105
end gv_sha256;
106
 
107
architecture rtl of gv_sha256 is
108
    -- internal register data values
109
    signal R0_data : std_logic_vector (31 downto 0);
110
    signal R1_data : std_logic_vector (31 downto 0);
111
    signal R2_data : std_logic_vector (31 downto 0);
112
    signal R3_data : std_logic_vector (31 downto 0);
113
    signal R4_data : std_logic_vector (31 downto 0);
114
    signal R5_data : std_logic_vector (31 downto 0);
115
    signal R6_data : std_logic_vector (31 downto 0);
116
    signal R7_data : std_logic_vector (31 downto 0);
117
    -- initial hash data values
118
    signal K0_data : std_logic_vector (31 downto 0);
119
    signal K1_data : std_logic_vector (31 downto 0);
120
    signal K2_data : std_logic_vector (31 downto 0);
121
    signal K3_data : std_logic_vector (31 downto 0);
122
    signal K4_data : std_logic_vector (31 downto 0);
123
    signal K5_data : std_logic_vector (31 downto 0);
124
    signal K6_data : std_logic_vector (31 downto 0);
125
    signal K7_data : std_logic_vector (31 downto 0);
126
    -- hash result lookahead port
127
    signal N0_data : std_logic_vector (31 downto 0);
128
    signal N1_data : std_logic_vector (31 downto 0);
129
    signal N2_data : std_logic_vector (31 downto 0);
130
    signal N3_data : std_logic_vector (31 downto 0);
131
    signal N4_data : std_logic_vector (31 downto 0);
132
    signal N5_data : std_logic_vector (31 downto 0);
133
    signal N6_data : std_logic_vector (31 downto 0);
134
    signal N7_data : std_logic_vector (31 downto 0);
135
    -- hash result data
136
    signal H0_data : std_logic_vector (31 downto 0);
137
    signal H1_data : std_logic_vector (31 downto 0);
138
    signal H2_data : std_logic_vector (31 downto 0);
139
    signal H3_data : std_logic_vector (31 downto 0);
140
    signal H4_data : std_logic_vector (31 downto 0);
141
    signal H5_data : std_logic_vector (31 downto 0);
142
    signal H6_data : std_logic_vector (31 downto 0);
143
    signal H7_data : std_logic_vector (31 downto 0);
144
    -- message schedule word datapath
145
    signal Mi_data : std_logic_vector (31 downto 0);
146
    signal Wt_data : std_logic_vector (31 downto 0);
147
    -- coefficients ROMs
148
    signal Kt_data : std_logic_vector (31 downto 0);
149
    signal Kt_addr : std_logic_vector (5 downto 0);
150
    -- padding control
151
    signal words_sel : std_logic_vector (1 downto 0);
152
    signal bytes_ena : std_logic_vector (3 downto 0);
153
    signal one_insert : std_logic;
154
    signal msg_bitlen : std_logic_vector (63 downto 0);
155
    signal pad_data : std_logic_vector (31 downto 0);
156
    -- block mux selectors
157
    signal sch_ld : std_logic;
158
    signal core_ld : std_logic;
159
    signal oregs_ld : std_logic;
160
    -- block clock enables
161
    signal sch_ce : std_logic;
162
    signal core_ce : std_logic;
163
    signal oregs_ce : std_logic;
164
    -- output data valid / error
165
    signal data_valid : std_logic;
166
    signal error_pad : std_logic;
167
    signal error_ctrl : std_logic;
168
begin
169
    --=============================================================================================
170
    -- INTERNAL COMPONENT INSTANTIATIONS AND CONNECTIONS
171
    --=============================================================================================
172
 
173
    -- control path core logic
174
    Inst_sha256_control: entity work.sha256_control(rtl)
175
        port map(
176
            -- inputs
177
            clk_i           => clk_i,
178
            ce_i            => ce_i,
179
            bytes_i         => bytes_i,
180
            ack_i           => di_ack_i,
181
            start_i         => start_i,
182
            end_i           => end_i,
183
            error_i         => error_pad,
184
            -- output control signals
185
            bitlen_o        => msg_bitlen,
186
            words_sel_o     => words_sel,
187
            Kt_addr_o       => Kt_addr,
188
            sch_ld_o        => sch_ld,
189
            core_ld_o       => core_ld,
190
            oregs_ld_o      => oregs_ld,
191
            sch_ce_o        => sch_ce,
192
            core_ce_o       => core_ce,
193
            oregs_ce_o      => oregs_ce,
194
            one_insert_o    => one_insert,
195
            bytes_ena_o     => bytes_ena,
196
            di_req_o        => di_req_o,
197
            data_valid_o    => data_valid,
198
            error_o         => error_ctrl
199
        );
200
 
201
    -- datapath: sha256 byte padding
202
    Inst_sha256_padding: entity work.sha256_padding(rtl)
203
        port map(
204
            words_sel_i     => words_sel,
205
            one_insert_i    => one_insert,
206
            bytes_ena_i     => bytes_ena,
207
            bitlen_i        => msg_bitlen,
208
            di_i            => di_i,
209
            do_o            => Mi_data,
210
            error_o         => error_pad
211
        );
212
 
213
    -- datapath: sha256 message schedule
214
    Inst_sha256_msg_sch: entity work.sha256_msg_sch(rtl)
215
        port map(
216
            clk_i => clk_i,
217
            ce_i  => sch_ce,
218
            ld_i  => sch_ld,
219
            M_i   => Mi_data,
220
            Wt_o  => Wt_data
221
        );
222
 
223
    -- datapath: sha256 core logic
224
    Inst_sha256_hash_core: entity work.sha256_hash_core(rtl)
225
        port map(
226
            clk_i => clk_i,
227
            ce_i  => core_ce,
228
            ld_i  => core_ld,
229
            -- initial hash data values 
230
            A_i => N0_data,
231
            B_i => N1_data,
232
            C_i => N2_data,
233
            D_i => N3_data,
234
            E_i => N4_data,
235
            F_i => N5_data,
236
            G_i => N6_data,
237
            H_i => N7_data,
238
            -- block hash values
239
            A_o => R0_data,
240
            B_o => R1_data,
241
            C_o => R2_data,
242
            D_o => R3_data,
243
            E_o => R4_data,
244
            F_o => R5_data,
245
            G_o => R6_data,
246
            H_o => R7_data,
247
            -- key coefficients 
248
            Kt_i => Kt_data,
249
            -- message schedule word input
250
            Wt_i => Wt_data
251
        );
252
 
253
    -- datapath: sha256 output registers
254
    Inst_sha256_regs: entity work.sha256_regs(rtl)
255
        port map(
256
            clk_i => clk_i,
257
            ce_i  => oregs_ce,
258
            ld_i =>  oregs_ld,
259
            -- register data from the core logic
260
            A_i => R0_data,
261
            B_i => R1_data,
262
            C_i => R2_data,
263
            D_i => R3_data,
264
            E_i => R4_data,
265
            F_i => R5_data,
266
            G_i => R6_data,
267
            H_i => R7_data,
268
            -- initial hash values
269
            K0_i => K0_data,
270
            K1_i => K1_data,
271
            K2_i => K2_data,
272
            K3_i => K3_data,
273
            K4_i => K4_data,
274
            K5_i => K5_data,
275
            K6_i => K6_data,
276
            K7_i => K7_data,
277
            -- lookahead output hash values, one pipeline advanced
278
            N0_o => N0_data,
279
            N1_o => N1_data,
280
            N2_o => N2_data,
281
            N3_o => N3_data,
282
            N4_o => N4_data,
283
            N5_o => N5_data,
284
            N6_o => N6_data,
285
            N7_o => N7_data,
286
            -- output hash values
287
            H0_o => H0_data,
288
            H1_o => H1_data,
289
            H2_o => H2_data,
290
            H3_o => H3_data,
291
            H4_o => H4_data,
292
            H5_o => H5_data,
293
            H6_o => H6_data,
294
            H7_o => H7_data
295
        );
296
 
297
    -- coefficients ROM: modelled as an asynchronously addressable ROM
298
    Inst_sha256_kt_rom: entity work.sha256_kt_rom(behavioral)
299
        port map(
300
            addr_i => Kt_addr,
301
            dout_o => Kt_data
302
        );
303
 
304
    -- init output data ROM: modelled as a statically defined constant
305
    Inst_sha256_ki_rom: entity work.sha256_ki_rom(behavioral)
306
        port map(
307
            K0_o => K0_data,
308
            K1_o => K1_data,
309
            K2_o => K2_data,
310
            K3_o => K3_data,
311
            K4_o => K4_data,
312
            K5_o => K5_data,
313
            K6_o => K6_data,
314
            K7_o => K7_data
315
        );
316
 
317
    --=============================================================================================
318
    --  OUTPUTS LOGIC
319
    --=============================================================================================
320
 
321
    error_o_proc:       error_o <= error_ctrl;
322
    do_valid_o_proc:    do_valid_o <= data_valid;
323
    H0_o_proc:          H0_o <= H0_data;
324
    H1_o_proc:          H1_o <= H1_data;
325
    H2_o_proc:          H2_o <= H2_data;
326
    H3_o_proc:          H3_o <= H3_data;
327
    H4_o_proc:          H4_o <= H4_data;
328
    H5_o_proc:          H5_o <= H5_data;
329
    H6_o_proc:          H6_o <= H6_data;
330
    H7_o_proc:          H7_o <= H7_data;
331
 
332
end rtl;
333
 

powered by: WebSVN 2.1.0

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