OpenCores
URL https://opencores.org/ocsvn/camellia-vhdl/camellia-vhdl/trunk

Subversion Repositories camellia-vhdl

[/] [camellia-vhdl/] [trunk/] [looping/] [control.vhd] - Blame information for rev 10

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 pfulgoni
 
2
--------------------------------------------------------------------------------
3
-- Designer:      Paolo Fulgoni <pfulgoni@opencores.org>
4
--
5
-- Create Date:   01/31/2008
6 4 pfulgoni
-- Last Update:   03/28/2008
7 3 pfulgoni
-- Project Name:  camellia-vhdl
8
-- Description:   Control unit and key handling
9
--
10
-- Copyright (C) 2008  Paolo Fulgoni
11
-- This file is part of camellia-vhdl.
12
-- camellia-vhdl is free software; you can redistribute it and/or modify
13
-- it under the terms of the GNU General Public License as published by
14
-- the Free Software Foundation; either version 3 of the License, or
15
-- (at your option) any later version.
16
-- camellia-vhdl is distributed in the hope that it will be useful,
17
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
18
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
-- GNU General Public License for more details.
20
-- You should have received a copy of the GNU General Public License
21
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
--
23
-- The Camellia cipher algorithm is 128 bit cipher developed by NTT and
24
-- Mitsubishi Electric researchers.
25
-- http://info.isl.ntt.co.jp/crypt/eng/camellia/
26
--------------------------------------------------------------------------------
27
library IEEE;
28
use IEEE.std_logic_1164.all;
29
 
30
entity control is
31
    port    (
32
            clk        : in  STD_LOGIC;
33
            reset      : in  STD_LOGIC;
34 4 pfulgoni
 
35 3 pfulgoni
            data_in    : in  STD_LOGIC_VECTOR (0 to 127);
36 4 pfulgoni
            enc_dec    : in  STD_LOGIC;
37
            data_rdy   : in  STD_LOGIC;
38
            data_acq   : out STD_LOGIC;
39
 
40 3 pfulgoni
            key_in     : in  STD_LOGIC_VECTOR (0 to 255);
41
            k_len      : in  STD_LOGIC_VECTOR (0 to 1);
42 4 pfulgoni
            key_rdy    : in  STD_LOGIC;
43
            key_acq    : out STD_LOGIC;
44
 
45 3 pfulgoni
            data_to    : out STD_LOGIC_VECTOR (0 to 127); -- data to datapath
46 4 pfulgoni
            output_rdy : out STD_LOGIC;
47 3 pfulgoni
            k1         : out STD_LOGIC_VECTOR (0 to 63);
48
            k2         : out STD_LOGIC_VECTOR (0 to 63);
49
            newdata    : out STD_LOGIC;
50
            sel        : out STD_LOGIC;
51
            pre_xor    : out STD_LOGIC_VECTOR (0 to 127);
52
            post_xor   : out STD_LOGIC_VECTOR (0 to 127);
53
            data_from  : in  STD_LOGIC_VECTOR (0 to 127)  -- data from datapath
54
            );
55
end control;
56
 
57
architecture RTL of control is
58
 
59 6 pfulgoni
    type STATUS is (KEYa, KEYb, KEYc, KEYd, KEYe, KEYf,
60 3 pfulgoni
                    SIX1a, SIX1b, SIX1c, SIX1d, SIX1e, SIX1f,
61
                    FL1,
62
                    SIX2a, SIX2b, SIX2c, SIX2d, SIX2e, SIX2f,
63
                    FL2,
64
                    SIX3a, SIX3b, SIX3c, SIX3d, SIX3e, SIX3f,
65
                    FL3,
66 4 pfulgoni
                    SIX4a, SIX4b, SIX4c, SIX4d, SIX4e, SIX4f,
67
                    WT
68
                    );
69 3 pfulgoni
    signal PS, NS    : STATUS;
70
 
71
    type K1_TYPE is (SIG1, SIG2, SIG3, SIG4, SIG5, SIG6,
72
                     KL_L, KL_R, KR_L, KR_R, KA_L, KA_R, KB_L, KB_R);
73
    signal k1_sel    : K1_TYPE;
74
    type K2_TYPE is (KL_L, KL_R, KR_L, KR_R, KA_L, KA_R, KB_L, KB_R);
75
    signal k2_sel    : K2_TYPE;
76
    type POSTXOR_TYPE is (KL, KA, KB, ZERO);
77
    signal postxor_sel    : POSTXOR_TYPE;
78
    type PREXOR_TYPE is (KL, KR, KA, KB, ZERO);
79
    signal prexor_sel    : PREXOR_TYPE;
80
 
81
    -- keys
82 4 pfulgoni
    signal reg_kl    : STD_LOGIC_VECTOR (0 to 127);
83 3 pfulgoni
    signal reg_kr    : STD_LOGIC_VECTOR (0 to 127);
84
    signal reg_ka    : STD_LOGIC_VECTOR (0 to 127);
85
    signal reg_kb    : STD_LOGIC_VECTOR (0 to 127);
