1 |
2 |
schengopen |
////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// This file is part of the AES SystemVerilog Behavioral ////
|
4 |
|
|
//// Model project ////
|
5 |
|
|
//// http://www.opencores.org/cores/aes_beh_model/ ////
|
6 |
|
|
//// ////
|
7 |
|
|
//// Description ////
|
8 |
|
|
//// Implementation of AES SystemVerilog Behavioral ////
|
9 |
|
|
//// Model according to AES Behavioral Model specification document.////
|
10 |
|
|
//// ////
|
11 |
|
|
//// To Do: ////
|
12 |
|
|
//// - ////
|
13 |
|
|
//// ////
|
14 |
|
|
//// Author(s): ////
|
15 |
|
|
//// - scheng, schengopencores@opencores.org ////
|
16 |
|
|
//// ////
|
17 |
|
|
////////////////////////////////////////////////////////////////////////
|
18 |
|
|
//// ////
|
19 |
|
|
//// Copyright (C) 2009 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 |
|
|
|
44 |
|
|
// This is a SystemVerilog implementation of the AES decryption algorithm
|
45 |
|
|
// described in FIPS-197. Only decryption is implemented in this version.
|
46 |
|
|
// The model is implemented as a SystemVerilog class which can be instantiated
|
47 |
|
|
// in a testbench to generate known good results for verification of AES
|
48 |
|
|
// decryption IPs.
|
49 |
|
|
//
|
50 |
|
|
// Try to use the typdefs at the end of this file instead of the class
|
51 |
|
|
// below while declaring variables for the aes model in your testbench.
|
52 |
|
|
//
|
53 |
|
|
// Refer to the specification document on how to use the model in your
|
54 |
|
|
// testbench.
|
55 |
|
|
|
56 |
|
|
class aes_decrypt_model #(int Nk=4, int Nr=10);
|
57 |
|
|
// Refer to section 5 fig.4 of FIPS-197 spec for definitions of Nk and Nr
|
58 |
|
|
//
|
59 |
|
|
// Key length Nk Nr
|
60 |
|
|
// 128 4 10
|
61 |
|
|
// 192 6 12
|
62 |
|
|
// 256 8 14
|
63 |
|
|
|
64 |
|
|
byte unsigned state[0:3][0:3];
|
65 |
|
|
byte unsigned keysch[0:4*(Nr+1)-1][0:3];
|
66 |
|
|
protected int unsigned curr_round;
|
67 |
|
|
bit done; // done=1 -> decryption done, valid plaintext available for read.
|
68 |
|
|
bit loaded; // Ciphertext loaded, ready to start decryption.
|
69 |
|
|
|
70 |
|
|
function new(); // Constructor
|
71 |
|
|
done = 0;
|
72 |
|
|
loaded = 0;
|
73 |
|
|
endfunction
|
74 |
|
|
|
75 |
|
|
function int unsigned GetCurrRound;
|
76 |
|
|
// Returns which round we are at in the decryption process. For AES decryption
|
77 |
|
|
// round counts down from Nr to 0.
|
78 |
|
|
GetCurrRound = curr_round;
|
79 |
|
|
endfunction
|
80 |
|
|
|
81 |
|
|
task LoadCt(bit [0:127] ct);
|
82 |
|
|
// Populate state array with ciphertext and set loaded flag
|
83 |
|
|
for (int j=0; j<=3; j++)
|
84 |
|
|
for (int k=0; k<=3; k++) state[k][j] = ct[(32*j+8*k)+:8];
|
85 |
|
|
loaded = 1;
|
86 |
|
|
done = 0;
|
87 |
|
|
curr_round = Nr; // Inverse cipher round counts down from Nr
|
88 |
|
|
endtask
|
89 |
|
|
|
90 |
|
|
function bit [0:127] GetState;
|
91 |
|
|
// Returns current state as a 128-bit vector.
|
92 |
|
|
// Once all rounds are completed, state contains the decrypted plaintext.
|
93 |
|
|
for (int j=0; j<=3; j++)
|
94 |
|
|
for (int k=0; k<=3; k++) GetState[(32*j+8*k)+:8] = state[k][j];
|
95 |
|
|
endfunction
|
96 |
|
|
|
97 |
|
|
function bit [0:127] GetCurrKsch;
|
98 |
|
|
// Get key schedule of the current round.
|
99 |
|
|
// Note that for decryption, round counts down from Nr.
|
100 |
|
|
for (int j=0; j<=3; j++)
|
101 |
|
|
for (int k=0; k<=3; k++) GetCurrKsch[(32*j+8*k)+:8] = keysch[curr_round*4+j][k];
|
102 |
|
|
endfunction
|
103 |
|
|
|
104 |
|
|
function bit [0:127] LookupKsch(int unsigned r);
|
105 |
|
|
// Lookup key schedule for any round.
|
106 |
|
|
for (int j=0; j<=3; j++)
|
107 |
|
|
for (int k=0; k<=3; k++) LookupKsch[(32*j+8*k)+:8] = keysch[r*4+j][k];
|
108 |
|
|
endfunction
|
109 |
|
|
|
110 |
|
|
task KeyExpand(bit [0:4*8*Nk-1] key);
|
111 |
|
|
// Load key to model and compute key_schedule
|
112 |
|
|
|
113 |
|
|
int j=0;
|
114 |
|
|
byte unsigned temp[0:3];
|
115 |
|
|
byte unsigned Rcon[1:11] = {8'h01,8'h02,8'h04,8'h08,8'h10,8'h20,8'h40,8'h80,8'h1b,8'h36,8'h6c};
|
116 |
|
|
byte unsigned kt[0:4*Nk-1]; // Array holding the key
|
117 |
|
|
|
118 |
|
|
// Populate kt array
|
119 |
|
|
for (int i=0; i<=4*Nk-1; i++) kt[i] = key[i*8+:8];
|
120 |
|
|
|
121 |
|
|
while (j < Nk)
|
122 |
|
|
begin
|
123 |
|
|
keysch[j][0] = kt[4*j];
|
124 |
|
|
keysch[j][1] = kt[4*j+1];
|
125 |
|
|
keysch[j][2] = kt[4*j+2];
|
126 |
|
|
keysch[j][3] = kt[4*j+3];
|
127 |
|
|
j++;
|
128 |
|
|
end
|
129 |
|
|
|
130 |
|
|
// Now j = Nk
|
131 |
|
|
while (j < 4*(Nr+1))
|
132 |
|
|
begin
|
133 |
|
|
temp[0] = keysch[j-1][0];
|
134 |
|
|
temp[1] = keysch[j-1][1];
|
135 |
|
|
temp[2] = keysch[j-1][2];
|
136 |
|
|
temp[3] = keysch[j-1][3];
|
137 |
|
|
|
138 |
|
|
if ((j % Nk) == 0) // When j is a multiple of key length
|
139 |
|
|
begin
|
140 |
|
|
RotWord(temp);
|
141 |
|
|
SubWord(temp);
|
142 |
|
|
temp[0] ^= Rcon[j/Nk];
|
143 |
|
|
end
|
144 |
|
|
else if ((Nk > 6) && ((j % Nk) == 4)) // Only Nk=8 (AES256) will hit this case
|
145 |
|
|
SubWord(temp);
|
146 |
|
|
|
147 |
|
|
keysch[j][0] = keysch[j-Nk][0] ^ temp[0];
|
148 |
|
|
keysch[j][1] = keysch[j-Nk][1] ^ temp[1];
|
149 |
|
|
keysch[j][2] = keysch[j-Nk][2] ^ temp[2];
|
150 |
|
|
keysch[j][3] = keysch[j-Nk][3] ^ temp[3];
|
151 |
|
|
|
152 |
|
|
j++;
|
153 |
|
|
end
|
154 |
|
|
|
155 |
|
|
endtask
|
156 |
|
|
|
157 |
|
|
protected task RotWord(inout byte unsigned x[0:3]);
|
158 |
|
|
byte unsigned tmp;
|
159 |
|
|
|
160 |
|
|
tmp = x[0];
|
161 |
|
|
x[0] = x[1];
|
162 |
|
|
x[1] = x[2];
|
163 |
|
|
x[2] = x[3];
|
164 |
|
|
x[3] = tmp;
|
165 |
|
|
endtask
|
166 |
|
|
|
167 |
|
|
protected function byte unsigned inv_sbox_transform(byte unsigned x);
|
168 |
|
|
// Inverse Sbox transform matrix
|
169 |
|
|
const byte unsigned inv_sbox[0:255] = {
|
170 |
|
|
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
|
171 |
|
|
// ===============================================================================================
|
172 |
|
|
/*0*/ 8'h52,8'h09,8'h6a,8'hd5,8'h30,8'h36,8'ha5,8'h38,8'hbf,8'h40,8'ha3,8'h9e,8'h81,8'hf3,8'hd7,8'hfb,
|
173 |
|
|
/*1*/ 8'h7c,8'he3,8'h39,8'h82,8'h9b,8'h2f,8'hff,8'h87,8'h34,8'h8e,8'h43,8'h44,8'hc4,8'hde,8'he9,8'hcb,
|
174 |
|
|
/*2*/ 8'h54,8'h7b,8'h94,8'h32,8'ha6,8'hc2,8'h23,8'h3d,8'hee,8'h4c,8'h95,8'h0b,8'h42,8'hfa,8'hc3,8'h4e,
|
175 |
|
|
/*3*/ 8'h08,8'h2e,8'ha1,8'h66,8'h28,8'hd9,8'h24,8'hb2,8'h76,8'h5b,8'ha2,8'h49,8'h6d,8'h8b,8'hd1,8'h25,
|
176 |
|
|
/*4*/ 8'h72,8'hf8,8'hf6,8'h64,8'h86,8'h68,8'h98,8'h16,8'hd4,8'ha4,8'h5c,8'hcc,8'h5d,8'h65,8'hb6,8'h92,
|
177 |
|
|
/*5*/ 8'h6c,8'h70,8'h48,8'h50,8'hfd,8'hed,8'hb9,8'hda,8'h5e,8'h15,8'h46,8'h57,8'ha7,8'h8d,8'h9d,8'h84,
|
178 |
|
|
/*6*/ 8'h90,8'hd8,8'hab,8'h00,8'h8c,8'hbc,8'hd3,8'h0a,8'hf7,8'he4,8'h58,8'h05,8'hb8,8'hb3,8'h45,8'h06,
|
179 |
|
|
/*7*/ 8'hd0,8'h2c,8'h1e,8'h8f,8'hca,8'h3f,8'h0f,8'h02,8'hc1,8'haf,8'hbd,8'h03,8'h01,8'h13,8'h8a,8'h6b,
|
180 |
|
|
/*8*/ 8'h3a,8'h91,8'h11,8'h41,8'h4f,8'h67,8'hdc,8'hea,8'h97,8'hf2,8'hcf,8'hce,8'hf0,8'hb4,8'he6,8'h73,
|
181 |
|
|
/*9*/ 8'h96,8'hac,8'h74,8'h22,8'he7,8'had,8'h35,8'h85,8'he2,8'hf9,8'h37,8'he8,8'h1c,8'h75,8'hdf,8'h6e,
|
182 |
|
|
/*a*/ 8'h47,8'hf1,8'h1a,8'h71,8'h1d,8'h29,8'hc5,8'h89,8'h6f,8'hb7,8'h62,8'h0e,8'haa,8'h18,8'hbe,8'h1b,
|
183 |
|
|
/*b*/ 8'hfc,8'h56,8'h3e,8'h4b,8'hc6,8'hd2,8'h79,8'h20,8'h9a,8'hdb,8'hc0,8'hfe,8'h78,8'hcd,8'h5a,8'hf4,
|
184 |
|
|
/*c*/ 8'h1f,8'hdd,8'ha8,8'h33,8'h88,8'h07,8'hc7,8'h31,8'hb1,8'h12,8'h10,8'h59,8'h27,8'h80,8'hec,8'h5f,
|
185 |
|
|
/*d*/ 8'h60,8'h51,8'h7f,8'ha9,8'h19,8'hb5,8'h4a,8'h0d,8'h2d,8'he5,8'h7a,8'h9f,8'h93,8'hc9,8'h9c,8'hef,
|
186 |
|
|
/*e*/ 8'ha0,8'he0,8'h3b,8'h4d,8'hae,8'h2a,8'hf5,8'hb0,8'hc8,8'heb,8'hbb,8'h3c,8'h83,8'h53,8'h99,8'h61,
|
187 |
|
|
/*f*/ 8'h17,8'h2b,8'h04,8'h7e,8'hba,8'h77,8'hd6,8'h26,8'he1,8'h69,8'h14,8'h63,8'h55,8'h21,8'h0c,8'h7d
|
188 |
|
|
};
|
189 |
|
|
inv_sbox_transform = inv_sbox[x];
|
190 |
|
|
endfunction
|
191 |
|
|
|
192 |
|
|
protected function byte unsigned sbox_transform(byte unsigned x);
|
193 |
|
|
// Sbox transform matrix
|
194 |
|
|
const byte unsigned sbox[0:255] = {
|
195 |
|
|
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
|
196 |
|
|
// ===============================================================================================
|
197 |
|
|
/*0*/ 8'h63,8'h7c,8'h77,8'h7b,8'hf2,8'h6b,8'h6f,8'hc5,8'h30,8'h01,8'h67,8'h2b,8'hfe,8'hd7,8'hab,8'h76,
|
198 |
|
|
/*1*/ 8'hca,8'h82,8'hc9,8'h7d,8'hfa,8'h59,8'h47,8'hf0,8'had,8'hd4,8'ha2,8'haf,8'h9c,8'ha4,8'h72,8'hc0,
|
199 |
|
|
/*2*/ 8'hb7,8'hfd,8'h93,8'h26,8'h36,8'h3f,8'hf7,8'hcc,8'h34,8'ha5,8'he5,8'hf1,8'h71,8'hd8,8'h31,8'h15,
|
200 |
|
|
/*3*/ 8'h04,8'hc7,8'h23,8'hc3,8'h18,8'h96,8'h05,8'h9a,8'h07,8'h12,8'h80,8'he2,8'heb,8'h27,8'hb2,8'h75,
|
201 |
|
|
/*4*/ 8'h09,8'h83,8'h2c,8'h1a,8'h1b,8'h6e,8'h5a,8'ha0,8'h52,8'h3b,8'hd6,8'hb3,8'h29,8'he3,8'h2f,8'h84,
|
202 |
|
|
/*5*/ 8'h53,8'hd1,8'h00,8'hed,8'h20,8'hfc,8'hb1,8'h5b,8'h6a,8'hcb,8'hbe,8'h39,8'h4a,8'h4c,8'h58,8'hcf,
|
203 |
|
|
/*6*/ 8'hd0,8'hef,8'haa,8'hfb,8'h43,8'h4d,8'h33,8'h85,8'h45,8'hf9,8'h02,8'h7f,8'h50,8'h3c,8'h9f,8'ha8,
|
204 |
|
|
/*7*/ 8'h51,8'ha3,8'h40,8'h8f,8'h92,8'h9d,8'h38,8'hf5,8'hbc,8'hb6,8'hda,8'h21,8'h10,8'hff,8'hf3,8'hd2,
|
205 |
|
|
/*8*/ 8'hcd,8'h0c,8'h13,8'hec,8'h5f,8'h97,8'h44,8'h17,8'hc4,8'ha7,8'h7e,8'h3d,8'h64,8'h5d,8'h19,8'h73,
|
206 |
|
|
/*9*/ 8'h60,8'h81,8'h4f,8'hdc,8'h22,8'h2a,8'h90,8'h88,8'h46,8'hee,8'hb8,8'h14,8'hde,8'h5e,8'h0b,8'hdb,
|
207 |
|
|
/*a*/ 8'he0,8'h32,8'h3a,8'h0a,8'h49,8'h06,8'h24,8'h5c,8'hc2,8'hd3,8'hac,8'h62,8'h91,8'h95,8'he4,8'h79,
|
208 |
|
|
/*b*/ 8'he7,8'hc8,8'h37,8'h6d,8'h8d,8'hd5,8'h4e,8'ha9,8'h6c,8'h56,8'hf4,8'hea,8'h65,8'h7a,8'hae,8'h08,
|
209 |
|
|
/*c*/ 8'hba,8'h78,8'h25,8'h2e,8'h1c,8'ha6,8'hb4,8'hc6,8'he8,8'hdd,8'h74,8'h1f,8'h4b,8'hbd,8'h8b,8'h8a,
|
210 |
|
|
/*d*/ 8'h70,8'h3e,8'hb5,8'h66,8'h48,8'h03,8'hf6,8'h0e,8'h61,8'h35,8'h57,8'hb9,8'h86,8'hc1,8'h1d,8'h9e,
|
211 |
|
|
/*e*/ 8'he1,8'hf8,8'h98,8'h11,8'h69,8'hd9,8'h8e,8'h94,8'h9b,8'h1e,8'h87,8'he9,8'hce,8'h55,8'h28,8'hdf,
|
212 |
|
|
/*f*/ 8'h8c,8'ha1,8'h89,8'h0d,8'hbf,8'he6,8'h42,8'h68,8'h41,8'h99,8'h2d,8'h0f,8'hb0,8'h54,8'hbb,8'h16
|
213 |
|
|
};
|
214 |
|
|
sbox_transform = sbox[x];
|
215 |
|
|
endfunction
|
216 |
|
|
|
217 |
|
|
protected task SubBytes;
|
218 |
|
|
for (int j=0; j<=3; j++)
|
219 |
|
|
for (int k=0; k<=3; k++) state[j][k] = sbox_transform(state[j][k]);
|
220 |
|
|
endtask
|
221 |
|
|
|
222 |
|
|
protected task InvSubBytes;
|
223 |
|
|
for (int j=0; j<=3; j++)
|
224 |
|
|
for (int k=0; k<=3; k++) state[j][k] = inv_sbox_transform(state[j][k]);
|
225 |
|
|
endtask
|
226 |
|
|
|
227 |
|
|
protected task SubWord(inout byte unsigned x[0:3]);
|
228 |
|
|
x[0] = sbox_transform(x[0]);
|
229 |
|
|
x[1] = sbox_transform(x[1]);
|
230 |
|
|
x[2] = sbox_transform(x[2]);
|
231 |
|
|
x[3] = sbox_transform(x[3]);
|
232 |
|
|
endtask
|
233 |
|
|
|
234 |
|
|
protected task InvShiftRows;
|
235 |
|
|
byte unsigned tmp_state[1:3][0:3]; // Row 0 of state is not shifted
|
236 |
|
|
|
237 |
|
|
for (int j=1; j<=3; j++)
|
238 |
|
|
for (int k=0; k<=3; k++) tmp_state[j][k] = state[j][(k+4-j)%4];
|
239 |
|
|
|
240 |
|
|
for (int j=1; j<=3; j++)
|
241 |
|
|
for (int k=0; k<=3; k++) state[j][k] = tmp_state[j][k];
|
242 |
|
|
endtask
|
243 |
|
|
|
244 |
|
|
protected function byte unsigned xtime(byte unsigned x);
|
245 |
|
|
// Multiplication by 2 over GF(256)
|
246 |
|
|
// Refer to FIPS-197 spec section 4.2.1 on definition of GF(256) multiplication
|
247 |
|
|
xtime = (x[7])? (x<<1) ^ 8'h1b : x<<1;
|
248 |
|
|
endfunction
|
249 |
|
|
|
250 |
|
|
protected function byte unsigned GFmul4(byte unsigned x);
|
251 |
|
|
// Multiply by 4 over GF(256)
|
252 |
|
|
// 4*x = 2*(2*x)
|
253 |
|
|
GFmul4 = xtime(xtime(x));
|
254 |
|
|
endfunction
|
255 |
|
|
|
256 |
|
|
protected function byte unsigned GFmul8(byte unsigned x);
|
257 |
|
|
// Multiply by 8 over GF(256)
|
258 |
|
|
// 8*x = 2*(4*x)
|
259 |
|
|
GFmul8 = xtime(GFmul4(x));
|
260 |
|
|
endfunction
|
261 |
|
|
|
262 |
|
|
protected function byte unsigned GFmul9(byte unsigned x);
|
263 |
|
|
// Multiply by 9 over GF(256)
|
264 |
|
|
// 9*x = 8*x + x
|
265 |
|
|
// Addition over GF(256) is xor
|
266 |
|
|
GFmul9 = GFmul8(x) ^ x;
|
267 |
|
|
endfunction
|
268 |
|
|
|
269 |
|
|
protected function byte unsigned GFmulb(byte unsigned x);
|
270 |
|
|
// Multiply by 0xb over GF(256)
|
271 |
|
|
// b*x = 8*x + 2*x +x
|
272 |
|
|
GFmulb = GFmul8(x) ^ xtime(x) ^ x;
|
273 |
|
|
endfunction
|
274 |
|
|
|
275 |
|
|
protected function byte unsigned GFmuld(byte unsigned x);
|
276 |
|
|
// Multiply by 0xd over GF(256)
|
277 |
|
|
// d*x = 8*x + 4*x + x
|
278 |
|
|
GFmuld = GFmul8(x) ^ GFmul4(x) ^ x;
|
279 |
|
|
endfunction
|
280 |
|
|
|
281 |
|
|
protected function byte unsigned GFmule(byte unsigned x);
|
282 |
|
|
// Multiply by 0xe over GF(256)
|
283 |
|
|
// e*x = 8*x + 4*x +2*x
|
284 |
|
|
GFmule = GFmul8(x) ^ GFmul4(x) ^ xtime(x);
|
285 |
|
|
endfunction
|
286 |
|
|
|
287 |
|
|
protected task InvMixColumns;
|
288 |
|
|
byte unsigned tmp_col[0:3];
|
289 |
|
|
|
290 |
|
|
for (int j=0; j<=3; j++)
|
291 |
|
|
begin
|
292 |
|
|
tmp_col[0] = GFmule(state[0][j]) ^ GFmulb(state[1][j]) ^ GFmuld(state[2][j]) ^ GFmul9(state[3][j]);
|
293 |
|
|
tmp_col[1] = GFmul9(state[0][j]) ^ GFmule(state[1][j]) ^ GFmulb(state[2][j]) ^ GFmuld(state[3][j]);
|
294 |
|
|
tmp_col[2] = GFmuld(state[0][j]) ^ GFmul9(state[1][j]) ^ GFmule(state[2][j]) ^ GFmulb(state[3][j]);
|
295 |
|
|
tmp_col[3] = GFmulb(state[0][j]) ^ GFmuld(state[1][j]) ^ GFmul9(state[2][j]) ^ GFmule(state[3][j]);
|
296 |
|
|
|
297 |
|
|
state[0][j] = tmp_col[0];
|
298 |
|
|
state[1][j] = tmp_col[1];
|
299 |
|
|
state[2][j] = tmp_col[2];
|
300 |
|
|
state[3][j] = tmp_col[3];
|
301 |
|
|
end
|
302 |
|
|
endtask
|
303 |
|
|
|
304 |
|
|
protected task AddRoundKey;
|
305 |
|
|
for (int j=0; j<=3; j++)
|
306 |
|
|
for (int k=0; k<=3; k++) state[k][j] ^= keysch[curr_round*4+j][k];
|
307 |
|
|
endtask
|
308 |
|
|
|
309 |
|
|
task run(int mode);
|
310 |
|
|
// Run inverse cipher rounds as defined in section 5.3 of FIPS-197 spec.
|
311 |
|
|
// mode=0 -> Run from current round to completion
|
312 |
|
|
// mode=1 -> Run 1 round only
|
313 |
|
|
// Both LoadCt() and KeyExpand() must be called first before calling run()
|
314 |
|
|
// to ensure the inverse cipher doesn't work on garbage.
|
315 |
|
|
|
316 |
|
|
// Only continue if ciphertext is loaded and there are unfinished round(s)
|
317 |
|
|
if (loaded & ~done)
|
318 |
|
|
do
|
319 |
|
|
begin
|
320 |
|
|
unique if (curr_round == Nr)
|
321 |
|
|
begin
|
322 |
|
|
`ifdef INTERNAL_DEBUG
|
323 |
|
|
$display("round[%2d].istart\t%h",Nr-curr_round,GetState);
|
324 |
|
|
$display("round[%2d].ik_sch\t%h",Nr-curr_round,GetCurrKsch);
|
325 |
|
|
`endif
|
326 |
|
|
|
327 |
|
|
done = 0;
|
328 |
|
|
AddRoundKey;
|
329 |
|
|
curr_round--;
|
330 |
|
|
end
|
331 |
|
|
else if ((curr_round <= Nr-1) && (curr_round >= 1))
|
332 |
|
|
begin
|
333 |
|
|
`ifdef INTERNAL_DEBUG
|
334 |
|
|
$display("round[%2d].istart\t%h",Nr-curr_round,GetState);
|
335 |
|
|
`endif
|
336 |
|
|
|
337 |
|
|
InvShiftRows;
|
338 |
|
|
`ifdef INTERNAL_DEBUG
|
339 |
|
|
$display("round[%2d].is_row\t%h",Nr-curr_round,GetState);
|
340 |
|
|
`endif
|
341 |
|
|
|
342 |
|
|
InvSubBytes;
|
343 |
|
|
`ifdef INTERNAL_DEBUG
|
344 |
|
|
$display("round[%2d].is_box\t%h",Nr-curr_round,GetState);
|
345 |
|
|
$display("round[%2d].ik_sch\t%h",Nr-curr_round,GetCurrKsch);
|
346 |
|
|
`endif
|
347 |
|
|
|
348 |
|
|
AddRoundKey;
|
349 |
|
|
`ifdef INTERNAL_DEBUG
|
350 |
|
|
$display("round[%2d].ik_add\t%h",Nr-curr_round,GetState);
|
351 |
|
|
`endif
|
352 |
|
|
|
353 |
|
|
InvMixColumns;
|
354 |
|
|
curr_round--;
|
355 |
|
|
end
|
356 |
|
|
else if (curr_round == 0)
|
357 |
|
|
begin
|
358 |
|
|
`ifdef INTERNAL_DEBUG
|
359 |
|
|
$display("round[%2d].istart\t%h",Nr-curr_round,GetState);
|
360 |
|
|
`endif
|
361 |
|
|
|
362 |
|
|
InvShiftRows;
|
363 |
|
|
`ifdef INTERNAL_DEBUG
|
364 |
|
|
$display("round[%2d].is_row\t%h",Nr-curr_round,GetState);
|
365 |
|
|
`endif
|
366 |
|
|
|
367 |
|
|
InvSubBytes;
|
368 |
|
|
`ifdef INTERNAL_DEBUG
|
369 |
|
|
$display("round[%2d].is_box\t%h",Nr-curr_round,GetState);
|
370 |
|
|
$display("round[%2d].ik_sch\t%h",Nr-curr_round,GetCurrKsch);
|
371 |
|
|
`endif
|
372 |
|
|
|
373 |
|
|
AddRoundKey;
|
374 |
|
|
`ifdef INTERNAL_DEBUG
|
375 |
|
|
$display("round[%2d].ioutput\t%h",Nr-curr_round,GetState);
|
376 |
|
|
`endif
|
377 |
|
|
|
378 |
|
|
done = 1; // Last round completed
|
379 |
|
|
loaded = 0;
|
380 |
|
|
end
|
381 |
|
|
|
382 |
|
|
if (mode == 1) break;
|
383 |
|
|
end
|
384 |
|
|
while (done == 0);
|
385 |
|
|
|
386 |
|
|
// Either ciphertext is not loaded or decryption has already completed
|
387 |
|
|
else $display("#Info : aes_decrypt_model::run() has nothing to do");
|
388 |
|
|
endtask
|
389 |
|
|
endclass // aes_decrypt_model
|
390 |
|
|
|
391 |
|
|
// The following types should be used for declaration of aes class objects in your source code.
|
392 |
|
|
// e.g. ....
|
393 |
|
|
// aes256_decrypt_t my_aes_decryptor;
|
394 |
|
|
// bit [0:127] pt;
|
395 |
|
|
// ....
|
396 |
|
|
// my_aes_decryptor = new;
|
397 |
|
|
// my_aes_decryptor.KeyExpand(256'h.......);
|
398 |
|
|
// my_aes_decryptor.LoadCt(128'h.........);
|
399 |
|
|
// my_aes_descryptor.run(0);
|
400 |
|
|
// pt = my_aes_descryptor.GetState();
|
401 |
|
|
|
402 |
|
|
typedef aes_decrypt_model #(.Nk(8),.Nr(14)) aes256_decrypt_t;
|
403 |
|
|
typedef aes_decrypt_model #(.Nk(6),.Nr(12)) aes192_decrypt_t;
|
404 |
|
|
typedef aes_decrypt_model #(.Nk(4),.Nr(10)) aes128_decrypt_t;
|