xref: /linux/drivers/crypto/qce/skcipher.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/device.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/interrupt.h>
9 #include <linux/moduleparam.h>
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <crypto/aes.h>
14 #include <crypto/internal/skcipher.h>
15 
16 #include "cipher.h"
17 
18 static unsigned int aes_sw_max_len = CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN;
19 module_param(aes_sw_max_len, uint, 0644);
20 MODULE_PARM_DESC(aes_sw_max_len,
21 		 "Only use hardware for AES requests larger than this "
22 		 "[0=always use hardware; anything <16 breaks AES-GCM; default="
23 		 __stringify(CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN)"]");
24 
25 static LIST_HEAD(skcipher_algs);
26 
27 static void qce_skcipher_done(void *data)
28 {
29 	struct crypto_async_request *async_req = data;
30 	struct skcipher_request *req = skcipher_request_cast(async_req);
31 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
32 	struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
33 	struct qce_device *qce = tmpl->qce;
34 	struct qce_result_dump *result_buf = qce->dma.result_buf;
35 	enum dma_data_direction dir_src, dir_dst;
36 	u32 status;
37 	int error;
38 	bool diff_dst;
39 
40 	diff_dst = (req->src != req->dst) ? true : false;
41 	dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
42 	dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
43 
44 	error = qce_dma_terminate_all(&qce->dma);
45 	if (error)
46 		dev_dbg(qce->dev, "skcipher dma termination error (%d)\n",
47 			error);
48 
49 	if (diff_dst)
50 		dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
51 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
52 
53 	sg_free_table(&rctx->dst_tbl);
54 
55 	error = qce_check_status(qce, &status);
56 	if (error < 0)
57 		dev_dbg(qce->dev, "skcipher operation error (%x)\n", status);
58 
59 	memcpy(rctx->iv, result_buf->encr_cntr_iv, rctx->ivsize);
60 	qce->async_req_done(tmpl->qce, error);
61 }
62 
63 static int
64 qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
65 {
66 	struct skcipher_request *req = skcipher_request_cast(async_req);
67 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
68 	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
69 	struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
70 	struct qce_device *qce = tmpl->qce;
71 	enum dma_data_direction dir_src, dir_dst;
72 	struct scatterlist *sg;
73 	bool diff_dst;
74 	gfp_t gfp;
75 	int dst_nents, src_nents, ret;
76 
77 	rctx->iv = req->iv;
78 	rctx->ivsize = crypto_skcipher_ivsize(skcipher);
79 	rctx->cryptlen = req->cryptlen;
80 
81 	diff_dst = (req->src != req->dst) ? true : false;
82 	dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
83 	dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
84 
85 	rctx->src_nents = sg_nents_for_len(req->src, req->cryptlen);
86 	if (diff_dst)
87 		rctx->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
88 	else
89 		rctx->dst_nents = rctx->src_nents;
90 	if (rctx->src_nents < 0) {
91 		dev_err(qce->dev, "Invalid numbers of src SG.\n");
92 		return rctx->src_nents;
93 	}
94 	if (rctx->dst_nents < 0) {
95 		dev_err(qce->dev, "Invalid numbers of dst SG.\n");
96 		return -rctx->dst_nents;
97 	}
98 
99 	rctx->dst_nents += 1;
100 
101 	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
102 						GFP_KERNEL : GFP_ATOMIC;
103 
104 	ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
105 	if (ret)
106 		return ret;
107 
108 	sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
109 
110 	sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, req->cryptlen);
111 	if (IS_ERR(sg)) {
112 		ret = PTR_ERR(sg);
113 		goto error_free;
114 	}
115 
116 	sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg,
117 			     QCE_RESULT_BUF_SZ);
118 	if (IS_ERR(sg)) {
119 		ret = PTR_ERR(sg);
120 		goto error_free;
121 	}
122 
123 	sg_mark_end(sg);
124 	rctx->dst_sg = rctx->dst_tbl.sgl;
125 
126 	dst_nents = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
127 	if (!dst_nents) {
128 		ret = -EIO;
129 		goto error_free;
130 	}
131 
132 	if (diff_dst) {
133 		src_nents = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
134 		if (!src_nents) {
135 			ret = -EIO;
136 			goto error_unmap_dst;
137 		}
138 		rctx->src_sg = req->src;
139 	} else {
140 		rctx->src_sg = rctx->dst_sg;
141 		src_nents = dst_nents - 1;
142 	}
143 
144 	ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents,
145 			       rctx->dst_sg, dst_nents,
146 			       qce_skcipher_done, async_req);
147 	if (ret)
148 		goto error_unmap_src;
149 
150 	qce_dma_issue_pending(&qce->dma);
151 
152 	ret = qce_start(async_req, tmpl->crypto_alg_type);
153 	if (ret)
154 		goto error_terminate;
155 
156 	return 0;
157 
158 error_terminate:
159 	qce_dma_terminate_all(&qce->dma);
160 error_unmap_src:
161 	if (diff_dst)
162 		dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
163 error_unmap_dst:
164 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
165 error_free:
166 	sg_free_table(&rctx->dst_tbl);
167 	return ret;
168 }
169 
170 static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key,
171 				 unsigned int keylen)
172 {
173 	struct crypto_tfm *tfm = crypto_skcipher_tfm(ablk);
174 	struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
175 	unsigned long flags = to_cipher_tmpl(ablk)->alg_flags;
176 	unsigned int __keylen;
177 	int ret;
178 
179 	if (!key || !keylen)
180 		return -EINVAL;
181 
182 	/*
183 	 * AES XTS key1 = key2 not supported by crypto engine.
184 	 * Revisit to request a fallback cipher in this case.
185 	 */
186 	if (IS_XTS(flags)) {
187 		__keylen = keylen >> 1;
188 		if (!memcmp(key, key + __keylen, __keylen))
189 			return -ENOKEY;
190 	} else {
191 		__keylen = keylen;
192 	}
193 
194 	switch (__keylen) {
195 	case AES_KEYSIZE_128:
196 	case AES_KEYSIZE_256:
197 		memcpy(ctx->enc_key, key, keylen);
198 		break;
199 	case AES_KEYSIZE_192:
200 		break;
201 	default:
202 		return -EINVAL;
203 	}
204 
205 	ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
206 	if (!ret)
207 		ctx->enc_keylen = keylen;
208 	return ret;
209 }
210 
211 static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
212 {
213 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
214 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
215 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
216 	struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
217 	unsigned int blocksize = crypto_skcipher_blocksize(tfm);
218 	int keylen;
219 
220 	rctx->flags = tmpl->alg_flags;
221 	rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
222 	keylen = IS_XTS(rctx->flags) ? ctx->enc_keylen >> 1 : ctx->enc_keylen;
223 
224 	/* CE does not handle 0 length messages */
225 	if (!req->cryptlen)
226 		return 0;
227 
228 	/*
229 	 * ECB and CBC algorithms require message lengths to be
230 	 * multiples of block size.
231 	 */
232 	if (IS_CBC(rctx->flags))
233 		if (!IS_ALIGNED(req->cryptlen, blocksize))
234 			return -EINVAL;
235 
236 	/*
237 	 * Conditions for requesting a fallback cipher
238 	 * AES-192 (not supported by crypto engine (CE))
239 	 * AES-XTS request with len <= 512 byte (not recommended to use CE)
240 	 * AES-XTS request with len > QCE_SECTOR_SIZE and
241 	 * is not a multiple of it.(Revisit this condition to check if it is
242 	 * needed in all versions of CE)
243 	 */
244 	if (IS_AES(rctx->flags) &&
245 	    ((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
246 	    (IS_XTS(rctx->flags) && ((req->cryptlen <= aes_sw_max_len) ||
247 	    (req->cryptlen > QCE_SECTOR_SIZE &&
248 	    req->cryptlen % QCE_SECTOR_SIZE))))) {
249 		skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
250 		skcipher_request_set_callback(&rctx->fallback_req,
251 					      req->base.flags,
252 					      req->base.complete,
253 					      req->base.data);
254 		skcipher_request_set_crypt(&rctx->fallback_req, req->src,
255 					   req->dst, req->cryptlen, req->iv);
256 		return encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
257 				 crypto_skcipher_decrypt(&rctx->fallback_req);
258 	}
259 
260 	return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
261 }
262 
263 static int qce_skcipher_encrypt(struct skcipher_request *req)
264 {
265 	return qce_skcipher_crypt(req, 1);
266 }
267 
268 static int qce_skcipher_decrypt(struct skcipher_request *req)
269 {
270 	return qce_skcipher_crypt(req, 0);
271 }
272 
273 static int qce_skcipher_init(struct crypto_skcipher *tfm)
274 {
275 	/* take the size without the fallback skcipher_request at the end */
276 	crypto_skcipher_set_reqsize(tfm, offsetof(struct qce_cipher_reqctx,
277 						  fallback_req));
278 	return 0;
279 }
280 
281 static int qce_skcipher_init_fallback(struct crypto_skcipher *tfm)
282 {
283 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
284 
285 	ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
286 					      0, CRYPTO_ALG_NEED_FALLBACK);
287 	if (IS_ERR(ctx->fallback))
288 		return PTR_ERR(ctx->fallback);
289 
290 	crypto_skcipher_set_reqsize(tfm, sizeof(struct qce_cipher_reqctx) +
291 					 crypto_skcipher_reqsize(ctx->fallback));
292 	return 0;
293 }
294 
295 static void qce_skcipher_exit(struct crypto_skcipher *tfm)
296 {
297 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
298 
299 	crypto_free_skcipher(ctx->fallback);
300 }
301 
302 struct qce_skcipher_def {
303 	unsigned long flags;
304 	const char *name;
305 	const char *drv_name;
306 	unsigned int blocksize;
307 	unsigned int chunksize;
308 	unsigned int ivsize;
309 	unsigned int min_keysize;
310 	unsigned int max_keysize;
311 };
312 
313 static const struct qce_skcipher_def skcipher_def[] = {
314 	{
315 		.flags		= QCE_ALG_AES | QCE_MODE_CBC,
316 		.name		= "cbc(aes)",
317 		.drv_name	= "cbc-aes-qce",
318 		.blocksize	= AES_BLOCK_SIZE,
319 		.ivsize		= AES_BLOCK_SIZE,
320 		.min_keysize	= AES_MIN_KEY_SIZE,
321 		.max_keysize	= AES_MAX_KEY_SIZE,
322 	},
323 	{
324 		.flags		= QCE_ALG_AES | QCE_MODE_CTR,
325 		.name		= "ctr(aes)",
326 		.drv_name	= "ctr-aes-qce",
327 		.blocksize	= 1,
328 		.chunksize	= AES_BLOCK_SIZE,
329 		.ivsize		= AES_BLOCK_SIZE,
330 		.min_keysize	= AES_MIN_KEY_SIZE,
331 		.max_keysize	= AES_MAX_KEY_SIZE,
332 	},
333 	{
334 		.flags		= QCE_ALG_AES | QCE_MODE_XTS,
335 		.name		= "xts(aes)",
336 		.drv_name	= "xts-aes-qce",
337 		.blocksize	= AES_BLOCK_SIZE,
338 		.ivsize		= AES_BLOCK_SIZE,
339 		.min_keysize	= AES_MIN_KEY_SIZE * 2,
340 		.max_keysize	= AES_MAX_KEY_SIZE * 2,
341 	},
342 };
343 
344 static int qce_skcipher_register_one(const struct qce_skcipher_def *def,
345 				       struct qce_device *qce)
346 {
347 	struct qce_alg_template *tmpl;
348 	struct skcipher_alg *alg;
349 	int ret;
350 
351 	tmpl = kzalloc_obj(*tmpl);
352 	if (!tmpl)
353 		return -ENOMEM;
354 
355 	alg = &tmpl->alg.skcipher;
356 
357 	strscpy(alg->base.cra_name, def->name);
358 	strscpy(alg->base.cra_driver_name, def->drv_name);
359 
360 	alg->base.cra_blocksize		= def->blocksize;
361 	alg->chunksize			= def->chunksize;
362 	alg->ivsize			= def->ivsize;
363 	alg->min_keysize		= def->min_keysize;
364 	alg->max_keysize		= def->max_keysize;
365 	alg->setkey			= qce_skcipher_setkey;
366 	alg->encrypt			= qce_skcipher_encrypt;
367 	alg->decrypt			= qce_skcipher_decrypt;
368 
369 	alg->base.cra_priority		= 275;
370 	alg->base.cra_flags		= CRYPTO_ALG_ASYNC |
371 					  CRYPTO_ALG_ALLOCATES_MEMORY |
372 					  CRYPTO_ALG_KERN_DRIVER_ONLY;
373 	alg->base.cra_ctxsize		= sizeof(struct qce_cipher_ctx);
374 	alg->base.cra_alignmask		= 0;
375 	alg->base.cra_module		= THIS_MODULE;
376 
377 	if (IS_AES(def->flags)) {
378 		alg->base.cra_flags    |= CRYPTO_ALG_NEED_FALLBACK;
379 		alg->init		= qce_skcipher_init_fallback;
380 		alg->exit		= qce_skcipher_exit;
381 	} else {
382 		alg->init		= qce_skcipher_init;
383 	}
384 
385 	INIT_LIST_HEAD(&tmpl->entry);
386 	tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_SKCIPHER;
387 	tmpl->alg_flags = def->flags;
388 	tmpl->qce = qce;
389 
390 	ret = crypto_register_skcipher(alg);
391 	if (ret) {
392 		dev_err(qce->dev, "%s registration failed\n", alg->base.cra_name);
393 		kfree(tmpl);
394 		return ret;
395 	}
396 
397 	list_add_tail(&tmpl->entry, &skcipher_algs);
398 	dev_dbg(qce->dev, "%s is registered\n", alg->base.cra_name);
399 	return 0;
400 }
401 
402 static void qce_skcipher_unregister(struct qce_device *qce)
403 {
404 	struct qce_alg_template *tmpl, *n;
405 
406 	list_for_each_entry_safe(tmpl, n, &skcipher_algs, entry) {
407 		crypto_unregister_skcipher(&tmpl->alg.skcipher);
408 		list_del(&tmpl->entry);
409 		kfree(tmpl);
410 	}
411 }
412 
413 static int qce_skcipher_register(struct qce_device *qce)
414 {
415 	int ret, i;
416 
417 	for (i = 0; i < ARRAY_SIZE(skcipher_def); i++) {
418 		ret = qce_skcipher_register_one(&skcipher_def[i], qce);
419 		if (ret)
420 			goto err;
421 	}
422 
423 	return 0;
424 err:
425 	qce_skcipher_unregister(qce);
426 	return ret;
427 }
428 
429 const struct qce_algo_ops skcipher_ops = {
430 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
431 	.register_algs = qce_skcipher_register,
432 	.unregister_algs = qce_skcipher_unregister,
433 	.async_req_handle = qce_skcipher_async_req_handle,
434 };
435