86
    --keys shifted each step
87
    signal reg_kl_s  : STD_LOGIC_VECTOR (0 to 127);
88
    signal reg_kr_s  : STD_LOGIC_VECTOR (0 to 127);
89
    signal reg_ka_s  : STD_LOGIC_VECTOR (0 to 127);
90
    signal reg_kb_s  : STD_LOGIC_VECTOR (0 to 127);
91 4 pfulgoni
 
92
    signal reg_k_len   : STD_LOGIC_VECTOR (0 to 1);
93
    signal reg_enc_dec : STD_LOGIC;
94 3 pfulgoni
 
95
    -- input constant
96
    constant KLEN_128 : STD_LOGIC_VECTOR (0 to 1) := "00";
97
    constant KLEN_192 : STD_LOGIC_VECTOR (0 to 1) := "01";
98
    constant KLEN_256 : STD_LOGIC_VECTOR (0 to 1) := "10";
99
    constant ENC      : STD_LOGIC := '0';
100
    constant DEC      : STD_LOGIC := '1';
101
    constant SEL_F    : STD_LOGIC := '0';
102
    constant SEL_FL   : STD_LOGIC := '1';
103
 
104
    -- constant keys
105
    constant sigma1 : STD_LOGIC_VECTOR (0 to 63) := X"A09E667F3BCC908B";
106
    constant sigma2 : STD_LOGIC_VECTOR (0 to 63) := X"B67AE8584CAA73B2";
107
    constant sigma3 : STD_LOGIC_VECTOR (0 to 63) := X"C6EF372FE94F82BE";
108
    constant sigma4 : STD_LOGIC_VECTOR (0 to 63) := X"54FF53A5F1D36F1C";
109
    constant sigma5 : STD_LOGIC_VECTOR (0 to 63) := X"10E527FADE682D1D";
110
    constant sigma6 : STD_LOGIC_VECTOR (0 to 63) := X"B05688C2B3E6C1FD";
111
 
112
begin
113
 
114
    with k1_sel select
115
        k1 <=   sigma1                 when SIG1,
116
                sigma2                 when SIG2,
117
                sigma3                 when SIG3,
118
                sigma4                 when SIG4,
119
                sigma5                 when SIG5,
120
                sigma6                 when SIG6,
121
                reg_kl_s(0 to 63)      when KL_L,
122
                reg_kl_s(64 to 127)    when KL_R,
123
                reg_kr_s(0 to 63)      when KR_L,
124
                reg_kr_s(64 to 127)    when KR_R,
125
                reg_ka_s(0 to 63)      when KA_L,
126
                reg_ka_s(64 to 127)    when KA_R,
127
                reg_kb_s(0 to 63)      when KB_L,
128
                reg_kb_s(64 to 127)    when others;
129
    with k2_sel select
130
        k2 <=   reg_kl_s(0 to 63)      when KL_L,
131
                reg_kl_s(64 to 127)    when KL_R,
132
                reg_kr_s(0 to 63)      when KR_L,
133
                reg_kr_s(64 to 127)    when KR_R,
134
                reg_ka_s(0 to 63)      when KA_L,
135
                reg_ka_s(64 to 127)    when KA_R,
136
                reg_kb_s(0 to 63)      when KB_L,
137
                reg_kb_s(64 to 127)    when others;
138
 
139
    with postxor_sel select
140 4 pfulgoni
        post_xor <= reg_kl_s(64 to 127) & reg_kl_s(0 to 63)   when KL,
141 3 pfulgoni
                    reg_ka_s(64 to 127) & reg_ka_s(0 to 63)   when KA,
142
                    reg_kb_s(64 to 127) & reg_kb_s(0 to 63)   when KB,
143
                    (others=>'0')                             when others;
144
 
145
    with prexor_sel select
146 4 pfulgoni
        pre_xor  <=   reg_kl_s           when KL,
147 3 pfulgoni
                      reg_kr_s           when KR,
148
                      reg_ka_s           when KA,
149
                      reg_kb_s           when KB,
150
                      (others=>'0')      when others;
151 6 pfulgoni
 
152 3 pfulgoni
 
153 6 pfulgoni
    REGISTERS_UPDATE : process(reset, clk)
154 3 pfulgoni
        variable coming_from_key : STD_LOGIC;
155
    begin
156 6 pfulgoni
    if (reset = '1') then
157
        reg_kl           <= (others=>'0');
158
        reg_kr           <= (others=>'0');
159
        reg_ka           <= (others=>'0');
160
        reg_kb           <= (others=>'0');
161
        reg_kl_s         <= (others=>'0');
162
        reg_kr_s         <= (others=>'0');
163
        reg_ka_s         <= (others=>'0');
164
        reg_kb_s         <= (others=>'0');
165
        reg_enc_dec      <= '0';
166
        reg_k_len        <= (others=>'0');
167
        output_rdy       <= '0';
168
        coming_from_key  := '0';
169
    else
