Path: blob/master/arch/x86/crypto/polyval-clmulni_asm.S
26424 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright 2021 Google LLC3*/4/*5* This is an efficient implementation of POLYVAL using intel PCLMULQDQ-NI6* instructions. It works on 8 blocks at a time, by precomputing the first 87* keys powers h^8, ..., h^1 in the POLYVAL finite field. This precomputation8* allows us to split finite field multiplication into two steps.9*10* In the first step, we consider h^i, m_i as normal polynomials of degree less11* than 128. We then compute p(x) = h^8m_0 + ... + h^1m_7 where multiplication12* is simply polynomial multiplication.13*14* In the second step, we compute the reduction of p(x) modulo the finite field15* modulus g(x) = x^128 + x^127 + x^126 + x^121 + 1.16*17* This two step process is equivalent to computing h^8m_0 + ... + h^1m_7 where18* multiplication is finite field multiplication. The advantage is that the19* two-step process only requires 1 finite field reduction for every 820* polynomial multiplications. Further parallelism is gained by interleaving the21* multiplications and polynomial reductions.22*/2324#include <linux/linkage.h>25#include <asm/frame.h>2627#define STRIDE_BLOCKS 82829#define GSTAR %xmm730#define PL %xmm831#define PH %xmm932#define TMP_XMM %xmm1133#define LO %xmm1234#define HI %xmm1335#define MI %xmm1436#define SUM %xmm153738#define KEY_POWERS %rdi39#define MSG %rsi40#define BLOCKS_LEFT %rdx41#define ACCUMULATOR %rcx42#define TMP %rax4344.section .rodata.cst16.gstar, "aM", @progbits, 1645.align 164647.Lgstar:48.quad 0xc200000000000000, 0xc2000000000000004950.text5152/*53* Performs schoolbook1_iteration on two lists of 128-bit polynomials of length54* count pointed to by MSG and KEY_POWERS.55*/56.macro schoolbook1 count57.set i, 058.rept (\count)59schoolbook1_iteration i 060.set i, (i +1)61.endr62.endm6364/*65* Computes the product of two 128-bit polynomials at the memory locations66* specified by (MSG + 16*i) and (KEY_POWERS + 16*i) and XORs the components of67* the 256-bit product into LO, MI, HI.68*69* Given:70* X = [X_1 : X_0]71* Y = [Y_1 : Y_0]72*73* We compute:74* LO += X_0 * Y_075* MI += X_0 * Y_1 + X_1 * Y_076* HI += X_1 * Y_177*78* Later, the 256-bit result can be extracted as:79* [HI_1 : HI_0 + MI_1 : LO_1 + MI_0 : LO_0]80* This step is done when computing the polynomial reduction for efficiency81* reasons.82*83* If xor_sum == 1, then also XOR the value of SUM into m_0. This avoids an84* extra multiplication of SUM and h^8.85*/86.macro schoolbook1_iteration i xor_sum87movups (16*\i)(MSG), %xmm088.if (\i == 0 && \xor_sum == 1)89pxor SUM, %xmm090.endif91vpclmulqdq $0x01, (16*\i)(KEY_POWERS), %xmm0, %xmm292vpclmulqdq $0x00, (16*\i)(KEY_POWERS), %xmm0, %xmm193vpclmulqdq $0x10, (16*\i)(KEY_POWERS), %xmm0, %xmm394vpclmulqdq $0x11, (16*\i)(KEY_POWERS), %xmm0, %xmm495vpxor %xmm2, MI, MI96vpxor %xmm1, LO, LO97vpxor %xmm4, HI, HI98vpxor %xmm3, MI, MI99.endm100101/*102* Performs the same computation as schoolbook1_iteration, except we expect the103* arguments to already be loaded into xmm0 and xmm1 and we set the result104* registers LO, MI, and HI directly rather than XOR'ing into them.105*/106.macro schoolbook1_noload107vpclmulqdq $0x01, %xmm0, %xmm1, MI108vpclmulqdq $0x10, %xmm0, %xmm1, %xmm2109vpclmulqdq $0x00, %xmm0, %xmm1, LO110vpclmulqdq $0x11, %xmm0, %xmm1, HI111vpxor %xmm2, MI, MI112.endm113114/*115* Computes the 256-bit polynomial represented by LO, HI, MI. Stores116* the result in PL, PH.117* [PH : PL] = [HI_1 : HI_0 + MI_1 : LO_1 + MI_0 : LO_0]118*/119.macro schoolbook2120vpslldq $8, MI, PL121vpsrldq $8, MI, PH122pxor LO, PL123pxor HI, PH124.endm125126/*127* Computes the 128-bit reduction of PH : PL. Stores the result in dest.128*129* This macro computes p(x) mod g(x) where p(x) is in montgomery form and g(x) =130* x^128 + x^127 + x^126 + x^121 + 1.131*132* We have a 256-bit polynomial PH : PL = P_3 : P_2 : P_1 : P_0 that is the133* product of two 128-bit polynomials in Montgomery form. We need to reduce it134* mod g(x). Also, since polynomials in Montgomery form have an "extra" factor135* of x^128, this product has two extra factors of x^128. To get it back into136* Montgomery form, we need to remove one of these factors by dividing by x^128.137*138* To accomplish both of these goals, we add multiples of g(x) that cancel out139* the low 128 bits P_1 : P_0, leaving just the high 128 bits. Since the low140* bits are zero, the polynomial division by x^128 can be done by right shifting.141*142* Since the only nonzero term in the low 64 bits of g(x) is the constant term,143* the multiple of g(x) needed to cancel out P_0 is P_0 * g(x). The CPU can144* only do 64x64 bit multiplications, so split P_0 * g(x) into x^128 * P_0 +145* x^64 * g*(x) * P_0 + P_0, where g*(x) is bits 64-127 of g(x). Adding this to146* the original polynomial gives P_3 : P_2 + P_0 + T_1 : P_1 + T_0 : 0, where T147* = T_1 : T_0 = g*(x) * P_0. Thus, bits 0-63 got "folded" into bits 64-191.148*149* Repeating this same process on the next 64 bits "folds" bits 64-127 into bits150* 128-255, giving the answer in bits 128-255. This time, we need to cancel P_1151* + T_0 in bits 64-127. The multiple of g(x) required is (P_1 + T_0) * g(x) *152* x^64. Adding this to our previous computation gives P_3 + P_1 + T_0 + V_1 :153* P_2 + P_0 + T_1 + V_0 : 0 : 0, where V = V_1 : V_0 = g*(x) * (P_1 + T_0).154*155* So our final computation is:156* T = T_1 : T_0 = g*(x) * P_0157* V = V_1 : V_0 = g*(x) * (P_1 + T_0)158* p(x) / x^{128} mod g(x) = P_3 + P_1 + T_0 + V_1 : P_2 + P_0 + T_1 + V_0159*160* The implementation below saves a XOR instruction by computing P_1 + T_0 : P_0161* + T_1 and XORing into dest, rather than separately XORing P_1 : P_0 and T_0 :162* T_1 into dest. This allows us to reuse P_1 + T_0 when computing V.163*/164.macro montgomery_reduction dest165vpclmulqdq $0x00, PL, GSTAR, TMP_XMM # TMP_XMM = T_1 : T_0 = P_0 * g*(x)166pshufd $0b01001110, TMP_XMM, TMP_XMM # TMP_XMM = T_0 : T_1167pxor PL, TMP_XMM # TMP_XMM = P_1 + T_0 : P_0 + T_1168pxor TMP_XMM, PH # PH = P_3 + P_1 + T_0 : P_2 + P_0 + T_1169pclmulqdq $0x11, GSTAR, TMP_XMM # TMP_XMM = V_1 : V_0 = V = [(P_1 + T_0) * g*(x)]170vpxor TMP_XMM, PH, \dest171.endm172173/*174* Compute schoolbook multiplication for 8 blocks175* m_0h^8 + ... + m_7h^1176*177* If reduce is set, also computes the montgomery reduction of the178* previous full_stride call and XORs with the first message block.179* (m_0 + REDUCE(PL, PH))h^8 + ... + m_7h^1.180* I.e., the first multiplication uses m_0 + REDUCE(PL, PH) instead of m_0.181*/182.macro full_stride reduce183pxor LO, LO184pxor HI, HI185pxor MI, MI186187schoolbook1_iteration 7 0188.if \reduce189vpclmulqdq $0x00, PL, GSTAR, TMP_XMM190.endif191192schoolbook1_iteration 6 0193.if \reduce194pshufd $0b01001110, TMP_XMM, TMP_XMM195.endif196197schoolbook1_iteration 5 0198.if \reduce199pxor PL, TMP_XMM200.endif201202schoolbook1_iteration 4 0203.if \reduce204pxor TMP_XMM, PH205.endif206207schoolbook1_iteration 3 0208.if \reduce209pclmulqdq $0x11, GSTAR, TMP_XMM210.endif211212schoolbook1_iteration 2 0213.if \reduce214vpxor TMP_XMM, PH, SUM215.endif216217schoolbook1_iteration 1 0218219schoolbook1_iteration 0 1220221addq $(8*16), MSG222schoolbook2223.endm224225/*226* Process BLOCKS_LEFT blocks, where 0 < BLOCKS_LEFT < STRIDE_BLOCKS227*/228.macro partial_stride229mov BLOCKS_LEFT, TMP230shlq $4, TMP231addq $(16*STRIDE_BLOCKS), KEY_POWERS232subq TMP, KEY_POWERS233234movups (MSG), %xmm0235pxor SUM, %xmm0236movaps (KEY_POWERS), %xmm1237schoolbook1_noload238dec BLOCKS_LEFT239addq $16, MSG240addq $16, KEY_POWERS241242test $4, BLOCKS_LEFT243jz .Lpartial4BlocksDone244schoolbook1 4245addq $(4*16), MSG246addq $(4*16), KEY_POWERS247.Lpartial4BlocksDone:248test $2, BLOCKS_LEFT249jz .Lpartial2BlocksDone250schoolbook1 2251addq $(2*16), MSG252addq $(2*16), KEY_POWERS253.Lpartial2BlocksDone:254test $1, BLOCKS_LEFT255jz .LpartialDone256schoolbook1 1257.LpartialDone:258schoolbook2259montgomery_reduction SUM260.endm261262/*263* Perform montgomery multiplication in GF(2^128) and store result in op1.264*265* Computes op1*op2*x^{-128} mod x^128 + x^127 + x^126 + x^121 + 1266* If op1, op2 are in montgomery form, this computes the montgomery267* form of op1*op2.268*269* void clmul_polyval_mul(u8 *op1, const u8 *op2);270*/271SYM_FUNC_START(clmul_polyval_mul)272FRAME_BEGIN273vmovdqa .Lgstar(%rip), GSTAR274movups (%rdi), %xmm0275movups (%rsi), %xmm1276schoolbook1_noload277schoolbook2278montgomery_reduction SUM279movups SUM, (%rdi)280FRAME_END281RET282SYM_FUNC_END(clmul_polyval_mul)283284/*285* Perform polynomial evaluation as specified by POLYVAL. This computes:286* h^n * accumulator + h^n * m_0 + ... + h^1 * m_{n-1}287* where n=nblocks, h is the hash key, and m_i are the message blocks.288*289* rdi - pointer to precomputed key powers h^8 ... h^1290* rsi - pointer to message blocks291* rdx - number of blocks to hash292* rcx - pointer to the accumulator293*294* void clmul_polyval_update(const struct polyval_tfm_ctx *keys,295* const u8 *in, size_t nblocks, u8 *accumulator);296*/297SYM_FUNC_START(clmul_polyval_update)298FRAME_BEGIN299vmovdqa .Lgstar(%rip), GSTAR300movups (ACCUMULATOR), SUM301subq $STRIDE_BLOCKS, BLOCKS_LEFT302js .LstrideLoopExit303full_stride 0304subq $STRIDE_BLOCKS, BLOCKS_LEFT305js .LstrideLoopExitReduce306.LstrideLoop:307full_stride 1308subq $STRIDE_BLOCKS, BLOCKS_LEFT309jns .LstrideLoop310.LstrideLoopExitReduce:311montgomery_reduction SUM312.LstrideLoopExit:313add $STRIDE_BLOCKS, BLOCKS_LEFT314jz .LskipPartial315partial_stride316.LskipPartial:317movups SUM, (ACCUMULATOR)318FRAME_END319RET320SYM_FUNC_END(clmul_polyval_update)321322323