1 |
6 |
feketebv |
library ieee;
|
2 |
|
|
use ieee.std_logic_1164.all;
|
3 |
|
|
use ieee.numeric_std.all;
|
4 |
3 |
feketebv |
use ieee.std_logic_unsigned.all;
|
5 |
|
|
|
6 |
|
|
entity keyexpansion is
|
7 |
|
|
port ( reset : in std_logic;
|
8 |
|
|
clock : in std_logic;
|
9 |
|
|
loadkey : in std_logic;
|
10 |
|
|
key : in std_logic_vector (31 downto 0);
|
11 |
|
|
subkeyenable : out std_logic;
|
12 |
|
|
subkeyaddress : out std_logic_vector (3 downto 0);
|
13 |
|
|
subkey : out std_logic_vector (127 downto 0);
|
14 |
|
|
keyexpansionready : inout std_logic;
|
15 |
|
|
numberofrounds : out std_logic_vector (3 downto 0));
|
16 |
|
|
end keyexpansion;
|
17 |
|
|
|
18 |
|
|
architecture Behavioral of keyExpansion is
|
19 |
|
|
signal temp, rotW0, subIn, subRotW0 : std_logic_vector(31 downto 0);
|
20 |
|
|
type wT is array(0 to 7) of std_logic_vector(31 downto 0);
|
21 |
|
|
signal w : wT;
|
22 |
|
|
type RconT is array(0 to 10) of std_logic_vector(7 downto 0);
|
23 |
|
|
constant Rcon : RconT := (x"01",x"02",x"04",x"08",x"10",x"20",x"40",x"80",x"1b",x"36",x"ee");
|
24 |
6 |
feketebv |
signal iModNk, Nk : integer range 0 to 7;
|
25 |
|
|
signal RconCnt : integer range 0 to 10;
|
26 |
|
|
signal roundCounter : std_Logic_vector(5 downto 0);
|
27 |
3 |
feketebv |
begin
|
28 |
|
|
|
29 |
|
|
keyExpansionFSM: process(clock)
|
30 |
|
|
begin
|
31 |
|
|
if rising_edge(clock) then
|
32 |
|
|
if reset = '1' then
|
33 |
6 |
feketebv |
Nk <= 0;
|
34 |
|
|
iModNk <= 0;
|
35 |
3 |
feketebv |
RoundCounter <= "000000";
|
36 |
6 |
feketebv |
RconCnt <= 0;
|
37 |
3 |
feketebv |
elsif loadKey = '1' then
|
38 |
6 |
feketebv |
Nk <= Nk + 1;
|
39 |
|
|
elsif keyExpansionReady = '0' then
|
40 |
3 |
feketebv |
roundCounter <= roundCounter + '1';
|
41 |
|
|
if iModNk = Nk then
|
42 |
6 |
feketebv |
iModNk <= 0;
|
43 |
|
|
if RconCnt < 10 then
|
44 |
|
|
RconCnt <= RconCnt + 1;
|
45 |
|
|
end if;
|
46 |
3 |
feketebv |
else
|
47 |
6 |
feketebv |
iModNk <= iModNk + 1;
|
48 |
3 |
feketebv |
end if;
|
49 |
|
|
end if;
|
50 |
|
|
end if;
|
51 |
|
|
end process;
|
52 |
|
|
--begin keyExpansionFSM asynchron circuitry
|
53 |
6 |
feketebv |
keyExpansionReady <= '1' when Nk = 3 and roundCounter = "101000" else --44=32+8+4
|
54 |
|
|
'1' when Nk = 5 and roundCounter = "110000" else --52=32+16+4
|
55 |
|
|
'1' when Nk = 7 and roundCounter = "111000" else --60=32+16+8+4
|
56 |
3 |
feketebv |
'0';
|
57 |
6 |
feketebv |
numberOfRounds <= x"a" when Nk = 3 else
|
58 |
|
|
x"c" when Nk = 5 else
|
59 |
|
|
x"e" when Nk = 7 else x"0";
|
60 |
3 |
feketebv |
--end keyExpansionFSM asynchron circuitry
|
61 |
|
|
|
62 |
|
|
keyExpansionPipe: process(clock)
|
63 |
|
|
begin
|
64 |
|
|
if rising_edge(clock) then
|
65 |
|
|
if loadKey = '1' then
|
66 |
|
|
w <= key & w(0 to 6);
|
67 |
|
|
elsif keyExpansionReady = '0' then
|
68 |
6 |
feketebv |
case Nk is
|
69 |
|
|
when 3 =>
|
70 |
|
|
w(0 to 3) <= w(1 to 3) & (w(0) xor temp);
|
71 |
|
|
when 5 =>
|
72 |
|
|
w(0 to 5) <= w(1 to 5) & (w(0) xor temp);
|
73 |
|
|
when others =>
|
74 |
|
|
w(0 to 7) <= w(1 to 7) & (w(0) xor temp);
|
75 |
|
|
end case;
|
76 |
|
|
end if;
|
77 |
3 |
feketebv |
end if;
|
78 |
|
|
end process;
|
79 |
|
|
--begin keyExpansionPipe asynchron circuitry
|
80 |
6 |
feketebv |
rotW0 <= w(Nk)(23 downto 0) & w(Nk)(31 downto 24);
|
81 |
|
|
subIn <= rotW0 when iModNk = 0 else
|
82 |
|
|
w(Nk) when Nk = 7 and iModNk = 4 else x"00000000";
|
83 |
|
|
subBytesFor: for i in 0 to 3 generate
|
84 |
3 |
feketebv |
type subT is array(0 to 255) of std_logic_vector(7 downto 0);
|
85 |
6 |
feketebv |
constant sub : subT :=
|
86 |
|
|
(x"63", x"7c", x"77", x"7b", x"f2", x"6b", x"6f", x"c5", x"30", x"01", x"67", x"2b", x"fe", x"d7", x"ab", x"76",
|
87 |
3 |
feketebv |
x"ca", x"82", x"c9", x"7d", x"fa", x"59", x"47", x"f0", x"ad", x"d4", x"a2", x"af", x"9c", x"a4", x"72", x"c0",
|
88 |
6 |
feketebv |
x"b7", x"fd", x"93", x"26", x"36", x"3f", x"f7", x"cc", x"34", x"a5", x"e5", x"f1", x"71", x"d8", x"31", x"15",
|
89 |
3 |
feketebv |
x"04", x"c7", x"23", x"c3", x"18", x"96", x"05", x"9a", x"07", x"12", x"80", x"e2", x"eb", x"27", x"b2", x"75",
|
90 |
6 |
feketebv |
x"09", x"83", x"2c", x"1a", x"1b", x"6e", x"5a", x"a0", x"52", x"3b", x"d6", x"b3", x"29", x"e3", x"2f", x"84",
|
91 |
|
|
x"53", x"d1", x"00", x"ed", x"20", x"fc", x"b1", x"5b", x"6a", x"cb", x"be", x"39", x"4a", x"4c", x"58", x"cf",
|
92 |
|
|
x"d0", x"ef", x"aa", x"fb", x"43", x"4d", x"33", x"85", x"45", x"f9", x"02", x"7f", x"50", x"3c", x"9f", x"a8",
|
93 |
|
|
x"51", x"a3", x"40", x"8f", x"92", x"9d", x"38", x"f5", x"bc", x"b6", x"da", x"21", x"10", x"ff", x"f3", x"d2",
|
94 |
|
|
x"cd", x"0c", x"13", x"ec", x"5f", x"97", x"44", x"17", x"c4", x"a7", x"7e", x"3d", x"64", x"5d", x"19", x"73",
|
95 |
|
|
x"60", x"81", x"4f", x"dc", x"22", x"2a", x"90", x"88", x"46", x"ee", x"b8", x"14", x"de", x"5e", x"0b", x"db",
|
96 |
|
|
x"e0", x"32", x"3a", x"0a", x"49", x"06", x"24", x"5c", x"c2", x"d3", x"ac", x"62", x"91", x"95", x"e4", x"79",
|
97 |
|
|
x"e7", x"c8", x"37", x"6d", x"8d", x"d5", x"4e", x"a9", x"6c", x"56", x"f4", x"ea", x"65", x"7a", x"ae", x"08",
|
98 |
|
|
x"ba", x"78", x"25", x"2e", x"1c", x"a6", x"b4", x"c6", x"e8", x"dd", x"74", x"1f", x"4b", x"bd", x"8b", x"8a",
|
99 |
|
|
x"70", x"3e", x"b5", x"66", x"48", x"03", x"f6", x"0e", x"61", x"35", x"57", x"b9", x"86", x"c1", x"1d", x"9e",
|
100 |
|
|
x"e1", x"f8", x"98", x"11", x"69", x"d9", x"8e", x"94", x"9b", x"1e", x"87", x"e9", x"ce", x"55", x"28", x"df",
|
101 |
3 |
feketebv |
x"8c", x"a1", x"89", x"0d", x"bf", x"e6", x"42", x"68", x"41", x"99", x"2d", x"0f", x"b0", x"54", x"bb", x"16");
|
102 |
|
|
begin
|
103 |
|
|
subRotW0(8*(i+1)-1 downto 8*i) <= sub(conv_integer(subIn(8*(i+1)-1 downto 8*i)));
|
104 |
|
|
end generate;
|
105 |
6 |
feketebv |
temp <= subRotW0 xor (Rcon(RconCnt) & x"000000") when iModNk = 0 else
|
106 |
|
|
subRotW0 when Nk = 7 and iModNk = 4 else
|
107 |
|
|
w(Nk);
|
108 |
3 |
feketebv |
--end keyExpansionPipe asynchron circuitry
|
109 |
|
|
|
110 |
|
|
subKey <= w(0) & w(1) & w(2) & w(3);
|
111 |
|
|
subKeyAddress <= roundCounter(5 downto 2);
|
112 |
|
|
subKeyEnable <= '1' when roundCounter(1 downto 0) = "00" else '0';
|
113 |
|
|
end Behavioral;
|