aesni-x86.pl 99.8 KB
Newer Older
R
Rich Salz 已提交
1
#! /usr/bin/env perl
M
Matt Caswell 已提交
2
# Copyright 2009-2020 The OpenSSL Project Authors. All Rights Reserved.
R
Rich Salz 已提交
3 4 5 6 7 8
#
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html

A
Andy Polyakov 已提交
9 10

# ====================================================================
11
# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
A
Andy Polyakov 已提交
12 13 14 15 16 17 18 19 20
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
# ====================================================================
#
# This module implements support for Intel AES-NI extension. In
# OpenSSL context it's used with Intel engine, but can also be used as
# drop-in replacement for crypto/aes/asm/aes-586.pl [see below for
# details].
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#
# Performance.
#
# To start with see corresponding paragraph in aesni-x86_64.pl...
# Instead of filling table similar to one found there I've chosen to
# summarize *comparison* results for raw ECB, CTR and CBC benchmarks.
# The simplified table below represents 32-bit performance relative
# to 64-bit one in every given point. Ratios vary for different
# encryption modes, therefore interval values.
#
#	16-byte     64-byte     256-byte    1-KB        8-KB
#	53-67%      67-84%      91-94%      95-98%      97-99.5%
#
# Lower ratios for smaller block sizes are perfectly understandable,
# because function call overhead is higher in 32-bit mode. Largest
# 8-KB block performance is virtually same: 32-bit code is less than
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
# 1% slower for ECB, CBC and CCM, and ~3% slower otherwise.

# January 2011
#
# See aesni-x86_64.pl for details. Unlike x86_64 version this module
# interleaves at most 6 aes[enc|dec] instructions, because there are
# not enough registers for 8x interleave [which should be optimal for
# Sandy Bridge]. Actually, performance results for 6x interleave
# factor presented in aesni-x86_64.pl (except for CTR) are for this
# module.

# April 2011
#
# Add aesni_xts_[en|de]crypt. Westmere spends 1.50 cycles processing
# one byte out of 8KB with 128-bit key, Sandy Bridge - 1.09.
A
Andy Polyakov 已提交
52

53 54 55 56
# November 2015
#
# Add aesni_ocb_[en|de]crypt.

57 58 59 60
######################################################################
# Current large-block performance in cycles per byte processed with
# 128-bit key (less is better).
#
61
#		CBC en-/decrypt	CTR	XTS	ECB	OCB
62
# Westmere	3.77/1.37	1.37	1.52	1.27
63 64
# * Bridge	5.07/0.98	0.99	1.09	0.91	1.10
# Haswell	4.44/0.80	0.97	1.03	0.72	0.76
65
# Skylake	2.68/0.65	0.65	0.66	0.64	0.66
66
# Silvermont	5.77/3.56	3.67	4.03	3.46	4.03
67
# Goldmont	3.84/1.39	1.39	1.63	1.31	1.70
68
# Bulldozer	5.80/0.98	1.05	1.24	0.93	1.23
69

A
Andy Polyakov 已提交
70 71 72
$PREFIX="aesni";	# if $PREFIX is set to "AES", the script
			# generates drop-in replacement for
			# crypto/aes/asm/aes-586.pl:-)
73
$inline=1;		# inline _aesni_[en|de]crypt
A
Andy Polyakov 已提交
74 75 76 77 78

$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
push(@INC,"${dir}","${dir}../../perlasm");
require "x86asm.pl";

79 80 81 82
$output = pop;
open OUT,">$output";
*STDOUT=*OUT;

83
&asm_init($ARGV[0]);
A
Andy Polyakov 已提交
84

85 86 87
&external_label("OPENSSL_ia32cap_P");
&static_label("key_const");

88 89
if ($PREFIX eq "aesni")	{ $movekey=\&movups; }
else			{ $movekey=\&movups; }
A
Andy Polyakov 已提交
90 91 92 93 94 95

$len="eax";
$rounds="ecx";
$key="edx";
$inp="esi";
$out="edi";
A
Andy Polyakov 已提交
96 97
$rounds_="ebx";	# backup copy for $rounds
$key_="ebp";	# backup copy for $key
A
Andy Polyakov 已提交
98

99 100 101 102 103 104 105 106
$rndkey0="xmm0";
$rndkey1="xmm1";
$inout0="xmm2";
$inout1="xmm3";
$inout2="xmm4";
$inout3="xmm5";	$in1="xmm5";
$inout4="xmm6";	$in0="xmm6";
$inout5="xmm7";	$ivec="xmm7";
107

A
Alessandro Ghedini 已提交
108
# AESNI extension
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
sub aeskeygenassist
{ my($dst,$src,$imm)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
    {	&data_byte(0x66,0x0f,0x3a,0xdf,0xc0|($1<<3)|$2,$imm);	}
}
sub aescommon
{ my($opcodelet,$dst,$src)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
    {	&data_byte(0x66,0x0f,0x38,$opcodelet,0xc0|($1<<3)|$2);}
}
sub aesimc	{ aescommon(0xdb,@_); }
sub aesenc	{ aescommon(0xdc,@_); }
sub aesenclast	{ aescommon(0xdd,@_); }
sub aesdec	{ aescommon(0xde,@_); }
sub aesdeclast	{ aescommon(0xdf,@_); }
A
Andy Polyakov 已提交
124

A
Andy Polyakov 已提交
125
# Inline version of internal aesni_[en|de]crypt1
126
{ my $sn;
A
Andy Polyakov 已提交
127
sub aesni_inline_generate1
128
{ my ($p,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
129
  $sn++;
A
Andy Polyakov 已提交
130

131
    &$movekey		($rndkey0,&QWP(0,$key));
A
Andy Polyakov 已提交
132
    &$movekey		($rndkey1,&QWP(16,$key));
133
    &xorps		($ivec,$rndkey0)	if (defined($ivec));
A
Andy Polyakov 已提交
134
    &lea		($key,&DWP(32,$key));
135 136
    &xorps		($inout,$ivec)		if (defined($ivec));
    &xorps		($inout,$rndkey0)	if (!defined($ivec));
137 138
    &set_label("${p}1_loop_$sn");
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
139 140
	&dec		($rounds);
	&$movekey	($rndkey1,&QWP(0,$key));
A
Andy Polyakov 已提交
141
	&lea		($key,&DWP(16,$key));
142 143 144
    &jnz		(&label("${p}1_loop_$sn"));
    eval"&aes${p}last	($inout,$rndkey1)";
}}
A
Andy Polyakov 已提交
145 146

sub aesni_generate1	# fully unrolled loop
147
{ my ($p,$inout)=@_; $inout=$inout0 if (!defined($inout));
A
Andy Polyakov 已提交
148 149

    &function_begin_B("_aesni_${p}rypt1");
150
	&movups		($rndkey0,&QWP(0,$key));
A
Andy Polyakov 已提交
151
	&$movekey	($rndkey1,&QWP(0x10,$key));
152
	&xorps		($inout,$rndkey0);
A
Andy Polyakov 已提交
153 154
	&$movekey	($rndkey0,&QWP(0x20,$key));
	&lea		($key,&DWP(0x30,$key));
155
	&cmp		($rounds,11);
A
Andy Polyakov 已提交
156 157 158 159
	&jb		(&label("${p}128"));
	&lea		($key,&DWP(0x20,$key));
	&je		(&label("${p}192"));
	&lea		($key,&DWP(0x20,$key));
160
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
161
	&$movekey	($rndkey1,&QWP(-0x40,$key));
162
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
163 164
	&$movekey	($rndkey0,&QWP(-0x30,$key));
    &set_label("${p}192");
165
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
166
	&$movekey	($rndkey1,&QWP(-0x20,$key));
167
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
168 169
	&$movekey	($rndkey0,&QWP(-0x10,$key));
    &set_label("${p}128");
170
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
171
	&$movekey	($rndkey1,&QWP(0,$key));
172
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
173
	&$movekey	($rndkey0,&QWP(0x10,$key));
174
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
175
	&$movekey	($rndkey1,&QWP(0x20,$key));
176
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
177
	&$movekey	($rndkey0,&QWP(0x30,$key));
178
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
179
	&$movekey	($rndkey1,&QWP(0x40,$key));
180
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
181
	&$movekey	($rndkey0,&QWP(0x50,$key));
182
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
183
	&$movekey	($rndkey1,&QWP(0x60,$key));
184
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
185
	&$movekey	($rndkey0,&QWP(0x70,$key));
186 187
	eval"&aes${p}	($inout,$rndkey1)";
    eval"&aes${p}last	($inout,$rndkey0)";
A
Andy Polyakov 已提交
188 189 190
    &ret();
    &function_end_B("_aesni_${p}rypt1");
}
A
Andy Polyakov 已提交
191

A
Andy Polyakov 已提交
192
# void $PREFIX_encrypt (const void *inp,void *out,const AES_KEY *key);
193
&aesni_generate1("enc") if (!$inline);
A
Andy Polyakov 已提交
194 195 196
&function_begin_B("${PREFIX}_encrypt");
	&mov	("eax",&wparam(0));
	&mov	($key,&wparam(2));
197
	&movups	($inout0,&QWP(0,"eax"));
A
Andy Polyakov 已提交
198 199
	&mov	($rounds,&DWP(240,$key));
	&mov	("eax",&wparam(1));
200 201 202 203
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
204 205
	&pxor	($rndkey0,$rndkey0);		# clear register bank
	&pxor	($rndkey1,$rndkey1);
A
Andy Polyakov 已提交
206
	&movups	(&QWP(0,"eax"),$inout0);
207
	&pxor	($inout0,$inout0);
A
Andy Polyakov 已提交
208 209 210 211
	&ret	();
&function_end_B("${PREFIX}_encrypt");

# void $PREFIX_decrypt (const void *inp,void *out,const AES_KEY *key);
212
&aesni_generate1("dec") if(!$inline);
A
Andy Polyakov 已提交
213 214 215
&function_begin_B("${PREFIX}_decrypt");
	&mov	("eax",&wparam(0));
	&mov	($key,&wparam(2));
216
	&movups	($inout0,&QWP(0,"eax"));
A
Andy Polyakov 已提交
217 218
	&mov	($rounds,&DWP(240,$key));
	&mov	("eax",&wparam(1));
219 220 221 222
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
223 224
	&pxor	($rndkey0,$rndkey0);		# clear register bank
	&pxor	($rndkey1,$rndkey1);
A
Andy Polyakov 已提交
225
	&movups	(&QWP(0,"eax"),$inout0);
226
	&pxor	($inout0,$inout0);
A
Andy Polyakov 已提交
227 228
	&ret	();
&function_end_B("${PREFIX}_decrypt");
A
Andy Polyakov 已提交
229

230 231 232 233
# _aesni_[en|de]cryptN are private interfaces, N denotes interleave
# factor. Why 3x subroutine were originally used in loops? Even though
# aes[enc|dec] latency was originally 6, it could be scheduled only
# every *2nd* cycle. Thus 3x interleave was the one providing optimal
A
Andy Polyakov 已提交
234 235
# utilization, i.e. when subroutine's throughput is virtually same as
# of non-interleaved subroutine [for number of input blocks up to 3].
236 237 238 239 240 241
# This is why it originally made no sense to implement 2x subroutine.
# But times change and it became appropriate to spend extra 192 bytes
# on 2x subroutine on Atom Silvermont account. For processors that
# can schedule aes[enc|dec] every cycle optimal interleave factor
# equals to corresponding instructions latency. 8x is optimal for
# * Bridge, but it's unfeasible to accommodate such implementation
J
Josh Soref 已提交
242
# in XMM registers addressable in 32-bit mode and therefore maximum
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
# of 6x is used instead...

sub aesni_generate2
{ my $p=shift;

    &function_begin_B("_aesni_${p}rypt2");
	&$movekey	($rndkey0,&QWP(0,$key));
	&shl		($rounds,4);
	&$movekey	($rndkey1,&QWP(16,$key));
	&xorps		($inout0,$rndkey0);
	&pxor		($inout1,$rndkey0);
	&$movekey	($rndkey0,&QWP(32,$key));
	&lea		($key,&DWP(32,$key,$rounds));
	&neg		($rounds);
	&add		($rounds,16);

    &set_label("${p}2_loop");
	eval"&aes${p}	($inout0,$rndkey1)";
	eval"&aes${p}	($inout1,$rndkey1)";
	&$movekey	($rndkey1,&QWP(0,$key,$rounds));
	&add		($rounds,32);
	eval"&aes${p}	($inout0,$rndkey0)";
	eval"&aes${p}	($inout1,$rndkey0)";
	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
	&jnz		(&label("${p}2_loop"));
    eval"&aes${p}	($inout0,$rndkey1)";
    eval"&aes${p}	($inout1,$rndkey1)";
    eval"&aes${p}last	($inout0,$rndkey0)";
    eval"&aes${p}last	($inout1,$rndkey0)";
    &ret();
    &function_end_B("_aesni_${p}rypt2");
}
275

A
Andy Polyakov 已提交
276 277 278 279 280
sub aesni_generate3
{ my $p=shift;

    &function_begin_B("_aesni_${p}rypt3");
	&$movekey	($rndkey0,&QWP(0,$key));
281
	&shl		($rounds,4);
A
Andy Polyakov 已提交
282
	&$movekey	($rndkey1,&QWP(16,$key));
283
	&xorps		($inout0,$rndkey0);
A
Andy Polyakov 已提交
284 285
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
286 287 288 289
	&$movekey	($rndkey0,&QWP(32,$key));
	&lea		($key,&DWP(32,$key,$rounds));
	&neg		($rounds);
	&add		($rounds,16);
290 291 292

    &set_label("${p}3_loop");
	eval"&aes${p}	($inout0,$rndkey1)";
A
Andy Polyakov 已提交
293 294
	eval"&aes${p}	($inout1,$rndkey1)";
	eval"&aes${p}	($inout2,$rndkey1)";
295 296
	&$movekey	($rndkey1,&QWP(0,$key,$rounds));
	&add		($rounds,32);
A
Andy Polyakov 已提交
297 298 299
	eval"&aes${p}	($inout0,$rndkey0)";
	eval"&aes${p}	($inout1,$rndkey0)";
	eval"&aes${p}	($inout2,$rndkey0)";
300
	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
A
Andy Polyakov 已提交
301 302 303 304 305 306 307 308 309 310
	&jnz		(&label("${p}3_loop"));
    eval"&aes${p}	($inout0,$rndkey1)";
    eval"&aes${p}	($inout1,$rndkey1)";
    eval"&aes${p}	($inout2,$rndkey1)";
    eval"&aes${p}last	($inout0,$rndkey0)";
    eval"&aes${p}last	($inout1,$rndkey0)";
    eval"&aes${p}last	($inout2,$rndkey0)";
    &ret();
    &function_end_B("_aesni_${p}rypt3");
}
A
Andy Polyakov 已提交
311 312 313 314 315 316 317 318 319 320 321

# 4x interleave is implemented to improve small block performance,
# most notably [and naturally] 4 block by ~30%. One can argue that one
# should have implemented 5x as well, but improvement  would be <20%,
# so it's not worth it...
sub aesni_generate4
{ my $p=shift;

    &function_begin_B("_aesni_${p}rypt4");
	&$movekey	($rndkey0,&QWP(0,$key));
	&$movekey	($rndkey1,&QWP(16,$key));
322
	&shl		($rounds,4);
323
	&xorps		($inout0,$rndkey0);
A
Andy Polyakov 已提交
324 325 326
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
	&pxor		($inout3,$rndkey0);
327 328 329 330 331
	&$movekey	($rndkey0,&QWP(32,$key));
	&lea		($key,&DWP(32,$key,$rounds));
	&neg		($rounds);
	&data_byte	(0x0f,0x1f,0x40,0x00);
	&add		($rounds,16);
332

333
    &set_label("${p}4_loop");
334
	eval"&aes${p}	($inout0,$rndkey1)";
A
Andy Polyakov 已提交
335 336 337
	eval"&aes${p}	($inout1,$rndkey1)";
	eval"&aes${p}	($inout2,$rndkey1)";
	eval"&aes${p}	($inout3,$rndkey1)";
338 339
	&$movekey	($rndkey1,&QWP(0,$key,$rounds));
	&add		($rounds,32);
A
Andy Polyakov 已提交
340 341 342 343
	eval"&aes${p}	($inout0,$rndkey0)";
	eval"&aes${p}	($inout1,$rndkey0)";
	eval"&aes${p}	($inout2,$rndkey0)";
	eval"&aes${p}	($inout3,$rndkey0)";
344
	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
345
    &jnz		(&label("${p}4_loop"));
346

A
Andy Polyakov 已提交
347 348 349 350 351 352 353 354 355 356 357
    eval"&aes${p}	($inout0,$rndkey1)";
    eval"&aes${p}	($inout1,$rndkey1)";
    eval"&aes${p}	($inout2,$rndkey1)";
    eval"&aes${p}	($inout3,$rndkey1)";
    eval"&aes${p}last	($inout0,$rndkey0)";
    eval"&aes${p}last	($inout1,$rndkey0)";
    eval"&aes${p}last	($inout2,$rndkey0)";
    eval"&aes${p}last	($inout3,$rndkey0)";
    &ret();
    &function_end_B("_aesni_${p}rypt4");
}
358 359 360 361 362 363 364

sub aesni_generate6
{ my $p=shift;

    &function_begin_B("_aesni_${p}rypt6");
    &static_label("_aesni_${p}rypt6_enter");
	&$movekey	($rndkey0,&QWP(0,$key));
365
	&shl		($rounds,4);
366 367 368 369
	&$movekey	($rndkey1,&QWP(16,$key));
	&xorps		($inout0,$rndkey0);
	&pxor		($inout1,$rndkey0);	# pxor does better here
	&pxor		($inout2,$rndkey0);
370
	eval"&aes${p}	($inout0,$rndkey1)";
371 372
	&pxor		($inout3,$rndkey0);
	&pxor		($inout4,$rndkey0);
373 374 375 376
	eval"&aes${p}	($inout1,$rndkey1)";
	&lea		($key,&DWP(32,$key,$rounds));
	&neg		($rounds);
	eval"&aes${p}	($inout2,$rndkey1)";
377
	&pxor		($inout5,$rndkey0);
378
	&$movekey	($rndkey0,&QWP(0,$key,$rounds));
379
	&add		($rounds,16);
380
	&jmp		(&label("_aesni_${p}rypt6_inner"));
381 382 383 384 385

    &set_label("${p}6_loop",16);
	eval"&aes${p}	($inout0,$rndkey1)";
	eval"&aes${p}	($inout1,$rndkey1)";
	eval"&aes${p}	($inout2,$rndkey1)";
386
    &set_label("_aesni_${p}rypt6_inner");
387 388 389
	eval"&aes${p}	($inout3,$rndkey1)";
	eval"&aes${p}	($inout4,$rndkey1)";
	eval"&aes${p}	($inout5,$rndkey1)";
390 391 392
    &set_label("_aesni_${p}rypt6_enter");
	&$movekey	($rndkey1,&QWP(0,$key,$rounds));
	&add		($rounds,32);
393 394 395 396 397 398
	eval"&aes${p}	($inout0,$rndkey0)";
	eval"&aes${p}	($inout1,$rndkey0)";
	eval"&aes${p}	($inout2,$rndkey0)";
	eval"&aes${p}	($inout3,$rndkey0)";
	eval"&aes${p}	($inout4,$rndkey0)";
	eval"&aes${p}	($inout5,$rndkey0)";
399
	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    &jnz		(&label("${p}6_loop"));

    eval"&aes${p}	($inout0,$rndkey1)";
    eval"&aes${p}	($inout1,$rndkey1)";
    eval"&aes${p}	($inout2,$rndkey1)";
    eval"&aes${p}	($inout3,$rndkey1)";
    eval"&aes${p}	($inout4,$rndkey1)";
    eval"&aes${p}	($inout5,$rndkey1)";
    eval"&aes${p}last	($inout0,$rndkey0)";
    eval"&aes${p}last	($inout1,$rndkey0)";
    eval"&aes${p}last	($inout2,$rndkey0)";
    eval"&aes${p}last	($inout3,$rndkey0)";
    eval"&aes${p}last	($inout4,$rndkey0)";
    eval"&aes${p}last	($inout5,$rndkey0)";
    &ret();
    &function_end_B("_aesni_${p}rypt6");
}
417 418
&aesni_generate2("enc") if ($PREFIX eq "aesni");
&aesni_generate2("dec");
A
Andy Polyakov 已提交
419 420
&aesni_generate3("enc") if ($PREFIX eq "aesni");
&aesni_generate3("dec");
A
Andy Polyakov 已提交
421 422
&aesni_generate4("enc") if ($PREFIX eq "aesni");
&aesni_generate4("dec");
423 424
&aesni_generate6("enc") if ($PREFIX eq "aesni");
&aesni_generate6("dec");
A
Andy Polyakov 已提交
425

A
Andy Polyakov 已提交
426
if ($PREFIX eq "aesni") {
A
Andy Polyakov 已提交
427
######################################################################
A
Andy Polyakov 已提交
428 429 430 431 432 433 434 435
# void aesni_ecb_encrypt (const void *in, void *out,
#                         size_t length, const AES_KEY *key,
#                         int enc);
&function_begin("aesni_ecb_encrypt");
	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
436
	&mov	($rounds_,&wparam(4));
A
Andy Polyakov 已提交
437
	&and	($len,-16);
438
	&jz	(&label("ecb_ret"));
A
Andy Polyakov 已提交
439
	&mov	($rounds,&DWP(240,$key));
440 441 442
	&test	($rounds_,$rounds_);
	&jz	(&label("ecb_decrypt"));

A
Andy Polyakov 已提交
443 444
	&mov	($key_,$key);		# backup $key
	&mov	($rounds_,$rounds);	# backup $rounds
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	&cmp	($len,0x60);
	&jb	(&label("ecb_enc_tail"));

	&movdqu	($inout0,&QWP(0,$inp));
	&movdqu	($inout1,&QWP(0x10,$inp));
	&movdqu	($inout2,&QWP(0x20,$inp));
	&movdqu	($inout3,&QWP(0x30,$inp));
	&movdqu	($inout4,&QWP(0x40,$inp));
	&movdqu	($inout5,&QWP(0x50,$inp));
	&lea	($inp,&DWP(0x60,$inp));
	&sub	($len,0x60);
	&jmp	(&label("ecb_enc_loop6_enter"));

&set_label("ecb_enc_loop6",16);
	&movups	(&QWP(0,$out),$inout0);
	&movdqu	($inout0,&QWP(0,$inp));
	&movups	(&QWP(0x10,$out),$inout1);
	&movdqu	($inout1,&QWP(0x10,$inp));
	&movups	(&QWP(0x20,$out),$inout2);
	&movdqu	($inout2,&QWP(0x20,$inp));
	&movups	(&QWP(0x30,$out),$inout3);
	&movdqu	($inout3,&QWP(0x30,$inp));
	&movups	(&QWP(0x40,$out),$inout4);
	&movdqu	($inout4,&QWP(0x40,$inp));
	&movups	(&QWP(0x50,$out),$inout5);
	&lea	($out,&DWP(0x60,$out));
	&movdqu	($inout5,&QWP(0x50,$inp));
	&lea	($inp,&DWP(0x60,$inp));
&set_label("ecb_enc_loop6_enter");
A
Andy Polyakov 已提交
474

475
	&call	("_aesni_encrypt6");
A
Andy Polyakov 已提交
476 477 478

	&mov	($key,$key_);		# restore $key
	&mov	($rounds,$rounds_);	# restore $rounds
479 480 481 482 483
	&sub	($len,0x60);
	&jnc	(&label("ecb_enc_loop6"));

	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
484
	&movups	(&QWP(0x20,$out),$inout2);
485 486 487 488 489 490
	&movups	(&QWP(0x30,$out),$inout3);
	&movups	(&QWP(0x40,$out),$inout4);
	&movups	(&QWP(0x50,$out),$inout5);
	&lea	($out,&DWP(0x60,$out));
	&add	($len,0x60);
	&jz	(&label("ecb_ret"));
A
Andy Polyakov 已提交
491

A
Andy Polyakov 已提交
492 493
&set_label("ecb_enc_tail");
	&movups	($inout0,&QWP(0,$inp));
494
	&cmp	($len,0x20);
A
Andy Polyakov 已提交
495
	&jb	(&label("ecb_enc_one"));
A
Andy Polyakov 已提交
496
	&movups	($inout1,&QWP(0x10,$inp));
A
Andy Polyakov 已提交
497 498
	&je	(&label("ecb_enc_two"));
	&movups	($inout2,&QWP(0x20,$inp));
499 500
	&cmp	($len,0x40);
	&jb	(&label("ecb_enc_three"));
A
Andy Polyakov 已提交
501
	&movups	($inout3,&QWP(0x30,$inp));
502 503 504 505
	&je	(&label("ecb_enc_four"));
	&movups	($inout4,&QWP(0x40,$inp));
	&xorps	($inout5,$inout5);
	&call	("_aesni_encrypt6");
A
Andy Polyakov 已提交
506 507
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
508 509
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
510
	&movups	(&QWP(0x40,$out),$inout4);
A
Andy Polyakov 已提交
511 512 513
	jmp	(&label("ecb_ret"));

&set_label("ecb_enc_one",16);
514 515 516 517
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
A
Andy Polyakov 已提交
518 519 520
	&movups	(&QWP(0,$out),$inout0);
	&jmp	(&label("ecb_ret"));

A
Andy Polyakov 已提交
521
&set_label("ecb_enc_two",16);
522
	&call	("_aesni_encrypt2");
A
Andy Polyakov 已提交
523 524 525 526 527 528 529 530 531 532
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&jmp	(&label("ecb_ret"));

&set_label("ecb_enc_three",16);
	&call	("_aesni_encrypt3");
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);
	&jmp	(&label("ecb_ret"));
533 534 535 536 537 538 539 540

&set_label("ecb_enc_four",16);
	&call	("_aesni_encrypt4");
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
	&jmp	(&label("ecb_ret"));
A
Andy Polyakov 已提交
541
######################################################################
A
Andy Polyakov 已提交
542
&set_label("ecb_decrypt",16);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	&mov	($key_,$key);		# backup $key
	&mov	($rounds_,$rounds);	# backup $rounds
	&cmp	($len,0x60);
	&jb	(&label("ecb_dec_tail"));

	&movdqu	($inout0,&QWP(0,$inp));
	&movdqu	($inout1,&QWP(0x10,$inp));
	&movdqu	($inout2,&QWP(0x20,$inp));
	&movdqu	($inout3,&QWP(0x30,$inp));
	&movdqu	($inout4,&QWP(0x40,$inp));
	&movdqu	($inout5,&QWP(0x50,$inp));
	&lea	($inp,&DWP(0x60,$inp));
	&sub	($len,0x60);
	&jmp	(&label("ecb_dec_loop6_enter"));

&set_label("ecb_dec_loop6",16);
559
	&movups	(&QWP(0,$out),$inout0);
560
	&movdqu	($inout0,&QWP(0,$inp));
561
	&movups	(&QWP(0x10,$out),$inout1);
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	&movdqu	($inout1,&QWP(0x10,$inp));
	&movups	(&QWP(0x20,$out),$inout2);
	&movdqu	($inout2,&QWP(0x20,$inp));
	&movups	(&QWP(0x30,$out),$inout3);
	&movdqu	($inout3,&QWP(0x30,$inp));
	&movups	(&QWP(0x40,$out),$inout4);
	&movdqu	($inout4,&QWP(0x40,$inp));
	&movups	(&QWP(0x50,$out),$inout5);
	&lea	($out,&DWP(0x60,$out));
	&movdqu	($inout5,&QWP(0x50,$inp));
	&lea	($inp,&DWP(0x60,$inp));
&set_label("ecb_dec_loop6_enter");

	&call	("_aesni_decrypt6");

	&mov	($key,$key_);		# restore $key
A
Andy Polyakov 已提交
578
	&mov	($rounds,$rounds_);	# restore $rounds
579 580 581 582 583
	&sub	($len,0x60);
	&jnc	(&label("ecb_dec_loop6"));

	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
584
	&movups	(&QWP(0x20,$out),$inout2);
585 586 587 588 589 590
	&movups	(&QWP(0x30,$out),$inout3);
	&movups	(&QWP(0x40,$out),$inout4);
	&movups	(&QWP(0x50,$out),$inout5);
	&lea	($out,&DWP(0x60,$out));
	&add	($len,0x60);
	&jz	(&label("ecb_ret"));
A
Andy Polyakov 已提交
591

A
Andy Polyakov 已提交
592 593
&set_label("ecb_dec_tail");
	&movups	($inout0,&QWP(0,$inp));
594
	&cmp	($len,0x20);
A
Andy Polyakov 已提交
595
	&jb	(&label("ecb_dec_one"));
A
Andy Polyakov 已提交
596
	&movups	($inout1,&QWP(0x10,$inp));
A
Andy Polyakov 已提交
597 598
	&je	(&label("ecb_dec_two"));
	&movups	($inout2,&QWP(0x20,$inp));
599 600
	&cmp	($len,0x40);
	&jb	(&label("ecb_dec_three"));
A
Andy Polyakov 已提交
601
	&movups	($inout3,&QWP(0x30,$inp));
602 603 604 605
	&je	(&label("ecb_dec_four"));
	&movups	($inout4,&QWP(0x40,$inp));
	&xorps	($inout5,$inout5);
	&call	("_aesni_decrypt6");
A
Andy Polyakov 已提交
606 607
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
608 609
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
610
	&movups	(&QWP(0x40,$out),$inout4);
A
Andy Polyakov 已提交
611
	&jmp	(&label("ecb_ret"));
A
Andy Polyakov 已提交
612 613

&set_label("ecb_dec_one",16);
614 615 616 617
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
A
Andy Polyakov 已提交
618
	&movups	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
619 620 621
	&jmp	(&label("ecb_ret"));

&set_label("ecb_dec_two",16);
622
	&call	("_aesni_decrypt2");
A
Andy Polyakov 已提交
623 624 625 626 627 628 629 630 631
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&jmp	(&label("ecb_ret"));

&set_label("ecb_dec_three",16);
	&call	("_aesni_decrypt3");
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);
632 633 634 635 636 637 638 639
	&jmp	(&label("ecb_ret"));

