2 * Accelerated GHASH implementation with NEON/ARMv8 vmull.p8/64 instructions.
4 * Copyright (C) 2015 - 2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/linkage.h>
12 #include <asm/assembler.h>
67 .fpu crypto-neon-fp-armv8
69 .macro __pmull_p64, rd, rn, rm, b1, b2, b3, b4
70 vmull.p64 \rd, \rn, \rm
74 * This implementation of 64x64 -> 128 bit polynomial multiplication
75 * using vmull.p8 instructions (8x8 -> 16) is taken from the paper
76 * "Fast Software Polynomial Multiplication on ARM Processors Using
77 * the NEON Engine" by Danilo Camara, Conrado Gouvea, Julio Lopez and
78 * Ricardo Dahab (https://hal.inria.fr/hal-01506572)
80 * It has been slightly tweaked for in-order performance, and to allow
81 * 'rq' to overlap with 'ad' or 'bd'.
83 .macro __pmull_p8, rq, ad, bd, b1=t4l, b2=t3l, b3=t4l, b4=t3l
84 vext.8 t0l, \ad, \ad, #1 @ A1
86 vext.8 t4l, \bd, \bd, #1 @ B1
88 vmull.p8 t0q, t0l, \bd @ F = A1*B
89 vext.8 t1l, \ad, \ad, #2 @ A2
90 vmull.p8 t4q, \ad, \b1 @ E = A*B1
92 vext.8 t3l, \bd, \bd, #2 @ B2
94 vmull.p8 t1q, t1l, \bd @ H = A2*B
95 vext.8 t2l, \ad, \ad, #3 @ A3
96 vmull.p8 t3q, \ad, \b2 @ G = A*B2
97 veor t0q, t0q, t4q @ L = E + F
99 vext.8 t4l, \bd, \bd, #3 @ B3
101 vmull.p8 t2q, t2l, \bd @ J = A3*B
102 veor t0l, t0l, t0h @ t0 = (L) (P0 + P1) << 8
103 veor t1q, t1q, t3q @ M = G + H
105 vext.8 t3l, \bd, \bd, #4 @ B4
107 vmull.p8 t4q, \ad, \b3 @ I = A*B3
108 veor t1l, t1l, t1h @ t1 = (M) (P2 + P3) << 16
109 vmull.p8 t3q, \ad, \b4 @ K = A*B4
112 veor t2q, t2q, t4q @ N = I + J
115 veor t2l, t2l, t2h @ t2 = (N) (P4 + P5) << 24
117 veor t3l, t3l, t3h @ t3 = (K) (P6 + P7) << 32
119 vext.8 t0q, t0q, t0q, #15
121 vext.8 t1q, t1q, t1q, #14
122 vmull.p8 \rq, \ad, \bd @ D = A*B
123 vext.8 t2q, t2q, t2q, #13
124 vext.8 t3q, t3q, t3q, #12
132 // PMULL (64x64->128) based reduction for CPUs that can do
133 // it in a single instruction.
135 .macro __pmull_reduce_p64
136 vmull.p64 T1, XL_L, MASK
138 veor XH_L, XH_L, XM_H
139 vext.8 T1, T1, T1, #8
140 veor XL_H, XL_H, XM_L
143 vmull.p64 XL, T1_H, MASK
147 // Alternative reduction for CPUs that lack support for the
148 // 64x64->128 PMULL instruction
150 .macro __pmull_reduce_p8
151 veor XL_H, XL_H, XM_L
152 veor XH_L, XH_L, XM_H
159 veor XL_H, XL_H, T1_L
160 veor XH_L, XH_L, T1_H
169 .macro ghash_update, pn
172 /* do the head block first, if supplied */
180 0: vld1.64 {T1}, [r2]!
183 1: /* multiply XL by SHASH in GF(2^128) */
184 #ifndef CONFIG_CPU_BIG_ENDIAN
187 vext.8 IN1, T1, T1, #8
188 veor T1_L, T1_L, XL_H
191 __pmull_\pn XH, XL_H, SHASH_H, s1h, s2h, s3h, s4h @ a1 * b1
193 __pmull_\pn XL, XL_L, SHASH_L, s1l, s2l, s3l, s4l @ a0 * b0
194 __pmull_\pn XM, T1_L, SHASH2_\pn @ (a1+a0)(b1+b0)
211 * void pmull_ghash_update(int blocks, u64 dg[], const char *src,
212 * struct ghash_key const *k, const char *head)
214 ENTRY(pmull_ghash_update_p64)
215 vld1.64 {SHASH}, [r3]
216 veor SHASH2_p64, SHASH_L, SHASH_H
219 vshl.u64 MASK, MASK, #57
222 ENDPROC(pmull_ghash_update_p64)
224 ENTRY(pmull_ghash_update_p8)
225 vld1.64 {SHASH}, [r3]
226 veor SHASH2_p8, SHASH_L, SHASH_H
228 vext.8 s1l, SHASH_L, SHASH_L, #1
229 vext.8 s2l, SHASH_L, SHASH_L, #2
230 vext.8 s3l, SHASH_L, SHASH_L, #3
231 vext.8 s4l, SHASH_L, SHASH_L, #4
232 vext.8 s1h, SHASH_H, SHASH_H, #1
233 vext.8 s2h, SHASH_H, SHASH_H, #2
234 vext.8 s3h, SHASH_H, SHASH_H, #3
235 vext.8 s4h, SHASH_H, SHASH_H, #4
237 vmov.i64 k16, #0xffff
238 vmov.i64 k32, #0xffffffff
239 vmov.i64 k48, #0xffffffffffff
242 ENDPROC(pmull_ghash_update_p8)