poly1305-avx2-x86_64.S 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Poly1305 authenticator algorithm, RFC7539, x64 AVX2 functions
  3. *
  4. * Copyright (C) 2015 Martin Willi
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/linkage.h>
  12. .section .rodata.cst32.ANMASK, "aM", @progbits, 32
  13. .align 32
  14. ANMASK: .octa 0x0000000003ffffff0000000003ffffff
  15. .octa 0x0000000003ffffff0000000003ffffff
  16. .section .rodata.cst32.ORMASK, "aM", @progbits, 32
  17. .align 32
  18. ORMASK: .octa 0x00000000010000000000000001000000
  19. .octa 0x00000000010000000000000001000000
  20. .text
  21. #define h0 0x00(%rdi)
  22. #define h1 0x04(%rdi)
  23. #define h2 0x08(%rdi)
  24. #define h3 0x0c(%rdi)
  25. #define h4 0x10(%rdi)
  26. #define r0 0x00(%rdx)
  27. #define r1 0x04(%rdx)
  28. #define r2 0x08(%rdx)
  29. #define r3 0x0c(%rdx)
  30. #define r4 0x10(%rdx)
  31. #define u0 0x00(%r8)
  32. #define u1 0x04(%r8)
  33. #define u2 0x08(%r8)
  34. #define u3 0x0c(%r8)
  35. #define u4 0x10(%r8)
  36. #define w0 0x14(%r8)
  37. #define w1 0x18(%r8)
  38. #define w2 0x1c(%r8)
  39. #define w3 0x20(%r8)
  40. #define w4 0x24(%r8)
  41. #define y0 0x28(%r8)
  42. #define y1 0x2c(%r8)
  43. #define y2 0x30(%r8)
  44. #define y3 0x34(%r8)
  45. #define y4 0x38(%r8)
  46. #define m %rsi
  47. #define hc0 %ymm0
  48. #define hc1 %ymm1
  49. #define hc2 %ymm2
  50. #define hc3 %ymm3
  51. #define hc4 %ymm4
  52. #define hc0x %xmm0
  53. #define hc1x %xmm1
  54. #define hc2x %xmm2
  55. #define hc3x %xmm3
  56. #define hc4x %xmm4
  57. #define t1 %ymm5
  58. #define t2 %ymm6
  59. #define t1x %xmm5
  60. #define t2x %xmm6
  61. #define ruwy0 %ymm7
  62. #define ruwy1 %ymm8
  63. #define ruwy2 %ymm9
  64. #define ruwy3 %ymm10
  65. #define ruwy4 %ymm11
  66. #define ruwy0x %xmm7
  67. #define ruwy1x %xmm8
  68. #define ruwy2x %xmm9
  69. #define ruwy3x %xmm10
  70. #define ruwy4x %xmm11
  71. #define svxz1 %ymm12
  72. #define svxz2 %ymm13
  73. #define svxz3 %ymm14
  74. #define svxz4 %ymm15
  75. #define d0 %r9
  76. #define d1 %r10
  77. #define d2 %r11
  78. #define d3 %r12
  79. #define d4 %r13
  80. ENTRY(poly1305_4block_avx2)
  81. # %rdi: Accumulator h[5]
  82. # %rsi: 64 byte input block m
  83. # %rdx: Poly1305 key r[5]
  84. # %rcx: Quadblock count
  85. # %r8: Poly1305 derived key r^2 u[5], r^3 w[5], r^4 y[5],
  86. # This four-block variant uses loop unrolled block processing. It
  87. # requires 4 Poly1305 keys: r, r^2, r^3 and r^4:
  88. # h = (h + m) * r => h = (h + m1) * r^4 + m2 * r^3 + m3 * r^2 + m4 * r
  89. vzeroupper
  90. push %rbx
  91. push %r12
  92. push %r13
  93. # combine r0,u0,w0,y0
  94. vmovd y0,ruwy0x
  95. vmovd w0,t1x
  96. vpunpcklqdq t1,ruwy0,ruwy0
  97. vmovd u0,t1x
  98. vmovd r0,t2x
  99. vpunpcklqdq t2,t1,t1
  100. vperm2i128 $0x20,t1,ruwy0,ruwy0
  101. # combine r1,u1,w1,y1 and s1=r1*5,v1=u1*5,x1=w1*5,z1=y1*5
  102. vmovd y1,ruwy1x
  103. vmovd w1,t1x
  104. vpunpcklqdq t1,ruwy1,ruwy1
  105. vmovd u1,t1x
  106. vmovd r1,t2x
  107. vpunpcklqdq t2,t1,t1
  108. vperm2i128 $0x20,t1,ruwy1,ruwy1
  109. vpslld $2,ruwy1,svxz1
  110. vpaddd ruwy1,svxz1,svxz1
  111. # combine r2,u2,w2,y2 and s2=r2*5,v2=u2*5,x2=w2*5,z2=y2*5
  112. vmovd y2,ruwy2x
  113. vmovd w2,t1x
  114. vpunpcklqdq t1,ruwy2,ruwy2
  115. vmovd u2,t1x
  116. vmovd r2,t2x
  117. vpunpcklqdq t2,t1,t1
  118. vperm2i128 $0x20,t1,ruwy2,ruwy2
  119. vpslld $2,ruwy2,svxz2
  120. vpaddd ruwy2,svxz2,svxz2
  121. # combine r3,u3,w3,y3 and s3=r3*5,v3=u3*5,x3=w3*5,z3=y3*5
  122. vmovd y3,ruwy3x
  123. vmovd w3,t1x
  124. vpunpcklqdq t1,ruwy3,ruwy3
  125. vmovd u3,t1x
  126. vmovd r3,t2x
  127. vpunpcklqdq t2,t1,t1
  128. vperm2i128 $0x20,t1,ruwy3,ruwy3
  129. vpslld $2,ruwy3,svxz3
  130. vpaddd ruwy3,svxz3,svxz3
  131. # combine r4,u4,w4,y4 and s4=r4*5,v4=u4*5,x4=w4*5,z4=y4*5
  132. vmovd y4,ruwy4x
  133. vmovd w4,t1x
  134. vpunpcklqdq t1,ruwy4,ruwy4
  135. vmovd u4,t1x
  136. vmovd r4,t2x
  137. vpunpcklqdq t2,t1,t1
  138. vperm2i128 $0x20,t1,ruwy4,ruwy4
  139. vpslld $2,ruwy4,svxz4
  140. vpaddd ruwy4,svxz4,svxz4
  141. .Ldoblock4:
  142. # hc0 = [m[48-51] & 0x3ffffff, m[32-35] & 0x3ffffff,
  143. # m[16-19] & 0x3ffffff, m[ 0- 3] & 0x3ffffff + h0]
  144. vmovd 0x00(m),hc0x
  145. vmovd 0x10(m),t1x
  146. vpunpcklqdq t1,hc0,hc0
  147. vmovd 0x20(m),t1x
  148. vmovd 0x30(m),t2x
  149. vpunpcklqdq t2,t1,t1
  150. vperm2i128 $0x20,t1,hc0,hc0
  151. vpand ANMASK(%rip),hc0,hc0
  152. vmovd h0,t1x
  153. vpaddd t1,hc0,hc0
  154. # hc1 = [(m[51-54] >> 2) & 0x3ffffff, (m[35-38] >> 2) & 0x3ffffff,
  155. # (m[19-22] >> 2) & 0x3ffffff, (m[ 3- 6] >> 2) & 0x3ffffff + h1]
  156. vmovd 0x03(m),hc1x
  157. vmovd 0x13(m),t1x
  158. vpunpcklqdq t1,hc1,hc1
  159. vmovd 0x23(m),t1x
  160. vmovd 0x33(m),t2x
  161. vpunpcklqdq t2,t1,t1
  162. vperm2i128 $0x20,t1,hc1,hc1
  163. vpsrld $2,hc1,hc1
  164. vpand ANMASK(%rip),hc1,hc1
  165. vmovd h1,t1x
  166. vpaddd t1,hc1,hc1
  167. # hc2 = [(m[54-57] >> 4) & 0x3ffffff, (m[38-41] >> 4) & 0x3ffffff,
  168. # (m[22-25] >> 4) & 0x3ffffff, (m[ 6- 9] >> 4) & 0x3ffffff + h2]
  169. vmovd 0x06(m),hc2x
  170. vmovd 0x16(m),t1x
  171. vpunpcklqdq t1,hc2,hc2
  172. vmovd 0x26(m),t1x
  173. vmovd 0x36(m),t2x
  174. vpunpcklqdq t2,t1,t1
  175. vperm2i128 $0x20,t1,hc2,hc2
  176. vpsrld $4,hc2,hc2
  177. vpand ANMASK(%rip),hc2,hc2
  178. vmovd h2,t1x
  179. vpaddd t1,hc2,hc2
  180. # hc3 = [(m[57-60] >> 6) & 0x3ffffff, (m[41-44] >> 6) & 0x3ffffff,
  181. # (m[25-28] >> 6) & 0x3ffffff, (m[ 9-12] >> 6) & 0x3ffffff + h3]
  182. vmovd 0x09(m),hc3x
  183. vmovd 0x19(m),t1x
  184. vpunpcklqdq t1,hc3,hc3
  185. vmovd 0x29(m),t1x
  186. vmovd 0x39(m),t2x
  187. vpunpcklqdq t2,t1,t1
  188. vperm2i128 $0x20,t1,hc3,hc3
  189. vpsrld $6,hc3,hc3
  190. vpand ANMASK(%rip),hc3,hc3
  191. vmovd h3,t1x
  192. vpaddd t1,hc3,hc3
  193. # hc4 = [(m[60-63] >> 8) | (1<<24), (m[44-47] >> 8) | (1<<24),
  194. # (m[28-31] >> 8) | (1<<24), (m[12-15] >> 8) | (1<<24) + h4]
  195. vmovd 0x0c(m),hc4x
  196. vmovd 0x1c(m),t1x
  197. vpunpcklqdq t1,hc4,hc4
  198. vmovd 0x2c(m),t1x
  199. vmovd 0x3c(m),t2x
  200. vpunpcklqdq t2,t1,t1
  201. vperm2i128 $0x20,t1,hc4,hc4
  202. vpsrld $8,hc4,hc4
  203. vpor ORMASK(%rip),hc4,hc4
  204. vmovd h4,t1x
  205. vpaddd t1,hc4,hc4
  206. # t1 = [ hc0[3] * r0, hc0[2] * u0, hc0[1] * w0, hc0[0] * y0 ]
  207. vpmuludq hc0,ruwy0,t1
  208. # t1 += [ hc1[3] * s4, hc1[2] * v4, hc1[1] * x4, hc1[0] * z4 ]
  209. vpmuludq hc1,svxz4,t2
  210. vpaddq t2,t1,t1
  211. # t1 += [ hc2[3] * s3, hc2[2] * v3, hc2[1] * x3, hc2[0] * z3 ]
  212. vpmuludq hc2,svxz3,t2
  213. vpaddq t2,t1,t1
  214. # t1 += [ hc3[3] * s2, hc3[2] * v2, hc3[1] * x2, hc3[0] * z2 ]
  215. vpmuludq hc3,svxz2,t2
  216. vpaddq t2,t1,t1
  217. # t1 += [ hc4[3] * s1, hc4[2] * v1, hc4[1] * x1, hc4[0] * z1 ]
  218. vpmuludq hc4,svxz1,t2
  219. vpaddq t2,t1,t1
  220. # d0 = t1[0] + t1[1] + t[2] + t[3]
  221. vpermq $0xee,t1,t2
  222. vpaddq t2,t1,t1
  223. vpsrldq $8,t1,t2
  224. vpaddq t2,t1,t1
  225. vmovq t1x,d0
  226. # t1 = [ hc0[3] * r1, hc0[2] * u1,hc0[1] * w1, hc0[0] * y1 ]
  227. vpmuludq hc0,ruwy1,t1
  228. # t1 += [ hc1[3] * r0, hc1[2] * u0, hc1[1] * w0, hc1[0] * y0 ]
  229. vpmuludq hc1,ruwy0,t2
  230. vpaddq t2,t1,t1
  231. # t1 += [ hc2[3] * s4, hc2[2] * v4, hc2[1] * x4, hc2[0] * z4 ]
  232. vpmuludq hc2,svxz4,t2
  233. vpaddq t2,t1,t1
  234. # t1 += [ hc3[3] * s3, hc3[2] * v3, hc3[1] * x3, hc3[0] * z3 ]
  235. vpmuludq hc3,svxz3,t2
  236. vpaddq t2,t1,t1
  237. # t1 += [ hc4[3] * s2, hc4[2] * v2, hc4[1] * x2, hc4[0] * z2 ]
  238. vpmuludq hc4,svxz2,t2
  239. vpaddq t2,t1,t1
  240. # d1 = t1[0] + t1[1] + t1[3] + t1[4]
  241. vpermq $0xee,t1,t2
  242. vpaddq t2,t1,t1
  243. vpsrldq $8,t1,t2
  244. vpaddq t2,t1,t1
  245. vmovq t1x,d1
  246. # t1 = [ hc0[3] * r2, hc0[2] * u2, hc0[1] * w2, hc0[0] * y2 ]
  247. vpmuludq hc0,ruwy2,t1
  248. # t1 += [ hc1[3] * r1, hc1[2] * u1, hc1[1] * w1, hc1[0] * y1 ]
  249. vpmuludq hc1,ruwy1,t2
  250. vpaddq t2,t1,t1
  251. # t1 += [ hc2[3] * r0, hc2[2] * u0, hc2[1] * w0, hc2[0] * y0 ]
  252. vpmuludq hc2,ruwy0,t2
  253. vpaddq t2,t1,t1
  254. # t1 += [ hc3[3] * s4, hc3[2] * v4, hc3[1] * x4, hc3[0] * z4 ]
  255. vpmuludq hc3,svxz4,t2
  256. vpaddq t2,t1,t1
  257. # t1 += [ hc4[3] * s3, hc4[2] * v3, hc4[1] * x3, hc4[0] * z3 ]
  258. vpmuludq hc4,svxz3,t2
  259. vpaddq t2,t1,t1
  260. # d2 = t1[0] + t1[1] + t1[2] + t1[3]
  261. vpermq $0xee,t1,t2
  262. vpaddq t2,t1,t1
  263. vpsrldq $8,t1,t2
  264. vpaddq t2,t1,t1
  265. vmovq t1x,d2
  266. # t1 = [ hc0[3] * r3, hc0[2] * u3, hc0[1] * w3, hc0[0] * y3 ]
  267. vpmuludq hc0,ruwy3,t1
  268. # t1 += [ hc1[3] * r2, hc1[2] * u2, hc1[1] * w2, hc1[0] * y2 ]
  269. vpmuludq hc1,ruwy2,t2
  270. vpaddq t2,t1,t1
  271. # t1 += [ hc2[3] * r1, hc2[2] * u1, hc2[1] * w1, hc2[0] * y1 ]
  272. vpmuludq hc2,ruwy1,t2
  273. vpaddq t2,t1,t1
  274. # t1 += [ hc3[3] * r0, hc3[2] * u0, hc3[1] * w0, hc3[0] * y0 ]
  275. vpmuludq hc3,ruwy0,t2
  276. vpaddq t2,t1,t1
  277. # t1 += [ hc4[3] * s4, hc4[2] * v4, hc4[1] * x4, hc4[0] * z4 ]
  278. vpmuludq hc4,svxz4,t2
  279. vpaddq t2,t1,t1
  280. # d3 = t1[0] + t1[1] + t1[2] + t1[3]
  281. vpermq $0xee,t1,t2
  282. vpaddq t2,t1,t1
  283. vpsrldq $8,t1,t2
  284. vpaddq t2,t1,t1
  285. vmovq t1x,d3
  286. # t1 = [ hc0[3] * r4, hc0[2] * u4, hc0[1] * w4, hc0[0] * y4 ]
  287. vpmuludq hc0,ruwy4,t1
  288. # t1 += [ hc1[3] * r3, hc1[2] * u3, hc1[1] * w3, hc1[0] * y3 ]
  289. vpmuludq hc1,ruwy3,t2
  290. vpaddq t2,t1,t1
  291. # t1 += [ hc2[3] * r2, hc2[2] * u2, hc2[1] * w2, hc2[0] * y2 ]
  292. vpmuludq hc2,ruwy2,t2
  293. vpaddq t2,t1,t1
  294. # t1 += [ hc3[3] * r1, hc3[2] * u1, hc3[1] * w1, hc3[0] * y1 ]
  295. vpmuludq hc3,ruwy1,t2
  296. vpaddq t2,t1,t1
  297. # t1 += [ hc4[3] * r0, hc4[2] * u0, hc4[1] * w0, hc4[0] * y0 ]
  298. vpmuludq hc4,ruwy0,t2
  299. vpaddq t2,t1,t1
  300. # d4 = t1[0] + t1[1] + t1[2] + t1[3]
  301. vpermq $0xee,t1,t2
  302. vpaddq t2,t1,t1
  303. vpsrldq $8,t1,t2
  304. vpaddq t2,t1,t1
  305. vmovq t1x,d4
  306. # Now do a partial reduction mod (2^130)-5, carrying h0 -> h1 -> h2 ->
  307. # h3 -> h4 -> h0 -> h1 to get h0,h2,h3,h4 < 2^26 and h1 < 2^26 + a small
  308. # amount. Careful: we must not assume the carry bits 'd0 >> 26',
  309. # 'd1 >> 26', 'd2 >> 26', 'd3 >> 26', and '(d4 >> 26) * 5' fit in 32-bit
  310. # integers. It's true in a single-block implementation, but not here.
  311. # d1 += d0 >> 26
  312. mov d0,%rax
  313. shr $26,%rax
  314. add %rax,d1
  315. # h0 = d0 & 0x3ffffff
  316. mov d0,%rbx
  317. and $0x3ffffff,%ebx
  318. # d2 += d1 >> 26
  319. mov d1,%rax
  320. shr $26,%rax
  321. add %rax,d2
  322. # h1 = d1 & 0x3ffffff
  323. mov d1,%rax
  324. and $0x3ffffff,%eax
  325. mov %eax,h1
  326. # d3 += d2 >> 26
  327. mov d2,%rax
  328. shr $26,%rax
  329. add %rax,d3
  330. # h2 = d2 & 0x3ffffff
  331. mov d2,%rax
  332. and $0x3ffffff,%eax
  333. mov %eax,h2
  334. # d4 += d3 >> 26
  335. mov d3,%rax
  336. shr $26,%rax
  337. add %rax,d4
  338. # h3 = d3 & 0x3ffffff
  339. mov d3,%rax
  340. and $0x3ffffff,%eax
  341. mov %eax,h3
  342. # h0 += (d4 >> 26) * 5
  343. mov d4,%rax
  344. shr $26,%rax
  345. lea (%rax,%rax,4),%rax
  346. add %rax,%rbx
  347. # h4 = d4 & 0x3ffffff
  348. mov d4,%rax
  349. and $0x3ffffff,%eax
  350. mov %eax,h4
  351. # h1 += h0 >> 26
  352. mov %rbx,%rax
  353. shr $26,%rax
  354. add %eax,h1
  355. # h0 = h0 & 0x3ffffff
  356. andl $0x3ffffff,%ebx
  357. mov %ebx,h0
  358. add $0x40,m
  359. dec %rcx
  360. jnz .Ldoblock4
  361. vzeroupper
  362. pop %r13
  363. pop %r12
  364. pop %rbx
  365. ret
  366. ENDPROC(poly1305_4block_avx2)