1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * x64 SIMD accelerated ChaCha and XChaCha stream ciphers,
4 * including ChaCha20 (RFC7539)
6 * Copyright (C) 2015 Martin Willi
9 #include <crypto/algapi.h>
10 #include <crypto/internal/chacha.h>
11 #include <crypto/internal/simd.h>
12 #include <crypto/internal/skcipher.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
17 #define CHACHA_STATE_ALIGN 16
19 asmlinkage void chacha_block_xor_ssse3(u32 *state, u8 *dst, const u8 *src,
20 unsigned int len, int nrounds);
21 asmlinkage void chacha_4block_xor_ssse3(u32 *state, u8 *dst, const u8 *src,
22 unsigned int len, int nrounds);
23 asmlinkage void hchacha_block_ssse3(const u32 *state, u32 *out, int nrounds);
25 asmlinkage void chacha_2block_xor_avx2(u32 *state, u8 *dst, const u8 *src,
26 unsigned int len, int nrounds);
27 asmlinkage void chacha_4block_xor_avx2(u32 *state, u8 *dst, const u8 *src,
28 unsigned int len, int nrounds);
29 asmlinkage void chacha_8block_xor_avx2(u32 *state, u8 *dst, const u8 *src,
30 unsigned int len, int nrounds);
32 asmlinkage void chacha_2block_xor_avx512vl(u32 *state, u8 *dst, const u8 *src,
33 unsigned int len, int nrounds);
34 asmlinkage void chacha_4block_xor_avx512vl(u32 *state, u8 *dst, const u8 *src,
35 unsigned int len, int nrounds);
36 asmlinkage void chacha_8block_xor_avx512vl(u32 *state, u8 *dst, const u8 *src,
37 unsigned int len, int nrounds);
39 static __ro_after_init DEFINE_STATIC_KEY_FALSE(chacha_use_simd);
40 static __ro_after_init DEFINE_STATIC_KEY_FALSE(chacha_use_avx2);
41 static __ro_after_init DEFINE_STATIC_KEY_FALSE(chacha_use_avx512vl);
43 static unsigned int chacha_advance(unsigned int len, unsigned int maxblocks)
45 len = min(len, maxblocks * CHACHA_BLOCK_SIZE);
46 return round_up(len, CHACHA_BLOCK_SIZE) / CHACHA_BLOCK_SIZE;
49 static void chacha_dosimd(u32 *state, u8 *dst, const u8 *src,
50 unsigned int bytes, int nrounds)
52 if (IS_ENABLED(CONFIG_AS_AVX512) &&
53 static_branch_likely(&chacha_use_avx512vl)) {
54 while (bytes >= CHACHA_BLOCK_SIZE * 8) {
55 chacha_8block_xor_avx512vl(state, dst, src, bytes,
57 bytes -= CHACHA_BLOCK_SIZE * 8;
58 src += CHACHA_BLOCK_SIZE * 8;
59 dst += CHACHA_BLOCK_SIZE * 8;
62 if (bytes > CHACHA_BLOCK_SIZE * 4) {
63 chacha_8block_xor_avx512vl(state, dst, src, bytes,
65 state[12] += chacha_advance(bytes, 8);
68 if (bytes > CHACHA_BLOCK_SIZE * 2) {
69 chacha_4block_xor_avx512vl(state, dst, src, bytes,
71 state[12] += chacha_advance(bytes, 4);
75 chacha_2block_xor_avx512vl(state, dst, src, bytes,
77 state[12] += chacha_advance(bytes, 2);
82 if (static_branch_likely(&chacha_use_avx2)) {
83 while (bytes >= CHACHA_BLOCK_SIZE * 8) {
84 chacha_8block_xor_avx2(state, dst, src, bytes, nrounds);
85 bytes -= CHACHA_BLOCK_SIZE * 8;
86 src += CHACHA_BLOCK_SIZE * 8;
87 dst += CHACHA_BLOCK_SIZE * 8;
90 if (bytes > CHACHA_BLOCK_SIZE * 4) {
91 chacha_8block_xor_avx2(state, dst, src, bytes, nrounds);
92 state[12] += chacha_advance(bytes, 8);
95 if (bytes > CHACHA_BLOCK_SIZE * 2) {
96 chacha_4block_xor_avx2(state, dst, src, bytes, nrounds);
97 state[12] += chacha_advance(bytes, 4);
100 if (bytes > CHACHA_BLOCK_SIZE) {
101 chacha_2block_xor_avx2(state, dst, src, bytes, nrounds);
102 state[12] += chacha_advance(bytes, 2);
107 while (bytes >= CHACHA_BLOCK_SIZE * 4) {
108 chacha_4block_xor_ssse3(state, dst, src, bytes, nrounds);
109 bytes -= CHACHA_BLOCK_SIZE * 4;
110 src += CHACHA_BLOCK_SIZE * 4;
111 dst += CHACHA_BLOCK_SIZE * 4;
114 if (bytes > CHACHA_BLOCK_SIZE) {
115 chacha_4block_xor_ssse3(state, dst, src, bytes, nrounds);
116 state[12] += chacha_advance(bytes, 4);
120 chacha_block_xor_ssse3(state, dst, src, bytes, nrounds);
125 void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds)
127 state = PTR_ALIGN(state, CHACHA_STATE_ALIGN);
129 if (!static_branch_likely(&chacha_use_simd) || !crypto_simd_usable()) {
130 hchacha_block_generic(state, stream, nrounds);
133 hchacha_block_ssse3(state, stream, nrounds);
137 EXPORT_SYMBOL(hchacha_block_arch);
139 void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
141 state = PTR_ALIGN(state, CHACHA_STATE_ALIGN);
143 chacha_init_generic(state, key, iv);
145 EXPORT_SYMBOL(chacha_init_arch);
147 void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src, unsigned int bytes,
150 state = PTR_ALIGN(state, CHACHA_STATE_ALIGN);
152 if (!static_branch_likely(&chacha_use_simd) || !crypto_simd_usable() ||
153 bytes <= CHACHA_BLOCK_SIZE)
154 return chacha_crypt_generic(state, dst, src, bytes, nrounds);
157 unsigned int todo = min_t(unsigned int, bytes, SZ_4K);
160 chacha_dosimd(state, dst, src, todo, nrounds);
168 EXPORT_SYMBOL(chacha_crypt_arch);
170 static int chacha_simd_stream_xor(struct skcipher_request *req,
171 const struct chacha_ctx *ctx, const u8 *iv)
173 u32 *state, state_buf[16 + 2] __aligned(8);
174 struct skcipher_walk walk;
177 err = skcipher_walk_virt(&walk, req, false);
179 BUILD_BUG_ON(CHACHA_STATE_ALIGN != 16);
180 state = PTR_ALIGN(state_buf + 0, CHACHA_STATE_ALIGN);
182 chacha_init_generic(state, ctx->key, iv);
184 while (walk.nbytes > 0) {
185 unsigned int nbytes = walk.nbytes;
187 if (nbytes < walk.total)
188 nbytes = round_down(nbytes, walk.stride);
190 if (!static_branch_likely(&chacha_use_simd) ||
191 !crypto_simd_usable()) {
192 chacha_crypt_generic(state, walk.dst.virt.addr,
193 walk.src.virt.addr, nbytes,
197 chacha_dosimd(state, walk.dst.virt.addr,
198 walk.src.virt.addr, nbytes,
202 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
208 static int chacha_simd(struct skcipher_request *req)
210 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
211 struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
213 return chacha_simd_stream_xor(req, ctx, req->iv);
216 static int xchacha_simd(struct skcipher_request *req)
218 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
219 struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
220 u32 *state, state_buf[16 + 2] __aligned(8);
221 struct chacha_ctx subctx;
224 BUILD_BUG_ON(CHACHA_STATE_ALIGN != 16);
225 state = PTR_ALIGN(state_buf + 0, CHACHA_STATE_ALIGN);
226 chacha_init_generic(state, ctx->key, req->iv);
228 if (req->cryptlen > CHACHA_BLOCK_SIZE && crypto_simd_usable()) {
230 hchacha_block_ssse3(state, subctx.key, ctx->nrounds);
233 hchacha_block_generic(state, subctx.key, ctx->nrounds);
235 subctx.nrounds = ctx->nrounds;
237 memcpy(&real_iv[0], req->iv + 24, 8);
238 memcpy(&real_iv[8], req->iv + 16, 8);
239 return chacha_simd_stream_xor(req, &subctx, real_iv);
242 static struct skcipher_alg algs[] = {
244 .base.cra_name = "chacha20",
245 .base.cra_driver_name = "chacha20-simd",
246 .base.cra_priority = 300,
247 .base.cra_blocksize = 1,
248 .base.cra_ctxsize = sizeof(struct chacha_ctx),
249 .base.cra_module = THIS_MODULE,
251 .min_keysize = CHACHA_KEY_SIZE,
252 .max_keysize = CHACHA_KEY_SIZE,
253 .ivsize = CHACHA_IV_SIZE,
254 .chunksize = CHACHA_BLOCK_SIZE,
255 .setkey = chacha20_setkey,
256 .encrypt = chacha_simd,
257 .decrypt = chacha_simd,
259 .base.cra_name = "xchacha20",
260 .base.cra_driver_name = "xchacha20-simd",
261 .base.cra_priority = 300,
262 .base.cra_blocksize = 1,
263 .base.cra_ctxsize = sizeof(struct chacha_ctx),
264 .base.cra_module = THIS_MODULE,
266 .min_keysize = CHACHA_KEY_SIZE,
267 .max_keysize = CHACHA_KEY_SIZE,
268 .ivsize = XCHACHA_IV_SIZE,
269 .chunksize = CHACHA_BLOCK_SIZE,
270 .setkey = chacha20_setkey,
271 .encrypt = xchacha_simd,
272 .decrypt = xchacha_simd,
274 .base.cra_name = "xchacha12",
275 .base.cra_driver_name = "xchacha12-simd",
276 .base.cra_priority = 300,
277 .base.cra_blocksize = 1,
278 .base.cra_ctxsize = sizeof(struct chacha_ctx),
279 .base.cra_module = THIS_MODULE,
281 .min_keysize = CHACHA_KEY_SIZE,
282 .max_keysize = CHACHA_KEY_SIZE,
283 .ivsize = XCHACHA_IV_SIZE,
284 .chunksize = CHACHA_BLOCK_SIZE,
285 .setkey = chacha12_setkey,
286 .encrypt = xchacha_simd,
287 .decrypt = xchacha_simd,
291 static int __init chacha_simd_mod_init(void)
293 if (!boot_cpu_has(X86_FEATURE_SSSE3))
296 static_branch_enable(&chacha_use_simd);
298 if (boot_cpu_has(X86_FEATURE_AVX) &&
299 boot_cpu_has(X86_FEATURE_AVX2) &&
300 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
301 static_branch_enable(&chacha_use_avx2);
303 if (IS_ENABLED(CONFIG_AS_AVX512) &&
304 boot_cpu_has(X86_FEATURE_AVX512VL) &&
305 boot_cpu_has(X86_FEATURE_AVX512BW)) /* kmovq */
306 static_branch_enable(&chacha_use_avx512vl);
308 return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
309 crypto_register_skciphers(algs, ARRAY_SIZE(algs)) : 0;
312 static void __exit chacha_simd_mod_fini(void)
314 if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) && boot_cpu_has(X86_FEATURE_SSSE3))
315 crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
318 module_init(chacha_simd_mod_init);
319 module_exit(chacha_simd_mod_fini);
321 MODULE_LICENSE("GPL");
322 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
323 MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (x64 SIMD accelerated)");
324 MODULE_ALIAS_CRYPTO("chacha20");
325 MODULE_ALIAS_CRYPTO("chacha20-simd");
326 MODULE_ALIAS_CRYPTO("xchacha20");
327 MODULE_ALIAS_CRYPTO("xchacha20-simd");
328 MODULE_ALIAS_CRYPTO("xchacha12");
329 MODULE_ALIAS_CRYPTO("xchacha12-simd");