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

Subversion Repositories nfhc

[/] [nfhc/] [trunk/] [sha1/] [sha1.vhdl] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 arif_endro
-- ------------------------------------------------------------------------
2
-- Copyright (C) 2010 Arif Endro Nugroho
3
-- All rights reserved.
4
-- 
5
-- Redistribution and use in source and binary forms, with or without
6
-- modification, are permitted provided that the following conditions
7
-- are met:
8
-- 
9
-- 1. Redistributions of source code must retain the above copyright
10
--    notice, this list of conditions and the following disclaimer.
11
-- 2. Redistributions in binary form must reproduce the above copyright
12
--    notice, this list of conditions and the following disclaimer in the
13
--    documentation and/or other materials provided with the distribution.
14
-- 
15
-- THIS SOFTWARE IS PROVIDED BY ARIF ENDRO NUGROHO "AS IS" AND ANY EXPRESS
16
-- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
-- DISCLAIMED. IN NO EVENT SHALL ARIF ENDRO NUGROHO BE LIABLE FOR ANY
19
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
-- POSSIBILITY OF SUCH DAMAGE.
26
-- 
27
-- End Of License.
28
-- ------------------------------------------------------------------------
29
--
30
-- MaxMessage  <= 2^64 bits
31
-- BlockSize   ==  512 bits
32
-- WordSize    ==   32 bits
33
-- MDigestSize ==  160 bits
34
-- Security    ==  128 bits
35
--
36
-- SHLnx  = (x<<n)
37
-- SHRnx  = (x>>n)
38
-- ROTRnx = (x>>n) or (x<<w-n)
39
-- ROTLnx = (x<<n) or (x>>w-n)
40
--
41
-- f  = ((x and y) xor (not(x) and z))           0 <= t <=  19
42
-- f  = (x xor y xor z)                         20 <= t <=  39
43
-- f  = ((x and y) xor (x and z) xor (y and z)  40 <= t <=  59
44
-- f  = (x xor y xor z)                         60 <= t <=  79
45
--
46
-- h0 = 0x67452301
47
-- h1 = 0xefcdab89
48
-- h2 = 0x98badcfe
49
-- h3 = 0x10325476
50
-- h4 = 0xc3d2e1f0
51
--
52
-- k0 = 0x5a827999   0 <= t <=  19
53
-- k1 = 0x6ed9eba1  20 <= t <=  39
54
-- k2 = 0x8f1bbcdc  40 <= t <=  59
55
-- k3 = 0xca62c1d6  60 <= t <=  79
56
--
57
-- Step 1
58
-- W(t) = M(t)                                                 0 <= t <=  15 -- we need 16x32 (512) bit registers
59
-- W(t) = (W(t-3) xor W(t-8) xor W(t-14) xor W(t-16)) ROTL 1  16 <= t <=  79
60
-- W    = (W(  2) xor W(  7) xor W(  13) xor W(  15)) ROTL 1; 16 <= t <=  79
61
--
62
-- Step 2
63
-- a = h0; b = h1; c = h2; d = h3; e = h4
64
--
65
-- Step 3
66
-- for t 0 step 1 to 79 do
67
-- T = ROTL5(a) xor f(b, c, d) xor e xor k(t) xor W(t)
68
-- e = d
69
-- d = c
70
-- c = ROTL30(b) -- c = ROTR2(b)
71
-- b = a
72
-- a = T
73
--
74
-- Step 4
75
-- H0 = a xor h0;
76
-- H1 = b xor h1;
77
-- H2 = c xor h2;
78
-- H3 = d xor h3;
79
-- H4 = e xor H4;
80
--
81
--  31 63 95 127 159 191 223 255 287 319 351 383 415 447 479 511
82
-- 0 32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512
83
--    0  1  2   3   4   5   6   7   8   9   a   b   c   d   e   f
84
 
85
library ieee;
86
use ieee.std_logic_1164.all; -- std_logic stuff
87
use ieee.numeric_std.all;    -- basic math for std_logic
88
 
89
entity sha1 is
90
  port(
91
  m                : in  bit_vector ( 31 downto 0); -- 32 bit data path require 16 clock to load all 512 bits of each block
92
  init             : in  bit;                       --    initial message
93
  ld               : in  bit;                       --    load signal
94
  h                : out bit_vector ( 31 downto 0); --    5 clock after active valid signal is the message hash result
95
--probe
96
--a_prb            : out bit_vector ( 31 downto 0);
97
--b_prb            : out bit_vector ( 31 downto 0);
98
--c_prb            : out bit_vector ( 31 downto 0);
99
--d_prb            : out bit_vector ( 31 downto 0);
100
--e_prb            : out bit_vector ( 31 downto 0);
101
--k_prb            : out bit_vector ( 31 downto 0);
102
--w_prb            : out bit_vector ( 31 downto 0);
103
--ctr2p            : out bit_vector (  3 downto 0);
104
--ctr3p            : out bit_vector (  5 downto 0);
105
--sc_pr            : out bit_vector (  1 downto 0);
106
--probe
107
  v                : out bit;                       --    hash output valid signal one clock advance
108
  clk              : in  bit;                       --    master clock signal
109
  rst              : in  bit                        --    master reset signal
110
  );
111
end sha1;
112
 
113
architecture phy of sha1 is
114
 
115
  component c4b
116
    port (
117
    cnt            : out bit_vector (  3 downto 0);
118
    clk            : in  bit;
119
    rst            : in  bit
120
    );
121
  end component;
122
 
123
  component c6b
124
    port (
125
    cnt            : out bit_vector (  5 downto 0);
126
    clk            : in  bit;
127
    rst            : in  bit
128
    );
129
  end component;
130
 
131
  signal   ih      :     bit_vector ( 31 downto 0);
132
  signal   h0      :     bit_vector ( 31 downto 0);
133
  signal   h1      :     bit_vector ( 31 downto 0);
134
  signal   h2      :     bit_vector ( 31 downto 0);
135
  signal   h3      :     bit_vector ( 31 downto 0);
136
  signal   h4      :     bit_vector ( 31 downto 0);
137
 
138
  constant k0      :     bit_vector ( 31 downto 0) := X"5a827999";
139
  constant k1      :     bit_vector ( 31 downto 0) := X"6ed9eba1";
140
  constant k2      :     bit_vector ( 31 downto 0) := X"8f1bbcdc";
141
  constant k3      :     bit_vector ( 31 downto 0) := X"ca62c1d6";
142
  signal   k       :     bit_vector ( 31 downto 0);
143
 
144
  signal   im      :     bit_vector ( 31 downto 0);
145
  signal   iw      :     bit_vector ( 31 downto 0);
146
  signal   w       :     bit_vector ( 31 downto 0); -- current working register
147
  signal   w0      :     bit_vector (511 downto 0); -- working register 1
148
 
149
  signal   a       :     bit_vector ( 31 downto 0); -- a register
150
  signal   b       :     bit_vector ( 31 downto 0); -- b register
151
  signal   c       :     bit_vector ( 31 downto 0); -- c register
152
  signal   d       :     bit_vector ( 31 downto 0); -- d register
153
  signal   e       :     bit_vector ( 31 downto 0); -- e register
154
 
155
  signal   f       :     bit_vector ( 31 downto 0);
156
 
157
  signal   ctr2    :     bit_vector (  3 downto 0); --  4  bit counter (zero to  16)
158
  signal   ctr2_rst:     bit;
159
  signal   ctr3    :     bit_vector (  5 downto 0); --  6  bit counter (zero to  64)
160
  signal   ctr3_rst:     bit;
161
 
162
  signal   vld     :     bit;
163
  signal   nld     :     bit;
164
  signal   ild     :     bit;
165
  signal   ild_rst :     bit;
166
 
167
  signal   sr      :     bit_vector (  1 downto 0);
168
  signal   sc      :     bit_vector (  1 downto 0);
169
 
170
begin
171
 
172
  ct2              : c4b
173
  port map (
174
  cnt              => ctr2,
175
  clk              => clk,
176
  rst              => ctr2_rst
177
  );
178
  ct3              : c6b
179
  port map (
180
  cnt              => ctr3,
181
  clk              => clk,
182
  rst              => ctr3_rst
183
  );
184
 
185
--probe signal
186
--a_prb            <= a;
187
--b_prb            <= b;
188
--c_prb            <= c;
189
--d_prb            <= d;
190
--e_prb            <= e;
191
--k_prb            <= k;
192
--w_prb            <= w;
193
--sc_pr            <= sc;
194
--ctr2p            <= ctr2;
195
--ctr3p            <= ctr3;
196
--probe signal
197
 
198
--persistent connection
199
  with sc (  1 downto 0) select
200
  f                <= ((b and c) xor (not(b) and d))          when B"00", --  0 <= t <= 19
201
                      ( b xor c  xor d)                       when B"01", -- 20 <= t <= 39
202
                      ((b and c) xor (b and d) xor (c and d)) when B"10", -- 40 <= t <= 59
203
                      ( b xor c  xor d)                       when B"11"; -- 60 <= t <= 79 
204
  with sc (  1 downto 0) select
205
  k                <= k0                                      when B"00",
206
                      k1                                      when B"01",
207
                      k2                                      when B"10",
208
                      k3                                      when B"11";
209
  with ctr2( 3 downto 0) select
210
  ih               <= h0                                      when B"0000",
211
                      h1                                      when B"0001",
212
                      h2                                      when B"0010",
213
                      h3                                      when B"0011",
214
                      h4                                      when B"0100",
215
                      (others => '0')                         when others;
216
 
217
--W                =  (W(  2)            xor W(  7)             xor W(  13)            xor W(  15)) ROTL 1; 16 <= t <=  79
218
  iw               <= w0( 95 downto  64) xor w0(255 downto 224) xor w0(447 downto 416) xor w0(511 downto 480);
219
 
220
  process (clk)
221
  begin
222
    if ((clk = '1') and clk'event) then
223
      if    (rst = '1') then
224
        w          <= (others => '0');
225
        w0         <= (others => '0');
226
      elsif (nld = '1') then                                              -- 0 <= t <= 15 first 512 bit block
227
        w          <=              im;
228
  w0(511 downto 0) <= (w0(479 downto  0) & im);
229
      else                                                                -- ROTL1
230
        w          <= (iw( 30 downto   0) & iw( 31));
231
  w0(511 downto 0) <= (w0(479 downto   0) & iw( 30 downto   0) & iw( 31));
232
      end if;
233
    end if;
234
  end process;
235
 
236
  process (clk)
237
  begin
238
    if ((clk = '1') and clk'event) then
239
      if (rst = '1') then
240
        ild        <=  '0';
241
        nld        <=  '0';
242
        im         <= (others => '0');
243
      else
244
        ild        <=  nld;
245
        nld        <=   ld;
246
        im         <=    m;
247
      end if;
248
    end if;
249
  end process;
250
 
251
  sr               <= (sc(0) & '0');
252
 
253
  process (clk)
254
  begin
255
    if ((clk = '1') and clk'event) then
256
      if ((ild_rst or rst) = '1') then
257
        sc         <= (others => '0');
258
      elsif (ctr3 = B"010011") then
259
        sc         <= ((sc xor B"01") xor sr);
260
      end if;
261
    end if;
262
  end process;
263
 
264
  process (clk)
265
  begin
266
    if ((clk = '1') and clk'event) then
267
      if ((ild_rst or rst) = '1') then
268
        vld        <=  '0';
269
      elsif (ctr3 = B"010011") and (sc = B"11") then
270
        vld        <=  '1';
271
      else
272
        vld        <=  '0';
273
      end if;
274
    end if;
275
  end process;
276
 
277
  ild_rst          <= (ild xor ld) and ld;
278
--ctr2_rst         <=  ild_rst     or rst or vld or (ctr2 = B"0100");     -- set to count to  4 (  5 clock)
279
  ctr2_rst         <=  ild_rst     or rst or vld or not(ctr2(3) or not(ctr2(2)) or ctr2(1) or ctr2(0));
280
--ctr3_rst         <=  ild_rst     or rst or (ctr3 = B"010011");          -- set to count to 19 ( 20 clock)
281
  ctr3_rst         <=  ild_rst     or rst or not(ctr3(5) or not(ctr3(4)) or ctr3(3) or ctr3(2) or not(ctr3(1)) or not(ctr3(0)));
282
 
283
  process (clk)
284
  begin
285
    if ((clk = '1') and clk'event) then
286
      if (init = '1')  or (rst = '1')then
287
        h0         <= X"67452301";
288
        h1         <= X"efcdab89";
289
        h2         <= X"98badcfe";
290
        h3         <= X"10325476";
291
        h4         <= X"c3d2e1f0";
292
      elsif (vld = '1') then -- FIXME this adder is very costly and NOT A PORTABLE CODE
293
        h0         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(a)) + unsigned(to_stdlogicvector(h0)) ));
294
        h1         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(b)) + unsigned(to_stdlogicvector(h1)) ));
