1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
6 #include <linux/device.h>
7 #include <linux/interrupt.h>
8 #include <linux/types.h>
9 #include <crypto/aes.h>
10 #include <crypto/internal/des.h>
11 #include <crypto/internal/skcipher.h>
15 static LIST_HEAD(ablkcipher_algs);
17 static void qce_ablkcipher_done(void *data)
19 struct crypto_async_request *async_req = data;
20 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
21 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
22 struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm);
23 struct qce_device *qce = tmpl->qce;
24 enum dma_data_direction dir_src, dir_dst;
29 diff_dst = (req->src != req->dst) ? true : false;
30 dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
31 dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
33 error = qce_dma_terminate_all(&qce->dma);
35 dev_dbg(qce->dev, "ablkcipher dma termination error (%d)\n",
39 dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
40 dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
42 sg_free_table(&rctx->dst_tbl);
44 error = qce_check_status(qce, &status);
46 dev_dbg(qce->dev, "ablkcipher operation error (%x)\n", status);
48 qce->async_req_done(tmpl->qce, error);
52 qce_ablkcipher_async_req_handle(struct crypto_async_request *async_req)
54 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
55 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
56 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
57 struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm);
58 struct qce_device *qce = tmpl->qce;
59 enum dma_data_direction dir_src, dir_dst;
60 struct scatterlist *sg;
66 rctx->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
67 rctx->cryptlen = req->nbytes;
69 diff_dst = (req->src != req->dst) ? true : false;
70 dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
71 dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
73 rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
75 rctx->dst_nents = sg_nents_for_len(req->dst, req->nbytes);
77 rctx->dst_nents = rctx->src_nents;
78 if (rctx->src_nents < 0) {
79 dev_err(qce->dev, "Invalid numbers of src SG.\n");
80 return rctx->src_nents;
82 if (rctx->dst_nents < 0) {
83 dev_err(qce->dev, "Invalid numbers of dst SG.\n");
84 return -rctx->dst_nents;
89 gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
90 GFP_KERNEL : GFP_ATOMIC;
92 ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
96 sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
98 sg = qce_sgtable_add(&rctx->dst_tbl, req->dst);
104 sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg);
111 rctx->dst_sg = rctx->dst_tbl.sgl;
113 ret = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
118 ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
120 goto error_unmap_dst;
121 rctx->src_sg = req->src;
123 rctx->src_sg = rctx->dst_sg;
126 ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, rctx->src_nents,
127 rctx->dst_sg, rctx->dst_nents,
128 qce_ablkcipher_done, async_req);
130 goto error_unmap_src;
132 qce_dma_issue_pending(&qce->dma);
134 ret = qce_start(async_req, tmpl->crypto_alg_type, req->nbytes, 0);
136 goto error_terminate;
141 qce_dma_terminate_all(&qce->dma);
144 dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
146 dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
148 sg_free_table(&rctx->dst_tbl);
152 static int qce_ablkcipher_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
155 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(ablk);
156 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
163 case AES_KEYSIZE_128:
164 case AES_KEYSIZE_256:
170 ctx->enc_keylen = keylen;
171 memcpy(ctx->enc_key, key, keylen);
174 ret = crypto_sync_skcipher_setkey(ctx->fallback, key, keylen);
176 ctx->enc_keylen = keylen;
180 static int qce_des_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
183 struct qce_cipher_ctx *ctx = crypto_ablkcipher_ctx(ablk);
186 err = verify_ablkcipher_des_key(ablk, key);
190 ctx->enc_keylen = keylen;
191 memcpy(ctx->enc_key, key, keylen);
195 static int qce_des3_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
198 struct qce_cipher_ctx *ctx = crypto_ablkcipher_ctx(ablk);
201 err = verify_ablkcipher_des3_key(ablk, key);
205 ctx->enc_keylen = keylen;
206 memcpy(ctx->enc_key, key, keylen);
210 static int qce_ablkcipher_crypt(struct ablkcipher_request *req, int encrypt)
212 struct crypto_tfm *tfm =
213 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
214 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
215 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
216 struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
219 rctx->flags = tmpl->alg_flags;
220 rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
222 if (IS_AES(rctx->flags) && ctx->enc_keylen != AES_KEYSIZE_128 &&
223 ctx->enc_keylen != AES_KEYSIZE_256) {
224 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
226 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
227 skcipher_request_set_callback(subreq, req->base.flags,
229 skcipher_request_set_crypt(subreq, req->src, req->dst,
230 req->nbytes, req->info);
231 ret = encrypt ? crypto_skcipher_encrypt(subreq) :
232 crypto_skcipher_decrypt(subreq);
233 skcipher_request_zero(subreq);
237 return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
240 static int qce_ablkcipher_encrypt(struct ablkcipher_request *req)
242 return qce_ablkcipher_crypt(req, 1);
245 static int qce_ablkcipher_decrypt(struct ablkcipher_request *req)
247 return qce_ablkcipher_crypt(req, 0);
250 static int qce_ablkcipher_init(struct crypto_tfm *tfm)
252 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
254 memset(ctx, 0, sizeof(*ctx));
255 tfm->crt_ablkcipher.reqsize = sizeof(struct qce_cipher_reqctx);
257 ctx->fallback = crypto_alloc_sync_skcipher(crypto_tfm_alg_name(tfm),
258 0, CRYPTO_ALG_NEED_FALLBACK);
259 return PTR_ERR_OR_ZERO(ctx->fallback);
262 static void qce_ablkcipher_exit(struct crypto_tfm *tfm)
264 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
266 crypto_free_sync_skcipher(ctx->fallback);
269 struct qce_ablkcipher_def {
272 const char *drv_name;
273 unsigned int blocksize;
275 unsigned int min_keysize;
276 unsigned int max_keysize;
279 static const struct qce_ablkcipher_def ablkcipher_def[] = {
281 .flags = QCE_ALG_AES | QCE_MODE_ECB,
283 .drv_name = "ecb-aes-qce",
284 .blocksize = AES_BLOCK_SIZE,
285 .ivsize = AES_BLOCK_SIZE,
286 .min_keysize = AES_MIN_KEY_SIZE,
287 .max_keysize = AES_MAX_KEY_SIZE,
290 .flags = QCE_ALG_AES | QCE_MODE_CBC,
292 .drv_name = "cbc-aes-qce",
293 .blocksize = AES_BLOCK_SIZE,
294 .ivsize = AES_BLOCK_SIZE,
295 .min_keysize = AES_MIN_KEY_SIZE,
296 .max_keysize = AES_MAX_KEY_SIZE,
299 .flags = QCE_ALG_AES | QCE_MODE_CTR,
301 .drv_name = "ctr-aes-qce",
302 .blocksize = AES_BLOCK_SIZE,
303 .ivsize = AES_BLOCK_SIZE,
304 .min_keysize = AES_MIN_KEY_SIZE,
305 .max_keysize = AES_MAX_KEY_SIZE,
308 .flags = QCE_ALG_AES | QCE_MODE_XTS,
310 .drv_name = "xts-aes-qce",
311 .blocksize = AES_BLOCK_SIZE,
312 .ivsize = AES_BLOCK_SIZE,
313 .min_keysize = AES_MIN_KEY_SIZE,
314 .max_keysize = AES_MAX_KEY_SIZE,
317 .flags = QCE_ALG_DES | QCE_MODE_ECB,
319 .drv_name = "ecb-des-qce",
320 .blocksize = DES_BLOCK_SIZE,
322 .min_keysize = DES_KEY_SIZE,
323 .max_keysize = DES_KEY_SIZE,
326 .flags = QCE_ALG_DES | QCE_MODE_CBC,
328 .drv_name = "cbc-des-qce",
329 .blocksize = DES_BLOCK_SIZE,
330 .ivsize = DES_BLOCK_SIZE,
331 .min_keysize = DES_KEY_SIZE,
332 .max_keysize = DES_KEY_SIZE,
335 .flags = QCE_ALG_3DES | QCE_MODE_ECB,
336 .name = "ecb(des3_ede)",
337 .drv_name = "ecb-3des-qce",
338 .blocksize = DES3_EDE_BLOCK_SIZE,
340 .min_keysize = DES3_EDE_KEY_SIZE,
341 .max_keysize = DES3_EDE_KEY_SIZE,
344 .flags = QCE_ALG_3DES | QCE_MODE_CBC,
345 .name = "cbc(des3_ede)",
346 .drv_name = "cbc-3des-qce",
347 .blocksize = DES3_EDE_BLOCK_SIZE,
348 .ivsize = DES3_EDE_BLOCK_SIZE,
349 .min_keysize = DES3_EDE_KEY_SIZE,
350 .max_keysize = DES3_EDE_KEY_SIZE,
354 static int qce_ablkcipher_register_one(const struct qce_ablkcipher_def *def,
355 struct qce_device *qce)
357 struct qce_alg_template *tmpl;
358 struct crypto_alg *alg;
361 tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
365 alg = &tmpl->alg.crypto;
367 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
368 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
371 alg->cra_blocksize = def->blocksize;
372 alg->cra_ablkcipher.ivsize = def->ivsize;
373 alg->cra_ablkcipher.min_keysize = def->min_keysize;
374 alg->cra_ablkcipher.max_keysize = def->max_keysize;
375 alg->cra_ablkcipher.setkey = IS_3DES(def->flags) ? qce_des3_setkey :
376 IS_DES(def->flags) ? qce_des_setkey :
377 qce_ablkcipher_setkey;
378 alg->cra_ablkcipher.encrypt = qce_ablkcipher_encrypt;
379 alg->cra_ablkcipher.decrypt = qce_ablkcipher_decrypt;
381 alg->cra_priority = 300;
382 alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
383 CRYPTO_ALG_NEED_FALLBACK;
384 alg->cra_ctxsize = sizeof(struct qce_cipher_ctx);
385 alg->cra_alignmask = 0;
386 alg->cra_type = &crypto_ablkcipher_type;
387 alg->cra_module = THIS_MODULE;
388 alg->cra_init = qce_ablkcipher_init;
389 alg->cra_exit = qce_ablkcipher_exit;
391 INIT_LIST_HEAD(&tmpl->entry);
392 tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_ABLKCIPHER;
393 tmpl->alg_flags = def->flags;
396 ret = crypto_register_alg(alg);
399 dev_err(qce->dev, "%s registration failed\n", alg->cra_name);
403 list_add_tail(&tmpl->entry, &ablkcipher_algs);
404 dev_dbg(qce->dev, "%s is registered\n", alg->cra_name);
408 static void qce_ablkcipher_unregister(struct qce_device *qce)
410 struct qce_alg_template *tmpl, *n;
412 list_for_each_entry_safe(tmpl, n, &ablkcipher_algs, entry) {
413 crypto_unregister_alg(&tmpl->alg.crypto);
414 list_del(&tmpl->entry);
419 static int qce_ablkcipher_register(struct qce_device *qce)
423 for (i = 0; i < ARRAY_SIZE(ablkcipher_def); i++) {
424 ret = qce_ablkcipher_register_one(&ablkcipher_def[i], qce);
431 qce_ablkcipher_unregister(qce);
435 const struct qce_algo_ops ablkcipher_ops = {
436 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
437 .register_algs = qce_ablkcipher_register,
438 .unregister_algs = qce_ablkcipher_unregister,
439 .async_req_handle = qce_ablkcipher_async_req_handle,