aesni-x86.pl 34.0 KB
Newer Older
A
Andy Polyakov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/env perl

# ====================================================================
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
# 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].
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#
# 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
# 1% slower for ECB, CBC and CCM, and ~3% slower otherwise. 
A
Andy Polyakov 已提交
31 32 33 34

$PREFIX="aesni";	# if $PREFIX is set to "AES", the script
			# generates drop-in replacement for
			# crypto/aes/asm/aes-586.pl:-)
35
$inline=1;		# inline _aesni_[en|de]crypt
A
Andy Polyakov 已提交
36 37 38 39 40 41 42

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

&asm_init($ARGV[0],$0);

A
Andy Polyakov 已提交
43 44
if ($PREFIX eq "aesni")	{ $movekey=*movaps; }
else			{ $movekey=*movups; }
A
Andy Polyakov 已提交
45 46 47 48 49 50

$len="eax";
$rounds="ecx";
$key="edx";
$inp="esi";
$out="edi";
A
Andy Polyakov 已提交
51 52
$rounds_="ebx";	# backup copy for $rounds
$key_="ebp";	# backup copy for $key
A
Andy Polyakov 已提交
53 54 55 56 57 58 59 60

$inout0="xmm0";
$inout1="xmm1";
$inout2="xmm2";
$rndkey0="xmm3";
$rndkey1="xmm4";
$ivec="xmm5";
$in0="xmm6";
A
Andy Polyakov 已提交
61
$in1="xmm7";	$inout3="xmm7";
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

# AESNI extenstion
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 已提交
79

A
Andy Polyakov 已提交
80
# Inline version of internal aesni_[en|de]crypt1
81
{ my $sn;
A
Andy Polyakov 已提交
82
sub aesni_inline_generate1
83 84
{ my ($p,$inout)=@_; $inout=$inout0 if (!defined($inout));
  $sn++;
A
Andy Polyakov 已提交
85

86
    &movdqu		($rndkey0,&QWP(0,$key));
A
Andy Polyakov 已提交
87 88
    &$movekey		($rndkey1,&QWP(16,$key));
    &lea		($key,&DWP(32,$key));
89 90 91
    &pxor		($inout,$rndkey0);
    &set_label("${p}1_loop_$sn");
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
92 93
	&dec		($rounds);
	&$movekey	($rndkey1,&QWP(0,$key));
A
Andy Polyakov 已提交
94
	&lea		($key,&DWP(16,$key));
95 96 97
    &jnz		(&label("${p}1_loop_$sn"));
    eval"&aes${p}last	($inout,$rndkey1)";
}}
A
Andy Polyakov 已提交
98 99

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

    &function_begin_B("_aesni_${p}rypt1");
103
	&movdqu		($rndkey0,&QWP(0,$key));
A
Andy Polyakov 已提交
104
	&$movekey	($rndkey1,&QWP(0x10,$key));
105
	&pxor		($inout,$rndkey0);
A
Andy Polyakov 已提交
106 107
	&$movekey	($rndkey0,&QWP(0x20,$key));
	&lea		($key,&DWP(0x30,$key));
108
	&cmp		($rounds,11);
A
Andy Polyakov 已提交
109 110 111 112
	&jb		(&label("${p}128"));
	&lea		($key,&DWP(0x20,$key));
	&je		(&label("${p}192"));
	&lea		($key,&DWP(0x20,$key));
113
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
114
	&$movekey	($rndkey1,&QWP(-0x40,$key));
115
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
116 117
	&$movekey	($rndkey0,&QWP(-0x30,$key));
    &set_label("${p}192");
118
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
119
	&$movekey	($rndkey1,&QWP(-0x20,$key));
120
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
121 122
	&$movekey	($rndkey0,&QWP(-0x10,$key));
    &set_label("${p}128");
123
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
124
	&$movekey	($rndkey1,&QWP(0,$key));
125
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
126
	&$movekey	($rndkey0,&QWP(0x10,$key));
127
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
128
	&$movekey	($rndkey1,&QWP(0x20,$key));
129
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
130
	&$movekey	($rndkey0,&QWP(0x30,$key));
131
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
132
	&$movekey	($rndkey1,&QWP(0x40,$key));
133
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
134
	&$movekey	($rndkey0,&QWP(0x50,$key));
135
	eval"&aes${p}	($inout,$rndkey1)";
A
Andy Polyakov 已提交
136
	&$movekey	($rndkey1,&QWP(0x60,$key));
137
	eval"&aes${p}	($inout,$rndkey0)";
A
Andy Polyakov 已提交
138
	&$movekey	($rndkey0,&QWP(0x70,$key));
139 140
	eval"&aes${p}	($inout,$rndkey1)";
    eval"&aes${p}last	($inout,$rndkey0)";
A
Andy Polyakov 已提交
141 142 143
    &ret();
    &function_end_B("_aesni_${p}rypt1");
}
A
Andy Polyakov 已提交
144

A
Andy Polyakov 已提交
145
# void $PREFIX_encrypt (const void *inp,void *out,const AES_KEY *key);
146
&aesni_generate1("enc") if (!$inline);
A
Andy Polyakov 已提交
147 148 149
&function_begin_B("${PREFIX}_encrypt");
	&mov	("eax",&wparam(0));
	&mov	($key,&wparam(2));
150
	&movdqu	($inout0,&QWP(0,"eax"));
