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

Subversion Repositories nfhc

[/] [nfhc/] [trunk/] [sha256/] [sha256.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 ==  256 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
-- f0 = ((x and y) xor (not(x) and z))              --   Ch(x,y,z)
42
-- f1 = ((x and y) xor (x and z)  xor (y and z)     --  Maj(x,y,z)
43
-- f2 = ROTR  2(x) xor ROTR 13(x) xor ROTR 22(x)    --   Sigma0(x)
44
-- f3 = ROTR  6(x) xor ROTR 11(x) xor ROTR 25(x)    --   Sigma1(x)
45
-- f4 = ROTR  7(x) xor ROTR 18(x) xor SHR   3(x)    --   Tetha0(x)
46
-- f5 = ROTR 17(x) xor ROTR 19(x) xor SHR  10(x)    --   Tetha1(x)
47
--
48
-- h0 = 0x6a09e667
49
-- h1 = 0xbb67ae85
50
-- h2 = 0x3c6ef372
51
-- h3 = 0xa54ff53a
52
-- h4 = 0x510e527f
53
-- h5 = 0x9b05688c
54
-- h6 = 0x1f83d9ab
55
-- h7 = 0x5be0cd19
56
--
57
-- k[0-63] looks like better implemented in ROM file
58
--         with 32 bit in each contants it would take
59
--         64 x 32 bit storage which equal to
60
--            2048 bit ROM
61
--
62
-- Step 1
63
-- W(t) = M(t)                                                  0 <= t <=  15 -- we need 16x32 (512) bit registers
64
-- W(t) = f5(W(t-2)) + W(t-7) + f4(W(t-15)) + W(t-16);         16 <= t <=  79
65
-- W    = f5(W(  1)) + W(  6) + f4(W(  14)) + W(  15);         16 <= t <=  79
66
--
67
-- Step 2
68
-- a = h0; b = h1; c = h2; d = h3; e = h4; f = h5; g = h6; h = h7;
69
--
70
-- Step 3
71
-- for t 0 step 1 to 63 do
72
-- T1= h + f3(e) + f0(e, f, g) + k(t) + W(t)
73
-- T2=     f2(a) + f1(a, b, c)
74
-- h = g
75
-- g = f
76
-- f = e
77
-- e = d + T1
78
-- d = c
79
-- c = b
80
-- b = a
81
-- a = T1 + T2
82
--
83
-- Step 4
84
-- H0 = a + h0;
85
-- H1 = b + h1;
86
-- H2 = c + h2;
87
-- H3 = d + h3;
88
-- H4 = e + H4;
89
-- H5 = f + H5;
90
-- H6 = g + H6;
91
-- H7 = h + H7;
92
--
93
--  31 63 95 127 159 191 223 255 287 319 351 383 415 447 479 511
94
-- 0 32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512
95
--    0  1  2   3   4   5   6   7   8   9   a   b   c   d   e   f
96
 
97
library ieee;
98
use ieee.std_logic_1164.all;
99
use ieee.numeric_std.all;
100
 
101
entity sha256 is
102
  port(
103
  m                : in  bit_vector ( 31 downto 0); -- 32 bit data path require 16 clock to load all 512 bits of each block
104
  init             : in  bit;                       --    initial message
105
  ld               : in  bit;                       --    load signal
106
  md               : out bit_vector ( 31 downto 0); --    5 clock after active valid signal is the message hash result
107
--probe
108
--a_prb            : out bit_vector ( 31 downto 0);
109
--b_prb            : out bit_vector ( 31 downto 0);
110
--c_prb            : out bit_vector ( 31 downto 0);
111
--d_prb            : out bit_vector ( 31 downto 0);
112
--e_prb            : out bit_vector ( 31 downto 0);
113
--f_prb            : out bit_vector ( 31 downto 0);
114
--g_prb            : out bit_vector ( 31 downto 0);
115
--h_prb            : out bit_vector ( 31 downto 0);
116
--k_prb            : out bit_vector ( 31 downto 0);
117
--w_prb            : out bit_vector ( 31 downto 0);
118
--ctr2p            : out bit_vector (  3 downto 0);
119
--ctr3p            : out bit_vector (  5 downto 0);
120
--sc_pr            : out bit_vector (  1 downto 0);
121
--probe
122
  v                : out bit;                       --    hash output valid signal one clock advance
123
  clk              : in  bit;                       --    master clock signal
124
  rst              : in  bit                        --    master reset signal
125
  );
126
end sha256;
127
 
128
architecture phy of sha256 is
129
 
130
  component c4b
131
    port (
132
    cnt            : out bit_vector (  3 downto 0);
133
    clk            : in  bit;
134
    rst            : in  bit
135
    );
136
  end component;
137
 
138
  component c6b
139
    port (
140
    cnt            : out bit_vector (  5 downto 0);
141
    clk            : in  bit;
142
    rst            : in  bit
143
    );
144
  end component;
145
 
146
  component romk
147
    port (
148
    addr           : in  bit_vector (  5 downto 0);
149
    k              : out bit_vector ( 31 downto 0)
150
    );
151
  end component;
152
 
153
  signal   ih      :     bit_vector ( 31 downto 0);
154
  signal   h0      :     bit_vector ( 31 downto 0);
155
  signal   h1      :     bit_vector ( 31 downto 0);
156
  signal   h2      :     bit_vector ( 31 downto 0);
157
  signal   h3      :     bit_vector ( 31 downto 0);
158
  signal   h4      :     bit_vector ( 31 downto 0);
159
  signal   h5      :     bit_vector ( 31 downto 0);
160
  signal   h6      :     bit_vector ( 31 downto 0);
161
  signal   h7      :     bit_vector ( 31 downto 0);
162
 
163
  signal   k       :     bit_vector ( 31 downto 0);
164
 
165
  signal   im      :     bit_vector ( 31 downto 0);
166
  signal   iw      :     bit_vector ( 31 downto 0);
167
  signal   w       :     bit_vector ( 31 downto 0); -- current working register
168
  signal   w0      :     bit_vector (511 downto 0); -- working register 1
169
 
170
  signal   a       :     bit_vector ( 31 downto 0); -- a register
171
  signal   b       :     bit_vector ( 31 downto 0); -- b register
172
  signal   c       :     bit_vector ( 31 downto 0); -- c register
173
  signal   d       :     bit_vector ( 31 downto 0); -- d register
174
  signal   e       :     bit_vector ( 31 downto 0); -- e register
175
  signal   f       :     bit_vector ( 31 downto 0); -- f register
176
  signal   g       :     bit_vector ( 31 downto 0); -- g register
177
  signal   h       :     bit_vector ( 31 downto 0); -- h register
178
 
179
  signal   f0      :     bit_vector ( 31 downto 0);
180
  signal   f1      :     bit_vector ( 31 downto 0);
181
  signal   f2      :     bit_vector ( 31 downto 0);
182
  signal   f3      :     bit_vector ( 31 downto 0);
183
  signal   f4      :     bit_vector ( 31 downto 0);
184
  signal   f5      :     bit_vector ( 31 downto 0);
185
 
186
  signal   ctr2    :     bit_vector (  3 downto 0); --  4  bit counter (zero to  16)
187
  signal   ctr2_rst:     bit;
188
  signal   ctr3    :     bit_vector (  5 downto 0); --  6  bit counter (zero to  64)
189
  signal   ctr3_rst:     bit;
190
 
191
  signal   vld     :     bit;
192
  signal   nld     :     bit;
193
  signal   ild     :     bit;
194
  signal   ild_rst :     bit;
195
 
196
begin
197
 
198
  ct2              : c4b
199
  port map (
200
  cnt              => ctr2,
201
  clk              => clk,
202
  rst              => ctr2_rst
203
  );
204
  ct3              : c6b
205
  port map (
206
  cnt              => ctr3,
207
  clk              => clk,
208
  rst              => ctr3_rst
209
  );
210
  rom0             : romk
211
  port map (
212
  addr             => ctr3,
213
  k                => k
214
  );
215
 
216
--probe signal
217
--a_prb            <= a;
218
--b_prb            <= b;
219
--c_prb            <= c;
220
--d_prb            <= d;
221
--e_prb            <= e;
222
--f_prb            <= e;
223
--g_prb            <= e;
224
--h_prb            <= e;
225
--k_prb            <= k;
226
--w_prb            <= w;
227
--ctr2p            <= ctr2;
228
--ctr3p            <= ctr3;
229
--probe signal
230
 
231
--persistent connection
232
 
233
--f0               == ((x and y) xor (not(x) and z))                      -- f0(e, f, g)
234
  f0               <= ((e and f) xor (not(e) and g));
235
--f1               == ((x and y) xor (x and z) xor (y and z)              -- f1(a, b, c)
236
  f1               <= ((a and b) xor (a and c) xor (b and c));
237
--f2               == ROTR  2(x)  xor ROTR 13(x) xor ROTR 22(x)           -- f2(a)
238
  f2               <= (a (  1 downto   0) & a ( 31 downto   2)) xor
239
                      (a ( 12 downto   0) & a ( 31 downto  13)) xor
240
                      (a ( 21 downto   0) & a ( 31 downto  22));
241
--f3               == ROTR  6(x)  xor ROTR 11(x) xor ROTR 25(x)           -- f3(e)
242
  f3               <= (e (  5 downto   0) & e ( 31 downto   6)) xor
243
                      (e ( 10 downto   0) & e ( 31 downto  11)) xor
244
                      (e ( 24 downto   0) & e ( 31 downto  25));
245
--f4               == ROTR  7(x)  xor ROTR 18(x) xor SHR   3(x)           -- w0(479 downto 448)
246
  f4               <= (w0(454 downto 448) & w0(479 downto 455)) xor
247
                      (w0(465 downto 448) & w0(479 downto 466)) xor
248
                      (B"000"             & w0(479 downto 451));
249
--f5               == ROTR 17(x)  xor ROTR 19(x) xor SHR  10(x)           -- w0( 63 downto  32)
250
  f5               <= (w0( 48 downto  32) & w0( 63 downto  49)) xor
251
                      (w0( 50 downto  32) & w0( 63 downto  51)) xor
252
                      (B"0000000000"      & w0( 63 downto  42));
253
 
254
  with ctr2( 2 downto 0) select -- omit bit 4
255
  ih               <= h0                                      when B"000",
256
                      h1                                      when B"001",
257
                      h2                                      when B"010",
258
                      h3                                      when B"011",
259
                      h4                                      when B"100",
260
                      h5                                      when B"101",
261
                      h6                                      when B"110",
262
                      h7                                      when B"111";
263
 
264
--W                == f5(W(  1)) + W(  6)             + f4(W(  14)) + W(  15);             16 <= t <=  79
265
--iw               <= f5         + w0(223 downto 192) + f4          + w0(511 downto 480); -- FIXME this adder is very costly and NOT A PORTABLE CODE
266
  iw               <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(f5)) + unsigned(to_stdlogicvector(w0(223 downto 192))) + unsigned(to_stdlogicvector(f4)) + unsigned(to_stdlogicvector(w0(511 downto 480))) ));
