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

Subversion Repositories twofish

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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