&set_label("ecb_dec_four",16);
	&call	("_aesni_decrypt4");
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
A
Andy Polyakov 已提交
640 641

&set_label("ecb_ret");
642 643 644 645 646 647 648 649
	&pxor	("xmm0","xmm0");		# clear register bank
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&pxor	("xmm3","xmm3");
	&pxor	("xmm4","xmm4");
	&pxor	("xmm5","xmm5");
	&pxor	("xmm6","xmm6");
	&pxor	("xmm7","xmm7");
A
Andy Polyakov 已提交
650
&function_end("aesni_ecb_encrypt");
A
Andy Polyakov 已提交
651 652

######################################################################
653 654 655 656 657 658 659
# void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
#                         size_t blocks, const AES_KEY *key,
#                         const char *ivec,char *cmac);
#
# Handles only complete blocks, operates on 64-bit counter and
# does not update *ivec! Nor does it finalize CMAC value
# (see engine/eng_aesni.c for details)
A
Andy Polyakov 已提交
660
#
661
{ my $cmac=$inout1;
662 663 664 665 666 667 668 669 670 671 672 673 674
&function_begin("aesni_ccm64_encrypt_blocks");
	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
	&mov	($rounds_,&wparam(4));
	&mov	($rounds,&wparam(5));
	&mov	($key_,"esp");
	&sub	("esp",60);
	&and	("esp",-16);			# align stack
	&mov	(&DWP(48,"esp"),$key_);

	&movdqu	($ivec,&QWP(0,$rounds_));	# load ivec
675
	&movdqu	($cmac,&QWP(0,$rounds));	# load cmac
676
	&mov	($rounds,&DWP(240,$key));
677 678 679 680 681 682 683 684

	# compose byte-swap control mask for pshufb on stack
	&mov	(&DWP(0,"esp"),0x0c0d0e0f);
	&mov	(&DWP(4,"esp"),0x08090a0b);
	&mov	(&DWP(8,"esp"),0x04050607);
	&mov	(&DWP(12,"esp"),0x00010203);

	# compose counter increment vector on stack
685
	&mov	($rounds_,1);
686
	&xor	($key_,$key_);
687
	&mov	(&DWP(16,"esp"),$rounds_);
688 689 690 691
	&mov	(&DWP(20,"esp"),$key_);
	&mov	(&DWP(24,"esp"),$key_);
	&mov	(&DWP(28,"esp"),$key_);

692 693
	&shl	($rounds,4);
	&mov	($rounds_,16);
694
	&lea	($key_,&DWP(0,$key));
695
	&movdqa	($inout3,&QWP(0,"esp"));
696
	&movdqa	($inout0,$ivec);
697 698
	&lea	($key,&DWP(32,$key,$rounds));
	&sub	($rounds_,$rounds);
699
	&pshufb	($ivec,$inout3);
700 701

&set_label("ccm64_enc_outer");
702
	&$movekey	($rndkey0,&QWP(0,$key_));
703
	&mov		($rounds,$rounds_);
704
	&movups		($in0,&QWP(0,$inp));
705

706
	&xorps		($inout0,$rndkey0);
707 708 709
	&$movekey	($rndkey1,&QWP(16,$key_));
	&xorps		($rndkey0,$in0);
	&xorps		($cmac,$rndkey0);		# cmac^=inp
710
	&$movekey	($rndkey0,&QWP(32,$key_));
711 712 713 714

&set_label("ccm64_enc2_loop");
	&aesenc		($inout0,$rndkey1);
	&aesenc		($cmac,$rndkey1);
715 716
	&$movekey	($rndkey1,&QWP(0,$key,$rounds));
	&add		($rounds,32);
717 718
	&aesenc		($inout0,$rndkey0);
	&aesenc		($cmac,$rndkey0);
719
	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
720 721 722
	&jnz		(&label("ccm64_enc2_loop"));
	&aesenc		($inout0,$rndkey1);
	&aesenc		($cmac,$rndkey1);
723
	&paddq		($ivec,&QWP(16,"esp"));
724
	&dec		($len);
725 726
	&aesenclast	($inout0,$rndkey0);
	&aesenclast	($cmac,$rndkey0);
727 728

	&lea	($inp,&DWP(16,$inp));
729
	&xorps	($in0,$inout0);			# inp^=E(ivec)
730
	&movdqa	($inout0,$ivec);
731
	&movups	(&QWP(0,$out),$in0);		# save output
732
	&pshufb	($inout0,$inout3);
733
	&lea	($out,&DWP(16,$out));
734 735 736 737
	&jnz	(&label("ccm64_enc_outer"));

	&mov	("esp",&DWP(48,"esp"));
	&mov	($out,&wparam(5));
738
	&movups	(&QWP(0,$out),$cmac);
739 740 741 742 743 744 745 746 747

	&pxor	("xmm0","xmm0");		# clear register bank
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&pxor	("xmm3","xmm3");
	&pxor	("xmm4","xmm4");
	&pxor	("xmm5","xmm5");
	&pxor	("xmm6","xmm6");
	&pxor	("xmm7","xmm7");
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
&function_end("aesni_ccm64_encrypt_blocks");

&function_begin("aesni_ccm64_decrypt_blocks");
	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
	&mov	($rounds_,&wparam(4));
	&mov	($rounds,&wparam(5));
	&mov	($key_,"esp");
	&sub	("esp",60);
	&and	("esp",-16);			# align stack
	&mov	(&DWP(48,"esp"),$key_);

	&movdqu	($ivec,&QWP(0,$rounds_));	# load ivec
763
	&movdqu	($cmac,&QWP(0,$rounds));	# load cmac
764
	&mov	($rounds,&DWP(240,$key));
765 766 767 768 769 770 771 772

	# compose byte-swap control mask for pshufb on stack
	&mov	(&DWP(0,"esp"),0x0c0d0e0f);
	&mov	(&DWP(4,"esp"),0x08090a0b);
	&mov	(&DWP(8,"esp"),0x04050607);
	&mov	(&DWP(12,"esp"),0x00010203);

	# compose counter increment vector on stack
773
	&mov	($rounds_,1);
774
	&xor	($key_,$key_);
775
	&mov	(&DWP(16,"esp"),$rounds_);
776 777 778 779 780 781 782 783 784 785
	&mov	(&DWP(20,"esp"),$key_);
	&mov	(&DWP(24,"esp"),$key_);
	&mov	(&DWP(28,"esp"),$key_);

	&movdqa	($inout3,&QWP(0,"esp"));	# bswap mask
	&movdqa	($inout0,$ivec);

	&mov	($key_,$key);
	&mov	($rounds_,$rounds);

786
	&pshufb	($ivec,$inout3);
787 788 789 790
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
791 792
	&shl	($rounds_,4);
	&mov	($rounds,16);
793
	&movups	($in0,&QWP(0,$inp));		# load inp
794
	&paddq	($ivec,&QWP(16,"esp"));
795
	&lea	($inp,&QWP(16,$inp));
796 797 798
	&sub	($rounds,$rounds_);
	&lea	($key,&DWP(32,$key_,$rounds_));
	&mov	($rounds_,$rounds);
799 800 801 802 803 804
	&jmp	(&label("ccm64_dec_outer"));

&set_label("ccm64_dec_outer",16);
	&xorps	($in0,$inout0);			# inp ^= E(ivec)
	&movdqa	($inout0,$ivec);
	&movups	(&QWP(0,$out),$in0);		# save output
805
	&lea	($out,&DWP(16,$out));
806
	&pshufb	($inout0,$inout3);
807

808
	&sub	($len,1);
809 810
	&jz	(&label("ccm64_dec_break"));

811
	&$movekey	($rndkey0,&QWP(0,$key_));
812
	&mov		($rounds,$rounds_);
813
	&$movekey	($rndkey1,&QWP(16,$key_));
814 815 816
	&xorps		($in0,$rndkey0);
	&xorps		($inout0,$rndkey0);
	&xorps		($cmac,$in0);		# cmac^=out
817
	&$movekey	($rndkey0,&QWP(32,$key_));
818

819 820 821
&set_label("ccm64_dec2_loop");
	&aesenc		($inout0,$rndkey1);
	&aesenc		($cmac,$rndkey1);
822 823
	&$movekey	($rndkey1,&QWP(0,$key,$rounds));
	&add		($rounds,32);
824 825
	&aesenc		($inout0,$rndkey0);
	&aesenc		($cmac,$rndkey0);
826
	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
827
	&jnz		(&label("ccm64_dec2_loop"));
828 829
	&movups		($in0,&QWP(0,$inp));	# load inp
	&paddq		($ivec,&QWP(16,"esp"));
830 831 832 833
	&aesenc		($inout0,$rndkey1);
	&aesenc		($cmac,$rndkey1);
	&aesenclast	($inout0,$rndkey0);
	&aesenclast	($cmac,$rndkey0);
834
	&lea		($inp,&QWP(16,$inp));
835 836 837
	&jmp	(&label("ccm64_dec_outer"));

&set_label("ccm64_dec_break",16);
838
	&mov	($rounds,&DWP(240,$key_));
839
	&mov	($key,$key_);
840
	if ($inline)
841
	{   &aesni_inline_generate1("enc",$cmac,$in0);	}
842
	else
843
	{   &call	("_aesni_encrypt1",$cmac);	}
844 845 846

	&mov	("esp",&DWP(48,"esp"));
	&mov	($out,&wparam(5));
847
	&movups	(&QWP(0,$out),$cmac);
848 849 850 851 852 853 854 855 856

	&pxor	("xmm0","xmm0");		# clear register bank
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&pxor	("xmm3","xmm3");
	&pxor	("xmm4","xmm4");
	&pxor	("xmm5","xmm5");
	&pxor	("xmm6","xmm6");
	&pxor	("xmm7","xmm7");
857
&function_end("aesni_ccm64_decrypt_blocks");
858
}
859 860

