chacha_glue.c 9.17 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3 4
 * x64 SIMD accelerated ChaCha and XChaCha stream ciphers,
 * including ChaCha20 (RFC7539)
5 6 7 8 9
 *
 * Copyright (C) 2015 Martin Willi
 */

#include <crypto/algapi.h>
10
#include <crypto/internal/chacha.h>
11
#include <crypto/internal/simd.h>
12
#include <crypto/internal/skcipher.h>
13 14
#include <linux/kernel.h>
#include <linux/module.h>
15
#include <linux/sizes.h>
16 17
#include <asm/simd.h>

18 19 20 21 22
asmlinkage void chacha_block_xor_ssse3(u32 *state, u8 *dst, const u8 *src,
				       unsigned int len, int nrounds);
asmlinkage void chacha_4block_xor_ssse3(u32 *state, u8 *dst, const u8 *src,
					unsigned int len, int nrounds);
asmlinkage void hchacha_block_ssse3(const u32 *state, u32 *out, int nrounds);
23

24 25 26 27 28 29
asmlinkage void chacha_2block_xor_avx2(u32 *state, u8 *dst, const u8 *src,
				       unsigned int len, int nrounds);
asmlinkage void chacha_4block_xor_avx2(u32 *state, u8 *dst, const u8 *src,
				       unsigned int len, int nrounds);
asmlinkage void chacha_8block_xor_avx2(u32 *state, u8 *dst, const u8 *src,
				       unsigned int len, int nrounds);
30

31 32 33 34 35 36
asmlinkage void chacha_2block_xor_avx512vl(u32 *state, u8 *dst, const u8 *src,
					   unsigned int len, int nrounds);
asmlinkage void chacha_4block_xor_avx512vl(u32 *state, u8 *dst, const u8 *src,
					   unsigned int len, int nrounds);
asmlinkage void chacha_8block_xor_avx512vl(u32 *state, u8 *dst, const u8 *src,
					   unsigned int len, int nrounds);
37 38 39 40

static __ro_after_init DEFINE_STATIC_KEY_FALSE(chacha_use_simd);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(chacha_use_avx2);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(chacha_use_avx512vl);
41

42
static unsigned int chacha_advance(unsigned int len, unsigned int maxblocks)
43
{
44 45
	len = min(len, maxblocks * CHACHA_BLOCK_SIZE);
	return round_up(len, CHACHA_BLOCK_SIZE) / CHACHA_BLOCK_SIZE;
46 47
}

48 49
static void chacha_dosimd(u32 *state, u8 *dst, const u8 *src,
			  unsigned int bytes, int nrounds)
50
{
51 52
	if (IS_ENABLED(CONFIG_AS_AVX512) &&
	    static_branch_likely(&chacha_use_avx512vl)) {
53
		while (bytes >= CHACHA_BLOCK_SIZE * 8) {
54 55
			chacha_8block_xor_avx512vl(state, dst, src, bytes,
						   nrounds);
56 57 58 59 60 61
			bytes -= CHACHA_BLOCK_SIZE * 8;
			src += CHACHA_BLOCK_SIZE * 8;
			dst += CHACHA_BLOCK_SIZE * 8;
			state[12] += 8;
		}
		if (bytes > CHACHA_BLOCK_SIZE * 4) {
62 63 64
			chacha_8block_xor_avx512vl(state, dst, src, bytes,
						   nrounds);
			state[12] += chacha_advance(bytes, 8);
65 66
			return;
		}
67
		if (bytes > CHACHA_BLOCK_SIZE * 2) {
68 69 70
			chacha_4block_xor_avx512vl(state, dst, src, bytes,
						   nrounds);
			state[12] += chacha_advance(bytes, 4);
71 72
			return;
		}
73
		if (bytes) {
74 75 76
			chacha_2block_xor_avx512vl(state, dst, src, bytes,
						   nrounds);
			state[12] += chacha_advance(bytes, 2);
77 78
			return;
		}
79
	}
80

81
	if (static_branch_likely(&chacha_use_avx2)) {
82
		while (bytes >= CHACHA_BLOCK_SIZE * 8) {
83
			chacha_8block_xor_avx2(state, dst, src, bytes, nrounds);
84 85 86
			bytes -= CHACHA_BLOCK_SIZE * 8;
			src += CHACHA_BLOCK_SIZE * 8;
			dst += CHACHA_BLOCK_SIZE * 8;
87 88
			state[12] += 8;
		}
89
		if (bytes > CHACHA_BLOCK_SIZE * 4) {
90 91
			chacha_8block_xor_avx2(state, dst, src, bytes, nrounds);
			state[12] += chacha_advance(bytes, 8);
92 93
			return;
		}
94
		if (bytes > CHACHA_BLOCK_SIZE * 2) {
95 96
			chacha_4block_xor_avx2(state, dst, src, bytes, nrounds);
			state[12] += chacha_advance(bytes, 4);
97 98
			return;
		}
99
		if (bytes > CHACHA_BLOCK_SIZE) {
100 101
			chacha_2block_xor_avx2(state, dst, src, bytes, nrounds);
			state[12] += chacha_advance(bytes, 2);
102 103
			return;
		}
104
	}
105

106
	while (bytes >= CHACHA_BLOCK_SIZE * 4) {
107
		chacha_4block_xor_ssse3(state, dst, src, bytes, nrounds);
108 109 110
		bytes -= CHACHA_BLOCK_SIZE * 4;
		src += CHACHA_BLOCK_SIZE * 4;
		dst += CHACHA_BLOCK_SIZE * 4;
111 112
		state[12] += 4;
	}
113
	if (bytes > CHACHA_BLOCK_SIZE) {
114 115
		chacha_4block_xor_ssse3(state, dst, src, bytes, nrounds);
		state[12] += chacha_advance(bytes, 4);
116
		return;
117 118
	}
	if (bytes) {
119
		chacha_block_xor_ssse3(state, dst, src, bytes, nrounds);
120
		state[12]++;
121 122 123
	}
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds)
{
	if (!static_branch_likely(&chacha_use_simd) || !crypto_simd_usable()) {
		hchacha_block_generic(state, stream, nrounds);
	} else {
		kernel_fpu_begin();
		hchacha_block_ssse3(state, stream, nrounds);
		kernel_fpu_end();
	}
}
EXPORT_SYMBOL(hchacha_block_arch);

