1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Accelerated GHASH implementation with NEON/ARMv8 vmull.p8/64 instructions.
5 * Copyright (C) 2015 - 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
8 #include <linux/linkage.h>
9 #include <asm/assembler.h>
92 .fpu crypto-neon-fp-armv8
94 .macro __pmull_p64, rd, rn, rm, b1, b2, b3, b4
95 vmull.p64 \rd, \rn, \rm
99 * This implementation of 64x64 -> 128 bit polynomial multiplication
100 * using vmull.p8 instructions (8x8 -> 16) is taken from the paper
101 * "Fast Software Polynomial Multiplication on ARM Processors Using
102 * the NEON Engine" by Danilo Camara, Conrado Gouvea, Julio Lopez and
103 * Ricardo Dahab (https://hal.inria.fr/hal-01506572)
105 * It has been slightly tweaked for in-order performance, and to allow
106 * 'rq' to overlap with 'ad' or 'bd'.
108 .macro __pmull_p8, rq, ad, bd, b1=t4l, b2=t3l, b3=t4l, b4=t3l
109 vext.8 t0l, \ad, \ad, #1 @ A1
111 vext.8 t4l, \bd, \bd, #1 @ B1
113 vmull.p8 t0q, t0l, \bd @ F = A1*B
114 vext.8 t1l, \ad, \ad, #2 @ A2
115 vmull.p8 t4q, \ad, \b1 @ E = A*B1
117 vext.8 t3l, \bd, \bd, #2 @ B2
119 vmull.p8 t1q, t1l, \bd @ H = A2*B
120 vext.8 t2l, \ad, \ad, #3 @ A3
121 vmull.p8 t3q, \ad, \b2 @ G = A*B2
122 veor t0q, t0q, t4q @ L = E + F
124 vext.8 t4l, \bd, \bd, #3 @ B3
126 vmull.p8 t2q, t2l, \bd @ J = A3*B
127 veor t0l, t0l, t0h @ t0 = (L) (P0 + P1) << 8
128 veor t1q, t1q, t3q @ M = G + H
130 vext.8 t3l, \bd, \bd, #4 @ B4
132 vmull.p8 t4q, \ad, \b3 @ I = A*B3
133 veor t1l, t1l, t1h @ t1 = (M) (P2 + P3) << 16
134 vmull.p8 t3q, \ad, \b4 @ K = A*B4
137 veor t2q, t2q, t4q @ N = I + J
140 veor t2l, t2l, t2h @ t2 = (N) (P4 + P5) << 24
142 veor t3l, t3l, t3h @ t3 = (K) (P6 + P7) << 32
144 vext.8 t0q, t0q, t0q, #15
146 vext.8 t1q, t1q, t1q, #14
147 vmull.p8 \rq, \ad, \bd @ D = A*B
148 vext.8 t2q, t2q, t2q, #13
149 vext.8 t3q, t3q, t3q, #12
157 // PMULL (64x64->128) based reduction for CPUs that can do
158 // it in a single instruction.
160 .macro __pmull_reduce_p64
161 vmull.p64 T1, XL_L, MASK
163 veor XH_L, XH_L, XM_H
164 vext.8 T1, T1, T1, #8
165 veor XL_H, XL_H, XM_L
168 vmull.p64 XL, T1_H, MASK
172 // Alternative reduction for CPUs that lack support for the
173 // 64x64->128 PMULL instruction
175 .macro __pmull_reduce_p8
176 veor XL_H, XL_H, XM_L
177 veor XH_L, XH_L, XM_H
184 veor XL_H, XL_H, T1_L
185 veor XH_L, XH_L, T1_H
194 .macro ghash_update, pn
197 /* do the head block first, if supplied */
206 tst r0, #3 // skip until #blocks is a
207 bne 2f // round multiple of 4
209 vld1.8 {XL2-XM2}, [r2]!
210 1: vld1.8 {T3-T2}, [r2]!
216 vext.8 T1, XL2, XL2, #8
217 veor XL2_H, XL2_H, XL_L
223 vmull.p64 XH, HH4_H, XL_H // a1 * b1
224 veor XL2_H, XL2_H, XL_H
225 vmull.p64 XL, HH4_L, XL_L // a0 * b0
226 vmull.p64 XM, HH34_H, XL2_H // (a1 + a0)(b1 + b0)
228 vmull.p64 XH2, HH3_H, XM2_L // a1 * b1
229 veor XM2_L, XM2_L, XM2_H
230 vmull.p64 XL2, HH3_L, XM2_H // a0 * b0
231 vmull.p64 XM2, HH34_L, XM2_L // (a1 + a0)(b1 + b0)
237 vmull.p64 XH2, HH_H, T3_L // a1 * b1
238 veor T3_L, T3_L, T3_H
239 vmull.p64 XL2, HH_L, T3_H // a0 * b0
240 vmull.p64 XM2, SHASH2_H, T3_L // (a1 + a0)(b1 + b0)
246 vmull.p64 XH2, SHASH_H, T1_L // a1 * b1
247 veor T1_L, T1_L, T1_H
248 vmull.p64 XL2, SHASH_L, T1_H // a0 * b0
249 vmull.p64 XM2, SHASH2_p64, T1_L // (a1 + a0)(b1 + b0)
257 vld1.8 {XL2-XM2}, [r2]!
270 2: vld1.64 {T1}, [r2]!
273 3: /* multiply XL by SHASH in GF(2^128) */
274 #ifndef CONFIG_CPU_BIG_ENDIAN
277 vext.8 IN1, T1, T1, #8
278 veor T1_L, T1_L, XL_H
281 __pmull_\pn XH, XL_H, SHASH_H, s1h, s2h, s3h, s4h @ a1 * b1
283 __pmull_\pn XL, XL_L, SHASH_L, s1l, s2l, s3l, s4l @ a0 * b0
284 __pmull_\pn XM, T1_L, SHASH2_\pn @ (a1+a0)(b1+b0)
301 * void pmull_ghash_update(int blocks, u64 dg[], const char *src,
302 * struct ghash_key const *k, const char *head)
304 ENTRY(pmull_ghash_update_p64)
305 vld1.64 {SHASH}, [r3]!
307 vld1.64 {HH3-HH4}, [r3]
309 veor SHASH2_p64, SHASH_L, SHASH_H
310 veor SHASH2_H, HH_L, HH_H
311 veor HH34_L, HH3_L, HH3_H
312 veor HH34_H, HH4_L, HH4_H
315 vshl.u64 MASK, MASK, #57
318 ENDPROC(pmull_ghash_update_p64)
320 ENTRY(pmull_ghash_update_p8)
321 vld1.64 {SHASH}, [r3]
322 veor SHASH2_p8, SHASH_L, SHASH_H
324 vext.8 s1l, SHASH_L, SHASH_L, #1
325 vext.8 s2l, SHASH_L, SHASH_L, #2
326 vext.8 s3l, SHASH_L, SHASH_L, #3
327 vext.8 s4l, SHASH_L, SHASH_L, #4
328 vext.8 s1h, SHASH_H, SHASH_H, #1
329 vext.8 s2h, SHASH_H, SHASH_H, #2
330 vext.8 s3h, SHASH_H, SHASH_H, #3
331 vext.8 s4h, SHASH_H, SHASH_H, #4
333 vmov.i64 k16, #0xffff
334 vmov.i64 k32, #0xffffffff
335 vmov.i64 k48, #0xffffffffffff
338 ENDPROC(pmull_ghash_update_p8)