267
 
268
  process (clk)
269
  begin
270
    if ((clk = '1') and clk'event) then
271
      if    (rst = '1') then
272
        w          <= (others => '0');
273
        w0         <= (others => '0');
274
      elsif (nld = '1') then                                              -- 0 <= t <= 15 first 512 bit block
275
        w          <=              im;
276
  w0(511 downto 0) <= (w0(479 downto  0) & im);
277
      else
278
        w          <=  iw( 31 downto   0)                      ;
279
  w0(511 downto 0) <= (w0(479 downto   0) & iw( 31 downto   0));
280
      end if;
281
    end if;
282
  end process;
283
 
284
  process (clk)
285
  begin
286
    if ((clk = '1') and clk'event) then
287
      if (rst = '1') then
288
        ild        <=  '0';
289
        nld        <=  '0';
290
        im         <= (others => '0');
291
      else
292
        ild        <=  nld;
293
        nld        <=   ld;
294
        im         <=    m;
295
      end if;
296
    end if;
297
  end process;
298
 
299
  process (clk)
300
  begin
301
    if ((clk = '1') and clk'event) then
302
      if ((ild_rst or rst) = '1') then
303
        vld        <=  '0';
304
      elsif (ctr3 = B"111111") then
305
        vld        <=  '1';
306
      else
307
        vld        <=  '0';
308
      end if;
309
    end if;
310
  end process;
311
 
312
  ild_rst          <= (ild xor ld) and ld;
313
--ctr2_rst         <=  ild_rst     or rst or vld or (ctr2 = B"0111");     -- set to count to  7 (  8 clock)
314
  ctr2_rst         <=  ild_rst     or rst or vld or not(ctr2(3) or not(ctr2(2)) or not(ctr2(1)) or not(ctr2(0)));
315
  ctr3_rst         <=  ild_rst     or rst;-- (ctr3 = B"010011");          -- set to count to 63 ( 64 clock)
316
 
317
  process (clk)
318
  begin
319
    if ((clk = '1') and clk'event) then
320
      if (init = '1')  or (rst = '1') then
321
        h0         <= X"6a09e667";
322
        h1         <= X"bb67ae85";
323
        h2         <= X"3c6ef372";
324
        h3         <= X"a54ff53a";
325
        h4         <= X"510e527f";
326
        h5         <= X"9b05688c";
327
        h6         <= X"1f83d9ab";
328
        h7         <= X"5be0cd19";
329
      elsif (vld = '1') then -- FIXME this adder is very costly and NOT A PORTABLE CODE
330
        h0         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(a)) + unsigned(to_stdlogicvector(h0)) ));
331
        h1         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(b)) + unsigned(to_stdlogicvector(h1)) ));
