1 |
2 |
arif_endro |
-- ------------------------------------------------------------------------
|
2 |
|
|
-- Copyright (C) 2010 Arif Endro Nugroho
|
3 |
|
|
-- All rights reserved.
|
4 |
|
|
--
|
5 |
|
|
-- Redistribution and use in source and binary forms, with or without
|
6 |
|
|
-- modification, are permitted provided that the following conditions
|
7 |
|
|
-- are met:
|
8 |
|
|
--
|
9 |
|
|
-- 1. Redistributions of source code must retain the above copyright
|
10 |
|
|
-- notice, this list of conditions and the following disclaimer.
|
11 |
|
|
-- 2. Redistributions in binary form must reproduce the above copyright
|
12 |
|
|
-- notice, this list of conditions and the following disclaimer in the
|
13 |
|
|
-- documentation and/or other materials provided with the distribution.
|
14 |
|
|
--
|
15 |
|
|
-- THIS SOFTWARE IS PROVIDED BY ARIF ENDRO NUGROHO "AS IS" AND ANY EXPRESS
|
16 |
|
|
-- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
17 |
|
|
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
-- DISCLAIMED. IN NO EVENT SHALL ARIF ENDRO NUGROHO BE LIABLE FOR ANY
|
19 |
|
|
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
21 |
|
|
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
22 |
|
|
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
23 |
|
|
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
24 |
|
|
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25 |
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
26 |
|
|
--
|
27 |
|
|
-- End Of License.
|
28 |
|
|
-- ------------------------------------------------------------------------
|
29 |
|
|
--
|
30 |
|
|
-- Cipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)])
|
31 |
|
|
-- begin
|
32 |
|
|
-- byte state[4,Nb]
|
33 |
|
|
-- state = in
|
34 |
|
|
--
|
35 |
|
|
-- AddRoundKey(state, w[0, Nb-1])
|
36 |
|
|
--
|
37 |
|
|
-- for round = 1 step 1 to Nr-1
|
38 |
|
|
-- SubBytes(state)
|
39 |
|
|
-- ShiftRows(state)
|
40 |
|
|
-- MixColumns(state)
|
41 |
|
|
-- AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
|
42 |
|
|
-- end for
|
43 |
|
|
--
|
44 |
|
|
-- SubBytes(state)
|
45 |
|
|
-- ShiftRows(state)
|
46 |
|
|
-- AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
|
47 |
|
|
--
|
48 |
|
|
-- out = state
|
49 |
|
|
-- end
|
50 |
|
|
--
|
51 |
|
|
-- Nb = Number of Block, Nr = Number of Round
|
52 |
|
|
-- AES-128 => Nk(4), Nb(4), Nr(10)
|
53 |
|
|
-- AES-192 => Nk(6), Nb(4), Nr(12)
|
54 |
|
|
-- AES-256 => Nk(8), Nb(4), Nr(14)
|
55 |
|
|
--
|
56 |
|
|
|
57 |
|
|
library ieee;
|
58 |
|
|
use ieee.std_logic_1164.all;
|
59 |
|
|
use ieee.std_logic_unsigned.all;
|
60 |
|
|
|
61 |
|
|
entity cipher is
|
62 |
|
|
port (
|
63 |
|
|
pt : in bit_vector ( 31 downto 0); -- plain text
|
64 |
|
|
key : in bit_vector ( 31 downto 0); -- source key
|
65 |
|
|
Nk : in bit_vector ( 3 downto 0); -- 128,192,256 => 4,6,8 (0100,0110,1000)
|
66 |
|
|
ldpt : in bit; -- load signal for the first 128 bit block
|
67 |
|
|
ct : out bit_vector ( 31 downto 0); -- cipher text
|
68 |
|
|
v : out bit; -- valid cipher text output
|
69 |
|
|
clk : in bit; -- master clock
|
70 |
|
|
rst : in bit -- master reset
|
71 |
|
|
);
|
72 |
|
|
end cipher;
|
73 |
|
|
|
74 |
|
|
architecture phy of cipher is
|
75 |
|
|
|
76 |
|
|
signal ireg1 : bit_vector (127 downto 0); -- 128 bit internal register 1
|
77 |
|
|
signal ireg2 : bit_vector (127 downto 0); -- 128 bit internal register 2
|
78 |
|
|
signal ct2b : bit_vector ( 1 downto 0); -- 2 bit counter
|
79 |
|
|
signal wsb1 : bit_vector ( 31 downto 0); -- SubBytes
|
80 |
|
|
signal wsb2 : bit_vector ( 31 downto 0); -- SubBytes
|
81 |
|
|
signal wsr : bit_vector ( 31 downto 0); -- ShiftRows
|
82 |
|
|
signal wmc : bit_vector ( 31 downto 0); -- MixColumns
|
83 |
|
|
signal ssm : bit_vector ( 31 downto 0); -- SubBytes, ShiftRows, MixColumns
|
84 |
|
|
signal ikey : bit_vector ( 31 downto 0); -- internal round key
|
85 |
|
|
signal rnd : bit_vector ( 3 downto 0); -- current round number
|
86 |
|
|
signal rnd_cr : bit_vector ( 3 downto 0); -- currend round number carry
|
87 |
|
|
signal ipt : bit_vector ( 31 downto 0); -- internal plain text
|
88 |
|
|
signal s1i : bit_vector ( 7 downto 0); -- Input SubBytes 1
|
89 |
|
|
signal s2i : bit_vector ( 7 downto 0); -- Input SubBytes 2
|
90 |
|
|
signal s3i : bit_vector ( 7 downto 0); -- Input SubBytes 3
|
91 |
|
|
signal s4i : bit_vector ( 7 downto 0); -- Input SubBytes 4
|
92 |
|
|
signal s1o : bit_vector ( 7 downto 0); -- Output SubBytes 1
|
93 |
|
|
signal s2o : bit_vector ( 7 downto 0); -- Output SubBytes 2
|
94 |
|
|
signal s3o : bit_vector ( 7 downto 0); -- Output SubBytes 3
|
95 |
|
|
signal s4o : bit_vector ( 7 downto 0); -- Output SubBytes 4
|
96 |
|
|
signal x2ai : bit_vector ( 7 downto 0); -- Input xtime 2 a
|
97 |
|
|
signal x2bi : bit_vector ( 7 downto 0); -- Input xtime 2 b
|
98 |
|
|
signal x2ci : bit_vector ( 7 downto 0); -- Input xtime 2 c
|
99 |
|
|
signal x2di : bit_vector ( 7 downto 0); -- Input xtime 2 d
|
100 |
|
|
signal x2ao : bit_vector ( 7 downto 0); -- Output xtime 2 a
|
101 |
|
|
signal x2bo : bit_vector ( 7 downto 0); -- Output xtime 2 b
|
102 |
|
|
signal x2co : bit_vector ( 7 downto 0); -- Output xtime 2 c
|
103 |
|
|
signal x2do : bit_vector ( 7 downto 0); -- Output xtime 2 d
|
104 |
|
|
signal ct2b_rst : bit; -- reset for internal block operation
|
105 |
|
|
signal swp : bit; -- swap internal register
|
106 |
|
|
signal swp1 : bit; -- swap internal register
|
107 |
|
|
signal vld : bit; -- final round
|
108 |
|
|
signal vld1 : bit; -- final round
|
109 |
|
|
signal ildpt : bit; -- internal load plain text
|
110 |
|
|
signal ildpt_rst : bit; -- internal load plain text reset
|
111 |
|
|
|
112 |
|
|
component sbox
|
113 |
|
|
port (
|
114 |
|
|
di : in bit_vector ( 7 downto 0);
|
115 |
|
|
do : out bit_vector ( 7 downto 0)
|
116 |
|
|
);
|
117 |
|
|
end component;
|
118 |
|
|
|
119 |
|
|
component c2b
|
120 |
|
|
port (
|
121 |
|
|
cnt : out bit_vector ( 1 downto 0);
|
122 |
|
|
clk : in bit;
|
123 |
|
|
rst : in bit
|
124 |
|
|
);
|
125 |
|
|
end component;
|
126 |
|
|
|
127 |
|
|
component xtime_2
|
128 |
|
|
port (
|
129 |
|
|
x2i : in bit_vector ( 7 downto 0);
|
130 |
|
|
x2o : out bit_vector ( 7 downto 0)
|
131 |
|
|
);
|
132 |
|
|
end component;
|
133 |
|
|
|
134 |
|
|
begin
|
135 |
|
|
|
136 |
|
|
sb1 : sbox
|
137 |
|
|
port map (
|
138 |
|
|
di => s1i,
|
139 |
|
|
do => s1o
|
140 |
|
|
);
|
141 |
|
|
sb2 : sbox
|
142 |
|
|
port map (
|
143 |
|
|
di => s2i,
|
144 |
|
|
do => s2o
|
145 |
|
|
);
|
146 |
|
|
sb3 : sbox
|
147 |
|
|
port map (
|
148 |
|
|
di => s3i,
|
149 |
|
|
do => s3o
|
150 |
|
|
);
|
151 |
|
|
sb4 : sbox
|
152 |
|
|
port map (
|
153 |
|
|
di => s4i,
|
154 |
|
|
do => s4o
|
155 |
|
|
);
|
156 |
|
|
ctr1 : c2b
|
157 |
|
|
port map (
|
158 |
|
|
cnt => ct2b,
|
159 |
|
|
clk => clk,
|
160 |
|
|
rst => ct2b_rst
|
161 |
|
|
);
|
162 |
|
|
x2a : xtime_2
|
163 |
|
|
port map (
|
164 |
|
|
x2i => x2ai,
|
165 |
|
|
x2o => x2ao
|
166 |
|
|
);
|
167 |
|
|
x2b : xtime_2
|
168 |
|
|
port map (
|
169 |
|
|
x2i => x2bi,
|
170 |
|
|
x2o => x2bo
|
171 |
|
|
);
|
172 |
|
|
x2c : xtime_2
|
173 |
|
|
port map (
|
174 |
|
|
x2i => x2ci,
|
175 |
|
|
x2o => x2co
|
176 |
|
|
);
|
177 |
|
|
x2d : xtime_2
|
178 |
|
|
port map (
|
179 |
|
|
x2i => x2di,
|
180 |
|
|
x2o => x2do
|
181 |
|
|
);
|
182 |
|
|
|
183 |
|
|
-- 007 039 071 103 | 007 039 071 103
|
184 |
|
|
-- 015 047 079 111 | 047 079 111 015
|
185 |
|
|
-- 023 055 087 119 | 087 119 023 055
|
186 |
|
|
-- 031 063 095 127 | 127 031 063 095
|
187 |
|
|
|
188 |
|
|
with ct2b(01 downto 00) select
|
189 |
|
|
wsb1 <= ireg1(127 downto 120) & ireg1( 87 downto 80) & ireg1( 47 downto 40) & ireg1( 7 downto 0) when B"00", -- 1st column
|
190 |
|
|
ireg1( 31 downto 24) & ireg1(119 downto 112) & ireg1( 79 downto 72) & ireg1( 39 downto 32) when B"11", -- 4th column
|
191 |
|
|
ireg1( 63 downto 56) & ireg1( 23 downto 16) & ireg1(111 downto 104) & ireg1( 71 downto 64) when B"10", -- 3rd column
|
192 |
|
|
ireg1( 95 downto 88) & ireg1( 55 downto 48) & ireg1( 15 downto 8) & ireg1(103 downto 96) when B"01"; -- 2nd column
|
193 |
|
|
with ct2b(01 downto 00) select
|
194 |
|
|
wsb2 <= ireg2(127 downto 120) & ireg2( 87 downto 80) & ireg2( 47 downto 40) & ireg2( 7 downto 0) when B"00", -- 1st column
|
195 |
|
|
ireg2( 31 downto 24) & ireg2(119 downto 112) & ireg2( 79 downto 72) & ireg2( 39 downto 32) when B"11", -- 4th column
|
196 |
|
|
ireg2( 63 downto 56) & ireg2( 23 downto 16) & ireg2(111 downto 104) & ireg2( 71 downto 64) when B"10", -- 3rd column
|
197 |
|
|
ireg2( 95 downto 88) & ireg2( 55 downto 48) & ireg2( 15 downto 8) & ireg2(103 downto 96) when B"01"; -- 2nd column
|
198 |
|
|
|
199 |
|
|
--SubBytes
|
200 |
|
|
s1i(07 downto 00)<= wsb1(31 downto 24) when swp = '1' else wsb2(31 downto 24);
|
201 |
|
|
s2i(07 downto 00)<= wsb1(23 downto 16) when swp = '1' else wsb2(23 downto 16);
|
202 |
|
|
s3i(07 downto 00)<= wsb1(15 downto 08) when swp = '1' else wsb2(15 downto 08);
|
203 |
|
|
s4i(07 downto 00)<= wsb1(07 downto 00) when swp = '1' else wsb2(07 downto 00);
|
204 |
|
|
|
205 |
|
|
--ShiftRows
|
206 |
|
|
wsr <= s1o & s2o & s3o & s4o;
|
207 |
|
|
|
208 |
|
|
--MixColumns
|
209 |
|
|
x2ai <= wsr(31 downto 24);
|
210 |
|
|
x2bi <= wsr(23 downto 16);
|
211 |
|
|
x2ci <= wsr(15 downto 08);
|
212 |
|
|
x2di <= wsr(07 downto 00);
|
213 |
|
|
|
214 |
|
|
wmc(31 downto 24)<= x2ao xor x2bo xor x2bi xor x2ci xor x2di;
|
215 |
|
|
wmc(23 downto 16)<= x2ai xor x2bo xor x2co xor x2ci xor x2di;
|
216 |
|
|
wmc(15 downto 08)<= x2ai xor x2bi xor x2co xor x2do xor x2di;
|
217 |
|
|
wmc(07 downto 00)<= x2ao xor x2ai xor x2bi xor x2ci xor x2do;
|
218 |
|
|
|
219 |
|
|
process (clk)
|
220 |
|
|
begin
|
221 |
|
|
if ((clk = '1') and clk'event) then
|
222 |
|
|
ildpt <= ldpt;
|
223 |
|
|
end if;
|
224 |
|
|
end process;
|
225 |
|
|
|
226 |
|
|
ildpt_rst <= ((ildpt xor ldpt) and ldpt);
|
227 |
|
|
ct2b_rst <= rst or ildpt_rst;
|
228 |
|
|
|
229 |
|
|
rnd_cr(0) <= '0'; -- LSB always zero
|
230 |
|
|
rnd_cr(3 downto 1) <= ( ((rnd(2 downto 0) and B"001") or (rnd(2 downto 0) and rnd_cr(2 downto 0))) or (B"001" and rnd_cr(2 downto 0)) );
|
231 |
|
|
|
232 |
|
|
process (clk)
|
233 |
|
|
begin
|
234 |
|
|
if ((clk = '1') and clk'event) then
|
235 |
|
|
if ((ildpt_rst or rst) = '1') then
|
236 |
|
|
swp <= '0';
|
237 |
|
|
rnd <= B"0000";
|
238 |
|
|
elsif (not(not(ct2b(1)) or not(ct2b(0))) = '1') then
|
239 |
|
|
swp <= not(swp);
|
240 |
|
|
rnd <= ((rnd xor B"0001") xor rnd_cr);
|
241 |
|
|
end if;
|
242 |
|
|
end if;
|
243 |
|
|
end process;
|
244 |
|
|
|
245 |
|
|
vld <= (not(Nk(3) or not(Nk(2)) or Nk(1) or Nk(0)) and not(not(rnd(3)) or rnd(2) or not(rnd(1)) or rnd(0))) or -- Nk 0100 (10 round)
|
246 |
|
|
(not(Nk(3) or not(Nk(2)) or not(Nk(1)) or Nk(0)) and not(not(rnd(3)) or not(rnd(2)) or rnd(1) or rnd(0))) or -- Nk 0110 (12 round)
|
247 |
|
|
(not(not(Nk(3)) or Nk(2) or Nk(1) or Nk(0)) and not(not(rnd(3)) or not(rnd(2)) or not(rnd(1)) or rnd(0))); -- Nk 1000 (14 round)
|
248 |
|
|
|
249 |
|
|
ssm <= wmc when vld = '0' else wsr;
|
250 |
|
|
ikey <= key;
|
251 |
|
|
|
252 |
|
|
process (clk)
|
253 |
|
|
begin
|
254 |
|
|
if ((clk = '1') and clk'event) then
|
255 |
|
|
if (rst = '1') then
|
256 |
|
|
ireg1(127 downto 00) <= (others => '0');
|
257 |
|
|
ireg2(127 downto 00) <= (others => '0');
|
258 |
|
|
elsif (ildpt = '1') then
|
259 |
|
|
ireg1(127 downto 00) <= ireg1(095 downto 00) & (ipt xor ikey); -- initial round
|
260 |
|
|
elsif ( swp = '0') then
|
261 |
|
|
ireg1(127 downto 00) <= ireg1(095 downto 00) & (ssm xor ikey);
|
262 |
|
|
else
|
263 |
|
|
ireg2(127 downto 00) <= ireg2(095 downto 00) & (ssm xor ikey);
|
264 |
|
|
end if;
|
265 |
|
|
end if;
|
266 |
|
|
end process;
|
267 |
|
|
|
268 |
|
|
process (clk)
|
269 |
|
|
begin
|
270 |
|
|
if ((clk = '1') and clk'event) then
|
271 |
|
|
swp1 <= swp;
|
272 |
|
|
vld1 <= vld;
|
273 |
|
|
ipt <= pt;
|
274 |
|
|
end if;
|
275 |
|
|
end process;
|
276 |
|
|
|
277 |
|
|
ct <= ireg1(31 downto 00) when swp1 = '0' else ireg2(31 downto 00);
|
278 |
|
|
v <= vld1;
|
279 |
|
|
|
280 |
|
|
end phy;
|