aes-neonbs-core.S 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Bit sliced AES using NEON instructions
  4. *
  5. * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. */
  7. /*
  8. * The algorithm implemented here is described in detail by the paper
  9. * 'Faster and Timing-Attack Resistant AES-GCM' by Emilia Kaesper and
  10. * Peter Schwabe (https://eprint.iacr.org/2009/129.pdf)
  11. *
  12. * This implementation is based primarily on the OpenSSL implementation
  13. * for 32-bit ARM written by Andy Polyakov <appro@openssl.org>
  14. */
  15. #include <linux/linkage.h>
  16. #include <linux/cfi_types.h>
  17. #include <asm/assembler.h>
  18. .text
  19. rounds .req x11
  20. bskey .req x12
  21. .macro in_bs_ch, b0, b1, b2, b3, b4, b5, b6, b7
  22. eor \b2, \b2, \b1
  23. eor \b5, \b5, \b6
  24. eor \b3, \b3, \b0
  25. eor \b6, \b6, \b2
  26. eor \b5, \b5, \b0
  27. eor \b6, \b6, \b3
  28. eor \b3, \b3, \b7
  29. eor \b7, \b7, \b5
  30. eor \b3, \b3, \b4
  31. eor \b4, \b4, \b5
  32. eor \b2, \b2, \b7
  33. eor \b3, \b3, \b1
  34. eor \b1, \b1, \b5
  35. .endm
  36. .macro out_bs_ch, b0, b1, b2, b3, b4, b5, b6, b7
  37. eor \b0, \b0, \b6
  38. eor \b1, \b1, \b4
  39. eor \b4, \b4, \b6
  40. eor \b2, \b2, \b0
  41. eor \b6, \b6, \b1
  42. eor \b1, \b1, \b5
  43. eor \b5, \b5, \b3
  44. eor \b3, \b3, \b7
  45. eor \b7, \b7, \b5
  46. eor \b2, \b2, \b5
  47. eor \b4, \b4, \b7
  48. .endm
  49. .macro inv_in_bs_ch, b6, b1, b2, b4, b7, b0, b3, b5
  50. eor \b1, \b1, \b7
  51. eor \b4, \b4, \b7
  52. eor \b7, \b7, \b5
  53. eor \b1, \b1, \b3
  54. eor \b2, \b2, \b5
  55. eor \b3, \b3, \b7
  56. eor \b6, \b6, \b1
  57. eor \b2, \b2, \b0
  58. eor \b5, \b5, \b3
  59. eor \b4, \b4, \b6
  60. eor \b0, \b0, \b6
  61. eor \b1, \b1, \b4
  62. .endm
  63. .macro inv_out_bs_ch, b6, b5, b0, b3, b7, b1, b4, b2
  64. eor \b1, \b1, \b5
  65. eor \b2, \b2, \b7
  66. eor \b3, \b3, \b1
  67. eor \b4, \b4, \b5
  68. eor \b7, \b7, \b5
  69. eor \b3, \b3, \b4
  70. eor \b5, \b5, \b0
  71. eor \b3, \b3, \b7
  72. eor \b6, \b6, \b2
  73. eor \b2, \b2, \b1
  74. eor \b6, \b6, \b3
  75. eor \b3, \b3, \b0
  76. eor \b5, \b5, \b6
  77. .endm
  78. .macro mul_gf4, x0, x1, y0, y1, t0, t1
  79. eor \t0, \y0, \y1
  80. and \t0, \t0, \x0
  81. eor \x0, \x0, \x1
  82. and \t1, \x1, \y0
  83. and \x0, \x0, \y1
  84. eor \x1, \t1, \t0
  85. eor \x0, \x0, \t1
  86. .endm
  87. .macro mul_gf4_n_gf4, x0, x1, y0, y1, t0, x2, x3, y2, y3, t1
  88. eor \t0, \y0, \y1
  89. eor \t1, \y2, \y3
  90. and \t0, \t0, \x0
  91. and \t1, \t1, \x2
  92. eor \x0, \x0, \x1
  93. eor \x2, \x2, \x3
  94. and \x1, \x1, \y0
  95. and \x3, \x3, \y2
  96. and \x0, \x0, \y1
  97. and \x2, \x2, \y3
  98. eor \x1, \x1, \x0
  99. eor \x2, \x2, \x3
  100. eor \x0, \x0, \t0
  101. eor \x3, \x3, \t1
  102. .endm
  103. .macro mul_gf16_2, x0, x1, x2, x3, x4, x5, x6, x7, \
  104. y0, y1, y2, y3, t0, t1, t2, t3
  105. eor \t0, \x0, \x2
  106. eor \t1, \x1, \x3
  107. mul_gf4 \x0, \x1, \y0, \y1, \t2, \t3
  108. eor \y0, \y0, \y2
  109. eor \y1, \y1, \y3
  110. mul_gf4_n_gf4 \t0, \t1, \y0, \y1, \t3, \x2, \x3, \y2, \y3, \t2
  111. eor \x0, \x0, \t0
  112. eor \x2, \x2, \t0
  113. eor \x1, \x1, \t1
  114. eor \x3, \x3, \t1
  115. eor \t0, \x4, \x6
  116. eor \t1, \x5, \x7
  117. mul_gf4_n_gf4 \t0, \t1, \y0, \y1, \t3, \x6, \x7, \y2, \y3, \t2
  118. eor \y0, \y0, \y2
  119. eor \y1, \y1, \y3
  120. mul_gf4 \x4, \x5, \y0, \y1, \t2, \t3
  121. eor \x4, \x4, \t0
  122. eor \x6, \x6, \t0
  123. eor \x5, \x5, \t1
  124. eor \x7, \x7, \t1
  125. .endm
  126. .macro inv_gf256, x0, x1, x2, x3, x4, x5, x6, x7, \
  127. t0, t1, t2, t3, s0, s1, s2, s3
  128. eor \t3, \x4, \x6
  129. eor \t0, \x5, \x7
  130. eor \t1, \x1, \x3
  131. eor \s1, \x7, \x6
  132. eor \s0, \x0, \x2
  133. eor \s3, \t3, \t0
  134. orr \t2, \t0, \t1
  135. and \s2, \t3, \s0
  136. orr \t3, \t3, \s0
  137. eor \s0, \s0, \t1
  138. and \t0, \t0, \t1
  139. eor \t1, \x3, \x2
  140. and \s3, \s3, \s0
  141. and \s1, \s1, \t1
  142. eor \t1, \x4, \x5
  143. eor \s0, \x1, \x0
  144. eor \t3, \t3, \s1
  145. eor \t2, \t2, \s1
  146. and \s1, \t1, \s0
  147. orr \t1, \t1, \s0
  148. eor \t3, \t3, \s3
  149. eor \t0, \t0, \s1
  150. eor \t2, \t2, \s2
  151. eor \t1, \t1, \s3
  152. eor \t0, \t0, \s2
  153. and \s0, \x7, \x3
  154. eor \t1, \t1, \s2
  155. and \s1, \x6, \x2
  156. and \s2, \x5, \x1
  157. orr \s3, \x4, \x0
  158. eor \t3, \t3, \s0
  159. eor \t1, \t1, \s2
  160. eor \s0, \t0, \s3
  161. eor \t2, \t2, \s1
  162. and \s2, \t3, \t1
  163. eor \s1, \t2, \s2
  164. eor \s3, \s0, \s2
  165. bsl \s1, \t1, \s0
  166. not \t0, \s0
  167. bsl \s0, \s1, \s3
  168. bsl \t0, \s1, \s3
  169. bsl \s3, \t3, \t2
  170. eor \t3, \t3, \t2
  171. and \s2, \s0, \s3
  172. eor \t1, \t1, \t0
  173. eor \s2, \s2, \t3
  174. mul_gf16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \
  175. \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
  176. .endm
  177. .macro sbox, b0, b1, b2, b3, b4, b5, b6, b7, \
  178. t0, t1, t2, t3, s0, s1, s2, s3
  179. in_bs_ch \b0\().16b, \b1\().16b, \b2\().16b, \b3\().16b, \
  180. \b4\().16b, \b5\().16b, \b6\().16b, \b7\().16b
  181. inv_gf256 \b6\().16b, \b5\().16b, \b0\().16b, \b3\().16b, \
  182. \b7\().16b, \b1\().16b, \b4\().16b, \b2\().16b, \
  183. \t0\().16b, \t1\().16b, \t2\().16b, \t3\().16b, \
  184. \s0\().16b, \s1\().16b, \s2\().16b, \s3\().16b
  185. out_bs_ch \b7\().16b, \b1\().16b, \b4\().16b, \b2\().16b, \
  186. \b6\().16b, \b5\().16b, \b0\().16b, \b3\().16b
  187. .endm
  188. .macro inv_sbox, b0, b1, b2, b3, b4, b5, b6, b7, \
  189. t0, t1, t2, t3, s0, s1, s2, s3
  190. inv_in_bs_ch \b0\().16b, \b1\().16b, \b2\().16b, \b3\().16b, \
  191. \b4\().16b, \b5\().16b, \b6\().16b, \b7\().16b
  192. inv_gf256 \b5\().16b, \b1\().16b, \b2\().16b, \b6\().16b, \
  193. \b3\().16b, \b7\().16b, \b0\().16b, \b4\().16b, \
  194. \t0\().16b, \t1\().16b, \t2\().16b, \t3\().16b, \
  195. \s0\().16b, \s1\().16b, \s2\().16b, \s3\().16b
  196. inv_out_bs_ch \b3\().16b, \b7\().16b, \b0\().16b, \b4\().16b, \
  197. \b5\().16b, \b1\().16b, \b2\().16b, \b6\().16b
  198. .endm
  199. .macro enc_next_rk
  200. ldp q16, q17, [bskey], #128
  201. ldp q18, q19, [bskey, #-96]
  202. ldp q20, q21, [bskey, #-64]
  203. ldp q22, q23, [bskey, #-32]
  204. .endm
  205. .macro dec_next_rk
  206. ldp q16, q17, [bskey, #-128]!
  207. ldp q18, q19, [bskey, #32]
  208. ldp q20, q21, [bskey, #64]
  209. ldp q22, q23, [bskey, #96]
  210. .endm
  211. .macro add_round_key, x0, x1, x2, x3, x4, x5, x6, x7
  212. eor \x0\().16b, \x0\().16b, v16.16b
  213. eor \x1\().16b, \x1\().16b, v17.16b
  214. eor \x2\().16b, \x2\().16b, v18.16b
  215. eor \x3\().16b, \x3\().16b, v19.16b
  216. eor \x4\().16b, \x4\().16b, v20.16b
  217. eor \x5\().16b, \x5\().16b, v21.16b
  218. eor \x6\().16b, \x6\().16b, v22.16b
  219. eor \x7\().16b, \x7\().16b, v23.16b
  220. .endm
  221. .macro shift_rows, x0, x1, x2, x3, x4, x5, x6, x7, mask
  222. tbl \x0\().16b, {\x0\().16b}, \mask\().16b
  223. tbl \x1\().16b, {\x1\().16b}, \mask\().16b
  224. tbl \x2\().16b, {\x2\().16b}, \mask\().16b
  225. tbl \x3\().16b, {\x3\().16b}, \mask\().16b
  226. tbl \x4\().16b, {\x4\().16b}, \mask\().16b
  227. tbl \x5\().16b, {\x5\().16b}, \mask\().16b
  228. tbl \x6\().16b, {\x6\().16b}, \mask\().16b
  229. tbl \x7\().16b, {\x7\().16b}, \mask\().16b
  230. .endm
  231. .macro mix_cols, x0, x1, x2, x3, x4, x5, x6, x7, \
  232. t0, t1, t2, t3, t4, t5, t6, t7, inv
  233. ext \t0\().16b, \x0\().16b, \x0\().16b, #12
  234. ext \t1\().16b, \x1\().16b, \x1\().16b, #12
  235. eor \x0\().16b, \x0\().16b, \t0\().16b
  236. ext \t2\().16b, \x2\().16b, \x2\().16b, #12
  237. eor \x1\().16b, \x1\().16b, \t1\().16b
  238. ext \t3\().16b, \x3\().16b, \x3\().16b, #12
  239. eor \x2\().16b, \x2\().16b, \t2\().16b
  240. ext \t4\().16b, \x4\().16b, \x4\().16b, #12
  241. eor \x3\().16b, \x3\().16b, \t3\().16b
  242. ext \t5\().16b, \x5\().16b, \x5\().16b, #12
  243. eor \x4\().16b, \x4\().16b, \t4\().16b
  244. ext \t6\().16b, \x6\().16b, \x6\().16b, #12
  245. eor \x5\().16b, \x5\().16b, \t5\().16b
  246. ext \t7\().16b, \x7\().16b, \x7\().16b, #12
  247. eor \x6\().16b, \x6\().16b, \t6\().16b
  248. eor \t1\().16b, \t1\().16b, \x0\().16b
  249. eor \x7\().16b, \x7\().16b, \t7\().16b
  250. ext \x0\().16b, \x0\().16b, \x0\().16b, #8
  251. eor \t2\().16b, \t2\().16b, \x1\().16b
  252. eor \t0\().16b, \t0\().16b, \x7\().16b
  253. eor \t1\().16b, \t1\().16b, \x7\().16b
  254. ext \x1\().16b, \x1\().16b, \x1\().16b, #8
  255. eor \t5\().16b, \t5\().16b, \x4\().16b
  256. eor \x0\().16b, \x0\().16b, \t0\().16b
  257. eor \t6\().16b, \t6\().16b, \x5\().16b
  258. eor \x1\().16b, \x1\().16b, \t1\().16b
  259. ext \t0\().16b, \x4\().16b, \x4\().16b, #8
  260. eor \t4\().16b, \t4\().16b, \x3\().16b
  261. ext \t1\().16b, \x5\().16b, \x5\().16b, #8
  262. eor \t7\().16b, \t7\().16b, \x6\().16b
  263. ext \x4\().16b, \x3\().16b, \x3\().16b, #8
  264. eor \t3\().16b, \t3\().16b, \x2\().16b
  265. ext \x5\().16b, \x7\().16b, \x7\().16b, #8
  266. eor \t4\().16b, \t4\().16b, \x7\().16b
  267. ext \x3\().16b, \x6\().16b, \x6\().16b, #8
  268. eor \t3\().16b, \t3\().16b, \x7\().16b
  269. ext \x6\().16b, \x2\().16b, \x2\().16b, #8
  270. eor \x7\().16b, \t1\().16b, \t5\().16b
  271. .ifb \inv
  272. eor \x2\().16b, \t0\().16b, \t4\().16b
  273. eor \x4\().16b, \x4\().16b, \t3\().16b
  274. eor \x5\().16b, \x5\().16b, \t7\().16b
  275. eor \x3\().16b, \x3\().16b, \t6\().16b
  276. eor \x6\().16b, \x6\().16b, \t2\().16b
  277. .else
  278. eor \t3\().16b, \t3\().16b, \x4\().16b
  279. eor \x5\().16b, \x5\().16b, \t7\().16b
  280. eor \x2\().16b, \x3\().16b, \t6\().16b
  281. eor \x3\().16b, \t0\().16b, \t4\().16b
  282. eor \x4\().16b, \x6\().16b, \t2\().16b
  283. mov \x6\().16b, \t3\().16b
  284. .endif
  285. .endm
  286. .macro inv_mix_cols, x0, x1, x2, x3, x4, x5, x6, x7, \
  287. t0, t1, t2, t3, t4, t5, t6, t7
  288. ext \t0\().16b, \x0\().16b, \x0\().16b, #8
  289. ext \t6\().16b, \x6\().16b, \x6\().16b, #8
  290. ext \t7\().16b, \x7\().16b, \x7\().16b, #8
  291. eor \t0\().16b, \t0\().16b, \x0\().16b
  292. ext \t1\().16b, \x1\().16b, \x1\().16b, #8
  293. eor \t6\().16b, \t6\().16b, \x6\().16b
  294. ext \t2\().16b, \x2\().16b, \x2\().16b, #8
  295. eor \t7\().16b, \t7\().16b, \x7\().16b
  296. ext \t3\().16b, \x3\().16b, \x3\().16b, #8
  297. eor \t1\().16b, \t1\().16b, \x1\().16b
  298. ext \t4\().16b, \x4\().16b, \x4\().16b, #8
  299. eor \t2\().16b, \t2\().16b, \x2\().16b
  300. ext \t5\().16b, \x5\().16b, \x5\().16b, #8
  301. eor \t3\().16b, \t3\().16b, \x3\().16b
  302. eor \t4\().16b, \t4\().16b, \x4\().16b
  303. eor \t5\().16b, \t5\().16b, \x5\().16b
  304. eor \x0\().16b, \x0\().16b, \t6\().16b
  305. eor \x1\().16b, \x1\().16b, \t6\().16b
  306. eor \x2\().16b, \x2\().16b, \t0\().16b
  307. eor \x4\().16b, \x4\().16b, \t2\().16b
  308. eor \x3\().16b, \x3\().16b, \t1\().16b
  309. eor \x1\().16b, \x1\().16b, \t7\().16b
  310. eor \x2\().16b, \x2\().16b, \t7\().16b
  311. eor \x4\().16b, \x4\().16b, \t6\().16b
  312. eor \x5\().16b, \x5\().16b, \t3\().16b
  313. eor \x3\().16b, \x3\().16b, \t6\().16b
  314. eor \x6\().16b, \x6\().16b, \t4\().16b
  315. eor \x4\().16b, \x4\().16b, \t7\().16b
  316. eor \x5\().16b, \x5\().16b, \t7\().16b
  317. eor \x7\().16b, \x7\().16b, \t5\().16b
  318. mix_cols \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \
  319. \t0, \t1, \t2, \t3, \t4, \t5, \t6, \t7, 1
  320. .endm
  321. .macro swapmove_2x, a0, b0, a1, b1, n, mask, t0, t1
  322. ushr \t0\().2d, \b0\().2d, #\n
  323. ushr \t1\().2d, \b1\().2d, #\n
  324. eor \t0\().16b, \t0\().16b, \a0\().16b
  325. eor \t1\().16b, \t1\().16b, \a1\().16b
  326. and \t0\().16b, \t0\().16b, \mask\().16b
  327. and \t1\().16b, \t1\().16b, \mask\().16b
  328. eor \a0\().16b, \a0\().16b, \t0\().16b
  329. shl \t0\().2d, \t0\().2d, #\n
  330. eor \a1\().16b, \a1\().16b, \t1\().16b
  331. shl \t1\().2d, \t1\().2d, #\n
  332. eor \b0\().16b, \b0\().16b, \t0\().16b
  333. eor \b1\().16b, \b1\().16b, \t1\().16b
  334. .endm
  335. .macro bitslice, x7, x6, x5, x4, x3, x2, x1, x0, t0, t1, t2, t3
  336. movi \t0\().16b, #0x55
  337. movi \t1\().16b, #0x33
  338. swapmove_2x \x0, \x1, \x2, \x3, 1, \t0, \t2, \t3
  339. swapmove_2x \x4, \x5, \x6, \x7, 1, \t0, \t2, \t3
  340. movi \t0\().16b, #0x0f
  341. swapmove_2x \x0, \x2, \x1, \x3, 2, \t1, \t2, \t3
  342. swapmove_2x \x4, \x6, \x5, \x7, 2, \t1, \t2, \t3
  343. swapmove_2x \x0, \x4, \x1, \x5, 4, \t0, \t2, \t3
  344. swapmove_2x \x2, \x6, \x3, \x7, 4, \t0, \t2, \t3
  345. .endm
  346. .align 6
  347. M0: .octa 0x0004080c0105090d02060a0e03070b0f
  348. M0SR: .octa 0x0004080c05090d010a0e02060f03070b
  349. SR: .octa 0x0f0e0d0c0a09080b0504070600030201
  350. SRM0: .octa 0x01060b0c0207080d0304090e00050a0f
  351. M0ISR: .octa 0x0004080c0d0105090a0e0206070b0f03
  352. ISR: .octa 0x0f0e0d0c080b0a090504070602010003
  353. ISRM0: .octa 0x0306090c00070a0d01040b0e0205080f
  354. /*
  355. * void aesbs_convert_key(u8 out[], u32 const rk[], int rounds)
  356. */
  357. SYM_FUNC_START(aesbs_convert_key)
  358. ld1 {v7.4s}, [x1], #16 // load round 0 key
  359. ld1 {v17.4s}, [x1], #16 // load round 1 key
  360. movi v8.16b, #0x01 // bit masks
  361. movi v9.16b, #0x02
  362. movi v10.16b, #0x04
  363. movi v11.16b, #0x08
  364. movi v12.16b, #0x10
  365. movi v13.16b, #0x20
  366. movi v14.16b, #0x40
  367. movi v15.16b, #0x80
  368. ldr q16, M0
  369. sub x2, x2, #1
  370. str q7, [x0], #16 // save round 0 key
  371. .Lkey_loop:
  372. tbl v7.16b ,{v17.16b}, v16.16b
  373. ld1 {v17.4s}, [x1], #16 // load next round key
  374. cmtst v0.16b, v7.16b, v8.16b
  375. cmtst v1.16b, v7.16b, v9.16b
  376. cmtst v2.16b, v7.16b, v10.16b
  377. cmtst v3.16b, v7.16b, v11.16b
  378. cmtst v4.16b, v7.16b, v12.16b
  379. cmtst v5.16b, v7.16b, v13.16b
  380. cmtst v6.16b, v7.16b, v14.16b
  381. cmtst v7.16b, v7.16b, v15.16b
  382. not v0.16b, v0.16b
  383. not v1.16b, v1.16b
  384. not v5.16b, v5.16b
  385. not v6.16b, v6.16b
  386. subs x2, x2, #1
  387. stp q0, q1, [x0], #128
  388. stp q2, q3, [x0, #-96]
  389. stp q4, q5, [x0, #-64]
  390. stp q6, q7, [x0, #-32]
  391. b.ne .Lkey_loop
  392. movi v7.16b, #0x63 // compose .L63
  393. eor v17.16b, v17.16b, v7.16b
  394. str q17, [x0]
  395. ret
  396. SYM_FUNC_END(aesbs_convert_key)
  397. .align 4
  398. SYM_FUNC_START_LOCAL(aesbs_encrypt8)
  399. ldr q9, [bskey], #16 // round 0 key
  400. ldr q8, M0SR
  401. ldr q24, SR
  402. eor v10.16b, v0.16b, v9.16b // xor with round0 key
  403. eor v11.16b, v1.16b, v9.16b
  404. tbl v0.16b, {v10.16b}, v8.16b
  405. eor v12.16b, v2.16b, v9.16b
  406. tbl v1.16b, {v11.16b}, v8.16b
  407. eor v13.16b, v3.16b, v9.16b
  408. tbl v2.16b, {v12.16b}, v8.16b
  409. eor v14.16b, v4.16b, v9.16b
  410. tbl v3.16b, {v13.16b}, v8.16b
  411. eor v15.16b, v5.16b, v9.16b
  412. tbl v4.16b, {v14.16b}, v8.16b
  413. eor v10.16b, v6.16b, v9.16b
  414. tbl v5.16b, {v15.16b}, v8.16b
  415. eor v11.16b, v7.16b, v9.16b
  416. tbl v6.16b, {v10.16b}, v8.16b
  417. tbl v7.16b, {v11.16b}, v8.16b
  418. bitslice v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
  419. sub rounds, rounds, #1
  420. b .Lenc_sbox
  421. .Lenc_loop:
  422. shift_rows v0, v1, v2, v3, v4, v5, v6, v7, v24
  423. .Lenc_sbox:
  424. sbox v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, \
  425. v13, v14, v15
  426. subs rounds, rounds, #1
  427. b.cc .Lenc_done
  428. enc_next_rk
  429. mix_cols v0, v1, v4, v6, v3, v7, v2, v5, v8, v9, v10, v11, v12, \
  430. v13, v14, v15
  431. add_round_key v0, v1, v2, v3, v4, v5, v6, v7
  432. b.ne .Lenc_loop
  433. ldr q24, SRM0
  434. b .Lenc_loop
  435. .Lenc_done:
  436. ldr q12, [bskey] // last round key
  437. bitslice v0, v1, v4, v6, v3, v7, v2, v5, v8, v9, v10, v11
  438. eor v0.16b, v0.16b, v12.16b
  439. eor v1.16b, v1.16b, v12.16b
  440. eor v4.16b, v4.16b, v12.16b
  441. eor v6.16b, v6.16b, v12.16b
  442. eor v3.16b, v3.16b, v12.16b
  443. eor v7.16b, v7.16b, v12.16b
  444. eor v2.16b, v2.16b, v12.16b
  445. eor v5.16b, v5.16b, v12.16b
  446. ret
  447. SYM_FUNC_END(aesbs_encrypt8)
  448. .align 4
  449. SYM_FUNC_START_LOCAL(aesbs_decrypt8)
  450. lsl x9, rounds, #7
  451. add bskey, bskey, x9
  452. ldr q9, [bskey, #-112]! // round 0 key
  453. ldr q8, M0ISR
  454. ldr q24, ISR
  455. eor v10.16b, v0.16b, v9.16b // xor with round0 key
  456. eor v11.16b, v1.16b, v9.16b
  457. tbl v0.16b, {v10.16b}, v8.16b
  458. eor v12.16b, v2.16b, v9.16b
  459. tbl v1.16b, {v11.16b}, v8.16b
  460. eor v13.16b, v3.16b, v9.16b
  461. tbl v2.16b, {v12.16b}, v8.16b
  462. eor v14.16b, v4.16b, v9.16b
  463. tbl v3.16b, {v13.16b}, v8.16b
  464. eor v15.16b, v5.16b, v9.16b
  465. tbl v4.16b, {v14.16b}, v8.16b
  466. eor v10.16b, v6.16b, v9.16b
  467. tbl v5.16b, {v15.16b}, v8.16b
  468. eor v11.16b, v7.16b, v9.16b
  469. tbl v6.16b, {v10.16b}, v8.16b
  470. tbl v7.16b, {v11.16b}, v8.16b
  471. bitslice v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
  472. sub rounds, rounds, #1
  473. b .Ldec_sbox
  474. .Ldec_loop:
  475. shift_rows v0, v1, v2, v3, v4, v5, v6, v7, v24
  476. .Ldec_sbox:
  477. inv_sbox v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, \
  478. v13, v14, v15
  479. subs rounds, rounds, #1
  480. b.cc .Ldec_done
  481. dec_next_rk
  482. add_round_key v0, v1, v6, v4, v2, v7, v3, v5
  483. inv_mix_cols v0, v1, v6, v4, v2, v7, v3, v5, v8, v9, v10, v11, v12, \
  484. v13, v14, v15
  485. b.ne .Ldec_loop
  486. ldr q24, ISRM0
  487. b .Ldec_loop
  488. .Ldec_done:
  489. ldr q12, [bskey, #-16] // last round key
  490. bitslice v0, v1, v6, v4, v2, v7, v3, v5, v8, v9, v10, v11
  491. eor v0.16b, v0.16b, v12.16b
  492. eor v1.16b, v1.16b, v12.16b
  493. eor v6.16b, v6.16b, v12.16b
  494. eor v4.16b, v4.16b, v12.16b
  495. eor v2.16b, v2.16b, v12.16b
  496. eor v7.16b, v7.16b, v12.16b
  497. eor v3.16b, v3.16b, v12.16b
  498. eor v5.16b, v5.16b, v12.16b
  499. ret
  500. SYM_FUNC_END(aesbs_decrypt8)
  501. /*
  502. * aesbs_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[], int rounds,
  503. * int blocks)
  504. * aesbs_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[], int rounds,
  505. * int blocks)
  506. */
  507. .macro __ecb_crypt, do8, o0, o1, o2, o3, o4, o5, o6, o7
  508. frame_push 5
  509. mov x19, x0
  510. mov x20, x1
  511. mov x21, x2
  512. mov x22, x3
  513. mov x23, x4
  514. 99: mov x5, #1
  515. lsl x5, x5, x23
  516. subs w23, w23, #8
  517. csel x23, x23, xzr, pl
  518. csel x5, x5, xzr, mi
  519. ld1 {v0.16b}, [x20], #16
  520. tbnz x5, #1, 0f
  521. ld1 {v1.16b}, [x20], #16
  522. tbnz x5, #2, 0f
  523. ld1 {v2.16b}, [x20], #16
  524. tbnz x5, #3, 0f
  525. ld1 {v3.16b}, [x20], #16
  526. tbnz x5, #4, 0f
  527. ld1 {v4.16b}, [x20], #16
  528. tbnz x5, #5, 0f
  529. ld1 {v5.16b}, [x20], #16
  530. tbnz x5, #6, 0f
  531. ld1 {v6.16b}, [x20], #16
  532. tbnz x5, #7, 0f
  533. ld1 {v7.16b}, [x20], #16
  534. 0: mov bskey, x21
  535. mov rounds, x22
  536. bl \do8
  537. st1 {\o0\().16b}, [x19], #16
  538. tbnz x5, #1, 1f
  539. st1 {\o1\().16b}, [x19], #16
  540. tbnz x5, #2, 1f
  541. st1 {\o2\().16b}, [x19], #16
  542. tbnz x5, #3, 1f
  543. st1 {\o3\().16b}, [x19], #16
  544. tbnz x5, #4, 1f
  545. st1 {\o4\().16b}, [x19], #16
  546. tbnz x5, #5, 1f
  547. st1 {\o5\().16b}, [x19], #16
  548. tbnz x5, #6, 1f
  549. st1 {\o6\().16b}, [x19], #16
  550. tbnz x5, #7, 1f
  551. st1 {\o7\().16b}, [x19], #16
  552. cbz x23, 1f
  553. b 99b
  554. 1: frame_pop
  555. ret
  556. .endm
  557. .align 4
  558. SYM_TYPED_FUNC_START(aesbs_ecb_encrypt)
  559. __ecb_crypt aesbs_encrypt8, v0, v1, v4, v6, v3, v7, v2, v5
  560. SYM_FUNC_END(aesbs_ecb_encrypt)
  561. .align 4
  562. SYM_TYPED_FUNC_START(aesbs_ecb_decrypt)
  563. __ecb_crypt aesbs_decrypt8, v0, v1, v6, v4, v2, v7, v3, v5
  564. SYM_FUNC_END(aesbs_ecb_decrypt)
  565. /*
  566. * aesbs_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[], int rounds,
  567. * int blocks, u8 iv[])
  568. */
  569. .align 4
  570. SYM_FUNC_START(aesbs_cbc_decrypt)
  571. frame_push 6
  572. mov x19, x0
  573. mov x20, x1
  574. mov x21, x2
  575. mov x22, x3
  576. mov x23, x4
  577. mov x24, x5
  578. 99: mov x6, #1
  579. lsl x6, x6, x23
  580. subs w23, w23, #8
  581. csel x23, x23, xzr, pl
  582. csel x6, x6, xzr, mi
  583. ld1 {v0.16b}, [x20], #16
  584. mov v25.16b, v0.16b
  585. tbnz x6, #1, 0f
  586. ld1 {v1.16b}, [x20], #16
  587. mov v26.16b, v1.16b
  588. tbnz x6, #2, 0f
  589. ld1 {v2.16b}, [x20], #16
  590. mov v27.16b, v2.16b
  591. tbnz x6, #3, 0f
  592. ld1 {v3.16b}, [x20], #16
  593. mov v28.16b, v3.16b
  594. tbnz x6, #4, 0f
  595. ld1 {v4.16b}, [x20], #16
  596. mov v29.16b, v4.16b
  597. tbnz x6, #5, 0f
  598. ld1 {v5.16b}, [x20], #16
  599. mov v30.16b, v5.16b
  600. tbnz x6, #6, 0f
  601. ld1 {v6.16b}, [x20], #16
  602. mov v31.16b, v6.16b
  603. tbnz x6, #7, 0f
  604. ld1 {v7.16b}, [x20]
  605. 0: mov bskey, x21
  606. mov rounds, x22
  607. bl aesbs_decrypt8
  608. ld1 {v24.16b}, [x24] // load IV
  609. eor v1.16b, v1.16b, v25.16b
  610. eor v6.16b, v6.16b, v26.16b
  611. eor v4.16b, v4.16b, v27.16b
  612. eor v2.16b, v2.16b, v28.16b
  613. eor v7.16b, v7.16b, v29.16b
  614. eor v0.16b, v0.16b, v24.16b
  615. eor v3.16b, v3.16b, v30.16b
  616. eor v5.16b, v5.16b, v31.16b
  617. st1 {v0.16b}, [x19], #16
  618. mov v24.16b, v25.16b
  619. tbnz x6, #1, 1f
  620. st1 {v1.16b}, [x19], #16
  621. mov v24.16b, v26.16b
  622. tbnz x6, #2, 1f
  623. st1 {v6.16b}, [x19], #16
  624. mov v24.16b, v27.16b
  625. tbnz x6, #3, 1f
  626. st1 {v4.16b}, [x19], #16
  627. mov v24.16b, v28.16b
  628. tbnz x6, #4, 1f
  629. st1 {v2.16b}, [x19], #16
  630. mov v24.16b, v29.16b
  631. tbnz x6, #5, 1f
  632. st1 {v7.16b}, [x19], #16
  633. mov v24.16b, v30.16b
  634. tbnz x6, #6, 1f
  635. st1 {v3.16b}, [x19], #16
  636. mov v24.16b, v31.16b
  637. tbnz x6, #7, 1f
  638. ld1 {v24.16b}, [x20], #16
  639. st1 {v5.16b}, [x19], #16
  640. 1: st1 {v24.16b}, [x24] // store IV
  641. cbz x23, 2f
  642. b 99b
  643. 2: frame_pop
  644. ret
  645. SYM_FUNC_END(aesbs_cbc_decrypt)
  646. .macro next_tweak, out, in, const, tmp
  647. sshr \tmp\().2d, \in\().2d, #63
  648. and \tmp\().16b, \tmp\().16b, \const\().16b
  649. add \out\().2d, \in\().2d, \in\().2d
  650. ext \tmp\().16b, \tmp\().16b, \tmp\().16b, #8
  651. eor \out\().16b, \out\().16b, \tmp\().16b
  652. .endm
  653. /*
  654. * aesbs_xts_encrypt(u8 out[], u8 const in[], u8 const rk[], int rounds,
  655. * int blocks, u8 iv[])
  656. * aesbs_xts_decrypt(u8 out[], u8 const in[], u8 const rk[], int rounds,
  657. * int blocks, u8 iv[])
  658. */
  659. SYM_FUNC_START_LOCAL(__xts_crypt8)
  660. movi v18.2s, #0x1
  661. movi v19.2s, #0x87
  662. uzp1 v18.4s, v18.4s, v19.4s
  663. ld1 {v0.16b-v3.16b}, [x1], #64
  664. ld1 {v4.16b-v7.16b}, [x1], #64
  665. next_tweak v26, v25, v18, v19
  666. next_tweak v27, v26, v18, v19
  667. next_tweak v28, v27, v18, v19
  668. next_tweak v29, v28, v18, v19
  669. next_tweak v30, v29, v18, v19
  670. next_tweak v31, v30, v18, v19
  671. next_tweak v16, v31, v18, v19
  672. next_tweak v17, v16, v18, v19
  673. eor v0.16b, v0.16b, v25.16b
  674. eor v1.16b, v1.16b, v26.16b
  675. eor v2.16b, v2.16b, v27.16b
  676. eor v3.16b, v3.16b, v28.16b
  677. eor v4.16b, v4.16b, v29.16b
  678. eor v5.16b, v5.16b, v30.16b
  679. eor v6.16b, v6.16b, v31.16b
  680. eor v7.16b, v7.16b, v16.16b
  681. stp q16, q17, [x6]
  682. mov bskey, x2
  683. mov rounds, x3
  684. br x16
  685. SYM_FUNC_END(__xts_crypt8)
  686. .macro __xts_crypt, do8, o0, o1, o2, o3, o4, o5, o6, o7
  687. frame_push 0, 32
  688. add x6, sp, #.Lframe_local_offset
  689. ld1 {v25.16b}, [x5]
  690. 0: adr x16, \do8
  691. bl __xts_crypt8
  692. eor v16.16b, \o0\().16b, v25.16b
  693. eor v17.16b, \o1\().16b, v26.16b
  694. eor v18.16b, \o2\().16b, v27.16b
  695. eor v19.16b, \o3\().16b, v28.16b
  696. ldp q24, q25, [x6]
  697. eor v20.16b, \o4\().16b, v29.16b
  698. eor v21.16b, \o5\().16b, v30.16b
  699. eor v22.16b, \o6\().16b, v31.16b
  700. eor v23.16b, \o7\().16b, v24.16b
  701. st1 {v16.16b-v19.16b}, [x0], #64
  702. st1 {v20.16b-v23.16b}, [x0], #64
  703. subs x4, x4, #8
  704. b.gt 0b
  705. st1 {v25.16b}, [x5]
  706. frame_pop
  707. ret
  708. .endm
  709. SYM_TYPED_FUNC_START(aesbs_xts_encrypt)
  710. __xts_crypt aesbs_encrypt8, v0, v1, v4, v6, v3, v7, v2, v5
  711. SYM_FUNC_END(aesbs_xts_encrypt)
  712. SYM_TYPED_FUNC_START(aesbs_xts_decrypt)
  713. __xts_crypt aesbs_decrypt8, v0, v1, v6, v4, v2, v7, v3, v5
  714. SYM_FUNC_END(aesbs_xts_decrypt)
  715. .macro next_ctr, v
  716. mov \v\().d[1], x8
  717. adds x8, x8, #1
  718. mov \v\().d[0], x7
  719. adc x7, x7, xzr
  720. rev64 \v\().16b, \v\().16b
  721. .endm
  722. /*
  723. * aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
  724. * int rounds, int blocks, u8 iv[])
  725. */
  726. SYM_FUNC_START(aesbs_ctr_encrypt)
  727. frame_push 0
  728. ldp x7, x8, [x5]
  729. ld1 {v0.16b}, [x5]
  730. CPU_LE( rev x7, x7 )
  731. CPU_LE( rev x8, x8 )
  732. adds x8, x8, #1
  733. adc x7, x7, xzr
  734. 0: next_ctr v1
  735. next_ctr v2
  736. next_ctr v3
  737. next_ctr v4
  738. next_ctr v5
  739. next_ctr v6
  740. next_ctr v7
  741. mov bskey, x2
  742. mov rounds, x3
  743. bl aesbs_encrypt8
  744. ld1 { v8.16b-v11.16b}, [x1], #64
  745. ld1 {v12.16b-v15.16b}, [x1], #64
  746. eor v8.16b, v0.16b, v8.16b
  747. eor v9.16b, v1.16b, v9.16b
  748. eor v10.16b, v4.16b, v10.16b
  749. eor v11.16b, v6.16b, v11.16b
  750. eor v12.16b, v3.16b, v12.16b
  751. eor v13.16b, v7.16b, v13.16b
  752. eor v14.16b, v2.16b, v14.16b
  753. eor v15.16b, v5.16b, v15.16b
  754. st1 { v8.16b-v11.16b}, [x0], #64
  755. st1 {v12.16b-v15.16b}, [x0], #64
  756. next_ctr v0
  757. subs x4, x4, #8
  758. b.gt 0b
  759. st1 {v0.16b}, [x5]
  760. frame_pop
  761. ret
  762. SYM_FUNC_END(aesbs_ctr_encrypt)