######################################################################
A
Andy Polyakov 已提交
861 862 863
# void aesni_ctr32_encrypt_blocks (const void *in, void *out,
#                         size_t blocks, const AES_KEY *key,
#                         const char *ivec);
864 865
#
# Handles only complete blocks, operates on 32-bit counter and
866
# does not update *ivec! (see crypto/modes/ctr128.c for details)
867
#
868 869 870 871 872 873 874 875
# stack layout:
#	0	pshufb mask
#	16	vector addend: 0,6,6,6
# 	32	counter-less ivec
#	48	1st triplet of counter vector
#	64	2nd triplet of counter vector
#	80	saved %esp

A
Andy Polyakov 已提交
876 877 878 879 880 881 882
&function_begin("aesni_ctr32_encrypt_blocks");
	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
	&mov	($rounds_,&wparam(4));
	&mov	($key_,"esp");
883
	&sub	("esp",88);
A
Andy Polyakov 已提交
884
	&and	("esp",-16);			# align stack
885
	&mov	(&DWP(80,"esp"),$key_);
A
Andy Polyakov 已提交
886

887 888 889
	&cmp	($len,1);
	&je	(&label("ctr32_one_shortcut"));

890
	&movdqu	($inout5,&QWP(0,$rounds_));	# load ivec
A
Andy Polyakov 已提交
891 892 893 894 895 896 897 898

	# compose byte-swap control mask for pshufb on stack
	&mov	(&DWP(0,"esp"),0x0c0d0e0f);
	&mov	(&DWP(4,"esp"),0x08090a0b);
	&mov	(&DWP(8,"esp"),0x04050607);
	&mov	(&DWP(12,"esp"),0x00010203);

	# compose counter increment vector on stack
899
	&mov	($rounds,6);
A
Andy Polyakov 已提交
900 901 902 903 904 905
	&xor	($key_,$key_);
	&mov	(&DWP(16,"esp"),$rounds);
	&mov	(&DWP(20,"esp"),$rounds);
	&mov	(&DWP(24,"esp"),$rounds);
	&mov	(&DWP(28,"esp"),$key_);

906 907
	&pextrd	($rounds_,$inout5,3);		# pull 32-bit counter
	&pinsrd	($inout5,$key_,3);		# wipe 32-bit counter
A
Andy Polyakov 已提交
908 909 910

	&mov	($rounds,&DWP(240,$key));	# key->rounds

911
	# compose 2 vectors of 3x32-bit counters
A
Andy Polyakov 已提交
912
	&bswap	($rounds_);
913
	&pxor	($rndkey0,$rndkey0);
914
	&pxor	($rndkey1,$rndkey1);
915
	&movdqa	($inout0,&QWP(0,"esp"));	# load byte-swap mask
916
	&pinsrd	($rndkey0,$rounds_,0);
917
	&lea	($key_,&DWP(3,$rounds_));
918
	&pinsrd	($rndkey1,$key_,0);
A
Andy Polyakov 已提交
919
	&inc	($rounds_);
920
	&pinsrd	($rndkey0,$rounds_,1);
921
	&inc	($key_);
922
	&pinsrd	($rndkey1,$key_,1);
A
Andy Polyakov 已提交
923
	&inc	($rounds_);
924
	&pinsrd	($rndkey0,$rounds_,2);
925
	&inc	($key_);
926 927
	&pinsrd	($rndkey1,$key_,2);
	&movdqa	(&QWP(48,"esp"),$rndkey0);	# save 1st triplet
928
	&pshufb	($rndkey0,$inout0);		# byte swap
929 930 931
	&movdqu	($inout4,&QWP(0,$key));		# key[0]
	&movdqa	(&QWP(64,"esp"),$rndkey1);	# save 2nd triplet
	&pshufb	($rndkey1,$inout0);		# byte swap
932

933 934
	&pshufd	($inout0,$rndkey0,3<<6);	# place counter to upper dword
	&pshufd	($inout1,$rndkey0,2<<6);
935 936
	&cmp	($len,6);
	&jb	(&label("ctr32_tail"));
937 938 939 940
	&pxor	($inout5,$inout4);		# counter-less ivec^key[0]
	&shl	($rounds,4);
	&mov	($rounds_,16);
	&movdqa	(&QWP(32,"esp"),$inout5);	# save counter-less ivec^key[0]
941
	&mov	($key_,$key);			# backup $key
942 943
	&sub	($rounds_,$rounds);		# backup twisted $rounds
	&lea	($key,&DWP(32,$key,$rounds));
944 945 946 947
	&sub	($len,6);
	&jmp	(&label("ctr32_loop6"));

&set_label("ctr32_loop6",16);
948 949 950 951 952 953
	# inlining _aesni_encrypt6's prologue gives ~6% improvement...
	&pshufd	($inout2,$rndkey0,1<<6);
	&movdqa	($rndkey0,&QWP(32,"esp"));	# pull counter-less ivec
	&pshufd	($inout3,$rndkey1,3<<6);
	&pxor		($inout0,$rndkey0);	# merge counter-less ivec
	&pshufd	($inout4,$rndkey1,2<<6);
954
	&pxor		($inout1,$rndkey0);
955 956
	&pshufd	($inout5,$rndkey1,1<<6);
	&$movekey	($rndkey1,&QWP(16,$key_));
957 958
	&pxor		($inout2,$rndkey0);
	&pxor		($inout3,$rndkey0);
959
	&aesenc		($inout0,$rndkey1);
960 961
	&pxor		($inout4,$rndkey0);
	&pxor		($inout5,$rndkey0);
962 963 964 965 966
	&aesenc		($inout1,$rndkey1);
	&$movekey	($rndkey0,&QWP(32,$key_));
	&mov		($rounds,$rounds_);
	&aesenc		($inout2,$rndkey1);
	&aesenc		($inout3,$rndkey1);
967 968
	&aesenc		($inout4,$rndkey1);
	&aesenc		($inout5,$rndkey1);
969

970 971 972 973 974 975 976 977 978 979
	&call		(&label("_aesni_encrypt6_enter"));

	&movups	($rndkey1,&QWP(0,$inp));
	&movups	($rndkey0,&QWP(0x10,$inp));
	&xorps	($inout0,$rndkey1);
	&movups	($rndkey1,&QWP(0x20,$inp));
	&xorps	($inout1,$rndkey0);
	&movups	(&QWP(0,$out),$inout0);
	&movdqa	($rndkey0,&QWP(16,"esp"));	# load increment
	&xorps	($inout2,$rndkey1);
980
	&movdqa	($rndkey1,&QWP(64,"esp"));	# load 2nd triplet
981 982 983
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);

984 985
	&paddd	($rndkey1,$rndkey0);		# 2nd triplet increment
	&paddd	($rndkey0,&QWP(48,"esp"));	# 1st triplet increment
986 987 988 989 990 991 992
	&movdqa	($inout0,&QWP(0,"esp"));	# load byte swap mask

	&movups	($inout1,&QWP(0x30,$inp));
	&movups	($inout2,&QWP(0x40,$inp));
	&xorps	($inout3,$inout1);
	&movups	($inout1,&QWP(0x50,$inp));
	&lea	($inp,&DWP(0x60,$inp));
993 994
	&movdqa	(&QWP(48,"esp"),$rndkey0);	# save 1st triplet
	&pshufb	($rndkey0,$inout0);		# byte swap
995 996 997
	&xorps	($inout4,$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
	&xorps	($inout5,$inout1);
998 999
	&movdqa	(&QWP(64,"esp"),$rndkey1);	# save 2nd triplet
	&pshufb	($rndkey1,$inout0);		# byte swap
1000
	&movups	(&QWP(0x40,$out),$inout4);
1001
	&pshufd	($inout0,$rndkey0,3<<6);
1002 1003
	&movups	(&QWP(0x50,$out),$inout5);
	&lea	($out,&DWP(0x60,$out));
1004

1005
	&pshufd	($inout1,$rndkey0,2<<6);
1006 1007
	&sub	($len,6);
	&jnc	(&label("ctr32_loop6"));
A
Andy Polyakov 已提交
1008

1009 1010
	&add	($len,6);
	&jz	(&label("ctr32_ret"));
1011
	&movdqu	($inout5,&QWP(0,$key_));
1012
	&mov	($key,$key_);
1013 1014
	&pxor	($inout5,&QWP(32,"esp"));	# restore count-less ivec
	&mov	($rounds,&DWP(240,$key_));	# restore $rounds
A
Andy Polyakov 已提交
1015 1016

&set_label("ctr32_tail");
1017
	&por	($inout0,$inout5);
1018
	&cmp	($len,2);
A
Andy Polyakov 已提交
1019 1020
	&jb	(&label("ctr32_one"));

1021
	&pshufd	($inout2,$rndkey0,1<<6);
1022 1023
	&por	($inout1,$inout5);
	&je	(&label("ctr32_two"));
A
Andy Polyakov 已提交
1024

1025
	&pshufd	($inout3,$rndkey1,3<<6);
1026 1027 1028 1029
	&por	($inout2,$inout5);
	&cmp	($len,4);
	&jb	(&label("ctr32_three"));

1030
	&pshufd	($inout4,$rndkey1,2<<6);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	&por	($inout3,$inout5);
	&je	(&label("ctr32_four"));

	&por	($inout4,$inout5);
	&call	("_aesni_encrypt6");
	&movups	($rndkey1,&QWP(0,$inp));
	&movups	($rndkey0,&QWP(0x10,$inp));
	&xorps	($inout0,$rndkey1);
	&movups	($rndkey1,&QWP(0x20,$inp));
	&xorps	($inout1,$rndkey0);
	&movups	($rndkey0,&QWP(0x30,$inp));
	&xorps	($inout2,$rndkey1);
	&movups	($rndkey1,&QWP(0x40,$inp));
	&xorps	($inout3,$rndkey0);
	&movups	(&QWP(0,$out),$inout0);
	&xorps	($inout4,$rndkey1);
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
	&movups	(&QWP(0x40,$out),$inout4);
A
Andy Polyakov 已提交
1051 1052
	&jmp	(&label("ctr32_ret"));

1053
&set_label("ctr32_one_shortcut",16);
1054
	&movups	($inout0,&QWP(0,$rounds_));	# load ivec
1055
	&mov	($rounds,&DWP(240,$key));
1056

1057
&set_label("ctr32_one");
A
Andy Polyakov 已提交
1058 1059 1060 1061
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
1062 1063 1064
	&movups	($in0,&QWP(0,$inp));
	&xorps	($in0,$inout0);
	&movups	(&QWP(0,$out),$in0);
A
Andy Polyakov 已提交
1065
	&jmp	(&label("ctr32_ret"));
A
Andy Polyakov 已提交
1066

A
Andy Polyakov 已提交
1067
&set_label("ctr32_two",16);
1068
	&call	("_aesni_encrypt2");
1069 1070 1071 1072 1073 1074
	&movups	($inout3,&QWP(0,$inp));
	&movups	($inout4,&QWP(0x10,$inp));
	&xorps	($inout0,$inout3);
	&xorps	($inout1,$inout4);
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
1075 1076 1077 1078
	&jmp	(&label("ctr32_ret"));

&set_label("ctr32_three",16);
	&call	("_aesni_encrypt3");
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	&movups	($inout3,&QWP(0,$inp));
	&movups	($inout4,&QWP(0x10,$inp));
	&xorps	($inout0,$inout3);
	&movups	($inout5,&QWP(0x20,$inp));
	&xorps	($inout1,$inout4);
	&movups	(&QWP(0,$out),$inout0);
	&xorps	($inout2,$inout5);
	&movups	(&QWP(0x10,$out),$inout1);
	&movups	(&QWP(0x20,$out),$inout2);
	&jmp	(&label("ctr32_ret"));

&set_label("ctr32_four",16);
	&call	("_aesni_encrypt4");
	&movups	($inout4,&QWP(0,$inp));
	&movups	($inout5,&QWP(0x10,$inp));
	&movups	($rndkey1,&QWP(0x20,$inp));
	&xorps	($inout0,$inout4);
	&movups	($rndkey0,&QWP(0x30,$inp));
	&xorps	($inout1,$inout5);
	&movups	(&QWP(0,$out),$inout0);
	&xorps	($inout2,$rndkey1);
	&movups	(&QWP(0x10,$out),$inout1);
	&xorps	($inout3,$rndkey0);
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
A
Andy Polyakov 已提交
1104 1105

&set_label("ctr32_ret");
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	&pxor	("xmm0","xmm0");		# clear register bank
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&pxor	("xmm3","xmm3");
	&pxor	("xmm4","xmm4");
	&movdqa	(&QWP(32,"esp"),"xmm0");	# clear stack
	&pxor	("xmm5","xmm5");
	&movdqa	(&QWP(48,"esp"),"xmm0");
	&pxor	("xmm6","xmm6");
	&movdqa	(&QWP(64,"esp"),"xmm0");
	&pxor	("xmm7","xmm7");
1117
	&mov	("esp",&DWP(80,"esp"));
A
Andy Polyakov 已提交
1118
&function_end("aesni_ctr32_encrypt_blocks");
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

######################################################################
# void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
#	const AES_KEY *key1, const AES_KEY *key2
#	const unsigned char iv[16]);
#
{ my ($tweak,$twtmp,$twres,$twmask)=($rndkey1,$rndkey0,$inout0,$inout1);

&function_begin("aesni_xts_encrypt");
	&mov	($key,&wparam(4));		# key2
	&mov	($inp,&wparam(5));		# clear-text tweak

	&mov	($rounds,&DWP(240,$key));	# key2->rounds
	&movups	($inout0,&QWP(0,$inp));
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}

	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));		# key1

	&mov	($key_,"esp");
	&sub	("esp",16*7+8);
	&mov	($rounds,&DWP(240,$key));	# key1->rounds
	&and	("esp",-16);			# align stack

	&mov	(&DWP(16*6+0,"esp"),0x87);	# compose the magic constant
	&mov	(&DWP(16*6+4,"esp"),0);
	&mov	(&DWP(16*6+8,"esp"),1);
	&mov	(&DWP(16*6+12,"esp"),0);
	&mov	(&DWP(16*7+0,"esp"),$len);	# save original $len
	&mov	(&DWP(16*7+4,"esp"),$key_);	# save original %esp

	&movdqa	($tweak,$inout0);
	&pxor	($twtmp,$twtmp);
	&movdqa	($twmask,&QWP(6*16,"esp"));	# 0x0...010...87
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits

	&and	($len,-16);
	&mov	($key_,$key);			# backup $key
	&mov	($rounds_,$rounds);		# backup $rounds
	&sub	($len,16*6);
	&jc	(&label("xts_enc_short"));

1166 1167 1168 1169
	&shl	($rounds,4);
	&mov	($rounds_,16);
	&sub	($rounds_,$rounds);
	&lea	($key,&DWP(32,$key,$rounds));
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	&jmp	(&label("xts_enc_loop6"));

&set_label("xts_enc_loop6",16);
	for ($i=0;$i<4;$i++) {
	    &pshufd	($twres,$twtmp,0x13);
	    &pxor	($twtmp,$twtmp);
	    &movdqa	(&QWP(16*$i,"esp"),$tweak);
	    &paddq	($tweak,$tweak);	# &psllq($tweak,1);
	    &pand	($twres,$twmask);	# isolate carry and residue
	    &pcmpgtd	($twtmp,$tweak);	# broadcast upper bits
	    &pxor	($tweak,$twres);
	}
	&pshufd	($inout5,$twtmp,0x13);
	&movdqa	(&QWP(16*$i++,"esp"),$tweak);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	 &$movekey	($rndkey0,&QWP(0,$key_));
	&pand	($inout5,$twmask);		# isolate carry and residue
	 &movups	($inout0,&QWP(0,$inp));	# load input
	&pxor	($inout5,$tweak);

	# inline _aesni_encrypt6 prologue and flip xor with tweak and key[0]
