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

Subversion Repositories mini_aes

[/] [mini_aes/] [trunk/] [source/] [key_scheduler.vhdl] - Blame information for rev 22

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 arif_endro
-- ------------------------------------------------------------------------
2 15 arif_endro
-- Copyright (C) 2005 Arif Endro Nugroho
3 21 arif_endro
-- All rights reserved.
4 2 arif_endro
-- 
5 21 arif_endro
-- Redistribution and use in source and binary forms, with or without
6
-- modification, are permitted provided that the following conditions
7
-- are met:
8 2 arif_endro
-- 
9 21 arif_endro
-- 1. Redistributions of source code must retain the above copyright
10
--    notice, this list of conditions and the following disclaimer.
11
-- 2. Redistributions in binary form must reproduce the above copyright
12
--    notice, this list of conditions and the following disclaimer in the
13
--    documentation and/or other materials provided with the distribution.
14 2 arif_endro
-- 
15 21 arif_endro
-- THIS SOFTWARE IS PROVIDED BY ARIF ENDRO NUGROHO "AS IS" AND ANY EXPRESS
16
-- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
-- DISCLAIMED. IN NO EVENT SHALL ARIF ENDRO NUGROHO BE LIABLE FOR ANY
19
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
-- POSSIBILITY OF SUCH DAMAGE.
26 2 arif_endro
-- 
27 21 arif_endro
-- End Of License.
28
-- ------------------------------------------------------------------------
29 2 arif_endro
 
30
library ieee;
31
use ieee.std_logic_1164.all;
32
use ieee.std_logic_unsigned.all;
33
 
34
entity key_scheduler is
35
 
36
  port (
37
    clock : in  std_logic;
38
    load  : in  std_logic;
39
    key_i : in  std_logic_vector (127 downto 00);
40
    key_o : out std_logic_vector (31 downto 00);
41
    done  : out std_logic
42
    );
43
 
44
end key_scheduler;
45
 
46
architecture key_expansion of key_scheduler is
47
 
48
  component bram_block_a
49
    port (
50
      clk_a_i    : in  std_logic;
51
      en_a_i     : in  std_logic;
52
      we_a_i     : in  std_logic;
53
      di_a_i     : in  std_logic_vector (07 downto 00);
54
      addr_a_1_i : in  std_logic_vector (08 downto 00);
55
      addr_a_2_i : in  std_logic_vector (08 downto 00);
56
      do_a_1_o   : out std_logic_vector (07 downto 00);
57
      do_a_2_o   : out std_logic_vector (07 downto 00)
58
      );
59
  end component;
60
--
61
  component bram_block_b
62
    port (
63
      clk_b_i    : in  std_logic;
64
      we_b_i     : in  std_logic;
65
      en_b_i     : in  std_logic;
66
      di_b_i     : in  std_logic_vector (07 downto 00);
67
      addr_b_1_i : in  std_logic_vector (08 downto 00);
68
      addr_b_2_i : in  std_logic_vector (08 downto 00);
69
      do_b_1_o   : out std_logic_vector (07 downto 00);
70
      do_b_2_o   : out std_logic_vector (07 downto 00)
71
      );
72
  end component;
73
--
74
  component counter2bit
75
    port (
76
      clock      : in  std_logic;
77
      clear      : in  std_logic;
78
      count      : out std_logic_vector (1 downto 0));
79
  end component;
80
 
81
  type state_element is array (03 downto 00) of std_logic_vector (07 downto 00);
82
 
83
  signal   clk_a_i        : std_logic;
84
  constant enc            : std_logic                       := '0';
85
  signal   en_a_i         : std_logic;
86
  signal   we_a_i         : std_logic;
87
  signal   di_a_i         : std_logic_vector (07 downto 00) := ( B"0000_0000" );
88
  signal   addr_a_1_i     : std_logic_vector (08 downto 00);
89
  signal   addr_a_2_i     : std_logic_vector (08 downto 00);
