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

Subversion Repositories twofish

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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