2 * Poly1305 authenticator algorithm, RFC7539, x64 AVX2 functions
4 * Copyright (C) 2015 Martin Willi
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.
12 #include <linux/linkage.h>
17 ANMASK: .octa 0x0000000003ffffff0000000003ffffff
18 .octa 0x0000000003ffffff0000000003ffffff
19 ORMASK: .octa 0x00000000010000000000000001000000
20 .octa 0x00000000010000000000000001000000
84 ENTRY(poly1305_4block_avx2)
85 # %rdi: Accumulator h[5]
86 # %rsi: 64 byte input block m
87 # %rdx: Poly1305 key r[5]
88 # %rcx: Quadblock count
89 # %r8: Poly1305 derived key r^2 u[5], r^3 w[5], r^4 y[5],
91 # This four-block variant uses loop unrolled block processing. It
92 # requires 4 Poly1305 keys: r, r^2, r^3 and r^4:
93 # h = (h + m) * r => h = (h + m1) * r^4 + m2 * r^3 + m3 * r^2 + m4 * r
100 # combine r0,u0,w0,y0
103 vpunpcklqdq t1,ruwy0,ruwy0
107 vperm2i128 $0x20,t1,ruwy0,ruwy0
109 # combine r1,u1,w1,y1 and s1=r1*5,v1=u1*5,x1=w1*5,z1=y1*5
112 vpunpcklqdq t1,ruwy1,ruwy1
116 vperm2i128 $0x20,t1,ruwy1,ruwy1
117 vpslld $2,ruwy1,svxz1
118 vpaddd ruwy1,svxz1,svxz1
120 # combine r2,u2,w2,y2 and s2=r2*5,v2=u2*5,x2=w2*5,z2=y2*5
123 vpunpcklqdq t1,ruwy2,ruwy2
127 vperm2i128 $0x20,t1,ruwy2,ruwy2
128 vpslld $2,ruwy2,svxz2
129 vpaddd ruwy2,svxz2,svxz2
131 # combine r3,u3,w3,y3 and s3=r3*5,v3=u3*5,x3=w3*5,z3=y3*5
134 vpunpcklqdq t1,ruwy3,ruwy3
138 vperm2i128 $0x20,t1,ruwy3,ruwy3
139 vpslld $2,ruwy3,svxz3
140 vpaddd ruwy3,svxz3,svxz3
142 # combine r4,u4,w4,y4 and s4=r4*5,v4=u4*5,x4=w4*5,z4=y4*5
145 vpunpcklqdq t1,ruwy4,ruwy4
149 vperm2i128 $0x20,t1,ruwy4,ruwy4
150 vpslld $2,ruwy4,svxz4
151 vpaddd ruwy4,svxz4,svxz4
154 # hc0 = [m[48-51] & 0x3ffffff, m[32-35] & 0x3ffffff,
155 # m[16-19] & 0x3ffffff, m[ 0- 3] & 0x3ffffff + h0]
158 vpunpcklqdq t1,hc0,hc0
162 vperm2i128 $0x20,t1,hc0,hc0
163 vpand ANMASK(%rip),hc0,hc0
166 # hc1 = [(m[51-54] >> 2) & 0x3ffffff, (m[35-38] >> 2) & 0x3ffffff,
167 # (m[19-22] >> 2) & 0x3ffffff, (m[ 3- 6] >> 2) & 0x3ffffff + h1]
170 vpunpcklqdq t1,hc1,hc1
174 vperm2i128 $0x20,t1,hc1,hc1
176 vpand ANMASK(%rip),hc1,hc1
179 # hc2 = [(m[54-57] >> 4) & 0x3ffffff, (m[38-41] >> 4) & 0x3ffffff,
180 # (m[22-25] >> 4) & 0x3ffffff, (m[ 6- 9] >> 4) & 0x3ffffff + h2]
183 vpunpcklqdq t1,hc2,hc2
187 vperm2i128 $0x20,t1,hc2,hc2
189 vpand ANMASK(%rip),hc2,hc2
192 # hc3 = [(m[57-60] >> 6) & 0x3ffffff, (m[41-44] >> 6) & 0x3ffffff,
193 # (m[25-28] >> 6) & 0x3ffffff, (m[ 9-12] >> 6) & 0x3ffffff + h3]
196 vpunpcklqdq t1,hc3,hc3
200 vperm2i128 $0x20,t1,hc3,hc3
202 vpand ANMASK(%rip),hc3,hc3
205 # hc4 = [(m[60-63] >> 8) | (1<<24), (m[44-47] >> 8) | (1<<24),
206 # (m[28-31] >> 8) | (1<<24), (m[12-15] >> 8) | (1<<24) + h4]
209 vpunpcklqdq t1,hc4,hc4
213 vperm2i128 $0x20,t1,hc4,hc4
215 vpor ORMASK(%rip),hc4,hc4
219 # t1 = [ hc0[3] * r0, hc0[2] * u0, hc0[1] * w0, hc0[0] * y0 ]
220 vpmuludq hc0,ruwy0,t1
221 # t1 += [ hc1[3] * s4, hc1[2] * v4, hc1[1] * x4, hc1[0] * z4 ]
222 vpmuludq hc1,svxz4,t2
224 # t1 += [ hc2[3] * s3, hc2[2] * v3, hc2[1] * x3, hc2[0] * z3 ]
225 vpmuludq hc2,svxz3,t2
227 # t1 += [ hc3[3] * s2, hc3[2] * v2, hc3[1] * x2, hc3[0] * z2 ]
228 vpmuludq hc3,svxz2,t2
230 # t1 += [ hc4[3] * s1, hc4[2] * v1, hc4[1] * x1, hc4[0] * z1 ]
231 vpmuludq hc4,svxz1,t2
233 # d0 = t1[0] + t1[1] + t[2] + t[3]
240 # t1 = [ hc0[3] * r1, hc0[2] * u1,hc0[1] * w1, hc0[0] * y1 ]
241 vpmuludq hc0,ruwy1,t1
242 # t1 += [ hc1[3] * r0, hc1[2] * u0, hc1[1] * w0, hc1[0] * y0 ]
243 vpmuludq hc1,ruwy0,t2
245 # t1 += [ hc2[3] * s4, hc2[2] * v4, hc2[1] * x4, hc2[0] * z4 ]
246 vpmuludq hc2,svxz4,t2
248 # t1 += [ hc3[3] * s3, hc3[2] * v3, hc3[1] * x3, hc3[0] * z3 ]
249 vpmuludq hc3,svxz3,t2
251 # t1 += [ hc4[3] * s2, hc4[2] * v2, hc4[1] * x2, hc4[0] * z2 ]
252 vpmuludq hc4,svxz2,t2
254 # d1 = t1[0] + t1[1] + t1[3] + t1[4]
261 # t1 = [ hc0[3] * r2, hc0[2] * u2, hc0[1] * w2, hc0[0] * y2 ]
262 vpmuludq hc0,ruwy2,t1
263 # t1 += [ hc1[3] * r1, hc1[2] * u1, hc1[1] * w1, hc1[0] * y1 ]
264 vpmuludq hc1,ruwy1,t2
266 # t1 += [ hc2[3] * r0, hc2[2] * u0, hc2[1] * w0, hc2[0] * y0 ]
267 vpmuludq hc2,ruwy0,t2
269 # t1 += [ hc3[3] * s4, hc3[2] * v4, hc3[1] * x4, hc3[0] * z4 ]
270 vpmuludq hc3,svxz4,t2
272 # t1 += [ hc4[3] * s3, hc4[2] * v3, hc4[1] * x3, hc4[0] * z3 ]
273 vpmuludq hc4,svxz3,t2
275 # d2 = t1[0] + t1[1] + t1[2] + t1[3]
282 # t1 = [ hc0[3] * r3, hc0[2] * u3, hc0[1] * w3, hc0[0] * y3 ]
283 vpmuludq hc0,ruwy3,t1
284 # t1 += [ hc1[3] * r2, hc1[2] * u2, hc1[1] * w2, hc1[0] * y2 ]
285 vpmuludq hc1,ruwy2,t2
287 # t1 += [ hc2[3] * r1, hc2[2] * u1, hc2[1] * w1, hc2[0] * y1 ]
288 vpmuludq hc2,ruwy1,t2
290 # t1 += [ hc3[3] * r0, hc3[2] * u0, hc3[1] * w0, hc3[0] * y0 ]
291 vpmuludq hc3,ruwy0,t2
293 # t1 += [ hc4[3] * s4, hc4[2] * v4, hc4[1] * x4, hc4[0] * z4 ]
294 vpmuludq hc4,svxz4,t2
296 # d3 = t1[0] + t1[1] + t1[2] + t1[3]
303 # t1 = [ hc0[3] * r4, hc0[2] * u4, hc0[1] * w4, hc0[0] * y4 ]
304 vpmuludq hc0,ruwy4,t1
305 # t1 += [ hc1[3] * r3, hc1[2] * u3, hc1[1] * w3, hc1[0] * y3 ]
306 vpmuludq hc1,ruwy3,t2
308 # t1 += [ hc2[3] * r2, hc2[2] * u2, hc2[1] * w2, hc2[0] * y2 ]
309 vpmuludq hc2,ruwy2,t2
311 # t1 += [ hc3[3] * r1, hc3[2] * u1, hc3[1] * w1, hc3[0] * y1 ]
312 vpmuludq hc3,ruwy1,t2
314 # t1 += [ hc4[3] * r0, hc4[2] * u0, hc4[1] * w0, hc4[0] * y0 ]
315 vpmuludq hc4,ruwy0,t2
317 # d4 = t1[0] + t1[1] + t1[2] + t1[3]
328 # h0 = d0 & 0x3ffffff
336 # h1 = d1 & 0x3ffffff
345 # h2 = d2 & 0x3ffffff
354 # h3 = d3 & 0x3ffffff
359 # h0 += (d4 >> 26) * 5
362 lea (%eax,%eax,4),%eax
364 # h4 = d4 & 0x3ffffff
373 # h0 = h0 & 0x3ffffff
386 ENDPROC(poly1305_4block_avx2)