1 |
2 |
dsocek |
---------------------------------------------------------------------
|
2 |
|
|
-- (c) Copyright 2006, CoreTex Systems, LLC --
|
3 |
|
|
-- www.coretexsys.com --
|
4 |
|
|
-- --
|
5 |
|
|
-- This source file may be used and distributed without --
|
6 |
|
|
-- restriction provided that this copyright statement is not --
|
7 |
|
|
-- removed from the file and that any derivative work contains --
|
8 |
|
|
-- the original copyright notice and the associated disclaimer. --
|
9 |
|
|
-- --
|
10 |
|
|
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY --
|
11 |
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED --
|
12 |
|
|
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS --
|
13 |
|
|
-- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR --
|
14 |
|
|
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, --
|
15 |
|
|
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES --
|
16 |
|
|
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE --
|
17 |
|
|
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR --
|
18 |
|
|
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF --
|
19 |
|
|
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
|
20 |
|
|
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT --
|
21 |
|
|
-- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE --
|
22 |
|
|
-- POSSIBILITY OF SUCH DAMAGE. --
|
23 |
|
|
-- --
|
24 |
|
|
---------------------------------------------------------------------
|
25 |
|
|
|
26 |
|
|
----------------------------------------------------------------------
|
27 |
|
|
|
28 |
|
|
-- Poject structure:
|
29 |
|
|
|
30 |
|
|
-- |- tdes_top.vhd
|
31 |
|
|
-- |
|
32 |
|
|
-- |- des_cipher_top.vhd
|
33 |
|
|
-- |- des_top.vhd
|
34 |
|
|
-- |- block_top.vhd
|
35 |
|
|
-- |- add_key.vhd
|
36 |
|
|
-- |
|
37 |
|
|
-- |- add_left.vhd
|
38 |
|
|
-- |
|
39 |
|
|
-- |- e_expansion_function.vhd
|
40 |
|
|
-- |
|
41 |
|
|
-- |- p_box.vhd
|
42 |
|
|
-- |
|
43 |
|
|
-- |- s_box.vhd
|
44 |
|
|
-- |- s1_box.vhd
|
45 |
|
|
-- |- s2_box.vhd
|
46 |
|
|
-- |- s3_box.vhd
|
47 |
|
|
-- |- s4_box.vhd
|
48 |
|
|
-- |- s5_box.vhd
|
49 |
|
|
-- |- s6_box.vhd
|
50 |
|
|
-- |- s7_box.vhd
|
51 |
|
|
-- |- s8_box.vhd
|
52 |
|
|
-- |- key_schedule.vhd
|
53 |
|
|
|
54 |
|
|
----------------------------------------------------------------------
|
55 |
|
|
---------------------------------------------------------------------------------------------------
|
56 |
|
|
--
|
57 |
|
|
-- Title : des_top
|
58 |
|
|
-- Company : CoreTex Systems, LLC
|
59 |
|
|
--
|
60 |
|
|
---------------------------------------------------------------------------------------------------
|
61 |
|
|
|
62 |
|
|
library IEEE;
|
63 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
64 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
65 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
66 |
|
|
|
67 |
|
|
entity des_top is
|
68 |
|
|
port (
|
69 |
|
|
|
70 |
|
|
-- input/output core signals
|
71 |
|
|
key_round_in: in std_logic_vector(0 to 47);
|
72 |
|
|
data_in: in std_logic_vector(0 to 63);
|
73 |
|
|
data_out: out std_logic_vector(0 to 63);
|
74 |
|
|
|
75 |
|
|
-- signals for communication with key expander module
|
76 |
|
|
KeySelect: inout std_logic_vector(3 downto 0); -- selector for key
|
77 |
|
|
key_ready: in std_logic; -- active high when key is ready
|
78 |
|
|
data_ready: in std_logic; -- active high when data is ready
|
79 |
|
|
func_select: in std_logic; -- encryption/decryption flag
|
80 |
|
|
|
81 |
|
|
des_out_rdy: out std_logic; -- active high when decrypted/encrypted data are ready
|
82 |
|
|
core_busy: out std_logic; -- active high when core is in process of encryption
|
83 |
|
|
|
84 |
|
|
reset: in std_logic; -- master reset
|
85 |
|
|
clock: in std_logic -- master clock
|
86 |
|
|
);
|
87 |
|
|
end des_top;
|
88 |
|
|
|
89 |
|
|
architecture Behavioral of des_top is
|
90 |
|
|
|
91 |
|
|
--
|
92 |
|
|
-- BLOCK_TOP entity performs encryption/deccryption operation. It uses expaned key for that process
|
93 |
|
|
--
|
94 |
|
|
component block_top is
|
95 |
|
|
port(
|
96 |
|
|
L_in: in std_logic_vector(0 to 31); -- left permuted input
|
97 |
|
|
R_in: in std_logic_vector(0 to 31); -- right permuted input
|
98 |
|
|
|
99 |
|
|
L_out: out std_logic_vector(0 to 31); -- left permuted output
|
100 |
|
|
R_out: out std_logic_vector(0 to 31); -- right permuted output
|
101 |
|
|
|
102 |
|
|
round_key_des: in std_logic_vector(0 to 47) -- current round key
|
103 |
|
|
|
104 |
|
|
);
|
105 |
|
|
end component;
|
106 |
|
|
|
107 |
|
|
--
|
108 |
|
|
-- Internal DES_TOP signals
|
109 |
|
|
--
|
110 |
|
|
signal L_in_internal, R_in_internal: std_logic_vector(0 to 31);
|
111 |
|
|
signal L_out_internal, R_out_internal: std_logic_vector(0 to 31);
|
112 |
|
|
type statetype is (WaitKey, WaitData, InitialRound, RepeatRound, FinalRound);
|
113 |
|
|
signal nextstate: statetype;
|
114 |
|
|
signal RoundCounter: std_logic_vector(3 downto 0);
|
115 |
|
|
|
116 |
|
|
begin
|
117 |
|
|
|
118 |
|
|
--
|
119 |
|
|
-- Finite state machine
|
120 |
|
|
--
|
121 |
|
|
process (clock)
|
122 |
|
|
begin
|
123 |
|
|
if rising_edge(clock) then
|
124 |
|
|
if reset = '1' then
|
125 |
|
|
--
|
126 |
|
|
-- Reset all signal to inital values
|
127 |
|
|
--
|
128 |
|
|
nextstate <= WaitKey;
|
129 |
|
|
RoundCounter <= "0000";
|
130 |
|
|
core_busy <= '0'; -- core is in reset state: not busy
|
131 |
|
|
des_out_rdy <= '0'; -- output data is not ready
|
132 |
|
|
|
133 |
|
|
else
|
134 |
|
|
|
135 |
|
|
case nextstate is
|
136 |
|
|
|
137 |
|
|
--
|
138 |
|
|
-- WaitKey: wait for key to be expanded
|
139 |
|
|
--
|
140 |
|
|
when WaitKey =>
|
141 |
|
|
|
142 |
|
|
-- wait until key has been expanded
|
143 |
|
|
if key_ready = '0' then
|
144 |
|
|
nextstate <= WaitKey;
|
145 |
|
|
else
|
146 |
|
|
nextstate <= WaitData;
|
147 |
|
|
end if;
|
148 |
|
|
|
149 |
|
|
core_busy <= '0'; -- core waits for the key: not busy
|
150 |
|
|
des_out_rdy <= '0'; -- output data is not ready
|
151 |
|
|
|
152 |
|
|
--
|
153 |
|
|
-- WaitData: waits for data until it is ready
|
154 |
|
|
--
|
155 |
|
|
when WaitData =>
|
156 |
|
|
|
157 |
|
|
-- wait for data to be loaded in input registers
|
158 |
|
|
if (data_ready = '0') then
|
159 |
|
|
|
160 |
|
|
nextstate <= WaitData;
|
161 |
|
|
|
162 |
|
|
else
|
163 |
|
|
core_busy <= '1'; -- core is processing = busy
|
164 |
|
|
|
165 |
|
|
L_in_internal <= data_in(57) & data_in(49) & data_in(41) & data_in(33) & data_in(25) & data_in(17) &
|
166 |
|
|
data_in(9) & data_in(1) & data_in(59) & data_in(51) & data_in(43) & data_in(35) &
|
167 |
|
|
data_in(27) & data_in(19) & data_in(11) & data_in(3) & data_in(61) & data_in(53) &
|
168 |
|
|
data_in(45) & data_in(37) & data_in(29) & data_in(21) & data_in(13) & data_in(5) &
|
169 |
|
|
data_in(63) & data_in(55) & data_in(47) & data_in(39) & data_in(31) & data_in(23) &
|
170 |
|
|
data_in(15) & data_in(7);
|
171 |
|
|
|
172 |
|
|
R_in_internal <= data_in(56) & data_in(48) & data_in(40) & data_in(32) & data_in(24) & data_in(16) &
|
173 |
|
|
data_in(8) & data_in(0) & data_in(58) & data_in(50) & data_in(42) & data_in(34) &
|
174 |
|
|
data_in(26) & data_in(18) & data_in(10) & data_in(2) & data_in(60) & data_in(52) &
|
175 |
|
|
data_in(44) & data_in(36) & data_in(28) & data_in(20) & data_in(12) & data_in(4) &
|
176 |
|
|
data_in(62) & data_in(54) & data_in(46) & data_in(38) & data_in(30) & data_in(22) &
|
177 |
|
|
data_in(14) & data_in(6);
|
178 |
|
|
|
179 |
|
|
nextstate <= InitialRound;
|
180 |
|
|
|
181 |
|
|
-- function select (decrypting/encrypting) will determine key selection
|
182 |
|
|
if func_select = '1' then
|
183 |
|
|
KeySelect <= "0000";
|
184 |
|
|
else
|
185 |
|
|
KeySelect <= "1111";
|
186 |
|
|
end if;
|
187 |
|
|
|
188 |
|
|
end if;
|
189 |
|
|
|
190 |
|
|
--
|
191 |
|
|
-- Initial State where input is equal to a block that we need to encode
|
192 |
|
|
--
|
193 |
|
|
when InitialRound =>
|
194 |
|
|
|
195 |
|
|
L_in_internal <= L_out_internal;
|
196 |
|
|
R_in_internal <= R_out_internal;
|
197 |
|
|
|
198 |
|
|
-- fuction select determines direction of key selection
|
199 |
|
|
if func_select = '1' then
|
200 |
|
|
KeySelect <= KeySelect + '1';
|
201 |
|
|
else
|
202 |
|
|
KeySelect <= KeySelect - '1';
|
203 |
|
|
end if;
|
204 |
|
|
|
205 |
|
|
nextstate <= RepeatRound;
|
206 |
|
|
|
207 |
|
|
--
|
208 |
|
|
-- Repeat Section, where input is output from prevous state
|
209 |
|
|
--
|
210 |
|
|
when RepeatRound =>
|
211 |
|
|
|
212 |
|
|
L_in_internal <= L_out_internal;
|
213 |
|
|
R_in_internal <= R_out_internal;
|
214 |
|
|
|
215 |
|
|
-- fuction select determines direction of key selection
|
216 |
|
|
if func_select = '1' then
|
217 |
|
|
KeySelect <= KeySelect + '1';
|
218 |
|
|
else
|
219 |
|
|
KeySelect <= KeySelect - '1';
|
220 |
|
|
end if;
|
221 |
|
|
|
222 |
|
|
RoundCounter <= RoundCounter + '1';
|
223 |
|
|
|
224 |
|
|
-- if finished with all rounds, go to the final round
|
225 |
|
|
if RoundCounter = x"E" then
|
226 |
|
|
|
227 |
|
|
-- perform inverse initial permutation
|
228 |
|
|
data_out <= L_out_internal(7) & R_out_internal(7) & L_out_internal(15) & R_out_internal(15) &
|
229 |
|
|
L_out_internal(23) & R_out_internal(23) & L_out_internal(31) & R_out_internal(31) &
|
230 |
|
|
L_out_internal(6) & R_out_internal(6) & L_out_internal(14) & R_out_internal(14) &
|
231 |
|
|
L_out_internal(22) & R_out_internal(22) & L_out_internal(30) & R_out_internal(30) &
|
232 |
|
|
L_out_internal(5) & R_out_internal(5) & L_out_internal(13) & R_out_internal(13) &
|
233 |
|
|
L_out_internal(21) & R_out_internal(21) & L_out_internal(29) & R_out_internal(29) &
|
234 |
|
|
L_out_internal(4) & R_out_internal(4) & L_out_internal(12) & R_out_internal(12) &
|
235 |
|
|
L_out_internal(20) & R_out_internal(20) & L_out_internal(28) & R_out_internal(28) &
|
236 |
|
|
L_out_internal(3) & R_out_internal(3) & L_out_internal(11) & R_out_internal(11) &
|
237 |
|
|
L_out_internal(19) & R_out_internal(19) & L_out_internal(27) & R_out_internal(27) &
|
238 |
|
|
L_out_internal(2) & R_out_internal(2) & L_out_internal(10) & R_out_internal(10) &
|
239 |
|
|
L_out_internal(18) & R_out_internal(18) & L_out_internal(26) & R_out_internal(26) &
|
240 |
|
|
L_out_internal(1) & R_out_internal(1) & L_out_internal(9) & R_out_internal(9) &
|
241 |
|
|
L_out_internal(17) & R_out_internal(17) & L_out_internal(25) & R_out_internal(25) &
|
242 |
|
|
L_out_internal(0) & R_out_internal(0) & L_out_internal(8) & R_out_internal(8) &
|
243 |
|
|
L_out_internal(16) & R_out_internal(16) & L_out_internal(24) & R_out_internal(24);
|
244 |
|
|
|
245 |
|
|
core_busy <= '0'; -- core is not busy
|
246 |
|
|
des_out_rdy <= '1'; -- output data is ready
|
247 |
|
|
nextstate <= FinalRound;
|
248 |
|
|
|
249 |
|
|
else
|
250 |
|
|
|
251 |
|
|
-- Continue with regular rounds
|
252 |
|
|
nextstate <= RepeatRound;
|
253 |
|
|
|
254 |
|
|
end if;
|
255 |
|
|
|
256 |
|
|
--
|
257 |
|
|
-- Last round
|
258 |
|
|
--
|
259 |
|
|
when FinalRound =>
|
260 |
|
|
|
261 |
|
|
RoundCounter <= "0000";
|
262 |
|
|
nextstate <= WaitKey;
|
263 |
|
|
des_out_rdy <= '0'; -- deselect out data ready signal
|
264 |
|
|
|
265 |
|
|
when others =>
|
266 |
|
|
-- should never happen
|
267 |
|
|
end case;
|
268 |
|
|
end if;
|
269 |
|
|
end if;
|
270 |
|
|
end process;
|
271 |
|
|
|
272 |
|
|
--
|
273 |
|
|
-- Instantations
|
274 |
|
|
--
|
275 |
|
|
BLOCKTOP: block_top
|
276 |
|
|
port map (
|
277 |
|
|
L_in => L_in_internal,
|
278 |
|
|
R_in => R_in_internal,
|
279 |
|
|
|
280 |
|
|
round_key_des => key_round_in,
|
281 |
|
|
|
282 |
|
|
L_out => L_out_internal,
|
283 |
|
|
R_out => R_out_internal
|
284 |
|
|
|
285 |
|
|
);
|
286 |
|
|
|
287 |
|
|
end Behavioral;
|