| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# ==================================================================== |
| 4 |
# Written by Andy Polyakov <[email protected]> for the OpenSSL |
| 5 |
# project. The module is, however, dual licensed under OpenSSL and |
| 6 |
# CRYPTOGAMS licenses depending on where you obtain it. For further |
| 7 |
# details see http://www.openssl.org/~appro/cryptogams/. |
| 8 |
# ==================================================================== |
| 9 |
# |
| 10 |
# |
| 11 |
# AES-NI-CTR+GHASH stitch. |
| 12 |
# |
| 13 |
# February 2013 |
| 14 |
# |
| 15 |
# OpenSSL GCM implementation is organized in such way that its |
| 16 |
# performance is rather close to the sum of its streamed components, |
| 17 |
# in the context parallelized AES-NI CTR and modulo-scheduled |
| 18 |
# PCLMULQDQ-enabled GHASH. Unfortunately, as no stitch implementation |
| 19 |
# was observed to perform significantly better than the sum of the |
| 20 |
# components on contemporary CPUs, the effort was deemed impossible to |
| 21 |
# justify. This module is based on combination of Intel submissions, |
| 22 |
# [1] and [2], with MOVBE twist suggested by Ilya Albrekht and Max |
| 23 |
# Locktyukhin of Intel Corp. who verified that it reduces shuffles |
| 24 |
# pressure with notable relative improvement, achieving 1.0 cycle per |
| 25 |
# byte processed with 128-bit key on Haswell processor, and 0.74 - |
| 26 |
# on Broadwell. [Mentioned results are raw profiled measurements for |
| 27 |
# favourable packet size, one divisible by 96. Applications using the |
| 28 |
# EVP interface will observe a few percent worse performance.] |
| 29 |
# |
| 30 |
# [1] http://rt.openssl.org/Ticket/Display.html?id=2900&user=guest&pass=guest |
| 31 |
# [2] http://www.intel.com/content/dam/www/public/us/en/documents/software-support/enabling-high-performance-gcm.pdf |
| 32 |
|
| 33 |
$flavour = shift; |
| 34 |
$output = shift; |
| 35 |
if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } |
| 36 |
|
| 37 |
$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); |
| 38 |
|
| 39 |
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; |
| 40 |
( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or |
| 41 |
( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or |
| 42 |
die "can't locate x86_64-xlate.pl"; |
| 43 |
|
| 44 |
# This must be kept in sync with |$avx| in ghash-x86_64.pl; otherwise tags will |
| 45 |
# be computed incorrectly. |
| 46 |
# |
| 47 |
# In upstream, this is controlled by shelling out to the compiler to check |
| 48 |
# versions, but BoringSSL is intended to be used with pre-generated perlasm |
| 49 |
# output, so this isn't useful anyway. |
| 50 |
$avx = 0; |
| 51 |
|
| 52 |
open OUT,"| \"$^X\" $xlate $flavour $output"; |
| 53 |
*STDOUT=*OUT; |
| 54 |
|
| 55 |
if ($avx>1) {{{ |
| 56 |
|
| 57 |
($inp,$out,$len,$key,$ivp,$Xip)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9"); |
| 58 |
|
| 59 |
($Ii,$T1,$T2,$Hkey, |
| 60 |
$Z0,$Z1,$Z2,$Z3,$Xi) = map("%xmm$_",(0..8)); |
| 61 |
|
| 62 |
($inout0,$inout1,$inout2,$inout3,$inout4,$inout5,$rndkey) = map("%xmm$_",(9..15)); |
| 63 |
|
| 64 |
($counter,$rounds,$ret,$const,$in0,$end0)=("%ebx","%ebp","%r10","%r11","%r14","%r15"); |
| 65 |
|
| 66 |
$code=<<___; |
| 67 |
.text |
| 68 |
|
| 69 |
.type _aesni_ctr32_ghash_6x,\@abi-omnipotent |
| 70 |
.align 32 |
| 71 |
_aesni_ctr32_ghash_6x: |
| 72 |
vmovdqu 0x20($const),$T2 # borrow $T2, .Lone_msb |
| 73 |
sub \$6,$len |
| 74 |
vpxor $Z0,$Z0,$Z0 # $Z0 = 0 |
| 75 |
vmovdqu 0x00-0x80($key),$rndkey |
| 76 |
vpaddb $T2,$T1,$inout1 |
| 77 |
vpaddb $T2,$inout1,$inout2 |
| 78 |
vpaddb $T2,$inout2,$inout3 |
| 79 |
vpaddb $T2,$inout3,$inout4 |
| 80 |
vpaddb $T2,$inout4,$inout5 |
| 81 |
vpxor $rndkey,$T1,$inout0 |
| 82 |
vmovdqu $Z0,16+8(%rsp) # "$Z3" = 0 |
| 83 |
jmp .Loop6x |
| 84 |
|
| 85 |
.align 32 |
| 86 |
.Loop6x: |
| 87 |
add \$`6<<24`,$counter |
| 88 |
jc .Lhandle_ctr32 # discard $inout[1-5]? |
| 89 |
vmovdqu 0x00-0x20($Xip),$Hkey # $Hkey^1 |
| 90 |
vpaddb $T2,$inout5,$T1 # next counter value |
| 91 |
vpxor $rndkey,$inout1,$inout1 |
| 92 |
vpxor $rndkey,$inout2,$inout2 |
| 93 |
|
| 94 |
.Lresume_ctr32: |
| 95 |
vmovdqu $T1,($ivp) # save next counter value |
| 96 |
vpclmulqdq \$0x10,$Hkey,$Z3,$Z1 |
| 97 |
vpxor $rndkey,$inout3,$inout3 |
| 98 |
vmovups 0x10-0x80($key),$T2 # borrow $T2 for $rndkey |
| 99 |
vpclmulqdq \$0x01,$Hkey,$Z3,$Z2 |
| 100 |
|
| 101 |
# At this point, the current block of 96 (0x60) bytes has already been |
| 102 |
# loaded into registers. Concurrently with processing it, we want to |
| 103 |
# load the next 96 bytes of input for the next round. Obviously, we can |
| 104 |
# only do this if there are at least 96 more bytes of input beyond the |
| 105 |
# input we're currently processing, or else we'd read past the end of |
| 106 |
# the input buffer. Here, we set |%r12| to 96 if there are at least 96 |
| 107 |
# bytes of input beyond the 96 bytes we're already processing, and we |
| 108 |
# set |%r12| to 0 otherwise. In the case where we set |%r12| to 96, |
| 109 |
# we'll read in the next block so that it is in registers for the next |
| 110 |
# loop iteration. In the case where we set |%r12| to 0, we'll re-read |
| 111 |
# the current block and then ignore what we re-read. |
| 112 |
# |
| 113 |
# At this point, |$in0| points to the current (already read into |
| 114 |
# registers) block, and |$end0| points to 2*96 bytes before the end of |
| 115 |
# the input. Thus, |$in0| > |$end0| means that we do not have the next |
| 116 |
# 96-byte block to read in, and |$in0| <= |$end0| means we do. |
| 117 |
xor %r12,%r12 |
| 118 |
cmp $in0,$end0 |
| 119 |
|
| 120 |
vaesenc $T2,$inout0,$inout0 |
| 121 |
vmovdqu 0x30+8(%rsp),$Ii # I[4] |
| 122 |
vpxor $rndkey,$inout4,$inout4 |
| 123 |
vpclmulqdq \$0x00,$Hkey,$Z3,$T1 |
| 124 |
vaesenc $T2,$inout1,$inout1 |
| 125 |
vpxor $rndkey,$inout5,$inout5 |
| 126 |
setnc %r12b |
| 127 |
vpclmulqdq \$0x11,$Hkey,$Z3,$Z3 |
| 128 |
vaesenc $T2,$inout2,$inout2 |
| 129 |
vmovdqu 0x10-0x20($Xip),$Hkey # $Hkey^2 |
| 130 |
neg %r12 |
| 131 |
vaesenc $T2,$inout3,$inout3 |
| 132 |
vpxor $Z1,$Z2,$Z2 |
| 133 |
vpclmulqdq \$0x00,$Hkey,$Ii,$Z1 |
| 134 |
vpxor $Z0,$Xi,$Xi # modulo-scheduled |
| 135 |
vaesenc $T2,$inout4,$inout4 |
| 136 |
vpxor $Z1,$T1,$Z0 |
| 137 |
and \$0x60,%r12 |
| 138 |
vmovups 0x20-0x80($key),$rndkey |
| 139 |
vpclmulqdq \$0x10,$Hkey,$Ii,$T1 |
| 140 |
vaesenc $T2,$inout5,$inout5 |
| 141 |
|
| 142 |
vpclmulqdq \$0x01,$Hkey,$Ii,$T2 |
| 143 |
lea ($in0,%r12),$in0 |
| 144 |
vaesenc $rndkey,$inout0,$inout0 |
| 145 |
vpxor 16+8(%rsp),$Xi,$Xi # modulo-scheduled [vpxor $Z3,$Xi,$Xi] |
| 146 |
vpclmulqdq \$0x11,$Hkey,$Ii,$Hkey |
| 147 |
vmovdqu 0x40+8(%rsp),$Ii # I[3] |
| 148 |
vaesenc $rndkey,$inout1,$inout1 |
| 149 |
movbe 0x58($in0),%r13 |
| 150 |
vaesenc $rndkey,$inout2,$inout2 |
| 151 |
movbe 0x50($in0),%r12 |
| 152 |
vaesenc $rndkey,$inout3,$inout3 |
| 153 |
mov %r13,0x20+8(%rsp) |
| 154 |
vaesenc $rndkey,$inout4,$inout4 |
| 155 |
mov %r12,0x28+8(%rsp) |
| 156 |
vmovdqu 0x30-0x20($Xip),$Z1 # borrow $Z1 for $Hkey^3 |
| 157 |
vaesenc $rndkey,$inout5,$inout5 |
| 158 |
|
| 159 |
vmovups 0x30-0x80($key),$rndkey |
| 160 |
vpxor $T1,$Z2,$Z2 |
| 161 |
vpclmulqdq \$0x00,$Z1,$Ii,$T1 |
| 162 |
vaesenc $rndkey,$inout0,$inout0 |
| 163 |
vpxor $T2,$Z2,$Z2 |
| 164 |
vpclmulqdq \$0x10,$Z1,$Ii,$T2 |
| 165 |
vaesenc $rndkey,$inout1,$inout1 |
| 166 |
vpxor $Hkey,$Z3,$Z3 |
| 167 |
vpclmulqdq \$0x01,$Z1,$Ii,$Hkey |
| 168 |
vaesenc $rndkey,$inout2,$inout2 |
| 169 |
vpclmulqdq \$0x11,$Z1,$Ii,$Z1 |
| 170 |
vmovdqu 0x50+8(%rsp),$Ii # I[2] |
| 171 |
vaesenc $rndkey,$inout3,$inout3 |
| 172 |
vaesenc $rndkey,$inout4,$inout4 |
| 173 |
vpxor $T1,$Z0,$Z0 |
| 174 |
vmovdqu 0x40-0x20($Xip),$T1 # borrow $T1 for $Hkey^4 |
| 175 |
vaesenc $rndkey,$inout5,$inout5 |
| 176 |
|
| 177 |
vmovups 0x40-0x80($key),$rndkey |
| 178 |
vpxor $T2,$Z2,$Z2 |
| 179 |
vpclmulqdq \$0x00,$T1,$Ii,$T2 |
| 180 |
vaesenc $rndkey,$inout0,$inout0 |
| 181 |
vpxor $Hkey,$Z2,$Z2 |
| 182 |
vpclmulqdq \$0x10,$T1,$Ii,$Hkey |
| 183 |
vaesenc $rndkey,$inout1,$inout1 |
| 184 |
movbe 0x48($in0),%r13 |
| 185 |
vpxor $Z1,$Z3,$Z3 |
| 186 |
vpclmulqdq \$0x01,$T1,$Ii,$Z1 |
| 187 |
vaesenc $rndkey,$inout2,$inout2 |
| 188 |
movbe 0x40($in0),%r12 |
| 189 |
vpclmulqdq \$0x11,$T1,$Ii,$T1 |
| 190 |
vmovdqu 0x60+8(%rsp),$Ii # I[1] |
| 191 |
vaesenc $rndkey,$inout3,$inout3 |
| 192 |
mov %r13,0x30+8(%rsp) |
| 193 |
vaesenc $rndkey,$inout4,$inout4 |
| 194 |
mov %r12,0x38+8(%rsp) |
| 195 |
vpxor $T2,$Z0,$Z0 |
| 196 |
vmovdqu 0x60-0x20($Xip),$T2 # borrow $T2 for $Hkey^5 |
| 197 |
vaesenc $rndkey,$inout5,$inout5 |
| 198 |
|
| 199 |
vmovups 0x50-0x80($key),$rndkey |
| 200 |
vpxor $Hkey,$Z2,$Z2 |
| 201 |
vpclmulqdq \$0x00,$T2,$Ii,$Hkey |
| 202 |
vaesenc $rndkey,$inout0,$inout0 |
| 203 |
vpxor $Z1,$Z2,$Z2 |
| 204 |
vpclmulqdq \$0x10,$T2,$Ii,$Z1 |
| 205 |
vaesenc $rndkey,$inout1,$inout1 |
| 206 |
movbe 0x38($in0),%r13 |
| 207 |
vpxor $T1,$Z3,$Z3 |
| 208 |
vpclmulqdq \$0x01,$T2,$Ii,$T1 |
| 209 |
vpxor 0x70+8(%rsp),$Xi,$Xi # accumulate I[0] |
| 210 |
vaesenc $rndkey,$inout2,$inout2 |
| 211 |
movbe 0x30($in0),%r12 |
| 212 |
vpclmulqdq \$0x11,$T2,$Ii,$T2 |
| 213 |
vaesenc $rndkey,$inout3,$inout3 |
| 214 |
mov %r13,0x40+8(%rsp) |
| 215 |
vaesenc $rndkey,$inout4,$inout4 |
| 216 |
mov %r12,0x48+8(%rsp) |
| 217 |
vpxor $Hkey,$Z0,$Z0 |
| 218 |
vmovdqu 0x70-0x20($Xip),$Hkey # $Hkey^6 |
| 219 |
vaesenc $rndkey,$inout5,$inout5 |
| 220 |
|
| 221 |
vmovups 0x60-0x80($key),$rndkey |
| 222 |
vpxor $Z1,$Z2,$Z2 |
| 223 |
vpclmulqdq \$0x10,$Hkey,$Xi,$Z1 |
| 224 |
vaesenc $rndkey,$inout0,$inout0 |
| 225 |
vpxor $T1,$Z2,$Z2 |
| 226 |
vpclmulqdq \$0x01,$Hkey,$Xi,$T1 |
| 227 |
vaesenc $rndkey,$inout1,$inout1 |
| 228 |
movbe 0x28($in0),%r13 |
| 229 |
vpxor $T2,$Z3,$Z3 |
| 230 |
vpclmulqdq \$0x00,$Hkey,$Xi,$T2 |
| 231 |
vaesenc $rndkey,$inout2,$inout2 |
| 232 |
movbe 0x20($in0),%r12 |
| 233 |
vpclmulqdq \$0x11,$Hkey,$Xi,$Xi |
| 234 |
vaesenc $rndkey,$inout3,$inout3 |
| 235 |
mov %r13,0x50+8(%rsp) |
| 236 |
vaesenc $rndkey,$inout4,$inout4 |
| 237 |
mov %r12,0x58+8(%rsp) |
| 238 |
vpxor $Z1,$Z2,$Z2 |
| 239 |
vaesenc $rndkey,$inout5,$inout5 |
| 240 |
vpxor $T1,$Z2,$Z2 |
| 241 |
|
| 242 |
vmovups 0x70-0x80($key),$rndkey |
| 243 |
vpslldq \$8,$Z2,$Z1 |
| 244 |
vpxor $T2,$Z0,$Z0 |
| 245 |
vmovdqu 0x10($const),$Hkey # .Lpoly |
| 246 |
|
| 247 |
vaesenc $rndkey,$inout0,$inout0 |
| 248 |
vpxor $Xi,$Z3,$Z3 |
| 249 |
vaesenc $rndkey,$inout1,$inout1 |
| 250 |
vpxor $Z1,$Z0,$Z0 |
| 251 |
movbe 0x18($in0),%r13 |
| 252 |
vaesenc $rndkey,$inout2,$inout2 |
| 253 |
movbe 0x10($in0),%r12 |
| 254 |
vpalignr \$8,$Z0,$Z0,$Ii # 1st phase |
| 255 |
vpclmulqdq \$0x10,$Hkey,$Z0,$Z0 |
| 256 |
mov %r13,0x60+8(%rsp) |
| 257 |
vaesenc $rndkey,$inout3,$inout3 |
| 258 |
mov %r12,0x68+8(%rsp) |
| 259 |
vaesenc $rndkey,$inout4,$inout4 |
| 260 |
vmovups 0x80-0x80($key),$T1 # borrow $T1 for $rndkey |
| 261 |
vaesenc $rndkey,$inout5,$inout5 |
| 262 |
|
| 263 |
vaesenc $T1,$inout0,$inout0 |
| 264 |
vmovups 0x90-0x80($key),$rndkey |
| 265 |
vaesenc $T1,$inout1,$inout1 |
| 266 |
vpsrldq \$8,$Z2,$Z2 |
| 267 |
vaesenc $T1,$inout2,$inout2 |
| 268 |
vpxor $Z2,$Z3,$Z3 |
| 269 |
vaesenc $T1,$inout3,$inout3 |
| 270 |
vpxor $Ii,$Z0,$Z0 |
| 271 |
movbe 0x08($in0),%r13 |
| 272 |
vaesenc $T1,$inout4,$inout4 |
| 273 |
movbe 0x00($in0),%r12 |
| 274 |
vaesenc $T1,$inout5,$inout5 |
| 275 |
vmovups 0xa0-0x80($key),$T1 |
| 276 |
cmp \$11,$rounds |
| 277 |
jb .Lenc_tail # 128-bit key |
| 278 |
|
| 279 |
vaesenc $rndkey,$inout0,$inout0 |
| 280 |
vaesenc $rndkey,$inout1,$inout1 |
| 281 |
vaesenc $rndkey,$inout2,$inout2 |
| 282 |
vaesenc $rndkey,$inout3,$inout3 |
| 283 |
vaesenc $rndkey,$inout4,$inout4 |
| 284 |
vaesenc $rndkey,$inout5,$inout5 |
| 285 |
|
| 286 |
vaesenc $T1,$inout0,$inout0 |
| 287 |
vaesenc $T1,$inout1,$inout1 |
| 288 |
vaesenc $T1,$inout2,$inout2 |
| 289 |
vaesenc $T1,$inout3,$inout3 |
| 290 |
vaesenc $T1,$inout4,$inout4 |
| 291 |
vmovups 0xb0-0x80($key),$rndkey |
| 292 |
vaesenc $T1,$inout5,$inout5 |
| 293 |
vmovups 0xc0-0x80($key),$T1 |
| 294 |
je .Lenc_tail # 192-bit key |
| 295 |
|
| 296 |
vaesenc $rndkey,$inout0,$inout0 |
| 297 |
vaesenc $rndkey,$inout1,$inout1 |
| 298 |
vaesenc $rndkey,$inout2,$inout2 |
| 299 |
vaesenc $rndkey,$inout3,$inout3 |
| 300 |
vaesenc $rndkey,$inout4,$inout4 |
| 301 |
vaesenc $rndkey,$inout5,$inout5 |
| 302 |
|
| 303 |
vaesenc $T1,$inout0,$inout0 |
| 304 |
vaesenc $T1,$inout1,$inout1 |
| 305 |
vaesenc $T1,$inout2,$inout2 |
| 306 |
vaesenc $T1,$inout3,$inout3 |
| 307 |
vaesenc $T1,$inout4,$inout4 |
| 308 |
vmovups 0xd0-0x80($key),$rndkey |
| 309 |
vaesenc $T1,$inout5,$inout5 |
| 310 |
vmovups 0xe0-0x80($key),$T1 |
| 311 |
jmp .Lenc_tail # 256-bit key |
| 312 |
|
| 313 |
.align 32 |
| 314 |
.Lhandle_ctr32: |
| 315 |
vmovdqu ($const),$Ii # borrow $Ii for .Lbswap_mask |
| 316 |
vpshufb $Ii,$T1,$Z2 # byte-swap counter |
| 317 |
vmovdqu 0x30($const),$Z1 # borrow $Z1, .Ltwo_lsb |
| 318 |
vpaddd 0x40($const),$Z2,$inout1 # .Lone_lsb |
| 319 |
vpaddd $Z1,$Z2,$inout2 |
| 320 |
vmovdqu 0x00-0x20($Xip),$Hkey # $Hkey^1 |
| 321 |
vpaddd $Z1,$inout1,$inout3 |
| 322 |
vpshufb $Ii,$inout1,$inout1 |
| 323 |
vpaddd $Z1,$inout2,$inout4 |
| 324 |
vpshufb $Ii,$inout2,$inout2 |
| 325 |
vpxor $rndkey,$inout1,$inout1 |
| 326 |
vpaddd $Z1,$inout3,$inout5 |
| 327 |
vpshufb $Ii,$inout3,$inout3 |
| 328 |
vpxor $rndkey,$inout2,$inout2 |
| 329 |
vpaddd $Z1,$inout4,$T1 # byte-swapped next counter value |
| 330 |
vpshufb $Ii,$inout4,$inout4 |
| 331 |
vpshufb $Ii,$inout5,$inout5 |
| 332 |
vpshufb $Ii,$T1,$T1 # next counter value |
| 333 |
jmp .Lresume_ctr32 |
| 334 |
|
| 335 |
.align 32 |
| 336 |
.Lenc_tail: |
| 337 |
vaesenc $rndkey,$inout0,$inout0 |
| 338 |
vmovdqu $Z3,16+8(%rsp) # postpone vpxor $Z3,$Xi,$Xi |
| 339 |
vpalignr \$8,$Z0,$Z0,$Xi # 2nd phase |
| 340 |
vaesenc $rndkey,$inout1,$inout1 |
| 341 |
vpclmulqdq \$0x10,$Hkey,$Z0,$Z0 |
| 342 |
vpxor 0x00($inp),$T1,$T2 |
| 343 |
vaesenc $rndkey,$inout2,$inout2 |
| 344 |
vpxor 0x10($inp),$T1,$Ii |
| 345 |
vaesenc $rndkey,$inout3,$inout3 |
| 346 |
vpxor 0x20($inp),$T1,$Z1 |
| 347 |
vaesenc $rndkey,$inout4,$inout4 |
| 348 |
vpxor 0x30($inp),$T1,$Z2 |
| 349 |
vaesenc $rndkey,$inout5,$inout5 |
| 350 |
vpxor 0x40($inp),$T1,$Z3 |
| 351 |
vpxor 0x50($inp),$T1,$Hkey |
| 352 |
vmovdqu ($ivp),$T1 # load next counter value |
| 353 |
|
| 354 |
vaesenclast $T2,$inout0,$inout0 |
| 355 |
vmovdqu 0x20($const),$T2 # borrow $T2, .Lone_msb |
| 356 |
vaesenclast $Ii,$inout1,$inout1 |
| 357 |
vpaddb $T2,$T1,$Ii |
| 358 |
mov %r13,0x70+8(%rsp) |
| 359 |
lea 0x60($inp),$inp |
| 360 |
vaesenclast $Z1,$inout2,$inout2 |
| 361 |
vpaddb $T2,$Ii,$Z1 |
| 362 |
mov %r12,0x78+8(%rsp) |
| 363 |
lea 0x60($out),$out |
| 364 |
vmovdqu 0x00-0x80($key),$rndkey |
| 365 |
vaesenclast $Z2,$inout3,$inout3 |
| 366 |
vpaddb $T2,$Z1,$Z2 |
| 367 |
vaesenclast $Z3, $inout4,$inout4 |
| 368 |
vpaddb $T2,$Z2,$Z3 |
| 369 |
vaesenclast $Hkey,$inout5,$inout5 |
| 370 |
vpaddb $T2,$Z3,$Hkey |
| 371 |
|
| 372 |
add \$0x60,$ret |
| 373 |
sub \$0x6,$len |
| 374 |
jc .L6x_done |
| 375 |
|
| 376 |
vmovups $inout0,-0x60($out) # save output |
| 377 |
vpxor $rndkey,$T1,$inout0 |
| 378 |
vmovups $inout1,-0x50($out) |
| 379 |
vmovdqa $Ii,$inout1 # 0 latency |
| 380 |
vmovups $inout2,-0x40($out) |
| 381 |
vmovdqa $Z1,$inout2 # 0 latency |
| 382 |
vmovups $inout3,-0x30($out) |
| 383 |
vmovdqa $Z2,$inout3 # 0 latency |
| 384 |
vmovups $inout4,-0x20($out) |
| 385 |
vmovdqa $Z3,$inout4 # 0 latency |
| 386 |
vmovups $inout5,-0x10($out) |
| 387 |
vmovdqa $Hkey,$inout5 # 0 latency |
| 388 |
vmovdqu 0x20+8(%rsp),$Z3 # I[5] |
| 389 |
jmp .Loop6x |
| 390 |
|
| 391 |
.L6x_done: |
| 392 |
vpxor 16+8(%rsp),$Xi,$Xi # modulo-scheduled |
| 393 |
vpxor $Z0,$Xi,$Xi # modulo-scheduled |
| 394 |
|
| 395 |
ret |
| 396 |
.size _aesni_ctr32_ghash_6x,.-_aesni_ctr32_ghash_6x |
| 397 |
___ |
| 398 |
###################################################################### |
| 399 |
# |
| 400 |
# size_t aesni_gcm_[en|de]crypt(const void *inp, void *out, size_t len, |
| 401 |
# const AES_KEY *key, unsigned char iv[16], |
| 402 |
# struct { u128 Xi,H,Htbl[9]; } *Xip); |
| 403 |
$code.=<<___; |
| 404 |
.globl aesni_gcm_decrypt |
| 405 |
.type aesni_gcm_decrypt,\@function,6 |
| 406 |
.align 32 |
| 407 |
aesni_gcm_decrypt: |
| 408 |
xor $ret,$ret |
| 409 |
|
| 410 |
# We call |_aesni_ctr32_ghash_6x|, which requires at least 96 (0x60) |
| 411 |
# bytes of input. |
| 412 |
cmp \$0x60,$len # minimal accepted length |
| 413 |
jb .Lgcm_dec_abort |
| 414 |
|
| 415 |
lea (%rsp),%rax # save stack pointer |
| 416 |
push %rbx |
| 417 |
push %rbp |
| 418 |
push %r12 |
| 419 |
push %r13 |
| 420 |
push %r14 |
| 421 |
push %r15 |
| 422 |
___ |
| 423 |
$code.=<<___ if ($win64); |
| 424 |
lea -0xa8(%rsp),%rsp |
| 425 |
movaps %xmm6,-0xd8(%rax) |
| 426 |
movaps %xmm7,-0xc8(%rax) |
| 427 |
movaps %xmm8,-0xb8(%rax) |
| 428 |
movaps %xmm9,-0xa8(%rax) |
| 429 |
movaps %xmm10,-0x98(%rax) |
| 430 |
movaps %xmm11,-0x88(%rax) |
| 431 |
movaps %xmm12,-0x78(%rax) |
| 432 |
movaps %xmm13,-0x68(%rax) |
| 433 |
movaps %xmm14,-0x58(%rax) |
| 434 |
movaps %xmm15,-0x48(%rax) |
| 435 |
.Lgcm_dec_body: |
| 436 |
___ |
| 437 |
$code.=<<___; |
| 438 |
vzeroupper |
| 439 |
|
| 440 |
vmovdqu ($ivp),$T1 # input counter value |
| 441 |
add \$-128,%rsp |
| 442 |
mov 12($ivp),$counter |
| 443 |
lea .Lbswap_mask(%rip),$const |
| 444 |
lea -0x80($key),$in0 # borrow $in0 |
| 445 |
mov \$0xf80,$end0 # borrow $end0 |
| 446 |
vmovdqu ($Xip),$Xi # load Xi |
| 447 |
and \$-128,%rsp # ensure stack alignment |
| 448 |
vmovdqu ($const),$Ii # borrow $Ii for .Lbswap_mask |
| 449 |
lea 0x80($key),$key # size optimization |
| 450 |
lea 0x20+0x20($Xip),$Xip # size optimization |
| 451 |
mov 0xf0-0x80($key),$rounds |
| 452 |
vpshufb $Ii,$Xi,$Xi |
| 453 |
|
| 454 |
and $end0,$in0 |
| 455 |
and %rsp,$end0 |
| 456 |
sub $in0,$end0 |
| 457 |
jc .Ldec_no_key_aliasing |
| 458 |
cmp \$768,$end0 |
| 459 |
jnc .Ldec_no_key_aliasing |
| 460 |
sub $end0,%rsp # avoid aliasing with key |
| 461 |
.Ldec_no_key_aliasing: |
| 462 |
|
| 463 |
vmovdqu 0x50($inp),$Z3 # I[5] |
| 464 |
lea ($inp),$in0 |
| 465 |
vmovdqu 0x40($inp),$Z0 |
| 466 |
|
| 467 |
# |_aesni_ctr32_ghash_6x| requires |$end0| to point to 2*96 (0xc0) |
| 468 |
# bytes before the end of the input. Note, in particular, that this is |
| 469 |
# correct even if |$len| is not an even multiple of 96 or 16. XXX: This |
| 470 |
# seems to require that |$inp| + |$len| >= 2*96 (0xc0); i.e. |$inp| must |
| 471 |
# not be near the very beginning of the address space when |$len| < 2*96 |
| 472 |
# (0xc0). |
| 473 |
lea -0xc0($inp,$len),$end0 |
| 474 |
|
| 475 |
vmovdqu 0x30($inp),$Z1 |
| 476 |
shr \$4,$len |
| 477 |
xor $ret,$ret |
| 478 |
vmovdqu 0x20($inp),$Z2 |
| 479 |
vpshufb $Ii,$Z3,$Z3 # passed to _aesni_ctr32_ghash_6x |
| 480 |
vmovdqu 0x10($inp),$T2 |
| 481 |
vpshufb $Ii,$Z0,$Z0 |
| 482 |
vmovdqu ($inp),$Hkey |
| 483 |
vpshufb $Ii,$Z1,$Z1 |
| 484 |
vmovdqu $Z0,0x30(%rsp) |
| 485 |
vpshufb $Ii,$Z2,$Z2 |
| 486 |
vmovdqu $Z1,0x40(%rsp) |
| 487 |
vpshufb $Ii,$T2,$T2 |
| 488 |
vmovdqu $Z2,0x50(%rsp) |
| 489 |
vpshufb $Ii,$Hkey,$Hkey |
| 490 |
vmovdqu $T2,0x60(%rsp) |
| 491 |
vmovdqu $Hkey,0x70(%rsp) |
| 492 |
|
| 493 |
call _aesni_ctr32_ghash_6x |
| 494 |
|
| 495 |
vmovups $inout0,-0x60($out) # save output |
| 496 |
vmovups $inout1,-0x50($out) |
| 497 |
vmovups $inout2,-0x40($out) |
| 498 |
vmovups $inout3,-0x30($out) |
| 499 |
vmovups $inout4,-0x20($out) |
| 500 |
vmovups $inout5,-0x10($out) |
| 501 |
|
| 502 |
vpshufb ($const),$Xi,$Xi # .Lbswap_mask |
| 503 |
vmovdqu $Xi,-0x40($Xip) # output Xi |
| 504 |
|
| 505 |
vzeroupper |
| 506 |
___ |
| 507 |
$code.=<<___ if ($win64); |
| 508 |
movaps -0xd8(%rax),%xmm6 |
| 509 |
movaps -0xc8(%rax),%xmm7 |
| 510 |
movaps -0xb8(%rax),%xmm8 |
| 511 |
movaps -0xa8(%rax),%xmm9 |
| 512 |
movaps -0x98(%rax),%xmm10 |
| 513 |
movaps -0x88(%rax),%xmm11 |
| 514 |
movaps -0x78(%rax),%xmm12 |
| 515 |
movaps -0x68(%rax),%xmm13 |
| 516 |
movaps -0x58(%rax),%xmm14 |
| 517 |
movaps -0x48(%rax),%xmm15 |
| 518 |
___ |
| 519 |
$code.=<<___; |
| 520 |
mov -48(%rax),%r15 |
| 521 |
mov -40(%rax),%r14 |
| 522 |
mov -32(%rax),%r13 |
| 523 |
mov -24(%rax),%r12 |
| 524 |
mov -16(%rax),%rbp |
| 525 |
mov -8(%rax),%rbx |
| 526 |
lea (%rax),%rsp # restore %rsp |
| 527 |
.Lgcm_dec_abort: |
| 528 |
mov $ret,%rax # return value |
| 529 |
ret |
| 530 |
.size aesni_gcm_decrypt,.-aesni_gcm_decrypt |
| 531 |
___ |
| 532 |
|
| 533 |
$code.=<<___; |
| 534 |
.type _aesni_ctr32_6x,\@abi-omnipotent |
| 535 |
.align 32 |
| 536 |
_aesni_ctr32_6x: |
| 537 |
vmovdqu 0x00-0x80($key),$Z0 # borrow $Z0 for $rndkey |
| 538 |
vmovdqu 0x20($const),$T2 # borrow $T2, .Lone_msb |
| 539 |
lea -1($rounds),%r13 |
| 540 |
vmovups 0x10-0x80($key),$rndkey |
| 541 |
lea 0x20-0x80($key),%r12 |
| 542 |
vpxor $Z0,$T1,$inout0 |
| 543 |
add \$`6<<24`,$counter |
| 544 |
jc .Lhandle_ctr32_2 |
| 545 |
vpaddb $T2,$T1,$inout1 |
| 546 |
vpaddb $T2,$inout1,$inout2 |
| 547 |
vpxor $Z0,$inout1,$inout1 |
| 548 |
vpaddb $T2,$inout2,$inout3 |
| 549 |
vpxor $Z0,$inout2,$inout2 |
| 550 |
vpaddb $T2,$inout3,$inout4 |
| 551 |
vpxor $Z0,$inout3,$inout3 |
| 552 |
vpaddb $T2,$inout4,$inout5 |
| 553 |
vpxor $Z0,$inout4,$inout4 |
| 554 |
vpaddb $T2,$inout5,$T1 |
| 555 |
vpxor $Z0,$inout5,$inout5 |
| 556 |
jmp .Loop_ctr32 |
| 557 |
|
| 558 |
.align 16 |
| 559 |
.Loop_ctr32: |
| 560 |
vaesenc $rndkey,$inout0,$inout0 |
| 561 |
vaesenc $rndkey,$inout1,$inout1 |
| 562 |
vaesenc $rndkey,$inout2,$inout2 |
| 563 |
vaesenc $rndkey,$inout3,$inout3 |
| 564 |
vaesenc $rndkey,$inout4,$inout4 |
| 565 |
vaesenc $rndkey,$inout5,$inout5 |
| 566 |
vmovups (%r12),$rndkey |
| 567 |
lea 0x10(%r12),%r12 |
| 568 |
dec %r13d |
| 569 |
jnz .Loop_ctr32 |
| 570 |
|
| 571 |
vmovdqu (%r12),$Hkey # last round key |
| 572 |
vaesenc $rndkey,$inout0,$inout0 |
| 573 |
vpxor 0x00($inp),$Hkey,$Z0 |
| 574 |
vaesenc $rndkey,$inout1,$inout1 |
| 575 |
vpxor 0x10($inp),$Hkey,$Z1 |
| 576 |
vaesenc $rndkey,$inout2,$inout2 |
| 577 |
vpxor 0x20($inp),$Hkey,$Z2 |
| 578 |
vaesenc $rndkey,$inout3,$inout3 |
| 579 |
vpxor 0x30($inp),$Hkey,$Xi |
| 580 |
vaesenc $rndkey,$inout4,$inout4 |
| 581 |
vpxor 0x40($inp),$Hkey,$T2 |
| 582 |
vaesenc $rndkey,$inout5,$inout5 |
| 583 |
vpxor 0x50($inp),$Hkey,$Hkey |
| 584 |
lea 0x60($inp),$inp |
| 585 |
|
| 586 |
vaesenclast $Z0,$inout0,$inout0 |
| 587 |
vaesenclast $Z1,$inout1,$inout1 |
| 588 |
vaesenclast $Z2,$inout2,$inout2 |
| 589 |
vaesenclast $Xi,$inout3,$inout3 |
| 590 |
vaesenclast $T2,$inout4,$inout4 |
| 591 |
vaesenclast $Hkey,$inout5,$inout5 |
| 592 |
vmovups $inout0,0x00($out) |
| 593 |
vmovups $inout1,0x10($out) |
| 594 |
vmovups $inout2,0x20($out) |
| 595 |
vmovups $inout3,0x30($out) |
| 596 |
vmovups $inout4,0x40($out) |
| 597 |
vmovups $inout5,0x50($out) |
| 598 |
lea 0x60($out),$out |
| 599 |
|
| 600 |
ret |
| 601 |
.align 32 |
| 602 |
.Lhandle_ctr32_2: |
| 603 |
vpshufb $Ii,$T1,$Z2 # byte-swap counter |
| 604 |
vmovdqu 0x30($const),$Z1 # borrow $Z1, .Ltwo_lsb |
| 605 |
vpaddd 0x40($const),$Z2,$inout1 # .Lone_lsb |
| 606 |
vpaddd $Z1,$Z2,$inout2 |
| 607 |
vpaddd $Z1,$inout1,$inout3 |
| 608 |
vpshufb $Ii,$inout1,$inout1 |
| 609 |
vpaddd $Z1,$inout2,$inout4 |
| 610 |
vpshufb $Ii,$inout2,$inout2 |
| 611 |
vpxor $Z0,$inout1,$inout1 |
| 612 |
vpaddd $Z1,$inout3,$inout5 |
| 613 |
vpshufb $Ii,$inout3,$inout3 |
| 614 |
vpxor $Z0,$inout2,$inout2 |
| 615 |
vpaddd $Z1,$inout4,$T1 # byte-swapped next counter value |
| 616 |
vpshufb $Ii,$inout4,$inout4 |
| 617 |
vpxor $Z0,$inout3,$inout3 |
| 618 |
vpshufb $Ii,$inout5,$inout5 |
| 619 |
vpxor $Z0,$inout4,$inout4 |
| 620 |
vpshufb $Ii,$T1,$T1 # next counter value |
| 621 |
vpxor $Z0,$inout5,$inout5 |
| 622 |
jmp .Loop_ctr32 |
| 623 |
.size _aesni_ctr32_6x,.-_aesni_ctr32_6x |
| 624 |
|
| 625 |
.globl aesni_gcm_encrypt |
| 626 |
.type aesni_gcm_encrypt,\@function,6 |
| 627 |
.align 32 |
| 628 |
aesni_gcm_encrypt: |
| 629 |
xor $ret,$ret |
| 630 |
|
| 631 |
# We call |_aesni_ctr32_6x| twice, each call consuming 96 bytes of |
| 632 |
# input. Then we call |_aesni_ctr32_ghash_6x|, which requires at |
| 633 |
# least 96 more bytes of input. |
| 634 |
cmp \$0x60*3,$len # minimal accepted length |
| 635 |
jb .Lgcm_enc_abort |
| 636 |
|
| 637 |
lea (%rsp),%rax # save stack pointer |
| 638 |
push %rbx |
| 639 |
push %rbp |
| 640 |
push %r12 |
| 641 |
push %r13 |
| 642 |
push %r14 |
| 643 |
push %r15 |
| 644 |
___ |
| 645 |
$code.=<<___ if ($win64); |
| 646 |
lea -0xa8(%rsp),%rsp |
| 647 |
movaps %xmm6,-0xd8(%rax) |
| 648 |
movaps %xmm7,-0xc8(%rax) |
| 649 |
movaps %xmm8,-0xb8(%rax) |
| 650 |
movaps %xmm9,-0xa8(%rax) |
| 651 |
movaps %xmm10,-0x98(%rax) |
| 652 |
movaps %xmm11,-0x88(%rax) |
| 653 |
movaps %xmm12,-0x78(%rax) |
| 654 |
movaps %xmm13,-0x68(%rax) |
| 655 |
movaps %xmm14,-0x58(%rax) |
| 656 |
movaps %xmm15,-0x48(%rax) |
| 657 |
.Lgcm_enc_body: |
| 658 |
___ |
| 659 |
$code.=<<___; |
| 660 |
vzeroupper |
| 661 |
|
| 662 |
vmovdqu ($ivp),$T1 # input counter value |
| 663 |
add \$-128,%rsp |
| 664 |
mov 12($ivp),$counter |
| 665 |
lea .Lbswap_mask(%rip),$const |
| 666 |
lea -0x80($key),$in0 # borrow $in0 |
| 667 |
mov \$0xf80,$end0 # borrow $end0 |
| 668 |
lea 0x80($key),$key # size optimization |
| 669 |
vmovdqu ($const),$Ii # borrow $Ii for .Lbswap_mask |
| 670 |
and \$-128,%rsp # ensure stack alignment |
| 671 |
mov 0xf0-0x80($key),$rounds |
| 672 |
|
| 673 |
and $end0,$in0 |
| 674 |
and %rsp,$end0 |
| 675 |
sub $in0,$end0 |
| 676 |
jc .Lenc_no_key_aliasing |
| 677 |
cmp \$768,$end0 |
| 678 |
jnc .Lenc_no_key_aliasing |
| 679 |
sub $end0,%rsp # avoid aliasing with key |
| 680 |
.Lenc_no_key_aliasing: |
| 681 |
|
| 682 |
lea ($out),$in0 |
| 683 |
|
| 684 |
# |_aesni_ctr32_ghash_6x| requires |$end0| to point to 2*96 (0xc0) |
| 685 |
# bytes before the end of the input. Note, in particular, that this is |
| 686 |
# correct even if |$len| is not an even multiple of 96 or 16. Unlike in |
| 687 |
# the decryption case, there's no caveat that |$out| must not be near |
| 688 |
# the very beginning of the address space, because we know that |
| 689 |
# |$len| >= 3*96 from the check above, and so we know |
| 690 |
# |$out| + |$len| >= 2*96 (0xc0). |
| 691 |
lea -0xc0($out,$len),$end0 |
| 692 |
|
| 693 |
shr \$4,$len |
| 694 |
|
| 695 |
call _aesni_ctr32_6x |
| 696 |
vpshufb $Ii,$inout0,$Xi # save bswapped output on stack |
| 697 |
vpshufb $Ii,$inout1,$T2 |
| 698 |
vmovdqu $Xi,0x70(%rsp) |
| 699 |
vpshufb $Ii,$inout2,$Z0 |
| 700 |
vmovdqu $T2,0x60(%rsp) |
| 701 |
vpshufb $Ii,$inout3,$Z1 |
| 702 |
vmovdqu $Z0,0x50(%rsp) |
| 703 |
vpshufb $Ii,$inout4,$Z2 |
| 704 |
vmovdqu $Z1,0x40(%rsp) |
| 705 |
vpshufb $Ii,$inout5,$Z3 # passed to _aesni_ctr32_ghash_6x |
| 706 |
vmovdqu $Z2,0x30(%rsp) |
| 707 |
|
| 708 |
call _aesni_ctr32_6x |
| 709 |
|
| 710 |
vmovdqu ($Xip),$Xi # load Xi |
| 711 |
lea 0x20+0x20($Xip),$Xip # size optimization |
| 712 |
sub \$12,$len |
| 713 |
mov \$0x60*2,$ret |
| 714 |
vpshufb $Ii,$Xi,$Xi |
| 715 |
|
| 716 |
call _aesni_ctr32_ghash_6x |
| 717 |
vmovdqu 0x20(%rsp),$Z3 # I[5] |
| 718 |
vmovdqu ($const),$Ii # borrow $Ii for .Lbswap_mask |
| 719 |
vmovdqu 0x00-0x20($Xip),$Hkey # $Hkey^1 |
| 720 |
vpunpckhqdq $Z3,$Z3,$T1 |
| 721 |
vmovdqu 0x20-0x20($Xip),$rndkey # borrow $rndkey for $HK |
| 722 |
vmovups $inout0,-0x60($out) # save output |
| 723 |
vpshufb $Ii,$inout0,$inout0 # but keep bswapped copy |
| 724 |
vpxor $Z3,$T1,$T1 |
| 725 |
vmovups $inout1,-0x50($out) |
| 726 |
vpshufb $Ii,$inout1,$inout1 |
| 727 |
vmovups $inout2,-0x40($out) |
| 728 |
vpshufb $Ii,$inout2,$inout2 |
| 729 |
vmovups $inout3,-0x30($out) |
| 730 |
vpshufb $Ii,$inout3,$inout3 |
| 731 |
vmovups $inout4,-0x20($out) |
| 732 |
vpshufb $Ii,$inout4,$inout4 |
| 733 |
vmovups $inout5,-0x10($out) |
| 734 |
vpshufb $Ii,$inout5,$inout5 |
| 735 |
vmovdqu $inout0,0x10(%rsp) # free $inout0 |
| 736 |
___ |
| 737 |
{ my ($HK,$T3)=($rndkey,$inout0); |
| 738 |
|
| 739 |
$code.=<<___; |
| 740 |
vmovdqu 0x30(%rsp),$Z2 # I[4] |
| 741 |
vmovdqu 0x10-0x20($Xip),$Ii # borrow $Ii for $Hkey^2 |
| 742 |
vpunpckhqdq $Z2,$Z2,$T2 |
| 743 |
vpclmulqdq \$0x00,$Hkey,$Z3,$Z1 |
| 744 |
vpxor $Z2,$T2,$T2 |
| 745 |
vpclmulqdq \$0x11,$Hkey,$Z3,$Z3 |
| 746 |
vpclmulqdq \$0x00,$HK,$T1,$T1 |
| 747 |
|
| 748 |
vmovdqu 0x40(%rsp),$T3 # I[3] |
| 749 |
vpclmulqdq \$0x00,$Ii,$Z2,$Z0 |
| 750 |
vmovdqu 0x30-0x20($Xip),$Hkey # $Hkey^3 |
| 751 |
vpxor $Z1,$Z0,$Z0 |
| 752 |
vpunpckhqdq $T3,$T3,$Z1 |
| 753 |
vpclmulqdq \$0x11,$Ii,$Z2,$Z2 |
| 754 |
vpxor $T3,$Z1,$Z1 |
| 755 |
vpxor $Z3,$Z2,$Z2 |
| 756 |
vpclmulqdq \$0x10,$HK,$T2,$T2 |
| 757 |
vmovdqu 0x50-0x20($Xip),$HK |
| 758 |
vpxor $T1,$T2,$T2 |
| 759 |
|
| 760 |
vmovdqu 0x50(%rsp),$T1 # I[2] |
| 761 |
vpclmulqdq \$0x00,$Hkey,$T3,$Z3 |
| 762 |
vmovdqu 0x40-0x20($Xip),$Ii # borrow $Ii for $Hkey^4 |
| 763 |
vpxor $Z0,$Z3,$Z3 |
| 764 |
vpunpckhqdq $T1,$T1,$Z0 |
| 765 |
vpclmulqdq \$0x11,$Hkey,$T3,$T3 |
| 766 |
vpxor $T1,$Z0,$Z0 |
| 767 |
vpxor $Z2,$T3,$T3 |
| 768 |
vpclmulqdq \$0x00,$HK,$Z1,$Z1 |
| 769 |
vpxor $T2,$Z1,$Z1 |
| 770 |
|
| 771 |
vmovdqu 0x60(%rsp),$T2 # I[1] |
| 772 |
vpclmulqdq \$0x00,$Ii,$T1,$Z2 |
| 773 |
vmovdqu 0x60-0x20($Xip),$Hkey # $Hkey^5 |
| 774 |
vpxor $Z3,$Z2,$Z2 |
| 775 |
vpunpckhqdq $T2,$T2,$Z3 |
| 776 |
vpclmulqdq \$0x11,$Ii,$T1,$T1 |
| 777 |
vpxor $T2,$Z3,$Z3 |
| 778 |
vpxor $T3,$T1,$T1 |
| 779 |
vpclmulqdq \$0x10,$HK,$Z0,$Z0 |
| 780 |
vmovdqu 0x80-0x20($Xip),$HK |
| 781 |
vpxor $Z1,$Z0,$Z0 |
| 782 |
|
| 783 |
vpxor 0x70(%rsp),$Xi,$Xi # accumulate I[0] |
| 784 |
vpclmulqdq \$0x00,$Hkey,$T2,$Z1 |
| 785 |
vmovdqu 0x70-0x20($Xip),$Ii # borrow $Ii for $Hkey^6 |
| 786 |
vpunpckhqdq $Xi,$Xi,$T3 |
| 787 |
vpxor $Z2,$Z1,$Z1 |
| 788 |
vpclmulqdq \$0x11,$Hkey,$T2,$T2 |
| 789 |
vpxor $Xi,$T3,$T3 |
| 790 |
vpxor $T1,$T2,$T2 |
| 791 |
vpclmulqdq \$0x00,$HK,$Z3,$Z3 |
| 792 |
vpxor $Z0,$Z3,$Z0 |
| 793 |
|
| 794 |
vpclmulqdq \$0x00,$Ii,$Xi,$Z2 |
| 795 |
vmovdqu 0x00-0x20($Xip),$Hkey # $Hkey^1 |
| 796 |
vpunpckhqdq $inout5,$inout5,$T1 |
| 797 |
vpclmulqdq \$0x11,$Ii,$Xi,$Xi |
| 798 |
vpxor $inout5,$T1,$T1 |
| 799 |
vpxor $Z1,$Z2,$Z1 |
| 800 |
vpclmulqdq \$0x10,$HK,$T3,$T3 |
| 801 |
vmovdqu 0x20-0x20($Xip),$HK |
| 802 |
vpxor $T2,$Xi,$Z3 |
| 803 |
vpxor $Z0,$T3,$Z2 |
| 804 |
|
| 805 |
vmovdqu 0x10-0x20($Xip),$Ii # borrow $Ii for $Hkey^2 |
| 806 |
vpxor $Z1,$Z3,$T3 # aggregated Karatsuba post-processing |
| 807 |
vpclmulqdq \$0x00,$Hkey,$inout5,$Z0 |
| 808 |
vpxor $T3,$Z2,$Z2 |
| 809 |
vpunpckhqdq $inout4,$inout4,$T2 |
| 810 |
vpclmulqdq \$0x11,$Hkey,$inout5,$inout5 |
| 811 |
vpxor $inout4,$T2,$T2 |
| 812 |
vpslldq \$8,$Z2,$T3 |
| 813 |
vpclmulqdq \$0x00,$HK,$T1,$T1 |
| 814 |
vpxor $T3,$Z1,$Xi |
| 815 |
vpsrldq \$8,$Z2,$Z2 |
| 816 |
vpxor $Z2,$Z3,$Z3 |
| 817 |
|
| 818 |
vpclmulqdq \$0x00,$Ii,$inout4,$Z1 |
| 819 |
vmovdqu 0x30-0x20($Xip),$Hkey # $Hkey^3 |
| 820 |
vpxor $Z0,$Z1,$Z1 |
| 821 |
vpunpckhqdq $inout3,$inout3,$T3 |
| 822 |
vpclmulqdq \$0x11,$Ii,$inout4,$inout4 |
| 823 |
vpxor $inout3,$T3,$T3 |
| 824 |
vpxor $inout5,$inout4,$inout4 |
| 825 |
vpalignr \$8,$Xi,$Xi,$inout5 # 1st phase |
| 826 |
vpclmulqdq \$0x10,$HK,$T2,$T2 |
| 827 |
vmovdqu 0x50-0x20($Xip),$HK |
| 828 |
vpxor $T1,$T2,$T2 |
| 829 |
|
| 830 |
vpclmulqdq \$0x00,$Hkey,$inout3,$Z0 |
| 831 |
vmovdqu 0x40-0x20($Xip),$Ii # borrow $Ii for $Hkey^4 |
| 832 |
vpxor $Z1,$Z0,$Z0 |
| 833 |
vpunpckhqdq $inout2,$inout2,$T1 |
| 834 |
vpclmulqdq \$0x11,$Hkey,$inout3,$inout3 |
| 835 |
vpxor $inout2,$T1,$T1 |
| 836 |
vpxor $inout4,$inout3,$inout3 |
| 837 |
vxorps 0x10(%rsp),$Z3,$Z3 # accumulate $inout0 |
| 838 |
vpclmulqdq \$0x00,$HK,$T3,$T3 |
| 839 |
vpxor $T2,$T3,$T3 |
| 840 |
|
| 841 |
vpclmulqdq \$0x10,0x10($const),$Xi,$Xi |
| 842 |
vxorps $inout5,$Xi,$Xi |
| 843 |
|
| 844 |
vpclmulqdq \$0x00,$Ii,$inout2,$Z1 |
| 845 |
vmovdqu 0x60-0x20($Xip),$Hkey # $Hkey^5 |
| 846 |
vpxor $Z0,$Z1,$Z1 |
| 847 |
vpunpckhqdq $inout1,$inout1,$T2 |
| 848 |
vpclmulqdq \$0x11,$Ii,$inout2,$inout2 |
| 849 |
vpxor $inout1,$T2,$T2 |
| 850 |
vpalignr \$8,$Xi,$Xi,$inout5 # 2nd phase |
| 851 |
vpxor $inout3,$inout2,$inout2 |
| 852 |
vpclmulqdq \$0x10,$HK,$T1,$T1 |
| 853 |
vmovdqu 0x80-0x20($Xip),$HK |
| 854 |
vpxor $T3,$T1,$T1 |
| 855 |
|
| 856 |
vxorps $Z3,$inout5,$inout5 |
| 857 |
vpclmulqdq \$0x10,0x10($const),$Xi,$Xi |
| 858 |
vxorps $inout5,$Xi,$Xi |
| 859 |
|
| 860 |
vpclmulqdq \$0x00,$Hkey,$inout1,$Z0 |
| 861 |
vmovdqu 0x70-0x20($Xip),$Ii # borrow $Ii for $Hkey^6 |
| 862 |
vpxor $Z1,$Z0,$Z0 |
| 863 |
vpunpckhqdq $Xi,$Xi,$T3 |
| 864 |
vpclmulqdq \$0x11,$Hkey,$inout1,$inout1 |
| 865 |
vpxor $Xi,$T3,$T3 |
| 866 |
vpxor $inout2,$inout1,$inout1 |
| 867 |
vpclmulqdq \$0x00,$HK,$T2,$T2 |
| 868 |
vpxor $T1,$T2,$T2 |
| 869 |
|
| 870 |
vpclmulqdq \$0x00,$Ii,$Xi,$Z1 |
| 871 |
vpclmulqdq \$0x11,$Ii,$Xi,$Z3 |
| 872 |
vpxor $Z0,$Z1,$Z1 |
| 873 |
vpclmulqdq \$0x10,$HK,$T3,$Z2 |
| 874 |
vpxor $inout1,$Z3,$Z3 |
| 875 |
vpxor $T2,$Z2,$Z2 |
| 876 |
|
| 877 |
vpxor $Z1,$Z3,$Z0 # aggregated Karatsuba post-processing |
| 878 |
vpxor $Z0,$Z2,$Z2 |
| 879 |
vpslldq \$8,$Z2,$T1 |
| 880 |
vmovdqu 0x10($const),$Hkey # .Lpoly |
| 881 |
vpsrldq \$8,$Z2,$Z2 |
| 882 |
vpxor $T1,$Z1,$Xi |
| 883 |
vpxor $Z2,$Z3,$Z3 |
| 884 |
|
| 885 |
vpalignr \$8,$Xi,$Xi,$T2 # 1st phase |
| 886 |
vpclmulqdq \$0x10,$Hkey,$Xi,$Xi |
| 887 |
vpxor $T2,$Xi,$Xi |
| 888 |
|
| 889 |
vpalignr \$8,$Xi,$Xi,$T2 # 2nd phase |
| 890 |
vpclmulqdq \$0x10,$Hkey,$Xi,$Xi |
| 891 |
vpxor $Z3,$T2,$T2 |
| 892 |
vpxor $T2,$Xi,$Xi |
| 893 |
___ |
| 894 |
} |
| 895 |
$code.=<<___; |
| 896 |
vpshufb ($const),$Xi,$Xi # .Lbswap_mask |
| 897 |
vmovdqu $Xi,-0x40($Xip) # output Xi |
| 898 |
|
| 899 |
vzeroupper |
| 900 |
___ |
| 901 |
$code.=<<___ if ($win64); |
| 902 |
movaps -0xd8(%rax),%xmm6 |
| 903 |
movaps -0xc8(%rax),%xmm7 |
| 904 |
movaps -0xb8(%rax),%xmm8 |
| 905 |
movaps -0xa8(%rax),%xmm9 |
| 906 |
movaps -0x98(%rax),%xmm10 |
| 907 |
movaps -0x88(%rax),%xmm11 |
| 908 |
movaps -0x78(%rax),%xmm12 |
| 909 |
movaps -0x68(%rax),%xmm13 |
| 910 |
movaps -0x58(%rax),%xmm14 |
| 911 |
movaps -0x48(%rax),%xmm15 |
| 912 |
___ |
| 913 |
$code.=<<___; |
| 914 |
mov -48(%rax),%r15 |
| 915 |
mov -40(%rax),%r14 |
| 916 |
mov -32(%rax),%r13 |
| 917 |
mov -24(%rax),%r12 |
| 918 |
mov -16(%rax),%rbp |
| 919 |
mov -8(%rax),%rbx |
| 920 |
lea (%rax),%rsp # restore %rsp |
| 921 |
.Lgcm_enc_abort: |
| 922 |
mov $ret,%rax # return value |
| 923 |
ret |
| 924 |
.size aesni_gcm_encrypt,.-aesni_gcm_encrypt |
| 925 |
___ |
| 926 |
|
| 927 |
$code.=<<___; |
| 928 |
.align 64 |
| 929 |
.Lbswap_mask: |
| 930 |
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 |
| 931 |
.Lpoly: |
| 932 |
.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2 |
| 933 |
.Lone_msb: |
| 934 |
.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 |
| 935 |
.Ltwo_lsb: |
| 936 |
.byte 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 937 |
.Lone_lsb: |
| 938 |
.byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 939 |
.asciz "AES-NI GCM module for x86_64, CRYPTOGAMS by <appro\@openssl.org>" |
| 940 |
.align 64 |
| 941 |
___ |
| 942 |
if ($win64) { |
| 943 |
$rec="%rcx"; |
| 944 |
$frame="%rdx"; |
| 945 |
$context="%r8"; |
| 946 |
$disp="%r9"; |
| 947 |
|
| 948 |
$code.=<<___ |
| 949 |
.extern __imp_RtlVirtualUnwind |
| 950 |
.type gcm_se_handler,\@abi-omnipotent |
| 951 |
.align 16 |
| 952 |
gcm_se_handler: |
| 953 |
push %rsi |
| 954 |
push %rdi |
| 955 |
push %rbx |
| 956 |
push %rbp |
| 957 |
push %r12 |
| 958 |
push %r13 |
| 959 |
push %r14 |
| 960 |
push %r15 |
| 961 |
pushfq |
| 962 |
sub \$64,%rsp |
| 963 |
|
| 964 |
mov 120($context),%rax # pull context->Rax |
| 965 |
mov 248($context),%rbx # pull context->Rip |
| 966 |
|
| 967 |
mov 8($disp),%rsi # disp->ImageBase |
| 968 |
mov 56($disp),%r11 # disp->HandlerData |
| 969 |
|
| 970 |
mov 0(%r11),%r10d # HandlerData[0] |
| 971 |
lea (%rsi,%r10),%r10 # prologue label |
| 972 |
cmp %r10,%rbx # context->Rip<prologue label |
| 973 |
jb .Lcommon_seh_tail |
| 974 |
|
| 975 |
mov 152($context),%rax # pull context->Rsp |
| 976 |
|
| 977 |
mov 4(%r11),%r10d # HandlerData[1] |
| 978 |
lea (%rsi,%r10),%r10 # epilogue label |
| 979 |
cmp %r10,%rbx # context->Rip>=epilogue label |
| 980 |
jae .Lcommon_seh_tail |
| 981 |
|
| 982 |
mov 120($context),%rax # pull context->Rax |
| 983 |
|
| 984 |
mov -48(%rax),%r15 |
| 985 |
mov -40(%rax),%r14 |
| 986 |
mov -32(%rax),%r13 |
| 987 |
mov -24(%rax),%r12 |
| 988 |
mov -16(%rax),%rbp |
| 989 |
mov -8(%rax),%rbx |
| 990 |
mov %r15,240($context) |
| 991 |
mov %r14,232($context) |
| 992 |
mov %r13,224($context) |
| 993 |
mov %r12,216($context) |
| 994 |
mov %rbp,160($context) |
| 995 |
mov %rbx,144($context) |
| 996 |
|
| 997 |
lea -0xd8(%rax),%rsi # %xmm save area |
| 998 |
lea 512($context),%rdi # & context.Xmm6 |
| 999 |
mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax) |
| 1000 |
.long 0xa548f3fc # cld; rep movsq |
| 1001 |
|
| 1002 |
.Lcommon_seh_tail: |
| 1003 |
mov 8(%rax),%rdi |
| 1004 |
mov 16(%rax),%rsi |
| 1005 |
mov %rax,152($context) # restore context->Rsp |
| 1006 |
mov %rsi,168($context) # restore context->Rsi |
| 1007 |
mov %rdi,176($context) # restore context->Rdi |
| 1008 |
|
| 1009 |
mov 40($disp),%rdi # disp->ContextRecord |
| 1010 |
mov $context,%rsi # context |
| 1011 |
mov \$154,%ecx # sizeof(CONTEXT) |
| 1012 |
.long 0xa548f3fc # cld; rep movsq |
| 1013 |
|
| 1014 |
mov $disp,%rsi |
| 1015 |
xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER |
| 1016 |
mov 8(%rsi),%rdx # arg2, disp->ImageBase |
| 1017 |
mov 0(%rsi),%r8 # arg3, disp->ControlPc |
| 1018 |
mov 16(%rsi),%r9 # arg4, disp->FunctionEntry |
| 1019 |
mov 40(%rsi),%r10 # disp->ContextRecord |
| 1020 |
lea 56(%rsi),%r11 # &disp->HandlerData |
| 1021 |
lea 24(%rsi),%r12 # &disp->EstablisherFrame |
| 1022 |
mov %r10,32(%rsp) # arg5 |
| 1023 |
mov %r11,40(%rsp) # arg6 |
| 1024 |
mov %r12,48(%rsp) # arg7 |
| 1025 |
mov %rcx,56(%rsp) # arg8, (NULL) |
| 1026 |
call *__imp_RtlVirtualUnwind(%rip) |
| 1027 |
|
| 1028 |
mov \$1,%eax # ExceptionContinueSearch |
| 1029 |
add \$64,%rsp |
| 1030 |
popfq |
| 1031 |
pop %r15 |
| 1032 |
pop %r14 |
| 1033 |
pop %r13 |
| 1034 |
pop %r12 |
| 1035 |
pop %rbp |
| 1036 |
pop %rbx |
| 1037 |
pop %rdi |
| 1038 |
pop %rsi |
| 1039 |
ret |
| 1040 |
.size gcm_se_handler,.-gcm_se_handler |
| 1041 |
|
| 1042 |
.section .pdata |
| 1043 |
.align 4 |
| 1044 |
.rva .LSEH_begin_aesni_gcm_decrypt |
| 1045 |
.rva .LSEH_end_aesni_gcm_decrypt |
| 1046 |
.rva .LSEH_gcm_dec_info |
| 1047 |
|
| 1048 |
.rva .LSEH_begin_aesni_gcm_encrypt |
| 1049 |
.rva .LSEH_end_aesni_gcm_encrypt |
| 1050 |
.rva .LSEH_gcm_enc_info |
| 1051 |
.section .xdata |
| 1052 |
.align 8 |
| 1053 |
.LSEH_gcm_dec_info: |
| 1054 |
.byte 9,0,0,0 |
| 1055 |
.rva gcm_se_handler |
| 1056 |
.rva .Lgcm_dec_body,.Lgcm_dec_abort |
| 1057 |
.LSEH_gcm_enc_info: |
| 1058 |
.byte 9,0,0,0 |
| 1059 |
.rva gcm_se_handler |
| 1060 |
.rva .Lgcm_enc_body,.Lgcm_enc_abort |
| 1061 |
___ |
| 1062 |
} |
| 1063 |
}}} else {{{ |
| 1064 |
$code=<<___; # assembler is too old |
| 1065 |
.text |
| 1066 |
|
| 1067 |
.globl aesni_gcm_encrypt |
| 1068 |
.type aesni_gcm_encrypt,\@abi-omnipotent |
| 1069 |
aesni_gcm_encrypt: |
| 1070 |
xor %eax,%eax |
| 1071 |
ret |
| 1072 |
.size aesni_gcm_encrypt,.-aesni_gcm_encrypt |
| 1073 |
|
| 1074 |
.globl aesni_gcm_decrypt |
| 1075 |
.type aesni_gcm_decrypt,\@abi-omnipotent |
| 1076 |
aesni_gcm_decrypt: |
| 1077 |
xor %eax,%eax |
| 1078 |
ret |
| 1079 |
.size aesni_gcm_decrypt,.-aesni_gcm_decrypt |
| 1080 |
___ |
| 1081 |
}}} |
| 1082 |
|
| 1083 |
$code =~ s/\`([^\`]*)\`/eval($1)/gem; |
| 1084 |
|
| 1085 |
print $code; |
| 1086 |
|
| 1087 |
close STDOUT; |