A
Andy Polyakov 已提交
151 152
	&mov	($rounds,&DWP(240,$key));
	&mov	("eax",&wparam(1));
153 154 155 156
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
A
Andy Polyakov 已提交
157 158 159 160 161
	&movups	(&QWP(0,"eax"),$inout0);
	&ret	();
&function_end_B("${PREFIX}_encrypt");

# void $PREFIX_decrypt (const void *inp,void *out,const AES_KEY *key);
162
&aesni_generate1("dec") if(!$inline);
A
Andy Polyakov 已提交
163 164 165
&function_begin_B("${PREFIX}_decrypt");
	&mov	("eax",&wparam(0));
	&mov	($key,&wparam(2));
166
	&movdqu	($inout0,&QWP(0,"eax"));
A
Andy Polyakov 已提交
167 168
	&mov	($rounds,&DWP(240,$key));
	&mov	("eax",&wparam(1));
169 170 171 172
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
A
Andy Polyakov 已提交
173 174 175
	&movups	(&QWP(0,"eax"),$inout0);
	&ret	();
&function_end_B("${PREFIX}_decrypt");
A
Andy Polyakov 已提交
176

A
Andy Polyakov 已提交
177 178 179 180 181 182 183 184 185 186
# _aesni_[en|de]crypt[34] are private interfaces, N denotes interleave
# factor. Why 3x subroutine is used in loops? Even though aes[enc|dec]
# latency is 6, it turned out that it can be scheduled only every
# *second* cycle. Thus 3x interleave is the one providing optimal
# utilization, i.e. when subroutine's throughput is virtually same as
# of non-interleaved subroutine [for number of input blocks up to 3].
# This is why it makes no sense to implement 2x subroutine. As soon
# as/if Intel improves throughput by making it possible to schedule
# the instructions in question *every* cycles I would have to
# implement 6x interleave and use it in loop...
A
Andy Polyakov 已提交
187 188 189 190 191 192
sub aesni_generate3
{ my $p=shift;

    &function_begin_B("_aesni_${p}rypt3");
	&$movekey	($rndkey0,&QWP(0,$key));
	&shr		($rounds,1);
A
Andy Polyakov 已提交
193
	&$movekey	($rndkey1,&QWP(16,$key));
A
Andy Polyakov 已提交
194 195 196 197 198
	&lea		($key,&DWP(32,$key));
	&pxor		($inout0,$rndkey0);
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
	&$movekey	($rndkey0,&QWP(0,$key));
199 200 201

    &set_label("${p}3_loop");
	eval"&aes${p}	($inout0,$rndkey1)";
A
Andy Polyakov 已提交
202 203 204 205 206 207
	eval"&aes${p}	($inout1,$rndkey1)";
	&dec		($rounds);
	eval"&aes${p}	($inout2,$rndkey1)";
	&$movekey	($rndkey1,&QWP(16,$key));
	eval"&aes${p}	($inout0,$rndkey0)";
	eval"&aes${p}	($inout1,$rndkey0)";
208
	&lea		($key,&DWP(32,$key));
A
Andy Polyakov 已提交
209
	eval"&aes${p}	($inout2,$rndkey0)";
210
	&$movekey	($rndkey0,&QWP(0,$key));
A
Andy Polyakov 已提交
211 212 213 214 215 216 217 218 219 220
	&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 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

# 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));
	&shr		($rounds,1);
	&lea		($key,&DWP(32,$key));
	&pxor		($inout0,$rndkey0);
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
	&pxor		($inout3,$rndkey0);
	&$movekey	($rndkey0,&QWP(0,$key));
239 240 241

    &set_label("${p}3_loop");
	eval"&aes${p}	($inout0,$rndkey1)";
A
Andy Polyakov 已提交
242 243 244 245 246 247 248
	eval"&aes${p}	($inout1,$rndkey1)";
	&dec		($rounds);
	eval"&aes${p}	($inout2,$rndkey1)";
	eval"&aes${p}	($inout3,$rndkey1)";
	&$movekey	($rndkey1,&QWP(16,$key));
	eval"&aes${p}	($inout0,$rndkey0)";
	eval"&aes${p}	($inout1,$rndkey0)";
249
	&lea		($key,&DWP(32,$key));
A
Andy Polyakov 已提交
250 251
	eval"&aes${p}	($inout2,$rndkey0)";
	eval"&aes${p}	($inout3,$rndkey0)";
252 253 254
	&$movekey	($rndkey0,&QWP(0,$key));
    &jnz		(&label("${p}3_loop"));

A
Andy Polyakov 已提交
255 256 257 258 259 260 261 262 263 264 265
    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");
}
A
Andy Polyakov 已提交
266 267
&aesni_generate3("enc") if ($PREFIX eq "aesni");
&aesni_generate3("dec");
A
Andy Polyakov 已提交
268 269
&aesni_generate4("enc") if ($PREFIX eq "aesni");
&aesni_generate4("dec");
A
Andy Polyakov 已提交
270

