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_padding.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_padding - RTL
6
-- Project Name:    sha256 processor
7
-- Target Devices:  Spartan-6
8
-- Tool versions:   ISE 14.7
9
-- Description: 
10
--
11
--      This is the byte padding datapath logic for the sha256 processor. 
12
--      The padding of the last block is controlled by the byte lane selectors and the last words selectors.
13
--      These control signals are generated at the Control Logic block of the SHA256 processor. 
14
--      A consistency check error signal is generated, to flag illegal control conditions.
15
--      This block is a fully combinational circuit, with 2 layers of logic. 
16
--
17
------------------------------ COPYRIGHT NOTICE -----------------------------------------------------------------------
18
--                                                                   
19
--                                                                   
20
--      Author(s):      Jonny Doin, jonnydoin@gridvortex.com, jonnydoin@gmail.com
21
--                                                                   
22
--      Copyright (C) 2016 GridVortex, All Rights Reserved
23
--      --------------------------------------------------
24
--                                                                   
25
------------------------------ REVISION HISTORY -----------------------------------------------------------------------
26
--
27
-- 2016/05/22   v0.01.0010  [JD]    started development. design of blocks and port interfaces.
28
-- 2016/06/05   v0.01.0090  [JD]    all modules integrated. testbench for basic test vectors verification.
29
-- 2016/06/05   v0.01.0095  [JD]    verification failed. misalignment of words in the datapath. 
30
-- 2016/06/06   v0.01.0100  [JD]    first simulation verification against NIST-FIPS-180-4 test vectors passed.
31
--
32
-----------------------------------------------------------------------------------------------------------------------
33
--  TODO
34
--  ====
35
--
36
-----------------------------------------------------------------------------------------------------------------------
37
library ieee;
38
use ieee.std_logic_1164.all;
39
use ieee.numeric_std.all;
40
 
41
 
42
entity sha256_padding is
43
    port (
44
        words_sel_i : in std_logic_vector (1 downto 0) := (others => 'X');      -- selector for bitcnt insertion at the last block
45
        one_insert_i : in std_logic;                                            -- insert a leading one in the padding
46
        bytes_ena_i : in std_logic_vector (3 downto 0) := (others => 'X');      -- byte lane selector lines
47
        bitlen_i : in std_logic_vector (63 downto 0) := (others => 'X');        -- 64bit message bit length
48
        di_i : in std_logic_vector (31 downto 0) := (others => 'X');            -- big endian input message words
49
        do_o : out std_logic_vector (31 downto 0);                              -- padded output words
50
        error_o : out std_logic                                                 -- '1' if error in the byte_ena selectors
51
    );
52
end sha256_padding;
53
 
54
architecture rtl of sha256_padding is
55
    -- byte lane wires 
56
    signal BL0 : std_logic_vector (7 downto 0);                                 -- byte lane 0 (MSB)
57
    signal BL1 : std_logic_vector (7 downto 0);                                 -- byte lane 1 (1SB)
58
    signal BL2 : std_logic_vector (7 downto 0);                                 -- byte lane 2 (2SB)
59
    signal BL3 : std_logic_vector (7 downto 0);                                 -- byte lane 3 (LSB)
60
    -- selectors
61
    signal one_insert : std_logic;                                              -- leading one padding
62
    signal B_ena : std_logic_vector (3 downto 0);                               -- byte lane selectors
63
    signal W_sel : std_logic_vector (1 downto 0);                               -- last words selector
64
    -- padded words
65
    signal W_pad : std_logic_vector (31 downto 0);                              -- padding mux output
66
    signal W_out : std_logic_vector (31 downto 0);                              -- output word
67
    -- ones insertion muxes
68
    signal BL0_top_bit : std_logic;
69
    signal BL1_top_bit : std_logic;
70
    signal BL2_top_bit : std_logic;
71
    signal BL3_top_bit : std_logic;
