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

Subversion Repositories aes_crypto_core

[/] [aes_crypto_core/] [trunk/] [rtl/] [aes128_fast.vhd] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 hemanth
--*************************************************************************
2
-- Project    : AES128                                                    *
3
--                                                                        *
4
-- Block Name : aes128_fast.vhd                                           *
5
--                                                                        *
6
-- Author     : Hemanth Satyanarayana                                     *
7
--                                                                        *
8
-- Email      : hemanth@opencores.org                                     *
9
--                                                                        *
10
-- Description: This is the top level module for the aes core.            *
11
--              It instantiates the key expander and uses the             *
12
--              aes package for other transformations.                    *
13
--              Implementation is ECB mode.                               *
14
--                                                                        *
15
-- Revision History                                                       *
16
-- |-----------|-------------|---------|---------------------------------|*
17
-- |   Name    |    Date     | Version |          Revision details       |*
18
-- |-----------|-------------|---------|---------------------------------|*
19
-- | Hemanth   | 15-Dec-2004 | 1.1.1.1 |            Uploaded             |*
20
-- |-----------|-------------|---------|---------------------------------|*
21
--                                                                        *
22
--  Refer FIPS-197 document for details                                   *
23
--*************************************************************************
24
--                                                                        *
25
-- Copyright (C) 2004 Author                                              *
26
--                                                                        *
27
-- This source file may be used and distributed without                   *
28
-- restriction provided that this copyright statement is not              *
29
-- removed from the file and that any derivative work contains            *
30
-- the original copyright notice and the associated disclaimer.           *
31
--                                                                        *
32
-- This source file is free software; you can redistribute it             *
33
-- and/or modify it under the terms of the GNU Lesser General             *
34
-- Public License as published by the Free Software Foundation;           *
35
-- either version 2.1 of the License, or (at your option) any             *
36
-- later version.                                                         *
37
--                                                                        *
38
-- This source is distributed in the hope that it will be                 *
39
-- useful, but WITHOUT ANY WARRANTY; without even the implied             *
40
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR                *
41
-- PURPOSE.  See the GNU Lesser General Public License for more           *
42
-- details.                                                               *
43
--                                                                        *
44
-- You should have received a copy of the GNU Lesser General              *
45
-- Public License along with this source; if not, download it             *
46
-- from http://www.opencores.org/lgpl.shtml                               *
47
--                                                                        *
48
--*************************************************************************
49
library ieee;
50
use ieee.std_logic_1164.all;
51
use ieee.std_logic_unsigned.all;
52
use work.aes_package.all;
53
 
54
entity aes128_fast is
55
port(
56
      clk       : in std_logic;
57
      reset     : in std_logic;
58
      start     : in std_logic; -- to initiate the encryption/decryption process after loading
59
      mode      : in std_logic; -- to select encryption or decryption
60
      load      : in std_logic; -- to load the input and keys.has to 
61
      key       : in std_logic_vector(63 downto 0);
62
      data_in   : in std_logic_vector(63 downto 0);
63
      data_out  : out std_logic_vector(127 downto 0);
64
      done      : out std_logic
65
     );
66
 
67
end aes128_fast;
68
 
69
architecture mapping of aes128_fast is
70
 
71
 
72
component key_expander
73
port(
74
      clk      : in std_logic;
75
      reset    : in std_logic;
76
      key_in_c0: in state_array_type;
77
      key_in_c1: in state_array_type;
78
      key_in_c2: in state_array_type;
79
      key_in_c3: in state_array_type;
80
      count    : in integer;
81
      mode     : in std_logic;
82
      keyout_c0: out state_array_type;
83
      keyout_c1: out state_array_type;
84
      keyout_c2: out state_array_type;
85
      keyout_c3: out state_array_type
86
      );
87
end component;
88
 
89
signal data_in_reg0: state_array_type;
90
signal data_in_reg1: state_array_type;
91
signal data_in_reg2: state_array_type;
92
signal data_in_reg3: state_array_type;
93
signal key_reg0: state_array_type;
94
signal key_reg1: state_array_type;
95
signal key_reg2: state_array_type;
96
signal key_reg3: state_array_type;
97
signal s0      : state_array_type;
98
signal s1      : state_array_type;
99
signal s2      : state_array_type;
100
signal s3      : state_array_type;
101
signal s_00    : state_array_type;
102
signal s_01    : state_array_type;
103
signal s_02    : state_array_type;
104
signal s_03    : state_array_type;
105
signal r_00    : state_array_type;
106
signal r_01    : state_array_type;
107
signal r_02    : state_array_type;
108
signal r_03    : state_array_type;
109
signal load_d1 : std_logic;
110
signal start_d1: std_logic;
111
signal start_d2: std_logic;
112
signal round_cnt: integer range 0 to 15;
113
signal flag_cnt: std_logic;
114
signal done_d1 : std_logic;
115
signal done_d2 : std_logic;
116
 