A
Andy Polyakov 已提交
271
if ($PREFIX eq "aesni") {
A
Andy Polyakov 已提交
272
######################################################################
A
Andy Polyakov 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
# 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));
	&mov	($rounds,&wparam(4));
	&cmp	($len,16);
	&jb	(&label("ecb_ret"));
	&and	($len,-16);
	&test	($rounds,$rounds)
	&mov	($rounds,&DWP(240,$key));
	&mov	($key_,$key);		# backup $key
	&mov	($rounds_,$rounds);	# backup $rounds
	&jz	(&label("ecb_decrypt"));

A
Andy Polyakov 已提交
291
	&cmp	($len,0x40);
A
Andy Polyakov 已提交
292
	&jbe	(&label("ecb_enc_tail"));
A
Andy Polyakov 已提交
293
	&sub	($len,0x40);
A
Andy Polyakov 已提交
294
	&jmp	(&label("ecb_enc_loop3"));
A
Andy Polyakov 已提交
295 296 297 298 299 300

&set_label("ecb_enc_loop3",16);
	&movups	($inout0,&QWP(0,$inp));
	&movups	($inout1,&QWP(0x10,$inp));
	&movups	($inout2,&QWP(0x20,$inp));
	&call	("_aesni_encrypt3");
A
Andy Polyakov 已提交
301
	&lea	($inp,&DWP(0x30,$inp));
302
	&movups	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
303
	&mov	($key,$key_);		# restore $key
304
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
305
	&mov	($rounds,$rounds_);	# restore $rounds
306 307 308
	&movups	(&QWP(0x20,$out),$inout2);
	&lea	($out,&DWP(0x30,$out));
	&sub	($len,0x30);
A
Andy Polyakov 已提交
309
	&ja	(&label("ecb_enc_loop3"));
A
Andy Polyakov 已提交
310

A
Andy Polyakov 已提交
311
	&add	($len,0x40);
A
Andy Polyakov 已提交
312 313
&set_label("ecb_enc_tail");
	&movups	($inout0,&QWP(0,$inp));
314
	&cmp	($len,0x20);
A
Andy Polyakov 已提交
315
	&jb	(&label("ecb_enc_one"));
A
Andy Polyakov 已提交
316
	&movups	($inout1,&QWP(0x10,$inp));
A
Andy Polyakov 已提交
317 318
	&je	(&label("ecb_enc_two"));
	&movups	($inout2,&QWP(0x20,$inp));
319
	&cmp	($len,0x30);
A
Andy Polyakov 已提交
320 321 322
	&je	(&label("ecb_enc_three"));
	&movups	($inout3,&QWP(0x30,$inp));
	&call	("_aesni_encrypt4");
A
Andy Polyakov 已提交
323 324
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
325 326
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
A
Andy Polyakov 已提交
327 328 329
	jmp	(&label("ecb_ret"));

&set_label("ecb_enc_one",16);
330 331 332 333
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
A
Andy Polyakov 已提交
334 335 336
	&movups	(&QWP(0,$out),$inout0);
	&jmp	(&label("ecb_ret"));

A
Andy Polyakov 已提交
337
&set_label("ecb_enc_two",16);
338
	&pxor	($inout2,$inout2);
A
Andy Polyakov 已提交
339 340 341 342 343 344 345 346 347 348 349
	&call	("_aesni_encrypt3");
	&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"));
A
Andy Polyakov 已提交
350
######################################################################
A
Andy Polyakov 已提交
351
&set_label("ecb_decrypt",16);
A
Andy Polyakov 已提交
352
	&cmp	($len,0x40);
A
Andy Polyakov 已提交
353
	&jbe	(&label("ecb_dec_tail"));
A
Andy Polyakov 已提交
354
	&sub	($len,0x40);
A
Andy Polyakov 已提交
355
	&jmp	(&label("ecb_dec_loop3"));
A
Andy Polyakov 已提交
356 357 358 359 360 361 362

&set_label("ecb_dec_loop3",16);
	&movups	($inout0,&QWP(0,$inp));
	&movups	($inout1,&QWP(0x10,$inp));
	&movups	($inout2,&QWP(0x20,$inp));
	&call	("_aesni_decrypt3");
	&lea	($inp,&DWP(0x30,$inp));
363
	&movups	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
364
	&mov	($key,$key_);		# restore $key
365
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
366
	&mov	($rounds,$rounds_);	# restore $rounds
367 368 369
	&movups	(&QWP(0x20,$out),$inout2);
	&lea	($out,&DWP(0x30,$out));
	&sub	($len,0x30);
A
Andy Polyakov 已提交
370
	&ja	(&label("ecb_dec_loop3"));
A
Andy Polyakov 已提交
371

A
Andy Polyakov 已提交
372
	&add	($len,0x40);
A
Andy Polyakov 已提交
373 374
&set_label("ecb_dec_tail");
	&movups	($inout0,&QWP(0,$inp));
375
	&cmp	($len,0x20);
A
Andy Polyakov 已提交
376
	&jb	(&label("ecb_dec_one"));
A
Andy Polyakov 已提交
377
	&movups	($inout1,&QWP(0x10,$inp));
A
Andy Polyakov 已提交
378 379
	&je	(&label("ecb_dec_two"));
	&movups	($inout2,&QWP(0x20,$inp));
380
	&cmp	($len,0x30);
A
Andy Polyakov 已提交
381 382 383
	&je	(&label("ecb_dec_three"));
	&movups	($inout3,&QWP(0x30,$inp));
	&call	("_aesni_decrypt4");
A
Andy Polyakov 已提交
384 385
	&movups	(&QWP(0,$out),$inout0);
	&movups	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
386 387 388
	&movups	(&QWP(0x20,$out),$inout2);
	&movups	(&QWP(0x30,$out),$inout3);
	&jmp	(&label("ecb_ret"));
A
Andy Polyakov 已提交
389 390

&set_label("ecb_dec_one",16);
391 392 393 394
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
A
Andy Polyakov 已提交
395
	&movups	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
396 397 398
	&jmp	(&label("ecb_ret"));

&set_label("ecb_dec_two",16);
399
	&pxor	($inout2,$inout2);
A
Andy Polyakov 已提交
400 401 402 403 404 405 406 407 408 409
	&call	("_aesni_decrypt3");
	&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);
