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

Subversion Repositories twofish

[/] [twofish/] [trunk/] [vhdl/] [twofish_ecb_encryption_monte_carlo_testbench_256bits.vhd] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 spyros
-- Twofish_ecb_encryption_monte_carlo_testbench_256bits.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 VARIABLE TEXT KAT of the twofish cipher with 256 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_testbench256 is
32
end ecb_encryption_monte_carlo_testbench256;
33
 
34
architecture ecb_encryption256_monte_carlo_testbench_arch of ecb_encryption_monte_carlo_testbench256 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_keysched256
45
        port    (
46
                        odd_in_tk256,
47
                        even_in_tk256           : in std_logic_vector(7 downto 0);
48
                        in_key_tk256            : in std_logic_vector(255 downto 0);
49
                        out_key_up_tk256,
50
                        out_key_down_tk256      : out std_logic_vector(31 downto 0)
51
                        );
52
        end component;
53
 
54
        component twofish_whit_keysched256
55
        port    (
56
                        in_key_twk256           : in std_logic_vector(255 downto 0);
57
                        out_K0_twk256,
58
                        out_K1_twk256,
59
                        out_K2_twk256,
60
                        out_K3_twk256,
61
                        out_K4_twk256,
62
                        out_K5_twk256,
63
                        out_K6_twk256,
64
                        out_K7_twk256                   : out std_logic_vector(31 downto 0)
65
                        );
66
        end component;
67
 
68
        component twofish_encryption_round256
69
        port    (
70
                        in1_ter256,
71
                        in2_ter256,
72
                        in3_ter256,
73
                        in4_ter256,
74
                        in_Sfirst_ter256,
75
                        in_Ssecond_ter256,
76
                        in_Sthird_ter256,
77
                        in_Sfourth_ter256,
78
                        in_key_up_ter256,
79
                        in_key_down_ter256              : in std_logic_vector(31 downto 0);
80
                        out1_ter256,
81
                        out2_ter256,
82
                        out3_ter256,
83
                        out4_ter256                     : out std_logic_vector(31 downto 0)
84
                        );
85
        end component;
86
 
87
        component twofish_data_input
88
        port    (
89
                        in_tdi  : in std_logic_vector(127 downto 0);
90
                        out_tdi : out std_logic_vector(127 downto 0)
91
                        );
92
        end component;
93
 
94
        component twofish_data_output
95
        port    (
96
                        in_tdo  : in std_logic_vector(127 downto 0);
97
                        out_tdo : out std_logic_vector(127 downto 0)
98
                        );
99
        end component;
100
 
101
        component demux128
102
        port    ( in_demux128 : in std_logic_vector(127 downto 0);
103
                        out1_demux128, out2_demux128 : out std_logic_vector(127 downto 0);
104
                        selection_demux128 : in std_logic
105
                );
106
        end component;
107
 
108
        component mux128
109
        port ( in1_mux128, in2_mux128   : in std_logic_vector(127 downto 0);
110
                        selection_mux128        : in std_logic;
111
                        out_mux128 : out std_logic_vector(127 downto 0)
112
                );
113
        end component;
114
 
115
        component twofish_S256
116
        port    (
117
                        in_key_ts256            : in std_logic_vector(255 downto 0);
118
                        out_Sfirst_ts256,
119
                        out_Ssecond_ts256,
120
                        out_Sthird_ts256,
121
                        out_Sfourth_ts256                       : out std_logic_vector(31 downto 0)
122
                        );
123
        end component;
124
 
125
        FILE input_file : text is in "twofish_ecb_encryption_monte_carlo_testvalues_256bits.txt";
126
        FILE output_file : text is out "twofish_ecb_encryption_monte_carlo_256bits_results.txt";
127
 
128
        -- we create the functions that transform a number to text
129
        -- transforming a signle digit to a character
130
        function digit_to_char(number : integer range 0 to 9) return character is