90
  signal   do_a_1_o       : std_logic_vector (07 downto 00);
91
  signal   do_a_2_o       : std_logic_vector (07 downto 00);
92
--
93
  signal   clk_b_i        : std_logic;
94
  signal   en_b_i         : std_logic;
95
  signal   we_b_i         : std_logic;
96
  signal   di_b_i         : std_logic_vector (07 downto 00) := ( B"0000_0000" );
97
  signal   addr_b_1_i     : std_logic_vector (08 downto 00);
98
  signal   addr_b_2_i     : std_logic_vector (08 downto 00);
99
  signal   do_b_1_o       : std_logic_vector (07 downto 00);
100
  signal   do_b_2_o       : std_logic_vector (07 downto 00);
101
--
102
  signal   temp           : state_element                   := ( B"00000000", B"00000000", B"00000000", B"00000000" );
103
  signal   side_opt       : state_element                   := ( B"00000000", B"00000000", B"00000000", B"00000000" );
104
  signal   result         : state_element                   := ( B"00000000", B"00000000", B"00000000", B"00000000" );
105
--
106
  signal   rot            : std_logic                       := '0';
107
  signal   count          : std_logic_vector (1 downto 0)   := ( B"00" );
108
  signal   rcon           : std_logic_vector (07 downto 00) := ( X"01" );
109
  constant round_constant : std_logic_vector (79 downto 00) := ( X"01020408_10204080_1B36");
110
  signal   rcon10x8       : std_logic_vector (79 downto 00) := ( X"01020408_10204080_1B36");
111
  signal   fifo12x8       : std_logic_vector (95 downto 00) := ( X"00000000_00000000_00000000");
112
 
113
begin
114
 
115
  clk_a_i <= clock;
116
  clk_b_i <= clock;
117
  en_a_i  <= '1';
118
  en_b_i  <= '1';
119
  we_a_i  <= '0';
120
  we_b_i  <= '0';
121
--
122
  sbox1 : bram_block_a
123
    port map (
124
      clk_a_i    => clk_a_i,
125
      en_a_i     => en_a_i,
126
      we_a_i     => we_a_i,
127
      di_a_i     => di_a_i,
128
      addr_a_1_i => addr_a_1_i,
129
      addr_a_2_i => addr_a_2_i,
130
      do_a_1_o   => do_a_1_o,
131
      do_a_2_o   => do_a_2_o
132
      );
133
--
134
  sbox2 : bram_block_b
135
    port map (
136
      clk_b_i    => clk_b_i,
137
      we_b_i     => we_b_i,
138
      en_b_i     => en_b_i,
139
      di_b_i     => di_b_i,
140
      addr_b_1_i => addr_b_1_i,
141
      addr_b_2_i => addr_b_2_i,
142
      do_b_1_o   => do_b_1_o,
143
      do_b_2_o   => do_b_2_o
144
      );
145
--
146
  rc    : counter2bit
147
    port map (
148
      clock      => clock,
149
      clear      => load,
150
      count      => count
151
      );
152
 
153
  ---------------------------------------------------------------
154
  -- key input 127 - 96 => column 0
155
  -- key input  95 - 64 => column 1
156
  -- key input  63 - 32 => column 2
157
  -- key input  31 -  0 => column 3 (root word) (shift) (subbyte)
158
  ---------------------------------------------------------------
159
 
160
  ---------------------------------------------------------------
161
  -- Round constant table
162
  --  encrypt:        decrypt:
163
  -- round 0 : 0x0100_0000   : 0x3600_0000
164
  -- round 1 : 0x0200_0000   : 0x1B00_0000
165
  -- round 2 : 0x0400_0000   : 0x8000_0000
166
  -- round 3 : 0x0800_0000   : 0x4000_0000
167
  -- round 4 : 0x1000_0000   : 0x2000_0000
168
  -- round 5 : 0x2000_0000   : 0x1000_0000