1191
	&mov	($rounds,$rounds_);		# restore $rounds
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	&movdqu	($inout1,&QWP(16*1,$inp));
	 &xorps		($inout0,$rndkey0);	# input^=rndkey[0]
	&movdqu	($inout2,&QWP(16*2,$inp));
	 &pxor		($inout1,$rndkey0);
	&movdqu	($inout3,&QWP(16*3,$inp));
	 &pxor		($inout2,$rndkey0);
	&movdqu	($inout4,&QWP(16*4,$inp));
	 &pxor		($inout3,$rndkey0);
	&movdqu	($rndkey1,&QWP(16*5,$inp));
	 &pxor		($inout4,$rndkey0);
	&lea	($inp,&DWP(16*6,$inp));
	&pxor	($inout0,&QWP(16*0,"esp"));	# input^=tweak
	&movdqa	(&QWP(16*$i,"esp"),$inout5);	# save last tweak
	&pxor	($inout5,$rndkey1);

	 &$movekey	($rndkey1,&QWP(16,$key_));
	&pxor	($inout1,&QWP(16*1,"esp"));
	&pxor	($inout2,&QWP(16*2,"esp"));
1210
	 &aesenc	($inout0,$rndkey1);
1211 1212
	&pxor	($inout3,&QWP(16*3,"esp"));
	&pxor	($inout4,&QWP(16*4,"esp"));
1213
	 &aesenc	($inout1,$rndkey1);
1214
	&pxor		($inout5,$rndkey0);
1215 1216 1217
	 &$movekey	($rndkey0,&QWP(32,$key_));
	 &aesenc	($inout2,$rndkey1);
	 &aesenc	($inout3,$rndkey1);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
	 &aesenc	($inout4,$rndkey1);
	 &aesenc	($inout5,$rndkey1);
	&call		(&label("_aesni_encrypt6_enter"));

	&movdqa	($tweak,&QWP(16*5,"esp"));	# last tweak
       &pxor	($twtmp,$twtmp);
	&xorps	($inout0,&QWP(16*0,"esp"));	# output^=tweak
       &pcmpgtd	($twtmp,$tweak);		# broadcast upper bits
	&xorps	($inout1,&QWP(16*1,"esp"));
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&xorps	($inout2,&QWP(16*2,"esp"));
	&movups	(&QWP(16*1,$out),$inout1);
	&xorps	($inout3,&QWP(16*3,"esp"));
	&movups	(&QWP(16*2,$out),$inout2);
	&xorps	($inout4,&QWP(16*4,"esp"));
	&movups	(&QWP(16*3,$out),$inout3);
	&xorps	($inout5,$tweak);
	&movups	(&QWP(16*4,$out),$inout4);
       &pshufd	($twres,$twtmp,0x13);
	&movups	(&QWP(16*5,$out),$inout5);
	&lea	($out,&DWP(16*6,$out));
       &movdqa	($twmask,&QWP(16*6,"esp"));	# 0x0...010...87

	&pxor	($twtmp,$twtmp);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);

	&sub	($len,16*6);
	&jnc	(&label("xts_enc_loop6"));

1250
	&mov	($rounds,&DWP(240,$key_));	# restore $rounds
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	&mov	($key,$key_);			# restore $key
	&mov	($rounds_,$rounds);

&set_label("xts_enc_short");
	&add	($len,16*6);
	&jz	(&label("xts_enc_done6x"));

	&movdqa	($inout3,$tweak);		# put aside previous tweak
	&cmp	($len,0x20);
	&jb	(&label("xts_enc_one"));

	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);
	&je	(&label("xts_enc_two"));

	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&movdqa	($inout4,$tweak);		# put aside previous tweak
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);
	&cmp	($len,0x40);
	&jb	(&label("xts_enc_three"));

	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&movdqa	($inout5,$tweak);		# put aside previous tweak
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);
	&movdqa	(&QWP(16*0,"esp"),$inout3);
	&movdqa	(&QWP(16*1,"esp"),$inout4);
	&je	(&label("xts_enc_four"));

	&movdqa	(&QWP(16*2,"esp"),$inout5);
	&pshufd	($inout5,$twtmp,0x13);
	&movdqa	(&QWP(16*3,"esp"),$tweak);
	&paddq	($tweak,$tweak);		# &psllq($inout0,1);
	&pand	($inout5,$twmask);		# isolate carry and residue
	&pxor	($inout5,$tweak);

	&movdqu	($inout0,&QWP(16*0,$inp));	# load input
	&movdqu	($inout1,&QWP(16*1,$inp));
	&movdqu	($inout2,&QWP(16*2,$inp));
	&pxor	($inout0,&QWP(16*0,"esp"));	# input^=tweak
	&movdqu	($inout3,&QWP(16*3,$inp));
	&pxor	($inout1,&QWP(16*1,"esp"));
	&movdqu	($inout4,&QWP(16*4,$inp));
	&pxor	($inout2,&QWP(16*2,"esp"));
	&lea	($inp,&DWP(16*5,$inp));
	&pxor	($inout3,&QWP(16*3,"esp"));
	&movdqa	(&QWP(16*4,"esp"),$inout5);	# save last tweak
	&pxor	($inout4,$inout5);

	&call	("_aesni_encrypt6");

	&movaps	($tweak,&QWP(16*4,"esp"));	# last tweak
	&xorps	($inout0,&QWP(16*0,"esp"));	# output^=tweak
	&xorps	($inout1,&QWP(16*1,"esp"));
	&xorps	($inout2,&QWP(16*2,"esp"));
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&xorps	($inout3,&QWP(16*3,"esp"));
	&movups	(&QWP(16*1,$out),$inout1);
	&xorps	($inout4,$tweak);
	&movups	(&QWP(16*2,$out),$inout2);
	&movups	(&QWP(16*3,$out),$inout3);
	&movups	(&QWP(16*4,$out),$inout4);
	&lea	($out,&DWP(16*5,$out));
	&jmp	(&label("xts_enc_done"));

&set_label("xts_enc_one",16);
	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&lea	($inp,&DWP(16*1,$inp));
	&xorps	($inout0,$inout3);		# input^=tweak
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
	&xorps	($inout0,$inout3);		# output^=tweak
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&lea	($out,&DWP(16*1,$out));

	&movdqa	($tweak,$inout3);		# last tweak
	&jmp	(&label("xts_enc_done"));

&set_label("xts_enc_two",16);
	&movaps	($inout4,$tweak);		# put aside last tweak

	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&movups	($inout1,&QWP(16*1,$inp));
	&lea	($inp,&DWP(16*2,$inp));
	&xorps	($inout0,$inout3);		# input^=tweak
	&xorps	($inout1,$inout4);

1351
	&call	("_aesni_encrypt2");
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

	&xorps	($inout0,$inout3);		# output^=tweak
	&xorps	($inout1,$inout4);
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&movups	(&QWP(16*1,$out),$inout1);
	&lea	($out,&DWP(16*2,$out));

	&movdqa	($tweak,$inout4);		# last tweak
	&jmp	(&label("xts_enc_done"));

&set_label("xts_enc_three",16);
	&movaps	($inout5,$tweak);		# put aside last tweak
	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&movups	($inout1,&QWP(16*1,$inp));
	&movups	($inout2,&QWP(16*2,$inp));
	&lea	($inp,&DWP(16*3,$inp));
	&xorps	($inout0,$inout3);		# input^=tweak
	&xorps	($inout1,$inout4);
	&xorps	($inout2,$inout5);

	&call	("_aesni_encrypt3");

	&xorps	($inout0,$inout3);		# output^=tweak
	&xorps	($inout1,$inout4);
	&xorps	($inout2,$inout5);
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&movups	(&QWP(16*1,$out),$inout1);
	&movups	(&QWP(16*2,$out),$inout2);
	&lea	($out,&DWP(16*3,$out));

	&movdqa	($tweak,$inout5);		# last tweak
	&jmp	(&label("xts_enc_done"));

&set_label("xts_enc_four",16);
	&movaps	($inout4,$tweak);		# put aside last tweak

	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&movups	($inout1,&QWP(16*1,$inp));
	&movups	($inout2,&QWP(16*2,$inp));
	&xorps	($inout0,&QWP(16*0,"esp"));	# input^=tweak
	&movups	($inout3,&QWP(16*3,$inp));
	&lea	($inp,&DWP(16*4,$inp));
	&xorps	($inout1,&QWP(16*1,"esp"));
	&xorps	($inout2,$inout5);
	&xorps	($inout3,$inout4);

	&call	("_aesni_encrypt4");

	&xorps	($inout0,&QWP(16*0,"esp"));	# output^=tweak
	&xorps	($inout1,&QWP(16*1,"esp"));
	&xorps	($inout2,$inout5);
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&xorps	($inout3,$inout4);
	&movups	(&QWP(16*1,$out),$inout1);
	&movups	(&QWP(16*2,$out),$inout2);
	&movups	(&QWP(16*3,$out),$inout3);
	&lea	($out,&DWP(16*4,$out));

	&movdqa	($tweak,$inout4);		# last tweak
	&jmp	(&label("xts_enc_done"));

&set_label("xts_enc_done6x",16);		# $tweak is pre-calculated
	&mov	($len,&DWP(16*7+0,"esp"));	# restore original $len
	&and	($len,15);
	&jz	(&label("xts_enc_ret"));
	&movdqa	($inout3,$tweak);
	&mov	(&DWP(16*7+0,"esp"),$len);	# save $len%16
	&jmp	(&label("xts_enc_steal"));

&set_label("xts_enc_done",16);
	&mov	($len,&DWP(16*7+0,"esp"));	# restore original $len
	&pxor	($twtmp,$twtmp);
	&and	($len,15);
	&jz	(&label("xts_enc_ret"));

	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&mov	(&DWP(16*7+0,"esp"),$len);	# save $len%16
	&pshufd	($inout3,$twtmp,0x13);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($inout3,&QWP(16*6,"esp"));	# isolate carry and residue
	&pxor	($inout3,$tweak);

&set_label("xts_enc_steal");
	&movz	($rounds,&BP(0,$inp));
	&movz	($key,&BP(-16,$out));
	&lea	($inp,&DWP(1,$inp));
	&mov	(&BP(-16,$out),&LB($rounds));
	&mov	(&BP(0,$out),&LB($key));
	&lea	($out,&DWP(1,$out));
	&sub	($len,1);
	&jnz	(&label("xts_enc_steal"));

	&sub	($out,&DWP(16*7+0,"esp"));	# rewind $out
	&mov	($key,$key_);			# restore $key
	&mov	($rounds,$rounds_);		# restore $rounds

	&movups	($inout0,&QWP(-16,$out));	# load input
	&xorps	($inout0,$inout3);		# input^=tweak
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
	&xorps	($inout0,$inout3);		# output^=tweak
	&movups	(&QWP(-16,$out),$inout0);	# write output

&set_label("xts_enc_ret");
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	&pxor	("xmm0","xmm0");		# clear register bank
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&movdqa	(&QWP(16*0,"esp"),"xmm0");	# clear stack
	&pxor	("xmm3","xmm3");
	&movdqa	(&QWP(16*1,"esp"),"xmm0");
	&pxor	("xmm4","xmm4");
	&movdqa	(&QWP(16*2,"esp"),"xmm0");
	&pxor	("xmm5","xmm5");
	&movdqa	(&QWP(16*3,"esp"),"xmm0");
	&pxor	("xmm6","xmm6");
	&movdqa	(&QWP(16*4,"esp"),"xmm0");
	&pxor	("xmm7","xmm7");
	&movdqa	(&QWP(16*5,"esp"),"xmm0");
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
	&mov	("esp",&DWP(16*7+4,"esp"));	# restore %esp
&function_end("aesni_xts_encrypt");

&function_begin("aesni_xts_decrypt");
	&mov	($key,&wparam(4));		# key2
	&mov	($inp,&wparam(5));		# clear-text tweak

	&mov	($rounds,&DWP(240,$key));	# key2->rounds
	&movups	($inout0,&QWP(0,$inp));
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}

	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));		# key1

	&mov	($key_,"esp");
	&sub	("esp",16*7+8);
	&and	("esp",-16);			# align stack

	&xor	($rounds_,$rounds_);		# if(len%16) len-=16;
	&test	($len,15);
	&setnz	(&LB($rounds_));
	&shl	($rounds_,4);
	&sub	($len,$rounds_);

	&mov	(&DWP(16*6+0,"esp"),0x87);	# compose the magic constant
	&mov	(&DWP(16*6+4,"esp"),0);
	&mov	(&DWP(16*6+8,"esp"),1);
	&mov	(&DWP(16*6+12,"esp"),0);
	&mov	(&DWP(16*7+0,"esp"),$len);	# save original $len
	&mov	(&DWP(16*7+4,"esp"),$key_);	# save original %esp

	&mov	($rounds,&DWP(240,$key));	# key1->rounds
	&mov	($key_,$key);			# backup $key
	&mov	($rounds_,$rounds);		# backup $rounds

	&movdqa	($tweak,$inout0);
	&pxor	($twtmp,$twtmp);
	&movdqa	($twmask,&QWP(6*16,"esp"));	# 0x0...010...87
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits

	&and	($len,-16);
	&sub	($len,16*6);
	&jc	(&label("xts_dec_short"));

1521 1522 1523 1524
	&shl	($rounds,4);
	&mov	($rounds_,16);
	&sub	($rounds_,$rounds);
	&lea	($key,&DWP(32,$key,$rounds));
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	&jmp	(&label("xts_dec_loop6"));

&set_label("xts_dec_loop6",16);
	for ($i=0;$i<4;$i++) {
	    &pshufd	($twres,$twtmp,0x13);
	    &pxor	($twtmp,$twtmp);
	    &movdqa	(&QWP(16*$i,"esp"),$tweak);
	    &paddq	($tweak,$tweak);	# &psllq($tweak,1);
	    &pand	($twres,$twmask);	# isolate carry and residue
	    &pcmpgtd	($twtmp,$tweak);	# broadcast upper bits
	    &pxor	($tweak,$twres);
	}
	&pshufd	($inout5,$twtmp,0x13);
	&movdqa	(&QWP(16*$i++,"esp"),$tweak);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	 &$movekey	($rndkey0,&QWP(0,$key_));
	&pand	($inout5,$twmask);		# isolate carry and residue
	 &movups	($inout0,&QWP(0,$inp));	# load input
	&pxor	($inout5,$tweak);

	# inline _aesni_encrypt6 prologue and flip xor with tweak and key[0]
1546
	&mov	($rounds,$rounds_);
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
	&movdqu	($inout1,&QWP(16*1,$inp));
	 &xorps		($inout0,$rndkey0);	# input^=rndkey[0]
	&movdqu	($inout2,&QWP(16*2,$inp));
	 &pxor		($inout1,$rndkey0);
	&movdqu	($inout3,&QWP(16*3,$inp));
	 &pxor		($inout2,$rndkey0);
	&movdqu	($inout4,&QWP(16*4,$inp));
	 &pxor		($inout3,$rndkey0);
	&movdqu	($rndkey1,&QWP(16*5,$inp));
	 &pxor		($inout4,$rndkey0);
	&lea	($inp,&DWP(16*6,$inp));
	&pxor	($inout0,&QWP(16*0,"esp"));	# input^=tweak
	&movdqa	(&QWP(16*$i,"esp"),$inout5);	# save last tweak
	&pxor	($inout5,$rndkey1);

	 &$movekey	($rndkey1,&QWP(16,$key_));
	&pxor	($inout1,&QWP(16*1,"esp"));
	&pxor	($inout2,&QWP(16*2,"esp"));
1565
	 &aesdec	($inout0,$rndkey1);
1566 1567
	&pxor	($inout3,&QWP(16*3,"esp"));
	&pxor	($inout4,&QWP(16*4,"esp"));
1568
	 &aesdec	($inout1,$rndkey1);
1569
	&pxor		($inout5,$rndkey0);
1570 1571 1572
	 &$movekey	($rndkey0,&QWP(32,$key_));
	 &aesdec	($inout2,$rndkey1);
	 &aesdec	($inout3,$rndkey1);
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
	 &aesdec	($inout4,$rndkey1);
	 &aesdec	($inout5,$rndkey1);
	&call		(&label("_aesni_decrypt6_enter"));

	&movdqa	($tweak,&QWP(16*5,"esp"));	# last tweak
       &pxor	($twtmp,$twtmp);
	&xorps	($inout0,&QWP(16*0,"esp"));	# output^=tweak
       &pcmpgtd	($twtmp,$tweak);		# broadcast upper bits
	&xorps	($inout1,&QWP(16*1,"esp"));
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&xorps	($inout2,&QWP(16*2,"esp"));
	&movups	(&QWP(16*1,$out),$inout1);
	&xorps	($inout3,&QWP(16*3,"esp"));
	&movups	(&QWP(16*2,$out),$inout2);
	&xorps	($inout4,&QWP(16*4,"esp"));
	&movups	(&QWP(16*3,$out),$inout3);
	&xorps	($inout5,$tweak);
	&movups	(&QWP(16*4,$out),$inout4);
       &pshufd	($twres,$twtmp,0x13);
	&movups	(&QWP(16*5,$out),$inout5);
	&lea	($out,&DWP(16*6,$out));
       &movdqa	($twmask,&QWP(16*6,"esp"));	# 0x0...010...87

	&pxor	($twtmp,$twtmp);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);

	&sub	($len,16*6);
	&jnc	(&label("xts_dec_loop6"));

1605
	&mov	($rounds,&DWP(240,$key_));	# restore $rounds
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
	&mov	($key,$key_);			# restore $key
	&mov	($rounds_,$rounds);

