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

Subversion Repositories twofish

[/] [twofish/] [trunk/] [vhdl/] [twofish_cbc_decryption_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_cbc_decryption_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
--
18
-- description  :       this file is the testbench for the Decryption Monte Carlo KAT of the twofish cipher with 256 bit key 
19
--
20
 
21
library ieee;
22
use ieee.std_logic_1164.all;
23
use ieee.std_logic_unsigned.all;
24
use ieee.std_logic_textio.all;
25
use ieee.std_logic_arith.all;
26
use std.textio.all;
27
 
28
entity cbc_decryption_monte_carlo_testbench256 is
29
end cbc_decryption_monte_carlo_testbench256;
30
 
31
architecture cbc_decryption256_monte_carlo_testbench_arch of cbc_decryption_monte_carlo_testbench256 is
32
 
33
        component       reg128
34
        port (
35
                        in_reg128       : in std_logic_vector(127 downto 0);
36
                        out_reg128 : out std_logic_vector(127 downto 0);
37
                        enable_reg128, reset_reg128, clk_reg128 : in std_logic
38
                        );
39
        end component;
40
 
41
        component twofish_keysched256
42
        port    (
43
                        odd_in_tk256,
44
                        even_in_tk256           : in std_logic_vector(7 downto 0);
45
                        in_key_tk256            : in std_logic_vector(255 downto 0);
46
                        out_key_up_tk256,
47
                        out_key_down_tk256      : out std_logic_vector(31 downto 0)
48
                        );
49
        end component;
50
 
51
        component twofish_whit_keysched256
52
        port    (
53
                        in_key_twk256           : in std_logic_vector(255 downto 0);
54
                        out_K0_twk256,
55
                        out_K1_twk256,
56
                        out_K2_twk256,
57
                        out_K3_twk256,
58
                        out_K4_twk256,
59
                        out_K5_twk256,
60
                        out_K6_twk256,
61
                        out_K7_twk256                   : out std_logic_vector(31 downto 0)
62
                        );
63
        end component;
64
 
65
        component twofish_decryption_round256
66
        port    (
67
                        in1_tdr256,
68
                        in2_tdr256,
69
                        in3_tdr256,
70
                        in4_tdr256,
71
                        in_Sfirst_tdr256,
72
                        in_Ssecond_tdr256,
73
                        in_Sthird_tdr256,
74
                        in_Sfourth_tdr256,
75
                        in_key_up_tdr256,
76
                        in_key_down_tdr256              : in std_logic_vector(31 downto 0);
77
                        out1_tdr256,
78
                        out2_tdr256,
79
                        out3_tdr256,
80
                        out4_tdr256                     : out std_logic_vector(31 downto 0)
81
                        );
82
        end component;
83
 
84
        component twofish_data_input
85
        port    (
86
                        in_tdi  : in std_logic_vector(127 downto 0);
87
                        out_tdi : out std_logic_vector(127 downto 0)
88
                        );
89
        end component;
90
 
91
        component twofish_data_output
92
        port    (
93
                        in_tdo  : in std_logic_vector(127 downto 0);
94
                        out_tdo : out std_logic_vector(127 downto 0)
95
                        );
96
        end component;
97
 
98
        component demux128
99
        port    ( in_demux128 : in std_logic_vector(127 downto 0);
100
                        out1_demux128, out2_demux128 : out std_logic_vector(127 downto 0);
101
                        selection_demux128 : in std_logic
102
                );
103
        end component;
104
 
105
        component mux128
106
        port ( in1_mux128, in2_mux128   : in std_logic_vector(127 downto 0);
107
                        selection_mux128        : in std_logic;
108
                        out_mux128 : out std_logic_vector(127 downto 0)
109
                );
110
        end component;
111
 
112
        component twofish_S256
113
        port    (
114
                        in_key_ts256            : in std_logic_vector(255 downto 0);
115
                        out_Sfirst_ts256,
116
                        out_Ssecond_ts256,
117
                        out_Sthird_ts256,
118
                        out_Sfourth_ts256                       : out std_logic_vector(31 downto 0)
119
                        );
120
        end component;
121
 
122
        FILE input_file : text is in "twofish_cbc_decryption_monte_carlo_testvalues_256bits.txt";
123
        FILE output_file : text is out "twofish_cbc_decryption_monte_carlo_256bits_results.txt";
124
 
125
        -- we create the functions that transform a number to text
126
        -- transforming a signle digit to a character
127
        function digit_to_char(number : integer range 0 to 9) return character is
128
        begin
129
                case number is
130
                        when 0 => return '0';
131
                        when 1 => return '1';
132
                        when 2 => return '2';
133
                        when 3 => return '3';
134
                        when 4 => return '4';
135
                        when 5 => return '5';
136
                        when 6 => return '6';
137
                        when 7 => return '7';
138
                        when 8 => return '8';
139
                        when 9 => return '9';
140
                end case;
141
        end;
142
 
143
        -- transforming multi-digit number to text
144
        function to_text(int_number : integer range 0 to 9999) return string is
145
                variable        our_text : string (1 to 4) := (others => ' ');
146
                variable thousands,
147
                                                hundreds,
148
                                                tens,
149
                                                ones            : integer range 0 to 9;
150
        begin
151
                ones := int_number mod 10;
152
                tens := ((int_number mod 100) - ones) / 10;
153
                hundreds := ((int_number mod 1000) - (int_number mod 100)) / 100;
154
                thousands := (int_number - (int_number mod 1000)) / 1000;
155
                our_text(1) := digit_to_char(thousands);
156
                our_text(2) := digit_to_char(hundreds);
157
                our_text(3) := digit_to_char(tens);
158
                our_text(4) := 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 <= K4 XOR from_tdi_to_xors(127 downto 96);
225
        from_xor1 <= K5 XOR from_tdi_to_xors(95 downto 64);
226
        from_xor2 <= K6 XOR from_tdi_to_xors(63 downto 32);
227
        from_xor3 <= K7 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_decryption_circuit: twofish_decryption_round256
266
        port map        (
267
                                in1_tdr256 => to_round(127 downto 96),
268
                                in2_tdr256 => to_round(95 downto 64),
269
                                in3_tdr256 => to_round(63 downto 32),
270
                                in4_tdr256 => to_round(31 downto 0),
271
                                in_Sfirst_tdr256 => Sfirst,
272
                                in_Ssecond_tdr256 => Ssecond,
273
                                in_Sthird_tdr256 => Sthird,
274
                                in_Sfourth_tdr256 => Sfourth,
275
                                in_key_up_tdr256 => key_up,
276
                                in_key_down_tdr256              => key_down,
277
                                out1_tdr256 => to_encr_reg128(127 downto 96),
278
                                out2_tdr256 => to_encr_reg128(95 downto 64),
279
                                out3_tdr256 => to_encr_reg128(63 downto 32),
280
                                out4_tdr256     => 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) <= K0 XOR to_output_whit_xors(63 downto 32);
298
        from_xors_to_tdo(95 downto 64) <= K1 XOR to_output_whit_xors(31 downto 0);
299
        from_xors_to_tdo(63 downto 32) <= K2 XOR to_output_whit_xors(127 downto 96);
300
        from_xors_to_tdo(31 downto 0) <= K3 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
        cbc_dmc_proc: process
313
 
314
                variable key_f,  -- key input from file
315
                                        pt_f,  -- plaintext from file
316
                                        ct_f,
317
                                        iv_f            : line; -- ciphertext from file
318
                variable        key_v : std_logic_vector(255 downto 0);  -- key vector input
319
                variable                        pt_v , -- plaintext vector
320
                                        ct_v,
321
                                        iv_v            : std_logic_vector(127 downto 0); -- ciphertext vector
322
 
323
                variable counter_10000 : integer range 0 to 9999 := 0; -- counter for the 10.000 repeats in the 400 next ones
324
                variable counter_400 : integer range 0 to 399 := 0; -- counter for the 400 repeats
325
                variable round : integer range 0 to 16 := 0;  -- holds the rounds
326
                variable PT, CT, CV, CTj_1              : std_logic_vector(127 downto 0) := (others => '0');
327
 
328
        begin
329
 
330
                while not endfile(input_file) loop
331
 
332
                        readline(input_file, key_f);
333
                        readline(input_file, iv_f);
334
                        readline(input_file,ct_f);
335
                        readline(input_file, pt_f);
336
                        hread(key_f,key_v);
337
                        hread(iv_f, iv_v);
338
                        hread(ct_f,ct_v);
339
                        hread(pt_f,pt_v);
340
 
341
                        twofish_key <= key_v;
342
                        CV := iv_v;
343
                        CT := ct_v;
344
 
345
                        for counter_10000 in 0 to 9999 loop
346
 
347
                                input_data <= CT;
348
 
349
                                wait for 25 ns;
350
                                reset <= '1';
351
                                wait for 50 ns;
352
                                reset <= '0';
353
 
354
                                mux_selection <= '0';
355
                                demux_selection <= '1';
356
                                enable_encr_reg <= '0';
357
                                enable_round_reg <= '0';
358
                                wait for 50 ns;
359
                                enable_round_reg <= '1';
360
                                wait for 50 ns;
361
                                enable_round_reg <= '0';
362
 
363
                                -- the first round
364
                                even_number <= "00100110"; -- 38
365
                                odd_number <= "00100111"; -- 39
366
                                wait for 50 ns;
367
                                enable_encr_reg <= '1';
368
                                wait for 50 ns;
369
                                enable_encr_reg <= '0';
370
                                demux_selection <= '1';
371
                                mux_selection <= '1';
372
 
373
                                -- the rest 15 rounds
374
                                for round in 1 to 15 loop
375
                                        even_number <= conv_std_logic_vector((((15-round)*2)+8), 8);
376
                                        odd_number <= conv_std_logic_vector((((15-round)*2)+9), 8);
377
                                        wait for 50 ns;
378
                                        enable_encr_reg <= '1';
379
                                        wait for 50 ns;
380
                                        enable_encr_reg <= '0';
381
                                end loop;
382
 
383
                                -- taking final results
384
                                demux_selection <= '0';
385
                                wait for 25 ns;
386
 
387
                                PT := output_data XOR CV;
388
                                CV := CT;
389
                                CT := PT;
390
 
391
                                assert false report "I=" & to_text(counter_400) & " R=" & to_text(counter_10000) severity note;
392
 
393
                        end loop; -- counter_10000
394
 
395
                        hwrite(key_f, key_v);
396
                        hwrite(iv_f, iv_v);
397
                        hwrite(ct_f, ct_v);
398
                        hwrite(pt_f, PT);
399
                        writeline(output_file,key_f);
400
                        writeline(output_file, iv_f);
401
                        writeline(output_file,ct_f);
402
                        writeline(output_file,pt_f);
403
 
404
                        assert (pt_v = PT) report "file entry and decryption result DO NOT match!!! :( " severity failure;
405
                        assert (pt_v /= PT) report "Decryption I=" & to_text(counter_400) &" OK" severity note;
406
 
407
                        counter_400 := counter_400 + 1;
408
 
409
                end loop;
410
                assert false    report  "***** CBC Decryption Monte Carlo Test with 256 bits key size ended succesfully! :) *****"      severity failure;
411
        end process cbc_dmc_proc;
412
 
413
end cbc_decryption256_monte_carlo_testbench_arch;
414
 

powered by: WebSVN 2.1.0

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