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

Subversion Repositories threeaesc

[/] [threeaesc/] [trunk/] [aes_c_1/] [src/] [aes_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 work.aes_lib.all;
23
 
24
entity aes_enc is
25
        port(     clk: in std_logic;
26
                  rst : in std_logic;
27
                  block_in : in std_logic_vector(127 downto 0);
28
                  sub_key : in std_logic_vector(127 downto 0);
29
                  load : in std_logic;
30
                  enc : in std_logic;
31
                  last : in std_logic;
32
 
33
                  block_out : out std_logic_vector(127 downto 0));
34
        end aes_enc;
35
 
36
architecture Behavioral of aes_enc is
37
        signal reg: std_logic_vector(127 downto 0);
38
        signal key_reg_delayed: std_logic_vector(127 downto 0);
39
 
40
        signal sub_tmp_0 : std_logic_vector(7 downto 0);
41
        signal sub_tmp_1 : std_logic_vector(7 downto 0);
42
        signal sub_tmp_2 : std_logic_vector(7 downto 0);
43
        signal sub_tmp_3 : std_logic_vector(7 downto 0);
44
 
45
        signal sub_tmp_mix_0 : std_logic_vector(7 downto 0);
46
        signal sub_tmp_mix_1 : std_logic_vector(7 downto 0);
47
        signal sub_tmp_mix_2 : std_logic_vector(7 downto 0);
48
        signal sub_tmp_mix_3 : std_logic_vector(7 downto 0);
49
 
50
        signal sub_tmp_key_0 : std_logic_vector(7 downto 0);
51
        signal sub_tmp_key_1 : std_logic_vector(7 downto 0);
52
        signal sub_tmp_key_2 : std_logic_vector(7 downto 0);
53
        signal sub_tmp_key_3 : std_logic_vector(7 downto 0);
54
 
55
        signal key_reg : std_logic_vector(127 downto 0);
56
begin
57
 
58
        S_BOX_DUAL_1: entity work.dual_mem(rtl) port map (clk, '0', reg(7 downto 0),   reg(15 downto 8), (others=>'0'), sub_tmp_0, sub_tmp_1);
59
        S_BOX_DUAL_2: entity work.dual_mem(rtl) port map (clk, '0', reg(23 downto 16), reg(31 downto 24), (others=>'0'), sub_tmp_2, sub_tmp_3);
60
 
61
 
62
        sum_proc_1: process(clk, rst, block_in, sub_key)
63
                variable reg_v : std_logic_vector(127 downto 0);
64
                variable key_reg_v : std_logic_vector(127 downto 0);
65
        begin
66
                if rising_edge(clk) then
67
                        if rst = '1' then
68
                                reg_v := (others=>'0');
69
                                key_reg_v := (others=>'0');
70
                        elsif load = '1' then
71
 
72
 
73
                                -- The current state is arranged to:
74
                                -- { 0,5,a,f; 4,9,e,3; 8,d,2,7; c,1,6,b; } as
75
                                -- Gaj & Chodowiec describe in "FPGA and ASIC Implementations of AES" from
76
                                -- Cryptographic Engineering, Çetin Kaya Koç, Springer, 2009.
77
 
78
                                reg_v := block_in(95 downto 88)   &   block_in(55 downto 48)   & block_in(15 downto 8)    & block_in(103 downto 96) & -- (b,6,1,c)
79
                                         block_in(63 downto 56)   &   block_in(23 downto 16)   & block_in(111 downto 104) & block_in(71 downto 64)  & -- (7,2,d,8)
80
                                         block_in(31 downto 24)   &   block_in(119 downto 112) & block_in(79 downto 72)   & block_in(39 downto 32)  & -- (3,e,9,4)
81
                                         block_in(127 downto 120) &   block_in(87 downto 80)   & block_in(47 downto 40)   & block_in(7 downto 0);     -- (f,a,5,0) 
82
 
83
                                key_reg_v := sub_key;
84
                        elsif enc = '1' then
85
                                reg_v := to_stdlogicvector(to_bitvector(reg_v) ror 32);
86
                                key_reg_v := to_stdlogicvector(to_bitvector(key_reg_v) ror 32);
87
                        end if;
88
                end if;
89
 
90
                reg <= reg_v;
91
                key_reg <= key_reg_v;
92
 
93
        end process;
94
 
95
        MIX_COL: process(sub_tmp_0, sub_tmp_1, sub_tmp_2, sub_tmp_3, last)
96
        begin
97
                if last = '0' then
98
                        sub_tmp_mix_0 <= gfmult2(sub_tmp_0) xor gfmult3(sub_tmp_1) xor sub_tmp_2 xor sub_tmp_3;
99
                        sub_tmp_mix_1 <= sub_tmp_0 xor gfmult2(sub_tmp_1) xor gfmult3(sub_tmp_2) xor sub_tmp_3;
100
                        sub_tmp_mix_2 <= sub_tmp_0 xor sub_tmp_1 xor gfmult2(sub_tmp_2) xor gfmult3(sub_tmp_3);
101
                        sub_tmp_mix_3 <= gfmult3(sub_tmp_0) xor sub_tmp_1 xor sub_tmp_2 xor gfmult2(sub_tmp_3);
102
                else
103
                        sub_tmp_mix_0 <= sub_tmp_0;
104
                        sub_tmp_mix_1 <= sub_tmp_1;
105
                        sub_tmp_mix_2 <= sub_tmp_2;
106
                        sub_tmp_mix_3 <= sub_tmp_3;
107
                end if;
108
        end process;
109
 
110
        ADD_KEY: process(key_reg_delayed, sub_tmp_mix_0, sub_tmp_mix_1, sub_tmp_mix_2, sub_tmp_mix_3)
111
        begin
112
                sub_tmp_key_0 <= sub_tmp_mix_0 xor key_reg_delayed(7 downto 0);
113
                sub_tmp_key_1 <= sub_tmp_mix_1 xor key_reg_delayed(15 downto 8);
114
                sub_tmp_key_2 <= sub_tmp_mix_2 xor key_reg_delayed(23 downto 16);
115
                sub_tmp_key_3 <= sub_tmp_mix_3 xor key_reg_delayed(31 downto 24);
116
        end process;
117
 
118
        FF_DELAY: process(clk, key_reg)
119
        begin
120
                if rising_edge(clk) then
121
                        key_reg_delayed <= key_reg;
122
                end if;
123
        end process;
124
 
125
        gen_output: process(enc, clk, sub_tmp_key_0, sub_tmp_key_1, sub_tmp_key_2, sub_tmp_key_3)
126
                variable out_buffer_v : std_logic_vector(127 downto 0);
127
        begin
128
                if rising_edge(clk) then
129
                        if enc = '1' then
130
                                out_buffer_v := out_buffer_v(127 downto 32) & sub_tmp_key_3 & sub_tmp_key_2 & sub_tmp_key_1 & sub_tmp_key_0;
131
                                out_buffer_v := to_stdlogicvector(to_bitvector(out_buffer_v) ror 32);
132
                        end if;
133
                end if;
134
 
135
                block_out <= out_buffer_v;
136
 
137
        end process;
138
 
139
end Behavioral;
140
 

powered by: WebSVN 2.1.0

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