void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
{
	chacha_init_generic(state, key, iv);
}
EXPORT_SYMBOL(chacha_init_arch);

void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src, unsigned int bytes,
		       int nrounds)
{
	if (!static_branch_likely(&chacha_use_simd) || !crypto_simd_usable() ||
	    bytes <= CHACHA_BLOCK_SIZE)
		return chacha_crypt_generic(state, dst, src, bytes, nrounds);

149 150 151 152 153 154 155 156 157 158 159
	do {
		unsigned int todo = min_t(unsigned int, bytes, SZ_4K);

		kernel_fpu_begin();
		chacha_dosimd(state, dst, src, todo, nrounds);
		kernel_fpu_end();

		bytes -= todo;
		src += todo;
		dst += todo;
	} while (bytes);
160 161 162
}
EXPORT_SYMBOL(chacha_crypt_arch);

163
static int chacha_simd_stream_xor(struct skcipher_request *req,
164
				  const struct chacha_ctx *ctx, const u8 *iv)
165
{
166
	u32 state[CHACHA_STATE_WORDS] __aligned(8);
167 168 169 170
	struct skcipher_walk walk;
	int err;

	err = skcipher_walk_virt(&walk, req, false);
171

172
	chacha_init_generic(state, ctx->key, iv);
173

174 175
	while (walk.nbytes > 0) {
		unsigned int nbytes = walk.nbytes;
176

177 178
		if (nbytes < walk.total)
			nbytes = round_down(nbytes, walk.stride);
179

180 181
		if (!static_branch_likely(&chacha_use_simd) ||
		    !crypto_simd_usable()) {
182 183 184 185
			chacha_crypt_generic(state, walk.dst.virt.addr,
					     walk.src.virt.addr, nbytes,
					     ctx->nrounds);
		} else {
186
			kernel_fpu_begin();
187 188 189 190
			chacha_dosimd(state, walk.dst.virt.addr,
				      walk.src.virt.addr, nbytes,
				      ctx->nrounds);
			kernel_fpu_end();
191
		}
192
		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
193 194
	}

195 196 197
	return err;
}

198
static int chacha_simd(struct skcipher_request *req)
199 200 201 202
{
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);

203
	return chacha_simd_stream_xor(req, ctx, req->iv);
204 205
}