&set_label("xts_dec_short");
	&add	($len,16*6);
	&jz	(&label("xts_dec_done6x"));

	&movdqa	($inout3,$tweak);		# put aside previous tweak
	&cmp	($len,0x20);
	&jb	(&label("xts_dec_one"));

	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);
	&je	(&label("xts_dec_two"));

	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&movdqa	($inout4,$tweak);		# put aside previous tweak
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);
	&cmp	($len,0x40);
	&jb	(&label("xts_dec_three"));

	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&movdqa	($inout5,$tweak);		# put aside previous tweak
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);
	&movdqa	(&QWP(16*0,"esp"),$inout3);
	&movdqa	(&QWP(16*1,"esp"),$inout4);
	&je	(&label("xts_dec_four"));

	&movdqa	(&QWP(16*2,"esp"),$inout5);
	&pshufd	($inout5,$twtmp,0x13);
	&movdqa	(&QWP(16*3,"esp"),$tweak);
	&paddq	($tweak,$tweak);		# &psllq($inout0,1);
	&pand	($inout5,$twmask);		# isolate carry and residue
	&pxor	($inout5,$tweak);

	&movdqu	($inout0,&QWP(16*0,$inp));	# load input
	&movdqu	($inout1,&QWP(16*1,$inp));
	&movdqu	($inout2,&QWP(16*2,$inp));
	&pxor	($inout0,&QWP(16*0,"esp"));	# input^=tweak
	&movdqu	($inout3,&QWP(16*3,$inp));
	&pxor	($inout1,&QWP(16*1,"esp"));
	&movdqu	($inout4,&QWP(16*4,$inp));
	&pxor	($inout2,&QWP(16*2,"esp"));
	&lea	($inp,&DWP(16*5,$inp));
	&pxor	($inout3,&QWP(16*3,"esp"));
	&movdqa	(&QWP(16*4,"esp"),$inout5);	# save last tweak
	&pxor	($inout4,$inout5);

	&call	("_aesni_decrypt6");

	&movaps	($tweak,&QWP(16*4,"esp"));	# last tweak
	&xorps	($inout0,&QWP(16*0,"esp"));	# output^=tweak
	&xorps	($inout1,&QWP(16*1,"esp"));
	&xorps	($inout2,&QWP(16*2,"esp"));
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&xorps	($inout3,&QWP(16*3,"esp"));
	&movups	(&QWP(16*1,$out),$inout1);
	&xorps	($inout4,$tweak);
	&movups	(&QWP(16*2,$out),$inout2);
	&movups	(&QWP(16*3,$out),$inout3);
	&movups	(&QWP(16*4,$out),$inout4);
	&lea	($out,&DWP(16*5,$out));
	&jmp	(&label("xts_dec_done"));

&set_label("xts_dec_one",16);
	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&lea	($inp,&DWP(16*1,$inp));
	&xorps	($inout0,$inout3);		# input^=tweak
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
	&xorps	($inout0,$inout3);		# output^=tweak
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&lea	($out,&DWP(16*1,$out));

	&movdqa	($tweak,$inout3);		# last tweak
	&jmp	(&label("xts_dec_done"));

&set_label("xts_dec_two",16);
	&movaps	($inout4,$tweak);		# put aside last tweak

	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&movups	($inout1,&QWP(16*1,$inp));
	&lea	($inp,&DWP(16*2,$inp));
	&xorps	($inout0,$inout3);		# input^=tweak
	&xorps	($inout1,$inout4);

1706
	&call	("_aesni_decrypt2");
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

	&xorps	($inout0,$inout3);		# output^=tweak
	&xorps	($inout1,$inout4);
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&movups	(&QWP(16*1,$out),$inout1);
	&lea	($out,&DWP(16*2,$out));

	&movdqa	($tweak,$inout4);		# last tweak
	&jmp	(&label("xts_dec_done"));

&set_label("xts_dec_three",16);
	&movaps	($inout5,$tweak);		# put aside last tweak
	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&movups	($inout1,&QWP(16*1,$inp));
	&movups	($inout2,&QWP(16*2,$inp));
	&lea	($inp,&DWP(16*3,$inp));
	&xorps	($inout0,$inout3);		# input^=tweak
	&xorps	($inout1,$inout4);
	&xorps	($inout2,$inout5);

	&call	("_aesni_decrypt3");

	&xorps	($inout0,$inout3);		# output^=tweak
	&xorps	($inout1,$inout4);
	&xorps	($inout2,$inout5);
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&movups	(&QWP(16*1,$out),$inout1);
	&movups	(&QWP(16*2,$out),$inout2);
	&lea	($out,&DWP(16*3,$out));

	&movdqa	($tweak,$inout5);		# last tweak
	&jmp	(&label("xts_dec_done"));

&set_label("xts_dec_four",16);
	&movaps	($inout4,$tweak);		# put aside last tweak

	&movups	($inout0,&QWP(16*0,$inp));	# load input
	&movups	($inout1,&QWP(16*1,$inp));
	&movups	($inout2,&QWP(16*2,$inp));
	&xorps	($inout0,&QWP(16*0,"esp"));	# input^=tweak
	&movups	($inout3,&QWP(16*3,$inp));
	&lea	($inp,&DWP(16*4,$inp));
	&xorps	($inout1,&QWP(16*1,"esp"));
	&xorps	($inout2,$inout5);
	&xorps	($inout3,$inout4);

	&call	("_aesni_decrypt4");

	&xorps	($inout0,&QWP(16*0,"esp"));	# output^=tweak
	&xorps	($inout1,&QWP(16*1,"esp"));
	&xorps	($inout2,$inout5);
	&movups	(&QWP(16*0,$out),$inout0);	# write output
	&xorps	($inout3,$inout4);
	&movups	(&QWP(16*1,$out),$inout1);
	&movups	(&QWP(16*2,$out),$inout2);
	&movups	(&QWP(16*3,$out),$inout3);
	&lea	($out,&DWP(16*4,$out));

	&movdqa	($tweak,$inout4);		# last tweak
	&jmp	(&label("xts_dec_done"));

&set_label("xts_dec_done6x",16);		# $tweak is pre-calculated
	&mov	($len,&DWP(16*7+0,"esp"));	# restore original $len
	&and	($len,15);
	&jz	(&label("xts_dec_ret"));
	&mov	(&DWP(16*7+0,"esp"),$len);	# save $len%16
	&jmp	(&label("xts_dec_only_one_more"));

&set_label("xts_dec_done",16);
	&mov	($len,&DWP(16*7+0,"esp"));	# restore original $len
	&pxor	($twtmp,$twtmp);
	&and	($len,15);
	&jz	(&label("xts_dec_ret"));

	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&mov	(&DWP(16*7+0,"esp"),$len);	# save $len%16
	&pshufd	($twres,$twtmp,0x13);
	&pxor	($twtmp,$twtmp);
	&movdqa	($twmask,&QWP(16*6,"esp"));
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($twres,$twmask);		# isolate carry and residue
	&pcmpgtd($twtmp,$tweak);		# broadcast upper bits
	&pxor	($tweak,$twres);

&set_label("xts_dec_only_one_more");
	&pshufd	($inout3,$twtmp,0x13);
	&movdqa	($inout4,$tweak);		# put aside previous tweak
	&paddq	($tweak,$tweak);		# &psllq($tweak,1);
	&pand	($inout3,$twmask);		# isolate carry and residue
	&pxor	($inout3,$tweak);

	&mov	($key,$key_);			# restore $key
	&mov	($rounds,$rounds_);		# restore $rounds

	&movups	($inout0,&QWP(0,$inp));		# load input
	&xorps	($inout0,$inout3);		# input^=tweak
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
	&xorps	($inout0,$inout3);		# output^=tweak
	&movups	(&QWP(0,$out),$inout0);		# write output

&set_label("xts_dec_steal");
	&movz	($rounds,&BP(16,$inp));
	&movz	($key,&BP(0,$out));
	&lea	($inp,&DWP(1,$inp));
	&mov	(&BP(0,$out),&LB($rounds));
	&mov	(&BP(16,$out),&LB($key));
	&lea	($out,&DWP(1,$out));
	&sub	($len,1);
	&jnz	(&label("xts_dec_steal"));

	&sub	($out,&DWP(16*7+0,"esp"));	# rewind $out
	&mov	($key,$key_);			# restore $key
	&mov	($rounds,$rounds_);		# restore $rounds

	&movups	($inout0,&QWP(0,$out));		# load input
	&xorps	($inout0,$inout4);		# input^=tweak
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
	&xorps	($inout0,$inout4);		# output^=tweak
	&movups	(&QWP(0,$out),$inout0);		# write output

&set_label("xts_dec_ret");
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
	&pxor	("xmm0","xmm0");		# clear register bank
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&movdqa	(&QWP(16*0,"esp"),"xmm0");	# clear stack
	&pxor	("xmm3","xmm3");
	&movdqa	(&QWP(16*1,"esp"),"xmm0");
	&pxor	("xmm4","xmm4");
	&movdqa	(&QWP(16*2,"esp"),"xmm0");
	&pxor	("xmm5","xmm5");
	&movdqa	(&QWP(16*3,"esp"),"xmm0");
	&pxor	("xmm6","xmm6");
	&movdqa	(&QWP(16*4,"esp"),"xmm0");
	&pxor	("xmm7","xmm7");
	&movdqa	(&QWP(16*5,"esp"),"xmm0");
1848 1849 1850
	&mov	("esp",&DWP(16*7+4,"esp"));	# restore %esp
&function_end("aesni_xts_decrypt");
}
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029

######################################################################
# void aesni_ocb_[en|de]crypt(const char *inp, char *out, size_t blocks,
#	const AES_KEY *key, unsigned int start_block_num,
#	unsigned char offset_i[16], const unsigned char L_[][16],
#	unsigned char checksum[16]);
#
{
# offsets within stack frame
my $checksum = 16*6;
my ($key_off,$rounds_off,$out_off,$end_off,$esp_off)=map(16*7+4*$_,(0..4));

# reassigned registers
my ($l_,$block,$i1,$i3,$i5) = ($rounds_,$key_,$rounds,$len,$out);
# $l_, $blocks, $inp, $key are permanently allocated in registers;
# remaining non-volatile ones are offloaded to stack, which even
# stay invariant after written to stack.

&function_begin("aesni_ocb_encrypt");
	&mov	($rounds,&wparam(5));		# &offset_i
	&mov	($rounds_,&wparam(7));		# &checksum

	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
	&movdqu	($rndkey0,&QWP(0,$rounds));	# load offset_i
	&mov	($block,&wparam(4));		# start_block_num
	&movdqu	($rndkey1,&QWP(0,$rounds_));	# load checksum
	&mov	($l_,&wparam(6));		# L_

	&mov	($rounds,"esp");
	&sub	("esp",$esp_off+4);		# alloca
	&and	("esp",-16);			# align stack

	&sub	($out,$inp);
	&shl	($len,4);
	&lea	($len,&DWP(-16*6,$inp,$len));	# end of input - 16*6
	&mov	(&DWP($out_off,"esp"),$out);
	&mov	(&DWP($end_off,"esp"),$len);
	&mov	(&DWP($esp_off,"esp"),$rounds);

	&mov	($rounds,&DWP(240,$key));

	&test	($block,1);
	&jnz	(&label("odd"));

	&bsf		($i3,$block);
	&add		($block,1);
	&shl		($i3,4);
	&movdqu		($inout5,&QWP(0,$l_,$i3));
	&mov		($i3,$key);			# put aside key

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&lea		($inp,&DWP(16,$inp));

	&pxor		($inout5,$rndkey0);		# ^ last offset_i
	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,$inout5);		# ^ offset_i

	&movdqa		($inout4,$rndkey1);
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}

	&xorps		($inout0,$inout5);		# ^ offset_i
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movdqa		($rndkey1,$inout4);		# pass the checksum

	&movups		(&QWP(-16,$out,$inp),$inout0);	# store output

	&mov		($rounds,&DWP(240,$i3));
	&mov		($key,$i3);			# restore key
	&mov		($len,&DWP($end_off,"esp"));

&set_label("odd");
	&shl		($rounds,4);
	&mov		($out,16);
	&sub		($out,$rounds);			# twisted rounds
	&mov		(&DWP($key_off,"esp"),$key);
	&lea		($key,&DWP(32,$key,$rounds));	# end of key schedule
	&mov		(&DWP($rounds_off,"esp"),$out);

	&cmp		($inp,$len);
	&ja		(&label("short"));
	&jmp		(&label("grandloop"));

&set_label("grandloop",32);
	&lea		($i1,&DWP(1,$block));
	&lea		($i3,&DWP(3,$block));
	&lea		($i5,&DWP(5,$block));
	&add		($block,6);
	&bsf		($i1,$i1);
	&bsf		($i3,$i3);
	&bsf		($i5,$i5);
	&shl		($i1,4);
	&shl		($i3,4);
	&shl		($i5,4);
	&movdqu		($inout0,&QWP(0,$l_));
	&movdqu		($inout1,&QWP(0,$l_,$i1));
	&mov		($rounds,&DWP($rounds_off,"esp"));
	&movdqa		($inout2,$inout0);
	&movdqu		($inout3,&QWP(0,$l_,$i3));
	&movdqa		($inout4,$inout0);
	&movdqu		($inout5,&QWP(0,$l_,$i5));

	&pxor		($inout0,$rndkey0);		# ^ last offset_i
	&pxor		($inout1,$inout0);
	&movdqa		(&QWP(16*0,"esp"),$inout0);
	&pxor		($inout2,$inout1);
	&movdqa		(&QWP(16*1,"esp"),$inout1);
	&pxor		($inout3,$inout2);
	&movdqa		(&QWP(16*2,"esp"),$inout2);
	&pxor		($inout4,$inout3);
	&movdqa		(&QWP(16*3,"esp"),$inout3);
	&pxor		($inout5,$inout4);
	&movdqa		(&QWP(16*4,"esp"),$inout4);
	&movdqa		(&QWP(16*5,"esp"),$inout5);

	&$movekey	($rndkey0,&QWP(-48,$key,$rounds));
	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&movdqu		($inout2,&QWP(16*2,$inp));
	&movdqu		($inout3,&QWP(16*3,$inp));
	&movdqu		($inout4,&QWP(16*4,$inp));
	&movdqu		($inout5,&QWP(16*5,$inp));
	&lea		($inp,&DWP(16*6,$inp));

	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,$rndkey0);		# ^ roundkey[0]
	&pxor		($rndkey1,$inout1);
	&pxor		($inout1,$rndkey0);
	&pxor		($rndkey1,$inout2);
	&pxor		($inout2,$rndkey0);
	&pxor		($rndkey1,$inout3);
	&pxor		($inout3,$rndkey0);
	&pxor		($rndkey1,$inout4);
	&pxor		($inout4,$rndkey0);
	&pxor		($rndkey1,$inout5);
	&pxor		($inout5,$rndkey0);
	&movdqa		(&QWP($checksum,"esp"),$rndkey1);

	&$movekey	($rndkey1,&QWP(-32,$key,$rounds));
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,&QWP(16*4,"esp"));
	&pxor		($inout5,&QWP(16*5,"esp"));

	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
	&aesenc		($inout0,$rndkey1);
	&aesenc		($inout1,$rndkey1);
	&aesenc		($inout2,$rndkey1);
	&aesenc		($inout3,$rndkey1);
	&aesenc		($inout4,$rndkey1);
	&aesenc		($inout5,$rndkey1);

	&mov		($out,&DWP($out_off,"esp"));
	&mov		($len,&DWP($end_off,"esp"));
	&call		("_aesni_encrypt6_enter");

	&movdqa		($rndkey0,&QWP(16*5,"esp"));	# pass last offset_i
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,&QWP(16*4,"esp"));
	&pxor		($inout5,$rndkey0);
	&movdqa		($rndkey1,&QWP($checksum,"esp"));# pass the checksum

	&movdqu		(&QWP(-16*6,$out,$inp),$inout0);# store output
	&movdqu		(&QWP(-16*5,$out,$inp),$inout1);
	&movdqu		(&QWP(-16*4,$out,$inp),$inout2);
	&movdqu		(&QWP(-16*3,$out,$inp),$inout3);
	&movdqu		(&QWP(-16*2,$out,$inp),$inout4);
	&movdqu		(&QWP(-16*1,$out,$inp),$inout5);
	&cmp		($inp,$len);			# done yet?
2030
	&jbe		(&label("grandloop"));
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455

&set_label("short");
	&add		($len,16*6);
	&sub		($len,$inp);
	&jz		(&label("done"));

	&cmp		($len,16*2);
	&jb		(&label("one"));
	&je		(&label("two"));

	&cmp		($len,16*4);
	&jb		(&label("three"));
	&je		(&label("four"));

	&lea		($i1,&DWP(1,$block));
	&lea		($i3,&DWP(3,$block));
	&bsf		($i1,$i1);
	&bsf		($i3,$i3);
	&shl		($i1,4);
	&shl		($i3,4);
	&movdqu		($inout0,&QWP(0,$l_));
	&movdqu		($inout1,&QWP(0,$l_,$i1));
	&mov		($rounds,&DWP($rounds_off,"esp"));
	&movdqa		($inout2,$inout0);
	&movdqu		($inout3,&QWP(0,$l_,$i3));
	&movdqa		($inout4,$inout0);

	&pxor		($inout0,$rndkey0);		# ^ last offset_i
	&pxor		($inout1,$inout0);
	&movdqa		(&QWP(16*0,"esp"),$inout0);
	&pxor		($inout2,$inout1);
	&movdqa		(&QWP(16*1,"esp"),$inout1);
	&pxor		($inout3,$inout2);
	&movdqa		(&QWP(16*2,"esp"),$inout2);
	&pxor		($inout4,$inout3);
	&movdqa		(&QWP(16*3,"esp"),$inout3);
	&pxor		($inout5,$inout4);
	&movdqa		(&QWP(16*4,"esp"),$inout4);

	&$movekey	($rndkey0,&QWP(-48,$key,$rounds));
	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&movdqu		($inout2,&QWP(16*2,$inp));
	&movdqu		($inout3,&QWP(16*3,$inp));
	&movdqu		($inout4,&QWP(16*4,$inp));
	&pxor		($inout5,$inout5);

	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,$rndkey0);		# ^ roundkey[0]
	&pxor		($rndkey1,$inout1);
	&pxor		($inout1,$rndkey0);
	&pxor		($rndkey1,$inout2);
	&pxor		($inout2,$rndkey0);
	&pxor		($rndkey1,$inout3);
	&pxor		($inout3,$rndkey0);
	&pxor		($rndkey1,$inout4);
	&pxor		($inout4,$rndkey0);
	&movdqa		(&QWP($checksum,"esp"),$rndkey1);

	&$movekey	($rndkey1,&QWP(-32,$key,$rounds));
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,&QWP(16*4,"esp"));

	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
	&aesenc		($inout0,$rndkey1);
	&aesenc		($inout1,$rndkey1);
	&aesenc		($inout2,$rndkey1);
	&aesenc		($inout3,$rndkey1);
	&aesenc		($inout4,$rndkey1);
	&aesenc		($inout5,$rndkey1);

	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_encrypt6_enter");

	&movdqa		($rndkey0,&QWP(16*4,"esp"));	# pass last offset_i
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,$rndkey0);
	&movdqa		($rndkey1,&QWP($checksum,"esp"));# pass the checksum

	&movdqu		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&movdqu		(&QWP(16*1,$out,$inp),$inout1);
	&movdqu		(&QWP(16*2,$out,$inp),$inout2);
	&movdqu		(&QWP(16*3,$out,$inp),$inout3);
	&movdqu		(&QWP(16*4,$out,$inp),$inout4);

	&jmp		(&label("done"));