A
Andy Polyakov 已提交
410 411 412

&set_label("ecb_ret");
&function_end("aesni_ecb_encrypt");
A
Andy Polyakov 已提交
413 414

######################################################################
415 416 417 418 419 420 421
# 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 已提交
422
#
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
&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
	&movdqu	($inout1,&QWP(0,$rounds));	# load cmac

	# 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
	&mov	($rounds,1);
	&xor	($key_,$key_);
	&mov	(&DWP(16,"esp"),$rounds);
	&mov	(&DWP(20,"esp"),$key_);
	&mov	(&DWP(24,"esp"),$key_);
	&mov	(&DWP(28,"esp"),$key_);

	&movdqa	($inout3,&QWP(0,"esp"));
	&pshufb	($ivec,$inout3);		# keep iv in reverse order

	&mov	($rounds,&DWP(240,$key));
	&mov	($key_,$key);
	&mov	($rounds_,$rounds);
	&movdqa	($inout0,$ivec);

&set_label("ccm64_enc_outer");
	&movdqu	($in0,&QWP(0,$inp));
	&pshufb	($inout0,$inout3);
	&mov	($key,$key_);
	&mov	($rounds,$rounds_);
	&pxor	($inout1,$in0);			# cmac^=inp
	&pxor	($inout2,$inout2);

	&call	("_aesni_encrypt3");

	&paddq	($ivec,&QWP(16,"esp"));
	&dec	($len);
	&lea	($inp,&DWP(16,$inp));
	&pxor	($in0,$inout0);			# inp^=E(ivec)
	&movdqa	($inout0,$ivec);
	&movdqu	(&QWP(0,$out),$in0);
	&lea	($out,&DWP(16,$out));
	&jnz	(&label("ccm64_enc_outer"));

	&mov	("esp",&DWP(48,"esp"));
	&mov	($out,&wparam(5));
	&movdqu	(&QWP(0,$out),$inout1);
&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
	&movdqu	($inout1,&QWP(0,$rounds));	# load cmac

	# 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
	&mov	($rounds,1);
	&xor	($key_,$key_);
	&mov	(&DWP(16,"esp"),$rounds);
	&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);
	&pshufb	($ivec,$inout3);		# keep iv in reverse order

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

	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}

&set_label("ccm64_dec_outer");
	&movdqu	($in0,&QWP(0,$inp));
	&paddq	($ivec,&QWP(16,"esp"));
	&dec	($len);
	&lea	($inp,&QWP(16,$inp));
	&pxor	($in0,$inout0);
	&movdqa	($inout0,$ivec);
	&mov	($key,$key_);
	&mov	($rounds,$rounds_);
	&pshufb	($inout0,$inout3);
	&movdqu	(&QWP(0,$out),$in0);
	&lea	($out,&DWP(16,$out));

	&jz	(&label("ccm64_dec_break"));

	&pxor	($inout2,$inout2);
	&call	("_aesni_encrypt3");

	&jmp	(&label("ccm64_dec_outer"));

&set_label("ccm64_dec_break",16);
	if ($inline)
	{   &aesni_inline_generate1("enc",$inout1);	}
	else
	{   &call	("_aesni_encrypt1",$inout1);	}

	&mov	("esp",&DWP(48,"esp"));
	&mov	($out,&wparam(5));
	&movdqu	(&QWP(0,$out),$inout1);
&function_end("aesni_ccm64_decrypt_blocks");

######################################################################
A
Andy Polyakov 已提交
558 559 560
# void aesni_ctr32_encrypt_blocks (const void *in, void *out,
#                         size_t blocks, const AES_KEY *key,
#                         const char *ivec);
561 562 563 564
#
# Handles only complete blocks, operates on 32-bit counter and
# does not update *ivec! (see engine/eng_aesni.c for details)
#
A
Andy Polyakov 已提交
565 566 567 568 569 570 571 572 573 574 575
&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");
	&sub	("esp",60);
	&and	("esp",-16);			# align stack
	&mov	(&DWP(48,"esp"),$key_);

576 577 578
	&cmp	($len,1);
	&je	(&label("ctr32_one_shortcut"));

A
Andy Polyakov 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	&movups	($inout3,&QWP(0,$rounds_));	# load ivec

	# 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
	&mov	($rounds,3);
	&xor	($key_,$key_);
	&mov	(&DWP(16,"esp"),$rounds);
	&mov	(&DWP(20,"esp"),$rounds);
	&mov	(&DWP(24,"esp"),$rounds);
	&mov	(&DWP(28,"esp"),$key_);

	&pextrd	($rounds_,$inout3,3);		# pull 32-bit counter
	&pinsrd	($inout3,$key_,3);		# wipe 32-bit counter

	&mov	($rounds,&DWP(240,$key));	# key->rounds
599
	&movdqa	($rndkey0,&QWP(0,"esp"));	# load byte-swap mask
