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

Subversion Repositories threeaesc

[/] [threeaesc/] [trunk/] [aes_c_2/] [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, reinit, 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 :  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_div_2, rst_div, rst_cnt : std_logic;
53
 
54
  attribute buffer_type of clk_div_2: signal is "bufg";
55
 
56
begin
57
 
58
  process1: process (clk,rst)
59
  begin
60
    if (rst ='1') then
61
      state <= idle;
62
    elsif rising_edge(clk) then
63
      state <= next_state;
64
    end if;
65
  end process process1;
66
 
67
  process2 : process (state, enc, block_in, key)
68
  begin
69
    next_state <= state;
70
 
71
    last_s <= '0';
72
    block_in_s <= (others => '0');
73
    sub_key_s <= (others => '0');
74
    block_ready <= '0';
75
 
76
    rst_div <= '0';
77
    rst_cnt <= '0';
78
 
79
    en_cnt <= '0';
80
 
81
    case state is
82
          when idle =>
83
            if enc ='1' then
84
              rst_div <= '1';
85
              rst_cnt <= '1';
86
 
87
              next_state <= all_reset;
88
            else
89
              next_state <= idle;
90
            end if;
91
          when all_reset =>
92
            rst_div <= '0';
93
            rst_cnt <= '0';
94
 
95
            en_cnt <= '1';
96
 
97
            next_state <= pre;
98
          when pre =>
99
 
100
            en_cnt <= '1';
101
 
102
              sub_key_s <= key_data_1;
103
              block_in_s <= block_in xor key;
104
 
105
              next_state <= n_round_1;
106
          when n_round_1 =>
107
            en_cnt <= '1';
108
 
109
            sub_key_s <= key_data_1;
110
            last_s <= '0';
111
            block_in_s <= block_out_s;
112
 
113
            next_state <= n_round_2;
114
          when n_round_2 =>
115
            en_cnt <= '1';
116
 
117
            sub_key_s <= key_data_1;
118
            block_in_s <= block_out_s;
119
 
120
            if count = 9 then
121
              last_s <= '1';
122
              block_ready <= '1';
123
 
124
              next_state <= reinit;
125
            else
126
              last_s <= '0';
127
              next_state <= n_round_1;
128
            end if;
129
          when reinit =>
130
            en_cnt <= '0';
131
 
132
            next_state <= idle;
133
    end case;
134
 
135
  end process process2;
136
 
137
 
138
  mod_10_cnt : process(clk_div_2, rst_cnt)
139
  begin
140
    if rising_edge(clk_div_2) then
141
      if (rst_cnt = '1') then
142
        count <= 0;
143
      elsif(en_cnt = '1') then
144
        if (count = 9) then
145
          count <= 0;
146
        else
147
          count <= count + 1;
148
        end if;
149
      end if;
150
     end if;
151
  end process mod_10_cnt;
152
 
153
  key_addr_1 <= std_logic_vector(to_unsigned(count, key_addr_1'length));
154
  key_addr_2 <= std_logic_vector(to_unsigned(count, key_addr_2'length));
155
 
156
  AES_ROUND_N : entity work.aes_enc(Behavioral) port map (clk,
157
                                                          block_in_s,
158
                                                          sub_key_s,
159
                                                          last_s,
160
                                                          block_out_s);
161
 
162
  SUB_KEYS_DRAM : entity work.dual_mem(rtl) generic map (4, 128, 10)
163
                                            port map (clk,
164
                                                      '0',
165
                                                      key_addr_1,
166
                                                      key_addr_2,
167
                                                      (others => '0'),
168
                                                      key_data_1,
169
                                                      key_data_2);
170
  clk_div : process(clk, rst_div)
171
  begin
172
    if rising_edge(clk) then
173
      if rst_div = '1' then
174
        clk_div_2 <= '0';
175
      else
176
        clk_div_2 <= not(clk_div_2);
177
      end if;
178
    end if;
179
  end process;
180
 
181
  block_out <= block_out_s;
182
 
183
end Behavioral;
184
 

powered by: WebSVN 2.1.0

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