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

Subversion Repositories nfhc

[/] [nfhc/] [trunk/] [sha512/] [sha512.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   == 1024 bits
32
-- WordSize    ==   64 bits
33
-- MDigestSize ==  512 bits
34
-- Security    ==  256 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 28(x) xor ROTR 34(x) xor ROTR 39(x)    --   Sigma0(x)
44
-- f3 = ROTR 14(x) xor ROTR 18(x) xor ROTR 41(x)    --   Sigma1(x)
45
-- f4 = ROTR  1(x) xor ROTR  8(x) xor SHR   7(x)    --   Tetha0(x)
46
-- f5 = ROTR 19(x) xor ROTR 61(x) xor SHR   6(x)    --   Tetha1(x)
47
--
48
-- h0 = 0x6a09e667f3bcc908
49
-- h1 = 0xbb67ae8584caa73b
50
-- h2 = 0x3c6ef372fe94f82b
51
-- h3 = 0xa54ff53a5f1d36f1
52
-- h4 = 0x510e527fade682d1
53
-- h5 = 0x9b05688c2b3e6c1f
54
-- h6 = 0x1f83d9abfb41bd6b
55
-- h7 = 0x5be0cd19137e2179
56
--
57
-- k[0-63] looks like better implemented in ROM file
58
--         with 64 bit in each contants it would take
59
--         64 x 64 bit storage which equal to
60
--            4096 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 79 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
-- 0 64 128 192 256 320 384 448 512 576 640 704 768 832 896 960 1024
94
--    0   1   2   3   4   5   6   7   8   9   a   b   c   d   e    f
95
 
96
library ieee;
97
use ieee.std_logic_1164.all;
98
use ieee.numeric_std.all;
99
 
100
entity sha512 is
101
  port(
102
  m                : in  bit_vector ( 63 downto 0); -- 32 bit data path require 16 clock to load all 512 bits of each block
103
  init             : in  bit;                       --    initial message
104
  ld               : in  bit;                       --    load signal
105
  md               : out bit_vector ( 63 downto 0); --    5 clock after active valid signal is the message hash result
106
--probe
107
--a_prb            : out bit_vector ( 63 downto 0);
108
--b_prb            : out bit_vector ( 63 downto 0);
109
--c_prb            : out bit_vector ( 63 downto 0);
110
--d_prb            : out bit_vector ( 63 downto 0);
111
--e_prb            : out bit_vector ( 63 downto 0);
112
--f_prb            : out bit_vector ( 63 downto 0);
113
--g_prb            : out bit_vector ( 63 downto 0);
114
--h_prb            : out bit_vector ( 63 downto 0);
115
--k_prb            : out bit_vector ( 63 downto 0);
116
--w_prb            : out bit_vector ( 63 downto 0);
117
--ctr2p            : out bit_vector (  3 downto 0);
118
--ctr3p            : out bit_vector (  7 downto 0);
119
--probe
120
  v                : out bit;                       --    hash output valid signal one clock advance
121
  clk              : in  bit;                       --    master clock signal
122
  rst              : in  bit                        --    master reset signal
123
  );
124
end sha512;
125
 
126
architecture phy of sha512 is
127
 
128
  component c4b
129
    port (
130
    cnt            : out bit_vector (  3 downto 0);
131
    clk            : in  bit;
132
    rst            : in  bit
133
    );
134
  end component;
135
 
136
  component c8b
137
    port (
138
    cnt            : out bit_vector (  7 downto 0);
139
    clk            : in  bit;
140
    rst            : in  bit
141
    );
142
  end component;
143
 
144
  component romk
145
    port (
146
    addr           : in  bit_vector (  6 downto 0);
147
    k              : out bit_vector ( 63 downto 0)
148
    );
149
  end component;
150
 
151
  signal   ih      :     bit_vector ( 63 downto 0);
152
  signal   h0      :     bit_vector ( 63 downto 0);
153
  signal   h1      :     bit_vector ( 63 downto 0);
154
  signal   h2      :     bit_vector ( 63 downto 0);
155
  signal   h3      :     bit_vector ( 63 downto 0);
156
  signal   h4      :     bit_vector ( 63 downto 0);
157
  signal   h5      :     bit_vector ( 63 downto 0);
158
  signal   h6      :     bit_vector ( 63 downto 0);
159
  signal   h7      :     bit_vector ( 63 downto 0);
160
 
161
  signal   k       :     bit_vector ( 63 downto 0);
162
 
163
  signal   im      :     bit_vector ( 63 downto 0);
164
  signal   iw      :     bit_vector ( 63 downto 0);
165
  signal   w       :     bit_vector ( 63 downto 0); -- current working register
166
  signal   w0      :     bit_vector(1023 downto 0); -- working register 1
167
 
168
  signal   a       :     bit_vector ( 63 downto 0); -- a register
169
  signal   b       :     bit_vector ( 63 downto 0); -- b register
170
  signal   c       :     bit_vector ( 63 downto 0); -- c register
171
  signal   d       :     bit_vector ( 63 downto 0); -- d register
172
  signal   e       :     bit_vector ( 63 downto 0); -- e register
173
  signal   f       :     bit_vector ( 63 downto 0); -- f register
174
  signal   g       :     bit_vector ( 63 downto 0); -- g register
175
  signal   h       :     bit_vector ( 63 downto 0); -- h register
176
 
177
  signal   f0      :     bit_vector ( 63 downto 0);
178
  signal   f1      :     bit_vector ( 63 downto 0);
179
  signal   f2      :     bit_vector ( 63 downto 0);
180
  signal   f3      :     bit_vector ( 63 downto 0);
181
  signal   f4      :     bit_vector ( 63 downto 0);
182
  signal   f5      :     bit_vector ( 63 downto 0);
183
 
184
  signal   ctr2    :     bit_vector (  3 downto 0); --  4  bit counter (zero to  16)
185
  signal   ctr2_rst:     bit;
186
  signal   ctr3    :     bit_vector (  7 downto 0); --  8  bit counter (zero to 255)
187
  signal   ctr3_rst:     bit;
188
 
189
  signal   vld     :     bit;
190
  signal   nld     :     bit;
191
  signal   ild     :     bit;
192
  signal   ild_rst :     bit;
193
 
194
begin
195
 
196
  ct2              : c4b
197
  port map (
198
  cnt              => ctr2,
199
  clk              => clk,
200
  rst              => ctr2_rst
201
  );
202
  ct3              : c8b
203
  port map (
204
  cnt              => ctr3,
205
  clk              => clk,
206
  rst              => ctr3_rst
207
  );
208
  rom0             : romk
209
  port map (
210
  addr             => ctr3(  6 downto 0),
211
  k                => k
212
  );
213
 
214
--probe signal
215
--a_prb            <= a;
216
--b_prb            <= b;
217
--c_prb            <= c;
218
--d_prb            <= d;
219
--e_prb            <= e;
220
--f_prb            <= e;
221
--g_prb            <= e;
222
--h_prb            <= e;
223
--k_prb            <= k;
224
--w_prb            <= w;
225
--ctr2p            <= ctr2;
226
--ctr3p            <= ctr3;
227
--probe signal
228
 
229
--persistent connection
230
 
231
--f0               == ((x and y) xor (not(x) and z))                      -- f0(e, f, g)
232
  f0               <= ((e and f) xor (not(e) and g));
233
--f1               == ((x and y) xor (x and z) xor (y and z)              -- f1(a, b, c)
234
  f1               <= ((a and b) xor (a and c) xor (b and c));
235
--f2               == ROTR 28(x)  xor ROTR 34(x) xor ROTR 39(x)           -- f2(a)
236
  f2               <= (a ( 27 downto   0) & a ( 63 downto  28)) xor
237
                      (a ( 33 downto   0) & a ( 63 downto  34)) xor
238
                      (a ( 38 downto   0) & a ( 63 downto  39));
239
--f3               == ROTR 14(x)  xor ROTR 18(x) xor ROTR 41(x)           -- f3(e)
240
  f3               <= (e ( 13 downto   0) & e ( 63 downto  14)) xor
241
                      (e ( 17 downto   0) & e ( 63 downto  18)) xor
242
                      (e ( 40 downto   0) & e ( 63 downto  41));
243
--f4               == ROTR  1(x)  xor ROTR  8(x) xor SHR   7(x)           -- w0(959 downto 896)
244
  f4               <= (w0(           896) & w0(959 downto 897)) xor
245
                      (w0(903 downto 896) & w0(959 downto 904)) xor
246
                      (B"0000000"         & w0(959 downto 903));
247
--f5               == ROTR 19(x)  xor ROTR 61(x) xor SHR   6(x)           -- w0(127 downto  64)
248
  f5               <= (w0( 82 downto  64) & w0(127 downto  83)) xor
249
                      (w0(124 downto  64) & w0(127 downto 125)) xor
250
                      (B"000000"          & w0(127 downto  70));
251
 
252
  with ctr2(  2 downto 0) select
253
  ih               <= h0                                      when B"000",
254
                      h1                                      when B"001",
255
                      h2                                      when B"010",
256
                      h3                                      when B"011",
257
                      h4                                      when B"100",
258
                      h5                                      when B"101",
259
                      h6                                      when B"110",
260
                      h7                                      when B"111";
261
 
262
--W                == f5(W(  1)) + W(  6)             + f4(W(  14)) + W(  15);             16 <= t <=  79
263
--iw               <= f5         + w0(447 downto 384) + f4          + w0(1023 downto 960); -- FIXME this adder is very costly and NOT A PORTABLE CODE
264
  iw               <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(f5)) + unsigned(to_stdlogicvector(w0(447 downto 384))) + unsigned(to_stdlogicvector(f4)) + unsigned(to_stdlogicvector(w0(1023 downto 960))) ));
