dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / crypto / hisilicon / sec2 / sec_crypto.c
blob7f1c6a31b82f35360be719233fcbe693ca59782c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
4 #include <crypto/aes.h>
5 #include <crypto/algapi.h>
6 #include <crypto/authenc.h>
7 #include <crypto/des.h>
8 #include <crypto/hash.h>
9 #include <crypto/internal/aead.h>
10 #include <crypto/sha.h>
11 #include <crypto/skcipher.h>
12 #include <crypto/xts.h>
13 #include <linux/crypto.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/idr.h>
17 #include "sec.h"
18 #include "sec_crypto.h"
20 #define SEC_PRIORITY 4001
21 #define SEC_XTS_MIN_KEY_SIZE (2 * AES_MIN_KEY_SIZE)
22 #define SEC_XTS_MAX_KEY_SIZE (2 * AES_MAX_KEY_SIZE)
23 #define SEC_DES3_2KEY_SIZE (2 * DES_KEY_SIZE)
24 #define SEC_DES3_3KEY_SIZE (3 * DES_KEY_SIZE)
26 /* SEC sqe(bd) bit operational relative MACRO */
27 #define SEC_DE_OFFSET 1
28 #define SEC_CIPHER_OFFSET 4
29 #define SEC_SCENE_OFFSET 3
30 #define SEC_DST_SGL_OFFSET 2
31 #define SEC_SRC_SGL_OFFSET 7
32 #define SEC_CKEY_OFFSET 9
33 #define SEC_CMODE_OFFSET 12
34 #define SEC_AKEY_OFFSET 5
35 #define SEC_AEAD_ALG_OFFSET 11
36 #define SEC_AUTH_OFFSET 6
38 #define SEC_FLAG_OFFSET 7
39 #define SEC_FLAG_MASK 0x0780
40 #define SEC_TYPE_MASK 0x0F
41 #define SEC_DONE_MASK 0x0001
43 #define SEC_TOTAL_IV_SZ (SEC_IV_SIZE * QM_Q_DEPTH)
44 #define SEC_SGL_SGE_NR 128
45 #define SEC_CTX_DEV(ctx) (&(ctx)->sec->qm.pdev->dev)
46 #define SEC_CIPHER_AUTH 0xfe
47 #define SEC_AUTH_CIPHER 0x1
48 #define SEC_MAX_MAC_LEN 64
49 #define SEC_MAX_AAD_LEN 65535
50 #define SEC_TOTAL_MAC_SZ (SEC_MAX_MAC_LEN * QM_Q_DEPTH)
52 #define SEC_PBUF_SZ 512
53 #define SEC_PBUF_IV_OFFSET SEC_PBUF_SZ
54 #define SEC_PBUF_MAC_OFFSET (SEC_PBUF_SZ + SEC_IV_SIZE)
55 #define SEC_PBUF_PKG (SEC_PBUF_SZ + SEC_IV_SIZE + \
56 SEC_MAX_MAC_LEN * 2)
57 #define SEC_PBUF_NUM (PAGE_SIZE / SEC_PBUF_PKG)
58 #define SEC_PBUF_PAGE_NUM (QM_Q_DEPTH / SEC_PBUF_NUM)
59 #define SEC_PBUF_LEFT_SZ (SEC_PBUF_PKG * (QM_Q_DEPTH - \
60 SEC_PBUF_PAGE_NUM * SEC_PBUF_NUM))
61 #define SEC_TOTAL_PBUF_SZ (PAGE_SIZE * SEC_PBUF_PAGE_NUM + \
62 SEC_PBUF_LEFT_SZ)
64 #define SEC_SQE_LEN_RATE 4
65 #define SEC_SQE_CFLAG 2
66 #define SEC_SQE_AEAD_FLAG 3
67 #define SEC_SQE_DONE 0x1
69 static atomic_t sec_active_devs;
71 /* Get an en/de-cipher queue cyclically to balance load over queues of TFM */
72 static inline int sec_alloc_queue_id(struct sec_ctx *ctx, struct sec_req *req)
74 if (req->c_req.encrypt)
75 return (u32)atomic_inc_return(&ctx->enc_qcyclic) %
76 ctx->hlf_q_num;
78 return (u32)atomic_inc_return(&ctx->dec_qcyclic) % ctx->hlf_q_num +
79 ctx->hlf_q_num;
82 static inline void sec_free_queue_id(struct sec_ctx *ctx, struct sec_req *req)
84 if (req->c_req.encrypt)
85 atomic_dec(&ctx->enc_qcyclic);
86 else
87 atomic_dec(&ctx->dec_qcyclic);
90 static int sec_alloc_req_id(struct sec_req *req, struct sec_qp_ctx *qp_ctx)
92 int req_id;
94 mutex_lock(&qp_ctx->req_lock);
96 req_id = idr_alloc_cyclic(&qp_ctx->req_idr, NULL,
97 0, QM_Q_DEPTH, GFP_ATOMIC);
98 mutex_unlock(&qp_ctx->req_lock);
99 if (unlikely(req_id < 0)) {
100 dev_err(SEC_CTX_DEV(req->ctx), "alloc req id fail!\n");
101 return req_id;
104 req->qp_ctx = qp_ctx;
105 qp_ctx->req_list[req_id] = req;
106 return req_id;
109 static void sec_free_req_id(struct sec_req *req)
111 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
112 int req_id = req->req_id;
114 if (unlikely(req_id < 0 || req_id >= QM_Q_DEPTH)) {
115 dev_err(SEC_CTX_DEV(req->ctx), "free request id invalid!\n");
116 return;
119 qp_ctx->req_list[req_id] = NULL;
120 req->qp_ctx = NULL;
122 mutex_lock(&qp_ctx->req_lock);
123 idr_remove(&qp_ctx->req_idr, req_id);
124 mutex_unlock(&qp_ctx->req_lock);
127 static int sec_aead_verify(struct sec_req *req)
129 struct aead_request *aead_req = req->aead_req.aead_req;
130 struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
131 size_t authsize = crypto_aead_authsize(tfm);
132 u8 *mac_out = req->aead_req.out_mac;
133 u8 *mac = mac_out + SEC_MAX_MAC_LEN;
134 struct scatterlist *sgl = aead_req->src;
135 size_t sz;
137 sz = sg_pcopy_to_buffer(sgl, sg_nents(sgl), mac, authsize,
138 aead_req->cryptlen + aead_req->assoclen -
139 authsize);
140 if (unlikely(sz != authsize || memcmp(mac_out, mac, sz))) {
141 dev_err(SEC_CTX_DEV(req->ctx), "aead verify failure!\n");
142 return -EBADMSG;
145 return 0;
148 static void sec_req_cb(struct hisi_qp *qp, void *resp)
150 struct sec_qp_ctx *qp_ctx = qp->qp_ctx;
151 struct sec_sqe *bd = resp;
152 struct sec_ctx *ctx;
153 struct sec_req *req;
154 u16 done, flag;
155 int err = 0;
156 u8 type;
158 type = bd->type_cipher_auth & SEC_TYPE_MASK;
159 if (unlikely(type != SEC_BD_TYPE2)) {
160 pr_err("err bd type [%d]\n", type);
161 return;
164 req = qp_ctx->req_list[le16_to_cpu(bd->type2.tag)];
165 req->err_type = bd->type2.error_type;
166 ctx = req->ctx;
167 done = le16_to_cpu(bd->type2.done_flag) & SEC_DONE_MASK;
168 flag = (le16_to_cpu(bd->type2.done_flag) &
169 SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
170 if (unlikely(req->err_type || done != SEC_SQE_DONE ||
171 (ctx->alg_type == SEC_SKCIPHER && flag != SEC_SQE_CFLAG) ||
172 (ctx->alg_type == SEC_AEAD && flag != SEC_SQE_AEAD_FLAG))) {
173 dev_err(SEC_CTX_DEV(ctx),
174 "err_type[%d],done[%d],flag[%d]\n",
175 req->err_type, done, flag);
176 err = -EIO;
179 if (ctx->alg_type == SEC_AEAD && !req->c_req.encrypt)
180 err = sec_aead_verify(req);
182 atomic64_inc(&ctx->sec->debug.dfx.recv_cnt);
184 ctx->req_op->buf_unmap(ctx, req);
186 ctx->req_op->callback(ctx, req, err);
189 static int sec_bd_send(struct sec_ctx *ctx, struct sec_req *req)
191 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
192 int ret;
194 mutex_lock(&qp_ctx->req_lock);
195 ret = hisi_qp_send(qp_ctx->qp, &req->sec_sqe);
196 mutex_unlock(&qp_ctx->req_lock);
197 atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
199 if (unlikely(ret == -EBUSY))
200 return -ENOBUFS;
202 if (!ret) {
203 if (req->fake_busy)
204 ret = -EBUSY;
205 else
206 ret = -EINPROGRESS;
209 return ret;
212 /* Get DMA memory resources */
213 static int sec_alloc_civ_resource(struct device *dev, struct sec_alg_res *res)
215 int i;
217 res->c_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ,
218 &res->c_ivin_dma, GFP_KERNEL);
219 if (!res->c_ivin)
220 return -ENOMEM;
222 for (i = 1; i < QM_Q_DEPTH; i++) {
223 res[i].c_ivin_dma = res->c_ivin_dma + i * SEC_IV_SIZE;
224 res[i].c_ivin = res->c_ivin + i * SEC_IV_SIZE;
227 return 0;
230 static void sec_free_civ_resource(struct device *dev, struct sec_alg_res *res)
232 if (res->c_ivin)
233 dma_free_coherent(dev, SEC_TOTAL_IV_SZ,
234 res->c_ivin, res->c_ivin_dma);
237 static int sec_alloc_mac_resource(struct device *dev, struct sec_alg_res *res)
239 int i;
241 res->out_mac = dma_alloc_coherent(dev, SEC_TOTAL_MAC_SZ << 1,
242 &res->out_mac_dma, GFP_KERNEL);
243 if (!res->out_mac)
244 return -ENOMEM;
246 for (i = 1; i < QM_Q_DEPTH; i++) {
247 res[i].out_mac_dma = res->out_mac_dma +
248 i * (SEC_MAX_MAC_LEN << 1);
249 res[i].out_mac = res->out_mac + i * (SEC_MAX_MAC_LEN << 1);
252 return 0;
255 static void sec_free_mac_resource(struct device *dev, struct sec_alg_res *res)
257 if (res->out_mac)
258 dma_free_coherent(dev, SEC_TOTAL_MAC_SZ << 1,
259 res->out_mac, res->out_mac_dma);
262 static void sec_free_pbuf_resource(struct device *dev, struct sec_alg_res *res)
264 if (res->pbuf)
265 dma_free_coherent(dev, SEC_TOTAL_PBUF_SZ,
266 res->pbuf, res->pbuf_dma);
270 * To improve performance, pbuffer is used for
271 * small packets (< 512Bytes) as IOMMU translation using.
273 static int sec_alloc_pbuf_resource(struct device *dev, struct sec_alg_res *res)
275 int pbuf_page_offset;
276 int i, j, k;
278 res->pbuf = dma_alloc_coherent(dev, SEC_TOTAL_PBUF_SZ,
279 &res->pbuf_dma, GFP_KERNEL);
280 if (!res->pbuf)
281 return -ENOMEM;
284 * SEC_PBUF_PKG contains data pbuf, iv and
285 * out_mac : <SEC_PBUF|SEC_IV|SEC_MAC>
286 * Every PAGE contains six SEC_PBUF_PKG
287 * The sec_qp_ctx contains QM_Q_DEPTH numbers of SEC_PBUF_PKG
288 * So we need SEC_PBUF_PAGE_NUM numbers of PAGE
289 * for the SEC_TOTAL_PBUF_SZ
291 for (i = 0; i <= SEC_PBUF_PAGE_NUM; i++) {
292 pbuf_page_offset = PAGE_SIZE * i;
293 for (j = 0; j < SEC_PBUF_NUM; j++) {
294 k = i * SEC_PBUF_NUM + j;
295 if (k == QM_Q_DEPTH)
296 break;
297 res[k].pbuf = res->pbuf +
298 j * SEC_PBUF_PKG + pbuf_page_offset;
299 res[k].pbuf_dma = res->pbuf_dma +
300 j * SEC_PBUF_PKG + pbuf_page_offset;
303 return 0;
306 static int sec_alg_resource_alloc(struct sec_ctx *ctx,
307 struct sec_qp_ctx *qp_ctx)
309 struct device *dev = SEC_CTX_DEV(ctx);
310 struct sec_alg_res *res = qp_ctx->res;
311 int ret;
313 ret = sec_alloc_civ_resource(dev, res);
314 if (ret)
315 return ret;
317 if (ctx->alg_type == SEC_AEAD) {
318 ret = sec_alloc_mac_resource(dev, res);
319 if (ret)
320 goto alloc_fail;
322 if (ctx->pbuf_supported) {
323 ret = sec_alloc_pbuf_resource(dev, res);
324 if (ret) {
325 dev_err(dev, "fail to alloc pbuf dma resource!\n");
326 goto alloc_fail;
330 return 0;
331 alloc_fail:
332 sec_free_civ_resource(dev, res);
334 return ret;
337 static void sec_alg_resource_free(struct sec_ctx *ctx,
338 struct sec_qp_ctx *qp_ctx)
340 struct device *dev = SEC_CTX_DEV(ctx);
342 sec_free_civ_resource(dev, qp_ctx->res);
344 if (ctx->pbuf_supported)
345 sec_free_pbuf_resource(dev, qp_ctx->res);
346 if (ctx->alg_type == SEC_AEAD)
347 sec_free_mac_resource(dev, qp_ctx->res);
350 static int sec_create_qp_ctx(struct hisi_qm *qm, struct sec_ctx *ctx,
351 int qp_ctx_id, int alg_type)
353 struct device *dev = SEC_CTX_DEV(ctx);
354 struct sec_qp_ctx *qp_ctx;
355 struct hisi_qp *qp;
356 int ret = -ENOMEM;
358 qp_ctx = &ctx->qp_ctx[qp_ctx_id];
359 qp = ctx->qps[qp_ctx_id];
360 qp->req_type = 0;
361 qp->qp_ctx = qp_ctx;
362 qp->req_cb = sec_req_cb;
363 qp_ctx->qp = qp;
364 qp_ctx->ctx = ctx;
366 mutex_init(&qp_ctx->req_lock);
367 atomic_set(&qp_ctx->pending_reqs, 0);
368 idr_init(&qp_ctx->req_idr);
370 qp_ctx->c_in_pool = hisi_acc_create_sgl_pool(dev, QM_Q_DEPTH,
371 SEC_SGL_SGE_NR);
372 if (IS_ERR(qp_ctx->c_in_pool)) {
373 dev_err(dev, "fail to create sgl pool for input!\n");
374 goto err_destroy_idr;
377 qp_ctx->c_out_pool = hisi_acc_create_sgl_pool(dev, QM_Q_DEPTH,
378 SEC_SGL_SGE_NR);
379 if (IS_ERR(qp_ctx->c_out_pool)) {
380 dev_err(dev, "fail to create sgl pool for output!\n");
381 goto err_free_c_in_pool;
384 ret = sec_alg_resource_alloc(ctx, qp_ctx);
385 if (ret)
386 goto err_free_c_out_pool;
388 ret = hisi_qm_start_qp(qp, 0);
389 if (ret < 0)
390 goto err_queue_free;
392 return 0;
394 err_queue_free:
395 sec_alg_resource_free(ctx, qp_ctx);
396 err_free_c_out_pool:
397 hisi_acc_free_sgl_pool(dev, qp_ctx->c_out_pool);
398 err_free_c_in_pool:
399 hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
400 err_destroy_idr:
401 idr_destroy(&qp_ctx->req_idr);
403 return ret;
406 static void sec_release_qp_ctx(struct sec_ctx *ctx,
407 struct sec_qp_ctx *qp_ctx)
409 struct device *dev = SEC_CTX_DEV(ctx);
411 hisi_qm_stop_qp(qp_ctx->qp);
412 sec_alg_resource_free(ctx, qp_ctx);
414 hisi_acc_free_sgl_pool(dev, qp_ctx->c_out_pool);
415 hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
417 idr_destroy(&qp_ctx->req_idr);
420 static int sec_ctx_base_init(struct sec_ctx *ctx)
422 struct sec_dev *sec;
423 int i, ret;
425 ctx->qps = sec_create_qps();
426 if (!ctx->qps) {
427 pr_err("Can not create sec qps!\n");
428 return -ENODEV;
431 sec = container_of(ctx->qps[0]->qm, struct sec_dev, qm);
432 ctx->sec = sec;
433 ctx->hlf_q_num = sec->ctx_q_num >> 1;
435 ctx->pbuf_supported = ctx->sec->iommu_used;
437 /* Half of queue depth is taken as fake requests limit in the queue. */
438 ctx->fake_req_limit = QM_Q_DEPTH >> 1;
439 ctx->qp_ctx = kcalloc(sec->ctx_q_num, sizeof(struct sec_qp_ctx),
440 GFP_KERNEL);
441 if (!ctx->qp_ctx)
442 return -ENOMEM;
444 for (i = 0; i < sec->ctx_q_num; i++) {
445 ret = sec_create_qp_ctx(&sec->qm, ctx, i, 0);
446 if (ret)
447 goto err_sec_release_qp_ctx;
450 return 0;
451 err_sec_release_qp_ctx:
452 for (i = i - 1; i >= 0; i--)
453 sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
455 sec_destroy_qps(ctx->qps, sec->ctx_q_num);
456 kfree(ctx->qp_ctx);
457 return ret;
460 static void sec_ctx_base_uninit(struct sec_ctx *ctx)
462 int i;
464 for (i = 0; i < ctx->sec->ctx_q_num; i++)
465 sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
467 sec_destroy_qps(ctx->qps, ctx->sec->ctx_q_num);
468 kfree(ctx->qp_ctx);
471 static int sec_cipher_init(struct sec_ctx *ctx)
473 struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
475 c_ctx->c_key = dma_alloc_coherent(SEC_CTX_DEV(ctx), SEC_MAX_KEY_SIZE,
476 &c_ctx->c_key_dma, GFP_KERNEL);
477 if (!c_ctx->c_key)
478 return -ENOMEM;
480 return 0;
483 static void sec_cipher_uninit(struct sec_ctx *ctx)
485 struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
487 memzero_explicit(c_ctx->c_key, SEC_MAX_KEY_SIZE);
488 dma_free_coherent(SEC_CTX_DEV(ctx), SEC_MAX_KEY_SIZE,
489 c_ctx->c_key, c_ctx->c_key_dma);
492 static int sec_auth_init(struct sec_ctx *ctx)
494 struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
496 a_ctx->a_key = dma_alloc_coherent(SEC_CTX_DEV(ctx), SEC_MAX_KEY_SIZE,
497 &a_ctx->a_key_dma, GFP_KERNEL);
498 if (!a_ctx->a_key)
499 return -ENOMEM;
501 return 0;
504 static void sec_auth_uninit(struct sec_ctx *ctx)
506 struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
508 memzero_explicit(a_ctx->a_key, SEC_MAX_KEY_SIZE);
509 dma_free_coherent(SEC_CTX_DEV(ctx), SEC_MAX_KEY_SIZE,
510 a_ctx->a_key, a_ctx->a_key_dma);
513 static int sec_skcipher_init(struct crypto_skcipher *tfm)
515 struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
516 int ret;
518 ctx->alg_type = SEC_SKCIPHER;
519 crypto_skcipher_set_reqsize(tfm, sizeof(struct sec_req));
520 ctx->c_ctx.ivsize = crypto_skcipher_ivsize(tfm);
521 if (ctx->c_ctx.ivsize > SEC_IV_SIZE) {
522 dev_err(SEC_CTX_DEV(ctx), "get error skcipher iv size!\n");
523 return -EINVAL;
526 ret = sec_ctx_base_init(ctx);
527 if (ret)
528 return ret;
530 ret = sec_cipher_init(ctx);
531 if (ret)
532 goto err_cipher_init;
534 return 0;
535 err_cipher_init:
536 sec_ctx_base_uninit(ctx);
538 return ret;
541 static void sec_skcipher_uninit(struct crypto_skcipher *tfm)
543 struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
545 sec_cipher_uninit(ctx);
546 sec_ctx_base_uninit(ctx);
549 static int sec_skcipher_3des_setkey(struct sec_cipher_ctx *c_ctx,
550 const u32 keylen,
551 const enum sec_cmode c_mode)
553 switch (keylen) {
554 case SEC_DES3_2KEY_SIZE:
555 c_ctx->c_key_len = SEC_CKEY_3DES_2KEY;
556 break;
557 case SEC_DES3_3KEY_SIZE:
558 c_ctx->c_key_len = SEC_CKEY_3DES_3KEY;
559 break;
560 default:
561 return -EINVAL;
564 return 0;
567 static int sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx *c_ctx,
568 const u32 keylen,
569 const enum sec_cmode c_mode)
571 if (c_mode == SEC_CMODE_XTS) {
572 switch (keylen) {
573 case SEC_XTS_MIN_KEY_SIZE:
574 c_ctx->c_key_len = SEC_CKEY_128BIT;
575 break;
576 case SEC_XTS_MAX_KEY_SIZE:
577 c_ctx->c_key_len = SEC_CKEY_256BIT;
578 break;
579 default:
580 pr_err("hisi_sec2: xts mode key error!\n");
581 return -EINVAL;
583 } else {
584 switch (keylen) {
585 case AES_KEYSIZE_128:
586 c_ctx->c_key_len = SEC_CKEY_128BIT;
587 break;
588 case AES_KEYSIZE_192:
589 c_ctx->c_key_len = SEC_CKEY_192BIT;
590 break;
591 case AES_KEYSIZE_256:
592 c_ctx->c_key_len = SEC_CKEY_256BIT;
593 break;
594 default:
595 pr_err("hisi_sec2: aes key error!\n");
596 return -EINVAL;
600 return 0;
603 static int sec_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
604 const u32 keylen, const enum sec_calg c_alg,
605 const enum sec_cmode c_mode)
607 struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
608 struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
609 int ret;
611 if (c_mode == SEC_CMODE_XTS) {
612 ret = xts_verify_key(tfm, key, keylen);
613 if (ret) {
614 dev_err(SEC_CTX_DEV(ctx), "xts mode key err!\n");
615 return ret;
619 c_ctx->c_alg = c_alg;
620 c_ctx->c_mode = c_mode;
622 switch (c_alg) {
623 case SEC_CALG_3DES:
624 ret = sec_skcipher_3des_setkey(c_ctx, keylen, c_mode);
625 break;
626 case SEC_CALG_AES:
627 case SEC_CALG_SM4:
628 ret = sec_skcipher_aes_sm4_setkey(c_ctx, keylen, c_mode);
629 break;
630 default:
631 return -EINVAL;
634 if (ret) {
635 dev_err(SEC_CTX_DEV(ctx), "set sec key err!\n");
636 return ret;
639 memcpy(c_ctx->c_key, key, keylen);
641 return 0;
644 #define GEN_SEC_SETKEY_FUNC(name, c_alg, c_mode) \
645 static int sec_setkey_##name(struct crypto_skcipher *tfm, const u8 *key,\
646 u32 keylen) \
648 return sec_skcipher_setkey(tfm, key, keylen, c_alg, c_mode); \
651 GEN_SEC_SETKEY_FUNC(aes_ecb, SEC_CALG_AES, SEC_CMODE_ECB)
652 GEN_SEC_SETKEY_FUNC(aes_cbc, SEC_CALG_AES, SEC_CMODE_CBC)
653 GEN_SEC_SETKEY_FUNC(aes_xts, SEC_CALG_AES, SEC_CMODE_XTS)
655 GEN_SEC_SETKEY_FUNC(3des_ecb, SEC_CALG_3DES, SEC_CMODE_ECB)
656 GEN_SEC_SETKEY_FUNC(3des_cbc, SEC_CALG_3DES, SEC_CMODE_CBC)
658 GEN_SEC_SETKEY_FUNC(sm4_xts, SEC_CALG_SM4, SEC_CMODE_XTS)
659 GEN_SEC_SETKEY_FUNC(sm4_cbc, SEC_CALG_SM4, SEC_CMODE_CBC)
661 static int sec_cipher_pbuf_map(struct sec_ctx *ctx, struct sec_req *req,
662 struct scatterlist *src)
664 struct aead_request *aead_req = req->aead_req.aead_req;
665 struct sec_cipher_req *c_req = &req->c_req;
666 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
667 struct device *dev = SEC_CTX_DEV(ctx);
668 int copy_size, pbuf_length;
669 int req_id = req->req_id;
671 if (ctx->alg_type == SEC_AEAD)
672 copy_size = aead_req->cryptlen + aead_req->assoclen;
673 else
674 copy_size = c_req->c_len;
676 pbuf_length = sg_copy_to_buffer(src, sg_nents(src),
677 qp_ctx->res[req_id].pbuf,
678 copy_size);
680 if (unlikely(pbuf_length != copy_size)) {
681 dev_err(dev, "copy src data to pbuf error!\n");
682 return -EINVAL;
685 c_req->c_in_dma = qp_ctx->res[req_id].pbuf_dma;
687 if (!c_req->c_in_dma) {
688 dev_err(dev, "fail to set pbuffer address!\n");
689 return -ENOMEM;
692 c_req->c_out_dma = c_req->c_in_dma;
694 return 0;
697 static void sec_cipher_pbuf_unmap(struct sec_ctx *ctx, struct sec_req *req,
698 struct scatterlist *dst)
700 struct aead_request *aead_req = req->aead_req.aead_req;
701 struct sec_cipher_req *c_req = &req->c_req;
702 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
703 struct device *dev = SEC_CTX_DEV(ctx);
704 int copy_size, pbuf_length;
705 int req_id = req->req_id;
707 if (ctx->alg_type == SEC_AEAD)
708 copy_size = c_req->c_len + aead_req->assoclen;
709 else
710 copy_size = c_req->c_len;
712 pbuf_length = sg_copy_from_buffer(dst, sg_nents(dst),
713 qp_ctx->res[req_id].pbuf,
714 copy_size);
716 if (unlikely(pbuf_length != copy_size))
717 dev_err(dev, "copy pbuf data to dst error!\n");
721 static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
722 struct scatterlist *src, struct scatterlist *dst)
724 struct sec_cipher_req *c_req = &req->c_req;
725 struct sec_aead_req *a_req = &req->aead_req;
726 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
727 struct sec_alg_res *res = &qp_ctx->res[req->req_id];
728 struct device *dev = SEC_CTX_DEV(ctx);
729 int ret;
731 if (req->use_pbuf) {
732 ret = sec_cipher_pbuf_map(ctx, req, src);
733 c_req->c_ivin = res->pbuf + SEC_PBUF_IV_OFFSET;
734 c_req->c_ivin_dma = res->pbuf_dma + SEC_PBUF_IV_OFFSET;
735 if (ctx->alg_type == SEC_AEAD) {
736 a_req->out_mac = res->pbuf + SEC_PBUF_MAC_OFFSET;
737 a_req->out_mac_dma = res->pbuf_dma +
738 SEC_PBUF_MAC_OFFSET;
741 return ret;
743 c_req->c_ivin = res->c_ivin;
744 c_req->c_ivin_dma = res->c_ivin_dma;
745 if (ctx->alg_type == SEC_AEAD) {
746 a_req->out_mac = res->out_mac;
747 a_req->out_mac_dma = res->out_mac_dma;
750 c_req->c_in = hisi_acc_sg_buf_map_to_hw_sgl(dev, src,
751 qp_ctx->c_in_pool,
752 req->req_id,
753 &c_req->c_in_dma);
755 if (IS_ERR(c_req->c_in)) {
756 dev_err(dev, "fail to dma map input sgl buffers!\n");
757 return PTR_ERR(c_req->c_in);
760 if (dst == src) {
761 c_req->c_out = c_req->c_in;
762 c_req->c_out_dma = c_req->c_in_dma;
763 } else {
764 c_req->c_out = hisi_acc_sg_buf_map_to_hw_sgl(dev, dst,
765 qp_ctx->c_out_pool,
766 req->req_id,
767 &c_req->c_out_dma);
769 if (IS_ERR(c_req->c_out)) {
770 dev_err(dev, "fail to dma map output sgl buffers!\n");
771 hisi_acc_sg_buf_unmap(dev, src, c_req->c_in);
772 return PTR_ERR(c_req->c_out);
776 return 0;
779 static void sec_cipher_unmap(struct sec_ctx *ctx, struct sec_req *req,
780 struct scatterlist *src, struct scatterlist *dst)
782 struct sec_cipher_req *c_req = &req->c_req;
783 struct device *dev = SEC_CTX_DEV(ctx);
785 if (req->use_pbuf) {
786 sec_cipher_pbuf_unmap(ctx, req, dst);
787 } else {
788 if (dst != src)
789 hisi_acc_sg_buf_unmap(dev, src, c_req->c_in);
791 hisi_acc_sg_buf_unmap(dev, dst, c_req->c_out);
795 static int sec_skcipher_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
797 struct skcipher_request *sq = req->c_req.sk_req;
799 return sec_cipher_map(ctx, req, sq->src, sq->dst);
802 static void sec_skcipher_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
804 struct skcipher_request *sq = req->c_req.sk_req;
806 sec_cipher_unmap(ctx, req, sq->src, sq->dst);
809 static int sec_aead_aes_set_key(struct sec_cipher_ctx *c_ctx,
810 struct crypto_authenc_keys *keys)
812 switch (keys->enckeylen) {
813 case AES_KEYSIZE_128:
814 c_ctx->c_key_len = SEC_CKEY_128BIT;
815 break;
816 case AES_KEYSIZE_192:
817 c_ctx->c_key_len = SEC_CKEY_192BIT;
818 break;
819 case AES_KEYSIZE_256:
820 c_ctx->c_key_len = SEC_CKEY_256BIT;
821 break;
822 default:
823 pr_err("hisi_sec2: aead aes key error!\n");
824 return -EINVAL;
826 memcpy(c_ctx->c_key, keys->enckey, keys->enckeylen);
828 return 0;
831 static int sec_aead_auth_set_key(struct sec_auth_ctx *ctx,
832 struct crypto_authenc_keys *keys)
834 struct crypto_shash *hash_tfm = ctx->hash_tfm;
835 SHASH_DESC_ON_STACK(shash, hash_tfm);
836 int blocksize, ret;
838 if (!keys->authkeylen) {
839 pr_err("hisi_sec2: aead auth key error!\n");
840 return -EINVAL;
843 blocksize = crypto_shash_blocksize(hash_tfm);
844 if (keys->authkeylen > blocksize) {
845 ret = crypto_shash_digest(shash, keys->authkey,
846 keys->authkeylen, ctx->a_key);
847 if (ret) {
848 pr_err("hisi_sec2: aead auth digest error!\n");
849 return -EINVAL;
851 ctx->a_key_len = blocksize;
852 } else {
853 memcpy(ctx->a_key, keys->authkey, keys->authkeylen);
854 ctx->a_key_len = keys->authkeylen;
857 return 0;
860 static int sec_aead_setkey(struct crypto_aead *tfm, const u8 *key,
861 const u32 keylen, const enum sec_hash_alg a_alg,
862 const enum sec_calg c_alg,
863 const enum sec_mac_len mac_len,
864 const enum sec_cmode c_mode)
866 struct sec_ctx *ctx = crypto_aead_ctx(tfm);
867 struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
868 struct crypto_authenc_keys keys;
869 int ret;
871 ctx->a_ctx.a_alg = a_alg;
872 ctx->c_ctx.c_alg = c_alg;
873 ctx->a_ctx.mac_len = mac_len;
874 c_ctx->c_mode = c_mode;
876 if (crypto_authenc_extractkeys(&keys, key, keylen))
877 goto bad_key;
879 ret = sec_aead_aes_set_key(c_ctx, &keys);
880 if (ret) {
881 dev_err(SEC_CTX_DEV(ctx), "set sec cipher key err!\n");
882 goto bad_key;
885 ret = sec_aead_auth_set_key(&ctx->a_ctx, &keys);
886 if (ret) {
887 dev_err(SEC_CTX_DEV(ctx), "set sec auth key err!\n");
888 goto bad_key;
891 return 0;
892 bad_key:
893 memzero_explicit(&keys, sizeof(struct crypto_authenc_keys));
895 return -EINVAL;
899 #define GEN_SEC_AEAD_SETKEY_FUNC(name, aalg, calg, maclen, cmode) \
900 static int sec_setkey_##name(struct crypto_aead *tfm, const u8 *key, \
901 u32 keylen) \
903 return sec_aead_setkey(tfm, key, keylen, aalg, calg, maclen, cmode);\
906 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1, SEC_A_HMAC_SHA1,
907 SEC_CALG_AES, SEC_HMAC_SHA1_MAC, SEC_CMODE_CBC)
908 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha256, SEC_A_HMAC_SHA256,
909 SEC_CALG_AES, SEC_HMAC_SHA256_MAC, SEC_CMODE_CBC)
910 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha512, SEC_A_HMAC_SHA512,
911 SEC_CALG_AES, SEC_HMAC_SHA512_MAC, SEC_CMODE_CBC)
913 static int sec_aead_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
915 struct aead_request *aq = req->aead_req.aead_req;
917 return sec_cipher_map(ctx, req, aq->src, aq->dst);
920 static void sec_aead_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
922 struct aead_request *aq = req->aead_req.aead_req;
924 sec_cipher_unmap(ctx, req, aq->src, aq->dst);
927 static int sec_request_transfer(struct sec_ctx *ctx, struct sec_req *req)
929 int ret;
931 ret = ctx->req_op->buf_map(ctx, req);
932 if (unlikely(ret))
933 return ret;
935 ctx->req_op->do_transfer(ctx, req);
937 ret = ctx->req_op->bd_fill(ctx, req);
938 if (unlikely(ret))
939 goto unmap_req_buf;
941 return ret;
943 unmap_req_buf:
944 ctx->req_op->buf_unmap(ctx, req);
946 return ret;
949 static void sec_request_untransfer(struct sec_ctx *ctx, struct sec_req *req)
951 ctx->req_op->buf_unmap(ctx, req);
954 static void sec_skcipher_copy_iv(struct sec_ctx *ctx, struct sec_req *req)
956 struct skcipher_request *sk_req = req->c_req.sk_req;
957 struct sec_cipher_req *c_req = &req->c_req;
959 memcpy(c_req->c_ivin, sk_req->iv, ctx->c_ctx.ivsize);
962 static int sec_skcipher_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
964 struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
965 struct sec_cipher_req *c_req = &req->c_req;
966 struct sec_sqe *sec_sqe = &req->sec_sqe;
967 u8 scene, sa_type, da_type;
968 u8 bd_type, cipher;
969 u8 de = 0;
971 memset(sec_sqe, 0, sizeof(struct sec_sqe));
973 sec_sqe->type2.c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
974 sec_sqe->type2.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
975 sec_sqe->type2.data_src_addr = cpu_to_le64(c_req->c_in_dma);
976 sec_sqe->type2.data_dst_addr = cpu_to_le64(c_req->c_out_dma);
978 sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_mode) <<
979 SEC_CMODE_OFFSET);
980 sec_sqe->type2.c_alg = c_ctx->c_alg;
981 sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
982 SEC_CKEY_OFFSET);
984 bd_type = SEC_BD_TYPE2;
985 if (c_req->encrypt)
986 cipher = SEC_CIPHER_ENC << SEC_CIPHER_OFFSET;
987 else
988 cipher = SEC_CIPHER_DEC << SEC_CIPHER_OFFSET;
989 sec_sqe->type_cipher_auth = bd_type | cipher;
991 if (req->use_pbuf)
992 sa_type = SEC_PBUF << SEC_SRC_SGL_OFFSET;
993 else
994 sa_type = SEC_SGL << SEC_SRC_SGL_OFFSET;
995 scene = SEC_COMM_SCENE << SEC_SCENE_OFFSET;
996 if (c_req->c_in_dma != c_req->c_out_dma)
997 de = 0x1 << SEC_DE_OFFSET;
999 sec_sqe->sds_sa_type = (de | scene | sa_type);
1001 /* Just set DST address type */
1002 if (req->use_pbuf)
1003 da_type = SEC_PBUF << SEC_DST_SGL_OFFSET;
1004 else
1005 da_type = SEC_SGL << SEC_DST_SGL_OFFSET;
1006 sec_sqe->sdm_addr_type |= da_type;
1008 sec_sqe->type2.clen_ivhlen |= cpu_to_le32(c_req->c_len);
1009 sec_sqe->type2.tag = cpu_to_le16((u16)req->req_id);
1011 return 0;
1014 static void sec_update_iv(struct sec_req *req, enum sec_alg_type alg_type)
1016 struct aead_request *aead_req = req->aead_req.aead_req;
1017 struct skcipher_request *sk_req = req->c_req.sk_req;
1018 u32 iv_size = req->ctx->c_ctx.ivsize;
1019 struct scatterlist *sgl;
1020 unsigned int cryptlen;
1021 size_t sz;
1022 u8 *iv;
1024 if (req->c_req.encrypt)
1025 sgl = alg_type == SEC_SKCIPHER ? sk_req->dst : aead_req->dst;
1026 else
1027 sgl = alg_type == SEC_SKCIPHER ? sk_req->src : aead_req->src;
1029 if (alg_type == SEC_SKCIPHER) {
1030 iv = sk_req->iv;
1031 cryptlen = sk_req->cryptlen;
1032 } else {
1033 iv = aead_req->iv;
1034 cryptlen = aead_req->cryptlen;
1037 sz = sg_pcopy_to_buffer(sgl, sg_nents(sgl), iv, iv_size,
1038 cryptlen - iv_size);
1039 if (unlikely(sz != iv_size))
1040 dev_err(SEC_CTX_DEV(req->ctx), "copy output iv error!\n");
1043 static void sec_skcipher_callback(struct sec_ctx *ctx, struct sec_req *req,
1044 int err)
1046 struct skcipher_request *sk_req = req->c_req.sk_req;
1047 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1049 atomic_dec(&qp_ctx->pending_reqs);
1050 sec_free_req_id(req);
1052 /* IV output at encrypto of CBC mode */
1053 if (!err && ctx->c_ctx.c_mode == SEC_CMODE_CBC && req->c_req.encrypt)
1054 sec_update_iv(req, SEC_SKCIPHER);
1056 if (req->fake_busy)
1057 sk_req->base.complete(&sk_req->base, -EINPROGRESS);
1059 sk_req->base.complete(&sk_req->base, err);
1062 static void sec_aead_copy_iv(struct sec_ctx *ctx, struct sec_req *req)
1064 struct aead_request *aead_req = req->aead_req.aead_req;
1065 struct sec_cipher_req *c_req = &req->c_req;
1067 memcpy(c_req->c_ivin, aead_req->iv, ctx->c_ctx.ivsize);
1070 static void sec_auth_bd_fill_ex(struct sec_auth_ctx *ctx, int dir,
1071 struct sec_req *req, struct sec_sqe *sec_sqe)
1073 struct sec_aead_req *a_req = &req->aead_req;
1074 struct sec_cipher_req *c_req = &req->c_req;
1075 struct aead_request *aq = a_req->aead_req;
1077 sec_sqe->type2.a_key_addr = cpu_to_le64(ctx->a_key_dma);
1079 sec_sqe->type2.mac_key_alg =
1080 cpu_to_le32(ctx->mac_len / SEC_SQE_LEN_RATE);
1082 sec_sqe->type2.mac_key_alg |=
1083 cpu_to_le32((u32)((ctx->a_key_len) /
1084 SEC_SQE_LEN_RATE) << SEC_AKEY_OFFSET);
1086 sec_sqe->type2.mac_key_alg |=
1087 cpu_to_le32((u32)(ctx->a_alg) << SEC_AEAD_ALG_OFFSET);
1089 sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE1 << SEC_AUTH_OFFSET;
1091 if (dir)
1092 sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1093 else
1094 sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1096 sec_sqe->type2.alen_ivllen = cpu_to_le32(c_req->c_len + aq->assoclen);
1098 sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1100 sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1103 static int sec_aead_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1105 struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1106 struct sec_sqe *sec_sqe = &req->sec_sqe;
1107 int ret;
1109 ret = sec_skcipher_bd_fill(ctx, req);
1110 if (unlikely(ret)) {
1111 dev_err(SEC_CTX_DEV(ctx), "skcipher bd fill is error!\n");
1112 return ret;
1115 sec_auth_bd_fill_ex(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1117 return 0;
1120 static void sec_aead_callback(struct sec_ctx *c, struct sec_req *req, int err)
1122 struct aead_request *a_req = req->aead_req.aead_req;
1123 struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
1124 struct sec_aead_req *aead_req = &req->aead_req;
1125 struct sec_cipher_req *c_req = &req->c_req;
1126 size_t authsize = crypto_aead_authsize(tfm);
1127 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1128 size_t sz;
1130 atomic_dec(&qp_ctx->pending_reqs);
1132 if (!err && c->c_ctx.c_mode == SEC_CMODE_CBC && c_req->encrypt)
1133 sec_update_iv(req, SEC_AEAD);
1135 /* Copy output mac */
1136 if (!err && c_req->encrypt) {
1137 struct scatterlist *sgl = a_req->dst;
1139 sz = sg_pcopy_from_buffer(sgl, sg_nents(sgl),
1140 aead_req->out_mac,
1141 authsize, a_req->cryptlen +
1142 a_req->assoclen);
1144 if (unlikely(sz != authsize)) {
1145 dev_err(SEC_CTX_DEV(req->ctx), "copy out mac err!\n");
1146 err = -EINVAL;
1150 sec_free_req_id(req);
1152 if (req->fake_busy)
1153 a_req->base.complete(&a_req->base, -EINPROGRESS);
1155 a_req->base.complete(&a_req->base, err);
1158 static void sec_request_uninit(struct sec_ctx *ctx, struct sec_req *req)
1160 struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1162 atomic_dec(&qp_ctx->pending_reqs);
1163 sec_free_req_id(req);
1164 sec_free_queue_id(ctx, req);
1167 static int sec_request_init(struct sec_ctx *ctx, struct sec_req *req)
1169 struct sec_qp_ctx *qp_ctx;
1170 int queue_id;
1172 /* To load balance */
1173 queue_id = sec_alloc_queue_id(ctx, req);
1174 qp_ctx = &ctx->qp_ctx[queue_id];
1176 req->req_id = sec_alloc_req_id(req, qp_ctx);
1177 if (unlikely(req->req_id < 0)) {
1178 sec_free_queue_id(ctx, req);
1179 return req->req_id;
1182 if (ctx->fake_req_limit <= atomic_inc_return(&qp_ctx->pending_reqs))
1183 req->fake_busy = true;
1184 else
1185 req->fake_busy = false;
1187 return 0;
1190 static int sec_process(struct sec_ctx *ctx, struct sec_req *req)
1192 struct sec_cipher_req *c_req = &req->c_req;
1193 int ret;
1195 ret = sec_request_init(ctx, req);
1196 if (unlikely(ret))
1197 return ret;
1199 ret = sec_request_transfer(ctx, req);
1200 if (unlikely(ret))
1201 goto err_uninit_req;
1203 /* Output IV as decrypto */
1204 if (ctx->c_ctx.c_mode == SEC_CMODE_CBC && !req->c_req.encrypt)
1205 sec_update_iv(req, ctx->alg_type);
1207 ret = ctx->req_op->bd_send(ctx, req);
1208 if (unlikely(ret != -EBUSY && ret != -EINPROGRESS)) {
1209 dev_err_ratelimited(SEC_CTX_DEV(ctx), "send sec request failed!\n");
1210 goto err_send_req;
1213 return ret;
1215 err_send_req:
1216 /* As failing, restore the IV from user */
1217 if (ctx->c_ctx.c_mode == SEC_CMODE_CBC && !req->c_req.encrypt) {
1218 if (ctx->alg_type == SEC_SKCIPHER)
1219 memcpy(req->c_req.sk_req->iv, c_req->c_ivin,
1220 ctx->c_ctx.ivsize);
1221 else
1222 memcpy(req->aead_req.aead_req->iv, c_req->c_ivin,
1223 ctx->c_ctx.ivsize);
1226 sec_request_untransfer(ctx, req);
1227 err_uninit_req:
1228 sec_request_uninit(ctx, req);
1230 return ret;
1233 static const struct sec_req_op sec_skcipher_req_ops = {
1234 .buf_map = sec_skcipher_sgl_map,
1235 .buf_unmap = sec_skcipher_sgl_unmap,
1236 .do_transfer = sec_skcipher_copy_iv,
1237 .bd_fill = sec_skcipher_bd_fill,
1238 .bd_send = sec_bd_send,
1239 .callback = sec_skcipher_callback,
1240 .process = sec_process,
1243 static const struct sec_req_op sec_aead_req_ops = {
1244 .buf_map = sec_aead_sgl_map,
1245 .buf_unmap = sec_aead_sgl_unmap,
1246 .do_transfer = sec_aead_copy_iv,
1247 .bd_fill = sec_aead_bd_fill,
1248 .bd_send = sec_bd_send,
1249 .callback = sec_aead_callback,
1250 .process = sec_process,
1253 static int sec_skcipher_ctx_init(struct crypto_skcipher *tfm)
1255 struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
1257 ctx->req_op = &sec_skcipher_req_ops;
1259 return sec_skcipher_init(tfm);
1262 static void sec_skcipher_ctx_exit(struct crypto_skcipher *tfm)
1264 sec_skcipher_uninit(tfm);
1267 static int sec_aead_init(struct crypto_aead *tfm)
1269 struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1270 int ret;
1272 crypto_aead_set_reqsize(tfm, sizeof(struct sec_req));
1273 ctx->alg_type = SEC_AEAD;
1274 ctx->c_ctx.ivsize = crypto_aead_ivsize(tfm);
1275 if (ctx->c_ctx.ivsize > SEC_IV_SIZE) {
1276 dev_err(SEC_CTX_DEV(ctx), "get error aead iv size!\n");
1277 return -EINVAL;
1280 ctx->req_op = &sec_aead_req_ops;
1281 ret = sec_ctx_base_init(ctx);
1282 if (ret)
1283 return ret;
1285 ret = sec_auth_init(ctx);
1286 if (ret)
1287 goto err_auth_init;
1289 ret = sec_cipher_init(ctx);
1290 if (ret)
1291 goto err_cipher_init;
1293 return ret;
1295 err_cipher_init:
1296 sec_auth_uninit(ctx);
1297 err_auth_init:
1298 sec_ctx_base_uninit(ctx);
1300 return ret;
1303 static void sec_aead_exit(struct crypto_aead *tfm)
1305 struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1307 sec_cipher_uninit(ctx);
1308 sec_auth_uninit(ctx);
1309 sec_ctx_base_uninit(ctx);
1312 static int sec_aead_ctx_init(struct crypto_aead *tfm, const char *hash_name)
1314 struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1315 struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1316 int ret;
1318 ret = sec_aead_init(tfm);
1319 if (ret) {
1320 pr_err("hisi_sec2: aead init error!\n");
1321 return ret;
1324 auth_ctx->hash_tfm = crypto_alloc_shash(hash_name, 0, 0);
1325 if (IS_ERR(auth_ctx->hash_tfm)) {
1326 dev_err(SEC_CTX_DEV(ctx), "aead alloc shash error!\n");
1327 sec_aead_exit(tfm);
1328 return PTR_ERR(auth_ctx->hash_tfm);
1331 return 0;
1334 static void sec_aead_ctx_exit(struct crypto_aead *tfm)
1336 struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1338 crypto_free_shash(ctx->a_ctx.hash_tfm);
1339 sec_aead_exit(tfm);
1342 static int sec_aead_sha1_ctx_init(struct crypto_aead *tfm)
1344 return sec_aead_ctx_init(tfm, "sha1");
1347 static int sec_aead_sha256_ctx_init(struct crypto_aead *tfm)
1349 return sec_aead_ctx_init(tfm, "sha256");
1352 static int sec_aead_sha512_ctx_init(struct crypto_aead *tfm)
1354 return sec_aead_ctx_init(tfm, "sha512");
1357 static int sec_skcipher_param_check(struct sec_ctx *ctx, struct sec_req *sreq)
1359 struct skcipher_request *sk_req = sreq->c_req.sk_req;
1360 struct device *dev = SEC_CTX_DEV(ctx);
1361 u8 c_alg = ctx->c_ctx.c_alg;
1363 if (unlikely(!sk_req->src || !sk_req->dst)) {
1364 dev_err(dev, "skcipher input param error!\n");
1365 return -EINVAL;
1367 sreq->c_req.c_len = sk_req->cryptlen;
1369 if (ctx->pbuf_supported && sk_req->cryptlen <= SEC_PBUF_SZ)
1370 sreq->use_pbuf = true;
1371 else
1372 sreq->use_pbuf = false;
1374 if (c_alg == SEC_CALG_3DES) {
1375 if (unlikely(sk_req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) {
1376 dev_err(dev, "skcipher 3des input length error!\n");
1377 return -EINVAL;
1379 return 0;
1380 } else if (c_alg == SEC_CALG_AES || c_alg == SEC_CALG_SM4) {
1381 if (unlikely(sk_req->cryptlen & (AES_BLOCK_SIZE - 1))) {
1382 dev_err(dev, "skcipher aes input length error!\n");
1383 return -EINVAL;
1385 return 0;
1388 dev_err(dev, "skcipher algorithm error!\n");
1389 return -EINVAL;
1392 static int sec_skcipher_crypto(struct skcipher_request *sk_req, bool encrypt)
1394 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(sk_req);
1395 struct sec_req *req = skcipher_request_ctx(sk_req);
1396 struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
1397 int ret;
1399 if (!sk_req->cryptlen)
1400 return 0;
1402 req->c_req.sk_req = sk_req;
1403 req->c_req.encrypt = encrypt;
1404 req->ctx = ctx;
1406 ret = sec_skcipher_param_check(ctx, req);
1407 if (unlikely(ret))
1408 return -EINVAL;
1410 return ctx->req_op->process(ctx, req);
1413 static int sec_skcipher_encrypt(struct skcipher_request *sk_req)
1415 return sec_skcipher_crypto(sk_req, true);
1418 static int sec_skcipher_decrypt(struct skcipher_request *sk_req)
1420 return sec_skcipher_crypto(sk_req, false);
1423 #define SEC_SKCIPHER_GEN_ALG(sec_cra_name, sec_set_key, sec_min_key_size, \
1424 sec_max_key_size, ctx_init, ctx_exit, blk_size, iv_size)\
1426 .base = {\
1427 .cra_name = sec_cra_name,\
1428 .cra_driver_name = "hisi_sec_"sec_cra_name,\
1429 .cra_priority = SEC_PRIORITY,\
1430 .cra_flags = CRYPTO_ALG_ASYNC,\
1431 .cra_blocksize = blk_size,\
1432 .cra_ctxsize = sizeof(struct sec_ctx),\
1433 .cra_module = THIS_MODULE,\
1435 .init = ctx_init,\
1436 .exit = ctx_exit,\
1437 .setkey = sec_set_key,\
1438 .decrypt = sec_skcipher_decrypt,\
1439 .encrypt = sec_skcipher_encrypt,\
1440 .min_keysize = sec_min_key_size,\
1441 .max_keysize = sec_max_key_size,\
1442 .ivsize = iv_size,\
1445 #define SEC_SKCIPHER_ALG(name, key_func, min_key_size, \
1446 max_key_size, blk_size, iv_size) \
1447 SEC_SKCIPHER_GEN_ALG(name, key_func, min_key_size, max_key_size, \
1448 sec_skcipher_ctx_init, sec_skcipher_ctx_exit, blk_size, iv_size)
1450 static struct skcipher_alg sec_skciphers[] = {
1451 SEC_SKCIPHER_ALG("ecb(aes)", sec_setkey_aes_ecb,
1452 AES_MIN_KEY_SIZE, AES_MAX_KEY_SIZE,
1453 AES_BLOCK_SIZE, 0)
1455 SEC_SKCIPHER_ALG("cbc(aes)", sec_setkey_aes_cbc,
1456 AES_MIN_KEY_SIZE, AES_MAX_KEY_SIZE,
1457 AES_BLOCK_SIZE, AES_BLOCK_SIZE)
1459 SEC_SKCIPHER_ALG("xts(aes)", sec_setkey_aes_xts,
1460 SEC_XTS_MIN_KEY_SIZE, SEC_XTS_MAX_KEY_SIZE,
1461 AES_BLOCK_SIZE, AES_BLOCK_SIZE)
1463 SEC_SKCIPHER_ALG("ecb(des3_ede)", sec_setkey_3des_ecb,
1464 SEC_DES3_2KEY_SIZE, SEC_DES3_3KEY_SIZE,
1465 DES3_EDE_BLOCK_SIZE, 0)
1467 SEC_SKCIPHER_ALG("cbc(des3_ede)", sec_setkey_3des_cbc,
1468 SEC_DES3_2KEY_SIZE, SEC_DES3_3KEY_SIZE,
1469 DES3_EDE_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE)
1471 SEC_SKCIPHER_ALG("xts(sm4)", sec_setkey_sm4_xts,
1472 SEC_XTS_MIN_KEY_SIZE, SEC_XTS_MIN_KEY_SIZE,
1473 AES_BLOCK_SIZE, AES_BLOCK_SIZE)
1475 SEC_SKCIPHER_ALG("cbc(sm4)", sec_setkey_sm4_cbc,
1476 AES_MIN_KEY_SIZE, AES_MIN_KEY_SIZE,
1477 AES_BLOCK_SIZE, AES_BLOCK_SIZE)
1480 static int sec_aead_param_check(struct sec_ctx *ctx, struct sec_req *sreq)
1482 u8 c_alg = ctx->c_ctx.c_alg;
1483 struct aead_request *req = sreq->aead_req.aead_req;
1484 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1485 size_t authsize = crypto_aead_authsize(tfm);
1487 if (unlikely(!req->src || !req->dst || !req->cryptlen ||
1488 req->assoclen > SEC_MAX_AAD_LEN)) {
1489 dev_err(SEC_CTX_DEV(ctx), "aead input param error!\n");
1490 return -EINVAL;
1493 if (ctx->pbuf_supported && (req->cryptlen + req->assoclen) <=
1494 SEC_PBUF_SZ)
1495 sreq->use_pbuf = true;
1496 else
1497 sreq->use_pbuf = false;
1499 /* Support AES only */
1500 if (unlikely(c_alg != SEC_CALG_AES)) {
1501 dev_err(SEC_CTX_DEV(ctx), "aead crypto alg error!\n");
1502 return -EINVAL;
1505 if (sreq->c_req.encrypt)
1506 sreq->c_req.c_len = req->cryptlen;
1507 else
1508 sreq->c_req.c_len = req->cryptlen - authsize;
1510 if (unlikely(sreq->c_req.c_len & (AES_BLOCK_SIZE - 1))) {
1511 dev_err(SEC_CTX_DEV(ctx), "aead crypto length error!\n");
1512 return -EINVAL;
1515 return 0;
1518 static int sec_aead_crypto(struct aead_request *a_req, bool encrypt)
1520 struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
1521 struct sec_req *req = aead_request_ctx(a_req);
1522 struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1523 int ret;
1525 req->aead_req.aead_req = a_req;
1526 req->c_req.encrypt = encrypt;
1527 req->ctx = ctx;
1529 ret = sec_aead_param_check(ctx, req);
1530 if (unlikely(ret))
1531 return -EINVAL;
1533 return ctx->req_op->process(ctx, req);
1536 static int sec_aead_encrypt(struct aead_request *a_req)
1538 return sec_aead_crypto(a_req, true);
1541 static int sec_aead_decrypt(struct aead_request *a_req)
1543 return sec_aead_crypto(a_req, false);
1546 #define SEC_AEAD_GEN_ALG(sec_cra_name, sec_set_key, ctx_init,\
1547 ctx_exit, blk_size, iv_size, max_authsize)\
1549 .base = {\
1550 .cra_name = sec_cra_name,\
1551 .cra_driver_name = "hisi_sec_"sec_cra_name,\
1552 .cra_priority = SEC_PRIORITY,\
1553 .cra_flags = CRYPTO_ALG_ASYNC,\
1554 .cra_blocksize = blk_size,\
1555 .cra_ctxsize = sizeof(struct sec_ctx),\
1556 .cra_module = THIS_MODULE,\
1558 .init = ctx_init,\
1559 .exit = ctx_exit,\
1560 .setkey = sec_set_key,\
1561 .decrypt = sec_aead_decrypt,\
1562 .encrypt = sec_aead_encrypt,\
1563 .ivsize = iv_size,\
1564 .maxauthsize = max_authsize,\
1567 #define SEC_AEAD_ALG(algname, keyfunc, aead_init, blksize, ivsize, authsize)\
1568 SEC_AEAD_GEN_ALG(algname, keyfunc, aead_init,\
1569 sec_aead_ctx_exit, blksize, ivsize, authsize)
1571 static struct aead_alg sec_aeads[] = {
1572 SEC_AEAD_ALG("authenc(hmac(sha1),cbc(aes))",
1573 sec_setkey_aes_cbc_sha1, sec_aead_sha1_ctx_init,
1574 AES_BLOCK_SIZE, AES_BLOCK_SIZE, SHA1_DIGEST_SIZE),
1576 SEC_AEAD_ALG("authenc(hmac(sha256),cbc(aes))",
1577 sec_setkey_aes_cbc_sha256, sec_aead_sha256_ctx_init,
1578 AES_BLOCK_SIZE, AES_BLOCK_SIZE, SHA256_DIGEST_SIZE),
1580 SEC_AEAD_ALG("authenc(hmac(sha512),cbc(aes))",
1581 sec_setkey_aes_cbc_sha512, sec_aead_sha512_ctx_init,
1582 AES_BLOCK_SIZE, AES_BLOCK_SIZE, SHA512_DIGEST_SIZE),
1585 int sec_register_to_crypto(void)
1587 int ret = 0;
1589 /* To avoid repeat register */
1590 if (atomic_add_return(1, &sec_active_devs) == 1) {
1591 ret = crypto_register_skciphers(sec_skciphers,
1592 ARRAY_SIZE(sec_skciphers));
1593 if (ret)
1594 return ret;
1596 ret = crypto_register_aeads(sec_aeads, ARRAY_SIZE(sec_aeads));
1597 if (ret)
1598 goto reg_aead_fail;
1601 return ret;
1603 reg_aead_fail:
1604 crypto_unregister_skciphers(sec_skciphers, ARRAY_SIZE(sec_skciphers));
1606 return ret;
1609 void sec_unregister_from_crypto(void)
1611 if (atomic_sub_return(1, &sec_active_devs) == 0) {
1612 crypto_unregister_skciphers(sec_skciphers,
1613 ARRAY_SIZE(sec_skciphers));
1614 crypto_unregister_aeads(sec_aeads, ARRAY_SIZE(sec_aeads));