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

Subversion Repositories avs_aes

[/] [avs_aes/] [trunk/] [rtl/] [VHDL/] [avs_aes.vhd] - Blame information for rev 20

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ruschi
--------------------------------------------------------------------------------
2 10 ruschi
-- This file is part of the project      avs_aes
3
-- see: http://opencores.org/project,avs_aes
4 2 ruschi
--
5
-- description:
6
--      Avalon Slave bus interface for aes_core. Top level component to integrate
7
--      into SoC
8
--      
9
-- Memory address offsets:
10
-- 0 - 7        key
11
-- 8 - 11       input data if write='1', result of operation if read='1'
12
-- 12- 14       result of operation
13
-- 31           command word
14
--
15
-- Command word bit offsets meanings:
16
-- Byte 3-1 reserved
17
--
18
-- Byte 0:
19
-- Bit 7  key valid --> run key expansion
20
-- Bit 6  interrupt enabled
21
-- Bit 5-2 reserved
22
-- Bit 1  input data valid interpret as cypher text       --> run decrypt mode
23
-- Bit 0  input data valid interpret as clear text       --> run encrypt mode
24
--
25
-- All other bits are regarded as "reserved". The bits in one byte of the
26
-- command word are mutually exclusive. the behavior of the core is not
27
-- specified if more than one bit is set.
28
--
29
-- Author(s):
30
--         Thomas Ruschival -- ruschi@opencores.org (www.ruschival.de)
31
--
32
--------------------------------------------------------------------------------
33
-- Copyright (c) 2009, Authors and opencores.org
34
-- All rights reserved.
35
--
36
-- Redistribution and use in source and binary forms, with or without modification,
37
-- are permitted provided that the following conditions are met:
38
--        * Redistributions of source code must retain the above copyright notice,
39
--        this list of conditions and the following disclaimer.
40
--        * Redistributions in binary form must reproduce the above copyright notice,
41
--        this list of conditions and the following disclaimer in the documentation
42
--        and/or other materials provided with the distribution.
43
--        * Neither the name of the organization nor the names of its contributors
44
--        may be used to endorse or promote products derived from this software without
45
--        specific prior written permission.
46
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
50
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
51
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56
-- THE POSSIBILITY OF SUCH DAMAGE
57
-------------------------------------------------------------------------------
58
-- version management:
59 20 ruschi
-- $Author::                                         $
60
-- $Date::                                           $
61
-- $Revision::                                       $
62 2 ruschi
-------------------------------------------------------------------------------
63
 
64
 
65
 
66
library ieee;
67
use ieee.std_logic_1164.all;
68
use ieee.numeric_std.all;
69
 
70 11 ruschi
library avs_aes_lib;
71
use avs_aes_lib.avs_aes_pkg.all;
72 2 ruschi
 
73
entity avs_AES is
74
        generic (
75
                KEYLENGTH  : NATURAL := 256;    -- AES key length
76
                DECRYPTION : BOOLEAN := true);  -- With decrypt or encrypt only
77
        port (
78
                -- Avalon global
79
                clk                                : in  STD_LOGIC;      -- avalon bus clock
80
                reset                      : in  STD_LOGIC;      -- avalon bus reset
81
                -- Interface specific
82
                avs_s1_chipselect  : in  STD_LOGIC;      -- enable component
83
                avs_s1_writedata   : in  STD_LOGIC_VECTOR(31 downto 0);   -- data write port
84
                avs_s1_address     : in  STD_LOGIC_VECTOR(4 downto 0);   -- slave address space offset
85
                avs_s1_write       : in  STD_LOGIC;      -- write enable
86
                avs_s1_read                : in  STD_LOGIC;      -- read request form avalon
87
                avs_s1_irq                 : out STD_LOGIC;      -- interrupt to signal completion
88
                avs_s1_waitrequest : out STD_LOGIC;      -- slave not ready, request master
89
                                                                                         -- to retry later
90
                avs_s1_readdata    : out STD_LOGIC_VECTOR(31 downto 0)   -- result read port
91
                );
92
end entity avs_AES;
93
 
94
architecture arch1 of avs_aes is
95
-- Signals interfacing the AES core
96
        signal data_stable : STD_LOGIC;  -- input data is valid --> process it
97
        signal data_in     : STATE;                     -- register for input data of core
98
 
