1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 2021 Google LLC
6 * This is an efficient implementation of POLYVAL using intel PCLMULQDQ-NI
7 * instructions. It works on 8 blocks at a time, by precomputing the first 8
8 * keys powers h^8, ..., h^1 in the POLYVAL finite field. This precomputation
9 * allows us to split finite field multiplication into two steps.
11 * In the first step, we consider h^i, m_i as normal polynomials of degree less
12 * than 128. We then compute p(x) = h^8m_0 + ... + h^1m_7 where multiplication
13 * is simply polynomial multiplication.
15 * In the second step, we compute the reduction of p(x) modulo the finite field
16 * modulus g(x) = x^128 + x^127 + x^126 + x^121 + 1.
18 * This two step process is equivalent to computing h^8m_0 + ... + h^1m_7 where
19 * multiplication is finite field multiplication. The advantage is that the
20 * two-step process only requires 1 finite field reduction for every 8
21 * polynomial multiplications. Further parallelism is gained by interleaving the
22 * multiplications and polynomial reductions.
25 #include <linux/linkage.h>
26 #include <asm/frame.h>
28 #define STRIDE_BLOCKS 8
33 #define TMP_XMM %xmm11
39 #define KEY_POWERS %rdi
41 #define BLOCKS_LEFT %rdx
42 #define ACCUMULATOR %rcx
45 .section .rodata.cst16.gstar, "aM", @progbits, 16
49 .quad 0xc200000000000000, 0xc200000000000000
54 * Performs schoolbook1_iteration on two lists of 128-bit polynomials of length
55 * count pointed to by MSG and KEY_POWERS.
57 .macro schoolbook1 count
60 schoolbook1_iteration i 0
66 * Computes the product of two 128-bit polynomials at the memory locations
67 * specified by (MSG + 16*i) and (KEY_POWERS + 16*i) and XORs the components of
68 * the 256-bit product into LO, MI, HI.
76 * MI += X_0 * Y_1 + X_1 * Y_0
79 * Later, the 256-bit result can be extracted as:
80 * [HI_1 : HI_0 + MI_1 : LO_1 + MI_0 : LO_0]
81 * This step is done when computing the polynomial reduction for efficiency
84 * If xor_sum == 1, then also XOR the value of SUM into m_0. This avoids an
85 * extra multiplication of SUM and h^8.
87 .macro schoolbook1_iteration i xor_sum
88 movups (16*\i)(MSG), %xmm0
89 .if (\i == 0 && \xor_sum == 1)
92 vpclmulqdq $0x01, (16*\i)(KEY_POWERS), %xmm0, %xmm2
93 vpclmulqdq $0x00, (16*\i)(KEY_POWERS), %xmm0, %xmm1
94 vpclmulqdq $0x10, (16*\i)(KEY_POWERS), %xmm0, %xmm3
95 vpclmulqdq $0x11, (16*\i)(KEY_POWERS), %xmm0, %xmm4
103 * Performs the same computation as schoolbook1_iteration, except we expect the
104 * arguments to already be loaded into xmm0 and xmm1 and we set the result
105 * registers LO, MI, and HI directly rather than XOR'ing into them.
107 .macro schoolbook1_noload
108 vpclmulqdq $0x01, %xmm0, %xmm1, MI
109 vpclmulqdq $0x10, %xmm0, %xmm1, %xmm2
110 vpclmulqdq $0x00, %xmm0, %xmm1, LO
111 vpclmulqdq $0x11, %xmm0, %xmm1, HI
116 * Computes the 256-bit polynomial represented by LO, HI, MI. Stores
117 * the result in PL, PH.
118 * [PH : PL] = [HI_1 : HI_0 + MI_1 : LO_1 + MI_0 : LO_0]
128 * Computes the 128-bit reduction of PH : PL. Stores the result in dest.
130 * This macro computes p(x) mod g(x) where p(x) is in montgomery form and g(x) =
131 * x^128 + x^127 + x^126 + x^121 + 1.
133 * We have a 256-bit polynomial PH : PL = P_3 : P_2 : P_1 : P_0 that is the
134 * product of two 128-bit polynomials in Montgomery form. We need to reduce it
135 * mod g(x). Also, since polynomials in Montgomery form have an "extra" factor
136 * of x^128, this product has two extra factors of x^128. To get it back into
137 * Montgomery form, we need to remove one of these factors by dividing by x^128.
139 * To accomplish both of these goals, we add multiples of g(x) that cancel out
140 * the low 128 bits P_1 : P_0, leaving just the high 128 bits. Since the low
141 * bits are zero, the polynomial division by x^128 can be done by right shifting.
143 * Since the only nonzero term in the low 64 bits of g(x) is the constant term,
144 * the multiple of g(x) needed to cancel out P_0 is P_0 * g(x). The CPU can
145 * only do 64x64 bit multiplications, so split P_0 * g(x) into x^128 * P_0 +
146 * x^64 * g*(x) * P_0 + P_0, where g*(x) is bits 64-127 of g(x). Adding this to
147 * the original polynomial gives P_3 : P_2 + P_0 + T_1 : P_1 + T_0 : 0, where T
148 * = T_1 : T_0 = g*(x) * P_0. Thus, bits 0-63 got "folded" into bits 64-191.
150 * Repeating this same process on the next 64 bits "folds" bits 64-127 into bits
151 * 128-255, giving the answer in bits 128-255. This time, we need to cancel P_1
152 * + T_0 in bits 64-127. The multiple of g(x) required is (P_1 + T_0) * g(x) *
153 * x^64. Adding this to our previous computation gives P_3 + P_1 + T_0 + V_1 :
154 * P_2 + P_0 + T_1 + V_0 : 0 : 0, where V = V_1 : V_0 = g*(x) * (P_1 + T_0).
156 * So our final computation is:
157 * T = T_1 : T_0 = g*(x) * P_0
158 * V = V_1 : V_0 = g*(x) * (P_1 + T_0)
159 * p(x) / x^{128} mod g(x) = P_3 + P_1 + T_0 + V_1 : P_2 + P_0 + T_1 + V_0
161 * The implementation below saves a XOR instruction by computing P_1 + T_0 : P_0
162 * + T_1 and XORing into dest, rather than separately XORing P_1 : P_0 and T_0 :
163 * T_1 into dest. This allows us to reuse P_1 + T_0 when computing V.
165 .macro montgomery_reduction dest
166 vpclmulqdq $0x00, PL, GSTAR, TMP_XMM # TMP_XMM = T_1 : T_0 = P_0 * g*(x)
167 pshufd $0b01001110, TMP_XMM, TMP_XMM # TMP_XMM = T_0 : T_1
168 pxor PL, TMP_XMM # TMP_XMM = P_1 + T_0 : P_0 + T_1
169 pxor TMP_XMM, PH # PH = P_3 + P_1 + T_0 : P_2 + P_0 + T_1
170 pclmulqdq $0x11, GSTAR, TMP_XMM # TMP_XMM = V_1 : V_0 = V = [(P_1 + T_0) * g*(x)]
171 vpxor TMP_XMM, PH, \dest
175 * Compute schoolbook multiplication for 8 blocks
176 * m_0h^8 + ... + m_7h^1
178 * If reduce is set, also computes the montgomery reduction of the
179 * previous full_stride call and XORs with the first message block.
180 * (m_0 + REDUCE(PL, PH))h^8 + ... + m_7h^1.
181 * I.e., the first multiplication uses m_0 + REDUCE(PL, PH) instead of m_0.
183 .macro full_stride reduce
188 schoolbook1_iteration 7 0
190 vpclmulqdq $0x00, PL, GSTAR, TMP_XMM
193 schoolbook1_iteration 6 0
195 pshufd $0b01001110, TMP_XMM, TMP_XMM
198 schoolbook1_iteration 5 0
203 schoolbook1_iteration 4 0
208 schoolbook1_iteration 3 0
210 pclmulqdq $0x11, GSTAR, TMP_XMM
213 schoolbook1_iteration 2 0
215 vpxor TMP_XMM, PH, SUM
218 schoolbook1_iteration 1 0
220 schoolbook1_iteration 0 1
227 * Process BLOCKS_LEFT blocks, where 0 < BLOCKS_LEFT < STRIDE_BLOCKS
229 .macro partial_stride
232 addq $(16*STRIDE_BLOCKS), KEY_POWERS
237 movaps (KEY_POWERS), %xmm1
244 jz .Lpartial4BlocksDone
247 addq $(4*16), KEY_POWERS
248 .Lpartial4BlocksDone:
250 jz .Lpartial2BlocksDone
253 addq $(2*16), KEY_POWERS
254 .Lpartial2BlocksDone:
260 montgomery_reduction SUM
264 * Perform montgomery multiplication in GF(2^128) and store result in op1.
266 * Computes op1*op2*x^{-128} mod x^128 + x^127 + x^126 + x^121 + 1
267 * If op1, op2 are in montgomery form, this computes the montgomery
270 * void clmul_polyval_mul(u8 *op1, const u8 *op2);
272 SYM_FUNC_START(clmul_polyval_mul)
274 vmovdqa .Lgstar(%rip), GSTAR
279 montgomery_reduction SUM
283 SYM_FUNC_END(clmul_polyval_mul)
286 * Perform polynomial evaluation as specified by POLYVAL. This computes:
287 * h^n * accumulator + h^n * m_0 + ... + h^1 * m_{n-1}
288 * where n=nblocks, h is the hash key, and m_i are the message blocks.
290 * rdi - pointer to precomputed key powers h^8 ... h^1
291 * rsi - pointer to message blocks
292 * rdx - number of blocks to hash
293 * rcx - pointer to the accumulator
295 * void clmul_polyval_update(const struct polyval_tfm_ctx *keys,
296 * const u8 *in, size_t nblocks, u8 *accumulator);
298 SYM_FUNC_START(clmul_polyval_update)
300 vmovdqa .Lgstar(%rip), GSTAR
301 movups (ACCUMULATOR), SUM
302 subq $STRIDE_BLOCKS, BLOCKS_LEFT
305 subq $STRIDE_BLOCKS, BLOCKS_LEFT
306 js .LstrideLoopExitReduce
309 subq $STRIDE_BLOCKS, BLOCKS_LEFT
311 .LstrideLoopExitReduce:
312 montgomery_reduction SUM
314 add $STRIDE_BLOCKS, BLOCKS_LEFT
318 movups SUM, (ACCUMULATOR)
321 SYM_FUNC_END(clmul_polyval_update)