&set_label("one",16);
	&movdqu		($inout5,&QWP(0,$l_));
	&mov		($key,&DWP($key_off,"esp"));	# restore key

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&mov		($rounds,&DWP(240,$key));

	&pxor		($inout5,$rndkey0);		# ^ last offset_i
	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,$inout5);		# ^ offset_i

	&movdqa		($inout4,$rndkey1);
	&mov		($out,&DWP($out_off,"esp"));
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}

	&xorps		($inout0,$inout5);		# ^ offset_i
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movdqa		($rndkey1,$inout4);		# pass the checksum
	&movups		(&QWP(0,$out,$inp),$inout0);

	&jmp		(&label("done"));

&set_label("two",16);
	&lea		($i1,&DWP(1,$block));
	&mov		($key,&DWP($key_off,"esp"));	# restore key
	&bsf		($i1,$i1);
	&shl		($i1,4);
	&movdqu		($inout4,&QWP(0,$l_));
	&movdqu		($inout5,&QWP(0,$l_,$i1));

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&mov		($rounds,&DWP(240,$key));

	&pxor		($inout4,$rndkey0);		# ^ last offset_i
	&pxor		($inout5,$inout4);

	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,$inout4);		# ^ offset_i
	&pxor		($rndkey1,$inout1);
	&pxor		($inout1,$inout5);

	&movdqa		($inout3,$rndkey1)
	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_encrypt2");

	&xorps		($inout0,$inout4);		# ^ offset_i
	&xorps		($inout1,$inout5);
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movdqa		($rndkey1,$inout3);		# pass the checksum
	&movups		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&movups		(&QWP(16*1,$out,$inp),$inout1);

	&jmp		(&label("done"));

&set_label("three",16);
	&lea		($i1,&DWP(1,$block));
	&mov		($key,&DWP($key_off,"esp"));	# restore key
	&bsf		($i1,$i1);
	&shl		($i1,4);
	&movdqu		($inout3,&QWP(0,$l_));
	&movdqu		($inout4,&QWP(0,$l_,$i1));
	&movdqa		($inout5,$inout3);

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&movdqu		($inout2,&QWP(16*2,$inp));
	&mov		($rounds,&DWP(240,$key));

	&pxor		($inout3,$rndkey0);		# ^ last offset_i
	&pxor		($inout4,$inout3);
	&pxor		($inout5,$inout4);

	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,$inout3);		# ^ offset_i
	&pxor		($rndkey1,$inout1);
	&pxor		($inout1,$inout4);
	&pxor		($rndkey1,$inout2);
	&pxor		($inout2,$inout5);

	&movdqa		(&QWP($checksum,"esp"),$rndkey1);
	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_encrypt3");

	&xorps		($inout0,$inout3);		# ^ offset_i
	&xorps		($inout1,$inout4);
	&xorps		($inout2,$inout5);
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movdqa		($rndkey1,&QWP($checksum,"esp"));# pass the checksum
	&movups		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&movups		(&QWP(16*1,$out,$inp),$inout1);
	&movups		(&QWP(16*2,$out,$inp),$inout2);

	&jmp		(&label("done"));

&set_label("four",16);
	&lea		($i1,&DWP(1,$block));
	&lea		($i3,&DWP(3,$block));
	&bsf		($i1,$i1);
	&bsf		($i3,$i3);
	&mov		($key,&DWP($key_off,"esp"));	# restore key
	&shl		($i1,4);
	&shl		($i3,4);
	&movdqu		($inout2,&QWP(0,$l_));
	&movdqu		($inout3,&QWP(0,$l_,$i1));
	&movdqa		($inout4,$inout2);
	&movdqu		($inout5,&QWP(0,$l_,$i3));

	&pxor		($inout2,$rndkey0);		# ^ last offset_i
	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&pxor		($inout3,$inout2);
	&movdqu		($inout1,&QWP(16*1,$inp));
	&pxor		($inout4,$inout3);
	&movdqa		(&QWP(16*0,"esp"),$inout2);
	&pxor		($inout5,$inout4);
	&movdqa		(&QWP(16*1,"esp"),$inout3);
	&movdqu		($inout2,&QWP(16*2,$inp));
	&movdqu		($inout3,&QWP(16*3,$inp));
	&mov		($rounds,&DWP(240,$key));

	&pxor		($rndkey1,$inout0);		# checksum
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($rndkey1,$inout1);
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($rndkey1,$inout2);
	&pxor		($inout2,$inout4);
	&pxor		($rndkey1,$inout3);
	&pxor		($inout3,$inout5);

	&movdqa		(&QWP($checksum,"esp"),$rndkey1)
	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_encrypt4");

	&xorps		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&xorps		($inout1,&QWP(16*1,"esp"));
	&xorps		($inout2,$inout4);
	&movups		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&xorps		($inout3,$inout5);
	&movups		(&QWP(16*1,$out,$inp),$inout1);
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movups		(&QWP(16*2,$out,$inp),$inout2);
	&movdqa		($rndkey1,&QWP($checksum,"esp"));# pass the checksum
	&movups		(&QWP(16*3,$out,$inp),$inout3);

&set_label("done");
	&mov	($key,&DWP($esp_off,"esp"));
	&pxor	($inout0,$inout0);		# clear register bank
	&pxor	($inout1,$inout1);
	&movdqa	(&QWP(16*0,"esp"),$inout0);	# clear stack
	&pxor	($inout2,$inout2);
	&movdqa	(&QWP(16*1,"esp"),$inout0);
	&pxor	($inout3,$inout3);
	&movdqa	(&QWP(16*2,"esp"),$inout0);
	&pxor	($inout4,$inout4);
	&movdqa	(&QWP(16*3,"esp"),$inout0);
	&pxor	($inout5,$inout5);
	&movdqa	(&QWP(16*4,"esp"),$inout0);
	&movdqa	(&QWP(16*5,"esp"),$inout0);
	&movdqa	(&QWP(16*6,"esp"),$inout0);

	&lea	("esp",&DWP(0,$key));
	&mov	($rounds,&wparam(5));		# &offset_i
	&mov	($rounds_,&wparam(7));		# &checksum
	&movdqu	(&QWP(0,$rounds),$rndkey0);
	&pxor	($rndkey0,$rndkey0);
	&movdqu	(&QWP(0,$rounds_),$rndkey1);
	&pxor	($rndkey1,$rndkey1);
&function_end("aesni_ocb_encrypt");

&function_begin("aesni_ocb_decrypt");
	&mov	($rounds,&wparam(5));		# &offset_i
	&mov	($rounds_,&wparam(7));		# &checksum

	&mov	($inp,&wparam(0));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
	&movdqu	($rndkey0,&QWP(0,$rounds));	# load offset_i
	&mov	($block,&wparam(4));		# start_block_num
	&movdqu	($rndkey1,&QWP(0,$rounds_));	# load checksum
	&mov	($l_,&wparam(6));		# L_

	&mov	($rounds,"esp");
	&sub	("esp",$esp_off+4);		# alloca
	&and	("esp",-16);			# align stack

	&sub	($out,$inp);
	&shl	($len,4);
	&lea	($len,&DWP(-16*6,$inp,$len));	# end of input - 16*6
	&mov	(&DWP($out_off,"esp"),$out);
	&mov	(&DWP($end_off,"esp"),$len);
	&mov	(&DWP($esp_off,"esp"),$rounds);

	&mov	($rounds,&DWP(240,$key));

	&test	($block,1);
	&jnz	(&label("odd"));

	&bsf		($i3,$block);
	&add		($block,1);
	&shl		($i3,4);
	&movdqu		($inout5,&QWP(0,$l_,$i3));
	&mov		($i3,$key);			# put aside key

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&lea		($inp,&DWP(16,$inp));

	&pxor		($inout5,$rndkey0);		# ^ last offset_i
	&pxor		($inout0,$inout5);		# ^ offset_i

	&movdqa		($inout4,$rndkey1);
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}

	&xorps		($inout0,$inout5);		# ^ offset_i
	&movaps		($rndkey1,$inout4);		# pass the checksum
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&xorps		($rndkey1,$inout0);		# checksum
	&movups		(&QWP(-16,$out,$inp),$inout0);	# store output

	&mov		($rounds,&DWP(240,$i3));
	&mov		($key,$i3);			# restore key
	&mov		($len,&DWP($end_off,"esp"));

&set_label("odd");
	&shl		($rounds,4);
	&mov		($out,16);
	&sub		($out,$rounds);			# twisted rounds
	&mov		(&DWP($key_off,"esp"),$key);
	&lea		($key,&DWP(32,$key,$rounds));	# end of key schedule
	&mov		(&DWP($rounds_off,"esp"),$out);

	&cmp		($inp,$len);
	&ja		(&label("short"));
	&jmp		(&label("grandloop"));

&set_label("grandloop",32);
	&lea		($i1,&DWP(1,$block));
	&lea		($i3,&DWP(3,$block));
	&lea		($i5,&DWP(5,$block));
	&add		($block,6);
	&bsf		($i1,$i1);
	&bsf		($i3,$i3);
	&bsf		($i5,$i5);
	&shl		($i1,4);
	&shl		($i3,4);
	&shl		($i5,4);
	&movdqu		($inout0,&QWP(0,$l_));
	&movdqu		($inout1,&QWP(0,$l_,$i1));
	&mov		($rounds,&DWP($rounds_off,"esp"));
	&movdqa		($inout2,$inout0);
	&movdqu		($inout3,&QWP(0,$l_,$i3));
	&movdqa		($inout4,$inout0);
	&movdqu		($inout5,&QWP(0,$l_,$i5));

	&pxor		($inout0,$rndkey0);		# ^ last offset_i
	&pxor		($inout1,$inout0);
	&movdqa		(&QWP(16*0,"esp"),$inout0);
	&pxor		($inout2,$inout1);
	&movdqa		(&QWP(16*1,"esp"),$inout1);
	&pxor		($inout3,$inout2);
	&movdqa		(&QWP(16*2,"esp"),$inout2);
	&pxor		($inout4,$inout3);
	&movdqa		(&QWP(16*3,"esp"),$inout3);
	&pxor		($inout5,$inout4);
	&movdqa		(&QWP(16*4,"esp"),$inout4);
	&movdqa		(&QWP(16*5,"esp"),$inout5);

	&$movekey	($rndkey0,&QWP(-48,$key,$rounds));
	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&movdqu		($inout2,&QWP(16*2,$inp));
	&movdqu		($inout3,&QWP(16*3,$inp));
	&movdqu		($inout4,&QWP(16*4,$inp));
	&movdqu		($inout5,&QWP(16*5,$inp));
	&lea		($inp,&DWP(16*6,$inp));

	&movdqa		(&QWP($checksum,"esp"),$rndkey1);
	&pxor		($inout0,$rndkey0);		# ^ roundkey[0]
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
	&pxor		($inout3,$rndkey0);
	&pxor		($inout4,$rndkey0);
	&pxor		($inout5,$rndkey0);

	&$movekey	($rndkey1,&QWP(-32,$key,$rounds));
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,&QWP(16*4,"esp"));
	&pxor		($inout5,&QWP(16*5,"esp"));

	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
	&aesdec		($inout0,$rndkey1);
	&aesdec		($inout1,$rndkey1);
	&aesdec		($inout2,$rndkey1);
	&aesdec		($inout3,$rndkey1);
	&aesdec		($inout4,$rndkey1);
	&aesdec		($inout5,$rndkey1);

	&mov		($out,&DWP($out_off,"esp"));
	&mov		($len,&DWP($end_off,"esp"));
	&call		("_aesni_decrypt6_enter");

	&movdqa		($rndkey0,&QWP(16*5,"esp"));	# pass last offset_i
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&movdqa		($rndkey1,&QWP($checksum,"esp"));
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,&QWP(16*4,"esp"));
	&pxor		($inout5,$rndkey0);

	&pxor		($rndkey1,$inout0);		# checksum
	&movdqu		(&QWP(-16*6,$out,$inp),$inout0);# store output
	&pxor		($rndkey1,$inout1);
	&movdqu		(&QWP(-16*5,$out,$inp),$inout1);
	&pxor		($rndkey1,$inout2);
	&movdqu		(&QWP(-16*4,$out,$inp),$inout2);
	&pxor		($rndkey1,$inout3);
	&movdqu		(&QWP(-16*3,$out,$inp),$inout3);
	&pxor		($rndkey1,$inout4);
	&movdqu		(&QWP(-16*2,$out,$inp),$inout4);
	&pxor		($rndkey1,$inout5);
	&movdqu		(&QWP(-16*1,$out,$inp),$inout5);
	&cmp		($inp,$len);			# done yet?
2456
	&jbe		(&label("grandloop"));
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721

&set_label("short");
	&add		($len,16*6);
	&sub		($len,$inp);
	&jz		(&label("done"));

	&cmp		($len,16*2);
	&jb		(&label("one"));
	&je		(&label("two"));

	&cmp		($len,16*4);
	&jb		(&label("three"));
	&je		(&label("four"));

	&lea		($i1,&DWP(1,$block));
	&lea		($i3,&DWP(3,$block));
	&bsf		($i1,$i1);
	&bsf		($i3,$i3);
	&shl		($i1,4);
	&shl		($i3,4);
	&movdqu		($inout0,&QWP(0,$l_));
	&movdqu		($inout1,&QWP(0,$l_,$i1));
	&mov		($rounds,&DWP($rounds_off,"esp"));
	&movdqa		($inout2,$inout0);
	&movdqu		($inout3,&QWP(0,$l_,$i3));
	&movdqa		($inout4,$inout0);

	&pxor		($inout0,$rndkey0);		# ^ last offset_i
	&pxor		($inout1,$inout0);
	&movdqa		(&QWP(16*0,"esp"),$inout0);
	&pxor		($inout2,$inout1);
	&movdqa		(&QWP(16*1,"esp"),$inout1);
	&pxor		($inout3,$inout2);
	&movdqa		(&QWP(16*2,"esp"),$inout2);
	&pxor		($inout4,$inout3);
	&movdqa		(&QWP(16*3,"esp"),$inout3);
	&pxor		($inout5,$inout4);
	&movdqa		(&QWP(16*4,"esp"),$inout4);

	&$movekey	($rndkey0,&QWP(-48,$key,$rounds));
	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&movdqu		($inout2,&QWP(16*2,$inp));
	&movdqu		($inout3,&QWP(16*3,$inp));
	&movdqu		($inout4,&QWP(16*4,$inp));
	&pxor		($inout5,$inout5);

	&movdqa		(&QWP($checksum,"esp"),$rndkey1);
	&pxor		($inout0,$rndkey0);		# ^ roundkey[0]
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
	&pxor		($inout3,$rndkey0);
	&pxor		($inout4,$rndkey0);

	&$movekey	($rndkey1,&QWP(-32,$key,$rounds));
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,&QWP(16*4,"esp"));

	&$movekey	($rndkey0,&QWP(-16,$key,$rounds));
	&aesdec		($inout0,$rndkey1);
	&aesdec		($inout1,$rndkey1);
	&aesdec		($inout2,$rndkey1);
	&aesdec		($inout3,$rndkey1);
	&aesdec		($inout4,$rndkey1);
	&aesdec		($inout5,$rndkey1);

	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_decrypt6_enter");

	&movdqa		($rndkey0,&QWP(16*4,"esp"));	# pass last offset_i
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&movdqa		($rndkey1,&QWP($checksum,"esp"));
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,&QWP(16*2,"esp"));
	&pxor		($inout3,&QWP(16*3,"esp"));
	&pxor		($inout4,$rndkey0);

	&pxor		($rndkey1,$inout0);		# checksum
	&movdqu		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&pxor		($rndkey1,$inout1);
	&movdqu		(&QWP(16*1,$out,$inp),$inout1);
	&pxor		($rndkey1,$inout2);
	&movdqu		(&QWP(16*2,$out,$inp),$inout2);
	&pxor		($rndkey1,$inout3);
	&movdqu		(&QWP(16*3,$out,$inp),$inout3);
	&pxor		($rndkey1,$inout4);
	&movdqu		(&QWP(16*4,$out,$inp),$inout4);

	&jmp		(&label("done"));

&set_label("one",16);
	&movdqu		($inout5,&QWP(0,$l_));
	&mov		($key,&DWP($key_off,"esp"));	# restore key

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&mov		($rounds,&DWP(240,$key));

	&pxor		($inout5,$rndkey0);		# ^ last offset_i
	&pxor		($inout0,$inout5);		# ^ offset_i

	&movdqa		($inout4,$rndkey1);
	&mov		($out,&DWP($out_off,"esp"));
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}

	&xorps		($inout0,$inout5);		# ^ offset_i
	&movaps		($rndkey1,$inout4);		# pass the checksum
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&xorps		($rndkey1,$inout0);		# checksum
	&movups		(&QWP(0,$out,$inp),$inout0);

	&jmp		(&label("done"));

&set_label("two",16);
	&lea		($i1,&DWP(1,$block));
	&mov		($key,&DWP($key_off,"esp"));	# restore key
	&bsf		($i1,$i1);
	&shl		($i1,4);
	&movdqu		($inout4,&QWP(0,$l_));
	&movdqu		($inout5,&QWP(0,$l_,$i1));

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&mov		($rounds,&DWP(240,$key));

	&movdqa		($inout3,$rndkey1);
	&pxor		($inout4,$rndkey0);		# ^ last offset_i
	&pxor		($inout5,$inout4);

	&pxor		($inout0,$inout4);		# ^ offset_i
	&pxor		($inout1,$inout5);

	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_decrypt2");

	&xorps		($inout0,$inout4);		# ^ offset_i
	&xorps		($inout1,$inout5);
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&xorps		($inout3,$inout0);		# checksum
	&movups		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&xorps		($inout3,$inout1);
	&movups		(&QWP(16*1,$out,$inp),$inout1);
	&movaps		($rndkey1,$inout3);		# pass the checksum

	&jmp		(&label("done"));

&set_label("three",16);
	&lea		($i1,&DWP(1,$block));
	&mov		($key,&DWP($key_off,"esp"));	# restore key
	&bsf		($i1,$i1);
	&shl		($i1,4);
	&movdqu		($inout3,&QWP(0,$l_));
	&movdqu		($inout4,&QWP(0,$l_,$i1));
	&movdqa		($inout5,$inout3);

	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&movdqu		($inout1,&QWP(16*1,$inp));
	&movdqu		($inout2,&QWP(16*2,$inp));
	&mov		($rounds,&DWP(240,$key));

	&movdqa		(&QWP($checksum,"esp"),$rndkey1);
	&pxor		($inout3,$rndkey0);		# ^ last offset_i
	&pxor		($inout4,$inout3);
	&pxor		($inout5,$inout4);

	&pxor		($inout0,$inout3);		# ^ offset_i
	&pxor		($inout1,$inout4);
	&pxor		($inout2,$inout5);

	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_decrypt3");

	&movdqa		($rndkey1,&QWP($checksum,"esp"));# pass the checksum
	&xorps		($inout0,$inout3);		# ^ offset_i
	&xorps		($inout1,$inout4);
	&xorps		($inout2,$inout5);
	&movups		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&pxor		($rndkey1,$inout0);		# checksum
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movups		(&QWP(16*1,$out,$inp),$inout1);
	&pxor		($rndkey1,$inout1);
	&movups		(&QWP(16*2,$out,$inp),$inout2);
	&pxor		($rndkey1,$inout2);

	&jmp		(&label("done"));

