vpaes-armv8.pl 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. ## Constant-time SSSE3 AES core implementation.
  10. ## version 0.1
  11. ##
  12. ## By Mike Hamburg (Stanford University), 2009
  13. ## Public domain.
  14. ##
  15. ## For details see http://shiftleft.org/papers/vector_aes/ and
  16. ## http://crypto.stanford.edu/vpaes/.
  17. ##
  18. ######################################################################
  19. # ARMv8 NEON adaptation by <appro@openssl.org>
  20. #
  21. # Reason for undertaken effort is that there is at least one popular
  22. # SoC based on Cortex-A53 that doesn't have crypto extensions.
  23. #
  24. # CBC enc ECB enc/dec(*) [bit-sliced enc/dec]
  25. # Cortex-A53 21.5 18.1/20.6 [17.5/19.8 ]
  26. # Cortex-A57 36.0(**) 20.4/24.9(**) [14.4/16.6 ]
  27. # X-Gene 45.9(**) 45.8/57.7(**) [33.1/37.6(**) ]
  28. # Denver(***) 16.6(**) 15.1/17.8(**) [8.80/9.93 ]
  29. # Apple A7(***) 22.7(**) 10.9/14.3 [8.45/10.0 ]
  30. # Mongoose(***) 26.3(**) 21.0/25.0(**) [13.3/16.8 ]
  31. #
  32. # (*) ECB denotes approximate result for parallelizable modes
  33. # such as CBC decrypt, CTR, etc.;
  34. # (**) these results are worse than scalar compiler-generated
  35. # code, but it's constant-time and therefore preferred;
  36. # (***) presented for reference/comparison purposes;
  37. $flavour = shift;
  38. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  39. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  40. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  41. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  42. die "can't locate arm-xlate.pl";
  43. open OUT,"| \"$^X\" $xlate $flavour $output";
  44. *STDOUT=*OUT;
  45. $code.=<<___;
  46. .text
  47. .type _vpaes_consts,%object
  48. .align 7 // totally strategic alignment
  49. _vpaes_consts:
  50. .Lk_mc_forward: // mc_forward
  51. .quad 0x0407060500030201, 0x0C0F0E0D080B0A09
  52. .quad 0x080B0A0904070605, 0x000302010C0F0E0D
  53. .quad 0x0C0F0E0D080B0A09, 0x0407060500030201
  54. .quad 0x000302010C0F0E0D, 0x080B0A0904070605
  55. .Lk_mc_backward:// mc_backward
  56. .quad 0x0605040702010003, 0x0E0D0C0F0A09080B
  57. .quad 0x020100030E0D0C0F, 0x0A09080B06050407
  58. .quad 0x0E0D0C0F0A09080B, 0x0605040702010003
  59. .quad 0x0A09080B06050407, 0x020100030E0D0C0F
  60. .Lk_sr: // sr
  61. .quad 0x0706050403020100, 0x0F0E0D0C0B0A0908
  62. .quad 0x030E09040F0A0500, 0x0B06010C07020D08
  63. .quad 0x0F060D040B020900, 0x070E050C030A0108
  64. .quad 0x0B0E0104070A0D00, 0x0306090C0F020508
  65. //
  66. // "Hot" constants
  67. //
  68. .Lk_inv: // inv, inva
  69. .quad 0x0E05060F0D080180, 0x040703090A0B0C02
  70. .quad 0x01040A060F0B0780, 0x030D0E0C02050809
  71. .Lk_ipt: // input transform (lo, hi)
  72. .quad 0xC2B2E8985A2A7000, 0xCABAE09052227808
  73. .quad 0x4C01307D317C4D00, 0xCD80B1FCB0FDCC81
  74. .Lk_sbo: // sbou, sbot
  75. .quad 0xD0D26D176FBDC700, 0x15AABF7AC502A878
  76. .quad 0xCFE474A55FBB6A00, 0x8E1E90D1412B35FA
  77. .Lk_sb1: // sb1u, sb1t
  78. .quad 0x3618D415FAE22300, 0x3BF7CCC10D2ED9EF
  79. .quad 0xB19BE18FCB503E00, 0xA5DF7A6E142AF544
  80. .Lk_sb2: // sb2u, sb2t
  81. .quad 0x69EB88400AE12900, 0xC2A163C8AB82234A
  82. .quad 0xE27A93C60B712400, 0x5EB7E955BC982FCD
  83. //
  84. // Decryption stuff
  85. //
  86. .Lk_dipt: // decryption input transform
  87. .quad 0x0F505B040B545F00, 0x154A411E114E451A
  88. .quad 0x86E383E660056500, 0x12771772F491F194
  89. .Lk_dsbo: // decryption sbox final output
  90. .quad 0x1387EA537EF94000, 0xC7AA6DB9D4943E2D
  91. .quad 0x12D7560F93441D00, 0xCA4B8159D8C58E9C
  92. .Lk_dsb9: // decryption sbox output *9*u, *9*t
  93. .quad 0x851C03539A86D600, 0xCAD51F504F994CC9
  94. .quad 0xC03B1789ECD74900, 0x725E2C9EB2FBA565
  95. .Lk_dsbd: // decryption sbox output *D*u, *D*t
  96. .quad 0x7D57CCDFE6B1A200, 0xF56E9B13882A4439
  97. .quad 0x3CE2FAF724C6CB00, 0x2931180D15DEEFD3
  98. .Lk_dsbb: // decryption sbox output *B*u, *B*t
  99. .quad 0xD022649296B44200, 0x602646F6B0F2D404
  100. .quad 0xC19498A6CD596700, 0xF3FF0C3E3255AA6B
  101. .Lk_dsbe: // decryption sbox output *E*u, *E*t
  102. .quad 0x46F2929626D4D000, 0x2242600464B4F6B0
  103. .quad 0x0C55A6CDFFAAC100, 0x9467F36B98593E32
  104. //
  105. // Key schedule constants
  106. //
  107. .Lk_dksd: // decryption key schedule: invskew x*D
  108. .quad 0xFEB91A5DA3E44700, 0x0740E3A45A1DBEF9
  109. .quad 0x41C277F4B5368300, 0x5FDC69EAAB289D1E
  110. .Lk_dksb: // decryption key schedule: invskew x*B
  111. .quad 0x9A4FCA1F8550D500, 0x03D653861CC94C99
  112. .quad 0x115BEDA7B6FC4A00, 0xD993256F7E3482C8
  113. .Lk_dkse: // decryption key schedule: invskew x*E + 0x63
  114. .quad 0xD5031CCA1FC9D600, 0x53859A4C994F5086
  115. .quad 0xA23196054FDC7BE8, 0xCD5EF96A20B31487
  116. .Lk_dks9: // decryption key schedule: invskew x*9
  117. .quad 0xB6116FC87ED9A700, 0x4AED933482255BFC
  118. .quad 0x4576516227143300, 0x8BB89FACE9DAFDCE
  119. .Lk_rcon: // rcon
  120. .quad 0x1F8391B9AF9DEEB6, 0x702A98084D7C7D81
  121. .Lk_opt: // output transform
  122. .quad 0xFF9F4929D6B66000, 0xF7974121DEBE6808
  123. .quad 0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0
  124. .Lk_deskew: // deskew tables: inverts the sbox's "skew"
  125. .quad 0x07E4A34047A4E300, 0x1DFEB95A5DBEF91A
  126. .quad 0x5F36B5DC83EA6900, 0x2841C2ABF49D1E77
  127. .asciz "Vector Permutation AES for ARMv8, Mike Hamburg (Stanford University)"
  128. .size _vpaes_consts,.-_vpaes_consts
  129. .align 6
  130. ___
  131. {
  132. my ($inp,$out,$key) = map("x$_",(0..2));
  133. my ($invlo,$invhi,$iptlo,$ipthi,$sbou,$sbot) = map("v$_.16b",(18..23));
  134. my ($sb1u,$sb1t,$sb2u,$sb2t) = map("v$_.16b",(24..27));
  135. my ($sb9u,$sb9t,$sbdu,$sbdt,$sbbu,$sbbt,$sbeu,$sbet)=map("v$_.16b",(24..31));
  136. $code.=<<___;
  137. ##
  138. ## _aes_preheat
  139. ##
  140. ## Fills register %r10 -> .aes_consts (so you can -fPIC)
  141. ## and %xmm9-%xmm15 as specified below.
  142. ##
  143. .type _vpaes_encrypt_preheat,%function
  144. .align 4
  145. _vpaes_encrypt_preheat:
  146. adr x10, .Lk_inv
  147. movi v17.16b, #0x0f
  148. ld1 {v18.2d-v19.2d}, [x10],#32 // .Lk_inv
  149. ld1 {v20.2d-v23.2d}, [x10],#64 // .Lk_ipt, .Lk_sbo
  150. ld1 {v24.2d-v27.2d}, [x10] // .Lk_sb1, .Lk_sb2
  151. ret
  152. .size _vpaes_encrypt_preheat,.-_vpaes_encrypt_preheat
  153. ##
  154. ## _aes_encrypt_core
  155. ##
  156. ## AES-encrypt %xmm0.
  157. ##
  158. ## Inputs:
  159. ## %xmm0 = input
  160. ## %xmm9-%xmm15 as in _vpaes_preheat
  161. ## (%rdx) = scheduled keys
  162. ##
  163. ## Output in %xmm0
  164. ## Clobbers %xmm1-%xmm5, %r9, %r10, %r11, %rax
  165. ## Preserves %xmm6 - %xmm8 so you get some local vectors
  166. ##
  167. ##
  168. .type _vpaes_encrypt_core,%function
  169. .align 4
  170. _vpaes_encrypt_core:
  171. mov x9, $key
  172. ldr w8, [$key,#240] // pull rounds
  173. adr x11, .Lk_mc_forward+16
  174. // vmovdqa .Lk_ipt(%rip), %xmm2 # iptlo
  175. ld1 {v16.2d}, [x9], #16 // vmovdqu (%r9), %xmm5 # round0 key
  176. and v1.16b, v7.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1
  177. ushr v0.16b, v7.16b, #4 // vpsrlb \$4, %xmm0, %xmm0
  178. tbl v1.16b, {$iptlo}, v1.16b // vpshufb %xmm1, %xmm2, %xmm1
  179. // vmovdqa .Lk_ipt+16(%rip), %xmm3 # ipthi
  180. tbl v2.16b, {$ipthi}, v0.16b // vpshufb %xmm0, %xmm3, %xmm2
  181. eor v0.16b, v1.16b, v16.16b // vpxor %xmm5, %xmm1, %xmm0
  182. eor v0.16b, v0.16b, v2.16b // vpxor %xmm2, %xmm0, %xmm0
  183. b .Lenc_entry
  184. .align 4
  185. .Lenc_loop:
  186. // middle of middle round
  187. add x10, x11, #0x40
  188. tbl v4.16b, {$sb1t}, v2.16b // vpshufb %xmm2, %xmm13, %xmm4 # 4 = sb1u
  189. ld1 {v1.2d}, [x11], #16 // vmovdqa -0x40(%r11,%r10), %xmm1 # .Lk_mc_forward[]
  190. tbl v0.16b, {$sb1u}, v3.16b // vpshufb %xmm3, %xmm12, %xmm0 # 0 = sb1t
  191. eor v4.16b, v4.16b, v16.16b // vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k
  192. tbl v5.16b, {$sb2t}, v2.16b // vpshufb %xmm2, %xmm15, %xmm5 # 4 = sb2u
  193. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 0 = A
  194. tbl v2.16b, {$sb2u}, v3.16b // vpshufb %xmm3, %xmm14, %xmm2 # 2 = sb2t
  195. ld1 {v4.2d}, [x10] // vmovdqa (%r11,%r10), %xmm4 # .Lk_mc_backward[]
  196. tbl v3.16b, {v0.16b}, v1.16b // vpshufb %xmm1, %xmm0, %xmm3 # 0 = B
  197. eor v2.16b, v2.16b, v5.16b // vpxor %xmm5, %xmm2, %xmm2 # 2 = 2A
  198. tbl v0.16b, {v0.16b}, v4.16b // vpshufb %xmm4, %xmm0, %xmm0 # 3 = D
  199. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3 # 0 = 2A+B
  200. tbl v4.16b, {v3.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm4 # 0 = 2B+C
  201. eor v0.16b, v0.16b, v3.16b // vpxor %xmm3, %xmm0, %xmm0 # 3 = 2A+B+D
  202. and x11, x11, #~(1<<6) // and \$0x30, %r11 # ... mod 4
  203. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 0 = 2A+3B+C+D
  204. sub w8, w8, #1 // nr--
  205. .Lenc_entry:
  206. // top of round
  207. and v1.16b, v0.16b, v17.16b // vpand %xmm0, %xmm9, %xmm1 # 0 = k
  208. ushr v0.16b, v0.16b, #4 // vpsrlb \$4, %xmm0, %xmm0 # 1 = i
  209. tbl v5.16b, {$invhi}, v1.16b // vpshufb %xmm1, %xmm11, %xmm5 # 2 = a/k
  210. eor v1.16b, v1.16b, v0.16b // vpxor %xmm0, %xmm1, %xmm1 # 0 = j
  211. tbl v3.16b, {$invlo}, v0.16b // vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i
  212. tbl v4.16b, {$invlo}, v1.16b // vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j
  213. eor v3.16b, v3.16b, v5.16b // vpxor %xmm5, %xmm3, %xmm3 # 3 = iak = 1/i + a/k
  214. eor v4.16b, v4.16b, v5.16b // vpxor %xmm5, %xmm4, %xmm4 # 4 = jak = 1/j + a/k
  215. tbl v2.16b, {$invlo}, v3.16b // vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak
  216. tbl v3.16b, {$invlo}, v4.16b // vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak
  217. eor v2.16b, v2.16b, v1.16b // vpxor %xmm1, %xmm2, %xmm2 # 2 = io
  218. eor v3.16b, v3.16b, v0.16b // vpxor %xmm0, %xmm3, %xmm3 # 3 = jo
  219. ld1 {v16.2d}, [x9],#16 // vmovdqu (%r9), %xmm5
  220. cbnz w8, .Lenc_loop
  221. // middle of last round
  222. add x10, x11, #0x80
  223. // vmovdqa -0x60(%r10), %xmm4 # 3 : sbou .Lk_sbo
  224. // vmovdqa -0x50(%r10), %xmm0 # 0 : sbot .Lk_sbo+16
  225. tbl v4.16b, {$sbou}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou
  226. ld1 {v1.2d}, [x10] // vmovdqa 0x40(%r11,%r10), %xmm1 # .Lk_sr[]
  227. tbl v0.16b, {$sbot}, v3.16b // vpshufb %xmm3, %xmm0, %xmm0 # 0 = sb1t
  228. eor v4.16b, v4.16b, v16.16b // vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k
  229. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 0 = A
  230. tbl v0.16b, {v0.16b}, v1.16b // vpshufb %xmm1, %xmm0, %xmm0
  231. ret
  232. .size _vpaes_encrypt_core,.-_vpaes_encrypt_core
  233. .globl vpaes_encrypt
  234. .type vpaes_encrypt,%function
  235. .align 4
  236. vpaes_encrypt:
  237. .inst 0xd503233f // paciasp
  238. stp x29,x30,[sp,#-16]!
  239. add x29,sp,#0
  240. ld1 {v7.16b}, [$inp]
  241. bl _vpaes_encrypt_preheat
  242. bl _vpaes_encrypt_core
  243. st1 {v0.16b}, [$out]
  244. ldp x29,x30,[sp],#16
  245. .inst 0xd50323bf // autiasp
  246. ret
  247. .size vpaes_encrypt,.-vpaes_encrypt
  248. .type _vpaes_encrypt_2x,%function
  249. .align 4
  250. _vpaes_encrypt_2x:
  251. mov x9, $key
  252. ldr w8, [$key,#240] // pull rounds
  253. adr x11, .Lk_mc_forward+16
  254. // vmovdqa .Lk_ipt(%rip), %xmm2 # iptlo
  255. ld1 {v16.2d}, [x9], #16 // vmovdqu (%r9), %xmm5 # round0 key
  256. and v1.16b, v14.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1
  257. ushr v0.16b, v14.16b, #4 // vpsrlb \$4, %xmm0, %xmm0
  258. and v9.16b, v15.16b, v17.16b
  259. ushr v8.16b, v15.16b, #4
  260. tbl v1.16b, {$iptlo}, v1.16b // vpshufb %xmm1, %xmm2, %xmm1
  261. tbl v9.16b, {$iptlo}, v9.16b
  262. // vmovdqa .Lk_ipt+16(%rip), %xmm3 # ipthi
  263. tbl v2.16b, {$ipthi}, v0.16b // vpshufb %xmm0, %xmm3, %xmm2
  264. tbl v10.16b, {$ipthi}, v8.16b
  265. eor v0.16b, v1.16b, v16.16b // vpxor %xmm5, %xmm1, %xmm0
  266. eor v8.16b, v9.16b, v16.16b
  267. eor v0.16b, v0.16b, v2.16b // vpxor %xmm2, %xmm0, %xmm0
  268. eor v8.16b, v8.16b, v10.16b
  269. b .Lenc_2x_entry
  270. .align 4
  271. .Lenc_2x_loop:
  272. // middle of middle round
  273. add x10, x11, #0x40
  274. tbl v4.16b, {$sb1t}, v2.16b // vpshufb %xmm2, %xmm13, %xmm4 # 4 = sb1u
  275. tbl v12.16b, {$sb1t}, v10.16b
  276. ld1 {v1.2d}, [x11], #16 // vmovdqa -0x40(%r11,%r10), %xmm1 # .Lk_mc_forward[]
  277. tbl v0.16b, {$sb1u}, v3.16b // vpshufb %xmm3, %xmm12, %xmm0 # 0 = sb1t
  278. tbl v8.16b, {$sb1u}, v11.16b
  279. eor v4.16b, v4.16b, v16.16b // vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k
  280. eor v12.16b, v12.16b, v16.16b
  281. tbl v5.16b, {$sb2t}, v2.16b // vpshufb %xmm2, %xmm15, %xmm5 # 4 = sb2u
  282. tbl v13.16b, {$sb2t}, v10.16b
  283. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 0 = A
  284. eor v8.16b, v8.16b, v12.16b
  285. tbl v2.16b, {$sb2u}, v3.16b // vpshufb %xmm3, %xmm14, %xmm2 # 2 = sb2t
  286. tbl v10.16b, {$sb2u}, v11.16b
  287. ld1 {v4.2d}, [x10] // vmovdqa (%r11,%r10), %xmm4 # .Lk_mc_backward[]
  288. tbl v3.16b, {v0.16b}, v1.16b // vpshufb %xmm1, %xmm0, %xmm3 # 0 = B
  289. tbl v11.16b, {v8.16b}, v1.16b
  290. eor v2.16b, v2.16b, v5.16b // vpxor %xmm5, %xmm2, %xmm2 # 2 = 2A
  291. eor v10.16b, v10.16b, v13.16b
  292. tbl v0.16b, {v0.16b}, v4.16b // vpshufb %xmm4, %xmm0, %xmm0 # 3 = D
  293. tbl v8.16b, {v8.16b}, v4.16b
  294. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3 # 0 = 2A+B
  295. eor v11.16b, v11.16b, v10.16b
  296. tbl v4.16b, {v3.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm4 # 0 = 2B+C
  297. tbl v12.16b, {v11.16b},v1.16b
  298. eor v0.16b, v0.16b, v3.16b // vpxor %xmm3, %xmm0, %xmm0 # 3 = 2A+B+D
  299. eor v8.16b, v8.16b, v11.16b
  300. and x11, x11, #~(1<<6) // and \$0x30, %r11 # ... mod 4
  301. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 0 = 2A+3B+C+D
  302. eor v8.16b, v8.16b, v12.16b
  303. sub w8, w8, #1 // nr--
  304. .Lenc_2x_entry:
  305. // top of round
  306. and v1.16b, v0.16b, v17.16b // vpand %xmm0, %xmm9, %xmm1 # 0 = k
  307. ushr v0.16b, v0.16b, #4 // vpsrlb \$4, %xmm0, %xmm0 # 1 = i
  308. and v9.16b, v8.16b, v17.16b
  309. ushr v8.16b, v8.16b, #4
  310. tbl v5.16b, {$invhi},v1.16b // vpshufb %xmm1, %xmm11, %xmm5 # 2 = a/k
  311. tbl v13.16b, {$invhi},v9.16b
  312. eor v1.16b, v1.16b, v0.16b // vpxor %xmm0, %xmm1, %xmm1 # 0 = j
  313. eor v9.16b, v9.16b, v8.16b
  314. tbl v3.16b, {$invlo},v0.16b // vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i
  315. tbl v11.16b, {$invlo},v8.16b
  316. tbl v4.16b, {$invlo},v1.16b // vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j
  317. tbl v12.16b, {$invlo},v9.16b
  318. eor v3.16b, v3.16b, v5.16b // vpxor %xmm5, %xmm3, %xmm3 # 3 = iak = 1/i + a/k
  319. eor v11.16b, v11.16b, v13.16b
  320. eor v4.16b, v4.16b, v5.16b // vpxor %xmm5, %xmm4, %xmm4 # 4 = jak = 1/j + a/k
  321. eor v12.16b, v12.16b, v13.16b
  322. tbl v2.16b, {$invlo},v3.16b // vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak
  323. tbl v10.16b, {$invlo},v11.16b
  324. tbl v3.16b, {$invlo},v4.16b // vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak
  325. tbl v11.16b, {$invlo},v12.16b
  326. eor v2.16b, v2.16b, v1.16b // vpxor %xmm1, %xmm2, %xmm2 # 2 = io
  327. eor v10.16b, v10.16b, v9.16b
  328. eor v3.16b, v3.16b, v0.16b // vpxor %xmm0, %xmm3, %xmm3 # 3 = jo
  329. eor v11.16b, v11.16b, v8.16b
  330. ld1 {v16.2d}, [x9],#16 // vmovdqu (%r9), %xmm5
  331. cbnz w8, .Lenc_2x_loop
  332. // middle of last round
  333. add x10, x11, #0x80
  334. // vmovdqa -0x60(%r10), %xmm4 # 3 : sbou .Lk_sbo
  335. // vmovdqa -0x50(%r10), %xmm0 # 0 : sbot .Lk_sbo+16
  336. tbl v4.16b, {$sbou}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou
  337. tbl v12.16b, {$sbou}, v10.16b
  338. ld1 {v1.2d}, [x10] // vmovdqa 0x40(%r11,%r10), %xmm1 # .Lk_sr[]
  339. tbl v0.16b, {$sbot}, v3.16b // vpshufb %xmm3, %xmm0, %xmm0 # 0 = sb1t
  340. tbl v8.16b, {$sbot}, v11.16b
  341. eor v4.16b, v4.16b, v16.16b // vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k
  342. eor v12.16b, v12.16b, v16.16b
  343. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 0 = A
  344. eor v8.16b, v8.16b, v12.16b
  345. tbl v0.16b, {v0.16b},v1.16b // vpshufb %xmm1, %xmm0, %xmm0
  346. tbl v1.16b, {v8.16b},v1.16b
  347. ret
  348. .size _vpaes_encrypt_2x,.-_vpaes_encrypt_2x
  349. .type _vpaes_decrypt_preheat,%function
  350. .align 4
  351. _vpaes_decrypt_preheat:
  352. adr x10, .Lk_inv
  353. movi v17.16b, #0x0f
  354. adr x11, .Lk_dipt
  355. ld1 {v18.2d-v19.2d}, [x10],#32 // .Lk_inv
  356. ld1 {v20.2d-v23.2d}, [x11],#64 // .Lk_dipt, .Lk_dsbo
  357. ld1 {v24.2d-v27.2d}, [x11],#64 // .Lk_dsb9, .Lk_dsbd
  358. ld1 {v28.2d-v31.2d}, [x11] // .Lk_dsbb, .Lk_dsbe
  359. ret
  360. .size _vpaes_decrypt_preheat,.-_vpaes_decrypt_preheat
  361. ##
  362. ## Decryption core
  363. ##
  364. ## Same API as encryption core.
  365. ##
  366. .type _vpaes_decrypt_core,%function
  367. .align 4
  368. _vpaes_decrypt_core:
  369. mov x9, $key
  370. ldr w8, [$key,#240] // pull rounds
  371. // vmovdqa .Lk_dipt(%rip), %xmm2 # iptlo
  372. lsl x11, x8, #4 // mov %rax, %r11; shl \$4, %r11
  373. eor x11, x11, #0x30 // xor \$0x30, %r11
  374. adr x10, .Lk_sr
  375. and x11, x11, #0x30 // and \$0x30, %r11
  376. add x11, x11, x10
  377. adr x10, .Lk_mc_forward+48
  378. ld1 {v16.2d}, [x9],#16 // vmovdqu (%r9), %xmm4 # round0 key
  379. and v1.16b, v7.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1
  380. ushr v0.16b, v7.16b, #4 // vpsrlb \$4, %xmm0, %xmm0
  381. tbl v2.16b, {$iptlo}, v1.16b // vpshufb %xmm1, %xmm2, %xmm2
  382. ld1 {v5.2d}, [x10] // vmovdqa .Lk_mc_forward+48(%rip), %xmm5
  383. // vmovdqa .Lk_dipt+16(%rip), %xmm1 # ipthi
  384. tbl v0.16b, {$ipthi}, v0.16b // vpshufb %xmm0, %xmm1, %xmm0
  385. eor v2.16b, v2.16b, v16.16b // vpxor %xmm4, %xmm2, %xmm2
  386. eor v0.16b, v0.16b, v2.16b // vpxor %xmm2, %xmm0, %xmm0
  387. b .Ldec_entry
  388. .align 4
  389. .Ldec_loop:
  390. //
  391. // Inverse mix columns
  392. //
  393. // vmovdqa -0x20(%r10),%xmm4 # 4 : sb9u
  394. // vmovdqa -0x10(%r10),%xmm1 # 0 : sb9t
  395. tbl v4.16b, {$sb9u}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sb9u
  396. tbl v1.16b, {$sb9t}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb9t
  397. eor v0.16b, v4.16b, v16.16b // vpxor %xmm4, %xmm0, %xmm0
  398. // vmovdqa 0x00(%r10),%xmm4 # 4 : sbdu
  399. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  400. // vmovdqa 0x10(%r10),%xmm1 # 0 : sbdt
  401. tbl v4.16b, {$sbdu}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbdu
  402. tbl v0.16b, {v0.16b}, v5.16b // vpshufb %xmm5, %xmm0, %xmm0 # MC ch
  403. tbl v1.16b, {$sbdt}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbdt
  404. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 4 = ch
  405. // vmovdqa 0x20(%r10), %xmm4 # 4 : sbbu
  406. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  407. // vmovdqa 0x30(%r10), %xmm1 # 0 : sbbt
  408. tbl v4.16b, {$sbbu}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbbu
  409. tbl v0.16b, {v0.16b}, v5.16b // vpshufb %xmm5, %xmm0, %xmm0 # MC ch
  410. tbl v1.16b, {$sbbt}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbbt
  411. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 4 = ch
  412. // vmovdqa 0x40(%r10), %xmm4 # 4 : sbeu
  413. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  414. // vmovdqa 0x50(%r10), %xmm1 # 0 : sbet
  415. tbl v4.16b, {$sbeu}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbeu
  416. tbl v0.16b, {v0.16b}, v5.16b // vpshufb %xmm5, %xmm0, %xmm0 # MC ch
  417. tbl v1.16b, {$sbet}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbet
  418. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 4 = ch
  419. ext v5.16b, v5.16b, v5.16b, #12 // vpalignr \$12, %xmm5, %xmm5, %xmm5
  420. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  421. sub w8, w8, #1 // sub \$1,%rax # nr--
  422. .Ldec_entry:
  423. // top of round
  424. and v1.16b, v0.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1 # 0 = k
  425. ushr v0.16b, v0.16b, #4 // vpsrlb \$4, %xmm0, %xmm0 # 1 = i
  426. tbl v2.16b, {$invhi}, v1.16b // vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k
  427. eor v1.16b, v1.16b, v0.16b // vpxor %xmm0, %xmm1, %xmm1 # 0 = j
  428. tbl v3.16b, {$invlo}, v0.16b // vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i
  429. tbl v4.16b, {$invlo}, v1.16b // vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j
  430. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k
  431. eor v4.16b, v4.16b, v2.16b // vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k
  432. tbl v2.16b, {$invlo}, v3.16b // vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak
  433. tbl v3.16b, {$invlo}, v4.16b // vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak
  434. eor v2.16b, v2.16b, v1.16b // vpxor %xmm1, %xmm2, %xmm2 # 2 = io
  435. eor v3.16b, v3.16b, v0.16b // vpxor %xmm0, %xmm3, %xmm3 # 3 = jo
  436. ld1 {v16.2d}, [x9],#16 // vmovdqu (%r9), %xmm0
  437. cbnz w8, .Ldec_loop
  438. // middle of last round
  439. // vmovdqa 0x60(%r10), %xmm4 # 3 : sbou
  440. tbl v4.16b, {$sbou}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou
  441. // vmovdqa 0x70(%r10), %xmm1 # 0 : sbot
  442. ld1 {v2.2d}, [x11] // vmovdqa -0x160(%r11), %xmm2 # .Lk_sr-.Lk_dsbd=-0x160
  443. tbl v1.16b, {$sbot}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb1t
  444. eor v4.16b, v4.16b, v16.16b // vpxor %xmm0, %xmm4, %xmm4 # 4 = sb1u + k
  445. eor v0.16b, v1.16b, v4.16b // vpxor %xmm4, %xmm1, %xmm0 # 0 = A
  446. tbl v0.16b, {v0.16b}, v2.16b // vpshufb %xmm2, %xmm0, %xmm0
  447. ret
  448. .size _vpaes_decrypt_core,.-_vpaes_decrypt_core
  449. .globl vpaes_decrypt
  450. .type vpaes_decrypt,%function
  451. .align 4
  452. vpaes_decrypt:
  453. .inst 0xd503233f // paciasp
  454. stp x29,x30,[sp,#-16]!
  455. add x29,sp,#0
  456. ld1 {v7.16b}, [$inp]
  457. bl _vpaes_decrypt_preheat
  458. bl _vpaes_decrypt_core
  459. st1 {v0.16b}, [$out]
  460. ldp x29,x30,[sp],#16
  461. .inst 0xd50323bf // autiasp
  462. ret
  463. .size vpaes_decrypt,.-vpaes_decrypt
  464. // v14-v15 input, v0-v1 output
  465. .type _vpaes_decrypt_2x,%function
  466. .align 4
  467. _vpaes_decrypt_2x:
  468. mov x9, $key
  469. ldr w8, [$key,#240] // pull rounds
  470. // vmovdqa .Lk_dipt(%rip), %xmm2 # iptlo
  471. lsl x11, x8, #4 // mov %rax, %r11; shl \$4, %r11
  472. eor x11, x11, #0x30 // xor \$0x30, %r11
  473. adr x10, .Lk_sr
  474. and x11, x11, #0x30 // and \$0x30, %r11
  475. add x11, x11, x10
  476. adr x10, .Lk_mc_forward+48
  477. ld1 {v16.2d}, [x9],#16 // vmovdqu (%r9), %xmm4 # round0 key
  478. and v1.16b, v14.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1
  479. ushr v0.16b, v14.16b, #4 // vpsrlb \$4, %xmm0, %xmm0
  480. and v9.16b, v15.16b, v17.16b
  481. ushr v8.16b, v15.16b, #4
  482. tbl v2.16b, {$iptlo},v1.16b // vpshufb %xmm1, %xmm2, %xmm2
  483. tbl v10.16b, {$iptlo},v9.16b
  484. ld1 {v5.2d}, [x10] // vmovdqa .Lk_mc_forward+48(%rip), %xmm5
  485. // vmovdqa .Lk_dipt+16(%rip), %xmm1 # ipthi
  486. tbl v0.16b, {$ipthi},v0.16b // vpshufb %xmm0, %xmm1, %xmm0
  487. tbl v8.16b, {$ipthi},v8.16b
  488. eor v2.16b, v2.16b, v16.16b // vpxor %xmm4, %xmm2, %xmm2
  489. eor v10.16b, v10.16b, v16.16b
  490. eor v0.16b, v0.16b, v2.16b // vpxor %xmm2, %xmm0, %xmm0
  491. eor v8.16b, v8.16b, v10.16b
  492. b .Ldec_2x_entry
  493. .align 4
  494. .Ldec_2x_loop:
  495. //
  496. // Inverse mix columns
  497. //
  498. // vmovdqa -0x20(%r10),%xmm4 # 4 : sb9u
  499. // vmovdqa -0x10(%r10),%xmm1 # 0 : sb9t
  500. tbl v4.16b, {$sb9u}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sb9u
  501. tbl v12.16b, {$sb9u}, v10.16b
  502. tbl v1.16b, {$sb9t}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb9t
  503. tbl v9.16b, {$sb9t}, v11.16b
  504. eor v0.16b, v4.16b, v16.16b // vpxor %xmm4, %xmm0, %xmm0
  505. eor v8.16b, v12.16b, v16.16b
  506. // vmovdqa 0x00(%r10),%xmm4 # 4 : sbdu
  507. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  508. eor v8.16b, v8.16b, v9.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  509. // vmovdqa 0x10(%r10),%xmm1 # 0 : sbdt
  510. tbl v4.16b, {$sbdu}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbdu
  511. tbl v12.16b, {$sbdu}, v10.16b
  512. tbl v0.16b, {v0.16b},v5.16b // vpshufb %xmm5, %xmm0, %xmm0 # MC ch
  513. tbl v8.16b, {v8.16b},v5.16b
  514. tbl v1.16b, {$sbdt}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbdt
  515. tbl v9.16b, {$sbdt}, v11.16b
  516. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 4 = ch
  517. eor v8.16b, v8.16b, v12.16b
  518. // vmovdqa 0x20(%r10), %xmm4 # 4 : sbbu
  519. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  520. eor v8.16b, v8.16b, v9.16b
  521. // vmovdqa 0x30(%r10), %xmm1 # 0 : sbbt
  522. tbl v4.16b, {$sbbu}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbbu
  523. tbl v12.16b, {$sbbu}, v10.16b
  524. tbl v0.16b, {v0.16b},v5.16b // vpshufb %xmm5, %xmm0, %xmm0 # MC ch
  525. tbl v8.16b, {v8.16b},v5.16b
  526. tbl v1.16b, {$sbbt}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbbt
  527. tbl v9.16b, {$sbbt}, v11.16b
  528. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 4 = ch
  529. eor v8.16b, v8.16b, v12.16b
  530. // vmovdqa 0x40(%r10), %xmm4 # 4 : sbeu
  531. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  532. eor v8.16b, v8.16b, v9.16b
  533. // vmovdqa 0x50(%r10), %xmm1 # 0 : sbet
  534. tbl v4.16b, {$sbeu}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbeu
  535. tbl v12.16b, {$sbeu}, v10.16b
  536. tbl v0.16b, {v0.16b},v5.16b // vpshufb %xmm5, %xmm0, %xmm0 # MC ch
  537. tbl v8.16b, {v8.16b},v5.16b
  538. tbl v1.16b, {$sbet}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbet
  539. tbl v9.16b, {$sbet}, v11.16b
  540. eor v0.16b, v0.16b, v4.16b // vpxor %xmm4, %xmm0, %xmm0 # 4 = ch
  541. eor v8.16b, v8.16b, v12.16b
  542. ext v5.16b, v5.16b, v5.16b, #12 // vpalignr \$12, %xmm5, %xmm5, %xmm5
  543. eor v0.16b, v0.16b, v1.16b // vpxor %xmm1, %xmm0, %xmm0 # 0 = ch
  544. eor v8.16b, v8.16b, v9.16b
  545. sub w8, w8, #1 // sub \$1,%rax # nr--
  546. .Ldec_2x_entry:
  547. // top of round
  548. and v1.16b, v0.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1 # 0 = k
  549. ushr v0.16b, v0.16b, #4 // vpsrlb \$4, %xmm0, %xmm0 # 1 = i
  550. and v9.16b, v8.16b, v17.16b
  551. ushr v8.16b, v8.16b, #4
  552. tbl v2.16b, {$invhi},v1.16b // vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k
  553. tbl v10.16b, {$invhi},v9.16b
  554. eor v1.16b, v1.16b, v0.16b // vpxor %xmm0, %xmm1, %xmm1 # 0 = j
  555. eor v9.16b, v9.16b, v8.16b
  556. tbl v3.16b, {$invlo},v0.16b // vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i
  557. tbl v11.16b, {$invlo},v8.16b
  558. tbl v4.16b, {$invlo},v1.16b // vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j
  559. tbl v12.16b, {$invlo},v9.16b
  560. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k
  561. eor v11.16b, v11.16b, v10.16b
  562. eor v4.16b, v4.16b, v2.16b // vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k
  563. eor v12.16b, v12.16b, v10.16b
  564. tbl v2.16b, {$invlo},v3.16b // vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak
  565. tbl v10.16b, {$invlo},v11.16b
  566. tbl v3.16b, {$invlo},v4.16b // vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak
  567. tbl v11.16b, {$invlo},v12.16b
  568. eor v2.16b, v2.16b, v1.16b // vpxor %xmm1, %xmm2, %xmm2 # 2 = io
  569. eor v10.16b, v10.16b, v9.16b
  570. eor v3.16b, v3.16b, v0.16b // vpxor %xmm0, %xmm3, %xmm3 # 3 = jo
  571. eor v11.16b, v11.16b, v8.16b
  572. ld1 {v16.2d}, [x9],#16 // vmovdqu (%r9), %xmm0
  573. cbnz w8, .Ldec_2x_loop
  574. // middle of last round
  575. // vmovdqa 0x60(%r10), %xmm4 # 3 : sbou
  576. tbl v4.16b, {$sbou}, v2.16b // vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou
  577. tbl v12.16b, {$sbou}, v10.16b
  578. // vmovdqa 0x70(%r10), %xmm1 # 0 : sbot
  579. tbl v1.16b, {$sbot}, v3.16b // vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb1t
  580. tbl v9.16b, {$sbot}, v11.16b
  581. ld1 {v2.2d}, [x11] // vmovdqa -0x160(%r11), %xmm2 # .Lk_sr-.Lk_dsbd=-0x160
  582. eor v4.16b, v4.16b, v16.16b // vpxor %xmm0, %xmm4, %xmm4 # 4 = sb1u + k
  583. eor v12.16b, v12.16b, v16.16b
  584. eor v0.16b, v1.16b, v4.16b // vpxor %xmm4, %xmm1, %xmm0 # 0 = A
  585. eor v8.16b, v9.16b, v12.16b
  586. tbl v0.16b, {v0.16b},v2.16b // vpshufb %xmm2, %xmm0, %xmm0
  587. tbl v1.16b, {v8.16b},v2.16b
  588. ret
  589. .size _vpaes_decrypt_2x,.-_vpaes_decrypt_2x
  590. ___
  591. }
  592. {
  593. my ($inp,$bits,$out,$dir)=("x0","w1","x2","w3");
  594. my ($invlo,$invhi,$iptlo,$ipthi,$rcon) = map("v$_.16b",(18..21,8));
  595. $code.=<<___;
  596. ########################################################
  597. ## ##
  598. ## AES key schedule ##
  599. ## ##
  600. ########################################################
  601. .type _vpaes_key_preheat,%function
  602. .align 4
  603. _vpaes_key_preheat:
  604. adr x10, .Lk_inv
  605. movi v16.16b, #0x5b // .Lk_s63
  606. adr x11, .Lk_sb1
  607. movi v17.16b, #0x0f // .Lk_s0F
  608. ld1 {v18.2d-v21.2d}, [x10] // .Lk_inv, .Lk_ipt
  609. adr x10, .Lk_dksd
  610. ld1 {v22.2d-v23.2d}, [x11] // .Lk_sb1
  611. adr x11, .Lk_mc_forward
  612. ld1 {v24.2d-v27.2d}, [x10],#64 // .Lk_dksd, .Lk_dksb
  613. ld1 {v28.2d-v31.2d}, [x10],#64 // .Lk_dkse, .Lk_dks9
  614. ld1 {v8.2d}, [x10] // .Lk_rcon
  615. ld1 {v9.2d}, [x11] // .Lk_mc_forward[0]
  616. ret
  617. .size _vpaes_key_preheat,.-_vpaes_key_preheat
  618. .type _vpaes_schedule_core,%function
  619. .align 4
  620. _vpaes_schedule_core:
  621. .inst 0xd503233f // paciasp
  622. stp x29, x30, [sp,#-16]!
  623. add x29,sp,#0
  624. bl _vpaes_key_preheat // load the tables
  625. ld1 {v0.16b}, [$inp],#16 // vmovdqu (%rdi), %xmm0 # load key (unaligned)
  626. // input transform
  627. mov v3.16b, v0.16b // vmovdqa %xmm0, %xmm3
  628. bl _vpaes_schedule_transform
  629. mov v7.16b, v0.16b // vmovdqa %xmm0, %xmm7
  630. adr x10, .Lk_sr // lea .Lk_sr(%rip),%r10
  631. add x8, x8, x10
  632. cbnz $dir, .Lschedule_am_decrypting
  633. // encrypting, output zeroth round key after transform
  634. st1 {v0.2d}, [$out] // vmovdqu %xmm0, (%rdx)
  635. b .Lschedule_go
  636. .Lschedule_am_decrypting:
  637. // decrypting, output zeroth round key after shiftrows
  638. ld1 {v1.2d}, [x8] // vmovdqa (%r8,%r10), %xmm1
  639. tbl v3.16b, {v3.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm3
  640. st1 {v3.2d}, [$out] // vmovdqu %xmm3, (%rdx)
  641. eor x8, x8, #0x30 // xor \$0x30, %r8
  642. .Lschedule_go:
  643. cmp $bits, #192 // cmp \$192, %esi
  644. b.hi .Lschedule_256
  645. b.eq .Lschedule_192
  646. // 128: fall though
  647. ##
  648. ## .schedule_128
  649. ##
  650. ## 128-bit specific part of key schedule.
  651. ##
  652. ## This schedule is really simple, because all its parts
  653. ## are accomplished by the subroutines.
  654. ##
  655. .Lschedule_128:
  656. mov $inp, #10 // mov \$10, %esi
  657. .Loop_schedule_128:
  658. sub $inp, $inp, #1 // dec %esi
  659. bl _vpaes_schedule_round
  660. cbz $inp, .Lschedule_mangle_last
  661. bl _vpaes_schedule_mangle // write output
  662. b .Loop_schedule_128
  663. ##
  664. ## .aes_schedule_192
  665. ##
  666. ## 192-bit specific part of key schedule.
  667. ##
  668. ## The main body of this schedule is the same as the 128-bit
  669. ## schedule, but with more smearing. The long, high side is
  670. ## stored in %xmm7 as before, and the short, low side is in
  671. ## the high bits of %xmm6.
  672. ##
  673. ## This schedule is somewhat nastier, however, because each
  674. ## round produces 192 bits of key material, or 1.5 round keys.
  675. ## Therefore, on each cycle we do 2 rounds and produce 3 round
  676. ## keys.
  677. ##
  678. .align 4
  679. .Lschedule_192:
  680. sub $inp, $inp, #8
  681. ld1 {v0.16b}, [$inp] // vmovdqu 8(%rdi),%xmm0 # load key part 2 (very unaligned)
  682. bl _vpaes_schedule_transform // input transform
  683. mov v6.16b, v0.16b // vmovdqa %xmm0, %xmm6 # save short part
  684. eor v4.16b, v4.16b, v4.16b // vpxor %xmm4, %xmm4, %xmm4 # clear 4
  685. ins v6.d[0], v4.d[0] // vmovhlps %xmm4, %xmm6, %xmm6 # clobber low side with zeros
  686. mov $inp, #4 // mov \$4, %esi
  687. .Loop_schedule_192:
  688. sub $inp, $inp, #1 // dec %esi
  689. bl _vpaes_schedule_round
  690. ext v0.16b, v6.16b, v0.16b, #8 // vpalignr \$8,%xmm6,%xmm0,%xmm0
  691. bl _vpaes_schedule_mangle // save key n
  692. bl _vpaes_schedule_192_smear
  693. bl _vpaes_schedule_mangle // save key n+1
  694. bl _vpaes_schedule_round
  695. cbz $inp, .Lschedule_mangle_last
  696. bl _vpaes_schedule_mangle // save key n+2
  697. bl _vpaes_schedule_192_smear
  698. b .Loop_schedule_192
  699. ##
  700. ## .aes_schedule_256
  701. ##
  702. ## 256-bit specific part of key schedule.
  703. ##
  704. ## The structure here is very similar to the 128-bit
  705. ## schedule, but with an additional "low side" in
  706. ## %xmm6. The low side's rounds are the same as the
  707. ## high side's, except no rcon and no rotation.
  708. ##
  709. .align 4
  710. .Lschedule_256:
  711. ld1 {v0.16b}, [$inp] // vmovdqu 16(%rdi),%xmm0 # load key part 2 (unaligned)
  712. bl _vpaes_schedule_transform // input transform
  713. mov $inp, #7 // mov \$7, %esi
  714. .Loop_schedule_256:
  715. sub $inp, $inp, #1 // dec %esi
  716. bl _vpaes_schedule_mangle // output low result
  717. mov v6.16b, v0.16b // vmovdqa %xmm0, %xmm6 # save cur_lo in xmm6
  718. // high round
  719. bl _vpaes_schedule_round
  720. cbz $inp, .Lschedule_mangle_last
  721. bl _vpaes_schedule_mangle
  722. // low round. swap xmm7 and xmm6
  723. dup v0.4s, v0.s[3] // vpshufd \$0xFF, %xmm0, %xmm0
  724. movi v4.16b, #0
  725. mov v5.16b, v7.16b // vmovdqa %xmm7, %xmm5
  726. mov v7.16b, v6.16b // vmovdqa %xmm6, %xmm7
  727. bl _vpaes_schedule_low_round
  728. mov v7.16b, v5.16b // vmovdqa %xmm5, %xmm7
  729. b .Loop_schedule_256
  730. ##
  731. ## .aes_schedule_mangle_last
  732. ##
  733. ## Mangler for last round of key schedule
  734. ## Mangles %xmm0
  735. ## when encrypting, outputs out(%xmm0) ^ 63
  736. ## when decrypting, outputs unskew(%xmm0)
  737. ##
  738. ## Always called right before return... jumps to cleanup and exits
  739. ##
  740. .align 4
  741. .Lschedule_mangle_last:
  742. // schedule last round key from xmm0
  743. adr x11, .Lk_deskew // lea .Lk_deskew(%rip),%r11 # prepare to deskew
  744. cbnz $dir, .Lschedule_mangle_last_dec
  745. // encrypting
  746. ld1 {v1.2d}, [x8] // vmovdqa (%r8,%r10),%xmm1
  747. adr x11, .Lk_opt // lea .Lk_opt(%rip), %r11 # prepare to output transform
  748. add $out, $out, #32 // add \$32, %rdx
  749. tbl v0.16b, {v0.16b}, v1.16b // vpshufb %xmm1, %xmm0, %xmm0 # output permute
  750. .Lschedule_mangle_last_dec:
  751. ld1 {v20.2d-v21.2d}, [x11] // reload constants
  752. sub $out, $out, #16 // add \$-16, %rdx
  753. eor v0.16b, v0.16b, v16.16b // vpxor .Lk_s63(%rip), %xmm0, %xmm0
  754. bl _vpaes_schedule_transform // output transform
  755. st1 {v0.2d}, [$out] // vmovdqu %xmm0, (%rdx) # save last key
  756. // cleanup
  757. eor v0.16b, v0.16b, v0.16b // vpxor %xmm0, %xmm0, %xmm0
  758. eor v1.16b, v1.16b, v1.16b // vpxor %xmm1, %xmm1, %xmm1
  759. eor v2.16b, v2.16b, v2.16b // vpxor %xmm2, %xmm2, %xmm2
  760. eor v3.16b, v3.16b, v3.16b // vpxor %xmm3, %xmm3, %xmm3
  761. eor v4.16b, v4.16b, v4.16b // vpxor %xmm4, %xmm4, %xmm4
  762. eor v5.16b, v5.16b, v5.16b // vpxor %xmm5, %xmm5, %xmm5
  763. eor v6.16b, v6.16b, v6.16b // vpxor %xmm6, %xmm6, %xmm6
  764. eor v7.16b, v7.16b, v7.16b // vpxor %xmm7, %xmm7, %xmm7
  765. ldp x29, x30, [sp],#16
  766. .inst 0xd50323bf // autiasp
  767. ret
  768. .size _vpaes_schedule_core,.-_vpaes_schedule_core
  769. ##
  770. ## .aes_schedule_192_smear
  771. ##
  772. ## Smear the short, low side in the 192-bit key schedule.
  773. ##
  774. ## Inputs:
  775. ## %xmm7: high side, b a x y
  776. ## %xmm6: low side, d c 0 0
  777. ## %xmm13: 0
  778. ##
  779. ## Outputs:
  780. ## %xmm6: b+c+d b+c 0 0
  781. ## %xmm0: b+c+d b+c b a
  782. ##
  783. .type _vpaes_schedule_192_smear,%function
  784. .align 4
  785. _vpaes_schedule_192_smear:
  786. movi v1.16b, #0
  787. dup v0.4s, v7.s[3]
  788. ins v1.s[3], v6.s[2] // vpshufd \$0x80, %xmm6, %xmm1 # d c 0 0 -> c 0 0 0
  789. ins v0.s[0], v7.s[2] // vpshufd \$0xFE, %xmm7, %xmm0 # b a _ _ -> b b b a
  790. eor v6.16b, v6.16b, v1.16b // vpxor %xmm1, %xmm6, %xmm6 # -> c+d c 0 0
  791. eor v1.16b, v1.16b, v1.16b // vpxor %xmm1, %xmm1, %xmm1
  792. eor v6.16b, v6.16b, v0.16b // vpxor %xmm0, %xmm6, %xmm6 # -> b+c+d b+c b a
  793. mov v0.16b, v6.16b // vmovdqa %xmm6, %xmm0
  794. ins v6.d[0], v1.d[0] // vmovhlps %xmm1, %xmm6, %xmm6 # clobber low side with zeros
  795. ret
  796. .size _vpaes_schedule_192_smear,.-_vpaes_schedule_192_smear
  797. ##
  798. ## .aes_schedule_round
  799. ##
  800. ## Runs one main round of the key schedule on %xmm0, %xmm7
  801. ##
  802. ## Specifically, runs subbytes on the high dword of %xmm0
  803. ## then rotates it by one byte and xors into the low dword of
  804. ## %xmm7.
  805. ##
  806. ## Adds rcon from low byte of %xmm8, then rotates %xmm8 for
  807. ## next rcon.
  808. ##
  809. ## Smears the dwords of %xmm7 by xoring the low into the
  810. ## second low, result into third, result into highest.
  811. ##
  812. ## Returns results in %xmm7 = %xmm0.
  813. ## Clobbers %xmm1-%xmm4, %r11.
  814. ##
  815. .type _vpaes_schedule_round,%function
  816. .align 4
  817. _vpaes_schedule_round:
  818. // extract rcon from xmm8
  819. movi v4.16b, #0 // vpxor %xmm4, %xmm4, %xmm4
  820. ext v1.16b, $rcon, v4.16b, #15 // vpalignr \$15, %xmm8, %xmm4, %xmm1
  821. ext $rcon, $rcon, $rcon, #15 // vpalignr \$15, %xmm8, %xmm8, %xmm8
  822. eor v7.16b, v7.16b, v1.16b // vpxor %xmm1, %xmm7, %xmm7
  823. // rotate
  824. dup v0.4s, v0.s[3] // vpshufd \$0xFF, %xmm0, %xmm0
  825. ext v0.16b, v0.16b, v0.16b, #1 // vpalignr \$1, %xmm0, %xmm0, %xmm0
  826. // fall through...
  827. // low round: same as high round, but no rotation and no rcon.
  828. _vpaes_schedule_low_round:
  829. // smear xmm7
  830. ext v1.16b, v4.16b, v7.16b, #12 // vpslldq \$4, %xmm7, %xmm1
  831. eor v7.16b, v7.16b, v1.16b // vpxor %xmm1, %xmm7, %xmm7
  832. ext v4.16b, v4.16b, v7.16b, #8 // vpslldq \$8, %xmm7, %xmm4
  833. // subbytes
  834. and v1.16b, v0.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1 # 0 = k
  835. ushr v0.16b, v0.16b, #4 // vpsrlb \$4, %xmm0, %xmm0 # 1 = i
  836. eor v7.16b, v7.16b, v4.16b // vpxor %xmm4, %xmm7, %xmm7
  837. tbl v2.16b, {$invhi}, v1.16b // vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k
  838. eor v1.16b, v1.16b, v0.16b // vpxor %xmm0, %xmm1, %xmm1 # 0 = j
  839. tbl v3.16b, {$invlo}, v0.16b // vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i
  840. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k
  841. tbl v4.16b, {$invlo}, v1.16b // vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j
  842. eor v7.16b, v7.16b, v16.16b // vpxor .Lk_s63(%rip), %xmm7, %xmm7
  843. tbl v3.16b, {$invlo}, v3.16b // vpshufb %xmm3, %xmm10, %xmm3 # 2 = 1/iak
  844. eor v4.16b, v4.16b, v2.16b // vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k
  845. tbl v2.16b, {$invlo}, v4.16b // vpshufb %xmm4, %xmm10, %xmm2 # 3 = 1/jak
  846. eor v3.16b, v3.16b, v1.16b // vpxor %xmm1, %xmm3, %xmm3 # 2 = io
  847. eor v2.16b, v2.16b, v0.16b // vpxor %xmm0, %xmm2, %xmm2 # 3 = jo
  848. tbl v4.16b, {v23.16b}, v3.16b // vpshufb %xmm3, %xmm13, %xmm4 # 4 = sbou
  849. tbl v1.16b, {v22.16b}, v2.16b // vpshufb %xmm2, %xmm12, %xmm1 # 0 = sb1t
  850. eor v1.16b, v1.16b, v4.16b // vpxor %xmm4, %xmm1, %xmm1 # 0 = sbox output
  851. // add in smeared stuff
  852. eor v0.16b, v1.16b, v7.16b // vpxor %xmm7, %xmm1, %xmm0
  853. eor v7.16b, v1.16b, v7.16b // vmovdqa %xmm0, %xmm7
  854. ret
  855. .size _vpaes_schedule_round,.-_vpaes_schedule_round
  856. ##
  857. ## .aes_schedule_transform
  858. ##
  859. ## Linear-transform %xmm0 according to tables at (%r11)
  860. ##
  861. ## Requires that %xmm9 = 0x0F0F... as in preheat
  862. ## Output in %xmm0
  863. ## Clobbers %xmm1, %xmm2
  864. ##
  865. .type _vpaes_schedule_transform,%function
  866. .align 4
  867. _vpaes_schedule_transform:
  868. and v1.16b, v0.16b, v17.16b // vpand %xmm9, %xmm0, %xmm1
  869. ushr v0.16b, v0.16b, #4 // vpsrlb \$4, %xmm0, %xmm0
  870. // vmovdqa (%r11), %xmm2 # lo
  871. tbl v2.16b, {$iptlo}, v1.16b // vpshufb %xmm1, %xmm2, %xmm2
  872. // vmovdqa 16(%r11), %xmm1 # hi
  873. tbl v0.16b, {$ipthi}, v0.16b // vpshufb %xmm0, %xmm1, %xmm0
  874. eor v0.16b, v0.16b, v2.16b // vpxor %xmm2, %xmm0, %xmm0
  875. ret
  876. .size _vpaes_schedule_transform,.-_vpaes_schedule_transform
  877. ##
  878. ## .aes_schedule_mangle
  879. ##
  880. ## Mangle xmm0 from (basis-transformed) standard version
  881. ## to our version.
  882. ##
  883. ## On encrypt,
  884. ## xor with 0x63
  885. ## multiply by circulant 0,1,1,1
  886. ## apply shiftrows transform
  887. ##
  888. ## On decrypt,
  889. ## xor with 0x63
  890. ## multiply by "inverse mixcolumns" circulant E,B,D,9
  891. ## deskew
  892. ## apply shiftrows transform
  893. ##
  894. ##
  895. ## Writes out to (%rdx), and increments or decrements it
  896. ## Keeps track of round number mod 4 in %r8
  897. ## Preserves xmm0
  898. ## Clobbers xmm1-xmm5
  899. ##
  900. .type _vpaes_schedule_mangle,%function
  901. .align 4
  902. _vpaes_schedule_mangle:
  903. mov v4.16b, v0.16b // vmovdqa %xmm0, %xmm4 # save xmm0 for later
  904. // vmovdqa .Lk_mc_forward(%rip),%xmm5
  905. cbnz $dir, .Lschedule_mangle_dec
  906. // encrypting
  907. eor v4.16b, v0.16b, v16.16b // vpxor .Lk_s63(%rip), %xmm0, %xmm4
  908. add $out, $out, #16 // add \$16, %rdx
  909. tbl v4.16b, {v4.16b}, v9.16b // vpshufb %xmm5, %xmm4, %xmm4
  910. tbl v1.16b, {v4.16b}, v9.16b // vpshufb %xmm5, %xmm4, %xmm1
  911. tbl v3.16b, {v1.16b}, v9.16b // vpshufb %xmm5, %xmm1, %xmm3
  912. eor v4.16b, v4.16b, v1.16b // vpxor %xmm1, %xmm4, %xmm4
  913. ld1 {v1.2d}, [x8] // vmovdqa (%r8,%r10), %xmm1
  914. eor v3.16b, v3.16b, v4.16b // vpxor %xmm4, %xmm3, %xmm3
  915. b .Lschedule_mangle_both
  916. .align 4
  917. .Lschedule_mangle_dec:
  918. // inverse mix columns
  919. // lea .Lk_dksd(%rip),%r11
  920. ushr v1.16b, v4.16b, #4 // vpsrlb \$4, %xmm4, %xmm1 # 1 = hi
  921. and v4.16b, v4.16b, v17.16b // vpand %xmm9, %xmm4, %xmm4 # 4 = lo
  922. // vmovdqa 0x00(%r11), %xmm2
  923. tbl v2.16b, {v24.16b}, v4.16b // vpshufb %xmm4, %xmm2, %xmm2
  924. // vmovdqa 0x10(%r11), %xmm3
  925. tbl v3.16b, {v25.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm3
  926. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3
  927. tbl v3.16b, {v3.16b}, v9.16b // vpshufb %xmm5, %xmm3, %xmm3
  928. // vmovdqa 0x20(%r11), %xmm2
  929. tbl v2.16b, {v26.16b}, v4.16b // vpshufb %xmm4, %xmm2, %xmm2
  930. eor v2.16b, v2.16b, v3.16b // vpxor %xmm3, %xmm2, %xmm2
  931. // vmovdqa 0x30(%r11), %xmm3
  932. tbl v3.16b, {v27.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm3
  933. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3
  934. tbl v3.16b, {v3.16b}, v9.16b // vpshufb %xmm5, %xmm3, %xmm3
  935. // vmovdqa 0x40(%r11), %xmm2
  936. tbl v2.16b, {v28.16b}, v4.16b // vpshufb %xmm4, %xmm2, %xmm2
  937. eor v2.16b, v2.16b, v3.16b // vpxor %xmm3, %xmm2, %xmm2
  938. // vmovdqa 0x50(%r11), %xmm3
  939. tbl v3.16b, {v29.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm3
  940. eor v3.16b, v3.16b, v2.16b // vpxor %xmm2, %xmm3, %xmm3
  941. // vmovdqa 0x60(%r11), %xmm2
  942. tbl v2.16b, {v30.16b}, v4.16b // vpshufb %xmm4, %xmm2, %xmm2
  943. tbl v3.16b, {v3.16b}, v9.16b // vpshufb %xmm5, %xmm3, %xmm3
  944. // vmovdqa 0x70(%r11), %xmm4
  945. tbl v4.16b, {v31.16b}, v1.16b // vpshufb %xmm1, %xmm4, %xmm4
  946. ld1 {v1.2d}, [x8] // vmovdqa (%r8,%r10), %xmm1
  947. eor v2.16b, v2.16b, v3.16b // vpxor %xmm3, %xmm2, %xmm2
  948. eor v3.16b, v4.16b, v2.16b // vpxor %xmm2, %xmm4, %xmm3
  949. sub $out, $out, #16 // add \$-16, %rdx
  950. .Lschedule_mangle_both:
  951. tbl v3.16b, {v3.16b}, v1.16b // vpshufb %xmm1, %xmm3, %xmm3
  952. add x8, x8, #64-16 // add \$-16, %r8
  953. and x8, x8, #~(1<<6) // and \$0x30, %r8
  954. st1 {v3.2d}, [$out] // vmovdqu %xmm3, (%rdx)
  955. ret
  956. .size _vpaes_schedule_mangle,.-_vpaes_schedule_mangle
  957. .globl vpaes_set_encrypt_key
  958. .type vpaes_set_encrypt_key,%function
  959. .align 4
  960. vpaes_set_encrypt_key:
  961. .inst 0xd503233f // paciasp
  962. stp x29,x30,[sp,#-16]!
  963. add x29,sp,#0
  964. stp d8,d9,[sp,#-16]! // ABI spec says so
  965. lsr w9, $bits, #5 // shr \$5,%eax
  966. add w9, w9, #5 // \$5,%eax
  967. str w9, [$out,#240] // mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5;
  968. mov $dir, #0 // mov \$0,%ecx
  969. mov x8, #0x30 // mov \$0x30,%r8d
  970. bl _vpaes_schedule_core
  971. eor x0, x0, x0
  972. ldp d8,d9,[sp],#16
  973. ldp x29,x30,[sp],#16
  974. .inst 0xd50323bf // autiasp
  975. ret
  976. .size vpaes_set_encrypt_key,.-vpaes_set_encrypt_key
  977. .globl vpaes_set_decrypt_key
  978. .type vpaes_set_decrypt_key,%function
  979. .align 4
  980. vpaes_set_decrypt_key:
  981. .inst 0xd503233f // paciasp
  982. stp x29,x30,[sp,#-16]!
  983. add x29,sp,#0
  984. stp d8,d9,[sp,#-16]! // ABI spec says so
  985. lsr w9, $bits, #5 // shr \$5,%eax
  986. add w9, w9, #5 // \$5,%eax
  987. str w9, [$out,#240] // mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5;
  988. lsl w9, w9, #4 // shl \$4,%eax
  989. add $out, $out, #16 // lea 16(%rdx,%rax),%rdx
  990. add $out, $out, x9
  991. mov $dir, #1 // mov \$1,%ecx
  992. lsr w8, $bits, #1 // shr \$1,%r8d
  993. and x8, x8, #32 // and \$32,%r8d
  994. eor x8, x8, #32 // xor \$32,%r8d # nbits==192?0:32
  995. bl _vpaes_schedule_core
  996. ldp d8,d9,[sp],#16
  997. ldp x29,x30,[sp],#16
  998. .inst 0xd50323bf // autiasp
  999. ret
  1000. .size vpaes_set_decrypt_key,.-vpaes_set_decrypt_key
  1001. ___
  1002. }
  1003. {
  1004. my ($inp,$out,$len,$key,$ivec,$dir) = map("x$_",(0..5));
  1005. $code.=<<___;
  1006. .globl vpaes_cbc_encrypt
  1007. .type vpaes_cbc_encrypt,%function
  1008. .align 4
  1009. vpaes_cbc_encrypt:
  1010. cbz $len, .Lcbc_abort
  1011. cmp w5, #0 // check direction
  1012. b.eq vpaes_cbc_decrypt
  1013. .inst 0xd503233f // paciasp
  1014. stp x29,x30,[sp,#-16]!
  1015. add x29,sp,#0
  1016. mov x17, $len // reassign
  1017. mov x2, $key // reassign
  1018. ld1 {v0.16b}, [$ivec] // load ivec
  1019. bl _vpaes_encrypt_preheat
  1020. b .Lcbc_enc_loop
  1021. .align 4
  1022. .Lcbc_enc_loop:
  1023. ld1 {v7.16b}, [$inp],#16 // load input
  1024. eor v7.16b, v7.16b, v0.16b // xor with ivec
  1025. bl _vpaes_encrypt_core
  1026. st1 {v0.16b}, [$out],#16 // save output
  1027. subs x17, x17, #16
  1028. b.hi .Lcbc_enc_loop
  1029. st1 {v0.16b}, [$ivec] // write ivec
  1030. ldp x29,x30,[sp],#16
  1031. .inst 0xd50323bf // autiasp
  1032. .Lcbc_abort:
  1033. ret
  1034. .size vpaes_cbc_encrypt,.-vpaes_cbc_encrypt
  1035. .type vpaes_cbc_decrypt,%function
  1036. .align 4
  1037. vpaes_cbc_decrypt:
  1038. .inst 0xd503233f // paciasp
  1039. stp x29,x30,[sp,#-16]!
  1040. add x29,sp,#0
  1041. stp d8,d9,[sp,#-16]! // ABI spec says so
  1042. stp d10,d11,[sp,#-16]!
  1043. stp d12,d13,[sp,#-16]!
  1044. stp d14,d15,[sp,#-16]!
  1045. mov x17, $len // reassign
  1046. mov x2, $key // reassign
  1047. ld1 {v6.16b}, [$ivec] // load ivec
  1048. bl _vpaes_decrypt_preheat
  1049. tst x17, #16
  1050. b.eq .Lcbc_dec_loop2x
  1051. ld1 {v7.16b}, [$inp], #16 // load input
  1052. bl _vpaes_decrypt_core
  1053. eor v0.16b, v0.16b, v6.16b // xor with ivec
  1054. orr v6.16b, v7.16b, v7.16b // next ivec value
  1055. st1 {v0.16b}, [$out], #16
  1056. subs x17, x17, #16
  1057. b.ls .Lcbc_dec_done
  1058. .align 4
  1059. .Lcbc_dec_loop2x:
  1060. ld1 {v14.16b,v15.16b}, [$inp], #32
  1061. bl _vpaes_decrypt_2x
  1062. eor v0.16b, v0.16b, v6.16b // xor with ivec
  1063. eor v1.16b, v1.16b, v14.16b
  1064. orr v6.16b, v15.16b, v15.16b
  1065. st1 {v0.16b,v1.16b}, [$out], #32
  1066. subs x17, x17, #32
  1067. b.hi .Lcbc_dec_loop2x
  1068. .Lcbc_dec_done:
  1069. st1 {v6.16b}, [$ivec]
  1070. ldp d14,d15,[sp],#16
  1071. ldp d12,d13,[sp],#16
  1072. ldp d10,d11,[sp],#16
  1073. ldp d8,d9,[sp],#16
  1074. ldp x29,x30,[sp],#16
  1075. .inst 0xd50323bf // autiasp
  1076. ret
  1077. .size vpaes_cbc_decrypt,.-vpaes_cbc_decrypt
  1078. ___
  1079. if (1) {
  1080. $code.=<<___;
  1081. .globl vpaes_ecb_encrypt
  1082. .type vpaes_ecb_encrypt,%function
  1083. .align 4
  1084. vpaes_ecb_encrypt:
  1085. .inst 0xd503233f // paciasp
  1086. stp x29,x30,[sp,#-16]!
  1087. add x29,sp,#0
  1088. stp d8,d9,[sp,#-16]! // ABI spec says so
  1089. stp d10,d11,[sp,#-16]!
  1090. stp d12,d13,[sp,#-16]!
  1091. stp d14,d15,[sp,#-16]!
  1092. mov x17, $len
  1093. mov x2, $key
  1094. bl _vpaes_encrypt_preheat
  1095. tst x17, #16
  1096. b.eq .Lecb_enc_loop
  1097. ld1 {v7.16b}, [$inp],#16
  1098. bl _vpaes_encrypt_core
  1099. st1 {v0.16b}, [$out],#16
  1100. subs x17, x17, #16
  1101. b.ls .Lecb_enc_done
  1102. .align 4
  1103. .Lecb_enc_loop:
  1104. ld1 {v14.16b,v15.16b}, [$inp], #32
  1105. bl _vpaes_encrypt_2x
  1106. st1 {v0.16b,v1.16b}, [$out], #32
  1107. subs x17, x17, #32
  1108. b.hi .Lecb_enc_loop
  1109. .Lecb_enc_done:
  1110. ldp d14,d15,[sp],#16
  1111. ldp d12,d13,[sp],#16
  1112. ldp d10,d11,[sp],#16
  1113. ldp d8,d9,[sp],#16
  1114. ldp x29,x30,[sp],#16
  1115. .inst 0xd50323bf // autiasp
  1116. ret
  1117. .size vpaes_ecb_encrypt,.-vpaes_ecb_encrypt
  1118. .globl vpaes_ecb_decrypt
  1119. .type vpaes_ecb_decrypt,%function
  1120. .align 4
  1121. vpaes_ecb_decrypt:
  1122. .inst 0xd503233f // paciasp
  1123. stp x29,x30,[sp,#-16]!
  1124. add x29,sp,#0
  1125. stp d8,d9,[sp,#-16]! // ABI spec says so
  1126. stp d10,d11,[sp,#-16]!
  1127. stp d12,d13,[sp,#-16]!
  1128. stp d14,d15,[sp,#-16]!
  1129. mov x17, $len
  1130. mov x2, $key
  1131. bl _vpaes_decrypt_preheat
  1132. tst x17, #16
  1133. b.eq .Lecb_dec_loop
  1134. ld1 {v7.16b}, [$inp],#16
  1135. bl _vpaes_encrypt_core
  1136. st1 {v0.16b}, [$out],#16
  1137. subs x17, x17, #16
  1138. b.ls .Lecb_dec_done
  1139. .align 4
  1140. .Lecb_dec_loop:
  1141. ld1 {v14.16b,v15.16b}, [$inp], #32
  1142. bl _vpaes_decrypt_2x
  1143. st1 {v0.16b,v1.16b}, [$out], #32
  1144. subs x17, x17, #32
  1145. b.hi .Lecb_dec_loop
  1146. .Lecb_dec_done:
  1147. ldp d14,d15,[sp],#16
  1148. ldp d12,d13,[sp],#16
  1149. ldp d10,d11,[sp],#16
  1150. ldp d8,d9,[sp],#16
  1151. ldp x29,x30,[sp],#16
  1152. .inst 0xd50323bf // autiasp
  1153. ret
  1154. .size vpaes_ecb_decrypt,.-vpaes_ecb_decrypt
  1155. ___
  1156. } }
  1157. print $code;
  1158. close STDOUT or die "error closing STDOUT: $!";