25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

aesni-gcm-x86_64.pl 30 KiB

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