206
static int xchacha_simd(struct skcipher_request *req)
207 208 209
{
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
210
	u32 state[CHACHA_STATE_WORDS] __aligned(8);
211
	struct chacha_ctx subctx;
212
	u8 real_iv[16];
213

214 215 216 217 218 219 220 221 222
	chacha_init_generic(state, ctx->key, req->iv);

	if (req->cryptlen > CHACHA_BLOCK_SIZE && crypto_simd_usable()) {
		kernel_fpu_begin();
		hchacha_block_ssse3(state, subctx.key, ctx->nrounds);
		kernel_fpu_end();
	} else {
		hchacha_block_generic(state, subctx.key, ctx->nrounds);
	}
223
	subctx.nrounds = ctx->nrounds;
224 225 226

	memcpy(&real_iv[0], req->iv + 24, 8);
	memcpy(&real_iv[8], req->iv + 16, 8);
227
	return chacha_simd_stream_xor(req, &subctx, real_iv);
228 229
}

230 231 232 233 234 235 236 237 238 239 240 241 242
static struct skcipher_alg algs[] = {
	{
		.base.cra_name		= "chacha20",
		.base.cra_driver_name	= "chacha20-simd",
		.base.cra_priority	= 300,
		.base.cra_blocksize	= 1,
		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
		.base.cra_module	= THIS_MODULE,

		.min_keysize		= CHACHA_KEY_SIZE,
		.max_keysize		= CHACHA_KEY_SIZE,
		.ivsize			= CHACHA_IV_SIZE,
		.chunksize		= CHACHA_BLOCK_SIZE,
243
		.setkey			= chacha20_setkey,
244 245
		.encrypt		= chacha_simd,
		.decrypt		= chacha_simd,
246 247 248 249 250 251 252 253 254 255 256 257
	}, {
		.base.cra_name		= "xchacha20",
		.base.cra_driver_name	= "xchacha20-simd",
		.base.cra_priority	= 300,
		.base.cra_blocksize	= 1,
		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
		.base.cra_module	= THIS_MODULE,

		.min_keysize		= CHACHA_KEY_SIZE,
		.max_keysize		= CHACHA_KEY_SIZE,
		.ivsize			= XCHACHA_IV_SIZE,
		.chunksize		= CHACHA_BLOCK_SIZE,
258
		.setkey			= chacha20_setkey,
259 260
		.encrypt		= xchacha_simd,
		.decrypt		= xchacha_simd,
261 262 263 264 265 266 267 268 269 270 271 272
	}, {
		.base.cra_name		= "xchacha12",
		.base.cra_driver_name	= "xchacha12-simd",
		.base.cra_priority	= 300,
		.base.cra_blocksize	= 1,
		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
		.base.cra_module	= THIS_MODULE,

		.min_keysize		= CHACHA_KEY_SIZE,
		.max_keysize		= CHACHA_KEY_SIZE,
		.ivsize			= XCHACHA_IV_SIZE,
		.chunksize		= CHACHA_BLOCK_SIZE,
273
		.setkey			= chacha12_setkey,
274 275
		.encrypt		= xchacha_simd,
		.decrypt		= xchacha_simd,
276
	},
277 278
};

279
static int __init chacha_simd_mod_init(void)
280
{
281
	if (!boot_cpu_has(X86_FEATURE_SSSE3))
282 283 284 285
		return 0;

	static_branch_enable(&chacha_use_simd);

286
	if (boot_cpu_has(X86_FEATURE_AVX) &&
287 288 289 290 291 292 293 294 295
	    boot_cpu_has(X86_FEATURE_AVX2) &&
	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
		static_branch_enable(&chacha_use_avx2);

		if (IS_ENABLED(CONFIG_AS_AVX512) &&
		    boot_cpu_has(X86_FEATURE_AVX512VL) &&
		    boot_cpu_has(X86_FEATURE_AVX512BW)) /* kmovq */
			static_branch_enable(&chacha_use_avx512vl);
	}
296 297
	return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
		crypto_register_skciphers(algs, ARRAY_SIZE(algs)) : 0;
298 299
}

300
static void __exit chacha_simd_mod_fini(void)
301
{
302
	if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) && boot_cpu_has(X86_FEATURE_SSSE3))
303
		crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
304 305
}

306 307
module_init(chacha_simd_mod_init);
module_exit(chacha_simd_mod_fini);
308 309 310

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
311
MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (x64 SIMD accelerated)");
312 313
MODULE_ALIAS_CRYPTO("chacha20");
MODULE_ALIAS_CRYPTO("chacha20-simd");
314 315
MODULE_ALIAS_CRYPTO("xchacha20");
MODULE_ALIAS_CRYPTO("xchacha20-simd");
316 317
MODULE_ALIAS_CRYPTO("xchacha12");
MODULE_ALIAS_CRYPTO("xchacha12-simd");