1 /*
2  * ARM NEON and scalar accelerated ChaCha and XChaCha stream ciphers,
3  * including ChaCha20 (RFC7539)
4  *
5  * Copyright (C) 2016 - 2017 Linaro, Ltd. <ard.biesheuvel@linaro.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Based on:
12  * ChaCha20 256-bit cipher algorithm, RFC7539, SIMD glue code
13  *
14  * Copyright (C) 2015 Martin Willi
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  */
21 
22 #include <crypto/algapi.h>
23 #include <crypto/internal/chacha.h>
24 #include <crypto/internal/simd.h>
25 #include <crypto/internal/skcipher.h>
26 #include <linux/jump_label.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 
30 #include <asm/hwcap.h>
31 #include <asm/neon.h>
32 #include <asm/simd.h>
33 
34 asmlinkage void chacha_block_xor_neon(u32 *state, u8 *dst, const u8 *src,
35 				      int nrounds);
36 asmlinkage void chacha_4block_xor_neon(u32 *state, u8 *dst, const u8 *src,
37 				       int nrounds, int bytes);
38 asmlinkage void hchacha_block_neon(const u32 *state, u32 *out, int nrounds);
39 
40 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
41 
chacha_doneon(u32 * state,u8 * dst,const u8 * src,int bytes,int nrounds)42 static void chacha_doneon(u32 *state, u8 *dst, const u8 *src,
43 			  int bytes, int nrounds)
44 {
45 	while (bytes > 0) {
46 		int l = min(bytes, CHACHA_BLOCK_SIZE * 5);
47 
48 		if (l <= CHACHA_BLOCK_SIZE) {
49 			u8 buf[CHACHA_BLOCK_SIZE];
50 
51 			memcpy(buf, src, l);
52 			chacha_block_xor_neon(state, buf, buf, nrounds);
53 			memcpy(dst, buf, l);
54 			state[12] += 1;
55 			break;
56 		}
57 		chacha_4block_xor_neon(state, dst, src, nrounds, l);
58 		bytes -= l;
59 		src += l;
60 		dst += l;
61 		state[12] += DIV_ROUND_UP(l, CHACHA_BLOCK_SIZE);
62 	}
63 }
64 
hchacha_block_arch(const u32 * state,u32 * stream,int nrounds)65 void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds)
66 {
67 	if (!static_branch_likely(&have_neon) || !crypto_simd_usable()) {
68 		hchacha_block_generic(state, stream, nrounds);
69 	} else {
70 		kernel_neon_begin();
71 		hchacha_block_neon(state, stream, nrounds);
72 		kernel_neon_end();
73 	}
74 }
75 EXPORT_SYMBOL(hchacha_block_arch);
76 
chacha_crypt_arch(u32 * state,u8 * dst,const u8 * src,unsigned int bytes,int nrounds)77 void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src, unsigned int bytes,
78 		       int nrounds)
79 {
80 	if (!static_branch_likely(&have_neon) || bytes <= CHACHA_BLOCK_SIZE ||
81 	    !crypto_simd_usable())
82 		return chacha_crypt_generic(state, dst, src, bytes, nrounds);
83 
84 	do {
85 		unsigned int todo = min_t(unsigned int, bytes, SZ_4K);
86 
87 		kernel_neon_begin();
88 		chacha_doneon(state, dst, src, todo, nrounds);
89 		kernel_neon_end();
90 
91 		bytes -= todo;
92 		src += todo;
93 		dst += todo;
94 	} while (bytes);
95 }
96 EXPORT_SYMBOL(chacha_crypt_arch);
97 
chacha_neon_stream_xor(struct skcipher_request * req,const struct chacha_ctx * ctx,const u8 * iv)98 static int chacha_neon_stream_xor(struct skcipher_request *req,
99 				  const struct chacha_ctx *ctx, const u8 *iv)
100 {
101 	struct skcipher_walk walk;
102 	u32 state[16];
103 	int err;
104 
105 	err = skcipher_walk_virt(&walk, req, false);
106 
107 	chacha_init(state, ctx->key, iv);
108 
109 	while (walk.nbytes > 0) {
110 		unsigned int nbytes = walk.nbytes;
111 
112 		if (nbytes < walk.total)
113 			nbytes = rounddown(nbytes, walk.stride);
114 
115 		if (!static_branch_likely(&have_neon) ||
116 		    !crypto_simd_usable()) {
117 			chacha_crypt_generic(state, walk.dst.virt.addr,
118 					     walk.src.virt.addr, nbytes,
119 					     ctx->nrounds);
120 		} else {
121 			kernel_neon_begin();
122 			chacha_doneon(state, walk.dst.virt.addr,
123 				      walk.src.virt.addr, nbytes, ctx->nrounds);
124 			kernel_neon_end();
125 		}
126 		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
127 	}
128 
129 	return err;
130 }
131 
chacha_neon(struct skcipher_request * req)132 static int chacha_neon(struct skcipher_request *req)
133 {
134 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
135 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
136 
137 	return chacha_neon_stream_xor(req, ctx, req->iv);
138 }
139 
xchacha_neon(struct skcipher_request * req)140 static int xchacha_neon(struct skcipher_request *req)
141 {
142 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
143 	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
144 	struct chacha_ctx subctx;
145 	u32 state[16];
146 	u8 real_iv[16];
147 
148 	chacha_init(state, ctx->key, req->iv);
149 	hchacha_block_arch(state, subctx.key, ctx->nrounds);
150 	subctx.nrounds = ctx->nrounds;
151 
152 	memcpy(&real_iv[0], req->iv + 24, 8);
153 	memcpy(&real_iv[8], req->iv + 16, 8);
154 	return chacha_neon_stream_xor(req, &subctx, real_iv);
155 }
156 
157 static struct skcipher_alg algs[] = {
158 	{
159 		.base.cra_name		= "chacha20",
160 		.base.cra_driver_name	= "chacha20-neon",
161 		.base.cra_priority	= 300,
162 		.base.cra_blocksize	= 1,
163 		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
164 		.base.cra_module	= THIS_MODULE,
165 
166 		.min_keysize		= CHACHA_KEY_SIZE,
167 		.max_keysize		= CHACHA_KEY_SIZE,
168 		.ivsize			= CHACHA_IV_SIZE,
169 		.chunksize		= CHACHA_BLOCK_SIZE,
170 		.walksize		= 5 * CHACHA_BLOCK_SIZE,
171 		.setkey			= chacha20_setkey,
172 		.encrypt		= chacha_neon,
173 		.decrypt		= chacha_neon,
174 	}, {
175 		.base.cra_name		= "xchacha20",
176 		.base.cra_driver_name	= "xchacha20-neon",
177 		.base.cra_priority	= 300,
178 		.base.cra_blocksize	= 1,
179 		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
180 		.base.cra_module	= THIS_MODULE,
181 
182 		.min_keysize		= CHACHA_KEY_SIZE,
183 		.max_keysize		= CHACHA_KEY_SIZE,
184 		.ivsize			= XCHACHA_IV_SIZE,
185 		.chunksize		= CHACHA_BLOCK_SIZE,
186 		.walksize		= 5 * CHACHA_BLOCK_SIZE,
187 		.setkey			= chacha20_setkey,
188 		.encrypt		= xchacha_neon,
189 		.decrypt		= xchacha_neon,
190 	}, {
191 		.base.cra_name		= "xchacha12",
192 		.base.cra_driver_name	= "xchacha12-neon",
193 		.base.cra_priority	= 300,
194 		.base.cra_blocksize	= 1,
195 		.base.cra_ctxsize	= sizeof(struct chacha_ctx),
196 		.base.cra_module	= THIS_MODULE,
197 
198 		.min_keysize		= CHACHA_KEY_SIZE,
199 		.max_keysize		= CHACHA_KEY_SIZE,
200 		.ivsize			= XCHACHA_IV_SIZE,
201 		.chunksize		= CHACHA_BLOCK_SIZE,
202 		.walksize		= 5 * CHACHA_BLOCK_SIZE,
203 		.setkey			= chacha12_setkey,
204 		.encrypt		= xchacha_neon,
205 		.decrypt		= xchacha_neon,
206 	}
207 };
208 
chacha_simd_mod_init(void)209 static int __init chacha_simd_mod_init(void)
210 {
211 	if (!cpu_have_named_feature(ASIMD))
212 		return 0;
213 
214 	static_branch_enable(&have_neon);
215 
216 	return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
217 		crypto_register_skciphers(algs, ARRAY_SIZE(algs)) : 0;
218 }
219 
chacha_simd_mod_fini(void)220 static void __exit chacha_simd_mod_fini(void)
221 {
222 	if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) && cpu_have_named_feature(ASIMD))
223 		crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
224 }
225 
226 module_init(chacha_simd_mod_init);
227 module_exit(chacha_simd_mod_fini);
228 
229 MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (NEON accelerated)");
230 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
231 MODULE_LICENSE("GPL v2");
232 MODULE_ALIAS_CRYPTO("chacha20");
233 MODULE_ALIAS_CRYPTO("chacha20-neon");
234 MODULE_ALIAS_CRYPTO("xchacha20");
235 MODULE_ALIAS_CRYPTO("xchacha20-neon");
236 MODULE_ALIAS_CRYPTO("xchacha12");
237 MODULE_ALIAS_CRYPTO("xchacha12-neon");
238