99
        signal w_ena_keyword : STD_LOGIC;       -- write enable of keyword to wordaddr
100
        signal key_stable        : STD_LOGIC;  -- key is complete and valid, start expansion
101
 
102
        signal decrypt_mode : STD_LOGIC;        -- decrypt='1',encrypt='0'
103
        signal result           : STATE;                -- output
104
        signal finished         : STD_LOGIC;    -- output valid
105
 
106
-- internal logic
107
        signal result_reg : STATE;                      -- register for result
108
        signal ctrl_reg   : DWORD;                      -- control register
109
        signal irq                : STD_LOGIC;  -- internal interrupt request (register)
110
        signal irq_i      : STD_LOGIC;          -- combinational value for interrupt
111
 
112
        signal irq_ena : STD_LOGIC;                     -- alias for ctrl_reg(6)
113
 
114
        signal w_ena_data_in  : STD_LOGIC;      -- write enable of data_in register
115
        signal w_ena_ctrl_reg : STD_LOGIC;      -- write enable of control register
116
        signal keyexp_done        : STD_LOGIC;  -- signal to create waitrequests if new key is written while previous is still in
117
                                                                                -- expansion 
118
begin  -- architecture arch1
119
 
120
        -- map internal irq to avalon interface
121
        avs_s1_irq <= irq;
122
 
123
        -- rename signals for better debugging, will be optimized away in synthesis
124
        key_stable <= ctrl_reg(7);
125
        irq_ena    <= ctrl_reg(6);
126
 
127
        ---------------------------------------------------------------------------
128
        -- depending on generic enable decrypt_mode signal or permanently disable
129
        -- it 
130
        ---------------------------------------------------------------------------
131
        enable_decrypt_mode : if DECRYPTION generate
132
                decrypt_mode <= ctrl_reg(1);
133
                data_stable      <= ctrl_reg(0) or ctrl_reg(1);
134
        end generate enable_decrypt_mode;
135
 
136
        disable_decrypt_mode : if not DECRYPTION generate
137
                decrypt_mode <= '0';
138
                data_stable      <= ctrl_reg(0);
139
        end generate disable_decrypt_mode;
140
 
141
 
142
        -- purpose: write input data to registers
143
        -- type   : sequential
144
        -- inputs : clk
145
        -- outputs: ctrl_reg, irq, avs_s1_readdata, key, data
146
        write_inputs : process (clk) is
147
        begin  -- process write_inputs
148
                if rising_edge(clk) then
149
                        -- synchronous reset
150
                        if reset = '1' then
151
                                irq              <= '0';
152
                                ctrl_reg <= (others => '0');
153
                        end if;
154
                        -- DFF for IRQ
155
                        irq <= irq_i;
156
                        -- write control register
157
                        if w_ena_ctrl_reg = '1' then
158
                                ctrl_reg <= avs_s1_writedata;
159
                        end if;
160
                        -- write input to data register
161
                        if w_ena_data_in = '1' then
162
                                data_in(to_integer(UNSIGNED(avs_s1_address(1 downto 0)))) <= avs_s1_writedata;
163
                        end if;
164
 
165
                        -- signalling the outside world about the terminiation of the
166
                        -- computation by blanking the data_stable register bits
167
                        if finished = '1' then
168
                                -- Work is done - reset ENC and DEC
169
                                ctrl_reg(1 downto 0) <= "00";
170
                        end if;
171
                end if;
172
        end process write_inputs;
173
 
174
 
175
 
176
        -- purpose: set/reset interrupt request flag
177
        -- type   : combinational
178
        -- inputs : finished, avs_s1_read, irq, irq_ena
179
        -- outputs: irq_i
180
        IRQhandling : process (avs_s1_read, finished, irq, irq_ena) is
181
        begin  -- process IRQhandling
182
 
183
                -- Set the interrupt if enabed and process finished
184
                if irq_ena = '1' and finished = '1' then
185
                        irq_i <= '1';
186 4 ruschi
                elsif irq_ena = '0' or avs_s1_read = '1' then
187
                        -- any read operation resets the interrupt
188
                        irq_i <= '0';
189 2 ruschi
                else
190
                        irq_i <= irq;                           -- just keep the way it is 
191
                end if;
192
 
193
        end process IRQhandling;
194
 
195
 
196
        -- purpose: decode the write operation to the address ranges and map it to the
