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>
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 + \
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 + \
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
) %
78 return (u32
)atomic_inc_return(&ctx
->dec_qcyclic
) % 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
);
87 atomic_dec(&ctx
->dec_qcyclic
);
90 static int sec_alloc_req_id(struct sec_req
*req
, struct sec_qp_ctx
*qp_ctx
)
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");
104 req
->qp_ctx
= qp_ctx
;
105 qp_ctx
->req_list
[req_id
] = req
;
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");
119 qp_ctx
->req_list
[req_id
] = 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
;
137 sz
= sg_pcopy_to_buffer(sgl
, sg_nents(sgl
), mac
, authsize
,
138 aead_req
->cryptlen
+ aead_req
->assoclen
-
140 if (unlikely(sz
!= authsize
|| memcmp(mac_out
, mac
, sz
))) {
141 dev_err(SEC_CTX_DEV(req
->ctx
), "aead verify failure!\n");
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
;
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
);
164 req
= qp_ctx
->req_list
[le16_to_cpu(bd
->type2
.tag
)];
165 req
->err_type
= bd
->type2
.error_type
;
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
);
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
;
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
))
212 /* Get DMA memory resources */
213 static int sec_alloc_civ_resource(struct device
*dev
, struct sec_alg_res
*res
)
217 res
->c_ivin
= dma_alloc_coherent(dev
, SEC_TOTAL_IV_SZ
,
218 &res
->c_ivin_dma
, GFP_KERNEL
);
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
;
230 static void sec_free_civ_resource(struct device
*dev
, struct sec_alg_res
*res
)
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
)
241 res
->out_mac
= dma_alloc_coherent(dev
, SEC_TOTAL_MAC_SZ
<< 1,
242 &res
->out_mac_dma
, GFP_KERNEL
);
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);
255 static void sec_free_mac_resource(struct device
*dev
, struct sec_alg_res
*res
)
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
)
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
;
278 res
->pbuf
= dma_alloc_coherent(dev
, SEC_TOTAL_PBUF_SZ
,
279 &res
->pbuf_dma
, GFP_KERNEL
);
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
;
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
;
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
;
313 ret
= sec_alloc_civ_resource(dev
, res
);
317 if (ctx
->alg_type
== SEC_AEAD
) {
318 ret
= sec_alloc_mac_resource(dev
, res
);
322 if (ctx
->pbuf_supported
) {
323 ret
= sec_alloc_pbuf_resource(dev
, res
);
325 dev_err(dev
, "fail to alloc pbuf dma resource!\n");
332 sec_free_civ_resource(dev
, res
);
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
;
358 qp_ctx
= &ctx
->qp_ctx
[qp_ctx_id
];
359 qp
= ctx
->qps
[qp_ctx_id
];
362 qp
->req_cb
= sec_req_cb
;
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
,
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
,
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
);
386 goto err_free_c_out_pool
;
388 ret
= hisi_qm_start_qp(qp
, 0);
395 sec_alg_resource_free(ctx
, qp_ctx
);
397 hisi_acc_free_sgl_pool(dev
, qp_ctx
->c_out_pool
);
399 hisi_acc_free_sgl_pool(dev
, qp_ctx
->c_in_pool
);
401 idr_destroy(&qp_ctx
->req_idr
);
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
)
425 ctx
->qps
= sec_create_qps();
427 pr_err("Can not create sec qps!\n");
431 sec
= container_of(ctx
->qps
[0]->qm
, struct sec_dev
, qm
);
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
),
444 for (i
= 0; i
< sec
->ctx_q_num
; i
++) {
445 ret
= sec_create_qp_ctx(&sec
->qm
, ctx
, i
, 0);
447 goto err_sec_release_qp_ctx
;
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
);
460 static void sec_ctx_base_uninit(struct sec_ctx
*ctx
)
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
);
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
);
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
);
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
);
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");
526 ret
= sec_ctx_base_init(ctx
);
530 ret
= sec_cipher_init(ctx
);
532 goto err_cipher_init
;
536 sec_ctx_base_uninit(ctx
);
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
,
551 const enum sec_cmode c_mode
)
554 case SEC_DES3_2KEY_SIZE
:
555 c_ctx
->c_key_len
= SEC_CKEY_3DES_2KEY
;
557 case SEC_DES3_3KEY_SIZE
:
558 c_ctx
->c_key_len
= SEC_CKEY_3DES_3KEY
;
567 static int sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx
*c_ctx
,
569 const enum sec_cmode c_mode
)
571 if (c_mode
== SEC_CMODE_XTS
) {
573 case SEC_XTS_MIN_KEY_SIZE
:
574 c_ctx
->c_key_len
= SEC_CKEY_128BIT
;
576 case SEC_XTS_MAX_KEY_SIZE
:
577 c_ctx
->c_key_len
= SEC_CKEY_256BIT
;
580 pr_err("hisi_sec2: xts mode key error!\n");
585 case AES_KEYSIZE_128
:
586 c_ctx
->c_key_len
= SEC_CKEY_128BIT
;
588 case AES_KEYSIZE_192
:
589 c_ctx
->c_key_len
= SEC_CKEY_192BIT
;
591 case AES_KEYSIZE_256
:
592 c_ctx
->c_key_len
= SEC_CKEY_256BIT
;
595 pr_err("hisi_sec2: aes key error!\n");
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
;
611 if (c_mode
== SEC_CMODE_XTS
) {
612 ret
= xts_verify_key(tfm
, key
, keylen
);
614 dev_err(SEC_CTX_DEV(ctx
), "xts mode key err!\n");
619 c_ctx
->c_alg
= c_alg
;
620 c_ctx
->c_mode
= c_mode
;
624 ret
= sec_skcipher_3des_setkey(c_ctx
, keylen
, c_mode
);
628 ret
= sec_skcipher_aes_sm4_setkey(c_ctx
, keylen
, c_mode
);
635 dev_err(SEC_CTX_DEV(ctx
), "set sec key err!\n");
639 memcpy(c_ctx
->c_key
, key
, keylen
);
644 #define GEN_SEC_SETKEY_FUNC(name, c_alg, c_mode) \
645 static int sec_setkey_##name(struct crypto_skcipher *tfm, const u8 *key,\
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
;
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
,
680 if (unlikely(pbuf_length
!= copy_size
)) {
681 dev_err(dev
, "copy src data to pbuf error!\n");
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");
692 c_req
->c_out_dma
= c_req
->c_in_dma
;
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
;
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
,
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
);
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
+
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
,
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
);
761 c_req
->c_out
= c_req
->c_in
;
762 c_req
->c_out_dma
= c_req
->c_in_dma
;
764 c_req
->c_out
= hisi_acc_sg_buf_map_to_hw_sgl(dev
, dst
,
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
);
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
);
786 sec_cipher_pbuf_unmap(ctx
, req
, dst
);
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
;
816 case AES_KEYSIZE_192
:
817 c_ctx
->c_key_len
= SEC_CKEY_192BIT
;
819 case AES_KEYSIZE_256
:
820 c_ctx
->c_key_len
= SEC_CKEY_256BIT
;
823 pr_err("hisi_sec2: aead aes key error!\n");
826 memcpy(c_ctx
->c_key
, keys
->enckey
, keys
->enckeylen
);
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
);
838 if (!keys
->authkeylen
) {
839 pr_err("hisi_sec2: aead auth key error!\n");
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
);
848 pr_err("hisi_sec2: aead auth digest error!\n");
851 ctx
->a_key_len
= blocksize
;
853 memcpy(ctx
->a_key
, keys
->authkey
, keys
->authkeylen
);
854 ctx
->a_key_len
= keys
->authkeylen
;
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
;
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
))
879 ret
= sec_aead_aes_set_key(c_ctx
, &keys
);
881 dev_err(SEC_CTX_DEV(ctx
), "set sec cipher key err!\n");
885 ret
= sec_aead_auth_set_key(&ctx
->a_ctx
, &keys
);
887 dev_err(SEC_CTX_DEV(ctx
), "set sec auth key err!\n");
893 memzero_explicit(&keys
, sizeof(struct crypto_authenc_keys
));
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, \
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
)
931 ret
= ctx
->req_op
->buf_map(ctx
, req
);
935 ctx
->req_op
->do_transfer(ctx
, req
);
937 ret
= ctx
->req_op
->bd_fill(ctx
, req
);
944 ctx
->req_op
->buf_unmap(ctx
, req
);
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
;
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
) <<
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
) <<
984 bd_type
= SEC_BD_TYPE2
;
986 cipher
= SEC_CIPHER_ENC
<< SEC_CIPHER_OFFSET
;
988 cipher
= SEC_CIPHER_DEC
<< SEC_CIPHER_OFFSET
;
989 sec_sqe
->type_cipher_auth
= bd_type
| cipher
;
992 sa_type
= SEC_PBUF
<< SEC_SRC_SGL_OFFSET
;
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 */
1003 da_type
= SEC_PBUF
<< SEC_DST_SGL_OFFSET
;
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
);
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
;
1024 if (req
->c_req
.encrypt
)
1025 sgl
= alg_type
== SEC_SKCIPHER
? sk_req
->dst
: aead_req
->dst
;
1027 sgl
= alg_type
== SEC_SKCIPHER
? sk_req
->src
: aead_req
->src
;
1029 if (alg_type
== SEC_SKCIPHER
) {
1031 cryptlen
= sk_req
->cryptlen
;
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
,
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
);
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
;
1092 sec_sqe
->sds_sa_type
&= SEC_CIPHER_AUTH
;
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
;
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");
1115 sec_auth_bd_fill_ex(auth_ctx
, req
->c_req
.encrypt
, req
, sec_sqe
);
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
;
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
),
1141 authsize
, a_req
->cryptlen
+
1144 if (unlikely(sz
!= authsize
)) {
1145 dev_err(SEC_CTX_DEV(req
->ctx
), "copy out mac err!\n");
1150 sec_free_req_id(req
);
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
;
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
);
1182 if (ctx
->fake_req_limit
<= atomic_inc_return(&qp_ctx
->pending_reqs
))
1183 req
->fake_busy
= true;
1185 req
->fake_busy
= false;
1190 static int sec_process(struct sec_ctx
*ctx
, struct sec_req
*req
)
1192 struct sec_cipher_req
*c_req
= &req
->c_req
;
1195 ret
= sec_request_init(ctx
, req
);
1199 ret
= sec_request_transfer(ctx
, req
);
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");
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
,
1222 memcpy(req
->aead_req
.aead_req
->iv
, c_req
->c_ivin
,
1226 sec_request_untransfer(ctx
, req
);
1228 sec_request_uninit(ctx
, req
);
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
);
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");
1280 ctx
->req_op
= &sec_aead_req_ops
;
1281 ret
= sec_ctx_base_init(ctx
);
1285 ret
= sec_auth_init(ctx
);
1289 ret
= sec_cipher_init(ctx
);
1291 goto err_cipher_init
;
1296 sec_auth_uninit(ctx
);
1298 sec_ctx_base_uninit(ctx
);
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
;
1318 ret
= sec_aead_init(tfm
);
1320 pr_err("hisi_sec2: aead init error!\n");
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");
1328 return PTR_ERR(auth_ctx
->hash_tfm
);
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
);
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");
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;
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");
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");
1388 dev_err(dev
, "skcipher algorithm error!\n");
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
);
1399 if (!sk_req
->cryptlen
)
1402 req
->c_req
.sk_req
= sk_req
;
1403 req
->c_req
.encrypt
= encrypt
;
1406 ret
= sec_skcipher_param_check(ctx
, req
);
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)\
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,\
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,\
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
,
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");
1493 if (ctx
->pbuf_supported
&& (req
->cryptlen
+ req
->assoclen
) <=
1495 sreq
->use_pbuf
= true;
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");
1505 if (sreq
->c_req
.encrypt
)
1506 sreq
->c_req
.c_len
= req
->cryptlen
;
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");
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
);
1525 req
->aead_req
.aead_req
= a_req
;
1526 req
->c_req
.encrypt
= encrypt
;
1529 ret
= sec_aead_param_check(ctx
, req
);
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)\
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,\
1560 .setkey = sec_set_key,\
1561 .decrypt = sec_aead_decrypt,\
1562 .encrypt = sec_aead_encrypt,\
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)
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
));
1596 ret
= crypto_register_aeads(sec_aeads
, ARRAY_SIZE(sec_aeads
));
1604 crypto_unregister_skciphers(sec_skciphers
, ARRAY_SIZE(sec_skciphers
));
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
));