117
signal mixcol_0: state_array_type;
118
signal mixcol_1: state_array_type;
119
signal mixcol_2: state_array_type;
120
signal mixcol_3: state_array_type;
121
 
122
signal new_key0: state_array_type;
123
signal new_key1: state_array_type;
124
signal new_key2: state_array_type;
125
signal new_key3: state_array_type;
126
signal new_key0_d1: state_array_type;
127
signal new_key1_d1: state_array_type;
128
signal new_key2_d1: state_array_type;
129
signal new_key3_d1: state_array_type;
130
 
131
signal s0_buf  : state_array_type;
132
signal s1_buf  : state_array_type;
133
signal s2_buf  : state_array_type;
134
signal s3_buf  : state_array_type;
135
 
136
signal next_round_data_0: state_array_type;
137
signal next_round_data_1: state_array_type;
138
signal next_round_data_2: state_array_type;
139
signal next_round_data_3: state_array_type;
140
 
141
signal pr_data_0: state_array_type;
142
signal pr_data_1: state_array_type;
143
signal pr_data_2: state_array_type;
144
signal pr_data_3: state_array_type;
145
 
146
signal mix_col_array   : std_logic_vector(0 to 127);
147
signal mixcol_key_array: std_logic_vector(0 to 127);
148
signal mixcol_key_0    : state_array_type;
149
signal mixcol_key_1    : state_array_type;
150
signal mixcol_key_2    : state_array_type;
151
signal mixcol_key_3    : state_array_type;
152
signal key_select_0    : state_array_type;
153
signal key_select_1    : state_array_type;
154
signal key_select_2    : state_array_type;
155
signal key_select_3    : state_array_type;
156
begin
157
 
158
-- Loading the data and keys
159
process(clk,reset)
160
begin
161
  if(reset = '1') then
162
    key_reg0 <= (others =>(others => '0'));
163
    key_reg1 <= (others =>(others => '0'));
164
    key_reg2 <= (others =>(others => '0'));
165
    key_reg3 <= (others =>(others => '0'));
166
    data_in_reg0 <= (others =>(others => '0'));
167
    data_in_reg1 <= (others =>(others => '0'));
168
    data_in_reg2 <= (others =>(others => '0'));
169
    data_in_reg3 <= (others =>(others => '0'));
170
  elsif rising_edge(clk) then
171
    if(load = '1' and load_d1 = '0') then
172
      key_reg0     <= (key(63 downto 56),key(55 downto 48),key(47 downto 40),key(39 downto 32));
173
      key_reg1     <= (key(31 downto 24),key(23 downto 16),key(15 downto 8),key(7 downto 0));
174
      data_in_reg0 <= (data_in(63 downto 56),data_in(55 downto 48),data_in(47 downto 40),data_in(39 downto 32));
175
      data_in_reg1 <= (data_in(31 downto 24),data_in(23 downto 16),data_in(15 downto 8),data_in(7 downto 0));
176
    elsif(load_d1 = '1' and load = '0') then
177
      key_reg2     <= (key(63 downto 56),key(55 downto 48),key(47 downto 40),key(39 downto 32));
178
      key_reg3     <= (key(31 downto 24),key(23 downto 16),key(15 downto 8),key(7 downto 0));
179
      data_in_reg2 <= (data_in(63 downto 56),data_in(55 downto 48),data_in(47 downto 40),data_in(39 downto 32));
180
      data_in_reg3 <= (data_in(31 downto 24),data_in(23 downto 16),data_in(15 downto 8),data_in(7 downto 0));
181
    end if;
182
  end if;
183
end process;
184
 
185
 
