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 |
|
|
-- Statemachine controlling the decryption datapath within aes_core.vhd does no
|
7 |
|
|
-- dataprocessing itself but only set enables and multiplexer selector ports
|
8 |
|
|
--
|
9 |
|
|
-- Author(s):
|
10 |
|
|
-- Thomas Ruschival -- ruschi@opencores.org (www.ruschival.de)
|
11 |
|
|
--
|
12 |
|
|
--------------------------------------------------------------------------------
|
13 |
|
|
-- Copyright (c) 2009, Thomas Ruschival
|
14 |
|
|
-- All rights reserved.
|
15 |
|
|
--
|
16 |
|
|
-- Redistribution and use in source and binary forms, with or without modification,
|
17 |
|
|
-- are permitted provided that the following conditions are met:
|
18 |
|
|
-- * Redistributions of source code must retain the above copyright notice,
|
19 |
|
|
-- this list of conditions and the following disclaimer.
|
20 |
|
|
-- * Redistributions in binary form must reproduce the above copyright notice,
|
21 |
|
|
-- this list of conditions and the following disclaimer in the documentation
|
22 |
|
|
-- and/or other materials provided with the distribution.
|
23 |
|
|
-- * Neither the name of the nor the names of its contributors
|
24 |
|
|
-- may be used to endorse or promote products derived from this software without
|
25 |
|
|
-- specific prior written permission.
|
26 |
|
|
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
27 |
|
|
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
28 |
|
|
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
29 |
|
|
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
30 |
|
|
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
31 |
|
|
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
32 |
|
|
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
33 |
|
|
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
34 |
|
|
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
35 |
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
36 |
|
|
-- THE POSSIBILITY OF SUCH DAMAGE
|
37 |
|
|
-------------------------------------------------------------------------------
|
38 |
|
|
-- version management:
|
39 |
20 |
ruschi |
-- $Author:: $
|
40 |
|
|
-- $Date:: $
|
41 |
|
|
-- $Revision:: $
|
42 |
2 |
ruschi |
-------------------------------------------------------------------------------
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
library ieee;
|
46 |
|
|
use ieee.std_logic_1164.all;
|
47 |
|
|
use ieee.numeric_std.all;
|
48 |
|
|
|
49 |
11 |
ruschi |
library avs_aes_lib;
|
50 |
|
|
use avs_aes_lib.avs_aes_pkg.all;
|
51 |
2 |
ruschi |
|
52 |
|
|
entity aes_fsm_decrypt is
|
53 |
|
|
generic (
|
54 |
|
|
NO_ROUNDS : NATURAL := 10); -- number of rounds
|
55 |
|
|
port (
|
56 |
|
|
clk : in STD_LOGIC; -- System clock
|
57 |
|
|
data_stable : in STD_LOGIC; -- flag valid data/activate the process
|
58 |
|
|
-- interface for keygenerator
|
59 |
|
|
key_ready : in STD_LOGIC; -- flag valid roundkeys
|
60 |
|
|
round_index_out : out NIBBLE; -- address for roundkeys memory
|
61 |
|
|
-- Result of Process
|
62 |
|
|
finished : out STD_LOGIC; -- flag valid result
|
63 |
|
|
-- Control ports for the Core
|
64 |
|
|
round_type_sel : out STD_LOGIC_VECTOR(1 downto 0) -- selector for mux around mixcols
|
65 |
|
|
);
|
66 |
|
|
end entity aes_fsm_decrypt;
|
67 |
|
|
|
68 |
|
|
--
|
69 |
|
|
architecture Arch1 of AES_FSM_DECRYPT is
|
70 |
|
|
-- types for the FSM
|
71 |
|
|
type AESstates is (WAIT_KEY, WAIT_DATA, INITIAL_ROUND,DO_ROUND, FINAL_ROUND);
|
72 |
|
|
|
73 |
|
|
-- FSM signals
|
74 |
|
|
signal FSM : AESstates; -- current state
|
75 |
|
|
signal next_FSM : AESstates; -- combinational next state
|
76 |
|
|
|
77 |
|
|
-- Round Counter & address for keygenerate
|
78 |
|
|
signal round_index : NIBBLE; -- currently processed round
|
79 |
|
|
signal next_round_index : NIBBLE; -- next round, index for keygenerate
|
80 |
|
|
|
81 |
|
|
begin
|
82 |
|
|
---------------------------------------------------------------------------
|
83 |
|
|
-- assign internal values to interface ports
|
84 |
|
|
---------------------------------------------------------------------------
|
85 |
|
|
round_index_out <= next_round_index; -- roundkey address
|
86 |
|
|
|
87 |
|
|
-- purpose: combinational generation of next state for encrytion FSM
|
88 |
|
|
-- type : sequential
|
89 |
|
|
-- inputs : FSM, data_stable, key_ready, round_index
|
90 |
|
|
-- outputs: next_FSM
|
91 |
|
|
gen_next_fsm : process (FSM, data_stable, key_ready, round_index) is
|
92 |
|
|
begin -- process gen_next_fsm
|
93 |
|
|
case FSM is
|
94 |
|
|
when WAIT_KEY =>
|
95 |
|
|
if key_ready = '1' then
|
96 |
|
|
next_FSM <= WAIT_DATA;
|
97 |
|
|
else
|
98 |
|
|
next_FSM <= WAIT_KEY;
|
99 |
|
|
end if;
|
100 |
|
|
when WAIT_DATA =>
|
101 |
|
|
if data_stable = '1' then
|
102 |
|
|
next_FSM <= INITIAL_ROUND;
|
103 |
|
|
else
|
104 |
|
|
next_FSM <= WAIT_DATA;
|
105 |
|
|
end if;
|
106 |
|
|
when INITIAL_ROUND =>
|
107 |
|
|
next_FSM <= DO_ROUND;
|
108 |
|
|
when DO_ROUND =>
|
109 |
|
|
if round_index = X"1" then
|
110 |
|
|
next_FSM <= FINAL_ROUND;
|
111 |
|
|
else
|
112 |
|
|
next_FSM <= DO_ROUND;
|
113 |
|
|
end if;
|
114 |
|
|
when FINAL_ROUND =>
|
115 |
|
|
next_FSM <= WAIT_DATA;
|
116 |
|
|
-- pragma synthesis_off
|
117 |
|
|
when others =>
|
118 |
|
|
report "FSM in strange state - aborting" severity failure;
|
119 |
|
|
-- pragma synthesis_on
|
120 |
|
|
end case;
|
121 |
|
|
|
122 |
|
|
-- Default behaviour in case key is invalid
|
123 |
|
|
if key_ready = '0' then
|
124 |
|
|
next_FSM <= WAIT_KEY;
|
125 |
|
|
end if;
|
126 |
|
|
|
127 |
|
|
end process gen_next_fsm;
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
-- purpose: assign outputs for decryption
|
131 |
|
|
-- type : combinational
|
132 |
|
|
-- inputs : FSM
|
133 |
|
|
com_output_assign : process (FSM, round_index) is
|
134 |
|
|
begin -- process com_output_assign
|
135 |
|
|
-- save defaults for decrypt_FSM
|
136 |
|
|
round_type_sel <= "00"; -- signal initial_round
|
137 |
|
|
next_round_index <= round_index;
|
138 |
|
|
finished <= '0';
|
139 |
|
|
|
140 |
|
|
case FSM is
|
141 |
|
|
when WAIT_KEY =>
|
142 |
|
|
-- start at last index
|
143 |
|
|
next_round_index <= STD_LOGIC_VECTOR(to_unsigned(NO_ROUNDS,4));
|
144 |
|
|
when WAIT_DATA =>
|
145 |
|
|
next_round_index <= STD_LOGIC_VECTOR(to_unsigned(NO_ROUNDS,4));
|
146 |
|
|
when INITIAL_ROUND =>
|
147 |
|
|
round_type_sel <= "00"; -- use Data_in for Addkey and pass
|
148 |
|
|
-- result directly to Inverse Shiftrow
|
149 |
|
|
next_round_index <= STD_LOGIC_VECTOR(UNSIGNED(round_index)-1);
|
150 |
|
|
when DO_ROUND =>
|
151 |
|
|
round_type_sel <= "01";
|
152 |
|
|
next_round_index <= STD_LOGIC_VECTOR(UNSIGNED(round_index)-1);
|
153 |
|
|
when FINAL_ROUND =>
|
154 |
|
|
round_type_sel <= "01";
|
155 |
|
|
finished <= '1';
|
156 |
|
|
when others =>
|
157 |
|
|
null;
|
158 |
|
|
end case;
|
159 |
|
|
end process com_output_assign;
|
160 |
|
|
|
161 |
|
|
-- purpose: clocked FSM for decryption
|
162 |
|
|
-- type : sequential
|
163 |
|
|
-- inputs : clk, res_n
|
164 |
|
|
clocked_FSM : process (clk) is
|
165 |
|
|
begin -- process clocked_FSM
|
166 |
|
|
if rising_edge(clk) then -- rising clock edge
|
167 |
|
|
FSM <= next_FSM;
|
168 |
|
|
round_index <= next_round_index;
|
169 |
|
|
end if;
|
170 |
|
|
end process clocked_FSM;
|
171 |
|
|
|
172 |
|
|
end architecture Arch1;
|