&set_label("four",16);
	&lea		($i1,&DWP(1,$block));
	&lea		($i3,&DWP(3,$block));
	&bsf		($i1,$i1);
	&bsf		($i3,$i3);
	&mov		($key,&DWP($key_off,"esp"));	# restore key
	&shl		($i1,4);
	&shl		($i3,4);
	&movdqu		($inout2,&QWP(0,$l_));
	&movdqu		($inout3,&QWP(0,$l_,$i1));
	&movdqa		($inout4,$inout2);
	&movdqu		($inout5,&QWP(0,$l_,$i3));

	&pxor		($inout2,$rndkey0);		# ^ last offset_i
	&movdqu		($inout0,&QWP(16*0,$inp));	# load input
	&pxor		($inout3,$inout2);
	&movdqu		($inout1,&QWP(16*1,$inp));
	&pxor		($inout4,$inout3);
	&movdqa		(&QWP(16*0,"esp"),$inout2);
	&pxor		($inout5,$inout4);
	&movdqa		(&QWP(16*1,"esp"),$inout3);
	&movdqu		($inout2,&QWP(16*2,$inp));
	&movdqu		($inout3,&QWP(16*3,$inp));
	&mov		($rounds,&DWP(240,$key));

	&movdqa		(&QWP($checksum,"esp"),$rndkey1);
	&pxor		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&pxor		($inout1,&QWP(16*1,"esp"));
	&pxor		($inout2,$inout4);
	&pxor		($inout3,$inout5);

	&mov		($out,&DWP($out_off,"esp"));
	&call		("_aesni_decrypt4");

	&movdqa		($rndkey1,&QWP($checksum,"esp"));# pass the checksum
	&xorps		($inout0,&QWP(16*0,"esp"));	# ^ offset_i
	&xorps		($inout1,&QWP(16*1,"esp"));
	&xorps		($inout2,$inout4);
	&movups		(&QWP(16*0,$out,$inp),$inout0);	# store output
	&pxor		($rndkey1,$inout0);		# checksum
	&xorps		($inout3,$inout5);
	&movups		(&QWP(16*1,$out,$inp),$inout1);
	&pxor		($rndkey1,$inout1);
	&movdqa		($rndkey0,$inout5);		# pass last offset_i
	&movups		(&QWP(16*2,$out,$inp),$inout2);
	&pxor		($rndkey1,$inout2);
	&movups		(&QWP(16*3,$out,$inp),$inout3);
	&pxor		($rndkey1,$inout3);

&set_label("done");
	&mov	($key,&DWP($esp_off,"esp"));
	&pxor	($inout0,$inout0);		# clear register bank
	&pxor	($inout1,$inout1);
	&movdqa	(&QWP(16*0,"esp"),$inout0);	# clear stack
	&pxor	($inout2,$inout2);
	&movdqa	(&QWP(16*1,"esp"),$inout0);
	&pxor	($inout3,$inout3);
	&movdqa	(&QWP(16*2,"esp"),$inout0);
	&pxor	($inout4,$inout4);
	&movdqa	(&QWP(16*3,"esp"),$inout0);
	&pxor	($inout5,$inout5);
	&movdqa	(&QWP(16*4,"esp"),$inout0);
	&movdqa	(&QWP(16*5,"esp"),$inout0);
	&movdqa	(&QWP(16*6,"esp"),$inout0);

	&lea	("esp",&DWP(0,$key));
	&mov	($rounds,&wparam(5));		# &offset_i
	&mov	($rounds_,&wparam(7));		# &checksum
	&movdqu	(&QWP(0,$rounds),$rndkey0);
	&pxor	($rndkey0,$rndkey0);
	&movdqu	(&QWP(0,$rounds_),$rndkey1);
	&pxor	($rndkey1,$rndkey1);
&function_end("aesni_ocb_decrypt");
}
A
Andy Polyakov 已提交
2722 2723 2724
}

######################################################################
A
Andy Polyakov 已提交
2725 2726 2727 2728 2729
# void $PREFIX_cbc_encrypt (const void *inp, void *out,
#                           size_t length, const AES_KEY *key,
#                           unsigned char *ivp,const int enc);
&function_begin("${PREFIX}_cbc_encrypt");
	&mov	($inp,&wparam(0));
2730
	&mov	($rounds_,"esp");
A
Andy Polyakov 已提交
2731
	&mov	($out,&wparam(1));
2732
	&sub	($rounds_,24);
A
Andy Polyakov 已提交
2733
	&mov	($len,&wparam(2));
2734
	&and	($rounds_,-16);
A
Andy Polyakov 已提交
2735 2736
	&mov	($key,&wparam(3));
	&mov	($key_,&wparam(4));
2737
	&test	($len,$len);
2738
	&jz	(&label("cbc_abort"));
A
Andy Polyakov 已提交
2739 2740

	&cmp	(&wparam(5),0);
2741 2742
	&xchg	($rounds_,"esp");		# alloca
	&movups	($ivec,&QWP(0,$key_));		# load IV
A
Andy Polyakov 已提交
2743
	&mov	($rounds,&DWP(240,$key));
2744 2745 2746
	&mov	($key_,$key);			# backup $key
	&mov	(&DWP(16,"esp"),$rounds_);	# save original %esp
	&mov	($rounds_,$rounds);		# backup $rounds
A
Andy Polyakov 已提交
2747 2748
	&je	(&label("cbc_decrypt"));

2749
	&movaps	($inout0,$ivec);
A
Andy Polyakov 已提交
2750 2751 2752 2753 2754 2755
	&cmp	($len,16);
	&jb	(&label("cbc_enc_tail"));
	&sub	($len,16);
	&jmp	(&label("cbc_enc_loop"));

&set_label("cbc_enc_loop",16);
2756
	&movups	($ivec,&QWP(0,$inp));		# input actually
A
Andy Polyakov 已提交
2757
	&lea	($inp,&DWP(16,$inp));
2758
	if ($inline)
2759
	{   &aesni_inline_generate1("enc",$inout0,$ivec);	}
2760
	else
2761
	{   &xorps($inout0,$ivec); &call("_aesni_encrypt1");	}
A
Andy Polyakov 已提交
2762 2763
	&mov	($rounds,$rounds_);	# restore $rounds
	&mov	($key,$key_);		# restore $key
2764 2765 2766
	&movups	(&QWP(0,$out),$inout0);	# store output
	&lea	($out,&DWP(16,$out));
	&sub	($len,16);
A
Andy Polyakov 已提交
2767 2768 2769 2770
	&jnc	(&label("cbc_enc_loop"));
	&add	($len,16);
	&jnz	(&label("cbc_enc_tail"));
	&movaps	($ivec,$inout0);
2771
	&pxor	($inout0,$inout0);
A
Andy Polyakov 已提交
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
	&jmp	(&label("cbc_ret"));

&set_label("cbc_enc_tail");
	&mov	("ecx",$len);		# zaps $rounds
	&data_word(0xA4F3F689);		# rep movsb
	&mov	("ecx",16);		# zero tail
	&sub	("ecx",$len);
	&xor	("eax","eax");		# zaps $len
	&data_word(0xAAF3F689);		# rep stosb
	&lea	($out,&DWP(-16,$out));	# rewind $out by 1 block
	&mov	($rounds,$rounds_);	# restore $rounds
	&mov	($inp,$out);		# $inp and $out are the same
	&mov	($key,$key_);		# restore $key
	&jmp	(&label("cbc_enc_loop"));
A
Andy Polyakov 已提交
2786
######################################################################
A
Andy Polyakov 已提交
2787
&set_label("cbc_decrypt",16);
2788
	&cmp	($len,0x50);
A
Andy Polyakov 已提交
2789
	&jbe	(&label("cbc_dec_tail"));
2790 2791 2792
	&movaps	(&QWP(0,"esp"),$ivec);		# save IV
	&sub	($len,0x50);
	&jmp	(&label("cbc_dec_loop6_enter"));
A
Andy Polyakov 已提交
2793

2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
&set_label("cbc_dec_loop6",16);
	&movaps	(&QWP(0,"esp"),$rndkey0);	# save IV
	&movups	(&QWP(0,$out),$inout5);
	&lea	($out,&DWP(0x10,$out));
&set_label("cbc_dec_loop6_enter");
	&movdqu	($inout0,&QWP(0,$inp));
	&movdqu	($inout1,&QWP(0x10,$inp));
	&movdqu	($inout2,&QWP(0x20,$inp));
	&movdqu	($inout3,&QWP(0x30,$inp));
	&movdqu	($inout4,&QWP(0x40,$inp));
	&movdqu	($inout5,&QWP(0x50,$inp));

	&call	("_aesni_decrypt6");

	&movups	($rndkey1,&QWP(0,$inp));
	&movups	($rndkey0,&QWP(0x10,$inp));
	&xorps	($inout0,&QWP(0,"esp"));	# ^=IV
	&xorps	($inout1,$rndkey1);
	&movups	($rndkey1,&QWP(0x20,$inp));
	&xorps	($inout2,$rndkey0);
	&movups	($rndkey0,&QWP(0x30,$inp));
	&xorps	($inout3,$rndkey1);
	&movups	($rndkey1,&QWP(0x40,$inp));
	&xorps	($inout4,$rndkey0);
	&movups	($rndkey0,&QWP(0x50,$inp));	# IV
	&xorps	($inout5,$rndkey1);
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
	&lea	($inp,&DWP(0x60,$inp));
	&movups	(&QWP(0x20,$out),$inout2);
2824
	&mov	($rounds,$rounds_);		# restore $rounds
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
	&movups	(&QWP(0x30,$out),$inout3);
	&mov	($key,$key_);			# restore $key
	&movups	(&QWP(0x40,$out),$inout4);
	&lea	($out,&DWP(0x50,$out));
	&sub	($len,0x60);
	&ja	(&label("cbc_dec_loop6"));

	&movaps	($inout0,$inout5);
	&movaps	($ivec,$rndkey0);
	&add	($len,0x50);
2835
	&jle	(&label("cbc_dec_clear_tail_collected"));
2836 2837
	&movups	(&QWP(0,$out),$inout0);
	&lea	($out,&DWP(0x10,$out));
A
Andy Polyakov 已提交
2838
&set_label("cbc_dec_tail");
A
Andy Polyakov 已提交
2839 2840
	&movups	($inout0,&QWP(0,$inp));
	&movaps	($in0,$inout0);
2841
	&cmp	($len,0x10);
A
Andy Polyakov 已提交
2842
	&jbe	(&label("cbc_dec_one"));
2843

A
Andy Polyakov 已提交
2844 2845
	&movups	($inout1,&QWP(0x10,$inp));
	&movaps	($in1,$inout1);
2846
	&cmp	($len,0x20);
A
Andy Polyakov 已提交
2847
	&jbe	(&label("cbc_dec_two"));
2848

A
Andy Polyakov 已提交
2849
	&movups	($inout2,&QWP(0x20,$inp));
A
Andy Polyakov 已提交
2850 2851
	&cmp	($len,0x30);
	&jbe	(&label("cbc_dec_three"));
2852

A
Andy Polyakov 已提交
2853
	&movups	($inout3,&QWP(0x30,$inp));
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
	&cmp	($len,0x40);
	&jbe	(&label("cbc_dec_four"));

	&movups	($inout4,&QWP(0x40,$inp));
	&movaps	(&QWP(0,"esp"),$ivec);		# save IV
	&movups	($inout0,&QWP(0,$inp));
	&xorps	($inout5,$inout5);
	&call	("_aesni_decrypt6");
	&movups	($rndkey1,&QWP(0,$inp));
	&movups	($rndkey0,&QWP(0x10,$inp));
	&xorps	($inout0,&QWP(0,"esp"));	# ^= IV
	&xorps	($inout1,$rndkey1);
	&movups	($rndkey1,&QWP(0x20,$inp));
	&xorps	($inout2,$rndkey0);
	&movups	($rndkey0,&QWP(0x30,$inp));
	&xorps	($inout3,$rndkey1);
	&movups	($ivec,&QWP(0x40,$inp));	# IV
	&xorps	($inout4,$rndkey0);
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
2874
	&pxor	($inout1,$inout1);
2875
	&movups	(&QWP(0x20,$out),$inout2);
2876
	&pxor	($inout2,$inout2);
2877
	&movups	(&QWP(0x30,$out),$inout3);
2878
	&pxor	($inout3,$inout3);
2879 2880
	&lea	($out,&DWP(0x40,$out));
	&movaps	($inout0,$inout4);
2881
	&pxor	($inout4,$inout4);
2882
	&sub	($len,0x50);
A
Andy Polyakov 已提交
2883 2884
	&jmp	(&label("cbc_dec_tail_collected"));

2885
&set_label("cbc_dec_one",16);
2886 2887 2888 2889
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
2890 2891 2892
	&xorps	($inout0,$ivec);
	&movaps	($ivec,$in0);
	&sub	($len,0x10);
A
Andy Polyakov 已提交
2893 2894
	&jmp	(&label("cbc_dec_tail_collected"));

2895
&set_label("cbc_dec_two",16);
2896
	&call	("_aesni_decrypt2");
2897 2898 2899 2900
	&xorps	($inout0,$ivec);
	&xorps	($inout1,$in0);
	&movups	(&QWP(0,$out),$inout0);
	&movaps	($inout0,$inout1);
2901
	&pxor	($inout1,$inout1);
A
Andy Polyakov 已提交
2902
	&lea	($out,&DWP(0x10,$out));
2903 2904
	&movaps	($ivec,$in1);
	&sub	($len,0x20);
A
Andy Polyakov 已提交
2905 2906
	&jmp	(&label("cbc_dec_tail_collected"));

2907
&set_label("cbc_dec_three",16);
A
Andy Polyakov 已提交
2908
	&call	("_aesni_decrypt3");
2909 2910 2911 2912 2913
	&xorps	($inout0,$ivec);
	&xorps	($inout1,$in0);
	&xorps	($inout2,$in1);
	&movups	(&QWP(0,$out),$inout0);
	&movaps	($inout0,$inout2);
2914
	&pxor	($inout2,$inout2);
2915
	&movups	(&QWP(0x10,$out),$inout1);
2916
	&pxor	($inout1,$inout1);
A
Andy Polyakov 已提交
2917
	&lea	($out,&DWP(0x20,$out));
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
	&movups	($ivec,&QWP(0x20,$inp));
	&sub	($len,0x30);
	&jmp	(&label("cbc_dec_tail_collected"));

&set_label("cbc_dec_four",16);
	&call	("_aesni_decrypt4");
	&movups	($rndkey1,&QWP(0x10,$inp));
	&movups	($rndkey0,&QWP(0x20,$inp));
	&xorps	($inout0,$ivec);
	&movups	($ivec,&QWP(0x30,$inp));
	&xorps	($inout1,$in0);
	&movups	(&QWP(0,$out),$inout0);
	&xorps	($inout2,$rndkey1);
	&movups	(&QWP(0x10,$out),$inout1);
2932
	&pxor	($inout1,$inout1);
2933 2934
	&xorps	($inout3,$rndkey0);
	&movups	(&QWP(0x20,$out),$inout2);
2935
	&pxor	($inout2,$inout2);
2936 2937
	&lea	($out,&DWP(0x30,$out));
	&movaps	($inout0,$inout3);
2938
	&pxor	($inout3,$inout3);
2939
	&sub	($len,0x40);
2940
	&jmp	(&label("cbc_dec_tail_collected"));
A
Andy Polyakov 已提交
2941

2942 2943 2944 2945 2946
&set_label("cbc_dec_clear_tail_collected",16);
	&pxor	($inout1,$inout1);
	&pxor	($inout2,$inout2);
	&pxor	($inout3,$inout3);
	&pxor	($inout4,$inout4);
A
Andy Polyakov 已提交
2947 2948 2949
&set_label("cbc_dec_tail_collected");
	&and	($len,15);
	&jnz	(&label("cbc_dec_tail_partial"));
2950
	&movups	(&QWP(0,$out),$inout0);
2951
	&pxor	($rndkey0,$rndkey0);
A
Andy Polyakov 已提交
2952 2953
	&jmp	(&label("cbc_ret"));

2954
&set_label("cbc_dec_tail_partial",16);
2955
	&movaps	(&QWP(0,"esp"),$inout0);
2956
	&pxor	($rndkey0,$rndkey0);
2957
	&mov	("ecx",16);
A
Andy Polyakov 已提交
2958
	&mov	($inp,"esp");
2959
	&sub	("ecx",$len);
A
Andy Polyakov 已提交
2960
	&data_word(0xA4F3F689);		# rep movsb
2961
	&movdqa	(&QWP(0,"esp"),$inout0);
A
Andy Polyakov 已提交
2962 2963

&set_label("cbc_ret");
2964
	&mov	("esp",&DWP(16,"esp"));	# pull original %esp
A
Andy Polyakov 已提交
2965
	&mov	($key_,&wparam(4));
2966 2967
	&pxor	($inout0,$inout0);
	&pxor	($rndkey1,$rndkey1);
A
Andy Polyakov 已提交
2968
	&movups	(&QWP(0,$key_),$ivec);	# output IV
2969
	&pxor	($ivec,$ivec);
2970
&set_label("cbc_abort");
A
Andy Polyakov 已提交
2971
&function_end("${PREFIX}_cbc_encrypt");
A
Andy Polyakov 已提交
2972 2973

######################################################################
A
Andy Polyakov 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
# Mechanical port from aesni-x86_64.pl.
#
# _aesni_set_encrypt_key is private interface,
# input:
#	"eax"	const unsigned char *userKey
#	$rounds	int bits
#	$key	AES_KEY *key
# output:
#	"eax"	return code
#	$round	rounds

&function_begin_B("_aesni_set_encrypt_key");
2986 2987
	&push	("ebp");
	&push	("ebx");
A
Andy Polyakov 已提交
2988 2989 2990 2991 2992
	&test	("eax","eax");
	&jz	(&label("bad_pointer"));
	&test	($key,$key);
	&jz	(&label("bad_pointer"));

2993 2994 2995 2996 2997 2998
	&call	(&label("pic"));
