1 |
3 |
nuxi1209 |
-------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- Copyright (C) 2013 Author and VariStream Studio --
|
4 |
|
|
-- Author : Yu Peng --
|
5 |
|
|
-- --
|
6 |
|
|
-- This source file may be used and distributed without --
|
7 |
|
|
-- restriction provided that this copyright statement is not --
|
8 |
|
|
-- removed from the file and that any derivative work contains --
|
9 |
|
|
-- the original copyright notice and the associated disclaimer. --
|
10 |
|
|
-- --
|
11 |
|
|
-- This source file is free software; you can redistribute it --
|
12 |
|
|
-- and/or modify it under the terms of the GNU Lesser General --
|
13 |
|
|
-- Public License as published by the Free Software Foundation; --
|
14 |
|
|
-- either version 2.1 of the License, or (at your option) any --
|
15 |
|
|
-- later version. --
|
16 |
|
|
-- --
|
17 |
|
|
-- This source is distributed in the hope that it will be --
|
18 |
|
|
-- useful, but WITHOUT ANY WARRANTY; without even the implied --
|
19 |
|
|
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR --
|
20 |
|
|
-- PURPOSE. See the GNU Lesser General Public License for more --
|
21 |
|
|
-- details. --
|
22 |
|
|
-- --
|
23 |
|
|
-- You should have received a copy of the GNU Lesser General --
|
24 |
|
|
-- Public License along with this source; if not, download it --
|
25 |
|
|
-- from http://www.opencores.org/lgpl.shtml --
|
26 |
|
|
-- --
|
27 |
|
|
-------------------------------------------------------------------
|
28 |
2 |
nuxi1209 |
|
29 |
|
|
library IEEE;
|
30 |
|
|
use IEEE.STD_LOGIC_1164.all;
|
31 |
|
|
use IEEE.std_logic_arith.all;
|
32 |
|
|
|
33 |
|
|
package sha_256_pkg is
|
34 |
|
|
type tDwordArray is array (natural range<>) of std_logic_vector(31 downto 0);
|
35 |
|
|
|
36 |
|
|
function sigma_0 (signal x : in std_logic_vector) return std_logic_vector;
|
37 |
|
|
function sigma_1 (signal x : in std_logic_vector) return std_logic_vector;
|
38 |
|
|
|
39 |
|
|
function sum_0 (signal x : in std_logic_vector) return std_logic_vector;
|
40 |
|
|
function sum_1 (signal x : in std_logic_vector) return std_logic_vector;
|
41 |
|
|
|
42 |
|
|
function chi (signal x : in std_logic_vector;
|
43 |
|
|
signal y : in std_logic_vector;
|
44 |
|
|
signal z : in std_logic_vector) return std_logic_vector;
|
45 |
|
|
|
46 |
|
|
function maj (signal x : in std_logic_vector;
|
47 |
|
|
signal y : in std_logic_vector;
|
48 |
|
|
signal z : in std_logic_vector) return std_logic_vector;
|
49 |
|
|
|
50 |
|
|
function getW_IS_CONST (is_const : in std_logic_vector) return std_logic_vector;
|
51 |
|
|
function conv_str_to_msg (str : in string) return tDwordArray;
|
52 |
|
|
end sha_256_pkg;
|
53 |
|
|
|
54 |
|
|
package body sha_256_pkg is
|
55 |
|
|
|
56 |
|
|
function sigma_0 (signal x : in std_logic_vector) return std_logic_vector is
|
57 |
|
|
variable tmp_0 : std_logic_vector(31 downto 0);
|
58 |
|
|
variable tmp_1 : std_logic_vector(31 downto 0);
|
59 |
|
|
variable tmp_2 : std_logic_vector(31 downto 0);
|
60 |
|
|
variable r : std_logic_vector(31 downto 0);
|
61 |
|
|
begin
|
62 |
|
|
tmp_0 := x(6 downto 0) & x(31 downto 7);
|
63 |
|
|
tmp_1 := x(17 downto 0) & x(31 downto 18);
|
64 |
|
|
tmp_2 := "000" & x(31 downto 3);
|
65 |
|
|
|
66 |
|
|
r := tmp_0 xor tmp_1 xor tmp_2;
|
67 |
|
|
|
68 |
|
|
return r;
|
69 |
|
|
end sigma_0;
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
function sigma_1 (signal x : in std_logic_vector) return std_logic_vector is
|
73 |
|
|
variable tmp_0 : std_logic_vector(31 downto 0);
|
74 |
|
|
variable tmp_1 : std_logic_vector(31 downto 0);
|
75 |
|
|
variable tmp_2 : std_logic_vector(31 downto 0);
|
76 |
|
|
|
77 |
|
|
variable r : std_logic_vector(31 downto 0);
|
78 |
|
|
begin
|
79 |
|
|
tmp_0 := x(16 downto 0) & x(31 downto 17);
|
80 |
|
|
tmp_1 := x(18 downto 0) & x(31 downto 19);
|
81 |
|
|
tmp_2 := "0000000000" & x(31 downto 10);
|
82 |
|
|
|
83 |
|
|
r := tmp_0 xor tmp_1 xor tmp_2;
|
84 |
|
|
|
85 |
|
|
return r;
|
86 |
|
|
end sigma_1;
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
function chi (signal x : in std_logic_vector;
|
90 |
|
|
signal y : in std_logic_vector;
|
91 |
|
|
signal z : in std_logic_vector) return std_logic_vector is
|
92 |
|
|
|
93 |
|
|
variable r : std_logic_vector(31 downto 0);
|
94 |
|
|
|
95 |
|
|
begin
|
96 |
|
|
r := (x and y) xor (not(x) and z);
|
97 |
|
|
|
98 |
|
|
return r;
|
99 |
|
|
end chi;
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
function maj (signal x : in std_logic_vector;
|
103 |
|
|
signal y : in std_logic_vector;
|
104 |
|
|
signal z : in std_logic_vector) return std_logic_vector is
|
105 |
|
|
|
106 |
|
|
variable r : std_logic_vector(31 downto 0);
|
107 |
|
|
|
108 |
|
|
begin
|
109 |
|
|
r := (x and y) xor (x and z) xor (y and z);
|
110 |
|
|
|
111 |
|
|
return r;
|
112 |
|
|
|
113 |
|
|
end maj;
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
function sum_0 (signal x : in std_logic_vector) return std_logic_vector is
|
117 |
|
|
variable tmp_0 : std_logic_vector(31 downto 0);
|
118 |
|
|
variable tmp_1 : std_logic_vector(31 downto 0);
|
119 |
|
|
variable tmp_2 : std_logic_vector(31 downto 0);
|
120 |
|
|
|
121 |
|
|
variable r : std_logic_vector(31 downto 0);
|
122 |
|
|
begin
|
123 |
|
|
tmp_0 := x(1 downto 0) & x(31 downto 2);
|
124 |
|
|
tmp_1 := x(12 downto 0) & x(31 downto 13);
|
125 |
|
|
tmp_2 := x(21 downto 0) & x(31 downto 22);
|
126 |
|
|
|
127 |
|
|
r := tmp_0 xor tmp_1 xor tmp_2;
|
128 |
|
|
|
129 |
|
|
return r;
|
130 |
|
|
|
131 |
|
|
end sum_0;
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
function sum_1 (signal x : in std_logic_vector) return std_logic_vector is
|
135 |
|
|
variable tmp_0 : std_logic_vector(31 downto 0);
|
136 |
|
|
variable tmp_1 : std_logic_vector(31 downto 0);
|
137 |
|
|
variable tmp_2 : std_logic_vector(31 downto 0);
|
138 |
|
|
|
139 |
|
|
variable r : std_logic_vector(31 downto 0);
|
140 |
|
|
begin
|
141 |
|
|
|
142 |
|
|
tmp_0 := x(5 downto 0) & x(31 downto 6);
|
143 |
|
|
tmp_1 := x(10 downto 0) & x(31 downto 11);
|
144 |
|
|
tmp_2 := x(24 downto 0) & x(31 downto 25);
|
145 |
|
|
|
146 |
|
|
r := tmp_0 xor tmp_1 xor tmp_2;
|
147 |
|
|
|
148 |
|
|
return r;
|
149 |
|
|
end sum_1;
|
150 |
|
|
|
151 |
|
|
function getW_IS_CONST (is_const : in std_logic_vector) return std_logic_vector is
|
152 |
|
|
variable r : std_logic_vector(0 to 63);
|
153 |
|
|
begin
|
154 |
|
|
for i in 0 to 15 loop
|
155 |
|
|
r(i) := is_const(i);
|
156 |
|
|
end loop;
|
157 |
|
|
|
158 |
|
|
for i in 16 to 63 loop
|
159 |
|
|
r(i) := r(i-16) and r(i-15) and r(i-7) and r(i-2);
|
160 |
|
|
end loop;
|
161 |
|
|
|
162 |
|
|
return r;
|
163 |
|
|
end getW_IS_CONST;
|
164 |
|
|
|
165 |
|
|
function conv_str_to_msg (str : in string) return tDwordArray is
|
166 |
|
|
variable str_len : integer := str'length;
|
167 |
|
|
variable retval : tDwordArray(0 to 15) := (others=>(others=>'0'));
|
168 |
|
|
begin
|
169 |
|
|
if str_len > 0 then
|
170 |
|
|
for i in 0 to (str_len - 1) loop
|
171 |
|
|
retval(i / 4)(((4 - (i mod 4)) * 8 - 1) downto ((3 - (i mod 4)) * 8)) := conv_std_logic_vector(character'pos(str(i + 1)), 8);
|
172 |
|
|
end loop;
|
173 |
|
|
end if;
|
174 |
|
|
|
175 |
|
|
retval(str_len / 4)(((4 - (str_len mod 4)) * 8 - 1) downto ((3 - (str_len mod 4)) * 8)) := X"80";
|
176 |
|
|
retval(15) := conv_std_logic_vector((str_len * 8), 32);
|
177 |
|
|
|
178 |
|
|
return retVal;
|
179 |
|
|
end conv_str_to_msg;
|
180 |
|
|
|
181 |
|
|
end sha_256_pkg;
|