1 |
13 |
jcastillo |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// AES subbytes module implementation ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This file is part of the SystemC AES ////
|
6 |
|
|
//// ////
|
7 |
|
|
//// Description: ////
|
8 |
|
|
//// Subbytes stage implementation for AES algorithm ////
|
9 |
|
|
//// ////
|
10 |
|
|
//// To Do: ////
|
11 |
|
|
//// - done ////
|
12 |
|
|
//// ////
|
13 |
|
|
//// Author(s): ////
|
14 |
|
|
//// - Javier Castillo, jcastilo@opencores.org ////
|
15 |
|
|
//// ////
|
16 |
|
|
//////////////////////////////////////////////////////////////////////
|
17 |
|
|
//// ////
|
18 |
|
|
//// Copyright (C) 2000 Authors and OPENCORES.ORG ////
|
19 |
|
|
//// ////
|
20 |
|
|
//// This source file may be used and distributed without ////
|
21 |
|
|
//// restriction provided that this copyright statement is not ////
|
22 |
|
|
//// removed from the file and that any derivative work contains ////
|
23 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
24 |
|
|
//// ////
|
25 |
|
|
//// This source file is free software; you can redistribute it ////
|
26 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
27 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
28 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
29 |
|
|
//// later version. ////
|
30 |
|
|
//// ////
|
31 |
|
|
//// This source is distributed in the hope that it will be ////
|
32 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
33 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
34 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
35 |
|
|
//// details. ////
|
36 |
|
|
//// ////
|
37 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
38 |
|
|
//// Public License along with this source; if not, download it ////
|
39 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
40 |
|
|
//// ////
|
41 |
|
|
//////////////////////////////////////////////////////////////////////
|
42 |
|
|
//
|
43 |
|
|
// CVS Revision History
|
44 |
|
|
//
|
45 |
|
|
// $Log: not supported by cvs2svn $
|
46 |
|
|
// Revision 1.2 2004/08/30 14:44:44 jcastillo
|
47 |
|
|
// Code Formater used to give better appearance to SystemC code
|
48 |
|
|
//
|
49 |
|
|
// Revision 1.1.1.1 2004/07/05 09:46:22 jcastillo
|
50 |
|
|
// First import
|
51 |
|
|
//
|
52 |
|
|
|
53 |
|
|
#include "subbytes.h"
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
void subbytes::sub()
|
57 |
|
|
{
|
58 |
|
|
|
59 |
|
|
sc_biguint<128> data_i_var, data_reg_128;
|
60 |
|
|
sc_uint<8> data_array[16], data_reg_var[16];
|
61 |
|
|
|
62 |
|
|
#define assign_array_to_128() \
|
63 |
|
|
{ \
|
64 |
|
|
data_reg_128.range(127,120)=data_reg_var[0]; \
|
65 |
|
|
data_reg_128.range(119,112)=data_reg_var[1]; \
|
66 |
|
|
data_reg_128.range(111,104)=data_reg_var[2]; \
|
67 |
|
|
data_reg_128.range(103,96)=data_reg_var[3]; \
|
68 |
|
|
data_reg_128.range(95,88)=data_reg_var[4]; \
|
69 |
|
|
data_reg_128.range(87,80)=data_reg_var[5]; \
|
70 |
|
|
data_reg_128.range(79,72)=data_reg_var[6]; \
|
71 |
|
|
data_reg_128.range(71,64)=data_reg_var[7]; \
|
72 |
|
|
data_reg_128.range(63,56)=data_reg_var[8]; \
|
73 |
|
|
data_reg_128.range(55,48)=data_reg_var[9]; \
|
74 |
|
|
data_reg_128.range(47,40)=data_reg_var[10]; \
|
75 |
|
|
data_reg_128.range(39,32)=data_reg_var[11]; \
|
76 |
|
|
data_reg_128.range(31,24)=data_reg_var[12]; \
|
77 |
|
|
data_reg_128.range(23,16)=data_reg_var[13]; \
|
78 |
|
|
data_reg_128.range(15,8)=data_reg_var[14]; \
|
79 |
|
|
data_reg_128.range(7,0)=data_reg_var[15]; \
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
#define shift_array_to_128() \
|
83 |
|
|
{ \
|
84 |
|
|
data_reg_128.range(127,120)=data_reg_var[0]; \
|
85 |
|
|
data_reg_128.range(119,112)=data_reg_var[5]; \
|
86 |
|
|
data_reg_128.range(111,104)=data_reg_var[10]; \
|
87 |
|
|
data_reg_128.range(103,96)=data_reg_var[15]; \
|
88 |
|
|
data_reg_128.range(95,88)=data_reg_var[4]; \
|
89 |
|
|
data_reg_128.range(87,80)=data_reg_var[9]; \
|
90 |
|
|
data_reg_128.range(79,72)=data_reg_var[14]; \
|
91 |
|
|
data_reg_128.range(71,64)=data_reg_var[3]; \
|
92 |
|
|
data_reg_128.range(63,56)=data_reg_var[8]; \
|
93 |
|
|
data_reg_128.range(55,48)=data_reg_var[13]; \
|
94 |
|
|
data_reg_128.range(47,40)=data_reg_var[2]; \
|
95 |
|
|
data_reg_128.range(39,32)=data_reg_var[7]; \
|
96 |
|
|
data_reg_128.range(31,24)=data_reg_var[12]; \
|
97 |
|
|
data_reg_128.range(23,16)=data_reg_var[1]; \
|
98 |
|
|
data_reg_128.range(15,8)=data_reg_var[6]; \
|
99 |
|
|
data_reg_128.range(7,0)=data_reg_var[11]; \
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
#define invert_shift_array_to_128() \
|
103 |
|
|
{ \
|
104 |
|
|
data_reg_128.range(127,120)=data_reg_var[0]; \
|
105 |
|
|
data_reg_128.range(119,112)=data_reg_var[13]; \
|
106 |
|
|
data_reg_128.range(111,104)=data_reg_var[10]; \
|
107 |
|
|
data_reg_128.range(103,96)=data_reg_var[7]; \
|
108 |
|
|
data_reg_128.range(95,88)=data_reg_var[4]; \
|
109 |
|
|
data_reg_128.range(87,80)=data_reg_var[1]; \
|
110 |
|
|
data_reg_128.range(79,72)=data_reg_var[14]; \
|
111 |
|
|
data_reg_128.range(71,64)=data_reg_var[11]; \
|
112 |
|
|
data_reg_128.range(63,56)=data_reg_var[8]; \
|
113 |
|
|
data_reg_128.range(55,48)=data_reg_var[5]; \
|
114 |
|
|
data_reg_128.range(47,40)=data_reg_var[2]; \
|
115 |
|
|
data_reg_128.range(39,32)=data_reg_var[15]; \
|
116 |
|
|
data_reg_128.range(31,24)=data_reg_var[12]; \
|
117 |
|
|
data_reg_128.range(23,16)=data_reg_var[9]; \
|
118 |
|
|
data_reg_128.range(15,8)=data_reg_var[6]; \
|
119 |
|
|
data_reg_128.range(7,0)=data_reg_var[3]; \
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
data_i_var = data_i.read();
|
123 |
|
|
|
124 |
|
|
data_array[0] = data_i_var.range(127, 120);
|
125 |
|
|
data_array[1] = data_i_var.range(119, 112);
|
126 |
|
|
data_array[2] = data_i_var.range(111, 104);
|
127 |
|
|
data_array[3] = data_i_var.range(103, 96);
|
128 |
|
|
data_array[4] = data_i_var.range(95, 88);
|
129 |
|
|
data_array[5] = data_i_var.range(87, 80);
|
130 |
|
|
data_array[6] = data_i_var.range(79, 72);
|
131 |
|
|
data_array[7] = data_i_var.range(71, 64);
|
132 |
|
|
data_array[8] = data_i_var.range(63, 56);
|
133 |
|
|
data_array[9] = data_i_var.range(55, 48);
|
134 |
|
|
data_array[10] = data_i_var.range(47, 40);
|
135 |
|
|
data_array[11] = data_i_var.range(39, 32);
|
136 |
|
|
data_array[12] = data_i_var.range(31, 24);
|
137 |
|
|
data_array[13] = data_i_var.range(23, 16);
|
138 |
|
|
data_array[14] = data_i_var.range(15, 8);
|
139 |
|
|
data_array[15] = data_i_var.range(7, 0);
|
140 |
|
|
|
141 |
|
|
data_reg_var[0] = data_reg.read().range(127, 120);
|
142 |
|
|
data_reg_var[1] = data_reg.read().range(119, 112);
|
143 |
|
|
data_reg_var[2] = data_reg.read().range(111, 104);
|
144 |
|
|
data_reg_var[3] = data_reg.read().range(103, 96);
|
145 |
|
|
data_reg_var[4] = data_reg.read().range(95, 88);
|
146 |
|
|
data_reg_var[5] = data_reg.read().range(87, 80);
|
147 |
|
|
data_reg_var[6] = data_reg.read().range(79, 72);
|
148 |
|
|
data_reg_var[7] = data_reg.read().range(71, 64);
|
149 |
|
|
data_reg_var[8] = data_reg.read().range(63, 56);
|
150 |
|
|
data_reg_var[9] = data_reg.read().range(55, 48);
|
151 |
|
|
data_reg_var[10] = data_reg.read().range(47, 40);
|
152 |
|
|
data_reg_var[11] = data_reg.read().range(39, 32);
|
153 |
|
|
data_reg_var[12] = data_reg.read().range(31, 24);
|
154 |
|
|
data_reg_var[13] = data_reg.read().range(23, 16);
|
155 |
|
|
data_reg_var[14] = data_reg.read().range(15, 8);
|
156 |
|
|
data_reg_var[15] = data_reg.read().range(7, 0);
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
sbox_decrypt_o.write(decrypt_i.read());
|
160 |
|
|
sbox_data_o.write(0);
|
161 |
|
|
next_state.write(state.read());
|
162 |
|
|
next_data_reg.write(data_reg.read());
|
163 |
|
|
|
164 |
|
|
next_ready_o.write(0);
|
165 |
|
|
data_o.write(data_reg.read());
|
166 |
|
|
|
167 |
|
|
switch (state.read())
|
168 |
|
|
{
|
169 |
|
|
|
170 |
|
|
case 0:
|
171 |
|
|
if (start_i.read())
|
172 |
|
|
{
|
173 |
|
|
sbox_data_o.write(data_array[0]);
|
174 |
|
|
next_state.write(1);
|
175 |
|
|
}
|
176 |
|
|
break;
|
177 |
|
|
case 16:
|
178 |
|
|
data_reg_var[15] = sbox_data_i.read();
|
179 |
|
|
//Make shift rows stage
|
180 |
|
|
switch (decrypt_i.read())
|
181 |
|
|
{
|
182 |
|
|
case 0:
|
183 |
|
|
shift_array_to_128();
|
184 |
|
|
break;
|
185 |
|
|
case 1:
|
186 |
|
|
invert_shift_array_to_128();
|
187 |
|
|
break;
|
188 |
|
|
}
|
189 |
|
|
next_data_reg.write(data_reg_128);
|
190 |
|
|
next_ready_o.write(1);
|
191 |
|
|
next_state.write(0);
|
192 |
|
|
break;
|
193 |
|
|
default:
|
194 |
|
|
sbox_data_o.write(data_array[(int)state.read()]);
|
195 |
|
|
data_reg_var[(int)state.read()-1] = sbox_data_i.read();
|
196 |
|
|
assign_array_to_128();
|
197 |
|
|
next_data_reg.write(data_reg_128);
|
198 |
|
|
next_state.write(state.read() + 1);
|
199 |
|
|
break;
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
void subbytes::registers()
|
204 |
|
|
{
|
205 |
|
|
if (!reset.read())
|
206 |
|
|
{
|
207 |
|
|
data_reg.write(0);
|
208 |
|
|
state.write(0);
|
209 |
|
|
ready_o.write(0);
|
210 |
|
|
}
|
211 |
|
|
else
|
212 |
|
|
{
|
213 |
|
|
data_reg.write(next_data_reg.read());
|
214 |
|
|
state.write(next_state.read());
|
215 |
|
|
ready_o.write(next_ready_o.read());
|
216 |
|
|
}
|
217 |
|
|
}
|