A
Andy Polyakov 已提交
600 601 602 603 604 605 606 607 608

	# $ivec is vector of 3 32-bit counters
	&pxor	($ivec,$ivec);
	&bswap	($rounds_);
	&pinsrd	($ivec,$rounds_,0);
	&inc	($rounds_);
	&pinsrd	($ivec,$rounds_,1);
	&inc	($rounds_);
	&pinsrd	($ivec,$rounds_,2);
609
	&pshufb	($ivec,$rndkey0);		# byte swap
A
Andy Polyakov 已提交
610 611 612

	&cmp	($len,4);
	&jbe	(&label("ctr32_tail"));
613
	&movdqa	(&QWP(32,"esp"),$inout3);	# save counter-less ivec
A
Andy Polyakov 已提交
614 615 616 617 618 619 620 621 622
	&mov	($rounds_,$rounds);
	&mov	($key_,$key);
	&sub	($len,4);
	&jmp	(&label("ctr32_loop3"));

&set_label("ctr32_loop3",16);
	&pshufd	($inout0,$ivec,3<<6);		# place counter to upper dword
	&pshufd	($inout1,$ivec,2<<6);
	&por	($inout0,$inout3);		# merge counter-less ivec
623
	&pshufd	($inout2,$ivec,1<<6);
A
Andy Polyakov 已提交
624 625 626
	&por	($inout1,$inout3);
	&por	($inout2,$inout3);

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
	# inline _aesni_encrypt3 and interleave last round
	# with own code...

	&$movekey	($rndkey0,&QWP(0,$key));
	&shr		($rounds,1);
	&$movekey	($rndkey1,&QWP(16,$key));
	&lea		($key,&DWP(32,$key));
	&pxor		($inout0,$rndkey0);
	&pxor		($inout1,$rndkey0);
	&pxor		($inout2,$rndkey0);
	&$movekey	($rndkey0,&QWP(0,$key));

&set_label("ctr32_enc_loop3");
	&aesenc		($inout0,$rndkey1);
	&aesenc		($inout1,$rndkey1);
	&dec		($rounds);
	&aesenc		($inout2,$rndkey1);
	&$movekey	($rndkey1,&QWP(16,$key));
	&aesenc		($inout0,$rndkey0);
	&aesenc		($inout1,$rndkey0);
	&lea		($key,&DWP(32,$key));
	&aesenc		($inout2,$rndkey0);
	&$movekey	($rndkey0,&QWP(0,$key));
	&jnz		(&label("ctr32_enc_loop3"));

	&aesenc		($inout0,$rndkey1);
	&aesenc		($inout1,$rndkey1);
	&aesenc		($inout2,$rndkey1);
	&movdqa		($rndkey1,&QWP(0,"esp"));	# load byte-swap mask

	&aesenclast	($inout0,$rndkey0);
	&pshufb		($ivec,$rndkey1);		# byte swap
	&movdqu		($in0,&QWP(0,$inp));
	&aesenclast	($inout1,$rndkey0);
	&paddd		($ivec,&QWP(16,"esp"));		# counter increment
	&movdqu		($in1,&QWP(0x10,$inp));
	&aesenclast	($inout2,$rndkey0);
	&pshufb		($ivec,$rndkey1);		# byte swap
	&movdqu		($rndkey0,&QWP(0x20,$inp));
	&lea		($inp,&DWP(0x30,$inp));
A
Andy Polyakov 已提交
667 668

	&pxor	($in0,$inout0);
669
	&mov	($key,$key_);
A
Andy Polyakov 已提交
670
	&pxor	($in1,$inout1);
671 672 673 674 675
	&movdqu	(&QWP(0,$out),$in0);
	&pxor	($rndkey0,$inout2);
	&movdqu	(&QWP(0x10,$out),$in1);
	&movdqu	(&QWP(0x20,$out),$rndkey0);
	&movdqa	($inout3,&QWP(32,"esp"));	# load counter-less ivec
A
Andy Polyakov 已提交
676 677 678 679 680 681 682

	&sub	($len,3);
	&lea	($out,&DWP(0x30,$out));
	&mov	($rounds,$rounds_);
	&ja	(&label("ctr32_loop3"));

	&pextrd	($rounds_,$ivec,1);		# might need last counter value
683
	&add	($len,4);
A
Andy Polyakov 已提交
684 685 686 687 688 689
	&bswap	($rounds_);

&set_label("ctr32_tail");
	&pshufd	($inout0,$ivec,3<<6);
	&pshufd	($inout1,$ivec,2<<6);
	&por	($inout0,$inout3);
690
	&cmp	($len,2);
A
Andy Polyakov 已提交
691
	&jb	(&label("ctr32_one"));
692 693
	&lea	($rounds_,&DWP(1,$rounds_));
	&pshufd	($inout2,$ivec,1<<6);
A
Andy Polyakov 已提交
694 695
	&por	($inout1,$inout3);
	&je	(&label("ctr32_two"));
696
	&bswap	($rounds_);
A
Andy Polyakov 已提交
697
	&por	($inout2,$inout3);
698
	&cmp	($len,3);
A
Andy Polyakov 已提交
699 700
	&je	(&label("ctr32_three"));

701
	&pinsrd	($inout3,$rounds_,3);		# compose last counter value
A
Andy Polyakov 已提交
702 703 704

	&call	("_aesni_encrypt4");

705 706
	&movdqu	($in0,&QWP(0,$inp));
	&movdqu	($rndkey1,&QWP(0x10,$inp));
