-
Notifications
You must be signed in to change notification settings - Fork 88
/
aes_ni.c
696 lines (595 loc) · 18.1 KB
/
aes_ni.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 03/08/2018
*/
#include "aes_ni.h"
#if defined( USE_INTEL_AES_IF_PRESENT )
#include <assert.h>
#if defined(_MSC_VER)
#include <intrin.h>
#pragma intrinsic(__cpuid)
#define INLINE __inline
INLINE int has_aes_ni(void)
{
static int test = -1;
if(test < 0)
{
int cpu_info[4];
__cpuid(cpu_info, 1);
test = cpu_info[2] & 0x02000000;
}
return test;
}
#elif defined( __GNUC__ )
#include <cpuid.h>
#pragma GCC target ("ssse3")
#pragma GCC target ("sse4.1")
#pragma GCC target ("aes")
#include <x86intrin.h>
#define INLINE static __inline
INLINE int has_aes_ni()
{
static int test = -1;
if(test < 0)
{
unsigned int a, b, c, d;
if(!__get_cpuid(1, &a, &b, &c, &d))
test = 0;
else
test = (c & 0x2000000);
}
return test;
}
#else
#error AES New Instructions require Microsoft, Intel, GNU C, or CLANG
#endif
INLINE __m128i aes_128_assist(__m128i t1, __m128i t2)
{
__m128i t3;
t2 = _mm_shuffle_epi32(t2, 0xff);
t3 = _mm_slli_si128(t1, 0x4);
t1 = _mm_xor_si128(t1, t3);
t3 = _mm_slli_si128(t3, 0x4);
t1 = _mm_xor_si128(t1, t3);
t3 = _mm_slli_si128(t3, 0x4);
t1 = _mm_xor_si128(t1, t3);
t1 = _mm_xor_si128(t1, t2);
return t1;
}
AES_RETURN aes_ni(encrypt_key128)(const unsigned char *key, aes_encrypt_ctx cx[1])
{
__m128i t1, t2;
__m128i *ks = (__m128i*)cx->ks;
if(!has_aes_ni())
{
return aes_xi(encrypt_key128)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
t1 = _mm_loadu_si128((__m128i*)key);
ks[0] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x1);
t1 = aes_128_assist(t1, t2);
ks[1] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x2);
t1 = aes_128_assist(t1, t2);
ks[2] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x4);
t1 = aes_128_assist(t1, t2);
ks[3] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x8);
t1 = aes_128_assist(t1, t2);
ks[4] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x10);
t1 = aes_128_assist(t1, t2);
ks[5] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x20);
t1 = aes_128_assist(t1, t2);
ks[6] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x40);
t1 = aes_128_assist(t1, t2);
ks[7] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x80);
t1 = aes_128_assist(t1, t2);
ks[8] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x1b);
t1 = aes_128_assist(t1, t2);
ks[9] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x36);
t1 = aes_128_assist(t1, t2);
ks[10] = t1;
cx->inf.l = 0;
cx->inf.b[0] = 10 * AES_BLOCK_SIZE;
return EXIT_SUCCESS;
}
INLINE void aes_192_assist(__m128i* t1, __m128i * t2, __m128i * t3)
{
__m128i t4;
*t2 = _mm_shuffle_epi32(*t2, 0x55);
t4 = _mm_slli_si128(*t1, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
*t1 = _mm_xor_si128(*t1, *t2);
*t2 = _mm_shuffle_epi32(*t1, 0xff);
t4 = _mm_slli_si128(*t3, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
*t3 = _mm_xor_si128(*t3, *t2);
}
AES_RETURN aes_ni(encrypt_key192)(const unsigned char *key, aes_encrypt_ctx cx[1])
{
__m128i t1, t2, t3;
__m128i *ks = (__m128i*)cx->ks;
if(!has_aes_ni())
{
return aes_xi(encrypt_key192)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
t1 = _mm_loadu_si128((__m128i*)key);
t3 = _mm_loadu_si128((__m128i*)(key + 16));
ks[0] = t1;
ks[1] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x1);
aes_192_assist(&t1, &t2, &t3);
ks[1] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[1]), _mm_castsi128_pd(t1), 0));
ks[2] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x2);
aes_192_assist(&t1, &t2, &t3);
ks[3] = t1;
ks[4] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x4);
aes_192_assist(&t1, &t2, &t3);
ks[4] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[4]), _mm_castsi128_pd(t1), 0));
ks[5] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x8);
aes_192_assist(&t1, &t2, &t3);
ks[6] = t1;
ks[7] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x10);
aes_192_assist(&t1, &t2, &t3);
ks[7] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[7]), _mm_castsi128_pd(t1), 0));
ks[8] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x20);
aes_192_assist(&t1, &t2, &t3);
ks[9] = t1;
ks[10] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x40);
aes_192_assist(&t1, &t2, &t3);
ks[10] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[10]), _mm_castsi128_pd(t1), 0));
ks[11] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x80);
aes_192_assist(&t1, &t2, &t3);
ks[12] = t1;
cx->inf.l = 0;
cx->inf.b[0] = 12 * AES_BLOCK_SIZE;
return EXIT_SUCCESS;
}
INLINE void aes_256_assist1(__m128i* t1, __m128i * t2)
{
__m128i t4;
*t2 = _mm_shuffle_epi32(*t2, 0xff);
t4 = _mm_slli_si128(*t1, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
*t1 = _mm_xor_si128(*t1, *t2);
}
INLINE void aes_256_assist2(__m128i* t1, __m128i * t3)
{
__m128i t2, t4;
t4 = _mm_aeskeygenassist_si128(*t1, 0x0);
t2 = _mm_shuffle_epi32(t4, 0xaa);
t4 = _mm_slli_si128(*t3, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
*t3 = _mm_xor_si128(*t3, t2);
}
AES_RETURN aes_ni(encrypt_key256)(const unsigned char *key, aes_encrypt_ctx cx[1])
{
__m128i t1, t2, t3;
__m128i *ks = (__m128i*)cx->ks;
if(!has_aes_ni())
{
return aes_xi(encrypt_key256)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
t1 = _mm_loadu_si128((__m128i*)key);
t3 = _mm_loadu_si128((__m128i*)(key + 16));
ks[0] = t1;
ks[1] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x01);
aes_256_assist1(&t1, &t2);
ks[2] = t1;
aes_256_assist2(&t1, &t3);
ks[3] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x02);
aes_256_assist1(&t1, &t2);
ks[4] = t1;
aes_256_assist2(&t1, &t3);
ks[5] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x04);
aes_256_assist1(&t1, &t2);
ks[6] = t1;
aes_256_assist2(&t1, &t3);
ks[7] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x08);
aes_256_assist1(&t1, &t2);
ks[8] = t1;
aes_256_assist2(&t1, &t3);
ks[9] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x10);
aes_256_assist1(&t1, &t2);
ks[10] = t1;
aes_256_assist2(&t1, &t3);
ks[11] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x20);
aes_256_assist1(&t1, &t2);
ks[12] = t1;
aes_256_assist2(&t1, &t3);
ks[13] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x40);
aes_256_assist1(&t1, &t2);
ks[14] = t1;
cx->inf.l = 0;
cx->inf.b[0] = 14 * AES_BLOCK_SIZE;
return EXIT_SUCCESS;
}
INLINE void enc_to_dec(aes_decrypt_ctx cx[1])
{
__m128i *ks = (__m128i*)cx->ks;
int j;
for( j = 1 ; j < (cx->inf.b[0] >> 4) ; ++j )
ks[j] = _mm_aesimc_si128(ks[j]);
}
AES_RETURN aes_ni(decrypt_key128)(const unsigned char *key, aes_decrypt_ctx cx[1])
{
if(!has_aes_ni())
{
return aes_xi(decrypt_key128)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
if(aes_ni(encrypt_key128)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
enc_to_dec(cx);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
AES_RETURN aes_ni(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1])
{
if(!has_aes_ni())
{
return aes_xi(decrypt_key192)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
if(aes_ni(encrypt_key192)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
enc_to_dec(cx);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
AES_RETURN aes_ni(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1])
{
if(!has_aes_ni())
{
return aes_xi(decrypt_key256)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
if(aes_ni(encrypt_key256)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
enc_to_dec(cx);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
AES_RETURN aes_ni(encrypt)(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
{
__m128i *key = (__m128i*)cx->ks, t;
if(cx->inf.b[0] != 10 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE && cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_xi(encrypt)(in, out, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
t = _mm_xor_si128(_mm_loadu_si128((__m128i*)in), *(__m128i*)key);
switch(cx->inf.b[0])
{
case 14 * AES_BLOCK_SIZE:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
case 12 * AES_BLOCK_SIZE:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
case 10 * AES_BLOCK_SIZE:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenclast_si128(t, *(__m128i*)++key);
}
_mm_storeu_si128(&((__m128i*)out)[0], t);
return EXIT_SUCCESS;
}
AES_RETURN aes_ni(decrypt)(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
{
__m128i *key = (__m128i*)cx->ks + (cx->inf.b[0] >> 4), t;
if(cx->inf.b[0] != 10 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE && cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_xi(decrypt)(in, out, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
t = _mm_xor_si128(_mm_loadu_si128((__m128i*)in), *(__m128i*)key);
switch(cx->inf.b[0])
{
case 14 * AES_BLOCK_SIZE:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
case 12 * AES_BLOCK_SIZE:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
case 10 * AES_BLOCK_SIZE:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdeclast_si128(t, *(__m128i*)--key);
}
_mm_storeu_si128((__m128i*)out, t);
return EXIT_SUCCESS;
}
#ifdef ADD_AESNI_MODE_CALLS
#ifdef USE_AES_CONTEXT
AES_RETURN aes_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
const aes_encrypt_ctx cx[1])
{
__m128i feedback, data, *key = (__m128i*)cx->ks;
int number_of_rounds = cx->inf.b[0] >> 4, j;
unsigned long i;
if(number_of_rounds != 10 && number_of_rounds != 12 && number_of_rounds != 14)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_cbc_encrypt(in, out, length, ivec, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
data = _mm_loadu_si128(&((__m128i*)in)[i]);
feedback = _mm_xor_si128(data, feedback);
feedback = _mm_xor_si128(feedback, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
feedback = _mm_aesenc_si128(feedback, ((__m128i*)key)[j]);
feedback = _mm_aesenclast_si128(feedback, ((__m128i*)key)[j]);
_mm_storeu_si128(&((__m128i*)out)[i], feedback);
}
return EXIT_SUCCESS;
}
AES_RETURN aes_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
const aes_decrypt_ctx cx[1])
{
__m128i data, feedback, last_in, *key = (__m128i*)cx->ks;
int number_of_rounds = cx->inf.b[0] >> 4, j;
unsigned long i;
if(number_of_rounds != 10 && number_of_rounds != 12 && number_of_rounds != 14)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_cbc_decrypt(in, out, length, ivec, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
last_in = _mm_loadu_si128(&((__m128i*)in)[i]);
data = _mm_xor_si128(last_in, ((__m128i*)key)[number_of_rounds]);
for(j = number_of_rounds - 1; j > 0; j--)
{
data = _mm_aesdec_si128(data, ((__m128i*)key)[j]);
}
data = _mm_aesdeclast_si128(data, ((__m128i*)key)[0]);
data = _mm_xor_si128(data, feedback);
_mm_storeu_si128(&((__m128i*)out)[i], data);
feedback = last_in;
}
return EXIT_SUCCESS;
}
static void ctr_inc(unsigned char *ctr_blk)
{
uint32_t c;
c = *(uint32_t*)(ctr_blk + 8);
c++;
*(uint32_t*)(ctr_blk + 8) = c;
if(!c)
*(uint32_t*)(ctr_blk + 12) = *(uint32_t*)(ctr_blk + 12) + 1;
}
AES_RETURN aes_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
unsigned long length,
const aes_encrypt_ctx cx[1])
{
__m128i ctr_block = { 0 }, *key = (__m128i*)cx->ks, tmp, ONE, BSWAP_EPI64;
int number_of_rounds = cx->inf.b[0] >> 4, j;
unsigned long i;
if(number_of_rounds != 10 && number_of_rounds != 12 && number_of_rounds != 14)
return EXIT_FAILURE;
if(!has_aes_ni())
{
unsigned char ctr_blk[16];
*(uint64_t*)ctr_blk = *(uint64_t*)ivec;
*(uint32_t*)(ctr_blk + 8) = *(uint32_t*)nonce;
return aes_ctr_crypt(in, out, length, (unsigned char*)ctr_blk, ctr_inc, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);
if(length % 16)
length = length / 16 + 1;
else length /= 16;
ONE = _mm_set_epi32(0, 1, 0, 0);
BSWAP_EPI64 = _mm_setr_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
#ifdef _MSC_VER
ctr_block = _mm_insert_epi64(ctr_block, *(long long*)ivec, 1);
#else
ctr_block = _mm_set_epi64(*(__m64*)ivec, *(__m64*)&ctr_block);
#endif
ctr_block = _mm_insert_epi32(ctr_block, *(long*)nonce, 1);
ctr_block = _mm_srli_si128(ctr_block, 4);
ctr_block = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
for(i = 0; i < length; i++)
{
tmp = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
tmp = _mm_xor_si128(tmp, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
{
tmp = _mm_aesenc_si128(tmp, ((__m128i*)key)[j]);
};
tmp = _mm_aesenclast_si128(tmp, ((__m128i*)key)[j]);
tmp = _mm_xor_si128(tmp, _mm_loadu_si128(&((__m128i*)in)[i]));
_mm_storeu_si128(&((__m128i*)out)[i], tmp);
}
return EXIT_SUCCESS;
}
#else
void aes_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds)
{
__m128i feedback, data;
unsigned long i;
int j;
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
data = _mm_loadu_si128(&((__m128i*)in)[i]);
feedback = _mm_xor_si128(data, feedback);
feedback = _mm_xor_si128(feedback, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
feedback = _mm_aesenc_si128(feedback, ((__m128i*)key)[j]);
feedback = _mm_aesenclast_si128(feedback, ((__m128i*)key)[j]);
_mm_storeu_si128(&((__m128i*)out)[i], feedback);
}
}
void aes_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds)
{
__m128i data, feedback, last_in;
unsigned long i;
int j;
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
last_in = _mm_loadu_si128(&((__m128i*)in)[i]);
data = _mm_xor_si128(last_in, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
{
data = _mm_aesdec_si128(data, ((__m128i*)key)[j]);
}
data = _mm_aesdeclast_si128(data, ((__m128i*)key)[j]);
data = _mm_xor_si128(data, feedback);
_mm_storeu_si128(&((__m128i*)out)[i], data);
feedback = last_in;
}
}
void aes_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
unsigned long length,
const unsigned char *key,
int number_of_rounds)
{
__m128i ctr_block = { 0 }, tmp, ONE, BSWAP_EPI64;
unsigned long i;
int j;
if(length % 16)
length = length / 16 + 1;
else length /= 16;
ONE = _mm_set_epi32(0, 1, 0, 0);
BSWAP_EPI64 = _mm_setr_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
#ifdef _MSC_VER
ctr_block = _mm_insert_epi64(ctr_block, *(long long*)ivec, 1);
#else
ctr_block = _mm_set_epi64(*(__m64*)ivec, *(__m64*)&ctr_block);
#endif
ctr_block = _mm_insert_epi32(ctr_block, *(long*)nonce, 1);
ctr_block = _mm_srli_si128(ctr_block, 4);
ctr_block = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
for(i = 0; i < length; i++)
{
tmp = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
tmp = _mm_xor_si128(tmp, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
{
tmp = _mm_aesenc_si128(tmp, ((__m128i*)key)[j]);
};
tmp = _mm_aesenclast_si128(tmp, ((__m128i*)key)[j]);
tmp = _mm_xor_si128(tmp, _mm_loadu_si128(&((__m128i*)in)[i]));
_mm_storeu_si128(&((__m128i*)out)[i], tmp);
}
}
#endif
#endif
#endif