186
----------STATE MATRIX ROW WORDS ------
187
-- Given input xored with given key for generating input to the first round
188
s0(0) <= data_in_reg0(0) xor key_reg0(0);
189
s0(1) <= data_in_reg0(1) xor key_reg0(1);
190
s0(2) <= data_in_reg0(2) xor key_reg0(2);
191
s0(3) <= data_in_reg0(3) xor key_reg0(3);
192
s1(0) <= data_in_reg1(0) xor key_reg1(0);
193
s1(1) <= data_in_reg1(1) xor key_reg1(1);
194
s1(2) <= data_in_reg1(2) xor key_reg1(2);
195
s1(3) <= data_in_reg1(3) xor key_reg1(3);
196
s2(0) <= data_in_reg2(0) xor key_reg2(0);
197
s2(1) <= data_in_reg2(1) xor key_reg2(1);
198
s2(2) <= data_in_reg2(2) xor key_reg2(2);
199
s2(3) <= data_in_reg2(3) xor key_reg2(3);
200
s3(0) <= data_in_reg3(0) xor key_reg3(0);
201
s3(1) <= data_in_reg3(1) xor key_reg3(1);
202
s3(2) <= data_in_reg3(2) xor key_reg3(2);
203
s3(3) <= data_in_reg3(3) xor key_reg3(3);
204
 
205
-----------------SUB BYTES TRANSFORMATION--------------------------------------
206
process(s0_buf,s1_buf,s2_buf,s3_buf,mode)
207
begin
208
  if(mode = '1') then
209
    s_00(0) <= sbox_val(s0_buf(0));
210
    s_00(1) <= sbox_val(s0_buf(1));
211
    s_00(2) <= sbox_val(s0_buf(2));
212
    s_00(3) <= sbox_val(s0_buf(3));
213
 
214
    s_01(0) <= sbox_val(s1_buf(0));
215
    s_01(1) <= sbox_val(s1_buf(1));
216
    s_01(2) <= sbox_val(s1_buf(2));
217
    s_01(3) <= sbox_val(s1_buf(3));
218
 
219
    s_02(0) <= sbox_val(s2_buf(0));
220
    s_02(1) <= sbox_val(s2_buf(1));
221
    s_02(2) <= sbox_val(s2_buf(2));
222
    s_02(3) <= sbox_val(s2_buf(3));
223
 
224
    s_03(0) <= sbox_val(s3_buf(0));
225
    s_03(1) <= sbox_val(s3_buf(1));
226
    s_03(2) <= sbox_val(s3_buf(2));
227
    s_03(3) <= sbox_val(s3_buf(3));
228
  else
229
    s_00(0) <= inv_sbox_val(s0_buf(0));
230
    s_00(1) <= inv_sbox_val(s0_buf(1));
231
    s_00(2) <= inv_sbox_val(s0_buf(2));
232
    s_00(3) <= inv_sbox_val(s0_buf(3));
233
 
234
    s_01(0) <= inv_sbox_val(s1_buf(0));
235
    s_01(1) <= inv_sbox_val(s1_buf(1));
236
    s_01(2) <= inv_sbox_val(s1_buf(2));
237
    s_01(3) <= inv_sbox_val(s1_buf(3));
238
 
239
    s_02(0) <= inv_sbox_val(s2_buf(0));
240
    s_02(1) <= inv_sbox_val(s2_buf(1));
241
    s_02(2) <= inv_sbox_val(s2_buf(2));
242
    s_02(3) <= inv_sbox_val(s2_buf(3));
243
 
244
    s_03(0) <= inv_sbox_val(s3_buf(0));
245
    s_03(1) <= inv_sbox_val(s3_buf(1));
246
    s_03(2) <= inv_sbox_val(s3_buf(2));
247
    s_03(3) <= inv_sbox_val(s3_buf(3));
248
  end if;
249
end process;
250
 
251
-----------SHIFT ROWS TRANSFORMATION--------------------------------------
252
process(s_00,s_01,s_02,s_03,mode)
253
begin
254
  if(mode = '1') then
255
    r_00 <= (s_00(0),s_01(1),s_02(2),s_03(3));
256
    r_01 <= (s_01(0),s_02(1),s_03(2),s_00(3));
257
    r_02 <= (s_02(0),s_03(1),s_00(2),s_01(3));
258
    r_03 <= (s_03(0),s_00(1),s_01(2),s_02(3));
259
  else
260
    r_00 <= (s_00(0),s_03(1),s_02(2),s_01(3));
261
    r_01 <= (s_01(0),s_00(1),s_03(2),s_02(3));