A
Andy Polyakov 已提交
707
	&pxor	($in0,$inout0);
708
	&movdqu	($rndkey0,&QWP(0x20,$inp));
A
Andy Polyakov 已提交
709
	&pxor	($rndkey1,$inout1);
710
	&movdqu	($ivec,&QWP(0x30,$inp));
A
Andy Polyakov 已提交
711
	&pxor	($rndkey0,$inout2);
712
	&movdqu	(&QWP(0,$out),$in0);
A
Andy Polyakov 已提交
713
	&pxor	($ivec,$inout3);
714 715 716
	&movdqu	(&QWP(0x10,$out),$rndkey1);
	&movdqu	(&QWP(0x20,$out),$rndkey0);
	&movdqu	(&QWP(0x30,$out),$ivec);
A
Andy Polyakov 已提交
717 718
	&jmp	(&label("ctr32_ret"));

719 720 721 722 723
&set_label("ctr32_one_shortcut",16);
	&movdqu	($inout0,&QWP(0,$rounds_));	# load ivec
	&mov	($rounds,&DWP(240,$key));
	
&set_label("ctr32_one");
A
Andy Polyakov 已提交
724 725 726 727
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
728
	&movdqu	($in0,&QWP(0,$inp));
A
Andy Polyakov 已提交
729
	&pxor	($in0,$inout0);
730
	&movdqu	(&QWP(0,$out),$in0);
A
Andy Polyakov 已提交
731
	&jmp	(&label("ctr32_ret"));
A
Andy Polyakov 已提交
732

A
Andy Polyakov 已提交
733
&set_label("ctr32_two",16);
734
	&pxor	($inout2,$inout2);
A
Andy Polyakov 已提交
735
	&call	("_aesni_encrypt3");
736 737
	&movdqu	($in0,&QWP(0,$inp));
	&movdqu	($in1,&QWP(0x10,$inp));
A
Andy Polyakov 已提交
738 739
	&pxor	($in0,$inout0);
	&pxor	($in1,$inout1);
740 741
	&movdqu	(&QWP(0,$out),$in0);
	&movdqu	(&QWP(0x10,$out),$in1);
A
Andy Polyakov 已提交
742 743 744 745
	&jmp	(&label("ctr32_ret"));

&set_label("ctr32_three",16);
	&call	("_aesni_encrypt3");
746 747 748
	&movdqu	($in0,&QWP(0,$inp));
	&movdqu	($in1,&QWP(0x10,$inp));
	&movdqu	($rndkey1,&QWP(0x20,$inp));
A
Andy Polyakov 已提交
749 750
	&pxor	($in0,$inout0);
	&pxor	($in1,$inout1);
751
	&movdqu	(&QWP(0,$out),$in0);
A
Andy Polyakov 已提交
752
	&pxor	($rndkey1,$inout2);
753 754
	&movdqu	(&QWP(0x10,$out),$in1);
	&movdqu	(&QWP(0x20,$out),$rndkey1);
A
Andy Polyakov 已提交
755 756 757 758 759 760 761

&set_label("ctr32_ret");
	&mov	("esp",&DWP(48,"esp"));
&function_end("aesni_ctr32_encrypt_blocks");
}

######################################################################
A
Andy Polyakov 已提交
762 763 764 765 766 767 768 769 770
# 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));
	&mov	($out,&wparam(1));
	&mov	($len,&wparam(2));
	&mov	($key,&wparam(3));
	&mov	($key_,&wparam(4));
771
	&test	($len,$len);
A
Andy Polyakov 已提交
772
	&jz	(&label("cbc_ret"));
A
Andy Polyakov 已提交
773 774

	&cmp	(&wparam(5),0);
775
	&movdqu	($ivec,&QWP(0,$key_));	# load IV
A
Andy Polyakov 已提交
776 777 778 779 780
	&mov	($rounds,&DWP(240,$key));
	&mov	($key_,$key);		# backup $key
	&mov	($rounds_,$rounds);	# backup $rounds
	&je	(&label("cbc_decrypt"));

781
	&movdqa	($inout0,$ivec);
A
Andy Polyakov 已提交
782 783 784 785 786 787
	&cmp	($len,16);
	&jb	(&label("cbc_enc_tail"));
	&sub	($len,16);
	&jmp	(&label("cbc_enc_loop"));

&set_label("cbc_enc_loop",16);
788
	&movdqu	($ivec,&QWP(0,$inp));
A
Andy Polyakov 已提交
789 790
	&lea	($inp,&DWP(16,$inp));
	&pxor	($inout0,$ivec);
791 792 793 794
	if ($inline)
	{   &aesni_inline_generate1("enc");	}
	else
	{   &call	("_aesni_encrypt1");	}
A
Andy Polyakov 已提交
795 796
	&mov	($rounds,$rounds_);	# restore $rounds
	&mov	($key,$key_);		# restore $key
797 798 799
	&movups	(&QWP(0,$out),$inout0);	# store output
	&lea	($out,&DWP(16,$out));
	&sub	($len,16);
A
Andy Polyakov 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	&jnc	(&label("cbc_enc_loop"));
	&add	($len,16);
	&jnz	(&label("cbc_enc_tail"));
	&movaps	($ivec,$inout0);
	&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 已提交
818
######################################################################
A
Andy Polyakov 已提交
819
&set_label("cbc_decrypt",16);
A
Andy Polyakov 已提交
820
	&cmp	($len,0x40);