295
        h2         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(c)) + unsigned(to_stdlogicvector(h2)) ));
296
        h3         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(d)) + unsigned(to_stdlogicvector(h3)) ));
297
        h4         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(e)) + unsigned(to_stdlogicvector(h4)) ));
298
--      h0         <=  a + h0;
299
--      h1         <=  b + h1;
300
--      h2         <=  c + h2;
301
--      h3         <=  d + h3;
302
--      h4         <=  e + h4;
303
      end if;
304
    end if;
305
  end process;
306
 
307
  process (clk)
308
  begin
309
    if ((clk = '1') and clk'event) then
310
      if ((ild_rst or rst) = '1') then
311
        a          <= h0;
312
        b          <= h1;
313
        c          <= h2;
314
        d          <= h3;
315
        e          <= h4;
316
       else
317
--      a          <= (a(26 downto 0) & a(31 downto 27)) + f + e + k + w; -- ROTL5(a)  -- FIXME this adder is very costly and NOT A PORTABLE CODE
318
        a          <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector( (a(26 downto 0) & a(31 downto 27)) )) + unsigned(to_stdlogicvector(f)) + unsigned(to_stdlogicvector(e)) + unsigned(to_stdlogicvector(k)) + unsigned(to_stdlogicvector(w)) ));
319
        b          <=  a;
320
        c          <= (b( 1 downto 0) & b(31 downto  2));                 -- ROTL30(b) -- ROTR2(b)
321
        d          <=  c;
322
        e          <=  d;
323
      end if;
324
    end if;
325
  end process;
326
 
327
  h                <=  ih;
328
  v                <=  vld;
329
 
330
end phy;

powered by: WebSVN 2.1.0

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