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

Subversion Repositories twofish

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 spyros
-- Twofish_ecb_vk_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 KEY 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 vk_testbench256 is
32
end vk_testbench256;
33
 
34
architecture vk_encryption256_testbench_arch of vk_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_vk_testvalues_256bits.txt";
126
        FILE output_file : text is out "twofish_ecb_vk_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 1 to 256) return string is
148
                variable        our_text : string (1 to 3) := (others => ' ');
149
                variable hundreds,
150
                                                tens,
151
                                                ones            : integer range 0 to 9;
152
        begin
153
                ones := int_number mod 10;
154
                tens := ((int_number mod 100) - ones) / 10;
155
                hundreds := (int_number - (int_number mod 100)) / 100;
156
                our_text(1) := digit_to_char(hundreds);
157
                our_text(2) := digit_to_char(tens);
158
                our_text(3) := digit_to_char(ones);
159
                return our_text;
160
        end;
161
 
162
        signal                  odd_number,
163
                                even_number                                     : std_logic_vector(7 downto 0);
164
 
165
        signal                  input_data,
166
                                output_data,
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                  twofish_key                             : std_logic_vector(255 downto 0);
177
 
178
        signal                  key_up,
179
                                key_down,
180
                                Sfirst,
181
                                Ssecond,
182
                                Sthird,