262
    r_02 <= (s_02(0),s_01(1),s_00(2),s_03(3));
263
    r_03 <= (s_03(0),s_02(1),s_01(2),s_00(3));
264
  end if;
265
end process;
266
-----------MIX COLUMNS TRANSFORMATION--------------------------------------        
267
 
268
mix_col_array <= mix_cols_routine(r_00,r_01,r_02,r_03,mode);
269
mixcol_0 <= (mix_col_array(0 to 7),mix_col_array(8 to 15),mix_col_array(16 to 23),mix_col_array(24 to 31));
270
mixcol_1 <= (mix_col_array(32 to 39),mix_col_array(40 to 47),mix_col_array(48 to 55),mix_col_array(56 to 63));
271
mixcol_2 <= (mix_col_array(64 to 71),mix_col_array(72 to 79),mix_col_array(80 to 87),mix_col_array(88 to 95));
272
mixcol_3 <= (mix_col_array(96 to 103),mix_col_array(104 to 111),mix_col_array(112 to 119),mix_col_array(120 to 127));
273
 
274
mixcol_key_array <= mix_cols_routine(new_key0_d1,new_key1_d1,new_key2_d1,new_key3_d1,mode);
275
mixcol_key_0 <= (mixcol_key_array(0 to 7),mixcol_key_array(8 to 15),mixcol_key_array(16 to 23),mixcol_key_array(24 to 31));
276
mixcol_key_1 <= (mixcol_key_array(32 to 39),mixcol_key_array(40 to 47),mixcol_key_array(48 to 55),mixcol_key_array(56 to 63));
277
mixcol_key_2 <= (mixcol_key_array(64 to 71),mixcol_key_array(72 to 79),mixcol_key_array(80 to 87),mixcol_key_array(88 to 95));
278
mixcol_key_3 <= (mixcol_key_array(96 to 103),mixcol_key_array(104 to 111),mixcol_key_array(112 to 119),mixcol_key_array(120 to 127));
279
 
280
---------ADD ROUND KEY STEP-------------------------------------------------
281
expand_key:  key_expander
282
             port map(
283
                          clk       => clk,
284
                          reset     => reset,
285
                          key_in_c0 => key_reg0,
286
                          key_in_c1 => key_reg1,
287
                          key_in_c2 => key_reg2,
288
                          key_in_c3 => key_reg3,
289
                          count     => round_cnt,
290
                          mode      => mode,
291
                          keyout_c0 => new_key0,
292
                          keyout_c1 => new_key1,
293
                          keyout_c2 => new_key2,
294
                          keyout_c3 => new_key3
295
                       );
296
 
297
process(clk,reset)  ---- registered to increase speed
298
begin
299
  if(reset = '1') then
300
    new_key0_d1 <= (others =>(others => '0'));
301
    new_key1_d1 <= (others =>(others => '0'));
302
    new_key2_d1 <= (others =>(others => '0'));
303
    new_key3_d1 <= (others =>(others => '0'));
304
  elsif rising_edge(clk) then
305
    new_key0_d1 <= new_key0;
306
    new_key1_d1 <= new_key1;
307
    new_key2_d1 <= new_key2;
308
    new_key3_d1 <= new_key3;
309
  end if;
310
end process;
311
 
312
-- Previous round output as input to next round
313
next_round_data_0 <= (pr_data_0(0) xor key_select_0(0),pr_data_0(1) xor key_select_0(1),pr_data_0(2) xor key_select_0(2),pr_data_0(3) xor key_select_0(3));
314
next_round_data_1 <= (pr_data_1(0) xor key_select_1(0),pr_data_1(1) xor key_select_1(1),pr_data_1(2) xor key_select_1(2),pr_data_1(3) xor key_select_1(3));
315
next_round_data_2 <= (pr_data_2(0) xor key_select_2(0),pr_data_2(1) xor key_select_2(1),pr_data_2(2) xor key_select_2(2),pr_data_2(3) xor key_select_2(3));
316
next_round_data_3 <= (pr_data_3(0) xor key_select_3(0),pr_data_3(1) xor key_select_3(1),pr_data_3(2) xor key_select_3(2),pr_data_3(3) xor key_select_3(3));
317
 
318
-- Muxing for choosing data for the last round
319
pr_data_0 <= r_00 when round_cnt=11 else
320
             mixcol_0;