&set_label("pic");
	&blindpop("ebx");
	&lea	("ebx",&DWP(&label("key_const")."-".&label("pic"),"ebx"));

	&picmeup("ebp","OPENSSL_ia32cap_P","ebx",&label("key_const"));
A
Andy Polyakov 已提交
2999
	&movups	("xmm0",&QWP(0,"eax"));	# pull first 128 bits of *userKey
3000
	&xorps	("xmm4","xmm4");	# low dword of xmm4 is assumed 0
3001
	&mov	("ebp",&DWP(4,"ebp"));
A
Andy Polyakov 已提交
3002
	&lea	($key,&DWP(16,$key));
3003
	&and	("ebp",1<<28|1<<11);	# AVX and XOP bits
A
Andy Polyakov 已提交
3004 3005 3006 3007 3008 3009 3010 3011
	&cmp	($rounds,256);
	&je	(&label("14rounds"));
	&cmp	($rounds,192);
	&je	(&label("12rounds"));
	&cmp	($rounds,128);
	&jne	(&label("bad_keybits"));

&set_label("10rounds",16);
3012 3013 3014
	&cmp		("ebp",1<<28);
	&je		(&label("10rounds_alt"));

A
Andy Polyakov 已提交
3015
	&mov		($rounds,9);
A
Andy Polyakov 已提交
3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
	&$movekey	(&QWP(-16,$key),"xmm0");	# round 0
	&aeskeygenassist("xmm1","xmm0",0x01);		# round 1
	&call		(&label("key_128_cold"));
	&aeskeygenassist("xmm1","xmm0",0x2);		# round 2
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x04);		# round 3
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x08);		# round 4
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x10);		# round 5
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x20);		# round 6
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x40);		# round 7
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x80);		# round 8
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x1b);		# round 9
	&call		(&label("key_128"));
	&aeskeygenassist("xmm1","xmm0",0x36);		# round 10
	&call		(&label("key_128"));
	&$movekey	(&QWP(0,$key),"xmm0");
	&mov		(&DWP(80,$key),$rounds);
3039 3040

	&jmp	(&label("good_key"));
A
Andy Polyakov 已提交
3041 3042 3043 3044 3045 3046

&set_label("key_128",16);
	&$movekey	(&QWP(0,$key),"xmm0");
	&lea		($key,&DWP(16,$key));
&set_label("key_128_cold");
	&shufps		("xmm4","xmm0",0b00010000);
3047 3048 3049 3050 3051
	&xorps		("xmm0","xmm4");
	&shufps		("xmm4","xmm0",0b10001100);
	&xorps		("xmm0","xmm4");
	&shufps		("xmm1","xmm1",0b11111111);	# critical path
	&xorps		("xmm0","xmm1");
A
Andy Polyakov 已提交
3052 3053
	&ret();

3054 3055 3056 3057 3058
&set_label("10rounds_alt",16);
	&movdqa		("xmm5",&QWP(0x00,"ebx"));
	&mov		($rounds,8);
	&movdqa		("xmm4",&QWP(0x20,"ebx"));
	&movdqa		("xmm2","xmm0");
3059
	&movdqu		(&QWP(-16,$key),"xmm0");
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118

&set_label("loop_key128");
	&pshufb		("xmm0","xmm5");
	&aesenclast	("xmm0","xmm4");
	&pslld		("xmm4",1);
	&lea		($key,&DWP(16,$key));

	&movdqa		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm2","xmm3");

	&pxor		("xmm0","xmm2");
	&movdqu		(&QWP(-16,$key),"xmm0");
	&movdqa		("xmm2","xmm0");

	&dec		($rounds);
	&jnz		(&label("loop_key128"));

	&movdqa		("xmm4",&QWP(0x30,"ebx"));

	&pshufb		("xmm0","xmm5");
	&aesenclast	("xmm0","xmm4");
	&pslld		("xmm4",1);

	&movdqa		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm2","xmm3");

	&pxor		("xmm0","xmm2");
	&movdqu		(&QWP(0,$key),"xmm0");

	&movdqa		("xmm2","xmm0");
	&pshufb		("xmm0","xmm5");
	&aesenclast	("xmm0","xmm4");

	&movdqa		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm3","xmm2");
	&pslldq		("xmm2",4);
	&pxor		("xmm2","xmm3");

	&pxor		("xmm0","xmm2");
	&movdqu		(&QWP(16,$key),"xmm0");

	&mov		($rounds,9);
	&mov		(&DWP(96,$key),$rounds);

	&jmp	(&label("good_key"));

A
Andy Polyakov 已提交
3119 3120
&set_label("12rounds",16);
	&movq		("xmm2",&QWP(16,"eax"));	# remaining 1/3 of *userKey
3121 3122 3123
	&cmp		("ebp",1<<28);
	&je		(&label("12rounds_alt"));

A
Andy Polyakov 已提交
3124
	&mov		($rounds,11);
3125
	&$movekey	(&QWP(-16,$key),"xmm0");	# round 0
A
Andy Polyakov 已提交
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
	&aeskeygenassist("xmm1","xmm2",0x01);		# round 1,2
	&call		(&label("key_192a_cold"));
	&aeskeygenassist("xmm1","xmm2",0x02);		# round 2,3
	&call		(&label("key_192b"));
	&aeskeygenassist("xmm1","xmm2",0x04);		# round 4,5
	&call		(&label("key_192a"));
	&aeskeygenassist("xmm1","xmm2",0x08);		# round 5,6
	&call		(&label("key_192b"));
	&aeskeygenassist("xmm1","xmm2",0x10);		# round 7,8
	&call		(&label("key_192a"));
	&aeskeygenassist("xmm1","xmm2",0x20);		# round 8,9
	&call		(&label("key_192b"));
	&aeskeygenassist("xmm1","xmm2",0x40);		# round 10,11
	&call		(&label("key_192a"));
	&aeskeygenassist("xmm1","xmm2",0x80);		# round 11,12
	&call		(&label("key_192b"));
	&$movekey	(&QWP(0,$key),"xmm0");
	&mov		(&DWP(48,$key),$rounds);
3144 3145

	&jmp	(&label("good_key"));
A
Andy Polyakov 已提交
3146 3147 3148 3149 3150 3151 3152 3153

&set_label("key_192a",16);
	&$movekey	(&QWP(0,$key),"xmm0");
	&lea		($key,&DWP(16,$key));
&set_label("key_192a_cold",16);
	&movaps		("xmm5","xmm2");
&set_label("key_192b_warm");
	&shufps		("xmm4","xmm0",0b00010000);
3154 3155
	&movdqa		("xmm3","xmm2");
	&xorps		("xmm0","xmm4");
A
Andy Polyakov 已提交
3156 3157
	&shufps		("xmm4","xmm0",0b10001100);
	&pslldq		("xmm3",4);
3158
	&xorps		("xmm0","xmm4");
A
Andy Polyakov 已提交
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174
	&pshufd		("xmm1","xmm1",0b01010101);	# critical path
	&pxor		("xmm2","xmm3");
	&pxor		("xmm0","xmm1");
	&pshufd		("xmm3","xmm0",0b11111111);
	&pxor		("xmm2","xmm3");
	&ret();

&set_label("key_192b",16);
	&movaps		("xmm3","xmm0");
	&shufps		("xmm5","xmm0",0b01000100);
	&$movekey	(&QWP(0,$key),"xmm5");
	&shufps		("xmm3","xmm2",0b01001110);
	&$movekey	(&QWP(16,$key),"xmm3");
	&lea		($key,&DWP(32,$key));
	&jmp		(&label("key_192b_warm"));

3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
&set_label("12rounds_alt",16);
	&movdqa		("xmm5",&QWP(0x10,"ebx"));
	&movdqa		("xmm4",&QWP(0x20,"ebx"));
	&mov		($rounds,8);
	&movdqu		(&QWP(-16,$key),"xmm0");

&set_label("loop_key192");
	&movq		(&QWP(0,$key),"xmm2");
	&movdqa		("xmm1","xmm2");
	&pshufb		("xmm2","xmm5");
	&aesenclast	("xmm2","xmm4");
	&pslld		("xmm4",1);
	&lea		($key,&DWP(24,$key));

	&movdqa		("xmm3","xmm0");
	&pslldq		("xmm0",4);
	&pxor		("xmm3","xmm0");
	&pslldq		("xmm0",4);
	&pxor		("xmm3","xmm0");
	&pslldq		("xmm0",4);
	&pxor		("xmm0","xmm3");

	&pshufd		("xmm3","xmm0",0xff);
	&pxor		("xmm3","xmm1");
	&pslldq		("xmm1",4);
	&pxor		("xmm3","xmm1");

	&pxor		("xmm0","xmm2");
	&pxor		("xmm2","xmm3");
	&movdqu		(&QWP(-16,$key),"xmm0");

	&dec		($rounds);
	&jnz		(&label("loop_key192"));

	&mov	($rounds,11);
	&mov	(&DWP(32,$key),$rounds);

	&jmp	(&label("good_key"));

A
Andy Polyakov 已提交
3214 3215 3216
&set_label("14rounds",16);
	&movups		("xmm2",&QWP(16,"eax"));	# remaining half of *userKey
	&lea		($key,&DWP(16,$key));
3217 3218 3219 3220
	&cmp		("ebp",1<<28);
	&je		(&label("14rounds_alt"));

	&mov		($rounds,13);
A
Andy Polyakov 已提交
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
	&$movekey	(&QWP(-32,$key),"xmm0");	# round 0
	&$movekey	(&QWP(-16,$key),"xmm2");	# round 1
	&aeskeygenassist("xmm1","xmm2",0x01);		# round 2
	&call		(&label("key_256a_cold"));
	&aeskeygenassist("xmm1","xmm0",0x01);		# round 3
	&call		(&label("key_256b"));
	&aeskeygenassist("xmm1","xmm2",0x02);		# round 4
	&call		(&label("key_256a"));
	&aeskeygenassist("xmm1","xmm0",0x02);		# round 5
	&call		(&label("key_256b"));
	&aeskeygenassist("xmm1","xmm2",0x04);		# round 6
	&call		(&label("key_256a"));
	&aeskeygenassist("xmm1","xmm0",0x04);		# round 7
	&call		(&label("key_256b"));
	&aeskeygenassist("xmm1","xmm2",0x08);		# round 8
	&call		(&label("key_256a"));
	&aeskeygenassist("xmm1","xmm0",0x08);		# round 9
	&call		(&label("key_256b"));
	&aeskeygenassist("xmm1","xmm2",0x10);		# round 10
	&call		(&label("key_256a"));
	&aeskeygenassist("xmm1","xmm0",0x10);		# round 11
	&call		(&label("key_256b"));
	&aeskeygenassist("xmm1","xmm2",0x20);		# round 12
	&call		(&label("key_256a"));
	&aeskeygenassist("xmm1","xmm0",0x20);		# round 13
	&call		(&label("key_256b"));
	&aeskeygenassist("xmm1","xmm2",0x40);		# round 14
	&call		(&label("key_256a"));
	&$movekey	(&QWP(0,$key),"xmm0");
	&mov		(&DWP(16,$key),$rounds);
	&xor		("eax","eax");
3252 3253

	&jmp	(&label("good_key"));
A
Andy Polyakov 已提交
3254 3255 3256 3257 3258 3259

&set_label("key_256a",16);
	&$movekey	(&QWP(0,$key),"xmm2");
	&lea		($key,&DWP(16,$key));
&set_label("key_256a_cold");
	&shufps		("xmm4","xmm0",0b00010000);
3260
	&xorps		("xmm0","xmm4");
A
Andy Polyakov 已提交
3261
	&shufps		("xmm4","xmm0",0b10001100);
3262 3263 3264
	&xorps		("xmm0","xmm4");
	&shufps		("xmm1","xmm1",0b11111111);	# critical path
	&xorps		("xmm0","xmm1");
A
Andy Polyakov 已提交
3265 3266 3267 3268 3269 3270 3271
	&ret();

&set_label("key_256b",16);
	&$movekey	(&QWP(0,$key),"xmm0");
	&lea		($key,&DWP(16,$key));

	&shufps		("xmm4","xmm2",0b00010000);
3272
	&xorps		("xmm2","xmm4");
A
Andy Polyakov 已提交
3273
	&shufps		("xmm4","xmm2",0b10001100);
3274 3275 3276
	&xorps		("xmm2","xmm4");
	&shufps		("xmm1","xmm1",0b10101010);	# critical path
	&xorps		("xmm2","xmm1");
A
Andy Polyakov 已提交
3277 3278
	&ret();

3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309
&set_label("14rounds_alt",16);
	&movdqa		("xmm5",&QWP(0x00,"ebx"));
	&movdqa		("xmm4",&QWP(0x20,"ebx"));
	&mov		($rounds,7);
	&movdqu		(&QWP(-32,$key),"xmm0");
	&movdqa		("xmm1","xmm2");
	&movdqu		(&QWP(-16,$key),"xmm2");

&set_label("loop_key256");
	&pshufb		("xmm2","xmm5");
	&aesenclast	("xmm2","xmm4");

	&movdqa		("xmm3","xmm0");
	&pslldq		("xmm0",4);
	&pxor		("xmm3","xmm0");
	&pslldq		("xmm0",4);
	&pxor		("xmm3","xmm0");
	&pslldq		("xmm0",4);
	&pxor		("xmm0","xmm3");
	&pslld		("xmm4",1);

	&pxor		("xmm0","xmm2");
	&movdqu		(&QWP(0,$key),"xmm0");

	&dec		($rounds);
	&jz		(&label("done_key256"));

	&pshufd		("xmm2","xmm0",0xff);
	&pxor		("xmm3","xmm3");
	&aesenclast	("xmm2","xmm3");

3310
	&movdqa		("xmm3","xmm1");
3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
	&pslldq		("xmm1",4);
	&pxor		("xmm3","xmm1");
	&pslldq		("xmm1",4);
	&pxor		("xmm3","xmm1");
	&pslldq		("xmm1",4);
	&pxor		("xmm1","xmm3");

	&pxor		("xmm2","xmm1");
	&movdqu		(&QWP(16,$key),"xmm2");
	&lea		($key,&DWP(32,$key));
	&movdqa		("xmm1","xmm2");
	&jmp		(&label("loop_key256"));

&set_label("done_key256");
	&mov		($rounds,13);
	&mov		(&DWP(16,$key),$rounds);

&set_label("good_key");
	&pxor	("xmm0","xmm0");
	&pxor	("xmm1","xmm1");
	&pxor	("xmm2","xmm2");
	&pxor	("xmm3","xmm3");
	&pxor	("xmm4","xmm4");
	&pxor	("xmm5","xmm5");
	&xor	("eax","eax");
	&pop	("ebx");
	&pop	("ebp");
	&ret	();

A
Andy Polyakov 已提交
3340 3341
&set_label("bad_pointer",4);
	&mov	("eax",-1);
3342 3343
	&pop	("ebx");
	&pop	("ebp");
A
Andy Polyakov 已提交
3344 3345
	&ret	();
&set_label("bad_keybits",4);
3346
	&pxor	("xmm0","xmm0");
A
Andy Polyakov 已提交
3347
	&mov	("eax",-2);
3348 3349
	&pop	("ebx");
	&pop	("ebp");
A
Andy Polyakov 已提交
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370
	&ret	();
&function_end_B("_aesni_set_encrypt_key");

# int $PREFIX_set_encrypt_key (const unsigned char *userKey, int bits,
#                              AES_KEY *key)
&function_begin_B("${PREFIX}_set_encrypt_key");
	&mov	("eax",&wparam(0));
	&mov	($rounds,&wparam(1));
	&mov	($key,&wparam(2));
	&call	("_aesni_set_encrypt_key");
	&ret	();
&function_end_B("${PREFIX}_set_encrypt_key");

# int $PREFIX_set_decrypt_key (const unsigned char *userKey, int bits,
#                              AES_KEY *key)
&function_begin_B("${PREFIX}_set_decrypt_key");
	&mov	("eax",&wparam(0));
	&mov	($rounds,&wparam(1));
	&mov	($key,&wparam(2));
	&call	("_aesni_set_encrypt_key");
	&mov	($key,&wparam(2));
3371
	&shl	($rounds,4);	# rounds-1 after _aesni_set_encrypt_key
A
Andy Polyakov 已提交
3372 3373
	&test	("eax","eax");
	&jnz	(&label("dec_key_ret"));
A
Andy Polyakov 已提交
3374
	&lea	("eax",&DWP(16,$key,$rounds));	# end of key schedule
A
Andy Polyakov 已提交
3375 3376 3377 3378 3379 3380 3381 3382

	&$movekey	("xmm0",&QWP(0,$key));	# just swap
	&$movekey	("xmm1",&QWP(0,"eax"));
	&$movekey	(&QWP(0,"eax"),"xmm0");
	&$movekey	(&QWP(0,$key),"xmm1");
	&lea		($key,&DWP(16,$key));
	&lea		("eax",&DWP(-16,"eax"));

A
Andy Polyakov 已提交
3383
&set_label("dec_key_inverse");
A
Andy Polyakov 已提交
3384 3385 3386 3387 3388 3389 3390 3391
	&$movekey	("xmm0",&QWP(0,$key));	# swap and inverse
	&$movekey	("xmm1",&QWP(0,"eax"));
	&aesimc		("xmm0","xmm0");
	&aesimc		("xmm1","xmm1");
	&lea		($key,&DWP(16,$key));
	&lea		("eax",&DWP(-16,"eax"));
	&$movekey	(&QWP(16,"eax"),"xmm0");
	&$movekey	(&QWP(-16,$key),"xmm1");
3392
	&cmp		("eax",$key);
A
Andy Polyakov 已提交
3393 3394 3395 3396 3397 3398
	&ja		(&label("dec_key_inverse"));

	&$movekey	("xmm0",&QWP(0,$key));	# inverse middle
	&aesimc		("xmm0","xmm0");
	&$movekey	(&QWP(0,$key),"xmm0");

3399 3400
	&pxor		("xmm0","xmm0");
	&pxor		("xmm1","xmm1");
A
Andy Polyakov 已提交
3401 3402 3403 3404
	&xor		("eax","eax");		# return success
&set_label("dec_key_ret");
	&ret	();
&function_end_B("${PREFIX}_set_decrypt_key");
3405 3406 3407 3408 3409 3410

&set_label("key_const",64);
&data_word(0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d);
&data_word(0x04070605,0x04070605,0x04070605,0x04070605);
&data_word(1,1,1,1);
&data_word(0x1b,0x1b,0x1b,0x1b);
A
Andy Polyakov 已提交
3411 3412 3413
&asciz("AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>");

&asm_finish();
3414

3415
close STDOUT or die "error closing STDOUT: $!";