131
        begin
132
                case number is
133
                        when 0 => return '0';
134
                        when 1 => return '1';
135
                        when 2 => return '2';
136
                        when 3 => return '3';
137
                        when 4 => return '4';
138
                        when 5 => return '5';
139
                        when 6 => return '6';
140
                        when 7 => return '7';
141
                        when 8 => return '8';
142
                        when 9 => return '9';
143
                end case;
144
        end;
145
 
146
        -- transforming multi-digit number to text
147
        function to_text(int_number : integer range 0 to 9999) return string is
148
                variable        our_text : string (1 to 4) := (others => ' ');
149
                variable thousands,
150
                                                hundreds,
151
                                                tens,
152
                                                ones            : integer range 0 to 9;
153
        begin
154
                ones := int_number mod 10;
155
                tens := ((int_number mod 100) - ones) / 10;
156
                hundreds := ((int_number mod 1000) - (int_number mod 100)) / 100;
157
                thousands := (int_number - (int_number mod 1000)) / 1000;
158
                our_text(1) := digit_to_char(thousands);
159
                our_text(2) := digit_to_char(hundreds);
160
                our_text(3) := digit_to_char(tens);
161
                our_text(4) := digit_to_char(ones);
162
                return our_text;
163
        end;
164
 
165
        signal                  odd_number,
166
                                even_number                                     : std_logic_vector(7 downto 0);
167
 
168
        signal                  input_data,
169
                                output_data,
170
                                to_encr_reg128,
171
                                from_tdi_to_xors,
172
                                to_output_whit_xors,
173
                                from_xors_to_tdo,
174
                                to_mux, to_demux,
175
                                from_input_whit_xors,
176
                                to_round,
177
                                to_input_mux                                                    : std_logic_vector(127 downto 0) ;
178
 
179
        signal                  twofish_key                             : std_logic_vector(255 downto 0);
180
 
181
        signal                  key_up,
182
                                key_down,
183
                                Sfirst,
184
                                Ssecond,
185
                                Sthird,
186
                                Sfourth,
187
                                from_xor0,
188
                                from_xor1,
189
                                from_xor2,
190
                                from_xor3,
191
                                K0,K1,K2,K3,
192
                                K4,K5,K6,K7                                                             :  std_logic_vector(31 downto 0);
193
 
194
        signal                  clk                     : std_logic := '0';
195
        signal                  mux_selection   : std_logic := '0';
196
        signal                  demux_selection: std_logic := '0';
197
        signal                  enable_encr_reg : std_logic := '0';
198
        signal                  reset : std_logic := '0';
199
        signal                  enable_round_reg : std_logic := '0';
200
 
201
-- begin the testbench arch description
202
begin
203
 
204
 
205
        -- getting data to encrypt
206
        data_input: twofish_data_input
207
        port map        (
208
                                in_tdi  => input_data,
209
                                out_tdi => from_tdi_to_xors
210
                                );
211
 
212
        -- producing whitening keys K0..7
213
        the_whitening_step: twofish_whit_keysched256
214
        port    map (
215
                        in_key_twk256           => twofish_key,
216
                        out_K0_twk256 => K0,
217
                        out_K1_twk256 => K1,
218
                        out_K2_twk256 => K2,
219
                        out_K3_twk256 => K3,
220
                        out_K4_twk256 => K4,
221
                        out_K5_twk256 => K5,
222
                        out_K6_twk256 => K6,
223
                        out_K7_twk256 => K7
224
                        );
225
 
226
        -- performing the input whitening XORs
227
        from_xor0 <= K0 XOR from_tdi_to_xors(127 downto 96);
228
        from_xor1 <= K1 XOR from_tdi_to_xors(95 downto 64);
229
        from_xor2 <= K2 XOR from_tdi_to_xors(63 downto 32);
230
        from_xor3 <= K3 XOR from_tdi_to_xors(31 downto 0);