72
    -- bit length words
73
    signal bitlen_hi : std_logic_vector (31 downto 0);
74
    signal bitlen_lo : std_logic_vector (31 downto 0);
75
    -- error indication
76
    signal bsel_error : std_logic;
77
    signal wsel_error : std_logic;
78
    signal err : std_logic;
79
begin
80
    --=============================================================================================
81
    -- INPUT LOGIC
82
    --=============================================================================================
83
    -- copy the input ports into the internal wires
84
 
85
    -- byte lanes are internally spliced from the 32bit input word
86
    BL0 <= di_i(31 downto 24);
87
    BL1 <= di_i(23 downto 16);
88
    BL2 <= di_i(15 downto 8);
89
    BL3 <= di_i(7 downto 0);
90
 
91
    -- bitlen words spliced from the 64bit bitlen input word
92
    bitlen_hi <= bitlen_i(63 downto 32);
93
    bitlen_lo <= bitlen_i(31 downto 0);
94
 
95
    -- byte lane selectors
96
    one_insert <= one_insert_i;
97
    B_ena <= bytes_ena_i;
98
    W_sel <= words_sel_i;
99
 
100
    --=============================================================================================
101
    -- PADDING LOGIC
102
    --=============================================================================================
103
    -- The last block padding logic is implemented as a stream insertion into the input words datapath.
104
 
105
    -- top bit for the padding bytes. one_insert controls whether a leading one will be inserted on the leading pad byte.
106
    BL0_top_bit <= one_insert and (not B_ena(0));
107
    BL1_top_bit <= one_insert and (B_ena(0) and not B_ena(1));
108
    BL2_top_bit <= one_insert and (B_ena(1) and not B_ena(2));
109
    BL3_top_bit <= one_insert and (B_ena(2) and not B_ena(3));
110
 
111
    -- byte lane padding muxes
112
    W_pad(31 downto 24) <= BL0 when B_ena(0) = '1' else (BL0_top_bit & b"0000000");
113
    W_pad(23 downto 16) <= BL1 when B_ena(1) = '1' else (BL1_top_bit & b"0000000");
114
    W_pad(15 downto 8)  <= BL2 when B_ena(2) = '1' else (BL2_top_bit & b"0000000");
115
    W_pad(7 downto 0)   <= BL3 when B_ena(3) = '1' else (BL3_top_bit & b"0000000");
116
 
117
    --=============================================================================================
118
    -- BIT COUNTER INSERTION LOGIC
119
    --=============================================================================================
120
    -- At the end of the last block, the 64bit message length is inserted as the last 2 words.
121
 
122
    W_out <=    bitlen_hi   when W_sel = b"01" else
123
                bitlen_lo   when W_sel = b"10" else
124
                W_pad;
125
 
126
    --=============================================================================================
127
    -- ERROR INDICATION LOGIC
128
    --=============================================================================================
129
    -- Invalid selectors conditions are flagged as errors
130
 
131
    -- byte lane selectors error: priority encoder
132
    bsel_error <=   '1' when (B_ena(3) = '1') and ((B_ena(2) = '0') or (B_ena(1) = '0') or (B_ena(0) = '0')) else
133
                    '1' when (B_ena(2) = '1') and ((B_ena(1) = '0') or (B_ena(0) = '0')) else
134
                    '1' when (B_ena(1) = '1') and  (B_ena(0) = '0') else
135
                    '0';
136
 
137
    -- word selector error: invalid code B"11"
138
    wsel_error <=   '1' when W_sel = b"11" else '0';
139
 
140
    err <= bsel_error or wsel_error;
141
 
142
    --=============================================================================================
143
    -- OUTPUT LOGIC
144
    --=============================================================================================
145
    -- connect output ports
146
    do_o_proc:      do_o <= W_out;
147
    error_o_proc:   error_o <= err;
148
end rtl;
149
 

powered by: WebSVN 2.1.0

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