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

Subversion Repositories twofish

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 spyros
-- Twofish_ecb_vt_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 VARIABLE TEXT 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 vt_testbench128 is
32
end vt_testbench128;
33
 
34
architecture vt_encryption128_testbench_arch of vt_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_ecb_vt_testvalues_128bits.txt";
122
        FILE output_file : text is out "twofish_ecb_vt_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 1 to 129) return string is
144
                variable        our_text : string (1 to 3) := (others => ' ');
145
                variable hundreds,
146
                                                tens,
147
                                                ones            : integer range 0 to 9;
148
        begin
149
                ones := int_number mod 10;
150
                tens := ((int_number mod 100) - ones) / 10;
151
                hundreds := (int_number - (int_number mod 100)) / 100;
152
                our_text(1) := digit_to_char(hundreds);
153
                our_text(2) := digit_to_char(tens);
154
                our_text(3) := digit_to_char(ones);
155
                return our_text;
156
        end;
157
 
158
        signal                  odd_number,
159
                                even_number                                     : std_logic_vector(7 downto 0);
160
 
161
        signal                  input_data,
162
                                output_data,
163
                                twofish_key,
164
                                to_encr_reg128,
165
                                from_tdi_to_xors,
166
                                to_output_whit_xors,
167
                                from_xors_to_tdo,
168
                                to_mux, to_demux,
169
                                from_input_whit_xors,
170
                                to_round,
171
                                to_input_mux                                                    : std_logic_vector(127 downto 0) ;
172
 
173
        signal                  key_up,
174
                                key_down,
175
                                Sfirst,
176
                                Ssecond,
177
                                from_xor0,
178
                                from_xor1,
179
                                from_xor2,
180
                                from_xor3,
181
                                K0,K1,K2,K3,
182
                                K4,K5,K6,K7                                                             :  std_logic_vector(31 downto 0);
183
 
184
        signal                  clk                     : std_logic := '0';
185
        signal                  mux_selection   : std_logic := '0';
186
        signal                  demux_selection: std_logic := '0';
187
        signal                  enable_encr_reg : std_logic := '0';
188
        signal                  reset : std_logic := '0';
189
        signal                  enable_round_reg : std_logic := '0';
190
 
191
-- begin the testbench arch description
192
begin
193
 
194
 
195
        -- getting data to encrypt
196
        data_input: twofish_data_input
197
        port map        (
198
                                in_tdi  => input_data,
199
                                out_tdi => from_tdi_to_xors
200
                                );
201
 
202
        -- producing whitening keys K0..7
203
        the_whitening_step: twofish_whit_keysched128
204
        port    map (
205
                        in_key_twk128           => twofish_key,
206
                        out_K0_twk128 => K0,
207
                        out_K1_twk128 => K1,
208
                        out_K2_twk128 => K2,
209
                        out_K3_twk128 => K3,
210
                        out_K4_twk128 => K4,
211
                        out_K5_twk128 => K5,
212
                        out_K6_twk128 => K6,
213
                        out_K7_twk128 => K7
214
                        );
215
 
216
        -- performing the input whitening XORs
217
        from_xor0 <= K0 XOR from_tdi_to_xors(127 downto 96);
218
        from_xor1 <= K1 XOR from_tdi_to_xors(95 downto 64);
219
        from_xor2 <= K2 XOR from_tdi_to_xors(63 downto 32);
220
        from_xor3 <= K3 XOR from_tdi_to_xors(31 downto 0);
221
 
222
        from_input_whit_xors <= from_xor0 & from_xor1 & from_xor2 & from_xor3;
223
 
224
        round_reg: reg128
225
        port map ( in_reg128 => from_input_whit_xors,
226
                                out_reg128 => to_input_mux,
227
                                enable_reg128 => enable_round_reg,
228
                                reset_reg128 => reset,
229
                                clk_reg128 => clk );
230
 
231
        input_mux: mux128
232
        port map ( in1_mux128 => to_input_mux,
233
                                in2_mux128 => to_mux,
234
                                out_mux128 => to_round,
235
                                selection_mux128 => mux_selection
236
                                );
237
 
238
 
239
        -- creating a round
240
        the_keysched_of_the_round: twofish_keysched128
241
        port    map     (
242
                                odd_in_tk128 => odd_number,
243
                                even_in_tk128   => even_number,
244
                                in_key_tk128 => twofish_key,
245
                                out_key_up_tk128 => key_up,
246
                                out_key_down_tk128 => key_down
247
                                );
248
 
249
        producing_the_Skeys: twofish_S128
250
        port     map (
251
                                in_key_ts128            => twofish_key,
252
                                out_Sfirst_ts128 => Sfirst,
253
                                out_Ssecond_ts128 => Ssecond
254
                                );
255
 
256
        the_encryption_circuit: twofish_encryption_round128