A
Andy Polyakov 已提交
821
	&jbe	(&label("cbc_dec_tail"));
A
Andy Polyakov 已提交
822
	&sub	($len,0x40);
A
Andy Polyakov 已提交
823 824 825 826 827 828 829 830
	&jmp	(&label("cbc_dec_loop3"));

&set_label("cbc_dec_loop3",16);
	&movups	($inout0,&QWP(0,$inp));
	&movups	($inout1,&QWP(0x10,$inp));
	&movups	($inout2,&QWP(0x20,$inp));
	&movaps	($in0,$inout0);
	&movaps	($in1,$inout1);
831

A
Andy Polyakov 已提交
832
	&call	("_aesni_decrypt3");
833

A
Andy Polyakov 已提交
834 835
	&pxor	($inout0,$ivec);
	&pxor	($inout1,$in0);
836 837
	&movdqu	($ivec,&QWP(0x20,$inp));
	&lea	($inp,&DWP(0x30,$inp));
A
Andy Polyakov 已提交
838
	&pxor	($inout2,$in1);
839
	&movdqu	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
840
	&mov	($rounds,$rounds_)	# restore $rounds
841
	&movdqu	(&QWP(0x10,$out),$inout1);
A
Andy Polyakov 已提交
842
	&mov	($key,$key_);		# restore $key
843 844 845
	&movdqu	(&QWP(0x20,$out),$inout2);
	&lea	($out,&DWP(0x30,$out));
	&sub	($len,0x30);
A
Andy Polyakov 已提交
846
	&ja	(&label("cbc_dec_loop3"));
A
Andy Polyakov 已提交
847

A
Andy Polyakov 已提交
848
	&add	($len,0x40);
A
Andy Polyakov 已提交
849
&set_label("cbc_dec_tail");
A
Andy Polyakov 已提交
850 851
	&movups	($inout0,&QWP(0,$inp));
	&movaps	($in0,$inout0);
852
	&cmp	($len,0x10);
A
Andy Polyakov 已提交
853 854 855
	&jbe	(&label("cbc_dec_one"));
	&movups	($inout1,&QWP(0x10,$inp));
	&movaps	($in1,$inout1);
856
	&cmp	($len,0x20);
A
Andy Polyakov 已提交
857 858
	&jbe	(&label("cbc_dec_two"));
	&movups	($inout2,&QWP(0x20,$inp));
A
Andy Polyakov 已提交
859 860 861 862
	&cmp	($len,0x30);
	&jbe	(&label("cbc_dec_three"));
	&movups	($inout3,&QWP(0x30,$inp));
	&call	("_aesni_decrypt4");
863 864
	&movdqu	($rndkey0,&QWP(0x10,$inp));
	&movdqu	($rndkey1,&QWP(0x20,$inp));
A
Andy Polyakov 已提交
865 866
	&pxor	($inout0,$ivec);
	&pxor	($inout1,$in0);
867 868
	&movdqu	($ivec,&QWP(0x30,$inp));
	&movdqu	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
869 870
	&pxor	($inout2,$rndkey0);
	&pxor	($inout3,$rndkey1);
871 872 873
	&movdqu	(&QWP(0x10,$out),$inout1);
	&movdqu	(&QWP(0x20,$out),$inout2);
	&movdqa	($inout0,$inout3);
A
Andy Polyakov 已提交
874
	&lea	($out,&DWP(0x30,$out));
A
Andy Polyakov 已提交
875 876
	&jmp	(&label("cbc_dec_tail_collected"));

877
&set_label("cbc_dec_one",16);
878 879 880 881
	if ($inline)
	{   &aesni_inline_generate1("dec");	}
	else
	{   &call	("_aesni_decrypt1");	}
A
Andy Polyakov 已提交
882
	&pxor	($inout0,$ivec);
883
	&movdqa	($ivec,$in0);
A
Andy Polyakov 已提交
884 885
	&jmp	(&label("cbc_dec_tail_collected"));

886 887
&set_label("cbc_dec_two",16);
	&pxor	($inout2,$inout2);
A
Andy Polyakov 已提交
888 889 890
	&call	("_aesni_decrypt3");
	&pxor	($inout0,$ivec);
	&pxor	($inout1,$in0);
891 892 893
	&movdqu	(&QWP(0,$out),$inout0);
	&movdqa	($inout0,$inout1);
	&movdqa	($ivec,$in1);
A
Andy Polyakov 已提交
894
	&lea	($out,&DWP(0x10,$out));
A
Andy Polyakov 已提交
895 896
	&jmp	(&label("cbc_dec_tail_collected"));

897
&set_label("cbc_dec_three",16);
A
Andy Polyakov 已提交
898 899 900 901
	&call	("_aesni_decrypt3");
	&pxor	($inout0,$ivec);
	&pxor	($inout1,$in0);
	&pxor	($inout2,$in1);
902 903 904 905
	&movdqu	(&QWP(0,$out),$inout0);
	&movdqu	(&QWP(0x10,$out),$inout1);
	&movdqa	($inout0,$inout2);
	&movdqu	($ivec,&QWP(0x20,$inp));
A
Andy Polyakov 已提交
906
	&lea	($out,&DWP(0x20,$out));
A
Andy Polyakov 已提交
907 908 909 910

