-
Notifications
You must be signed in to change notification settings - Fork 3
/
aes.c
468 lines (421 loc) · 11.1 KB
/
aes.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright (c) 2002, 2003, 2009 Bob Deblier
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*!\file aes.c
* \brief AES block cipher, as specified by NIST FIPS 197.
*
* The table lookup method was inspired by Brian Gladman's AES implementation,
* which is much more readable than the standard code.
*
* \author Bob Deblier <[email protected]>
* \ingroup BC_aes_m BC_m
*/
#define BEECRYPT_DLL_EXPORT
#if HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef OPTIMIZE_MMX
# include <mmintrin.h>
#endif
#include "beecrypt/aes.h"
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN)
# if (BYTE_ORDER != BIG_ENDIAN) && (BYTE_ORDER != LITTLE_ENDIAN)
# error unsupported endian-ness.
# endif
#endif
#if WORDS_BIGENDIAN
# include "beecrypt/aes_be.h"
#else
# include "beecrypt/aes_le.h"
#endif
#ifdef ASM_AESENCRYPTECB
extern int aesEncryptECB(aesParam*, uint32_t*, const uint32_t*, unsigned int);
#endif
#ifdef ASM_AESDECRYPTECB
extern int aesDecryptECB(aesParam*, uint32_t*, const uint32_t*, unsigned int);
#endif
#ifdef ASM_AESENCRYPTCBC
extern int aesEncryptCBC(aesParam*, uint32_t*, const uint32_t*, unsigned int);
#endif
#ifdef ASM_AESDECRYPTCBC
extern int aesDecryptCBC(aesParam*, uint32_t*, const uint32_t*, unsigned int);
#endif
#ifdef ASM_AESENCRYPTCTR
extern int aesEncryptCTR(aesParam*, uint32_t*, const uint32_t*, unsigned int);
#endif
#ifdef ASM_AESDECRYPTCTR
extern int aesDecryptCTR(aesParam*, uint32_t*, const uint32_t*, unsigned int);
#endif
const blockCipher aes = {
.name = "AES",
.paramsize = sizeof(aesParam),
.blocksize = 16,
.keybitsmin = 128,
.keybitsmax = 256,
.keybitsinc = 64,
.setup = (blockCipherSetup) aesSetup,
.setiv = (blockCipherSetIV) aesSetIV,
.setctr = (blockCipherSetCTR) aesSetCTR,
.getfb = (blockCipherFeedback) aesFeedback,
.raw =
{
.encrypt = (blockCipherRawcrypt) aesEncrypt,
.decrypt = (blockCipherRawcrypt) aesDecrypt
},
.ecb =
{
#ifdef ASM_AESENCRYPTECB
.encrypt = (blockCipherModcrypt) aesEncryptECB,
#else
.encrypt = (blockCipherModcrypt) 0,
#endif
#ifdef ASM_AESDECRYPTECB
.decrypt = (blockCipherModcrypt) aesDecryptECB,
#else
.decrypt = (blockCipherModcrypt) 0,
#endif
},
.cbc =
{
#ifdef ASM_AESENCRYPTCBC
.encrypt = (blockCipherModcrypt) aesEncryptCBC,
#else
.encrypt = (blockCipherModcrypt) 0,
#endif
#ifdef ASM_AESDECRYPTCBC
.decrypt = (blockCipherModcrypt) aesDecryptCBC,
#else
.decrypt = (blockCipherModcrypt) 0
#endif
},
.ctr =
{
#ifdef ASM_AESENCRYPTCTR
.encrypt = (blockCipherModcrypt) aesEncryptCTR,
#else
.encrypt = (blockCipherModcrypt) 0,
#endif
#ifdef ASM_AESDECRYPTCTR
.decrypt = (blockCipherModcrypt) aesDecryptCTR,
#else
.decrypt = (blockCipherModcrypt) 0
#endif
}
};
int aesSetup(aesParam* ap, const byte* key, size_t keybits, cipherOperation op)
{
if ((op != ENCRYPT) && (op != DECRYPT))
return -1;
if (((keybits & 63) == 0) && (keybits >= 128) && (keybits <= 256))
{
register uint32_t* rk, t, i, j;
/* clear fdback/iv */
ap->fdback[0] = 0;
ap->fdback[1] = 0;
ap->fdback[2] = 0;
ap->fdback[3] = 0;
ap->nr = 6 + (keybits >> 5);
rk = ap->k;
memcpy(rk, key, keybits >> 3);
i = 0;
if (keybits == 128)
{
while (1)
{
t = rk[3];
#if WORDS_BIGENDIAN
t = (_ae4[(t >> 16) & 0xff] & 0xff000000) ^
(_ae4[(t >> 8) & 0xff] & 0x00ff0000) ^
(_ae4[(t ) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 24) ] & 0x000000ff) ^
_arc[i];
#else
t = (_ae4[(t >> 8) & 0xff] & 0x000000ff) ^
(_ae4[(t >> 16) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 24) ] & 0x00ff0000) ^
(_ae4[(t ) & 0xff] & 0xff000000) ^
_arc[i];
#endif
rk[4] = (t ^= rk[0]);
rk[5] = (t ^= rk[1]);
rk[6] = (t ^= rk[2]);
rk[7] = (t ^= rk[3]);
if (++i == 10)
break;
rk += 4;
}
}
else if (keybits == 192)
{
while (1)
{
t = rk[5];
#if WORDS_BIGENDIAN
t = (_ae4[(t >> 16) & 0xff] & 0xff000000) ^
(_ae4[(t >> 8) & 0xff] & 0x00ff0000) ^
(_ae4[(t ) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 24) ] & 0x000000ff) ^
_arc[i];
#else
t = (_ae4[(t >> 8) & 0xff] & 0x000000ff) ^
(_ae4[(t >> 16) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 24) ] & 0x00ff0000) ^
(_ae4[(t ) & 0xff] & 0xff000000) ^
_arc[i];
#endif
rk[6] = (t ^= rk[0]);
rk[7] = (t ^= rk[1]);
rk[8] = (t ^= rk[2]);
rk[9] = (t ^= rk[3]);
if (++i == 8)
break;
rk[10] = (t ^= rk[4]);
rk[11] = (t ^= rk[5]);
rk += 6;
}
}
else if (keybits == 256)
{
while (1)
{
t = rk[7];
#if WORDS_BIGENDIAN
t = (_ae4[(t >> 16) & 0xff] & 0xff000000) ^
(_ae4[(t >> 8) & 0xff] & 0x00ff0000) ^
(_ae4[(t ) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 24) ] & 0x000000ff) ^
_arc[i];
#else
t = (_ae4[(t >> 8) & 0xff] & 0x000000ff) ^
(_ae4[(t >> 16) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 24) ] & 0x00ff0000) ^
(_ae4[(t ) & 0xff] & 0xff000000) ^
_arc[i];
#endif
rk[8] = (t ^= rk[0]);
rk[9] = (t ^= rk[1]);
rk[10] = (t ^= rk[2]);
rk[11] = (t ^= rk[3]);
if (++i == 7)
break;
#if WORDS_BIGENDIAN
t = (_ae4[(t >> 24) ] & 0xff000000) ^
(_ae4[(t >> 16) & 0xff] & 0x00ff0000) ^
(_ae4[(t >> 8) & 0xff] & 0x0000ff00) ^
(_ae4[(t ) & 0xff] & 0x000000ff);
#else
t = (_ae4[(t ) & 0xff] & 0x000000ff) ^
(_ae4[(t >> 8) & 0xff] & 0x0000ff00) ^
(_ae4[(t >> 16) & 0xff] & 0x00ff0000) ^
(_ae4[(t >> 24) ] & 0xff000000);
#endif
rk[12] = (t ^= rk[4]);
rk[13] = (t ^= rk[5]);
rk[14] = (t ^= rk[6]);
rk[15] = (t ^= rk[7]);
rk += 8;
}
}
if (op == DECRYPT)
{
rk = ap->k;
for (i = 0, j = (ap->nr << 2); i < j; i += 4, j -= 4)
{
t = rk[i ]; rk[i ] = rk[j ]; rk[j ] = t;
t = rk[i+1]; rk[i+1] = rk[j+1]; rk[j+1] = t;
t = rk[i+2]; rk[i+2] = rk[j+2]; rk[j+2] = t;
t = rk[i+3]; rk[i+3] = rk[j+3]; rk[j+3] = t;
}
for (i = 1; i < ap->nr; i++)
{
rk += 4;
#if WORDS_BIGENDIAN
rk[0] =
_ad0[_ae4[(rk[0] >> 24) ] & 0xff] ^
_ad1[_ae4[(rk[0] >> 16) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[0] >> 8) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[0] ) & 0xff] & 0xff];
rk[1] =
_ad0[_ae4[(rk[1] >> 24) ] & 0xff] ^
_ad1[_ae4[(rk[1] >> 16) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[1] >> 8) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[1] ) & 0xff] & 0xff];
rk[2] =
_ad0[_ae4[(rk[2] >> 24) ] & 0xff] ^
_ad1[_ae4[(rk[2] >> 16) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[2] >> 8) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[2] ) & 0xff] & 0xff];
rk[3] =
_ad0[_ae4[(rk[3] >> 24) ] & 0xff] ^
_ad1[_ae4[(rk[3] >> 16) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[3] >> 8) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[3] ) & 0xff] & 0xff];
#else
rk[0] =
_ad0[_ae4[(rk[0] ) & 0xff] & 0xff] ^
_ad1[_ae4[(rk[0] >> 8) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[0] >> 16) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[0] >> 24) ] & 0xff];
rk[1] =
_ad0[_ae4[(rk[1] ) & 0xff] & 0xff] ^
_ad1[_ae4[(rk[1] >> 8) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[1] >> 16) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[1] >> 24) ] & 0xff];
rk[2] =
_ad0[_ae4[(rk[2] ) & 0xff] & 0xff] ^
_ad1[_ae4[(rk[2] >> 8) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[2] >> 16) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[2] >> 24) ] & 0xff];
rk[3] =
_ad0[_ae4[(rk[3] ) & 0xff] & 0xff] ^
_ad1[_ae4[(rk[3] >> 8) & 0xff] & 0xff] ^
_ad2[_ae4[(rk[3] >> 16) & 0xff] & 0xff] ^
_ad3[_ae4[(rk[3] >> 24) ] & 0xff];
#endif
}
}
return 0;
}
return -1;
}
#ifndef ASM_AESSETIV
int aesSetIV(aesParam* ap, const byte* iv)
{
if (iv)
memcpy(ap->fdback, iv, 16);
else
memset(ap->fdback, 0, 16);
return 0;
}
#endif
#ifndef ASM_AESSETCTR
int aesSetCTR(aesParam* ap, const byte* nivz, size_t counter)
{
unsigned int blockwords = MP_BYTES_TO_WORDS(16);
if (nivz)
{
mpw tmp[MP_BYTES_TO_WORDS(16)];
os2ip((mpw*) ap->fdback, blockwords, nivz, 16);
mpsetws(blockwords, tmp, counter);
mpadd(blockwords, (mpw*) ap->fdback, tmp);
}
else
mpsetws(blockwords, (mpw*) ap->fdback, counter);
return 0;
}
#endif
#ifndef ASM_AESENCRYPT
int aesEncrypt(aesParam* ap, uint32_t* dst, const uint32_t* src)
{
#if defined (OPTIMIZE_MMX) && (defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686))
register __m64 s0, s1, s2, s3;
register __m64 t0, t1, t2, t3;
register uint32_t i0, i1, i2, i3;
#else
register uint32_t s0, s1, s2, s3;
register uint32_t t0, t1, t2, t3;
#endif
register uint32_t* rk = ap->k;
#if defined (OPTIMIZE_MMX) && (defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686))
s0 = _mm_cvtsi32_si64(src[0] ^ rk[0]);
s1 = _mm_cvtsi32_si64(src[1] ^ rk[1]);
s2 = _mm_cvtsi32_si64(src[2] ^ rk[2]);
s3 = _mm_cvtsi32_si64(src[3] ^ rk[3]);
#else
s0 = src[0] ^ rk[0];
s1 = src[1] ^ rk[1];
s2 = src[2] ^ rk[2];
s3 = src[3] ^ rk[3];
#endif
etfs(4); /* round 1 */
esft(8); /* round 2 */
etfs(12); /* round 3 */
esft(16); /* round 4 */
etfs(20); /* round 5 */
esft(24); /* round 6 */
etfs(28); /* round 7 */
esft(32); /* round 8 */
etfs(36); /* round 9 */
if (ap->nr > 10)
{
esft(40); /* round 10 */
etfs(44); /* round 11 */
if (ap->nr > 12)
{
esft(48); /* round 12 */
etfs(52); /* round 13 */
}
}
rk += (ap->nr << 2);
elr(); /* last round */
#if defined(OPTIMIZE_MMX) && (defined(OPTIMIZE_I586) || defined(OPTIMIZE_I686))
dst[0] = _mm_cvtsi64_si32(s0);
dst[1] = _mm_cvtsi64_si32(s1);
dst[2] = _mm_cvtsi64_si32(s2);
dst[3] = _mm_cvtsi64_si32(s3);
#else
dst[0] = s0;
dst[1] = s1;
dst[2] = s2;
dst[3] = s3;
#endif
return 0;
}
#endif
#ifndef ASM_AESDECRYPT
int aesDecrypt(aesParam* ap, uint32_t* dst, const uint32_t* src)
{
register uint32_t s0, s1, s2, s3;
register uint32_t t0, t1, t2, t3;
register uint32_t* rk = ap->k;
s0 = src[0] ^ rk[0];
s1 = src[1] ^ rk[1];
s2 = src[2] ^ rk[2];
s3 = src[3] ^ rk[3];
dtfs(4); /* round 1 */
dsft(8); /* round 2 */
dtfs(12); /* round 3 */
dsft(16); /* round 4 */
dtfs(20); /* round 5 */
dsft(24); /* round 6 */
dtfs(28); /* round 7 */
dsft(32); /* round 8 */
dtfs(36); /* round 9 */
if (ap->nr > 10)
{
dsft(40); /* round 10 */
dtfs(44); /* round 11 */
if (ap->nr > 12)
{
dsft(48); /* round 12 */
dtfs(52); /* round 13 */
}
}
rk += (ap->nr << 2);
dlr(); /* last round */
dst[0] = s0;
dst[1] = s1;
dst[2] = s2;
dst[3] = s3;
return 0;
}
#endif
uint32_t* aesFeedback(aesParam* ap)
{
return ap->fdback;
}