-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaes_Encrypt_FSM.vhd
193 lines (168 loc) · 5.37 KB
/
aes_Encrypt_FSM.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library work;
use work.aes_types.all;
entity aes_Encrypt_FSM is
port(
key_in : in matrix(3 downto 0, 3 downto 0);
data_block_in : in matrix(3 downto 0, 3 downto 0);
data_block_out : out matrix(3 downto 0, 3 downto 0);
key_load : in std_logic;
start : in std_logic;
done : out std_logic;
busy : out std_logic;
clk : in std_logic;
rst : in std_logic
);
end entity aes_Encrypt_FSM;
architecture RTL of aes_Encrypt_FSM is
component aes_KeySchedule_FSM
port(key_in : in matrix(3 downto 0, 3 downto 0);
keychain_out : out matrix_128(10 downto 0);
start : in std_logic;
done : out std_logic;
clk : in std_logic;
rst : in std_logic);
end component aes_KeySchedule_FSM;
component aes_SubBytes_ShiftRows
port(data_in : in matrix(3 downto 0, 3 downto 0);
data_out : out matrix(3 downto 0, 3 downto 0);
start : in std_logic;
done : out std_logic;
clk : in std_logic;
rst : in std_logic);
end component aes_SubBytes_ShiftRows;
component aes_MixColumns
port(data_in : in matrix(3 downto 0, 3 downto 0);
data_out : out matrix(3 downto 0, 3 downto 0);
start : in std_logic;
done : out std_logic;
clk : in std_logic;
rst : in std_logic);
end component aes_MixColumns;
component aes_AddRoundKey
port(data_in : in matrix(3 downto 0, 3 downto 0);
key_in : in matrix(3 downto 0, 3 downto 0);
data_out : out matrix(3 downto 0, 3 downto 0);
clk : in std_logic;
rst : in std_logic);
end component aes_AddRoundKey;
signal latched_data_in, round_data_out, round_data_in, round_key, latched_key_in : matrix(3 downto 0, 3 downto 0);
signal ss_data_in, ss_data_out, mc_data_in, mc_data_out : matrix(3 downto 0, 3 downto 0);
signal mc_start, mc_done, ss_done, ss_start : std_logic;
signal keychain : matrix_128(10 downto 0);
signal round_counter : integer range 1 to 10 := 1;
signal ks_start, ks_done : std_logic;
type state is (IDLE, KEYSCHEDULE, INITIAL_ROUND, MAIN_ROUND, ENC_OUTPUT);
signal current_state : state := IDLE;
begin
Inst_aes_KeySchedule_FSM : aes_KeySchedule_FSM
port map(
key_in => latched_key_in,
keychain_out => keychain,
start => ks_start,
done => ks_done,
clk => clk,
rst => rst
);
Inst_aes_SubBytes_ShiftRows : aes_SubBytes_ShiftRows
port map(
data_in => ss_data_in,
data_out => ss_data_out,
done => ss_done,
start => ss_start,
clk => clk,
rst => rst
);
Inst_aes_MixColumns : aes_MixColumns
port map(
data_in => mc_data_in,
data_out => mc_data_out,
start => mc_start,
done => mc_done,
clk => clk,
rst => rst
);
state_proc : process(clk) is
begin
if (rising_edge(clk)) then
if (rst = '1') then
current_state <= IDLE;
for i in 0 to 3 loop
for j in 0 to 3 loop
round_data_in(i, j) <= (others => '0');
round_data_out(i, j) <= (others => '0');
latched_key_in(i, j) <= (others => '0');
round_counter <= 1;
end loop;
end loop;
else
if (start = '1') then
latched_data_in <= data_block_in;
if (key_load = '1') then
latched_key_in <= key_in;
current_state <= KEYSCHEDULE;
else
latched_key_in <= latched_key_in;
current_state <= INITIAL_ROUND;
end if;
else
latched_key_in <= latched_key_in;
current_state <= current_state;
end if;
case current_state is
when IDLE =>
null;
when KEYSCHEDULE =>
if (ks_done = '1') then
current_state <= INITIAL_ROUND;
ks_start <= '0';
else
current_state <= current_state;
ks_start <= '1';
end if;
when INITIAL_ROUND =>
round_key <= keychain(0);
round_data_in <= round_data_out;
round_data_out <= latched_data_in XOR round_key;
current_state <= MAIN_ROUND;
done <= '0';
when MAIN_ROUND =>
round_key <= keychain(round_counter);
if (round_counter = 10) then
ss_data_in <= round_data_in;
round_data_out <= ss_data_in XOR round_data_out;
ss_start <= '1';
if (ss_done = '1') then
current_state <= ENC_OUTPUT;
round_counter <= 1;
else
current_state <= current_state;
end if;
else
ss_data_in <= round_data_in;
mc_data_in <= ss_data_out;
round_data_out <= mc_data_out XOR round_key;
ss_start <= '1';
if (ss_done = '1') then
mc_start <= '1';
else
mc_start <= '0';
end if;
if (mc_done = '1') then
round_counter <= round_counter + 1;
round_data_in <= round_data_out;
end if;
current_state <= current_state;
end if;
done <= '0';
when ENC_OUTPUT =>
done <= '1';
data_block_out <= round_data_out;
current_state <= IDLE;
end case;
end if;
end if;
end process state_proc;
end architecture RTL;