chacha-neon-core.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * ChaCha/XChaCha NEON helper functions
  3. *
  4. * Copyright (C) 2016 Linaro, Ltd. <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on:
  11. * ChaCha20 256-bit cipher algorithm, RFC7539, x64 SSE3 functions
  12. *
  13. * Copyright (C) 2015 Martin Willi
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. */
  20. /*
  21. * NEON doesn't have a rotate instruction. The alternatives are, more or less:
  22. *
  23. * (a) vshl.u32 + vsri.u32 (needs temporary register)
  24. * (b) vshl.u32 + vshr.u32 + vorr (needs temporary register)
  25. * (c) vrev32.16 (16-bit rotations only)
  26. * (d) vtbl.8 + vtbl.8 (multiple of 8 bits rotations only,
  27. * needs index vector)
  28. *
  29. * ChaCha has 16, 12, 8, and 7-bit rotations. For the 12 and 7-bit rotations,
  30. * the only choices are (a) and (b). We use (a) since it takes two-thirds the
  31. * cycles of (b) on both Cortex-A7 and Cortex-A53.
  32. *
  33. * For the 16-bit rotation, we use vrev32.16 since it's consistently fastest
  34. * and doesn't need a temporary register.
  35. *
  36. * For the 8-bit rotation, we use vtbl.8 + vtbl.8. On Cortex-A7, this sequence
  37. * is twice as fast as (a), even when doing (a) on multiple registers
  38. * simultaneously to eliminate the stall between vshl and vsri. Also, it
  39. * parallelizes better when temporary registers are scarce.
  40. *
  41. * A disadvantage is that on Cortex-A53, the vtbl sequence is the same speed as
  42. * (a), so the need to load the rotation table actually makes the vtbl method
  43. * slightly slower overall on that CPU (~1.3% slower ChaCha20). Still, it
  44. * seems to be a good compromise to get a more significant speed boost on some
  45. * CPUs, e.g. ~4.8% faster ChaCha20 on Cortex-A7.
  46. */
  47. #include <linux/linkage.h>
  48. .text
  49. .fpu neon
  50. .align 5
  51. /*
  52. * chacha_permute - permute one block
  53. *
  54. * Permute one 64-byte block where the state matrix is stored in the four NEON
  55. * registers q0-q3. It performs matrix operations on four words in parallel,
  56. * but requires shuffling to rearrange the words after each round.
  57. *
  58. * The round count is given in r3.
  59. *
  60. * Clobbers: r3, ip, q4-q5
  61. */
  62. chacha_permute:
  63. adr ip, .Lrol8_table
  64. vld1.8 {d10}, [ip, :64]
  65. .Ldoubleround:
  66. // x0 += x1, x3 = rotl32(x3 ^ x0, 16)
  67. vadd.i32 q0, q0, q1
  68. veor q3, q3, q0
  69. vrev32.16 q3, q3
  70. // x2 += x3, x1 = rotl32(x1 ^ x2, 12)
  71. vadd.i32 q2, q2, q3
  72. veor q4, q1, q2
  73. vshl.u32 q1, q4, #12
  74. vsri.u32 q1, q4, #20
  75. // x0 += x1, x3 = rotl32(x3 ^ x0, 8)
  76. vadd.i32 q0, q0, q1
  77. veor q3, q3, q0
  78. vtbl.8 d6, {d6}, d10
  79. vtbl.8 d7, {d7}, d10
  80. // x2 += x3, x1 = rotl32(x1 ^ x2, 7)
  81. vadd.i32 q2, q2, q3
  82. veor q4, q1, q2
  83. vshl.u32 q1, q4, #7
  84. vsri.u32 q1, q4, #25
  85. // x1 = shuffle32(x1, MASK(0, 3, 2, 1))
  86. vext.8 q1, q1, q1, #4
  87. // x2 = shuffle32(x2, MASK(1, 0, 3, 2))
  88. vext.8 q2, q2, q2, #8
  89. // x3 = shuffle32(x3, MASK(2, 1, 0, 3))
  90. vext.8 q3, q3, q3, #12
  91. // x0 += x1, x3 = rotl32(x3 ^ x0, 16)
  92. vadd.i32 q0, q0, q1
  93. veor q3, q3, q0
  94. vrev32.16 q3, q3
  95. // x2 += x3, x1 = rotl32(x1 ^ x2, 12)
  96. vadd.i32 q2, q2, q3
  97. veor q4, q1, q2
  98. vshl.u32 q1, q4, #12
  99. vsri.u32 q1, q4, #20
  100. // x0 += x1, x3 = rotl32(x3 ^ x0, 8)
  101. vadd.i32 q0, q0, q1
  102. veor q3, q3, q0
  103. vtbl.8 d6, {d6}, d10
  104. vtbl.8 d7, {d7}, d10
  105. // x2 += x3, x1 = rotl32(x1 ^ x2, 7)
  106. vadd.i32 q2, q2, q3
  107. veor q4, q1, q2
  108. vshl.u32 q1, q4, #7
  109. vsri.u32 q1, q4, #25
  110. // x1 = shuffle32(x1, MASK(2, 1, 0, 3))
  111. vext.8 q1, q1, q1, #12
  112. // x2 = shuffle32(x2, MASK(1, 0, 3, 2))
  113. vext.8 q2, q2, q2, #8
  114. // x3 = shuffle32(x3, MASK(0, 3, 2, 1))
  115. vext.8 q3, q3, q3, #4
  116. subs r3, r3, #2
  117. bne .Ldoubleround
  118. bx lr
  119. ENDPROC(chacha_permute)
  120. ENTRY(chacha_block_xor_neon)
  121. // r0: Input state matrix, s
  122. // r1: 1 data block output, o
  123. // r2: 1 data block input, i
  124. // r3: nrounds
  125. push {lr}
  126. // x0..3 = s0..3
  127. add ip, r0, #0x20
  128. vld1.32 {q0-q1}, [r0]
  129. vld1.32 {q2-q3}, [ip]
  130. vmov q8, q0
  131. vmov q9, q1
  132. vmov q10, q2
  133. vmov q11, q3
  134. bl chacha_permute
  135. add ip, r2, #0x20
  136. vld1.8 {q4-q5}, [r2]
  137. vld1.8 {q6-q7}, [ip]
  138. // o0 = i0 ^ (x0 + s0)
  139. vadd.i32 q0, q0, q8
  140. veor q0, q0, q4
  141. // o1 = i1 ^ (x1 + s1)
  142. vadd.i32 q1, q1, q9
  143. veor q1, q1, q5
  144. // o2 = i2 ^ (x2 + s2)
  145. vadd.i32 q2, q2, q10
  146. veor q2, q2, q6
  147. // o3 = i3 ^ (x3 + s3)
  148. vadd.i32 q3, q3, q11
  149. veor q3, q3, q7
  150. add ip, r1, #0x20
  151. vst1.8 {q0-q1}, [r1]
  152. vst1.8 {q2-q3}, [ip]
  153. pop {pc}
  154. ENDPROC(chacha_block_xor_neon)
  155. ENTRY(hchacha_block_neon)
  156. // r0: Input state matrix, s
  157. // r1: output (8 32-bit words)
  158. // r2: nrounds
  159. push {lr}
  160. vld1.32 {q0-q1}, [r0]!
  161. vld1.32 {q2-q3}, [r0]
  162. mov r3, r2
  163. bl chacha_permute
  164. vst1.32 {q0}, [r1]!
  165. vst1.32 {q3}, [r1]
  166. pop {pc}
  167. ENDPROC(hchacha_block_neon)
  168. .align 4
  169. .Lctrinc: .word 0, 1, 2, 3
  170. .Lrol8_table: .byte 3, 0, 1, 2, 7, 4, 5, 6
  171. .align 5
  172. ENTRY(chacha_4block_xor_neon)
  173. push {r4-r5}
  174. mov r4, sp // preserve the stack pointer
  175. sub ip, sp, #0x20 // allocate a 32 byte buffer
  176. bic ip, ip, #0x1f // aligned to 32 bytes
  177. mov sp, ip
  178. // r0: Input state matrix, s
  179. // r1: 4 data blocks output, o
  180. // r2: 4 data blocks input, i
  181. // r3: nrounds
  182. //
  183. // This function encrypts four consecutive ChaCha blocks by loading
  184. // the state matrix in NEON registers four times. The algorithm performs
  185. // each operation on the corresponding word of each state matrix, hence
  186. // requires no word shuffling. The words are re-interleaved before the
  187. // final addition of the original state and the XORing step.
  188. //
  189. // x0..15[0-3] = s0..15[0-3]
  190. add ip, r0, #0x20
  191. vld1.32 {q0-q1}, [r0]
  192. vld1.32 {q2-q3}, [ip]
  193. adr r5, .Lctrinc
  194. vdup.32 q15, d7[1]
  195. vdup.32 q14, d7[0]
  196. vld1.32 {q4}, [r5, :128]
  197. vdup.32 q13, d6[1]
  198. vdup.32 q12, d6[0]
  199. vdup.32 q11, d5[1]
  200. vdup.32 q10, d5[0]
  201. vadd.u32 q12, q12, q4 // x12 += counter values 0-3
  202. vdup.32 q9, d4[1]
  203. vdup.32 q8, d4[0]
  204. vdup.32 q7, d3[1]
  205. vdup.32 q6, d3[0]
  206. vdup.32 q5, d2[1]
  207. vdup.32 q4, d2[0]
  208. vdup.32 q3, d1[1]
  209. vdup.32 q2, d1[0]
  210. vdup.32 q1, d0[1]
  211. vdup.32 q0, d0[0]
  212. adr ip, .Lrol8_table
  213. b 1f
  214. .Ldoubleround4:
  215. vld1.32 {q8-q9}, [sp, :256]
  216. 1:
  217. // x0 += x4, x12 = rotl32(x12 ^ x0, 16)
  218. // x1 += x5, x13 = rotl32(x13 ^ x1, 16)
  219. // x2 += x6, x14 = rotl32(x14 ^ x2, 16)
  220. // x3 += x7, x15 = rotl32(x15 ^ x3, 16)
  221. vadd.i32 q0, q0, q4
  222. vadd.i32 q1, q1, q5
  223. vadd.i32 q2, q2, q6
  224. vadd.i32 q3, q3, q7
  225. veor q12, q12, q0
  226. veor q13, q13, q1
  227. veor q14, q14, q2
  228. veor q15, q15, q3
  229. vrev32.16 q12, q12
  230. vrev32.16 q13, q13
  231. vrev32.16 q14, q14
  232. vrev32.16 q15, q15
  233. // x8 += x12, x4 = rotl32(x4 ^ x8, 12)
  234. // x9 += x13, x5 = rotl32(x5 ^ x9, 12)
  235. // x10 += x14, x6 = rotl32(x6 ^ x10, 12)
  236. // x11 += x15, x7 = rotl32(x7 ^ x11, 12)
  237. vadd.i32 q8, q8, q12
  238. vadd.i32 q9, q9, q13
  239. vadd.i32 q10, q10, q14
  240. vadd.i32 q11, q11, q15
  241. vst1.32 {q8-q9}, [sp, :256]
  242. veor q8, q4, q8
  243. veor q9, q5, q9
  244. vshl.u32 q4, q8, #12
  245. vshl.u32 q5, q9, #12
  246. vsri.u32 q4, q8, #20
  247. vsri.u32 q5, q9, #20
  248. veor q8, q6, q10
  249. veor q9, q7, q11
  250. vshl.u32 q6, q8, #12
  251. vshl.u32 q7, q9, #12
  252. vsri.u32 q6, q8, #20
  253. vsri.u32 q7, q9, #20
  254. // x0 += x4, x12 = rotl32(x12 ^ x0, 8)
  255. // x1 += x5, x13 = rotl32(x13 ^ x1, 8)
  256. // x2 += x6, x14 = rotl32(x14 ^ x2, 8)
  257. // x3 += x7, x15 = rotl32(x15 ^ x3, 8)
  258. vld1.8 {d16}, [ip, :64]
  259. vadd.i32 q0, q0, q4
  260. vadd.i32 q1, q1, q5
  261. vadd.i32 q2, q2, q6
  262. vadd.i32 q3, q3, q7
  263. veor q12, q12, q0
  264. veor q13, q13, q1
  265. veor q14, q14, q2
  266. veor q15, q15, q3
  267. vtbl.8 d24, {d24}, d16
  268. vtbl.8 d25, {d25}, d16
  269. vtbl.8 d26, {d26}, d16
  270. vtbl.8 d27, {d27}, d16
  271. vtbl.8 d28, {d28}, d16
  272. vtbl.8 d29, {d29}, d16
  273. vtbl.8 d30, {d30}, d16
  274. vtbl.8 d31, {d31}, d16
  275. vld1.32 {q8-q9}, [sp, :256]
  276. // x8 += x12, x4 = rotl32(x4 ^ x8, 7)
  277. // x9 += x13, x5 = rotl32(x5 ^ x9, 7)
  278. // x10 += x14, x6 = rotl32(x6 ^ x10, 7)
  279. // x11 += x15, x7 = rotl32(x7 ^ x11, 7)
  280. vadd.i32 q8, q8, q12
  281. vadd.i32 q9, q9, q13
  282. vadd.i32 q10, q10, q14
  283. vadd.i32 q11, q11, q15
  284. vst1.32 {q8-q9}, [sp, :256]
  285. veor q8, q4, q8
  286. veor q9, q5, q9
  287. vshl.u32 q4, q8, #7
  288. vshl.u32 q5, q9, #7
  289. vsri.u32 q4, q8, #25
  290. vsri.u32 q5, q9, #25
  291. veor q8, q6, q10
  292. veor q9, q7, q11
  293. vshl.u32 q6, q8, #7
  294. vshl.u32 q7, q9, #7
  295. vsri.u32 q6, q8, #25
  296. vsri.u32 q7, q9, #25
  297. vld1.32 {q8-q9}, [sp, :256]
  298. // x0 += x5, x15 = rotl32(x15 ^ x0, 16)
  299. // x1 += x6, x12 = rotl32(x12 ^ x1, 16)
  300. // x2 += x7, x13 = rotl32(x13 ^ x2, 16)
  301. // x3 += x4, x14 = rotl32(x14 ^ x3, 16)
  302. vadd.i32 q0, q0, q5
  303. vadd.i32 q1, q1, q6
  304. vadd.i32 q2, q2, q7
  305. vadd.i32 q3, q3, q4
  306. veor q15, q15, q0
  307. veor q12, q12, q1
  308. veor q13, q13, q2
  309. veor q14, q14, q3
  310. vrev32.16 q15, q15
  311. vrev32.16 q12, q12
  312. vrev32.16 q13, q13
  313. vrev32.16 q14, q14
  314. // x10 += x15, x5 = rotl32(x5 ^ x10, 12)
  315. // x11 += x12, x6 = rotl32(x6 ^ x11, 12)
  316. // x8 += x13, x7 = rotl32(x7 ^ x8, 12)
  317. // x9 += x14, x4 = rotl32(x4 ^ x9, 12)
  318. vadd.i32 q10, q10, q15
  319. vadd.i32 q11, q11, q12
  320. vadd.i32 q8, q8, q13
  321. vadd.i32 q9, q9, q14
  322. vst1.32 {q8-q9}, [sp, :256]
  323. veor q8, q7, q8
  324. veor q9, q4, q9
  325. vshl.u32 q7, q8, #12
  326. vshl.u32 q4, q9, #12
  327. vsri.u32 q7, q8, #20
  328. vsri.u32 q4, q9, #20
  329. veor q8, q5, q10
  330. veor q9, q6, q11
  331. vshl.u32 q5, q8, #12
  332. vshl.u32 q6, q9, #12
  333. vsri.u32 q5, q8, #20
  334. vsri.u32 q6, q9, #20
  335. // x0 += x5, x15 = rotl32(x15 ^ x0, 8)
  336. // x1 += x6, x12 = rotl32(x12 ^ x1, 8)
  337. // x2 += x7, x13 = rotl32(x13 ^ x2, 8)
  338. // x3 += x4, x14 = rotl32(x14 ^ x3, 8)
  339. vld1.8 {d16}, [ip, :64]
  340. vadd.i32 q0, q0, q5
  341. vadd.i32 q1, q1, q6
  342. vadd.i32 q2, q2, q7
  343. vadd.i32 q3, q3, q4
  344. veor q15, q15, q0
  345. veor q12, q12, q1
  346. veor q13, q13, q2
  347. veor q14, q14, q3
  348. vtbl.8 d30, {d30}, d16
  349. vtbl.8 d31, {d31}, d16
  350. vtbl.8 d24, {d24}, d16
  351. vtbl.8 d25, {d25}, d16
  352. vtbl.8 d26, {d26}, d16
  353. vtbl.8 d27, {d27}, d16
  354. vtbl.8 d28, {d28}, d16
  355. vtbl.8 d29, {d29}, d16
  356. vld1.32 {q8-q9}, [sp, :256]
  357. // x10 += x15, x5 = rotl32(x5 ^ x10, 7)
  358. // x11 += x12, x6 = rotl32(x6 ^ x11, 7)
  359. // x8 += x13, x7 = rotl32(x7 ^ x8, 7)
  360. // x9 += x14, x4 = rotl32(x4 ^ x9, 7)
  361. vadd.i32 q10, q10, q15
  362. vadd.i32 q11, q11, q12
  363. vadd.i32 q8, q8, q13
  364. vadd.i32 q9, q9, q14
  365. vst1.32 {q8-q9}, [sp, :256]
  366. veor q8, q7, q8
  367. veor q9, q4, q9
  368. vshl.u32 q7, q8, #7
  369. vshl.u32 q4, q9, #7
  370. vsri.u32 q7, q8, #25
  371. vsri.u32 q4, q9, #25
  372. veor q8, q5, q10
  373. veor q9, q6, q11
  374. vshl.u32 q5, q8, #7
  375. vshl.u32 q6, q9, #7
  376. vsri.u32 q5, q8, #25
  377. vsri.u32 q6, q9, #25
  378. subs r3, r3, #2
  379. bne .Ldoubleround4
  380. // x0..7[0-3] are in q0-q7, x10..15[0-3] are in q10-q15.
  381. // x8..9[0-3] are on the stack.
  382. // Re-interleave the words in the first two rows of each block (x0..7).
  383. // Also add the counter values 0-3 to x12[0-3].
  384. vld1.32 {q8}, [r5, :128] // load counter values 0-3
  385. vzip.32 q0, q1 // => (0 1 0 1) (0 1 0 1)
  386. vzip.32 q2, q3 // => (2 3 2 3) (2 3 2 3)
  387. vzip.32 q4, q5 // => (4 5 4 5) (4 5 4 5)
  388. vzip.32 q6, q7 // => (6 7 6 7) (6 7 6 7)
  389. vadd.u32 q12, q8 // x12 += counter values 0-3
  390. vswp d1, d4
  391. vswp d3, d6
  392. vld1.32 {q8-q9}, [r0]! // load s0..7
  393. vswp d9, d12
  394. vswp d11, d14
  395. // Swap q1 and q4 so that we'll free up consecutive registers (q0-q1)
  396. // after XORing the first 32 bytes.
  397. vswp q1, q4
  398. // First two rows of each block are (q0 q1) (q2 q6) (q4 q5) (q3 q7)
  399. // x0..3[0-3] += s0..3[0-3] (add orig state to 1st row of each block)
  400. vadd.u32 q0, q0, q8
  401. vadd.u32 q2, q2, q8
  402. vadd.u32 q4, q4, q8
  403. vadd.u32 q3, q3, q8
  404. // x4..7[0-3] += s4..7[0-3] (add orig state to 2nd row of each block)
  405. vadd.u32 q1, q1, q9
  406. vadd.u32 q6, q6, q9
  407. vadd.u32 q5, q5, q9
  408. vadd.u32 q7, q7, q9
  409. // XOR first 32 bytes using keystream from first two rows of first block
  410. vld1.8 {q8-q9}, [r2]!
  411. veor q8, q8, q0
  412. veor q9, q9, q1
  413. vst1.8 {q8-q9}, [r1]!
  414. // Re-interleave the words in the last two rows of each block (x8..15).
  415. vld1.32 {q8-q9}, [sp, :256]
  416. vzip.32 q12, q13 // => (12 13 12 13) (12 13 12 13)
  417. vzip.32 q14, q15 // => (14 15 14 15) (14 15 14 15)
  418. vzip.32 q8, q9 // => (8 9 8 9) (8 9 8 9)
  419. vzip.32 q10, q11 // => (10 11 10 11) (10 11 10 11)
  420. vld1.32 {q0-q1}, [r0] // load s8..15
  421. vswp d25, d28
  422. vswp d27, d30
  423. vswp d17, d20
  424. vswp d19, d22
  425. // Last two rows of each block are (q8 q12) (q10 q14) (q9 q13) (q11 q15)
  426. // x8..11[0-3] += s8..11[0-3] (add orig state to 3rd row of each block)
  427. vadd.u32 q8, q8, q0
  428. vadd.u32 q10, q10, q0
  429. vadd.u32 q9, q9, q0
  430. vadd.u32 q11, q11, q0
  431. // x12..15[0-3] += s12..15[0-3] (add orig state to 4th row of each block)
  432. vadd.u32 q12, q12, q1
  433. vadd.u32 q14, q14, q1
  434. vadd.u32 q13, q13, q1
  435. vadd.u32 q15, q15, q1
  436. // XOR the rest of the data with the keystream
  437. vld1.8 {q0-q1}, [r2]!
  438. veor q0, q0, q8
  439. veor q1, q1, q12
  440. vst1.8 {q0-q1}, [r1]!
  441. vld1.8 {q0-q1}, [r2]!
  442. veor q0, q0, q2
  443. veor q1, q1, q6
  444. vst1.8 {q0-q1}, [r1]!
  445. vld1.8 {q0-q1}, [r2]!
  446. veor q0, q0, q10
  447. veor q1, q1, q14
  448. vst1.8 {q0-q1}, [r1]!
  449. vld1.8 {q0-q1}, [r2]!
  450. veor q0, q0, q4
  451. veor q1, q1, q5
  452. vst1.8 {q0-q1}, [r1]!
  453. vld1.8 {q0-q1}, [r2]!
  454. veor q0, q0, q9
  455. veor q1, q1, q13
  456. vst1.8 {q0-q1}, [r1]!
  457. vld1.8 {q0-q1}, [r2]!
  458. veor q0, q0, q3
  459. veor q1, q1, q7
  460. vst1.8 {q0-q1}, [r1]!
  461. vld1.8 {q0-q1}, [r2]
  462. mov sp, r4 // restore original stack pointer
  463. veor q0, q0, q11
  464. veor q1, q1, q15
  465. vst1.8 {q0-q1}, [r1]
  466. pop {r4-r5}
  467. bx lr
  468. ENDPROC(chacha_4block_xor_neon)