231
 
232
        from_input_whit_xors <= from_xor0 & from_xor1 & from_xor2 & from_xor3;
233
 
234
        round_reg: reg128
235
        port map ( in_reg128 => from_input_whit_xors,
236
                                out_reg128 => to_input_mux,
237
                                enable_reg128 => enable_round_reg,
238
                                reset_reg128 => reset,
239
                                clk_reg128 => clk );
240
 
241
        input_mux: mux128
242
        port map ( in1_mux128 => to_input_mux,
243
                                in2_mux128 => to_mux,
244
                                out_mux128 => to_round,
245
                                selection_mux128 => mux_selection
246
                                );
247
 
248
 
249
        -- creating a round
250
        the_keysched_of_the_round: twofish_keysched256
251
        port    map     (
252
                                odd_in_tk256 => odd_number,
253
                                even_in_tk256   => even_number,
254
                                in_key_tk256 => twofish_key,
255
                                out_key_up_tk256 => key_up,
256
                                out_key_down_tk256 => key_down
257
                                );
258
 
259
        producing_the_Skeys: twofish_S256
260
        port     map (
261
                                in_key_ts256            => twofish_key,
262
                                out_Sfirst_ts256 => Sfirst,
263
                                out_Ssecond_ts256 => Ssecond,
264
                                out_Sthird_ts256 => Sthird,
265
                                out_Sfourth_ts256 => Sfourth
266
                                );
267
 
268
        the_encryption_circuit: twofish_encryption_round256
269
        port map        (
270
                                in1_ter256 => to_round(127 downto 96),
271
                                in2_ter256 => to_round(95 downto 64),
272
                                in3_ter256 => to_round(63 downto 32),
273
                                in4_ter256 => to_round(31 downto 0),
274
                                in_Sfirst_ter256 => Sfirst,
275
                                in_Ssecond_ter256 => Ssecond,
276
                                in_Sthird_ter256 => Sthird,
277
                                in_Sfourth_ter256 => Sfourth,
278
                                in_key_up_ter256 => key_up,
279
                                in_key_down_ter256              => key_down,
280
                                out1_ter256 => to_encr_reg128(127 downto 96),
281
                                out2_ter256 => to_encr_reg128(95 downto 64),
282
                                out3_ter256 => to_encr_reg128(63 downto 32),
283
                                out4_ter256     => to_encr_reg128(31 downto 0)
284
                                );
285
 
286
        encr_reg: reg128
287
        port map ( in_reg128 => to_encr_reg128,
288
                                out_reg128 => to_demux,
289
                                enable_reg128 => enable_encr_reg,
290
                                reset_reg128 => reset,
291
                                clk_reg128 => clk );
292
 
293
        output_demux: demux128
294
        port map ( in_demux128 => to_demux,
295
                                        out1_demux128 => to_output_whit_xors,
296
                                        out2_demux128 => to_mux,
297
                                        selection_demux128 => demux_selection );
298
 
299
        -- don't forget the last swap !!!
300
        from_xors_to_tdo(127 downto 96) <= K4 XOR to_output_whit_xors(63 downto 32);
301
        from_xors_to_tdo(95 downto 64) <= K5 XOR to_output_whit_xors(31 downto 0);
302
        from_xors_to_tdo(63 downto 32) <= K6 XOR to_output_whit_xors(127 downto 96);
303
        from_xors_to_tdo(31 downto 0) <= K7 XOR to_output_whit_xors(95 downto 64);
304
 
305
        taking_the_output: twofish_data_output
306
        port    map (
307
                                in_tdo  => from_xors_to_tdo,
308
                                out_tdo => output_data
309
                                );
310
 
311
        -- we create the clock 
312
        clk <= not clk after 50 ns; -- period 100 ns
313
 
314
 
315
        ecb_emc_proc: process
316
 
317
                variable        key_f,  -- key input from file