257
        port map        (
258
                                in1_ter128 => to_round(127 downto 96),
259
                                in2_ter128 => to_round(95 downto 64),
260
                                in3_ter128 => to_round(63 downto 32),
261
                                in4_ter128 => to_round(31 downto 0),
262
                                in_Sfirst_ter128 => Sfirst,
263
                                in_Ssecond_ter128 => Ssecond,
264
                                in_key_up_ter128 => key_up,
265
                                in_key_down_ter128              => key_down,
266
                                out1_ter128 => to_encr_reg128(127 downto 96),
267
                                out2_ter128 => to_encr_reg128(95 downto 64),
268
                                out3_ter128 => to_encr_reg128(63 downto 32),
269
                                out4_ter128     => to_encr_reg128(31 downto 0)
270
                                );
271
 
272
        encr_reg: reg128
273
        port map ( in_reg128 => to_encr_reg128,
274
                                out_reg128 => to_demux,
275
                                enable_reg128 => enable_encr_reg,
276
                                reset_reg128 => reset,
277
                                clk_reg128 => clk );
278
 
279
        output_demux: demux128
280
        port map ( in_demux128 => to_demux,
281
                                        out1_demux128 => to_output_whit_xors,
282
                                        out2_demux128 => to_mux,
283
                                        selection_demux128 => demux_selection );
284
 
285
        -- don't forget the last swap !!!
286
        from_xors_to_tdo(127 downto 96) <= K4 XOR to_output_whit_xors(63 downto 32);
287
        from_xors_to_tdo(95 downto 64) <= K5 XOR to_output_whit_xors(31 downto 0);
288
        from_xors_to_tdo(63 downto 32) <= K6 XOR to_output_whit_xors(127 downto 96);
289
        from_xors_to_tdo(31 downto 0) <= K7 XOR to_output_whit_xors(95 downto 64);
290
 
291
        taking_the_output: twofish_data_output
292
        port    map (
293
                                in_tdo  => from_xors_to_tdo,
294
                                out_tdo => output_data
295
                                );
296
 
297
        -- we create the clock 
298
        clk <= not clk after 50 ns; -- period 100 ns
299
 
300
 
301
        vt_proc: process
302
 
303
                variable pt_f,  -- plaintext input from file
304
                                        ct_f    : line; -- ciphertext from file
305
                variable        pt_v,  -- plaintext vector input
306
                                        ct_v    : std_logic_vector(127 downto 0); -- ciphertext vector
307
 
308
                variable counter : integer range 1 to 129 := 1; -- counts the encryptions
309
                variable round : integer range 1 to 16 := 1; -- holds the rounds of encryption
310
 
311
        begin
312
 
313
                -- key stays fixed to zero
314
                twofish_key <= (others => '0');
315
 
316
                while not endfile(input_file) loop
317
                        readline(input_file, pt_f);
318
                        readline(input_file,ct_f);
319
                        hread(pt_f,pt_v);
320
                        hread(ct_f,ct_v);
321
                        input_data <= pt_v;
322
                        wait for 25 ns;
323
                        reset <= '1';
324
                        wait for 50 ns;
325
                        reset <= '0';
326
                        mux_selection <= '0';
327
                        demux_selection <= '1';
328
                        enable_encr_reg <= '0';
329
                        enable_round_reg <= '0';
330
                        wait for 50 ns;
331
                        enable_round_reg <= '1';
332
                        wait for 50 ns;
333
                        enable_round_reg <= '0';
334
 
335
                        -- the first round
336
                        even_number <= "00001000"; -- 8
337
                        odd_number <= "00001001"; -- 9
338
                        wait for 50 ns;
339
                        enable_encr_reg <= '1';
340
                        wait for 50 ns;
341
                        enable_encr_reg <= '0';
342
                        demux_selection <= '1';
343
                        mux_selection <= '1';
344
 
345
                        -- the rest 15 rounds
346
                        for round in 1 to 15 loop
347
                                even_number <= conv_std_logic_vector(((round*2)+8), 8);
348
                                odd_number <= conv_std_logic_vector(((round*2)+9), 8);
349
                                wait for 50 ns;
350
                                enable_encr_reg <= '1';
351
                                wait for 50 ns;
352
                                enable_encr_reg <= '0';
353
                        end loop;
354
 
355
                        -- taking final results
356
                        demux_selection <= '0';
357
                        wait for 25 ns;
358
                        assert (ct_v = output_data) report "file entry and encryption result DO NOT match!!! :( " severity failure;
359
                        assert (ct_v /= output_data) report "Encryption I=" & to_text(counter) &" OK" severity note;
360
                        counter := counter+1;
361
                        hwrite(ct_f,output_data);
362
                        hwrite(pt_f,pt_v);
363
                        writeline(output_file,pt_f);
364
                        writeline(output_file,ct_f);
365
                end loop;
366
                assert false    report  "***** Variable Text Known Answer Test with 128 bits key size ended succesfully! :) *****"      severity failure;
367
        end process vt_proc;
368
 
369
end vt_encryption128_testbench_arch;
370
 

powered by: WebSVN 2.1.0

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