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

Subversion Repositories nfcc

[/] [nfcc/] [trunk/] [rijndael/] [invcipher/] [invcipher.vhdl] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
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
-- InvCipher (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[Nr*Nb, (Nr+1)*Nb-1])
36
--
37
--   for round = Nr-1 step -1 downto 1
38
--     InvShiftRows(state)
39
--     InvSubBytes(state)
40
--     AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
41
--     InvMixColumns(state)
42
--   end for
43
--
44
--   InvShiftRows(state)
45
--   InvSubBytes(state)
46
--   AddRoundKey(state, w[0, Nb-1])
47
--
48
--   out = state
49
-- end
50
--
51
--
52
-- EqInvCipher (byte in[4*Nb], byte out[4*Nb], word dw[Nb*(Nr+1)])
53
-- begin
54
--   byte state[4,Nb]
55
--   state = in
56
--
57
--   AddRoundKey(state, dw[Nr*Nb, (Nr+1)*Nb-1])
58
--
59
--   for round = Nr-1 step -1 downto 1
60
--     InvSubBytes(state)
61
--     InvShiftRows(state)
62
--     InvMixColumns(state)
63
--     AddRoundKey(state, dw[round*Nb, (round+1)*Nb-1])
64
--   end for
65
--
66
--   InvSubBytes(state)
67
--   InvShiftRows(state)
68
--   AddRoundKey(state, dw[0, Nb-1])
69
--
70
--   out = state
71
-- end
72
--
73
-- for i = 0 step 1 to (Nr+1)*Nb-1
74
--   dw[i] = w[i]
75
-- end for
76
--
77
-- for round = 1 step 1 to Nr-1
78
--   InvMixColumns(dw[round*Nb, (round+1)*Nb-1]);
79
-- end for
80
 
81
library ieee;
82
use ieee.std_logic_1164.all;
83
use ieee.std_logic_unsigned.all;
84
 
85
entity invcipher is
86
  port (
87
  ct               : in  bit_vector ( 31 downto 0); -- cipher text
88
  key              : in  bit_vector ( 31 downto 0); -- source key
89
  Nk               : in  bit_vector (  3 downto 0); --
90
  ldct             : in  bit;                       -- load cipher text
91
  pt               : out bit_vector ( 31 downto 0); -- plain text
92
  v                : out bit;                       -- valid plain text output
93
  clk              : in  bit;                       -- master clock
94
  rst              : in  bit                        -- master reset
95
  );
96
end invcipher;
97
 
98
architecture phy of invcipher is
99
 
100
  component invsbox
101
    port (
102
    di             : in  bit_vector (  7 downto 0);
103
    do             : out bit_vector (  7 downto 0)
104
    );
105
  end component;
106
 
107
  component c2b
108
    port (
109
    cnt            : out bit_vector (  1 downto 0);
110
    clk            : in  bit;
111
    rst            : in  bit
112
    );
113
  end component;
114
 
115
  component xtime_2
116
    port (
117
    x2i            : in  bit_vector (  7 downto 0);
118
    x2o            : out bit_vector (  7 downto 0)
119
    );
120
  end component;
121
 
122
  component xtime_4
123
    port (
124
    x4i            : in  bit_vector (  7 downto 0);
125
    x4o            : out bit_vector (  7 downto 0)
126
    );
127
  end component;
128
 
129
  signal ireg1     :     bit_vector (127 downto 0); -- 128 bit internal register 1
130
  signal ireg2     :     bit_vector (127 downto 0); -- 128 bit internal register 2
131
  signal ct2b      :     bit_vector (  1 downto 0); --   2 bit counter
132
  signal wsb1      :     bit_vector ( 31 downto 0); -- SubBytes
133
  signal wsb2      :     bit_vector ( 31 downto 0); -- SubBytes
134
  signal wsr       :     bit_vector ( 31 downto 0); -- ShiftRows
135
  signal wmc       :     bit_vector ( 31 downto 0); -- MixColumns
136
  signal iwmc      :     bit_vector ( 31 downto 0); -- InvMixColumns
137
  signal ssm       :     bit_vector ( 31 downto 0); -- SubBytes, ShiftRows, MixColumns
138
  signal ikey      :     bit_vector ( 31 downto 0); -- internal round key
139
  signal rnd       :     bit_vector (  3 downto 0); -- current round number
140
  signal rnd_cr    :     bit_vector (  3 downto 0); -- currend round number carry
141
  signal ict       :     bit_vector ( 31 downto 0); -- internal cipher text
142
  signal s1i       :     bit_vector (  7 downto 0); --  Input SubBytes 1
143
  signal s2i       :     bit_vector (  7 downto 0); --  Input SubBytes 2
144
  signal s3i       :     bit_vector (  7 downto 0); --  Input SubBytes 3
145
  signal s4i       :     bit_vector (  7 downto 0); --  Input SubBytes 4
146
  signal s1o       :     bit_vector (  7 downto 0); -- Output SubBytes 1
147
  signal s2o       :     bit_vector (  7 downto 0); -- Output SubBytes 2
