59a2d0da19053c0e5fe83fbdcb5fb118bbc210c4
[openssl.git] / crypto / aes / asm / aes-586.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. Rights for redistribution and usage in source and binary
6 # forms are granted according to the OpenSSL license.
7 # ====================================================================
8 #
9 # Version 3.3.
10 #
11 # You might fail to appreciate this module performance from the first
12 # try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
13 # to be *the* best Intel C compiler without -KPIC, performance appears
14 # to be virtually identical... But try to re-configure with shared
15 # library support... Aha! Intel compiler "suddenly" lags behind by 30%
16 # [on P4, more on others]:-) And if compared to position-independent
17 # code generated by GNU C, this code performs *more* than *twice* as
18 # fast! Yes, all this buzz about PIC means that unlike other hand-
19 # coded implementations, this one was explicitly designed to be safe
20 # to use even in shared library context... This also means that this
21 # code isn't necessarily absolutely fastest "ever," because in order
22 # to achieve position independence an extra register has to be
23 # off-loaded to stack, which affects the benchmark result.
24 #
25 # Special note about instruction choice. Do you recall RC4_INT code
26 # performing poorly on P4? It might be the time to figure out why.
27 # RC4_INT code implies effective address calculations in base+offset*4
28 # form. Trouble is that it seems that offset scaling turned to be
29 # critical path... At least eliminating scaling resulted in 2.8x RC4
30 # performance improvement [as you might recall]. As AES code is hungry
31 # for scaling too, I [try to] avoid the latter by favoring off-by-2
32 # shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
33 #
34 # As was shown by Dean Gaudet <dean@arctic.org>, the above note turned
35 # void. Performance improvement with off-by-2 shifts was observed on
36 # intermediate implementation, which was spilling yet another register
37 # to stack... Final offset*4 code below runs just a tad faster on P4,
38 # but exhibits up to 10% improvement on other cores.
39 #
40 # Second version is "monolithic" replacement for aes_core.c, which in
41 # addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key.
42 # This made it possible to implement little-endian variant of the
43 # algorithm without modifying the base C code. Motivating factor for
44 # the undertaken effort was that it appeared that in tight IA-32
45 # register window little-endian flavor could achieve slightly higher
46 # Instruction Level Parallelism, and it indeed resulted in up to 15%
47 # better performance on most recent µ-archs...
48 #
49 # Third version adds AES_cbc_encrypt implementation, which resulted in
50 # up to 40% performance imrovement of CBC benchmark results. 40% was
51 # observed on P4 core, where "overall" imrovement coefficient, i.e. if
52 # compared to PIC generated by GCC and in CBC mode, was observed to be
53 # as large as 4x:-) CBC performance is virtually identical to ECB now
54 # and on some platforms even better, e.g. 17.6 "small" cycles/byte on
55 # Opteron, because certain function prologues and epilogues are
56 # effectively taken out of the loop...
57 #
58 # Version 3.2 implements compressed tables and prefetch of these tables
59 # in CBC[!] mode. Former means that 3/4 of table references are now
60 # misaligned, which unfortunately has negative impact on elder IA-32
61 # implementations, Pentium suffered 30% penalty, PIII - 10%.
62 #
63 # Current ECB performance numbers for 128-bit key in CPU cycles per
64 # processed byte [measure commonly used by AES benchmarkers] are:
65 #
66 #               small footprint         fully unrolled
67 # P4            24                      22
68 # AMD K8        20                      19
69 # PIII          25                      23
70 # Pentium       81                      78
71
72 push(@INC,"perlasm","../../perlasm");
73 require "x86asm.pl";
74
75 &asm_init($ARGV[0],"aes-586.pl",$ARGV[$#ARGV] eq "386");
76
77 $s0="eax";
78 $s1="ebx";
79 $s2="ecx";
80 $s3="edx";
81 $key="edi";
82 $acc="esi";
83
84 $small_footprint=1;     # $small_footprint=1 code is ~5% slower [on
85                         # recent µ-archs], but ~5 times smaller!
86                         # I favor compact code to minimize cache
87                         # contention and in hope to "collect" 5% back
88                         # in real-life applications...
89 $vertical_spin=0;       # shift "verticaly" defaults to 0, because of
90                         # its proof-of-concept status...
91
92 # Note that there is no decvert(), as well as last encryption round is
93 # performed with "horizontal" shifts. This is because this "vertical"
94 # implementation [one which groups shifts on a given $s[i] to form a
95 # "column," unlike "horizontal" one, which groups shifts on different
96 # $s[i] to form a "row"] is work in progress. It was observed to run
97 # few percents faster on Intel cores, but not AMD. On AMD K8 core it's
98 # whole 12% slower:-( So we face a trade-off... Shall it be resolved
99 # some day? Till then the code is considered experimental and by
100 # default remains dormant...
101
102 sub encvert()
103 { my ($te,@s) = @_;
104   my $v0 = $acc, $v1 = $key;
105
106         &mov    ($v0,$s[3]);                            # copy s3
107         &mov    (&DWP(4,"esp"),$s[2]);                  # save s2
108         &mov    ($v1,$s[0]);                            # copy s0
109         &mov    (&DWP(8,"esp"),$s[1]);                  # save s1
110
111         &movz   ($s[2],&HB($s[0]));
112         &and    ($s[0],0xFF);
113         &mov    ($s[0],&DWP(0,$te,$s[0],8));            # s0>>0
114         &shr    ($v1,16);
115         &mov    ($s[3],&DWP(3,$te,$s[2],8));            # s0>>8
116         &movz   ($s[1],&HB($v1));
117         &and    ($v1,0xFF);
118         &mov    ($s[2],&DWP(2,$te,$v1,8));              # s0>>16
119          &mov   ($v1,$v0);
120         &mov    ($s[1],&DWP(1,$te,$s[1],8));            # s0>>24
121
122         &and    ($v0,0xFF);
123         &xor    ($s[3],&DWP(0,$te,$v0,8));              # s3>>0
124         &movz   ($v0,&HB($v1));
125         &shr    ($v1,16);
126         &xor    ($s[2],&DWP(3,$te,$v0,8));              # s3>>8
127         &movz   ($v0,&HB($v1));
128         &and    ($v1,0xFF);
129         &xor    ($s[1],&DWP(2,$te,$v1,8));              # s3>>16
130          &mov   ($v1,&DWP(4,"esp"));                    # restore s2
131         &xor    ($s[0],&DWP(1,$te,$v0,8));              # s3>>24
132
133         &mov    ($v0,$v1);
134         &and    ($v1,0xFF);
135         &xor    ($s[2],&DWP(0,$te,$v1,8));              # s2>>0
136         &movz   ($v1,&HB($v0));
137         &shr    ($v0,16);
138         &xor    ($s[1],&DWP(3,$te,$v1,8));              # s2>>8
139         &movz   ($v1,&HB($v0));
140         &and    ($v0,0xFF);
141         &xor    ($s[0],&DWP(2,$te,$v0,8));              # s2>>16
142          &mov   ($v0,&DWP(8,"esp"));                    # restore s1
143         &xor    ($s[3],&DWP(1,$te,$v1,8));              # s2>>24
144
145         &mov    ($v1,$v0);
146         &and    ($v0,0xFF);
147         &xor    ($s[1],&DWP(0,$te,$v0,8));              # s1>>0
148         &movz   ($v0,&HB($v1));
149         &shr    ($v1,16);
150         &xor    ($s[0],&DWP(3,$te,$v0,8));              # s1>>8
151         &movz   ($v0,&HB($v1));
152         &and    ($v1,0xFF);
153         &xor    ($s[3],&DWP(2,$te,$v1,8));              # s1>>16
154          &mov   ($key,&DWP(12,"esp"));                  # reincarnate v1 as key
155         &xor    ($s[2],&DWP(1,$te,$v0,8));              # s1>>24
156 }
157
158 sub encstep()
159 { my ($i,$te,@s) = @_;
160   my $tmp = $key;
161   my $out = $i==3?$s[0]:$acc;
162
163         # lines marked with #%e?x[i] denote "reordered" instructions...
164         if ($i==3)  {   &mov    ($key,&DWP(12,"esp"));          }##%edx
165         else        {   &mov    ($out,$s[0]);
166                         &and    ($out,0xFF);                    }
167         if ($i==1)  {   &shr    ($s[0],16);                     }#%ebx[1]
168         if ($i==2)  {   &shr    ($s[0],24);                     }#%ecx[2]
169                         &mov    ($out,&DWP(0,$te,$out,8));
170
171         if ($i==3)  {   $tmp=$s[1];                             }##%eax
172                         &movz   ($tmp,&HB($s[1]));
173                         &xor    ($out,&DWP(3,$te,$tmp,8));
174
175         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],&DWP(4,"esp")); }##%ebx
176         else        {   &mov    ($tmp,$s[2]);
177                         &shr    ($tmp,16);                      }
178         if ($i==2)  {   &and    ($s[1],0xFF);                   }#%edx[2]
179                         &and    ($tmp,0xFF);
180                         &xor    ($out,&DWP(2,$te,$tmp,8));
181
182         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }##%ecx
183         elsif($i==2){   &movz   ($tmp,&HB($s[3]));              }#%ebx[2]
184         else        {   &mov    ($tmp,$s[3]); 
185                         &shr    ($tmp,24)                       }
186                         &xor    ($out,&DWP(1,$te,$tmp,8));
187         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
188         if ($i==3)  {   &mov    ($s[3],$acc);                   }
189                         &comment();
190 }
191
192 sub enclast()
193 { my ($i,$te,@s)=@_;
194   my $tmp = $key;
195   my $out = $i==3?$s[0]:$acc;
196
197         if ($i==3)  {   &mov    ($key,&DWP(12,"esp"));          }##%edx
198         else        {   &mov    ($out,$s[0]);                   }
199                         &and    ($out,0xFF);
200         if ($i==1)  {   &shr    ($s[0],16);                     }#%ebx[1]
201         if ($i==2)  {   &shr    ($s[0],24);                     }#%ecx[2]
202                         &mov    ($out,&DWP(2,$te,$out,8));
203                         &and    ($out,0x000000ff);
204
205         if ($i==3)  {   $tmp=$s[1];                             }##%eax
206                         &movz   ($tmp,&HB($s[1]));
207                         &mov    ($tmp,&DWP(0,$te,$tmp,8));
208                         &and    ($tmp,0x0000ff00);
209                         &xor    ($out,$tmp);
210
211         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],&DWP(4,"esp")); }##%ebx
212         else        {   mov     ($tmp,$s[2]);
213                         &shr    ($tmp,16);                      }
214         if ($i==2)  {   &and    ($s[1],0xFF);                   }#%edx[2]
215                         &and    ($tmp,0xFF);
216                         &mov    ($tmp,&DWP(0,$te,$tmp,8));
217                         &and    ($tmp,0x00ff0000);
218                         &xor    ($out,$tmp);
219
220         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }##%ecx
221         elsif($i==2){   &movz   ($tmp,&HB($s[3]));              }#%ebx[2]
222         else        {   &mov    ($tmp,$s[3]);
223                         &shr    ($tmp,24);                      }
224                         &mov    ($tmp,&DWP(2,$te,$tmp,8));
225                         &and    ($tmp,0xff000000);
226                         &xor    ($out,$tmp);
227         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
228         if ($i==3)  {   &mov    ($s[3],$acc);                   }
229 }
230
231 sub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
232
233 &public_label("AES_Te");
234 &function_begin_B("_x86_AES_encrypt");
235         if ($vertical_spin) {
236                 # I need high parts of volatile registers to be accessible...
237                 &exch   ($s1="edi",$key="ebx");
238                 &mov    ($s2="esi",$acc="ecx");
239         }
240
241         # note that caller is expected to allocate stack frame for me!
242         &mov    (&DWP(12,"esp"),$key);          # save key
243
244         &xor    ($s0,&DWP(0,$key));             # xor with key
245         &xor    ($s1,&DWP(4,$key));
246         &xor    ($s2,&DWP(8,$key));
247         &xor    ($s3,&DWP(12,$key));
248
249         &mov    ($acc,&DWP(240,$key));          # load key->rounds
250
251         if ($small_footprint) {
252             &lea        ($acc,&DWP(-2,$acc,$acc));
253             &lea        ($acc,&DWP(0,$key,$acc,8));
254             &mov        (&DWP(16,"esp"),$acc);  # end of key schedule
255             &align      (4);
256             &set_label("loop");
257                 if ($vertical_spin) {
258                     &encvert("ebp",$s0,$s1,$s2,$s3);
259                 } else {
260                     &encstep(0,"ebp",$s0,$s1,$s2,$s3);
261                     &encstep(1,"ebp",$s1,$s2,$s3,$s0);
262                     &encstep(2,"ebp",$s2,$s3,$s0,$s1);
263                     &encstep(3,"ebp",$s3,$s0,$s1,$s2);
264                 }
265                 &add    ($key,16);              # advance rd_key
266                 &xor    ($s0,&DWP(0,$key));
267                 &xor    ($s1,&DWP(4,$key));
268                 &xor    ($s2,&DWP(8,$key));
269                 &xor    ($s3,&DWP(12,$key));
270             &cmp        ($key,&DWP(16,"esp"));
271             &mov        (&DWP(12,"esp"),$key);
272             &jb         (&label("loop"));
273         }
274         else {
275             &cmp        ($acc,10);
276             &jle        (&label("10rounds"));
277             &cmp        ($acc,12);
278             &jle        (&label("12rounds"));
279
280         &set_label("14rounds");
281             for ($i=1;$i<3;$i++) {
282                 if ($vertical_spin) {
283                     &encvert("ebp",$s0,$s1,$s2,$s3);
284                 } else {
285                     &encstep(0,"ebp",$s0,$s1,$s2,$s3);
286                     &encstep(1,"ebp",$s1,$s2,$s3,$s0);
287                     &encstep(2,"ebp",$s2,$s3,$s0,$s1);
288                     &encstep(3,"ebp",$s3,$s0,$s1,$s2);
289                 }
290                 &xor    ($s0,&DWP(16*$i+0,$key));
291                 &xor    ($s1,&DWP(16*$i+4,$key));
292                 &xor    ($s2,&DWP(16*$i+8,$key));
293                 &xor    ($s3,&DWP(16*$i+12,$key));
294             }
295             &add        ($key,32);
296             &mov        (&DWP(12,"esp"),$key);  # advance rd_key
297         &set_label("12rounds");
298             for ($i=1;$i<3;$i++) {
299                 if ($vertical_spin) {
300                     &encvert("ebp",$s0,$s1,$s2,$s3);
301                 } else {
302                     &encstep(0,"ebp",$s0,$s1,$s2,$s3);
303                     &encstep(1,"ebp",$s1,$s2,$s3,$s0);
304                     &encstep(2,"ebp",$s2,$s3,$s0,$s1);
305                     &encstep(3,"ebp",$s3,$s0,$s1,$s2);
306                 }
307                 &xor    ($s0,&DWP(16*$i+0,$key));
308                 &xor    ($s1,&DWP(16*$i+4,$key));
309                 &xor    ($s2,&DWP(16*$i+8,$key));
310                 &xor    ($s3,&DWP(16*$i+12,$key));
311             }
312             &add        ($key,32);
313             &mov        (&DWP(12,"esp"),$key);  # advance rd_key
314         &set_label("10rounds");
315             for ($i=1;$i<10;$i++) {
316                 if ($vertical_spin) {
317                     &encvert("ebp",$s0,$s1,$s2,$s3);
318                 } else {
319                     &encstep(0,"ebp",$s0,$s1,$s2,$s3);
320                     &encstep(1,"ebp",$s1,$s2,$s3,$s0);
321                     &encstep(2,"ebp",$s2,$s3,$s0,$s1);
322                     &encstep(3,"ebp",$s3,$s0,$s1,$s2);
323                 }
324                 &xor    ($s0,&DWP(16*$i+0,$key));
325                 &xor    ($s1,&DWP(16*$i+4,$key));
326                 &xor    ($s2,&DWP(16*$i+8,$key));
327                 &xor    ($s3,&DWP(16*$i+12,$key));
328             }
329         }
330
331         if ($vertical_spin) {
332             # "reincarnate" some registers for "horizontal" spin...
333             &mov        ($s1="ebx",$key="edi");
334             &mov        ($s2="ecx",$acc="esi");
335         }
336         &enclast(0,"ebp",$s0,$s1,$s2,$s3);
337         &enclast(1,"ebp",$s1,$s2,$s3,$s0);
338         &enclast(2,"ebp",$s2,$s3,$s0,$s1);
339         &enclast(3,"ebp",$s3,$s0,$s1,$s2);
340
341         &add    ($key,$small_footprint?16:160);
342         &xor    ($s0,&DWP(0,$key));
343         &xor    ($s1,&DWP(4,$key));
344         &xor    ($s2,&DWP(8,$key));
345         &xor    ($s3,&DWP(12,$key));
346
347         &ret    ();
348
349 &set_label("AES_Te",64);        # Yes! I keep it in the code segment!
350         &_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
351         &_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
352         &_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
353         &_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
354         &_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
355         &_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
356         &_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
357         &_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
358         &_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
359         &_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
360         &_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
361         &_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
362         &_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
363         &_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
364         &_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
365         &_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
366         &_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
367         &_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
368         &_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
369         &_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
370         &_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
371         &_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
372         &_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
373         &_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
374         &_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
375         &_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
376         &_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
377         &_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
378         &_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
379         &_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
380         &_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
381         &_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
382         &_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
383         &_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
384         &_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
385         &_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
386         &_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
387         &_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
388         &_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
389         &_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
390         &_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
391         &_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
392         &_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
393         &_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
394         &_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
395         &_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
396         &_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
397         &_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
398         &_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
399         &_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
400         &_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
401         &_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
402         &_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
403         &_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
404         &_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
405         &_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
406         &_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
407         &_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
408         &_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
409         &_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
410         &_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
411         &_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
412         &_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
413         &_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
414 #rcon:
415         &data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
416         &data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
417         &data_word(0x0000001b, 0x00000036);
418 &function_end_B("_x86_AES_encrypt");
419
420 # void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
421 &public_label("AES_Te");
422 &function_begin("AES_encrypt");
423         &mov    ($acc,&wparam(0));              # load inp
424         &mov    ($key,&wparam(2));              # load key
425
426         &mov    ($s0,"esp");
427         &sub    ("esp",24);
428         &and    ("esp",-64);
429         &add    ("esp",4);
430         &mov    (&DWP(16,"esp"),$s0);
431
432         &call   (&label("pic_point"));          # make it PIC!
433         &set_label("pic_point");
434         &blindpop("ebp");
435         &lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
436
437         &mov    ($s0,&DWP(0,$acc));             # load input data
438         &mov    ($s1,&DWP(4,$acc));
439         &mov    ($s2,&DWP(8,$acc));
440         &mov    ($s3,&DWP(12,$acc));
441
442         &call   ("_x86_AES_encrypt");
443
444         &mov    ("esp",&DWP(16,"esp"));
445
446         &mov    ($acc,&wparam(1));              # load out
447         &mov    (&DWP(0,$acc),$s0);             # write output data
448         &mov    (&DWP(4,$acc),$s1);
449         &mov    (&DWP(8,$acc),$s2);
450         &mov    (&DWP(12,$acc),$s3);
451 &function_end("AES_encrypt");
452
453 #------------------------------------------------------------------#
454
455 sub decstep()
456 { my ($i,$td,@s) = @_;
457   my $tmp = $key;
458   my $out = $i==3?$s[0]:$acc;
459
460         # no instructions are reordered, as performance appears
461         # optimal... or rather that all attempts to reorder didn't
462         # result in better performance [which by the way is not a
463         # bit lower than ecryption].
464         if($i==3)   {   &mov    ($key,&DWP(12,"esp"));          }
465         else        {   &mov    ($out,$s[0]);                   }
466                         &and    ($out,0xFF);
467                         &mov    ($out,&DWP(0,$td,$out,8));
468
469         if ($i==3)  {   $tmp=$s[1];                             }
470                         &movz   ($tmp,&HB($s[1]));
471                         &xor    ($out,&DWP(3,$td,$tmp,8));
472
473         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],$acc);          }
474         else        {   &mov    ($tmp,$s[2]);                   }
475                         &shr    ($tmp,16);
476                         &and    ($tmp,0xFF);
477                         &xor    ($out,&DWP(2,$td,$tmp,8));
478
479         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }
480         else        {   &mov    ($tmp,$s[3]);                   }
481                         &shr    ($tmp,24);
482                         &xor    ($out,&DWP(1,$td,$tmp,8));
483         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
484         if ($i==3)  {   &mov    ($s[3],&DWP(4,"esp"));          }
485                         &comment();
486 }
487
488 sub declast()
489 { my ($i,$td,@s)=@_;
490   my $tmp = $key;
491   my $out = $i==3?$s[0]:$acc;
492
493         if($i==3)   {   &mov    ($key,&DWP(12,"esp"));          }
494         else        {   &mov    ($out,$s[0]);                   }
495                         &and    ($out,0xFF);
496                         &mov    ($out,&DWP(2048,$td,$out,4));
497                         &and    ($out,0x000000ff);
498
499         if ($i==3)  {   $tmp=$s[1];                             }
500                         &movz   ($tmp,&HB($s[1]));
501                         &mov    ($tmp,&DWP(2048,$td,$tmp,4));
502                         &and    ($tmp,0x0000ff00);
503                         &xor    ($out,$tmp);
504
505         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],$acc);          }
506         else        {   mov     ($tmp,$s[2]);                   }
507                         &shr    ($tmp,16);
508                         &and    ($tmp,0xFF);
509                         &mov    ($tmp,&DWP(2048,$td,$tmp,4));
510                         &and    ($tmp,0x00ff0000);
511                         &xor    ($out,$tmp);
512
513         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }
514         else        {   &mov    ($tmp,$s[3]);                   }
515                         &shr    ($tmp,24);
516                         &mov    ($tmp,&DWP(2048,$td,$tmp,4));
517                         &and    ($tmp,0xff000000);
518                         &xor    ($out,$tmp);
519         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
520         if ($i==3)  {   &mov    ($s[3],&DWP(4,"esp"));          }
521 }
522
523 &public_label("AES_Td");
524 &function_begin_B("_x86_AES_decrypt");
525         # note that caller is expected to allocate stack frame for me!
526         &mov    (&DWP(12,"esp"),$key);          # save key
527
528         &xor    ($s0,&DWP(0,$key));             # xor with key
529         &xor    ($s1,&DWP(4,$key));
530         &xor    ($s2,&DWP(8,$key));
531         &xor    ($s3,&DWP(12,$key));
532
533         &mov    ($acc,&DWP(240,$key));          # load key->rounds
534
535         if ($small_footprint) {
536             &lea        ($acc,&DWP(-2,$acc,$acc));
537             &lea        ($acc,&DWP(0,$key,$acc,8));
538             &mov        (&DWP(16,"esp"),$acc);  # end of key schedule
539             &align      (4);
540             &set_label("loop");
541                 &decstep(0,"ebp",$s0,$s3,$s2,$s1);
542                 &decstep(1,"ebp",$s1,$s0,$s3,$s2);
543                 &decstep(2,"ebp",$s2,$s1,$s0,$s3);
544                 &decstep(3,"ebp",$s3,$s2,$s1,$s0);
545                 &add    ($key,16);              # advance rd_key
546                 &xor    ($s0,&DWP(0,$key));
547                 &xor    ($s1,&DWP(4,$key));
548                 &xor    ($s2,&DWP(8,$key));
549                 &xor    ($s3,&DWP(12,$key));
550             &cmp        ($key,&DWP(16,"esp"));
551             &mov        (&DWP(12,"esp"),$key);
552             &jb         (&label("loop"));
553         }
554         else {
555             &cmp        ($acc,10);
556             &jle        (&label("10rounds"));
557             &cmp        ($acc,12);
558             &jle        (&label("12rounds"));
559
560         &set_label("14rounds");
561             for ($i=1;$i<3;$i++) {
562                 &decstep(0,"ebp",$s0,$s3,$s2,$s1);
563                 &decstep(1,"ebp",$s1,$s0,$s3,$s2);
564                 &decstep(2,"ebp",$s2,$s1,$s0,$s3);
565                 &decstep(3,"ebp",$s3,$s2,$s1,$s0);
566                 &xor    ($s0,&DWP(16*$i+0,$key));
567                 &xor    ($s1,&DWP(16*$i+4,$key));
568                 &xor    ($s2,&DWP(16*$i+8,$key));
569                 &xor    ($s3,&DWP(16*$i+12,$key));
570             }
571             &add        ($key,32);
572             &mov        (&DWP(12,"esp"),$key);  # advance rd_key
573         &set_label("12rounds");
574             for ($i=1;$i<3;$i++) {
575                 &decstep(0,"ebp",$s0,$s3,$s2,$s1);
576                 &decstep(1,"ebp",$s1,$s0,$s3,$s2);
577                 &decstep(2,"ebp",$s2,$s1,$s0,$s3);
578                 &decstep(3,"ebp",$s3,$s2,$s1,$s0);
579                 &xor    ($s0,&DWP(16*$i+0,$key));
580                 &xor    ($s1,&DWP(16*$i+4,$key));
581                 &xor    ($s2,&DWP(16*$i+8,$key));
582                 &xor    ($s3,&DWP(16*$i+12,$key));
583             }
584             &add        ($key,32);
585             &mov        (&DWP(12,"esp"),$key);  # advance rd_key
586         &set_label("10rounds");
587             for ($i=1;$i<10;$i++) {
588                 &decstep(0,"ebp",$s0,$s3,$s2,$s1);
589                 &decstep(1,"ebp",$s1,$s0,$s3,$s2);
590                 &decstep(2,"ebp",$s2,$s1,$s0,$s3);
591                 &decstep(3,"ebp",$s3,$s2,$s1,$s0);
592                 &xor    ($s0,&DWP(16*$i+0,$key));
593                 &xor    ($s1,&DWP(16*$i+4,$key));
594                 &xor    ($s2,&DWP(16*$i+8,$key));
595                 &xor    ($s3,&DWP(16*$i+12,$key));
596             }
597         }
598
599         &declast(0,"ebp",$s0,$s3,$s2,$s1);
600         &declast(1,"ebp",$s1,$s0,$s3,$s2);
601         &declast(2,"ebp",$s2,$s1,$s0,$s3);
602         &declast(3,"ebp",$s3,$s2,$s1,$s0);
603
604         &add    ($key,$small_footprint?16:160);
605         &xor    ($s0,&DWP(0,$key));
606         &xor    ($s1,&DWP(4,$key));
607         &xor    ($s2,&DWP(8,$key));
608         &xor    ($s3,&DWP(12,$key));
609
610         &ret    ();
611
612 &set_label("AES_Td",64);        # Yes! I keep it in the code segment!
613         &_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
614         &_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
615         &_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
616         &_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
617         &_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
618         &_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
619         &_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
620         &_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
621         &_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
622         &_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
623         &_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
624         &_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
625         &_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
626         &_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
627         &_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
628         &_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
629         &_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
630         &_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
631         &_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
632         &_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
633         &_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
634         &_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
635         &_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
636         &_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
637         &_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
638         &_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
639         &_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
640         &_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
641         &_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
642         &_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
643         &_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
644         &_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
645         &_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
646         &_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
647         &_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
648         &_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
649         &_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
650         &_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
651         &_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
652         &_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
653         &_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
654         &_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
655         &_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
656         &_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
657         &_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
658         &_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
659         &_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
660         &_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
661         &_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
662         &_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
663         &_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
664         &_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
665         &_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
666         &_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
667         &_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
668         &_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
669         &_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
670         &_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
671         &_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
672         &_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
673         &_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
674         &_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
675         &_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
676         &_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
677 #Td4:
678         &data_word(0x52525252, 0x09090909, 0x6a6a6a6a, 0xd5d5d5d5);
679         &data_word(0x30303030, 0x36363636, 0xa5a5a5a5, 0x38383838);
680         &data_word(0xbfbfbfbf, 0x40404040, 0xa3a3a3a3, 0x9e9e9e9e);
681         &data_word(0x81818181, 0xf3f3f3f3, 0xd7d7d7d7, 0xfbfbfbfb);
682         &data_word(0x7c7c7c7c, 0xe3e3e3e3, 0x39393939, 0x82828282);
683         &data_word(0x9b9b9b9b, 0x2f2f2f2f, 0xffffffff, 0x87878787);
684         &data_word(0x34343434, 0x8e8e8e8e, 0x43434343, 0x44444444);
685         &data_word(0xc4c4c4c4, 0xdededede, 0xe9e9e9e9, 0xcbcbcbcb);
686         &data_word(0x54545454, 0x7b7b7b7b, 0x94949494, 0x32323232);
687         &data_word(0xa6a6a6a6, 0xc2c2c2c2, 0x23232323, 0x3d3d3d3d);
688         &data_word(0xeeeeeeee, 0x4c4c4c4c, 0x95959595, 0x0b0b0b0b);
689         &data_word(0x42424242, 0xfafafafa, 0xc3c3c3c3, 0x4e4e4e4e);
690         &data_word(0x08080808, 0x2e2e2e2e, 0xa1a1a1a1, 0x66666666);
691         &data_word(0x28282828, 0xd9d9d9d9, 0x24242424, 0xb2b2b2b2);
692         &data_word(0x76767676, 0x5b5b5b5b, 0xa2a2a2a2, 0x49494949);
693         &data_word(0x6d6d6d6d, 0x8b8b8b8b, 0xd1d1d1d1, 0x25252525);
694         &data_word(0x72727272, 0xf8f8f8f8, 0xf6f6f6f6, 0x64646464);
695         &data_word(0x86868686, 0x68686868, 0x98989898, 0x16161616);
696         &data_word(0xd4d4d4d4, 0xa4a4a4a4, 0x5c5c5c5c, 0xcccccccc);
697         &data_word(0x5d5d5d5d, 0x65656565, 0xb6b6b6b6, 0x92929292);
698         &data_word(0x6c6c6c6c, 0x70707070, 0x48484848, 0x50505050);
699         &data_word(0xfdfdfdfd, 0xedededed, 0xb9b9b9b9, 0xdadadada);
700         &data_word(0x5e5e5e5e, 0x15151515, 0x46464646, 0x57575757);
701         &data_word(0xa7a7a7a7, 0x8d8d8d8d, 0x9d9d9d9d, 0x84848484);
702         &data_word(0x90909090, 0xd8d8d8d8, 0xabababab, 0x00000000);
703         &data_word(0x8c8c8c8c, 0xbcbcbcbc, 0xd3d3d3d3, 0x0a0a0a0a);
704         &data_word(0xf7f7f7f7, 0xe4e4e4e4, 0x58585858, 0x05050505);
705         &data_word(0xb8b8b8b8, 0xb3b3b3b3, 0x45454545, 0x06060606);
706         &data_word(0xd0d0d0d0, 0x2c2c2c2c, 0x1e1e1e1e, 0x8f8f8f8f);
707         &data_word(0xcacacaca, 0x3f3f3f3f, 0x0f0f0f0f, 0x02020202);
708         &data_word(0xc1c1c1c1, 0xafafafaf, 0xbdbdbdbd, 0x03030303);
709         &data_word(0x01010101, 0x13131313, 0x8a8a8a8a, 0x6b6b6b6b);
710         &data_word(0x3a3a3a3a, 0x91919191, 0x11111111, 0x41414141);
711         &data_word(0x4f4f4f4f, 0x67676767, 0xdcdcdcdc, 0xeaeaeaea);
712         &data_word(0x97979797, 0xf2f2f2f2, 0xcfcfcfcf, 0xcececece);
713         &data_word(0xf0f0f0f0, 0xb4b4b4b4, 0xe6e6e6e6, 0x73737373);
714         &data_word(0x96969696, 0xacacacac, 0x74747474, 0x22222222);
715         &data_word(0xe7e7e7e7, 0xadadadad, 0x35353535, 0x85858585);
716         &data_word(0xe2e2e2e2, 0xf9f9f9f9, 0x37373737, 0xe8e8e8e8);
717         &data_word(0x1c1c1c1c, 0x75757575, 0xdfdfdfdf, 0x6e6e6e6e);
718         &data_word(0x47474747, 0xf1f1f1f1, 0x1a1a1a1a, 0x71717171);
719         &data_word(0x1d1d1d1d, 0x29292929, 0xc5c5c5c5, 0x89898989);
720         &data_word(0x6f6f6f6f, 0xb7b7b7b7, 0x62626262, 0x0e0e0e0e);
721         &data_word(0xaaaaaaaa, 0x18181818, 0xbebebebe, 0x1b1b1b1b);
722         &data_word(0xfcfcfcfc, 0x56565656, 0x3e3e3e3e, 0x4b4b4b4b);
723         &data_word(0xc6c6c6c6, 0xd2d2d2d2, 0x79797979, 0x20202020);
724         &data_word(0x9a9a9a9a, 0xdbdbdbdb, 0xc0c0c0c0, 0xfefefefe);
725         &data_word(0x78787878, 0xcdcdcdcd, 0x5a5a5a5a, 0xf4f4f4f4);
726         &data_word(0x1f1f1f1f, 0xdddddddd, 0xa8a8a8a8, 0x33333333);
727         &data_word(0x88888888, 0x07070707, 0xc7c7c7c7, 0x31313131);
728         &data_word(0xb1b1b1b1, 0x12121212, 0x10101010, 0x59595959);
729         &data_word(0x27272727, 0x80808080, 0xecececec, 0x5f5f5f5f);
730         &data_word(0x60606060, 0x51515151, 0x7f7f7f7f, 0xa9a9a9a9);
731         &data_word(0x19191919, 0xb5b5b5b5, 0x4a4a4a4a, 0x0d0d0d0d);
732         &data_word(0x2d2d2d2d, 0xe5e5e5e5, 0x7a7a7a7a, 0x9f9f9f9f);
733         &data_word(0x93939393, 0xc9c9c9c9, 0x9c9c9c9c, 0xefefefef);
734         &data_word(0xa0a0a0a0, 0xe0e0e0e0, 0x3b3b3b3b, 0x4d4d4d4d);
735         &data_word(0xaeaeaeae, 0x2a2a2a2a, 0xf5f5f5f5, 0xb0b0b0b0);
736         &data_word(0xc8c8c8c8, 0xebebebeb, 0xbbbbbbbb, 0x3c3c3c3c);
737         &data_word(0x83838383, 0x53535353, 0x99999999, 0x61616161);
738         &data_word(0x17171717, 0x2b2b2b2b, 0x04040404, 0x7e7e7e7e);
739         &data_word(0xbabababa, 0x77777777, 0xd6d6d6d6, 0x26262626);
740         &data_word(0xe1e1e1e1, 0x69696969, 0x14141414, 0x63636363);
741         &data_word(0x55555555, 0x21212121, 0x0c0c0c0c, 0x7d7d7d7d);
742 &function_end_B("_x86_AES_decrypt");
743
744 # void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
745 &public_label("AES_Td");
746 &function_begin("AES_decrypt");
747         &mov    ($acc,&wparam(0));              # load inp
748         &mov    ($key,&wparam(2));              # load key
749
750         &mov    ($s0,"esp");
751         &sub    ("esp",24);
752         &and    ("esp",-64);
753         &add    ("esp",4);
754         &mov    (&DWP(16,"esp"),$s0);
755
756         &call   (&label("pic_point"));          # make it PIC!
757         &set_label("pic_point");
758         &blindpop("ebp");
759         &lea    ("ebp",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
760
761         &mov    ($s0,&DWP(0,$acc));             # load input data
762         &mov    ($s1,&DWP(4,$acc));
763         &mov    ($s2,&DWP(8,$acc));
764         &mov    ($s3,&DWP(12,$acc));
765
766         &call   ("_x86_AES_decrypt");
767
768         &mov    ("esp",&DWP(16,"esp"));
769
770         &mov    ($acc,&wparam(1));              # load out
771         &mov    (&DWP(0,$acc),$s0);             # write output data
772         &mov    (&DWP(4,$acc),$s1);
773         &mov    (&DWP(8,$acc),$s2);
774         &mov    (&DWP(12,$acc),$s3);
775 &function_end("AES_decrypt");
776
777 # void AES_cbc_encrypt (const void char *inp, unsigned char *out,
778 #                       size_t length, const AES_KEY *key,
779 #                       unsigned char *ivp,const int enc);
780 {
781 # stack frame layout
782 # -4(%esp)      0(%esp)         return address
783 # 0(%esp)       4(%esp)         tmp1
784 # 4(%esp)       8(%esp)         tmp2
785 # 8(%esp)       12(%esp)        key
786 # 12(%esp)      16(%esp)        end of key schedule
787 my $_esp=&DWP(16,"esp");        #saved %esp
788 my $_inp=&DWP(20,"esp");        #copy of wparam(0)
789 my $_out=&DWP(24,"esp");        #copy of wparam(1)
790 my $_len=&DWP(28,"esp");        #copy of wparam(2)
791 my $_key=&DWP(32,"esp");        #copy of wparam(3)
792 my $_ivp=&DWP(36,"esp");        #copy of wparam(4)
793 my $_tmp=&DWP(40,"esp");        #volatile variable
794 my $ivec=&DWP(44,"esp");        #ivec[16]
795
796 &public_label("AES_Te");
797 &public_label("AES_Td");
798 &function_begin("AES_cbc_encrypt");
799         &mov    ($s2 eq "ecx"? $s2 : "",&wparam(2));    # load len
800         &cmp    ($s2,0);
801         &je     (&label("enc_out"));
802
803         &call   (&label("pic_point"));          # make it PIC!
804         &set_label("pic_point");
805         &blindpop("ebp");
806
807         &cmp    (&wparam(5),0);
808         &je     (&label("DECRYPT"));
809
810         &lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
811
812         # allocate aligned stack frame...
813         &lea    ($key,&DWP(-44,"esp"));
814         &and    ($key,-64);
815
816         # ... and make sure it doesn't alias with AES_Te modulo 4096
817         &mov    ($s1,"ebp");
818         &mov    ($s3,$key);
819         &and    ($s1,0xfff);            # t = %ebp&0xfff
820         &and    ($s3,0xfff);            # p = %esp&0xfff
821
822         &cmp    ($s3,$s1);              # if (p<t) goto ok
823         &jb     (&label("te_ok"));
824         &lea    ($acc,&DWP(2048,$s1));
825         &cmp    ($s3,$acc);             # if (p>=(t+2048)) goto ok
826         &jae    (&label("te_ok"));
827         &sub    ($s1,$s3);              # t -= p
828         &lea    ($key,&DWP(-64,$key,$s1));# %esp -= (p-t)+64
829         &set_label("te_ok");
830
831         &mov    ($s0,&wparam(0));       # load inp
832         &mov    ($s1,&wparam(1));       # load out
833         &mov    ($s3,&wparam(3));       # load key
834         &mov    ($acc,&wparam(4));      # load ivp
835
836         &exch   ("esp",$key);
837         &add    ("esp",4);              # reserve for return address!
838         &mov    ($_esp,$key);           # save %esp
839
840         &mov    ($_inp,$s0);            # save copy of inp
841         &mov    ($_out,$s1);            # save copy of out
842         &mov    ($_len,$s2);            # save copy of len
843         &mov    ($_key,$s3);            # save copy of key
844         &mov    ($_ivp,$acc);           # save copy of ivp
845
846         &mov    ($acc,$s0);
847         &mov    ($key,16);
848         &align  (4);
849         &set_label("prefetch_te");
850                 &mov    ($s0,&DWP(0,"ebp"));
851                 &mov    ($s1,&DWP(32,"ebp"));
852                 &mov    ($s2,&DWP(64,"ebp"));
853                 &mov    ($s3,&DWP(96,"ebp"));
854                 &lea    ("ebp",&DWP(128,"ebp"));
855                 &dec    ($key);
856         &jnz    (&label("prefetch_te"));
857         &sub    ("ebp",2048);
858
859         &mov    ($s2,$_len);
860         &mov    ($key,$_ivp);
861         &test   ($s2,0xFFFFFFF0);
862         &jz     (&label("enc_tail"));           # short input...
863
864         &mov    ($s0,&DWP(0,$key));             # load iv
865         &mov    ($s1,&DWP(4,$key));
866
867         &align  (4);
868         &set_label("enc_loop");
869                 &mov    ($s2,&DWP(8,$key));
870                 &mov    ($s3,&DWP(12,$key));
871
872                 &xor    ($s0,&DWP(0,$acc));     # xor input data
873                 &xor    ($s1,&DWP(4,$acc));
874                 &xor    ($s2,&DWP(8,$acc));
875                 &xor    ($s3,&DWP(12,$acc));
876
877                 &mov    ($key,$_key);           # load key
878                 &call   ("_x86_AES_encrypt");
879
880                 &mov    ($acc,$_inp);           # load inp
881                 &mov    ($key,$_out);           # load out
882
883                 &mov    (&DWP(0,$key),$s0);     # save output data
884                 &mov    (&DWP(4,$key),$s1);
885                 &mov    (&DWP(8,$key),$s2);
886                 &mov    (&DWP(12,$key),$s3);
887
888                 &mov    ($s2,$_len);            # load len
889
890                 &lea    ($acc,&DWP(16,$acc));
891                 &mov    ($_inp,$acc);           # save inp
892
893                 &lea    ($s3,&DWP(16,$key));
894                 &mov    ($_out,$s3);            # save out
895
896                 &sub    ($s2,16);
897                 &test   ($s2,0xFFFFFFF0);
898                 &mov    ($_len,$s2);            # save len
899         &jnz    (&label("enc_loop"));
900         &test   ($s2,15);
901         &jnz    (&label("enc_tail"));
902         &mov    ($acc,$_ivp);           # load ivp
903         &mov    ($s2,&DWP(8,$key));     # restore last dwords
904         &mov    ($s3,&DWP(12,$key));
905         &mov    (&DWP(0,$acc),$s0);     # save ivec
906         &mov    (&DWP(4,$acc),$s1);
907         &mov    (&DWP(8,$acc),$s2);
908         &mov    (&DWP(12,$acc),$s3);
909         &mov    ("esp",$_esp);
910     &set_label("enc_out");
911         &function_end_A();
912
913     &align      (4);
914     &set_label("enc_tail");
915         &push   ($key eq "edi" ? $key : "");    # push ivp
916         &pushf  ();
917         &mov    ($key,$_out);                   # load out
918         &mov    ($s1,16);
919         &sub    ($s1,$s2);
920         &cmp    ($key,$acc);                    # compare with inp
921         &je     (&label("enc_in_place"));
922         &data_word(0x90A4F3FC); # cld; rep movsb; nop   # copy input
923         &jmp    (&label("enc_skip_in_place"));
924     &set_label("enc_in_place");
925         &lea    ($key,&DWP(0,$key,$s2));
926     &set_label("enc_skip_in_place");
927         &mov    ($s2,$s1);
928         &xor    ($s0,$s0);
929         &data_word(0x90AAF3FC); # cld; rep stosb; nop   # zero tail
930         &popf   ();
931         &pop    ($key);                         # pop ivp
932
933         &mov    ($acc,$_out);                   # output as input
934         &mov    ($s0,&DWP(0,$key));
935         &mov    ($s1,&DWP(4,$key));
936         &mov    ($_len,16);                     # len=16
937         &jmp    (&label("enc_loop"));           # one more spin...
938
939 #----------------------------- DECRYPT -----------------------------#
940 &align  (4);
941 &set_label("DECRYPT");
942         &lea    ("ebp",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
943
944         # allocate aligned stack frame...
945         &lea    ($key,&DWP(-64,"esp"));
946         &and    ($key,-64);
947
948         # ... and make sure it doesn't alias with AES_Td modulo 4096
949         &mov    ($s1,"ebp");
950         &mov    ($s3,$key);
951         &and    ($s1,0xfff);            # t = %ebp&0xfff
952         &and    ($s3,0xfff);            # p = %esp&0xfff
953
954         &cmp    ($s3,$s1);              # if (p<t) goto ok
955         &jb     (&label("td_ok"));
956         &lea    ($acc,&DWP(3072,$s1));
957         &cmp    ($s3,$acc);             # if (p>=(t+3072)) goto ok
958         &jae    (&label("td_ok"));
959         &sub    ($s1,$s3);              # t -= p
960         &lea    ($key,&DWP(-64,$key,$s1));# %esp -= (p-t)+64
961         &set_label("td_ok");
962
963         &mov    ($s0,&wparam(0));       # load inp
964         &mov    ($s1,&wparam(1));       # load out
965         &mov    ($s3,&wparam(3));       # load key
966         &mov    ($acc,&wparam(4));      # load ivp
967
968         &exch   ("esp",$key);
969         &add    ("esp",4);              # reserve for return address!
970         &mov    ($_esp,$key);           # save %esp
971
972         &mov    ($_inp,$s0);            # save copy of inp
973         &mov    ($_out,$s1);            # save copy of out
974         &mov    ($_len,$s2);            # save copy of len
975         &mov    ($_key,$s3);            # save copy of key
976         &mov    ($_ivp,$acc);           # save copy of ivp
977
978         &mov    ($acc,$s0);
979         &mov    ($key,24);
980         &align  (4);
981         &set_label("prefetch_td");
982                 &mov    ($s0,&DWP(0,"ebp"));
983                 &mov    ($s1,&DWP(32,"ebp"));
984                 &mov    ($s2,&DWP(64,"ebp"));
985                 &mov    ($s3,&DWP(96,"ebp"));
986                 &lea    ("ebp",&DWP(128,"ebp"));
987                 &dec    ($key);
988         &jnz    (&label("prefetch_td"));
989         &sub    ("ebp",3072);
990
991         &cmp    ($acc,$_out);
992         &je     (&label("dec_in_place"));       # in-place processing...
993
994         &mov    ($key,$_ivp);           # load ivp
995         &mov    ($_tmp,$key);
996
997         &align  (4);
998         &set_label("dec_loop");
999                 &mov    ($s0,&DWP(0,$acc));     # read input
1000                 &mov    ($s1,&DWP(4,$acc));
1001                 &mov    ($s2,&DWP(8,$acc));
1002                 &mov    ($s3,&DWP(12,$acc));
1003
1004                 &mov    ($key,$_key);           # load key
1005                 &call   ("_x86_AES_decrypt");
1006
1007                 &mov    ($key,$_tmp);           # load ivp
1008                 &mov    ($acc,$_len);           # load len
1009                 &xor    ($s0,&DWP(0,$key));     # xor iv
1010                 &xor    ($s1,&DWP(4,$key));
1011                 &xor    ($s2,&DWP(8,$key));
1012                 &xor    ($s3,&DWP(12,$key));
1013
1014                 &sub    ($acc,16);
1015                 &jc     (&label("dec_partial"));
1016                 &mov    ($_len,$acc);           # save len
1017                 &mov    ($acc,$_inp);           # load inp
1018                 &mov    ($key,$_out);           # load out
1019
1020                 &mov    (&DWP(0,$key),$s0);     # write output
1021                 &mov    (&DWP(4,$key),$s1);
1022                 &mov    (&DWP(8,$key),$s2);
1023                 &mov    (&DWP(12,$key),$s3);
1024
1025                 &mov    ($_tmp,$acc);           # save ivp
1026                 &lea    ($acc,&DWP(16,$acc));
1027                 &mov    ($_inp,$acc);           # save inp
1028
1029                 &lea    ($key,&DWP(16,$key));
1030                 &mov    ($_out,$key);           # save out
1031
1032         &jnz    (&label("dec_loop"));
1033         &mov    ($key,$_tmp);           # load temp ivp
1034     &set_label("dec_end");
1035         &mov    ($acc,$_ivp);           # load user ivp
1036         &mov    ($s0,&DWP(0,$key));     # load iv
1037         &mov    ($s1,&DWP(4,$key));
1038         &mov    ($s2,&DWP(8,$key));
1039         &mov    ($s3,&DWP(12,$key));
1040         &mov    (&DWP(0,$acc),$s0);     # copy back to user
1041         &mov    (&DWP(4,$acc),$s1);
1042         &mov    (&DWP(8,$acc),$s2);
1043         &mov    (&DWP(12,$acc),$s3);
1044         &jmp    (&label("dec_out"));
1045
1046     &align      (4);
1047     &set_label("dec_partial");
1048         &lea    ($key,$ivec);
1049         &mov    (&DWP(0,$key),$s0);     # dump output to stack
1050         &mov    (&DWP(4,$key),$s1);
1051         &mov    (&DWP(8,$key),$s2);
1052         &mov    (&DWP(12,$key),$s3);
1053         &lea    ($s2 eq "ecx" ? $s2 : "",&DWP(16,$acc));
1054         &mov    ($acc eq "esi" ? $acc : "",$key);
1055         &mov    ($key eq "edi" ? $key : "",$_out);      # load out
1056         &pushf  ();
1057         &data_word(0x90A4F3FC); # cld; rep movsb; nop   # copy output
1058         &popf   ();
1059         &mov    ($key,$_inp);           # use inp as temp ivp
1060         &jmp    (&label("dec_end"));
1061
1062     &align      (4);
1063     &set_label("dec_in_place");
1064         &set_label("dec_in_place_loop");
1065                 &lea    ($key,$ivec);
1066                 &mov    ($s0,&DWP(0,$acc));     # read input
1067                 &mov    ($s1,&DWP(4,$acc));
1068                 &mov    ($s2,&DWP(8,$acc));
1069                 &mov    ($s3,&DWP(12,$acc));
1070
1071                 &mov    (&DWP(0,$key),$s0);     # copy to temp
1072                 &mov    (&DWP(4,$key),$s1);
1073                 &mov    (&DWP(8,$key),$s2);
1074                 &mov    (&DWP(12,$key),$s3);
1075
1076                 &mov    ($key,$_key);           # load key
1077                 &call   ("_x86_AES_decrypt");
1078
1079                 &mov    ($key,$_ivp);           # load ivp
1080                 &mov    ($acc,$_out);           # load out
1081                 &xor    ($s0,&DWP(0,$key));     # xor iv
1082                 &xor    ($s1,&DWP(4,$key));
1083                 &xor    ($s2,&DWP(8,$key));
1084                 &xor    ($s3,&DWP(12,$key));
1085
1086                 &mov    (&DWP(0,$acc),$s0);     # write output
1087                 &mov    (&DWP(4,$acc),$s1);
1088                 &mov    (&DWP(8,$acc),$s2);
1089                 &mov    (&DWP(12,$acc),$s3);
1090
1091                 &lea    ($acc,&DWP(16,$acc));
1092                 &mov    ($_out,$acc);           # save out
1093
1094                 &lea    ($acc,$ivec);
1095                 &mov    ($s0,&DWP(0,$acc));     # read temp
1096                 &mov    ($s1,&DWP(4,$acc));
1097                 &mov    ($s2,&DWP(8,$acc));
1098                 &mov    ($s3,&DWP(12,$acc));
1099
1100                 &mov    (&DWP(0,$key),$s0);     # copy iv
1101                 &mov    (&DWP(4,$key),$s1);
1102                 &mov    (&DWP(8,$key),$s2);
1103                 &mov    (&DWP(12,$key),$s3);
1104
1105                 &mov    ($acc,$_inp);           # load inp
1106
1107                 &lea    ($acc,&DWP(16,$acc));
1108                 &mov    ($_inp,$acc);           # save inp
1109
1110                 &mov    ($s2,$_len);            # load len
1111                 &sub    ($s2,16);
1112                 &jc     (&label("dec_in_place_partial"));
1113                 &mov    ($_len,$s2);            # save len
1114         &jnz    (&label("dec_in_place_loop"));
1115         &jmp    (&label("dec_out"));
1116
1117     &align      (4);
1118     &set_label("dec_in_place_partial");
1119         # one can argue if this is actually required...
1120         &mov    ($key eq "edi" ? $key : "",$_out);
1121         &lea    ($acc eq "esi" ? $acc : "",$ivec);
1122         &lea    ($key,&DWP(0,$key,$s2));
1123         &lea    ($acc,&DWP(16,$acc,$s2));
1124         &neg    ($s2 eq "ecx" ? $s2 : "");
1125         &pushf  ();
1126         &data_word(0x90A4F3FC); # cld; rep movsb; nop   # restore tail
1127         &popf   ();
1128
1129     &align      (4);
1130     &set_label("dec_out");
1131     &mov        ("esp",$_esp);
1132 &function_end("AES_cbc_encrypt");
1133 }
1134
1135 #------------------------------------------------------------------#
1136
1137 sub enckey()
1138 {
1139         &movz   ("esi",&LB("edx"));             # rk[i]>>0
1140         &mov    ("ebx",&DWP(2,"ebp","esi",8));
1141         &movz   ("esi",&HB("edx"));             # rk[i]>>8
1142         &and    ("ebx",0xFF000000);
1143         &xor    ("eax","ebx");
1144
1145         &mov    ("ebx",&DWP(2,"ebp","esi",8));
1146         &shr    ("edx",16);
1147         &and    ("ebx",0x000000FF);
1148         &movz   ("esi",&LB("edx"));             # rk[i]>>16
1149         &xor    ("eax","ebx");
1150
1151         &mov    ("ebx",&DWP(0,"ebp","esi",8));
1152         &movz   ("esi",&HB("edx"));             # rk[i]>>24
1153         &and    ("ebx",0x0000FF00);
1154         &xor    ("eax","ebx");
1155
1156         &mov    ("ebx",&DWP(0,"ebp","esi",8));
1157         &and    ("ebx",0x00FF0000);
1158         &xor    ("eax","ebx");
1159
1160         &xor    ("eax",&DWP(2048,"ebp","ecx",4));       # rcon
1161 }
1162
1163 # int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
1164 #                        AES_KEY *key)
1165 &public_label("AES_Te");
1166 &function_begin("AES_set_encrypt_key");
1167         &mov    ("esi",&wparam(0));             # user supplied key
1168         &mov    ("edi",&wparam(2));             # private key schedule
1169
1170         &test   ("esi",-1);
1171         &jz     (&label("badpointer"));
1172         &test   ("edi",-1);
1173         &jz     (&label("badpointer"));
1174
1175         &call   (&label("pic_point"));
1176         &set_label("pic_point");
1177         &blindpop("ebp");
1178         &lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
1179
1180         &mov    ("ecx",&wparam(1));             # number of bits in key
1181         &cmp    ("ecx",128);
1182         &je     (&label("10rounds"));
1183         &cmp    ("ecx",192);
1184         &je     (&label("12rounds"));
1185         &cmp    ("ecx",256);
1186         &je     (&label("14rounds"));
1187         &mov    ("eax",-2);                     # invalid number of bits
1188         &jmp    (&label("exit"));
1189
1190     &set_label("10rounds");
1191         &mov    ("eax",&DWP(0,"esi"));          # copy first 4 dwords
1192         &mov    ("ebx",&DWP(4,"esi"));
1193         &mov    ("ecx",&DWP(8,"esi"));
1194         &mov    ("edx",&DWP(12,"esi"));
1195         &mov    (&DWP(0,"edi"),"eax");
1196         &mov    (&DWP(4,"edi"),"ebx");
1197         &mov    (&DWP(8,"edi"),"ecx");
1198         &mov    (&DWP(12,"edi"),"edx");
1199
1200         &xor    ("ecx","ecx");
1201         &jmp    (&label("10shortcut"));
1202
1203         &align  (4);
1204         &set_label("10loop");
1205                 &mov    ("eax",&DWP(0,"edi"));          # rk[0]
1206                 &mov    ("edx",&DWP(12,"edi"));         # rk[3]
1207         &set_label("10shortcut");
1208                 &enckey ();
1209
1210                 &mov    (&DWP(16,"edi"),"eax");         # rk[4]
1211                 &xor    ("eax",&DWP(4,"edi"));
1212                 &mov    (&DWP(20,"edi"),"eax");         # rk[5]
1213                 &xor    ("eax",&DWP(8,"edi"));
1214                 &mov    (&DWP(24,"edi"),"eax");         # rk[6]
1215                 &xor    ("eax",&DWP(12,"edi"));
1216                 &mov    (&DWP(28,"edi"),"eax");         # rk[7]
1217                 &inc    ("ecx");
1218                 &add    ("edi",16);
1219                 &cmp    ("ecx",10);
1220         &jl     (&label("10loop"));
1221
1222         &mov    (&DWP(80,"edi"),10);            # setup number of rounds
1223         &xor    ("eax","eax");
1224         &jmp    (&label("exit"));
1225                 
1226     &set_label("12rounds");
1227         &mov    ("eax",&DWP(0,"esi"));          # copy first 6 dwords
1228         &mov    ("ebx",&DWP(4,"esi"));
1229         &mov    ("ecx",&DWP(8,"esi"));
1230         &mov    ("edx",&DWP(12,"esi"));
1231         &mov    (&DWP(0,"edi"),"eax");
1232         &mov    (&DWP(4,"edi"),"ebx");
1233         &mov    (&DWP(8,"edi"),"ecx");
1234         &mov    (&DWP(12,"edi"),"edx");
1235         &mov    ("ecx",&DWP(16,"esi"));
1236         &mov    ("edx",&DWP(20,"esi"));
1237         &mov    (&DWP(16,"edi"),"ecx");
1238         &mov    (&DWP(20,"edi"),"edx");
1239
1240         &xor    ("ecx","ecx");
1241         &jmp    (&label("12shortcut"));
1242
1243         &align  (4);
1244         &set_label("12loop");
1245                 &mov    ("eax",&DWP(0,"edi"));          # rk[0]
1246                 &mov    ("edx",&DWP(20,"edi"));         # rk[5]
1247         &set_label("12shortcut");
1248                 &enckey ();
1249
1250                 &mov    (&DWP(24,"edi"),"eax");         # rk[6]
1251                 &xor    ("eax",&DWP(4,"edi"));
1252                 &mov    (&DWP(28,"edi"),"eax");         # rk[7]
1253                 &xor    ("eax",&DWP(8,"edi"));
1254                 &mov    (&DWP(32,"edi"),"eax");         # rk[8]
1255                 &xor    ("eax",&DWP(12,"edi"));
1256                 &mov    (&DWP(36,"edi"),"eax");         # rk[9]
1257
1258                 &cmp    ("ecx",7);
1259                 &je     (&label("12break"));
1260                 &inc    ("ecx");
1261
1262                 &xor    ("eax",&DWP(16,"edi"));
1263                 &mov    (&DWP(40,"edi"),"eax");         # rk[10]
1264                 &xor    ("eax",&DWP(20,"edi"));
1265                 &mov    (&DWP(44,"edi"),"eax");         # rk[11]
1266
1267                 &add    ("edi",24);
1268         &jmp    (&label("12loop"));
1269
1270         &set_label("12break");
1271         &mov    (&DWP(72,"edi"),12);            # setup number of rounds
1272         &xor    ("eax","eax");
1273         &jmp    (&label("exit"));
1274
1275     &set_label("14rounds");
1276         &mov    ("eax",&DWP(0,"esi"));          # copy first 8 dwords
1277         &mov    ("ebx",&DWP(4,"esi"));
1278         &mov    ("ecx",&DWP(8,"esi"));
1279         &mov    ("edx",&DWP(12,"esi"));
1280         &mov    (&DWP(0,"edi"),"eax");
1281         &mov    (&DWP(4,"edi"),"ebx");
1282         &mov    (&DWP(8,"edi"),"ecx");
1283         &mov    (&DWP(12,"edi"),"edx");
1284         &mov    ("eax",&DWP(16,"esi"));
1285         &mov    ("ebx",&DWP(20,"esi"));
1286         &mov    ("ecx",&DWP(24,"esi"));
1287         &mov    ("edx",&DWP(28,"esi"));
1288         &mov    (&DWP(16,"edi"),"eax");
1289         &mov    (&DWP(20,"edi"),"ebx");
1290         &mov    (&DWP(24,"edi"),"ecx");
1291         &mov    (&DWP(28,"edi"),"edx");
1292
1293         &xor    ("ecx","ecx");
1294         &jmp    (&label("14shortcut"));
1295
1296         &align  (4);
1297         &set_label("14loop");
1298                 &mov    ("edx",&DWP(28,"edi"));         # rk[7]
1299         &set_label("14shortcut");
1300                 &mov    ("eax",&DWP(0,"edi"));          # rk[0]
1301
1302                 &enckey ();
1303
1304                 &mov    (&DWP(32,"edi"),"eax");         # rk[8]
1305                 &xor    ("eax",&DWP(4,"edi"));
1306                 &mov    (&DWP(36,"edi"),"eax");         # rk[9]
1307                 &xor    ("eax",&DWP(8,"edi"));
1308                 &mov    (&DWP(40,"edi"),"eax");         # rk[10]
1309                 &xor    ("eax",&DWP(12,"edi"));
1310                 &mov    (&DWP(44,"edi"),"eax");         # rk[11]
1311
1312                 &cmp    ("ecx",6);
1313                 &je     (&label("14break"));
1314                 &inc    ("ecx");
1315
1316                 &mov    ("edx","eax");
1317                 &mov    ("eax",&DWP(16,"edi"));         # rk[4]
1318                 &movz   ("esi",&LB("edx"));             # rk[11]>>0
1319                 &mov    ("ebx",&DWP(2,"ebp","esi",8));
1320                 &movz   ("esi",&HB("edx"));             # rk[11]>>8
1321                 &and    ("ebx",0x000000FF);
1322                 &xor    ("eax","ebx");
1323
1324                 &mov    ("ebx",&DWP(0,"ebp","esi",8));
1325                 &shr    ("edx",16);
1326                 &and    ("ebx",0x0000FF00);
1327                 &movz   ("esi",&LB("edx"));             # rk[11]>>16
1328                 &xor    ("eax","ebx");
1329
1330                 &mov    ("ebx",&DWP(0,"ebp","esi",8));
1331                 &movz   ("esi",&HB("edx"));             # rk[11]>>24
1332                 &and    ("ebx",0x00FF0000);
1333                 &xor    ("eax","ebx");
1334
1335                 &mov    ("ebx",&DWP(2,"ebp","esi",8));
1336                 &and    ("ebx",0xFF000000);
1337                 &xor    ("eax","ebx");
1338
1339                 &mov    (&DWP(48,"edi"),"eax");         # rk[12]
1340                 &xor    ("eax",&DWP(20,"edi"));
1341                 &mov    (&DWP(52,"edi"),"eax");         # rk[13]
1342                 &xor    ("eax",&DWP(24,"edi"));
1343                 &mov    (&DWP(56,"edi"),"eax");         # rk[14]
1344                 &xor    ("eax",&DWP(28,"edi"));
1345                 &mov    (&DWP(60,"edi"),"eax");         # rk[15]
1346
1347                 &add    ("edi",32);
1348         &jmp    (&label("14loop"));
1349
1350         &set_label("14break");
1351         &mov    (&DWP(48,"edi"),14);            # setup number of rounds
1352         &xor    ("eax","eax");
1353         &jmp    (&label("exit"));
1354
1355     &set_label("badpointer");
1356         &mov    ("eax",-1);
1357     &set_label("exit");
1358 &function_end("AES_set_encrypt_key");
1359
1360 sub deckey()
1361 { my ($i,$ptr,$te,$td) = @_;
1362
1363         &mov    ("eax",&DWP($i,$ptr));
1364         &mov    ("edx","eax");
1365         &movz   ("ebx",&HB("eax"));
1366         &shr    ("edx",16);
1367         &and    ("eax",0xFF);
1368         &movz   ("eax",&BP(2,$te,"eax",8));
1369         &movz   ("ebx",&BP(2,$te,"ebx",8));
1370         &mov    ("eax",&DWP(0,$td,"eax",8));
1371         &xor    ("eax",&DWP(3,$td,"ebx",8));
1372         &movz   ("ebx",&HB("edx"));
1373         &and    ("edx",0xFF);
1374         &movz   ("edx",&BP(2,$te,"edx",8));
1375         &movz   ("ebx",&BP(2,$te,"ebx",8));
1376         &xor    ("eax",&DWP(2,$td,"edx",8));
1377         &xor    ("eax",&DWP(1,$td,"ebx",8));
1378         &mov    (&DWP($i,$ptr),"eax");
1379 }
1380
1381 # int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
1382 #                        AES_KEY *key)
1383 &public_label("AES_Td");
1384 &public_label("AES_Te");
1385 &function_begin_B("AES_set_decrypt_key");
1386         &mov    ("eax",&wparam(0));
1387         &mov    ("ecx",&wparam(1));
1388         &mov    ("edx",&wparam(2));
1389         &sub    ("esp",12);
1390         &mov    (&DWP(0,"esp"),"eax");
1391         &mov    (&DWP(4,"esp"),"ecx");
1392         &mov    (&DWP(8,"esp"),"edx");
1393         &call   ("AES_set_encrypt_key");
1394         &add    ("esp",12);
1395         &cmp    ("eax",0);
1396         &je     (&label("proceed"));
1397         &ret    ();
1398
1399     &set_label("proceed");
1400         &push   ("ebp");
1401         &push   ("ebx");
1402         &push   ("esi");
1403         &push   ("edi");
1404
1405         &mov    ("esi",&wparam(2));
1406         &mov    ("ecx",&DWP(240,"esi"));        # pull number of rounds
1407         &lea    ("ecx",&DWP(0,"","ecx",4));
1408         &lea    ("edi",&DWP(0,"esi","ecx",4));  # pointer to last chunk
1409
1410         &align  (4);
1411         &set_label("invert");                   # invert order of chunks
1412                 &mov    ("eax",&DWP(0,"esi"));
1413                 &mov    ("ebx",&DWP(4,"esi"));
1414                 &mov    ("ecx",&DWP(0,"edi"));
1415                 &mov    ("edx",&DWP(4,"edi"));
1416                 &mov    (&DWP(0,"edi"),"eax");
1417                 &mov    (&DWP(4,"edi"),"ebx");
1418                 &mov    (&DWP(0,"esi"),"ecx");
1419                 &mov    (&DWP(4,"esi"),"edx");
1420                 &mov    ("eax",&DWP(8,"esi"));
1421                 &mov    ("ebx",&DWP(12,"esi"));
1422                 &mov    ("ecx",&DWP(8,"edi"));
1423                 &mov    ("edx",&DWP(12,"edi"));
1424                 &mov    (&DWP(8,"edi"),"eax");
1425                 &mov    (&DWP(12,"edi"),"ebx");
1426                 &mov    (&DWP(8,"esi"),"ecx");
1427                 &mov    (&DWP(12,"esi"),"edx");
1428                 &add    ("esi",16);
1429                 &sub    ("edi",16);
1430                 &cmp    ("esi","edi");
1431         &jne    (&label("invert"));
1432
1433         &call   (&label("pic_point"));
1434         &set_label("pic_point");
1435         blindpop("ebp");
1436         &lea    ("edi",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
1437         &lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
1438
1439         &mov    ("esi",&wparam(2));
1440         &mov    ("ecx",&DWP(240,"esi"));        # pull number of rounds
1441         &dec    ("ecx");
1442         &align  (4);
1443         &set_label("permute");                  # permute the key schedule
1444                 &add    ("esi",16);
1445                 &deckey (0,"esi","ebp","edi");
1446                 &deckey (4,"esi","ebp","edi");
1447                 &deckey (8,"esi","ebp","edi");
1448                 &deckey (12,"esi","ebp","edi");
1449                 &dec    ("ecx");
1450         &jnz    (&label("permute"));
1451
1452         &xor    ("eax","eax");                  # return success
1453 &function_end("AES_set_decrypt_key");
1454
1455 &asm_finish();