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/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);
157 unsigned long flags = to_cipher_tmpl(tfm)->alg_flags;
165 case AES_KEYSIZE_128:
166 case AES_KEYSIZE_256:
171 } else if (IS_DES(flags)) {
172 u32 tmp[DES_EXPKEY_WORDS];
174 ret = des_ekey(tmp, key);
175 if (!ret && (crypto_ablkcipher_get_flags(ablk) &
176 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS))
180 ctx->enc_keylen = keylen;
181 memcpy(ctx->enc_key, key, keylen);
184 ret = crypto_sync_skcipher_setkey(ctx->fallback, key, keylen);
186 ctx->enc_keylen = keylen;
189 crypto_ablkcipher_set_flags(ablk, CRYPTO_TFM_RES_WEAK_KEY);
193 static int qce_des3_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
196 struct qce_cipher_ctx *ctx = crypto_ablkcipher_ctx(ablk);
200 flags = crypto_ablkcipher_get_flags(ablk);
201 err = __des3_verify_key(&flags, key);
203 crypto_ablkcipher_set_flags(ablk, flags);
207 ctx->enc_keylen = keylen;
208 memcpy(ctx->enc_key, key, keylen);
212 static int qce_ablkcipher_crypt(struct ablkcipher_request *req, int encrypt)
214 struct crypto_tfm *tfm =
215 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
216 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
217 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
218 struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
221 rctx->flags = tmpl->alg_flags;
222 rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
224 if (IS_AES(rctx->flags) && ctx->enc_keylen != AES_KEYSIZE_128 &&
225 ctx->enc_keylen != AES_KEYSIZE_256) {
226 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
228 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
229 skcipher_request_set_callback(subreq, req->base.flags,
231 skcipher_request_set_crypt(subreq, req->src, req->dst,
232 req->nbytes, req->info);
233 ret = encrypt ? crypto_skcipher_encrypt(subreq) :
234 crypto_skcipher_decrypt(subreq);
235 skcipher_request_zero(subreq);
239 return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
242 static int qce_ablkcipher_encrypt(struct ablkcipher_request *req)
244 return qce_ablkcipher_crypt(req, 1);
247 static int qce_ablkcipher_decrypt(struct ablkcipher_request *req)
249 return qce_ablkcipher_crypt(req, 0);
252 static int qce_ablkcipher_init(struct crypto_tfm *tfm)
254 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
256 memset(ctx, 0, sizeof(*ctx));
257 tfm->crt_ablkcipher.reqsize = sizeof(struct qce_cipher_reqctx);
259 ctx->fallback = crypto_alloc_sync_skcipher(crypto_tfm_alg_name(tfm),
260 0, CRYPTO_ALG_NEED_FALLBACK);
261 return PTR_ERR_OR_ZERO(ctx->fallback);
264 static void qce_ablkcipher_exit(struct crypto_tfm *tfm)
266 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
268 crypto_free_sync_skcipher(ctx->fallback);
271 struct qce_ablkcipher_def {
274 const char *drv_name;
275 unsigned int blocksize;
277 unsigned int min_keysize;
278 unsigned int max_keysize;
281 static const struct qce_ablkcipher_def ablkcipher_def[] = {
283 .flags = QCE_ALG_AES | QCE_MODE_ECB,
285 .drv_name = "ecb-aes-qce",
286 .blocksize = AES_BLOCK_SIZE,
287 .ivsize = AES_BLOCK_SIZE,
288 .min_keysize = AES_MIN_KEY_SIZE,
289 .max_keysize = AES_MAX_KEY_SIZE,
292 .flags = QCE_ALG_AES | QCE_MODE_CBC,
294 .drv_name = "cbc-aes-qce",
295 .blocksize = AES_BLOCK_SIZE,
296 .ivsize = AES_BLOCK_SIZE,
297 .min_keysize = AES_MIN_KEY_SIZE,
298 .max_keysize = AES_MAX_KEY_SIZE,
301 .flags = QCE_ALG_AES | QCE_MODE_CTR,
303 .drv_name = "ctr-aes-qce",
304 .blocksize = AES_BLOCK_SIZE,
305 .ivsize = AES_BLOCK_SIZE,
306 .min_keysize = AES_MIN_KEY_SIZE,
307 .max_keysize = AES_MAX_KEY_SIZE,
310 .flags = QCE_ALG_AES | QCE_MODE_XTS,
312 .drv_name = "xts-aes-qce",
313 .blocksize = AES_BLOCK_SIZE,
314 .ivsize = AES_BLOCK_SIZE,
315 .min_keysize = AES_MIN_KEY_SIZE,
316 .max_keysize = AES_MAX_KEY_SIZE,
319 .flags = QCE_ALG_DES | QCE_MODE_ECB,
321 .drv_name = "ecb-des-qce",
322 .blocksize = DES_BLOCK_SIZE,
324 .min_keysize = DES_KEY_SIZE,
325 .max_keysize = DES_KEY_SIZE,
328 .flags = QCE_ALG_DES | QCE_MODE_CBC,
330 .drv_name = "cbc-des-qce",
331 .blocksize = DES_BLOCK_SIZE,
332 .ivsize = DES_BLOCK_SIZE,
333 .min_keysize = DES_KEY_SIZE,
334 .max_keysize = DES_KEY_SIZE,
337 .flags = QCE_ALG_3DES | QCE_MODE_ECB,
338 .name = "ecb(des3_ede)",
339 .drv_name = "ecb-3des-qce",
340 .blocksize = DES3_EDE_BLOCK_SIZE,
342 .min_keysize = DES3_EDE_KEY_SIZE,
343 .max_keysize = DES3_EDE_KEY_SIZE,
346 .flags = QCE_ALG_3DES | QCE_MODE_CBC,
347 .name = "cbc(des3_ede)",
348 .drv_name = "cbc-3des-qce",
349 .blocksize = DES3_EDE_BLOCK_SIZE,
350 .ivsize = DES3_EDE_BLOCK_SIZE,
351 .min_keysize = DES3_EDE_KEY_SIZE,
352 .max_keysize = DES3_EDE_KEY_SIZE,
356 static int qce_ablkcipher_register_one(const struct qce_ablkcipher_def *def,
357 struct qce_device *qce)
359 struct qce_alg_template *tmpl;
360 struct crypto_alg *alg;
363 tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
367 alg = &tmpl->alg.crypto;
369 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
370 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
373 alg->cra_blocksize = def->blocksize;
374 alg->cra_ablkcipher.ivsize = def->ivsize;
375 alg->cra_ablkcipher.min_keysize = def->min_keysize;
376 alg->cra_ablkcipher.max_keysize = def->max_keysize;
377 alg->cra_ablkcipher.setkey = IS_3DES(def->flags) ?
378 qce_des3_setkey : qce_ablkcipher_setkey;
379 alg->cra_ablkcipher.encrypt = qce_ablkcipher_encrypt;
380 alg->cra_ablkcipher.decrypt = qce_ablkcipher_decrypt;
382 alg->cra_priority = 300;
383 alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
384 CRYPTO_ALG_NEED_FALLBACK;
385 alg->cra_ctxsize = sizeof(struct qce_cipher_ctx);
386 alg->cra_alignmask = 0;
387 alg->cra_type = &crypto_ablkcipher_type;
388 alg->cra_module = THIS_MODULE;
389 alg->cra_init = qce_ablkcipher_init;
390 alg->cra_exit = qce_ablkcipher_exit;
392 INIT_LIST_HEAD(&tmpl->entry);
393 tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_ABLKCIPHER;
394 tmpl->alg_flags = def->flags;
397 ret = crypto_register_alg(alg);
400 dev_err(qce->dev, "%s registration failed\n", alg->cra_name);
404 list_add_tail(&tmpl->entry, &ablkcipher_algs);
405 dev_dbg(qce->dev, "%s is registered\n", alg->cra_name);
409 static void qce_ablkcipher_unregister(struct qce_device *qce)
411 struct qce_alg_template *tmpl, *n;
413 list_for_each_entry_safe(tmpl, n, &ablkcipher_algs, entry) {
414 crypto_unregister_alg(&tmpl->alg.crypto);
415 list_del(&tmpl->entry);
420 static int qce_ablkcipher_register(struct qce_device *qce)
424 for (i = 0; i < ARRAY_SIZE(ablkcipher_def); i++) {
425 ret = qce_ablkcipher_register_one(&ablkcipher_def[i], qce);
432 qce_ablkcipher_unregister(qce);
436 const struct qce_algo_ops ablkcipher_ops = {
437 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
438 .register_algs = qce_ablkcipher_register,
439 .unregister_algs = qce_ablkcipher_unregister,
440 .async_req_handle = qce_ablkcipher_async_req_handle,