148
  signal s3o       :     bit_vector (  7 downto 0); -- Output SubBytes 3
149
  signal s4o       :     bit_vector (  7 downto 0); -- Output SubBytes 4
150
  signal x2ai      :     bit_vector (  7 downto 0); --  Input xtime 2  a
151
  signal x2bi      :     bit_vector (  7 downto 0); --  Input xtime 2  b
152
  signal x2ci      :     bit_vector (  7 downto 0); --  Input xtime 2  c
153
  signal x2di      :     bit_vector (  7 downto 0); --  Input xtime 2  d
154
  signal x2ao      :     bit_vector (  7 downto 0); -- Output xtime 2  a
155
  signal x2bo      :     bit_vector (  7 downto 0); -- Output xtime 2  b
156
  signal x2co      :     bit_vector (  7 downto 0); -- Output xtime 2  c
157
  signal x2do      :     bit_vector (  7 downto 0); -- Output xtime 2  d
158
  signal x4ai      :     bit_vector (  7 downto 0); --  Input xtime 4  a
159
  signal x4bi      :     bit_vector (  7 downto 0); --  Input xtime 4  b
160
  signal x4ci      :     bit_vector (  7 downto 0); --  Input xtime 4  c
161
  signal x4di      :     bit_vector (  7 downto 0); --  Input xtime 4  d
162
  signal x4ao      :     bit_vector (  7 downto 0); -- Output xtime 4  a
163
  signal x4bo      :     bit_vector (  7 downto 0); -- Output xtime 4  b
164
  signal x4co      :     bit_vector (  7 downto 0); -- Output xtime 4  c
165
  signal x4do      :     bit_vector (  7 downto 0); -- Output xtime 4  d
166
  signal ct2b_rst  :     bit;                       -- reset for internal block operation
167
  signal swp       :     bit;                       -- swap internal register
168
  signal swp1      :     bit;                       -- swap internal register
169
  signal vld       :     bit;                       -- final round
170
  signal vld1      :     bit;                       -- final round
171
  signal ildct     :     bit;                       -- internal load cipher text
172
  signal ildct_rst :     bit;                       -- internal load cipher text reset
173
 
174
begin
175
 
176
  sb1 : invsbox
177
  port map (
178
    di => s1i,
179
    do => s1o
180
    );
181
  sb2 : invsbox
182
  port map (
183
    di => s2i,
184
    do => s2o
185
    );
186
  sb3 : invsbox
187
  port map (
188
    di => s3i,
189
    do => s3o
190
    );
191
  sb4 : invsbox
192
  port map (
193
    di => s4i,
194
    do => s4o
195
    );
196
  ctr1 : c2b
197
  port map (
198
    cnt => ct2b,
199
    clk => clk,
200
    rst => ct2b_rst
201
    );
202
  x2a : xtime_2
203
  port map (
204
    x2i => x2ai,
205
    x2o => x2ao
206
    );
207
  x2b : xtime_2
208
  port map (
209
    x2i => x2bi,
210
    x2o => x2bo
211
    );
212
  x2c : xtime_2
213
  port map (
214
    x2i => x2ci,
215
    x2o => x2co
216
    );
217
  x2d : xtime_2
218
  port map (
219
    x2i => x2di,
220
    x2o => x2do
221
    );
222
  x4a : xtime_4
223
  port map (
224
    x4i => x4ai,
225
    x4o => x4ao
226
    );
227
  x4b : xtime_4
228
  port map (
229
    x4i => x4bi,
230
    x4o => x4bo
231
    );
232
  x4c : xtime_4
233
  port map (
234
    x4i => x4ci,
235
    x4o => x4co
236
    );
237
  x4d : xtime_4
238
  port map (
239
    x4i => x4di,
240
    x4o => x4do
241
    );
242
 
243
--   7  39  71 103 |   7  39  71 103 |   7  39  71 103
244
--  15  47  79 111 |  47  79 111  15 | 111  15  47  79
245
--  23  55  87 119 |  87 119  23  55 |  87 119  23  55
246
--  31  63  95 127 | 127  31  63  95 |  63  95 127  31
247
 
248
  with ct2b(  1 downto 0) select
249
  wsb1 <= ireg1( 63 downto  56) & ireg1( 87 downto  80) & ireg1(111 downto 104) & ireg1(  7 downto   0) when B"10", -- 1st column
250
          ireg1( 95 downto  88) & ireg1(119 downto 112) & ireg1( 15 downto   8) & ireg1( 39 downto  32) when B"01", -- 4th column
251
          ireg1(127 downto 120) & ireg1( 23 downto  16) & ireg1( 47 downto  40) & ireg1( 71 downto  64) when B"00", -- 3rd column
252
          ireg1( 31 downto  24) & ireg1( 55 downto  48) & ireg1( 79 downto  72) & ireg1(103 downto  96) when B"11"; -- 2nd column
253
  with ct2b(  1 downto 0) select
