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

Subversion Repositories threeaesc

[/] [threeaesc/] [trunk/] [aes_c_3/] [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
use ieee.numeric_std.all;
22
 
23
use work.aes_lib.all;
24
 
25
entity aes_fsm_enc is
26
        port(     clk: in std_logic;
27
                  rst : in std_logic;
28
                  block_in : in std_logic_vector(127 downto 0);
29
                  key : in std_logic_vector(127 downto 0);
30
                  enc : in std_logic;
31
                  block_out : out std_logic_vector(127 downto 0);
32
                  block_ready : out std_logic);
33
        end aes_fsm_enc;
34
 
35
architecture Behavioral of aes_fsm_enc is
36
 
37
  attribute buffer_type: string;
38
 
39
  type state_type is (idle, n_round_1, n_round_2, n_round_3, reinit, reinit2, pre, all_reset);
40
 
41
  signal state, next_state: state_type ;
42
  signal block_in_s :  std_logic_vector(127 downto 0);
43
  signal sub_key_s :  std_logic_vector(127 downto 0);
44
  signal last_s :  std_logic;
45
  signal block_out_s, tmp :  std_logic_vector(127 downto 0);
46
 
47
  signal key_addr_1, key_addr_2 : std_logic_vector(3 downto 0);
48
  signal key_data_1, key_data_delay_1, key_data_2, key_data_delay_2 : std_logic_vector(127 downto 0);
49
 
50
  signal count: natural range 0 to 10;
51
  signal en_cnt : std_logic;
52
  signal clk_3, clk_tmp : std_logic;
53
 
54
  signal pos_cnt :std_logic_vector (1 downto 0);
55
  signal neg_cnt :std_logic_vector (1 downto 0);
56
 
57
  signal rst_div, rst_cnt : std_logic;
58
 
59
  attribute buffer_type of clk_3: signal is "bufg";
60
 
61
begin
62
 
63
  process1: process (clk,rst)
64
  begin
65
    if (rst ='1') then
66
      state <= idle;
67
    elsif rising_edge(clk) then
68
      state <= next_state;
69
    end if;
70
  end process process1;
71
 
72
  process2 : process (state, enc, block_in, key)
73
  begin
74
    next_state <= state;
75
 
76
    last_s <= '0';
77
    block_in_s <= (others => '0');
78
    sub_key_s <= (others => '0');
79
    block_ready <= '0';
80
    en_cnt <= '0';
81
 
82
    rst_div <= '0';
83
    rst_cnt <= '0';
84
 
85
    case state is
86
          when idle =>
87
            if enc ='1' then
88
              next_state <= all_reset;
89
            else
90
              en_cnt <= '0';
91
              next_state <= idle;
92
            end if;
93
          when all_reset =>
94
            rst_div <= '1';
95
            rst_cnt <= '1';
96
 
97
            next_state <= pre;
98
          when pre =>
99
              rst_cnt <= '0';
100
              rst_div <= '0';
101
 
102
              sub_key_s <= key_data_1;
103
              block_in_s <= block_in xor key;
104
              en_cnt <= '1';
105
 
106
              next_state <= n_round_1;
107
          when n_round_1 =>
108
            en_cnt <= '1';
109
            block_in_s <= tmp;
110
            sub_key_s <= key_data_1;
111
            next_state <= n_round_2;
112
          when n_round_2 =>
113
            en_cnt <= '1';
114
            sub_key_s <= key_data_1;
115
            block_in_s <= tmp;
116
            next_state <= n_round_3;
117
          when n_round_3 =>
118
            en_cnt <= '1';
119
            sub_key_s <= key_data_1;
120
 
121
            block_in_s <= tmp;
122
 
123
              if count = 9 then
124
                last_s <= '1';
125
                block_ready <= '1';
126
                sub_key_s <= key_data_1;
127
                block_in_s <= tmp;
128
                next_state <= reinit;
129
            else
130
              next_state <= n_round_1;
131
            end if;
132
          when reinit =>
133
            en_cnt <= '1';
134
            next_state <= idle;
135
          when reinit2 =>
136
            en_cnt <= '1';
137
            next_state <= idle;
138
          end case;
139
 
140
  end process process2;
141
 
142
  get_output : process(clk, state)
143
  begin
144
    if rising_edge(clk) then
145
      if state = n_round_1 then
146
        tmp <= block_out_s;
147
      end if;
148
    end if;
149
  end process;
150
 
151
  mod_10_cnt : process(clk_3, rst_cnt)
152
  begin
153
    if rising_edge(clk_3) then
154
      if (rst_cnt = '1') then
155
        count <= 0;
156
      elsif(en_cnt = '1') then
157
        if (count = 9) then
158
          count <= 0;
159
        else
160
          count <= count + 1;
161
        end if;
162
      end if;
163
     end if;
164
  end process mod_10_cnt;
165
 
166
  key_addr_1 <= std_logic_vector(to_unsigned(count, key_addr_1'length));
167
  key_addr_2 <= std_logic_vector(to_unsigned(count, key_addr_2'length));
168
 
169
  AES_ROUND_N : entity work.aes_enc(Behavioral) port map (clk,
170
                                                          block_in_s,
171
                                                          sub_key_s,
172
                                                          last_s,
173
                                                          block_out_s);
174
 
175
  SUB_KEYS_DRAM : entity work.dual_mem(rtl) generic map (4, 128, 10)
176
                                            port map (clk,
177
                                                      '0',
178
                                                      key_addr_1,
179
                                                      key_addr_2,
180
                                                      (others => '0'),
181
                                                      key_data_1,
182
                                                      key_data_2);
183
 
184
  block_out <= block_out_s;
185
 
186
      div_3_p_1: process (clk, rst_div) begin
187
          if (rst_div = '1') then
188
              pos_cnt <= (others=>'0');
189
          elsif (rising_edge(clk)) then
190
              pos_cnt <= pos_cnt + 1;
191
              if (pos_cnt = 2) then
192
                  pos_cnt <= (others => '0');
193
              end if;
194
         end if;
195
      end process;
196
 
197
      div_3_p_2: process (clk, rst_div) begin
198
          if (rst_div = '1') then
199
              neg_cnt <= (others=>'0');
200
          elsif (falling_edge(clk)) then
201
              neg_cnt <= neg_cnt + 1;
202
              if (neg_cnt = 2) then
203
                  neg_cnt <= (others => '0');
204
              end if;
205
          end if;
206
      end process;
207
 
208
      block_out <= block_out_s;
209
 
210
 
211
      clk_3 <= '0' when ((pos_cnt /= 2) and (neg_cnt /= 2)) else
212
              '1';
213
 
214
end Behavioral;
215
 

powered by: WebSVN 2.1.0

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