&set_label("cbc_dec_tail_collected");
	&and	($len,15);
	&jnz	(&label("cbc_dec_tail_partial"));
911
	&movdqu	(&QWP(0,$out),$inout0);
A
Andy Polyakov 已提交
912 913
	&jmp	(&label("cbc_ret"));

914
&set_label("cbc_dec_tail_partial",16);
A
Andy Polyakov 已提交
915 916 917
	&mov	($key_,"esp");
	&sub	("esp",16);
	&and	("esp",-16);
918
	&movdqa	(&QWP(0,"esp"),$inout0);
A
Andy Polyakov 已提交
919 920 921 922 923 924 925 926 927
	&mov	($inp,"esp");
	&mov	("ecx",$len);
	&data_word(0xA4F3F689);		# rep movsb
	&mov	("esp",$key_);

&set_label("cbc_ret");
	&mov	($key_,&wparam(4));
	&movups	(&QWP(0,$key_),$ivec);	# output IV
&function_end("${PREFIX}_cbc_encrypt");
A
Andy Polyakov 已提交
928 929

######################################################################
A
Andy Polyakov 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
# 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");
	&test	("eax","eax");
	&jz	(&label("bad_pointer"));
	&test	($key,$key);
	&jz	(&label("bad_pointer"));

	&movups	("xmm0",&QWP(0,"eax"));	# pull first 128 bits of *userKey
	&pxor	("xmm4","xmm4");	# low dword of xmm4 is assumed 0
	&lea	($key,&DWP(16,$key));
	&cmp	($rounds,256);
	&je	(&label("14rounds"));
	&cmp	($rounds,192);
	&je	(&label("12rounds"));
	&cmp	($rounds,128);
	&jne	(&label("bad_keybits"));

&set_label("10rounds",16);
A
Andy Polyakov 已提交
958
	&mov		($rounds,9);
A
Andy Polyakov 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	&$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);
	&xor		("eax","eax");
	&ret();

&set_label("key_128",16);
	&$movekey	(&QWP(0,$key),"xmm0");
	&lea		($key,&DWP(16,$key));
&set_label("key_128_cold");
	&shufps		("xmm4","xmm0",0b00010000);
	&pxor		("xmm0","xmm4");
	&shufps		("xmm4","xmm0",0b10001100,);
	&pxor		("xmm0","xmm4");
	&pshufd		("xmm1","xmm1",0b11111111);	# critical path
	&pxor		("xmm0","xmm1");
	&ret();

&set_label("12rounds",16);
	&movq		("xmm2",&QWP(16,"eax"));	# remaining 1/3 of *userKey
A
Andy Polyakov 已提交
999
	&mov		($rounds,11);
A
Andy Polyakov 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	&$movekey	(&QWP(-16,$key),"xmm0")		# round 0
	&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);
	&xor		("eax","eax");
	&ret();

&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);
	&movaps		("xmm3","xmm2");
	&pxor		("xmm0","xmm4");
	&shufps		("xmm4","xmm0",0b10001100);
	&pslldq		("xmm3",4);
	&pxor		("xmm0","xmm4");
	&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"));

&set_label("14rounds",16);
	&movups		("xmm2",&QWP(16,"eax"));	# remaining half of *userKey
A
Andy Polyakov 已提交
1052
	&mov		($rounds,13);
A
Andy Polyakov 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 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 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	&lea		($key,&DWP(16,$key));
	&$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");
	&ret();

&set_label("key_256a",16);
	&$movekey	(&QWP(0,$key),"xmm2");
	&lea		($key,&DWP(16,$key));
&set_label("key_256a_cold");
	&shufps		("xmm4","xmm0",0b00010000);
	&pxor		("xmm0","xmm4");
	&shufps		("xmm4","xmm0",0b10001100);
	&pxor		("xmm0","xmm4");
	&pshufd		("xmm1","xmm1",0b11111111);	# critical path
	&pxor		("xmm0","xmm1");
	&ret();

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

	&shufps		("xmm4","xmm2",0b00010000);
	&pxor		("xmm2","xmm4");
	&shufps		("xmm4","xmm2",0b10001100);
	&pxor		("xmm2","xmm4");
	&pshufd		("xmm1","xmm1",0b10101010);	# critical path
	&pxor		("xmm2","xmm1");
	&ret();

&set_label("bad_pointer",4);
	&mov	("eax",-1);
	&ret	();
&set_label("bad_keybits",4);
	&mov	("eax",-2);
	&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));
A
Andy Polyakov 已提交
1137
	&shl	($rounds,4)	# rounds-1 after _aesni_set_encrypt_key
A
Andy Polyakov 已提交
1138 1139
	&test	("eax","eax");
	&jnz	(&label("dec_key_ret"));
A
Andy Polyakov 已提交
1140
	&lea	("eax",&DWP(16,$key,$rounds));	# end of key schedule
A
Andy Polyakov 已提交
1141 1142 1143 1144 1145 1146 1147 1148

	&$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 已提交
1149
&set_label("dec_key_inverse");
A
Andy Polyakov 已提交
1150 1151 1152 1153 1154 1155 1156 1157
	&$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");
1158
	&cmp		("eax",$key);
A
Andy Polyakov 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	&ja		(&label("dec_key_inverse"));

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

	&xor		("eax","eax");		# return success
&set_label("dec_key_ret");
	&ret	();
&function_end_B("${PREFIX}_set_decrypt_key");
&asciz("AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>");

&asm_finish();