321
pr_data_1 <= r_01 when round_cnt=11 else
322
             mixcol_1;
323
pr_data_2 <= r_02 when round_cnt=11 else
324
             mixcol_2;
325
pr_data_3 <= r_03 when round_cnt=11 else
326
             mixcol_3;
327
 
328
key_select_0 <= new_key0_d1 when (mode = '1') else
329
                mixcol_key_0 when(mode = '0' and round_cnt < 11) else
330
                new_key0_d1;
331
key_select_1 <= new_key1_d1 when (mode = '1') else
332
                mixcol_key_1 when(mode = '0' and round_cnt < 11) else
333
                new_key1_d1;
334
key_select_2 <= new_key2_d1 when (mode = '1') else
335
                mixcol_key_2 when(mode = '0' and round_cnt < 11) else
336
                new_key2_d1;
337
key_select_3 <= new_key3_d1 when (mode = '1') else
338
                mixcol_key_3 when(mode = '0' and round_cnt < 11) else
339
                new_key3_d1;
340
done <= done_d2;
341
 
342
-- Registering start and load             
343
process(clk,reset)
344
begin
345
  if(reset = '1') then
346
    load_d1  <= '0';
347
    start_d1 <= '0';
348
    start_d2 <= '0';
349
  elsif rising_edge(clk) then
350
    load_d1  <= load;
351
    start_d1 <= start;
352
    start_d2 <= start_d1;
353
  end if;
354
end process;
355
 
356
-- Register outputs at end of each round
357
process(clk,reset)
358
begin
359
  if(reset = '1') then
360
    s0_buf <= (others =>(others => '0'));
361
    s1_buf <= (others =>(others => '0'));
362
    s2_buf <= (others =>(others => '0'));
363
    s3_buf <= (others =>(others => '0'));
364
  elsif rising_edge(clk) then
365
    if(round_cnt = 0 or round_cnt = 1) then
366
      s0_buf <= s0;
367
      s1_buf <= s1;
368
      s2_buf <= s2;
369
      s3_buf <= s3;
370
    else
371
      s0_buf <= next_round_data_0;
372
      s1_buf <= next_round_data_1;
373
      s2_buf <= next_round_data_2;
374
      s3_buf <= next_round_data_3;
375
    end if;
376
  end if;
377
end process;
378
 
379
-- Initiator process
380
process(clk,reset)
381
begin
382
  if(reset = '1') then
383
    round_cnt <= 0;
384
    flag_cnt <= '0';
385
  elsif rising_edge(clk) then
386
    if((start_d2 = '1' and start_d1 = '0') or flag_cnt = '1') then
387
      if(round_cnt < 11) then
388
        round_cnt <= round_cnt + 1;
389
        flag_cnt <= '1';
390
      else
391
        round_cnt <= 0;
392
        flag_cnt <= '0';
393
      end if;
394
    end if;
395
  end if;
396
end process;
397
 
398
-- Completion signalling process
399
process(clk,reset)
400
begin
401
  if(reset = '1') then
402
    done_d1 <= '0';
403
    done_d2 <= '0';
404
  elsif rising_edge(clk) then
405
    if(start_d2 = '1' and start_d1 = '0') then
406
      done_d1 <= '0';
407
      done_d2 <= '0';
408
    elsif(round_cnt = 10) then
409
      done_d1 <= '1';
410
    end if;
411
    done_d2 <= done_d1;
412
  end if;
413
end process;
414
 
415
-- Output assignment process        
416
process(clk,reset)
417
begin
418
  if(reset= '1') then
419
    data_out <= (others => '0');
420
  elsif rising_edge(clk) then
421
    if(done_d1 = '1' and done_d2 = '0') then
422
        data_out <= (next_round_data_0(0) & next_round_data_0(1) & next_round_data_0(2) & next_round_data_0(3) &
423
                     next_round_data_1(0) & next_round_data_1(1) & next_round_data_1(2) & next_round_data_1(3) &
424
                     next_round_data_2(0) & next_round_data_2(1) & next_round_data_2(2) & next_round_data_2(3) &
425
                     next_round_data_3(0) & next_round_data_3(1) & next_round_data_3(2) & next_round_data_3(3));
426
    end if;
427
  end if;
428
end process;
429
 
430
end mapping;
431
 
432
 
433
 
434
 

powered by: WebSVN 2.1.0

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