aesni-gcm-x86_64.pl 29 KB

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