265
 
266
  process (clk)
267
  begin
268
    if ((clk = '1') and clk'event) then
269
--    if    (rst = '1') then -- not to reset scratch register 
270
--      w          <= (others => '0')         ;
271
--      w0         <= (others => '0')         ;
272
      if (nld = '1') then                                                 -- 0 <= t <= 15 first 512 bit block
273
        w          <=                      im ;
274
  w0(1023 downto 0)<= (w0(959 downto  0) & im);
275
      else
276
        w          <=  iw                     ;
277
  w0(1023 downto 0)<= (w0(959 downto  0) & iw);
278
      end if;
279
    end if;
280
  end process;
281
 
282
  process (clk)
283
  begin
284
    if ((clk = '1') and clk'event) then
285
      if (rst = '1') then
286
        ild        <=  '0';
287
        nld        <=  '0';
288
        im         <= (others => '0');
289
      else
290
        ild        <=  nld;
291
        nld        <=   ld;
292
        im         <=    m;
293
      end if;
294
    end if;
295
  end process;
296
 
297
  process (clk)
298
  begin
299
    if ((clk = '1') and clk'event) then
300
      if ((ild_rst or rst) = '1') then
301
        vld        <=  '0';
302
      elsif (ctr3 = X"4f") then
303
        vld        <=  '1';
304
      else
305
        vld        <=  '0';
306
      end if;
307
    end if;
308
  end process;
309
 
310
  ild_rst          <= (ild xor ld) and ld;
311
--ctr2_rst         <=  ild_rst     or rst or vld or (ctr2 = X"7");        -- set to count to  7 (  8 clock)
312
  ctr2_rst         <=  ild_rst     or rst or vld or not(ctr2(3) or not(ctr2(2)) or not(ctr2(1)) or not(ctr2(0)));
313
--ctr3_rst         <=  ild_rst     or rst or (ctr3 = X"4f");              -- set to count to 79 ( 80 clock) 0100 1111
314
  ctr3_rst         <=  ild_rst     or rst or not(ctr3(7) or not(ctr3(6)) or ctr3(5) or ctr3(4) or not(ctr3(3)) or not(ctr3(2)) or not(ctr3(1)) or not(ctr3(0)));
315
 
316
  process (clk)
317
  begin
318
    if ((clk = '1') and clk'event) then
319
      if (init = '1')  or (rst = '1') then
320
        h0         <= X"6a09e667f3bcc908";
321
        h1         <= X"bb67ae8584caa73b";
322
        h2         <= X"3c6ef372fe94f82b";
323
        h3         <= X"a54ff53a5f1d36f1";
324
        h4         <= X"510e527fade682d1";
325
        h5         <= X"9b05688c2b3e6c1f";
326
        h6         <= X"1f83d9abfb41bd6b";
327
        h7         <= X"5be0cd19137e2179";
328
      elsif (vld = '1') then -- FIXME this adder is very costly and NOT A PORTABLE CODE
329
        h0         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(a)) + unsigned(to_stdlogicvector(h0)) ));
330
        h1         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(b)) + unsigned(to_stdlogicvector(h1)) ));
