-
Notifications
You must be signed in to change notification settings - Fork 88
/
aes_x86_v2.asm
1396 lines (1179 loc) · 35.8 KB
/
aes_x86_v2.asm
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ---------------------------------------------------------------------------
; 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: 20/11/2013
;
; This code requires either ASM_X86_V2 or ASM_X86_V2C to be set in aesopt.h
; and the same define to be set here as well. If AES_V2C is set this file
; requires the C files aeskey.c and aestab.c for support.
; An AES implementation for x86 processors using the YASM (or NASM) assembler.
; This is a full assembler implementation covering encryption, decryption and
; key scheduling. It uses 2k bytes of tables but its encryption and decryption
; performance is very close to that obtained using large tables. Key schedule
; expansion is slower for both encryption and decryption but this is likely to
; be offset by the much smaller load that this version places on the processor
; cache. I acknowledge the contribution made by Daniel Bernstein to aspects of
; the design of the AES round function used here.
;
; This code provides the standard AES block size (128 bits, 16 bytes) and the
; three standard AES key sizes (128, 192 and 256 bits). It has the same call
; interface as my C implementation. The ebx, esi, edi and ebp registers are
; preserved across calls but eax, ecx and edx and the artihmetic status flags
; are not. Although this is a full assembler implementation, it can be used
; in conjunction with my C code which provides faster key scheduling using
; large tables. In this case aeskey.c should be compiled with ASM_X86_V2C
; defined. It is also important that the defines below match those used in the
; C code. This code uses the VC++ register saving conentions; if it is used
; with another compiler, conventions for using and saving registers may need
; to be checked (and calling conventions). The YASM command line for the VC++
; custom build step is:
;
; yasm -Xvc -f win32 -D <Z> -o "$(TargetDir)\$(InputName).obj" "$(InputPath)"
;
; For the cryptlib build this is (pcg):
;
; yasm -Xvc -f win32 -D ASM_X86_V2C -o aescrypt2.obj aes_x86_v2.asm
;
; where <Z> is ASM_X86_V2 or ASM_X86_V2C. The calling intefaces are:
;
; AES_RETURN aes_encrypt(const unsigned char in_blk[],
; unsigned char out_blk[], const aes_encrypt_ctx cx[1]);
;
; AES_RETURN aes_decrypt(const unsigned char in_blk[],
; unsigned char out_blk[], const aes_decrypt_ctx cx[1]);
;
; AES_RETURN aes_encrypt_key<NNN>(const unsigned char key[],
; const aes_encrypt_ctx cx[1]);
;
; AES_RETURN aes_decrypt_key<NNN>(const unsigned char key[],
; const aes_decrypt_ctx cx[1]);
;
; AES_RETURN aes_encrypt_key(const unsigned char key[],
; unsigned int len, const aes_decrypt_ctx cx[1]);
;
; AES_RETURN aes_decrypt_key(const unsigned char key[],
; unsigned int len, const aes_decrypt_ctx cx[1]);
;
; where <NNN> is 128, 102 or 256. In the last two calls the length can be in
; either bits or bytes.
; Use of this assembler code in Windows kernel mode requires memory paging
; to be disabled
%ifdef NO_PAGING
%define set_page nopage
%else
%define set_page
%endif
; The DLL interface must use the _stdcall convention in which the number
; of bytes of parameter space is added after an @ to the sutine's name.
; We must also remove our parameters from the stack before return (see
; the do_exit macro). Define DLL_EXPORT for the Dynamic Link Library version.
;%define DLL_EXPORT
; Comment in/out the following lines to obtain the desired subroutines. These
; selections MUST match those in the C header file aes.h
; The size of the code can be reduced by using functions for the encryption
; and decryption rounds in place of macro expansion
%define REDUCE_CODE_SIZE
%define AES_128 ; define if AES with 128 bit keys is needed
%define AES_192 ; define if AES with 192 bit keys is needed
%define AES_256 ; define if AES with 256 bit keys is needed
%define AES_VAR ; define if a variable key size is needed
%define ENCRYPTION ; define if encryption is needed
%define DECRYPTION ; define if decryption is needed
%define AES_REV_DKS ; define if key decryption schedule is reversed
%ifndef ASM_X86_V2C
%define ENCRYPTION_KEY_SCHEDULE ; define if encryption key expansion is needed
%define DECRYPTION_KEY_SCHEDULE ; define if decryption key expansion is needed
%endif
; The encryption key schedule has the following in memory layout where N is the
; number of rounds (10, 12 or 14):
;
; lo: | input key (round 0) | ; each round is four 32-bit words
; | encryption round 1 |
; | encryption round 2 |
; ....
; | encryption round N-1 |
; hi: | encryption round N |
;
; The decryption key schedule is normally set up so that it has the same
; layout as above by actually reversing the order of the encryption key
; schedule in memory (this happens when AES_REV_DKS is set):
;
; lo: | decryption round 0 | = | encryption round N |
; | decryption round 1 | = INV_MIX_COL[ | encryption round N-1 | ]
; | decryption round 2 | = INV_MIX_COL[ | encryption round N-2 | ]
; .... ....
; | decryption round N-1 | = INV_MIX_COL[ | encryption round 1 | ]
; hi: | decryption round N | = | input key (round 0) |
;
; with rounds except the first and last modified using inv_mix_column()
; But if AES_REV_DKS is NOT set the order of keys is left as it is for
; encryption so that it has to be accessed in reverse when used for
; decryption (although the inverse mix column modifications are done)
;
; lo: | decryption round 0 | = | input key (round 0) |
; | decryption round 1 | = INV_MIX_COL[ | encryption round 1 | ]
; | decryption round 2 | = INV_MIX_COL[ | encryption round 2 | ]
; .... ....
; | decryption round N-1 | = INV_MIX_COL[ | encryption round N-1 | ]
; hi: | decryption round N | = | encryption round N |
;
; This layout is faster when the assembler key scheduling provided here
; is used.
;
; End of user defines
section .text align=32 set_page
%ifdef AES_VAR
%ifndef AES_128
%define AES_128
%endif
%ifndef AES_192
%define AES_192
%endif
%ifndef AES_256
%define AES_256
%endif
%endif
%ifdef AES_VAR
%define KS_LENGTH 60
%elifdef AES_256
%define KS_LENGTH 60
%elifdef AES_192
%define KS_LENGTH 52
%else
%define KS_LENGTH 44
%endif
%ifdef REDUCE_CODE_SIZE
%macro mf_call 1
call %1
%endmacro
%else
%macro mf_call 1
%1
%endmacro
%endif
; the DLL has to implement the _stdcall calling interface on return
; In this case we have to take our parameters (3 4-byte pointers)
; off the stack
%define parms 12
%macro do_name 1-2 parms
%ifndef DLL_EXPORT
global %1
%1:
%else
global %1@%2
export %1@%2
%1@%2:
%endif
%endmacro
%macro do_call 1-2 parms
%ifndef DLL_EXPORT
call %1
add esp,%2
%else
call %1@%2
%endif
%endmacro
%macro do_exit 0-1 parms
%ifdef DLL_EXPORT
ret %1
%else
ret
%endif
%endmacro
%ifndef ASM_X86_V2C
do_name _aes_init,0
do_exit 0
%endif
; finite field multiplies by {02}, {04} and {08}
%define f2(x) ((x<<1)^(((x>>7)&1)*0x11b))
%define f4(x) ((x<<2)^(((x>>6)&1)*0x11b)^(((x>>6)&2)*0x11b))
%define f8(x) ((x<<3)^(((x>>5)&1)*0x11b)^(((x>>5)&2)*0x11b)^(((x>>5)&4)*0x11b))
; finite field multiplies required in table generation
%define f3(x) (f2(x) ^ x)
%define f9(x) (f8(x) ^ x)
%define fb(x) (f8(x) ^ f2(x) ^ x)
%define fd(x) (f8(x) ^ f4(x) ^ x)
%define fe(x) (f8(x) ^ f4(x) ^ f2(x))
%define etab_0(x) [enc_tab+4+8*x]
%define etab_1(x) [enc_tab+3+8*x]
%define etab_2(x) [enc_tab+2+8*x]
%define etab_3(x) [enc_tab+1+8*x]
%define etab_b(x) byte [enc_tab+1+8*x] ; used with movzx for 0x000000xx
%define etab_w(x) word [enc_tab+8*x] ; used with movzx for 0x0000xx00
%define btab_0(x) [enc_tab+6+8*x]
%define btab_1(x) [enc_tab+5+8*x]
%define btab_2(x) [enc_tab+4+8*x]
%define btab_3(x) [enc_tab+3+8*x]
; ROUND FUNCTION. Build column[2] on ESI and column[3] on EDI that have the
; round keys pre-loaded. Build column[0] in EBP and column[1] in EBX.
;
; Input:
;
; EAX column[0]
; EBX column[1]
; ECX column[2]
; EDX column[3]
; ESI column key[round][2]
; EDI column key[round][3]
; EBP scratch
;
; Output:
;
; EBP column[0] unkeyed
; EBX column[1] unkeyed
; ESI column[2] keyed
; EDI column[3] keyed
; EAX scratch
; ECX scratch
; EDX scratch
%macro rnd_fun 2
rol ebx,16
%1 esi, cl, 0, ebp
%1 esi, dh, 1, ebp
%1 esi, bh, 3, ebp
%1 edi, dl, 0, ebp
%1 edi, ah, 1, ebp
%1 edi, bl, 2, ebp
%2 ebp, al, 0, ebp
shr ebx,16
and eax,0xffff0000
or eax,ebx
shr edx,16
%1 ebp, ah, 1, ebx
%1 ebp, dh, 3, ebx
%2 ebx, dl, 2, ebx
%1 ebx, ch, 1, edx
%1 ebx, al, 0, edx
shr eax,16
shr ecx,16
%1 ebp, cl, 2, edx
%1 edi, ch, 3, edx
%1 esi, al, 2, edx
%1 ebx, ah, 3, edx
%endmacro
; Basic MOV and XOR Operations for normal rounds
%macro nr_xor 4
movzx %4,%2
xor %1,etab_%3(%4)
%endmacro
%macro nr_mov 4
movzx %4,%2
mov %1,etab_%3(%4)
%endmacro
; Basic MOV and XOR Operations for last round
%if 1
%macro lr_xor 4
movzx %4,%2
movzx %4,etab_b(%4)
%if %3 != 0
shl %4,8*%3
%endif
xor %1,%4
%endmacro
%macro lr_mov 4
movzx %4,%2
movzx %1,etab_b(%4)
%if %3 != 0
shl %1,8*%3
%endif
%endmacro
%else ; less effective but worth leaving as an option
%macro lr_xor 4
movzx %4,%2
mov %4,btab_%3(%4)
and %4,0x000000ff << 8 * %3
xor %1,%4
%endmacro
%macro lr_mov 4
movzx %4,%2
mov %1,btab_%3(%4)
and %1,0x000000ff << 8 * %3
%endmacro
%endif
; Apply S-Box to the 4 bytes in a 32-bit word and rotate byte positions
%ifdef REDUCE_CODE_SIZE
global _ls_sub
_ls_sub: ; ls_sub(t,n) = ls_box(t,n)
mov ecx,[esp+8]
mov eax,[esp+4]
shl ecx,3
rol eax,cl
xor edx,edx
l3s_col:
movzx ecx,al ; in eax
movzx ecx, etab_b(ecx) ; out eax
xor edx,ecx ; scratch ecx,edx
movzx ecx,ah
movzx ecx, etab_b(ecx)
shl ecx,8
xor edx,ecx
shr eax,16
movzx ecx,al
movzx ecx, etab_b(ecx)
shl ecx,16
xor edx,ecx
movzx ecx,ah
movzx ecx, etab_b(ecx)
shl ecx,24
xor edx,ecx
mov eax,edx
ret
%else
%macro l3s_col 0
movzx ecx,al ; in eax
movzx ecx, etab_b(ecx) ; out eax
xor edx,ecx ; scratch ecx,edx
movzx ecx,ah
movzx ecx, etab_b(ecx)
shl ecx,8
xor edx,ecx
shr eax,16
movzx ecx,al
movzx ecx, etab_b(ecx)
shl ecx,16
xor edx,ecx
movzx ecx,ah
movzx ecx, etab_b(ecx)
shl ecx,24
xor edx,ecx
mov eax,edx
%endmacro
%endif
; offsets to parameters
in_blk equ 4 ; input byte array address parameter
out_blk equ 8 ; output byte array address parameter
ctx equ 12 ; AES context structure
stk_spc equ 16 ; stack space
%ifdef ENCRYPTION
%define ENCRYPTION_TABLE
%macro _enc_round 0
add ebp,16
mov esi,[ebp+8]
mov edi,[ebp+12]
push ebp
rnd_fun nr_xor, nr_mov
mov eax,ebp
pop ebp
mov ecx,esi
mov edx,edi
xor eax,[ebp]
xor ebx,[ebp+4]
%endmacro
%ifdef REDUCE_CODE_SIZE
enc_round:
_enc_round
ret
%else
%macro enc_round 0
_enc_round
%endmacro
%endif
%macro enc_last_round 0
add ebp,16
mov esi,[ebp+8]
mov edi,[ebp+12]
push ebp
rnd_fun lr_xor, lr_mov
mov eax,ebp
pop ebp
xor eax,[ebp]
xor ebx,[ebp+4]
%endmacro
; AES Encryption Subroutine
align 32
do_name _aes_encrypt,12
push ebp
push ebx
push esi
push edi
; load the input block
mov esi,[esp+in_blk+stk_spc] ; input pointer
mov eax,[esi ]
mov ebx,[esi+ 4]
mov ecx,[esi+ 8]
mov edx,[esi+12]
; and xor in first key block
mov ebp,[esp+ctx+stk_spc] ; key pointer
movzx edi,byte [ebp+4*KS_LENGTH]
xor eax,[ebp ]
xor ebx,[ebp+ 4]
xor ecx,[ebp+ 8]
xor edx,[ebp+12]
; determine the number of rounds
cmp edi,10*16
je .3
cmp edi,12*16
je .2
cmp edi,14*16
je .1
mov eax,-1
jmp .5
; do the encryption rounds
.1: mf_call enc_round
mf_call enc_round
.2: mf_call enc_round
mf_call enc_round
.3: mf_call enc_round
mf_call enc_round
mf_call enc_round
mf_call enc_round
mf_call enc_round
mf_call enc_round
mf_call enc_round
mf_call enc_round
mf_call enc_round
enc_last_round
; output the block
mov edx,[esp+out_blk+stk_spc]
mov [edx ],eax
mov [edx+ 4],ebx
mov [edx+ 8],esi
mov [edx+12],edi
xor eax,eax
.5: pop edi
pop esi
pop ebx
pop ebp
do_exit 12
%endif
%macro f_key 2
push ecx
push edx
mov edx,esi
ror eax,8
mf_call l3s_col
mov esi,eax
pop edx
pop ecx
xor esi,rc_val
mov [ebp+%1*%2],esi
xor edi,esi
mov [ebp+%1*%2+4],edi
xor ecx,edi
mov [ebp+%1*%2+8],ecx
xor edx,ecx
mov [ebp+%1*%2+12],edx
mov eax,edx
%if %2 == 24
%if %1 < 7
xor eax,[ebp+%1*%2+16-%2]
mov [ebp+%1*%2+16],eax
xor eax,[ebp+%1*%2+20-%2]
mov [ebp+%1*%2+20],eax
%endif
%elif %2 == 32
%if %1 < 6
push ecx
push edx
mov edx,[ebp+%1*%2+16-%2]
mf_call l3s_col
pop edx
pop ecx
mov [ebp+%1*%2+16],eax
xor eax,[ebp+%1*%2+20-%2]
mov [ebp+%1*%2+20],eax
xor eax,[ebp+%1*%2+24-%2]
mov [ebp+%1*%2+24],eax
xor eax,[ebp+%1*%2+28-%2]
mov [ebp+%1*%2+28],eax
%endif
%endif
%assign rc_val f2(rc_val)
%endmacro
%ifdef ENCRYPTION_KEY_SCHEDULE
%ifdef AES_128
%ifndef ENCRYPTION_TABLE
%define ENCRYPTION_TABLE
%endif
%assign rc_val 1
align 32
do_name _aes_encrypt_key128,8
push ebp
push ebx
push esi
push edi
mov ebp,[esp+24]
mov [ebp+4*KS_LENGTH],dword 10*16
mov ebx,[esp+20]
mov esi,[ebx]
mov [ebp],esi
mov edi,[ebx+4]
mov [ebp+4],edi
mov ecx,[ebx+8]
mov [ebp+8],ecx
mov edx,[ebx+12]
mov [ebp+12],edx
add ebp,16
mov eax,edx
f_key 0,16 ; 11 * 4 = 44 unsigned longs
f_key 1,16 ; 4 + 4 * 10 generated = 44
f_key 2,16
f_key 3,16
f_key 4,16
f_key 5,16
f_key 6,16
f_key 7,16
f_key 8,16
f_key 9,16
pop edi
pop esi
pop ebx
pop ebp
xor eax,eax
do_exit 8
%endif
%ifdef AES_192
%ifndef ENCRYPTION_TABLE
%define ENCRYPTION_TABLE
%endif
%assign rc_val 1
align 32
do_name _aes_encrypt_key192,8
push ebp
push ebx
push esi
push edi
mov ebp,[esp+24]
mov [ebp+4*KS_LENGTH],dword 12 * 16
mov ebx,[esp+20]
mov esi,[ebx]
mov [ebp],esi
mov edi,[ebx+4]
mov [ebp+4],edi
mov ecx,[ebx+8]
mov [ebp+8],ecx
mov edx,[ebx+12]
mov [ebp+12],edx
mov eax,[ebx+16]
mov [ebp+16],eax
mov eax,[ebx+20]
mov [ebp+20],eax
add ebp,24
f_key 0,24 ; 13 * 4 = 52 unsigned longs
f_key 1,24 ; 6 + 6 * 8 generated = 54
f_key 2,24
f_key 3,24
f_key 4,24
f_key 5,24
f_key 6,24
f_key 7,24
pop edi
pop esi
pop ebx
pop ebp
xor eax,eax
do_exit 8
%endif
%ifdef AES_256
%ifndef ENCRYPTION_TABLE
%define ENCRYPTION_TABLE
%endif
%assign rc_val 1
align 32
do_name _aes_encrypt_key256,8
push ebp
push ebx
push esi
push edi
mov ebp,[esp+24]
mov [ebp+4*KS_LENGTH],dword 14 * 16
mov ebx,[esp+20]
mov esi,[ebx]
mov [ebp],esi
mov edi,[ebx+4]
mov [ebp+4],edi
mov ecx,[ebx+8]
mov [ebp+8],ecx
mov edx,[ebx+12]
mov [ebp+12],edx
mov eax,[ebx+16]
mov [ebp+16],eax
mov eax,[ebx+20]
mov [ebp+20],eax
mov eax,[ebx+24]
mov [ebp+24],eax
mov eax,[ebx+28]
mov [ebp+28],eax
add ebp,32
f_key 0,32 ; 15 * 4 = 60 unsigned longs
f_key 1,32 ; 8 + 8 * 7 generated = 64
f_key 2,32
f_key 3,32
f_key 4,32
f_key 5,32
f_key 6,32
pop edi
pop esi
pop ebx
pop ebp
xor eax,eax
do_exit 8
%endif
%ifdef AES_VAR
%ifndef ENCRYPTION_TABLE
%define ENCRYPTION_TABLE
%endif
align 32
do_name _aes_encrypt_key,12
mov ecx,[esp+4]
mov eax,[esp+8]
mov edx,[esp+12]
push edx
push ecx
cmp eax,16
je .1
cmp eax,128
je .1
cmp eax,24
je .2
cmp eax,192
je .2
cmp eax,32
je .3
cmp eax,256
je .3
mov eax,-1
add esp,8
do_exit 12
.1: do_call _aes_encrypt_key128,8
do_exit 12
.2: do_call _aes_encrypt_key192,8
do_exit 12
.3: do_call _aes_encrypt_key256,8
do_exit 12
%endif
%endif
%ifdef DECRYPTION
%define DECRYPTION_TABLE
%define dtab_0(x) [dec_tab+ 8*x]
%define dtab_1(x) [dec_tab+3+8*x]
%define dtab_2(x) [dec_tab+2+8*x]
%define dtab_3(x) [dec_tab+1+8*x]
%define dtab_x(x) byte [dec_tab+7+8*x]
%macro irn_fun 2
rol eax,16
%1 esi, cl, 0, ebp
%1 esi, bh, 1, ebp
%1 esi, al, 2, ebp
%1 edi, dl, 0, ebp
%1 edi, ch, 1, ebp
%1 edi, ah, 3, ebp
%2 ebp, bl, 0, ebp
shr eax,16
and ebx,0xffff0000
or ebx,eax
shr ecx,16
%1 ebp, bh, 1, eax
%1 ebp, ch, 3, eax
%2 eax, cl, 2, ecx
%1 eax, bl, 0, ecx
%1 eax, dh, 1, ecx
shr ebx,16
shr edx,16
%1 esi, dh, 3, ecx
%1 ebp, dl, 2, ecx
%1 eax, bh, 3, ecx
%1 edi, bl, 2, ecx
%endmacro
; Basic MOV and XOR Operations for normal rounds
%macro ni_xor 4
movzx %4,%2
xor %1,dtab_%3(%4)
%endmacro
%macro ni_mov 4
movzx %4,%2
mov %1,dtab_%3(%4)
%endmacro
; Basic MOV and XOR Operations for last round
%macro li_xor 4
movzx %4,%2
movzx %4,dtab_x(%4)
%if %3 != 0
shl %4,8*%3
%endif
xor %1,%4
%endmacro
%macro li_mov 4
movzx %4,%2
movzx %1,dtab_x(%4)
%if %3 != 0
shl %1,8*%3
%endif
%endmacro
%macro _dec_round 0
%ifdef AES_REV_DKS
add ebp,16
%else
sub ebp,16
%endif
mov esi,[ebp+8]
mov edi,[ebp+12]
push ebp
irn_fun ni_xor, ni_mov
mov ebx,ebp
pop ebp
mov ecx,esi
mov edx,edi
xor eax,[ebp]
xor ebx,[ebp+4]
%endmacro
%ifdef REDUCE_CODE_SIZE
align 32
dec_round:
_dec_round
ret
%else
%macro dec_round 0
_dec_round
%endmacro
%endif
%macro dec_last_round 0
%ifdef AES_REV_DKS
add ebp,16
%else
sub ebp,16
%endif
mov esi,[ebp+8]
mov edi,[ebp+12]
push ebp
irn_fun li_xor, li_mov
mov ebx,ebp
pop ebp
xor eax,[ebp]
xor ebx,[ebp+4]
%endmacro
; AES Decryption Subroutine
align 32
do_name _aes_decrypt,12
push ebp
push ebx
push esi
push edi
; load the input block
mov esi,[esp+in_blk+stk_spc] ; input pointer
mov eax,[esi ]
mov ebx,[esi+ 4]
mov ecx,[esi+ 8]
mov edx,[esi+12]
lea esi,[esi+16]
; xor in the first round key
mov ebp,[esp+ctx+stk_spc] ; key pointer
movzx edi,byte[ebp+4*KS_LENGTH]
%ifndef AES_REV_DKS ; if decryption key schedule is not reversed
lea ebp,[ebp+edi] ; we have to access it from the top down
%endif
xor eax,[ebp ] ; key schedule
xor ebx,[ebp+ 4]
xor ecx,[ebp+ 8]
xor edx,[ebp+12]
; determine the number of rounds
cmp edi,10*16
je .3
cmp edi,12*16
je .2
cmp edi,14*16
je .1
mov eax,-1
jmp .5
; perform the decryption rounds
.1: mf_call dec_round
mf_call dec_round
.2: mf_call dec_round
mf_call dec_round
.3: mf_call dec_round
mf_call dec_round
mf_call dec_round
mf_call dec_round
mf_call dec_round
mf_call dec_round
mf_call dec_round
mf_call dec_round
mf_call dec_round
dec_last_round
; output the block
mov ebp,[esp+out_blk+stk_spc]
mov [ebp ],eax
mov [ebp+ 4],ebx
mov [ebp+ 8],esi
mov [ebp+12],edi
xor eax,eax
.5: pop edi
pop esi
pop ebx
pop ebp
do_exit 12
%endif
%ifdef REDUCE_CODE_SIZE
global _im_sub
_im_sub:
mov edx,[esp+4]
inv_mix_col:
movzx ecx,dl ; input eax, edx
movzx ecx,etab_b(ecx) ; output eax
mov eax,dtab_0(ecx) ; used ecx
movzx ecx,dh
shr edx,16
movzx ecx,etab_b(ecx)
xor eax,dtab_1(ecx)
movzx ecx,dl
movzx ecx,etab_b(ecx)
xor eax,dtab_2(ecx)
movzx ecx,dh
movzx ecx,etab_b(ecx)
xor eax,dtab_3(ecx)