No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

2988 líneas
101 KiB

  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # Version 4.3.
  11. #
  12. # You might fail to appreciate this module performance from the first
  13. # try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
  14. # to be *the* best Intel C compiler without -KPIC, performance appears
  15. # to be virtually identical... But try to re-configure with shared
  16. # library support... Aha! Intel compiler "suddenly" lags behind by 30%
  17. # [on P4, more on others]:-) And if compared to position-independent
  18. # code generated by GNU C, this code performs *more* than *twice* as
  19. # fast! Yes, all this buzz about PIC means that unlike other hand-
  20. # coded implementations, this one was explicitly designed to be safe
  21. # to use even in shared library context... This also means that this
  22. # code isn't necessarily absolutely fastest "ever," because in order
  23. # to achieve position independence an extra register has to be
  24. # off-loaded to stack, which affects the benchmark result.
  25. #
  26. # Special note about instruction choice. Do you recall RC4_INT code
  27. # performing poorly on P4? It might be the time to figure out why.
  28. # RC4_INT code implies effective address calculations in base+offset*4
  29. # form. Trouble is that it seems that offset scaling turned to be
  30. # critical path... At least eliminating scaling resulted in 2.8x RC4
  31. # performance improvement [as you might recall]. As AES code is hungry
  32. # for scaling too, I [try to] avoid the latter by favoring off-by-2
  33. # shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
  34. #
  35. # As was shown by Dean Gaudet <dean@arctic.org>, the above note turned
  36. # void. Performance improvement with off-by-2 shifts was observed on
  37. # intermediate implementation, which was spilling yet another register
  38. # to stack... Final offset*4 code below runs just a tad faster on P4,
  39. # but exhibits up to 10% improvement on other cores.
  40. #
  41. # Second version is "monolithic" replacement for aes_core.c, which in
  42. # addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key.
  43. # This made it possible to implement little-endian variant of the
  44. # algorithm without modifying the base C code. Motivating factor for
  45. # the undertaken effort was that it appeared that in tight IA-32
  46. # register window little-endian flavor could achieve slightly higher
  47. # Instruction Level Parallelism, and it indeed resulted in up to 15%
  48. # better performance on most recent µ-archs...
  49. #
  50. # Third version adds AES_cbc_encrypt implementation, which resulted in
  51. # up to 40% performance imrovement of CBC benchmark results. 40% was
  52. # observed on P4 core, where "overall" imrovement coefficient, i.e. if
  53. # compared to PIC generated by GCC and in CBC mode, was observed to be
  54. # as large as 4x:-) CBC performance is virtually identical to ECB now
  55. # and on some platforms even better, e.g. 17.6 "small" cycles/byte on
  56. # Opteron, because certain function prologues and epilogues are
  57. # effectively taken out of the loop...
  58. #
  59. # Version 3.2 implements compressed tables and prefetch of these tables
  60. # in CBC[!] mode. Former means that 3/4 of table references are now
  61. # misaligned, which unfortunately has negative impact on elder IA-32
  62. # implementations, Pentium suffered 30% penalty, PIII - 10%.
  63. #
  64. # Version 3.3 avoids L1 cache aliasing between stack frame and
  65. # S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The
  66. # latter is achieved by copying the key schedule to controlled place in
  67. # stack. This unfortunately has rather strong impact on small block CBC
  68. # performance, ~2x deterioration on 16-byte block if compared to 3.3.
  69. #
  70. # Version 3.5 checks if there is L1 cache aliasing between user-supplied
  71. # key schedule and S-boxes and abstains from copying the former if
  72. # there is no. This allows end-user to consciously retain small block
  73. # performance by aligning key schedule in specific manner.
  74. #
  75. # Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB.
  76. #
  77. # Current ECB performance numbers for 128-bit key in CPU cycles per
  78. # processed byte [measure commonly used by AES benchmarkers] are:
  79. #
  80. # small footprint fully unrolled
  81. # P4 24 22
  82. # AMD K8 20 19
  83. # PIII 25 23
  84. # Pentium 81 78
  85. #
  86. # Version 3.7 reimplements outer rounds as "compact." Meaning that
  87. # first and last rounds reference compact 256 bytes S-box. This means
  88. # that first round consumes a lot more CPU cycles and that encrypt
  89. # and decrypt performance becomes asymmetric. Encrypt performance
  90. # drops by 10-12%, while decrypt - by 20-25%:-( 256 bytes S-box is
  91. # aggressively pre-fetched.
  92. #
  93. # Version 4.0 effectively rolls back to 3.6 and instead implements
  94. # additional set of functions, _[x86|sse]_AES_[en|de]crypt_compact,
  95. # which use exclusively 256 byte S-box. These functions are to be
  96. # called in modes not concealing plain text, such as ECB, or when
  97. # we're asked to process smaller amount of data [or unconditionally
  98. # on hyper-threading CPU]. Currently it's called unconditionally from
  99. # AES_[en|de]crypt, which affects all modes, but CBC. CBC routine
  100. # still needs to be modified to switch between slower and faster
  101. # mode when appropriate... But in either case benchmark landscape
  102. # changes dramatically and below numbers are CPU cycles per processed
  103. # byte for 128-bit key.
  104. #
  105. # ECB encrypt ECB decrypt CBC large chunk
  106. # P4 52[54] 83[95] 23
  107. # AMD K8 46[41] 66[70] 18
  108. # PIII 41[50] 60[77] 24
  109. # Core 2 31[36] 45[64] 18.5
  110. # Atom 76[100] 96[138] 60
  111. # Pentium 115 150 77
  112. #
  113. # Version 4.1 switches to compact S-box even in key schedule setup.
  114. #
  115. # Version 4.2 prefetches compact S-box in every SSE round or in other
  116. # words every cache-line is *guaranteed* to be accessed within ~50
  117. # cycles window. Why just SSE? Because it's needed on hyper-threading
  118. # CPU! Which is also why it's prefetched with 64 byte stride. Best
  119. # part is that it has no negative effect on performance:-)
  120. #
  121. # Version 4.3 implements switch between compact and non-compact block
  122. # functions in AES_cbc_encrypt depending on how much data was asked
  123. # to be processed in one stroke.
  124. #
  125. ######################################################################
  126. # Timing attacks are classified in two classes: synchronous when
  127. # attacker consciously initiates cryptographic operation and collects
  128. # timing data of various character afterwards, and asynchronous when
  129. # malicious code is executed on same CPU simultaneously with AES,
  130. # instruments itself and performs statistical analysis of this data.
  131. #
  132. # As far as synchronous attacks go the root to the AES timing
  133. # vulnerability is twofold. Firstly, of 256 S-box elements at most 160
  134. # are referred to in single 128-bit block operation. Well, in C
  135. # implementation with 4 distinct tables it's actually as little as 40
  136. # references per 256 elements table, but anyway... Secondly, even
  137. # though S-box elements are clustered into smaller amount of cache-
  138. # lines, smaller than 160 and even 40, it turned out that for certain
  139. # plain-text pattern[s] or simply put chosen plain-text and given key
  140. # few cache-lines remain unaccessed during block operation. Now, if
  141. # attacker can figure out this access pattern, he can deduct the key
  142. # [or at least part of it]. The natural way to mitigate this kind of
  143. # attacks is to minimize the amount of cache-lines in S-box and/or
  144. # prefetch them to ensure that every one is accessed for more uniform
  145. # timing. But note that *if* plain-text was concealed in such way that
  146. # input to block function is distributed *uniformly*, then attack
  147. # wouldn't apply. Now note that some encryption modes, most notably
  148. # CBC, do mask the plain-text in this exact way [secure cipher output
  149. # is distributed uniformly]. Yes, one still might find input that
  150. # would reveal the information about given key, but if amount of
  151. # candidate inputs to be tried is larger than amount of possible key
  152. # combinations then attack becomes infeasible. This is why revised
  153. # AES_cbc_encrypt "dares" to switch to larger S-box when larger chunk
  154. # of data is to be processed in one stroke. The current size limit of
  155. # 512 bytes is chosen to provide same [diminishigly low] probability
  156. # for cache-line to remain untouched in large chunk operation with
  157. # large S-box as for single block operation with compact S-box and
  158. # surely needs more careful consideration...
  159. #
  160. # As for asynchronous attacks. There are two flavours: attacker code
  161. # being interleaved with AES on hyper-threading CPU at *instruction*
  162. # level, and two processes time sharing single core. As for latter.
  163. # Two vectors. 1. Given that attacker process has higher priority,
  164. # yield execution to process performing AES just before timer fires
  165. # off the scheduler, immediately regain control of CPU and analyze the
  166. # cache state. For this attack to be efficient attacker would have to
  167. # effectively slow down the operation by several *orders* of magnitute,
  168. # by ratio of time slice to duration of handful of AES rounds, which
  169. # unlikely to remain unnoticed. Not to mention that this also means
  170. # that he would spend correspondigly more time to collect enough
  171. # statistical data to mount the attack. It's probably appropriate to
  172. # say that if adeversary reckons that this attack is beneficial and
  173. # risks to be noticed, you probably have larger problems having him
  174. # mere opportunity. In other words suggested code design expects you
  175. # to preclude/mitigate this attack by overall system security design.
  176. # 2. Attacker manages to make his code interrupt driven. In order for
  177. # this kind of attack to be feasible, interrupt rate has to be high
  178. # enough, again comparable to duration of handful of AES rounds. But
  179. # is there interrupt source of such rate? Hardly, not even 1Gbps NIC
  180. # generates interrupts at such raging rate...
  181. #
  182. # And now back to the former, hyper-threading CPU or more specifically
  183. # Intel P4. Recall that asynchronous attack implies that malicious
  184. # code instruments itself. And naturally instrumentation granularity
  185. # has be noticeably lower than duration of codepath accessing S-box.
  186. # Given that all cache-lines are accessed during that time that is.
  187. # Current implementation accesses *all* cache-lines within ~50 cycles
  188. # window, which is actually *less* than RDTSC latency on Intel P4!
  189. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  190. push(@INC,"${dir}","${dir}../../perlasm");
  191. require "x86asm.pl";
  192. &asm_init($ARGV[0],"aes-586.pl",$x86only = $ARGV[$#ARGV] eq "386");
  193. &static_label("AES_Te");
  194. &static_label("AES_Td");
  195. $s0="eax";
  196. $s1="ebx";
  197. $s2="ecx";
  198. $s3="edx";
  199. $key="edi";
  200. $acc="esi";
  201. $tbl="ebp";
  202. # stack frame layout in _[x86|sse]_AES_* routines, frame is allocated
  203. # by caller
  204. $__ra=&DWP(0,"esp"); # return address
  205. $__s0=&DWP(4,"esp"); # s0 backing store
  206. $__s1=&DWP(8,"esp"); # s1 backing store
  207. $__s2=&DWP(12,"esp"); # s2 backing store
  208. $__s3=&DWP(16,"esp"); # s3 backing store
  209. $__key=&DWP(20,"esp"); # pointer to key schedule
  210. $__end=&DWP(24,"esp"); # pointer to end of key schedule
  211. $__tbl=&DWP(28,"esp"); # %ebp backing store
  212. # stack frame layout in AES_[en|crypt] routines, which differs from
  213. # above by 4 and overlaps by %ebp backing store
  214. $_tbl=&DWP(24,"esp");
  215. $_esp=&DWP(28,"esp");
  216. sub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
  217. $speed_limit=512; # chunks smaller than $speed_limit are
  218. # processed with compact routine in CBC mode
  219. $small_footprint=1; # $small_footprint=1 code is ~5% slower [on
  220. # recent µ-archs], but ~5 times smaller!
  221. # I favor compact code to minimize cache
  222. # contention and in hope to "collect" 5% back
  223. # in real-life applications...
  224. $vertical_spin=0; # shift "verticaly" defaults to 0, because of
  225. # its proof-of-concept status...
  226. # Note that there is no decvert(), as well as last encryption round is
  227. # performed with "horizontal" shifts. This is because this "vertical"
  228. # implementation [one which groups shifts on a given $s[i] to form a
  229. # "column," unlike "horizontal" one, which groups shifts on different
  230. # $s[i] to form a "row"] is work in progress. It was observed to run
  231. # few percents faster on Intel cores, but not AMD. On AMD K8 core it's
  232. # whole 12% slower:-( So we face a trade-off... Shall it be resolved
  233. # some day? Till then the code is considered experimental and by
  234. # default remains dormant...
  235. sub encvert()
  236. { my ($te,@s) = @_;
  237. my ($v0,$v1) = ($acc,$key);
  238. &mov ($v0,$s[3]); # copy s3
  239. &mov (&DWP(4,"esp"),$s[2]); # save s2
  240. &mov ($v1,$s[0]); # copy s0
  241. &mov (&DWP(8,"esp"),$s[1]); # save s1
  242. &movz ($s[2],&HB($s[0]));
  243. &and ($s[0],0xFF);
  244. &mov ($s[0],&DWP(0,$te,$s[0],8)); # s0>>0
  245. &shr ($v1,16);
  246. &mov ($s[3],&DWP(3,$te,$s[2],8)); # s0>>8
  247. &movz ($s[1],&HB($v1));
  248. &and ($v1,0xFF);
  249. &mov ($s[2],&DWP(2,$te,$v1,8)); # s0>>16
  250. &mov ($v1,$v0);
  251. &mov ($s[1],&DWP(1,$te,$s[1],8)); # s0>>24
  252. &and ($v0,0xFF);
  253. &xor ($s[3],&DWP(0,$te,$v0,8)); # s3>>0
  254. &movz ($v0,&HB($v1));
  255. &shr ($v1,16);
  256. &xor ($s[2],&DWP(3,$te,$v0,8)); # s3>>8
  257. &movz ($v0,&HB($v1));
  258. &and ($v1,0xFF);
  259. &xor ($s[1],&DWP(2,$te,$v1,8)); # s3>>16
  260. &mov ($v1,&DWP(4,"esp")); # restore s2
  261. &xor ($s[0],&DWP(1,$te,$v0,8)); # s3>>24
  262. &mov ($v0,$v1);
  263. &and ($v1,0xFF);
  264. &xor ($s[2],&DWP(0,$te,$v1,8)); # s2>>0
  265. &movz ($v1,&HB($v0));
  266. &shr ($v0,16);
  267. &xor ($s[1],&DWP(3,$te,$v1,8)); # s2>>8
  268. &movz ($v1,&HB($v0));
  269. &and ($v0,0xFF);
  270. &xor ($s[0],&DWP(2,$te,$v0,8)); # s2>>16
  271. &mov ($v0,&DWP(8,"esp")); # restore s1
  272. &xor ($s[3],&DWP(1,$te,$v1,8)); # s2>>24
  273. &mov ($v1,$v0);
  274. &and ($v0,0xFF);
  275. &xor ($s[1],&DWP(0,$te,$v0,8)); # s1>>0
  276. &movz ($v0,&HB($v1));
  277. &shr ($v1,16);
  278. &xor ($s[0],&DWP(3,$te,$v0,8)); # s1>>8
  279. &movz ($v0,&HB($v1));
  280. &and ($v1,0xFF);
  281. &xor ($s[3],&DWP(2,$te,$v1,8)); # s1>>16
  282. &mov ($key,$__key); # reincarnate v1 as key
  283. &xor ($s[2],&DWP(1,$te,$v0,8)); # s1>>24
  284. }
  285. # Another experimental routine, which features "horizontal spin," but
  286. # eliminates one reference to stack. Strangely enough runs slower...
  287. sub enchoriz()
  288. { my ($v0,$v1) = ($key,$acc);
  289. &movz ($v0,&LB($s0)); # 3, 2, 1, 0*
  290. &rotr ($s2,8); # 8,11,10, 9
  291. &mov ($v1,&DWP(0,$te,$v0,8)); # 0
  292. &movz ($v0,&HB($s1)); # 7, 6, 5*, 4
  293. &rotr ($s3,16); # 13,12,15,14
  294. &xor ($v1,&DWP(3,$te,$v0,8)); # 5
  295. &movz ($v0,&HB($s2)); # 8,11,10*, 9
  296. &rotr ($s0,16); # 1, 0, 3, 2
  297. &xor ($v1,&DWP(2,$te,$v0,8)); # 10
  298. &movz ($v0,&HB($s3)); # 13,12,15*,14
  299. &xor ($v1,&DWP(1,$te,$v0,8)); # 15, t[0] collected
  300. &mov ($__s0,$v1); # t[0] saved
  301. &movz ($v0,&LB($s1)); # 7, 6, 5, 4*
  302. &shr ($s1,16); # -, -, 7, 6
  303. &mov ($v1,&DWP(0,$te,$v0,8)); # 4
  304. &movz ($v0,&LB($s3)); # 13,12,15,14*
  305. &xor ($v1,&DWP(2,$te,$v0,8)); # 14
  306. &movz ($v0,&HB($s0)); # 1, 0, 3*, 2
  307. &and ($s3,0xffff0000); # 13,12, -, -
  308. &xor ($v1,&DWP(1,$te,$v0,8)); # 3
  309. &movz ($v0,&LB($s2)); # 8,11,10, 9*
  310. &or ($s3,$s1); # 13,12, 7, 6
  311. &xor ($v1,&DWP(3,$te,$v0,8)); # 9, t[1] collected
  312. &mov ($s1,$v1); # s[1]=t[1]
  313. &movz ($v0,&LB($s0)); # 1, 0, 3, 2*
  314. &shr ($s2,16); # -, -, 8,11
  315. &mov ($v1,&DWP(2,$te,$v0,8)); # 2
  316. &movz ($v0,&HB($s3)); # 13,12, 7*, 6
  317. &xor ($v1,&DWP(1,$te,$v0,8)); # 7
  318. &movz ($v0,&HB($s2)); # -, -, 8*,11
  319. &xor ($v1,&DWP(0,$te,$v0,8)); # 8
  320. &mov ($v0,$s3);
  321. &shr ($v0,24); # 13
  322. &xor ($v1,&DWP(3,$te,$v0,8)); # 13, t[2] collected
  323. &movz ($v0,&LB($s2)); # -, -, 8,11*
  324. &shr ($s0,24); # 1*
  325. &mov ($s2,&DWP(1,$te,$v0,8)); # 11
  326. &xor ($s2,&DWP(3,$te,$s0,8)); # 1
  327. &mov ($s0,$__s0); # s[0]=t[0]
  328. &movz ($v0,&LB($s3)); # 13,12, 7, 6*
  329. &shr ($s3,16); # , ,13,12
  330. &xor ($s2,&DWP(2,$te,$v0,8)); # 6
  331. &mov ($key,$__key); # reincarnate v0 as key
  332. &and ($s3,0xff); # , ,13,12*
  333. &mov ($s3,&DWP(0,$te,$s3,8)); # 12
  334. &xor ($s3,$s2); # s[2]=t[3] collected
  335. &mov ($s2,$v1); # s[2]=t[2]
  336. }
  337. # More experimental code... SSE one... Even though this one eliminates
  338. # *all* references to stack, it's not faster...
  339. sub sse_encbody()
  340. {
  341. &movz ($acc,&LB("eax")); # 0
  342. &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 0
  343. &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2
  344. &movz ("edx",&HB("eax")); # 1
  345. &mov ("edx",&DWP(3,$tbl,"edx",8)); # 1
  346. &shr ("eax",16); # 5, 4
  347. &movz ($acc,&LB("ebx")); # 10
  348. &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 10
  349. &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8
  350. &movz ($acc,&HB("ebx")); # 11
  351. &xor ("edx",&DWP(1,$tbl,$acc,8)); # 11
  352. &shr ("ebx",16); # 15,14
  353. &movz ($acc,&HB("eax")); # 5
  354. &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 5
  355. &movq ("mm3",QWP(16,$key));
  356. &movz ($acc,&HB("ebx")); # 15
  357. &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 15
  358. &movd ("mm0","ecx"); # t[0] collected
  359. &movz ($acc,&LB("eax")); # 4
  360. &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 4
  361. &movd ("eax","mm2"); # 7, 6, 3, 2
  362. &movz ($acc,&LB("ebx")); # 14
  363. &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 14
  364. &movd ("ebx","mm6"); # 13,12, 9, 8
  365. &movz ($acc,&HB("eax")); # 3
  366. &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 3
  367. &movz ($acc,&HB("ebx")); # 9
  368. &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 9
  369. &movd ("mm1","ecx"); # t[1] collected
  370. &movz ($acc,&LB("eax")); # 2
  371. &mov ("ecx",&DWP(2,$tbl,$acc,8)); # 2
  372. &shr ("eax",16); # 7, 6
  373. &punpckldq ("mm0","mm1"); # t[0,1] collected
  374. &movz ($acc,&LB("ebx")); # 8
  375. &xor ("ecx",&DWP(0,$tbl,$acc,8)); # 8
  376. &shr ("ebx",16); # 13,12
  377. &movz ($acc,&HB("eax")); # 7
  378. &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 7
  379. &pxor ("mm0","mm3");
  380. &movz ("eax",&LB("eax")); # 6
  381. &xor ("edx",&DWP(2,$tbl,"eax",8)); # 6
  382. &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0
  383. &movz ($acc,&HB("ebx")); # 13
  384. &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 13
  385. &xor ("ecx",&DWP(24,$key)); # t[2]
  386. &movd ("mm4","ecx"); # t[2] collected
  387. &movz ("ebx",&LB("ebx")); # 12
  388. &xor ("edx",&DWP(0,$tbl,"ebx",8)); # 12
  389. &shr ("ecx",16);
  390. &movd ("eax","mm1"); # 5, 4, 1, 0
  391. &mov ("ebx",&DWP(28,$key)); # t[3]
  392. &xor ("ebx","edx");
  393. &movd ("mm5","ebx"); # t[3] collected
  394. &and ("ebx",0xffff0000);
  395. &or ("ebx","ecx");
  396. &punpckldq ("mm4","mm5"); # t[2,3] collected
  397. }
  398. ######################################################################
  399. # "Compact" block function
  400. ######################################################################
  401. sub enccompact()
  402. { my $Fn = \&mov;
  403. while ($#_>5) { pop(@_); $Fn=sub{}; }
  404. my ($i,$te,@s)=@_;
  405. my $tmp = $key;
  406. my $out = $i==3?$s[0]:$acc;
  407. # $Fn is used in first compact round and its purpose is to
  408. # void restoration of some values from stack, so that after
  409. # 4xenccompact with extra argument $key value is left there...
  410. if ($i==3) { &$Fn ($key,$__key); }##%edx
  411. else { &mov ($out,$s[0]); }
  412. &and ($out,0xFF);
  413. if ($i==1) { &shr ($s[0],16); }#%ebx[1]
  414. if ($i==2) { &shr ($s[0],24); }#%ecx[2]
  415. &movz ($out,&BP(-128,$te,$out,1));
  416. if ($i==3) { $tmp=$s[1]; }##%eax
  417. &movz ($tmp,&HB($s[1]));
  418. &movz ($tmp,&BP(-128,$te,$tmp,1));
  419. &shl ($tmp,8);
  420. &xor ($out,$tmp);
  421. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx
  422. else { &mov ($tmp,$s[2]);
  423. &shr ($tmp,16); }
  424. if ($i==2) { &and ($s[1],0xFF); }#%edx[2]
  425. &and ($tmp,0xFF);
  426. &movz ($tmp,&BP(-128,$te,$tmp,1));
  427. &shl ($tmp,16);
  428. &xor ($out,$tmp);
  429. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx
  430. elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2]
  431. else { &mov ($tmp,$s[3]);
  432. &shr ($tmp,24); }
  433. &movz ($tmp,&BP(-128,$te,$tmp,1));
  434. &shl ($tmp,24);
  435. &xor ($out,$tmp);
  436. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  437. if ($i==3) { &mov ($s[3],$acc); }
  438. &comment();
  439. }
  440. sub enctransform()
  441. { my @s = ($s0,$s1,$s2,$s3);
  442. my $i = shift;
  443. my $tmp = $tbl;
  444. my $r2 = $key ;
  445. &and ($tmp,$s[$i]);
  446. &lea ($r2,&DWP(0,$s[$i],$s[$i]));
  447. &mov ($acc,$tmp);
  448. &shr ($tmp,7);
  449. &and ($r2,0xfefefefe);
  450. &sub ($acc,$tmp);
  451. &mov ($tmp,$s[$i]);
  452. &and ($acc,0x1b1b1b1b);
  453. &rotr ($tmp,16);
  454. &xor ($acc,$r2); # r2
  455. &mov ($r2,$s[$i]);
  456. &xor ($s[$i],$acc); # r0 ^ r2
  457. &rotr ($r2,16+8);
  458. &xor ($acc,$tmp);
  459. &rotl ($s[$i],24);
  460. &xor ($acc,$r2);
  461. &mov ($tmp,0x80808080) if ($i!=1);
  462. &xor ($s[$i],$acc); # ROTATE(r2^r0,24) ^ r2
  463. }
  464. &function_begin_B("_x86_AES_encrypt_compact");
  465. # note that caller is expected to allocate stack frame for me!
  466. &mov ($__key,$key); # save key
  467. &xor ($s0,&DWP(0,$key)); # xor with key
  468. &xor ($s1,&DWP(4,$key));
  469. &xor ($s2,&DWP(8,$key));
  470. &xor ($s3,&DWP(12,$key));
  471. &mov ($acc,&DWP(240,$key)); # load key->rounds
  472. &lea ($acc,&DWP(-2,$acc,$acc));
  473. &lea ($acc,&DWP(0,$key,$acc,8));
  474. &mov ($__end,$acc); # end of key schedule
  475. # prefetch Te4
  476. &mov ($key,&DWP(0-128,$tbl));
  477. &mov ($acc,&DWP(32-128,$tbl));
  478. &mov ($key,&DWP(64-128,$tbl));
  479. &mov ($acc,&DWP(96-128,$tbl));
  480. &mov ($key,&DWP(128-128,$tbl));
  481. &mov ($acc,&DWP(160-128,$tbl));
  482. &mov ($key,&DWP(192-128,$tbl));
  483. &mov ($acc,&DWP(224-128,$tbl));
  484. &set_label("loop",16);
  485. &enccompact(0,$tbl,$s0,$s1,$s2,$s3,1);
  486. &enccompact(1,$tbl,$s1,$s2,$s3,$s0,1);
  487. &enccompact(2,$tbl,$s2,$s3,$s0,$s1,1);
  488. &enccompact(3,$tbl,$s3,$s0,$s1,$s2,1);
  489. &mov ($tbl,0x80808080);
  490. &enctransform(2);
  491. &enctransform(3);
  492. &enctransform(0);
  493. &enctransform(1);
  494. &mov ($key,$__key);
  495. &mov ($tbl,$__tbl);
  496. &add ($key,16); # advance rd_key
  497. &xor ($s0,&DWP(0,$key));
  498. &xor ($s1,&DWP(4,$key));
  499. &xor ($s2,&DWP(8,$key));
  500. &xor ($s3,&DWP(12,$key));
  501. &cmp ($key,$__end);
  502. &mov ($__key,$key);
  503. &jb (&label("loop"));
  504. &enccompact(0,$tbl,$s0,$s1,$s2,$s3);
  505. &enccompact(1,$tbl,$s1,$s2,$s3,$s0);
  506. &enccompact(2,$tbl,$s2,$s3,$s0,$s1);
  507. &enccompact(3,$tbl,$s3,$s0,$s1,$s2);
  508. &xor ($s0,&DWP(16,$key));
  509. &xor ($s1,&DWP(20,$key));
  510. &xor ($s2,&DWP(24,$key));
  511. &xor ($s3,&DWP(28,$key));
  512. &ret ();
  513. &function_end_B("_x86_AES_encrypt_compact");
  514. ######################################################################
  515. # "Compact" SSE block function.
  516. ######################################################################
  517. #
  518. # Performance is not actually extraordinary in comparison to pure
  519. # x86 code. In particular encrypt performance is virtually the same.
  520. # Decrypt performance on the other hand is 15-20% better on newer
  521. # µ-archs [but we're thankful for *any* improvement here], and ~50%
  522. # better on PIII:-) And additionally on the pros side this code
  523. # eliminates redundant references to stack and thus relieves/
  524. # minimizes the pressure on the memory bus.
  525. #
  526. # MMX register layout lsb
  527. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  528. # | mm4 | mm0 |
  529. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  530. # | s3 | s2 | s1 | s0 |
  531. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  532. # |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
  533. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  534. #
  535. # Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8.
  536. # In this terms encryption and decryption "compact" permutation
  537. # matrices can be depicted as following:
  538. #
  539. # encryption lsb # decryption lsb
  540. # +----++----+----+----+----+ # +----++----+----+----+----+
  541. # | t0 || 15 | 10 | 5 | 0 | # | t0 || 7 | 10 | 13 | 0 |
  542. # +----++----+----+----+----+ # +----++----+----+----+----+
  543. # | t1 || 3 | 14 | 9 | 4 | # | t1 || 11 | 14 | 1 | 4 |
  544. # +----++----+----+----+----+ # +----++----+----+----+----+
  545. # | t2 || 7 | 2 | 13 | 8 | # | t2 || 15 | 2 | 5 | 8 |
  546. # +----++----+----+----+----+ # +----++----+----+----+----+
  547. # | t3 || 11 | 6 | 1 | 12 | # | t3 || 3 | 6 | 9 | 12 |
  548. # +----++----+----+----+----+ # +----++----+----+----+----+
  549. #
  550. ######################################################################
  551. # Why not xmm registers? Short answer. It was actually tested and
  552. # was not any faster, but *contrary*, most notably on Intel CPUs.
  553. # Longer answer. Main advantage of using mm registers is that movd
  554. # latency is lower, especially on Intel P4. While arithmetic
  555. # instructions are twice as many, they can be scheduled every cycle
  556. # and not every second one when they are operating on xmm register,
  557. # so that "arithmetic throughput" remains virtually the same. And
  558. # finally the code can be executed even on elder SSE-only CPUs:-)
  559. sub sse_enccompact()
  560. {
  561. &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0
  562. &pshufw ("mm5","mm4",0x0d); # 15,14,11,10
  563. &movd ("eax","mm1"); # 5, 4, 1, 0
  564. &movd ("ebx","mm5"); # 15,14,11,10
  565. &mov ($__key,$key);
  566. &movz ($acc,&LB("eax")); # 0
  567. &movz ("edx",&HB("eax")); # 1
  568. &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2
  569. &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0
  570. &movz ($key,&LB("ebx")); # 10
  571. &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1
  572. &shr ("eax",16); # 5, 4
  573. &shl ("edx",8); # 1
  574. &movz ($acc,&BP(-128,$tbl,$key,1)); # 10
  575. &movz ($key,&HB("ebx")); # 11
  576. &shl ($acc,16); # 10
  577. &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8
  578. &or ("ecx",$acc); # 10
  579. &movz ($acc,&BP(-128,$tbl,$key,1)); # 11
  580. &movz ($key,&HB("eax")); # 5
  581. &shl ($acc,24); # 11
  582. &shr ("ebx",16); # 15,14
  583. &or ("edx",$acc); # 11
  584. &movz ($acc,&BP(-128,$tbl,$key,1)); # 5
  585. &movz ($key,&HB("ebx")); # 15
  586. &shl ($acc,8); # 5
  587. &or ("ecx",$acc); # 5
  588. &movz ($acc,&BP(-128,$tbl,$key,1)); # 15
  589. &movz ($key,&LB("eax")); # 4
  590. &shl ($acc,24); # 15
  591. &or ("ecx",$acc); # 15
  592. &movz ($acc,&BP(-128,$tbl,$key,1)); # 4
  593. &movz ($key,&LB("ebx")); # 14
  594. &movd ("eax","mm2"); # 7, 6, 3, 2
  595. &movd ("mm0","ecx"); # t[0] collected
  596. &movz ("ecx",&BP(-128,$tbl,$key,1)); # 14
  597. &movz ($key,&HB("eax")); # 3
  598. &shl ("ecx",16); # 14
  599. &movd ("ebx","mm6"); # 13,12, 9, 8
  600. &or ("ecx",$acc); # 14
  601. &movz ($acc,&BP(-128,$tbl,$key,1)); # 3
  602. &movz ($key,&HB("ebx")); # 9
  603. &shl ($acc,24); # 3
  604. &or ("ecx",$acc); # 3
  605. &movz ($acc,&BP(-128,$tbl,$key,1)); # 9
  606. &movz ($key,&LB("ebx")); # 8
  607. &shl ($acc,8); # 9
  608. &shr ("ebx",16); # 13,12
  609. &or ("ecx",$acc); # 9
  610. &movz ($acc,&BP(-128,$tbl,$key,1)); # 8
  611. &movz ($key,&LB("eax")); # 2
  612. &shr ("eax",16); # 7, 6
  613. &movd ("mm1","ecx"); # t[1] collected
  614. &movz ("ecx",&BP(-128,$tbl,$key,1)); # 2
  615. &movz ($key,&HB("eax")); # 7
  616. &shl ("ecx",16); # 2
  617. &and ("eax",0xff); # 6
  618. &or ("ecx",$acc); # 2
  619. &punpckldq ("mm0","mm1"); # t[0,1] collected
  620. &movz ($acc,&BP(-128,$tbl,$key,1)); # 7
  621. &movz ($key,&HB("ebx")); # 13
  622. &shl ($acc,24); # 7
  623. &and ("ebx",0xff); # 12
  624. &movz ("eax",&BP(-128,$tbl,"eax",1)); # 6
  625. &or ("ecx",$acc); # 7
  626. &shl ("eax",16); # 6
  627. &movz ($acc,&BP(-128,$tbl,$key,1)); # 13
  628. &or ("edx","eax"); # 6
  629. &shl ($acc,8); # 13
  630. &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 12
  631. &or ("ecx",$acc); # 13
  632. &or ("edx","ebx"); # 12
  633. &mov ($key,$__key);
  634. &movd ("mm4","ecx"); # t[2] collected
  635. &movd ("mm5","edx"); # t[3] collected
  636. &punpckldq ("mm4","mm5"); # t[2,3] collected
  637. }
  638. if (!$x86only) {
  639. &function_begin_B("_sse_AES_encrypt_compact");
  640. &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0
  641. &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8
  642. # note that caller is expected to allocate stack frame for me!
  643. &mov ($acc,&DWP(240,$key)); # load key->rounds
  644. &lea ($acc,&DWP(-2,$acc,$acc));
  645. &lea ($acc,&DWP(0,$key,$acc,8));
  646. &mov ($__end,$acc); # end of key schedule
  647. &mov ($s0,0x1b1b1b1b); # magic constant
  648. &mov (&DWP(8,"esp"),$s0);
  649. &mov (&DWP(12,"esp"),$s0);
  650. # prefetch Te4
  651. &mov ($s0,&DWP(0-128,$tbl));
  652. &mov ($s1,&DWP(32-128,$tbl));
  653. &mov ($s2,&DWP(64-128,$tbl));
  654. &mov ($s3,&DWP(96-128,$tbl));
  655. &mov ($s0,&DWP(128-128,$tbl));
  656. &mov ($s1,&DWP(160-128,$tbl));
  657. &mov ($s2,&DWP(192-128,$tbl));
  658. &mov ($s3,&DWP(224-128,$tbl));
  659. &set_label("loop",16);
  660. &sse_enccompact();
  661. &add ($key,16);
  662. &cmp ($key,$__end);
  663. &ja (&label("out"));
  664. &movq ("mm2",&QWP(8,"esp"));
  665. &pxor ("mm3","mm3"); &pxor ("mm7","mm7");
  666. &movq ("mm1","mm0"); &movq ("mm5","mm4"); # r0
  667. &pcmpgtb("mm3","mm0"); &pcmpgtb("mm7","mm4");
  668. &pand ("mm3","mm2"); &pand ("mm7","mm2");
  669. &pshufw ("mm2","mm0",0xb1); &pshufw ("mm6","mm4",0xb1);# ROTATE(r0,16)
  670. &paddb ("mm0","mm0"); &paddb ("mm4","mm4");
  671. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # = r2
  672. &pshufw ("mm3","mm2",0xb1); &pshufw ("mm7","mm6",0xb1);# r0
  673. &pxor ("mm1","mm0"); &pxor ("mm5","mm4"); # r0^r2
  674. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(r0,16)
  675. &movq ("mm2","mm3"); &movq ("mm6","mm7");
  676. &pslld ("mm3",8); &pslld ("mm7",8);
  677. &psrld ("mm2",24); &psrld ("mm6",24);
  678. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= r0<<8
  679. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= r0>>24
  680. &movq ("mm3","mm1"); &movq ("mm7","mm5");
  681. &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key));
  682. &psrld ("mm1",8); &psrld ("mm5",8);
  683. &mov ($s0,&DWP(0-128,$tbl));
  684. &pslld ("mm3",24); &pslld ("mm7",24);
  685. &mov ($s1,&DWP(64-128,$tbl));
  686. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= (r2^r0)<<8
  687. &mov ($s2,&DWP(128-128,$tbl));
  688. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= (r2^r0)>>24
  689. &mov ($s3,&DWP(192-128,$tbl));
  690. &pxor ("mm0","mm2"); &pxor ("mm4","mm6");
  691. &jmp (&label("loop"));
  692. &set_label("out",16);
  693. &pxor ("mm0",&QWP(0,$key));
  694. &pxor ("mm4",&QWP(8,$key));
  695. &ret ();
  696. &function_end_B("_sse_AES_encrypt_compact");
  697. }
  698. ######################################################################
  699. # Vanilla block function.
  700. ######################################################################
  701. sub encstep()
  702. { my ($i,$te,@s) = @_;
  703. my $tmp = $key;
  704. my $out = $i==3?$s[0]:$acc;
  705. # lines marked with #%e?x[i] denote "reordered" instructions...
  706. if ($i==3) { &mov ($key,$__key); }##%edx
  707. else { &mov ($out,$s[0]);
  708. &and ($out,0xFF); }
  709. if ($i==1) { &shr ($s[0],16); }#%ebx[1]
  710. if ($i==2) { &shr ($s[0],24); }#%ecx[2]
  711. &mov ($out,&DWP(0,$te,$out,8));
  712. if ($i==3) { $tmp=$s[1]; }##%eax
  713. &movz ($tmp,&HB($s[1]));
  714. &xor ($out,&DWP(3,$te,$tmp,8));
  715. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx
  716. else { &mov ($tmp,$s[2]);
  717. &shr ($tmp,16); }
  718. if ($i==2) { &and ($s[1],0xFF); }#%edx[2]
  719. &and ($tmp,0xFF);
  720. &xor ($out,&DWP(2,$te,$tmp,8));
  721. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx
  722. elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2]
  723. else { &mov ($tmp,$s[3]);
  724. &shr ($tmp,24) }
  725. &xor ($out,&DWP(1,$te,$tmp,8));
  726. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  727. if ($i==3) { &mov ($s[3],$acc); }
  728. &comment();
  729. }
  730. sub enclast()
  731. { my ($i,$te,@s)=@_;
  732. my $tmp = $key;
  733. my $out = $i==3?$s[0]:$acc;
  734. if ($i==3) { &mov ($key,$__key); }##%edx
  735. else { &mov ($out,$s[0]); }
  736. &and ($out,0xFF);
  737. if ($i==1) { &shr ($s[0],16); }#%ebx[1]
  738. if ($i==2) { &shr ($s[0],24); }#%ecx[2]
  739. &mov ($out,&DWP(2,$te,$out,8));
  740. &and ($out,0x000000ff);
  741. if ($i==3) { $tmp=$s[1]; }##%eax
  742. &movz ($tmp,&HB($s[1]));
  743. &mov ($tmp,&DWP(0,$te,$tmp,8));
  744. &and ($tmp,0x0000ff00);
  745. &xor ($out,$tmp);
  746. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx
  747. else { &mov ($tmp,$s[2]);
  748. &shr ($tmp,16); }
  749. if ($i==2) { &and ($s[1],0xFF); }#%edx[2]
  750. &and ($tmp,0xFF);
  751. &mov ($tmp,&DWP(0,$te,$tmp,8));
  752. &and ($tmp,0x00ff0000);
  753. &xor ($out,$tmp);
  754. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx
  755. elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2]
  756. else { &mov ($tmp,$s[3]);
  757. &shr ($tmp,24); }
  758. &mov ($tmp,&DWP(2,$te,$tmp,8));
  759. &and ($tmp,0xff000000);
  760. &xor ($out,$tmp);
  761. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  762. if ($i==3) { &mov ($s[3],$acc); }
  763. }
  764. &function_begin_B("_x86_AES_encrypt");
  765. if ($vertical_spin) {
  766. # I need high parts of volatile registers to be accessible...
  767. &exch ($s1="edi",$key="ebx");
  768. &mov ($s2="esi",$acc="ecx");
  769. }
  770. # note that caller is expected to allocate stack frame for me!
  771. &mov ($__key,$key); # save key
  772. &xor ($s0,&DWP(0,$key)); # xor with key
  773. &xor ($s1,&DWP(4,$key));
  774. &xor ($s2,&DWP(8,$key));
  775. &xor ($s3,&DWP(12,$key));
  776. &mov ($acc,&DWP(240,$key)); # load key->rounds
  777. if ($small_footprint) {
  778. &lea ($acc,&DWP(-2,$acc,$acc));
  779. &lea ($acc,&DWP(0,$key,$acc,8));
  780. &mov ($__end,$acc); # end of key schedule
  781. &set_label("loop",16);
  782. if ($vertical_spin) {
  783. &encvert($tbl,$s0,$s1,$s2,$s3);
  784. } else {
  785. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  786. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  787. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  788. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  789. }
  790. &add ($key,16); # advance rd_key
  791. &xor ($s0,&DWP(0,$key));
  792. &xor ($s1,&DWP(4,$key));
  793. &xor ($s2,&DWP(8,$key));
  794. &xor ($s3,&DWP(12,$key));
  795. &cmp ($key,$__end);
  796. &mov ($__key,$key);
  797. &jb (&label("loop"));
  798. }
  799. else {
  800. &cmp ($acc,10);
  801. &jle (&label("10rounds"));
  802. &cmp ($acc,12);
  803. &jle (&label("12rounds"));
  804. &set_label("14rounds",4);
  805. for ($i=1;$i<3;$i++) {
  806. if ($vertical_spin) {
  807. &encvert($tbl,$s0,$s1,$s2,$s3);
  808. } else {
  809. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  810. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  811. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  812. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  813. }
  814. &xor ($s0,&DWP(16*$i+0,$key));
  815. &xor ($s1,&DWP(16*$i+4,$key));
  816. &xor ($s2,&DWP(16*$i+8,$key));
  817. &xor ($s3,&DWP(16*$i+12,$key));
  818. }
  819. &add ($key,32);
  820. &mov ($__key,$key); # advance rd_key
  821. &set_label("12rounds",4);
  822. for ($i=1;$i<3;$i++) {
  823. if ($vertical_spin) {
  824. &encvert($tbl,$s0,$s1,$s2,$s3);
  825. } else {
  826. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  827. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  828. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  829. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  830. }
  831. &xor ($s0,&DWP(16*$i+0,$key));
  832. &xor ($s1,&DWP(16*$i+4,$key));
  833. &xor ($s2,&DWP(16*$i+8,$key));
  834. &xor ($s3,&DWP(16*$i+12,$key));
  835. }
  836. &add ($key,32);
  837. &mov ($__key,$key); # advance rd_key
  838. &set_label("10rounds",4);
  839. for ($i=1;$i<10;$i++) {
  840. if ($vertical_spin) {
  841. &encvert($tbl,$s0,$s1,$s2,$s3);
  842. } else {
  843. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  844. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  845. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  846. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  847. }
  848. &xor ($s0,&DWP(16*$i+0,$key));
  849. &xor ($s1,&DWP(16*$i+4,$key));
  850. &xor ($s2,&DWP(16*$i+8,$key));
  851. &xor ($s3,&DWP(16*$i+12,$key));
  852. }
  853. }
  854. if ($vertical_spin) {
  855. # "reincarnate" some registers for "horizontal" spin...
  856. &mov ($s1="ebx",$key="edi");
  857. &mov ($s2="ecx",$acc="esi");
  858. }
  859. &enclast(0,$tbl,$s0,$s1,$s2,$s3);
  860. &enclast(1,$tbl,$s1,$s2,$s3,$s0);
  861. &enclast(2,$tbl,$s2,$s3,$s0,$s1);
  862. &enclast(3,$tbl,$s3,$s0,$s1,$s2);
  863. &add ($key,$small_footprint?16:160);
  864. &xor ($s0,&DWP(0,$key));
  865. &xor ($s1,&DWP(4,$key));
  866. &xor ($s2,&DWP(8,$key));
  867. &xor ($s3,&DWP(12,$key));
  868. &ret ();
  869. &set_label("AES_Te",64); # Yes! I keep it in the code segment!
  870. &_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
  871. &_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
  872. &_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
  873. &_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
  874. &_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
  875. &_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
  876. &_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
  877. &_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
  878. &_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
  879. &_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
  880. &_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
  881. &_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
  882. &_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
  883. &_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
  884. &_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
  885. &_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
  886. &_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
  887. &_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
  888. &_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
  889. &_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
  890. &_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
  891. &_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
  892. &_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
  893. &_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
  894. &_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
  895. &_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
  896. &_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
  897. &_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
  898. &_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
  899. &_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
  900. &_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
  901. &_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
  902. &_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
  903. &_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
  904. &_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
  905. &_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
  906. &_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
  907. &_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
  908. &_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
  909. &_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
  910. &_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
  911. &_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
  912. &_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
  913. &_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
  914. &_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
  915. &_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
  916. &_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
  917. &_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
  918. &_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
  919. &_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
  920. &_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
  921. &_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
  922. &_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
  923. &_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
  924. &_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
  925. &_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
  926. &_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
  927. &_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
  928. &_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
  929. &_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
  930. &_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
  931. &_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
  932. &_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
  933. &_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
  934. #Te4 # four copies of Te4 to choose from to avoid L1 aliasing
  935. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  936. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  937. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  938. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  939. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  940. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  941. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  942. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  943. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  944. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  945. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  946. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  947. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  948. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  949. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  950. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  951. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  952. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  953. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  954. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  955. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  956. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  957. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  958. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  959. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  960. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  961. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  962. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  963. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  964. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  965. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  966. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  967. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  968. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  969. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  970. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  971. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  972. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  973. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  974. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  975. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  976. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  977. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  978. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  979. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  980. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  981. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  982. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  983. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  984. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  985. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  986. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  987. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  988. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  989. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  990. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  991. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  992. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  993. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  994. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  995. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  996. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  997. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  998. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  999. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  1000. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  1001. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  1002. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  1003. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  1004. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  1005. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  1006. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  1007. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  1008. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  1009. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  1010. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  1011. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  1012. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  1013. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  1014. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  1015. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  1016. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  1017. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  1018. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  1019. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  1020. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  1021. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  1022. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  1023. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  1024. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  1025. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  1026. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  1027. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  1028. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  1029. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  1030. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  1031. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  1032. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  1033. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  1034. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  1035. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  1036. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  1037. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  1038. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  1039. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  1040. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  1041. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  1042. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  1043. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  1044. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  1045. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  1046. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  1047. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  1048. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  1049. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  1050. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  1051. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  1052. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  1053. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  1054. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  1055. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  1056. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  1057. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  1058. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  1059. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  1060. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  1061. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  1062. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  1063. #rcon:
  1064. &data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
  1065. &data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
  1066. &data_word(0x0000001b, 0x00000036, 0x00000000, 0x00000000);
  1067. &data_word(0x00000000, 0x00000000, 0x00000000, 0x00000000);
  1068. &function_end_B("_x86_AES_encrypt");
  1069. # void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
  1070. &function_begin("AES_encrypt");
  1071. &mov ($acc,&wparam(0)); # load inp
  1072. &mov ($key,&wparam(2)); # load key
  1073. &mov ($s0,"esp");
  1074. &sub ("esp",36);
  1075. &and ("esp",-64); # align to cache-line
  1076. # place stack frame just "above" the key schedule
  1077. &lea ($s1,&DWP(-64-63,$key));
  1078. &sub ($s1,"esp");
  1079. &neg ($s1);
  1080. &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line
  1081. &sub ("esp",$s1);
  1082. &add ("esp",4); # 4 is reserved for caller's return address
  1083. &mov ($_esp,$s0); # save stack pointer
  1084. &call (&label("pic_point")); # make it PIC!
  1085. &set_label("pic_point");
  1086. &blindpop($tbl);
  1087. &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if (!$x86only);
  1088. &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
  1089. # pick Te4 copy which can't "overlap" with stack frame or key schedule
  1090. &lea ($s1,&DWP(768-4,"esp"));
  1091. &sub ($s1,$tbl);
  1092. &and ($s1,0x300);
  1093. &lea ($tbl,&DWP(2048+128,$tbl,$s1));
  1094. if (!$x86only) {
  1095. &bt (&DWP(0,$s0),25); # check for SSE bit
  1096. &jnc (&label("x86"));
  1097. &movq ("mm0",&QWP(0,$acc));
  1098. &movq ("mm4",&QWP(8,$acc));
  1099. &call ("_sse_AES_encrypt_compact");
  1100. &mov ("esp",$_esp); # restore stack pointer
  1101. &mov ($acc,&wparam(1)); # load out
  1102. &movq (&QWP(0,$acc),"mm0"); # write output data
  1103. &movq (&QWP(8,$acc),"mm4");
  1104. &emms ();
  1105. &function_end_A();
  1106. }
  1107. &set_label("x86",16);
  1108. &mov ($_tbl,$tbl);
  1109. &mov ($s0,&DWP(0,$acc)); # load input data
  1110. &mov ($s1,&DWP(4,$acc));
  1111. &mov ($s2,&DWP(8,$acc));
  1112. &mov ($s3,&DWP(12,$acc));
  1113. &call ("_x86_AES_encrypt_compact");
  1114. &mov ("esp",$_esp); # restore stack pointer
  1115. &mov ($acc,&wparam(1)); # load out
  1116. &mov (&DWP(0,$acc),$s0); # write output data
  1117. &mov (&DWP(4,$acc),$s1);
  1118. &mov (&DWP(8,$acc),$s2);
  1119. &mov (&DWP(12,$acc),$s3);
  1120. &function_end("AES_encrypt");
  1121. #--------------------------------------------------------------------#
  1122. ######################################################################
  1123. # "Compact" block function
  1124. ######################################################################
  1125. sub deccompact()
  1126. { my $Fn = \&mov;
  1127. while ($#_>5) { pop(@_); $Fn=sub{}; }
  1128. my ($i,$td,@s)=@_;
  1129. my $tmp = $key;
  1130. my $out = $i==3?$s[0]:$acc;
  1131. # $Fn is used in first compact round and its purpose is to
  1132. # void restoration of some values from stack, so that after
  1133. # 4xdeccompact with extra argument $key, $s0 and $s1 values
  1134. # are left there...
  1135. if($i==3) { &$Fn ($key,$__key); }
  1136. else { &mov ($out,$s[0]); }
  1137. &and ($out,0xFF);
  1138. &movz ($out,&BP(-128,$td,$out,1));
  1139. if ($i==3) { $tmp=$s[1]; }
  1140. &movz ($tmp,&HB($s[1]));
  1141. &movz ($tmp,&BP(-128,$td,$tmp,1));
  1142. &shl ($tmp,8);
  1143. &xor ($out,$tmp);
  1144. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); }
  1145. else { mov ($tmp,$s[2]); }
  1146. &shr ($tmp,16);
  1147. &and ($tmp,0xFF);
  1148. &movz ($tmp,&BP(-128,$td,$tmp,1));
  1149. &shl ($tmp,16);
  1150. &xor ($out,$tmp);
  1151. if ($i==3) { $tmp=$s[3]; &$Fn ($s[2],$__s1); }
  1152. else { &mov ($tmp,$s[3]); }
  1153. &shr ($tmp,24);
  1154. &movz ($tmp,&BP(-128,$td,$tmp,1));
  1155. &shl ($tmp,24);
  1156. &xor ($out,$tmp);
  1157. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  1158. if ($i==3) { &$Fn ($s[3],$__s0); }
  1159. }
  1160. # must be called with 2,3,0,1 as argument sequence!!!
  1161. sub dectransform()
  1162. { my @s = ($s0,$s1,$s2,$s3);
  1163. my $i = shift;
  1164. my $tmp = $key;
  1165. my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1);
  1166. my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1);
  1167. my $tp8 = $tbl;
  1168. &mov ($tmp,0x80808080);
  1169. &and ($tmp,$s[$i]);
  1170. &mov ($acc,$tmp);
  1171. &shr ($tmp,7);
  1172. &lea ($tp2,&DWP(0,$s[$i],$s[$i]));
  1173. &sub ($acc,$tmp);
  1174. &and ($tp2,0xfefefefe);
  1175. &and ($acc,0x1b1b1b1b);
  1176. &xor ($tp2,$acc);
  1177. &mov ($tmp,0x80808080);
  1178. &and ($tmp,$tp2);
  1179. &mov ($acc,$tmp);
  1180. &shr ($tmp,7);
  1181. &lea ($tp4,&DWP(0,$tp2,$tp2));
  1182. &sub ($acc,$tmp);
  1183. &and ($tp4,0xfefefefe);
  1184. &and ($acc,0x1b1b1b1b);
  1185. &xor ($tp2,$s[$i]); # tp2^tp1
  1186. &xor ($tp4,$acc);
  1187. &mov ($tmp,0x80808080);
  1188. &and ($tmp,$tp4);
  1189. &mov ($acc,$tmp);
  1190. &shr ($tmp,7);
  1191. &lea ($tp8,&DWP(0,$tp4,$tp4));
  1192. &sub ($acc,$tmp);
  1193. &and ($tp8,0xfefefefe);
  1194. &and ($acc,0x1b1b1b1b);
  1195. &xor ($tp4,$s[$i]); # tp4^tp1
  1196. &rotl ($s[$i],8); # = ROTATE(tp1,8)
  1197. &xor ($tp8,$acc);
  1198. &xor ($s[$i],$tp2);
  1199. &xor ($tp2,$tp8);
  1200. &xor ($s[$i],$tp4);
  1201. &xor ($tp4,$tp8);
  1202. &rotl ($tp2,24);
  1203. &xor ($s[$i],$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1)
  1204. &rotl ($tp4,16);
  1205. &xor ($s[$i],$tp2); # ^= ROTATE(tp8^tp2^tp1,24)
  1206. &rotl ($tp8,8);
  1207. &xor ($s[$i],$tp4); # ^= ROTATE(tp8^tp4^tp1,16)
  1208. &mov ($s[0],$__s0) if($i==2); #prefetch $s0
  1209. &mov ($s[1],$__s1) if($i==3); #prefetch $s1
  1210. &mov ($s[2],$__s2) if($i==1);
  1211. &xor ($s[$i],$tp8); # ^= ROTATE(tp8,8)
  1212. &mov ($s[3],$__s3) if($i==1);
  1213. &mov (&DWP(4+4*$i,"esp"),$s[$i]) if($i>=2);
  1214. }
  1215. &function_begin_B("_x86_AES_decrypt_compact");
  1216. # note that caller is expected to allocate stack frame for me!
  1217. &mov ($__key,$key); # save key
  1218. &xor ($s0,&DWP(0,$key)); # xor with key
  1219. &xor ($s1,&DWP(4,$key));
  1220. &xor ($s2,&DWP(8,$key));
  1221. &xor ($s3,&DWP(12,$key));
  1222. &mov ($acc,&DWP(240,$key)); # load key->rounds
  1223. &lea ($acc,&DWP(-2,$acc,$acc));
  1224. &lea ($acc,&DWP(0,$key,$acc,8));
  1225. &mov ($__end,$acc); # end of key schedule
  1226. # prefetch Td4
  1227. &mov ($key,&DWP(0-128,$tbl));
  1228. &mov ($acc,&DWP(32-128,$tbl));
  1229. &mov ($key,&DWP(64-128,$tbl));
  1230. &mov ($acc,&DWP(96-128,$tbl));
  1231. &mov ($key,&DWP(128-128,$tbl));
  1232. &mov ($acc,&DWP(160-128,$tbl));
  1233. &mov ($key,&DWP(192-128,$tbl));
  1234. &mov ($acc,&DWP(224-128,$tbl));
  1235. &set_label("loop",16);
  1236. &deccompact(0,$tbl,$s0,$s3,$s2,$s1,1);
  1237. &deccompact(1,$tbl,$s1,$s0,$s3,$s2,1);
  1238. &deccompact(2,$tbl,$s2,$s1,$s0,$s3,1);
  1239. &deccompact(3,$tbl,$s3,$s2,$s1,$s0,1);
  1240. &dectransform(2);
  1241. &dectransform(3);
  1242. &dectransform(0);
  1243. &dectransform(1);
  1244. &mov ($key,$__key);
  1245. &mov ($tbl,$__tbl);
  1246. &add ($key,16); # advance rd_key
  1247. &xor ($s0,&DWP(0,$key));
  1248. &xor ($s1,&DWP(4,$key));
  1249. &xor ($s2,&DWP(8,$key));
  1250. &xor ($s3,&DWP(12,$key));
  1251. &cmp ($key,$__end);
  1252. &mov ($__key,$key);
  1253. &jb (&label("loop"));
  1254. &deccompact(0,$tbl,$s0,$s3,$s2,$s1);
  1255. &deccompact(1,$tbl,$s1,$s0,$s3,$s2);
  1256. &deccompact(2,$tbl,$s2,$s1,$s0,$s3);
  1257. &deccompact(3,$tbl,$s3,$s2,$s1,$s0);
  1258. &xor ($s0,&DWP(16,$key));
  1259. &xor ($s1,&DWP(20,$key));
  1260. &xor ($s2,&DWP(24,$key));
  1261. &xor ($s3,&DWP(28,$key));
  1262. &ret ();
  1263. &function_end_B("_x86_AES_decrypt_compact");
  1264. ######################################################################
  1265. # "Compact" SSE block function.
  1266. ######################################################################
  1267. sub sse_deccompact()
  1268. {
  1269. &pshufw ("mm1","mm0",0x0c); # 7, 6, 1, 0
  1270. &pshufw ("mm5","mm4",0x09); # 13,12,11,10
  1271. &movd ("eax","mm1"); # 7, 6, 1, 0
  1272. &movd ("ebx","mm5"); # 13,12,11,10
  1273. &mov ($__key,$key);
  1274. &movz ($acc,&LB("eax")); # 0
  1275. &movz ("edx",&HB("eax")); # 1
  1276. &pshufw ("mm2","mm0",0x06); # 3, 2, 5, 4
  1277. &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0
  1278. &movz ($key,&LB("ebx")); # 10
  1279. &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1
  1280. &shr ("eax",16); # 7, 6
  1281. &shl ("edx",8); # 1
  1282. &movz ($acc,&BP(-128,$tbl,$key,1)); # 10
  1283. &movz ($key,&HB("ebx")); # 11
  1284. &shl ($acc,16); # 10
  1285. &pshufw ("mm6","mm4",0x03); # 9, 8,15,14
  1286. &or ("ecx",$acc); # 10
  1287. &movz ($acc,&BP(-128,$tbl,$key,1)); # 11
  1288. &movz ($key,&HB("eax")); # 7
  1289. &shl ($acc,24); # 11
  1290. &shr ("ebx",16); # 13,12
  1291. &or ("edx",$acc); # 11
  1292. &movz ($acc,&BP(-128,$tbl,$key,1)); # 7
  1293. &movz ($key,&HB("ebx")); # 13
  1294. &shl ($acc,24); # 7
  1295. &or ("ecx",$acc); # 7
  1296. &movz ($acc,&BP(-128,$tbl,$key,1)); # 13
  1297. &movz ($key,&LB("eax")); # 6
  1298. &shl ($acc,8); # 13
  1299. &movd ("eax","mm2"); # 3, 2, 5, 4
  1300. &or ("ecx",$acc); # 13
  1301. &movz ($acc,&BP(-128,$tbl,$key,1)); # 6
  1302. &movz ($key,&LB("ebx")); # 12
  1303. &shl ($acc,16); # 6
  1304. &movd ("ebx","mm6"); # 9, 8,15,14
  1305. &movd ("mm0","ecx"); # t[0] collected
  1306. &movz ("ecx",&BP(-128,$tbl,$key,1)); # 12
  1307. &movz ($key,&LB("eax")); # 4
  1308. &or ("ecx",$acc); # 12
  1309. &movz ($acc,&BP(-128,$tbl,$key,1)); # 4
  1310. &movz ($key,&LB("ebx")); # 14
  1311. &or ("edx",$acc); # 4
  1312. &movz ($acc,&BP(-128,$tbl,$key,1)); # 14
  1313. &movz ($key,&HB("eax")); # 5
  1314. &shl ($acc,16); # 14
  1315. &shr ("eax",16); # 3, 2
  1316. &or ("edx",$acc); # 14
  1317. &movz ($acc,&BP(-128,$tbl,$key,1)); # 5
  1318. &movz ($key,&HB("ebx")); # 15
  1319. &shr ("ebx",16); # 9, 8
  1320. &shl ($acc,8); # 5
  1321. &movd ("mm1","edx"); # t[1] collected
  1322. &movz ("edx",&BP(-128,$tbl,$key,1)); # 15
  1323. &movz ($key,&HB("ebx")); # 9
  1324. &shl ("edx",24); # 15
  1325. &and ("ebx",0xff); # 8
  1326. &or ("edx",$acc); # 15
  1327. &punpckldq ("mm0","mm1"); # t[0,1] collected
  1328. &movz ($acc,&BP(-128,$tbl,$key,1)); # 9
  1329. &movz ($key,&LB("eax")); # 2
  1330. &shl ($acc,8); # 9
  1331. &movz ("eax",&HB("eax")); # 3
  1332. &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 8
  1333. &or ("ecx",$acc); # 9
  1334. &movz ($acc,&BP(-128,$tbl,$key,1)); # 2
  1335. &or ("edx","ebx"); # 8
  1336. &shl ($acc,16); # 2
  1337. &movz ("eax",&BP(-128,$tbl,"eax",1)); # 3
  1338. &or ("edx",$acc); # 2
  1339. &shl ("eax",24); # 3
  1340. &or ("ecx","eax"); # 3
  1341. &mov ($key,$__key);
  1342. &movd ("mm4","edx"); # t[2] collected
  1343. &movd ("mm5","ecx"); # t[3] collected
  1344. &punpckldq ("mm4","mm5"); # t[2,3] collected
  1345. }
  1346. if (!$x86only) {
  1347. &function_begin_B("_sse_AES_decrypt_compact");
  1348. &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0
  1349. &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8
  1350. # note that caller is expected to allocate stack frame for me!
  1351. &mov ($acc,&DWP(240,$key)); # load key->rounds
  1352. &lea ($acc,&DWP(-2,$acc,$acc));
  1353. &lea ($acc,&DWP(0,$key,$acc,8));
  1354. &mov ($__end,$acc); # end of key schedule
  1355. &mov ($s0,0x1b1b1b1b); # magic constant
  1356. &mov (&DWP(8,"esp"),$s0);
  1357. &mov (&DWP(12,"esp"),$s0);
  1358. # prefetch Td4
  1359. &mov ($s0,&DWP(0-128,$tbl));
  1360. &mov ($s1,&DWP(32-128,$tbl));
  1361. &mov ($s2,&DWP(64-128,$tbl));
  1362. &mov ($s3,&DWP(96-128,$tbl));
  1363. &mov ($s0,&DWP(128-128,$tbl));
  1364. &mov ($s1,&DWP(160-128,$tbl));
  1365. &mov ($s2,&DWP(192-128,$tbl));
  1366. &mov ($s3,&DWP(224-128,$tbl));
  1367. &set_label("loop",16);
  1368. &sse_deccompact();
  1369. &add ($key,16);
  1370. &cmp ($key,$__end);
  1371. &ja (&label("out"));
  1372. # ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N)
  1373. &movq ("mm3","mm0"); &movq ("mm7","mm4");
  1374. &movq ("mm2","mm0",1); &movq ("mm6","mm4",1);
  1375. &movq ("mm1","mm0"); &movq ("mm5","mm4");
  1376. &pshufw ("mm0","mm0",0xb1); &pshufw ("mm4","mm4",0xb1);# = ROTATE(tp0,16)
  1377. &pslld ("mm2",8); &pslld ("mm6",8);
  1378. &psrld ("mm3",8); &psrld ("mm7",8);
  1379. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<8
  1380. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>8
  1381. &pslld ("mm2",16); &pslld ("mm6",16);
  1382. &psrld ("mm3",16); &psrld ("mm7",16);
  1383. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<24
  1384. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>24
  1385. &movq ("mm3",&QWP(8,"esp"));
  1386. &pxor ("mm2","mm2"); &pxor ("mm6","mm6");
  1387. &pcmpgtb("mm2","mm1"); &pcmpgtb("mm6","mm5");
  1388. &pand ("mm2","mm3"); &pand ("mm6","mm3");
  1389. &paddb ("mm1","mm1"); &paddb ("mm5","mm5");
  1390. &pxor ("mm1","mm2"); &pxor ("mm5","mm6"); # tp2
  1391. &movq ("mm3","mm1"); &movq ("mm7","mm5");
  1392. &movq ("mm2","mm1"); &movq ("mm6","mm5");
  1393. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp2
  1394. &pslld ("mm3",24); &pslld ("mm7",24);
  1395. &psrld ("mm2",8); &psrld ("mm6",8);
  1396. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp2<<24
  1397. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp2>>8
  1398. &movq ("mm2",&QWP(8,"esp"));
  1399. &pxor ("mm3","mm3"); &pxor ("mm7","mm7");
  1400. &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5");
  1401. &pand ("mm3","mm2"); &pand ("mm7","mm2");
  1402. &paddb ("mm1","mm1"); &paddb ("mm5","mm5");
  1403. &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp4
  1404. &pshufw ("mm3","mm1",0xb1); &pshufw ("mm7","mm5",0xb1);
  1405. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp4
  1406. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= ROTATE(tp4,16)
  1407. &pxor ("mm3","mm3"); &pxor ("mm7","mm7");
  1408. &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5");
  1409. &pand ("mm3","mm2"); &pand ("mm7","mm2");
  1410. &paddb ("mm1","mm1"); &paddb ("mm5","mm5");
  1411. &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp8
  1412. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8
  1413. &movq ("mm3","mm1"); &movq ("mm7","mm5");
  1414. &pshufw ("mm2","mm1",0xb1); &pshufw ("mm6","mm5",0xb1);
  1415. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(tp8,16)
  1416. &pslld ("mm1",8); &pslld ("mm5",8);
  1417. &psrld ("mm3",8); &psrld ("mm7",8);
  1418. &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key));
  1419. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<8
  1420. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>8
  1421. &mov ($s0,&DWP(0-128,$tbl));
  1422. &pslld ("mm1",16); &pslld ("mm5",16);
  1423. &mov ($s1,&DWP(64-128,$tbl));
  1424. &psrld ("mm3",16); &psrld ("mm7",16);
  1425. &mov ($s2,&DWP(128-128,$tbl));
  1426. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<24
  1427. &mov ($s3,&DWP(192-128,$tbl));
  1428. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>24
  1429. &pxor ("mm0","mm2"); &pxor ("mm4","mm6");
  1430. &jmp (&label("loop"));
  1431. &set_label("out",16);
  1432. &pxor ("mm0",&QWP(0,$key));
  1433. &pxor ("mm4",&QWP(8,$key));
  1434. &ret ();
  1435. &function_end_B("_sse_AES_decrypt_compact");
  1436. }
  1437. ######################################################################
  1438. # Vanilla block function.
  1439. ######################################################################
  1440. sub decstep()
  1441. { my ($i,$td,@s) = @_;
  1442. my $tmp = $key;
  1443. my $out = $i==3?$s[0]:$acc;
  1444. # no instructions are reordered, as performance appears
  1445. # optimal... or rather that all attempts to reorder didn't
  1446. # result in better performance [which by the way is not a
  1447. # bit lower than ecryption].
  1448. if($i==3) { &mov ($key,$__key); }
  1449. else { &mov ($out,$s[0]); }
  1450. &and ($out,0xFF);
  1451. &mov ($out,&DWP(0,$td,$out,8));
  1452. if ($i==3) { $tmp=$s[1]; }
  1453. &movz ($tmp,&HB($s[1]));
  1454. &xor ($out,&DWP(3,$td,$tmp,8));
  1455. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); }
  1456. else { &mov ($tmp,$s[2]); }
  1457. &shr ($tmp,16);
  1458. &and ($tmp,0xFF);
  1459. &xor ($out,&DWP(2,$td,$tmp,8));
  1460. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }
  1461. else { &mov ($tmp,$s[3]); }
  1462. &shr ($tmp,24);
  1463. &xor ($out,&DWP(1,$td,$tmp,8));
  1464. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  1465. if ($i==3) { &mov ($s[3],$__s0); }
  1466. &comment();
  1467. }
  1468. sub declast()
  1469. { my ($i,$td,@s)=@_;
  1470. my $tmp = $key;
  1471. my $out = $i==3?$s[0]:$acc;
  1472. if($i==0) { &lea ($td,&DWP(2048+128,$td));
  1473. &mov ($tmp,&DWP(0-128,$td));
  1474. &mov ($acc,&DWP(32-128,$td));
  1475. &mov ($tmp,&DWP(64-128,$td));
  1476. &mov ($acc,&DWP(96-128,$td));
  1477. &mov ($tmp,&DWP(128-128,$td));
  1478. &mov ($acc,&DWP(160-128,$td));
  1479. &mov ($tmp,&DWP(192-128,$td));
  1480. &mov ($acc,&DWP(224-128,$td));
  1481. &lea ($td,&DWP(-128,$td)); }
  1482. if($i==3) { &mov ($key,$__key); }
  1483. else { &mov ($out,$s[0]); }
  1484. &and ($out,0xFF);
  1485. &movz ($out,&BP(0,$td,$out,1));
  1486. if ($i==3) { $tmp=$s[1]; }
  1487. &movz ($tmp,&HB($s[1]));
  1488. &movz ($tmp,&BP(0,$td,$tmp,1));
  1489. &shl ($tmp,8);
  1490. &xor ($out,$tmp);
  1491. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); }
  1492. else { mov ($tmp,$s[2]); }
  1493. &shr ($tmp,16);
  1494. &and ($tmp,0xFF);
  1495. &movz ($tmp,&BP(0,$td,$tmp,1));
  1496. &shl ($tmp,16);
  1497. &xor ($out,$tmp);
  1498. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }
  1499. else { &mov ($tmp,$s[3]); }
  1500. &shr ($tmp,24);
  1501. &movz ($tmp,&BP(0,$td,$tmp,1));
  1502. &shl ($tmp,24);
  1503. &xor ($out,$tmp);
  1504. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  1505. if ($i==3) { &mov ($s[3],$__s0);
  1506. &lea ($td,&DWP(-2048,$td)); }
  1507. }
  1508. &function_begin_B("_x86_AES_decrypt");
  1509. # note that caller is expected to allocate stack frame for me!
  1510. &mov ($__key,$key); # save key
  1511. &xor ($s0,&DWP(0,$key)); # xor with key
  1512. &xor ($s1,&DWP(4,$key));
  1513. &xor ($s2,&DWP(8,$key));
  1514. &xor ($s3,&DWP(12,$key));
  1515. &mov ($acc,&DWP(240,$key)); # load key->rounds
  1516. if ($small_footprint) {
  1517. &lea ($acc,&DWP(-2,$acc,$acc));
  1518. &lea ($acc,&DWP(0,$key,$acc,8));
  1519. &mov ($__end,$acc); # end of key schedule
  1520. &set_label("loop",16);
  1521. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1522. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1523. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1524. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1525. &add ($key,16); # advance rd_key
  1526. &xor ($s0,&DWP(0,$key));
  1527. &xor ($s1,&DWP(4,$key));
  1528. &xor ($s2,&DWP(8,$key));
  1529. &xor ($s3,&DWP(12,$key));
  1530. &cmp ($key,$__end);
  1531. &mov ($__key,$key);
  1532. &jb (&label("loop"));
  1533. }
  1534. else {
  1535. &cmp ($acc,10);
  1536. &jle (&label("10rounds"));
  1537. &cmp ($acc,12);
  1538. &jle (&label("12rounds"));
  1539. &set_label("14rounds",4);
  1540. for ($i=1;$i<3;$i++) {
  1541. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1542. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1543. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1544. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1545. &xor ($s0,&DWP(16*$i+0,$key));
  1546. &xor ($s1,&DWP(16*$i+4,$key));
  1547. &xor ($s2,&DWP(16*$i+8,$key));
  1548. &xor ($s3,&DWP(16*$i+12,$key));
  1549. }
  1550. &add ($key,32);
  1551. &mov ($__key,$key); # advance rd_key
  1552. &set_label("12rounds",4);
  1553. for ($i=1;$i<3;$i++) {
  1554. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1555. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1556. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1557. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1558. &xor ($s0,&DWP(16*$i+0,$key));
  1559. &xor ($s1,&DWP(16*$i+4,$key));
  1560. &xor ($s2,&DWP(16*$i+8,$key));
  1561. &xor ($s3,&DWP(16*$i+12,$key));
  1562. }
  1563. &add ($key,32);
  1564. &mov ($__key,$key); # advance rd_key
  1565. &set_label("10rounds",4);
  1566. for ($i=1;$i<10;$i++) {
  1567. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1568. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1569. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1570. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1571. &xor ($s0,&DWP(16*$i+0,$key));
  1572. &xor ($s1,&DWP(16*$i+4,$key));
  1573. &xor ($s2,&DWP(16*$i+8,$key));
  1574. &xor ($s3,&DWP(16*$i+12,$key));
  1575. }
  1576. }
  1577. &declast(0,$tbl,$s0,$s3,$s2,$s1);
  1578. &declast(1,$tbl,$s1,$s0,$s3,$s2);
  1579. &declast(2,$tbl,$s2,$s1,$s0,$s3);
  1580. &declast(3,$tbl,$s3,$s2,$s1,$s0);
  1581. &add ($key,$small_footprint?16:160);
  1582. &xor ($s0,&DWP(0,$key));
  1583. &xor ($s1,&DWP(4,$key));
  1584. &xor ($s2,&DWP(8,$key));
  1585. &xor ($s3,&DWP(12,$key));
  1586. &ret ();
  1587. &set_label("AES_Td",64); # Yes! I keep it in the code segment!
  1588. &_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
  1589. &_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
  1590. &_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
  1591. &_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
  1592. &_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
  1593. &_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
  1594. &_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
  1595. &_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
  1596. &_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
  1597. &_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
  1598. &_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
  1599. &_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
  1600. &_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
  1601. &_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
  1602. &_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
  1603. &_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
  1604. &_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
  1605. &_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
  1606. &_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
  1607. &_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
  1608. &_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
  1609. &_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
  1610. &_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
  1611. &_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
  1612. &_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
  1613. &_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
  1614. &_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
  1615. &_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
  1616. &_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
  1617. &_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
  1618. &_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
  1619. &_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
  1620. &_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
  1621. &_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
  1622. &_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
  1623. &_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
  1624. &_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
  1625. &_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
  1626. &_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
  1627. &_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
  1628. &_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
  1629. &_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
  1630. &_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
  1631. &_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
  1632. &_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
  1633. &_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
  1634. &_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
  1635. &_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
  1636. &_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
  1637. &_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
  1638. &_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
  1639. &_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
  1640. &_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
  1641. &_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
  1642. &_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
  1643. &_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
  1644. &_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
  1645. &_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
  1646. &_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
  1647. &_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
  1648. &_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
  1649. &_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
  1650. &_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
  1651. &_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
  1652. #Td4: # four copies of Td4 to choose from to avoid L1 aliasing
  1653. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1654. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1655. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1656. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1657. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1658. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1659. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1660. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1661. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1662. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1663. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1664. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1665. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1666. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1667. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1668. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1669. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1670. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1671. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1672. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1673. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1674. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1675. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1676. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1677. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1678. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1679. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1680. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1681. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1682. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1683. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1684. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1685. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1686. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1687. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1688. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1689. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1690. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1691. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1692. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1693. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1694. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1695. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1696. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1697. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1698. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1699. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1700. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1701. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1702. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1703. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1704. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1705. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1706. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1707. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1708. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1709. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1710. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1711. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1712. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1713. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1714. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1715. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1716. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1717. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1718. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1719. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1720. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1721. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1722. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1723. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1724. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1725. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1726. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1727. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1728. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1729. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1730. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1731. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1732. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1733. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1734. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1735. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1736. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1737. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1738. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1739. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1740. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1741. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1742. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1743. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1744. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1745. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1746. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1747. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1748. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1749. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1750. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1751. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1752. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1753. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1754. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1755. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1756. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1757. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1758. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1759. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1760. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1761. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1762. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1763. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1764. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1765. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1766. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1767. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1768. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1769. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1770. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1771. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1772. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1773. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1774. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1775. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1776. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1777. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1778. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1779. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1780. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1781. &function_end_B("_x86_AES_decrypt");
  1782. # void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
  1783. &function_begin("AES_decrypt");
  1784. &mov ($acc,&wparam(0)); # load inp
  1785. &mov ($key,&wparam(2)); # load key
  1786. &mov ($s0,"esp");
  1787. &sub ("esp",36);
  1788. &and ("esp",-64); # align to cache-line
  1789. # place stack frame just "above" the key schedule
  1790. &lea ($s1,&DWP(-64-63,$key));
  1791. &sub ($s1,"esp");
  1792. &neg ($s1);
  1793. &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line
  1794. &sub ("esp",$s1);
  1795. &add ("esp",4); # 4 is reserved for caller's return address
  1796. &mov ($_esp,$s0); # save stack pointer
  1797. &call (&label("pic_point")); # make it PIC!
  1798. &set_label("pic_point");
  1799. &blindpop($tbl);
  1800. &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
  1801. &lea ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl));
  1802. # pick Td4 copy which can't "overlap" with stack frame or key schedule
  1803. &lea ($s1,&DWP(768-4,"esp"));
  1804. &sub ($s1,$tbl);
  1805. &and ($s1,0x300);
  1806. &lea ($tbl,&DWP(2048+128,$tbl,$s1));
  1807. if (!$x86only) {
  1808. &bt (&DWP(0,$s0),25); # check for SSE bit
  1809. &jnc (&label("x86"));
  1810. &movq ("mm0",&QWP(0,$acc));
  1811. &movq ("mm4",&QWP(8,$acc));
  1812. &call ("_sse_AES_decrypt_compact");
  1813. &mov ("esp",$_esp); # restore stack pointer
  1814. &mov ($acc,&wparam(1)); # load out
  1815. &movq (&QWP(0,$acc),"mm0"); # write output data
  1816. &movq (&QWP(8,$acc),"mm4");
  1817. &emms ();
  1818. &function_end_A();
  1819. }
  1820. &set_label("x86",16);
  1821. &mov ($_tbl,$tbl);
  1822. &mov ($s0,&DWP(0,$acc)); # load input data
  1823. &mov ($s1,&DWP(4,$acc));
  1824. &mov ($s2,&DWP(8,$acc));
  1825. &mov ($s3,&DWP(12,$acc));
  1826. &call ("_x86_AES_decrypt_compact");
  1827. &mov ("esp",$_esp); # restore stack pointer
  1828. &mov ($acc,&wparam(1)); # load out
  1829. &mov (&DWP(0,$acc),$s0); # write output data
  1830. &mov (&DWP(4,$acc),$s1);
  1831. &mov (&DWP(8,$acc),$s2);
  1832. &mov (&DWP(12,$acc),$s3);
  1833. &function_end("AES_decrypt");
  1834. # void AES_cbc_encrypt (const void char *inp, unsigned char *out,
  1835. # size_t length, const AES_KEY *key,
  1836. # unsigned char *ivp,const int enc);
  1837. {
  1838. # stack frame layout
  1839. # -4(%esp) # return address 0(%esp)
  1840. # 0(%esp) # s0 backing store 4(%esp)
  1841. # 4(%esp) # s1 backing store 8(%esp)
  1842. # 8(%esp) # s2 backing store 12(%esp)
  1843. # 12(%esp) # s3 backing store 16(%esp)
  1844. # 16(%esp) # key backup 20(%esp)
  1845. # 20(%esp) # end of key schedule 24(%esp)
  1846. # 24(%esp) # %ebp backup 28(%esp)
  1847. # 28(%esp) # %esp backup
  1848. my $_inp=&DWP(32,"esp"); # copy of wparam(0)
  1849. my $_out=&DWP(36,"esp"); # copy of wparam(1)
  1850. my $_len=&DWP(40,"esp"); # copy of wparam(2)
  1851. my $_key=&DWP(44,"esp"); # copy of wparam(3)
  1852. my $_ivp=&DWP(48,"esp"); # copy of wparam(4)
  1853. my $_tmp=&DWP(52,"esp"); # volatile variable
  1854. #
  1855. my $ivec=&DWP(60,"esp"); # ivec[16]
  1856. my $aes_key=&DWP(76,"esp"); # copy of aes_key
  1857. my $mark=&DWP(76+240,"esp"); # copy of aes_key->rounds
  1858. &function_begin("AES_cbc_encrypt");
  1859. &mov ($s2 eq "ecx"? $s2 : "",&wparam(2)); # load len
  1860. &cmp ($s2,0);
  1861. &je (&label("drop_out"));
  1862. &call (&label("pic_point")); # make it PIC!
  1863. &set_label("pic_point");
  1864. &blindpop($tbl);
  1865. &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
  1866. &cmp (&wparam(5),0);
  1867. &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
  1868. &jne (&label("picked_te"));
  1869. &lea ($tbl,&DWP(&label("AES_Td")."-".&label("AES_Te"),$tbl));
  1870. &set_label("picked_te");
  1871. # one can argue if this is required
  1872. &pushf ();
  1873. &cld ();
  1874. &cmp ($s2,$speed_limit);
  1875. &jb (&label("slow_way"));
  1876. &test ($s2,15);
  1877. &jnz (&label("slow_way"));
  1878. if (!$x86only) {
  1879. &bt (&DWP(0,$s0),28); # check for hyper-threading bit
  1880. &jc (&label("slow_way"));
  1881. }
  1882. # pre-allocate aligned stack frame...
  1883. &lea ($acc,&DWP(-80-244,"esp"));
  1884. &and ($acc,-64);
  1885. # ... and make sure it doesn't alias with $tbl modulo 4096
  1886. &mov ($s0,$tbl);
  1887. &lea ($s1,&DWP(2048+256,$tbl));
  1888. &mov ($s3,$acc);
  1889. &and ($s0,0xfff); # s = %ebp&0xfff
  1890. &and ($s1,0xfff); # e = (%ebp+2048+256)&0xfff
  1891. &and ($s3,0xfff); # p = %esp&0xfff
  1892. &cmp ($s3,$s1); # if (p>=e) %esp =- (p-e);
  1893. &jb (&label("tbl_break_out"));
  1894. &sub ($s3,$s1);
  1895. &sub ($acc,$s3);
  1896. &jmp (&label("tbl_ok"));
  1897. &set_label("tbl_break_out",4); # else %esp -= (p-s)&0xfff + framesz;
  1898. &sub ($s3,$s0);
  1899. &and ($s3,0xfff);
  1900. &add ($s3,384);
  1901. &sub ($acc,$s3);
  1902. &set_label("tbl_ok",4);
  1903. &lea ($s3,&wparam(0)); # obtain pointer to parameter block
  1904. &exch ("esp",$acc); # allocate stack frame
  1905. &add ("esp",4); # reserve for return address!
  1906. &mov ($_tbl,$tbl); # save %ebp
  1907. &mov ($_esp,$acc); # save %esp
  1908. &mov ($s0,&DWP(0,$s3)); # load inp
  1909. &mov ($s1,&DWP(4,$s3)); # load out
  1910. #&mov ($s2,&DWP(8,$s3)); # load len
  1911. &mov ($key,&DWP(12,$s3)); # load key
  1912. &mov ($acc,&DWP(16,$s3)); # load ivp
  1913. &mov ($s3,&DWP(20,$s3)); # load enc flag
  1914. &mov ($_inp,$s0); # save copy of inp
  1915. &mov ($_out,$s1); # save copy of out
  1916. &mov ($_len,$s2); # save copy of len
  1917. &mov ($_key,$key); # save copy of key
  1918. &mov ($_ivp,$acc); # save copy of ivp
  1919. &mov ($mark,0); # copy of aes_key->rounds = 0;
  1920. # do we copy key schedule to stack?
  1921. &mov ($s1 eq "ebx" ? $s1 : "",$key);
  1922. &mov ($s2 eq "ecx" ? $s2 : "",244/4);
  1923. &sub ($s1,$tbl);
  1924. &mov ("esi",$key);
  1925. &and ($s1,0xfff);
  1926. &lea ("edi",$aes_key);
  1927. &cmp ($s1,2048+256);
  1928. &jb (&label("do_copy"));
  1929. &cmp ($s1,4096-244);
  1930. &jb (&label("skip_copy"));
  1931. &set_label("do_copy",4);
  1932. &mov ($_key,"edi");
  1933. &data_word(0xA5F3F689); # rep movsd
  1934. &set_label("skip_copy");
  1935. &mov ($key,16);
  1936. &set_label("prefetch_tbl",4);
  1937. &mov ($s0,&DWP(0,$tbl));
  1938. &mov ($s1,&DWP(32,$tbl));
  1939. &mov ($s2,&DWP(64,$tbl));
  1940. &mov ($acc,&DWP(96,$tbl));
  1941. &lea ($tbl,&DWP(128,$tbl));
  1942. &sub ($key,1);
  1943. &jnz (&label("prefetch_tbl"));
  1944. &sub ($tbl,2048);
  1945. &mov ($acc,$_inp);
  1946. &mov ($key,$_ivp);
  1947. &cmp ($s3,0);
  1948. &je (&label("fast_decrypt"));
  1949. #----------------------------- ENCRYPT -----------------------------#
  1950. &mov ($s0,&DWP(0,$key)); # load iv
  1951. &mov ($s1,&DWP(4,$key));
  1952. &set_label("fast_enc_loop",16);
  1953. &mov ($s2,&DWP(8,$key));
  1954. &mov ($s3,&DWP(12,$key));
  1955. &xor ($s0,&DWP(0,$acc)); # xor input data
  1956. &xor ($s1,&DWP(4,$acc));
  1957. &xor ($s2,&DWP(8,$acc));
  1958. &xor ($s3,&DWP(12,$acc));
  1959. &mov ($key,$_key); # load key
  1960. &call ("_x86_AES_encrypt");
  1961. &mov ($acc,$_inp); # load inp
  1962. &mov ($key,$_out); # load out
  1963. &mov (&DWP(0,$key),$s0); # save output data
  1964. &mov (&DWP(4,$key),$s1);
  1965. &mov (&DWP(8,$key),$s2);
  1966. &mov (&DWP(12,$key),$s3);
  1967. &lea ($acc,&DWP(16,$acc)); # advance inp
  1968. &mov ($s2,$_len); # load len
  1969. &mov ($_inp,$acc); # save inp
  1970. &lea ($s3,&DWP(16,$key)); # advance out
  1971. &mov ($_out,$s3); # save out
  1972. &sub ($s2,16); # decrease len
  1973. &mov ($_len,$s2); # save len
  1974. &jnz (&label("fast_enc_loop"));
  1975. &mov ($acc,$_ivp); # load ivp
  1976. &mov ($s2,&DWP(8,$key)); # restore last 2 dwords
  1977. &mov ($s3,&DWP(12,$key));
  1978. &mov (&DWP(0,$acc),$s0); # save ivec
  1979. &mov (&DWP(4,$acc),$s1);
  1980. &mov (&DWP(8,$acc),$s2);
  1981. &mov (&DWP(12,$acc),$s3);
  1982. &cmp ($mark,0); # was the key schedule copied?
  1983. &mov ("edi",$_key);
  1984. &je (&label("skip_ezero"));
  1985. # zero copy of key schedule
  1986. &mov ("ecx",240/4);
  1987. &xor ("eax","eax");
  1988. &align (4);
  1989. &data_word(0xABF3F689); # rep stosd
  1990. &set_label("skip_ezero");
  1991. &mov ("esp",$_esp);
  1992. &popf ();
  1993. &set_label("drop_out");
  1994. &function_end_A();
  1995. &pushf (); # kludge, never executed
  1996. #----------------------------- DECRYPT -----------------------------#
  1997. &set_label("fast_decrypt",16);
  1998. &cmp ($acc,$_out);
  1999. &je (&label("fast_dec_in_place")); # in-place processing...
  2000. &mov ($_tmp,$key);
  2001. &align (4);
  2002. &set_label("fast_dec_loop",16);
  2003. &mov ($s0,&DWP(0,$acc)); # read input
  2004. &mov ($s1,&DWP(4,$acc));
  2005. &mov ($s2,&DWP(8,$acc));
  2006. &mov ($s3,&DWP(12,$acc));
  2007. &mov ($key,$_key); # load key
  2008. &call ("_x86_AES_decrypt");
  2009. &mov ($key,$_tmp); # load ivp
  2010. &mov ($acc,$_len); # load len
  2011. &xor ($s0,&DWP(0,$key)); # xor iv
  2012. &xor ($s1,&DWP(4,$key));
  2013. &xor ($s2,&DWP(8,$key));
  2014. &xor ($s3,&DWP(12,$key));
  2015. &mov ($key,$_out); # load out
  2016. &mov ($acc,$_inp); # load inp
  2017. &mov (&DWP(0,$key),$s0); # write output
  2018. &mov (&DWP(4,$key),$s1);
  2019. &mov (&DWP(8,$key),$s2);
  2020. &mov (&DWP(12,$key),$s3);
  2021. &mov ($s2,$_len); # load len
  2022. &mov ($_tmp,$acc); # save ivp
  2023. &lea ($acc,&DWP(16,$acc)); # advance inp
  2024. &mov ($_inp,$acc); # save inp
  2025. &lea ($key,&DWP(16,$key)); # advance out
  2026. &mov ($_out,$key); # save out
  2027. &sub ($s2,16); # decrease len
  2028. &mov ($_len,$s2); # save len
  2029. &jnz (&label("fast_dec_loop"));
  2030. &mov ($key,$_tmp); # load temp ivp
  2031. &mov ($acc,$_ivp); # load user ivp
  2032. &mov ($s0,&DWP(0,$key)); # load iv
  2033. &mov ($s1,&DWP(4,$key));
  2034. &mov ($s2,&DWP(8,$key));
  2035. &mov ($s3,&DWP(12,$key));
  2036. &mov (&DWP(0,$acc),$s0); # copy back to user
  2037. &mov (&DWP(4,$acc),$s1);
  2038. &mov (&DWP(8,$acc),$s2);
  2039. &mov (&DWP(12,$acc),$s3);
  2040. &jmp (&label("fast_dec_out"));
  2041. &set_label("fast_dec_in_place",16);
  2042. &set_label("fast_dec_in_place_loop");
  2043. &mov ($s0,&DWP(0,$acc)); # read input
  2044. &mov ($s1,&DWP(4,$acc));
  2045. &mov ($s2,&DWP(8,$acc));
  2046. &mov ($s3,&DWP(12,$acc));
  2047. &lea ($key,$ivec);
  2048. &mov (&DWP(0,$key),$s0); # copy to temp
  2049. &mov (&DWP(4,$key),$s1);
  2050. &mov (&DWP(8,$key),$s2);
  2051. &mov (&DWP(12,$key),$s3);
  2052. &mov ($key,$_key); # load key
  2053. &call ("_x86_AES_decrypt");
  2054. &mov ($key,$_ivp); # load ivp
  2055. &mov ($acc,$_out); # load out
  2056. &xor ($s0,&DWP(0,$key)); # xor iv
  2057. &xor ($s1,&DWP(4,$key));
  2058. &xor ($s2,&DWP(8,$key));
  2059. &xor ($s3,&DWP(12,$key));
  2060. &mov (&DWP(0,$acc),$s0); # write output
  2061. &mov (&DWP(4,$acc),$s1);
  2062. &mov (&DWP(8,$acc),$s2);
  2063. &mov (&DWP(12,$acc),$s3);
  2064. &lea ($acc,&DWP(16,$acc)); # advance out
  2065. &mov ($_out,$acc); # save out
  2066. &lea ($acc,$ivec);
  2067. &mov ($s0,&DWP(0,$acc)); # read temp
  2068. &mov ($s1,&DWP(4,$acc));
  2069. &mov ($s2,&DWP(8,$acc));
  2070. &mov ($s3,&DWP(12,$acc));
  2071. &mov (&DWP(0,$key),$s0); # copy iv
  2072. &mov (&DWP(4,$key),$s1);
  2073. &mov (&DWP(8,$key),$s2);
  2074. &mov (&DWP(12,$key),$s3);
  2075. &mov ($acc,$_inp); # load inp
  2076. &mov ($s2,$_len); # load len
  2077. &lea ($acc,&DWP(16,$acc)); # advance inp
  2078. &mov ($_inp,$acc); # save inp
  2079. &sub ($s2,16); # decrease len
  2080. &mov ($_len,$s2); # save len
  2081. &jnz (&label("fast_dec_in_place_loop"));
  2082. &set_label("fast_dec_out",4);
  2083. &cmp ($mark,0); # was the key schedule copied?
  2084. &mov ("edi",$_key);
  2085. &je (&label("skip_dzero"));
  2086. # zero copy of key schedule
  2087. &mov ("ecx",240/4);
  2088. &xor ("eax","eax");
  2089. &align (4);
  2090. &data_word(0xABF3F689); # rep stosd
  2091. &set_label("skip_dzero");
  2092. &mov ("esp",$_esp);
  2093. &popf ();
  2094. &function_end_A();
  2095. &pushf (); # kludge, never executed
  2096. #--------------------------- SLOW ROUTINE ---------------------------#
  2097. &set_label("slow_way",16);
  2098. &mov ($s0,&DWP(0,$s0)) if (!$x86only);# load OPENSSL_ia32cap
  2099. &mov ($key,&wparam(3)); # load key
  2100. # pre-allocate aligned stack frame...
  2101. &lea ($acc,&DWP(-80,"esp"));
  2102. &and ($acc,-64);
  2103. # ... and make sure it doesn't alias with $key modulo 1024
  2104. &lea ($s1,&DWP(-80-63,$key));
  2105. &sub ($s1,$acc);
  2106. &neg ($s1);
  2107. &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line
  2108. &sub ($acc,$s1);
  2109. # pick S-box copy which can't overlap with stack frame or $key
  2110. &lea ($s1,&DWP(768,$acc));
  2111. &sub ($s1,$tbl);
  2112. &and ($s1,0x300);
  2113. &lea ($tbl,&DWP(2048+128,$tbl,$s1));
  2114. &lea ($s3,&wparam(0)); # pointer to parameter block
  2115. &exch ("esp",$acc);
  2116. &add ("esp",4); # reserve for return address!
  2117. &mov ($_tbl,$tbl); # save %ebp
  2118. &mov ($_esp,$acc); # save %esp
  2119. &mov ($_tmp,$s0); # save OPENSSL_ia32cap
  2120. &mov ($s0,&DWP(0,$s3)); # load inp
  2121. &mov ($s1,&DWP(4,$s3)); # load out
  2122. #&mov ($s2,&DWP(8,$s3)); # load len
  2123. #&mov ($key,&DWP(12,$s3)); # load key
  2124. &mov ($acc,&DWP(16,$s3)); # load ivp
  2125. &mov ($s3,&DWP(20,$s3)); # load enc flag
  2126. &mov ($_inp,$s0); # save copy of inp
  2127. &mov ($_out,$s1); # save copy of out
  2128. &mov ($_len,$s2); # save copy of len
  2129. &mov ($_key,$key); # save copy of key
  2130. &mov ($_ivp,$acc); # save copy of ivp
  2131. &mov ($key,$acc);
  2132. &mov ($acc,$s0);
  2133. &cmp ($s3,0);
  2134. &je (&label("slow_decrypt"));
  2135. #--------------------------- SLOW ENCRYPT ---------------------------#
  2136. &cmp ($s2,16);
  2137. &mov ($s3,$s1);
  2138. &jb (&label("slow_enc_tail"));
  2139. if (!$x86only) {
  2140. &bt ($_tmp,25); # check for SSE bit
  2141. &jnc (&label("slow_enc_x86"));
  2142. &movq ("mm0",&QWP(0,$key)); # load iv
  2143. &movq ("mm4",&QWP(8,$key));
  2144. &set_label("slow_enc_loop_sse",16);
  2145. &pxor ("mm0",&QWP(0,$acc)); # xor input data
  2146. &pxor ("mm4",&QWP(8,$acc));
  2147. &mov ($key,$_key);
  2148. &call ("_sse_AES_encrypt_compact");
  2149. &mov ($acc,$_inp); # load inp
  2150. &mov ($key,$_out); # load out
  2151. &mov ($s2,$_len); # load len
  2152. &movq (&QWP(0,$key),"mm0"); # save output data
  2153. &movq (&QWP(8,$key),"mm4");
  2154. &lea ($acc,&DWP(16,$acc)); # advance inp
  2155. &mov ($_inp,$acc); # save inp
  2156. &lea ($s3,&DWP(16,$key)); # advance out
  2157. &mov ($_out,$s3); # save out
  2158. &sub ($s2,16); # decrease len
  2159. &cmp ($s2,16);
  2160. &mov ($_len,$s2); # save len
  2161. &jae (&label("slow_enc_loop_sse"));
  2162. &test ($s2,15);
  2163. &jnz (&label("slow_enc_tail"));
  2164. &mov ($acc,$_ivp); # load ivp
  2165. &movq (&QWP(0,$acc),"mm0"); # save ivec
  2166. &movq (&QWP(8,$acc),"mm4");
  2167. &emms ();
  2168. &mov ("esp",$_esp);
  2169. &popf ();
  2170. &function_end_A();
  2171. &pushf (); # kludge, never executed
  2172. }
  2173. &set_label("slow_enc_x86",16);
  2174. &mov ($s0,&DWP(0,$key)); # load iv
  2175. &mov ($s1,&DWP(4,$key));
  2176. &set_label("slow_enc_loop_x86",4);
  2177. &mov ($s2,&DWP(8,$key));
  2178. &mov ($s3,&DWP(12,$key));
  2179. &xor ($s0,&DWP(0,$acc)); # xor input data
  2180. &xor ($s1,&DWP(4,$acc));
  2181. &xor ($s2,&DWP(8,$acc));
  2182. &xor ($s3,&DWP(12,$acc));
  2183. &mov ($key,$_key); # load key
  2184. &call ("_x86_AES_encrypt_compact");
  2185. &mov ($acc,$_inp); # load inp
  2186. &mov ($key,$_out); # load out
  2187. &mov (&DWP(0,$key),$s0); # save output data
  2188. &mov (&DWP(4,$key),$s1);
  2189. &mov (&DWP(8,$key),$s2);
  2190. &mov (&DWP(12,$key),$s3);
  2191. &mov ($s2,$_len); # load len
  2192. &lea ($acc,&DWP(16,$acc)); # advance inp
  2193. &mov ($_inp,$acc); # save inp
  2194. &lea ($s3,&DWP(16,$key)); # advance out
  2195. &mov ($_out,$s3); # save out
  2196. &sub ($s2,16); # decrease len
  2197. &cmp ($s2,16);
  2198. &mov ($_len,$s2); # save len
  2199. &jae (&label("slow_enc_loop_x86"));
  2200. &test ($s2,15);
  2201. &jnz (&label("slow_enc_tail"));
  2202. &mov ($acc,$_ivp); # load ivp
  2203. &mov ($s2,&DWP(8,$key)); # restore last dwords
  2204. &mov ($s3,&DWP(12,$key));
  2205. &mov (&DWP(0,$acc),$s0); # save ivec
  2206. &mov (&DWP(4,$acc),$s1);
  2207. &mov (&DWP(8,$acc),$s2);
  2208. &mov (&DWP(12,$acc),$s3);
  2209. &mov ("esp",$_esp);
  2210. &popf ();
  2211. &function_end_A();
  2212. &pushf (); # kludge, never executed
  2213. &set_label("slow_enc_tail",16);
  2214. &emms () if (!$x86only);
  2215. &mov ($key eq "edi"? $key:"",$s3); # load out to edi
  2216. &mov ($s1,16);
  2217. &sub ($s1,$s2);
  2218. &cmp ($key,$acc eq "esi"? $acc:""); # compare with inp
  2219. &je (&label("enc_in_place"));
  2220. &align (4);
  2221. &data_word(0xA4F3F689); # rep movsb # copy input
  2222. &jmp (&label("enc_skip_in_place"));
  2223. &set_label("enc_in_place");
  2224. &lea ($key,&DWP(0,$key,$s2));
  2225. &set_label("enc_skip_in_place");
  2226. &mov ($s2,$s1);
  2227. &xor ($s0,$s0);
  2228. &align (4);
  2229. &data_word(0xAAF3F689); # rep stosb # zero tail
  2230. &mov ($key,$_ivp); # restore ivp
  2231. &mov ($acc,$s3); # output as input
  2232. &mov ($s0,&DWP(0,$key));
  2233. &mov ($s1,&DWP(4,$key));
  2234. &mov ($_len,16); # len=16
  2235. &jmp (&label("slow_enc_loop_x86")); # one more spin...
  2236. #--------------------------- SLOW DECRYPT ---------------------------#
  2237. &set_label("slow_decrypt",16);
  2238. if (!$x86only) {
  2239. &bt ($_tmp,25); # check for SSE bit
  2240. &jnc (&label("slow_dec_loop_x86"));
  2241. &set_label("slow_dec_loop_sse",4);
  2242. &movq ("mm0",&QWP(0,$acc)); # read input
  2243. &movq ("mm4",&QWP(8,$acc));
  2244. &mov ($key,$_key);
  2245. &call ("_sse_AES_decrypt_compact");
  2246. &mov ($acc,$_inp); # load inp
  2247. &lea ($s0,$ivec);
  2248. &mov ($s1,$_out); # load out
  2249. &mov ($s2,$_len); # load len
  2250. &mov ($key,$_ivp); # load ivp
  2251. &movq ("mm1",&QWP(0,$acc)); # re-read input
  2252. &movq ("mm5",&QWP(8,$acc));
  2253. &pxor ("mm0",&QWP(0,$key)); # xor iv
  2254. &pxor ("mm4",&QWP(8,$key));
  2255. &movq (&QWP(0,$key),"mm1"); # copy input to iv
  2256. &movq (&QWP(8,$key),"mm5");
  2257. &sub ($s2,16); # decrease len
  2258. &jc (&label("slow_dec_partial_sse"));
  2259. &movq (&QWP(0,$s1),"mm0"); # write output
  2260. &movq (&QWP(8,$s1),"mm4");
  2261. &lea ($s1,&DWP(16,$s1)); # advance out
  2262. &mov ($_out,$s1); # save out
  2263. &lea ($acc,&DWP(16,$acc)); # advance inp
  2264. &mov ($_inp,$acc); # save inp
  2265. &mov ($_len,$s2); # save len
  2266. &jnz (&label("slow_dec_loop_sse"));
  2267. &emms ();
  2268. &mov ("esp",$_esp);
  2269. &popf ();
  2270. &function_end_A();
  2271. &pushf (); # kludge, never executed
  2272. &set_label("slow_dec_partial_sse",16);
  2273. &movq (&QWP(0,$s0),"mm0"); # save output to temp
  2274. &movq (&QWP(8,$s0),"mm4");
  2275. &emms ();
  2276. &add ($s2 eq "ecx" ? "ecx":"",16);
  2277. &mov ("edi",$s1); # out
  2278. &mov ("esi",$s0); # temp
  2279. &align (4);
  2280. &data_word(0xA4F3F689); # rep movsb # copy partial output
  2281. &mov ("esp",$_esp);
  2282. &popf ();
  2283. &function_end_A();
  2284. &pushf (); # kludge, never executed
  2285. }
  2286. &set_label("slow_dec_loop_x86",16);
  2287. &mov ($s0,&DWP(0,$acc)); # read input
  2288. &mov ($s1,&DWP(4,$acc));
  2289. &mov ($s2,&DWP(8,$acc));
  2290. &mov ($s3,&DWP(12,$acc));
  2291. &lea ($key,$ivec);
  2292. &mov (&DWP(0,$key),$s0); # copy to temp
  2293. &mov (&DWP(4,$key),$s1);
  2294. &mov (&DWP(8,$key),$s2);
  2295. &mov (&DWP(12,$key),$s3);
  2296. &mov ($key,$_key); # load key
  2297. &call ("_x86_AES_decrypt_compact");
  2298. &mov ($key,$_ivp); # load ivp
  2299. &mov ($acc,$_len); # load len
  2300. &xor ($s0,&DWP(0,$key)); # xor iv
  2301. &xor ($s1,&DWP(4,$key));
  2302. &xor ($s2,&DWP(8,$key));
  2303. &xor ($s3,&DWP(12,$key));
  2304. &sub ($acc,16);
  2305. &jc (&label("slow_dec_partial_x86"));
  2306. &mov ($_len,$acc); # save len
  2307. &mov ($acc,$_out); # load out
  2308. &mov (&DWP(0,$acc),$s0); # write output
  2309. &mov (&DWP(4,$acc),$s1);
  2310. &mov (&DWP(8,$acc),$s2);
  2311. &mov (&DWP(12,$acc),$s3);
  2312. &lea ($acc,&DWP(16,$acc)); # advance out
  2313. &mov ($_out,$acc); # save out
  2314. &lea ($acc,$ivec);
  2315. &mov ($s0,&DWP(0,$acc)); # read temp
  2316. &mov ($s1,&DWP(4,$acc));
  2317. &mov ($s2,&DWP(8,$acc));
  2318. &mov ($s3,&DWP(12,$acc));
  2319. &mov (&DWP(0,$key),$s0); # copy it to iv
  2320. &mov (&DWP(4,$key),$s1);
  2321. &mov (&DWP(8,$key),$s2);
  2322. &mov (&DWP(12,$key),$s3);
  2323. &mov ($acc,$_inp); # load inp
  2324. &lea ($acc,&DWP(16,$acc)); # advance inp
  2325. &mov ($_inp,$acc); # save inp
  2326. &jnz (&label("slow_dec_loop_x86"));
  2327. &mov ("esp",$_esp);
  2328. &popf ();
  2329. &function_end_A();
  2330. &pushf (); # kludge, never executed
  2331. &set_label("slow_dec_partial_x86",16);
  2332. &lea ($acc,$ivec);
  2333. &mov (&DWP(0,$acc),$s0); # save output to temp
  2334. &mov (&DWP(4,$acc),$s1);
  2335. &mov (&DWP(8,$acc),$s2);
  2336. &mov (&DWP(12,$acc),$s3);
  2337. &mov ($acc,$_inp);
  2338. &mov ($s0,&DWP(0,$acc)); # re-read input
  2339. &mov ($s1,&DWP(4,$acc));
  2340. &mov ($s2,&DWP(8,$acc));
  2341. &mov ($s3,&DWP(12,$acc));
  2342. &mov (&DWP(0,$key),$s0); # copy it to iv
  2343. &mov (&DWP(4,$key),$s1);
  2344. &mov (&DWP(8,$key),$s2);
  2345. &mov (&DWP(12,$key),$s3);
  2346. &mov ("ecx",$_len);
  2347. &mov ("edi",$_out);
  2348. &lea ("esi",$ivec);
  2349. &align (4);
  2350. &data_word(0xA4F3F689); # rep movsb # copy partial output
  2351. &mov ("esp",$_esp);
  2352. &popf ();
  2353. &function_end("AES_cbc_encrypt");
  2354. }
  2355. #------------------------------------------------------------------#
  2356. sub enckey()
  2357. {
  2358. &movz ("esi",&LB("edx")); # rk[i]>>0
  2359. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2360. &movz ("esi",&HB("edx")); # rk[i]>>8
  2361. &shl ("ebx",24);
  2362. &xor ("eax","ebx");
  2363. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2364. &shr ("edx",16);
  2365. &movz ("esi",&LB("edx")); # rk[i]>>16
  2366. &xor ("eax","ebx");
  2367. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2368. &movz ("esi",&HB("edx")); # rk[i]>>24
  2369. &shl ("ebx",8);
  2370. &xor ("eax","ebx");
  2371. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2372. &shl ("ebx",16);
  2373. &xor ("eax","ebx");
  2374. &xor ("eax",&DWP(1024-128,$tbl,"ecx",4)); # rcon
  2375. }
  2376. &function_begin("_x86_AES_set_encrypt_key");
  2377. &mov ("esi",&wparam(1)); # user supplied key
  2378. &mov ("edi",&wparam(3)); # private key schedule
  2379. &test ("esi",-1);
  2380. &jz (&label("badpointer"));
  2381. &test ("edi",-1);
  2382. &jz (&label("badpointer"));
  2383. &call (&label("pic_point"));
  2384. &set_label("pic_point");
  2385. &blindpop($tbl);
  2386. &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
  2387. &lea ($tbl,&DWP(2048+128,$tbl));
  2388. # prefetch Te4
  2389. &mov ("eax",&DWP(0-128,$tbl));
  2390. &mov ("ebx",&DWP(32-128,$tbl));
  2391. &mov ("ecx",&DWP(64-128,$tbl));
  2392. &mov ("edx",&DWP(96-128,$tbl));
  2393. &mov ("eax",&DWP(128-128,$tbl));
  2394. &mov ("ebx",&DWP(160-128,$tbl));
  2395. &mov ("ecx",&DWP(192-128,$tbl));
  2396. &mov ("edx",&DWP(224-128,$tbl));
  2397. &mov ("ecx",&wparam(2)); # number of bits in key
  2398. &cmp ("ecx",128);
  2399. &je (&label("10rounds"));
  2400. &cmp ("ecx",192);
  2401. &je (&label("12rounds"));
  2402. &cmp ("ecx",256);
  2403. &je (&label("14rounds"));
  2404. &mov ("eax",-2); # invalid number of bits
  2405. &jmp (&label("exit"));
  2406. &set_label("10rounds");
  2407. &mov ("eax",&DWP(0,"esi")); # copy first 4 dwords
  2408. &mov ("ebx",&DWP(4,"esi"));
  2409. &mov ("ecx",&DWP(8,"esi"));
  2410. &mov ("edx",&DWP(12,"esi"));
  2411. &mov (&DWP(0,"edi"),"eax");
  2412. &mov (&DWP(4,"edi"),"ebx");
  2413. &mov (&DWP(8,"edi"),"ecx");
  2414. &mov (&DWP(12,"edi"),"edx");
  2415. &xor ("ecx","ecx");
  2416. &jmp (&label("10shortcut"));
  2417. &align (4);
  2418. &set_label("10loop");
  2419. &mov ("eax",&DWP(0,"edi")); # rk[0]
  2420. &mov ("edx",&DWP(12,"edi")); # rk[3]
  2421. &set_label("10shortcut");
  2422. &enckey ();
  2423. &mov (&DWP(16,"edi"),"eax"); # rk[4]
  2424. &xor ("eax",&DWP(4,"edi"));
  2425. &mov (&DWP(20,"edi"),"eax"); # rk[5]
  2426. &xor ("eax",&DWP(8,"edi"));
  2427. &mov (&DWP(24,"edi"),"eax"); # rk[6]
  2428. &xor ("eax",&DWP(12,"edi"));
  2429. &mov (&DWP(28,"edi"),"eax"); # rk[7]
  2430. &inc ("ecx");
  2431. &add ("edi",16);
  2432. &cmp ("ecx",10);
  2433. &jl (&label("10loop"));
  2434. &mov (&DWP(80,"edi"),10); # setup number of rounds
  2435. &xor ("eax","eax");
  2436. &jmp (&label("exit"));
  2437. &set_label("12rounds");
  2438. &mov ("eax",&DWP(0,"esi")); # copy first 6 dwords
  2439. &mov ("ebx",&DWP(4,"esi"));
  2440. &mov ("ecx",&DWP(8,"esi"));
  2441. &mov ("edx",&DWP(12,"esi"));
  2442. &mov (&DWP(0,"edi"),"eax");
  2443. &mov (&DWP(4,"edi"),"ebx");
  2444. &mov (&DWP(8,"edi"),"ecx");
  2445. &mov (&DWP(12,"edi"),"edx");
  2446. &mov ("ecx",&DWP(16,"esi"));
  2447. &mov ("edx",&DWP(20,"esi"));
  2448. &mov (&DWP(16,"edi"),"ecx");
  2449. &mov (&DWP(20,"edi"),"edx");
  2450. &xor ("ecx","ecx");
  2451. &jmp (&label("12shortcut"));
  2452. &align (4);
  2453. &set_label("12loop");
  2454. &mov ("eax",&DWP(0,"edi")); # rk[0]
  2455. &mov ("edx",&DWP(20,"edi")); # rk[5]
  2456. &set_label("12shortcut");
  2457. &enckey ();
  2458. &mov (&DWP(24,"edi"),"eax"); # rk[6]
  2459. &xor ("eax",&DWP(4,"edi"));
  2460. &mov (&DWP(28,"edi"),"eax"); # rk[7]
  2461. &xor ("eax",&DWP(8,"edi"));
  2462. &mov (&DWP(32,"edi"),"eax"); # rk[8]
  2463. &xor ("eax",&DWP(12,"edi"));
  2464. &mov (&DWP(36,"edi"),"eax"); # rk[9]
  2465. &cmp ("ecx",7);
  2466. &je (&label("12break"));
  2467. &inc ("ecx");
  2468. &xor ("eax",&DWP(16,"edi"));
  2469. &mov (&DWP(40,"edi"),"eax"); # rk[10]
  2470. &xor ("eax",&DWP(20,"edi"));
  2471. &mov (&DWP(44,"edi"),"eax"); # rk[11]
  2472. &add ("edi",24);
  2473. &jmp (&label("12loop"));
  2474. &set_label("12break");
  2475. &mov (&DWP(72,"edi"),12); # setup number of rounds
  2476. &xor ("eax","eax");
  2477. &jmp (&label("exit"));
  2478. &set_label("14rounds");
  2479. &mov ("eax",&DWP(0,"esi")); # copy first 8 dwords
  2480. &mov ("ebx",&DWP(4,"esi"));
  2481. &mov ("ecx",&DWP(8,"esi"));
  2482. &mov ("edx",&DWP(12,"esi"));
  2483. &mov (&DWP(0,"edi"),"eax");
  2484. &mov (&DWP(4,"edi"),"ebx");
  2485. &mov (&DWP(8,"edi"),"ecx");
  2486. &mov (&DWP(12,"edi"),"edx");
  2487. &mov ("eax",&DWP(16,"esi"));
  2488. &mov ("ebx",&DWP(20,"esi"));
  2489. &mov ("ecx",&DWP(24,"esi"));
  2490. &mov ("edx",&DWP(28,"esi"));
  2491. &mov (&DWP(16,"edi"),"eax");
  2492. &mov (&DWP(20,"edi"),"ebx");
  2493. &mov (&DWP(24,"edi"),"ecx");
  2494. &mov (&DWP(28,"edi"),"edx");
  2495. &xor ("ecx","ecx");
  2496. &jmp (&label("14shortcut"));
  2497. &align (4);
  2498. &set_label("14loop");
  2499. &mov ("edx",&DWP(28,"edi")); # rk[7]
  2500. &set_label("14shortcut");
  2501. &mov ("eax",&DWP(0,"edi")); # rk[0]
  2502. &enckey ();
  2503. &mov (&DWP(32,"edi"),"eax"); # rk[8]
  2504. &xor ("eax",&DWP(4,"edi"));
  2505. &mov (&DWP(36,"edi"),"eax"); # rk[9]
  2506. &xor ("eax",&DWP(8,"edi"));
  2507. &mov (&DWP(40,"edi"),"eax"); # rk[10]
  2508. &xor ("eax",&DWP(12,"edi"));
  2509. &mov (&DWP(44,"edi"),"eax"); # rk[11]
  2510. &cmp ("ecx",6);
  2511. &je (&label("14break"));
  2512. &inc ("ecx");
  2513. &mov ("edx","eax");
  2514. &mov ("eax",&DWP(16,"edi")); # rk[4]
  2515. &movz ("esi",&LB("edx")); # rk[11]>>0
  2516. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2517. &movz ("esi",&HB("edx")); # rk[11]>>8
  2518. &xor ("eax","ebx");
  2519. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2520. &shr ("edx",16);
  2521. &shl ("ebx",8);
  2522. &movz ("esi",&LB("edx")); # rk[11]>>16
  2523. &xor ("eax","ebx");
  2524. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2525. &movz ("esi",&HB("edx")); # rk[11]>>24
  2526. &shl ("ebx",16);
  2527. &xor ("eax","ebx");
  2528. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2529. &shl ("ebx",24);
  2530. &xor ("eax","ebx");
  2531. &mov (&DWP(48,"edi"),"eax"); # rk[12]
  2532. &xor ("eax",&DWP(20,"edi"));
  2533. &mov (&DWP(52,"edi"),"eax"); # rk[13]
  2534. &xor ("eax",&DWP(24,"edi"));
  2535. &mov (&DWP(56,"edi"),"eax"); # rk[14]
  2536. &xor ("eax",&DWP(28,"edi"));
  2537. &mov (&DWP(60,"edi"),"eax"); # rk[15]
  2538. &add ("edi",32);
  2539. &jmp (&label("14loop"));
  2540. &set_label("14break");
  2541. &mov (&DWP(48,"edi"),14); # setup number of rounds
  2542. &xor ("eax","eax");
  2543. &jmp (&label("exit"));
  2544. &set_label("badpointer");
  2545. &mov ("eax",-1);
  2546. &set_label("exit");
  2547. &function_end("_x86_AES_set_encrypt_key");
  2548. # int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
  2549. # AES_KEY *key)
  2550. &function_begin_B("AES_set_encrypt_key");
  2551. &call ("_x86_AES_set_encrypt_key");
  2552. &ret ();
  2553. &function_end_B("AES_set_encrypt_key");
  2554. sub deckey()
  2555. { my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_;
  2556. my $tmp = $tbl;
  2557. &mov ($tmp,0x80808080);
  2558. &and ($tmp,$tp1);
  2559. &lea ($tp2,&DWP(0,$tp1,$tp1));
  2560. &mov ($acc,$tmp);
  2561. &shr ($tmp,7);
  2562. &sub ($acc,$tmp);
  2563. &and ($tp2,0xfefefefe);
  2564. &and ($acc,0x1b1b1b1b);
  2565. &xor ($tp2,$acc);
  2566. &mov ($tmp,0x80808080);
  2567. &and ($tmp,$tp2);
  2568. &lea ($tp4,&DWP(0,$tp2,$tp2));
  2569. &mov ($acc,$tmp);
  2570. &shr ($tmp,7);
  2571. &sub ($acc,$tmp);
  2572. &and ($tp4,0xfefefefe);
  2573. &and ($acc,0x1b1b1b1b);
  2574. &xor ($tp2,$tp1); # tp2^tp1
  2575. &xor ($tp4,$acc);
  2576. &mov ($tmp,0x80808080);
  2577. &and ($tmp,$tp4);
  2578. &lea ($tp8,&DWP(0,$tp4,$tp4));
  2579. &mov ($acc,$tmp);
  2580. &shr ($tmp,7);
  2581. &xor ($tp4,$tp1); # tp4^tp1
  2582. &sub ($acc,$tmp);
  2583. &and ($tp8,0xfefefefe);
  2584. &and ($acc,0x1b1b1b1b);
  2585. &rotl ($tp1,8); # = ROTATE(tp1,8)
  2586. &xor ($tp8,$acc);
  2587. &mov ($tmp,&DWP(4*($i+1),$key)); # modulo-scheduled load
  2588. &xor ($tp1,$tp2);
  2589. &xor ($tp2,$tp8);
  2590. &xor ($tp1,$tp4);
  2591. &rotl ($tp2,24);
  2592. &xor ($tp4,$tp8);
  2593. &xor ($tp1,$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1)
  2594. &rotl ($tp4,16);
  2595. &xor ($tp1,$tp2); # ^= ROTATE(tp8^tp2^tp1,24)
  2596. &rotl ($tp8,8);
  2597. &xor ($tp1,$tp4); # ^= ROTATE(tp8^tp4^tp1,16)
  2598. &mov ($tp2,$tmp);
  2599. &xor ($tp1,$tp8); # ^= ROTATE(tp8,8)
  2600. &mov (&DWP(4*$i,$key),$tp1);
  2601. }
  2602. # int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
  2603. # AES_KEY *key)
  2604. &function_begin_B("AES_set_decrypt_key");
  2605. &call ("_x86_AES_set_encrypt_key");
  2606. &cmp ("eax",0);
  2607. &je (&label("proceed"));
  2608. &ret ();
  2609. &set_label("proceed");
  2610. &push ("ebp");
  2611. &push ("ebx");
  2612. &push ("esi");
  2613. &push ("edi");
  2614. &mov ("esi",&wparam(2));
  2615. &mov ("ecx",&DWP(240,"esi")); # pull number of rounds
  2616. &lea ("ecx",&DWP(0,"","ecx",4));
  2617. &lea ("edi",&DWP(0,"esi","ecx",4)); # pointer to last chunk
  2618. &set_label("invert",4); # invert order of chunks
  2619. &mov ("eax",&DWP(0,"esi"));
  2620. &mov ("ebx",&DWP(4,"esi"));
  2621. &mov ("ecx",&DWP(0,"edi"));
  2622. &mov ("edx",&DWP(4,"edi"));
  2623. &mov (&DWP(0,"edi"),"eax");
  2624. &mov (&DWP(4,"edi"),"ebx");
  2625. &mov (&DWP(0,"esi"),"ecx");
  2626. &mov (&DWP(4,"esi"),"edx");
  2627. &mov ("eax",&DWP(8,"esi"));
  2628. &mov ("ebx",&DWP(12,"esi"));
  2629. &mov ("ecx",&DWP(8,"edi"));
  2630. &mov ("edx",&DWP(12,"edi"));
  2631. &mov (&DWP(8,"edi"),"eax");
  2632. &mov (&DWP(12,"edi"),"ebx");
  2633. &mov (&DWP(8,"esi"),"ecx");
  2634. &mov (&DWP(12,"esi"),"edx");
  2635. &add ("esi",16);
  2636. &sub ("edi",16);
  2637. &cmp ("esi","edi");
  2638. &jne (&label("invert"));
  2639. &mov ($key,&wparam(2));
  2640. &mov ($acc,&DWP(240,$key)); # pull number of rounds
  2641. &lea ($acc,&DWP(-2,$acc,$acc));
  2642. &lea ($acc,&DWP(0,$key,$acc,8));
  2643. &mov (&wparam(2),$acc);
  2644. &mov ($s0,&DWP(16,$key)); # modulo-scheduled load
  2645. &set_label("permute",4); # permute the key schedule
  2646. &add ($key,16);
  2647. &deckey (0,$key,$s0,$s1,$s2,$s3);
  2648. &deckey (1,$key,$s1,$s2,$s3,$s0);
  2649. &deckey (2,$key,$s2,$s3,$s0,$s1);
  2650. &deckey (3,$key,$s3,$s0,$s1,$s2);
  2651. &cmp ($key,&wparam(2));
  2652. &jb (&label("permute"));
  2653. &xor ("eax","eax"); # return success
  2654. &function_end("AES_set_decrypt_key");
  2655. &asciz("AES for x86, CRYPTOGAMS by <appro\@openssl.org>");
  2656. &asm_finish();