aes-586.pl revision 238405
1105197Ssam#!/usr/bin/env perl 2112758Ssam# 3139823Simp# ==================================================================== 4112758Ssam# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL 5112758Ssam# project. The module is, however, dual licensed under OpenSSL and 6112758Ssam# CRYPTOGAMS licenses depending on where you obtain it. For further 7112758Ssam# details see http://www.openssl.org/~appro/cryptogams/. 8112758Ssam# ==================================================================== 9112758Ssam# 10112758Ssam# Version 4.3. 11112758Ssam# 12112758Ssam# You might fail to appreciate this module performance from the first 13112758Ssam# try. If compared to "vanilla" linux-ia32-icc target, i.e. considered 14112758Ssam# to be *the* best Intel C compiler without -KPIC, performance appears 15112758Ssam# to be virtually identical... But try to re-configure with shared 16112758Ssam# library support... Aha! Intel compiler "suddenly" lags behind by 30% 17112758Ssam# [on P4, more on others]:-) And if compared to position-independent 18112758Ssam# code generated by GNU C, this code performs *more* than *twice* as 19112758Ssam# fast! Yes, all this buzz about PIC means that unlike other hand- 20112758Ssam# coded implementations, this one was explicitly designed to be safe 21112758Ssam# to use even in shared library context... This also means that this 22112758Ssam# code isn't necessarily absolutely fastest "ever," because in order 23112758Ssam# to achieve position independence an extra register has to be 24112758Ssam# off-loaded to stack, which affects the benchmark result. 25112758Ssam# 26112758Ssam# Special note about instruction choice. Do you recall RC4_INT code 27112758Ssam# performing poorly on P4? It might be the time to figure out why. 28112758Ssam# RC4_INT code implies effective address calculations in base+offset*4 29112758Ssam# form. Trouble is that it seems that offset scaling turned to be 30112758Ssam# critical path... At least eliminating scaling resulted in 2.8x RC4 31112758Ssam# performance improvement [as you might recall]. As AES code is hungry 32112758Ssam# for scaling too, I [try to] avoid the latter by favoring off-by-2 33112758Ssam# shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF. 34112758Ssam# 35112758Ssam# As was shown by Dean Gaudet <dean@arctic.org>, the above note turned 36112758Ssam# void. Performance improvement with off-by-2 shifts was observed on 37112758Ssam# intermediate implementation, which was spilling yet another register 38105197Ssam# to stack... Final offset*4 code below runs just a tad faster on P4, 39105197Ssam# but exhibits up to 10% improvement on other cores. 40105197Ssam# 41105197Ssam# Second version is "monolithic" replacement for aes_core.c, which in 42105197Ssam# addition to AES_[de|en]crypt implements private_AES_set_[de|en]cryption_key. 43105197Ssam# This made it possible to implement little-endian variant of the 44105197Ssam# algorithm without modifying the base C code. Motivating factor for 45105197Ssam# the undertaken effort was that it appeared that in tight IA-32 46159965Sthompsa# register window little-endian flavor could achieve slightly higher 47105197Ssam# Instruction Level Parallelism, and it indeed resulted in up to 15% 48105197Ssam# better performance on most recent �-archs... 49105197Ssam# 50105197Ssam# Third version adds AES_cbc_encrypt implementation, which resulted in 51105197Ssam# up to 40% performance imrovement of CBC benchmark results. 40% was 52105197Ssam# observed on P4 core, where "overall" imrovement coefficient, i.e. if 53105197Ssam# compared to PIC generated by GCC and in CBC mode, was observed to be 54105197Ssam# as large as 4x:-) CBC performance is virtually identical to ECB now 55105197Ssam# and on some platforms even better, e.g. 17.6 "small" cycles/byte on 56105197Ssam# Opteron, because certain function prologues and epilogues are 57105197Ssam# effectively taken out of the loop... 58105197Ssam# 59171497Sbz# Version 3.2 implements compressed tables and prefetch of these tables 60105197Ssam# in CBC[!] mode. Former means that 3/4 of table references are now 61105197Ssam# misaligned, which unfortunately has negative impact on elder IA-32 62195699Srwatson# implementations, Pentium suffered 30% penalty, PIII - 10%. 63105197Ssam# 64105197Ssam# Version 3.3 avoids L1 cache aliasing between stack frame and 65105197Ssam# S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The 66105197Ssam# latter is achieved by copying the key schedule to controlled place in 67105197Ssam# stack. This unfortunately has rather strong impact on small block CBC 68105197Ssam# performance, ~2x deterioration on 16-byte block if compared to 3.3. 69105197Ssam# 70105197Ssam# Version 3.5 checks if there is L1 cache aliasing between user-supplied 71105197Ssam# key schedule and S-boxes and abstains from copying the former if 72105197Ssam# there is no. This allows end-user to consciously retain small block 73105197Ssam# performance by aligning key schedule in specific manner. 74105197Ssam# 75105197Ssam# Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB. 76105197Ssam# 77105197Ssam# Current ECB performance numbers for 128-bit key in CPU cycles per 78105197Ssam# processed byte [measure commonly used by AES benchmarkers] are: 79105197Ssam# 80105197Ssam# small footprint fully unrolled 81105197Ssam# P4 24 22 82105197Ssam# AMD K8 20 19 83105197Ssam# PIII 25 23 84105197Ssam# Pentium 81 78 85105197Ssam# 86105197Ssam# Version 3.7 reimplements outer rounds as "compact." Meaning that 87105197Ssam# first and last rounds reference compact 256 bytes S-box. This means 88105197Ssam# that first round consumes a lot more CPU cycles and that encrypt 89105197Ssam# and decrypt performance becomes asymmetric. Encrypt performance 90105197Ssam# drops by 10-12%, while decrypt - by 20-25%:-( 256 bytes S-box is 91105197Ssam# aggressively pre-fetched. 92105197Ssam# 93105197Ssam# Version 4.0 effectively rolls back to 3.6 and instead implements 94105197Ssam# additional set of functions, _[x86|sse]_AES_[en|de]crypt_compact, 95105197Ssam# which use exclusively 256 byte S-box. These functions are to be 96105197Ssam# called in modes not concealing plain text, such as ECB, or when 97181627Svanhu# we're asked to process smaller amount of data [or unconditionally 98181627Svanhu# on hyper-threading CPU]. Currently it's called unconditionally from 99181627Svanhu# AES_[en|de]crypt, which affects all modes, but CBC. CBC routine 100181627Svanhu# still needs to be modified to switch between slower and faster 101181627Svanhu# mode when appropriate... But in either case benchmark landscape 102105197Ssam# changes dramatically and below numbers are CPU cycles per processed 103105197Ssam# byte for 128-bit key. 104105197Ssam# 105193947Sbz# ECB encrypt ECB decrypt CBC large chunk 106120585Ssam# P4 56[60] 84[100] 23 107193947Sbz# AMD K8 48[44] 70[79] 18 108120585Ssam# PIII 41[50] 61[91] 24 109105197Ssam# Core 2 32[38] 45[70] 18.5 110105197Ssam# Pentium 120 160 77 111214351Sthomas# 112170793Sbz# Version 4.1 switches to compact S-box even in key schedule setup. 113105197Ssam# 114105197Ssam# Version 4.2 prefetches compact S-box in every SSE round or in other 115105197Ssam# words every cache-line is *guaranteed* to be accessed within ~50 116105197Ssam# cycles window. Why just SSE? Because it's needed on hyper-threading 117105197Ssam# CPU! Which is also why it's prefetched with 64 byte stride. Best 118105197Ssam# part is that it has no negative effect on performance:-) 119105197Ssam# 120105197Ssam# Version 4.3 implements switch between compact and non-compact block 121119643Ssam# functions in AES_cbc_encrypt depending on how much data was asked 122221129Sbz# to be processed in one stroke. 123194062Svanhu# 124194062Svanhu###################################################################### 125194062Svanhu# Timing attacks are classified in two classes: synchronous when 126221129Sbz# attacker consciously initiates cryptographic operation and collects 127105197Ssam# timing data of various character afterwards, and asynchronous when 128181803Sbz# malicious code is executed on same CPU simultaneously with AES, 129181803Sbz# instruments itself and performs statistical analysis of this data. 130105197Ssam# 131120585Ssam# As far as synchronous attacks go the root to the AES timing 132105197Ssam# vulnerability is twofold. Firstly, of 256 S-box elements at most 160 133170792Sbz# are referred to in single 128-bit block operation. Well, in C 134170792Sbz# implementation with 4 distinct tables it's actually as little as 40 135170792Sbz# references per 256 elements table, but anyway... Secondly, even 136170792Sbz# though S-box elements are clustered into smaller amount of cache- 137181803Sbz# lines, smaller than 160 and even 40, it turned out that for certain 138181803Sbz# plain-text pattern[s] or simply put chosen plain-text and given key 139181803Sbz# few cache-lines remain unaccessed during block operation. Now, if 140105197Ssam# attacker can figure out this access pattern, he can deduct the key 141181803Sbz# [or at least part of it]. The natural way to mitigate this kind of 142181803Sbz# attacks is to minimize the amount of cache-lines in S-box and/or 143105197Ssam# prefetch them to ensure that every one is accessed for more uniform 144105197Ssam# timing. But note that *if* plain-text was concealed in such way that 145105197Ssam# input to block function is distributed *uniformly*, then attack 146105197Ssam# wouldn't apply. Now note that some encryption modes, most notably 147105197Ssam# CBC, do mask the plain-text in this exact way [secure cipher output 148181803Sbz# is distributed uniformly]. Yes, one still might find input that 149181803Sbz# would reveal the information about given key, but if amount of 150120585Ssam# candidate inputs to be tried is larger than amount of possible key 151105197Ssam# combinations then attack becomes infeasible. This is why revised 152105197Ssam# AES_cbc_encrypt "dares" to switch to larger S-box when larger chunk 153105197Ssam# of data is to be processed in one stroke. The current size limit of 154105197Ssam# 512 bytes is chosen to provide same [diminishigly low] probability 155105197Ssam# for cache-line to remain untouched in large chunk operation with 156105197Ssam# large S-box as for single block operation with compact S-box and 157105197Ssam# surely needs more careful consideration... 158105197Ssam# 159105197Ssam# As for asynchronous attacks. There are two flavours: attacker code 160105197Ssam# being interleaved with AES on hyper-threading CPU at *instruction* 161105197Ssam# level, and two processes time sharing single core. As for latter. 162105197Ssam# Two vectors. 1. Given that attacker process has higher priority, 163105197Ssam# yield execution to process performing AES just before timer fires 164105197Ssam# off the scheduler, immediately regain control of CPU and analyze the 165105197Ssam# cache state. For this attack to be efficient attacker would have to 166105197Ssam# effectively slow down the operation by several *orders* of magnitute, 167105197Ssam# by ratio of time slice to duration of handful of AES rounds, which 168105197Ssam# unlikely to remain unnoticed. Not to mention that this also means 169105197Ssam# that he would spend correspondigly more time to collect enough 170105197Ssam# statistical data to mount the attack. It's probably appropriate to 171105197Ssam# say that if adeversary reckons that this attack is beneficial and 172105197Ssam# risks to be noticed, you probably have larger problems having him 173105197Ssam# mere opportunity. In other words suggested code design expects you 174105197Ssam# to preclude/mitigate this attack by overall system security design. 175105197Ssam# 2. Attacker manages to make his code interrupt driven. In order for 176105197Ssam# this kind of attack to be feasible, interrupt rate has to be high 177105197Ssam# enough, again comparable to duration of handful of AES rounds. But 178105197Ssam# is there interrupt source of such rate? Hardly, not even 1Gbps NIC 179105197Ssam# generates interrupts at such raging rate... 180105197Ssam# 181194062Svanhu# And now back to the former, hyper-threading CPU or more specifically 182194062Svanhu# Intel P4. Recall that asynchronous attack implies that malicious 183194062Svanhu# code instruments itself. And naturally instrumentation granularity 184194062Svanhu# has be noticeably lower than duration of codepath accessing S-box. 185194062Svanhu# Given that all cache-lines are accessed during that time that is. 186194062Svanhu# Current implementation accesses *all* cache-lines within ~50 cycles 187105197Ssam# window, which is actually *less* than RDTSC latency on Intel P4! 188105197Ssam 189105197Ssam$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 190105197Ssampush(@INC,"${dir}","${dir}../../perlasm"); 191105197Ssamrequire "x86asm.pl"; 192105197Ssam 193105197Ssam&asm_init($ARGV[0],"aes-586.pl",$x86only = $ARGV[$#ARGV] eq "386"); 194105197Ssam&static_label("AES_Te"); 195105197Ssam&static_label("AES_Td"); 196105197Ssam 197105197Ssam$s0="eax"; 198120585Ssam$s1="ebx"; 199105197Ssam$s2="ecx"; 200181803Sbz$s3="edx"; 201181803Sbz$key="edi"; 202105197Ssam$acc="esi"; 203105197Ssam$tbl="ebp"; 204105197Ssam 205105197Ssam# stack frame layout in _[x86|sse]_AES_* routines, frame is allocated 206105197Ssam# by caller 207105197Ssam$__ra=&DWP(0,"esp"); # return address 208120585Ssam$__s0=&DWP(4,"esp"); # s0 backing store 209120585Ssam$__s1=&DWP(8,"esp"); # s1 backing store 210105197Ssam$__s2=&DWP(12,"esp"); # s2 backing store 211181803Sbz$__s3=&DWP(16,"esp"); # s3 backing store 212181803Sbz$__key=&DWP(20,"esp"); # pointer to key schedule 213105197Ssam$__end=&DWP(24,"esp"); # pointer to end of key schedule 214105197Ssam$__tbl=&DWP(28,"esp"); # %ebp backing store 215105197Ssam 216105197Ssam# stack frame layout in AES_[en|crypt] routines, which differs from 217105197Ssam# above by 4 and overlaps by %ebp backing store 218120585Ssam$_tbl=&DWP(24,"esp"); 219120585Ssam$_esp=&DWP(28,"esp"); 220105197Ssam 221181803Sbzsub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } } 222181803Sbz 223105197Ssam$speed_limit=512; # chunks smaller than $speed_limit are 224105197Ssam # processed with compact routine in CBC mode 225105197Ssam$small_footprint=1; # $small_footprint=1 code is ~5% slower [on 226105197Ssam # recent �-archs], but ~5 times smaller! 227105197Ssam # I favor compact code to minimize cache 228105197Ssam # contention and in hope to "collect" 5% back 229105197Ssam # in real-life applications... 230105197Ssam 231105197Ssam$vertical_spin=0; # shift "verticaly" defaults to 0, because of 232105197Ssam # its proof-of-concept status... 233105197Ssam# Note that there is no decvert(), as well as last encryption round is 234105197Ssam# performed with "horizontal" shifts. This is because this "vertical" 235105197Ssam# implementation [one which groups shifts on a given $s[i] to form a 236105197Ssam# "column," unlike "horizontal" one, which groups shifts on different 237105197Ssam# $s[i] to form a "row"] is work in progress. It was observed to run 238105197Ssam# few percents faster on Intel cores, but not AMD. On AMD K8 core it's 239105197Ssam# whole 12% slower:-( So we face a trade-off... Shall it be resolved 240105197Ssam# some day? Till then the code is considered experimental and by 241105197Ssam# default remains dormant... 242105197Ssam 243105197Ssamsub encvert() 244105197Ssam{ my ($te,@s) = @_; 245105197Ssam my $v0 = $acc, $v1 = $key; 246105197Ssam 247105197Ssam &mov ($v0,$s[3]); # copy s3 248105197Ssam &mov (&DWP(4,"esp"),$s[2]); # save s2 249105197Ssam &mov ($v1,$s[0]); # copy s0 250105197Ssam &mov (&DWP(8,"esp"),$s[1]); # save s1 251105197Ssam 252105197Ssam &movz ($s[2],&HB($s[0])); 253105197Ssam &and ($s[0],0xFF); 254105197Ssam &mov ($s[0],&DWP(0,$te,$s[0],8)); # s0>>0 255105197Ssam &shr ($v1,16); 256106680Ssam &mov ($s[3],&DWP(3,$te,$s[2],8)); # s0>>8 257106680Ssam &movz ($s[1],&HB($v1)); 258106680Ssam &and ($v1,0xFF); 259106680Ssam &mov ($s[2],&DWP(2,$te,$v1,8)); # s0>>16 260106680Ssam &mov ($v1,$v0); 261120585Ssam &mov ($s[1],&DWP(1,$te,$s[1],8)); # s0>>24 262120585Ssam 263120585Ssam &and ($v0,0xFF); 264120585Ssam &xor ($s[3],&DWP(0,$te,$v0,8)); # s3>>0 265120585Ssam &movz ($v0,&HB($v1)); 266120585Ssam &shr ($v1,16); 267120585Ssam &xor ($s[2],&DWP(3,$te,$v0,8)); # s3>>8 268106680Ssam &movz ($v0,&HB($v1)); 269106680Ssam &and ($v1,0xFF); 270106680Ssam &xor ($s[1],&DWP(2,$te,$v1,8)); # s3>>16 271106680Ssam &mov ($v1,&DWP(4,"esp")); # restore s2 272106680Ssam &xor ($s[0],&DWP(1,$te,$v0,8)); # s3>>24 273106680Ssam 274120585Ssam &mov ($v0,$v1); 275120585Ssam &and ($v1,0xFF); 276120585Ssam &xor ($s[2],&DWP(0,$te,$v1,8)); # s2>>0 277120585Ssam &movz ($v1,&HB($v0)); 278120585Ssam &shr ($v0,16); 279120585Ssam &xor ($s[1],&DWP(3,$te,$v1,8)); # s2>>8 280120585Ssam &movz ($v1,&HB($v0)); 281106680Ssam &and ($v0,0xFF); 282106680Ssam &xor ($s[0],&DWP(2,$te,$v0,8)); # s2>>16 283106680Ssam &mov ($v0,&DWP(8,"esp")); # restore s1 284106680Ssam &xor ($s[3],&DWP(1,$te,$v1,8)); # s2>>24 285106680Ssam 286106680Ssam &mov ($v1,$v0); 287106680Ssam &and ($v0,0xFF); 288105197Ssam &xor ($s[1],&DWP(0,$te,$v0,8)); # s1>>0 289105197Ssam &movz ($v0,&HB($v1)); 290105197Ssam &shr ($v1,16); 291105197Ssam &xor ($s[0],&DWP(3,$te,$v0,8)); # s1>>8 292105197Ssam &movz ($v0,&HB($v1)); 293105197Ssam &and ($v1,0xFF); 294105197Ssam &xor ($s[3],&DWP(2,$te,$v1,8)); # s1>>16 295105197Ssam &mov ($key,$__key); # reincarnate v1 as key 296105197Ssam &xor ($s[2],&DWP(1,$te,$v0,8)); # s1>>24 297105197Ssam} 298105197Ssam 299105197Ssam# Another experimental routine, which features "horizontal spin," but 300105197Ssam# eliminates one reference to stack. Strangely enough runs slower... 301105197Ssamsub enchoriz() 302105197Ssam{ my $v0 = $key, $v1 = $acc; 303105197Ssam 304165222Sbz &movz ($v0,&LB($s0)); # 3, 2, 1, 0* 305165118Sbz &rotr ($s2,8); # 8,11,10, 9 306165118Sbz &mov ($v1,&DWP(0,$te,$v0,8)); # 0 307165118Sbz &movz ($v0,&HB($s1)); # 7, 6, 5*, 4 308165118Sbz &rotr ($s3,16); # 13,12,15,14 309105197Ssam &xor ($v1,&DWP(3,$te,$v0,8)); # 5 310120585Ssam &movz ($v0,&HB($s2)); # 8,11,10*, 9 311120585Ssam &rotr ($s0,16); # 1, 0, 3, 2 312120585Ssam &xor ($v1,&DWP(2,$te,$v0,8)); # 10 313105197Ssam &movz ($v0,&HB($s3)); # 13,12,15*,14 314105197Ssam &xor ($v1,&DWP(1,$te,$v0,8)); # 15, t[0] collected 315120585Ssam &mov ($__s0,$v1); # t[0] saved 316105197Ssam 317120585Ssam &movz ($v0,&LB($s1)); # 7, 6, 5, 4* 318105197Ssam &shr ($s1,16); # -, -, 7, 6 319120585Ssam &mov ($v1,&DWP(0,$te,$v0,8)); # 4 320105197Ssam &movz ($v0,&LB($s3)); # 13,12,15,14* 321105197Ssam &xor ($v1,&DWP(2,$te,$v0,8)); # 14 322105197Ssam &movz ($v0,&HB($s0)); # 1, 0, 3*, 2 323120585Ssam &and ($s3,0xffff0000); # 13,12, -, - 324181803Sbz &xor ($v1,&DWP(1,$te,$v0,8)); # 3 325181803Sbz &movz ($v0,&LB($s2)); # 8,11,10, 9* 326105197Ssam &or ($s3,$s1); # 13,12, 7, 6 327105197Ssam &xor ($v1,&DWP(3,$te,$v0,8)); # 9, t[1] collected 328105197Ssam &mov ($s1,$v1); # s[1]=t[1] 329105197Ssam 330105197Ssam &movz ($v0,&LB($s0)); # 1, 0, 3, 2* 331105197Ssam &shr ($s2,16); # -, -, 8,11 332105197Ssam &mov ($v1,&DWP(2,$te,$v0,8)); # 2 333120585Ssam &movz ($v0,&HB($s3)); # 13,12, 7*, 6 334120585Ssam &xor ($v1,&DWP(1,$te,$v0,8)); # 7 335105197Ssam &movz ($v0,&HB($s2)); # -, -, 8*,11 336181803Sbz &xor ($v1,&DWP(0,$te,$v0,8)); # 8 337181803Sbz &mov ($v0,$s3); 338105197Ssam &shr ($v0,24); # 13 339105197Ssam &xor ($v1,&DWP(3,$te,$v0,8)); # 13, t[2] collected 340105197Ssam 341105197Ssam &movz ($v0,&LB($s2)); # -, -, 8,11* 342105197Ssam &shr ($s0,24); # 1* 343105197Ssam &mov ($s2,&DWP(1,$te,$v0,8)); # 11 344105197Ssam &xor ($s2,&DWP(3,$te,$s0,8)); # 1 345105197Ssam &mov ($s0,$__s0); # s[0]=t[0] 346105197Ssam &movz ($v0,&LB($s3)); # 13,12, 7, 6* 347105197Ssam &shr ($s3,16); # , ,13,12 348105197Ssam &xor ($s2,&DWP(2,$te,$v0,8)); # 6 349105197Ssam &mov ($key,$__key); # reincarnate v0 as key 350105197Ssam &and ($s3,0xff); # , ,13,12* 351105197Ssam &mov ($s3,&DWP(0,$te,$s3,8)); # 12 352159215Sgnn &xor ($s3,$s2); # s[2]=t[3] collected 353105197Ssam &mov ($s2,$v1); # s[2]=t[2] 354105197Ssam} 355105197Ssam 356105197Ssam# More experimental code... SSE one... Even though this one eliminates 357118888Ssam# *all* references to stack, it's not faster... 358181803Sbzsub sse_encbody() 359181803Sbz{ 360181803Sbz &movz ($acc,&LB("eax")); # 0 361118888Ssam &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 0 362118888Ssam &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2 363118888Ssam &movz ("edx",&HB("eax")); # 1 364105197Ssam &mov ("edx",&DWP(3,$tbl,"edx",8)); # 1 365105197Ssam &shr ("eax",16); # 5, 4 366105197Ssam 367105197Ssam &movz ($acc,&LB("ebx")); # 10 368105197Ssam &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 10 369105197Ssam &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8 370105197Ssam &movz ($acc,&HB("ebx")); # 11 371105197Ssam &xor ("edx",&DWP(1,$tbl,$acc,8)); # 11 372105197Ssam &shr ("ebx",16); # 15,14 373105197Ssam 374105197Ssam &movz ($acc,&HB("eax")); # 5 375105197Ssam &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 5 376105197Ssam &movq ("mm3",QWP(16,$key)); 377105197Ssam &movz ($acc,&HB("ebx")); # 15 378105197Ssam &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 15 379105197Ssam &movd ("mm0","ecx"); # t[0] collected 380105197Ssam 381120585Ssam &movz ($acc,&LB("eax")); # 4 382120585Ssam &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 4 383120585Ssam &movd ("eax","mm2"); # 7, 6, 3, 2 384105197Ssam &movz ($acc,&LB("ebx")); # 14 385105197Ssam &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 14 386105197Ssam &movd ("ebx","mm6"); # 13,12, 9, 8 387105197Ssam 388105197Ssam &movz ($acc,&HB("eax")); # 3 389181803Sbz &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 3 390181803Sbz &movz ($acc,&HB("ebx")); # 9 391181803Sbz &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 9 392105197Ssam &movd ("mm1","ecx"); # t[1] collected 393105197Ssam 394105197Ssam &movz ($acc,&LB("eax")); # 2 395105197Ssam &mov ("ecx",&DWP(2,$tbl,$acc,8)); # 2 396159237Spjd &shr ("eax",16); # 7, 6 397105197Ssam &punpckldq ("mm0","mm1"); # t[0,1] collected 398105197Ssam &movz ($acc,&LB("ebx")); # 8 399105197Ssam &xor ("ecx",&DWP(0,$tbl,$acc,8)); # 8 400105197Ssam &shr ("ebx",16); # 13,12 401118888Ssam 402181803Sbz &movz ($acc,&HB("eax")); # 7 403181803Sbz &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 7 404181803Sbz &pxor ("mm0","mm3"); 405118888Ssam &movz ("eax",&LB("eax")); # 6 406118888Ssam &xor ("edx",&DWP(2,$tbl,"eax",8)); # 6 407118888Ssam &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0 408105197Ssam &movz ($acc,&HB("ebx")); # 13 409105197Ssam &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 13 410105197Ssam &xor ("ecx",&DWP(24,$key)); # t[2] 411105197Ssam &movd ("mm4","ecx"); # t[2] collected 412105197Ssam &movz ("ebx",&LB("ebx")); # 12 413105197Ssam &xor ("edx",&DWP(0,$tbl,"ebx",8)); # 12 414105197Ssam &shr ("ecx",16); 415105197Ssam &movd ("eax","mm1"); # 5, 4, 1, 0 416105197Ssam &mov ("ebx",&DWP(28,$key)); # t[3] 417105197Ssam &xor ("ebx","edx"); 418105197Ssam &movd ("mm5","ebx"); # t[3] collected 419105197Ssam &and ("ebx",0xffff0000); 420105197Ssam &or ("ebx","ecx"); 421105197Ssam 422105197Ssam &punpckldq ("mm4","mm5"); # t[2,3] collected 423120585Ssam} 424120585Ssam 425120585Ssam###################################################################### 426165118Sbz# "Compact" block function 427105197Ssam###################################################################### 428105197Ssam 429105197Ssamsub enccompact() 430105197Ssam{ my $Fn = mov; 431181803Sbz while ($#_>5) { pop(@_); $Fn=sub{}; } 432181803Sbz my ($i,$te,@s)=@_; 433181803Sbz my $tmp = $key; 434105197Ssam my $out = $i==3?$s[0]:$acc; 435105197Ssam 436105197Ssam # $Fn is used in first compact round and its purpose is to 437105197Ssam # void restoration of some values from stack, so that after 438105197Ssam # 4xenccompact with extra argument $key value is left there... 439159215Sgnn if ($i==3) { &$Fn ($key,$__key); }##%edx 440105197Ssam else { &mov ($out,$s[0]); } 441105197Ssam &and ($out,0xFF); 442105197Ssam if ($i==1) { &shr ($s[0],16); }#%ebx[1] 443105197Ssam if ($i==2) { &shr ($s[0],24); }#%ecx[2] 444105197Ssam &movz ($out,&BP(-128,$te,$out,1)); 445105197Ssam 446105197Ssam if ($i==3) { $tmp=$s[1]; }##%eax 447105197Ssam &movz ($tmp,&HB($s[1])); 448105197Ssam &movz ($tmp,&BP(-128,$te,$tmp,1)); 449105197Ssam &shl ($tmp,8); 450105197Ssam &xor ($out,$tmp); 451105197Ssam 452105197Ssam if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx 453120585Ssam else { &mov ($tmp,$s[2]); 454181803Sbz &shr ($tmp,16); } 455181803Sbz if ($i==2) { &and ($s[1],0xFF); }#%edx[2] 456105197Ssam &and ($tmp,0xFF); 457105197Ssam &movz ($tmp,&BP(-128,$te,$tmp,1)); 458105197Ssam &shl ($tmp,16); 459105197Ssam &xor ($out,$tmp); 460105197Ssam 461105197Ssam if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx 462105197Ssam elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2] 463105197Ssam else { &mov ($tmp,$s[3]); 464174054Sbz &shr ($tmp,24); } 465174054Sbz &movz ($tmp,&BP(-128,$te,$tmp,1)); 466174054Sbz &shl ($tmp,24); 467105197Ssam &xor ($out,$tmp); 468105197Ssam if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 469170797Sbz if ($i==3) { &mov ($s[3],$acc); } 470105197Ssam &comment(); 471105197Ssam} 472105197Ssam 473105197Ssamsub enctransform() 474105197Ssam{ my @s = ($s0,$s1,$s2,$s3); 475105197Ssam my $i = shift; 476159965Sthompsa my $tmp = $tbl; 477181627Svanhu my $r2 = $key ; 478181627Svanhu 479181627Svanhu &mov ($acc,$s[$i]); 480105197Ssam &and ($acc,0x80808080); 481159965Sthompsa &mov ($tmp,$acc); 482159965Sthompsa &shr ($tmp,7); 483159965Sthompsa &lea ($r2,&DWP(0,$s[$i],$s[$i])); 484174054Sbz &sub ($acc,$tmp); 485159965Sthompsa &and ($r2,0xfefefefe); 486159965Sthompsa &and ($acc,0x1b1b1b1b); 487174054Sbz &mov ($tmp,$s[$i]); 488159965Sthompsa &xor ($acc,$r2); # r2 489159965Sthompsa 490159965Sthompsa &xor ($s[$i],$acc); # r0 ^ r2 491159965Sthompsa &rotl ($s[$i],24); 492105197Ssam &xor ($s[$i],$acc) # ROTATE(r2^r0,24) ^ r2 493105197Ssam &rotr ($tmp,16); 494208508Sbz &xor ($s[$i],$tmp); 495181803Sbz &rotr ($tmp,8); 496181803Sbz &xor ($s[$i],$tmp); 497105197Ssam} 498120585Ssam 499120585Ssam&function_begin_B("_x86_AES_encrypt_compact"); 500134391Sandre # note that caller is expected to allocate stack frame for me! 501105197Ssam &mov ($__key,$key); # save key 502105197Ssam 503105197Ssam &xor ($s0,&DWP(0,$key)); # xor with key 504105197Ssam &xor ($s1,&DWP(4,$key)); 505105197Ssam &xor ($s2,&DWP(8,$key)); 506105197Ssam &xor ($s3,&DWP(12,$key)); 507120585Ssam 508120585Ssam &mov ($acc,&DWP(240,$key)); # load key->rounds 509120585Ssam &lea ($acc,&DWP(-2,$acc,$acc)); 510120585Ssam &lea ($acc,&DWP(0,$key,$acc,8)); 511120585Ssam &mov ($__end,$acc); # end of key schedule 512120585Ssam 513105197Ssam # prefetch Te4 514105197Ssam &mov ($key,&DWP(0-128,$tbl)); 515105197Ssam &mov ($acc,&DWP(32-128,$tbl)); 516105197Ssam &mov ($key,&DWP(64-128,$tbl)); 517105197Ssam &mov ($acc,&DWP(96-128,$tbl)); 518105197Ssam &mov ($key,&DWP(128-128,$tbl)); 519105197Ssam &mov ($acc,&DWP(160-128,$tbl)); 520105197Ssam &mov ($key,&DWP(192-128,$tbl)); 521105197Ssam &mov ($acc,&DWP(224-128,$tbl)); 522105197Ssam 523105197Ssam &set_label("loop",16); 524105197Ssam 525120585Ssam &enccompact(0,$tbl,$s0,$s1,$s2,$s3,1); 526105197Ssam &enccompact(1,$tbl,$s1,$s2,$s3,$s0,1); 527105197Ssam &enccompact(2,$tbl,$s2,$s3,$s0,$s1,1); 528105197Ssam &enccompact(3,$tbl,$s3,$s0,$s1,$s2,1); 529105197Ssam &enctransform(2); 530105197Ssam &enctransform(3); 531105197Ssam &enctransform(0); 532105197Ssam &enctransform(1); 533105197Ssam &mov ($key,$__key); 534105197Ssam &mov ($tbl,$__tbl); 535105197Ssam &add ($key,16); # advance rd_key 536105197Ssam &xor ($s0,&DWP(0,$key)); 537105197Ssam &xor ($s1,&DWP(4,$key)); 538105197Ssam &xor ($s2,&DWP(8,$key)); 539105197Ssam &xor ($s3,&DWP(12,$key)); 540105197Ssam 541105197Ssam &cmp ($key,$__end); 542120585Ssam &mov ($__key,$key); 543105197Ssam &jb (&label("loop")); 544105197Ssam 545105197Ssam &enccompact(0,$tbl,$s0,$s1,$s2,$s3); 546105197Ssam &enccompact(1,$tbl,$s1,$s2,$s3,$s0); 547120585Ssam &enccompact(2,$tbl,$s2,$s3,$s0,$s1); 548120585Ssam &enccompact(3,$tbl,$s3,$s0,$s1,$s2); 549181803Sbz 550181803Sbz &xor ($s0,&DWP(16,$key)); 551181803Sbz &xor ($s1,&DWP(20,$key)); 552105197Ssam &xor ($s2,&DWP(24,$key)); 553105197Ssam &xor ($s3,&DWP(28,$key)); 554105197Ssam 555105197Ssam &ret (); 556105197Ssam&function_end_B("_x86_AES_encrypt_compact"); 557105197Ssam 558105197Ssam###################################################################### 559105197Ssam# "Compact" SSE block function. 560105197Ssam###################################################################### 561105197Ssam# 562105197Ssam# Performance is not actually extraordinary in comparison to pure 563105197Ssam# x86 code. In particular encrypt performance is virtually the same. 564105197Ssam# Decrypt performance on the other hand is 15-20% better on newer 565105197Ssam# �-archs [but we're thankful for *any* improvement here], and ~50% 566105197Ssam# better on PIII:-) And additionally on the pros side this code 567105197Ssam# eliminates redundant references to stack and thus relieves/ 568105197Ssam# minimizes the pressure on the memory bus. 569105197Ssam# 570105197Ssam# MMX register layout lsb 571105197Ssam# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 572105197Ssam# | mm4 | mm0 | 573105197Ssam# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 574105197Ssam# | s3 | s2 | s1 | s0 | 575105197Ssam# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 576105197Ssam# |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| 577105197Ssam# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 578165118Sbz# 579165118Sbz# Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8. 580165118Sbz# In this terms encryption and decryption "compact" permutation 581105197Ssam# matrices can be depicted as following: 582120585Ssam# 583120585Ssam# encryption lsb # decryption lsb 584120585Ssam# +----++----+----+----+----+ # +----++----+----+----+----+ 585105197Ssam# | t0 || 15 | 10 | 5 | 0 | # | t0 || 7 | 10 | 13 | 0 | 586105197Ssam# +----++----+----+----+----+ # +----++----+----+----+----+ 587120585Ssam# | t1 || 3 | 14 | 9 | 4 | # | t1 || 11 | 14 | 1 | 4 | 588105197Ssam# +----++----+----+----+----+ # +----++----+----+----+----+ 589120585Ssam# | t2 || 7 | 2 | 13 | 8 | # | t2 || 15 | 2 | 5 | 8 | 590105197Ssam# +----++----+----+----+----+ # +----++----+----+----+----+ 591120585Ssam# | t3 || 11 | 6 | 1 | 12 | # | t3 || 3 | 6 | 9 | 12 | 592105197Ssam# +----++----+----+----+----+ # +----++----+----+----+----+ 593105197Ssam# 594105197Ssam###################################################################### 595120585Ssam# Why not xmm registers? Short answer. It was actually tested and 596181803Sbz# was not any faster, but *contrary*, most notably on Intel CPUs. 597181803Sbz# Longer answer. Main advantage of using mm registers is that movd 598105197Ssam# latency is lower, especially on Intel P4. While arithmetic 599105197Ssam# instructions are twice as many, they can be scheduled every cycle 600105197Ssam# and not every second one when they are operating on xmm register, 601105197Ssam# so that "arithmetic throughput" remains virtually the same. And 602105197Ssam# finally the code can be executed even on elder SSE-only CPUs:-) 603105197Ssam 604105197Ssamsub sse_enccompact() 605105197Ssam{ 606120585Ssam &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0 607120585Ssam &pshufw ("mm5","mm4",0x0d); # 15,14,11,10 608105197Ssam &movd ("eax","mm1"); # 5, 4, 1, 0 609105197Ssam &movd ("ebx","mm5"); # 15,14,11,10 610181803Sbz 611181803Sbz &movz ($acc,&LB("eax")); # 0 612105197Ssam &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0 613105197Ssam &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2 614105197Ssam &movz ("edx",&HB("eax")); # 1 615105197Ssam &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1 616105197Ssam &shl ("edx",8); # 1 617105197Ssam &shr ("eax",16); # 5, 4 618105197Ssam 619105197Ssam &movz ($acc,&LB("ebx")); # 10 620105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 10 621105197Ssam &shl ($acc,16); # 10 622159215Sgnn &or ("ecx",$acc); # 10 623105197Ssam &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8 624105197Ssam &movz ($acc,&HB("ebx")); # 11 625105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 11 626105197Ssam &shl ($acc,24); # 11 627105197Ssam &or ("edx",$acc); # 11 628118888Ssam &shr ("ebx",16); # 15,14 629181803Sbz 630181803Sbz &movz ($acc,&HB("eax")); # 5 631181803Sbz &movz ($acc,&BP(-128,$tbl,$acc,1)); # 5 632118888Ssam &shl ($acc,8); # 5 633118888Ssam &or ("ecx",$acc); # 5 634118888Ssam &movz ($acc,&HB("ebx")); # 15 635105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 15 636105197Ssam &shl ($acc,24); # 15 637105197Ssam &or ("ecx",$acc); # 15 638105197Ssam &movd ("mm0","ecx"); # t[0] collected 639105197Ssam 640105197Ssam &movz ($acc,&LB("eax")); # 4 641105197Ssam &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 4 642105197Ssam &movd ("eax","mm2"); # 7, 6, 3, 2 643105197Ssam &movz ($acc,&LB("ebx")); # 14 644105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 14 645105197Ssam &shl ($acc,16); # 14 646105197Ssam &or ("ecx",$acc); # 14 647105197Ssam 648120585Ssam &movd ("ebx","mm6"); # 13,12, 9, 8 649120585Ssam &movz ($acc,&HB("eax")); # 3 650120585Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 3 651105197Ssam &shl ($acc,24); # 3 652105197Ssam &or ("ecx",$acc); # 3 653105197Ssam &movz ($acc,&HB("ebx")); # 9 654105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 9 655105197Ssam &shl ($acc,8); # 9 656181803Sbz &or ("ecx",$acc); # 9 657181803Sbz &movd ("mm1","ecx"); # t[1] collected 658105197Ssam 659105197Ssam &movz ($acc,&LB("ebx")); # 8 660105197Ssam &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 8 661105197Ssam &shr ("ebx",16); # 13,12 662105197Ssam &movz ($acc,&LB("eax")); # 2 663105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 2 664105197Ssam &shl ($acc,16); # 2 665105197Ssam &or ("ecx",$acc); # 2 666105197Ssam &shr ("eax",16); # 7, 6 667105197Ssam 668118888Ssam &punpckldq ("mm0","mm1"); # t[0,1] collected 669181803Sbz 670181803Sbz &movz ($acc,&HB("eax")); # 7 671181803Sbz &movz ($acc,&BP(-128,$tbl,$acc,1)); # 7 672118888Ssam &shl ($acc,24); # 7 673118888Ssam &or ("ecx",$acc); # 7 674118888Ssam &and ("eax",0xff); # 6 675105197Ssam &movz ("eax",&BP(-128,$tbl,"eax",1)); # 6 676105197Ssam &shl ("eax",16); # 6 677105197Ssam &or ("edx","eax"); # 6 678105197Ssam &movz ($acc,&HB("ebx")); # 13 679105197Ssam &movz ($acc,&BP(-128,$tbl,$acc,1)); # 13 680105197Ssam &shl ($acc,8); # 13 681105197Ssam &or ("ecx",$acc); # 13 682105197Ssam &movd ("mm4","ecx"); # t[2] collected 683105197Ssam &and ("ebx",0xff); # 12 684105197Ssam &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 12 685105197Ssam &or ("edx","ebx"); # 12 686105197Ssam &movd ("mm5","edx"); # t[3] collected 687105197Ssam 688105197Ssam &punpckldq ("mm4","mm5"); # t[2,3] collected 689105197Ssam} 690120585Ssam 691120585Ssam if (!$x86only) { 692120585Ssam&function_begin_B("_sse_AES_encrypt_compact"); 693165118Sbz &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0 694105197Ssam &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8 695105197Ssam 696105197Ssam # note that caller is expected to allocate stack frame for me! 697105197Ssam &mov ($acc,&DWP(240,$key)); # load key->rounds 698181803Sbz &lea ($acc,&DWP(-2,$acc,$acc)); 699181803Sbz &lea ($acc,&DWP(0,$key,$acc,8)); 700105197Ssam &mov ($__end,$acc); # end of key schedule 701105197Ssam 702105197Ssam &mov ($s0,0x1b1b1b1b); # magic constant 703159215Sgnn &mov (&DWP(8,"esp"),$s0); 704105197Ssam &mov (&DWP(12,"esp"),$s0); 705105197Ssam 706105197Ssam # prefetch Te4 707105197Ssam &mov ($s0,&DWP(0-128,$tbl)); 708105197Ssam &mov ($s1,&DWP(32-128,$tbl)); 709105197Ssam &mov ($s2,&DWP(64-128,$tbl)); 710105197Ssam &mov ($s3,&DWP(96-128,$tbl)); 711105197Ssam &mov ($s0,&DWP(128-128,$tbl)); 712105197Ssam &mov ($s1,&DWP(160-128,$tbl)); 713105197Ssam &mov ($s2,&DWP(192-128,$tbl)); 714105197Ssam &mov ($s3,&DWP(224-128,$tbl)); 715105197Ssam 716105197Ssam &set_label("loop",16); 717105197Ssam &sse_enccompact(); 718120585Ssam &add ($key,16); 719181803Sbz &cmp ($key,$__end); 720181803Sbz &ja (&label("out")); 721105197Ssam 722105197Ssam &movq ("mm2",&QWP(8,"esp")); 723105197Ssam &pxor ("mm3","mm3"); &pxor ("mm7","mm7"); 724105197Ssam &movq ("mm1","mm0"); &movq ("mm5","mm4"); # r0 725105197Ssam &pcmpgtb("mm3","mm0"); &pcmpgtb("mm7","mm4"); 726105197Ssam &pand ("mm3","mm2"); &pand ("mm7","mm2"); 727105197Ssam &pshufw ("mm2","mm0",0xb1); &pshufw ("mm6","mm4",0xb1);# ROTATE(r0,16) 728105197Ssam &paddb ("mm0","mm0"); &paddb ("mm4","mm4"); 729174054Sbz &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # = r2 730174054Sbz &pshufw ("mm3","mm2",0xb1); &pshufw ("mm7","mm6",0xb1);# r0 731174054Sbz &pxor ("mm1","mm0"); &pxor ("mm5","mm4"); # r0^r2 732105197Ssam &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(r0,16) 733105197Ssam 734105197Ssam &movq ("mm2","mm3"); &movq ("mm6","mm7"); 735120585Ssam &pslld ("mm3",8); &pslld ("mm7",8); 736120585Ssam &psrld ("mm2",24); &psrld ("mm6",24); 737105197Ssam &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= r0<<8 738105197Ssam &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= r0>>24 739105197Ssam 740105197Ssam &movq ("mm3","mm1"); &movq ("mm7","mm5"); 741105197Ssam &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key)); 742174054Sbz &psrld ("mm1",8); &psrld ("mm5",8); 743181627Svanhu &mov ($s0,&DWP(0-128,$tbl)); 744181627Svanhu &pslld ("mm3",24); &pslld ("mm7",24); 745181627Svanhu &mov ($s1,&DWP(64-128,$tbl)); 746174054Sbz &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= (r2^r0)<<8 747174054Sbz &mov ($s2,&DWP(128-128,$tbl)); 748174054Sbz &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= (r2^r0)>>24 749174054Sbz &mov ($s3,&DWP(192-128,$tbl)); 750174054Sbz 751174054Sbz &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); 752174054Sbz &jmp (&label("loop")); 753174054Sbz 754174054Sbz &set_label("out",16); 755174054Sbz &pxor ("mm0",&QWP(0,$key)); 756174054Sbz &pxor ("mm4",&QWP(8,$key)); 757174054Sbz 758105197Ssam &ret (); 759105197Ssam&function_end_B("_sse_AES_encrypt_compact"); 760105197Ssam } 761105197Ssam 762105197Ssam###################################################################### 763105197Ssam# Vanilla block function. 764105197Ssam###################################################################### 765105197Ssam 766105197Ssamsub encstep() 767105197Ssam{ my ($i,$te,@s) = @_; 768181803Sbz my $tmp = $key; 769181803Sbz my $out = $i==3?$s[0]:$acc; 770105197Ssam 771105197Ssam # lines marked with #%e?x[i] denote "reordered" instructions... 772105197Ssam if ($i==3) { &mov ($key,$__key); }##%edx 773105197Ssam else { &mov ($out,$s[0]); 774105197Ssam &and ($out,0xFF); } 775105197Ssam if ($i==1) { &shr ($s[0],16); }#%ebx[1] 776105197Ssam if ($i==2) { &shr ($s[0],24); }#%ecx[2] 777105197Ssam &mov ($out,&DWP(0,$te,$out,8)); 778105197Ssam 779181803Sbz if ($i==3) { $tmp=$s[1]; }##%eax 780105197Ssam &movz ($tmp,&HB($s[1])); 781105197Ssam &xor ($out,&DWP(3,$te,$tmp,8)); 782105197Ssam 783105197Ssam if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx 784105197Ssam else { &mov ($tmp,$s[2]); 785105197Ssam &shr ($tmp,16); } 786105197Ssam if ($i==2) { &and ($s[1],0xFF); }#%edx[2] 787105197Ssam &and ($tmp,0xFF); 788105197Ssam &xor ($out,&DWP(2,$te,$tmp,8)); 789105197Ssam 790105197Ssam if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx 791105197Ssam elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2] 792105197Ssam else { &mov ($tmp,$s[3]); 793105197Ssam &shr ($tmp,24) } 794105197Ssam &xor ($out,&DWP(1,$te,$tmp,8)); 795105197Ssam if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 796105197Ssam if ($i==3) { &mov ($s[3],$acc); } 797105197Ssam &comment(); 798105197Ssam} 799105197Ssam 800105197Ssamsub enclast() 801105197Ssam{ my ($i,$te,@s)=@_; 802120585Ssam my $tmp = $key; 803120585Ssam my $out = $i==3?$s[0]:$acc; 804120585Ssam 805120585Ssam if ($i==3) { &mov ($key,$__key); }##%edx 806172149Sgnn else { &mov ($out,$s[0]); } 807172149Sgnn &and ($out,0xFF); 808172149Sgnn if ($i==1) { &shr ($s[0],16); }#%ebx[1] 809172149Sgnn if ($i==2) { &shr ($s[0],24); }#%ecx[2] 810172149Sgnn &mov ($out,&DWP(2,$te,$out,8)); 811120585Ssam &and ($out,0x000000ff); 812120585Ssam 813120585Ssam if ($i==3) { $tmp=$s[1]; }##%eax 814120585Ssam &movz ($tmp,&HB($s[1])); 815120585Ssam &mov ($tmp,&DWP(0,$te,$tmp,8)); 816120585Ssam &and ($tmp,0x0000ff00); 817120585Ssam &xor ($out,$tmp); 818172149Sgnn 819172149Sgnn if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx 820172149Sgnn else { &mov ($tmp,$s[2]); 821172149Sgnn &shr ($tmp,16); } 822172149Sgnn if ($i==2) { &and ($s[1],0xFF); }#%edx[2] 823172149Sgnn &and ($tmp,0xFF); 824172149Sgnn &mov ($tmp,&DWP(0,$te,$tmp,8)); 825172149Sgnn &and ($tmp,0x00ff0000); 826172149Sgnn &xor ($out,$tmp); 827172149Sgnn 828120585Ssam if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx 829172149Sgnn elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2] 830172149Sgnn else { &mov ($tmp,$s[3]); 831120585Ssam &shr ($tmp,24); } 832120585Ssam &mov ($tmp,&DWP(2,$te,$tmp,8)); 833120585Ssam &and ($tmp,0xff000000); 834120585Ssam &xor ($out,$tmp); 835120585Ssam if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 836120585Ssam if ($i==3) { &mov ($s[3],$acc); } 837120585Ssam} 838120585Ssam 839120585Ssam&function_begin_B("_x86_AES_encrypt"); 840120585Ssam if ($vertical_spin) { 841120585Ssam # I need high parts of volatile registers to be accessible... 842120585Ssam &exch ($s1="edi",$key="ebx"); 843120585Ssam &mov ($s2="esi",$acc="ecx"); 844120585Ssam } 845120585Ssam 846120585Ssam # note that caller is expected to allocate stack frame for me! 847120585Ssam &mov ($__key,$key); # save key 848120585Ssam 849120585Ssam &xor ($s0,&DWP(0,$key)); # xor with key 850120585Ssam &xor ($s1,&DWP(4,$key)); 851120585Ssam &xor ($s2,&DWP(8,$key)); 852120585Ssam &xor ($s3,&DWP(12,$key)); 853120585Ssam 854120585Ssam &mov ($acc,&DWP(240,$key)); # load key->rounds 855120585Ssam 856120585Ssam if ($small_footprint) { 857120585Ssam &lea ($acc,&DWP(-2,$acc,$acc)); 858120585Ssam &lea ($acc,&DWP(0,$key,$acc,8)); 859120585Ssam &mov ($__end,$acc); # end of key schedule 860120585Ssam 861120585Ssam &set_label("loop",16); 862120585Ssam if ($vertical_spin) { 863120585Ssam &encvert($tbl,$s0,$s1,$s2,$s3); 864120585Ssam } else { 865120585Ssam &encstep(0,$tbl,$s0,$s1,$s2,$s3); 866120585Ssam &encstep(1,$tbl,$s1,$s2,$s3,$s0); 867120585Ssam &encstep(2,$tbl,$s2,$s3,$s0,$s1); 868120585Ssam &encstep(3,$tbl,$s3,$s0,$s1,$s2); 869120585Ssam } 870120585Ssam &add ($key,16); # advance rd_key 871120585Ssam &xor ($s0,&DWP(0,$key)); 872120585Ssam &xor ($s1,&DWP(4,$key)); 873120585Ssam &xor ($s2,&DWP(8,$key)); 874120585Ssam &xor ($s3,&DWP(12,$key)); 875120585Ssam &cmp ($key,$__end); 876120585Ssam &mov ($__key,$key); 877120585Ssam &jb (&label("loop")); 878120585Ssam } 879120585Ssam else { 880120585Ssam &cmp ($acc,10); 881120585Ssam &jle (&label("10rounds")); 882120585Ssam &cmp ($acc,12); 883120585Ssam &jle (&label("12rounds")); 884120585Ssam 885120585Ssam &set_label("14rounds",4); 886120585Ssam for ($i=1;$i<3;$i++) { 887120585Ssam if ($vertical_spin) { 888120585Ssam &encvert($tbl,$s0,$s1,$s2,$s3); 889120585Ssam } else { 890120585Ssam &encstep(0,$tbl,$s0,$s1,$s2,$s3); 891105197Ssam &encstep(1,$tbl,$s1,$s2,$s3,$s0); 892 &encstep(2,$tbl,$s2,$s3,$s0,$s1); 893 &encstep(3,$tbl,$s3,$s0,$s1,$s2); 894 } 895 &xor ($s0,&DWP(16*$i+0,$key)); 896 &xor ($s1,&DWP(16*$i+4,$key)); 897 &xor ($s2,&DWP(16*$i+8,$key)); 898 &xor ($s3,&DWP(16*$i+12,$key)); 899 } 900 &add ($key,32); 901 &mov ($__key,$key); # advance rd_key 902 &set_label("12rounds",4); 903 for ($i=1;$i<3;$i++) { 904 if ($vertical_spin) { 905 &encvert($tbl,$s0,$s1,$s2,$s3); 906 } else { 907 &encstep(0,$tbl,$s0,$s1,$s2,$s3); 908 &encstep(1,$tbl,$s1,$s2,$s3,$s0); 909 &encstep(2,$tbl,$s2,$s3,$s0,$s1); 910 &encstep(3,$tbl,$s3,$s0,$s1,$s2); 911 } 912 &xor ($s0,&DWP(16*$i+0,$key)); 913 &xor ($s1,&DWP(16*$i+4,$key)); 914 &xor ($s2,&DWP(16*$i+8,$key)); 915 &xor ($s3,&DWP(16*$i+12,$key)); 916 } 917 &add ($key,32); 918 &mov ($__key,$key); # advance rd_key 919 &set_label("10rounds",4); 920 for ($i=1;$i<10;$i++) { 921 if ($vertical_spin) { 922 &encvert($tbl,$s0,$s1,$s2,$s3); 923 } else { 924 &encstep(0,$tbl,$s0,$s1,$s2,$s3); 925 &encstep(1,$tbl,$s1,$s2,$s3,$s0); 926 &encstep(2,$tbl,$s2,$s3,$s0,$s1); 927 &encstep(3,$tbl,$s3,$s0,$s1,$s2); 928 } 929 &xor ($s0,&DWP(16*$i+0,$key)); 930 &xor ($s1,&DWP(16*$i+4,$key)); 931 &xor ($s2,&DWP(16*$i+8,$key)); 932 &xor ($s3,&DWP(16*$i+12,$key)); 933 } 934 } 935 936 if ($vertical_spin) { 937 # "reincarnate" some registers for "horizontal" spin... 938 &mov ($s1="ebx",$key="edi"); 939 &mov ($s2="ecx",$acc="esi"); 940 } 941 &enclast(0,$tbl,$s0,$s1,$s2,$s3); 942 &enclast(1,$tbl,$s1,$s2,$s3,$s0); 943 &enclast(2,$tbl,$s2,$s3,$s0,$s1); 944 &enclast(3,$tbl,$s3,$s0,$s1,$s2); 945 946 &add ($key,$small_footprint?16:160); 947 &xor ($s0,&DWP(0,$key)); 948 &xor ($s1,&DWP(4,$key)); 949 &xor ($s2,&DWP(8,$key)); 950 &xor ($s3,&DWP(12,$key)); 951 952 &ret (); 953 954&set_label("AES_Te",64); # Yes! I keep it in the code segment! 955 &_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6); 956 &_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591); 957 &_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56); 958 &_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec); 959 &_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa); 960 &_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb); 961 &_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45); 962 &_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b); 963 &_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c); 964 &_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83); 965 &_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9); 966 &_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a); 967 &_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d); 968 &_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f); 969 &_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df); 970 &_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea); 971 &_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34); 972 &_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b); 973 &_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d); 974 &_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413); 975 &_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1); 976 &_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6); 977 &_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972); 978 &_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85); 979 &_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed); 980 &_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511); 981 &_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe); 982 &_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b); 983 &_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05); 984 &_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1); 985 &_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142); 986 &_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf); 987 &_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3); 988 &_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e); 989 &_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a); 990 &_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6); 991 &_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3); 992 &_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b); 993 &_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428); 994 &_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad); 995 &_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14); 996 &_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8); 997 &_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4); 998 &_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2); 999 &_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda); 1000 &_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949); 1001 &_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf); 1002 &_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810); 1003 &_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c); 1004 &_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697); 1005 &_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e); 1006 &_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f); 1007 &_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc); 1008 &_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c); 1009 &_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969); 1010 &_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27); 1011 &_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122); 1012 &_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433); 1013 &_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9); 1014 &_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5); 1015 &_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a); 1016 &_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0); 1017 &_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e); 1018 &_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c); 1019 1020#Te4 # four copies of Te4 to choose from to avoid L1 aliasing 1021 &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1022 &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1023 &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1024 &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1025 &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1026 &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1027 &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1028 &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1029 &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1030 &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1031 &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1032 &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1033 &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1034 &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1035 &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1036 &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1037 &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1038 &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1039 &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1040 &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1041 &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1042 &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1043 &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1044 &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1045 &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1046 &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1047 &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1048 &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1049 &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1050 &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1051 &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1052 &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1053 1054 &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1055 &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1056 &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1057 &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1058 &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1059 &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1060 &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1061 &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1062 &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1063 &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1064 &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1065 &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1066 &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1067 &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1068 &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1069 &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1070 &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1071 &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1072 &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1073 &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1074 &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1075 &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1076 &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1077 &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1078 &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1079 &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1080 &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1081 &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1082 &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1083 &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1084 &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1085 &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1086 1087 &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1088 &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1089 &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1090 &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1091 &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1092 &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1093 &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1094 &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1095 &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1096 &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1097 &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1098 &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1099 &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1100 &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1101 &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1102 &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1103 &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1104 &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1105 &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1106 &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1107 &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1108 &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1109 &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1110 &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1111 &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1112 &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1113 &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1114 &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1115 &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1116 &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1117 &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1118 &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1119 1120 &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1121 &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1122 &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1123 &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1124 &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1125 &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1126 &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1127 &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1128 &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1129 &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1130 &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1131 &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1132 &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1133 &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1134 &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1135 &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1136 &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1137 &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1138 &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1139 &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1140 &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1141 &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1142 &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1143 &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1144 &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1145 &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1146 &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1147 &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1148 &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1149 &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1150 &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1151 &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1152#rcon: 1153 &data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008); 1154 &data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080); 1155 &data_word(0x0000001b, 0x00000036, 0x00000000, 0x00000000); 1156 &data_word(0x00000000, 0x00000000, 0x00000000, 0x00000000); 1157&function_end_B("_x86_AES_encrypt"); 1158 1159# void AES_encrypt (const void *inp,void *out,const AES_KEY *key); 1160&function_begin("AES_encrypt"); 1161 &mov ($acc,&wparam(0)); # load inp 1162 &mov ($key,&wparam(2)); # load key 1163 1164 &mov ($s0,"esp"); 1165 &sub ("esp",36); 1166 &and ("esp",-64); # align to cache-line 1167 1168 # place stack frame just "above" the key schedule 1169 &lea ($s1,&DWP(-64-63,$key)); 1170 &sub ($s1,"esp"); 1171 &neg ($s1); 1172 &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line 1173 &sub ("esp",$s1); 1174 &add ("esp",4); # 4 is reserved for caller's return address 1175 &mov ($_esp,$s0); # save stack pointer 1176 1177 &call (&label("pic_point")); # make it PIC! 1178 &set_label("pic_point"); 1179 &blindpop($tbl); 1180 &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if (!$x86only); 1181 &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl)); 1182 1183 # pick Te4 copy which can't "overlap" with stack frame or key schedule 1184 &lea ($s1,&DWP(768-4,"esp")); 1185 &sub ($s1,$tbl); 1186 &and ($s1,0x300); 1187 &lea ($tbl,&DWP(2048+128,$tbl,$s1)); 1188 1189 if (!$x86only) { 1190 &bt (&DWP(0,$s0),25); # check for SSE bit 1191 &jnc (&label("x86")); 1192 1193 &movq ("mm0",&QWP(0,$acc)); 1194 &movq ("mm4",&QWP(8,$acc)); 1195 &call ("_sse_AES_encrypt_compact"); 1196 &mov ("esp",$_esp); # restore stack pointer 1197 &mov ($acc,&wparam(1)); # load out 1198 &movq (&QWP(0,$acc),"mm0"); # write output data 1199 &movq (&QWP(8,$acc),"mm4"); 1200 &emms (); 1201 &function_end_A(); 1202 } 1203 &set_label("x86",16); 1204 &mov ($_tbl,$tbl); 1205 &mov ($s0,&DWP(0,$acc)); # load input data 1206 &mov ($s1,&DWP(4,$acc)); 1207 &mov ($s2,&DWP(8,$acc)); 1208 &mov ($s3,&DWP(12,$acc)); 1209 &call ("_x86_AES_encrypt_compact"); 1210 &mov ("esp",$_esp); # restore stack pointer 1211 &mov ($acc,&wparam(1)); # load out 1212 &mov (&DWP(0,$acc),$s0); # write output data 1213 &mov (&DWP(4,$acc),$s1); 1214 &mov (&DWP(8,$acc),$s2); 1215 &mov (&DWP(12,$acc),$s3); 1216&function_end("AES_encrypt"); 1217 1218#--------------------------------------------------------------------# 1219 1220###################################################################### 1221# "Compact" block function 1222###################################################################### 1223 1224sub deccompact() 1225{ my $Fn = mov; 1226 while ($#_>5) { pop(@_); $Fn=sub{}; } 1227 my ($i,$td,@s)=@_; 1228 my $tmp = $key; 1229 my $out = $i==3?$s[0]:$acc; 1230 1231 # $Fn is used in first compact round and its purpose is to 1232 # void restoration of some values from stack, so that after 1233 # 4xdeccompact with extra argument $key, $s0 and $s1 values 1234 # are left there... 1235 if($i==3) { &$Fn ($key,$__key); } 1236 else { &mov ($out,$s[0]); } 1237 &and ($out,0xFF); 1238 &movz ($out,&BP(-128,$td,$out,1)); 1239 1240 if ($i==3) { $tmp=$s[1]; } 1241 &movz ($tmp,&HB($s[1])); 1242 &movz ($tmp,&BP(-128,$td,$tmp,1)); 1243 &shl ($tmp,8); 1244 &xor ($out,$tmp); 1245 1246 if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); } 1247 else { mov ($tmp,$s[2]); } 1248 &shr ($tmp,16); 1249 &and ($tmp,0xFF); 1250 &movz ($tmp,&BP(-128,$td,$tmp,1)); 1251 &shl ($tmp,16); 1252 &xor ($out,$tmp); 1253 1254 if ($i==3) { $tmp=$s[3]; &$Fn ($s[2],$__s1); } 1255 else { &mov ($tmp,$s[3]); } 1256 &shr ($tmp,24); 1257 &movz ($tmp,&BP(-128,$td,$tmp,1)); 1258 &shl ($tmp,24); 1259 &xor ($out,$tmp); 1260 if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 1261 if ($i==3) { &$Fn ($s[3],$__s0); } 1262} 1263 1264# must be called with 2,3,0,1 as argument sequence!!! 1265sub dectransform() 1266{ my @s = ($s0,$s1,$s2,$s3); 1267 my $i = shift; 1268 my $tmp = $key; 1269 my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1); 1270 my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1); 1271 my $tp8 = $tbl; 1272 1273 &mov ($acc,$s[$i]); 1274 &and ($acc,0x80808080); 1275 &mov ($tmp,$acc); 1276 &shr ($tmp,7); 1277 &lea ($tp2,&DWP(0,$s[$i],$s[$i])); 1278 &sub ($acc,$tmp); 1279 &and ($tp2,0xfefefefe); 1280 &and ($acc,0x1b1b1b1b); 1281 &xor ($acc,$tp2); 1282 &mov ($tp2,$acc); 1283 1284 &and ($acc,0x80808080); 1285 &mov ($tmp,$acc); 1286 &shr ($tmp,7); 1287 &lea ($tp4,&DWP(0,$tp2,$tp2)); 1288 &sub ($acc,$tmp); 1289 &and ($tp4,0xfefefefe); 1290 &and ($acc,0x1b1b1b1b); 1291 &xor ($tp2,$s[$i]); # tp2^tp1 1292 &xor ($acc,$tp4); 1293 &mov ($tp4,$acc); 1294 1295 &and ($acc,0x80808080); 1296 &mov ($tmp,$acc); 1297 &shr ($tmp,7); 1298 &lea ($tp8,&DWP(0,$tp4,$tp4)); 1299 &sub ($acc,$tmp); 1300 &and ($tp8,0xfefefefe); 1301 &and ($acc,0x1b1b1b1b); 1302 &xor ($tp4,$s[$i]); # tp4^tp1 1303 &rotl ($s[$i],8); # = ROTATE(tp1,8) 1304 &xor ($tp8,$acc); 1305 1306 &xor ($s[$i],$tp2); 1307 &xor ($tp2,$tp8); 1308 &rotl ($tp2,24); 1309 &xor ($s[$i],$tp4); 1310 &xor ($tp4,$tp8); 1311 &rotl ($tp4,16); 1312 &xor ($s[$i],$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1) 1313 &rotl ($tp8,8); 1314 &xor ($s[$i],$tp2); # ^= ROTATE(tp8^tp2^tp1,24) 1315 &xor ($s[$i],$tp4); # ^= ROTATE(tp8^tp4^tp1,16) 1316 &mov ($s[0],$__s0) if($i==2); #prefetch $s0 1317 &mov ($s[1],$__s1) if($i==3); #prefetch $s1 1318 &mov ($s[2],$__s2) if($i==1); 1319 &xor ($s[$i],$tp8); # ^= ROTATE(tp8,8) 1320 1321 &mov ($s[3],$__s3) if($i==1); 1322 &mov (&DWP(4+4*$i,"esp"),$s[$i]) if($i>=2); 1323} 1324 1325&function_begin_B("_x86_AES_decrypt_compact"); 1326 # note that caller is expected to allocate stack frame for me! 1327 &mov ($__key,$key); # save key 1328 1329 &xor ($s0,&DWP(0,$key)); # xor with key 1330 &xor ($s1,&DWP(4,$key)); 1331 &xor ($s2,&DWP(8,$key)); 1332 &xor ($s3,&DWP(12,$key)); 1333 1334 &mov ($acc,&DWP(240,$key)); # load key->rounds 1335 1336 &lea ($acc,&DWP(-2,$acc,$acc)); 1337 &lea ($acc,&DWP(0,$key,$acc,8)); 1338 &mov ($__end,$acc); # end of key schedule 1339 1340 # prefetch Td4 1341 &mov ($key,&DWP(0-128,$tbl)); 1342 &mov ($acc,&DWP(32-128,$tbl)); 1343 &mov ($key,&DWP(64-128,$tbl)); 1344 &mov ($acc,&DWP(96-128,$tbl)); 1345 &mov ($key,&DWP(128-128,$tbl)); 1346 &mov ($acc,&DWP(160-128,$tbl)); 1347 &mov ($key,&DWP(192-128,$tbl)); 1348 &mov ($acc,&DWP(224-128,$tbl)); 1349 1350 &set_label("loop",16); 1351 1352 &deccompact(0,$tbl,$s0,$s3,$s2,$s1,1); 1353 &deccompact(1,$tbl,$s1,$s0,$s3,$s2,1); 1354 &deccompact(2,$tbl,$s2,$s1,$s0,$s3,1); 1355 &deccompact(3,$tbl,$s3,$s2,$s1,$s0,1); 1356 &dectransform(2); 1357 &dectransform(3); 1358 &dectransform(0); 1359 &dectransform(1); 1360 &mov ($key,$__key); 1361 &mov ($tbl,$__tbl); 1362 &add ($key,16); # advance rd_key 1363 &xor ($s0,&DWP(0,$key)); 1364 &xor ($s1,&DWP(4,$key)); 1365 &xor ($s2,&DWP(8,$key)); 1366 &xor ($s3,&DWP(12,$key)); 1367 1368 &cmp ($key,$__end); 1369 &mov ($__key,$key); 1370 &jb (&label("loop")); 1371 1372 &deccompact(0,$tbl,$s0,$s3,$s2,$s1); 1373 &deccompact(1,$tbl,$s1,$s0,$s3,$s2); 1374 &deccompact(2,$tbl,$s2,$s1,$s0,$s3); 1375 &deccompact(3,$tbl,$s3,$s2,$s1,$s0); 1376 1377 &xor ($s0,&DWP(16,$key)); 1378 &xor ($s1,&DWP(20,$key)); 1379 &xor ($s2,&DWP(24,$key)); 1380 &xor ($s3,&DWP(28,$key)); 1381 1382 &ret (); 1383&function_end_B("_x86_AES_decrypt_compact"); 1384 1385###################################################################### 1386# "Compact" SSE block function. 1387###################################################################### 1388 1389sub sse_deccompact() 1390{ 1391 &pshufw ("mm1","mm0",0x0c); # 7, 6, 1, 0 1392 &movd ("eax","mm1"); # 7, 6, 1, 0 1393 1394 &pshufw ("mm5","mm4",0x09); # 13,12,11,10 1395 &movz ($acc,&LB("eax")); # 0 1396 &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0 1397 &movd ("ebx","mm5"); # 13,12,11,10 1398 &movz ("edx",&HB("eax")); # 1 1399 &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1 1400 &shl ("edx",8); # 1 1401 1402 &pshufw ("mm2","mm0",0x06); # 3, 2, 5, 4 1403 &movz ($acc,&LB("ebx")); # 10 1404 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 10 1405 &shl ($acc,16); # 10 1406 &or ("ecx",$acc); # 10 1407 &shr ("eax",16); # 7, 6 1408 &movz ($acc,&HB("ebx")); # 11 1409 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 11 1410 &shl ($acc,24); # 11 1411 &or ("edx",$acc); # 11 1412 &shr ("ebx",16); # 13,12 1413 1414 &pshufw ("mm6","mm4",0x03); # 9, 8,15,14 1415 &movz ($acc,&HB("eax")); # 7 1416 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 7 1417 &shl ($acc,24); # 7 1418 &or ("ecx",$acc); # 7 1419 &movz ($acc,&HB("ebx")); # 13 1420 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 13 1421 &shl ($acc,8); # 13 1422 &or ("ecx",$acc); # 13 1423 &movd ("mm0","ecx"); # t[0] collected 1424 1425 &movz ($acc,&LB("eax")); # 6 1426 &movd ("eax","mm2"); # 3, 2, 5, 4 1427 &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 6 1428 &shl ("ecx",16); # 6 1429 &movz ($acc,&LB("ebx")); # 12 1430 &movd ("ebx","mm6"); # 9, 8,15,14 1431 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 12 1432 &or ("ecx",$acc); # 12 1433 1434 &movz ($acc,&LB("eax")); # 4 1435 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 4 1436 &or ("edx",$acc); # 4 1437 &movz ($acc,&LB("ebx")); # 14 1438 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 14 1439 &shl ($acc,16); # 14 1440 &or ("edx",$acc); # 14 1441 &movd ("mm1","edx"); # t[1] collected 1442 1443 &movz ($acc,&HB("eax")); # 5 1444 &movz ("edx",&BP(-128,$tbl,$acc,1)); # 5 1445 &shl ("edx",8); # 5 1446 &movz ($acc,&HB("ebx")); # 15 1447 &shr ("eax",16); # 3, 2 1448 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 15 1449 &shl ($acc,24); # 15 1450 &or ("edx",$acc); # 15 1451 &shr ("ebx",16); # 9, 8 1452 1453 &punpckldq ("mm0","mm1"); # t[0,1] collected 1454 1455 &movz ($acc,&HB("ebx")); # 9 1456 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 9 1457 &shl ($acc,8); # 9 1458 &or ("ecx",$acc); # 9 1459 &and ("ebx",0xff); # 8 1460 &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 8 1461 &or ("edx","ebx"); # 8 1462 &movz ($acc,&LB("eax")); # 2 1463 &movz ($acc,&BP(-128,$tbl,$acc,1)); # 2 1464 &shl ($acc,16); # 2 1465 &or ("edx",$acc); # 2 1466 &movd ("mm4","edx"); # t[2] collected 1467 &movz ("eax",&HB("eax")); # 3 1468 &movz ("eax",&BP(-128,$tbl,"eax",1)); # 3 1469 &shl ("eax",24); # 3 1470 &or ("ecx","eax"); # 3 1471 &movd ("mm5","ecx"); # t[3] collected 1472 1473 &punpckldq ("mm4","mm5"); # t[2,3] collected 1474} 1475 1476 if (!$x86only) { 1477&function_begin_B("_sse_AES_decrypt_compact"); 1478 &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0 1479 &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8 1480 1481 # note that caller is expected to allocate stack frame for me! 1482 &mov ($acc,&DWP(240,$key)); # load key->rounds 1483 &lea ($acc,&DWP(-2,$acc,$acc)); 1484 &lea ($acc,&DWP(0,$key,$acc,8)); 1485 &mov ($__end,$acc); # end of key schedule 1486 1487 &mov ($s0,0x1b1b1b1b); # magic constant 1488 &mov (&DWP(8,"esp"),$s0); 1489 &mov (&DWP(12,"esp"),$s0); 1490 1491 # prefetch Td4 1492 &mov ($s0,&DWP(0-128,$tbl)); 1493 &mov ($s1,&DWP(32-128,$tbl)); 1494 &mov ($s2,&DWP(64-128,$tbl)); 1495 &mov ($s3,&DWP(96-128,$tbl)); 1496 &mov ($s0,&DWP(128-128,$tbl)); 1497 &mov ($s1,&DWP(160-128,$tbl)); 1498 &mov ($s2,&DWP(192-128,$tbl)); 1499 &mov ($s3,&DWP(224-128,$tbl)); 1500 1501 &set_label("loop",16); 1502 &sse_deccompact(); 1503 &add ($key,16); 1504 &cmp ($key,$__end); 1505 &ja (&label("out")); 1506 1507 # ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N) 1508 &movq ("mm3","mm0"); &movq ("mm7","mm4"); 1509 &movq ("mm2","mm0",1); &movq ("mm6","mm4",1); 1510 &movq ("mm1","mm0"); &movq ("mm5","mm4"); 1511 &pshufw ("mm0","mm0",0xb1); &pshufw ("mm4","mm4",0xb1);# = ROTATE(tp0,16) 1512 &pslld ("mm2",8); &pslld ("mm6",8); 1513 &psrld ("mm3",8); &psrld ("mm7",8); 1514 &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<8 1515 &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>8 1516 &pslld ("mm2",16); &pslld ("mm6",16); 1517 &psrld ("mm3",16); &psrld ("mm7",16); 1518 &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<24 1519 &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>24 1520 1521 &movq ("mm3",&QWP(8,"esp")); 1522 &pxor ("mm2","mm2"); &pxor ("mm6","mm6"); 1523 &pcmpgtb("mm2","mm1"); &pcmpgtb("mm6","mm5"); 1524 &pand ("mm2","mm3"); &pand ("mm6","mm3"); 1525 &paddb ("mm1","mm1"); &paddb ("mm5","mm5"); 1526 &pxor ("mm1","mm2"); &pxor ("mm5","mm6"); # tp2 1527 &movq ("mm3","mm1"); &movq ("mm7","mm5"); 1528 &movq ("mm2","mm1"); &movq ("mm6","mm5"); 1529 &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp2 1530 &pslld ("mm3",24); &pslld ("mm7",24); 1531 &psrld ("mm2",8); &psrld ("mm6",8); 1532 &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp2<<24 1533 &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp2>>8 1534 1535 &movq ("mm2",&QWP(8,"esp")); 1536 &pxor ("mm3","mm3"); &pxor ("mm7","mm7"); 1537 &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5"); 1538 &pand ("mm3","mm2"); &pand ("mm7","mm2"); 1539 &paddb ("mm1","mm1"); &paddb ("mm5","mm5"); 1540 &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp4 1541 &pshufw ("mm3","mm1",0xb1); &pshufw ("mm7","mm5",0xb1); 1542 &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp4 1543 &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= ROTATE(tp4,16) 1544 1545 &pxor ("mm3","mm3"); &pxor ("mm7","mm7"); 1546 &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5"); 1547 &pand ("mm3","mm2"); &pand ("mm7","mm2"); 1548 &paddb ("mm1","mm1"); &paddb ("mm5","mm5"); 1549 &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp8 1550 &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8 1551 &movq ("mm3","mm1"); &movq ("mm7","mm5"); 1552 &pshufw ("mm2","mm1",0xb1); &pshufw ("mm6","mm5",0xb1); 1553 &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(tp8,16) 1554 &pslld ("mm1",8); &pslld ("mm5",8); 1555 &psrld ("mm3",8); &psrld ("mm7",8); 1556 &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key)); 1557 &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<8 1558 &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>8 1559 &mov ($s0,&DWP(0-128,$tbl)); 1560 &pslld ("mm1",16); &pslld ("mm5",16); 1561 &mov ($s1,&DWP(64-128,$tbl)); 1562 &psrld ("mm3",16); &psrld ("mm7",16); 1563 &mov ($s2,&DWP(128-128,$tbl)); 1564 &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<24 1565 &mov ($s3,&DWP(192-128,$tbl)); 1566 &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>24 1567 1568 &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); 1569 &jmp (&label("loop")); 1570 1571 &set_label("out",16); 1572 &pxor ("mm0",&QWP(0,$key)); 1573 &pxor ("mm4",&QWP(8,$key)); 1574 1575 &ret (); 1576&function_end_B("_sse_AES_decrypt_compact"); 1577 } 1578 1579###################################################################### 1580# Vanilla block function. 1581###################################################################### 1582 1583sub decstep() 1584{ my ($i,$td,@s) = @_; 1585 my $tmp = $key; 1586 my $out = $i==3?$s[0]:$acc; 1587 1588 # no instructions are reordered, as performance appears 1589 # optimal... or rather that all attempts to reorder didn't 1590 # result in better performance [which by the way is not a 1591 # bit lower than ecryption]. 1592 if($i==3) { &mov ($key,$__key); } 1593 else { &mov ($out,$s[0]); } 1594 &and ($out,0xFF); 1595 &mov ($out,&DWP(0,$td,$out,8)); 1596 1597 if ($i==3) { $tmp=$s[1]; } 1598 &movz ($tmp,&HB($s[1])); 1599 &xor ($out,&DWP(3,$td,$tmp,8)); 1600 1601 if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); } 1602 else { &mov ($tmp,$s[2]); } 1603 &shr ($tmp,16); 1604 &and ($tmp,0xFF); 1605 &xor ($out,&DWP(2,$td,$tmp,8)); 1606 1607 if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); } 1608 else { &mov ($tmp,$s[3]); } 1609 &shr ($tmp,24); 1610 &xor ($out,&DWP(1,$td,$tmp,8)); 1611 if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 1612 if ($i==3) { &mov ($s[3],$__s0); } 1613 &comment(); 1614} 1615 1616sub declast() 1617{ my ($i,$td,@s)=@_; 1618 my $tmp = $key; 1619 my $out = $i==3?$s[0]:$acc; 1620 1621 if($i==0) { &lea ($td,&DWP(2048+128,$td)); 1622 &mov ($tmp,&DWP(0-128,$td)); 1623 &mov ($acc,&DWP(32-128,$td)); 1624 &mov ($tmp,&DWP(64-128,$td)); 1625 &mov ($acc,&DWP(96-128,$td)); 1626 &mov ($tmp,&DWP(128-128,$td)); 1627 &mov ($acc,&DWP(160-128,$td)); 1628 &mov ($tmp,&DWP(192-128,$td)); 1629 &mov ($acc,&DWP(224-128,$td)); 1630 &lea ($td,&DWP(-128,$td)); } 1631 if($i==3) { &mov ($key,$__key); } 1632 else { &mov ($out,$s[0]); } 1633 &and ($out,0xFF); 1634 &movz ($out,&BP(0,$td,$out,1)); 1635 1636 if ($i==3) { $tmp=$s[1]; } 1637 &movz ($tmp,&HB($s[1])); 1638 &movz ($tmp,&BP(0,$td,$tmp,1)); 1639 &shl ($tmp,8); 1640 &xor ($out,$tmp); 1641 1642 if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); } 1643 else { mov ($tmp,$s[2]); } 1644 &shr ($tmp,16); 1645 &and ($tmp,0xFF); 1646 &movz ($tmp,&BP(0,$td,$tmp,1)); 1647 &shl ($tmp,16); 1648 &xor ($out,$tmp); 1649 1650 if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); } 1651 else { &mov ($tmp,$s[3]); } 1652 &shr ($tmp,24); 1653 &movz ($tmp,&BP(0,$td,$tmp,1)); 1654 &shl ($tmp,24); 1655 &xor ($out,$tmp); 1656 if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 1657 if ($i==3) { &mov ($s[3],$__s0); 1658 &lea ($td,&DWP(-2048,$td)); } 1659} 1660 1661&function_begin_B("_x86_AES_decrypt"); 1662 # note that caller is expected to allocate stack frame for me! 1663 &mov ($__key,$key); # save key 1664 1665 &xor ($s0,&DWP(0,$key)); # xor with key 1666 &xor ($s1,&DWP(4,$key)); 1667 &xor ($s2,&DWP(8,$key)); 1668 &xor ($s3,&DWP(12,$key)); 1669 1670 &mov ($acc,&DWP(240,$key)); # load key->rounds 1671 1672 if ($small_footprint) { 1673 &lea ($acc,&DWP(-2,$acc,$acc)); 1674 &lea ($acc,&DWP(0,$key,$acc,8)); 1675 &mov ($__end,$acc); # end of key schedule 1676 &set_label("loop",16); 1677 &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1678 &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1679 &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1680 &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1681 &add ($key,16); # advance rd_key 1682 &xor ($s0,&DWP(0,$key)); 1683 &xor ($s1,&DWP(4,$key)); 1684 &xor ($s2,&DWP(8,$key)); 1685 &xor ($s3,&DWP(12,$key)); 1686 &cmp ($key,$__end); 1687 &mov ($__key,$key); 1688 &jb (&label("loop")); 1689 } 1690 else { 1691 &cmp ($acc,10); 1692 &jle (&label("10rounds")); 1693 &cmp ($acc,12); 1694 &jle (&label("12rounds")); 1695 1696 &set_label("14rounds",4); 1697 for ($i=1;$i<3;$i++) { 1698 &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1699 &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1700 &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1701 &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1702 &xor ($s0,&DWP(16*$i+0,$key)); 1703 &xor ($s1,&DWP(16*$i+4,$key)); 1704 &xor ($s2,&DWP(16*$i+8,$key)); 1705 &xor ($s3,&DWP(16*$i+12,$key)); 1706 } 1707 &add ($key,32); 1708 &mov ($__key,$key); # advance rd_key 1709 &set_label("12rounds",4); 1710 for ($i=1;$i<3;$i++) { 1711 &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1712 &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1713 &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1714 &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1715 &xor ($s0,&DWP(16*$i+0,$key)); 1716 &xor ($s1,&DWP(16*$i+4,$key)); 1717 &xor ($s2,&DWP(16*$i+8,$key)); 1718 &xor ($s3,&DWP(16*$i+12,$key)); 1719 } 1720 &add ($key,32); 1721 &mov ($__key,$key); # advance rd_key 1722 &set_label("10rounds",4); 1723 for ($i=1;$i<10;$i++) { 1724 &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1725 &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1726 &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1727 &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1728 &xor ($s0,&DWP(16*$i+0,$key)); 1729 &xor ($s1,&DWP(16*$i+4,$key)); 1730 &xor ($s2,&DWP(16*$i+8,$key)); 1731 &xor ($s3,&DWP(16*$i+12,$key)); 1732 } 1733 } 1734 1735 &declast(0,$tbl,$s0,$s3,$s2,$s1); 1736 &declast(1,$tbl,$s1,$s0,$s3,$s2); 1737 &declast(2,$tbl,$s2,$s1,$s0,$s3); 1738 &declast(3,$tbl,$s3,$s2,$s1,$s0); 1739 1740 &add ($key,$small_footprint?16:160); 1741 &xor ($s0,&DWP(0,$key)); 1742 &xor ($s1,&DWP(4,$key)); 1743 &xor ($s2,&DWP(8,$key)); 1744 &xor ($s3,&DWP(12,$key)); 1745 1746 &ret (); 1747 1748&set_label("AES_Td",64); # Yes! I keep it in the code segment! 1749 &_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a); 1750 &_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b); 1751 &_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5); 1752 &_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5); 1753 &_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d); 1754 &_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b); 1755 &_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295); 1756 &_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e); 1757 &_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927); 1758 &_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d); 1759 &_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362); 1760 &_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9); 1761 &_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52); 1762 &_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566); 1763 &_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3); 1764 &_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed); 1765 &_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e); 1766 &_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4); 1767 &_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4); 1768 &_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd); 1769 &_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d); 1770 &_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060); 1771 &_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967); 1772 &_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879); 1773 &_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000); 1774 &_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c); 1775 &_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36); 1776 &_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624); 1777 &_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b); 1778 &_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c); 1779 &_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12); 1780 &_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14); 1781 &_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3); 1782 &_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b); 1783 &_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8); 1784 &_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684); 1785 &_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7); 1786 &_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177); 1787 &_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947); 1788 &_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322); 1789 &_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498); 1790 &_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f); 1791 &_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54); 1792 &_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382); 1793 &_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf); 1794 &_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb); 1795 &_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83); 1796 &_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef); 1797 &_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029); 1798 &_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235); 1799 &_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733); 1800 &_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117); 1801 &_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4); 1802 &_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546); 1803 &_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb); 1804 &_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d); 1805 &_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb); 1806 &_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a); 1807 &_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773); 1808 &_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478); 1809 &_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2); 1810 &_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff); 1811 &_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664); 1812 &_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0); 1813 1814#Td4: # four copies of Td4 to choose from to avoid L1 aliasing 1815 &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1816 &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1817 &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1818 &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1819 &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1820 &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1821 &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1822 &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1823 &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1824 &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1825 &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1826 &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1827 &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1828 &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1829 &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1830 &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1831 &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1832 &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1833 &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1834 &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1835 &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1836 &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1837 &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1838 &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1839 &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1840 &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1841 &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1842 &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1843 &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1844 &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1845 &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1846 &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1847 1848 &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1849 &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1850 &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1851 &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1852 &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1853 &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1854 &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1855 &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1856 &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1857 &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1858 &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1859 &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1860 &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1861 &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1862 &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1863 &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1864 &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1865 &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1866 &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1867 &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1868 &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1869 &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1870 &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1871 &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1872 &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1873 &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1874 &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1875 &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1876 &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1877 &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1878 &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1879 &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1880 1881 &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1882 &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1883 &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1884 &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1885 &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1886 &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1887 &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1888 &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1889 &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1890 &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1891 &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1892 &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1893 &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1894 &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1895 &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1896 &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1897 &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1898 &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1899 &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1900 &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1901 &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1902 &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1903 &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1904 &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1905 &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1906 &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1907 &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1908 &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1909 &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1910 &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1911 &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1912 &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1913 1914 &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1915 &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1916 &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1917 &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1918 &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1919 &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1920 &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1921 &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1922 &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1923 &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1924 &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1925 &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1926 &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1927 &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1928 &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1929 &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1930 &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1931 &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1932 &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1933 &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1934 &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1935 &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1936 &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1937 &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1938 &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1939 &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1940 &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1941 &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1942 &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1943 &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1944 &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1945 &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1946&function_end_B("_x86_AES_decrypt"); 1947 1948# void AES_decrypt (const void *inp,void *out,const AES_KEY *key); 1949&function_begin("AES_decrypt"); 1950 &mov ($acc,&wparam(0)); # load inp 1951 &mov ($key,&wparam(2)); # load key 1952 1953 &mov ($s0,"esp"); 1954 &sub ("esp",36); 1955 &and ("esp",-64); # align to cache-line 1956 1957 # place stack frame just "above" the key schedule 1958 &lea ($s1,&DWP(-64-63,$key)); 1959 &sub ($s1,"esp"); 1960 &neg ($s1); 1961 &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line 1962 &sub ("esp",$s1); 1963 &add ("esp",4); # 4 is reserved for caller's return address 1964 &mov ($_esp,$s0); # save stack pointer 1965 1966 &call (&label("pic_point")); # make it PIC! 1967 &set_label("pic_point"); 1968 &blindpop($tbl); 1969 &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only); 1970 &lea ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl)); 1971 1972 # pick Td4 copy which can't "overlap" with stack frame or key schedule 1973 &lea ($s1,&DWP(768-4,"esp")); 1974 &sub ($s1,$tbl); 1975 &and ($s1,0x300); 1976 &lea ($tbl,&DWP(2048+128,$tbl,$s1)); 1977 1978 if (!$x86only) { 1979 &bt (&DWP(0,$s0),25); # check for SSE bit 1980 &jnc (&label("x86")); 1981 1982 &movq ("mm0",&QWP(0,$acc)); 1983 &movq ("mm4",&QWP(8,$acc)); 1984 &call ("_sse_AES_decrypt_compact"); 1985 &mov ("esp",$_esp); # restore stack pointer 1986 &mov ($acc,&wparam(1)); # load out 1987 &movq (&QWP(0,$acc),"mm0"); # write output data 1988 &movq (&QWP(8,$acc),"mm4"); 1989 &emms (); 1990 &function_end_A(); 1991 } 1992 &set_label("x86",16); 1993 &mov ($_tbl,$tbl); 1994 &mov ($s0,&DWP(0,$acc)); # load input data 1995 &mov ($s1,&DWP(4,$acc)); 1996 &mov ($s2,&DWP(8,$acc)); 1997 &mov ($s3,&DWP(12,$acc)); 1998 &call ("_x86_AES_decrypt_compact"); 1999 &mov ("esp",$_esp); # restore stack pointer 2000 &mov ($acc,&wparam(1)); # load out 2001 &mov (&DWP(0,$acc),$s0); # write output data 2002 &mov (&DWP(4,$acc),$s1); 2003 &mov (&DWP(8,$acc),$s2); 2004 &mov (&DWP(12,$acc),$s3); 2005&function_end("AES_decrypt"); 2006 2007# void AES_cbc_encrypt (const void char *inp, unsigned char *out, 2008# size_t length, const AES_KEY *key, 2009# unsigned char *ivp,const int enc); 2010{ 2011# stack frame layout 2012# -4(%esp) # return address 0(%esp) 2013# 0(%esp) # s0 backing store 4(%esp) 2014# 4(%esp) # s1 backing store 8(%esp) 2015# 8(%esp) # s2 backing store 12(%esp) 2016# 12(%esp) # s3 backing store 16(%esp) 2017# 16(%esp) # key backup 20(%esp) 2018# 20(%esp) # end of key schedule 24(%esp) 2019# 24(%esp) # %ebp backup 28(%esp) 2020# 28(%esp) # %esp backup 2021my $_inp=&DWP(32,"esp"); # copy of wparam(0) 2022my $_out=&DWP(36,"esp"); # copy of wparam(1) 2023my $_len=&DWP(40,"esp"); # copy of wparam(2) 2024my $_key=&DWP(44,"esp"); # copy of wparam(3) 2025my $_ivp=&DWP(48,"esp"); # copy of wparam(4) 2026my $_tmp=&DWP(52,"esp"); # volatile variable 2027# 2028my $ivec=&DWP(60,"esp"); # ivec[16] 2029my $aes_key=&DWP(76,"esp"); # copy of aes_key 2030my $mark=&DWP(76+240,"esp"); # copy of aes_key->rounds 2031 2032&function_begin("AES_cbc_encrypt"); 2033 &mov ($s2 eq "ecx"? $s2 : "",&wparam(2)); # load len 2034 &cmp ($s2,0); 2035 &je (&label("drop_out")); 2036 2037 &call (&label("pic_point")); # make it PIC! 2038 &set_label("pic_point"); 2039 &blindpop($tbl); 2040 &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only); 2041 2042 &cmp (&wparam(5),0); 2043 &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl)); 2044 &jne (&label("picked_te")); 2045 &lea ($tbl,&DWP(&label("AES_Td")."-".&label("AES_Te"),$tbl)); 2046 &set_label("picked_te"); 2047 2048 # one can argue if this is required 2049 &pushf (); 2050 &cld (); 2051 2052 &cmp ($s2,$speed_limit); 2053 &jb (&label("slow_way")); 2054 &test ($s2,15); 2055 &jnz (&label("slow_way")); 2056 if (!$x86only) { 2057 &bt (&DWP(0,$s0),28); # check for hyper-threading bit 2058 &jc (&label("slow_way")); 2059 } 2060 # pre-allocate aligned stack frame... 2061 &lea ($acc,&DWP(-80-244,"esp")); 2062 &and ($acc,-64); 2063 2064 # ... and make sure it doesn't alias with $tbl modulo 4096 2065 &mov ($s0,$tbl); 2066 &lea ($s1,&DWP(2048+256,$tbl)); 2067 &mov ($s3,$acc); 2068 &and ($s0,0xfff); # s = %ebp&0xfff 2069 &and ($s1,0xfff); # e = (%ebp+2048+256)&0xfff 2070 &and ($s3,0xfff); # p = %esp&0xfff 2071 2072 &cmp ($s3,$s1); # if (p>=e) %esp =- (p-e); 2073 &jb (&label("tbl_break_out")); 2074 &sub ($s3,$s1); 2075 &sub ($acc,$s3); 2076 &jmp (&label("tbl_ok")); 2077 &set_label("tbl_break_out",4); # else %esp -= (p-s)&0xfff + framesz; 2078 &sub ($s3,$s0); 2079 &and ($s3,0xfff); 2080 &add ($s3,384); 2081 &sub ($acc,$s3); 2082 &set_label("tbl_ok",4); 2083 2084 &lea ($s3,&wparam(0)); # obtain pointer to parameter block 2085 &exch ("esp",$acc); # allocate stack frame 2086 &add ("esp",4); # reserve for return address! 2087 &mov ($_tbl,$tbl); # save %ebp 2088 &mov ($_esp,$acc); # save %esp 2089 2090 &mov ($s0,&DWP(0,$s3)); # load inp 2091 &mov ($s1,&DWP(4,$s3)); # load out 2092 #&mov ($s2,&DWP(8,$s3)); # load len 2093 &mov ($key,&DWP(12,$s3)); # load key 2094 &mov ($acc,&DWP(16,$s3)); # load ivp 2095 &mov ($s3,&DWP(20,$s3)); # load enc flag 2096 2097 &mov ($_inp,$s0); # save copy of inp 2098 &mov ($_out,$s1); # save copy of out 2099 &mov ($_len,$s2); # save copy of len 2100 &mov ($_key,$key); # save copy of key 2101 &mov ($_ivp,$acc); # save copy of ivp 2102 2103 &mov ($mark,0); # copy of aes_key->rounds = 0; 2104 # do we copy key schedule to stack? 2105 &mov ($s1 eq "ebx" ? $s1 : "",$key); 2106 &mov ($s2 eq "ecx" ? $s2 : "",244/4); 2107 &sub ($s1,$tbl); 2108 &mov ("esi",$key); 2109 &and ($s1,0xfff); 2110 &lea ("edi",$aes_key); 2111 &cmp ($s1,2048+256); 2112 &jb (&label("do_copy")); 2113 &cmp ($s1,4096-244); 2114 &jb (&label("skip_copy")); 2115 &set_label("do_copy",4); 2116 &mov ($_key,"edi"); 2117 &data_word(0xA5F3F689); # rep movsd 2118 &set_label("skip_copy"); 2119 2120 &mov ($key,16); 2121 &set_label("prefetch_tbl",4); 2122 &mov ($s0,&DWP(0,$tbl)); 2123 &mov ($s1,&DWP(32,$tbl)); 2124 &mov ($s2,&DWP(64,$tbl)); 2125 &mov ($acc,&DWP(96,$tbl)); 2126 &lea ($tbl,&DWP(128,$tbl)); 2127 &sub ($key,1); 2128 &jnz (&label("prefetch_tbl")); 2129 &sub ($tbl,2048); 2130 2131 &mov ($acc,$_inp); 2132 &mov ($key,$_ivp); 2133 2134 &cmp ($s3,0); 2135 &je (&label("fast_decrypt")); 2136 2137#----------------------------- ENCRYPT -----------------------------# 2138 &mov ($s0,&DWP(0,$key)); # load iv 2139 &mov ($s1,&DWP(4,$key)); 2140 2141 &set_label("fast_enc_loop",16); 2142 &mov ($s2,&DWP(8,$key)); 2143 &mov ($s3,&DWP(12,$key)); 2144 2145 &xor ($s0,&DWP(0,$acc)); # xor input data 2146 &xor ($s1,&DWP(4,$acc)); 2147 &xor ($s2,&DWP(8,$acc)); 2148 &xor ($s3,&DWP(12,$acc)); 2149 2150 &mov ($key,$_key); # load key 2151 &call ("_x86_AES_encrypt"); 2152 2153 &mov ($acc,$_inp); # load inp 2154 &mov ($key,$_out); # load out 2155 2156 &mov (&DWP(0,$key),$s0); # save output data 2157 &mov (&DWP(4,$key),$s1); 2158 &mov (&DWP(8,$key),$s2); 2159 &mov (&DWP(12,$key),$s3); 2160 2161 &lea ($acc,&DWP(16,$acc)); # advance inp 2162 &mov ($s2,$_len); # load len 2163 &mov ($_inp,$acc); # save inp 2164 &lea ($s3,&DWP(16,$key)); # advance out 2165 &mov ($_out,$s3); # save out 2166 &sub ($s2,16); # decrease len 2167 &mov ($_len,$s2); # save len 2168 &jnz (&label("fast_enc_loop")); 2169 &mov ($acc,$_ivp); # load ivp 2170 &mov ($s2,&DWP(8,$key)); # restore last 2 dwords 2171 &mov ($s3,&DWP(12,$key)); 2172 &mov (&DWP(0,$acc),$s0); # save ivec 2173 &mov (&DWP(4,$acc),$s1); 2174 &mov (&DWP(8,$acc),$s2); 2175 &mov (&DWP(12,$acc),$s3); 2176 2177 &cmp ($mark,0); # was the key schedule copied? 2178 &mov ("edi",$_key); 2179 &je (&label("skip_ezero")); 2180 # zero copy of key schedule 2181 &mov ("ecx",240/4); 2182 &xor ("eax","eax"); 2183 &align (4); 2184 &data_word(0xABF3F689); # rep stosd 2185 &set_label("skip_ezero") 2186 &mov ("esp",$_esp); 2187 &popf (); 2188 &set_label("drop_out"); 2189 &function_end_A(); 2190 &pushf (); # kludge, never executed 2191 2192#----------------------------- DECRYPT -----------------------------# 2193&set_label("fast_decrypt",16); 2194 2195 &cmp ($acc,$_out); 2196 &je (&label("fast_dec_in_place")); # in-place processing... 2197 2198 &mov ($_tmp,$key); 2199 2200 &align (4); 2201 &set_label("fast_dec_loop",16); 2202 &mov ($s0,&DWP(0,$acc)); # read input 2203 &mov ($s1,&DWP(4,$acc)); 2204 &mov ($s2,&DWP(8,$acc)); 2205 &mov ($s3,&DWP(12,$acc)); 2206 2207 &mov ($key,$_key); # load key 2208 &call ("_x86_AES_decrypt"); 2209 2210 &mov ($key,$_tmp); # load ivp 2211 &mov ($acc,$_len); # load len 2212 &xor ($s0,&DWP(0,$key)); # xor iv 2213 &xor ($s1,&DWP(4,$key)); 2214 &xor ($s2,&DWP(8,$key)); 2215 &xor ($s3,&DWP(12,$key)); 2216 2217 &mov ($key,$_out); # load out 2218 &mov ($acc,$_inp); # load inp 2219 2220 &mov (&DWP(0,$key),$s0); # write output 2221 &mov (&DWP(4,$key),$s1); 2222 &mov (&DWP(8,$key),$s2); 2223 &mov (&DWP(12,$key),$s3); 2224 2225 &mov ($s2,$_len); # load len 2226 &mov ($_tmp,$acc); # save ivp 2227 &lea ($acc,&DWP(16,$acc)); # advance inp 2228 &mov ($_inp,$acc); # save inp 2229 &lea ($key,&DWP(16,$key)); # advance out 2230 &mov ($_out,$key); # save out 2231 &sub ($s2,16); # decrease len 2232 &mov ($_len,$s2); # save len 2233 &jnz (&label("fast_dec_loop")); 2234 &mov ($key,$_tmp); # load temp ivp 2235 &mov ($acc,$_ivp); # load user ivp 2236 &mov ($s0,&DWP(0,$key)); # load iv 2237 &mov ($s1,&DWP(4,$key)); 2238 &mov ($s2,&DWP(8,$key)); 2239 &mov ($s3,&DWP(12,$key)); 2240 &mov (&DWP(0,$acc),$s0); # copy back to user 2241 &mov (&DWP(4,$acc),$s1); 2242 &mov (&DWP(8,$acc),$s2); 2243 &mov (&DWP(12,$acc),$s3); 2244 &jmp (&label("fast_dec_out")); 2245 2246 &set_label("fast_dec_in_place",16); 2247 &set_label("fast_dec_in_place_loop"); 2248 &mov ($s0,&DWP(0,$acc)); # read input 2249 &mov ($s1,&DWP(4,$acc)); 2250 &mov ($s2,&DWP(8,$acc)); 2251 &mov ($s3,&DWP(12,$acc)); 2252 2253 &lea ($key,$ivec); 2254 &mov (&DWP(0,$key),$s0); # copy to temp 2255 &mov (&DWP(4,$key),$s1); 2256 &mov (&DWP(8,$key),$s2); 2257 &mov (&DWP(12,$key),$s3); 2258 2259 &mov ($key,$_key); # load key 2260 &call ("_x86_AES_decrypt"); 2261 2262 &mov ($key,$_ivp); # load ivp 2263 &mov ($acc,$_out); # load out 2264 &xor ($s0,&DWP(0,$key)); # xor iv 2265 &xor ($s1,&DWP(4,$key)); 2266 &xor ($s2,&DWP(8,$key)); 2267 &xor ($s3,&DWP(12,$key)); 2268 2269 &mov (&DWP(0,$acc),$s0); # write output 2270 &mov (&DWP(4,$acc),$s1); 2271 &mov (&DWP(8,$acc),$s2); 2272 &mov (&DWP(12,$acc),$s3); 2273 2274 &lea ($acc,&DWP(16,$acc)); # advance out 2275 &mov ($_out,$acc); # save out 2276 2277 &lea ($acc,$ivec); 2278 &mov ($s0,&DWP(0,$acc)); # read temp 2279 &mov ($s1,&DWP(4,$acc)); 2280 &mov ($s2,&DWP(8,$acc)); 2281 &mov ($s3,&DWP(12,$acc)); 2282 2283 &mov (&DWP(0,$key),$s0); # copy iv 2284 &mov (&DWP(4,$key),$s1); 2285 &mov (&DWP(8,$key),$s2); 2286 &mov (&DWP(12,$key),$s3); 2287 2288 &mov ($acc,$_inp); # load inp 2289 &mov ($s2,$_len); # load len 2290 &lea ($acc,&DWP(16,$acc)); # advance inp 2291 &mov ($_inp,$acc); # save inp 2292 &sub ($s2,16); # decrease len 2293 &mov ($_len,$s2); # save len 2294 &jnz (&label("fast_dec_in_place_loop")); 2295 2296 &set_label("fast_dec_out",4); 2297 &cmp ($mark,0); # was the key schedule copied? 2298 &mov ("edi",$_key); 2299 &je (&label("skip_dzero")); 2300 # zero copy of key schedule 2301 &mov ("ecx",240/4); 2302 &xor ("eax","eax"); 2303 &align (4); 2304 &data_word(0xABF3F689); # rep stosd 2305 &set_label("skip_dzero") 2306 &mov ("esp",$_esp); 2307 &popf (); 2308 &function_end_A(); 2309 &pushf (); # kludge, never executed 2310 2311#--------------------------- SLOW ROUTINE ---------------------------# 2312&set_label("slow_way",16); 2313 2314 &mov ($s0,&DWP(0,$s0)) if (!$x86only);# load OPENSSL_ia32cap 2315 &mov ($key,&wparam(3)); # load key 2316 2317 # pre-allocate aligned stack frame... 2318 &lea ($acc,&DWP(-80,"esp")); 2319 &and ($acc,-64); 2320 2321 # ... and make sure it doesn't alias with $key modulo 1024 2322 &lea ($s1,&DWP(-80-63,$key)); 2323 &sub ($s1,$acc); 2324 &neg ($s1); 2325 &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line 2326 &sub ($acc,$s1); 2327 2328 # pick S-box copy which can't overlap with stack frame or $key 2329 &lea ($s1,&DWP(768,$acc)); 2330 &sub ($s1,$tbl); 2331 &and ($s1,0x300); 2332 &lea ($tbl,&DWP(2048+128,$tbl,$s1)); 2333 2334 &lea ($s3,&wparam(0)); # pointer to parameter block 2335 2336 &exch ("esp",$acc); 2337 &add ("esp",4); # reserve for return address! 2338 &mov ($_tbl,$tbl); # save %ebp 2339 &mov ($_esp,$acc); # save %esp 2340 &mov ($_tmp,$s0); # save OPENSSL_ia32cap 2341 2342 &mov ($s0,&DWP(0,$s3)); # load inp 2343 &mov ($s1,&DWP(4,$s3)); # load out 2344 #&mov ($s2,&DWP(8,$s3)); # load len 2345 #&mov ($key,&DWP(12,$s3)); # load key 2346 &mov ($acc,&DWP(16,$s3)); # load ivp 2347 &mov ($s3,&DWP(20,$s3)); # load enc flag 2348 2349 &mov ($_inp,$s0); # save copy of inp 2350 &mov ($_out,$s1); # save copy of out 2351 &mov ($_len,$s2); # save copy of len 2352 &mov ($_key,$key); # save copy of key 2353 &mov ($_ivp,$acc); # save copy of ivp 2354 2355 &mov ($key,$acc); 2356 &mov ($acc,$s0); 2357 2358 &cmp ($s3,0); 2359 &je (&label("slow_decrypt")); 2360 2361#--------------------------- SLOW ENCRYPT ---------------------------# 2362 &cmp ($s2,16); 2363 &mov ($s3,$s1); 2364 &jb (&label("slow_enc_tail")); 2365 2366 if (!$x86only) { 2367 &bt ($_tmp,25); # check for SSE bit 2368 &jnc (&label("slow_enc_x86")); 2369 2370 &movq ("mm0",&QWP(0,$key)); # load iv 2371 &movq ("mm4",&QWP(8,$key)); 2372 2373 &set_label("slow_enc_loop_sse",16); 2374 &pxor ("mm0",&QWP(0,$acc)); # xor input data 2375 &pxor ("mm4",&QWP(8,$acc)); 2376 2377 &mov ($key,$_key); 2378 &call ("_sse_AES_encrypt_compact"); 2379 2380 &mov ($acc,$_inp); # load inp 2381 &mov ($key,$_out); # load out 2382 &mov ($s2,$_len); # load len 2383 2384 &movq (&QWP(0,$key),"mm0"); # save output data 2385 &movq (&QWP(8,$key),"mm4"); 2386 2387 &lea ($acc,&DWP(16,$acc)); # advance inp 2388 &mov ($_inp,$acc); # save inp 2389 &lea ($s3,&DWP(16,$key)); # advance out 2390 &mov ($_out,$s3); # save out 2391 &sub ($s2,16); # decrease len 2392 &cmp ($s2,16); 2393 &mov ($_len,$s2); # save len 2394 &jae (&label("slow_enc_loop_sse")); 2395 &test ($s2,15); 2396 &jnz (&label("slow_enc_tail")); 2397 &mov ($acc,$_ivp); # load ivp 2398 &movq (&QWP(0,$acc),"mm0"); # save ivec 2399 &movq (&QWP(8,$acc),"mm4"); 2400 &emms (); 2401 &mov ("esp",$_esp); 2402 &popf (); 2403 &function_end_A(); 2404 &pushf (); # kludge, never executed 2405 } 2406 &set_label("slow_enc_x86",16); 2407 &mov ($s0,&DWP(0,$key)); # load iv 2408 &mov ($s1,&DWP(4,$key)); 2409 2410 &set_label("slow_enc_loop_x86",4); 2411 &mov ($s2,&DWP(8,$key)); 2412 &mov ($s3,&DWP(12,$key)); 2413 2414 &xor ($s0,&DWP(0,$acc)); # xor input data 2415 &xor ($s1,&DWP(4,$acc)); 2416 &xor ($s2,&DWP(8,$acc)); 2417 &xor ($s3,&DWP(12,$acc)); 2418 2419 &mov ($key,$_key); # load key 2420 &call ("_x86_AES_encrypt_compact"); 2421 2422 &mov ($acc,$_inp); # load inp 2423 &mov ($key,$_out); # load out 2424 2425 &mov (&DWP(0,$key),$s0); # save output data 2426 &mov (&DWP(4,$key),$s1); 2427 &mov (&DWP(8,$key),$s2); 2428 &mov (&DWP(12,$key),$s3); 2429 2430 &mov ($s2,$_len); # load len 2431 &lea ($acc,&DWP(16,$acc)); # advance inp 2432 &mov ($_inp,$acc); # save inp 2433 &lea ($s3,&DWP(16,$key)); # advance out 2434 &mov ($_out,$s3); # save out 2435 &sub ($s2,16); # decrease len 2436 &cmp ($s2,16); 2437 &mov ($_len,$s2); # save len 2438 &jae (&label("slow_enc_loop_x86")); 2439 &test ($s2,15); 2440 &jnz (&label("slow_enc_tail")); 2441 &mov ($acc,$_ivp); # load ivp 2442 &mov ($s2,&DWP(8,$key)); # restore last dwords 2443 &mov ($s3,&DWP(12,$key)); 2444 &mov (&DWP(0,$acc),$s0); # save ivec 2445 &mov (&DWP(4,$acc),$s1); 2446 &mov (&DWP(8,$acc),$s2); 2447 &mov (&DWP(12,$acc),$s3); 2448 2449 &mov ("esp",$_esp); 2450 &popf (); 2451 &function_end_A(); 2452 &pushf (); # kludge, never executed 2453 2454 &set_label("slow_enc_tail",16); 2455 &emms () if (!$x86only); 2456 &mov ($key eq "edi"? $key:"",$s3); # load out to edi 2457 &mov ($s1,16); 2458 &sub ($s1,$s2); 2459 &cmp ($key,$acc eq "esi"? $acc:""); # compare with inp 2460 &je (&label("enc_in_place")); 2461 &align (4); 2462 &data_word(0xA4F3F689); # rep movsb # copy input 2463 &jmp (&label("enc_skip_in_place")); 2464 &set_label("enc_in_place"); 2465 &lea ($key,&DWP(0,$key,$s2)); 2466 &set_label("enc_skip_in_place"); 2467 &mov ($s2,$s1); 2468 &xor ($s0,$s0); 2469 &align (4); 2470 &data_word(0xAAF3F689); # rep stosb # zero tail 2471 2472 &mov ($key,$_ivp); # restore ivp 2473 &mov ($acc,$s3); # output as input 2474 &mov ($s0,&DWP(0,$key)); 2475 &mov ($s1,&DWP(4,$key)); 2476 &mov ($_len,16); # len=16 2477 &jmp (&label("slow_enc_loop_x86")); # one more spin... 2478 2479#--------------------------- SLOW DECRYPT ---------------------------# 2480&set_label("slow_decrypt",16); 2481 if (!$x86only) { 2482 &bt ($_tmp,25); # check for SSE bit 2483 &jnc (&label("slow_dec_loop_x86")); 2484 2485 &set_label("slow_dec_loop_sse",4); 2486 &movq ("mm0",&QWP(0,$acc)); # read input 2487 &movq ("mm4",&QWP(8,$acc)); 2488 2489 &mov ($key,$_key); 2490 &call ("_sse_AES_decrypt_compact"); 2491 2492 &mov ($acc,$_inp); # load inp 2493 &lea ($s0,$ivec); 2494 &mov ($s1,$_out); # load out 2495 &mov ($s2,$_len); # load len 2496 &mov ($key,$_ivp); # load ivp 2497 2498 &movq ("mm1",&QWP(0,$acc)); # re-read input 2499 &movq ("mm5",&QWP(8,$acc)); 2500 2501 &pxor ("mm0",&QWP(0,$key)); # xor iv 2502 &pxor ("mm4",&QWP(8,$key)); 2503 2504 &movq (&QWP(0,$key),"mm1"); # copy input to iv 2505 &movq (&QWP(8,$key),"mm5"); 2506 2507 &sub ($s2,16); # decrease len 2508 &jc (&label("slow_dec_partial_sse")); 2509 2510 &movq (&QWP(0,$s1),"mm0"); # write output 2511 &movq (&QWP(8,$s1),"mm4"); 2512 2513 &lea ($s1,&DWP(16,$s1)); # advance out 2514 &mov ($_out,$s1); # save out 2515 &lea ($acc,&DWP(16,$acc)); # advance inp 2516 &mov ($_inp,$acc); # save inp 2517 &mov ($_len,$s2); # save len 2518 &jnz (&label("slow_dec_loop_sse")); 2519 &emms (); 2520 &mov ("esp",$_esp); 2521 &popf (); 2522 &function_end_A(); 2523 &pushf (); # kludge, never executed 2524 2525 &set_label("slow_dec_partial_sse",16); 2526 &movq (&QWP(0,$s0),"mm0"); # save output to temp 2527 &movq (&QWP(8,$s0),"mm4"); 2528 &emms (); 2529 2530 &add ($s2 eq "ecx" ? "ecx":"",16); 2531 &mov ("edi",$s1); # out 2532 &mov ("esi",$s0); # temp 2533 &align (4); 2534 &data_word(0xA4F3F689); # rep movsb # copy partial output 2535 2536 &mov ("esp",$_esp); 2537 &popf (); 2538 &function_end_A(); 2539 &pushf (); # kludge, never executed 2540 } 2541 &set_label("slow_dec_loop_x86",16); 2542 &mov ($s0,&DWP(0,$acc)); # read input 2543 &mov ($s1,&DWP(4,$acc)); 2544 &mov ($s2,&DWP(8,$acc)); 2545 &mov ($s3,&DWP(12,$acc)); 2546 2547 &lea ($key,$ivec); 2548 &mov (&DWP(0,$key),$s0); # copy to temp 2549 &mov (&DWP(4,$key),$s1); 2550 &mov (&DWP(8,$key),$s2); 2551 &mov (&DWP(12,$key),$s3); 2552 2553 &mov ($key,$_key); # load key 2554 &call ("_x86_AES_decrypt_compact"); 2555 2556 &mov ($key,$_ivp); # load ivp 2557 &mov ($acc,$_len); # load len 2558 &xor ($s0,&DWP(0,$key)); # xor iv 2559 &xor ($s1,&DWP(4,$key)); 2560 &xor ($s2,&DWP(8,$key)); 2561 &xor ($s3,&DWP(12,$key)); 2562 2563 &sub ($acc,16); 2564 &jc (&label("slow_dec_partial_x86")); 2565 2566 &mov ($_len,$acc); # save len 2567 &mov ($acc,$_out); # load out 2568 2569 &mov (&DWP(0,$acc),$s0); # write output 2570 &mov (&DWP(4,$acc),$s1); 2571 &mov (&DWP(8,$acc),$s2); 2572 &mov (&DWP(12,$acc),$s3); 2573 2574 &lea ($acc,&DWP(16,$acc)); # advance out 2575 &mov ($_out,$acc); # save out 2576 2577 &lea ($acc,$ivec); 2578 &mov ($s0,&DWP(0,$acc)); # read temp 2579 &mov ($s1,&DWP(4,$acc)); 2580 &mov ($s2,&DWP(8,$acc)); 2581 &mov ($s3,&DWP(12,$acc)); 2582 2583 &mov (&DWP(0,$key),$s0); # copy it to iv 2584 &mov (&DWP(4,$key),$s1); 2585 &mov (&DWP(8,$key),$s2); 2586 &mov (&DWP(12,$key),$s3); 2587 2588 &mov ($acc,$_inp); # load inp 2589 &lea ($acc,&DWP(16,$acc)); # advance inp 2590 &mov ($_inp,$acc); # save inp 2591 &jnz (&label("slow_dec_loop_x86")); 2592 &mov ("esp",$_esp); 2593 &popf (); 2594 &function_end_A(); 2595 &pushf (); # kludge, never executed 2596 2597 &set_label("slow_dec_partial_x86",16); 2598 &lea ($acc,$ivec); 2599 &mov (&DWP(0,$acc),$s0); # save output to temp 2600 &mov (&DWP(4,$acc),$s1); 2601 &mov (&DWP(8,$acc),$s2); 2602 &mov (&DWP(12,$acc),$s3); 2603 2604 &mov ($acc,$_inp); 2605 &mov ($s0,&DWP(0,$acc)); # re-read input 2606 &mov ($s1,&DWP(4,$acc)); 2607 &mov ($s2,&DWP(8,$acc)); 2608 &mov ($s3,&DWP(12,$acc)); 2609 2610 &mov (&DWP(0,$key),$s0); # copy it to iv 2611 &mov (&DWP(4,$key),$s1); 2612 &mov (&DWP(8,$key),$s2); 2613 &mov (&DWP(12,$key),$s3); 2614 2615 &mov ("ecx",$_len); 2616 &mov ("edi",$_out); 2617 &lea ("esi",$ivec); 2618 &align (4); 2619 &data_word(0xA4F3F689); # rep movsb # copy partial output 2620 2621 &mov ("esp",$_esp); 2622 &popf (); 2623&function_end("AES_cbc_encrypt"); 2624} 2625 2626#------------------------------------------------------------------# 2627 2628sub enckey() 2629{ 2630 &movz ("esi",&LB("edx")); # rk[i]>>0 2631 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2632 &movz ("esi",&HB("edx")); # rk[i]>>8 2633 &shl ("ebx",24); 2634 &xor ("eax","ebx"); 2635 2636 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2637 &shr ("edx",16); 2638 &movz ("esi",&LB("edx")); # rk[i]>>16 2639 &xor ("eax","ebx"); 2640 2641 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2642 &movz ("esi",&HB("edx")); # rk[i]>>24 2643 &shl ("ebx",8); 2644 &xor ("eax","ebx"); 2645 2646 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2647 &shl ("ebx",16); 2648 &xor ("eax","ebx"); 2649 2650 &xor ("eax",&DWP(1024-128,$tbl,"ecx",4)); # rcon 2651} 2652 2653&function_begin("_x86_AES_set_encrypt_key"); 2654 &mov ("esi",&wparam(1)); # user supplied key 2655 &mov ("edi",&wparam(3)); # private key schedule 2656 2657 &test ("esi",-1); 2658 &jz (&label("badpointer")); 2659 &test ("edi",-1); 2660 &jz (&label("badpointer")); 2661 2662 &call (&label("pic_point")); 2663 &set_label("pic_point"); 2664 &blindpop($tbl); 2665 &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl)); 2666 &lea ($tbl,&DWP(2048+128,$tbl)); 2667 2668 # prefetch Te4 2669 &mov ("eax",&DWP(0-128,$tbl)); 2670 &mov ("ebx",&DWP(32-128,$tbl)); 2671 &mov ("ecx",&DWP(64-128,$tbl)); 2672 &mov ("edx",&DWP(96-128,$tbl)); 2673 &mov ("eax",&DWP(128-128,$tbl)); 2674 &mov ("ebx",&DWP(160-128,$tbl)); 2675 &mov ("ecx",&DWP(192-128,$tbl)); 2676 &mov ("edx",&DWP(224-128,$tbl)); 2677 2678 &mov ("ecx",&wparam(2)); # number of bits in key 2679 &cmp ("ecx",128); 2680 &je (&label("10rounds")); 2681 &cmp ("ecx",192); 2682 &je (&label("12rounds")); 2683 &cmp ("ecx",256); 2684 &je (&label("14rounds")); 2685 &mov ("eax",-2); # invalid number of bits 2686 &jmp (&label("exit")); 2687 2688 &set_label("10rounds"); 2689 &mov ("eax",&DWP(0,"esi")); # copy first 4 dwords 2690 &mov ("ebx",&DWP(4,"esi")); 2691 &mov ("ecx",&DWP(8,"esi")); 2692 &mov ("edx",&DWP(12,"esi")); 2693 &mov (&DWP(0,"edi"),"eax"); 2694 &mov (&DWP(4,"edi"),"ebx"); 2695 &mov (&DWP(8,"edi"),"ecx"); 2696 &mov (&DWP(12,"edi"),"edx"); 2697 2698 &xor ("ecx","ecx"); 2699 &jmp (&label("10shortcut")); 2700 2701 &align (4); 2702 &set_label("10loop"); 2703 &mov ("eax",&DWP(0,"edi")); # rk[0] 2704 &mov ("edx",&DWP(12,"edi")); # rk[3] 2705 &set_label("10shortcut"); 2706 &enckey (); 2707 2708 &mov (&DWP(16,"edi"),"eax"); # rk[4] 2709 &xor ("eax",&DWP(4,"edi")); 2710 &mov (&DWP(20,"edi"),"eax"); # rk[5] 2711 &xor ("eax",&DWP(8,"edi")); 2712 &mov (&DWP(24,"edi"),"eax"); # rk[6] 2713 &xor ("eax",&DWP(12,"edi")); 2714 &mov (&DWP(28,"edi"),"eax"); # rk[7] 2715 &inc ("ecx"); 2716 &add ("edi",16); 2717 &cmp ("ecx",10); 2718 &jl (&label("10loop")); 2719 2720 &mov (&DWP(80,"edi"),10); # setup number of rounds 2721 &xor ("eax","eax"); 2722 &jmp (&label("exit")); 2723 2724 &set_label("12rounds"); 2725 &mov ("eax",&DWP(0,"esi")); # copy first 6 dwords 2726 &mov ("ebx",&DWP(4,"esi")); 2727 &mov ("ecx",&DWP(8,"esi")); 2728 &mov ("edx",&DWP(12,"esi")); 2729 &mov (&DWP(0,"edi"),"eax"); 2730 &mov (&DWP(4,"edi"),"ebx"); 2731 &mov (&DWP(8,"edi"),"ecx"); 2732 &mov (&DWP(12,"edi"),"edx"); 2733 &mov ("ecx",&DWP(16,"esi")); 2734 &mov ("edx",&DWP(20,"esi")); 2735 &mov (&DWP(16,"edi"),"ecx"); 2736 &mov (&DWP(20,"edi"),"edx"); 2737 2738 &xor ("ecx","ecx"); 2739 &jmp (&label("12shortcut")); 2740 2741 &align (4); 2742 &set_label("12loop"); 2743 &mov ("eax",&DWP(0,"edi")); # rk[0] 2744 &mov ("edx",&DWP(20,"edi")); # rk[5] 2745 &set_label("12shortcut"); 2746 &enckey (); 2747 2748 &mov (&DWP(24,"edi"),"eax"); # rk[6] 2749 &xor ("eax",&DWP(4,"edi")); 2750 &mov (&DWP(28,"edi"),"eax"); # rk[7] 2751 &xor ("eax",&DWP(8,"edi")); 2752 &mov (&DWP(32,"edi"),"eax"); # rk[8] 2753 &xor ("eax",&DWP(12,"edi")); 2754 &mov (&DWP(36,"edi"),"eax"); # rk[9] 2755 2756 &cmp ("ecx",7); 2757 &je (&label("12break")); 2758 &inc ("ecx"); 2759 2760 &xor ("eax",&DWP(16,"edi")); 2761 &mov (&DWP(40,"edi"),"eax"); # rk[10] 2762 &xor ("eax",&DWP(20,"edi")); 2763 &mov (&DWP(44,"edi"),"eax"); # rk[11] 2764 2765 &add ("edi",24); 2766 &jmp (&label("12loop")); 2767 2768 &set_label("12break"); 2769 &mov (&DWP(72,"edi"),12); # setup number of rounds 2770 &xor ("eax","eax"); 2771 &jmp (&label("exit")); 2772 2773 &set_label("14rounds"); 2774 &mov ("eax",&DWP(0,"esi")); # copy first 8 dwords 2775 &mov ("ebx",&DWP(4,"esi")); 2776 &mov ("ecx",&DWP(8,"esi")); 2777 &mov ("edx",&DWP(12,"esi")); 2778 &mov (&DWP(0,"edi"),"eax"); 2779 &mov (&DWP(4,"edi"),"ebx"); 2780 &mov (&DWP(8,"edi"),"ecx"); 2781 &mov (&DWP(12,"edi"),"edx"); 2782 &mov ("eax",&DWP(16,"esi")); 2783 &mov ("ebx",&DWP(20,"esi")); 2784 &mov ("ecx",&DWP(24,"esi")); 2785 &mov ("edx",&DWP(28,"esi")); 2786 &mov (&DWP(16,"edi"),"eax"); 2787 &mov (&DWP(20,"edi"),"ebx"); 2788 &mov (&DWP(24,"edi"),"ecx"); 2789 &mov (&DWP(28,"edi"),"edx"); 2790 2791 &xor ("ecx","ecx"); 2792 &jmp (&label("14shortcut")); 2793 2794 &align (4); 2795 &set_label("14loop"); 2796 &mov ("edx",&DWP(28,"edi")); # rk[7] 2797 &set_label("14shortcut"); 2798 &mov ("eax",&DWP(0,"edi")); # rk[0] 2799 2800 &enckey (); 2801 2802 &mov (&DWP(32,"edi"),"eax"); # rk[8] 2803 &xor ("eax",&DWP(4,"edi")); 2804 &mov (&DWP(36,"edi"),"eax"); # rk[9] 2805 &xor ("eax",&DWP(8,"edi")); 2806 &mov (&DWP(40,"edi"),"eax"); # rk[10] 2807 &xor ("eax",&DWP(12,"edi")); 2808 &mov (&DWP(44,"edi"),"eax"); # rk[11] 2809 2810 &cmp ("ecx",6); 2811 &je (&label("14break")); 2812 &inc ("ecx"); 2813 2814 &mov ("edx","eax"); 2815 &mov ("eax",&DWP(16,"edi")); # rk[4] 2816 &movz ("esi",&LB("edx")); # rk[11]>>0 2817 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2818 &movz ("esi",&HB("edx")); # rk[11]>>8 2819 &xor ("eax","ebx"); 2820 2821 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2822 &shr ("edx",16); 2823 &shl ("ebx",8); 2824 &movz ("esi",&LB("edx")); # rk[11]>>16 2825 &xor ("eax","ebx"); 2826 2827 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2828 &movz ("esi",&HB("edx")); # rk[11]>>24 2829 &shl ("ebx",16); 2830 &xor ("eax","ebx"); 2831 2832 &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2833 &shl ("ebx",24); 2834 &xor ("eax","ebx"); 2835 2836 &mov (&DWP(48,"edi"),"eax"); # rk[12] 2837 &xor ("eax",&DWP(20,"edi")); 2838 &mov (&DWP(52,"edi"),"eax"); # rk[13] 2839 &xor ("eax",&DWP(24,"edi")); 2840 &mov (&DWP(56,"edi"),"eax"); # rk[14] 2841 &xor ("eax",&DWP(28,"edi")); 2842 &mov (&DWP(60,"edi"),"eax"); # rk[15] 2843 2844 &add ("edi",32); 2845 &jmp (&label("14loop")); 2846 2847 &set_label("14break"); 2848 &mov (&DWP(48,"edi"),14); # setup number of rounds 2849 &xor ("eax","eax"); 2850 &jmp (&label("exit")); 2851 2852 &set_label("badpointer"); 2853 &mov ("eax",-1); 2854 &set_label("exit"); 2855&function_end("_x86_AES_set_encrypt_key"); 2856 2857# int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits, 2858# AES_KEY *key) 2859&function_begin_B("private_AES_set_encrypt_key"); 2860 &call ("_x86_AES_set_encrypt_key"); 2861 &ret (); 2862&function_end_B("private_AES_set_encrypt_key"); 2863 2864sub deckey() 2865{ my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_; 2866 my $tmp = $tbl; 2867 2868 &mov ($acc,$tp1); 2869 &and ($acc,0x80808080); 2870 &mov ($tmp,$acc); 2871 &shr ($tmp,7); 2872 &lea ($tp2,&DWP(0,$tp1,$tp1)); 2873 &sub ($acc,$tmp); 2874 &and ($tp2,0xfefefefe); 2875 &and ($acc,0x1b1b1b1b); 2876 &xor ($acc,$tp2); 2877 &mov ($tp2,$acc); 2878 2879 &and ($acc,0x80808080); 2880 &mov ($tmp,$acc); 2881 &shr ($tmp,7); 2882 &lea ($tp4,&DWP(0,$tp2,$tp2)); 2883 &sub ($acc,$tmp); 2884 &and ($tp4,0xfefefefe); 2885 &and ($acc,0x1b1b1b1b); 2886 &xor ($tp2,$tp1); # tp2^tp1 2887 &xor ($acc,$tp4); 2888 &mov ($tp4,$acc); 2889 2890 &and ($acc,0x80808080); 2891 &mov ($tmp,$acc); 2892 &shr ($tmp,7); 2893 &lea ($tp8,&DWP(0,$tp4,$tp4)); 2894 &xor ($tp4,$tp1); # tp4^tp1 2895 &sub ($acc,$tmp); 2896 &and ($tp8,0xfefefefe); 2897 &and ($acc,0x1b1b1b1b); 2898 &rotl ($tp1,8); # = ROTATE(tp1,8) 2899 &xor ($tp8,$acc); 2900 2901 &mov ($tmp,&DWP(4*($i+1),$key)); # modulo-scheduled load 2902 2903 &xor ($tp1,$tp2); 2904 &xor ($tp2,$tp8); 2905 &xor ($tp1,$tp4); 2906 &rotl ($tp2,24); 2907 &xor ($tp4,$tp8); 2908 &xor ($tp1,$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1) 2909 &rotl ($tp4,16); 2910 &xor ($tp1,$tp2); # ^= ROTATE(tp8^tp2^tp1,24) 2911 &rotl ($tp8,8); 2912 &xor ($tp1,$tp4); # ^= ROTATE(tp8^tp4^tp1,16) 2913 &mov ($tp2,$tmp); 2914 &xor ($tp1,$tp8); # ^= ROTATE(tp8,8) 2915 2916 &mov (&DWP(4*$i,$key),$tp1); 2917} 2918 2919# int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits, 2920# AES_KEY *key) 2921&function_begin_B("private_AES_set_decrypt_key"); 2922 &call ("_x86_AES_set_encrypt_key"); 2923 &cmp ("eax",0); 2924 &je (&label("proceed")); 2925 &ret (); 2926 2927 &set_label("proceed"); 2928 &push ("ebp"); 2929 &push ("ebx"); 2930 &push ("esi"); 2931 &push ("edi"); 2932 2933 &mov ("esi",&wparam(2)); 2934 &mov ("ecx",&DWP(240,"esi")); # pull number of rounds 2935 &lea ("ecx",&DWP(0,"","ecx",4)); 2936 &lea ("edi",&DWP(0,"esi","ecx",4)); # pointer to last chunk 2937 2938 &set_label("invert",4); # invert order of chunks 2939 &mov ("eax",&DWP(0,"esi")); 2940 &mov ("ebx",&DWP(4,"esi")); 2941 &mov ("ecx",&DWP(0,"edi")); 2942 &mov ("edx",&DWP(4,"edi")); 2943 &mov (&DWP(0,"edi"),"eax"); 2944 &mov (&DWP(4,"edi"),"ebx"); 2945 &mov (&DWP(0,"esi"),"ecx"); 2946 &mov (&DWP(4,"esi"),"edx"); 2947 &mov ("eax",&DWP(8,"esi")); 2948 &mov ("ebx",&DWP(12,"esi")); 2949 &mov ("ecx",&DWP(8,"edi")); 2950 &mov ("edx",&DWP(12,"edi")); 2951 &mov (&DWP(8,"edi"),"eax"); 2952 &mov (&DWP(12,"edi"),"ebx"); 2953 &mov (&DWP(8,"esi"),"ecx"); 2954 &mov (&DWP(12,"esi"),"edx"); 2955 &add ("esi",16); 2956 &sub ("edi",16); 2957 &cmp ("esi","edi"); 2958 &jne (&label("invert")); 2959 2960 &mov ($key,&wparam(2)); 2961 &mov ($acc,&DWP(240,$key)); # pull number of rounds 2962 &lea ($acc,&DWP(-2,$acc,$acc)); 2963 &lea ($acc,&DWP(0,$key,$acc,8)); 2964 &mov (&wparam(2),$acc); 2965 2966 &mov ($s0,&DWP(16,$key)); # modulo-scheduled load 2967 &set_label("permute",4); # permute the key schedule 2968 &add ($key,16); 2969 &deckey (0,$key,$s0,$s1,$s2,$s3); 2970 &deckey (1,$key,$s1,$s2,$s3,$s0); 2971 &deckey (2,$key,$s2,$s3,$s0,$s1); 2972 &deckey (3,$key,$s3,$s0,$s1,$s2); 2973 &cmp ($key,&wparam(2)); 2974 &jb (&label("permute")); 2975 2976 &xor ("eax","eax"); # return success 2977&function_end("private_AES_set_decrypt_key"); 2978&asciz("AES for x86, CRYPTOGAMS by <appro\@openssl.org>"); 2979 2980&asm_finish(); 2981