1 |
4 |
gajos |
-----------------------------------------------------------------------
|
2 |
|
|
---- ----
|
3 |
|
|
---- Present - a lightweight block cipher project ----
|
4 |
|
|
---- ----
|
5 |
|
|
---- This file is part of the Present - a lightweight block ----
|
6 |
|
|
---- cipher project ----
|
7 |
|
|
---- http://www.http://opencores.org/project,present ----
|
8 |
|
|
---- ----
|
9 |
|
|
---- Description: ----
|
10 |
|
|
---- Top level of present encoder with 32 bit IO. ----
|
11 |
|
|
---- To Do: ----
|
12 |
|
|
---- ----
|
13 |
|
|
---- Author(s): ----
|
14 |
|
|
---- - Krzysztof Gajewski, gajos@opencores.org ----
|
15 |
|
|
---- k.gajewski@gmail.com ----
|
16 |
|
|
---- ----
|
17 |
|
|
-----------------------------------------------------------------------
|
18 |
|
|
---- ----
|
19 |
|
|
---- Copyright (C) 2013 Authors and OPENCORES.ORG ----
|
20 |
|
|
---- ----
|
21 |
|
|
---- This source file may be used and distributed without ----
|
22 |
|
|
---- restriction provided that this copyright statement is not ----
|
23 |
|
|
---- removed from the file and that any derivative work contains ----
|
24 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
25 |
|
|
---- ----
|
26 |
|
|
---- This source file is free software; you can redistribute it ----
|
27 |
|
|
---- and-or modify it under the terms of the GNU Lesser General ----
|
28 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
29 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
30 |
|
|
---- later version. ----
|
31 |
|
|
---- ----
|
32 |
|
|
---- This source is distributed in the hope that it will be ----
|
33 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
34 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
35 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
36 |
|
|
---- details. ----
|
37 |
|
|
---- ----
|
38 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
39 |
|
|
---- Public License along with this source; if not, download it ----
|
40 |
|
|
---- from http://www.opencores.org/lgpl.shtml ----
|
41 |
|
|
---- ----
|
42 |
|
|
-----------------------------------------------------------------------
|
43 |
2 |
gajos |
library IEEE;
|
44 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
45 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
46 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
47 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
48 |
|
|
|
49 |
|
|
entity PresentEnc is
|
50 |
|
|
generic (
|
51 |
|
|
w_2: integer := 2;
|
52 |
|
|
w_4: integer := 4;
|
53 |
|
|
w_5: integer := 5;
|
54 |
|
|
w_32: integer := 32;
|
55 |
|
|
w_64: integer := 64;
|
56 |
|
|
w_80: integer := 80
|
57 |
|
|
);
|
58 |
|
|
port(
|
59 |
|
|
ctrl : in std_logic_vector(w_4-1 downto 0);
|
60 |
|
|
input : in std_logic_vector(w_32-1 downto 0);
|
61 |
|
|
output : out std_logic_vector(w_32-1 downto 0);
|
62 |
|
|
clk, reset : in std_logic;
|
63 |
|
|
ready : out std_logic
|
64 |
|
|
);
|
65 |
|
|
end PresentEnc;
|
66 |
|
|
|
67 |
|
|
architecture Behavioral of PresentEnc is
|
68 |
|
|
|
69 |
|
|
component PresentStateMachine is
|
70 |
|
|
generic (
|
71 |
|
|
w_2: integer := 2;
|
72 |
|
|
w_4: integer := 4;
|
73 |
|
|
w_5: integer := 5;
|
74 |
|
|
w_32: integer := 32;
|
75 |
|
|
w_64: integer := 64;
|
76 |
|
|
w_80: integer := 80
|
77 |
|
|
);
|
78 |
|
|
port (
|
79 |
|
|
info : in std_logic_vector (w_2-1 downto 0);
|
80 |
|
|
ctrl : in std_logic_vector (w_4-1 downto 0);
|
81 |
|
|
key_ctrl: out std_logic_vector (w_2-1 downto 0);
|
82 |
|
|
plain_ctrl: out std_logic_vector (w_2-1 downto 0);
|
83 |
|
|
outReg : out std_logic_vector (w_2-1 downto 0);
|
84 |
|
|
reset, clk : in std_logic;
|
85 |
|
|
ready, cnt_res, ctrl_mux64, ctrl_mux80 : out std_logic
|
86 |
|
|
);
|
87 |
|
|
end component;
|
88 |
|
|
|
89 |
4 |
gajos |
-- substitution layer for decoding
|
90 |
2 |
gajos |
component slayer is
|
91 |
|
|
generic (
|
92 |
|
|
w_4 : integer := 4
|
93 |
|
|
);
|
94 |
|
|
port (
|
95 |
|
|
input : in std_logic_vector(w_4-1 downto 0);
|
96 |
|
|
output : out std_logic_vector(w_4-1 downto 0)
|
97 |
|
|
);
|
98 |
|
|
end component;
|
99 |
|
|
|
100 |
4 |
gajos |
-- permutation layer for decoding
|
101 |
2 |
gajos |
component pLayer is
|
102 |
|
|
generic(w_64 : integer := 64);
|
103 |
|
|
port(
|
104 |
|
|
input : in std_logic_vector(w_64-1 downto 0);
|
105 |
|
|
output : out std_logic_vector(w_64-1 downto 0)
|
106 |
|
|
);
|
107 |
|
|
end component;
|
108 |
|
|
|
109 |
4 |
gajos |
-- key update for decoding
|
110 |
2 |
gajos |
component keyupd is
|
111 |
|
|
generic(
|
112 |
|
|
w_5 : integer := 5;
|
113 |
|
|
w_80: integer := 80
|
114 |
|
|
);
|
115 |
|
|
port(
|
116 |
|
|
num : in std_logic_vector(w_5-1 downto 0);
|
117 |
|
|
key : in std_logic_vector(w_80-1 downto 0);
|
118 |
|
|
keyout : out std_logic_vector(w_80-1 downto 0)
|
119 |
|
|
);
|
120 |
|
|
end component;
|
121 |
|
|
|
122 |
4 |
gajos |
-- 'register' for 32 bit output and 64 bit input
|
123 |
2 |
gajos |
component outputRegister is
|
124 |
|
|
generic (
|
125 |
|
|
w_2 : integer := 2;
|
126 |
|
|
w_32: integer := 32;
|
127 |
|
|
w_64: integer := 64
|
128 |
|
|
);
|
129 |
|
|
port(
|
130 |
|
|
ctrl : in std_logic_vector(w_2-1 downto 0);
|
131 |
|
|
input : in std_logic_vector(w_64-1 downto 0);
|
132 |
|
|
output : out std_logic_vector(w_32-1 downto 0);
|
133 |
|
|
rst, clk, rd : in std_logic;
|
134 |
|
|
ready : out std_logic
|
135 |
|
|
);
|
136 |
|
|
end component;
|
137 |
|
|
|
138 |
4 |
gajos |
-- counter for decoding. It is counting up!!!
|
139 |
2 |
gajos |
component counter is
|
140 |
|
|
generic (
|
141 |
|
|
w_2 : integer := 2;
|
142 |
|
|
w_5 : integer := 5
|
143 |
|
|
);
|
144 |
|
|
port (
|
145 |
|
|
clk, reset, cnt_res : in std_logic;
|
146 |
|
|
info : out std_logic_vector(w_2-1 downto 0);
|
147 |
|
|
num : out std_logic_vector (w_5-1 downto 0)
|
148 |
|
|
);
|
149 |
|
|
end component;
|
150 |
|
|
|
151 |
4 |
gajos |
-- 'multiplexer' for 32/64 bit input and 64 bit output
|
152 |
2 |
gajos |
component mux64 is
|
153 |
|
|
generic (
|
154 |
|
|
w_2 : integer := 2;
|
155 |
|
|
w_32 : integer := 32;
|
156 |
|
|
w_64 : integer := 64
|
157 |
|
|
);
|
158 |
|
|
port(
|
159 |
|
|
i0ctrl : in std_logic_vector (w_2-1 downto 0);
|
160 |
|
|
input0 : in std_logic_vector(w_32-1 downto 0);
|
161 |
|
|
input1 : in std_logic_vector(w_64-1 downto 0);
|
162 |
|
|
output : inout std_logic_vector(w_64-1 downto 0);
|
163 |
|
|
ctrl, clk, reset : in std_logic
|
164 |
|
|
);
|
165 |
|
|
end component;
|
166 |
|
|
|
167 |
4 |
gajos |
-- 'multiplexer' for 32/80 bit input and 80 bit output
|
168 |
2 |
gajos |
component mux80 is
|
169 |
|
|
generic (
|
170 |
|
|
w_2 : integer := 2;
|
171 |
|
|
w_32 : integer := 32;
|
172 |
|
|
w_80 : integer := 80
|
173 |
|
|
);
|
174 |
|
|
port(
|
175 |
|
|
i0ctrl : in std_logic_vector (w_2-1 downto 0);
|
176 |
|
|
input0 : in std_logic_vector(w_32-1 downto 0);
|
177 |
|
|
input1 : in std_logic_vector(w_80-1 downto 0);
|
178 |
|
|
output : inout std_logic_vector(w_80-1 downto 0);
|
179 |
|
|
ctrl, clk, reset : in std_logic
|
180 |
|
|
);
|
181 |
|
|
end component;
|
182 |
|
|
|
183 |
|
|
component xorModule is
|
184 |
|
|
generic(
|
185 |
|
|
w : positive
|
186 |
|
|
);
|
187 |
|
|
port(
|
188 |
|
|
inputA, inputB : in std_logic_vector( w-1 downto 0 );
|
189 |
|
|
output : out std_logic_vector ( w-1 downto 0 )
|
190 |
|
|
);
|
191 |
|
|
end component;
|
192 |
|
|
|
193 |
4 |
gajos |
-- signals
|
194 |
|
|
|
195 |
2 |
gajos |
signal ro32ctrl, key_ctrl, plain_ctrl, info : std_logic_vector(w_2-1 downto 0);
|
196 |
|
|
signal keynum : std_logic_vector (w_5-1 downto 0);
|
197 |
|
|
signal toXor, ciphertext, P, Pout : std_logic_vector (w_64-1 downto 0);
|
198 |
|
|
signal keyfout, kupd : std_logic_vector (w_80-1 downto 0);
|
199 |
|
|
signal ready_sig, mux64ctrl, mux80ctrl, cnt_res : std_logic;
|
200 |
|
|
|
201 |
|
|
begin
|
202 |
4 |
gajos |
|
203 |
|
|
-- connections
|
204 |
|
|
|
205 |
2 |
gajos |
mux_64: mux64 port map(
|
206 |
|
|
input0 => input, input1 => Pout, output => toXor, ctrl => mux64ctrl,
|
207 |
|
|
i0ctrl => plain_ctrl, clk => clk, reset => reset);
|
208 |
|
|
mux_80: mux80 port map(
|
209 |
|
|
input0 => input, input1 => kupd, output => keyfout, ctrl => mux80ctrl,
|
210 |
|
|
i0ctrl => key_ctrl, clk => clk, reset => reset);
|
211 |
|
|
slayers : for N in 15 downto 0 generate
|
212 |
|
|
s_x: slayer port map(input => ciphertext(4*N+3 downto 4*N), output => P(4*N+3 downto 4*N));
|
213 |
|
|
end generate slayers;
|
214 |
|
|
p1: pLayer port map(input => P, output => Pout);
|
215 |
|
|
mixer: keyupd port map(key => keyfout, num => keynum, keyout => kupd);
|
216 |
|
|
output_reg: outputRegister port map(rst => reset, clk => clk, rd => ready_sig, ctrl => ro32ctrl,
|
217 |
|
|
input => ciphertext, output => output, ready => ready);
|
218 |
|
|
SM: PresentStateMachine port map(ctrl => ctrl, outReg => ro32ctrl, reset => reset,
|
219 |
|
|
ready => ready_sig, cnt_res => cnt_res, ctrl_mux64 => mux64ctrl, ctrl_mux80 => mux80ctrl,
|
220 |
|
|
clk => clk, key_ctrl => key_ctrl, plain_ctrl => plain_ctrl, info => info
|
221 |
|
|
);
|
222 |
|
|
count: counter port map( clk => clk, reset => reset, cnt_res => cnt_res, info => info, num => keynum);
|
223 |
|
|
ciphertext <= toXor xor keyfout(79 downto 16);
|
224 |
|
|
end Behavioral;
|