Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / crypto / qce / ablkcipher.c
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/interrupt.h>
8 #include <linux/types.h>
9 #include <crypto/aes.h>
10 #include <crypto/des.h>
11 #include <crypto/internal/skcipher.h>
12
13 #include "cipher.h"
14
15 static LIST_HEAD(ablkcipher_algs);
16
17 static void qce_ablkcipher_done(void *data)
18 {
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;
25         u32 status;
26         int error;
27         bool diff_dst;
28
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;
32
33         error = qce_dma_terminate_all(&qce->dma);
34         if (error)
35                 dev_dbg(qce->dev, "ablkcipher dma termination error (%d)\n",
36                         error);
37
38         if (diff_dst)
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);
41
42         sg_free_table(&rctx->dst_tbl);
43
44         error = qce_check_status(qce, &status);
45         if (error < 0)
46                 dev_dbg(qce->dev, "ablkcipher operation error (%x)\n", status);
47
48         qce->async_req_done(tmpl->qce, error);
49 }
50
51 static int
52 qce_ablkcipher_async_req_handle(struct crypto_async_request *async_req)
53 {
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;
61         bool diff_dst;
62         gfp_t gfp;
63         int ret;
64
65         rctx->iv = req->info;
66         rctx->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
67         rctx->cryptlen = req->nbytes;
68
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;
72
73         rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
74         if (diff_dst)
75                 rctx->dst_nents = sg_nents_for_len(req->dst, req->nbytes);
76         else
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;
81         }
82         if (rctx->dst_nents < 0) {
83                 dev_err(qce->dev, "Invalid numbers of dst SG.\n");
84                 return -rctx->dst_nents;
85         }
86
87         rctx->dst_nents += 1;
88
89         gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
90                                                 GFP_KERNEL : GFP_ATOMIC;
91
92         ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
93         if (ret)
94                 return ret;
95
96         sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
97
98         sg = qce_sgtable_add(&rctx->dst_tbl, req->dst);
99         if (IS_ERR(sg)) {
100                 ret = PTR_ERR(sg);
101                 goto error_free;
102         }
103
104         sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg);
105         if (IS_ERR(sg)) {
106                 ret = PTR_ERR(sg);
107                 goto error_free;
108         }
109
110         sg_mark_end(sg);
111         rctx->dst_sg = rctx->dst_tbl.sgl;
112
113         ret = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
114         if (ret < 0)
115                 goto error_free;
116
117         if (diff_dst) {
118                 ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
119                 if (ret < 0)
120                         goto error_unmap_dst;
121                 rctx->src_sg = req->src;
122         } else {
123                 rctx->src_sg = rctx->dst_sg;
124         }
125
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);
129         if (ret)
130                 goto error_unmap_src;
131
132         qce_dma_issue_pending(&qce->dma);
133
134         ret = qce_start(async_req, tmpl->crypto_alg_type, req->nbytes, 0);
135         if (ret)
136                 goto error_terminate;
137
138         return 0;
139
140 error_terminate:
141         qce_dma_terminate_all(&qce->dma);
142 error_unmap_src:
143         if (diff_dst)
144                 dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
145 error_unmap_dst:
146         dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
147 error_free:
148         sg_free_table(&rctx->dst_tbl);
149         return ret;
150 }
151
152 static int qce_ablkcipher_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
153                                  unsigned int keylen)
154 {
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;
158         int ret;
159
160         if (!key || !keylen)
161                 return -EINVAL;
162
163         if (IS_AES(flags)) {
164                 switch (keylen) {
165                 case AES_KEYSIZE_128:
166                 case AES_KEYSIZE_256:
167                         break;
168                 default:
169                         goto fallback;
170                 }
171         } else if (IS_DES(flags)) {
172                 u32 tmp[DES_EXPKEY_WORDS];
173
174                 ret = des_ekey(tmp, key);
175                 if (!ret && (crypto_ablkcipher_get_flags(ablk) &
176                              CRYPTO_TFM_REQ_FORBID_WEAK_KEYS))
177                         goto weakkey;
178         }
179
180         ctx->enc_keylen = keylen;
181         memcpy(ctx->enc_key, key, keylen);
182         return 0;
183 fallback:
184         ret = crypto_sync_skcipher_setkey(ctx->fallback, key, keylen);
185         if (!ret)
186                 ctx->enc_keylen = keylen;
187         return ret;
188 weakkey:
189         crypto_ablkcipher_set_flags(ablk, CRYPTO_TFM_RES_WEAK_KEY);
190         return -EINVAL;
191 }
192
193 static int qce_des3_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
194                            unsigned int keylen)
195 {
196         struct qce_cipher_ctx *ctx = crypto_ablkcipher_ctx(ablk);
197         u32 flags;
198         int err;
199
200         flags = crypto_ablkcipher_get_flags(ablk);
201         err = __des3_verify_key(&flags, key);
202         if (unlikely(err)) {
203                 crypto_ablkcipher_set_flags(ablk, flags);
204                 return err;
205         }
206
207         ctx->enc_keylen = keylen;
208         memcpy(ctx->enc_key, key, keylen);
209         return 0;
210 }
211
212 static int qce_ablkcipher_crypt(struct ablkcipher_request *req, int encrypt)
213 {
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);
219         int ret;
220
221         rctx->flags = tmpl->alg_flags;
222         rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
223
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);
227
228                 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
229                 skcipher_request_set_callback(subreq, req->base.flags,
230                                               NULL, NULL);
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);
236                 return ret;
237         }
238
239         return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
240 }
241
242 static int qce_ablkcipher_encrypt(struct ablkcipher_request *req)
243 {
244         return qce_ablkcipher_crypt(req, 1);
245 }
246
247 static int qce_ablkcipher_decrypt(struct ablkcipher_request *req)
248 {
249         return qce_ablkcipher_crypt(req, 0);
250 }
251
252 static int qce_ablkcipher_init(struct crypto_tfm *tfm)
253 {
254         struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
255
256         memset(ctx, 0, sizeof(*ctx));
257         tfm->crt_ablkcipher.reqsize = sizeof(struct qce_cipher_reqctx);
258
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);
262 }
263
264 static void qce_ablkcipher_exit(struct crypto_tfm *tfm)
265 {
266         struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
267
268         crypto_free_sync_skcipher(ctx->fallback);
269 }
270
271 struct qce_ablkcipher_def {
272         unsigned long flags;
273         const char *name;
274         const char *drv_name;
275         unsigned int blocksize;
276         unsigned int ivsize;
277         unsigned int min_keysize;
278         unsigned int max_keysize;
279 };
280
281 static const struct qce_ablkcipher_def ablkcipher_def[] = {
282         {
283                 .flags          = QCE_ALG_AES | QCE_MODE_ECB,
284                 .name           = "ecb(aes)",
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,
290         },
291         {
292                 .flags          = QCE_ALG_AES | QCE_MODE_CBC,
293                 .name           = "cbc(aes)",
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,
299         },
300         {
301                 .flags          = QCE_ALG_AES | QCE_MODE_CTR,
302                 .name           = "ctr(aes)",
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,
308         },
309         {
310                 .flags          = QCE_ALG_AES | QCE_MODE_XTS,
311                 .name           = "xts(aes)",
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,
317         },
318         {
319                 .flags          = QCE_ALG_DES | QCE_MODE_ECB,
320                 .name           = "ecb(des)",
321                 .drv_name       = "ecb-des-qce",
322                 .blocksize      = DES_BLOCK_SIZE,
323                 .ivsize         = 0,
324                 .min_keysize    = DES_KEY_SIZE,
325                 .max_keysize    = DES_KEY_SIZE,
326         },
327         {
328                 .flags          = QCE_ALG_DES | QCE_MODE_CBC,
329                 .name           = "cbc(des)",
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,
335         },
336         {
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,
341                 .ivsize         = 0,
342                 .min_keysize    = DES3_EDE_KEY_SIZE,
343                 .max_keysize    = DES3_EDE_KEY_SIZE,
344         },
345         {
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,
353         },
354 };
355
356 static int qce_ablkcipher_register_one(const struct qce_ablkcipher_def *def,
357                                        struct qce_device *qce)
358 {
359         struct qce_alg_template *tmpl;
360         struct crypto_alg *alg;
361         int ret;
362
363         tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
364         if (!tmpl)
365                 return -ENOMEM;
366
367         alg = &tmpl->alg.crypto;
368
369         snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
370         snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
371                  def->drv_name);
372
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;
381
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;
391
392         INIT_LIST_HEAD(&tmpl->entry);
393         tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_ABLKCIPHER;
394         tmpl->alg_flags = def->flags;
395         tmpl->qce = qce;
396
397         ret = crypto_register_alg(alg);
398         if (ret) {
399                 kfree(tmpl);
400                 dev_err(qce->dev, "%s registration failed\n", alg->cra_name);
401                 return ret;
402         }
403
404         list_add_tail(&tmpl->entry, &ablkcipher_algs);
405         dev_dbg(qce->dev, "%s is registered\n", alg->cra_name);
406         return 0;
407 }
408
409 static void qce_ablkcipher_unregister(struct qce_device *qce)
410 {
411         struct qce_alg_template *tmpl, *n;
412
413         list_for_each_entry_safe(tmpl, n, &ablkcipher_algs, entry) {
414                 crypto_unregister_alg(&tmpl->alg.crypto);
415                 list_del(&tmpl->entry);
416                 kfree(tmpl);
417         }
418 }
419
420 static int qce_ablkcipher_register(struct qce_device *qce)
421 {
422         int ret, i;
423
424         for (i = 0; i < ARRAY_SIZE(ablkcipher_def); i++) {
425                 ret = qce_ablkcipher_register_one(&ablkcipher_def[i], qce);
426                 if (ret)
427                         goto err;
428         }
429
430         return 0;
431 err:
432         qce_ablkcipher_unregister(qce);
433         return ret;
434 }
435
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,
441 };