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

Subversion Repositories threeaesc

[/] [threeaesc/] [trunk/] [aes_c_1/] [src/] [aes_fsm_enc.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 entactogen
-- Copyright (c) 2011 Antonio de la Piedra
2
 
3
-- This program is free software: you can redistribute it and/or modify
4
-- it under the terms of the GNU General Public License as published by
5
-- the Free Software Foundation, either version 3 of the License, or
6
-- (at your option) any later version.
7
 
8
-- This program is distributed in the hope that it will be useful,
9
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
10
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
-- GNU General Public License for more details.
12
 
13
-- You should have received a copy of the GNU General Public License
14
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
library IEEE;
17
 
18
use IEEE.STD_LOGIC_1164.ALL;
19
use IEEE.std_logic_ARITH.ALL;
20
use IEEE.std_logic_UNSIGNED.ALL;
21
 
22
use ieee.numeric_std.all;
23
 
24
use work.aes_lib.all;
25
 
26
entity aes_fsm_enc is
27
        port(     clk: in std_logic;
28
                  rst : in std_logic;
29
                  block_in : in std_logic_vector(127 downto 0);
30
                  key : in std_logic_vector(127 downto 0);
31
                  enc : in std_logic;
32
 
33
                  block_out : out std_logic_vector(127 downto 0);
34
                  block_ready : out std_logic);
35
        end aes_fsm_enc;
36
 
37
architecture Behavioral of aes_fsm_enc is
38
 
39
  type state_type is (idle, n_round_1, n_round_2, n_round_3, n_round_4, n_round_5, n_round_6, last_round_1,
40
                      last_round_2, last_round_3, last_round_4, last_round_5, last_round_6, pre);
41
 
42
  signal state, next_state: state_type ;
43
  signal block_in_s :  std_logic_vector(127 downto 0);
44
  signal sub_key_s :  std_logic_vector(127 downto 0);
45
  signal load_s :  std_logic;
46
  signal enc_s :  std_logic;
47
  signal last_s, rst_cnt :  std_logic;
48
  signal block_out_s :  std_logic_vector(127 downto 0);
49
  signal count: natural range 0 to 10;
50
  signal en_cnt : std_logic;
51
 
52
  signal key_addr_1, key_addr_2 : std_logic_vector(3 downto 0);
53
  signal key_data_1, key_data_delay_1, key_data_2, key_data_delay_2 : std_logic_vector(127 downto 0);
54
 
55
begin
56
 
57
  process1: process (clk,rst)
58
  begin
59
    if (rst ='1') then
60
      state <= idle;
61
    elsif rising_edge(clk) then
62
      state <= next_state;
63
    end if;
64
  end process process1;
65
 
66
  process2 : process (state, enc, block_in, key, block_out_s)
67
    variable block_reg_v : std_logic_vector(127 downto 0);
68
  begin
69
    next_state <= state;
70
 
71
    block_reg_v := (others => '0');
72
    block_in_s <= (others => '0');
73
 
74
   sub_key_s <= (others => '0');
75
 
76
   enc_s <= '0';
77
   load_s <= '0';
78
   last_s <= '0';
79
   block_ready <= '0';
80
 
81
    case state is
82
          when idle =>
83
            if enc ='1' then
84
              next_state <= pre;
85
            else
86
              next_state <= idle;
87
            end if;
88
          when pre =>
89
            rst_cnt <= '0';
90
 
91
            for i in 0 to 127 loop
92
              block_reg_v(i) := block_in(i) xor key(i);
93
            end loop;
94
 
95
            load_s <= '1';
96
            enc_s <= '0';
97
 
98
            sub_key_s <= key_data_1;
99
            block_in_s <= block_reg_v;
100
 
101
            next_state <= n_round_1;
102
          when n_round_1 =>
103
            enc_s <= '1';
104
            load_s <= '0';
105
 
106
            next_state <= n_round_2;
107
 
108
          when n_round_2 =>
109
            enc_s <= '1';
110
            load_s <= '0';
111
 
112
            next_state <= n_round_3;
113
          when n_round_3 =>
114
            enc_s <= '1';
115
            load_s <= '0';
116
 
117
            next_state <= n_round_4;
118
          when n_round_4 =>
119
 
120
            enc_s <= '1';
121
            load_s <= '0';
122
 
123
            next_state <= n_round_5;
124
          when n_round_5 =>
125
            enc_s <= '1';
126
            load_s <= '0';
127
 
128
            next_state <= n_round_6;
129
          when n_round_6 =>
130
            enc_s <= '1';
131
            load_s <= '1';
132
 
133
            sub_key_s <=  key_data_1;
134
            block_in_s <= block_out_s;
135
 
136
            if count = 9 then
137
              next_state <= last_round_1;
138
            else
139
              next_state <= n_round_1;
140
            end if;
141
          when last_round_1 =>
142
            enc_s <= '1';
143
            load_s <= '0';
144
            last_s <= '1';
145
 
146
            next_state <= last_round_2;
147
          when last_round_2 =>
148
            enc_s <= '1';
149
            load_s <= '0';
150
            last_s <= '1';
151
 
152
            next_state <= last_round_3;
153
          when last_round_3 =>
154
            enc_s <= '1';
155
            load_s <= '0';
156
            last_s <= '1';
157
 
158
            next_state <= last_round_4;
159
          when last_round_4 =>
160
            enc_s <= '1';
161
            load_s <= '0';
162
            last_s <= '1';
163
 
164
            next_state <= last_round_5;
165
          when last_round_5 =>
166
            enc_s <= '1';
167
            load_s <= '0';
168
            last_s <= '1';
169
 
170
            rst_cnt <= '1';
171
            next_state <= last_round_6;
172
          when last_round_6 =>
173
            enc_s <= '1';
174
            load_s <= '0';
175
            last_s <= '1';
176
 
177
            block_ready <= '1';
178
 
179
            rst_cnt <= '0';
180
            next_state <= idle;
181
    end case;
182
 
183
  end process process2;
184
 
185
  mod_10_cnt : process(clk, rst_cnt)
186
  begin
187
    if rising_edge(clk) then
188
      if (rst_cnt = '1') then
189
        count <= 0;
190
      elsif(en_cnt = '1' and state = n_round_1) then
191
        if (count = 9) then
192
          count <= 0;
193
        else
194
          count <= count + 1;
195
        end if;
196
      end if;
197
     end if;
198
  end process mod_10_cnt;
199
 
200
  en_cnt <= '1';
201
 
202
  AES_ROUND_N : entity work.aes_enc(Behavioral) port map (clk,
203
                                                          rst,
204
                                                          block_in_s,
205
                                                          sub_key_s,
206
                                                          load_s,
207
                                                          enc_s,
208
                                                          last_s,
209
                                                          block_out_s);
210
 
211
  SUB_KEYS_DRAM : entity work.dual_mem(rtl) generic map (4, 128, 10)
212
                                            port map (clk,
213
                                                      '0',
214
                                                      key_addr_1,
215
                                                      key_addr_2,
216
                                                      (others => '0'),
217
                                                      key_data_1,
218
                                                      key_data_2);
219
 
220
 
221
 
222
  key_addr_1 <= std_logic_vector(to_unsigned(count, key_addr_1'length));
223
  key_addr_2 <= std_logic_vector(to_unsigned(count, key_addr_2'length));
224
 
225
 block_out <= block_out_s;
226
 
227
end Behavioral;
228
 

powered by: WebSVN 2.1.0

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