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