-
Notifications
You must be signed in to change notification settings - Fork 18
/
AES.cpp
987 lines (869 loc) · 24.7 KB
/
AES.cpp
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/* AES - Advanced Encryption Standard
source version 1.0, June, 2005
Copyright (C) 2000-2005 Chris Lomont
This software is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Chris Lomont
chris@lomont.org
The AES Standard is maintained by NIST
http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
This legalese is patterned after the zlib compression library
*/
// code to implement Advanced Encryption Standard - Rijndael
// speed optimized version
#include "AES.h"
#include <stdio.h>
#include <assert.h>
#include <string>
#include <fstream>
#include <iostream>
//_rotl and _rotr definitions for non-windows compilers
#ifndef _WIN32
unsigned int _rotl(unsigned int value, int shift)
{
return (value<<shift)|(value>>(32-shift));
}
unsigned int _rotr(unsigned int value, int shift)
{
return (value>>shift)|(value<<(32-shift));
}
#endif //_WIN32
// todo - make faster 128 blocksize version with 128 blocksize hardcoded as necessary
// internally data is stored in the state in order
// 0 1 2 3
// 4 5 6 7
// 8 8 10 11
// ...
// up to Nb of these
// NOTE: thus rows and columns are interchanged from the paper
// TODO - test against the attack at http://cr.yp.to/mac/variability1.html and make fixes if necessary
using namespace std;
namespace { // anonymous namespace for local linkage
// tables for inverses, byte sub
unsigned char gf2_8_inv[256];
unsigned char byte_sub[256];
unsigned char inv_byte_sub[256];
// this table needs Nb*(Nr+1)/Nk entries - up to 8*(15)/4 = 60
// todo - remove table, note cycles every 17(?) elements
unsigned int Rcon[60];
// long tables for encryption stuff
unsigned int T0[256];
unsigned int T1[256];
unsigned int T2[256];
unsigned int T3[256];
// long tables for decryption stuff
unsigned int I0[256];
unsigned int I1[256];
unsigned int I2[256];
unsigned int I3[256];
// huge tables - todo - ifdef out
unsigned int T4[256];
unsigned int T5[256];
unsigned int T6[256];
unsigned int T7[256];
unsigned int I4[256];
unsigned int I5[256];
unsigned int I6[256];
unsigned int I7[256];
// have the tables been initialized?
bool tablesInitialized = false;
// define to mult a byte by x mod the proper poly
// todo - move magic numbers out?
#define xmult(a) ((a)<<1) ^ (((a)&128) ? 0x01B : 0)
// make 4 bytes (LSB first) into a 4 byte vector
#define VEC4(a,b,c,d) (((long)(a)) | (((long)(b))<<8) | (((long)(c))<<16) | (((long)(d))<<24))
// get byte 0 to 3 from word a
#define GetByte(a,n) ((unsigned char)((a) >> (n<<3)))
// bytes (a,b,c,d) -> (b,c,d,a) so low becomes high
#define RotByte(a) _rotr(a,8)
#define RotByteL(a) _rotl(a,8)
// mult 2 elements using gf2_8_poly as a reduction
inline unsigned char GF2_8_mult(unsigned char a, unsigned char b)
{ // todo - make 4x4 table for nibbles, use lookup
unsigned char result = 0;
// should give 0x57 . 0x13 = 0xFE with poly 0x11B
//
int count = 8;
while (count--)
{
if (b&1)
result ^= a;
if (a&128)
{
a <<= 1;
a ^= (0x1B);
}
else
a <<= 1;
b >>= 1;
}
return result;
} // GF2_8_mult
bool CheckLargeTables(bool create)
{
unsigned int i;
unsigned char a1,a2,a3,b1,b2,b3,b4,b5;
for (i = 0; i < 256; i++)
{
a1 = byte_sub[i];
a2 = xmult(a1);
a3 = a2^a1;
b5 = inv_byte_sub[i];
b1 = GF2_8_mult(0x0E,b5);
b2 = GF2_8_mult(0x09,b5);
b3 = GF2_8_mult(0x0D,b5);
b4 = GF2_8_mult(0x0B,b5);
if (create == true)
{
T0[i] = VEC4(a2,a1,a1,a3);
T1[i] = RotByteL(T0[i]);
T2[i] = RotByteL(T1[i]);
T3[i] = RotByteL(T2[i]);
T4[i] = VEC4(a1,0,0,0); // identity
T5[i] = RotByteL(T4[i]);
T6[i] = RotByteL(T5[i]);
T7[i] = RotByteL(T6[i]);
I0[i] = VEC4(b1,b2,b3,b4);
I1[i] = RotByteL(I0[i]);
I2[i] = RotByteL(I1[i]);
I3[i] = RotByteL(I2[i]);
I4[i] = VEC4(b5,0,0,0); // identity
I5[i] = RotByteL(I4[i]);
I6[i] = RotByteL(I5[i]);
I7[i] = RotByteL(I6[i]);
}
else
{
if (T0[i] != VEC4(a2,a1,a1,a3))
return false;
if (T1[i] != RotByteL(T0[i]))
return false;
if (T2[i] != RotByteL(T1[i]))
return false;
if (T3[i] != RotByteL(T2[i]))
return false;
if (T4[i] != VEC4(a1,0,0,0))
return false;
if (T5[i] != RotByteL(T4[i]))
return false;
if (T6[i] != RotByteL(T5[i]))
return false;
if (T7[i] != RotByteL(T6[i]))
return false;
if (I0[i] != VEC4(b1,b2,b3,b4))
return false;
if (I1[i] != RotByte(I0[i]))
return false;
if (I2[i] != RotByte(I1[i]))
return false;
if (I3[i] != RotByte(I2[i]))
return false;
if (I4[i] != VEC4(b5,0,0,0))
return false;
if (I5[i] != RotByteL(I4[i]))
return false;
if (I6[i] != RotByteL(I5[i]))
return false;
if (I7[i] != RotByteL(I6[i]))
return false;
}
}
return true;
} // CheckLargeTables
// some functions to create/verify table integrity
bool CheckInverses(bool create)
{
// we'll brute force the inverse table
assert(GF2_8_mult(0x57,0x13) == 0xFE); // test these first
assert(GF2_8_mult(0x01,0x01) == 0x01);
assert(GF2_8_mult(0xFF,0x55) == 0xF8);
unsigned int a,b; // need int here to prevent wraps in loop
if (create == true)
gf2_8_inv[0] = 0;
else if (gf2_8_inv[0] != 0)
return false;
for (a = 1; a <= 255; a++)
{
b = 1;
while (GF2_8_mult(a,b) != 1)
b++;
if (create == true)
gf2_8_inv[a] = b;
else if (gf2_8_inv[a] != b)
return false;
}
return true;
} // CheckInverses
unsigned char BitSum(unsigned char byte)
{ // return the sum of bits mod 2
byte = (byte>>4)^(byte&15);
byte = (byte>>2)^(byte&3);
return (byte>>1)^(byte&1);
} // BitSum
bool CheckByteSub(bool create)
{
if (CheckInverses(create) == false)
return false; // we cannot do this without inverses
unsigned int x,y; // need ints here to prevent wrap in loop
for (x = 0; x <= 255; x++)
{
y = gf2_8_inv[x]; // inverse to start with
// affine transform
y = BitSum(y&0xF1) | (BitSum(y&0xE3)<<1) | (BitSum(y&0xC7)<<2) | (BitSum(y&0x8F)<<3) |
(BitSum(y&0x1F)<<4) | (BitSum(y&0x3E)<<5) | (BitSum(y&0x7C)<<6) | (BitSum(y&0xF8)<<7);
y = y ^ 0x63;
if (create == true)
byte_sub[x] = y;
else if (byte_sub[x] != y)
return false;
}
return true;
} // CheckByteSub
bool CheckInvByteSub(bool create)
{
if (CheckInverses(create) == false)
return false; // we cannot do this without inverses
if (CheckByteSub(create) == false)
return false; // we cannot do this without byte_sub
unsigned int x,y; // need ints here to prevent wrap in loop
for (x = 0; x <= 255; x++)
{
// we brute force it...
y = 0;
while (byte_sub[y] != x)
y++;
if (create == true)
inv_byte_sub[x] = y;
else if (inv_byte_sub[x] != y)
return false;
}
return true;
} // CheckInvByteSub
bool CheckRcon(bool create)
{
unsigned char Ri = 1; // start here
if (create == true)
Rcon[0] = 0;
else if (Rcon[0] != 0)
return false; // todo - this is unused still check?
for (int i = 1; i < sizeof(Rcon)/sizeof(Rcon[0])-1; i++)
{
if (create == true)
Rcon[i] = Ri;
else if (Rcon[i] != Ri)
return false;
Ri = GF2_8_mult(Ri,0x02); // multiply by x - todo replace with xmult
}
return true;
} // CheckRCon
// key adding for 4,6,8 column cases
#define AddRoundKey4(dest,src) \
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;
#define AddRoundKey6(dest,src) \
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;
#define AddRoundKey8(dest,src) \
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;\
*dest++ = *r_ptr++ ^ *src++;
// this define computes one of the round vectors
#define compute_one(dest,src2,j,C1,C2,C3,Nb) *(dest+j) = \
T0[GetByte(src2[j],0)]^T1[GetByte(src2[((j+C1+Nb)%Nb)],1)]^ \
T2[GetByte(src2[((j+C2+Nb)%Nb)],2)]^T3[GetByte(src2[((j+C3+Nb)%Nb)],3)] \
^*r_ptr++
// single table version, slower
#define compute_one_small(dest,src2,j,C1,C2,C3,Nb) *(dest+j) = *r_ptr++^\
T0[GetByte(src2[j],0)]^\
RotByteL(T0[GetByte(src2[((j+C1+Nb)%Nb)],1)]^\
RotByteL(T0[GetByte(src2[((j+C2+Nb)%Nb)],2)]^\
RotByteL(T0[GetByte(src2[((j+C3+Nb)%Nb)],3)])))
#define Round4(d,s) \
compute_one(d,s,0,1,2,3,4); \
compute_one(d,s,1,1,2,3,4); \
compute_one(d,s,2,1,2,3,4); \
compute_one(d,s,3,1,2,3,4);
#define Round6(d,s) \
compute_one(d,s,0,1,2,3,6); \
compute_one(d,s,1,1,2,3,6); \
compute_one(d,s,2,1,2,3,6); \
compute_one(d,s,3,1,2,3,6); \
compute_one(d,s,4,1,2,3,6); \
compute_one(d,s,5,1,2,3,6);
#define Round8(d,s) \
compute_one(d,s,0,1,3,4,8); \
compute_one(d,s,1,1,3,4,8); \
compute_one(d,s,2,1,3,4,8); \
compute_one(d,s,3,1,3,4,8); \
compute_one(d,s,4,1,3,4,8); \
compute_one(d,s,5,1,3,4,8); \
compute_one(d,s,6,1,3,4,8); \
compute_one(d,s,7,1,3,4,8);
#define compute_one_inv(dest,src2,j,C1,C2,C3,Nb) *(dest+j) = \
I0[GetByte(src2[j],0)]^I1[GetByte(src2[((j-C1+Nb)%Nb)],1)]^ \
I2[GetByte(src2[((j-C2+Nb)%Nb)],2)]^I3[GetByte(src2[((j-C3+Nb)%Nb)],3)] \
^*r_ptr++
#define InvRound4(d,s) \
compute_one_inv(d,s,0,1,2,3,4); \
compute_one_inv(d,s,1,1,2,3,4); \
compute_one_inv(d,s,2,1,2,3,4); \
compute_one_inv(d,s,3,1,2,3,4);
#define InvRound6(d,s) \
compute_one_inv(d,s,0,1,2,3,6); \
compute_one_inv(d,s,1,1,2,3,6); \
compute_one_inv(d,s,2,1,2,3,6); \
compute_one_inv(d,s,3,1,2,3,6); \
compute_one_inv(d,s,4,1,2,3,6); \
compute_one_inv(d,s,5,1,2,3,6);
#define InvRound8(d,s) \
compute_one_inv(d,s,0,1,3,4,8); \
compute_one_inv(d,s,1,1,3,4,8); \
compute_one_inv(d,s,2,1,3,4,8); \
compute_one_inv(d,s,3,1,3,4,8); \
compute_one_inv(d,s,4,1,3,4,8); \
compute_one_inv(d,s,5,1,3,4,8); \
compute_one_inv(d,s,6,1,3,4,8); \
compute_one_inv(d,s,7,1,3,4,8);
// this define computes one of the final round vectors
#define compute_one_final1(dest,src,j,C1,C2,C3,Nb) *dest++ = \
(T3[GetByte(src[j],0)]&0xFF)^\
(T3[GetByte(src[((j+C1+Nb)%Nb)],1)]&0xFF00)^ \
(T1[GetByte(src[((j+C2+Nb)%Nb)],2)]&0xFF0000)^ \
(T1[GetByte(src[((j+C3+Nb)%Nb)],3)]&0xFF000000)^*r_ptr++
// for another 4K tables, we save 3 clock cycles - sick
#define compute_one_final(dest,src,j,C1,C2,C3,Nb) *dest++ = \
(T4[GetByte(src[j],0)])^\
(T5[GetByte(src[((j+C1+Nb)%Nb)],1)])^ \
(T6[GetByte(src[((j+C2+Nb)%Nb)],2)])^ \
(T7[GetByte(src[((j+C3+Nb)%Nb)],3)])^*r_ptr++
// final round defines - this one is for case for 4 columns
#define FinalRound4(d,s) compute_one_final(d,s,0,1,2,3,4); \
compute_one_final(d,s,1,1,2,3,4); \
compute_one_final(d,s,2,1,2,3,4); \
compute_one_final(d,s,3,1,2,3,4);
#define FinalRound6(d,s) compute_one_final(d,s,0,1,2,3,6); \
compute_one_final(d,s,1,1,2,3,6); \
compute_one_final(d,s,2,1,2,3,6); \
compute_one_final(d,s,3,1,2,3,6); \
compute_one_final(d,s,4,1,2,3,6); \
compute_one_final(d,s,5,1,2,3,6);
#define FinalRound8(d,s) compute_one_final(d,s,0,1,3,4,8); \
compute_one_final(d,s,1,1,3,4,8); \
compute_one_final(d,s,2,1,3,4,8); \
compute_one_final(d,s,3,1,3,4,8); \
compute_one_final(d,s,4,1,3,4,8); \
compute_one_final(d,s,5,1,3,4,8); \
compute_one_final(d,s,6,1,3,4,8); \
compute_one_final(d,s,7,1,3,4,8);
// inverse cipher stuff
#define compute_one_final_inv(dest,src,j,C1,C2,C3,Nb) *dest++ = \
(I4[GetByte(src[j],0)])^\
(I5[GetByte(src[((j-C1+Nb)%Nb)],1)])^ \
(I6[GetByte(src[((j-C2+Nb)%Nb)],2)])^ \
(I7[GetByte(src[((j-C3+Nb)%Nb)],3)])^*r_ptr++
// final round defines - this one is for case for 4 columns
#define InvFinalRound4(d,s) compute_one_final_inv(d,s,0,1,2,3,4); \
compute_one_final_inv(d,s,1,1,2,3,4); \
compute_one_final_inv(d,s,2,1,2,3,4); \
compute_one_final_inv(d,s,3,1,2,3,4);
#define InvFinalRound6(d,s) compute_one_final_inv(d,s,0,1,2,3,6); \
compute_one_final_inv(d,s,1,1,2,3,6); \
compute_one_final_inv(d,s,2,1,2,3,6); \
compute_one_final_inv(d,s,3,1,2,3,6); \
compute_one_final_inv(d,s,4,1,2,3,6); \
compute_one_final_inv(d,s,5,1,2,3,6);
#define InvFinalRound8(d,s) compute_one_final_inv(d,s,0,1,3,4,8); \
compute_one_final_inv(d,s,1,1,3,4,8); \
compute_one_final_inv(d,s,2,1,3,4,8); \
compute_one_final_inv(d,s,3,1,3,4,8); \
compute_one_final_inv(d,s,4,1,3,4,8); \
compute_one_final_inv(d,s,5,1,3,4,8); \
compute_one_final_inv(d,s,6,1,3,4,8); \
compute_one_final_inv(d,s,7,1,3,4,8);
unsigned int SubByte(unsigned int data)
{ // does the SBox on this 4 byte data
unsigned result = 0;
result = byte_sub[data>>24];
result <<= 8;
result |= byte_sub[(data>>16)&255];
result <<= 8;
result |= byte_sub[(data>>8)&255];
result <<= 8;
result |= byte_sub[data&255];
return result;
} // SubByte
void DumpCharTable(ostream & out, const char * name, const unsigned char * table, int length)
{ // dump the contents of a table to a file
int pos;
out << name << endl << hex;
for (pos = 0; pos < length; pos++)
{
out << "0x";
if (table[pos] < 16)
out << '0';
out << static_cast<unsigned int>(table[pos]) << ',';
if ((pos %16) == 15)
out << endl;
}
out << dec;
} // DumpCharTable
void DumpLongTable(ostream & out, const char * name, const unsigned int * table, int length)
{ // dump te contents of a table to a file
int pos;
out << name << endl << hex;
for (pos = 0; pos < length; pos++)
{
out << "0x";
if (table[pos] < 16)
out << '0';
if (table[pos] < 16*16)
out << '0';
if (table[pos] < 16*16*16)
out << '0';
if (table[pos] < 16*16*16*16)
out << '0';
if (table[pos] < 16*16*16*16*16)
out << '0';
if (table[pos] < 16*16*16*16*16*16)
out << '0';
if (table[pos] < 16*16*16*16*16*16*16)
out << '0';
out << static_cast<unsigned int>(table[pos]) << ',';
if ((pos % 8) == 7)
out << endl;
}
out << dec;
} // DumpCharTable
// return true iff tables are valid. create = true fills them in if not
bool CreateAESTables(bool create, bool create_file)
{
bool retval = true;
if (CheckInverses(create) == false)
retval = false;
if (CheckByteSub(create) == false)
retval = false;
if (CheckInvByteSub(create) == false)
retval = false;
if (CheckRcon(create) == false)
return false;
if (CheckLargeTables(create) == false)
return false;
if (create_file == true)
{ // dump tables
ofstream out;
out.open("Tables.dat");
if (out.is_open() == true)
{
DumpCharTable(out,"gf2_8_inv", gf2_8_inv, 256);
out << "\n\n";
DumpCharTable(out,"byte_sub", byte_sub, 256);
out << "\n\n";
DumpCharTable(out,"inv_byte_sub", inv_byte_sub, 256);
out << "\n\n";
DumpLongTable(out,"RCon", Rcon, 60);
out << "\n\n";
DumpLongTable(out,"T0", T0, 256);
out << "\n\n";
DumpLongTable(out,"T1", T1, 256);
out << "\n\n";
DumpLongTable(out,"T2", T2, 256);
out << "\n\n";
DumpLongTable(out,"T3", T3, 256);
out << "\n\n";
DumpLongTable(out,"T4", T4, 256);
out << "\n\n";
DumpLongTable(out,"I0", I0, 256);
out << "\n\n";
DumpLongTable(out,"I1", I1, 256);
out << "\n\n";
DumpLongTable(out,"I2", I2, 256);
out << "\n\n";
DumpLongTable(out,"I3", I3, 256);
out << "\n\n";
DumpLongTable(out,"I4", I4, 256);
out.close();
}
}
return retval;
} // CreateAESTables
void DumpHex(const unsigned char * table, int length)
{ // dump some hex values for debugging
int pos;
cerr << hex;
for (pos = 0; pos < length; pos++)
{
if (table[pos] < 16)
cerr << '0';
cerr << static_cast<unsigned int>(table[pos]) << ' ';
if ((pos %16) == 15)
cerr << endl;
}
cerr << dec;
} // DumpHex
}// end of anonymous namespace
// Key expansion code - makes local copy
void AES::KeyExpansion(const unsigned char * key)
{
assert(Nk > 0);
int i;
unsigned int temp, * Wb = reinterpret_cast<unsigned int*>(W); // todo not portable - Endian problems
if (Nk <= 6)
{
// todo - memcpy
for (i = 0; i < 4*Nk; i++)
W[i] = key[i];
for (i = Nk; i < Nb*(Nr+1); i++)
{
temp = Wb[i-1];
if ((i%Nk) == 0)
temp = SubByte(RotByte(temp)) ^ Rcon[i/Nk];
Wb[i] = Wb[i - Nk]^temp;
}
}
else
{
// todo - memcpy
for (i = 0; i < 4*Nk; i++)
W[i] = key[i];
for (i = Nk; i < Nb*(Nr+1); i++)
{
temp = Wb[i-1];
if ((i%Nk) == 0)
temp = SubByte(RotByte(temp)) ^ Rcon[i/Nk];
else if ((i%Nk) == 4)
temp = SubByte(temp);
Wb[i] = Wb[i - Nk]^temp;
}
}
} // KeyExpansion
void AES::SetParameters(int keylength, int blocklength)
{
Nk = Nr = Nb = 0; // default values
if ((keylength != 128) && (keylength != 192) && (keylength != 256))
return; // nothing - todo - throw error?
if ((blocklength != 128) && (blocklength != 192) && (blocklength != 256))
return; // nothing - todo - throw error?
static int const parameters[] = {
//Nk*32 128 192 256
10, 12, 14, // Nb*32 = 128
12, 12, 14, // Nb*32 = 192
14, 14, 14, // Nb*32 = 256
};
// legal parameters, so fire it up
Nk = keylength /32;
Nb = blocklength/32;
Nr = parameters[((Nk-4)/2 + 3*(Nb-4)/2)];
} // SetParameters
void AES::StartEncryption(const unsigned char * key)
{
KeyExpansion(key);
} // StartEncryption
void AES::EncryptBlock(const unsigned char * datain1, unsigned char * dataout1)
{ // todo ? allow in place encryption
// todo - clean up - lots of repeated macros
// we only encrypt one block from now on
unsigned int state[8*2]; // 2 buffers
unsigned int * r_ptr = reinterpret_cast<unsigned int*>(W);
unsigned int * dest = state;
unsigned int * src = state;
const unsigned int * datain = reinterpret_cast<const unsigned int*>(datain1);
unsigned int * dataout = reinterpret_cast<unsigned int*>(dataout1);
if (Nb == 4)
{
AddRoundKey4(dest,datain);
if (Nr == 14)
{
Round4(dest,src);
Round4(src,dest);
Round4(dest,src);
Round4(src,dest);
}
else if (Nr == 12)
{
Round4(dest,src);
Round4(src,dest);
}
Round4(dest,src);
Round4(src,dest);
Round4(dest,src);
Round4(src,dest);
Round4(dest,src);
Round4(src,dest);
Round4(dest,src);
Round4(src,dest);
Round4(dest,src);
FinalRound4(dataout,dest);
}
else if (Nb == 6)
{
AddRoundKey6(dest,datain);
if (Nr == 14)
{
Round6(dest,src);
Round6(src,dest);
}
Round6(dest,src);
Round6(src,dest);
Round6(dest,src);
Round6(src,dest);
Round6(dest,src);
Round6(src,dest);
Round6(dest,src);
Round6(src,dest);
Round6(dest,src);
Round6(src,dest);
Round6(dest,src);
FinalRound6(dataout,dest);
}
else // Nb == 8
{
AddRoundKey8(dest,datain);
Round8(dest,src);
Round8(src,dest);
Round8(dest,src);
Round8(src,dest);
Round8(dest,src);
Round8(src,dest);
Round8(dest,src);
Round8(src,dest);
Round8(dest,src);
Round8(src,dest);
Round8(dest,src);
Round8(src,dest);
Round8(dest,src);
FinalRound8(dataout,dest);
} // end switch on Nb
} // Encrypt
// call this to encrypt any size block
void AES::Encrypt(const unsigned char * datain, unsigned char * dataout, unsigned int numBlocks, BlockMode mode)
{
if (0 == numBlocks)
return;
unsigned int blocksize = Nb*4;
switch (mode)
{
case ECB :
while (numBlocks)
{
EncryptBlock(datain,dataout);
datain += blocksize;
dataout += blocksize;
--numBlocks;
}
break;
case CBC :
{
unsigned char buffer[64];
memset(buffer,0,sizeof(buffer)); // clear out - todo - allow setting the Initialization Vector - needed for security
while (numBlocks)
{
for (unsigned int pos = 0; pos < blocksize; ++pos)
buffer[pos] ^= *datain++;
EncryptBlock(buffer,dataout);
memcpy(buffer,dataout,blocksize);
dataout += blocksize;
--numBlocks;
}
}
break;
default :
assert(!"Unknown mode!");
break;
}
} // Encrypt
void AES::StartDecryption(const unsigned char * key)
{
KeyExpansion(key);
unsigned char a0,a1,a2,a3,b0,b1,b2,b3, * W_ptr = W;
for (int col = Nb; col < (Nr)*Nb; col++) // do all but first and last round
{
a0 = W_ptr[4*col+0];
a1 = W_ptr[4*col+1];
a2 = W_ptr[4*col+2];
a3 = W_ptr[4*col+3];
b0 = GF2_8_mult(0x0E,a0)^GF2_8_mult(0x0B,a1)^
GF2_8_mult(0x0D,a2)^GF2_8_mult(0x09,a3);
b1 = GF2_8_mult(0x09,a0)^GF2_8_mult(0x0E,a1)^
GF2_8_mult(0x0B,a2)^GF2_8_mult(0x0D,a3);
b2 = GF2_8_mult(0x0D,a0)^GF2_8_mult(0x09,a1)^
GF2_8_mult(0x0E,a2)^GF2_8_mult(0x0B,a3);
b3 = GF2_8_mult(0x0B,a0)^GF2_8_mult(0x0D,a1)^
GF2_8_mult(0x09,a2)^GF2_8_mult(0x0E,a3);
W_ptr[4*col+0] = b0;
W_ptr[4*col+1] = b1;
W_ptr[4*col+2] = b2;
W_ptr[4*col+3] = b3;
}
// we reverse the rounds to make decryption faster
unsigned int * WL = reinterpret_cast<unsigned int*>(W);
for (int pos = 0; pos < Nr/2; pos++)
for (int col = 0; col < Nb; col++)
swap(WL[col+pos*Nb],WL[col+(Nr-pos)*Nb]);
} // StartDecryption
void AES::DecryptBlock(const unsigned char * datain1, unsigned char * dataout1)
{
unsigned int state[8*2]; // 2 buffers
unsigned int * r_ptr = reinterpret_cast<unsigned int*>(W);
unsigned int * dest = state;
unsigned int * src = state;
const unsigned int * datain = reinterpret_cast<const unsigned int*>(datain1);
unsigned int * dataout = reinterpret_cast<unsigned int*>(dataout1);
if (Nb == 4)
{
AddRoundKey4(dest,datain);
if (Nr == 14)
{
InvRound4(dest,src);
InvRound4(src,dest);
InvRound4(dest,src);
InvRound4(src,dest);
}
else if (Nr == 12)
{
InvRound4(dest,src);
InvRound4(src,dest);
}
InvRound4(dest,src);
InvRound4(src,dest);
InvRound4(dest,src);
InvRound4(src,dest);
InvRound4(dest,src);
InvRound4(src,dest);
InvRound4(dest,src);
InvRound4(src,dest);
InvRound4(dest,src);
InvFinalRound4(dataout,dest);
}
else if (Nb == 6)
{
AddRoundKey6(dest,datain);
if (Nr == 14)
{
InvRound6(dest,src);
InvRound6(src,dest);
}
InvRound6(dest,src);
InvRound6(src,dest);
InvRound6(dest,src);
InvRound6(src,dest);
InvRound6(dest,src);
InvRound6(src,dest);
InvRound6(dest,src);
InvRound6(src,dest);
InvRound6(dest,src);
InvRound6(src,dest);
InvRound6(dest,src);
InvFinalRound6(dataout,dest);
}
else // Nb == 8
{
AddRoundKey8(dest,datain);
InvRound8(dest,src);
InvRound8(src,dest);
InvRound8(dest,src);
InvRound8(src,dest);
InvRound8(dest,src);
InvRound8(src,dest);
InvRound8(dest,src);
InvRound8(src,dest);
InvRound8(dest,src);
InvRound8(src,dest);
InvRound8(dest,src);
InvRound8(src,dest);
InvRound8(dest,src);
InvFinalRound8(dataout,dest);
} // end switch on Nb
} // Decrypt
// call this to decrypt any size block
void AES::Decrypt(const unsigned char * datain, unsigned char * dataout, unsigned int numBlocks, BlockMode mode)
{
if (0 == numBlocks)
return;
unsigned int blocksize = Nb*4;
switch (mode)
{
case ECB :
while (numBlocks)
{
DecryptBlock(datain,dataout);
datain += blocksize;
dataout += blocksize;
--numBlocks;
}
break;
case CBC :
{
unsigned char buffer[64];
memset(buffer,0,sizeof(buffer)); // clear out - todo - allow setting the Initialization Vector - needed for security
DecryptBlock(datain,dataout); // do first block
for (unsigned int pos = 0; pos < blocksize; ++pos)
*dataout++ ^= buffer[pos];
datain += blocksize;
numBlocks--;
while (numBlocks)
{
DecryptBlock(datain,dataout); // do first block
for (unsigned int pos = 0; pos < blocksize; ++pos)
*dataout++ ^= *(datain-blocksize+pos);
datain += blocksize;
--numBlocks;
}
}
break;
default :
assert(!"Unknown mode!");
}
} // Decrypt
void AES::TransformOFB(unsigned char* buffer, unsigned char* iv, int size){
unsigned char IV[16];
for(int i=0; i<16; i++){
IV[i] = iv[i%4];
}
int numBlocks = size/16 + (size%16 != 0);
unsigned char temp[16] = {0};
EncryptBlock(IV,temp);
unsigned char j = (size < 16) ? size : 16;
for (unsigned char pos = 0; pos < j; ++pos)
*buffer++ ^= temp[pos];
memcpy(IV, temp, 16);
numBlocks--;
while (numBlocks){
EncryptBlock(IV,temp);
j = (numBlocks == 1) ? size%16 : 16;
for (unsigned char pos = 0; pos < j; ++pos)
*buffer++ ^= temp[pos];
memcpy(IV, temp, 16);
--numBlocks;
}
}
// the constructor - makes sure local things are initialized
AES::AES(void)
{
if (false == tablesInitialized)
tablesInitialized = CreateAESTables(true,false);
if (false == tablesInitialized)
throw "Tables failed to initialize";
}
// end - AES.cpp