1160814Ssimon#!/usr/bin/env perl 2160814Ssimon# 3160814Ssimon# ==================================================================== 4160814Ssimon# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL 5238405Sjkim# project. The module is, however, dual licensed under OpenSSL and 6238405Sjkim# CRYPTOGAMS licenses depending on where you obtain it. For further 7238405Sjkim# details see http://www.openssl.org/~appro/cryptogams/. 8160814Ssimon# ==================================================================== 9160814Ssimon# 10238405Sjkim# Version 4.3. 11160814Ssimon# 12160814Ssimon# You might fail to appreciate this module performance from the first 13160814Ssimon# try. If compared to "vanilla" linux-ia32-icc target, i.e. considered 14160814Ssimon# to be *the* best Intel C compiler without -KPIC, performance appears 15160814Ssimon# to be virtually identical... But try to re-configure with shared 16160814Ssimon# library support... Aha! Intel compiler "suddenly" lags behind by 30% 17160814Ssimon# [on P4, more on others]:-) And if compared to position-independent 18160814Ssimon# code generated by GNU C, this code performs *more* than *twice* as 19160814Ssimon# fast! Yes, all this buzz about PIC means that unlike other hand- 20160814Ssimon# coded implementations, this one was explicitly designed to be safe 21160814Ssimon# to use even in shared library context... This also means that this 22160814Ssimon# code isn't necessarily absolutely fastest "ever," because in order 23160814Ssimon# to achieve position independence an extra register has to be 24160814Ssimon# off-loaded to stack, which affects the benchmark result. 25160814Ssimon# 26160814Ssimon# Special note about instruction choice. Do you recall RC4_INT code 27160814Ssimon# performing poorly on P4? It might be the time to figure out why. 28160814Ssimon# RC4_INT code implies effective address calculations in base+offset*4 29160814Ssimon# form. Trouble is that it seems that offset scaling turned to be 30160814Ssimon# critical path... At least eliminating scaling resulted in 2.8x RC4 31160814Ssimon# performance improvement [as you might recall]. As AES code is hungry 32160814Ssimon# for scaling too, I [try to] avoid the latter by favoring off-by-2 33160814Ssimon# shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF. 34160814Ssimon# 35160814Ssimon# As was shown by Dean Gaudet <dean@arctic.org>, the above note turned 36160814Ssimon# void. Performance improvement with off-by-2 shifts was observed on 37160814Ssimon# intermediate implementation, which was spilling yet another register 38160814Ssimon# to stack... Final offset*4 code below runs just a tad faster on P4, 39160814Ssimon# but exhibits up to 10% improvement on other cores. 40160814Ssimon# 41160814Ssimon# Second version is "monolithic" replacement for aes_core.c, which in 42290207Sjkim# addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key. 43160814Ssimon# This made it possible to implement little-endian variant of the 44160814Ssimon# algorithm without modifying the base C code. Motivating factor for 45160814Ssimon# the undertaken effort was that it appeared that in tight IA-32 46160814Ssimon# register window little-endian flavor could achieve slightly higher 47160814Ssimon# Instruction Level Parallelism, and it indeed resulted in up to 15% 48291719Sjkim# better performance on most recent ��-archs... 49160814Ssimon# 50160814Ssimon# Third version adds AES_cbc_encrypt implementation, which resulted in 51160814Ssimon# up to 40% performance imrovement of CBC benchmark results. 40% was 52160814Ssimon# observed on P4 core, where "overall" imrovement coefficient, i.e. if 53160814Ssimon# compared to PIC generated by GCC and in CBC mode, was observed to be 54160814Ssimon# as large as 4x:-) CBC performance is virtually identical to ECB now 55160814Ssimon# and on some platforms even better, e.g. 17.6 "small" cycles/byte on 56160814Ssimon# Opteron, because certain function prologues and epilogues are 57160814Ssimon# effectively taken out of the loop... 58160814Ssimon# 59160814Ssimon# Version 3.2 implements compressed tables and prefetch of these tables 60160814Ssimon# in CBC[!] mode. Former means that 3/4 of table references are now 61160814Ssimon# misaligned, which unfortunately has negative impact on elder IA-32 62160814Ssimon# implementations, Pentium suffered 30% penalty, PIII - 10%. 63160814Ssimon# 64160814Ssimon# Version 3.3 avoids L1 cache aliasing between stack frame and 65160814Ssimon# S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The 66160814Ssimon# latter is achieved by copying the key schedule to controlled place in 67160814Ssimon# stack. This unfortunately has rather strong impact on small block CBC 68160814Ssimon# performance, ~2x deterioration on 16-byte block if compared to 3.3. 69160814Ssimon# 70162911Ssimon# Version 3.5 checks if there is L1 cache aliasing between user-supplied 71162911Ssimon# key schedule and S-boxes and abstains from copying the former if 72162911Ssimon# there is no. This allows end-user to consciously retain small block 73162911Ssimon# performance by aligning key schedule in specific manner. 74162911Ssimon# 75162911Ssimon# Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB. 76162911Ssimon# 77160814Ssimon# Current ECB performance numbers for 128-bit key in CPU cycles per 78160814Ssimon# processed byte [measure commonly used by AES benchmarkers] are: 79160814Ssimon# 80160814Ssimon# small footprint fully unrolled 81160814Ssimon# P4 24 22 82160814Ssimon# AMD K8 20 19 83160814Ssimon# PIII 25 23 84160814Ssimon# Pentium 81 78 85238405Sjkim# 86238405Sjkim# Version 3.7 reimplements outer rounds as "compact." Meaning that 87238405Sjkim# first and last rounds reference compact 256 bytes S-box. This means 88238405Sjkim# that first round consumes a lot more CPU cycles and that encrypt 89238405Sjkim# and decrypt performance becomes asymmetric. Encrypt performance 90238405Sjkim# drops by 10-12%, while decrypt - by 20-25%:-( 256 bytes S-box is 91238405Sjkim# aggressively pre-fetched. 92238405Sjkim# 93238405Sjkim# Version 4.0 effectively rolls back to 3.6 and instead implements 94238405Sjkim# additional set of functions, _[x86|sse]_AES_[en|de]crypt_compact, 95238405Sjkim# which use exclusively 256 byte S-box. These functions are to be 96238405Sjkim# called in modes not concealing plain text, such as ECB, or when 97238405Sjkim# we're asked to process smaller amount of data [or unconditionally 98238405Sjkim# on hyper-threading CPU]. Currently it's called unconditionally from 99238405Sjkim# AES_[en|de]crypt, which affects all modes, but CBC. CBC routine 100238405Sjkim# still needs to be modified to switch between slower and faster 101238405Sjkim# mode when appropriate... But in either case benchmark landscape 102238405Sjkim# changes dramatically and below numbers are CPU cycles per processed 103238405Sjkim# byte for 128-bit key. 104238405Sjkim# 105238405Sjkim# ECB encrypt ECB decrypt CBC large chunk 106290207Sjkim# P4 52[54] 83[95] 23 107290207Sjkim# AMD K8 46[41] 66[70] 18 108290207Sjkim# PIII 41[50] 60[77] 24 109290207Sjkim# Core 2 31[36] 45[64] 18.5 110290207Sjkim# Atom 76[100] 96[138] 60 111290207Sjkim# Pentium 115 150 77 112238405Sjkim# 113238405Sjkim# Version 4.1 switches to compact S-box even in key schedule setup. 114238405Sjkim# 115238405Sjkim# Version 4.2 prefetches compact S-box in every SSE round or in other 116238405Sjkim# words every cache-line is *guaranteed* to be accessed within ~50 117238405Sjkim# cycles window. Why just SSE? Because it's needed on hyper-threading 118238405Sjkim# CPU! Which is also why it's prefetched with 64 byte stride. Best 119238405Sjkim# part is that it has no negative effect on performance:-) 120238405Sjkim# 121238405Sjkim# Version 4.3 implements switch between compact and non-compact block 122238405Sjkim# functions in AES_cbc_encrypt depending on how much data was asked 123238405Sjkim# to be processed in one stroke. 124238405Sjkim# 125238405Sjkim###################################################################### 126238405Sjkim# Timing attacks are classified in two classes: synchronous when 127238405Sjkim# attacker consciously initiates cryptographic operation and collects 128238405Sjkim# timing data of various character afterwards, and asynchronous when 129238405Sjkim# malicious code is executed on same CPU simultaneously with AES, 130238405Sjkim# instruments itself and performs statistical analysis of this data. 131238405Sjkim# 132238405Sjkim# As far as synchronous attacks go the root to the AES timing 133238405Sjkim# vulnerability is twofold. Firstly, of 256 S-box elements at most 160 134238405Sjkim# are referred to in single 128-bit block operation. Well, in C 135238405Sjkim# implementation with 4 distinct tables it's actually as little as 40 136238405Sjkim# references per 256 elements table, but anyway... Secondly, even 137238405Sjkim# though S-box elements are clustered into smaller amount of cache- 138238405Sjkim# lines, smaller than 160 and even 40, it turned out that for certain 139238405Sjkim# plain-text pattern[s] or simply put chosen plain-text and given key 140238405Sjkim# few cache-lines remain unaccessed during block operation. Now, if 141238405Sjkim# attacker can figure out this access pattern, he can deduct the key 142238405Sjkim# [or at least part of it]. The natural way to mitigate this kind of 143238405Sjkim# attacks is to minimize the amount of cache-lines in S-box and/or 144238405Sjkim# prefetch them to ensure that every one is accessed for more uniform 145238405Sjkim# timing. But note that *if* plain-text was concealed in such way that 146238405Sjkim# input to block function is distributed *uniformly*, then attack 147238405Sjkim# wouldn't apply. Now note that some encryption modes, most notably 148238405Sjkim# CBC, do mask the plain-text in this exact way [secure cipher output 149238405Sjkim# is distributed uniformly]. Yes, one still might find input that 150238405Sjkim# would reveal the information about given key, but if amount of 151238405Sjkim# candidate inputs to be tried is larger than amount of possible key 152238405Sjkim# combinations then attack becomes infeasible. This is why revised 153238405Sjkim# AES_cbc_encrypt "dares" to switch to larger S-box when larger chunk 154238405Sjkim# of data is to be processed in one stroke. The current size limit of 155238405Sjkim# 512 bytes is chosen to provide same [diminishigly low] probability 156238405Sjkim# for cache-line to remain untouched in large chunk operation with 157238405Sjkim# large S-box as for single block operation with compact S-box and 158238405Sjkim# surely needs more careful consideration... 159238405Sjkim# 160238405Sjkim# As for asynchronous attacks. There are two flavours: attacker code 161238405Sjkim# being interleaved with AES on hyper-threading CPU at *instruction* 162238405Sjkim# level, and two processes time sharing single core. As for latter. 163238405Sjkim# Two vectors. 1. Given that attacker process has higher priority, 164238405Sjkim# yield execution to process performing AES just before timer fires 165238405Sjkim# off the scheduler, immediately regain control of CPU and analyze the 166238405Sjkim# cache state. For this attack to be efficient attacker would have to 167238405Sjkim# effectively slow down the operation by several *orders* of magnitute, 168238405Sjkim# by ratio of time slice to duration of handful of AES rounds, which 169238405Sjkim# unlikely to remain unnoticed. Not to mention that this also means 170238405Sjkim# that he would spend correspondigly more time to collect enough 171238405Sjkim# statistical data to mount the attack. It's probably appropriate to 172238405Sjkim# say that if adeversary reckons that this attack is beneficial and 173238405Sjkim# risks to be noticed, you probably have larger problems having him 174238405Sjkim# mere opportunity. In other words suggested code design expects you 175238405Sjkim# to preclude/mitigate this attack by overall system security design. 176238405Sjkim# 2. Attacker manages to make his code interrupt driven. In order for 177238405Sjkim# this kind of attack to be feasible, interrupt rate has to be high 178238405Sjkim# enough, again comparable to duration of handful of AES rounds. But 179238405Sjkim# is there interrupt source of such rate? Hardly, not even 1Gbps NIC 180238405Sjkim# generates interrupts at such raging rate... 181238405Sjkim# 182238405Sjkim# And now back to the former, hyper-threading CPU or more specifically 183238405Sjkim# Intel P4. Recall that asynchronous attack implies that malicious 184238405Sjkim# code instruments itself. And naturally instrumentation granularity 185238405Sjkim# has be noticeably lower than duration of codepath accessing S-box. 186238405Sjkim# Given that all cache-lines are accessed during that time that is. 187238405Sjkim# Current implementation accesses *all* cache-lines within ~50 cycles 188238405Sjkim# window, which is actually *less* than RDTSC latency on Intel P4! 189160814Ssimon 190238405Sjkim$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 191238405Sjkimpush(@INC,"${dir}","${dir}../../perlasm"); 192160814Ssimonrequire "x86asm.pl"; 193160814Ssimon 194238405Sjkim&asm_init($ARGV[0],"aes-586.pl",$x86only = $ARGV[$#ARGV] eq "386"); 195238405Sjkim&static_label("AES_Te"); 196238405Sjkim&static_label("AES_Td"); 197160814Ssimon 198160814Ssimon$s0="eax"; 199160814Ssimon$s1="ebx"; 200160814Ssimon$s2="ecx"; 201160814Ssimon$s3="edx"; 202160814Ssimon$key="edi"; 203160814Ssimon$acc="esi"; 204238405Sjkim$tbl="ebp"; 205160814Ssimon 206238405Sjkim# stack frame layout in _[x86|sse]_AES_* routines, frame is allocated 207238405Sjkim# by caller 208238405Sjkim$__ra=&DWP(0,"esp"); # return address 209238405Sjkim$__s0=&DWP(4,"esp"); # s0 backing store 210238405Sjkim$__s1=&DWP(8,"esp"); # s1 backing store 211238405Sjkim$__s2=&DWP(12,"esp"); # s2 backing store 212238405Sjkim$__s3=&DWP(16,"esp"); # s3 backing store 213238405Sjkim$__key=&DWP(20,"esp"); # pointer to key schedule 214238405Sjkim$__end=&DWP(24,"esp"); # pointer to end of key schedule 215238405Sjkim$__tbl=&DWP(28,"esp"); # %ebp backing store 216238405Sjkim 217238405Sjkim# stack frame layout in AES_[en|crypt] routines, which differs from 218238405Sjkim# above by 4 and overlaps by %ebp backing store 219238405Sjkim$_tbl=&DWP(24,"esp"); 220238405Sjkim$_esp=&DWP(28,"esp"); 221238405Sjkim 222238405Sjkimsub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } } 223238405Sjkim 224238405Sjkim$speed_limit=512; # chunks smaller than $speed_limit are 225238405Sjkim # processed with compact routine in CBC mode 226160814Ssimon$small_footprint=1; # $small_footprint=1 code is ~5% slower [on 227291719Sjkim # recent ��-archs], but ~5 times smaller! 228160814Ssimon # I favor compact code to minimize cache 229160814Ssimon # contention and in hope to "collect" 5% back 230160814Ssimon # in real-life applications... 231238405Sjkim 232160814Ssimon$vertical_spin=0; # shift "verticaly" defaults to 0, because of 233160814Ssimon # its proof-of-concept status... 234160814Ssimon# Note that there is no decvert(), as well as last encryption round is 235160814Ssimon# performed with "horizontal" shifts. This is because this "vertical" 236160814Ssimon# implementation [one which groups shifts on a given $s[i] to form a 237160814Ssimon# "column," unlike "horizontal" one, which groups shifts on different 238160814Ssimon# $s[i] to form a "row"] is work in progress. It was observed to run 239160814Ssimon# few percents faster on Intel cores, but not AMD. On AMD K8 core it's 240160814Ssimon# whole 12% slower:-( So we face a trade-off... Shall it be resolved 241160814Ssimon# some day? Till then the code is considered experimental and by 242160814Ssimon# default remains dormant... 243160814Ssimon 244160814Ssimonsub encvert() 245160814Ssimon{ my ($te,@s) = @_; 246290207Sjkim my ($v0,$v1) = ($acc,$key); 247160814Ssimon 248160814Ssimon &mov ($v0,$s[3]); # copy s3 249160814Ssimon &mov (&DWP(4,"esp"),$s[2]); # save s2 250160814Ssimon &mov ($v1,$s[0]); # copy s0 251160814Ssimon &mov (&DWP(8,"esp"),$s[1]); # save s1 252160814Ssimon 253160814Ssimon &movz ($s[2],&HB($s[0])); 254160814Ssimon &and ($s[0],0xFF); 255160814Ssimon &mov ($s[0],&DWP(0,$te,$s[0],8)); # s0>>0 256160814Ssimon &shr ($v1,16); 257160814Ssimon &mov ($s[3],&DWP(3,$te,$s[2],8)); # s0>>8 258160814Ssimon &movz ($s[1],&HB($v1)); 259160814Ssimon &and ($v1,0xFF); 260160814Ssimon &mov ($s[2],&DWP(2,$te,$v1,8)); # s0>>16 261160814Ssimon &mov ($v1,$v0); 262160814Ssimon &mov ($s[1],&DWP(1,$te,$s[1],8)); # s0>>24 263160814Ssimon 264160814Ssimon &and ($v0,0xFF); 265160814Ssimon &xor ($s[3],&DWP(0,$te,$v0,8)); # s3>>0 266160814Ssimon &movz ($v0,&HB($v1)); 267160814Ssimon &shr ($v1,16); 268160814Ssimon &xor ($s[2],&DWP(3,$te,$v0,8)); # s3>>8 269160814Ssimon &movz ($v0,&HB($v1)); 270160814Ssimon &and ($v1,0xFF); 271160814Ssimon &xor ($s[1],&DWP(2,$te,$v1,8)); # s3>>16 272160814Ssimon &mov ($v1,&DWP(4,"esp")); # restore s2 273160814Ssimon &xor ($s[0],&DWP(1,$te,$v0,8)); # s3>>24 274160814Ssimon 275160814Ssimon &mov ($v0,$v1); 276160814Ssimon &and ($v1,0xFF); 277160814Ssimon &xor ($s[2],&DWP(0,$te,$v1,8)); # s2>>0 278160814Ssimon &movz ($v1,&HB($v0)); 279160814Ssimon &shr ($v0,16); 280160814Ssimon &xor ($s[1],&DWP(3,$te,$v1,8)); # s2>>8 281160814Ssimon &movz ($v1,&HB($v0)); 282160814Ssimon &and ($v0,0xFF); 283160814Ssimon &xor ($s[0],&DWP(2,$te,$v0,8)); # s2>>16 284160814Ssimon &mov ($v0,&DWP(8,"esp")); # restore s1 285160814Ssimon &xor ($s[3],&DWP(1,$te,$v1,8)); # s2>>24 286160814Ssimon 287160814Ssimon &mov ($v1,$v0); 288160814Ssimon &and ($v0,0xFF); 289160814Ssimon &xor ($s[1],&DWP(0,$te,$v0,8)); # s1>>0 290160814Ssimon &movz ($v0,&HB($v1)); 291160814Ssimon &shr ($v1,16); 292160814Ssimon &xor ($s[0],&DWP(3,$te,$v0,8)); # s1>>8 293160814Ssimon &movz ($v0,&HB($v1)); 294160814Ssimon &and ($v1,0xFF); 295160814Ssimon &xor ($s[3],&DWP(2,$te,$v1,8)); # s1>>16 296238405Sjkim &mov ($key,$__key); # reincarnate v1 as key 297160814Ssimon &xor ($s[2],&DWP(1,$te,$v0,8)); # s1>>24 298160814Ssimon} 299160814Ssimon 300238405Sjkim# Another experimental routine, which features "horizontal spin," but 301238405Sjkim# eliminates one reference to stack. Strangely enough runs slower... 302238405Sjkimsub enchoriz() 303290207Sjkim{ my ($v0,$v1) = ($key,$acc); 304238405Sjkim 305238405Sjkim &movz ($v0,&LB($s0)); # 3, 2, 1, 0* 306238405Sjkim &rotr ($s2,8); # 8,11,10, 9 307238405Sjkim &mov ($v1,&DWP(0,$te,$v0,8)); # 0 308238405Sjkim &movz ($v0,&HB($s1)); # 7, 6, 5*, 4 309238405Sjkim &rotr ($s3,16); # 13,12,15,14 310238405Sjkim &xor ($v1,&DWP(3,$te,$v0,8)); # 5 311238405Sjkim &movz ($v0,&HB($s2)); # 8,11,10*, 9 312238405Sjkim &rotr ($s0,16); # 1, 0, 3, 2 313238405Sjkim &xor ($v1,&DWP(2,$te,$v0,8)); # 10 314238405Sjkim &movz ($v0,&HB($s3)); # 13,12,15*,14 315238405Sjkim &xor ($v1,&DWP(1,$te,$v0,8)); # 15, t[0] collected 316238405Sjkim &mov ($__s0,$v1); # t[0] saved 317238405Sjkim 318238405Sjkim &movz ($v0,&LB($s1)); # 7, 6, 5, 4* 319238405Sjkim &shr ($s1,16); # -, -, 7, 6 320238405Sjkim &mov ($v1,&DWP(0,$te,$v0,8)); # 4 321238405Sjkim &movz ($v0,&LB($s3)); # 13,12,15,14* 322238405Sjkim &xor ($v1,&DWP(2,$te,$v0,8)); # 14 323238405Sjkim &movz ($v0,&HB($s0)); # 1, 0, 3*, 2 324238405Sjkim &and ($s3,0xffff0000); # 13,12, -, - 325238405Sjkim &xor ($v1,&DWP(1,$te,$v0,8)); # 3 326238405Sjkim &movz ($v0,&LB($s2)); # 8,11,10, 9* 327238405Sjkim &or ($s3,$s1); # 13,12, 7, 6 328238405Sjkim &xor ($v1,&DWP(3,$te,$v0,8)); # 9, t[1] collected 329238405Sjkim &mov ($s1,$v1); # s[1]=t[1] 330238405Sjkim 331238405Sjkim &movz ($v0,&LB($s0)); # 1, 0, 3, 2* 332238405Sjkim &shr ($s2,16); # -, -, 8,11 333238405Sjkim &mov ($v1,&DWP(2,$te,$v0,8)); # 2 334238405Sjkim &movz ($v0,&HB($s3)); # 13,12, 7*, 6 335238405Sjkim &xor ($v1,&DWP(1,$te,$v0,8)); # 7 336238405Sjkim &movz ($v0,&HB($s2)); # -, -, 8*,11 337238405Sjkim &xor ($v1,&DWP(0,$te,$v0,8)); # 8 338238405Sjkim &mov ($v0,$s3); 339238405Sjkim &shr ($v0,24); # 13 340238405Sjkim &xor ($v1,&DWP(3,$te,$v0,8)); # 13, t[2] collected 341238405Sjkim 342238405Sjkim &movz ($v0,&LB($s2)); # -, -, 8,11* 343238405Sjkim &shr ($s0,24); # 1* 344238405Sjkim &mov ($s2,&DWP(1,$te,$v0,8)); # 11 345238405Sjkim &xor ($s2,&DWP(3,$te,$s0,8)); # 1 346238405Sjkim &mov ($s0,$__s0); # s[0]=t[0] 347238405Sjkim &movz ($v0,&LB($s3)); # 13,12, 7, 6* 348238405Sjkim &shr ($s3,16); # , ,13,12 349238405Sjkim &xor ($s2,&DWP(2,$te,$v0,8)); # 6 350238405Sjkim &mov ($key,$__key); # reincarnate v0 as key 351238405Sjkim &and ($s3,0xff); # , ,13,12* 352238405Sjkim &mov ($s3,&DWP(0,$te,$s3,8)); # 12 353238405Sjkim &xor ($s3,$s2); # s[2]=t[3] collected 354238405Sjkim &mov ($s2,$v1); # s[2]=t[2] 355238405Sjkim} 356238405Sjkim 357238405Sjkim# More experimental code... SSE one... Even though this one eliminates 358238405Sjkim# *all* references to stack, it's not faster... 359238405Sjkimsub sse_encbody() 360238405Sjkim{ 361238405Sjkim &movz ($acc,&LB("eax")); # 0 362238405Sjkim &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 0 363238405Sjkim &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2 364238405Sjkim &movz ("edx",&HB("eax")); # 1 365238405Sjkim &mov ("edx",&DWP(3,$tbl,"edx",8)); # 1 366238405Sjkim &shr ("eax",16); # 5, 4 367238405Sjkim 368238405Sjkim &movz ($acc,&LB("ebx")); # 10 369238405Sjkim &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 10 370238405Sjkim &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8 371238405Sjkim &movz ($acc,&HB("ebx")); # 11 372238405Sjkim &xor ("edx",&DWP(1,$tbl,$acc,8)); # 11 373238405Sjkim &shr ("ebx",16); # 15,14 374238405Sjkim 375238405Sjkim &movz ($acc,&HB("eax")); # 5 376238405Sjkim &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 5 377238405Sjkim &movq ("mm3",QWP(16,$key)); 378238405Sjkim &movz ($acc,&HB("ebx")); # 15 379238405Sjkim &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 15 380238405Sjkim &movd ("mm0","ecx"); # t[0] collected 381238405Sjkim 382238405Sjkim &movz ($acc,&LB("eax")); # 4 383238405Sjkim &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 4 384238405Sjkim &movd ("eax","mm2"); # 7, 6, 3, 2 385238405Sjkim &movz ($acc,&LB("ebx")); # 14 386238405Sjkim &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 14 387238405Sjkim &movd ("ebx","mm6"); # 13,12, 9, 8 388238405Sjkim 389238405Sjkim &movz ($acc,&HB("eax")); # 3 390238405Sjkim &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 3 391238405Sjkim &movz ($acc,&HB("ebx")); # 9 392238405Sjkim &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 9 393238405Sjkim &movd ("mm1","ecx"); # t[1] collected 394238405Sjkim 395238405Sjkim &movz ($acc,&LB("eax")); # 2 396238405Sjkim &mov ("ecx",&DWP(2,$tbl,$acc,8)); # 2 397238405Sjkim &shr ("eax",16); # 7, 6 398238405Sjkim &punpckldq ("mm0","mm1"); # t[0,1] collected 399238405Sjkim &movz ($acc,&LB("ebx")); # 8 400238405Sjkim &xor ("ecx",&DWP(0,$tbl,$acc,8)); # 8 401238405Sjkim &shr ("ebx",16); # 13,12 402238405Sjkim 403238405Sjkim &movz ($acc,&HB("eax")); # 7 404238405Sjkim &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 7 405238405Sjkim &pxor ("mm0","mm3"); 406238405Sjkim &movz ("eax",&LB("eax")); # 6 407238405Sjkim &xor ("edx",&DWP(2,$tbl,"eax",8)); # 6 408238405Sjkim &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0 409238405Sjkim &movz ($acc,&HB("ebx")); # 13 410238405Sjkim &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 13 411238405Sjkim &xor ("ecx",&DWP(24,$key)); # t[2] 412238405Sjkim &movd ("mm4","ecx"); # t[2] collected 413238405Sjkim &movz ("ebx",&LB("ebx")); # 12 414238405Sjkim &xor ("edx",&DWP(0,$tbl,"ebx",8)); # 12 415238405Sjkim &shr ("ecx",16); 416238405Sjkim &movd ("eax","mm1"); # 5, 4, 1, 0 417238405Sjkim &mov ("ebx",&DWP(28,$key)); # t[3] 418238405Sjkim &xor ("ebx","edx"); 419238405Sjkim &movd ("mm5","ebx"); # t[3] collected 420238405Sjkim &and ("ebx",0xffff0000); 421238405Sjkim &or ("ebx","ecx"); 422238405Sjkim 423238405Sjkim &punpckldq ("mm4","mm5"); # t[2,3] collected 424238405Sjkim} 425238405Sjkim 426238405Sjkim###################################################################### 427238405Sjkim# "Compact" block function 428238405Sjkim###################################################################### 429238405Sjkim 430238405Sjkimsub enccompact() 431290207Sjkim{ my $Fn = \&mov; 432238405Sjkim while ($#_>5) { pop(@_); $Fn=sub{}; } 433238405Sjkim my ($i,$te,@s)=@_; 434238405Sjkim my $tmp = $key; 435238405Sjkim my $out = $i==3?$s[0]:$acc; 436238405Sjkim 437238405Sjkim # $Fn is used in first compact round and its purpose is to 438238405Sjkim # void restoration of some values from stack, so that after 439238405Sjkim # 4xenccompact with extra argument $key value is left there... 440238405Sjkim if ($i==3) { &$Fn ($key,$__key); }##%edx 441238405Sjkim else { &mov ($out,$s[0]); } 442238405Sjkim &and ($out,0xFF); 443238405Sjkim if ($i==1) { &shr ($s[0],16); }#%ebx[1] 444238405Sjkim if ($i==2) { &shr ($s[0],24); }#%ecx[2] 445238405Sjkim &movz ($out,&BP(-128,$te,$out,1)); 446238405Sjkim 447238405Sjkim if ($i==3) { $tmp=$s[1]; }##%eax 448238405Sjkim &movz ($tmp,&HB($s[1])); 449238405Sjkim &movz ($tmp,&BP(-128,$te,$tmp,1)); 450238405Sjkim &shl ($tmp,8); 451238405Sjkim &xor ($out,$tmp); 452238405Sjkim 453238405Sjkim if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx 454238405Sjkim else { &mov ($tmp,$s[2]); 455238405Sjkim &shr ($tmp,16); } 456238405Sjkim if ($i==2) { &and ($s[1],0xFF); }#%edx[2] 457238405Sjkim &and ($tmp,0xFF); 458238405Sjkim &movz ($tmp,&BP(-128,$te,$tmp,1)); 459238405Sjkim &shl ($tmp,16); 460238405Sjkim &xor ($out,$tmp); 461238405Sjkim 462238405Sjkim if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx 463238405Sjkim elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2] 464238405Sjkim else { &mov ($tmp,$s[3]); 465238405Sjkim &shr ($tmp,24); } 466238405Sjkim &movz ($tmp,&BP(-128,$te,$tmp,1)); 467238405Sjkim &shl ($tmp,24); 468238405Sjkim &xor ($out,$tmp); 469238405Sjkim if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 470238405Sjkim if ($i==3) { &mov ($s[3],$acc); } 471238405Sjkim &comment(); 472238405Sjkim} 473238405Sjkim 474238405Sjkimsub enctransform() 475238405Sjkim{ my @s = ($s0,$s1,$s2,$s3); 476238405Sjkim my $i = shift; 477238405Sjkim my $tmp = $tbl; 478238405Sjkim my $r2 = $key ; 479238405Sjkim 480290207Sjkim &and ($tmp,$s[$i]); 481290207Sjkim &lea ($r2,&DWP(0,$s[$i],$s[$i])); 482290207Sjkim &mov ($acc,$tmp); 483238405Sjkim &shr ($tmp,7); 484290207Sjkim &and ($r2,0xfefefefe); 485238405Sjkim &sub ($acc,$tmp); 486290207Sjkim &mov ($tmp,$s[$i]); 487238405Sjkim &and ($acc,0x1b1b1b1b); 488290207Sjkim &rotr ($tmp,16); 489238405Sjkim &xor ($acc,$r2); # r2 490290207Sjkim &mov ($r2,$s[$i]); 491238405Sjkim 492238405Sjkim &xor ($s[$i],$acc); # r0 ^ r2 493290207Sjkim &rotr ($r2,16+8); 494290207Sjkim &xor ($acc,$tmp); 495238405Sjkim &rotl ($s[$i],24); 496290207Sjkim &xor ($acc,$r2); 497290207Sjkim &mov ($tmp,0x80808080) if ($i!=1); 498290207Sjkim &xor ($s[$i],$acc); # ROTATE(r2^r0,24) ^ r2 499238405Sjkim} 500238405Sjkim 501238405Sjkim&function_begin_B("_x86_AES_encrypt_compact"); 502238405Sjkim # note that caller is expected to allocate stack frame for me! 503238405Sjkim &mov ($__key,$key); # save key 504238405Sjkim 505238405Sjkim &xor ($s0,&DWP(0,$key)); # xor with key 506238405Sjkim &xor ($s1,&DWP(4,$key)); 507238405Sjkim &xor ($s2,&DWP(8,$key)); 508238405Sjkim &xor ($s3,&DWP(12,$key)); 509238405Sjkim 510238405Sjkim &mov ($acc,&DWP(240,$key)); # load key->rounds 511238405Sjkim &lea ($acc,&DWP(-2,$acc,$acc)); 512238405Sjkim &lea ($acc,&DWP(0,$key,$acc,8)); 513238405Sjkim &mov ($__end,$acc); # end of key schedule 514238405Sjkim 515238405Sjkim # prefetch Te4 516238405Sjkim &mov ($key,&DWP(0-128,$tbl)); 517238405Sjkim &mov ($acc,&DWP(32-128,$tbl)); 518238405Sjkim &mov ($key,&DWP(64-128,$tbl)); 519238405Sjkim &mov ($acc,&DWP(96-128,$tbl)); 520238405Sjkim &mov ($key,&DWP(128-128,$tbl)); 521238405Sjkim &mov ($acc,&DWP(160-128,$tbl)); 522238405Sjkim &mov ($key,&DWP(192-128,$tbl)); 523238405Sjkim &mov ($acc,&DWP(224-128,$tbl)); 524238405Sjkim 525238405Sjkim &set_label("loop",16); 526238405Sjkim 527238405Sjkim &enccompact(0,$tbl,$s0,$s1,$s2,$s3,1); 528238405Sjkim &enccompact(1,$tbl,$s1,$s2,$s3,$s0,1); 529238405Sjkim &enccompact(2,$tbl,$s2,$s3,$s0,$s1,1); 530238405Sjkim &enccompact(3,$tbl,$s3,$s0,$s1,$s2,1); 531290207Sjkim &mov ($tbl,0x80808080); 532238405Sjkim &enctransform(2); 533238405Sjkim &enctransform(3); 534238405Sjkim &enctransform(0); 535238405Sjkim &enctransform(1); 536238405Sjkim &mov ($key,$__key); 537238405Sjkim &mov ($tbl,$__tbl); 538238405Sjkim &add ($key,16); # advance rd_key 539238405Sjkim &xor ($s0,&DWP(0,$key)); 540238405Sjkim &xor ($s1,&DWP(4,$key)); 541238405Sjkim &xor ($s2,&DWP(8,$key)); 542238405Sjkim &xor ($s3,&DWP(12,$key)); 543238405Sjkim 544238405Sjkim &cmp ($key,$__end); 545238405Sjkim &mov ($__key,$key); 546238405Sjkim &jb (&label("loop")); 547238405Sjkim 548238405Sjkim &enccompact(0,$tbl,$s0,$s1,$s2,$s3); 549238405Sjkim &enccompact(1,$tbl,$s1,$s2,$s3,$s0); 550238405Sjkim &enccompact(2,$tbl,$s2,$s3,$s0,$s1); 551238405Sjkim &enccompact(3,$tbl,$s3,$s0,$s1,$s2); 552238405Sjkim 553238405Sjkim &xor ($s0,&DWP(16,$key)); 554238405Sjkim &xor ($s1,&DWP(20,$key)); 555238405Sjkim &xor ($s2,&DWP(24,$key)); 556238405Sjkim &xor ($s3,&DWP(28,$key)); 557238405Sjkim 558238405Sjkim &ret (); 559238405Sjkim&function_end_B("_x86_AES_encrypt_compact"); 560238405Sjkim 561238405Sjkim###################################################################### 562238405Sjkim# "Compact" SSE block function. 563238405Sjkim###################################################################### 564238405Sjkim# 565238405Sjkim# Performance is not actually extraordinary in comparison to pure 566238405Sjkim# x86 code. In particular encrypt performance is virtually the same. 567238405Sjkim# Decrypt performance on the other hand is 15-20% better on newer 568291719Sjkim# ��-archs [but we're thankful for *any* improvement here], and ~50% 569238405Sjkim# better on PIII:-) And additionally on the pros side this code 570238405Sjkim# eliminates redundant references to stack and thus relieves/ 571238405Sjkim# minimizes the pressure on the memory bus. 572238405Sjkim# 573238405Sjkim# MMX register layout lsb 574238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 575238405Sjkim# | mm4 | mm0 | 576238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 577238405Sjkim# | s3 | s2 | s1 | s0 | 578238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 579238405Sjkim# |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| 580238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 581238405Sjkim# 582238405Sjkim# Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8. 583238405Sjkim# In this terms encryption and decryption "compact" permutation 584238405Sjkim# matrices can be depicted as following: 585238405Sjkim# 586238405Sjkim# encryption lsb # decryption lsb 587238405Sjkim# +----++----+----+----+----+ # +----++----+----+----+----+ 588238405Sjkim# | t0 || 15 | 10 | 5 | 0 | # | t0 || 7 | 10 | 13 | 0 | 589238405Sjkim# +----++----+----+----+----+ # +----++----+----+----+----+ 590238405Sjkim# | t1 || 3 | 14 | 9 | 4 | # | t1 || 11 | 14 | 1 | 4 | 591238405Sjkim# +----++----+----+----+----+ # +----++----+----+----+----+ 592238405Sjkim# | t2 || 7 | 2 | 13 | 8 | # | t2 || 15 | 2 | 5 | 8 | 593238405Sjkim# +----++----+----+----+----+ # +----++----+----+----+----+ 594238405Sjkim# | t3 || 11 | 6 | 1 | 12 | # | t3 || 3 | 6 | 9 | 12 | 595238405Sjkim# +----++----+----+----+----+ # +----++----+----+----+----+ 596238405Sjkim# 597238405Sjkim###################################################################### 598238405Sjkim# Why not xmm registers? Short answer. It was actually tested and 599238405Sjkim# was not any faster, but *contrary*, most notably on Intel CPUs. 600238405Sjkim# Longer answer. Main advantage of using mm registers is that movd 601238405Sjkim# latency is lower, especially on Intel P4. While arithmetic 602238405Sjkim# instructions are twice as many, they can be scheduled every cycle 603238405Sjkim# and not every second one when they are operating on xmm register, 604238405Sjkim# so that "arithmetic throughput" remains virtually the same. And 605238405Sjkim# finally the code can be executed even on elder SSE-only CPUs:-) 606238405Sjkim 607238405Sjkimsub sse_enccompact() 608238405Sjkim{ 609238405Sjkim &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0 610238405Sjkim &pshufw ("mm5","mm4",0x0d); # 15,14,11,10 611238405Sjkim &movd ("eax","mm1"); # 5, 4, 1, 0 612238405Sjkim &movd ("ebx","mm5"); # 15,14,11,10 613290207Sjkim &mov ($__key,$key); 614238405Sjkim 615238405Sjkim &movz ($acc,&LB("eax")); # 0 616290207Sjkim &movz ("edx",&HB("eax")); # 1 617290207Sjkim &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2 618238405Sjkim &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0 619290207Sjkim &movz ($key,&LB("ebx")); # 10 620238405Sjkim &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1 621290207Sjkim &shr ("eax",16); # 5, 4 622238405Sjkim &shl ("edx",8); # 1 623238405Sjkim 624290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 10 625290207Sjkim &movz ($key,&HB("ebx")); # 11 626238405Sjkim &shl ($acc,16); # 10 627290207Sjkim &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8 628238405Sjkim &or ("ecx",$acc); # 10 629290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 11 630290207Sjkim &movz ($key,&HB("eax")); # 5 631238405Sjkim &shl ($acc,24); # 11 632290207Sjkim &shr ("ebx",16); # 15,14 633238405Sjkim &or ("edx",$acc); # 11 634238405Sjkim 635290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 5 636290207Sjkim &movz ($key,&HB("ebx")); # 15 637238405Sjkim &shl ($acc,8); # 5 638238405Sjkim &or ("ecx",$acc); # 5 639290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 15 640290207Sjkim &movz ($key,&LB("eax")); # 4 641238405Sjkim &shl ($acc,24); # 15 642238405Sjkim &or ("ecx",$acc); # 15 643238405Sjkim 644290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 4 645290207Sjkim &movz ($key,&LB("ebx")); # 14 646238405Sjkim &movd ("eax","mm2"); # 7, 6, 3, 2 647290207Sjkim &movd ("mm0","ecx"); # t[0] collected 648290207Sjkim &movz ("ecx",&BP(-128,$tbl,$key,1)); # 14 649290207Sjkim &movz ($key,&HB("eax")); # 3 650290207Sjkim &shl ("ecx",16); # 14 651290207Sjkim &movd ("ebx","mm6"); # 13,12, 9, 8 652238405Sjkim &or ("ecx",$acc); # 14 653238405Sjkim 654290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 3 655290207Sjkim &movz ($key,&HB("ebx")); # 9 656238405Sjkim &shl ($acc,24); # 3 657238405Sjkim &or ("ecx",$acc); # 3 658290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 9 659290207Sjkim &movz ($key,&LB("ebx")); # 8 660238405Sjkim &shl ($acc,8); # 9 661290207Sjkim &shr ("ebx",16); # 13,12 662238405Sjkim &or ("ecx",$acc); # 9 663290207Sjkim 664290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 8 665290207Sjkim &movz ($key,&LB("eax")); # 2 666290207Sjkim &shr ("eax",16); # 7, 6 667238405Sjkim &movd ("mm1","ecx"); # t[1] collected 668290207Sjkim &movz ("ecx",&BP(-128,$tbl,$key,1)); # 2 669290207Sjkim &movz ($key,&HB("eax")); # 7 670290207Sjkim &shl ("ecx",16); # 2 671290207Sjkim &and ("eax",0xff); # 6 672238405Sjkim &or ("ecx",$acc); # 2 673238405Sjkim 674238405Sjkim &punpckldq ("mm0","mm1"); # t[0,1] collected 675238405Sjkim 676290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 7 677290207Sjkim &movz ($key,&HB("ebx")); # 13 678238405Sjkim &shl ($acc,24); # 7 679290207Sjkim &and ("ebx",0xff); # 12 680290207Sjkim &movz ("eax",&BP(-128,$tbl,"eax",1)); # 6 681238405Sjkim &or ("ecx",$acc); # 7 682238405Sjkim &shl ("eax",16); # 6 683290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 13 684238405Sjkim &or ("edx","eax"); # 6 685238405Sjkim &shl ($acc,8); # 13 686290207Sjkim &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 12 687238405Sjkim &or ("ecx",$acc); # 13 688290207Sjkim &or ("edx","ebx"); # 12 689290207Sjkim &mov ($key,$__key); 690238405Sjkim &movd ("mm4","ecx"); # t[2] collected 691238405Sjkim &movd ("mm5","edx"); # t[3] collected 692238405Sjkim 693238405Sjkim &punpckldq ("mm4","mm5"); # t[2,3] collected 694238405Sjkim} 695238405Sjkim 696238405Sjkim if (!$x86only) { 697238405Sjkim&function_begin_B("_sse_AES_encrypt_compact"); 698238405Sjkim &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0 699238405Sjkim &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8 700238405Sjkim 701238405Sjkim # note that caller is expected to allocate stack frame for me! 702238405Sjkim &mov ($acc,&DWP(240,$key)); # load key->rounds 703238405Sjkim &lea ($acc,&DWP(-2,$acc,$acc)); 704238405Sjkim &lea ($acc,&DWP(0,$key,$acc,8)); 705238405Sjkim &mov ($__end,$acc); # end of key schedule 706238405Sjkim 707238405Sjkim &mov ($s0,0x1b1b1b1b); # magic constant 708238405Sjkim &mov (&DWP(8,"esp"),$s0); 709238405Sjkim &mov (&DWP(12,"esp"),$s0); 710238405Sjkim 711238405Sjkim # prefetch Te4 712238405Sjkim &mov ($s0,&DWP(0-128,$tbl)); 713238405Sjkim &mov ($s1,&DWP(32-128,$tbl)); 714238405Sjkim &mov ($s2,&DWP(64-128,$tbl)); 715238405Sjkim &mov ($s3,&DWP(96-128,$tbl)); 716238405Sjkim &mov ($s0,&DWP(128-128,$tbl)); 717238405Sjkim &mov ($s1,&DWP(160-128,$tbl)); 718238405Sjkim &mov ($s2,&DWP(192-128,$tbl)); 719238405Sjkim &mov ($s3,&DWP(224-128,$tbl)); 720238405Sjkim 721238405Sjkim &set_label("loop",16); 722238405Sjkim &sse_enccompact(); 723238405Sjkim &add ($key,16); 724238405Sjkim &cmp ($key,$__end); 725238405Sjkim &ja (&label("out")); 726238405Sjkim 727238405Sjkim &movq ("mm2",&QWP(8,"esp")); 728238405Sjkim &pxor ("mm3","mm3"); &pxor ("mm7","mm7"); 729238405Sjkim &movq ("mm1","mm0"); &movq ("mm5","mm4"); # r0 730238405Sjkim &pcmpgtb("mm3","mm0"); &pcmpgtb("mm7","mm4"); 731238405Sjkim &pand ("mm3","mm2"); &pand ("mm7","mm2"); 732238405Sjkim &pshufw ("mm2","mm0",0xb1); &pshufw ("mm6","mm4",0xb1);# ROTATE(r0,16) 733238405Sjkim &paddb ("mm0","mm0"); &paddb ("mm4","mm4"); 734238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # = r2 735238405Sjkim &pshufw ("mm3","mm2",0xb1); &pshufw ("mm7","mm6",0xb1);# r0 736238405Sjkim &pxor ("mm1","mm0"); &pxor ("mm5","mm4"); # r0^r2 737238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(r0,16) 738238405Sjkim 739238405Sjkim &movq ("mm2","mm3"); &movq ("mm6","mm7"); 740238405Sjkim &pslld ("mm3",8); &pslld ("mm7",8); 741238405Sjkim &psrld ("mm2",24); &psrld ("mm6",24); 742238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= r0<<8 743238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= r0>>24 744238405Sjkim 745238405Sjkim &movq ("mm3","mm1"); &movq ("mm7","mm5"); 746238405Sjkim &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key)); 747238405Sjkim &psrld ("mm1",8); &psrld ("mm5",8); 748238405Sjkim &mov ($s0,&DWP(0-128,$tbl)); 749238405Sjkim &pslld ("mm3",24); &pslld ("mm7",24); 750238405Sjkim &mov ($s1,&DWP(64-128,$tbl)); 751238405Sjkim &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= (r2^r0)<<8 752238405Sjkim &mov ($s2,&DWP(128-128,$tbl)); 753238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= (r2^r0)>>24 754238405Sjkim &mov ($s3,&DWP(192-128,$tbl)); 755238405Sjkim 756238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); 757238405Sjkim &jmp (&label("loop")); 758238405Sjkim 759238405Sjkim &set_label("out",16); 760238405Sjkim &pxor ("mm0",&QWP(0,$key)); 761238405Sjkim &pxor ("mm4",&QWP(8,$key)); 762238405Sjkim 763238405Sjkim &ret (); 764238405Sjkim&function_end_B("_sse_AES_encrypt_compact"); 765238405Sjkim } 766238405Sjkim 767238405Sjkim###################################################################### 768238405Sjkim# Vanilla block function. 769238405Sjkim###################################################################### 770238405Sjkim 771160814Ssimonsub encstep() 772160814Ssimon{ my ($i,$te,@s) = @_; 773160814Ssimon my $tmp = $key; 774160814Ssimon my $out = $i==3?$s[0]:$acc; 775160814Ssimon 776160814Ssimon # lines marked with #%e?x[i] denote "reordered" instructions... 777238405Sjkim if ($i==3) { &mov ($key,$__key); }##%edx 778160814Ssimon else { &mov ($out,$s[0]); 779160814Ssimon &and ($out,0xFF); } 780160814Ssimon if ($i==1) { &shr ($s[0],16); }#%ebx[1] 781160814Ssimon if ($i==2) { &shr ($s[0],24); }#%ecx[2] 782160814Ssimon &mov ($out,&DWP(0,$te,$out,8)); 783160814Ssimon 784160814Ssimon if ($i==3) { $tmp=$s[1]; }##%eax 785160814Ssimon &movz ($tmp,&HB($s[1])); 786160814Ssimon &xor ($out,&DWP(3,$te,$tmp,8)); 787160814Ssimon 788238405Sjkim if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx 789160814Ssimon else { &mov ($tmp,$s[2]); 790160814Ssimon &shr ($tmp,16); } 791160814Ssimon if ($i==2) { &and ($s[1],0xFF); }#%edx[2] 792160814Ssimon &and ($tmp,0xFF); 793160814Ssimon &xor ($out,&DWP(2,$te,$tmp,8)); 794160814Ssimon 795238405Sjkim if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx 796160814Ssimon elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2] 797160814Ssimon else { &mov ($tmp,$s[3]); 798160814Ssimon &shr ($tmp,24) } 799160814Ssimon &xor ($out,&DWP(1,$te,$tmp,8)); 800160814Ssimon if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 801160814Ssimon if ($i==3) { &mov ($s[3],$acc); } 802160814Ssimon &comment(); 803160814Ssimon} 804160814Ssimon 805160814Ssimonsub enclast() 806160814Ssimon{ my ($i,$te,@s)=@_; 807160814Ssimon my $tmp = $key; 808160814Ssimon my $out = $i==3?$s[0]:$acc; 809160814Ssimon 810238405Sjkim if ($i==3) { &mov ($key,$__key); }##%edx 811160814Ssimon else { &mov ($out,$s[0]); } 812160814Ssimon &and ($out,0xFF); 813160814Ssimon if ($i==1) { &shr ($s[0],16); }#%ebx[1] 814160814Ssimon if ($i==2) { &shr ($s[0],24); }#%ecx[2] 815160814Ssimon &mov ($out,&DWP(2,$te,$out,8)); 816160814Ssimon &and ($out,0x000000ff); 817160814Ssimon 818160814Ssimon if ($i==3) { $tmp=$s[1]; }##%eax 819160814Ssimon &movz ($tmp,&HB($s[1])); 820160814Ssimon &mov ($tmp,&DWP(0,$te,$tmp,8)); 821160814Ssimon &and ($tmp,0x0000ff00); 822160814Ssimon &xor ($out,$tmp); 823160814Ssimon 824238405Sjkim if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx 825238405Sjkim else { &mov ($tmp,$s[2]); 826160814Ssimon &shr ($tmp,16); } 827160814Ssimon if ($i==2) { &and ($s[1],0xFF); }#%edx[2] 828160814Ssimon &and ($tmp,0xFF); 829160814Ssimon &mov ($tmp,&DWP(0,$te,$tmp,8)); 830160814Ssimon &and ($tmp,0x00ff0000); 831160814Ssimon &xor ($out,$tmp); 832160814Ssimon 833238405Sjkim if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx 834160814Ssimon elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2] 835160814Ssimon else { &mov ($tmp,$s[3]); 836160814Ssimon &shr ($tmp,24); } 837160814Ssimon &mov ($tmp,&DWP(2,$te,$tmp,8)); 838160814Ssimon &and ($tmp,0xff000000); 839160814Ssimon &xor ($out,$tmp); 840160814Ssimon if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 841160814Ssimon if ($i==3) { &mov ($s[3],$acc); } 842160814Ssimon} 843160814Ssimon 844160814Ssimon&function_begin_B("_x86_AES_encrypt"); 845160814Ssimon if ($vertical_spin) { 846160814Ssimon # I need high parts of volatile registers to be accessible... 847160814Ssimon &exch ($s1="edi",$key="ebx"); 848160814Ssimon &mov ($s2="esi",$acc="ecx"); 849160814Ssimon } 850160814Ssimon 851160814Ssimon # note that caller is expected to allocate stack frame for me! 852238405Sjkim &mov ($__key,$key); # save key 853160814Ssimon 854160814Ssimon &xor ($s0,&DWP(0,$key)); # xor with key 855160814Ssimon &xor ($s1,&DWP(4,$key)); 856160814Ssimon &xor ($s2,&DWP(8,$key)); 857160814Ssimon &xor ($s3,&DWP(12,$key)); 858160814Ssimon 859160814Ssimon &mov ($acc,&DWP(240,$key)); # load key->rounds 860160814Ssimon 861160814Ssimon if ($small_footprint) { 862160814Ssimon &lea ($acc,&DWP(-2,$acc,$acc)); 863160814Ssimon &lea ($acc,&DWP(0,$key,$acc,8)); 864238405Sjkim &mov ($__end,$acc); # end of key schedule 865238405Sjkim 866238405Sjkim &set_label("loop",16); 867160814Ssimon if ($vertical_spin) { 868238405Sjkim &encvert($tbl,$s0,$s1,$s2,$s3); 869160814Ssimon } else { 870238405Sjkim &encstep(0,$tbl,$s0,$s1,$s2,$s3); 871238405Sjkim &encstep(1,$tbl,$s1,$s2,$s3,$s0); 872238405Sjkim &encstep(2,$tbl,$s2,$s3,$s0,$s1); 873238405Sjkim &encstep(3,$tbl,$s3,$s0,$s1,$s2); 874160814Ssimon } 875160814Ssimon &add ($key,16); # advance rd_key 876160814Ssimon &xor ($s0,&DWP(0,$key)); 877160814Ssimon &xor ($s1,&DWP(4,$key)); 878160814Ssimon &xor ($s2,&DWP(8,$key)); 879160814Ssimon &xor ($s3,&DWP(12,$key)); 880238405Sjkim &cmp ($key,$__end); 881238405Sjkim &mov ($__key,$key); 882160814Ssimon &jb (&label("loop")); 883160814Ssimon } 884160814Ssimon else { 885160814Ssimon &cmp ($acc,10); 886160814Ssimon &jle (&label("10rounds")); 887160814Ssimon &cmp ($acc,12); 888160814Ssimon &jle (&label("12rounds")); 889160814Ssimon 890238405Sjkim &set_label("14rounds",4); 891160814Ssimon for ($i=1;$i<3;$i++) { 892160814Ssimon if ($vertical_spin) { 893238405Sjkim &encvert($tbl,$s0,$s1,$s2,$s3); 894160814Ssimon } else { 895238405Sjkim &encstep(0,$tbl,$s0,$s1,$s2,$s3); 896238405Sjkim &encstep(1,$tbl,$s1,$s2,$s3,$s0); 897238405Sjkim &encstep(2,$tbl,$s2,$s3,$s0,$s1); 898238405Sjkim &encstep(3,$tbl,$s3,$s0,$s1,$s2); 899160814Ssimon } 900160814Ssimon &xor ($s0,&DWP(16*$i+0,$key)); 901160814Ssimon &xor ($s1,&DWP(16*$i+4,$key)); 902160814Ssimon &xor ($s2,&DWP(16*$i+8,$key)); 903160814Ssimon &xor ($s3,&DWP(16*$i+12,$key)); 904160814Ssimon } 905160814Ssimon &add ($key,32); 906238405Sjkim &mov ($__key,$key); # advance rd_key 907238405Sjkim &set_label("12rounds",4); 908160814Ssimon for ($i=1;$i<3;$i++) { 909160814Ssimon if ($vertical_spin) { 910238405Sjkim &encvert($tbl,$s0,$s1,$s2,$s3); 911160814Ssimon } else { 912238405Sjkim &encstep(0,$tbl,$s0,$s1,$s2,$s3); 913238405Sjkim &encstep(1,$tbl,$s1,$s2,$s3,$s0); 914238405Sjkim &encstep(2,$tbl,$s2,$s3,$s0,$s1); 915238405Sjkim &encstep(3,$tbl,$s3,$s0,$s1,$s2); 916160814Ssimon } 917160814Ssimon &xor ($s0,&DWP(16*$i+0,$key)); 918160814Ssimon &xor ($s1,&DWP(16*$i+4,$key)); 919160814Ssimon &xor ($s2,&DWP(16*$i+8,$key)); 920160814Ssimon &xor ($s3,&DWP(16*$i+12,$key)); 921160814Ssimon } 922160814Ssimon &add ($key,32); 923238405Sjkim &mov ($__key,$key); # advance rd_key 924238405Sjkim &set_label("10rounds",4); 925160814Ssimon for ($i=1;$i<10;$i++) { 926160814Ssimon if ($vertical_spin) { 927238405Sjkim &encvert($tbl,$s0,$s1,$s2,$s3); 928160814Ssimon } else { 929238405Sjkim &encstep(0,$tbl,$s0,$s1,$s2,$s3); 930238405Sjkim &encstep(1,$tbl,$s1,$s2,$s3,$s0); 931238405Sjkim &encstep(2,$tbl,$s2,$s3,$s0,$s1); 932238405Sjkim &encstep(3,$tbl,$s3,$s0,$s1,$s2); 933160814Ssimon } 934160814Ssimon &xor ($s0,&DWP(16*$i+0,$key)); 935160814Ssimon &xor ($s1,&DWP(16*$i+4,$key)); 936160814Ssimon &xor ($s2,&DWP(16*$i+8,$key)); 937160814Ssimon &xor ($s3,&DWP(16*$i+12,$key)); 938160814Ssimon } 939160814Ssimon } 940160814Ssimon 941160814Ssimon if ($vertical_spin) { 942160814Ssimon # "reincarnate" some registers for "horizontal" spin... 943160814Ssimon &mov ($s1="ebx",$key="edi"); 944160814Ssimon &mov ($s2="ecx",$acc="esi"); 945160814Ssimon } 946238405Sjkim &enclast(0,$tbl,$s0,$s1,$s2,$s3); 947238405Sjkim &enclast(1,$tbl,$s1,$s2,$s3,$s0); 948238405Sjkim &enclast(2,$tbl,$s2,$s3,$s0,$s1); 949238405Sjkim &enclast(3,$tbl,$s3,$s0,$s1,$s2); 950160814Ssimon 951160814Ssimon &add ($key,$small_footprint?16:160); 952160814Ssimon &xor ($s0,&DWP(0,$key)); 953160814Ssimon &xor ($s1,&DWP(4,$key)); 954160814Ssimon &xor ($s2,&DWP(8,$key)); 955160814Ssimon &xor ($s3,&DWP(12,$key)); 956160814Ssimon 957160814Ssimon &ret (); 958160814Ssimon 959160814Ssimon&set_label("AES_Te",64); # Yes! I keep it in the code segment! 960160814Ssimon &_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6); 961160814Ssimon &_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591); 962160814Ssimon &_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56); 963160814Ssimon &_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec); 964160814Ssimon &_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa); 965160814Ssimon &_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb); 966160814Ssimon &_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45); 967160814Ssimon &_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b); 968160814Ssimon &_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c); 969160814Ssimon &_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83); 970160814Ssimon &_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9); 971160814Ssimon &_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a); 972160814Ssimon &_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d); 973160814Ssimon &_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f); 974160814Ssimon &_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df); 975160814Ssimon &_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea); 976160814Ssimon &_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34); 977160814Ssimon &_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b); 978160814Ssimon &_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d); 979160814Ssimon &_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413); 980160814Ssimon &_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1); 981160814Ssimon &_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6); 982160814Ssimon &_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972); 983160814Ssimon &_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85); 984160814Ssimon &_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed); 985160814Ssimon &_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511); 986160814Ssimon &_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe); 987160814Ssimon &_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b); 988160814Ssimon &_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05); 989160814Ssimon &_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1); 990160814Ssimon &_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142); 991160814Ssimon &_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf); 992160814Ssimon &_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3); 993160814Ssimon &_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e); 994160814Ssimon &_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a); 995160814Ssimon &_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6); 996160814Ssimon &_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3); 997160814Ssimon &_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b); 998160814Ssimon &_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428); 999160814Ssimon &_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad); 1000160814Ssimon &_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14); 1001160814Ssimon &_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8); 1002160814Ssimon &_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4); 1003160814Ssimon &_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2); 1004160814Ssimon &_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda); 1005160814Ssimon &_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949); 1006160814Ssimon &_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf); 1007160814Ssimon &_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810); 1008160814Ssimon &_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c); 1009160814Ssimon &_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697); 1010160814Ssimon &_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e); 1011160814Ssimon &_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f); 1012160814Ssimon &_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc); 1013160814Ssimon &_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c); 1014160814Ssimon &_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969); 1015160814Ssimon &_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27); 1016160814Ssimon &_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122); 1017160814Ssimon &_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433); 1018160814Ssimon &_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9); 1019160814Ssimon &_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5); 1020160814Ssimon &_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a); 1021160814Ssimon &_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0); 1022160814Ssimon &_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e); 1023160814Ssimon &_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c); 1024238405Sjkim 1025238405Sjkim#Te4 # four copies of Te4 to choose from to avoid L1 aliasing 1026238405Sjkim &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1027238405Sjkim &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1028238405Sjkim &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1029238405Sjkim &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1030238405Sjkim &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1031238405Sjkim &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1032238405Sjkim &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1033238405Sjkim &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1034238405Sjkim &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1035238405Sjkim &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1036238405Sjkim &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1037238405Sjkim &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1038238405Sjkim &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1039238405Sjkim &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1040238405Sjkim &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1041238405Sjkim &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1042238405Sjkim &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1043238405Sjkim &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1044238405Sjkim &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1045238405Sjkim &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1046238405Sjkim &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1047238405Sjkim &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1048238405Sjkim &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1049238405Sjkim &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1050238405Sjkim &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1051238405Sjkim &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1052238405Sjkim &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1053238405Sjkim &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1054238405Sjkim &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1055238405Sjkim &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1056238405Sjkim &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1057238405Sjkim &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1058238405Sjkim 1059238405Sjkim &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1060238405Sjkim &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1061238405Sjkim &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1062238405Sjkim &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1063238405Sjkim &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1064238405Sjkim &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1065238405Sjkim &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1066238405Sjkim &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1067238405Sjkim &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1068238405Sjkim &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1069238405Sjkim &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1070238405Sjkim &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1071238405Sjkim &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1072238405Sjkim &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1073238405Sjkim &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1074238405Sjkim &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1075238405Sjkim &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1076238405Sjkim &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1077238405Sjkim &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1078238405Sjkim &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1079238405Sjkim &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1080238405Sjkim &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1081238405Sjkim &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1082238405Sjkim &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1083238405Sjkim &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1084238405Sjkim &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1085238405Sjkim &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1086238405Sjkim &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1087238405Sjkim &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1088238405Sjkim &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1089238405Sjkim &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1090238405Sjkim &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1091238405Sjkim 1092238405Sjkim &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1093238405Sjkim &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1094238405Sjkim &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1095238405Sjkim &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1096238405Sjkim &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1097238405Sjkim &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1098238405Sjkim &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1099238405Sjkim &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1100238405Sjkim &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1101238405Sjkim &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1102238405Sjkim &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1103238405Sjkim &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1104238405Sjkim &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1105238405Sjkim &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1106238405Sjkim &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1107238405Sjkim &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1108238405Sjkim &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1109238405Sjkim &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1110238405Sjkim &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1111238405Sjkim &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1112238405Sjkim &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1113238405Sjkim &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1114238405Sjkim &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1115238405Sjkim &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1116238405Sjkim &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1117238405Sjkim &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1118238405Sjkim &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1119238405Sjkim &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1120238405Sjkim &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1121238405Sjkim &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1122238405Sjkim &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1123238405Sjkim &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1124238405Sjkim 1125238405Sjkim &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5); 1126238405Sjkim &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76); 1127238405Sjkim &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0); 1128238405Sjkim &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0); 1129238405Sjkim &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc); 1130238405Sjkim &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15); 1131238405Sjkim &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a); 1132238405Sjkim &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75); 1133238405Sjkim &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0); 1134238405Sjkim &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84); 1135238405Sjkim &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b); 1136238405Sjkim &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf); 1137238405Sjkim &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85); 1138238405Sjkim &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8); 1139238405Sjkim &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5); 1140238405Sjkim &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2); 1141238405Sjkim &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17); 1142238405Sjkim &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73); 1143238405Sjkim &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88); 1144238405Sjkim &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb); 1145238405Sjkim &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c); 1146238405Sjkim &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79); 1147238405Sjkim &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9); 1148238405Sjkim &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08); 1149238405Sjkim &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6); 1150238405Sjkim &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a); 1151238405Sjkim &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e); 1152238405Sjkim &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e); 1153238405Sjkim &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94); 1154238405Sjkim &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf); 1155238405Sjkim &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68); 1156238405Sjkim &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); 1157160814Ssimon#rcon: 1158160814Ssimon &data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008); 1159160814Ssimon &data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080); 1160238405Sjkim &data_word(0x0000001b, 0x00000036, 0x00000000, 0x00000000); 1161238405Sjkim &data_word(0x00000000, 0x00000000, 0x00000000, 0x00000000); 1162160814Ssimon&function_end_B("_x86_AES_encrypt"); 1163160814Ssimon 1164160814Ssimon# void AES_encrypt (const void *inp,void *out,const AES_KEY *key); 1165160814Ssimon&function_begin("AES_encrypt"); 1166160814Ssimon &mov ($acc,&wparam(0)); # load inp 1167160814Ssimon &mov ($key,&wparam(2)); # load key 1168160814Ssimon 1169160814Ssimon &mov ($s0,"esp"); 1170238405Sjkim &sub ("esp",36); 1171238405Sjkim &and ("esp",-64); # align to cache-line 1172160814Ssimon 1173238405Sjkim # place stack frame just "above" the key schedule 1174238405Sjkim &lea ($s1,&DWP(-64-63,$key)); 1175238405Sjkim &sub ($s1,"esp"); 1176238405Sjkim &neg ($s1); 1177238405Sjkim &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line 1178238405Sjkim &sub ("esp",$s1); 1179238405Sjkim &add ("esp",4); # 4 is reserved for caller's return address 1180238405Sjkim &mov ($_esp,$s0); # save stack pointer 1181238405Sjkim 1182160814Ssimon &call (&label("pic_point")); # make it PIC! 1183160814Ssimon &set_label("pic_point"); 1184238405Sjkim &blindpop($tbl); 1185238405Sjkim &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if (!$x86only); 1186238405Sjkim &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl)); 1187160814Ssimon 1188238405Sjkim # pick Te4 copy which can't "overlap" with stack frame or key schedule 1189238405Sjkim &lea ($s1,&DWP(768-4,"esp")); 1190238405Sjkim &sub ($s1,$tbl); 1191238405Sjkim &and ($s1,0x300); 1192238405Sjkim &lea ($tbl,&DWP(2048+128,$tbl,$s1)); 1193238405Sjkim 1194238405Sjkim if (!$x86only) { 1195238405Sjkim &bt (&DWP(0,$s0),25); # check for SSE bit 1196238405Sjkim &jnc (&label("x86")); 1197238405Sjkim 1198238405Sjkim &movq ("mm0",&QWP(0,$acc)); 1199238405Sjkim &movq ("mm4",&QWP(8,$acc)); 1200238405Sjkim &call ("_sse_AES_encrypt_compact"); 1201238405Sjkim &mov ("esp",$_esp); # restore stack pointer 1202238405Sjkim &mov ($acc,&wparam(1)); # load out 1203238405Sjkim &movq (&QWP(0,$acc),"mm0"); # write output data 1204238405Sjkim &movq (&QWP(8,$acc),"mm4"); 1205238405Sjkim &emms (); 1206238405Sjkim &function_end_A(); 1207238405Sjkim } 1208238405Sjkim &set_label("x86",16); 1209238405Sjkim &mov ($_tbl,$tbl); 1210160814Ssimon &mov ($s0,&DWP(0,$acc)); # load input data 1211160814Ssimon &mov ($s1,&DWP(4,$acc)); 1212160814Ssimon &mov ($s2,&DWP(8,$acc)); 1213160814Ssimon &mov ($s3,&DWP(12,$acc)); 1214238405Sjkim &call ("_x86_AES_encrypt_compact"); 1215238405Sjkim &mov ("esp",$_esp); # restore stack pointer 1216160814Ssimon &mov ($acc,&wparam(1)); # load out 1217160814Ssimon &mov (&DWP(0,$acc),$s0); # write output data 1218160814Ssimon &mov (&DWP(4,$acc),$s1); 1219160814Ssimon &mov (&DWP(8,$acc),$s2); 1220160814Ssimon &mov (&DWP(12,$acc),$s3); 1221160814Ssimon&function_end("AES_encrypt"); 1222160814Ssimon 1223238405Sjkim#--------------------------------------------------------------------# 1224160814Ssimon 1225238405Sjkim###################################################################### 1226238405Sjkim# "Compact" block function 1227238405Sjkim###################################################################### 1228238405Sjkim 1229238405Sjkimsub deccompact() 1230290207Sjkim{ my $Fn = \&mov; 1231238405Sjkim while ($#_>5) { pop(@_); $Fn=sub{}; } 1232238405Sjkim my ($i,$td,@s)=@_; 1233238405Sjkim my $tmp = $key; 1234238405Sjkim my $out = $i==3?$s[0]:$acc; 1235238405Sjkim 1236238405Sjkim # $Fn is used in first compact round and its purpose is to 1237238405Sjkim # void restoration of some values from stack, so that after 1238238405Sjkim # 4xdeccompact with extra argument $key, $s0 and $s1 values 1239238405Sjkim # are left there... 1240238405Sjkim if($i==3) { &$Fn ($key,$__key); } 1241238405Sjkim else { &mov ($out,$s[0]); } 1242238405Sjkim &and ($out,0xFF); 1243238405Sjkim &movz ($out,&BP(-128,$td,$out,1)); 1244238405Sjkim 1245238405Sjkim if ($i==3) { $tmp=$s[1]; } 1246238405Sjkim &movz ($tmp,&HB($s[1])); 1247238405Sjkim &movz ($tmp,&BP(-128,$td,$tmp,1)); 1248238405Sjkim &shl ($tmp,8); 1249238405Sjkim &xor ($out,$tmp); 1250238405Sjkim 1251238405Sjkim if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); } 1252238405Sjkim else { mov ($tmp,$s[2]); } 1253238405Sjkim &shr ($tmp,16); 1254238405Sjkim &and ($tmp,0xFF); 1255238405Sjkim &movz ($tmp,&BP(-128,$td,$tmp,1)); 1256238405Sjkim &shl ($tmp,16); 1257238405Sjkim &xor ($out,$tmp); 1258238405Sjkim 1259238405Sjkim if ($i==3) { $tmp=$s[3]; &$Fn ($s[2],$__s1); } 1260238405Sjkim else { &mov ($tmp,$s[3]); } 1261238405Sjkim &shr ($tmp,24); 1262238405Sjkim &movz ($tmp,&BP(-128,$td,$tmp,1)); 1263238405Sjkim &shl ($tmp,24); 1264238405Sjkim &xor ($out,$tmp); 1265238405Sjkim if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 1266238405Sjkim if ($i==3) { &$Fn ($s[3],$__s0); } 1267238405Sjkim} 1268238405Sjkim 1269238405Sjkim# must be called with 2,3,0,1 as argument sequence!!! 1270238405Sjkimsub dectransform() 1271238405Sjkim{ my @s = ($s0,$s1,$s2,$s3); 1272238405Sjkim my $i = shift; 1273238405Sjkim my $tmp = $key; 1274238405Sjkim my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1); 1275238405Sjkim my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1); 1276238405Sjkim my $tp8 = $tbl; 1277238405Sjkim 1278290207Sjkim &mov ($tmp,0x80808080); 1279290207Sjkim &and ($tmp,$s[$i]); 1280290207Sjkim &mov ($acc,$tmp); 1281238405Sjkim &shr ($tmp,7); 1282238405Sjkim &lea ($tp2,&DWP(0,$s[$i],$s[$i])); 1283238405Sjkim &sub ($acc,$tmp); 1284238405Sjkim &and ($tp2,0xfefefefe); 1285238405Sjkim &and ($acc,0x1b1b1b1b); 1286290207Sjkim &xor ($tp2,$acc); 1287290207Sjkim &mov ($tmp,0x80808080); 1288238405Sjkim 1289290207Sjkim &and ($tmp,$tp2); 1290290207Sjkim &mov ($acc,$tmp); 1291238405Sjkim &shr ($tmp,7); 1292238405Sjkim &lea ($tp4,&DWP(0,$tp2,$tp2)); 1293238405Sjkim &sub ($acc,$tmp); 1294238405Sjkim &and ($tp4,0xfefefefe); 1295238405Sjkim &and ($acc,0x1b1b1b1b); 1296238405Sjkim &xor ($tp2,$s[$i]); # tp2^tp1 1297290207Sjkim &xor ($tp4,$acc); 1298290207Sjkim &mov ($tmp,0x80808080); 1299238405Sjkim 1300290207Sjkim &and ($tmp,$tp4); 1301290207Sjkim &mov ($acc,$tmp); 1302238405Sjkim &shr ($tmp,7); 1303238405Sjkim &lea ($tp8,&DWP(0,$tp4,$tp4)); 1304238405Sjkim &sub ($acc,$tmp); 1305238405Sjkim &and ($tp8,0xfefefefe); 1306238405Sjkim &and ($acc,0x1b1b1b1b); 1307238405Sjkim &xor ($tp4,$s[$i]); # tp4^tp1 1308238405Sjkim &rotl ($s[$i],8); # = ROTATE(tp1,8) 1309238405Sjkim &xor ($tp8,$acc); 1310238405Sjkim 1311238405Sjkim &xor ($s[$i],$tp2); 1312238405Sjkim &xor ($tp2,$tp8); 1313238405Sjkim &xor ($s[$i],$tp4); 1314238405Sjkim &xor ($tp4,$tp8); 1315290207Sjkim &rotl ($tp2,24); 1316290207Sjkim &xor ($s[$i],$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1) 1317238405Sjkim &rotl ($tp4,16); 1318290207Sjkim &xor ($s[$i],$tp2); # ^= ROTATE(tp8^tp2^tp1,24) 1319238405Sjkim &rotl ($tp8,8); 1320238405Sjkim &xor ($s[$i],$tp4); # ^= ROTATE(tp8^tp4^tp1,16) 1321238405Sjkim &mov ($s[0],$__s0) if($i==2); #prefetch $s0 1322238405Sjkim &mov ($s[1],$__s1) if($i==3); #prefetch $s1 1323238405Sjkim &mov ($s[2],$__s2) if($i==1); 1324238405Sjkim &xor ($s[$i],$tp8); # ^= ROTATE(tp8,8) 1325238405Sjkim 1326238405Sjkim &mov ($s[3],$__s3) if($i==1); 1327238405Sjkim &mov (&DWP(4+4*$i,"esp"),$s[$i]) if($i>=2); 1328238405Sjkim} 1329238405Sjkim 1330238405Sjkim&function_begin_B("_x86_AES_decrypt_compact"); 1331238405Sjkim # note that caller is expected to allocate stack frame for me! 1332238405Sjkim &mov ($__key,$key); # save key 1333238405Sjkim 1334238405Sjkim &xor ($s0,&DWP(0,$key)); # xor with key 1335238405Sjkim &xor ($s1,&DWP(4,$key)); 1336238405Sjkim &xor ($s2,&DWP(8,$key)); 1337238405Sjkim &xor ($s3,&DWP(12,$key)); 1338238405Sjkim 1339238405Sjkim &mov ($acc,&DWP(240,$key)); # load key->rounds 1340238405Sjkim 1341238405Sjkim &lea ($acc,&DWP(-2,$acc,$acc)); 1342238405Sjkim &lea ($acc,&DWP(0,$key,$acc,8)); 1343238405Sjkim &mov ($__end,$acc); # end of key schedule 1344238405Sjkim 1345238405Sjkim # prefetch Td4 1346238405Sjkim &mov ($key,&DWP(0-128,$tbl)); 1347238405Sjkim &mov ($acc,&DWP(32-128,$tbl)); 1348238405Sjkim &mov ($key,&DWP(64-128,$tbl)); 1349238405Sjkim &mov ($acc,&DWP(96-128,$tbl)); 1350238405Sjkim &mov ($key,&DWP(128-128,$tbl)); 1351238405Sjkim &mov ($acc,&DWP(160-128,$tbl)); 1352238405Sjkim &mov ($key,&DWP(192-128,$tbl)); 1353238405Sjkim &mov ($acc,&DWP(224-128,$tbl)); 1354238405Sjkim 1355238405Sjkim &set_label("loop",16); 1356238405Sjkim 1357238405Sjkim &deccompact(0,$tbl,$s0,$s3,$s2,$s1,1); 1358238405Sjkim &deccompact(1,$tbl,$s1,$s0,$s3,$s2,1); 1359238405Sjkim &deccompact(2,$tbl,$s2,$s1,$s0,$s3,1); 1360238405Sjkim &deccompact(3,$tbl,$s3,$s2,$s1,$s0,1); 1361238405Sjkim &dectransform(2); 1362238405Sjkim &dectransform(3); 1363238405Sjkim &dectransform(0); 1364238405Sjkim &dectransform(1); 1365238405Sjkim &mov ($key,$__key); 1366238405Sjkim &mov ($tbl,$__tbl); 1367238405Sjkim &add ($key,16); # advance rd_key 1368238405Sjkim &xor ($s0,&DWP(0,$key)); 1369238405Sjkim &xor ($s1,&DWP(4,$key)); 1370238405Sjkim &xor ($s2,&DWP(8,$key)); 1371238405Sjkim &xor ($s3,&DWP(12,$key)); 1372238405Sjkim 1373238405Sjkim &cmp ($key,$__end); 1374238405Sjkim &mov ($__key,$key); 1375238405Sjkim &jb (&label("loop")); 1376238405Sjkim 1377238405Sjkim &deccompact(0,$tbl,$s0,$s3,$s2,$s1); 1378238405Sjkim &deccompact(1,$tbl,$s1,$s0,$s3,$s2); 1379238405Sjkim &deccompact(2,$tbl,$s2,$s1,$s0,$s3); 1380238405Sjkim &deccompact(3,$tbl,$s3,$s2,$s1,$s0); 1381238405Sjkim 1382238405Sjkim &xor ($s0,&DWP(16,$key)); 1383238405Sjkim &xor ($s1,&DWP(20,$key)); 1384238405Sjkim &xor ($s2,&DWP(24,$key)); 1385238405Sjkim &xor ($s3,&DWP(28,$key)); 1386238405Sjkim 1387238405Sjkim &ret (); 1388238405Sjkim&function_end_B("_x86_AES_decrypt_compact"); 1389238405Sjkim 1390238405Sjkim###################################################################### 1391238405Sjkim# "Compact" SSE block function. 1392238405Sjkim###################################################################### 1393238405Sjkim 1394238405Sjkimsub sse_deccompact() 1395238405Sjkim{ 1396238405Sjkim &pshufw ("mm1","mm0",0x0c); # 7, 6, 1, 0 1397290207Sjkim &pshufw ("mm5","mm4",0x09); # 13,12,11,10 1398238405Sjkim &movd ("eax","mm1"); # 7, 6, 1, 0 1399290207Sjkim &movd ("ebx","mm5"); # 13,12,11,10 1400290207Sjkim &mov ($__key,$key); 1401238405Sjkim 1402238405Sjkim &movz ($acc,&LB("eax")); # 0 1403290207Sjkim &movz ("edx",&HB("eax")); # 1 1404290207Sjkim &pshufw ("mm2","mm0",0x06); # 3, 2, 5, 4 1405238405Sjkim &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0 1406290207Sjkim &movz ($key,&LB("ebx")); # 10 1407238405Sjkim &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1 1408290207Sjkim &shr ("eax",16); # 7, 6 1409238405Sjkim &shl ("edx",8); # 1 1410238405Sjkim 1411290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 10 1412290207Sjkim &movz ($key,&HB("ebx")); # 11 1413238405Sjkim &shl ($acc,16); # 10 1414290207Sjkim &pshufw ("mm6","mm4",0x03); # 9, 8,15,14 1415238405Sjkim &or ("ecx",$acc); # 10 1416290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 11 1417290207Sjkim &movz ($key,&HB("eax")); # 7 1418238405Sjkim &shl ($acc,24); # 11 1419290207Sjkim &shr ("ebx",16); # 13,12 1420238405Sjkim &or ("edx",$acc); # 11 1421238405Sjkim 1422290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 7 1423290207Sjkim &movz ($key,&HB("ebx")); # 13 1424238405Sjkim &shl ($acc,24); # 7 1425238405Sjkim &or ("ecx",$acc); # 7 1426290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 13 1427290207Sjkim &movz ($key,&LB("eax")); # 6 1428238405Sjkim &shl ($acc,8); # 13 1429290207Sjkim &movd ("eax","mm2"); # 3, 2, 5, 4 1430238405Sjkim &or ("ecx",$acc); # 13 1431238405Sjkim 1432290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 6 1433290207Sjkim &movz ($key,&LB("ebx")); # 12 1434290207Sjkim &shl ($acc,16); # 6 1435238405Sjkim &movd ("ebx","mm6"); # 9, 8,15,14 1436290207Sjkim &movd ("mm0","ecx"); # t[0] collected 1437290207Sjkim &movz ("ecx",&BP(-128,$tbl,$key,1)); # 12 1438290207Sjkim &movz ($key,&LB("eax")); # 4 1439238405Sjkim &or ("ecx",$acc); # 12 1440238405Sjkim 1441290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 4 1442290207Sjkim &movz ($key,&LB("ebx")); # 14 1443238405Sjkim &or ("edx",$acc); # 4 1444290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 14 1445290207Sjkim &movz ($key,&HB("eax")); # 5 1446238405Sjkim &shl ($acc,16); # 14 1447290207Sjkim &shr ("eax",16); # 3, 2 1448238405Sjkim &or ("edx",$acc); # 14 1449290207Sjkim 1450290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 5 1451290207Sjkim &movz ($key,&HB("ebx")); # 15 1452290207Sjkim &shr ("ebx",16); # 9, 8 1453290207Sjkim &shl ($acc,8); # 5 1454238405Sjkim &movd ("mm1","edx"); # t[1] collected 1455290207Sjkim &movz ("edx",&BP(-128,$tbl,$key,1)); # 15 1456290207Sjkim &movz ($key,&HB("ebx")); # 9 1457290207Sjkim &shl ("edx",24); # 15 1458290207Sjkim &and ("ebx",0xff); # 8 1459238405Sjkim &or ("edx",$acc); # 15 1460238405Sjkim 1461238405Sjkim &punpckldq ("mm0","mm1"); # t[0,1] collected 1462238405Sjkim 1463290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 9 1464290207Sjkim &movz ($key,&LB("eax")); # 2 1465238405Sjkim &shl ($acc,8); # 9 1466290207Sjkim &movz ("eax",&HB("eax")); # 3 1467290207Sjkim &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 8 1468238405Sjkim &or ("ecx",$acc); # 9 1469290207Sjkim &movz ($acc,&BP(-128,$tbl,$key,1)); # 2 1470238405Sjkim &or ("edx","ebx"); # 8 1471238405Sjkim &shl ($acc,16); # 2 1472290207Sjkim &movz ("eax",&BP(-128,$tbl,"eax",1)); # 3 1473238405Sjkim &or ("edx",$acc); # 2 1474238405Sjkim &shl ("eax",24); # 3 1475238405Sjkim &or ("ecx","eax"); # 3 1476290207Sjkim &mov ($key,$__key); 1477290207Sjkim &movd ("mm4","edx"); # t[2] collected 1478238405Sjkim &movd ("mm5","ecx"); # t[3] collected 1479238405Sjkim 1480238405Sjkim &punpckldq ("mm4","mm5"); # t[2,3] collected 1481238405Sjkim} 1482238405Sjkim 1483238405Sjkim if (!$x86only) { 1484238405Sjkim&function_begin_B("_sse_AES_decrypt_compact"); 1485238405Sjkim &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0 1486238405Sjkim &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8 1487238405Sjkim 1488238405Sjkim # note that caller is expected to allocate stack frame for me! 1489238405Sjkim &mov ($acc,&DWP(240,$key)); # load key->rounds 1490238405Sjkim &lea ($acc,&DWP(-2,$acc,$acc)); 1491238405Sjkim &lea ($acc,&DWP(0,$key,$acc,8)); 1492238405Sjkim &mov ($__end,$acc); # end of key schedule 1493238405Sjkim 1494238405Sjkim &mov ($s0,0x1b1b1b1b); # magic constant 1495238405Sjkim &mov (&DWP(8,"esp"),$s0); 1496238405Sjkim &mov (&DWP(12,"esp"),$s0); 1497238405Sjkim 1498238405Sjkim # prefetch Td4 1499238405Sjkim &mov ($s0,&DWP(0-128,$tbl)); 1500238405Sjkim &mov ($s1,&DWP(32-128,$tbl)); 1501238405Sjkim &mov ($s2,&DWP(64-128,$tbl)); 1502238405Sjkim &mov ($s3,&DWP(96-128,$tbl)); 1503238405Sjkim &mov ($s0,&DWP(128-128,$tbl)); 1504238405Sjkim &mov ($s1,&DWP(160-128,$tbl)); 1505238405Sjkim &mov ($s2,&DWP(192-128,$tbl)); 1506238405Sjkim &mov ($s3,&DWP(224-128,$tbl)); 1507238405Sjkim 1508238405Sjkim &set_label("loop",16); 1509238405Sjkim &sse_deccompact(); 1510238405Sjkim &add ($key,16); 1511238405Sjkim &cmp ($key,$__end); 1512238405Sjkim &ja (&label("out")); 1513238405Sjkim 1514238405Sjkim # ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N) 1515238405Sjkim &movq ("mm3","mm0"); &movq ("mm7","mm4"); 1516238405Sjkim &movq ("mm2","mm0",1); &movq ("mm6","mm4",1); 1517238405Sjkim &movq ("mm1","mm0"); &movq ("mm5","mm4"); 1518238405Sjkim &pshufw ("mm0","mm0",0xb1); &pshufw ("mm4","mm4",0xb1);# = ROTATE(tp0,16) 1519238405Sjkim &pslld ("mm2",8); &pslld ("mm6",8); 1520238405Sjkim &psrld ("mm3",8); &psrld ("mm7",8); 1521238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<8 1522238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>8 1523238405Sjkim &pslld ("mm2",16); &pslld ("mm6",16); 1524238405Sjkim &psrld ("mm3",16); &psrld ("mm7",16); 1525238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<24 1526238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>24 1527238405Sjkim 1528238405Sjkim &movq ("mm3",&QWP(8,"esp")); 1529238405Sjkim &pxor ("mm2","mm2"); &pxor ("mm6","mm6"); 1530238405Sjkim &pcmpgtb("mm2","mm1"); &pcmpgtb("mm6","mm5"); 1531238405Sjkim &pand ("mm2","mm3"); &pand ("mm6","mm3"); 1532238405Sjkim &paddb ("mm1","mm1"); &paddb ("mm5","mm5"); 1533238405Sjkim &pxor ("mm1","mm2"); &pxor ("mm5","mm6"); # tp2 1534238405Sjkim &movq ("mm3","mm1"); &movq ("mm7","mm5"); 1535238405Sjkim &movq ("mm2","mm1"); &movq ("mm6","mm5"); 1536238405Sjkim &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp2 1537238405Sjkim &pslld ("mm3",24); &pslld ("mm7",24); 1538238405Sjkim &psrld ("mm2",8); &psrld ("mm6",8); 1539238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp2<<24 1540238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp2>>8 1541238405Sjkim 1542238405Sjkim &movq ("mm2",&QWP(8,"esp")); 1543238405Sjkim &pxor ("mm3","mm3"); &pxor ("mm7","mm7"); 1544238405Sjkim &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5"); 1545238405Sjkim &pand ("mm3","mm2"); &pand ("mm7","mm2"); 1546238405Sjkim &paddb ("mm1","mm1"); &paddb ("mm5","mm5"); 1547238405Sjkim &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp4 1548238405Sjkim &pshufw ("mm3","mm1",0xb1); &pshufw ("mm7","mm5",0xb1); 1549238405Sjkim &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp4 1550238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= ROTATE(tp4,16) 1551238405Sjkim 1552238405Sjkim &pxor ("mm3","mm3"); &pxor ("mm7","mm7"); 1553238405Sjkim &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5"); 1554238405Sjkim &pand ("mm3","mm2"); &pand ("mm7","mm2"); 1555238405Sjkim &paddb ("mm1","mm1"); &paddb ("mm5","mm5"); 1556238405Sjkim &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp8 1557238405Sjkim &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8 1558238405Sjkim &movq ("mm3","mm1"); &movq ("mm7","mm5"); 1559238405Sjkim &pshufw ("mm2","mm1",0xb1); &pshufw ("mm6","mm5",0xb1); 1560238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(tp8,16) 1561238405Sjkim &pslld ("mm1",8); &pslld ("mm5",8); 1562238405Sjkim &psrld ("mm3",8); &psrld ("mm7",8); 1563238405Sjkim &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key)); 1564238405Sjkim &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<8 1565238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>8 1566238405Sjkim &mov ($s0,&DWP(0-128,$tbl)); 1567238405Sjkim &pslld ("mm1",16); &pslld ("mm5",16); 1568238405Sjkim &mov ($s1,&DWP(64-128,$tbl)); 1569238405Sjkim &psrld ("mm3",16); &psrld ("mm7",16); 1570238405Sjkim &mov ($s2,&DWP(128-128,$tbl)); 1571238405Sjkim &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<24 1572238405Sjkim &mov ($s3,&DWP(192-128,$tbl)); 1573238405Sjkim &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>24 1574238405Sjkim 1575238405Sjkim &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); 1576238405Sjkim &jmp (&label("loop")); 1577238405Sjkim 1578238405Sjkim &set_label("out",16); 1579238405Sjkim &pxor ("mm0",&QWP(0,$key)); 1580238405Sjkim &pxor ("mm4",&QWP(8,$key)); 1581238405Sjkim 1582238405Sjkim &ret (); 1583238405Sjkim&function_end_B("_sse_AES_decrypt_compact"); 1584238405Sjkim } 1585238405Sjkim 1586238405Sjkim###################################################################### 1587238405Sjkim# Vanilla block function. 1588238405Sjkim###################################################################### 1589238405Sjkim 1590160814Ssimonsub decstep() 1591160814Ssimon{ my ($i,$td,@s) = @_; 1592160814Ssimon my $tmp = $key; 1593160814Ssimon my $out = $i==3?$s[0]:$acc; 1594160814Ssimon 1595160814Ssimon # no instructions are reordered, as performance appears 1596160814Ssimon # optimal... or rather that all attempts to reorder didn't 1597160814Ssimon # result in better performance [which by the way is not a 1598160814Ssimon # bit lower than ecryption]. 1599238405Sjkim if($i==3) { &mov ($key,$__key); } 1600160814Ssimon else { &mov ($out,$s[0]); } 1601160814Ssimon &and ($out,0xFF); 1602160814Ssimon &mov ($out,&DWP(0,$td,$out,8)); 1603160814Ssimon 1604160814Ssimon if ($i==3) { $tmp=$s[1]; } 1605160814Ssimon &movz ($tmp,&HB($s[1])); 1606160814Ssimon &xor ($out,&DWP(3,$td,$tmp,8)); 1607160814Ssimon 1608160814Ssimon if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); } 1609160814Ssimon else { &mov ($tmp,$s[2]); } 1610160814Ssimon &shr ($tmp,16); 1611160814Ssimon &and ($tmp,0xFF); 1612160814Ssimon &xor ($out,&DWP(2,$td,$tmp,8)); 1613160814Ssimon 1614238405Sjkim if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); } 1615160814Ssimon else { &mov ($tmp,$s[3]); } 1616160814Ssimon &shr ($tmp,24); 1617160814Ssimon &xor ($out,&DWP(1,$td,$tmp,8)); 1618160814Ssimon if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 1619238405Sjkim if ($i==3) { &mov ($s[3],$__s0); } 1620160814Ssimon &comment(); 1621160814Ssimon} 1622160814Ssimon 1623160814Ssimonsub declast() 1624160814Ssimon{ my ($i,$td,@s)=@_; 1625160814Ssimon my $tmp = $key; 1626160814Ssimon my $out = $i==3?$s[0]:$acc; 1627160814Ssimon 1628238405Sjkim if($i==0) { &lea ($td,&DWP(2048+128,$td)); 1629238405Sjkim &mov ($tmp,&DWP(0-128,$td)); 1630238405Sjkim &mov ($acc,&DWP(32-128,$td)); 1631238405Sjkim &mov ($tmp,&DWP(64-128,$td)); 1632238405Sjkim &mov ($acc,&DWP(96-128,$td)); 1633238405Sjkim &mov ($tmp,&DWP(128-128,$td)); 1634238405Sjkim &mov ($acc,&DWP(160-128,$td)); 1635238405Sjkim &mov ($tmp,&DWP(192-128,$td)); 1636238405Sjkim &mov ($acc,&DWP(224-128,$td)); 1637238405Sjkim &lea ($td,&DWP(-128,$td)); } 1638238405Sjkim if($i==3) { &mov ($key,$__key); } 1639160814Ssimon else { &mov ($out,$s[0]); } 1640160814Ssimon &and ($out,0xFF); 1641238405Sjkim &movz ($out,&BP(0,$td,$out,1)); 1642160814Ssimon 1643160814Ssimon if ($i==3) { $tmp=$s[1]; } 1644160814Ssimon &movz ($tmp,&HB($s[1])); 1645238405Sjkim &movz ($tmp,&BP(0,$td,$tmp,1)); 1646162911Ssimon &shl ($tmp,8); 1647160814Ssimon &xor ($out,$tmp); 1648160814Ssimon 1649160814Ssimon if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); } 1650160814Ssimon else { mov ($tmp,$s[2]); } 1651160814Ssimon &shr ($tmp,16); 1652160814Ssimon &and ($tmp,0xFF); 1653238405Sjkim &movz ($tmp,&BP(0,$td,$tmp,1)); 1654162911Ssimon &shl ($tmp,16); 1655160814Ssimon &xor ($out,$tmp); 1656160814Ssimon 1657238405Sjkim if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); } 1658160814Ssimon else { &mov ($tmp,$s[3]); } 1659160814Ssimon &shr ($tmp,24); 1660238405Sjkim &movz ($tmp,&BP(0,$td,$tmp,1)); 1661162911Ssimon &shl ($tmp,24); 1662160814Ssimon &xor ($out,$tmp); 1663160814Ssimon if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); } 1664238405Sjkim if ($i==3) { &mov ($s[3],$__s0); 1665238405Sjkim &lea ($td,&DWP(-2048,$td)); } 1666160814Ssimon} 1667160814Ssimon 1668160814Ssimon&function_begin_B("_x86_AES_decrypt"); 1669160814Ssimon # note that caller is expected to allocate stack frame for me! 1670238405Sjkim &mov ($__key,$key); # save key 1671160814Ssimon 1672160814Ssimon &xor ($s0,&DWP(0,$key)); # xor with key 1673160814Ssimon &xor ($s1,&DWP(4,$key)); 1674160814Ssimon &xor ($s2,&DWP(8,$key)); 1675160814Ssimon &xor ($s3,&DWP(12,$key)); 1676160814Ssimon 1677160814Ssimon &mov ($acc,&DWP(240,$key)); # load key->rounds 1678160814Ssimon 1679160814Ssimon if ($small_footprint) { 1680160814Ssimon &lea ($acc,&DWP(-2,$acc,$acc)); 1681160814Ssimon &lea ($acc,&DWP(0,$key,$acc,8)); 1682238405Sjkim &mov ($__end,$acc); # end of key schedule 1683238405Sjkim &set_label("loop",16); 1684238405Sjkim &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1685238405Sjkim &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1686238405Sjkim &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1687238405Sjkim &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1688160814Ssimon &add ($key,16); # advance rd_key 1689160814Ssimon &xor ($s0,&DWP(0,$key)); 1690160814Ssimon &xor ($s1,&DWP(4,$key)); 1691160814Ssimon &xor ($s2,&DWP(8,$key)); 1692160814Ssimon &xor ($s3,&DWP(12,$key)); 1693238405Sjkim &cmp ($key,$__end); 1694238405Sjkim &mov ($__key,$key); 1695160814Ssimon &jb (&label("loop")); 1696160814Ssimon } 1697160814Ssimon else { 1698160814Ssimon &cmp ($acc,10); 1699160814Ssimon &jle (&label("10rounds")); 1700160814Ssimon &cmp ($acc,12); 1701160814Ssimon &jle (&label("12rounds")); 1702160814Ssimon 1703238405Sjkim &set_label("14rounds",4); 1704160814Ssimon for ($i=1;$i<3;$i++) { 1705238405Sjkim &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1706238405Sjkim &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1707238405Sjkim &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1708238405Sjkim &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1709160814Ssimon &xor ($s0,&DWP(16*$i+0,$key)); 1710160814Ssimon &xor ($s1,&DWP(16*$i+4,$key)); 1711160814Ssimon &xor ($s2,&DWP(16*$i+8,$key)); 1712160814Ssimon &xor ($s3,&DWP(16*$i+12,$key)); 1713160814Ssimon } 1714160814Ssimon &add ($key,32); 1715238405Sjkim &mov ($__key,$key); # advance rd_key 1716238405Sjkim &set_label("12rounds",4); 1717160814Ssimon for ($i=1;$i<3;$i++) { 1718238405Sjkim &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1719238405Sjkim &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1720238405Sjkim &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1721238405Sjkim &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1722160814Ssimon &xor ($s0,&DWP(16*$i+0,$key)); 1723160814Ssimon &xor ($s1,&DWP(16*$i+4,$key)); 1724160814Ssimon &xor ($s2,&DWP(16*$i+8,$key)); 1725160814Ssimon &xor ($s3,&DWP(16*$i+12,$key)); 1726160814Ssimon } 1727160814Ssimon &add ($key,32); 1728238405Sjkim &mov ($__key,$key); # advance rd_key 1729238405Sjkim &set_label("10rounds",4); 1730160814Ssimon for ($i=1;$i<10;$i++) { 1731238405Sjkim &decstep(0,$tbl,$s0,$s3,$s2,$s1); 1732238405Sjkim &decstep(1,$tbl,$s1,$s0,$s3,$s2); 1733238405Sjkim &decstep(2,$tbl,$s2,$s1,$s0,$s3); 1734238405Sjkim &decstep(3,$tbl,$s3,$s2,$s1,$s0); 1735160814Ssimon &xor ($s0,&DWP(16*$i+0,$key)); 1736160814Ssimon &xor ($s1,&DWP(16*$i+4,$key)); 1737160814Ssimon &xor ($s2,&DWP(16*$i+8,$key)); 1738160814Ssimon &xor ($s3,&DWP(16*$i+12,$key)); 1739160814Ssimon } 1740160814Ssimon } 1741160814Ssimon 1742238405Sjkim &declast(0,$tbl,$s0,$s3,$s2,$s1); 1743238405Sjkim &declast(1,$tbl,$s1,$s0,$s3,$s2); 1744238405Sjkim &declast(2,$tbl,$s2,$s1,$s0,$s3); 1745238405Sjkim &declast(3,$tbl,$s3,$s2,$s1,$s0); 1746160814Ssimon 1747160814Ssimon &add ($key,$small_footprint?16:160); 1748160814Ssimon &xor ($s0,&DWP(0,$key)); 1749160814Ssimon &xor ($s1,&DWP(4,$key)); 1750160814Ssimon &xor ($s2,&DWP(8,$key)); 1751160814Ssimon &xor ($s3,&DWP(12,$key)); 1752160814Ssimon 1753160814Ssimon &ret (); 1754160814Ssimon 1755160814Ssimon&set_label("AES_Td",64); # Yes! I keep it in the code segment! 1756160814Ssimon &_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a); 1757160814Ssimon &_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b); 1758160814Ssimon &_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5); 1759160814Ssimon &_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5); 1760160814Ssimon &_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d); 1761160814Ssimon &_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b); 1762160814Ssimon &_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295); 1763160814Ssimon &_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e); 1764160814Ssimon &_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927); 1765160814Ssimon &_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d); 1766160814Ssimon &_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362); 1767160814Ssimon &_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9); 1768160814Ssimon &_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52); 1769160814Ssimon &_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566); 1770160814Ssimon &_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3); 1771160814Ssimon &_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed); 1772160814Ssimon &_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e); 1773160814Ssimon &_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4); 1774160814Ssimon &_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4); 1775160814Ssimon &_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd); 1776160814Ssimon &_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d); 1777160814Ssimon &_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060); 1778160814Ssimon &_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967); 1779160814Ssimon &_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879); 1780160814Ssimon &_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000); 1781160814Ssimon &_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c); 1782160814Ssimon &_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36); 1783160814Ssimon &_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624); 1784160814Ssimon &_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b); 1785160814Ssimon &_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c); 1786160814Ssimon &_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12); 1787160814Ssimon &_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14); 1788160814Ssimon &_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3); 1789160814Ssimon &_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b); 1790160814Ssimon &_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8); 1791160814Ssimon &_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684); 1792160814Ssimon &_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7); 1793160814Ssimon &_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177); 1794160814Ssimon &_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947); 1795160814Ssimon &_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322); 1796160814Ssimon &_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498); 1797160814Ssimon &_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f); 1798160814Ssimon &_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54); 1799160814Ssimon &_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382); 1800160814Ssimon &_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf); 1801160814Ssimon &_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb); 1802160814Ssimon &_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83); 1803160814Ssimon &_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef); 1804160814Ssimon &_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029); 1805160814Ssimon &_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235); 1806160814Ssimon &_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733); 1807160814Ssimon &_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117); 1808160814Ssimon &_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4); 1809160814Ssimon &_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546); 1810160814Ssimon &_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb); 1811160814Ssimon &_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d); 1812160814Ssimon &_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb); 1813160814Ssimon &_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a); 1814160814Ssimon &_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773); 1815160814Ssimon &_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478); 1816160814Ssimon &_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2); 1817160814Ssimon &_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff); 1818160814Ssimon &_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664); 1819160814Ssimon &_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0); 1820238405Sjkim 1821238405Sjkim#Td4: # four copies of Td4 to choose from to avoid L1 aliasing 1822162911Ssimon &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1823162911Ssimon &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1824162911Ssimon &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1825162911Ssimon &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1826162911Ssimon &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1827162911Ssimon &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1828162911Ssimon &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1829162911Ssimon &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1830162911Ssimon &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1831162911Ssimon &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1832162911Ssimon &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1833162911Ssimon &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1834162911Ssimon &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1835162911Ssimon &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1836162911Ssimon &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1837162911Ssimon &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1838162911Ssimon &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1839162911Ssimon &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1840162911Ssimon &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1841162911Ssimon &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1842162911Ssimon &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1843162911Ssimon &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1844162911Ssimon &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1845162911Ssimon &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1846162911Ssimon &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1847162911Ssimon &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1848162911Ssimon &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1849162911Ssimon &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1850162911Ssimon &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1851162911Ssimon &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1852162911Ssimon &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1853162911Ssimon &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1854238405Sjkim 1855238405Sjkim &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1856238405Sjkim &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1857238405Sjkim &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1858238405Sjkim &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1859238405Sjkim &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1860238405Sjkim &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1861238405Sjkim &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1862238405Sjkim &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1863238405Sjkim &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1864238405Sjkim &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1865238405Sjkim &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1866238405Sjkim &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1867238405Sjkim &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1868238405Sjkim &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1869238405Sjkim &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1870238405Sjkim &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1871238405Sjkim &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1872238405Sjkim &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1873238405Sjkim &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1874238405Sjkim &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1875238405Sjkim &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1876238405Sjkim &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1877238405Sjkim &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1878238405Sjkim &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1879238405Sjkim &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1880238405Sjkim &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1881238405Sjkim &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1882238405Sjkim &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1883238405Sjkim &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1884238405Sjkim &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1885238405Sjkim &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1886238405Sjkim &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1887238405Sjkim 1888238405Sjkim &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1889238405Sjkim &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1890238405Sjkim &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1891238405Sjkim &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1892238405Sjkim &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1893238405Sjkim &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1894238405Sjkim &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1895238405Sjkim &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1896238405Sjkim &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1897238405Sjkim &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1898238405Sjkim &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1899238405Sjkim &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1900238405Sjkim &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1901238405Sjkim &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1902238405Sjkim &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1903238405Sjkim &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1904238405Sjkim &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1905238405Sjkim &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1906238405Sjkim &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1907238405Sjkim &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1908238405Sjkim &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1909238405Sjkim &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1910238405Sjkim &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1911238405Sjkim &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1912238405Sjkim &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1913238405Sjkim &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1914238405Sjkim &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1915238405Sjkim &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1916238405Sjkim &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1917238405Sjkim &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1918238405Sjkim &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1919238405Sjkim &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1920238405Sjkim 1921238405Sjkim &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38); 1922238405Sjkim &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb); 1923238405Sjkim &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87); 1924238405Sjkim &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb); 1925238405Sjkim &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d); 1926238405Sjkim &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e); 1927238405Sjkim &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2); 1928238405Sjkim &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25); 1929238405Sjkim &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16); 1930238405Sjkim &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92); 1931238405Sjkim &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda); 1932238405Sjkim &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84); 1933238405Sjkim &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a); 1934238405Sjkim &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06); 1935238405Sjkim &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02); 1936238405Sjkim &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b); 1937238405Sjkim &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea); 1938238405Sjkim &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73); 1939238405Sjkim &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85); 1940238405Sjkim &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e); 1941238405Sjkim &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89); 1942238405Sjkim &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b); 1943238405Sjkim &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20); 1944238405Sjkim &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4); 1945238405Sjkim &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31); 1946238405Sjkim &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f); 1947238405Sjkim &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d); 1948238405Sjkim &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef); 1949238405Sjkim &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0); 1950238405Sjkim &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61); 1951238405Sjkim &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26); 1952238405Sjkim &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d); 1953160814Ssimon&function_end_B("_x86_AES_decrypt"); 1954160814Ssimon 1955160814Ssimon# void AES_decrypt (const void *inp,void *out,const AES_KEY *key); 1956160814Ssimon&function_begin("AES_decrypt"); 1957160814Ssimon &mov ($acc,&wparam(0)); # load inp 1958160814Ssimon &mov ($key,&wparam(2)); # load key 1959160814Ssimon 1960160814Ssimon &mov ($s0,"esp"); 1961238405Sjkim &sub ("esp",36); 1962238405Sjkim &and ("esp",-64); # align to cache-line 1963160814Ssimon 1964238405Sjkim # place stack frame just "above" the key schedule 1965238405Sjkim &lea ($s1,&DWP(-64-63,$key)); 1966238405Sjkim &sub ($s1,"esp"); 1967238405Sjkim &neg ($s1); 1968238405Sjkim &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line 1969238405Sjkim &sub ("esp",$s1); 1970238405Sjkim &add ("esp",4); # 4 is reserved for caller's return address 1971238405Sjkim &mov ($_esp,$s0); # save stack pointer 1972238405Sjkim 1973160814Ssimon &call (&label("pic_point")); # make it PIC! 1974160814Ssimon &set_label("pic_point"); 1975238405Sjkim &blindpop($tbl); 1976238405Sjkim &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only); 1977238405Sjkim &lea ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl)); 1978160814Ssimon 1979238405Sjkim # pick Td4 copy which can't "overlap" with stack frame or key schedule 1980238405Sjkim &lea ($s1,&DWP(768-4,"esp")); 1981238405Sjkim &sub ($s1,$tbl); 1982238405Sjkim &and ($s1,0x300); 1983238405Sjkim &lea ($tbl,&DWP(2048+128,$tbl,$s1)); 1984162911Ssimon 1985238405Sjkim if (!$x86only) { 1986238405Sjkim &bt (&DWP(0,$s0),25); # check for SSE bit 1987238405Sjkim &jnc (&label("x86")); 1988238405Sjkim 1989238405Sjkim &movq ("mm0",&QWP(0,$acc)); 1990238405Sjkim &movq ("mm4",&QWP(8,$acc)); 1991238405Sjkim &call ("_sse_AES_decrypt_compact"); 1992238405Sjkim &mov ("esp",$_esp); # restore stack pointer 1993238405Sjkim &mov ($acc,&wparam(1)); # load out 1994238405Sjkim &movq (&QWP(0,$acc),"mm0"); # write output data 1995238405Sjkim &movq (&QWP(8,$acc),"mm4"); 1996238405Sjkim &emms (); 1997238405Sjkim &function_end_A(); 1998238405Sjkim } 1999238405Sjkim &set_label("x86",16); 2000238405Sjkim &mov ($_tbl,$tbl); 2001160814Ssimon &mov ($s0,&DWP(0,$acc)); # load input data 2002160814Ssimon &mov ($s1,&DWP(4,$acc)); 2003160814Ssimon &mov ($s2,&DWP(8,$acc)); 2004160814Ssimon &mov ($s3,&DWP(12,$acc)); 2005238405Sjkim &call ("_x86_AES_decrypt_compact"); 2006238405Sjkim &mov ("esp",$_esp); # restore stack pointer 2007160814Ssimon &mov ($acc,&wparam(1)); # load out 2008160814Ssimon &mov (&DWP(0,$acc),$s0); # write output data 2009160814Ssimon &mov (&DWP(4,$acc),$s1); 2010160814Ssimon &mov (&DWP(8,$acc),$s2); 2011160814Ssimon &mov (&DWP(12,$acc),$s3); 2012160814Ssimon&function_end("AES_decrypt"); 2013160814Ssimon 2014160814Ssimon# void AES_cbc_encrypt (const void char *inp, unsigned char *out, 2015160814Ssimon# size_t length, const AES_KEY *key, 2016160814Ssimon# unsigned char *ivp,const int enc); 2017160814Ssimon{ 2018160814Ssimon# stack frame layout 2019238405Sjkim# -4(%esp) # return address 0(%esp) 2020238405Sjkim# 0(%esp) # s0 backing store 4(%esp) 2021238405Sjkim# 4(%esp) # s1 backing store 8(%esp) 2022238405Sjkim# 8(%esp) # s2 backing store 12(%esp) 2023238405Sjkim# 12(%esp) # s3 backing store 16(%esp) 2024238405Sjkim# 16(%esp) # key backup 20(%esp) 2025238405Sjkim# 20(%esp) # end of key schedule 24(%esp) 2026238405Sjkim# 24(%esp) # %ebp backup 28(%esp) 2027238405Sjkim# 28(%esp) # %esp backup 2028238405Sjkimmy $_inp=&DWP(32,"esp"); # copy of wparam(0) 2029238405Sjkimmy $_out=&DWP(36,"esp"); # copy of wparam(1) 2030238405Sjkimmy $_len=&DWP(40,"esp"); # copy of wparam(2) 2031238405Sjkimmy $_key=&DWP(44,"esp"); # copy of wparam(3) 2032238405Sjkimmy $_ivp=&DWP(48,"esp"); # copy of wparam(4) 2033238405Sjkimmy $_tmp=&DWP(52,"esp"); # volatile variable 2034238405Sjkim# 2035238405Sjkimmy $ivec=&DWP(60,"esp"); # ivec[16] 2036238405Sjkimmy $aes_key=&DWP(76,"esp"); # copy of aes_key 2037238405Sjkimmy $mark=&DWP(76+240,"esp"); # copy of aes_key->rounds 2038160814Ssimon 2039160814Ssimon&function_begin("AES_cbc_encrypt"); 2040160814Ssimon &mov ($s2 eq "ecx"? $s2 : "",&wparam(2)); # load len 2041160814Ssimon &cmp ($s2,0); 2042238405Sjkim &je (&label("drop_out")); 2043160814Ssimon 2044160814Ssimon &call (&label("pic_point")); # make it PIC! 2045160814Ssimon &set_label("pic_point"); 2046238405Sjkim &blindpop($tbl); 2047238405Sjkim &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only); 2048160814Ssimon 2049238405Sjkim &cmp (&wparam(5),0); 2050238405Sjkim &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl)); 2051238405Sjkim &jne (&label("picked_te")); 2052238405Sjkim &lea ($tbl,&DWP(&label("AES_Td")."-".&label("AES_Te"),$tbl)); 2053238405Sjkim &set_label("picked_te"); 2054238405Sjkim 2055238405Sjkim # one can argue if this is required 2056160814Ssimon &pushf (); 2057160814Ssimon &cld (); 2058160814Ssimon 2059238405Sjkim &cmp ($s2,$speed_limit); 2060238405Sjkim &jb (&label("slow_way")); 2061238405Sjkim &test ($s2,15); 2062238405Sjkim &jnz (&label("slow_way")); 2063238405Sjkim if (!$x86only) { 2064238405Sjkim &bt (&DWP(0,$s0),28); # check for hyper-threading bit 2065238405Sjkim &jc (&label("slow_way")); 2066238405Sjkim } 2067238405Sjkim # pre-allocate aligned stack frame... 2068238405Sjkim &lea ($acc,&DWP(-80-244,"esp")); 2069238405Sjkim &and ($acc,-64); 2070160814Ssimon 2071238405Sjkim # ... and make sure it doesn't alias with $tbl modulo 4096 2072238405Sjkim &mov ($s0,$tbl); 2073238405Sjkim &lea ($s1,&DWP(2048+256,$tbl)); 2074238405Sjkim &mov ($s3,$acc); 2075160814Ssimon &and ($s0,0xfff); # s = %ebp&0xfff 2076238405Sjkim &and ($s1,0xfff); # e = (%ebp+2048+256)&0xfff 2077160814Ssimon &and ($s3,0xfff); # p = %esp&0xfff 2078160814Ssimon 2079160814Ssimon &cmp ($s3,$s1); # if (p>=e) %esp =- (p-e); 2080238405Sjkim &jb (&label("tbl_break_out")); 2081160814Ssimon &sub ($s3,$s1); 2082238405Sjkim &sub ($acc,$s3); 2083238405Sjkim &jmp (&label("tbl_ok")); 2084238405Sjkim &set_label("tbl_break_out",4); # else %esp -= (p-s)&0xfff + framesz; 2085160814Ssimon &sub ($s3,$s0); 2086160814Ssimon &and ($s3,0xfff); 2087238405Sjkim &add ($s3,384); 2088238405Sjkim &sub ($acc,$s3); 2089238405Sjkim &set_label("tbl_ok",4); 2090160814Ssimon 2091238405Sjkim &lea ($s3,&wparam(0)); # obtain pointer to parameter block 2092238405Sjkim &exch ("esp",$acc); # allocate stack frame 2093160814Ssimon &add ("esp",4); # reserve for return address! 2094238405Sjkim &mov ($_tbl,$tbl); # save %ebp 2095238405Sjkim &mov ($_esp,$acc); # save %esp 2096160814Ssimon 2097238405Sjkim &mov ($s0,&DWP(0,$s3)); # load inp 2098238405Sjkim &mov ($s1,&DWP(4,$s3)); # load out 2099238405Sjkim #&mov ($s2,&DWP(8,$s3)); # load len 2100238405Sjkim &mov ($key,&DWP(12,$s3)); # load key 2101238405Sjkim &mov ($acc,&DWP(16,$s3)); # load ivp 2102238405Sjkim &mov ($s3,&DWP(20,$s3)); # load enc flag 2103238405Sjkim 2104160814Ssimon &mov ($_inp,$s0); # save copy of inp 2105160814Ssimon &mov ($_out,$s1); # save copy of out 2106160814Ssimon &mov ($_len,$s2); # save copy of len 2107238405Sjkim &mov ($_key,$key); # save copy of key 2108160814Ssimon &mov ($_ivp,$acc); # save copy of ivp 2109160814Ssimon 2110162911Ssimon &mov ($mark,0); # copy of aes_key->rounds = 0; 2111162911Ssimon # do we copy key schedule to stack? 2112238405Sjkim &mov ($s1 eq "ebx" ? $s1 : "",$key); 2113162911Ssimon &mov ($s2 eq "ecx" ? $s2 : "",244/4); 2114238405Sjkim &sub ($s1,$tbl); 2115238405Sjkim &mov ("esi",$key); 2116162911Ssimon &and ($s1,0xfff); 2117160814Ssimon &lea ("edi",$aes_key); 2118238405Sjkim &cmp ($s1,2048+256); 2119238405Sjkim &jb (&label("do_copy")); 2120162911Ssimon &cmp ($s1,4096-244); 2121238405Sjkim &jb (&label("skip_copy")); 2122238405Sjkim &set_label("do_copy",4); 2123162911Ssimon &mov ($_key,"edi"); 2124162911Ssimon &data_word(0xA5F3F689); # rep movsd 2125238405Sjkim &set_label("skip_copy"); 2126160814Ssimon 2127160814Ssimon &mov ($key,16); 2128238405Sjkim &set_label("prefetch_tbl",4); 2129238405Sjkim &mov ($s0,&DWP(0,$tbl)); 2130238405Sjkim &mov ($s1,&DWP(32,$tbl)); 2131238405Sjkim &mov ($s2,&DWP(64,$tbl)); 2132238405Sjkim &mov ($acc,&DWP(96,$tbl)); 2133238405Sjkim &lea ($tbl,&DWP(128,$tbl)); 2134238405Sjkim &sub ($key,1); 2135238405Sjkim &jnz (&label("prefetch_tbl")); 2136238405Sjkim &sub ($tbl,2048); 2137160814Ssimon 2138238405Sjkim &mov ($acc,$_inp); 2139160814Ssimon &mov ($key,$_ivp); 2140160814Ssimon 2141238405Sjkim &cmp ($s3,0); 2142238405Sjkim &je (&label("fast_decrypt")); 2143238405Sjkim 2144238405Sjkim#----------------------------- ENCRYPT -----------------------------# 2145160814Ssimon &mov ($s0,&DWP(0,$key)); # load iv 2146160814Ssimon &mov ($s1,&DWP(4,$key)); 2147160814Ssimon 2148238405Sjkim &set_label("fast_enc_loop",16); 2149160814Ssimon &mov ($s2,&DWP(8,$key)); 2150160814Ssimon &mov ($s3,&DWP(12,$key)); 2151160814Ssimon 2152160814Ssimon &xor ($s0,&DWP(0,$acc)); # xor input data 2153160814Ssimon &xor ($s1,&DWP(4,$acc)); 2154160814Ssimon &xor ($s2,&DWP(8,$acc)); 2155160814Ssimon &xor ($s3,&DWP(12,$acc)); 2156160814Ssimon 2157160814Ssimon &mov ($key,$_key); # load key 2158160814Ssimon &call ("_x86_AES_encrypt"); 2159160814Ssimon 2160160814Ssimon &mov ($acc,$_inp); # load inp 2161160814Ssimon &mov ($key,$_out); # load out 2162160814Ssimon 2163160814Ssimon &mov (&DWP(0,$key),$s0); # save output data 2164160814Ssimon &mov (&DWP(4,$key),$s1); 2165160814Ssimon &mov (&DWP(8,$key),$s2); 2166160814Ssimon &mov (&DWP(12,$key),$s3); 2167160814Ssimon 2168238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2169160814Ssimon &mov ($s2,$_len); # load len 2170160814Ssimon &mov ($_inp,$acc); # save inp 2171238405Sjkim &lea ($s3,&DWP(16,$key)); # advance out 2172160814Ssimon &mov ($_out,$s3); # save out 2173238405Sjkim &sub ($s2,16); # decrease len 2174160814Ssimon &mov ($_len,$s2); # save len 2175238405Sjkim &jnz (&label("fast_enc_loop")); 2176160814Ssimon &mov ($acc,$_ivp); # load ivp 2177238405Sjkim &mov ($s2,&DWP(8,$key)); # restore last 2 dwords 2178160814Ssimon &mov ($s3,&DWP(12,$key)); 2179160814Ssimon &mov (&DWP(0,$acc),$s0); # save ivec 2180160814Ssimon &mov (&DWP(4,$acc),$s1); 2181160814Ssimon &mov (&DWP(8,$acc),$s2); 2182160814Ssimon &mov (&DWP(12,$acc),$s3); 2183160814Ssimon 2184162911Ssimon &cmp ($mark,0); # was the key schedule copied? 2185160814Ssimon &mov ("edi",$_key); 2186162911Ssimon &je (&label("skip_ezero")); 2187160814Ssimon # zero copy of key schedule 2188160814Ssimon &mov ("ecx",240/4); 2189160814Ssimon &xor ("eax","eax"); 2190160814Ssimon &align (4); 2191290207Sjkim &data_word(0xABF3F689); # rep stosd 2192290207Sjkim &set_label("skip_ezero"); 2193194206Ssimon &mov ("esp",$_esp); 2194160814Ssimon &popf (); 2195238405Sjkim &set_label("drop_out"); 2196160814Ssimon &function_end_A(); 2197160814Ssimon &pushf (); # kludge, never executed 2198160814Ssimon 2199160814Ssimon#----------------------------- DECRYPT -----------------------------# 2200238405Sjkim&set_label("fast_decrypt",16); 2201160814Ssimon 2202160814Ssimon &cmp ($acc,$_out); 2203238405Sjkim &je (&label("fast_dec_in_place")); # in-place processing... 2204160814Ssimon 2205160814Ssimon &mov ($_tmp,$key); 2206160814Ssimon 2207160814Ssimon &align (4); 2208238405Sjkim &set_label("fast_dec_loop",16); 2209160814Ssimon &mov ($s0,&DWP(0,$acc)); # read input 2210160814Ssimon &mov ($s1,&DWP(4,$acc)); 2211160814Ssimon &mov ($s2,&DWP(8,$acc)); 2212160814Ssimon &mov ($s3,&DWP(12,$acc)); 2213160814Ssimon 2214160814Ssimon &mov ($key,$_key); # load key 2215160814Ssimon &call ("_x86_AES_decrypt"); 2216160814Ssimon 2217160814Ssimon &mov ($key,$_tmp); # load ivp 2218160814Ssimon &mov ($acc,$_len); # load len 2219160814Ssimon &xor ($s0,&DWP(0,$key)); # xor iv 2220160814Ssimon &xor ($s1,&DWP(4,$key)); 2221160814Ssimon &xor ($s2,&DWP(8,$key)); 2222160814Ssimon &xor ($s3,&DWP(12,$key)); 2223160814Ssimon 2224238405Sjkim &mov ($key,$_out); # load out 2225160814Ssimon &mov ($acc,$_inp); # load inp 2226160814Ssimon 2227160814Ssimon &mov (&DWP(0,$key),$s0); # write output 2228160814Ssimon &mov (&DWP(4,$key),$s1); 2229160814Ssimon &mov (&DWP(8,$key),$s2); 2230160814Ssimon &mov (&DWP(12,$key),$s3); 2231160814Ssimon 2232238405Sjkim &mov ($s2,$_len); # load len 2233160814Ssimon &mov ($_tmp,$acc); # save ivp 2234238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2235160814Ssimon &mov ($_inp,$acc); # save inp 2236238405Sjkim &lea ($key,&DWP(16,$key)); # advance out 2237160814Ssimon &mov ($_out,$key); # save out 2238238405Sjkim &sub ($s2,16); # decrease len 2239238405Sjkim &mov ($_len,$s2); # save len 2240238405Sjkim &jnz (&label("fast_dec_loop")); 2241160814Ssimon &mov ($key,$_tmp); # load temp ivp 2242160814Ssimon &mov ($acc,$_ivp); # load user ivp 2243160814Ssimon &mov ($s0,&DWP(0,$key)); # load iv 2244160814Ssimon &mov ($s1,&DWP(4,$key)); 2245160814Ssimon &mov ($s2,&DWP(8,$key)); 2246160814Ssimon &mov ($s3,&DWP(12,$key)); 2247160814Ssimon &mov (&DWP(0,$acc),$s0); # copy back to user 2248160814Ssimon &mov (&DWP(4,$acc),$s1); 2249160814Ssimon &mov (&DWP(8,$acc),$s2); 2250160814Ssimon &mov (&DWP(12,$acc),$s3); 2251238405Sjkim &jmp (&label("fast_dec_out")); 2252160814Ssimon 2253238405Sjkim &set_label("fast_dec_in_place",16); 2254238405Sjkim &set_label("fast_dec_in_place_loop"); 2255160814Ssimon &mov ($s0,&DWP(0,$acc)); # read input 2256160814Ssimon &mov ($s1,&DWP(4,$acc)); 2257160814Ssimon &mov ($s2,&DWP(8,$acc)); 2258160814Ssimon &mov ($s3,&DWP(12,$acc)); 2259160814Ssimon 2260238405Sjkim &lea ($key,$ivec); 2261160814Ssimon &mov (&DWP(0,$key),$s0); # copy to temp 2262160814Ssimon &mov (&DWP(4,$key),$s1); 2263160814Ssimon &mov (&DWP(8,$key),$s2); 2264160814Ssimon &mov (&DWP(12,$key),$s3); 2265160814Ssimon 2266160814Ssimon &mov ($key,$_key); # load key 2267160814Ssimon &call ("_x86_AES_decrypt"); 2268160814Ssimon 2269160814Ssimon &mov ($key,$_ivp); # load ivp 2270160814Ssimon &mov ($acc,$_out); # load out 2271160814Ssimon &xor ($s0,&DWP(0,$key)); # xor iv 2272160814Ssimon &xor ($s1,&DWP(4,$key)); 2273160814Ssimon &xor ($s2,&DWP(8,$key)); 2274160814Ssimon &xor ($s3,&DWP(12,$key)); 2275160814Ssimon 2276160814Ssimon &mov (&DWP(0,$acc),$s0); # write output 2277160814Ssimon &mov (&DWP(4,$acc),$s1); 2278160814Ssimon &mov (&DWP(8,$acc),$s2); 2279160814Ssimon &mov (&DWP(12,$acc),$s3); 2280160814Ssimon 2281238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance out 2282160814Ssimon &mov ($_out,$acc); # save out 2283160814Ssimon 2284160814Ssimon &lea ($acc,$ivec); 2285160814Ssimon &mov ($s0,&DWP(0,$acc)); # read temp 2286160814Ssimon &mov ($s1,&DWP(4,$acc)); 2287160814Ssimon &mov ($s2,&DWP(8,$acc)); 2288160814Ssimon &mov ($s3,&DWP(12,$acc)); 2289160814Ssimon 2290160814Ssimon &mov (&DWP(0,$key),$s0); # copy iv 2291160814Ssimon &mov (&DWP(4,$key),$s1); 2292160814Ssimon &mov (&DWP(8,$key),$s2); 2293160814Ssimon &mov (&DWP(12,$key),$s3); 2294160814Ssimon 2295160814Ssimon &mov ($acc,$_inp); # load inp 2296238405Sjkim &mov ($s2,$_len); # load len 2297238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2298238405Sjkim &mov ($_inp,$acc); # save inp 2299238405Sjkim &sub ($s2,16); # decrease len 2300238405Sjkim &mov ($_len,$s2); # save len 2301238405Sjkim &jnz (&label("fast_dec_in_place_loop")); 2302160814Ssimon 2303238405Sjkim &set_label("fast_dec_out",4); 2304238405Sjkim &cmp ($mark,0); # was the key schedule copied? 2305238405Sjkim &mov ("edi",$_key); 2306238405Sjkim &je (&label("skip_dzero")); 2307238405Sjkim # zero copy of key schedule 2308238405Sjkim &mov ("ecx",240/4); 2309238405Sjkim &xor ("eax","eax"); 2310238405Sjkim &align (4); 2311290207Sjkim &data_word(0xABF3F689); # rep stosd 2312290207Sjkim &set_label("skip_dzero"); 2313238405Sjkim &mov ("esp",$_esp); 2314238405Sjkim &popf (); 2315238405Sjkim &function_end_A(); 2316238405Sjkim &pushf (); # kludge, never executed 2317238405Sjkim 2318238405Sjkim#--------------------------- SLOW ROUTINE ---------------------------# 2319238405Sjkim&set_label("slow_way",16); 2320238405Sjkim 2321238405Sjkim &mov ($s0,&DWP(0,$s0)) if (!$x86only);# load OPENSSL_ia32cap 2322238405Sjkim &mov ($key,&wparam(3)); # load key 2323238405Sjkim 2324238405Sjkim # pre-allocate aligned stack frame... 2325238405Sjkim &lea ($acc,&DWP(-80,"esp")); 2326238405Sjkim &and ($acc,-64); 2327238405Sjkim 2328238405Sjkim # ... and make sure it doesn't alias with $key modulo 1024 2329238405Sjkim &lea ($s1,&DWP(-80-63,$key)); 2330238405Sjkim &sub ($s1,$acc); 2331238405Sjkim &neg ($s1); 2332238405Sjkim &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line 2333238405Sjkim &sub ($acc,$s1); 2334238405Sjkim 2335238405Sjkim # pick S-box copy which can't overlap with stack frame or $key 2336238405Sjkim &lea ($s1,&DWP(768,$acc)); 2337238405Sjkim &sub ($s1,$tbl); 2338238405Sjkim &and ($s1,0x300); 2339238405Sjkim &lea ($tbl,&DWP(2048+128,$tbl,$s1)); 2340238405Sjkim 2341238405Sjkim &lea ($s3,&wparam(0)); # pointer to parameter block 2342238405Sjkim 2343238405Sjkim &exch ("esp",$acc); 2344238405Sjkim &add ("esp",4); # reserve for return address! 2345238405Sjkim &mov ($_tbl,$tbl); # save %ebp 2346238405Sjkim &mov ($_esp,$acc); # save %esp 2347238405Sjkim &mov ($_tmp,$s0); # save OPENSSL_ia32cap 2348238405Sjkim 2349238405Sjkim &mov ($s0,&DWP(0,$s3)); # load inp 2350238405Sjkim &mov ($s1,&DWP(4,$s3)); # load out 2351238405Sjkim #&mov ($s2,&DWP(8,$s3)); # load len 2352238405Sjkim #&mov ($key,&DWP(12,$s3)); # load key 2353238405Sjkim &mov ($acc,&DWP(16,$s3)); # load ivp 2354238405Sjkim &mov ($s3,&DWP(20,$s3)); # load enc flag 2355238405Sjkim 2356238405Sjkim &mov ($_inp,$s0); # save copy of inp 2357238405Sjkim &mov ($_out,$s1); # save copy of out 2358238405Sjkim &mov ($_len,$s2); # save copy of len 2359238405Sjkim &mov ($_key,$key); # save copy of key 2360238405Sjkim &mov ($_ivp,$acc); # save copy of ivp 2361238405Sjkim 2362238405Sjkim &mov ($key,$acc); 2363238405Sjkim &mov ($acc,$s0); 2364238405Sjkim 2365238405Sjkim &cmp ($s3,0); 2366238405Sjkim &je (&label("slow_decrypt")); 2367238405Sjkim 2368238405Sjkim#--------------------------- SLOW ENCRYPT ---------------------------# 2369238405Sjkim &cmp ($s2,16); 2370238405Sjkim &mov ($s3,$s1); 2371238405Sjkim &jb (&label("slow_enc_tail")); 2372238405Sjkim 2373238405Sjkim if (!$x86only) { 2374238405Sjkim &bt ($_tmp,25); # check for SSE bit 2375238405Sjkim &jnc (&label("slow_enc_x86")); 2376238405Sjkim 2377238405Sjkim &movq ("mm0",&QWP(0,$key)); # load iv 2378238405Sjkim &movq ("mm4",&QWP(8,$key)); 2379238405Sjkim 2380238405Sjkim &set_label("slow_enc_loop_sse",16); 2381238405Sjkim &pxor ("mm0",&QWP(0,$acc)); # xor input data 2382238405Sjkim &pxor ("mm4",&QWP(8,$acc)); 2383238405Sjkim 2384238405Sjkim &mov ($key,$_key); 2385238405Sjkim &call ("_sse_AES_encrypt_compact"); 2386238405Sjkim 2387238405Sjkim &mov ($acc,$_inp); # load inp 2388238405Sjkim &mov ($key,$_out); # load out 2389238405Sjkim &mov ($s2,$_len); # load len 2390238405Sjkim 2391238405Sjkim &movq (&QWP(0,$key),"mm0"); # save output data 2392238405Sjkim &movq (&QWP(8,$key),"mm4"); 2393238405Sjkim 2394238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2395160814Ssimon &mov ($_inp,$acc); # save inp 2396238405Sjkim &lea ($s3,&DWP(16,$key)); # advance out 2397238405Sjkim &mov ($_out,$s3); # save out 2398238405Sjkim &sub ($s2,16); # decrease len 2399238405Sjkim &cmp ($s2,16); 2400238405Sjkim &mov ($_len,$s2); # save len 2401238405Sjkim &jae (&label("slow_enc_loop_sse")); 2402238405Sjkim &test ($s2,15); 2403238405Sjkim &jnz (&label("slow_enc_tail")); 2404238405Sjkim &mov ($acc,$_ivp); # load ivp 2405238405Sjkim &movq (&QWP(0,$acc),"mm0"); # save ivec 2406238405Sjkim &movq (&QWP(8,$acc),"mm4"); 2407238405Sjkim &emms (); 2408238405Sjkim &mov ("esp",$_esp); 2409238405Sjkim &popf (); 2410238405Sjkim &function_end_A(); 2411238405Sjkim &pushf (); # kludge, never executed 2412238405Sjkim } 2413238405Sjkim &set_label("slow_enc_x86",16); 2414238405Sjkim &mov ($s0,&DWP(0,$key)); # load iv 2415238405Sjkim &mov ($s1,&DWP(4,$key)); 2416160814Ssimon 2417238405Sjkim &set_label("slow_enc_loop_x86",4); 2418238405Sjkim &mov ($s2,&DWP(8,$key)); 2419238405Sjkim &mov ($s3,&DWP(12,$key)); 2420238405Sjkim 2421238405Sjkim &xor ($s0,&DWP(0,$acc)); # xor input data 2422238405Sjkim &xor ($s1,&DWP(4,$acc)); 2423238405Sjkim &xor ($s2,&DWP(8,$acc)); 2424238405Sjkim &xor ($s3,&DWP(12,$acc)); 2425238405Sjkim 2426238405Sjkim &mov ($key,$_key); # load key 2427238405Sjkim &call ("_x86_AES_encrypt_compact"); 2428238405Sjkim 2429238405Sjkim &mov ($acc,$_inp); # load inp 2430238405Sjkim &mov ($key,$_out); # load out 2431238405Sjkim 2432238405Sjkim &mov (&DWP(0,$key),$s0); # save output data 2433238405Sjkim &mov (&DWP(4,$key),$s1); 2434238405Sjkim &mov (&DWP(8,$key),$s2); 2435238405Sjkim &mov (&DWP(12,$key),$s3); 2436238405Sjkim 2437160814Ssimon &mov ($s2,$_len); # load len 2438238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2439238405Sjkim &mov ($_inp,$acc); # save inp 2440238405Sjkim &lea ($s3,&DWP(16,$key)); # advance out 2441238405Sjkim &mov ($_out,$s3); # save out 2442238405Sjkim &sub ($s2,16); # decrease len 2443238405Sjkim &cmp ($s2,16); 2444160814Ssimon &mov ($_len,$s2); # save len 2445238405Sjkim &jae (&label("slow_enc_loop_x86")); 2446238405Sjkim &test ($s2,15); 2447238405Sjkim &jnz (&label("slow_enc_tail")); 2448238405Sjkim &mov ($acc,$_ivp); # load ivp 2449238405Sjkim &mov ($s2,&DWP(8,$key)); # restore last dwords 2450238405Sjkim &mov ($s3,&DWP(12,$key)); 2451238405Sjkim &mov (&DWP(0,$acc),$s0); # save ivec 2452238405Sjkim &mov (&DWP(4,$acc),$s1); 2453238405Sjkim &mov (&DWP(8,$acc),$s2); 2454238405Sjkim &mov (&DWP(12,$acc),$s3); 2455160814Ssimon 2456238405Sjkim &mov ("esp",$_esp); 2457238405Sjkim &popf (); 2458238405Sjkim &function_end_A(); 2459238405Sjkim &pushf (); # kludge, never executed 2460238405Sjkim 2461238405Sjkim &set_label("slow_enc_tail",16); 2462238405Sjkim &emms () if (!$x86only); 2463238405Sjkim &mov ($key eq "edi"? $key:"",$s3); # load out to edi 2464238405Sjkim &mov ($s1,16); 2465238405Sjkim &sub ($s1,$s2); 2466238405Sjkim &cmp ($key,$acc eq "esi"? $acc:""); # compare with inp 2467238405Sjkim &je (&label("enc_in_place")); 2468238405Sjkim &align (4); 2469238405Sjkim &data_word(0xA4F3F689); # rep movsb # copy input 2470238405Sjkim &jmp (&label("enc_skip_in_place")); 2471238405Sjkim &set_label("enc_in_place"); 2472160814Ssimon &lea ($key,&DWP(0,$key,$s2)); 2473238405Sjkim &set_label("enc_skip_in_place"); 2474238405Sjkim &mov ($s2,$s1); 2475238405Sjkim &xor ($s0,$s0); 2476238405Sjkim &align (4); 2477238405Sjkim &data_word(0xAAF3F689); # rep stosb # zero tail 2478160814Ssimon 2479238405Sjkim &mov ($key,$_ivp); # restore ivp 2480238405Sjkim &mov ($acc,$s3); # output as input 2481238405Sjkim &mov ($s0,&DWP(0,$key)); 2482238405Sjkim &mov ($s1,&DWP(4,$key)); 2483238405Sjkim &mov ($_len,16); # len=16 2484238405Sjkim &jmp (&label("slow_enc_loop_x86")); # one more spin... 2485238405Sjkim 2486238405Sjkim#--------------------------- SLOW DECRYPT ---------------------------# 2487238405Sjkim&set_label("slow_decrypt",16); 2488238405Sjkim if (!$x86only) { 2489238405Sjkim &bt ($_tmp,25); # check for SSE bit 2490238405Sjkim &jnc (&label("slow_dec_loop_x86")); 2491238405Sjkim 2492238405Sjkim &set_label("slow_dec_loop_sse",4); 2493238405Sjkim &movq ("mm0",&QWP(0,$acc)); # read input 2494238405Sjkim &movq ("mm4",&QWP(8,$acc)); 2495238405Sjkim 2496238405Sjkim &mov ($key,$_key); 2497238405Sjkim &call ("_sse_AES_decrypt_compact"); 2498238405Sjkim 2499238405Sjkim &mov ($acc,$_inp); # load inp 2500238405Sjkim &lea ($s0,$ivec); 2501238405Sjkim &mov ($s1,$_out); # load out 2502238405Sjkim &mov ($s2,$_len); # load len 2503238405Sjkim &mov ($key,$_ivp); # load ivp 2504238405Sjkim 2505238405Sjkim &movq ("mm1",&QWP(0,$acc)); # re-read input 2506238405Sjkim &movq ("mm5",&QWP(8,$acc)); 2507238405Sjkim 2508238405Sjkim &pxor ("mm0",&QWP(0,$key)); # xor iv 2509238405Sjkim &pxor ("mm4",&QWP(8,$key)); 2510238405Sjkim 2511238405Sjkim &movq (&QWP(0,$key),"mm1"); # copy input to iv 2512238405Sjkim &movq (&QWP(8,$key),"mm5"); 2513238405Sjkim 2514238405Sjkim &sub ($s2,16); # decrease len 2515238405Sjkim &jc (&label("slow_dec_partial_sse")); 2516238405Sjkim 2517238405Sjkim &movq (&QWP(0,$s1),"mm0"); # write output 2518238405Sjkim &movq (&QWP(8,$s1),"mm4"); 2519238405Sjkim 2520238405Sjkim &lea ($s1,&DWP(16,$s1)); # advance out 2521238405Sjkim &mov ($_out,$s1); # save out 2522238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2523238405Sjkim &mov ($_inp,$acc); # save inp 2524238405Sjkim &mov ($_len,$s2); # save len 2525238405Sjkim &jnz (&label("slow_dec_loop_sse")); 2526238405Sjkim &emms (); 2527238405Sjkim &mov ("esp",$_esp); 2528238405Sjkim &popf (); 2529238405Sjkim &function_end_A(); 2530238405Sjkim &pushf (); # kludge, never executed 2531238405Sjkim 2532238405Sjkim &set_label("slow_dec_partial_sse",16); 2533238405Sjkim &movq (&QWP(0,$s0),"mm0"); # save output to temp 2534238405Sjkim &movq (&QWP(8,$s0),"mm4"); 2535238405Sjkim &emms (); 2536238405Sjkim 2537238405Sjkim &add ($s2 eq "ecx" ? "ecx":"",16); 2538238405Sjkim &mov ("edi",$s1); # out 2539238405Sjkim &mov ("esi",$s0); # temp 2540238405Sjkim &align (4); 2541238405Sjkim &data_word(0xA4F3F689); # rep movsb # copy partial output 2542238405Sjkim 2543238405Sjkim &mov ("esp",$_esp); 2544238405Sjkim &popf (); 2545238405Sjkim &function_end_A(); 2546238405Sjkim &pushf (); # kludge, never executed 2547238405Sjkim } 2548238405Sjkim &set_label("slow_dec_loop_x86",16); 2549238405Sjkim &mov ($s0,&DWP(0,$acc)); # read input 2550238405Sjkim &mov ($s1,&DWP(4,$acc)); 2551238405Sjkim &mov ($s2,&DWP(8,$acc)); 2552238405Sjkim &mov ($s3,&DWP(12,$acc)); 2553238405Sjkim 2554238405Sjkim &lea ($key,$ivec); 2555238405Sjkim &mov (&DWP(0,$key),$s0); # copy to temp 2556238405Sjkim &mov (&DWP(4,$key),$s1); 2557238405Sjkim &mov (&DWP(8,$key),$s2); 2558238405Sjkim &mov (&DWP(12,$key),$s3); 2559238405Sjkim 2560238405Sjkim &mov ($key,$_key); # load key 2561238405Sjkim &call ("_x86_AES_decrypt_compact"); 2562238405Sjkim 2563238405Sjkim &mov ($key,$_ivp); # load ivp 2564238405Sjkim &mov ($acc,$_len); # load len 2565238405Sjkim &xor ($s0,&DWP(0,$key)); # xor iv 2566238405Sjkim &xor ($s1,&DWP(4,$key)); 2567238405Sjkim &xor ($s2,&DWP(8,$key)); 2568238405Sjkim &xor ($s3,&DWP(12,$key)); 2569238405Sjkim 2570238405Sjkim &sub ($acc,16); 2571238405Sjkim &jc (&label("slow_dec_partial_x86")); 2572238405Sjkim 2573238405Sjkim &mov ($_len,$acc); # save len 2574238405Sjkim &mov ($acc,$_out); # load out 2575238405Sjkim 2576238405Sjkim &mov (&DWP(0,$acc),$s0); # write output 2577238405Sjkim &mov (&DWP(4,$acc),$s1); 2578238405Sjkim &mov (&DWP(8,$acc),$s2); 2579238405Sjkim &mov (&DWP(12,$acc),$s3); 2580238405Sjkim 2581238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance out 2582238405Sjkim &mov ($_out,$acc); # save out 2583238405Sjkim 2584238405Sjkim &lea ($acc,$ivec); 2585238405Sjkim &mov ($s0,&DWP(0,$acc)); # read temp 2586238405Sjkim &mov ($s1,&DWP(4,$acc)); 2587238405Sjkim &mov ($s2,&DWP(8,$acc)); 2588238405Sjkim &mov ($s3,&DWP(12,$acc)); 2589238405Sjkim 2590238405Sjkim &mov (&DWP(0,$key),$s0); # copy it to iv 2591238405Sjkim &mov (&DWP(4,$key),$s1); 2592238405Sjkim &mov (&DWP(8,$key),$s2); 2593238405Sjkim &mov (&DWP(12,$key),$s3); 2594238405Sjkim 2595238405Sjkim &mov ($acc,$_inp); # load inp 2596238405Sjkim &lea ($acc,&DWP(16,$acc)); # advance inp 2597238405Sjkim &mov ($_inp,$acc); # save inp 2598238405Sjkim &jnz (&label("slow_dec_loop_x86")); 2599238405Sjkim &mov ("esp",$_esp); 2600238405Sjkim &popf (); 2601238405Sjkim &function_end_A(); 2602238405Sjkim &pushf (); # kludge, never executed 2603238405Sjkim 2604238405Sjkim &set_label("slow_dec_partial_x86",16); 2605238405Sjkim &lea ($acc,$ivec); 2606238405Sjkim &mov (&DWP(0,$acc),$s0); # save output to temp 2607238405Sjkim &mov (&DWP(4,$acc),$s1); 2608238405Sjkim &mov (&DWP(8,$acc),$s2); 2609238405Sjkim &mov (&DWP(12,$acc),$s3); 2610238405Sjkim 2611238405Sjkim &mov ($acc,$_inp); 2612238405Sjkim &mov ($s0,&DWP(0,$acc)); # re-read input 2613238405Sjkim &mov ($s1,&DWP(4,$acc)); 2614238405Sjkim &mov ($s2,&DWP(8,$acc)); 2615238405Sjkim &mov ($s3,&DWP(12,$acc)); 2616238405Sjkim 2617238405Sjkim &mov (&DWP(0,$key),$s0); # copy it to iv 2618238405Sjkim &mov (&DWP(4,$key),$s1); 2619238405Sjkim &mov (&DWP(8,$key),$s2); 2620238405Sjkim &mov (&DWP(12,$key),$s3); 2621238405Sjkim 2622238405Sjkim &mov ("ecx",$_len); 2623238405Sjkim &mov ("edi",$_out); 2624238405Sjkim &lea ("esi",$ivec); 2625238405Sjkim &align (4); 2626238405Sjkim &data_word(0xA4F3F689); # rep movsb # copy partial output 2627238405Sjkim 2628238405Sjkim &mov ("esp",$_esp); 2629238405Sjkim &popf (); 2630160814Ssimon&function_end("AES_cbc_encrypt"); 2631160814Ssimon} 2632160814Ssimon 2633160814Ssimon#------------------------------------------------------------------# 2634160814Ssimon 2635160814Ssimonsub enckey() 2636160814Ssimon{ 2637160814Ssimon &movz ("esi",&LB("edx")); # rk[i]>>0 2638238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2639160814Ssimon &movz ("esi",&HB("edx")); # rk[i]>>8 2640238405Sjkim &shl ("ebx",24); 2641160814Ssimon &xor ("eax","ebx"); 2642160814Ssimon 2643238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2644160814Ssimon &shr ("edx",16); 2645160814Ssimon &movz ("esi",&LB("edx")); # rk[i]>>16 2646160814Ssimon &xor ("eax","ebx"); 2647160814Ssimon 2648238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2649160814Ssimon &movz ("esi",&HB("edx")); # rk[i]>>24 2650238405Sjkim &shl ("ebx",8); 2651160814Ssimon &xor ("eax","ebx"); 2652160814Ssimon 2653238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2654238405Sjkim &shl ("ebx",16); 2655160814Ssimon &xor ("eax","ebx"); 2656160814Ssimon 2657238405Sjkim &xor ("eax",&DWP(1024-128,$tbl,"ecx",4)); # rcon 2658160814Ssimon} 2659160814Ssimon 2660238405Sjkim&function_begin("_x86_AES_set_encrypt_key"); 2661238405Sjkim &mov ("esi",&wparam(1)); # user supplied key 2662238405Sjkim &mov ("edi",&wparam(3)); # private key schedule 2663160814Ssimon 2664160814Ssimon &test ("esi",-1); 2665160814Ssimon &jz (&label("badpointer")); 2666160814Ssimon &test ("edi",-1); 2667160814Ssimon &jz (&label("badpointer")); 2668160814Ssimon 2669160814Ssimon &call (&label("pic_point")); 2670160814Ssimon &set_label("pic_point"); 2671238405Sjkim &blindpop($tbl); 2672238405Sjkim &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl)); 2673238405Sjkim &lea ($tbl,&DWP(2048+128,$tbl)); 2674160814Ssimon 2675238405Sjkim # prefetch Te4 2676238405Sjkim &mov ("eax",&DWP(0-128,$tbl)); 2677238405Sjkim &mov ("ebx",&DWP(32-128,$tbl)); 2678238405Sjkim &mov ("ecx",&DWP(64-128,$tbl)); 2679238405Sjkim &mov ("edx",&DWP(96-128,$tbl)); 2680238405Sjkim &mov ("eax",&DWP(128-128,$tbl)); 2681238405Sjkim &mov ("ebx",&DWP(160-128,$tbl)); 2682238405Sjkim &mov ("ecx",&DWP(192-128,$tbl)); 2683238405Sjkim &mov ("edx",&DWP(224-128,$tbl)); 2684238405Sjkim 2685238405Sjkim &mov ("ecx",&wparam(2)); # number of bits in key 2686160814Ssimon &cmp ("ecx",128); 2687160814Ssimon &je (&label("10rounds")); 2688160814Ssimon &cmp ("ecx",192); 2689160814Ssimon &je (&label("12rounds")); 2690160814Ssimon &cmp ("ecx",256); 2691160814Ssimon &je (&label("14rounds")); 2692160814Ssimon &mov ("eax",-2); # invalid number of bits 2693160814Ssimon &jmp (&label("exit")); 2694160814Ssimon 2695160814Ssimon &set_label("10rounds"); 2696160814Ssimon &mov ("eax",&DWP(0,"esi")); # copy first 4 dwords 2697160814Ssimon &mov ("ebx",&DWP(4,"esi")); 2698160814Ssimon &mov ("ecx",&DWP(8,"esi")); 2699160814Ssimon &mov ("edx",&DWP(12,"esi")); 2700160814Ssimon &mov (&DWP(0,"edi"),"eax"); 2701160814Ssimon &mov (&DWP(4,"edi"),"ebx"); 2702160814Ssimon &mov (&DWP(8,"edi"),"ecx"); 2703160814Ssimon &mov (&DWP(12,"edi"),"edx"); 2704160814Ssimon 2705160814Ssimon &xor ("ecx","ecx"); 2706160814Ssimon &jmp (&label("10shortcut")); 2707160814Ssimon 2708160814Ssimon &align (4); 2709160814Ssimon &set_label("10loop"); 2710160814Ssimon &mov ("eax",&DWP(0,"edi")); # rk[0] 2711160814Ssimon &mov ("edx",&DWP(12,"edi")); # rk[3] 2712160814Ssimon &set_label("10shortcut"); 2713160814Ssimon &enckey (); 2714160814Ssimon 2715160814Ssimon &mov (&DWP(16,"edi"),"eax"); # rk[4] 2716160814Ssimon &xor ("eax",&DWP(4,"edi")); 2717160814Ssimon &mov (&DWP(20,"edi"),"eax"); # rk[5] 2718160814Ssimon &xor ("eax",&DWP(8,"edi")); 2719160814Ssimon &mov (&DWP(24,"edi"),"eax"); # rk[6] 2720160814Ssimon &xor ("eax",&DWP(12,"edi")); 2721160814Ssimon &mov (&DWP(28,"edi"),"eax"); # rk[7] 2722160814Ssimon &inc ("ecx"); 2723160814Ssimon &add ("edi",16); 2724160814Ssimon &cmp ("ecx",10); 2725160814Ssimon &jl (&label("10loop")); 2726160814Ssimon 2727160814Ssimon &mov (&DWP(80,"edi"),10); # setup number of rounds 2728160814Ssimon &xor ("eax","eax"); 2729160814Ssimon &jmp (&label("exit")); 2730160814Ssimon 2731160814Ssimon &set_label("12rounds"); 2732160814Ssimon &mov ("eax",&DWP(0,"esi")); # copy first 6 dwords 2733160814Ssimon &mov ("ebx",&DWP(4,"esi")); 2734160814Ssimon &mov ("ecx",&DWP(8,"esi")); 2735160814Ssimon &mov ("edx",&DWP(12,"esi")); 2736160814Ssimon &mov (&DWP(0,"edi"),"eax"); 2737160814Ssimon &mov (&DWP(4,"edi"),"ebx"); 2738160814Ssimon &mov (&DWP(8,"edi"),"ecx"); 2739160814Ssimon &mov (&DWP(12,"edi"),"edx"); 2740160814Ssimon &mov ("ecx",&DWP(16,"esi")); 2741160814Ssimon &mov ("edx",&DWP(20,"esi")); 2742160814Ssimon &mov (&DWP(16,"edi"),"ecx"); 2743160814Ssimon &mov (&DWP(20,"edi"),"edx"); 2744160814Ssimon 2745160814Ssimon &xor ("ecx","ecx"); 2746160814Ssimon &jmp (&label("12shortcut")); 2747160814Ssimon 2748160814Ssimon &align (4); 2749160814Ssimon &set_label("12loop"); 2750160814Ssimon &mov ("eax",&DWP(0,"edi")); # rk[0] 2751160814Ssimon &mov ("edx",&DWP(20,"edi")); # rk[5] 2752160814Ssimon &set_label("12shortcut"); 2753160814Ssimon &enckey (); 2754160814Ssimon 2755160814Ssimon &mov (&DWP(24,"edi"),"eax"); # rk[6] 2756160814Ssimon &xor ("eax",&DWP(4,"edi")); 2757160814Ssimon &mov (&DWP(28,"edi"),"eax"); # rk[7] 2758160814Ssimon &xor ("eax",&DWP(8,"edi")); 2759160814Ssimon &mov (&DWP(32,"edi"),"eax"); # rk[8] 2760160814Ssimon &xor ("eax",&DWP(12,"edi")); 2761160814Ssimon &mov (&DWP(36,"edi"),"eax"); # rk[9] 2762160814Ssimon 2763160814Ssimon &cmp ("ecx",7); 2764160814Ssimon &je (&label("12break")); 2765160814Ssimon &inc ("ecx"); 2766160814Ssimon 2767160814Ssimon &xor ("eax",&DWP(16,"edi")); 2768160814Ssimon &mov (&DWP(40,"edi"),"eax"); # rk[10] 2769160814Ssimon &xor ("eax",&DWP(20,"edi")); 2770160814Ssimon &mov (&DWP(44,"edi"),"eax"); # rk[11] 2771160814Ssimon 2772160814Ssimon &add ("edi",24); 2773160814Ssimon &jmp (&label("12loop")); 2774160814Ssimon 2775160814Ssimon &set_label("12break"); 2776160814Ssimon &mov (&DWP(72,"edi"),12); # setup number of rounds 2777160814Ssimon &xor ("eax","eax"); 2778160814Ssimon &jmp (&label("exit")); 2779160814Ssimon 2780160814Ssimon &set_label("14rounds"); 2781160814Ssimon &mov ("eax",&DWP(0,"esi")); # copy first 8 dwords 2782160814Ssimon &mov ("ebx",&DWP(4,"esi")); 2783160814Ssimon &mov ("ecx",&DWP(8,"esi")); 2784160814Ssimon &mov ("edx",&DWP(12,"esi")); 2785160814Ssimon &mov (&DWP(0,"edi"),"eax"); 2786160814Ssimon &mov (&DWP(4,"edi"),"ebx"); 2787160814Ssimon &mov (&DWP(8,"edi"),"ecx"); 2788160814Ssimon &mov (&DWP(12,"edi"),"edx"); 2789160814Ssimon &mov ("eax",&DWP(16,"esi")); 2790160814Ssimon &mov ("ebx",&DWP(20,"esi")); 2791160814Ssimon &mov ("ecx",&DWP(24,"esi")); 2792160814Ssimon &mov ("edx",&DWP(28,"esi")); 2793160814Ssimon &mov (&DWP(16,"edi"),"eax"); 2794160814Ssimon &mov (&DWP(20,"edi"),"ebx"); 2795160814Ssimon &mov (&DWP(24,"edi"),"ecx"); 2796160814Ssimon &mov (&DWP(28,"edi"),"edx"); 2797160814Ssimon 2798160814Ssimon &xor ("ecx","ecx"); 2799160814Ssimon &jmp (&label("14shortcut")); 2800160814Ssimon 2801160814Ssimon &align (4); 2802160814Ssimon &set_label("14loop"); 2803160814Ssimon &mov ("edx",&DWP(28,"edi")); # rk[7] 2804160814Ssimon &set_label("14shortcut"); 2805160814Ssimon &mov ("eax",&DWP(0,"edi")); # rk[0] 2806160814Ssimon 2807160814Ssimon &enckey (); 2808160814Ssimon 2809160814Ssimon &mov (&DWP(32,"edi"),"eax"); # rk[8] 2810160814Ssimon &xor ("eax",&DWP(4,"edi")); 2811160814Ssimon &mov (&DWP(36,"edi"),"eax"); # rk[9] 2812160814Ssimon &xor ("eax",&DWP(8,"edi")); 2813160814Ssimon &mov (&DWP(40,"edi"),"eax"); # rk[10] 2814160814Ssimon &xor ("eax",&DWP(12,"edi")); 2815160814Ssimon &mov (&DWP(44,"edi"),"eax"); # rk[11] 2816160814Ssimon 2817160814Ssimon &cmp ("ecx",6); 2818160814Ssimon &je (&label("14break")); 2819160814Ssimon &inc ("ecx"); 2820160814Ssimon 2821160814Ssimon &mov ("edx","eax"); 2822160814Ssimon &mov ("eax",&DWP(16,"edi")); # rk[4] 2823160814Ssimon &movz ("esi",&LB("edx")); # rk[11]>>0 2824238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2825160814Ssimon &movz ("esi",&HB("edx")); # rk[11]>>8 2826160814Ssimon &xor ("eax","ebx"); 2827160814Ssimon 2828238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2829160814Ssimon &shr ("edx",16); 2830238405Sjkim &shl ("ebx",8); 2831160814Ssimon &movz ("esi",&LB("edx")); # rk[11]>>16 2832160814Ssimon &xor ("eax","ebx"); 2833160814Ssimon 2834238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2835160814Ssimon &movz ("esi",&HB("edx")); # rk[11]>>24 2836238405Sjkim &shl ("ebx",16); 2837160814Ssimon &xor ("eax","ebx"); 2838160814Ssimon 2839238405Sjkim &movz ("ebx",&BP(-128,$tbl,"esi",1)); 2840238405Sjkim &shl ("ebx",24); 2841160814Ssimon &xor ("eax","ebx"); 2842160814Ssimon 2843160814Ssimon &mov (&DWP(48,"edi"),"eax"); # rk[12] 2844160814Ssimon &xor ("eax",&DWP(20,"edi")); 2845160814Ssimon &mov (&DWP(52,"edi"),"eax"); # rk[13] 2846160814Ssimon &xor ("eax",&DWP(24,"edi")); 2847160814Ssimon &mov (&DWP(56,"edi"),"eax"); # rk[14] 2848160814Ssimon &xor ("eax",&DWP(28,"edi")); 2849160814Ssimon &mov (&DWP(60,"edi"),"eax"); # rk[15] 2850160814Ssimon 2851160814Ssimon &add ("edi",32); 2852160814Ssimon &jmp (&label("14loop")); 2853160814Ssimon 2854160814Ssimon &set_label("14break"); 2855160814Ssimon &mov (&DWP(48,"edi"),14); # setup number of rounds 2856160814Ssimon &xor ("eax","eax"); 2857160814Ssimon &jmp (&label("exit")); 2858160814Ssimon 2859160814Ssimon &set_label("badpointer"); 2860160814Ssimon &mov ("eax",-1); 2861160814Ssimon &set_label("exit"); 2862238405Sjkim&function_end("_x86_AES_set_encrypt_key"); 2863160814Ssimon 2864238405Sjkim# int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits, 2865238405Sjkim# AES_KEY *key) 2866238405Sjkim&function_begin_B("private_AES_set_encrypt_key"); 2867238405Sjkim &call ("_x86_AES_set_encrypt_key"); 2868238405Sjkim &ret (); 2869238405Sjkim&function_end_B("private_AES_set_encrypt_key"); 2870238405Sjkim 2871160814Ssimonsub deckey() 2872238405Sjkim{ my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_; 2873238405Sjkim my $tmp = $tbl; 2874160814Ssimon 2875290207Sjkim &mov ($tmp,0x80808080); 2876290207Sjkim &and ($tmp,$tp1); 2877290207Sjkim &lea ($tp2,&DWP(0,$tp1,$tp1)); 2878290207Sjkim &mov ($acc,$tmp); 2879238405Sjkim &shr ($tmp,7); 2880238405Sjkim &sub ($acc,$tmp); 2881238405Sjkim &and ($tp2,0xfefefefe); 2882238405Sjkim &and ($acc,0x1b1b1b1b); 2883290207Sjkim &xor ($tp2,$acc); 2884290207Sjkim &mov ($tmp,0x80808080); 2885238405Sjkim 2886290207Sjkim &and ($tmp,$tp2); 2887290207Sjkim &lea ($tp4,&DWP(0,$tp2,$tp2)); 2888290207Sjkim &mov ($acc,$tmp); 2889238405Sjkim &shr ($tmp,7); 2890238405Sjkim &sub ($acc,$tmp); 2891238405Sjkim &and ($tp4,0xfefefefe); 2892238405Sjkim &and ($acc,0x1b1b1b1b); 2893238405Sjkim &xor ($tp2,$tp1); # tp2^tp1 2894290207Sjkim &xor ($tp4,$acc); 2895290207Sjkim &mov ($tmp,0x80808080); 2896238405Sjkim 2897290207Sjkim &and ($tmp,$tp4); 2898290207Sjkim &lea ($tp8,&DWP(0,$tp4,$tp4)); 2899290207Sjkim &mov ($acc,$tmp); 2900238405Sjkim &shr ($tmp,7); 2901238405Sjkim &xor ($tp4,$tp1); # tp4^tp1 2902238405Sjkim &sub ($acc,$tmp); 2903238405Sjkim &and ($tp8,0xfefefefe); 2904238405Sjkim &and ($acc,0x1b1b1b1b); 2905238405Sjkim &rotl ($tp1,8); # = ROTATE(tp1,8) 2906238405Sjkim &xor ($tp8,$acc); 2907238405Sjkim 2908238405Sjkim &mov ($tmp,&DWP(4*($i+1),$key)); # modulo-scheduled load 2909238405Sjkim 2910238405Sjkim &xor ($tp1,$tp2); 2911238405Sjkim &xor ($tp2,$tp8); 2912238405Sjkim &xor ($tp1,$tp4); 2913238405Sjkim &rotl ($tp2,24); 2914238405Sjkim &xor ($tp4,$tp8); 2915238405Sjkim &xor ($tp1,$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1) 2916238405Sjkim &rotl ($tp4,16); 2917238405Sjkim &xor ($tp1,$tp2); # ^= ROTATE(tp8^tp2^tp1,24) 2918238405Sjkim &rotl ($tp8,8); 2919238405Sjkim &xor ($tp1,$tp4); # ^= ROTATE(tp8^tp4^tp1,16) 2920238405Sjkim &mov ($tp2,$tmp); 2921238405Sjkim &xor ($tp1,$tp8); # ^= ROTATE(tp8,8) 2922238405Sjkim 2923238405Sjkim &mov (&DWP(4*$i,$key),$tp1); 2924160814Ssimon} 2925160814Ssimon 2926238405Sjkim# int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits, 2927160814Ssimon# AES_KEY *key) 2928238405Sjkim&function_begin_B("private_AES_set_decrypt_key"); 2929238405Sjkim &call ("_x86_AES_set_encrypt_key"); 2930160814Ssimon &cmp ("eax",0); 2931160814Ssimon &je (&label("proceed")); 2932160814Ssimon &ret (); 2933160814Ssimon 2934160814Ssimon &set_label("proceed"); 2935160814Ssimon &push ("ebp"); 2936160814Ssimon &push ("ebx"); 2937160814Ssimon &push ("esi"); 2938160814Ssimon &push ("edi"); 2939160814Ssimon 2940160814Ssimon &mov ("esi",&wparam(2)); 2941160814Ssimon &mov ("ecx",&DWP(240,"esi")); # pull number of rounds 2942160814Ssimon &lea ("ecx",&DWP(0,"","ecx",4)); 2943160814Ssimon &lea ("edi",&DWP(0,"esi","ecx",4)); # pointer to last chunk 2944160814Ssimon 2945238405Sjkim &set_label("invert",4); # invert order of chunks 2946160814Ssimon &mov ("eax",&DWP(0,"esi")); 2947160814Ssimon &mov ("ebx",&DWP(4,"esi")); 2948160814Ssimon &mov ("ecx",&DWP(0,"edi")); 2949160814Ssimon &mov ("edx",&DWP(4,"edi")); 2950160814Ssimon &mov (&DWP(0,"edi"),"eax"); 2951160814Ssimon &mov (&DWP(4,"edi"),"ebx"); 2952160814Ssimon &mov (&DWP(0,"esi"),"ecx"); 2953160814Ssimon &mov (&DWP(4,"esi"),"edx"); 2954160814Ssimon &mov ("eax",&DWP(8,"esi")); 2955160814Ssimon &mov ("ebx",&DWP(12,"esi")); 2956160814Ssimon &mov ("ecx",&DWP(8,"edi")); 2957160814Ssimon &mov ("edx",&DWP(12,"edi")); 2958160814Ssimon &mov (&DWP(8,"edi"),"eax"); 2959160814Ssimon &mov (&DWP(12,"edi"),"ebx"); 2960160814Ssimon &mov (&DWP(8,"esi"),"ecx"); 2961160814Ssimon &mov (&DWP(12,"esi"),"edx"); 2962160814Ssimon &add ("esi",16); 2963160814Ssimon &sub ("edi",16); 2964160814Ssimon &cmp ("esi","edi"); 2965160814Ssimon &jne (&label("invert")); 2966160814Ssimon 2967238405Sjkim &mov ($key,&wparam(2)); 2968238405Sjkim &mov ($acc,&DWP(240,$key)); # pull number of rounds 2969238405Sjkim &lea ($acc,&DWP(-2,$acc,$acc)); 2970238405Sjkim &lea ($acc,&DWP(0,$key,$acc,8)); 2971238405Sjkim &mov (&wparam(2),$acc); 2972160814Ssimon 2973238405Sjkim &mov ($s0,&DWP(16,$key)); # modulo-scheduled load 2974238405Sjkim &set_label("permute",4); # permute the key schedule 2975238405Sjkim &add ($key,16); 2976238405Sjkim &deckey (0,$key,$s0,$s1,$s2,$s3); 2977238405Sjkim &deckey (1,$key,$s1,$s2,$s3,$s0); 2978238405Sjkim &deckey (2,$key,$s2,$s3,$s0,$s1); 2979238405Sjkim &deckey (3,$key,$s3,$s0,$s1,$s2); 2980238405Sjkim &cmp ($key,&wparam(2)); 2981238405Sjkim &jb (&label("permute")); 2982160814Ssimon 2983160814Ssimon &xor ("eax","eax"); # return success 2984238405Sjkim&function_end("private_AES_set_decrypt_key"); 2985238405Sjkim&asciz("AES for x86, CRYPTOGAMS by <appro\@openssl.org>"); 2986160814Ssimon 2987160814Ssimon&asm_finish(); 2988