1 |
6 |
lukasz.dzi |
--
|
2 |
|
|
-- Copyright 2012 iQUBE research
|
3 |
|
|
--
|
4 |
|
|
-- This file is part of Salsa20IpCore.
|
5 |
|
|
--
|
6 |
|
|
-- Salsa20IpCore is free software: you can redistribute it and/or modify
|
7 |
|
|
-- it under the terms of the GNU Lesser General Public License as published by
|
8 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
9 |
|
|
-- (at your option) any later version.
|
10 |
|
|
--
|
11 |
|
|
-- Salsa20IpCore is distributed in the hope that it will be useful,
|
12 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
-- GNU Lesser General Public License for more details.
|
15 |
|
|
--
|
16 |
|
|
-- You should have received a copy of the GNU Lesser General Public License
|
17 |
|
|
-- along with Salsa20IpCore. If not, see <http://www.gnu.org/licenses/>.
|
18 |
|
|
--
|
19 |
|
|
-- contact: Rúa Fonte das Abelleiras s/n, Campus Universitario de Vigo, 36310, Vigo (Spain)
|
20 |
|
|
-- e-mail: lukasz.dzianach@iqube.es, info@iqube.es
|
21 |
|
|
--
|
22 |
|
|
|
23 |
|
|
library IEEE;
|
24 |
|
|
use IEEE.std_logic_1164.all;
|
25 |
|
|
use IEEE.NUMERIC_STD.all;
|
26 |
|
|
use IEEE.std_logic_unsigned.all;
|
27 |
|
|
|
28 |
|
|
entity salsaa_mc is
|
29 |
|
|
port (
|
30 |
|
|
clk : in std_logic;
|
31 |
|
|
reset : in std_logic;
|
32 |
|
|
|
33 |
|
|
-- iface for user
|
34 |
|
|
key : in std_logic_vector(255 downto 0);
|
35 |
|
|
nonce : in std_logic_vector(63 downto 0);
|
36 |
|
|
start : in std_logic;
|
37 |
|
|
|
38 |
|
|
-- iface to salsaa_dm
|
39 |
|
|
mc_data : out std_logic_vector(511 downto 0);
|
40 |
|
|
mc_restart : in std_logic := '0';
|
41 |
|
|
mc_busy : out std_logic
|
42 |
|
|
|
43 |
|
|
);
|
44 |
|
|
end entity salsaa_mc;
|
45 |
|
|
|
46 |
|
|
architecture rtl of salsaa_mc is
|
47 |
|
|
|
48 |
|
|
type x_type is array(0 to 15) of std_logic_vector(31 downto 0);
|
49 |
|
|
|
50 |
|
|
constant salsa_const_0 : std_logic_vector := x"61707865";
|
51 |
|
|
constant salsa_const_1 : std_logic_vector := x"3320646e";
|
52 |
|
|
constant salsa_const_2 : std_logic_vector := x"79622d32";
|
53 |
|
|
constant salsa_const_3 : std_logic_vector := x"6b206574";
|
54 |
|
|
|
55 |
|
|
constant st_rst : std_logic_vector := x"0";
|
56 |
|
|
constant st_read_in : std_logic_vector := x"1";
|
57 |
|
|
constant st_save_init : std_logic_vector := x"2";
|
58 |
|
|
constant st_perf_rnds : std_logic_vector := x"3";
|
59 |
|
|
constant st_sum_res : std_logic_vector := x"4";
|
60 |
|
|
constant st_calc_done : std_logic_vector := x"5";
|
61 |
|
|
|
62 |
|
|
constant max_calc_state : std_logic_vector (7 downto 0) := x"07";
|
63 |
|
|
constant max_rnds_state : std_logic_vector (7 downto 0) := x"09";
|
64 |
|
|
|
65 |
|
|
signal x : x_type;
|
66 |
|
|
signal idx : std_logic_vector (63 downto 0);
|
67 |
|
|
|
68 |
|
|
signal state : std_logic_vector (3 downto 0);
|
69 |
|
|
signal calc_state : std_logic_vector (7 downto 0);
|
70 |
|
|
signal rnds_state : std_logic_vector (7 downto 0);
|
71 |
|
|
|
72 |
|
|
signal mc_data_buf : std_logic_vector(511 downto 0);
|
73 |
|
|
|
74 |
|
|
begin
|
75 |
|
|
|
76 |
|
|
mc_data <= mc_data_buf;
|
77 |
|
|
|
78 |
|
|
main: process(clk) is
|
79 |
|
|
begin
|
80 |
|
|
if (clk'event and clk='1') then
|
81 |
|
|
if (reset='1') then
|
82 |
|
|
state <= st_rst;
|
83 |
|
|
mc_busy <= '1';
|
84 |
|
|
else
|
85 |
|
|
|
86 |
|
|
case state is
|
87 |
|
|
|
88 |
|
|
when st_rst =>
|
89 |
|
|
if start = '1' then
|
90 |
|
|
state <= st_read_in;
|
91 |
|
|
end if;
|
92 |
|
|
|
93 |
|
|
-- reseting the index (as new nonce can is being read)
|
94 |
|
|
idx <= x"0000000000000000";
|
95 |
|
|
mc_busy <= '1';
|
96 |
|
|
|
97 |
|
|
when st_read_in =>
|
98 |
|
|
|
99 |
|
|
state <= st_save_init;
|
100 |
|
|
|
101 |
|
|
mc_busy <= '1';
|
102 |
|
|
|
103 |
|
|
x(0) <= salsa_const_0;
|
104 |
|
|
x(1) <= key(32 * 0 + 31 downto 32 * 0 + 0);
|
105 |
|
|
x(2) <= key(32 * 1 + 31 downto 32 * 1 + 0);
|
106 |
|
|
x(3) <= key(32 * 2 + 31 downto 32 * 2 + 0);
|
107 |
|
|
x(4) <= key(32 * 3 + 31 downto 32 * 3 + 0);
|
108 |
|
|
x(5) <= salsa_const_1;
|
109 |
|
|
x(6) <= nonce(32 * 0 + 31 downto 32 * 0 + 0);
|
110 |
|
|
x(7) <= nonce(32 * 1 + 31 downto 32 * 1 + 0);
|
111 |
|
|
x(8) <= idx(32 * 0 + 31 downto 32 * 0 + 0);
|
112 |
|
|
x(9) <= idx(32 * 1 + 31 downto 32 * 1 + 0);
|
113 |
|
|
x(10) <= salsa_const_2;
|
114 |
|
|
x(11) <= key(32 * 4 + 31 downto 32 * 4 + 0);
|
115 |
|
|
x(12) <= key(32 * 5 + 31 downto 32 * 5 + 0);
|
116 |
|
|
x(13) <= key(32 * 6 + 31 downto 32 * 6 + 0);
|
117 |
|
|
x(14) <= key(32 * 7 + 31 downto 32 * 7 + 0);
|
118 |
|
|
x(15) <= salsa_const_3;
|
119 |
|
|
|
120 |
|
|
when st_save_init =>
|
121 |
|
|
|
122 |
|
|
-- prepare seq of runds
|
123 |
|
|
state <= st_perf_rnds;
|
124 |
|
|
calc_state <= x"00";
|
125 |
|
|
rnds_state <= x"00";
|
126 |
|
|
|
127 |
|
|
-- storing initial state of x
|
128 |
|
|
mc_data_buf(32 * 00 + 31 downto 32 * 00 + 0) <= x(00);
|
129 |
|
|
mc_data_buf(32 * 01 + 31 downto 32 * 01 + 0) <= x(01);
|
130 |
|
|
mc_data_buf(32 * 02 + 31 downto 32 * 02 + 0) <= x(02);
|
131 |
|
|
mc_data_buf(32 * 03 + 31 downto 32 * 03 + 0) <= x(03);
|
132 |
|
|
mc_data_buf(32 * 04 + 31 downto 32 * 04 + 0) <= x(04);
|
133 |
|
|
mc_data_buf(32 * 05 + 31 downto 32 * 05 + 0) <= x(05);
|
134 |
|
|
mc_data_buf(32 * 06 + 31 downto 32 * 06 + 0) <= x(06);
|
135 |
|
|
mc_data_buf(32 * 07 + 31 downto 32 * 07 + 0) <= x(07);
|
136 |
|
|
mc_data_buf(32 * 08 + 31 downto 32 * 08 + 0) <= x(08);
|
137 |
|
|
mc_data_buf(32 * 09 + 31 downto 32 * 09 + 0) <= x(09);
|
138 |
|
|
mc_data_buf(32 * 10 + 31 downto 32 * 10 + 0) <= x(10);
|
139 |
|
|
mc_data_buf(32 * 11 + 31 downto 32 * 11 + 0) <= x(11);
|
140 |
|
|
mc_data_buf(32 * 12 + 31 downto 32 * 12 + 0) <= x(12);
|
141 |
|
|
mc_data_buf(32 * 13 + 31 downto 32 * 13 + 0) <= x(13);
|
142 |
|
|
mc_data_buf(32 * 14 + 31 downto 32 * 14 + 0) <= x(14);
|
143 |
|
|
mc_data_buf(32 * 15 + 31 downto 32 * 15 + 0) <= x(15);
|
144 |
|
|
|
145 |
|
|
when st_perf_rnds =>
|
146 |
|
|
calc_state <= calc_state + x"01";
|
147 |
|
|
if calc_state = max_calc_state then
|
148 |
|
|
if rnds_state = max_rnds_state then
|
149 |
|
|
state <= st_sum_res;
|
150 |
|
|
else
|
151 |
|
|
calc_state <= x"00";
|
152 |
|
|
rnds_state <= rnds_state + x"01";
|
153 |
|
|
end if;
|
154 |
|
|
end if;
|
155 |
|
|
|
156 |
|
|
-- processing goes here
|
157 |
|
|
case calc_state is
|
158 |
|
|
when x"00" =>
|
159 |
|
|
x(04) <= x(04) xor std_logic_vector( rotate_left(unsigned(x(12)+x(00)),7));
|
160 |
|
|
x(09) <= x(09) xor std_logic_vector( rotate_left(unsigned(x(01)+x(05)),7));
|
161 |
|
|
x(14) <= x(14) xor std_logic_vector( rotate_left(unsigned(x(06)+x(10)),7));
|
162 |
|
|
x(03) <= x(03) xor std_logic_vector( rotate_left(unsigned(x(11)+x(15)),7));
|
163 |
|
|
when x"01" =>
|
164 |
|
|
x(08) <= x(08) xor std_logic_vector( rotate_left(unsigned(x(00)+x(04)),9));
|
165 |
|
|
x(13) <= x(13) xor std_logic_vector( rotate_left(unsigned(x(05)+x(09)),9));
|
166 |
|
|
x(02) <= x(02) xor std_logic_vector( rotate_left(unsigned(x(10)+x(14)),9));
|
167 |
|
|
x(07) <= x(07) xor std_logic_vector( rotate_left(unsigned(x(15)+x(03)),9));
|
168 |
|
|
when x"02" =>
|
169 |
|
|
x(12) <= x(12) xor std_logic_vector( rotate_left(unsigned(x(04)+x(08)),13));
|
170 |
|
|
x(01) <= x(01) xor std_logic_vector( rotate_left(unsigned(x(09)+x(13)),13));
|
171 |
|
|
x(06) <= x(06) xor std_logic_vector( rotate_left(unsigned(x(14)+x(02)),13));
|
172 |
|
|
x(11) <= x(11) xor std_logic_vector( rotate_left(unsigned(x(03)+x(07)),13));
|
173 |
|
|
when x"03" =>
|
174 |
|
|
x(00) <= x(00) xor std_logic_vector( rotate_left(unsigned(x(08)+x(12)),18));
|
175 |
|
|
x(05) <= x(05) xor std_logic_vector( rotate_left(unsigned(x(13)+x(01)),18));
|
176 |
|
|
x(10) <= x(10) xor std_logic_vector( rotate_left(unsigned(x(02)+x(06)),18));
|
177 |
|
|
x(15) <= x(15) xor std_logic_vector( rotate_left(unsigned(x(07)+x(11)),18));
|
178 |
|
|
|
179 |
|
|
when x"04" =>
|
180 |
|
|
x(01) <= x(01) xor std_logic_vector( rotate_left(unsigned(x(03)+x(00)),07));
|
181 |
|
|
x(06) <= x(06) xor std_logic_vector( rotate_left(unsigned(x(04)+x(05)),07));
|
182 |
|
|
x(11) <= x(11) xor std_logic_vector( rotate_left(unsigned(x(09)+x(10)),07));
|
183 |
|
|
x(12) <= x(12) xor std_logic_vector( rotate_left(unsigned(x(14)+x(15)),07));
|
184 |
|
|
when x"05" =>
|
185 |
|
|
x(02) <= x(02) xor std_logic_vector( rotate_left(unsigned(x(00)+x(01)),09));
|
186 |
|
|
x(07) <= x(07) xor std_logic_vector( rotate_left(unsigned(x(05)+x(06)),09));
|
187 |
|
|
x(08) <= x(08) xor std_logic_vector( rotate_left(unsigned(x(10)+x(11)),09));
|
188 |
|
|
x(13) <= x(13) xor std_logic_vector( rotate_left(unsigned(x(15)+x(12)),09));
|
189 |
|
|
when x"06" =>
|
190 |
|
|
x(03) <= x(03) xor std_logic_vector( rotate_left(unsigned(x(01)+x(02)),13));
|
191 |
|
|
x(04) <= x(04) xor std_logic_vector( rotate_left(unsigned(x(06)+x(07)),13));
|
192 |
|
|
x(09) <= x(09) xor std_logic_vector( rotate_left(unsigned(x(11)+x(08)),13));
|
193 |
|
|
x(14) <= x(14) xor std_logic_vector( rotate_left(unsigned(x(12)+x(13)),13));
|
194 |
|
|
when x"07" =>
|
195 |
|
|
x(00) <= x(00) xor std_logic_vector( rotate_left(unsigned(x(02)+x(03)),18));
|
196 |
|
|
x(05) <= x(05) xor std_logic_vector( rotate_left(unsigned(x(07)+x(04)),18));
|
197 |
|
|
x(10) <= x(10) xor std_logic_vector( rotate_left(unsigned(x(08)+x(09)),18));
|
198 |
|
|
x(15) <= x(15) xor std_logic_vector( rotate_left(unsigned(x(13)+x(14)),18));
|
199 |
|
|
when others =>
|
200 |
|
|
null;
|
201 |
|
|
end case;
|
202 |
|
|
|
203 |
|
|
when st_sum_res =>
|
204 |
|
|
|
205 |
|
|
-- preparing index for the next random block reneration
|
206 |
|
|
idx <= idx + x"0000000000000001";
|
207 |
|
|
|
208 |
|
|
mc_data_buf(32 * 00 + 31 downto 32 * 00 + 0) <= mc_data_buf(32 * 00 + 31 downto 32 * 00 + 0) + x(00);
|
209 |
|
|
mc_data_buf(32 * 01 + 31 downto 32 * 01 + 0) <= mc_data_buf(32 * 01 + 31 downto 32 * 01 + 0) + x(01);
|
210 |
|
|
mc_data_buf(32 * 02 + 31 downto 32 * 02 + 0) <= mc_data_buf(32 * 02 + 31 downto 32 * 02 + 0) + x(02);
|
211 |
|
|
mc_data_buf(32 * 03 + 31 downto 32 * 03 + 0) <= mc_data_buf(32 * 03 + 31 downto 32 * 03 + 0) + x(03);
|
212 |
|
|
mc_data_buf(32 * 04 + 31 downto 32 * 04 + 0) <= mc_data_buf(32 * 04 + 31 downto 32 * 04 + 0) + x(04);
|
213 |
|
|
mc_data_buf(32 * 05 + 31 downto 32 * 05 + 0) <= mc_data_buf(32 * 05 + 31 downto 32 * 05 + 0) + x(05);
|
214 |
|
|
mc_data_buf(32 * 06 + 31 downto 32 * 06 + 0) <= mc_data_buf(32 * 06 + 31 downto 32 * 06 + 0) + x(06);
|
215 |
|
|
mc_data_buf(32 * 07 + 31 downto 32 * 07 + 0) <= mc_data_buf(32 * 07 + 31 downto 32 * 07 + 0) + x(07);
|
216 |
|
|
mc_data_buf(32 * 08 + 31 downto 32 * 08 + 0) <= mc_data_buf(32 * 08 + 31 downto 32 * 08 + 0) + x(08);
|
217 |
|
|
mc_data_buf(32 * 09 + 31 downto 32 * 09 + 0) <= mc_data_buf(32 * 09 + 31 downto 32 * 09 + 0) + x(09);
|
218 |
|
|
mc_data_buf(32 * 10 + 31 downto 32 * 10 + 0) <= mc_data_buf(32 * 10 + 31 downto 32 * 10 + 0) + x(10);
|
219 |
|
|
mc_data_buf(32 * 11 + 31 downto 32 * 11 + 0) <= mc_data_buf(32 * 11 + 31 downto 32 * 11 + 0) + x(11);
|
220 |
|
|
mc_data_buf(32 * 12 + 31 downto 32 * 12 + 0) <= mc_data_buf(32 * 12 + 31 downto 32 * 12 + 0) + x(12);
|
221 |
|
|
mc_data_buf(32 * 13 + 31 downto 32 * 13 + 0) <= mc_data_buf(32 * 13 + 31 downto 32 * 13 + 0) + x(13);
|
222 |
|
|
mc_data_buf(32 * 14 + 31 downto 32 * 14 + 0) <= mc_data_buf(32 * 14 + 31 downto 32 * 14 + 0) + x(14);
|
223 |
|
|
mc_data_buf(32 * 15 + 31 downto 32 * 15 + 0) <= mc_data_buf(32 * 15 + 31 downto 32 * 15 + 0) + x(15);
|
224 |
|
|
|
225 |
|
|
mc_busy <= '0';
|
226 |
|
|
|
227 |
|
|
state <= st_calc_done;
|
228 |
|
|
|
229 |
|
|
when st_calc_done =>
|
230 |
|
|
|
231 |
|
|
if mc_restart = '1' then
|
232 |
|
|
-- new block requested with new input
|
233 |
|
|
state <= st_read_in;
|
234 |
|
|
end if;
|
235 |
|
|
|
236 |
|
|
when others =>
|
237 |
|
|
null;
|
238 |
|
|
|
239 |
|
|
end case;
|
240 |
|
|
|
241 |
|
|
end if;
|
242 |
|
|
end if;
|
243 |
|
|
end process;
|
244 |
|
|
|
245 |
|
|
end architecture rtl;
|
246 |
|
|
|