vpaes-armv8.pl 44 KB

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