183
                                Sfourth,
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_keysched256
211
        port    map (
212
                        in_key_twk256           => twofish_key,
213
                        out_K0_twk256 => K0,
214
                        out_K1_twk256 => K1,
215
                        out_K2_twk256 => K2,
216
                        out_K3_twk256 => K3,
217
                        out_K4_twk256 => K4,
218
                        out_K5_twk256 => K5,
219
                        out_K6_twk256 => K6,
220
                        out_K7_twk256 => 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_keysched256
248
        port    map     (
249
                                odd_in_tk256 => odd_number,
250
                                even_in_tk256   => even_number,
251
                                in_key_tk256 => twofish_key,
252
                                out_key_up_tk256 => key_up,
253
                                out_key_down_tk256 => key_down
254
                                );
255
 
256
        producing_the_Skeys: twofish_S256
257
        port     map (
258
                                in_key_ts256            => twofish_key,
259
                                out_Sfirst_ts256 => Sfirst,
260
                                out_Ssecond_ts256 => Ssecond,
261
                                out_Sthird_ts256 => Sthird,
262
                                out_Sfourth_ts256 => Sfourth
263
                                );
264
 
265
        the_encryption_circuit: twofish_encryption_round256
266
        port map        (
267
                                in1_ter256 => to_round(127 downto 96),
268
                                in2_ter256 => to_round(95 downto 64),
269
                                in3_ter256 => to_round(63 downto 32),
270
                                in4_ter256 => to_round(31 downto 0),
271
                                in_Sfirst_ter256 => Sfirst,
272
                                in_Ssecond_ter256 => Ssecond,
273
                                in_Sthird_ter256 => Sthird,
274
                                in_Sfourth_ter256 => Sfourth,
275
                                in_key_up_ter256 => key_up,
276
                                in_key_down_ter256              => key_down,
277
                                out1_ter256 => to_encr_reg128(127 downto 96),
278
                                out2_ter256 => to_encr_reg128(95 downto 64),
279
                                out3_ter256 => to_encr_reg128(63 downto 32),
280
                                out4_ter256     => to_encr_reg128(31 downto 0)
281
                                );
282
 
283
        encr_reg: reg128
284
        port map ( in_reg128 => to_encr_reg128,
285
                                out_reg128 => to_demux,
286
                                enable_reg128 => enable_encr_reg,
287
                                reset_reg128 => reset,
288
                                clk_reg128 => clk );
289
 
290
        output_demux: demux128
291
        port map ( in_demux128 => to_demux,
292
                                        out1_demux128 => to_output_whit_xors,
293
                                        out2_demux128 => to_mux,
294
                                        selection_demux128 => demux_selection );
295
 
296
        -- don't forget the last swap !!!
297
        from_xors_to_tdo(127 downto 96) <= K4 XOR to_output_whit_xors(63 downto 32);
298
        from_xors_to_tdo(95 downto 64) <= K5 XOR to_output_whit_xors(31 downto 0);
299
        from_xors_to_tdo(63 downto 32) <= K6 XOR to_output_whit_xors(127 downto 96);
300
        from_xors_to_tdo(31 downto 0) <= K7 XOR to_output_whit_xors(95 downto 64);
301
 
302
        taking_the_output: twofish_data_output
303
        port    map (
304
                                in_tdo  => from_xors_to_tdo,
305
                                out_tdo => output_data
306
                                );
307
 
308
        -- we create the clock 
309
        clk <= not clk after 50 ns; -- period 100 ns
310
 
311
 
312
        vk_proc: process
313
 
314
                variable key_f,  -- key input from file
315
                                        ct_f    : line; -- ciphertext from file
316
                variable        key_v : std_logic_vector(255 downto 0);  -- key vector input
317
                variable        ct_v    : std_logic_vector(127 downto 0); -- ciphertext vector
318
 
319
                variable counter : integer range 1 to 257 := 1; -- counts the encryptions
320
                variable round : integer range 1 to 16 := 1; -- holds the rounds of encryption
321
 
322
        begin
323
 
324
                -- plaintext stays fixed to zero
325
                input_data <= (others => '0');
326
 
327
                while not endfile(input_file) loop
328
                        readline(input_file, key_f);
329
                        readline(input_file,ct_f);
330
                        hread(key_f,key_v);
331
                        hread(ct_f,ct_v);
332
                        twofish_key <= key_v;
333
                        wait for 25 ns;
334
                        reset <= '1';
335
                        wait for 50 ns;
336
                        reset <= '0';
337
                        mux_selection <= '0';
338
                        demux_selection <= '1';
339
                        enable_encr_reg <= '0';
340
                        enable_round_reg <= '0';
341
                        wait for 50 ns;
342
                        enable_round_reg <= '1';
343
                        wait for 50 ns;
344
                        enable_round_reg <= '0';
345
 
346
                        -- the first round
347
                        even_number <= "00001000"; -- 8
348
                        odd_number <= "00001001"; -- 9
349
                        wait for 50 ns;
350
                        enable_encr_reg <= '1';
351
                        wait for 50 ns;
352
                        enable_encr_reg <= '0';
353
                        demux_selection <= '1';
354
                        mux_selection <= '1';
355
 
356
                        -- the rest 15 rounds
357
                        for round in 1 to 15 loop
358
                                even_number <= conv_std_logic_vector(((round*2)+8), 8);
359
                                odd_number <= conv_std_logic_vector(((round*2)+9), 8);
360
                                wait for 50 ns;
361
                                enable_encr_reg <= '1';
362
                                wait for 50 ns;
363
                                enable_encr_reg <= '0';
364
                        end loop;
365
 
366
                        -- taking final results
367
                        demux_selection <= '0';
368
                        wait for 25 ns;
369
                        assert (ct_v = output_data) report "file entry and encryption result DO NOT match!!! :( " severity failure;
370
                        assert (ct_v /= output_data) report "Encryption I=" & to_text(counter) &" OK" severity note;
371
                        counter := counter+1;
372
                        hwrite(ct_f,output_data);
373
                        hwrite(key_f,key_v);
374
                        writeline(output_file,key_f);
375
                        writeline(output_file,ct_f);
376
                end loop;
377
                assert false    report  "***** Variable Key Known Answer Test with 256 bits key size ended succesfully! :) *****"       severity failure;
378
        end process vk_proc;
379
 
380
end vk_encryption256_testbench_arch;
381
 

powered by: WebSVN 2.1.0

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