-
Notifications
You must be signed in to change notification settings - Fork 0
/
AES_Key.c
executable file
·314 lines (281 loc) · 9.6 KB
/
AES_Key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/******************************************************************************\
Copyright(C): Huazhong University of Science & Technology.
FileName: AES_Key.c
Author: Zhulinfeng
Version: 1.0
Date: 2014-3-1
Description: Some functions on keys.
Others:
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
\******************************************************************************/
/**include public head files**/
#include "Type.h"
#include "Sys.h"
#include "Err.h"
/**include module head files**/
#include "Length.h"
#include "Table.h"
#include "AES_Key.h"
/**function statements**/
PRIVATE void AES_Key_RotByte(INOUT byte *pbword);
PRIVATE void AES_Key_SubByte(INOUT byte *pbword);
PRIVATE void AES_Key_Rcon(INOUT byte *pbword, IN unsigned int iindex);
PRIVATE void AES_Key_Transpot(INOUT byte *pbroundKey, IN unsigned int ilength);
/**global values**/
PRIVATE const byte RCON[10][4] =
{
0x01,0x00,0x00,0x00,
0x02,0x00,0x00,0x00,
0x04,0x00,0x00,0x00,
0x08,0x00,0x00,0x00,
0x10,0x00,0x00,0x00,
0x20,0x00,0x00,0x00,
0x40,0x00,0x00,0x00,
0x80,0x00,0x00,0x00,
0x1b,0x00,0x00,0x00,
0x36,0x00,0x00,0x00
};
/*******************************************************************************
Func Name: AES_Key_Set
Date Created: 2014-3-2
Author: Zhulinfeng
Description: Get 16 byte key, set it in struct AES_Key_S.
Input: AES_Key_S *psKey Address of the instance of AES_Key_S.
byte *pbkey 16 bytes key.
unsigned int ilength The length of key, must be 16.
Output: AES_Key_S *psKey
Return: NONE
Caution: psKey should not be null, bkey must be 16 bytes,ilength must be
16, all these guaranteed by caller.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC void AES_Key_Set(INOUT struct AES_Key_S *psKey, IN byte *pbkey, IN unsigned int ilength)
{
assert(NULL != psKey);
assert(NULL != pbkey);
assert(AES_KEY_SIZE == ilength);
int icount = 0;
for(icount; icount < ilength; icount++)
{
psKey -> bkey[icount] = *(pbkey + icount);
}
}
/*******************************************************************************
Func Name: AES_Key_GetRoundKey
Date Created: 2014-3-16
Author: Zhulinfeng
Description: Get 16 byte round key.
Input: AES_Key_S *psKey Address of the instance of AES_Key_S.
unsigned int iindex Which round key, less than 11.
Output: NONE
Return: *byte The first address of the round key.
Caution: psKey should not be null, iindex smaller than 11, these
guaranteed by caller.
This function must be used after AES_Key_Make
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC byte* AES_Key_GetRoundKey(IN struct AES_Key_S *psKey, IN unsigned int iindex)
{
assert(NULL != psKey);
assert(iindex >=0 && iindex < AES_NR + 1);
return psKey -> bsubKeys[iindex];
}
/*******************************************************************************
Func Name: AES_Key_GetTransKey
Date Created: 2014-3-16
Author: Zhulinfeng
Description: Get 16 byte round key.
Input: AES_Key_S *psKey Address of the instance of AES_Key_S.
unsigned int iindex Which round key, less than 11.
Output: NONE
Return: *byte The first address of the round key.
Caution: psKey should not be null, iindex smaller than 11, these
guaranteed by caller.
This function must be used after AES_Key_Make
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC byte* AES_Key_GetTransKey(IN struct AES_Key_S *psKey, IN unsigned int iindex)
{
assert(NULL != psKey);
assert(iindex >=0 && iindex < AES_NR + 1);
return psKey -> btransKeys[iindex];
}
/*******************************************************************************
Func Name: AES_Key_Make
Date Created: 2014-3-3
Author: Zhulinfeng
Description: Make all the sub keys.
Input: AES_Key_S *psKey Address of the instance of AES_Key_S.
Output: AES_Key_S *psKey
Return: NONE
Caution: This function should be used after function AES_Key_Set.
This function should be used after AES_Key_Set.
All these guaranteed by caller.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC void AES_Key_Make(INOUT struct AES_Key_S *psKey)
{
assert(NULL != psKey);
byte *pbroundKey = psKey -> bsubKeys[0];
int icopyCount = 0;
for(icopyCount; icopyCount < AES_PAGE_SIZE; icopyCount++)
{
*(pbroundKey + icopyCount) = psKey -> bkey[icopyCount];
}
byte *pbtmpWord = pbroundKey + AES_PAGE_SIZE;
int iroundCount = AES_NK;
for(iroundCount; iroundCount < AES_NB*(AES_NR+1); iroundCount++)
{
int j = 0;
for(j; j < 4; j++)
{
*(pbtmpWord + j) = *(pbroundKey + 4*iroundCount + j - 4);
}
if(0 == iroundCount % AES_NK)
{
AES_Key_RotByte(pbtmpWord);
AES_Key_SubByte(pbtmpWord);
AES_Key_Rcon(pbtmpWord, iroundCount/AES_NK - 1);
}
int k = 0;
for(k; k < 4; k++)
{
*(pbtmpWord + k) ^= *(pbroundKey + (iroundCount-AES_NK)*4 + k);
}
pbtmpWord += 4;
}
byte *btmpdest = psKey -> btransKeys[0];
byte *btmpsour = psKey -> bsubKeys[0];
int jcopyCount = 0;
for(jcopyCount; jcopyCount < (AES_NR + 1) *AES_PAGE_SIZE; jcopyCount++)
{
*(btmpdest + jcopyCount) = *(btmpsour + jcopyCount);
}
int itransCount = 0;
for(itransCount; itransCount < AES_NR + 1; itransCount++)
{
AES_Key_Transpot(psKey-> btransKeys[itransCount], AES_KEY_SIZE);
}
return;
}
/*******************************************************************************
Func Name: AES_Key_RotByte
Date Created: 2014-3-16
Author: Zhulinfeng
Description: Rot byte.
Input: byte *pbword word to be roted
Output: byte *pbword
Return: NONE
Caution: pbword should not be null, these guaranteed by
caller.
Called by AES_Key_SubByte.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PRIVATE void AES_Key_RotByte(INOUT byte *pbword)
{
assert(NULL != pbword);
byte tmp = *pbword;
*(pbword) = *(pbword + 1);
*(pbword + 1) = *(pbword + 2);
*(pbword + 2) = *(pbword + 3);
*(pbword + 3) = tmp;
return;
}
/*******************************************************************************
Func Name: AES_Key_SubByte
Date Created: 2014-3-16
Author: Zhulinfeng
Description: subbyte.
Input: byte *pbword word to be sub byte
Output: byte *pbword
Return: NONE
Caution: pbword should no be null, guaranteed by caller.
Called by AES_Key_Make.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PRIVATE void AES_Key_SubByte(INOUT byte *pbword)
{
assert(NULL != pbword);
int icount = 0;
for(icount; icount < 4; icount++)
{
byte tmp = *(pbword + icount);
int irow = (tmp >> 4) & 0x0f;
int icol = tmp & 0x0f;
*(pbword + icount) = S_BOX[irow][icol];
}
return;
}
/*******************************************************************************
Func Name: AES_Key_Rcon
Date Created: 2014-3-16
Author: Zhulinfeng
Description: rcon.
Input: byte *pbword word to rcon
unsigned int iindex choose which rcon
Output: byte *pbword
Return: NONE
Caution: pbword should no be null, guaranteed by caller.
Called by AES_Key_Make.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PRIVATE void AES_Key_Rcon(INOUT byte *pbword, IN unsigned int iindex)
{
assert(NULL != pbword);
assert(iindex >= 0 && iindex < 10);
int icount = 0;
for(icount; icount < 4; icount++)
{
*(pbword + icount) ^= RCON[iindex][icount];
}
return;
}
PRIVATE void AES_Key_Transpot(INOUT byte *pbroundKey, IN unsigned int uilength)
{
assert(NULL != pbroundKey);
assert(AES_KEY_SIZE == uilength);
int irow = 0;
for(irow; irow < 4; irow++)
{
int icol = 0;
for(icol; icol < irow; icol++)
{
byte tmp = *(pbroundKey + irow*4 + icol);
*(pbroundKey + irow*4 + icol) = *(pbroundKey + icol*4 + irow);
*(pbroundKey + icol*4 + irow) = tmp;
}
}
return;
}