170
        if (clk'event and clk = '1') then
171
            case PS is
172
                when KEYa =>
173
                    coming_from_key  := '1';
174
                    reg_kl           <= key_in(0 to 127);
175
                    reg_kl_s         <= key_in(0 to 127);
176
                    reg_k_len        <= k_len;
177
                    case k_len is
178
                        when KLEN_192 =>
179
                            reg_kr    <= key_in(128 to 191) & not (key_in(128 to 191));
180
                            reg_kr_s  <= key_in(128 to 191) & not (key_in(128 to 191));
181
                        when KLEN_256 =>
182
                            reg_kr    <= key_in(128 to 255);
183
                            reg_kr_s  <= key_in(128 to 255);
184
                        when others =>
185
                            reg_kr    <= (others=>'0');
186
                            reg_kr_s  <= (others=>'0');
187
                    end case;
188
                    k1_sel    <= SIG1;
189
                when KEYb =>
190
                    k1_sel    <= SIG2;
191
                when KEYc =>
192
                    k1_sel    <= SIG3;
193
                when KEYd =>
194
                    k1_sel    <= SIG4;
195
                when KEYe =>
196
                    reg_ka    <= data_from;
197
                    reg_ka_s  <= data_from;
198
                    k1_sel    <= SIG5;
199
                when KEYf =>
200
                    k1_sel    <= SIG6;
201
                when SIX1a =>
202
                    if (enc_dec = ENC) then
203
                        if (coming_from_key = '1') then
204
                            if (reg_k_len = KLEN_128) then
205
                                reg_ka   <= data_from;
206
                                reg_ka_s <= data_from;
207
                            else
208
                                reg_kb   <= data_from;
209
                                reg_kb_s <= data_from;
210
                            end if;
211
                        else
212
                            reg_ka_s <= reg_ka;
213
                            reg_kb_s <= reg_kb;
214
                            reg_kl_s <= reg_kl;
215
                            reg_kr_s <= reg_kr;
216
                        end if;
217 4 pfulgoni
                        if (reg_k_len = KLEN_128) then
218 6 pfulgoni
                            k1_sel <= KA_L;
219 3 pfulgoni
                        else
220 6 pfulgoni
                            k1_sel <= KB_L;
221 3 pfulgoni
                        end if;
222 6 pfulgoni
                    else -- DEC
223
                        if (coming_from_key = '1') then
224
                            if (reg_k_len = KLEN_128) then
225
                                reg_ka   <= data_from;
226
                                reg_ka_s <= data_from(111 to 127) & data_from(0 to 110); -- >>> 17
227
                            else
228
                                reg_kb   <= data_from;
229
                                reg_kb_s <= data_from(111 to 127) & data_from(0 to 110); -- >>> 17
230
                                reg_ka_s <= reg_ka_s(111 to 127) & reg_ka_s(0 to 110); -- >>> 17
231
                                reg_kr_s <= reg_kr_s(111 to 127) & reg_kr_s(0 to 110); -- >>> 17
232
                            end if;
233
                            reg_kl_s  <= reg_kl_s(111 to 127) & reg_kl_s(0 to 110); -- >>> 17
234
                        else
235
                            reg_ka_s <= reg_ka(111 to 127) & reg_ka(0 to 110); -- >>> 17
236
                            reg_kb_s <= reg_kb(111 to 127) & reg_kb(0 to 110); -- >>> 17
237
                            reg_kl_s <= key_in(111 to 127) & key_in(0 to 110); --kl >>> 17
238
                            reg_kr_s <= reg_kr(111 to 127) & reg_kr(0 to 110); -- >>> 17
239
                        end if;
240
                        k1_sel <= KL_R;
241 3 pfulgoni
                    end if;
242 6 pfulgoni
                    reg_enc_dec <= enc_dec;
243
                when SIX1b =>
244
                    coming_from_key  := '0';
245
                    if (reg_enc_dec = ENC) then
246
                        if (reg_k_len = KLEN_128) then
247
                            k1_sel <= KA_R;
248
                        else
249
                            k1_sel <= KB_R;
250
                        end if;
251
                    else -- DEC
252
                        k1_sel <= KL_L; -- for each value of reg_k_len
253 3 pfulgoni
                    end if;
254 6 pfulgoni
                when SIX1c =>
255
                    if (reg_enc_dec = ENC) then
256 4 pfulgoni
                        if (reg_k_len = KLEN_128) then
257 6 pfulgoni
                            k1_sel <= KL_L;
258
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
259 3 pfulgoni
                        else
260 6 pfulgoni
                            k1_sel <= KR_L;
261
                            reg_kr_s  <= reg_kr_s(15 to 127) & reg_kr_s(0 to 14); -- <<< 15
262
                        end if;
263
                    else -- DEC
264
                        reg_ka_s <= reg_ka_s(111 to 127) & reg_ka_s(0 to 110); -- >>> 17
265
                        k1_sel <= KA_R; -- for each value of reg_k_len
266
                    end if;
267
                when SIX1d =>
268
                    if (reg_enc_dec = ENC) then
269
                        if (reg_k_len = KLEN_128) then
270
                            k1_sel <= KL_R;
271
                        else
272
                            k1_sel <= KR_R;
273
                        end if;
274
                    else -- DEC
275
                        k1_sel <= KA_L; -- for each value of reg_k_len
276
                    end if;
277
                when SIX1e =>
278
                    if (reg_enc_dec = ENC) then
279
                        if (reg_k_len = KLEN_128) then
280
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
281
                        else
282
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
283
                        end if;
284
                        k1_sel <= KA_L;
285
                    else -- DEC
286
                        if (reg_k_len = KLEN_128) then
287
                            reg_kl_s <= reg_kl_s(111 to 127) & reg_kl_s(0 to 110); -- >>> 17
288
                            k1_sel <= KL_R;
289
                        else
290 3 pfulgoni
                            reg_kr_s <= reg_kr_s(111 to 127) & reg_kr_s(0 to 110); -- >>> 17
291 6 pfulgoni
                            k1_sel <= KR_R;
292 3 pfulgoni
                        end if;
293
                    end if;
294 6 pfulgoni
                when SIX1f =>
295
                    if (reg_enc_dec = ENC) then
296 3 pfulgoni
                        k1_sel <= KA_R;
297 6 pfulgoni
                    else -- DEC
298
                        if (reg_k_len = KLEN_128) then
299
                            k1_sel <= KL_L;
300
                        else
301
                            k1_sel <= KR_L;
302
                        end if;
303 3 pfulgoni
                    end if;
304 6 pfulgoni
                when FL1 =>
305
                    if (reg_enc_dec = ENC) then
306
                        if (reg_k_len = KLEN_128) then
307
                            k1_sel <= KA_L;
308
                            k2_sel <= KA_R;
309
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
310
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
311
                        else
312
                            k1_sel <= KR_L;
313
                            k2_sel <= KR_R;
314
                            reg_kb_s  <= reg_kb_s(15 to 127) & reg_kb_s(0 to 14); -- <<< 15
315
                            reg_kr_s  <= reg_kr_s(15 to 127) & reg_kr_s(0 to 14); -- <<< 15
316
                        end if;
317
                    else -- DEC
318
                        if (reg_k_len = KLEN_128) then
319
                            k1_sel <= KL_R;
320
                            k2_sel <= KL_L;
321
                        else
322
                            k1_sel <= KA_R;
323
                            k2_sel <= KA_L;
324
                        end if;
325
                        reg_ka_s <= reg_ka_s(111 to 127) & reg_ka_s(0 to 110); -- >>> 17
326
                        reg_kl_s <= reg_kl_s(111 to 127) & reg_kl_s(0 to 110); -- >>> 17
327 3 pfulgoni
                    end if;
328 6 pfulgoni
                when SIX2a =>
329
                    if (reg_enc_dec = ENC) then
330
                        if (reg_k_len = KLEN_128) then
331
                            k1_sel <= KL_L;
332
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
333
                        else
334
                            k1_sel <= KB_L;
335
                            reg_kb_s  <= reg_kb_s(15 to 127) & reg_kb_s(0 to 14); -- <<< 15
336
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
337
                        end if;
338
                    else -- DEC
339
                        if (reg_k_len = KLEN_128) then
340
                            k1_sel <= KA_R;
341
                            reg_ka_s <= reg_ka_s(111 to 127) & reg_ka_s(0 to 110); -- >>> 17
342
                        else
343
                            k1_sel <= KL_R;
344
                            reg_kb_s <= reg_kb_s(111 to 127) & reg_kb_s(0 to 110); -- >>> 17
345
                            reg_kl_s <= reg_kl_s(111 to 127) & reg_kl_s(0 to 110); -- >>> 17
346
                        end if;
347 3 pfulgoni
                    end if;
348 6 pfulgoni
                when SIX2b =>
349
                    if (reg_enc_dec = ENC) then
350
                        if (reg_k_len = KLEN_128) then
351
                            k1_sel <= KL_R;
352
                        else
353
                            k1_sel <= KB_R;
354
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
355
                        end if;
356
                    else -- DEC
357
                        if (reg_k_len = KLEN_128) then
358
                            k1_sel <= KA_L;
359
                        else
360
                            k1_sel <= KL_L;
361
                            reg_kb_s <= reg_kb_s(111 to 127) & reg_kb_s(0 to 110); -- >>> 17
362
                        end if;
363 3 pfulgoni
                    end if;
364 6 pfulgoni
                when SIX2c =>
365
                    if (reg_enc_dec = ENC) then
366
                        if (reg_k_len = KLEN_128) then
367
                            k1_sel <= KA_L;
368
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
369
                        else
370
                            k1_sel <= KL_L;
371
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
372
                        end if;
373
                    else -- DEC
374
                        if (reg_k_len = KLEN_128) then
375
                            k1_sel <= KL_R;
376
                            reg_kl_s <= reg_kl_s(111 to 127) & reg_kl_s(0 to 110); -- >>> 17
377
                        else
378
                            k1_sel <= KB_R;
379
                            reg_kb_s <= reg_kb_s(111 to 127) & reg_kb_s(0 to 110); -- >>> 17
380
                        end if;
381
                    end if;
382
                when SIX2d =>
383
                    if (reg_enc_dec = ENC) then
384
                        if (reg_k_len = KLEN_128) then
385
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
386
                        else
387
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
388
                        end if;
389 3 pfulgoni
                        k1_sel <= KL_R;
390 6 pfulgoni
                    else -- DEC
391
                        if (reg_k_len = KLEN_128) then
392
                            k1_sel <= KA_L;
393
                            reg_ka_s <= reg_ka_s(113 to 127) & reg_ka_s(0 to 112); -- >>> 15
394
                        else
395
                            k1_sel <= KB_L;
396
                            reg_kr_s <= reg_kr_s(111 to 127) & reg_kr_s(0 to 110); -- >>> 17
397
                        end if;
398 3 pfulgoni
                    end if;
399 6 pfulgoni
                when SIX2e =>
400
                    if (reg_enc_dec = ENC) then
401
                        if (reg_k_len = KLEN_128) then
402
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
403
                        else
404
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
405
                        end if;
406 3 pfulgoni
                        k1_sel <= KA_L;
407 6 pfulgoni
                    else -- DEC
408
                        if (reg_k_len = KLEN_128) then
409
                            k1_sel <= KL_R;
410
                            reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
411
                        else
412
                            k1_sel <= KR_R;
413
                            reg_kr_s <= reg_kr_s(111 to 127) & reg_kr_s(0 to 110); -- >>> 17
414
                        end if;
415 3 pfulgoni
                    end if;
416 6 pfulgoni
                when SIX2f =>
417
                    if (reg_enc_dec = ENC) then
418 3 pfulgoni
                        k1_sel <= KA_R;
419 6 pfulgoni
                    else -- DEC
420
                        if (reg_k_len = KLEN_128) then
421
                            k1_sel <= KL_L;
422
                        else
423
                            k1_sel <= KR_L;
424
                        end if;
425 3 pfulgoni
                    end if;
426 6 pfulgoni
                when FL2 =>
427
                    if (reg_enc_dec = ENC) then
428
                        if (reg_k_len = KLEN_128) then
429
                            reg_kl_s  <= reg_kl_s(17 to 127) & reg_kl_s(0 to 16); -- <<< 17
430
                        else
431
                            reg_kr_s  <= reg_kr_s(15 to 127) & reg_kr_s(0 to 14); -- <<< 15
432
                            reg_kl_s  <= reg_kl_s(15 to 127) & reg_kl_s(0 to 14); -- <<< 15
433
                        end if;
434 3 pfulgoni
                        k1_sel <= KL_L;
435 6 pfulgoni
                        k2_sel <= KL_R;
436
                    else -- DEC
437
                        if (reg_k_len = KLEN_128) then
438
                            k1_sel <= KA_R;
439
                            k2_sel <= KA_L;
440
                            reg_ka_s <= reg_ka_s(113 to 127) & reg_ka_s(0 to 112); -- >>> 15
441
                        else
442
                            k1_sel <= KL_R;
443
                            k2_sel <= KL_L;
444
                            reg_ka_s <= reg_ka_s(111 to 127) & reg_ka_s(0 to 110); -- >>> 17
445
                            reg_kl_s <= reg_kl_s(111 to 127) & reg_kl_s(0 to 110); -- >>> 17
446
                        end if;
447 3 pfulgoni
                    end if;
448 6 pfulgoni
                when SIX3a =>
449
                    if (reg_enc_dec = ENC) then
450
                        if (reg_k_len = KLEN_128) then
451
                            k1_sel <= KL_L;
452
                            reg_kl_s  <= reg_kl_s(17 to 127) & reg_kl_s(0 to 16); -- <<< 17
453
                        else
454
                            k1_sel <= KR_L;
455
                            reg_kr_s  <= reg_kr_s(15 to 127) & reg_kr_s(0 to 14); -- <<< 15
456
                        end if;
457
                    else -- DEC
458
                        if (reg_k_len = KLEN_128) then
459
                            k1_sel <= KA_R;
460
                        else
461
                            k1_sel <= KA_R;
462
                        end if;
463
                        reg_ka_s <= reg_ka_s(113 to 127) & reg_ka_s(0 to 112); -- >>> 15
464 3 pfulgoni
                    end if;
465 6 pfulgoni
                when SIX3b =>
466
                    if (reg_enc_dec = ENC) then
467
                        if (reg_k_len = KLEN_128) then
468
                            k1_sel <= KL_R;
469
                            reg_ka_s  <= reg_ka_s(17 to 127) & reg_ka_s(0 to 16); -- <<< 17
470
                        else
471
                            k1_sel <= KR_R;
472
                            reg_kb_s  <= reg_kb_s(15 to 127) & reg_kb_s(0 to 14); -- <<< 15
473
                        end if;
474
                    else -- DEC
475
                        if (reg_k_len = KLEN_128) then
476
                            k1_sel <= KA_L;
477
                            reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
478
                        else
479
                            k1_sel <= KA_L;
480
                        end if;
481 3 pfulgoni
                    end if;
482 6 pfulgoni
                when SIX3c =>
483
                    if (reg_enc_dec = ENC) then
484
                        if (reg_k_len = KLEN_128) then
485
                            k1_sel <= KA_L;
486
                            reg_ka_s  <= reg_ka_s(17 to 127) & reg_ka_s(0 to 16); -- <<< 17
487
                        else
488
                            k1_sel <= KB_L;
489
                            reg_kb_s  <= reg_kb_s(15 to 127) & reg_kb_s(0 to 14); -- <<< 15
490
                        end if;
491
                    else -- DEC
492
                        if (reg_k_len = KLEN_128) then
493
                            k1_sel <= KL_R;
494
                        else
495
                            k1_sel <= KL_R;
496
                        end if;
497
                        reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
498 3 pfulgoni
                    end if;
499 6 pfulgoni
                when SIX3d =>
500
                    if (reg_enc_dec = ENC) then
501
                        if (reg_k_len = KLEN_128) then
502
                            k1_sel <= KA_R;
503
                        else
504
                            k1_sel <= KB_R;
505
                        end if;
506
                    else -- DEC
507
                        if (reg_k_len = KLEN_128) then
508
                            k1_sel <= KL_L;
509
                        else
510
                            k1_sel <= KL_L;
511
                            reg_kb_s <= reg_kb_s(113 to 127) & reg_kb_s(0 to 112); -- >>> 15
512
                        end if;
513
                    end if;
514
                when SIX3e =>
515
                    if (reg_enc_dec = ENC) then
516
                        if (reg_k_len = KLEN_128) then
517
                            reg_kl_s  <= reg_kl_s(17 to 127) & reg_kl_s(0 to 16); -- <<< 17
518
                        else
519
                            reg_kl_s  <= reg_kl_s(17 to 127) & reg_kl_s(0 to 16); -- <<< 17
520
                        end if;
521 3 pfulgoni
                        k1_sel <= KL_L;
522 6 pfulgoni
                    else -- DEC
523
                        if (reg_k_len = KLEN_128) then
524
                            k1_sel <= KA_R;
525
                            reg_ka_s <= reg_ka_s(113 to 127) & reg_ka_s(0 to 112); -- >>> 15
526
                        else
527
                            k1_sel <= KB_R;
528
                            reg_kb_s <= reg_kb_s(113 to 127) & reg_kb_s(0 to 112); -- >>> 15
529
                        end if;
530 3 pfulgoni
                    end if;
531 6 pfulgoni
                when SIX3f =>
532
                    if (reg_enc_dec = ENC) then
533
                        if (reg_k_len = KLEN_128) then
534
                            reg_ka_s  <= reg_ka_s(17 to 127) & reg_ka_s(0 to 16); -- <<< 17
535
                        else
536
                            reg_ka_s  <= reg_ka_s(15 to 127) & reg_ka_s(0 to 14); -- <<< 15
537
                        end if;
538 3 pfulgoni
                        k1_sel <= KL_R;
539 6 pfulgoni
                    else -- DEC
540
                        if (reg_k_len = KLEN_128) then
541
                            k1_sel <= KA_L;
542
                            reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
543
                        else
544
                            k1_sel <= KB_L;
545
                            reg_kr_s <= reg_kr_s(113 to 127) & reg_kr_s(0 to 112); -- >>> 15
546
                        end if;
547 3 pfulgoni
                    end if;
548 6 pfulgoni
                when FL3 =>
549
                    if (reg_enc_dec = ENC) then
550
                        k1_sel  <= KA_L;
551
                        k2_sel  <= KA_R;
552
                        reg_kr_s  <= reg_kr_s(17 to 127) & reg_kr_s(0 to 16); -- <<< 17
553
                        reg_ka_s  <= reg_ka_s(17 to 127) & reg_ka_s(0 to 16); -- <<< 17
554
                    else -- DEC
555
                        k1_sel  <= KR_R;
556
                        k2_sel  <= KR_L;
557 3 pfulgoni
                        reg_ka_s <= reg_ka_s(113 to 127) & reg_ka_s(0 to 112); -- >>> 15
558 6 pfulgoni
                        reg_kr_s <= reg_kr_s(113 to 127) & reg_kr_s(0 to 112); -- >>> 15
559 3 pfulgoni
                    end if;
560 6 pfulgoni
                when SIX4a =>
561
                    if (reg_enc_dec = ENC) then
562 3 pfulgoni
                        k1_sel <= KR_L;
563 6 pfulgoni
                        reg_kr_s  <= reg_kr_s(17 to 127) & reg_kr_s(0 to 16); -- <<< 17
564
                    else -- DEC
565 3 pfulgoni
                        k1_sel <= KA_R;
566
                        reg_ka_s <= reg_ka_s(113 to 127) & reg_ka_s(0 to 112); -- >>> 15
567
                    end if;
568 6 pfulgoni
                when SIX4b =>
569
                    if (reg_enc_dec = ENC) then
570 3 pfulgoni
                        k1_sel <= KR_R;
571 6 pfulgoni
                    else -- DEC
572 3 pfulgoni
                        k1_sel <= KA_L;
573
                    end if;
574 6 pfulgoni
                when SIX4c =>
575
                    if (reg_enc_dec = ENC) then
576 3 pfulgoni
                        k1_sel <= KA_L;
577
                        reg_ka_s  <= reg_ka_s(17 to 127) & reg_ka_s(0 to 16); -- <<< 17
578 6 pfulgoni
                    else -- DEC
579
                        k1_sel <= KR_R;
580
                        reg_kr_s <= reg_kr_s(113 to 127) & reg_kr_s(0 to 112); -- >>> 15
581 3 pfulgoni
                    end if;
582 6 pfulgoni
                when SIX4d =>
583
                    if (reg_enc_dec = ENC) then
584 3 pfulgoni
                        k1_sel <= KA_R;
585 6 pfulgoni
                        reg_kl_s  <= reg_kl_s(17 to 127) & reg_kl_s(0 to 16); -- <<< 17
586
                        reg_kb_s  <= reg_kb_s(17 to 127) & reg_kb_s(0 to 16); -- <<< 17
587
                    else -- DEC
588
                        k1_sel <= KR_L;
589
                        reg_kb_s <= reg_kb_s(113 to 127) & reg_kb_s(0 to 112); -- >>> 15
590
                        reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
591 3 pfulgoni
                    end if;
592 6 pfulgoni
                when SIX4e =>
593
                    if (reg_enc_dec = ENC) then
594 3 pfulgoni
                        k1_sel <= KL_L;
595
                        reg_kl_s  <= reg_kl_s(17 to 127) & reg_kl_s(0 to 16); -- <<< 17
596 6 pfulgoni
                        reg_kb_s  <= reg_kb_s(17 to 127) & reg_kb_s(0 to 16); -- <<< 17
597
                    else -- DEC
598 3 pfulgoni
                        k1_sel <= KB_R;
599
                        reg_kb_s <= reg_kb_s(113 to 127) & reg_kb_s(0 to 112); -- >>> 15
600 6 pfulgoni
                        reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
601 3 pfulgoni
                    end if;
602 6 pfulgoni
                when SIX4f =>
603
                    if (reg_enc_dec = ENC) then
604
                        k1_sel <= KL_R;
605
                        reg_kb_s  <= reg_kb_s(17 to 127) & reg_kb_s(0 to 16); -- <<< 17
606
                    else -- DEC
607
                        k1_sel <= KB_L;
608 3 pfulgoni
                        reg_kl_s <= reg_kl_s(113 to 127) & reg_kl_s(0 to 112); -- >>> 15
609
                    end if;
610 6 pfulgoni
                when WT =>
611
                    -- do nothing
612
            end case;
613 3 pfulgoni
 
614 6 pfulgoni
            if (PS = KEYa) then
615
                data_to <= key_in(0 to 127); --kl
616
            else
617
                data_to <= data_in;
618
            end if;
619 3 pfulgoni
 
620 6 pfulgoni
            case PS is
621
                when KEYc =>
622 3 pfulgoni
                    prexor_sel <= KL;
623 6 pfulgoni
                when KEYa | KEYe =>
624
                    prexor_sel <= KR;
625
                when SIX1a =>
626
                    if (enc_dec = ENC) then
627
                        prexor_sel <= KL;
628
                    else
629
                        if (reg_k_len = KLEN_128) then
630
                            prexor_sel <= KA;
631
                        else
632
                            prexor_sel <= KB;
633
                        end if;
634
                    end if;
635
                when others =>
636
                    prexor_sel <= ZERO;
637
            end case;
638
 
639
            case PS is
640
                when SIX3f =>
641 4 pfulgoni
                    if (reg_k_len = KLEN_128) then
642 6 pfulgoni
                        if (reg_enc_dec = ENC) then
643
                            postxor_sel <= KA;
644
                        else
645
                            postxor_sel <= KL;
646
                        end if;
647 3 pfulgoni
                    else
648 6 pfulgoni
                        postxor_sel <= ZERO;
649 3 pfulgoni
                    end if;
650 6 pfulgoni
                when SIX4f =>
651 4 pfulgoni
                    if (reg_enc_dec = ENC) then
652 6 pfulgoni
                        postxor_sel <= KB;
653 3 pfulgoni
                    else
654
                        postxor_sel <= KL;
655
                    end if;
656 6 pfulgoni
                when others =>
657 3 pfulgoni
                    postxor_sel <= ZERO;
658 6 pfulgoni
            end case;
659 3 pfulgoni
 
660 6 pfulgoni
            if (PS = SIX1a or PS = KEYa) then
661
                newdata <= '1';
662
            else
663
                newdata <= '0';
664
            end if;
665
 
666
            if ((PS = SIX3f and reg_k_len = KLEN_128) or PS = SIX4f) then
667
                output_rdy <= '1';
668
            else
669
                output_rdy <= '0';
670
            end if;
671 3 pfulgoni
 
672 6 pfulgoni
            if (PS = FL1 or PS = FL2 or PS = FL3) then
673
                sel <= SEL_FL;
674
            else
675
                sel <= SEL_F;
676
            end if;
677
 
678
            if (PS = KEYb) then
679
                key_acq   <=  '1';
680
            else
681
                key_acq   <=  '0';
682
            end if;
683
 
684
            if (PS = SIX1b) then
685
                data_acq   <=  '1';
686
            else
687
                data_acq   <=  '0';
688
            end if;
689
 
690 3 pfulgoni
        end if;
691 4 pfulgoni
    end if;
692 6 pfulgoni
 
693 3 pfulgoni
    end process;
694
 
695 6 pfulgoni
    STATE_UPDATE: process (reset, clk)
696 3 pfulgoni
    begin
697
 
698 6 pfulgoni
        if (reset = '1') then
699
            PS <= KEYa;
700
        else
701
            if (clk'event and clk = '1') then
702 3 pfulgoni
                PS <= NS;
703
            end if;
704
        end if;
705
    end process;
706
 
707 4 pfulgoni
    NEXT_STATE: process (PS, data_rdy, key_rdy)
708 3 pfulgoni
    begin
709
               case PS is
710
                when KEYa =>
711 4 pfulgoni
                    if(key_rdy = '1') then
712 3 pfulgoni
                        NS <= KEYb;
713
                    else
714
                        NS <= KEYa;
715
                    end if;
716
                when KEYb =>
717
                    NS <= KEYc;
718
                when KEYc =>
719
                    NS <= KEYd;
720
                when KEYd =>
721 4 pfulgoni
                    if (reg_k_len = KLEN_128) then
722 3 pfulgoni
                        NS <= SIX1a;
723
                    else
724
                        NS <= KEYe;
725
                    end if;
726
                when KEYe =>
727
                    NS <= KEYf;
728
                when KEYf =>
729
                    NS <= SIX1a;
730
                when SIX1a =>
731 4 pfulgoni
                    if(data_rdy = '1') then
732 3 pfulgoni
                        NS <= SIX1b;
733
                    else
734
                        NS <= SIX1a;
735
                    end if;
736
                when SIX1b =>
737
                    NS <= SIX1c;
738
                when SIX1c =>
739
                    NS <= SIX1d;
740
                when SIX1d =>
741
                    NS <= SIX1e;
742
                when SIX1e =>
743
                    NS <= SIX1f;
744
                when SIX1f =>
745
                    NS <= FL1;
746
                when FL1 =>
747
                    NS <= SIX2a;
748
                when SIX2a =>
749
                    NS <= SIX2b;
750
                when SIX2b =>
751
                    NS <= SIX2c;
752
                when SIX2c =>
753
                    NS <= SIX2d;
754
                when SIX2d =>
755
                    NS <= SIX2e;
756
                when SIX2e =>
757
                    NS <= SIX2f;
758
                when SIX2f =>
759
                    NS <= FL2;
760
                when FL2 =>
761
                    NS <= SIX3a;
762
                when SIX3a =>
763
                    NS <= SIX3b;
764
                when SIX3b =>
765
                    NS <= SIX3c;
766
                when SIX3c =>
767
                    NS <= SIX3d;
768
                when SIX3d =>
769
                    NS <= SIX3e;
770
                when SIX3e =>
771
                    NS <= SIX3f;
772
                when SIX3f =>
773 4 pfulgoni
                    if (reg_k_len = KLEN_128) then
774
                        if (key_rdy = '1') then
775 3 pfulgoni
                            NS <= KEYa;
776
                        else
777 4 pfulgoni
                            if (data_rdy = '1') then
778
                                NS <= SIX1a;
779
                            else
780
                                NS <= WT;
781
                            end if;
782 3 pfulgoni
                        end if;
783
                    else
784
                        NS <= FL3;
785
                    end if;
786
                when FL3 =>
787
                    NS <= SIX4a;
788
                when SIX4a =>
789
                    NS <= SIX4b;
790
                when SIX4b =>
791
                    NS <= SIX4c;
792
                when SIX4c =>
793
                    NS <= SIX4d;
794
                when SIX4d =>
795
                    NS <= SIX4e;
796
                when SIX4e =>
797
                    NS <= SIX4f;
798
                when SIX4f =>
799 4 pfulgoni
                    if (key_rdy = '1') then
800 3 pfulgoni
                        NS <= KEYa;
801
                    else
802 4 pfulgoni
                        if (data_rdy = '1') then
803
                            NS <= SIX1a;
804
                        else
805
                            NS <= WT;
806
                        end if;
807 3 pfulgoni
                    end if;
808 4 pfulgoni
                when WT =>
809
                    if (key_rdy = '1') then
810
                        NS <= KEYa;
811
                    else
812
                        if (data_rdy = '1') then
813
                            NS <= SIX1a;
814
                        else
815
                            NS <= WT;
816
                        end if;
817
                    end if;
818 3 pfulgoni
            end case;
819
    end process;
820
 
821
end RTL;

powered by: WebSVN 2.1.0

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