331
        h2         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(c)) + unsigned(to_stdlogicvector(h2)) ));
332
        h3         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(d)) + unsigned(to_stdlogicvector(h3)) ));
333
        h4         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(e)) + unsigned(to_stdlogicvector(h4)) ));
334
        h5         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(f)) + unsigned(to_stdlogicvector(h5)) ));
335
        h6         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(g)) + unsigned(to_stdlogicvector(h6)) ));
336
        h7         <= to_bitvector(std_logic_vector( unsigned(to_stdlogicvector(h)) + unsigned(to_stdlogicvector(h7)) ));
337
--      h0         <=      a + h0;
338
--      h1         <=      b + h1;
339
--      h2         <=      c + h2;
340
--      h3         <=      d + h3;
341
--      h4         <=      e + h4;
342
--      h5         <=      f + h5;
343
--      h6         <=      g + h6;
344
--      h7         <=      h + h7;
345
      end if;
346
    end if;
347
  end process;
348
 
349
  process (clk)
350
  begin
351
    if ((clk = '1') and clk'event) then
352
      if ((ild_rst or rst) = '1') then
353
        a          <= h0;
354
        b          <= h1;
355
        c          <= h2;
356
        d          <= h3;
357
        e          <= h4;
358
        f          <= h5;
359
        g          <= h6;
360
        h          <= h7;
361
       else -- FIXME this adder is very costly and NOT A PORTABLE CODE
362
--      T1         == h + f3(e) + f0(e, f, g) + k(t) + W(t)
363
--      T2         ==     f2(a) + f1(a, b, c)
364
        h          <=  g;
365
        g          <=  f;
366
        f          <=  e;
367
--      e          <=  d +          T1        ;
368
--      e          <=  d + h + f3 + f0 + k + w;
369
        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)) ));
370
        d          <=  c;
371
        c          <=  b;
372
        b          <=  a;
373
--      a          <=             T1           +    T2  ;
374
--      a          <=      h + f3 + f0 + k + w + f2 + f1;
375
        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)) ));
376
      end if;
377
    end if;
378
  end process;
379
 
380
  md               <=  ih;
381
  v                <=  vld;
382
 
383
end phy;

powered by: WebSVN 2.1.0

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