169
  -- round 6 : 0x4000_0000   : 0x0800_0000
170
  -- round 7 : 0x8000_0000   : 0x0400_0000
171
  -- round 8 : 0x1B00_0000   : 0x0200_0000
172
  -- round 9 : 0x3600_0000   : 0x0100_0000
173
  ---------------------------------------------------------------
174
 
175
  process (clock, load)
176
  begin
177
--
178
    if (load = '1') then
179
--
180
      fifo12x8 (095 downto 000) <= key_i (127 downto 032);
181
--
182
      side_opt (3)              <= key_i (031 downto 024);
183
      side_opt (2)              <= key_i (023 downto 016);
184
      side_opt (1)              <= key_i (015 downto 008);
185
      side_opt (0)              <= key_i (007 downto 000);
186
--
187
      addr_a_1_i                <= ( enc & key_i (023 downto 016) );
188
      addr_a_2_i                <= ( enc & key_i (015 downto 008) );
189
      addr_b_1_i                <= ( enc & key_i (007 downto 000) );
190
      addr_b_2_i                <= ( enc & key_i (031 downto 024) );
191
--
192
    elsif (clock = '1' and clock'event) then
193
--
194
      fifo12x8 (95 downto 32)   <= fifo12x8 (63 downto 00);
195
      fifo12x8 (31 downto 00)   <= side_opt (3) & side_opt (2) & side_opt (1) & side_opt (0);
196
--
197
      side_opt (3)              <= result(3);
198
      side_opt (2)              <= result(2);
199
      side_opt (1)              <= result(1);
200
      side_opt (0)              <= result(0);
201
--
202
      addr_a_1_i                <= ( enc & result (2) );
203
      addr_a_2_i                <= ( enc & result (1) );
204
      addr_b_1_i                <= ( enc & result (0) );
205
      addr_b_2_i                <= ( enc & result (3) );
206
--
207
    end if;
208
--
209
  end process;
210
--
211
  process (clock, load)
212
  begin
213
--
214
    if (load = '1') then
215
--
216
      rcon10x8 (79 downto 00)   <= round_constant (79 downto 00);
217
--
218
    elsif (clock = '1' and clock'event) then
219
--
220
      if (count = "10") then
221
--
222
        rcon10x8 (79 downto 08) <= rcon10x8 (71 downto 00);
223
        rcon10x8 (07 downto 00) <= rcon10x8 (79 downto 72);
224
--
225
      end if;
226
--
227
      done                      <= not(load) and count(1) and not(count(0)) and rcon(5) and rcon(4) and rcon(2) and rcon(1);
228
--
229
    end if;
230
--
231
  end process;
232
 
233
  rcon (07 downto 00)  <= rcon10x8 (79 downto 72);
234
--
235
  rot                  <= ( not(count(1)) and not(count(0)) ) when (load = '0') else '1';
236
--
237
  temp (3)             <= (do_a_1_o xor rcon)                 when (rot = '1')  else side_opt (3);
238
  temp (2)             <= (do_a_2_o)                          when (rot = '1')  else side_opt (2);
239
  temp (1)             <= (do_b_1_o)                          when (rot = '1')  else side_opt (1);
240
  temp (0)             <= (do_b_2_o)                          when (rot = '1')  else side_opt (0);
241
--
242
  result (3)           <= temp (3) xor fifo12x8 (95 downto 88);
243
  result (2)           <= temp (2) xor fifo12x8 (87 downto 80);
244
  result (1)           <= temp (1) xor fifo12x8 (79 downto 72);
245
  result (0)           <= temp (0) xor fifo12x8 (71 downto 64);
246
--
247
  key_o (31 downto 24) <= result(3);
248
  key_o (23 downto 16) <= result(2);
249
  key_o (15 downto 08) <= result(1);
250
  key_o (07 downto 00) <= result(0);
251
 
252
end key_expansion;

powered by: WebSVN 2.1.0

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