1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR MIT
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <crypto/algapi.h>
7*4882a593Smuzhiyun #include <crypto/internal/hash.h>
8*4882a593Smuzhiyun #include <crypto/internal/poly1305.h>
9*4882a593Smuzhiyun #include <crypto/internal/simd.h>
10*4882a593Smuzhiyun #include <linux/crypto.h>
11*4882a593Smuzhiyun #include <linux/jump_label.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/sizes.h>
15*4882a593Smuzhiyun #include <asm/intel-family.h>
16*4882a593Smuzhiyun #include <asm/simd.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun asmlinkage void poly1305_init_x86_64(void *ctx,
19*4882a593Smuzhiyun const u8 key[POLY1305_BLOCK_SIZE]);
20*4882a593Smuzhiyun asmlinkage void poly1305_blocks_x86_64(void *ctx, const u8 *inp,
21*4882a593Smuzhiyun const size_t len, const u32 padbit);
22*4882a593Smuzhiyun asmlinkage void poly1305_emit_x86_64(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],
23*4882a593Smuzhiyun const u32 nonce[4]);
24*4882a593Smuzhiyun asmlinkage void poly1305_emit_avx(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],
25*4882a593Smuzhiyun const u32 nonce[4]);
26*4882a593Smuzhiyun asmlinkage void poly1305_blocks_avx(void *ctx, const u8 *inp, const size_t len,
27*4882a593Smuzhiyun const u32 padbit);
28*4882a593Smuzhiyun asmlinkage void poly1305_blocks_avx2(void *ctx, const u8 *inp, const size_t len,
29*4882a593Smuzhiyun const u32 padbit);
30*4882a593Smuzhiyun asmlinkage void poly1305_blocks_avx512(void *ctx, const u8 *inp,
31*4882a593Smuzhiyun const size_t len, const u32 padbit);
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx);
34*4882a593Smuzhiyun static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx2);
35*4882a593Smuzhiyun static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx512);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun struct poly1305_arch_internal {
38*4882a593Smuzhiyun union {
39*4882a593Smuzhiyun struct {
40*4882a593Smuzhiyun u32 h[5];
41*4882a593Smuzhiyun u32 is_base2_26;
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun u64 hs[3];
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun u64 r[2];
46*4882a593Smuzhiyun u64 pad;
47*4882a593Smuzhiyun struct { u32 r2, r1, r4, r3; } rn[9];
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* The AVX code uses base 2^26, while the scalar code uses base 2^64. If we hit
51*4882a593Smuzhiyun * the unfortunate situation of using AVX and then having to go back to scalar
52*4882a593Smuzhiyun * -- because the user is silly and has called the update function from two
53*4882a593Smuzhiyun * separate contexts -- then we need to convert back to the original base before
54*4882a593Smuzhiyun * proceeding. It is possible to reason that the initial reduction below is
55*4882a593Smuzhiyun * sufficient given the implementation invariants. However, for an avoidance of
56*4882a593Smuzhiyun * doubt and because this is not performance critical, we do the full reduction
57*4882a593Smuzhiyun * anyway. Z3 proof of below function: https://xn--4db.cc/ltPtHCKN/py
58*4882a593Smuzhiyun */
convert_to_base2_64(void * ctx)59*4882a593Smuzhiyun static void convert_to_base2_64(void *ctx)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun struct poly1305_arch_internal *state = ctx;
62*4882a593Smuzhiyun u32 cy;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (!state->is_base2_26)
65*4882a593Smuzhiyun return;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun cy = state->h[0] >> 26; state->h[0] &= 0x3ffffff; state->h[1] += cy;
68*4882a593Smuzhiyun cy = state->h[1] >> 26; state->h[1] &= 0x3ffffff; state->h[2] += cy;
69*4882a593Smuzhiyun cy = state->h[2] >> 26; state->h[2] &= 0x3ffffff; state->h[3] += cy;
70*4882a593Smuzhiyun cy = state->h[3] >> 26; state->h[3] &= 0x3ffffff; state->h[4] += cy;
71*4882a593Smuzhiyun state->hs[0] = ((u64)state->h[2] << 52) | ((u64)state->h[1] << 26) | state->h[0];
72*4882a593Smuzhiyun state->hs[1] = ((u64)state->h[4] << 40) | ((u64)state->h[3] << 14) | (state->h[2] >> 12);
73*4882a593Smuzhiyun state->hs[2] = state->h[4] >> 24;
74*4882a593Smuzhiyun #define ULT(a, b) ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
75*4882a593Smuzhiyun cy = (state->hs[2] >> 2) + (state->hs[2] & ~3ULL);
76*4882a593Smuzhiyun state->hs[2] &= 3;
77*4882a593Smuzhiyun state->hs[0] += cy;
78*4882a593Smuzhiyun state->hs[1] += (cy = ULT(state->hs[0], cy));
79*4882a593Smuzhiyun state->hs[2] += ULT(state->hs[1], cy);
80*4882a593Smuzhiyun #undef ULT
81*4882a593Smuzhiyun state->is_base2_26 = 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
poly1305_simd_init(void * ctx,const u8 key[POLY1305_BLOCK_SIZE])84*4882a593Smuzhiyun static void poly1305_simd_init(void *ctx, const u8 key[POLY1305_BLOCK_SIZE])
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun poly1305_init_x86_64(ctx, key);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
poly1305_simd_blocks(void * ctx,const u8 * inp,size_t len,const u32 padbit)89*4882a593Smuzhiyun static void poly1305_simd_blocks(void *ctx, const u8 *inp, size_t len,
90*4882a593Smuzhiyun const u32 padbit)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun struct poly1305_arch_internal *state = ctx;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* SIMD disables preemption, so relax after processing each page. */
95*4882a593Smuzhiyun BUILD_BUG_ON(SZ_4K < POLY1305_BLOCK_SIZE ||
96*4882a593Smuzhiyun SZ_4K % POLY1305_BLOCK_SIZE);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (!static_branch_likely(&poly1305_use_avx) ||
99*4882a593Smuzhiyun (len < (POLY1305_BLOCK_SIZE * 18) && !state->is_base2_26) ||
100*4882a593Smuzhiyun !crypto_simd_usable()) {
101*4882a593Smuzhiyun convert_to_base2_64(ctx);
102*4882a593Smuzhiyun poly1305_blocks_x86_64(ctx, inp, len, padbit);
103*4882a593Smuzhiyun return;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun do {
107*4882a593Smuzhiyun const size_t bytes = min_t(size_t, len, SZ_4K);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun kernel_fpu_begin();
110*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_AS_AVX512) && static_branch_likely(&poly1305_use_avx512))
111*4882a593Smuzhiyun poly1305_blocks_avx512(ctx, inp, bytes, padbit);
112*4882a593Smuzhiyun else if (static_branch_likely(&poly1305_use_avx2))
113*4882a593Smuzhiyun poly1305_blocks_avx2(ctx, inp, bytes, padbit);
114*4882a593Smuzhiyun else
115*4882a593Smuzhiyun poly1305_blocks_avx(ctx, inp, bytes, padbit);
116*4882a593Smuzhiyun kernel_fpu_end();
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun len -= bytes;
119*4882a593Smuzhiyun inp += bytes;
120*4882a593Smuzhiyun } while (len);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
poly1305_simd_emit(void * ctx,u8 mac[POLY1305_DIGEST_SIZE],const u32 nonce[4])123*4882a593Smuzhiyun static void poly1305_simd_emit(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],
124*4882a593Smuzhiyun const u32 nonce[4])
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun if (!static_branch_likely(&poly1305_use_avx))
127*4882a593Smuzhiyun poly1305_emit_x86_64(ctx, mac, nonce);
128*4882a593Smuzhiyun else
129*4882a593Smuzhiyun poly1305_emit_avx(ctx, mac, nonce);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
poly1305_init_arch(struct poly1305_desc_ctx * dctx,const u8 key[POLY1305_KEY_SIZE])132*4882a593Smuzhiyun void poly1305_init_arch(struct poly1305_desc_ctx *dctx, const u8 key[POLY1305_KEY_SIZE])
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun poly1305_simd_init(&dctx->h, key);
135*4882a593Smuzhiyun dctx->s[0] = get_unaligned_le32(&key[16]);
136*4882a593Smuzhiyun dctx->s[1] = get_unaligned_le32(&key[20]);
137*4882a593Smuzhiyun dctx->s[2] = get_unaligned_le32(&key[24]);
138*4882a593Smuzhiyun dctx->s[3] = get_unaligned_le32(&key[28]);
139*4882a593Smuzhiyun dctx->buflen = 0;
140*4882a593Smuzhiyun dctx->sset = true;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun EXPORT_SYMBOL(poly1305_init_arch);
143*4882a593Smuzhiyun
crypto_poly1305_setdctxkey(struct poly1305_desc_ctx * dctx,const u8 * inp,unsigned int len)144*4882a593Smuzhiyun static unsigned int crypto_poly1305_setdctxkey(struct poly1305_desc_ctx *dctx,
145*4882a593Smuzhiyun const u8 *inp, unsigned int len)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun unsigned int acc = 0;
148*4882a593Smuzhiyun if (unlikely(!dctx->sset)) {
149*4882a593Smuzhiyun if (!dctx->rset && len >= POLY1305_BLOCK_SIZE) {
150*4882a593Smuzhiyun poly1305_simd_init(&dctx->h, inp);
151*4882a593Smuzhiyun inp += POLY1305_BLOCK_SIZE;
152*4882a593Smuzhiyun len -= POLY1305_BLOCK_SIZE;
153*4882a593Smuzhiyun acc += POLY1305_BLOCK_SIZE;
154*4882a593Smuzhiyun dctx->rset = 1;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun if (len >= POLY1305_BLOCK_SIZE) {
157*4882a593Smuzhiyun dctx->s[0] = get_unaligned_le32(&inp[0]);
158*4882a593Smuzhiyun dctx->s[1] = get_unaligned_le32(&inp[4]);
159*4882a593Smuzhiyun dctx->s[2] = get_unaligned_le32(&inp[8]);
160*4882a593Smuzhiyun dctx->s[3] = get_unaligned_le32(&inp[12]);
161*4882a593Smuzhiyun acc += POLY1305_BLOCK_SIZE;
162*4882a593Smuzhiyun dctx->sset = true;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun return acc;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
poly1305_update_arch(struct poly1305_desc_ctx * dctx,const u8 * src,unsigned int srclen)168*4882a593Smuzhiyun void poly1305_update_arch(struct poly1305_desc_ctx *dctx, const u8 *src,
169*4882a593Smuzhiyun unsigned int srclen)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun unsigned int bytes, used;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (unlikely(dctx->buflen)) {
174*4882a593Smuzhiyun bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
175*4882a593Smuzhiyun memcpy(dctx->buf + dctx->buflen, src, bytes);
176*4882a593Smuzhiyun src += bytes;
177*4882a593Smuzhiyun srclen -= bytes;
178*4882a593Smuzhiyun dctx->buflen += bytes;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun if (dctx->buflen == POLY1305_BLOCK_SIZE) {
181*4882a593Smuzhiyun if (likely(!crypto_poly1305_setdctxkey(dctx, dctx->buf, POLY1305_BLOCK_SIZE)))
182*4882a593Smuzhiyun poly1305_simd_blocks(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 1);
183*4882a593Smuzhiyun dctx->buflen = 0;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
188*4882a593Smuzhiyun bytes = round_down(srclen, POLY1305_BLOCK_SIZE);
189*4882a593Smuzhiyun srclen -= bytes;
190*4882a593Smuzhiyun used = crypto_poly1305_setdctxkey(dctx, src, bytes);
191*4882a593Smuzhiyun if (likely(bytes - used))
192*4882a593Smuzhiyun poly1305_simd_blocks(&dctx->h, src + used, bytes - used, 1);
193*4882a593Smuzhiyun src += bytes;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (unlikely(srclen)) {
197*4882a593Smuzhiyun dctx->buflen = srclen;
198*4882a593Smuzhiyun memcpy(dctx->buf, src, srclen);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun EXPORT_SYMBOL(poly1305_update_arch);
202*4882a593Smuzhiyun
poly1305_final_arch(struct poly1305_desc_ctx * dctx,u8 * dst)203*4882a593Smuzhiyun void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun if (unlikely(dctx->buflen)) {
206*4882a593Smuzhiyun dctx->buf[dctx->buflen++] = 1;
207*4882a593Smuzhiyun memset(dctx->buf + dctx->buflen, 0,
208*4882a593Smuzhiyun POLY1305_BLOCK_SIZE - dctx->buflen);
209*4882a593Smuzhiyun poly1305_simd_blocks(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 0);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun poly1305_simd_emit(&dctx->h, dst, dctx->s);
213*4882a593Smuzhiyun *dctx = (struct poly1305_desc_ctx){};
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun EXPORT_SYMBOL(poly1305_final_arch);
216*4882a593Smuzhiyun
crypto_poly1305_init(struct shash_desc * desc)217*4882a593Smuzhiyun static int crypto_poly1305_init(struct shash_desc *desc)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun *dctx = (struct poly1305_desc_ctx){};
222*4882a593Smuzhiyun return 0;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
crypto_poly1305_update(struct shash_desc * desc,const u8 * src,unsigned int srclen)225*4882a593Smuzhiyun static int crypto_poly1305_update(struct shash_desc *desc,
226*4882a593Smuzhiyun const u8 *src, unsigned int srclen)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun poly1305_update_arch(dctx, src, srclen);
231*4882a593Smuzhiyun return 0;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
crypto_poly1305_final(struct shash_desc * desc,u8 * dst)234*4882a593Smuzhiyun static int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (unlikely(!dctx->sset))
239*4882a593Smuzhiyun return -ENOKEY;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun poly1305_final_arch(dctx, dst);
242*4882a593Smuzhiyun return 0;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun static struct shash_alg alg = {
246*4882a593Smuzhiyun .digestsize = POLY1305_DIGEST_SIZE,
247*4882a593Smuzhiyun .init = crypto_poly1305_init,
248*4882a593Smuzhiyun .update = crypto_poly1305_update,
249*4882a593Smuzhiyun .final = crypto_poly1305_final,
250*4882a593Smuzhiyun .descsize = sizeof(struct poly1305_desc_ctx),
251*4882a593Smuzhiyun .base = {
252*4882a593Smuzhiyun .cra_name = "poly1305",
253*4882a593Smuzhiyun .cra_driver_name = "poly1305-simd",
254*4882a593Smuzhiyun .cra_priority = 300,
255*4882a593Smuzhiyun .cra_blocksize = POLY1305_BLOCK_SIZE,
256*4882a593Smuzhiyun .cra_module = THIS_MODULE,
257*4882a593Smuzhiyun },
258*4882a593Smuzhiyun };
259*4882a593Smuzhiyun
poly1305_simd_mod_init(void)260*4882a593Smuzhiyun static int __init poly1305_simd_mod_init(void)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun if (boot_cpu_has(X86_FEATURE_AVX) &&
263*4882a593Smuzhiyun cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
264*4882a593Smuzhiyun static_branch_enable(&poly1305_use_avx);
265*4882a593Smuzhiyun if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2) &&
266*4882a593Smuzhiyun cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
267*4882a593Smuzhiyun static_branch_enable(&poly1305_use_avx2);
268*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_AS_AVX512) && boot_cpu_has(X86_FEATURE_AVX) &&
269*4882a593Smuzhiyun boot_cpu_has(X86_FEATURE_AVX2) && boot_cpu_has(X86_FEATURE_AVX512F) &&
270*4882a593Smuzhiyun cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM | XFEATURE_MASK_AVX512, NULL) &&
271*4882a593Smuzhiyun /* Skylake downclocks unacceptably much when using zmm, but later generations are fast. */
272*4882a593Smuzhiyun boot_cpu_data.x86_model != INTEL_FAM6_SKYLAKE_X)
273*4882a593Smuzhiyun static_branch_enable(&poly1305_use_avx512);
274*4882a593Smuzhiyun return IS_REACHABLE(CONFIG_CRYPTO_HASH) ? crypto_register_shash(&alg) : 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
poly1305_simd_mod_exit(void)277*4882a593Smuzhiyun static void __exit poly1305_simd_mod_exit(void)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun if (IS_REACHABLE(CONFIG_CRYPTO_HASH))
280*4882a593Smuzhiyun crypto_unregister_shash(&alg);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun module_init(poly1305_simd_mod_init);
284*4882a593Smuzhiyun module_exit(poly1305_simd_mod_exit);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun MODULE_LICENSE("GPL");
287*4882a593Smuzhiyun MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
288*4882a593Smuzhiyun MODULE_DESCRIPTION("Poly1305 authenticator");
289*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("poly1305");
290*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("poly1305-simd");
291