318
                                        pt_f,  -- plaintext from file
319
                                        ct_f    : line; -- ciphertext from file
320
                variable        key_v : std_logic_vector(255 downto 0);  -- key vector input
321
                variable                pt_v , -- plaintext vector
322
                                        ct_v    : std_logic_vector(127 downto 0); -- ciphertext vector
323
 
324
                variable counter_10000 : integer range 0 to 9999 := 0; -- counter for the 10.000 repeats in the 400 next ones
325
                variable counter_400 : integer range 0 to 399 := 0; -- counter for the 400 repeats
326
                variable round : integer range 0 to 16 := 0;  -- holds the rounds
327
                variable intermediate_encryption_result : std_logic_vector(127 downto 0); -- holds the intermediate encryption result 
328
 
329
        begin
330
 
331
                while not endfile(input_file) loop
332
 
333
                        readline(input_file, key_f);
334
                        readline(input_file, pt_f);
335
                        readline(input_file,ct_f);
336
                        hread(key_f,key_v);
337
                        hread(pt_f,pt_v);
338
                        hread(ct_f,ct_v);
339
 
340
                        twofish_key <= key_v;
341
                        intermediate_encryption_result := pt_v;
342
 
343
                        for counter_10000 in 0 to 9999 loop
344
                                input_data <= intermediate_encryption_result;
345
 
346
                                wait for 25 ns;
347
                                reset <= '1';
348
                                wait for 50 ns;
349
                                reset <= '0';
350
 
351
                                mux_selection <= '0';
352
                                demux_selection <= '1';
353
                                enable_encr_reg <= '0';
354
                                enable_round_reg <= '0';
355
                                wait for 50 ns;
356
                                enable_round_reg <= '1';
357
                                wait for 50 ns;
358
                                enable_round_reg <= '0';
359
 
360
                                -- the first round
361
                                even_number <= "00001000"; -- 8
362
                                odd_number <= "00001001"; -- 9
363
                                wait for 50 ns;
364
                                enable_encr_reg <= '1';
365
                                wait for 50 ns;
366
                                enable_encr_reg <= '0';
367
                                demux_selection <= '1';
368
                                mux_selection <= '1';
369
 
370
                                -- the rest 15 rounds
371
                                for round in 1 to 15 loop
372
                                        even_number <= conv_std_logic_vector(((round*2)+8), 8);
373
                                        odd_number <= conv_std_logic_vector(((round*2)+9), 8);
374
                                        wait for 50 ns;
375
                                        enable_encr_reg <= '1';
376
                                        wait for 50 ns;
377
                                        enable_encr_reg <= '0';
378
                                end loop;
379
 
380
                                -- taking final results
381
                                demux_selection <= '0';
382
                                wait for 25 ns;
383
 
384
                                intermediate_encryption_result := output_data;
385
 
386
                                assert false report "I=" & to_text(counter_400) & " R=" & to_text(counter_10000) severity note;
387
 
388
                        end loop; -- counter_10000
389
 
390
                        hwrite(key_f, key_v);
391
                        hwrite(pt_f, pt_v);
392
                        hwrite(ct_f,output_data);
393
                        writeline(output_file,key_f);
394
                        writeline(output_file,pt_f);
395
                        writeline(output_file,ct_f);
396
 
397
                        assert (ct_v = output_data) report "file entry and encryption result DO NOT match!!! :( " severity failure;
398
                        assert (ct_v /= output_data) report "Encryption I=" & to_text(counter_400) &" OK" severity note;
399
 
400
                        counter_400 := counter_400 + 1;
401
 
402
                end loop;
403
                assert false    report  "***** ECB Encryption Monte Carlo Test with 256 bits key size ended succesfully! :) *****"      severity failure;
404
        end process ecb_emc_proc;
405
 
406
end ecb_encryption256_monte_carlo_testbench_arch;
407
 

powered by: WebSVN 2.1.0

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