1 |
2 |
spyros |
-- Twofish_ecb_encryption_monte_carlo_testbench_192bits.vhd
|
2 |
|
|
-- Copyright (C) 2006 Spyros Ninos
|
3 |
|
|
--
|
4 |
|
|
-- This program is free software; you can redistribute it and/or modify
|
5 |
|
|
-- it under the terms of the GNU General Public License as published by
|
6 |
|
|
-- the Free Software Foundation; either version 2 of the License, or
|
7 |
|
|
-- (at your option) any later version.
|
8 |
|
|
--
|
9 |
|
|
-- This program is distributed in the hope that it will be useful,
|
10 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
|
-- GNU General Public License for more details.
|
13 |
|
|
--
|
14 |
|
|
-- You should have received a copy of the GNU General Public License
|
15 |
|
|
-- along with this library; see the file COPYING. If not, write to:
|
16 |
|
|
--
|
17 |
|
|
-- Free Software Foundation
|
18 |
|
|
-- 59 Temple Place - Suite 330
|
19 |
|
|
-- Boston, MA 02111-1307, USA.
|
20 |
|
|
--
|
21 |
|
|
-- description : this file is the testbench for the Encryption Monte Carlo KAT of the twofish cipher with 192 bit key
|
22 |
|
|
--
|
23 |
|
|
|
24 |
|
|
library ieee;
|
25 |
|
|
use ieee.std_logic_1164.all;
|
26 |
|
|
use ieee.std_logic_unsigned.all;
|
27 |
|
|
use ieee.std_logic_textio.all;
|
28 |
|
|
use ieee.std_logic_arith.all;
|
29 |
|
|
use std.textio.all;
|
30 |
|
|
|
31 |
|
|
entity ecb_encryption_monte_carlo_testbench192 is
|
32 |
|
|
end ecb_encryption_monte_carlo_testbench192;
|
33 |
|
|
|
34 |
|
|
architecture ecb_encryption192_monte_carlo_testbench_arch of ecb_encryption_monte_carlo_testbench192 is
|
35 |
|
|
|
36 |
|
|
component reg128
|
37 |
|
|
port (
|
38 |
|
|
in_reg128 : in std_logic_vector(127 downto 0);
|
39 |
|
|
out_reg128 : out std_logic_vector(127 downto 0);
|
40 |
|
|
enable_reg128, reset_reg128, clk_reg128 : in std_logic
|
41 |
|
|
);
|
42 |
|
|
end component;
|
43 |
|
|
|
44 |
|
|
component twofish_keysched192
|
45 |
|
|
port (
|
46 |
|
|
odd_in_tk192,
|
47 |
|
|
even_in_tk192 : in std_logic_vector(7 downto 0);
|
48 |
|
|
in_key_tk192 : in std_logic_vector(191 downto 0);
|
49 |
|
|
out_key_up_tk192,
|
50 |
|
|
out_key_down_tk192 : out std_logic_vector(31 downto 0)
|
51 |
|
|
);
|
52 |
|
|
end component;
|
53 |
|
|
|
54 |
|
|
component twofish_whit_keysched192
|
55 |
|
|
port (
|
56 |
|
|
in_key_twk192 : in std_logic_vector(191 downto 0);
|
57 |
|
|
out_K0_twk192,
|
58 |
|
|
out_K1_twk192,
|
59 |
|
|
out_K2_twk192,
|
60 |
|
|
out_K3_twk192,
|
61 |
|
|
out_K4_twk192,
|
62 |
|
|
out_K5_twk192,
|
63 |
|
|
out_K6_twk192,
|
64 |
|
|
out_K7_twk192 : out std_logic_vector(31 downto 0)
|
65 |
|
|
);
|
66 |
|
|
end component;
|
67 |
|
|
|
68 |
|
|
component twofish_encryption_round192
|
69 |
|
|
port (
|
70 |
|
|
in1_ter192,
|
71 |
|
|
in2_ter192,
|
72 |
|
|
in3_ter192,
|
73 |
|
|
in4_ter192,
|
74 |
|
|
in_Sfirst_ter192,
|
75 |
|
|
in_Ssecond_ter192,
|
76 |
|
|
in_Sthird_ter192,
|
77 |
|
|
in_key_up_ter192,
|
78 |
|
|
in_key_down_ter192 : in std_logic_vector(31 downto 0);
|
79 |
|
|
out1_ter192,
|
80 |
|
|
out2_ter192,
|
81 |
|
|
out3_ter192,
|
82 |
|
|
out4_ter192 : out std_logic_vector(31 downto 0)
|
83 |
|
|
);
|
84 |
|
|
end component;
|
85 |
|
|
|
86 |
|
|
component twofish_data_input
|
87 |
|
|
port (
|
88 |
|
|
in_tdi : in std_logic_vector(127 downto 0);
|
89 |
|
|
out_tdi : out std_logic_vector(127 downto 0)
|
90 |
|
|
);
|
91 |
|
|
end component;
|
92 |
|
|
|
93 |
|
|
component twofish_data_output
|
94 |
|
|
port (
|
95 |
|
|
in_tdo : in std_logic_vector(127 downto 0);
|
96 |
|
|
out_tdo : out std_logic_vector(127 downto 0)
|
97 |
|
|
);
|
98 |
|
|
end component;
|
99 |
|
|
|
100 |
|
|
component demux128
|
101 |
|
|
port ( in_demux128 : in std_logic_vector(127 downto 0);
|
102 |
|
|
out1_demux128, out2_demux128 : out std_logic_vector(127 downto 0);
|
103 |
|
|
selection_demux128 : in std_logic
|
104 |
|
|
);
|
105 |
|
|
end component;
|
106 |
|
|
|
107 |
|
|
component mux128
|
108 |
|
|
port ( in1_mux128, in2_mux128 : in std_logic_vector(127 downto 0);
|
109 |
|
|
selection_mux128 : in std_logic;
|
110 |
|
|
out_mux128 : out std_logic_vector(127 downto 0)
|
111 |
|
|
);
|
112 |
|
|
end component;
|
113 |
|
|
|
114 |
|
|
component twofish_S192
|
115 |
|
|
port (
|
116 |
|
|
in_key_ts192 : in std_logic_vector(191 downto 0);
|
117 |
|
|
out_Sfirst_ts192,
|
118 |
|
|
out_Ssecond_ts192,
|
119 |
|
|
out_Sthird_ts192 : out std_logic_vector(31 downto 0)
|
120 |
|
|
);
|
121 |
|
|
end component;
|
122 |
|
|
|
123 |
|
|
FILE input_file : text is in "twofish_ecb_encryption_monte_carlo_testvalues_192bits.txt";
|
124 |
|
|
FILE output_file : text is out "twofish_ecb_encryption_monte_carlo_192bits_results.txt";
|
125 |
|
|
|
126 |
|
|
-- we create the functions that transform a number to text
|
127 |
|
|
-- transforming a signle digit to a character
|
128 |
|
|
function digit_to_char(number : integer range 0 to 9) return character is
|
129 |
|
|
begin
|
130 |
|
|
case number is
|
131 |
|
|
when 0 => return '0';
|
132 |
|
|
when 1 => return '1';
|
133 |
|
|
when 2 => return '2';
|
134 |
|
|
when 3 => return '3';
|
135 |
|
|
when 4 => return '4';
|
136 |
|
|
when 5 => return '5';
|
137 |
|
|
when 6 => return '6';
|
138 |
|
|
when 7 => return '7';
|
139 |
|
|
when 8 => return '8';
|
140 |
|
|
when 9 => return '9';
|
141 |
|
|
end case;
|
142 |
|
|
end;
|
143 |
|
|
|
144 |
|
|
-- transforming multi-digit number to text
|
145 |
|
|
function to_text(int_number : integer range 0 to 9999) return string is
|
146 |
|
|
variable our_text : string (1 to 4) := (others => ' ');
|
147 |
|
|
variable thousands,
|
148 |
|
|
hundreds,
|
149 |
|
|
tens,
|
150 |
|
|
ones : integer range 0 to 9;
|
151 |
|
|
begin
|
152 |
|
|
ones := int_number mod 10;
|
153 |
|
|
tens := ((int_number mod 100) - ones) / 10;
|
154 |
|
|
hundreds := ((int_number mod 1000) - (int_number mod 100)) / 100;
|
155 |
|
|
thousands := (int_number - (int_number mod 1000)) / 1000;
|
156 |
|
|
our_text(1) := digit_to_char(thousands);
|
157 |
|
|
our_text(2) := digit_to_char(hundreds);
|
158 |
|
|
our_text(3) := digit_to_char(tens);
|
159 |
|
|
our_text(4) := digit_to_char(ones);
|
160 |
|
|
return our_text;
|
161 |
|
|
end;
|
162 |
|
|
|
163 |
|
|
signal odd_number,
|
164 |
|
|
even_number : std_logic_vector(7 downto 0);
|
165 |
|
|
|
166 |
|
|
signal input_data,
|
167 |
|
|
output_data,
|
168 |
|
|
to_encr_reg128,
|
169 |
|
|
from_tdi_to_xors,
|
170 |
|
|
to_output_whit_xors,
|
171 |
|
|
from_xors_to_tdo,
|
172 |
|
|
to_mux, to_demux,
|
173 |
|
|
from_input_whit_xors,
|
174 |
|
|
to_round,
|
175 |
|
|
to_input_mux : std_logic_vector(127 downto 0) ;
|
176 |
|
|
|
177 |
|
|
signal twofish_key : std_logic_vector(191 downto 0);
|
178 |
|
|
|
179 |
|
|
signal key_up,
|
180 |
|
|
key_down,
|
181 |
|
|
Sfirst,
|
182 |
|
|
Ssecond,
|
183 |
|
|
Sthird,
|
184 |
|
|
from_xor0,
|
185 |
|
|
from_xor1,
|
186 |
|
|
from_xor2,
|
187 |
|
|
from_xor3,
|
188 |
|
|
K0,K1,K2,K3,
|
189 |
|
|
K4,K5,K6,K7 : std_logic_vector(31 downto 0);
|
190 |
|
|
|
191 |
|
|
signal clk : std_logic := '0';
|
192 |
|
|
signal mux_selection : std_logic := '0';
|
193 |
|
|
signal demux_selection: std_logic := '0';
|
194 |
|
|
signal enable_encr_reg : std_logic := '0';
|
195 |
|
|
signal reset : std_logic := '0';
|
196 |
|
|
signal enable_round_reg : std_logic := '0';
|
197 |
|
|
|
198 |
|
|
-- begin the testbench arch description
|
199 |
|
|
begin
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
-- getting data to encrypt
|
203 |
|
|
data_input: twofish_data_input
|
204 |
|
|
port map (
|
205 |
|
|
in_tdi => input_data,
|
206 |
|
|
out_tdi => from_tdi_to_xors
|
207 |
|
|
);
|
208 |
|
|
|
209 |
|
|
-- producing whitening keys K0..7
|
210 |
|
|
the_whitening_step: twofish_whit_keysched192
|
211 |
|
|
port map (
|
212 |
|
|
in_key_twk192 => twofish_key,
|
213 |
|
|
out_K0_twk192 => K0,
|
214 |
|
|
out_K1_twk192 => K1,
|
215 |
|
|
out_K2_twk192 => K2,
|
216 |
|
|
out_K3_twk192 => K3,
|
217 |
|
|
out_K4_twk192 => K4,
|
218 |
|
|
out_K5_twk192 => K5,
|
219 |
|
|
out_K6_twk192 => K6,
|
220 |
|
|
out_K7_twk192 => K7
|
221 |
|
|
);
|
222 |
|
|
|
223 |
|
|
-- performing the input whitening XORs
|
224 |
|
|
from_xor0 <= K0 XOR from_tdi_to_xors(127 downto 96);
|
225 |
|
|
from_xor1 <= K1 XOR from_tdi_to_xors(95 downto 64);
|
226 |
|
|
from_xor2 <= K2 XOR from_tdi_to_xors(63 downto 32);
|
227 |
|
|
from_xor3 <= K3 XOR from_tdi_to_xors(31 downto 0);
|
228 |
|
|
|
229 |
|
|
from_input_whit_xors <= from_xor0 & from_xor1 & from_xor2 & from_xor3;
|
230 |
|
|
|
231 |
|
|
round_reg: reg128
|
232 |
|
|
port map ( in_reg128 => from_input_whit_xors,
|
233 |
|
|
out_reg128 => to_input_mux,
|
234 |
|
|
enable_reg128 => enable_round_reg,
|
235 |
|
|
reset_reg128 => reset,
|
236 |
|
|
clk_reg128 => clk );
|
237 |
|
|
|
238 |
|
|
input_mux: mux128
|
239 |
|
|
port map ( in1_mux128 => to_input_mux,
|
240 |
|
|
in2_mux128 => to_mux,
|
241 |
|
|
out_mux128 => to_round,
|
242 |
|
|
selection_mux128 => mux_selection
|
243 |
|
|
);
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
-- creating a round
|
247 |
|
|
the_keysched_of_the_round: twofish_keysched192
|
248 |
|
|
port map (
|
249 |
|
|
odd_in_tk192 => odd_number,
|
250 |
|
|
even_in_tk192 => even_number,
|
251 |
|
|
in_key_tk192 => twofish_key,
|
252 |
|
|
out_key_up_tk192 => key_up,
|
253 |
|
|
out_key_down_tk192 => key_down
|
254 |
|
|
);
|
255 |
|
|
|
256 |
|
|
producing_the_Skeys: twofish_S192
|
257 |
|
|
port map (
|
258 |
|
|
in_key_ts192 => twofish_key,
|
259 |
|
|
out_Sfirst_ts192 => Sfirst,
|
260 |
|
|
out_Ssecond_ts192 => Ssecond,
|
261 |
|
|
out_Sthird_ts192 => Sthird
|
262 |
|
|
);
|
263 |
|
|
|
264 |
|
|
the_encryption_circuit: twofish_encryption_round192
|
265 |
|
|
port map (
|
266 |
|
|
in1_ter192 => to_round(127 downto 96),
|
267 |
|
|
in2_ter192 => to_round(95 downto 64),
|
268 |
|
|
in3_ter192 => to_round(63 downto 32),
|
269 |
|
|
in4_ter192 => to_round(31 downto 0),
|
270 |
|
|
in_Sfirst_ter192 => Sfirst,
|
271 |
|
|
in_Ssecond_ter192 => Ssecond,
|
272 |
|
|
in_Sthird_ter192 => Sthird,
|
273 |
|
|
in_key_up_ter192 => key_up,
|
274 |
|
|
in_key_down_ter192 => key_down,
|
275 |
|
|
out1_ter192 => to_encr_reg128(127 downto 96),
|
276 |
|
|
out2_ter192 => to_encr_reg128(95 downto 64),
|
277 |
|
|
out3_ter192 => to_encr_reg128(63 downto 32),
|
278 |
|
|
out4_ter192 => to_encr_reg128(31 downto 0)
|
279 |
|
|
);
|
280 |
|
|
|
281 |
|
|
encr_reg: reg128
|
282 |
|
|
port map ( in_reg128 => to_encr_reg128,
|
283 |
|
|
out_reg128 => to_demux,
|
284 |
|
|
enable_reg128 => enable_encr_reg,
|
285 |
|
|
reset_reg128 => reset,
|
286 |
|
|
clk_reg128 => clk );
|
287 |
|
|
|
288 |
|
|
output_demux: demux128
|
289 |
|
|
port map ( in_demux128 => to_demux,
|
290 |
|
|
out1_demux128 => to_output_whit_xors,
|
291 |
|
|
out2_demux128 => to_mux,
|
292 |
|
|
selection_demux128 => demux_selection );
|
293 |
|
|
|
294 |
|
|
-- don't forget the last swap !!!
|
295 |
|
|
from_xors_to_tdo(127 downto 96) <= K4 XOR to_output_whit_xors(63 downto 32);
|
296 |
|
|
from_xors_to_tdo(95 downto 64) <= K5 XOR to_output_whit_xors(31 downto 0);
|
297 |
|
|
from_xors_to_tdo(63 downto 32) <= K6 XOR to_output_whit_xors(127 downto 96);
|
298 |
|
|
from_xors_to_tdo(31 downto 0) <= K7 XOR to_output_whit_xors(95 downto 64);
|
299 |
|
|
|
300 |
|
|
taking_the_output: twofish_data_output
|
301 |
|
|
port map (
|
302 |
|
|
in_tdo => from_xors_to_tdo,
|
303 |
|
|
out_tdo => output_data
|
304 |
|
|
);
|
305 |
|
|
|
306 |
|
|
-- we create the clock
|
307 |
|
|
clk <= not clk after 50 ns; -- period 100 ns
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
ecb_emc_proc: process
|
311 |
|
|
|
312 |
|
|
variable key_f, -- key input from file
|
313 |
|
|
pt_f, -- plaintext from file
|
314 |
|
|
ct_f : line; -- ciphertext from file
|
315 |
|
|
variable key_v : std_logic_vector(191 downto 0); -- key vector input
|
316 |
|
|
variable pt_v , -- plaintext vector
|
317 |
|
|
ct_v : std_logic_vector(127 downto 0); -- ciphertext vector
|
318 |
|
|
|
319 |
|
|
variable counter_10000 : integer range 0 to 9999 := 0; -- counter for the 10.000 repeats in the 400 next ones
|
320 |
|
|
variable counter_400 : integer range 0 to 399 := 0; -- counter for the 400 repeats
|
321 |
|
|
variable round : integer range 0 to 16 := 0; -- holds the rounds
|
322 |
|
|
variable intermediate_encryption_result : std_logic_vector(127 downto 0); -- holds the intermediate encryption result
|
323 |
|
|
|
324 |
|
|
begin
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
while not endfile(input_file) loop
|
329 |
|
|
|
330 |
|
|
readline(input_file, key_f);
|
331 |
|
|
readline(input_file, pt_f);
|
332 |
|
|
readline(input_file,ct_f);
|
333 |
|
|
hread(key_f,key_v);
|
334 |
|
|
hread(pt_f,pt_v);
|
335 |
|
|
hread(ct_f,ct_v);
|
336 |
|
|
|
337 |
|
|
twofish_key <= key_v;
|
338 |
|
|
intermediate_encryption_result := pt_v;
|
339 |
|
|
|
340 |
|
|
for counter_10000 in 0 to 9999 loop
|
341 |
|
|
input_data <= intermediate_encryption_result;
|
342 |
|
|
|
343 |
|
|
wait for 25 ns;
|
344 |
|
|
reset <= '1';
|
345 |
|
|
wait for 50 ns;
|
346 |
|
|
reset <= '0';
|
347 |
|
|
|
348 |
|
|
mux_selection <= '0';
|
349 |
|
|
demux_selection <= '1';
|
350 |
|
|
enable_encr_reg <= '0';
|
351 |
|
|
enable_round_reg <= '0';
|
352 |
|
|
wait for 50 ns;
|
353 |
|
|
enable_round_reg <= '1';
|
354 |
|
|
wait for 50 ns;
|
355 |
|
|
enable_round_reg <= '0';
|
356 |
|
|
|
357 |
|
|
-- the first round
|
358 |
|
|
even_number <= "00001000"; -- 8
|
359 |
|
|
odd_number <= "00001001"; -- 9
|
360 |
|
|
wait for 50 ns;
|
361 |
|
|
enable_encr_reg <= '1';
|
362 |
|
|
wait for 50 ns;
|
363 |
|
|
enable_encr_reg <= '0';
|
364 |
|
|
demux_selection <= '1';
|
365 |
|
|
mux_selection <= '1';
|
366 |
|
|
|
367 |
|
|
-- the rest 15 rounds
|
368 |
|
|
for round in 1 to 15 loop
|
369 |
|
|
even_number <= conv_std_logic_vector(((round*2)+8), 8);
|
370 |
|
|
odd_number <= conv_std_logic_vector(((round*2)+9), 8);
|
371 |
|
|
wait for 50 ns;
|
372 |
|
|
enable_encr_reg <= '1';
|
373 |
|
|
wait for 50 ns;
|
374 |
|
|
enable_encr_reg <= '0';
|
375 |
|
|
end loop;
|
376 |
|
|
|
377 |
|
|
-- taking final results
|
378 |
|
|
demux_selection <= '0';
|
379 |
|
|
wait for 25 ns;
|
380 |
|
|
|
381 |
|
|
intermediate_encryption_result := output_data;
|
382 |
|
|
|
383 |
|
|
assert false report "I=" & to_text(counter_400) & " R=" & to_text(counter_10000) severity note;
|
384 |
|
|
|
385 |
|
|
end loop; -- counter_10000
|
386 |
|
|
|
387 |
|
|
hwrite(key_f, key_v);
|
388 |
|
|
hwrite(pt_f, pt_v);
|
389 |
|
|
hwrite(ct_f,output_data);
|
390 |
|
|
writeline(output_file,key_f);
|
391 |
|
|
writeline(output_file,pt_f);
|
392 |
|
|
writeline(output_file,ct_f);
|
393 |
|
|
|
394 |
|
|
assert (ct_v = output_data) report "file entry and encryption result DO NOT match!!! :( " severity failure;
|
395 |
|
|
assert (ct_v /= output_data) report "Encryption I=" & to_text(counter_400) &" OK" severity note;
|
396 |
|
|
counter_400 := counter_400 + 1;
|
397 |
|
|
end loop;
|
398 |
|
|
assert false report "***** ECB Encryption Monte Carlo Test with 192 bits key size ended succesfully! :) *****" severity failure;
|
399 |
|
|
end process ecb_emc_proc;
|
400 |
|
|
|
401 |
|
|
end ecb_encryption192_monte_carlo_testbench_arch;
|
402 |
|
|
|