1*b646b782SEric Biggers /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2*b646b782SEric Biggers /*
3*b646b782SEric Biggers * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4*b646b782SEric Biggers */
5*b646b782SEric Biggers
6*b646b782SEric Biggers #include <asm/cpu_device_id.h>
7*b646b782SEric Biggers #include <asm/fpu/api.h>
8*b646b782SEric Biggers #include <linux/jump_label.h>
9*b646b782SEric Biggers #include <linux/kernel.h>
10*b646b782SEric Biggers #include <linux/sizes.h>
11*b646b782SEric Biggers
12*b646b782SEric Biggers struct poly1305_arch_internal {
13*b646b782SEric Biggers union {
14*b646b782SEric Biggers struct {
15*b646b782SEric Biggers u32 h[5];
16*b646b782SEric Biggers u32 is_base2_26;
17*b646b782SEric Biggers };
18*b646b782SEric Biggers u64 hs[3];
19*b646b782SEric Biggers };
20*b646b782SEric Biggers u64 r[2];
21*b646b782SEric Biggers u64 pad;
22*b646b782SEric Biggers struct { u32 r2, r1, r4, r3; } rn[9];
23*b646b782SEric Biggers };
24*b646b782SEric Biggers
25*b646b782SEric Biggers /*
26*b646b782SEric Biggers * The AVX code uses base 2^26, while the scalar code uses base 2^64. If we hit
27*b646b782SEric Biggers * the unfortunate situation of using AVX and then having to go back to scalar
28*b646b782SEric Biggers * -- because the user is silly and has called the update function from two
29*b646b782SEric Biggers * separate contexts -- then we need to convert back to the original base before
30*b646b782SEric Biggers * proceeding. It is possible to reason that the initial reduction below is
31*b646b782SEric Biggers * sufficient given the implementation invariants. However, for an avoidance of
32*b646b782SEric Biggers * doubt and because this is not performance critical, we do the full reduction
33*b646b782SEric Biggers * anyway. Z3 proof of below function: https://xn--4db.cc/ltPtHCKN/py
34*b646b782SEric Biggers */
convert_to_base2_64(void * ctx)35*b646b782SEric Biggers static void convert_to_base2_64(void *ctx)
36*b646b782SEric Biggers {
37*b646b782SEric Biggers struct poly1305_arch_internal *state = ctx;
38*b646b782SEric Biggers u32 cy;
39*b646b782SEric Biggers
40*b646b782SEric Biggers if (!state->is_base2_26)
41*b646b782SEric Biggers return;
42*b646b782SEric Biggers
43*b646b782SEric Biggers cy = state->h[0] >> 26; state->h[0] &= 0x3ffffff; state->h[1] += cy;
44*b646b782SEric Biggers cy = state->h[1] >> 26; state->h[1] &= 0x3ffffff; state->h[2] += cy;
45*b646b782SEric Biggers cy = state->h[2] >> 26; state->h[2] &= 0x3ffffff; state->h[3] += cy;
46*b646b782SEric Biggers cy = state->h[3] >> 26; state->h[3] &= 0x3ffffff; state->h[4] += cy;
47*b646b782SEric Biggers state->hs[0] = ((u64)state->h[2] << 52) | ((u64)state->h[1] << 26) | state->h[0];
48*b646b782SEric Biggers state->hs[1] = ((u64)state->h[4] << 40) | ((u64)state->h[3] << 14) | (state->h[2] >> 12);
49*b646b782SEric Biggers state->hs[2] = state->h[4] >> 24;
50*b646b782SEric Biggers /* Unsigned Less Than: branchlessly produces 1 if a < b, else 0. */
51*b646b782SEric Biggers #define ULT(a, b) ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
52*b646b782SEric Biggers cy = (state->hs[2] >> 2) + (state->hs[2] & ~3ULL);
53*b646b782SEric Biggers state->hs[2] &= 3;
54*b646b782SEric Biggers state->hs[0] += cy;
55*b646b782SEric Biggers state->hs[1] += (cy = ULT(state->hs[0], cy));
56*b646b782SEric Biggers state->hs[2] += ULT(state->hs[1], cy);
57*b646b782SEric Biggers #undef ULT
58*b646b782SEric Biggers state->is_base2_26 = 0;
59*b646b782SEric Biggers }
60*b646b782SEric Biggers
61*b646b782SEric Biggers asmlinkage void poly1305_init_x86_64(struct poly1305_block_state *state,
62*b646b782SEric Biggers const u8 raw_key[POLY1305_BLOCK_SIZE]);
63*b646b782SEric Biggers asmlinkage void poly1305_blocks_x86_64(struct poly1305_arch_internal *ctx,
64*b646b782SEric Biggers const u8 *inp,
65*b646b782SEric Biggers const size_t len, const u32 padbit);
66*b646b782SEric Biggers asmlinkage void poly1305_emit_x86_64(const struct poly1305_state *ctx,
67*b646b782SEric Biggers u8 mac[POLY1305_DIGEST_SIZE],
68*b646b782SEric Biggers const u32 nonce[4]);
69*b646b782SEric Biggers asmlinkage void poly1305_emit_avx(const struct poly1305_state *ctx,
70*b646b782SEric Biggers u8 mac[POLY1305_DIGEST_SIZE],
71*b646b782SEric Biggers const u32 nonce[4]);
72*b646b782SEric Biggers asmlinkage void poly1305_blocks_avx(struct poly1305_arch_internal *ctx,
73*b646b782SEric Biggers const u8 *inp, const size_t len,
74*b646b782SEric Biggers const u32 padbit);
75*b646b782SEric Biggers asmlinkage void poly1305_blocks_avx2(struct poly1305_arch_internal *ctx,
76*b646b782SEric Biggers const u8 *inp, const size_t len,
77*b646b782SEric Biggers const u32 padbit);
78*b646b782SEric Biggers asmlinkage void poly1305_blocks_avx512(struct poly1305_arch_internal *ctx,
79*b646b782SEric Biggers const u8 *inp,
80*b646b782SEric Biggers const size_t len, const u32 padbit);
81*b646b782SEric Biggers
82*b646b782SEric Biggers static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx);
83*b646b782SEric Biggers static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx2);
84*b646b782SEric Biggers static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx512);
85*b646b782SEric Biggers
poly1305_block_init(struct poly1305_block_state * state,const u8 raw_key[POLY1305_BLOCK_SIZE])86*b646b782SEric Biggers static void poly1305_block_init(struct poly1305_block_state *state,
87*b646b782SEric Biggers const u8 raw_key[POLY1305_BLOCK_SIZE])
88*b646b782SEric Biggers {
89*b646b782SEric Biggers poly1305_init_x86_64(state, raw_key);
90*b646b782SEric Biggers }
91*b646b782SEric Biggers
poly1305_blocks(struct poly1305_block_state * state,const u8 * inp,unsigned int len,u32 padbit)92*b646b782SEric Biggers static void poly1305_blocks(struct poly1305_block_state *state, const u8 *inp,
93*b646b782SEric Biggers unsigned int len, u32 padbit)
94*b646b782SEric Biggers {
95*b646b782SEric Biggers struct poly1305_arch_internal *ctx =
96*b646b782SEric Biggers container_of(&state->h.h, struct poly1305_arch_internal, h);
97*b646b782SEric Biggers
98*b646b782SEric Biggers /* SIMD disables preemption, so relax after processing each page. */
99*b646b782SEric Biggers BUILD_BUG_ON(SZ_4K < POLY1305_BLOCK_SIZE ||
100*b646b782SEric Biggers SZ_4K % POLY1305_BLOCK_SIZE);
101*b646b782SEric Biggers
102*b646b782SEric Biggers /*
103*b646b782SEric Biggers * The AVX implementations have significant setup overhead (e.g. key
104*b646b782SEric Biggers * power computation, kernel FPU enabling) which makes them slower for
105*b646b782SEric Biggers * short messages. Fall back to the scalar implementation for messages
106*b646b782SEric Biggers * shorter than 288 bytes, unless the AVX-specific key setup has already
107*b646b782SEric Biggers * been performed (indicated by ctx->is_base2_26).
108*b646b782SEric Biggers */
109*b646b782SEric Biggers if (!static_branch_likely(&poly1305_use_avx) ||
110*b646b782SEric Biggers (len < POLY1305_BLOCK_SIZE * 18 && !ctx->is_base2_26) ||
111*b646b782SEric Biggers unlikely(!irq_fpu_usable())) {
112*b646b782SEric Biggers convert_to_base2_64(ctx);
113*b646b782SEric Biggers poly1305_blocks_x86_64(ctx, inp, len, padbit);
114*b646b782SEric Biggers return;
115*b646b782SEric Biggers }
116*b646b782SEric Biggers
117*b646b782SEric Biggers do {
118*b646b782SEric Biggers const unsigned int bytes = min(len, SZ_4K);
119*b646b782SEric Biggers
120*b646b782SEric Biggers kernel_fpu_begin();
121*b646b782SEric Biggers if (static_branch_likely(&poly1305_use_avx512))
122*b646b782SEric Biggers poly1305_blocks_avx512(ctx, inp, bytes, padbit);
123*b646b782SEric Biggers else if (static_branch_likely(&poly1305_use_avx2))
124*b646b782SEric Biggers poly1305_blocks_avx2(ctx, inp, bytes, padbit);
125*b646b782SEric Biggers else
126*b646b782SEric Biggers poly1305_blocks_avx(ctx, inp, bytes, padbit);
127*b646b782SEric Biggers kernel_fpu_end();
128*b646b782SEric Biggers
129*b646b782SEric Biggers len -= bytes;
130*b646b782SEric Biggers inp += bytes;
131*b646b782SEric Biggers } while (len);
132*b646b782SEric Biggers }
133*b646b782SEric Biggers
poly1305_emit(const struct poly1305_state * ctx,u8 mac[POLY1305_DIGEST_SIZE],const u32 nonce[4])134*b646b782SEric Biggers static void poly1305_emit(const struct poly1305_state *ctx,
135*b646b782SEric Biggers u8 mac[POLY1305_DIGEST_SIZE], const u32 nonce[4])
136*b646b782SEric Biggers {
137*b646b782SEric Biggers if (!static_branch_likely(&poly1305_use_avx))
138*b646b782SEric Biggers poly1305_emit_x86_64(ctx, mac, nonce);
139*b646b782SEric Biggers else
140*b646b782SEric Biggers poly1305_emit_avx(ctx, mac, nonce);
141*b646b782SEric Biggers }
142*b646b782SEric Biggers
143*b646b782SEric Biggers #define poly1305_mod_init_arch poly1305_mod_init_arch
poly1305_mod_init_arch(void)144*b646b782SEric Biggers static void poly1305_mod_init_arch(void)
145*b646b782SEric Biggers {
146*b646b782SEric Biggers if (boot_cpu_has(X86_FEATURE_AVX) &&
147*b646b782SEric Biggers cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
148*b646b782SEric Biggers static_branch_enable(&poly1305_use_avx);
149*b646b782SEric Biggers if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2) &&
150*b646b782SEric Biggers cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
151*b646b782SEric Biggers static_branch_enable(&poly1305_use_avx2);
152*b646b782SEric Biggers if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2) &&
153*b646b782SEric Biggers boot_cpu_has(X86_FEATURE_AVX512F) &&
154*b646b782SEric Biggers cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM | XFEATURE_MASK_AVX512, NULL) &&
155*b646b782SEric Biggers /* Skylake downclocks unacceptably much when using zmm, but later generations are fast. */
156*b646b782SEric Biggers boot_cpu_data.x86_vfm != INTEL_SKYLAKE_X)
157*b646b782SEric Biggers static_branch_enable(&poly1305_use_avx512);
158*b646b782SEric Biggers }
159