332
        h2         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(c)) + unsigned(to_stdlogicvector(h2)) ));
333
        h3         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(d)) + unsigned(to_stdlogicvector(h3)) ));
334
        h4         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(e)) + unsigned(to_stdlogicvector(h4)) ));
335
        h5         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(f)) + unsigned(to_stdlogicvector(h5)) ));
336
        h6         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(g)) + unsigned(to_stdlogicvector(h6)) ));
337
        h7         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(h7)) ));
338
--      h0         <=      a + h0;
339
--      h1         <=      b + h1;
340
--      h2         <=      c + h2;
341
--      h3         <=      d + h3;
342
--      h4         <=      e + h4;
343
--      h5         <=      f + h5;
344
--      h6         <=      g + h6;
345
--      h7         <=      h + h7;
346
      end if;
347
    end if;
348
  end process;
349
 
350
  process (clk)
351
  begin
352
    if ((clk = '1') and clk'event) then
353
      if ((ild_rst or rst) = '1') then
354
        a          <= h0;
355
        b          <= h1;
356
        c          <= h2;
357
        d          <= h3;
358
        e          <= h4;
359
        f          <= h5;
360
        g          <= h6;
361
        h          <= h7;
362
       else -- FIXME this adder is very costly and NOT A PORTABLE CODE
363
--      T1         == h + f3(e) + f0(e, f, g) + k(t) + W(t)
364
--      T2         ==     f2(a) + f1(a, b, c)
365
        h          <=  g;
366
        g          <=  f;
367
        f          <=  e;
368
--      e          <=  d +          T1        ;
369
--      e          <=  d + h + f3 + f0 + k + w;
370
        e          <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(d)) + unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(f3)) + unsigned(to_stdlogicvector(f0)) + unsigned(to_stdlogicvector(k)) + unsigned(to_stdlogicvector(w)) ));
371
        d          <=  c;
372
        c          <=  b;
373
        b          <=  a;
374
--      a          <=             T1           +    T2  ;
375
--      a          <=      h + f3 + f0 + k + w + f2 + f1;
376
        a          <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(f3)) + unsigned(to_stdlogicvector(f0)) + unsigned(to_stdlogicvector(k)) + unsigned(to_stdlogicvector(w))  + unsigned(to_stdlogicvector(f2)) + unsigned(to_stdlogicvector(f1)) ));
377
      end if;
378
    end if;
379
  end process;
380
 
381
  md               <=  ih;
382
  v                <=  vld;
383
 
384
end phy;

powered by: WebSVN 2.1.0

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