197
        -- registers - any other write address range
198
        -- e.g. avs_s1_address(4 downto 3) = "10" (result)      is illegal
199
        -- type   : combinational
200
        decode_write : process (avs_s1_address, avs_s1_write, key_stable,
201
                                                        keyexp_done) is
202
        begin
203
                -- safe default to avoid latching
204
                w_ena_data_in      <= '0';
205
                w_ena_ctrl_reg     <= '0';
206
                w_ena_keyword      <= '0';
207
                avs_s1_waitrequest <= '0';
208
                -- only do something if chipselect is asserted and write operation
209
                -- requested 
210
                if avs_s1_write = '1' then
211
                        if avs_s1_address(4 downto 3) = "00" then
212
                                -- write of keywords    
213
                                w_ena_keyword      <= '1';
214
                                -- stall the write process if old key is still in processing
215
                                -- the user can interrupt the expansion by deasserting key_stable
216
                                avs_s1_waitrequest <= key_stable and not keyexp_done;
217
                        elsif avs_s1_address(4 downto 3) = "01" then
218
                                -- write of data
219
                                w_ena_data_in <= '1';
220
                        elsif avs_s1_address(4 downto 3) = "11" then
221
                                -- write of control register
222
                                w_ena_ctrl_reg <= '1';
223
                        end if;
224
                end if;
225
        end process decode_write;
226
 
227
 
228
        -- purpose: assign read data
229
        -- type   : 
230
        -- inputs : 
231
        -- outputs: read_data
232
        decode_read : process (avs_s1_address, avs_s1_read, ctrl_reg, result_reg) is
233
        begin
234
                -- only address 0x10 to 0x1F are for read, thus address bit 4
235
                -- is always set when reading                   
236
                if avs_s1_read = '1' and avs_s1_address(3) = '0' then
237
                        -- address looks something like 10xxx which corresponds to
238
                        -- 0x10 to 0x17, however result has only 4 words thus
239
                        -- address bits 1 and 0 are sufficient for decoding
240
                        avs_s1_readdata <= result_reg(to_integer(UNSIGNED(avs_s1_address(1 downto 0))));
241
                else
242
                        -- address looks something like 11xxx which corresponds to
243
                        -- 0x18 to 0x1F, in this case always map control register,
244
                        -- we have plenty of address space so currently no exact
245
                        -- addressation needed.
246
                        -- save default, if nothing else is addressed show control register
247
                        avs_s1_readdata <= ctrl_reg;
248
                end if;
249
        end process decode_read;
250
 
251
 
252
 
253
        -- purpose: store the combinational output of the AES core to a register
254
        -- type   : sequential
255
        -- inputs : clk, res_n
256
        -- outputs: result
257
        store_result : process (clk) is
258
        begin  -- process store_result
259
                if rising_edge(clk) then                -- rising clock edge
260
                        -- Core has terminated, store the result and reset the 
261
                        if finished = '1' then
262
                                result_reg <= result;
263
                        end if;
264
                end if;
265
        end process store_result;
266
 
267
 
268
        ---------------------------------------------------------------------------
269
        -- Instance of the core
270
        ---------------------------------------------------------------------------
271
        AES_CORE_1 : AES_CORE
272
                generic map (
273
                        KEYLENGTH  => KEYLENGTH,  -- Size of keyblock (128, 192, 256 Bits)
274
                        DECRYPTION => DECRYPTION)       -- include decrypt datapath
275
                port map (
276
                        clk                       => clk,               -- system clock
277
                        data_in           => data_in,   -- payload to encrypt
278
                        data_stable       => data_stable,        -- flag valid payload
279
                        keyword           => avs_s1_writedata,  -- word of original userkey
280
                        keywordaddr       => avs_s1_address(2 downto 0),  -- keyword register address
281
                        w_ena_keyword => w_ena_keyword,  -- write enable of keyword to wordaddr
282
                        key_stable        => key_stable,  -- key is complete and valid, start expansion
283
                        decrypt_mode  => decrypt_mode,   -- decrypt='1',encrypt='0'
284
                        keyexp_done       => keyexp_done,        -- key is completely expanded
285
                        result            => result,    -- output
286
                        finished          => finished);   -- output valid
287
 
288
end architecture arch1;

powered by: WebSVN 2.1.0

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