254
  wsb2 <= ireg2( 63 downto  56) & ireg2( 87 downto  80) & ireg2(111 downto 104) & ireg2(  7 downto   0) when B"10", -- 1st column
255
          ireg2( 95 downto  88) & ireg2(119 downto 112) & ireg2( 15 downto   8) & ireg2( 39 downto  32) when B"01", -- 4th column
256
          ireg2(127 downto 120) & ireg2( 23 downto  16) & ireg2( 47 downto  40) & ireg2( 71 downto  64) when B"00", -- 3rd column
257
          ireg2( 31 downto  24) & ireg2( 55 downto  48) & ireg2( 79 downto  72) & ireg2(103 downto  96) when B"11"; -- 2nd column
258
 
259
--SubBytes
260
  s1i(  7 downto 0) <= wsb1( 31 downto 24) when swp = '1' else wsb2( 31 downto  24);
261
  s2i(  7 downto 0) <= wsb1( 23 downto 16) when swp = '1' else wsb2( 23 downto  16);
262
  s3i(  7 downto 0) <= wsb1( 15 downto  8) when swp = '1' else wsb2( 15 downto   8);
263
  s4i(  7 downto 0) <= wsb1(  7 downto  0) when swp = '1' else wsb2(  7 downto   0);
264
 
265
--ShiftRows
266
  wsr <= s1o & s2o & s3o & s4o;
267
 
268
--MixColumns -- addroundkey first
269
  x2ai <= wsr( 31 downto  24) xor ikey( 31 downto  24);
270
  x2bi <= wsr( 23 downto  16) xor ikey( 23 downto  16);
271
  x2ci <= wsr( 15 downto   8) xor ikey( 15 downto   8);
272
  x2di <= wsr(  7 downto   0) xor ikey(  7 downto   0);
273
 
274
  wmc( 31 downto  24) <= x2ao xor x2bo xor x2bi xor x2ci xor x2di;
275
  wmc( 23 downto  16) <= x2ai xor x2bo xor x2co xor x2ci xor x2di;
276
  wmc( 15 downto   8) <= x2ai xor x2bi xor x2co xor x2do xor x2di;
277
  wmc(  7 downto   0) <= x2ao xor x2ai xor x2bi xor x2ci xor x2do;
278
 
279
--InvMixColumns
280
  x4ai <= wmc( 31 downto  24);
281
  x4bi <= wmc( 23 downto  16);
282
  x4ci <= wmc( 15 downto   8);
283
  x4di <= wmc(  7 downto   0);
284
 
285
  iwmc( 31 downto  24) <= x4ao xor x4ai xor x4co ;
286
  iwmc( 23 downto  16) <= x4bo xor x4bi xor x4do ;
287
  iwmc( 15 downto   8) <= x4co xor x4ci xor x4ao ;
288
  iwmc(  7 downto   0) <= x4do xor x4di xor x4bo ;
289
 
290
  process (clk)
291
  begin
292
    if ((clk = '1') and clk'event) then
293
      ildct <= ldct;
294
    end if;
295
  end process;
296
 
297
  ildct_rst <= ((ildct xor ldct) and ldct);
298
  ct2b_rst  <= rst or ildct_rst;
299
 
300
  rnd_cr(0)          <= '0'; -- LSB always zero
301
  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)) );
302
 
303
  process (clk)
304
  begin
305
    if ((clk = '1') and clk'event) then
306
      if ((ildct_rst or rst) = '1') then
307
        swp <= '0';
308
        rnd <= B"0000";
309
      elsif (not(not(ct2b(1)) or not(ct2b(0))) = '1') then
310
        swp <= not(swp);
311
        rnd <= ((rnd xor B"0001") xor rnd_cr);
312
      end if;
313
    end if;
314
  end process;
315
 
316
  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)
317
          (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)
318
          (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)
319
 
320
  ikey <= key;
321
  ssm  <= iwmc when vld = '0' else wsr xor ikey;
322
 
323
  process (clk)
324
  begin
325
    if ((clk = '1') and clk'event) then
326
      if (rst = '1') then
327
        ireg1(127 downto 0) <= (others => '0');
328
        ireg2(127 downto 0) <= (others => '0');
329
      elsif (ildct = '1') then
330
        ireg1(127 downto 0) <= ireg1( 95 downto 0) & (ict xor ikey); -- initial round
331
      elsif (  swp = '0') then
332
        ireg1(127 downto 0) <= ireg1( 95 downto 0) & (ssm);
333
      else
334
        ireg2(127 downto 0) <= ireg2( 95 downto 0) & (ssm);
335
      end if;
336
    end if;
337
  end process;
338
 
339
  process (clk)
340
  begin
341
    if ((clk = '1') and clk'event) then
342
      swp1 <= swp;
343
      vld1 <= vld;
344
      ict  <= ct;
345
    end if;
346
  end process;
347
 
348
  pt  <= ireg1( 31 downto 0) when swp1 = '0' else ireg2( 31 downto 0);
349
  v   <= vld1;
350
 
351
end phy;

powered by: WebSVN 2.1.0

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