xref: /openbmc/linux/drivers/crypto/hisilicon/sec2/sec_crypto.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 
4 #include <crypto/aes.h>
5 #include <crypto/aead.h>
6 #include <crypto/algapi.h>
7 #include <crypto/authenc.h>
8 #include <crypto/des.h>
9 #include <crypto/hash.h>
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/des.h>
12 #include <crypto/sha1.h>
13 #include <crypto/sha2.h>
14 #include <crypto/skcipher.h>
15 #include <crypto/xts.h>
16 #include <linux/crypto.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/idr.h>
19 
20 #include "sec.h"
21 #include "sec_crypto.h"
22 
23 #define SEC_PRIORITY		4001
24 #define SEC_XTS_MIN_KEY_SIZE	(2 * AES_MIN_KEY_SIZE)
25 #define SEC_XTS_MID_KEY_SIZE	(3 * AES_MIN_KEY_SIZE)
26 #define SEC_XTS_MAX_KEY_SIZE	(2 * AES_MAX_KEY_SIZE)
27 #define SEC_DES3_2KEY_SIZE	(2 * DES_KEY_SIZE)
28 #define SEC_DES3_3KEY_SIZE	(3 * DES_KEY_SIZE)
29 
30 /* SEC sqe(bd) bit operational relative MACRO */
31 #define SEC_DE_OFFSET		1
32 #define SEC_CIPHER_OFFSET	4
33 #define SEC_SCENE_OFFSET	3
34 #define SEC_DST_SGL_OFFSET	2
35 #define SEC_SRC_SGL_OFFSET	7
36 #define SEC_CKEY_OFFSET		9
37 #define SEC_CMODE_OFFSET	12
38 #define SEC_AKEY_OFFSET         5
39 #define SEC_AEAD_ALG_OFFSET     11
40 #define SEC_AUTH_OFFSET		6
41 
42 #define SEC_DE_OFFSET_V3		9
43 #define SEC_SCENE_OFFSET_V3	5
44 #define SEC_CKEY_OFFSET_V3	13
45 #define SEC_CTR_CNT_OFFSET	25
46 #define SEC_CTR_CNT_ROLLOVER	2
47 #define SEC_SRC_SGL_OFFSET_V3	11
48 #define SEC_DST_SGL_OFFSET_V3	14
49 #define SEC_CALG_OFFSET_V3	4
50 #define SEC_AKEY_OFFSET_V3	9
51 #define SEC_MAC_OFFSET_V3	4
52 #define SEC_AUTH_ALG_OFFSET_V3	15
53 #define SEC_CIPHER_AUTH_V3	0xbf
54 #define SEC_AUTH_CIPHER_V3	0x40
55 #define SEC_FLAG_OFFSET		7
56 #define SEC_FLAG_MASK		0x0780
57 #define SEC_TYPE_MASK		0x0F
58 #define SEC_DONE_MASK		0x0001
59 #define SEC_ICV_MASK		0x000E
60 #define SEC_SQE_LEN_RATE_MASK	0x3
61 
62 #define SEC_TOTAL_IV_SZ(depth)	(SEC_IV_SIZE * (depth))
63 #define SEC_SGL_SGE_NR		128
64 #define SEC_CIPHER_AUTH		0xfe
65 #define SEC_AUTH_CIPHER		0x1
66 #define SEC_MAX_MAC_LEN		64
67 #define SEC_MAX_AAD_LEN		65535
68 #define SEC_MAX_CCM_AAD_LEN	65279
69 #define SEC_TOTAL_MAC_SZ(depth) (SEC_MAX_MAC_LEN * (depth))
70 
71 #define SEC_PBUF_SZ			512
72 #define SEC_PBUF_IV_OFFSET		SEC_PBUF_SZ
73 #define SEC_PBUF_MAC_OFFSET		(SEC_PBUF_SZ + SEC_IV_SIZE)
74 #define SEC_PBUF_PKG		(SEC_PBUF_SZ + SEC_IV_SIZE +	\
75 			SEC_MAX_MAC_LEN * 2)
76 #define SEC_PBUF_NUM		(PAGE_SIZE / SEC_PBUF_PKG)
77 #define SEC_PBUF_PAGE_NUM(depth)	((depth) / SEC_PBUF_NUM)
78 #define SEC_PBUF_LEFT_SZ(depth)		(SEC_PBUF_PKG * ((depth) -	\
79 				SEC_PBUF_PAGE_NUM(depth) * SEC_PBUF_NUM))
80 #define SEC_TOTAL_PBUF_SZ(depth)	(PAGE_SIZE * SEC_PBUF_PAGE_NUM(depth) +	\
81 				SEC_PBUF_LEFT_SZ(depth))
82 
83 #define SEC_SQE_LEN_RATE	4
84 #define SEC_SQE_CFLAG		2
85 #define SEC_SQE_AEAD_FLAG	3
86 #define SEC_SQE_DONE		0x1
87 #define SEC_ICV_ERR		0x2
88 #define MIN_MAC_LEN		4
89 #define MAC_LEN_MASK		0x1U
90 #define MAX_INPUT_DATA_LEN	0xFFFE00
91 #define BITS_MASK		0xFF
92 #define BYTE_BITS		0x8
93 #define SEC_XTS_NAME_SZ		0x3
94 #define IV_CM_CAL_NUM		2
95 #define IV_CL_MASK		0x7
96 #define IV_CL_MIN		2
97 #define IV_CL_MID		4
98 #define IV_CL_MAX		8
99 #define IV_FLAGS_OFFSET	0x6
100 #define IV_CM_OFFSET		0x3
101 #define IV_LAST_BYTE1		1
102 #define IV_LAST_BYTE2		2
103 #define IV_LAST_BYTE_MASK	0xFF
104 #define IV_CTR_INIT		0x1
105 #define IV_BYTE_OFFSET		0x8
106 
107 struct sec_skcipher {
108 	u64 alg_msk;
109 	struct skcipher_alg alg;
110 };
111 
112 struct sec_aead {
113 	u64 alg_msk;
114 	struct aead_alg alg;
115 };
116 
117 /* Get an en/de-cipher queue cyclically to balance load over queues of TFM */
sec_alloc_queue_id(struct sec_ctx * ctx,struct sec_req * req)118 static inline int sec_alloc_queue_id(struct sec_ctx *ctx, struct sec_req *req)
119 {
120 	if (req->c_req.encrypt)
121 		return (u32)atomic_inc_return(&ctx->enc_qcyclic) %
122 				 ctx->hlf_q_num;
123 
124 	return (u32)atomic_inc_return(&ctx->dec_qcyclic) % ctx->hlf_q_num +
125 				 ctx->hlf_q_num;
126 }
127 
sec_free_queue_id(struct sec_ctx * ctx,struct sec_req * req)128 static inline void sec_free_queue_id(struct sec_ctx *ctx, struct sec_req *req)
129 {
130 	if (req->c_req.encrypt)
131 		atomic_dec(&ctx->enc_qcyclic);
132 	else
133 		atomic_dec(&ctx->dec_qcyclic);
134 }
135 
sec_alloc_req_id(struct sec_req * req,struct sec_qp_ctx * qp_ctx)136 static int sec_alloc_req_id(struct sec_req *req, struct sec_qp_ctx *qp_ctx)
137 {
138 	int req_id;
139 
140 	spin_lock_bh(&qp_ctx->req_lock);
141 	req_id = idr_alloc_cyclic(&qp_ctx->req_idr, NULL, 0, qp_ctx->qp->sq_depth, GFP_ATOMIC);
142 	spin_unlock_bh(&qp_ctx->req_lock);
143 	if (unlikely(req_id < 0)) {
144 		dev_err(req->ctx->dev, "alloc req id fail!\n");
145 		return req_id;
146 	}
147 
148 	req->qp_ctx = qp_ctx;
149 	qp_ctx->req_list[req_id] = req;
150 
151 	return req_id;
152 }
153 
sec_free_req_id(struct sec_req * req)154 static void sec_free_req_id(struct sec_req *req)
155 {
156 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
157 	int req_id = req->req_id;
158 
159 	if (unlikely(req_id < 0 || req_id >= qp_ctx->qp->sq_depth)) {
160 		dev_err(req->ctx->dev, "free request id invalid!\n");
161 		return;
162 	}
163 
164 	qp_ctx->req_list[req_id] = NULL;
165 	req->qp_ctx = NULL;
166 
167 	spin_lock_bh(&qp_ctx->req_lock);
168 	idr_remove(&qp_ctx->req_idr, req_id);
169 	spin_unlock_bh(&qp_ctx->req_lock);
170 }
171 
pre_parse_finished_bd(struct bd_status * status,void * resp)172 static u8 pre_parse_finished_bd(struct bd_status *status, void *resp)
173 {
174 	struct sec_sqe *bd = resp;
175 
176 	status->done = le16_to_cpu(bd->type2.done_flag) & SEC_DONE_MASK;
177 	status->icv = (le16_to_cpu(bd->type2.done_flag) & SEC_ICV_MASK) >> 1;
178 	status->flag = (le16_to_cpu(bd->type2.done_flag) &
179 					SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
180 	status->tag = le16_to_cpu(bd->type2.tag);
181 	status->err_type = bd->type2.error_type;
182 
183 	return bd->type_cipher_auth & SEC_TYPE_MASK;
184 }
185 
pre_parse_finished_bd3(struct bd_status * status,void * resp)186 static u8 pre_parse_finished_bd3(struct bd_status *status, void *resp)
187 {
188 	struct sec_sqe3 *bd3 = resp;
189 
190 	status->done = le16_to_cpu(bd3->done_flag) & SEC_DONE_MASK;
191 	status->icv = (le16_to_cpu(bd3->done_flag) & SEC_ICV_MASK) >> 1;
192 	status->flag = (le16_to_cpu(bd3->done_flag) &
193 					SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
194 	status->tag = le64_to_cpu(bd3->tag);
195 	status->err_type = bd3->error_type;
196 
197 	return le32_to_cpu(bd3->bd_param) & SEC_TYPE_MASK;
198 }
199 
sec_cb_status_check(struct sec_req * req,struct bd_status * status)200 static int sec_cb_status_check(struct sec_req *req,
201 			       struct bd_status *status)
202 {
203 	struct sec_ctx *ctx = req->ctx;
204 
205 	if (unlikely(req->err_type || status->done != SEC_SQE_DONE)) {
206 		dev_err_ratelimited(ctx->dev, "err_type[%d], done[%u]\n",
207 				    req->err_type, status->done);
208 		return -EIO;
209 	}
210 
211 	if (unlikely(ctx->alg_type == SEC_SKCIPHER)) {
212 		if (unlikely(status->flag != SEC_SQE_CFLAG)) {
213 			dev_err_ratelimited(ctx->dev, "flag[%u]\n",
214 					    status->flag);
215 			return -EIO;
216 		}
217 	} else if (unlikely(ctx->alg_type == SEC_AEAD)) {
218 		if (unlikely(status->flag != SEC_SQE_AEAD_FLAG ||
219 			     status->icv == SEC_ICV_ERR)) {
220 			dev_err_ratelimited(ctx->dev,
221 					    "flag[%u], icv[%u]\n",
222 					    status->flag, status->icv);
223 			return -EBADMSG;
224 		}
225 	}
226 
227 	return 0;
228 }
229 
sec_req_cb(struct hisi_qp * qp,void * resp)230 static void sec_req_cb(struct hisi_qp *qp, void *resp)
231 {
232 	struct sec_qp_ctx *qp_ctx = qp->qp_ctx;
233 	struct sec_dfx *dfx = &qp_ctx->ctx->sec->debug.dfx;
234 	u8 type_supported = qp_ctx->ctx->type_supported;
235 	struct bd_status status;
236 	struct sec_ctx *ctx;
237 	struct sec_req *req;
238 	int err;
239 	u8 type;
240 
241 	if (type_supported == SEC_BD_TYPE2) {
242 		type = pre_parse_finished_bd(&status, resp);
243 		req = qp_ctx->req_list[status.tag];
244 	} else {
245 		type = pre_parse_finished_bd3(&status, resp);
246 		req = (void *)(uintptr_t)status.tag;
247 	}
248 
249 	if (unlikely(type != type_supported)) {
250 		atomic64_inc(&dfx->err_bd_cnt);
251 		pr_err("err bd type [%u]\n", type);
252 		return;
253 	}
254 
255 	if (unlikely(!req)) {
256 		atomic64_inc(&dfx->invalid_req_cnt);
257 		atomic_inc(&qp->qp_status.used);
258 		return;
259 	}
260 
261 	req->err_type = status.err_type;
262 	ctx = req->ctx;
263 	err = sec_cb_status_check(req, &status);
264 	if (err)
265 		atomic64_inc(&dfx->done_flag_cnt);
266 
267 	atomic64_inc(&dfx->recv_cnt);
268 
269 	ctx->req_op->buf_unmap(ctx, req);
270 
271 	ctx->req_op->callback(ctx, req, err);
272 }
273 
sec_bd_send(struct sec_ctx * ctx,struct sec_req * req)274 static int sec_bd_send(struct sec_ctx *ctx, struct sec_req *req)
275 {
276 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
277 	int ret;
278 
279 	if (ctx->fake_req_limit <=
280 	    atomic_read(&qp_ctx->qp->qp_status.used) &&
281 	    !(req->flag & CRYPTO_TFM_REQ_MAY_BACKLOG))
282 		return -EBUSY;
283 
284 	spin_lock_bh(&qp_ctx->req_lock);
285 	ret = hisi_qp_send(qp_ctx->qp, &req->sec_sqe);
286 	if (ctx->fake_req_limit <=
287 	    atomic_read(&qp_ctx->qp->qp_status.used) && !ret) {
288 		list_add_tail(&req->backlog_head, &qp_ctx->backlog);
289 		atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
290 		atomic64_inc(&ctx->sec->debug.dfx.send_busy_cnt);
291 		spin_unlock_bh(&qp_ctx->req_lock);
292 		return -EBUSY;
293 	}
294 	spin_unlock_bh(&qp_ctx->req_lock);
295 
296 	if (unlikely(ret == -EBUSY))
297 		return -ENOBUFS;
298 
299 	if (likely(!ret)) {
300 		ret = -EINPROGRESS;
301 		atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
302 	}
303 
304 	return ret;
305 }
306 
307 /* Get DMA memory resources */
sec_alloc_civ_resource(struct device * dev,struct sec_alg_res * res)308 static int sec_alloc_civ_resource(struct device *dev, struct sec_alg_res *res)
309 {
310 	u16 q_depth = res->depth;
311 	int i;
312 
313 	res->c_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ(q_depth),
314 					 &res->c_ivin_dma, GFP_KERNEL);
315 	if (!res->c_ivin)
316 		return -ENOMEM;
317 
318 	for (i = 1; i < q_depth; i++) {
319 		res[i].c_ivin_dma = res->c_ivin_dma + i * SEC_IV_SIZE;
320 		res[i].c_ivin = res->c_ivin + i * SEC_IV_SIZE;
321 	}
322 
323 	return 0;
324 }
325 
sec_free_civ_resource(struct device * dev,struct sec_alg_res * res)326 static void sec_free_civ_resource(struct device *dev, struct sec_alg_res *res)
327 {
328 	if (res->c_ivin)
329 		dma_free_coherent(dev, SEC_TOTAL_IV_SZ(res->depth),
330 				  res->c_ivin, res->c_ivin_dma);
331 }
332 
sec_alloc_aiv_resource(struct device * dev,struct sec_alg_res * res)333 static int sec_alloc_aiv_resource(struct device *dev, struct sec_alg_res *res)
334 {
335 	u16 q_depth = res->depth;
336 	int i;
337 
338 	res->a_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ(q_depth),
339 					 &res->a_ivin_dma, GFP_KERNEL);
340 	if (!res->a_ivin)
341 		return -ENOMEM;
342 
343 	for (i = 1; i < q_depth; i++) {
344 		res[i].a_ivin_dma = res->a_ivin_dma + i * SEC_IV_SIZE;
345 		res[i].a_ivin = res->a_ivin + i * SEC_IV_SIZE;
346 	}
347 
348 	return 0;
349 }
350 
sec_free_aiv_resource(struct device * dev,struct sec_alg_res * res)351 static void sec_free_aiv_resource(struct device *dev, struct sec_alg_res *res)
352 {
353 	if (res->a_ivin)
354 		dma_free_coherent(dev, SEC_TOTAL_IV_SZ(res->depth),
355 				  res->a_ivin, res->a_ivin_dma);
356 }
357 
sec_alloc_mac_resource(struct device * dev,struct sec_alg_res * res)358 static int sec_alloc_mac_resource(struct device *dev, struct sec_alg_res *res)
359 {
360 	u16 q_depth = res->depth;
361 	int i;
362 
363 	res->out_mac = dma_alloc_coherent(dev, SEC_TOTAL_MAC_SZ(q_depth) << 1,
364 					  &res->out_mac_dma, GFP_KERNEL);
365 	if (!res->out_mac)
366 		return -ENOMEM;
367 
368 	for (i = 1; i < q_depth; i++) {
369 		res[i].out_mac_dma = res->out_mac_dma +
370 				     i * (SEC_MAX_MAC_LEN << 1);
371 		res[i].out_mac = res->out_mac + i * (SEC_MAX_MAC_LEN << 1);
372 	}
373 
374 	return 0;
375 }
376 
sec_free_mac_resource(struct device * dev,struct sec_alg_res * res)377 static void sec_free_mac_resource(struct device *dev, struct sec_alg_res *res)
378 {
379 	if (res->out_mac)
380 		dma_free_coherent(dev, SEC_TOTAL_MAC_SZ(res->depth) << 1,
381 				  res->out_mac, res->out_mac_dma);
382 }
383 
sec_free_pbuf_resource(struct device * dev,struct sec_alg_res * res)384 static void sec_free_pbuf_resource(struct device *dev, struct sec_alg_res *res)
385 {
386 	if (res->pbuf)
387 		dma_free_coherent(dev, SEC_TOTAL_PBUF_SZ(res->depth),
388 				  res->pbuf, res->pbuf_dma);
389 }
390 
391 /*
392  * To improve performance, pbuffer is used for
393  * small packets (< 512Bytes) as IOMMU translation using.
394  */
sec_alloc_pbuf_resource(struct device * dev,struct sec_alg_res * res)395 static int sec_alloc_pbuf_resource(struct device *dev, struct sec_alg_res *res)
396 {
397 	u16 q_depth = res->depth;
398 	int size = SEC_PBUF_PAGE_NUM(q_depth);
399 	int pbuf_page_offset;
400 	int i, j, k;
401 
402 	res->pbuf = dma_alloc_coherent(dev, SEC_TOTAL_PBUF_SZ(q_depth),
403 				&res->pbuf_dma, GFP_KERNEL);
404 	if (!res->pbuf)
405 		return -ENOMEM;
406 
407 	/*
408 	 * SEC_PBUF_PKG contains data pbuf, iv and
409 	 * out_mac : <SEC_PBUF|SEC_IV|SEC_MAC>
410 	 * Every PAGE contains six SEC_PBUF_PKG
411 	 * The sec_qp_ctx contains QM_Q_DEPTH numbers of SEC_PBUF_PKG
412 	 * So we need SEC_PBUF_PAGE_NUM numbers of PAGE
413 	 * for the SEC_TOTAL_PBUF_SZ
414 	 */
415 	for (i = 0; i <= size; i++) {
416 		pbuf_page_offset = PAGE_SIZE * i;
417 		for (j = 0; j < SEC_PBUF_NUM; j++) {
418 			k = i * SEC_PBUF_NUM + j;
419 			if (k == q_depth)
420 				break;
421 			res[k].pbuf = res->pbuf +
422 				j * SEC_PBUF_PKG + pbuf_page_offset;
423 			res[k].pbuf_dma = res->pbuf_dma +
424 				j * SEC_PBUF_PKG + pbuf_page_offset;
425 		}
426 	}
427 
428 	return 0;
429 }
430 
sec_alg_resource_alloc(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)431 static int sec_alg_resource_alloc(struct sec_ctx *ctx,
432 				  struct sec_qp_ctx *qp_ctx)
433 {
434 	struct sec_alg_res *res = qp_ctx->res;
435 	struct device *dev = ctx->dev;
436 	int ret;
437 
438 	ret = sec_alloc_civ_resource(dev, res);
439 	if (ret)
440 		return ret;
441 
442 	if (ctx->alg_type == SEC_AEAD) {
443 		ret = sec_alloc_aiv_resource(dev, res);
444 		if (ret)
445 			goto alloc_aiv_fail;
446 
447 		ret = sec_alloc_mac_resource(dev, res);
448 		if (ret)
449 			goto alloc_mac_fail;
450 	}
451 	if (ctx->pbuf_supported) {
452 		ret = sec_alloc_pbuf_resource(dev, res);
453 		if (ret) {
454 			dev_err(dev, "fail to alloc pbuf dma resource!\n");
455 			goto alloc_pbuf_fail;
456 		}
457 	}
458 
459 	return 0;
460 
461 alloc_pbuf_fail:
462 	if (ctx->alg_type == SEC_AEAD)
463 		sec_free_mac_resource(dev, qp_ctx->res);
464 alloc_mac_fail:
465 	if (ctx->alg_type == SEC_AEAD)
466 		sec_free_aiv_resource(dev, res);
467 alloc_aiv_fail:
468 	sec_free_civ_resource(dev, res);
469 	return ret;
470 }
471 
sec_alg_resource_free(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)472 static void sec_alg_resource_free(struct sec_ctx *ctx,
473 				  struct sec_qp_ctx *qp_ctx)
474 {
475 	struct device *dev = ctx->dev;
476 
477 	sec_free_civ_resource(dev, qp_ctx->res);
478 
479 	if (ctx->pbuf_supported)
480 		sec_free_pbuf_resource(dev, qp_ctx->res);
481 	if (ctx->alg_type == SEC_AEAD) {
482 		sec_free_mac_resource(dev, qp_ctx->res);
483 		sec_free_aiv_resource(dev, qp_ctx->res);
484 	}
485 }
486 
sec_alloc_qp_ctx_resource(struct hisi_qm * qm,struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)487 static int sec_alloc_qp_ctx_resource(struct hisi_qm *qm, struct sec_ctx *ctx,
488 				     struct sec_qp_ctx *qp_ctx)
489 {
490 	u16 q_depth = qp_ctx->qp->sq_depth;
491 	struct device *dev = ctx->dev;
492 	int ret = -ENOMEM;
493 
494 	qp_ctx->req_list = kcalloc(q_depth, sizeof(struct sec_req *), GFP_KERNEL);
495 	if (!qp_ctx->req_list)
496 		return ret;
497 
498 	qp_ctx->res = kcalloc(q_depth, sizeof(struct sec_alg_res), GFP_KERNEL);
499 	if (!qp_ctx->res)
500 		goto err_free_req_list;
501 	qp_ctx->res->depth = q_depth;
502 
503 	qp_ctx->c_in_pool = hisi_acc_create_sgl_pool(dev, q_depth, SEC_SGL_SGE_NR);
504 	if (IS_ERR(qp_ctx->c_in_pool)) {
505 		dev_err(dev, "fail to create sgl pool for input!\n");
506 		goto err_free_res;
507 	}
508 
509 	qp_ctx->c_out_pool = hisi_acc_create_sgl_pool(dev, q_depth, SEC_SGL_SGE_NR);
510 	if (IS_ERR(qp_ctx->c_out_pool)) {
511 		dev_err(dev, "fail to create sgl pool for output!\n");
512 		goto err_free_c_in_pool;
513 	}
514 
515 	ret = sec_alg_resource_alloc(ctx, qp_ctx);
516 	if (ret)
517 		goto err_free_c_out_pool;
518 
519 	return 0;
520 
521 err_free_c_out_pool:
522 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_out_pool);
523 err_free_c_in_pool:
524 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
525 err_free_res:
526 	kfree(qp_ctx->res);
527 err_free_req_list:
528 	kfree(qp_ctx->req_list);
529 	return ret;
530 }
531 
sec_free_qp_ctx_resource(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)532 static void sec_free_qp_ctx_resource(struct sec_ctx *ctx, struct sec_qp_ctx *qp_ctx)
533 {
534 	struct device *dev = ctx->dev;
535 
536 	sec_alg_resource_free(ctx, qp_ctx);
537 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_out_pool);
538 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
539 	kfree(qp_ctx->res);
540 	kfree(qp_ctx->req_list);
541 }
542 
sec_create_qp_ctx(struct hisi_qm * qm,struct sec_ctx * ctx,int qp_ctx_id,int alg_type)543 static int sec_create_qp_ctx(struct hisi_qm *qm, struct sec_ctx *ctx,
544 			     int qp_ctx_id, int alg_type)
545 {
546 	struct sec_qp_ctx *qp_ctx;
547 	struct hisi_qp *qp;
548 	int ret;
549 
550 	qp_ctx = &ctx->qp_ctx[qp_ctx_id];
551 	qp = ctx->qps[qp_ctx_id];
552 	qp->req_type = 0;
553 	qp->qp_ctx = qp_ctx;
554 	qp_ctx->qp = qp;
555 	qp_ctx->ctx = ctx;
556 
557 	qp->req_cb = sec_req_cb;
558 
559 	spin_lock_init(&qp_ctx->req_lock);
560 	idr_init(&qp_ctx->req_idr);
561 	INIT_LIST_HEAD(&qp_ctx->backlog);
562 
563 	ret = sec_alloc_qp_ctx_resource(qm, ctx, qp_ctx);
564 	if (ret)
565 		goto err_destroy_idr;
566 
567 	ret = hisi_qm_start_qp(qp, 0);
568 	if (ret < 0)
569 		goto err_resource_free;
570 
571 	return 0;
572 
573 err_resource_free:
574 	sec_free_qp_ctx_resource(ctx, qp_ctx);
575 err_destroy_idr:
576 	idr_destroy(&qp_ctx->req_idr);
577 	return ret;
578 }
579 
sec_release_qp_ctx(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)580 static void sec_release_qp_ctx(struct sec_ctx *ctx,
581 			       struct sec_qp_ctx *qp_ctx)
582 {
583 	hisi_qm_stop_qp(qp_ctx->qp);
584 	sec_free_qp_ctx_resource(ctx, qp_ctx);
585 	idr_destroy(&qp_ctx->req_idr);
586 }
587 
sec_ctx_base_init(struct sec_ctx * ctx)588 static int sec_ctx_base_init(struct sec_ctx *ctx)
589 {
590 	struct sec_dev *sec;
591 	int i, ret;
592 
593 	ctx->qps = sec_create_qps();
594 	if (!ctx->qps) {
595 		pr_err("Can not create sec qps!\n");
596 		return -ENODEV;
597 	}
598 
599 	sec = container_of(ctx->qps[0]->qm, struct sec_dev, qm);
600 	ctx->sec = sec;
601 	ctx->dev = &sec->qm.pdev->dev;
602 	ctx->hlf_q_num = sec->ctx_q_num >> 1;
603 
604 	ctx->pbuf_supported = ctx->sec->iommu_used;
605 
606 	/* Half of queue depth is taken as fake requests limit in the queue. */
607 	ctx->fake_req_limit = ctx->qps[0]->sq_depth >> 1;
608 	ctx->qp_ctx = kcalloc(sec->ctx_q_num, sizeof(struct sec_qp_ctx),
609 			      GFP_KERNEL);
610 	if (!ctx->qp_ctx) {
611 		ret = -ENOMEM;
612 		goto err_destroy_qps;
613 	}
614 
615 	for (i = 0; i < sec->ctx_q_num; i++) {
616 		ret = sec_create_qp_ctx(&sec->qm, ctx, i, 0);
617 		if (ret)
618 			goto err_sec_release_qp_ctx;
619 	}
620 
621 	return 0;
622 
623 err_sec_release_qp_ctx:
624 	for (i = i - 1; i >= 0; i--)
625 		sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
626 	kfree(ctx->qp_ctx);
627 err_destroy_qps:
628 	sec_destroy_qps(ctx->qps, sec->ctx_q_num);
629 	return ret;
630 }
631 
sec_ctx_base_uninit(struct sec_ctx * ctx)632 static void sec_ctx_base_uninit(struct sec_ctx *ctx)
633 {
634 	int i;
635 
636 	for (i = 0; i < ctx->sec->ctx_q_num; i++)
637 		sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
638 
639 	sec_destroy_qps(ctx->qps, ctx->sec->ctx_q_num);
640 	kfree(ctx->qp_ctx);
641 }
642 
sec_cipher_init(struct sec_ctx * ctx)643 static int sec_cipher_init(struct sec_ctx *ctx)
644 {
645 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
646 
647 	c_ctx->c_key = dma_alloc_coherent(ctx->dev, SEC_MAX_KEY_SIZE,
648 					  &c_ctx->c_key_dma, GFP_KERNEL);
649 	if (!c_ctx->c_key)
650 		return -ENOMEM;
651 
652 	return 0;
653 }
654 
sec_cipher_uninit(struct sec_ctx * ctx)655 static void sec_cipher_uninit(struct sec_ctx *ctx)
656 {
657 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
658 
659 	memzero_explicit(c_ctx->c_key, SEC_MAX_KEY_SIZE);
660 	dma_free_coherent(ctx->dev, SEC_MAX_KEY_SIZE,
661 			  c_ctx->c_key, c_ctx->c_key_dma);
662 }
663 
sec_auth_init(struct sec_ctx * ctx)664 static int sec_auth_init(struct sec_ctx *ctx)
665 {
666 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
667 
668 	a_ctx->a_key = dma_alloc_coherent(ctx->dev, SEC_MAX_AKEY_SIZE,
669 					  &a_ctx->a_key_dma, GFP_KERNEL);
670 	if (!a_ctx->a_key)
671 		return -ENOMEM;
672 
673 	return 0;
674 }
675 
sec_auth_uninit(struct sec_ctx * ctx)676 static void sec_auth_uninit(struct sec_ctx *ctx)
677 {
678 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
679 
680 	memzero_explicit(a_ctx->a_key, SEC_MAX_AKEY_SIZE);
681 	dma_free_coherent(ctx->dev, SEC_MAX_AKEY_SIZE,
682 			  a_ctx->a_key, a_ctx->a_key_dma);
683 }
684 
sec_skcipher_fbtfm_init(struct crypto_skcipher * tfm)685 static int sec_skcipher_fbtfm_init(struct crypto_skcipher *tfm)
686 {
687 	const char *alg = crypto_tfm_alg_name(&tfm->base);
688 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
689 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
690 
691 	c_ctx->fallback = false;
692 
693 	/* Currently, only XTS mode need fallback tfm when using 192bit key */
694 	if (likely(strncmp(alg, "xts", SEC_XTS_NAME_SZ)))
695 		return 0;
696 
697 	c_ctx->fbtfm = crypto_alloc_sync_skcipher(alg, 0,
698 						  CRYPTO_ALG_NEED_FALLBACK);
699 	if (IS_ERR(c_ctx->fbtfm)) {
700 		pr_err("failed to alloc xts mode fallback tfm!\n");
701 		return PTR_ERR(c_ctx->fbtfm);
702 	}
703 
704 	return 0;
705 }
706 
sec_skcipher_init(struct crypto_skcipher * tfm)707 static int sec_skcipher_init(struct crypto_skcipher *tfm)
708 {
709 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
710 	int ret;
711 
712 	ctx->alg_type = SEC_SKCIPHER;
713 	crypto_skcipher_set_reqsize(tfm, sizeof(struct sec_req));
714 	ctx->c_ctx.ivsize = crypto_skcipher_ivsize(tfm);
715 	if (ctx->c_ctx.ivsize > SEC_IV_SIZE) {
716 		pr_err("get error skcipher iv size!\n");
717 		return -EINVAL;
718 	}
719 
720 	ret = sec_ctx_base_init(ctx);
721 	if (ret)
722 		return ret;
723 
724 	ret = sec_cipher_init(ctx);
725 	if (ret)
726 		goto err_cipher_init;
727 
728 	ret = sec_skcipher_fbtfm_init(tfm);
729 	if (ret)
730 		goto err_fbtfm_init;
731 
732 	return 0;
733 
734 err_fbtfm_init:
735 	sec_cipher_uninit(ctx);
736 err_cipher_init:
737 	sec_ctx_base_uninit(ctx);
738 	return ret;
739 }
740 
sec_skcipher_uninit(struct crypto_skcipher * tfm)741 static void sec_skcipher_uninit(struct crypto_skcipher *tfm)
742 {
743 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
744 
745 	if (ctx->c_ctx.fbtfm)
746 		crypto_free_sync_skcipher(ctx->c_ctx.fbtfm);
747 
748 	sec_cipher_uninit(ctx);
749 	sec_ctx_base_uninit(ctx);
750 }
751 
sec_skcipher_3des_setkey(struct crypto_skcipher * tfm,const u8 * key,const u32 keylen,const enum sec_cmode c_mode)752 static int sec_skcipher_3des_setkey(struct crypto_skcipher *tfm, const u8 *key,
753 				    const u32 keylen,
754 				    const enum sec_cmode c_mode)
755 {
756 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
757 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
758 	int ret;
759 
760 	ret = verify_skcipher_des3_key(tfm, key);
761 	if (ret)
762 		return ret;
763 
764 	switch (keylen) {
765 	case SEC_DES3_2KEY_SIZE:
766 		c_ctx->c_key_len = SEC_CKEY_3DES_2KEY;
767 		break;
768 	case SEC_DES3_3KEY_SIZE:
769 		c_ctx->c_key_len = SEC_CKEY_3DES_3KEY;
770 		break;
771 	default:
772 		return -EINVAL;
773 	}
774 
775 	return 0;
776 }
777 
sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx * c_ctx,const u32 keylen,const enum sec_cmode c_mode)778 static int sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx *c_ctx,
779 				       const u32 keylen,
780 				       const enum sec_cmode c_mode)
781 {
782 	if (c_mode == SEC_CMODE_XTS) {
783 		switch (keylen) {
784 		case SEC_XTS_MIN_KEY_SIZE:
785 			c_ctx->c_key_len = SEC_CKEY_128BIT;
786 			break;
787 		case SEC_XTS_MID_KEY_SIZE:
788 			c_ctx->fallback = true;
789 			break;
790 		case SEC_XTS_MAX_KEY_SIZE:
791 			c_ctx->c_key_len = SEC_CKEY_256BIT;
792 			break;
793 		default:
794 			pr_err("hisi_sec2: xts mode key error!\n");
795 			return -EINVAL;
796 		}
797 	} else {
798 		if (c_ctx->c_alg == SEC_CALG_SM4 &&
799 		    keylen != AES_KEYSIZE_128) {
800 			pr_err("hisi_sec2: sm4 key error!\n");
801 			return -EINVAL;
802 		} else {
803 			switch (keylen) {
804 			case AES_KEYSIZE_128:
805 				c_ctx->c_key_len = SEC_CKEY_128BIT;
806 				break;
807 			case AES_KEYSIZE_192:
808 				c_ctx->c_key_len = SEC_CKEY_192BIT;
809 				break;
810 			case AES_KEYSIZE_256:
811 				c_ctx->c_key_len = SEC_CKEY_256BIT;
812 				break;
813 			default:
814 				pr_err("hisi_sec2: aes key error!\n");
815 				return -EINVAL;
816 			}
817 		}
818 	}
819 
820 	return 0;
821 }
822 
sec_skcipher_setkey(struct crypto_skcipher * tfm,const u8 * key,const u32 keylen,const enum sec_calg c_alg,const enum sec_cmode c_mode)823 static int sec_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
824 			       const u32 keylen, const enum sec_calg c_alg,
825 			       const enum sec_cmode c_mode)
826 {
827 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
828 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
829 	struct device *dev = ctx->dev;
830 	int ret;
831 
832 	if (c_mode == SEC_CMODE_XTS) {
833 		ret = xts_verify_key(tfm, key, keylen);
834 		if (ret) {
835 			dev_err(dev, "xts mode key err!\n");
836 			return ret;
837 		}
838 	}
839 
840 	c_ctx->c_alg  = c_alg;
841 	c_ctx->c_mode = c_mode;
842 
843 	switch (c_alg) {
844 	case SEC_CALG_3DES:
845 		ret = sec_skcipher_3des_setkey(tfm, key, keylen, c_mode);
846 		break;
847 	case SEC_CALG_AES:
848 	case SEC_CALG_SM4:
849 		ret = sec_skcipher_aes_sm4_setkey(c_ctx, keylen, c_mode);
850 		break;
851 	default:
852 		dev_err(dev, "sec c_alg err!\n");
853 		return -EINVAL;
854 	}
855 
856 	if (ret) {
857 		dev_err(dev, "set sec key err!\n");
858 		return ret;
859 	}
860 
861 	memcpy(c_ctx->c_key, key, keylen);
862 	if (c_ctx->fallback && c_ctx->fbtfm) {
863 		ret = crypto_sync_skcipher_setkey(c_ctx->fbtfm, key, keylen);
864 		if (ret) {
865 			dev_err(dev, "failed to set fallback skcipher key!\n");
866 			return ret;
867 		}
868 	}
869 	return 0;
870 }
871 
872 #define GEN_SEC_SETKEY_FUNC(name, c_alg, c_mode)			\
873 static int sec_setkey_##name(struct crypto_skcipher *tfm, const u8 *key,\
874 	u32 keylen)							\
875 {									\
876 	return sec_skcipher_setkey(tfm, key, keylen, c_alg, c_mode);	\
877 }
878 
GEN_SEC_SETKEY_FUNC(aes_ecb,SEC_CALG_AES,SEC_CMODE_ECB)879 GEN_SEC_SETKEY_FUNC(aes_ecb, SEC_CALG_AES, SEC_CMODE_ECB)
880 GEN_SEC_SETKEY_FUNC(aes_cbc, SEC_CALG_AES, SEC_CMODE_CBC)
881 GEN_SEC_SETKEY_FUNC(aes_xts, SEC_CALG_AES, SEC_CMODE_XTS)
882 GEN_SEC_SETKEY_FUNC(aes_ofb, SEC_CALG_AES, SEC_CMODE_OFB)
883 GEN_SEC_SETKEY_FUNC(aes_cfb, SEC_CALG_AES, SEC_CMODE_CFB)
884 GEN_SEC_SETKEY_FUNC(aes_ctr, SEC_CALG_AES, SEC_CMODE_CTR)
885 GEN_SEC_SETKEY_FUNC(3des_ecb, SEC_CALG_3DES, SEC_CMODE_ECB)
886 GEN_SEC_SETKEY_FUNC(3des_cbc, SEC_CALG_3DES, SEC_CMODE_CBC)
887 GEN_SEC_SETKEY_FUNC(sm4_xts, SEC_CALG_SM4, SEC_CMODE_XTS)
888 GEN_SEC_SETKEY_FUNC(sm4_cbc, SEC_CALG_SM4, SEC_CMODE_CBC)
889 GEN_SEC_SETKEY_FUNC(sm4_ofb, SEC_CALG_SM4, SEC_CMODE_OFB)
890 GEN_SEC_SETKEY_FUNC(sm4_cfb, SEC_CALG_SM4, SEC_CMODE_CFB)
891 GEN_SEC_SETKEY_FUNC(sm4_ctr, SEC_CALG_SM4, SEC_CMODE_CTR)
892 
893 static int sec_cipher_pbuf_map(struct sec_ctx *ctx, struct sec_req *req,
894 			struct scatterlist *src)
895 {
896 	struct sec_aead_req *a_req = &req->aead_req;
897 	struct aead_request *aead_req = a_req->aead_req;
898 	struct sec_cipher_req *c_req = &req->c_req;
899 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
900 	struct device *dev = ctx->dev;
901 	int copy_size, pbuf_length;
902 	int req_id = req->req_id;
903 	struct crypto_aead *tfm;
904 	size_t authsize;
905 	u8 *mac_offset;
906 
907 	if (ctx->alg_type == SEC_AEAD)
908 		copy_size = aead_req->cryptlen + aead_req->assoclen;
909 	else
910 		copy_size = c_req->c_len;
911 
912 	pbuf_length = sg_copy_to_buffer(src, sg_nents(src),
913 			qp_ctx->res[req_id].pbuf, copy_size);
914 	if (unlikely(pbuf_length != copy_size)) {
915 		dev_err(dev, "copy src data to pbuf error!\n");
916 		return -EINVAL;
917 	}
918 	if (!c_req->encrypt && ctx->alg_type == SEC_AEAD) {
919 		tfm = crypto_aead_reqtfm(aead_req);
920 		authsize = crypto_aead_authsize(tfm);
921 		mac_offset = qp_ctx->res[req_id].pbuf + copy_size - authsize;
922 		memcpy(a_req->out_mac, mac_offset, authsize);
923 	}
924 
925 	req->in_dma = qp_ctx->res[req_id].pbuf_dma;
926 	c_req->c_out_dma = req->in_dma;
927 
928 	return 0;
929 }
930 
sec_cipher_pbuf_unmap(struct sec_ctx * ctx,struct sec_req * req,struct scatterlist * dst)931 static void sec_cipher_pbuf_unmap(struct sec_ctx *ctx, struct sec_req *req,
932 			struct scatterlist *dst)
933 {
934 	struct aead_request *aead_req = req->aead_req.aead_req;
935 	struct sec_cipher_req *c_req = &req->c_req;
936 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
937 	int copy_size, pbuf_length;
938 	int req_id = req->req_id;
939 
940 	if (ctx->alg_type == SEC_AEAD)
941 		copy_size = c_req->c_len + aead_req->assoclen;
942 	else
943 		copy_size = c_req->c_len;
944 
945 	pbuf_length = sg_copy_from_buffer(dst, sg_nents(dst),
946 			qp_ctx->res[req_id].pbuf, copy_size);
947 	if (unlikely(pbuf_length != copy_size))
948 		dev_err(ctx->dev, "copy pbuf data to dst error!\n");
949 }
950 
sec_aead_mac_init(struct sec_aead_req * req)951 static int sec_aead_mac_init(struct sec_aead_req *req)
952 {
953 	struct aead_request *aead_req = req->aead_req;
954 	struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
955 	size_t authsize = crypto_aead_authsize(tfm);
956 	struct scatterlist *sgl = aead_req->src;
957 	u8 *mac_out = req->out_mac;
958 	size_t copy_size;
959 	off_t skip_size;
960 
961 	/* Copy input mac */
962 	skip_size = aead_req->assoclen + aead_req->cryptlen - authsize;
963 	copy_size = sg_pcopy_to_buffer(sgl, sg_nents(sgl), mac_out, authsize, skip_size);
964 	if (unlikely(copy_size != authsize))
965 		return -EINVAL;
966 
967 	return 0;
968 }
969 
sec_cipher_map(struct sec_ctx * ctx,struct sec_req * req,struct scatterlist * src,struct scatterlist * dst)970 static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
971 			  struct scatterlist *src, struct scatterlist *dst)
972 {
973 	struct sec_cipher_req *c_req = &req->c_req;
974 	struct sec_aead_req *a_req = &req->aead_req;
975 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
976 	struct sec_alg_res *res = &qp_ctx->res[req->req_id];
977 	struct device *dev = ctx->dev;
978 	int ret;
979 
980 	if (req->use_pbuf) {
981 		c_req->c_ivin = res->pbuf + SEC_PBUF_IV_OFFSET;
982 		c_req->c_ivin_dma = res->pbuf_dma + SEC_PBUF_IV_OFFSET;
983 		if (ctx->alg_type == SEC_AEAD) {
984 			a_req->a_ivin = res->a_ivin;
985 			a_req->a_ivin_dma = res->a_ivin_dma;
986 			a_req->out_mac = res->pbuf + SEC_PBUF_MAC_OFFSET;
987 			a_req->out_mac_dma = res->pbuf_dma +
988 					SEC_PBUF_MAC_OFFSET;
989 		}
990 		ret = sec_cipher_pbuf_map(ctx, req, src);
991 
992 		return ret;
993 	}
994 	c_req->c_ivin = res->c_ivin;
995 	c_req->c_ivin_dma = res->c_ivin_dma;
996 	if (ctx->alg_type == SEC_AEAD) {
997 		a_req->a_ivin = res->a_ivin;
998 		a_req->a_ivin_dma = res->a_ivin_dma;
999 		a_req->out_mac = res->out_mac;
1000 		a_req->out_mac_dma = res->out_mac_dma;
1001 	}
1002 
1003 	req->in = hisi_acc_sg_buf_map_to_hw_sgl(dev, src,
1004 						qp_ctx->c_in_pool,
1005 						req->req_id,
1006 						&req->in_dma);
1007 	if (IS_ERR(req->in)) {
1008 		dev_err(dev, "fail to dma map input sgl buffers!\n");
1009 		return PTR_ERR(req->in);
1010 	}
1011 
1012 	if (!c_req->encrypt && ctx->alg_type == SEC_AEAD) {
1013 		ret = sec_aead_mac_init(a_req);
1014 		if (unlikely(ret)) {
1015 			dev_err(dev, "fail to init mac data for ICV!\n");
1016 			return ret;
1017 		}
1018 	}
1019 
1020 	if (dst == src) {
1021 		c_req->c_out = req->in;
1022 		c_req->c_out_dma = req->in_dma;
1023 	} else {
1024 		c_req->c_out = hisi_acc_sg_buf_map_to_hw_sgl(dev, dst,
1025 							     qp_ctx->c_out_pool,
1026 							     req->req_id,
1027 							     &c_req->c_out_dma);
1028 
1029 		if (IS_ERR(c_req->c_out)) {
1030 			dev_err(dev, "fail to dma map output sgl buffers!\n");
1031 			hisi_acc_sg_buf_unmap(dev, src, req->in);
1032 			return PTR_ERR(c_req->c_out);
1033 		}
1034 	}
1035 
1036 	return 0;
1037 }
1038 
sec_cipher_unmap(struct sec_ctx * ctx,struct sec_req * req,struct scatterlist * src,struct scatterlist * dst)1039 static void sec_cipher_unmap(struct sec_ctx *ctx, struct sec_req *req,
1040 			     struct scatterlist *src, struct scatterlist *dst)
1041 {
1042 	struct sec_cipher_req *c_req = &req->c_req;
1043 	struct device *dev = ctx->dev;
1044 
1045 	if (req->use_pbuf) {
1046 		sec_cipher_pbuf_unmap(ctx, req, dst);
1047 	} else {
1048 		if (dst != src)
1049 			hisi_acc_sg_buf_unmap(dev, src, req->in);
1050 
1051 		hisi_acc_sg_buf_unmap(dev, dst, c_req->c_out);
1052 	}
1053 }
1054 
sec_skcipher_sgl_map(struct sec_ctx * ctx,struct sec_req * req)1055 static int sec_skcipher_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
1056 {
1057 	struct skcipher_request *sq = req->c_req.sk_req;
1058 
1059 	return sec_cipher_map(ctx, req, sq->src, sq->dst);
1060 }
1061 
sec_skcipher_sgl_unmap(struct sec_ctx * ctx,struct sec_req * req)1062 static void sec_skcipher_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
1063 {
1064 	struct skcipher_request *sq = req->c_req.sk_req;
1065 
1066 	sec_cipher_unmap(ctx, req, sq->src, sq->dst);
1067 }
1068 
sec_aead_aes_set_key(struct sec_cipher_ctx * c_ctx,struct crypto_authenc_keys * keys)1069 static int sec_aead_aes_set_key(struct sec_cipher_ctx *c_ctx,
1070 				struct crypto_authenc_keys *keys)
1071 {
1072 	switch (keys->enckeylen) {
1073 	case AES_KEYSIZE_128:
1074 		c_ctx->c_key_len = SEC_CKEY_128BIT;
1075 		break;
1076 	case AES_KEYSIZE_192:
1077 		c_ctx->c_key_len = SEC_CKEY_192BIT;
1078 		break;
1079 	case AES_KEYSIZE_256:
1080 		c_ctx->c_key_len = SEC_CKEY_256BIT;
1081 		break;
1082 	default:
1083 		pr_err("hisi_sec2: aead aes key error!\n");
1084 		return -EINVAL;
1085 	}
1086 	memcpy(c_ctx->c_key, keys->enckey, keys->enckeylen);
1087 
1088 	return 0;
1089 }
1090 
sec_aead_auth_set_key(struct sec_auth_ctx * ctx,struct crypto_authenc_keys * keys)1091 static int sec_aead_auth_set_key(struct sec_auth_ctx *ctx,
1092 				 struct crypto_authenc_keys *keys)
1093 {
1094 	struct crypto_shash *hash_tfm = ctx->hash_tfm;
1095 	int blocksize, digestsize, ret;
1096 
1097 	if (!keys->authkeylen) {
1098 		pr_err("hisi_sec2: aead auth key error!\n");
1099 		return -EINVAL;
1100 	}
1101 
1102 	blocksize = crypto_shash_blocksize(hash_tfm);
1103 	digestsize = crypto_shash_digestsize(hash_tfm);
1104 	if (keys->authkeylen > blocksize) {
1105 		ret = crypto_shash_tfm_digest(hash_tfm, keys->authkey,
1106 					      keys->authkeylen, ctx->a_key);
1107 		if (ret) {
1108 			pr_err("hisi_sec2: aead auth digest error!\n");
1109 			return -EINVAL;
1110 		}
1111 		ctx->a_key_len = digestsize;
1112 	} else {
1113 		memcpy(ctx->a_key, keys->authkey, keys->authkeylen);
1114 		ctx->a_key_len = keys->authkeylen;
1115 	}
1116 
1117 	return 0;
1118 }
1119 
sec_aead_setauthsize(struct crypto_aead * aead,unsigned int authsize)1120 static int sec_aead_setauthsize(struct crypto_aead *aead, unsigned int authsize)
1121 {
1122 	struct crypto_tfm *tfm = crypto_aead_tfm(aead);
1123 	struct sec_ctx *ctx = crypto_tfm_ctx(tfm);
1124 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1125 
1126 	return crypto_aead_setauthsize(a_ctx->fallback_aead_tfm, authsize);
1127 }
1128 
sec_aead_fallback_setkey(struct sec_auth_ctx * a_ctx,struct crypto_aead * tfm,const u8 * key,unsigned int keylen)1129 static int sec_aead_fallback_setkey(struct sec_auth_ctx *a_ctx,
1130 				    struct crypto_aead *tfm, const u8 *key,
1131 				    unsigned int keylen)
1132 {
1133 	crypto_aead_clear_flags(a_ctx->fallback_aead_tfm, CRYPTO_TFM_REQ_MASK);
1134 	crypto_aead_set_flags(a_ctx->fallback_aead_tfm,
1135 			      crypto_aead_get_flags(tfm) & CRYPTO_TFM_REQ_MASK);
1136 	return crypto_aead_setkey(a_ctx->fallback_aead_tfm, key, keylen);
1137 }
1138 
sec_aead_setkey(struct crypto_aead * tfm,const u8 * key,const u32 keylen,const enum sec_hash_alg a_alg,const enum sec_calg c_alg,const enum sec_cmode c_mode)1139 static int sec_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1140 			   const u32 keylen, const enum sec_hash_alg a_alg,
1141 			   const enum sec_calg c_alg,
1142 			   const enum sec_cmode c_mode)
1143 {
1144 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1145 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1146 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1147 	struct device *dev = ctx->dev;
1148 	struct crypto_authenc_keys keys;
1149 	int ret;
1150 
1151 	ctx->a_ctx.a_alg = a_alg;
1152 	ctx->c_ctx.c_alg = c_alg;
1153 	c_ctx->c_mode = c_mode;
1154 
1155 	if (c_mode == SEC_CMODE_CCM || c_mode == SEC_CMODE_GCM) {
1156 		ret = sec_skcipher_aes_sm4_setkey(c_ctx, keylen, c_mode);
1157 		if (ret) {
1158 			dev_err(dev, "set sec aes ccm cipher key err!\n");
1159 			return ret;
1160 		}
1161 		memcpy(c_ctx->c_key, key, keylen);
1162 
1163 		return sec_aead_fallback_setkey(a_ctx, tfm, key, keylen);
1164 	}
1165 
1166 	ret = crypto_authenc_extractkeys(&keys, key, keylen);
1167 	if (ret)
1168 		goto bad_key;
1169 
1170 	ret = sec_aead_aes_set_key(c_ctx, &keys);
1171 	if (ret) {
1172 		dev_err(dev, "set sec cipher key err!\n");
1173 		goto bad_key;
1174 	}
1175 
1176 	ret = sec_aead_auth_set_key(&ctx->a_ctx, &keys);
1177 	if (ret) {
1178 		dev_err(dev, "set sec auth key err!\n");
1179 		goto bad_key;
1180 	}
1181 
1182 	if (ctx->a_ctx.a_key_len & SEC_SQE_LEN_RATE_MASK) {
1183 		ret = -EINVAL;
1184 		dev_err(dev, "AUTH key length error!\n");
1185 		goto bad_key;
1186 	}
1187 
1188 	ret = sec_aead_fallback_setkey(a_ctx, tfm, key, keylen);
1189 	if (ret) {
1190 		dev_err(dev, "set sec fallback key err!\n");
1191 		goto bad_key;
1192 	}
1193 
1194 	return 0;
1195 
1196 bad_key:
1197 	memzero_explicit(&keys, sizeof(struct crypto_authenc_keys));
1198 	return ret;
1199 }
1200 
1201 
1202 #define GEN_SEC_AEAD_SETKEY_FUNC(name, aalg, calg, cmode)				\
1203 static int sec_setkey_##name(struct crypto_aead *tfm, const u8 *key, u32 keylen)	\
1204 {											\
1205 	return sec_aead_setkey(tfm, key, keylen, aalg, calg, cmode);			\
1206 }
1207 
GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1,SEC_A_HMAC_SHA1,SEC_CALG_AES,SEC_CMODE_CBC)1208 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1, SEC_A_HMAC_SHA1, SEC_CALG_AES, SEC_CMODE_CBC)
1209 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha256, SEC_A_HMAC_SHA256, SEC_CALG_AES, SEC_CMODE_CBC)
1210 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha512, SEC_A_HMAC_SHA512, SEC_CALG_AES, SEC_CMODE_CBC)
1211 GEN_SEC_AEAD_SETKEY_FUNC(aes_ccm, 0, SEC_CALG_AES, SEC_CMODE_CCM)
1212 GEN_SEC_AEAD_SETKEY_FUNC(aes_gcm, 0, SEC_CALG_AES, SEC_CMODE_GCM)
1213 GEN_SEC_AEAD_SETKEY_FUNC(sm4_ccm, 0, SEC_CALG_SM4, SEC_CMODE_CCM)
1214 GEN_SEC_AEAD_SETKEY_FUNC(sm4_gcm, 0, SEC_CALG_SM4, SEC_CMODE_GCM)
1215 
1216 static int sec_aead_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
1217 {
1218 	struct aead_request *aq = req->aead_req.aead_req;
1219 
1220 	return sec_cipher_map(ctx, req, aq->src, aq->dst);
1221 }
1222 
sec_aead_sgl_unmap(struct sec_ctx * ctx,struct sec_req * req)1223 static void sec_aead_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
1224 {
1225 	struct aead_request *aq = req->aead_req.aead_req;
1226 
1227 	sec_cipher_unmap(ctx, req, aq->src, aq->dst);
1228 }
1229 
sec_request_transfer(struct sec_ctx * ctx,struct sec_req * req)1230 static int sec_request_transfer(struct sec_ctx *ctx, struct sec_req *req)
1231 {
1232 	int ret;
1233 
1234 	ret = ctx->req_op->buf_map(ctx, req);
1235 	if (unlikely(ret))
1236 		return ret;
1237 
1238 	ctx->req_op->do_transfer(ctx, req);
1239 
1240 	ret = ctx->req_op->bd_fill(ctx, req);
1241 	if (unlikely(ret))
1242 		goto unmap_req_buf;
1243 
1244 	return ret;
1245 
1246 unmap_req_buf:
1247 	ctx->req_op->buf_unmap(ctx, req);
1248 	return ret;
1249 }
1250 
sec_request_untransfer(struct sec_ctx * ctx,struct sec_req * req)1251 static void sec_request_untransfer(struct sec_ctx *ctx, struct sec_req *req)
1252 {
1253 	ctx->req_op->buf_unmap(ctx, req);
1254 }
1255 
sec_skcipher_copy_iv(struct sec_ctx * ctx,struct sec_req * req)1256 static void sec_skcipher_copy_iv(struct sec_ctx *ctx, struct sec_req *req)
1257 {
1258 	struct skcipher_request *sk_req = req->c_req.sk_req;
1259 	struct sec_cipher_req *c_req = &req->c_req;
1260 
1261 	memcpy(c_req->c_ivin, sk_req->iv, ctx->c_ctx.ivsize);
1262 }
1263 
sec_skcipher_bd_fill(struct sec_ctx * ctx,struct sec_req * req)1264 static int sec_skcipher_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1265 {
1266 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1267 	struct sec_cipher_req *c_req = &req->c_req;
1268 	struct sec_sqe *sec_sqe = &req->sec_sqe;
1269 	u8 scene, sa_type, da_type;
1270 	u8 bd_type, cipher;
1271 	u8 de = 0;
1272 
1273 	memset(sec_sqe, 0, sizeof(struct sec_sqe));
1274 
1275 	sec_sqe->type2.c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
1276 	sec_sqe->type2.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
1277 	sec_sqe->type2.data_src_addr = cpu_to_le64(req->in_dma);
1278 	sec_sqe->type2.data_dst_addr = cpu_to_le64(c_req->c_out_dma);
1279 
1280 	sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_mode) <<
1281 						SEC_CMODE_OFFSET);
1282 	sec_sqe->type2.c_alg = c_ctx->c_alg;
1283 	sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
1284 						SEC_CKEY_OFFSET);
1285 
1286 	bd_type = SEC_BD_TYPE2;
1287 	if (c_req->encrypt)
1288 		cipher = SEC_CIPHER_ENC << SEC_CIPHER_OFFSET;
1289 	else
1290 		cipher = SEC_CIPHER_DEC << SEC_CIPHER_OFFSET;
1291 	sec_sqe->type_cipher_auth = bd_type | cipher;
1292 
1293 	/* Set destination and source address type */
1294 	if (req->use_pbuf) {
1295 		sa_type = SEC_PBUF << SEC_SRC_SGL_OFFSET;
1296 		da_type = SEC_PBUF << SEC_DST_SGL_OFFSET;
1297 	} else {
1298 		sa_type = SEC_SGL << SEC_SRC_SGL_OFFSET;
1299 		da_type = SEC_SGL << SEC_DST_SGL_OFFSET;
1300 	}
1301 
1302 	sec_sqe->sdm_addr_type |= da_type;
1303 	scene = SEC_COMM_SCENE << SEC_SCENE_OFFSET;
1304 	if (req->in_dma != c_req->c_out_dma)
1305 		de = 0x1 << SEC_DE_OFFSET;
1306 
1307 	sec_sqe->sds_sa_type = (de | scene | sa_type);
1308 
1309 	sec_sqe->type2.clen_ivhlen |= cpu_to_le32(c_req->c_len);
1310 	sec_sqe->type2.tag = cpu_to_le16((u16)req->req_id);
1311 
1312 	return 0;
1313 }
1314 
sec_skcipher_bd_fill_v3(struct sec_ctx * ctx,struct sec_req * req)1315 static int sec_skcipher_bd_fill_v3(struct sec_ctx *ctx, struct sec_req *req)
1316 {
1317 	struct sec_sqe3 *sec_sqe3 = &req->sec_sqe3;
1318 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1319 	struct sec_cipher_req *c_req = &req->c_req;
1320 	u32 bd_param = 0;
1321 	u16 cipher;
1322 
1323 	memset(sec_sqe3, 0, sizeof(struct sec_sqe3));
1324 
1325 	sec_sqe3->c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
1326 	sec_sqe3->no_scene.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
1327 	sec_sqe3->data_src_addr = cpu_to_le64(req->in_dma);
1328 	sec_sqe3->data_dst_addr = cpu_to_le64(c_req->c_out_dma);
1329 
1330 	sec_sqe3->c_mode_alg = ((u8)c_ctx->c_alg << SEC_CALG_OFFSET_V3) |
1331 						c_ctx->c_mode;
1332 	sec_sqe3->c_icv_key |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
1333 						SEC_CKEY_OFFSET_V3);
1334 
1335 	if (c_req->encrypt)
1336 		cipher = SEC_CIPHER_ENC;
1337 	else
1338 		cipher = SEC_CIPHER_DEC;
1339 	sec_sqe3->c_icv_key |= cpu_to_le16(cipher);
1340 
1341 	/* Set the CTR counter mode is 128bit rollover */
1342 	sec_sqe3->auth_mac_key = cpu_to_le32((u32)SEC_CTR_CNT_ROLLOVER <<
1343 					SEC_CTR_CNT_OFFSET);
1344 
1345 	if (req->use_pbuf) {
1346 		bd_param |= SEC_PBUF << SEC_SRC_SGL_OFFSET_V3;
1347 		bd_param |= SEC_PBUF << SEC_DST_SGL_OFFSET_V3;
1348 	} else {
1349 		bd_param |= SEC_SGL << SEC_SRC_SGL_OFFSET_V3;
1350 		bd_param |= SEC_SGL << SEC_DST_SGL_OFFSET_V3;
1351 	}
1352 
1353 	bd_param |= SEC_COMM_SCENE << SEC_SCENE_OFFSET_V3;
1354 	if (req->in_dma != c_req->c_out_dma)
1355 		bd_param |= 0x1 << SEC_DE_OFFSET_V3;
1356 
1357 	bd_param |= SEC_BD_TYPE3;
1358 	sec_sqe3->bd_param = cpu_to_le32(bd_param);
1359 
1360 	sec_sqe3->c_len_ivin |= cpu_to_le32(c_req->c_len);
1361 	sec_sqe3->tag = cpu_to_le64(req);
1362 
1363 	return 0;
1364 }
1365 
1366 /* increment counter (128-bit int) */
ctr_iv_inc(__u8 * counter,__u8 bits,__u32 nums)1367 static void ctr_iv_inc(__u8 *counter, __u8 bits, __u32 nums)
1368 {
1369 	do {
1370 		--bits;
1371 		nums += counter[bits];
1372 		counter[bits] = nums & BITS_MASK;
1373 		nums >>= BYTE_BITS;
1374 	} while (bits && nums);
1375 }
1376 
sec_update_iv(struct sec_req * req,enum sec_alg_type alg_type)1377 static void sec_update_iv(struct sec_req *req, enum sec_alg_type alg_type)
1378 {
1379 	struct aead_request *aead_req = req->aead_req.aead_req;
1380 	struct skcipher_request *sk_req = req->c_req.sk_req;
1381 	u32 iv_size = req->ctx->c_ctx.ivsize;
1382 	struct scatterlist *sgl;
1383 	unsigned int cryptlen;
1384 	size_t sz;
1385 	u8 *iv;
1386 
1387 	if (req->c_req.encrypt)
1388 		sgl = alg_type == SEC_SKCIPHER ? sk_req->dst : aead_req->dst;
1389 	else
1390 		sgl = alg_type == SEC_SKCIPHER ? sk_req->src : aead_req->src;
1391 
1392 	if (alg_type == SEC_SKCIPHER) {
1393 		iv = sk_req->iv;
1394 		cryptlen = sk_req->cryptlen;
1395 	} else {
1396 		iv = aead_req->iv;
1397 		cryptlen = aead_req->cryptlen;
1398 	}
1399 
1400 	if (req->ctx->c_ctx.c_mode == SEC_CMODE_CBC) {
1401 		sz = sg_pcopy_to_buffer(sgl, sg_nents(sgl), iv, iv_size,
1402 					cryptlen - iv_size);
1403 		if (unlikely(sz != iv_size))
1404 			dev_err(req->ctx->dev, "copy output iv error!\n");
1405 	} else {
1406 		sz = cryptlen / iv_size;
1407 		if (cryptlen % iv_size)
1408 			sz += 1;
1409 		ctr_iv_inc(iv, iv_size, sz);
1410 	}
1411 }
1412 
sec_back_req_clear(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)1413 static struct sec_req *sec_back_req_clear(struct sec_ctx *ctx,
1414 				struct sec_qp_ctx *qp_ctx)
1415 {
1416 	struct sec_req *backlog_req = NULL;
1417 
1418 	spin_lock_bh(&qp_ctx->req_lock);
1419 	if (ctx->fake_req_limit >=
1420 	    atomic_read(&qp_ctx->qp->qp_status.used) &&
1421 	    !list_empty(&qp_ctx->backlog)) {
1422 		backlog_req = list_first_entry(&qp_ctx->backlog,
1423 				typeof(*backlog_req), backlog_head);
1424 		list_del(&backlog_req->backlog_head);
1425 	}
1426 	spin_unlock_bh(&qp_ctx->req_lock);
1427 
1428 	return backlog_req;
1429 }
1430 
sec_skcipher_callback(struct sec_ctx * ctx,struct sec_req * req,int err)1431 static void sec_skcipher_callback(struct sec_ctx *ctx, struct sec_req *req,
1432 				  int err)
1433 {
1434 	struct skcipher_request *sk_req = req->c_req.sk_req;
1435 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1436 	struct skcipher_request *backlog_sk_req;
1437 	struct sec_req *backlog_req;
1438 
1439 	sec_free_req_id(req);
1440 
1441 	/* IV output at encrypto of CBC/CTR mode */
1442 	if (!err && (ctx->c_ctx.c_mode == SEC_CMODE_CBC ||
1443 	    ctx->c_ctx.c_mode == SEC_CMODE_CTR) && req->c_req.encrypt)
1444 		sec_update_iv(req, SEC_SKCIPHER);
1445 
1446 	while (1) {
1447 		backlog_req = sec_back_req_clear(ctx, qp_ctx);
1448 		if (!backlog_req)
1449 			break;
1450 
1451 		backlog_sk_req = backlog_req->c_req.sk_req;
1452 		skcipher_request_complete(backlog_sk_req, -EINPROGRESS);
1453 		atomic64_inc(&ctx->sec->debug.dfx.recv_busy_cnt);
1454 	}
1455 
1456 	skcipher_request_complete(sk_req, err);
1457 }
1458 
set_aead_auth_iv(struct sec_ctx * ctx,struct sec_req * req)1459 static void set_aead_auth_iv(struct sec_ctx *ctx, struct sec_req *req)
1460 {
1461 	struct aead_request *aead_req = req->aead_req.aead_req;
1462 	struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
1463 	size_t authsize = crypto_aead_authsize(tfm);
1464 	struct sec_aead_req *a_req = &req->aead_req;
1465 	struct sec_cipher_req *c_req = &req->c_req;
1466 	u32 data_size = aead_req->cryptlen;
1467 	u8 flage = 0;
1468 	u8 cm, cl;
1469 
1470 	/* the specification has been checked in aead_iv_demension_check() */
1471 	cl = c_req->c_ivin[0] + 1;
1472 	c_req->c_ivin[ctx->c_ctx.ivsize - cl] = 0x00;
1473 	memset(&c_req->c_ivin[ctx->c_ctx.ivsize - cl], 0, cl);
1474 	c_req->c_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE1] = IV_CTR_INIT;
1475 
1476 	/* the last 3bit is L' */
1477 	flage |= c_req->c_ivin[0] & IV_CL_MASK;
1478 
1479 	/* the M' is bit3~bit5, the Flags is bit6 */
1480 	cm = (authsize - IV_CM_CAL_NUM) / IV_CM_CAL_NUM;
1481 	flage |= cm << IV_CM_OFFSET;
1482 	if (aead_req->assoclen)
1483 		flage |= 0x01 << IV_FLAGS_OFFSET;
1484 
1485 	memcpy(a_req->a_ivin, c_req->c_ivin, ctx->c_ctx.ivsize);
1486 	a_req->a_ivin[0] = flage;
1487 
1488 	/*
1489 	 * the last 32bit is counter's initial number,
1490 	 * but the nonce uses the first 16bit
1491 	 * the tail 16bit fill with the cipher length
1492 	 */
1493 	if (!c_req->encrypt)
1494 		data_size = aead_req->cryptlen - authsize;
1495 
1496 	a_req->a_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE1] =
1497 			data_size & IV_LAST_BYTE_MASK;
1498 	data_size >>= IV_BYTE_OFFSET;
1499 	a_req->a_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE2] =
1500 			data_size & IV_LAST_BYTE_MASK;
1501 }
1502 
sec_aead_set_iv(struct sec_ctx * ctx,struct sec_req * req)1503 static void sec_aead_set_iv(struct sec_ctx *ctx, struct sec_req *req)
1504 {
1505 	struct aead_request *aead_req = req->aead_req.aead_req;
1506 	struct sec_aead_req *a_req = &req->aead_req;
1507 	struct sec_cipher_req *c_req = &req->c_req;
1508 
1509 	memcpy(c_req->c_ivin, aead_req->iv, ctx->c_ctx.ivsize);
1510 
1511 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM) {
1512 		/*
1513 		 * CCM 16Byte Cipher_IV: {1B_Flage,13B_IV,2B_counter},
1514 		 * the  counter must set to 0x01
1515 		 * CCM 16Byte Auth_IV: {1B_AFlage,13B_IV,2B_Ptext_length}
1516 		 */
1517 		set_aead_auth_iv(ctx, req);
1518 	} else if (ctx->c_ctx.c_mode == SEC_CMODE_GCM) {
1519 		/* GCM 12Byte Cipher_IV == Auth_IV */
1520 		memcpy(a_req->a_ivin, c_req->c_ivin, SEC_AIV_SIZE);
1521 	}
1522 }
1523 
sec_auth_bd_fill_xcm(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe * sec_sqe)1524 static void sec_auth_bd_fill_xcm(struct sec_auth_ctx *ctx, int dir,
1525 				 struct sec_req *req, struct sec_sqe *sec_sqe)
1526 {
1527 	struct sec_aead_req *a_req = &req->aead_req;
1528 	struct aead_request *aq = a_req->aead_req;
1529 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1530 	size_t authsize = crypto_aead_authsize(tfm);
1531 
1532 	/* C_ICV_Len is MAC size, 0x4 ~ 0x10 */
1533 	sec_sqe->type2.icvw_kmode |= cpu_to_le16((u16)authsize);
1534 
1535 	/* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */
1536 	sec_sqe->type2.a_key_addr = sec_sqe->type2.c_key_addr;
1537 	sec_sqe->type2.a_ivin_addr = cpu_to_le64(a_req->a_ivin_dma);
1538 	sec_sqe->type_cipher_auth |= SEC_NO_AUTH << SEC_AUTH_OFFSET;
1539 
1540 	if (dir)
1541 		sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1542 	else
1543 		sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1544 
1545 	sec_sqe->type2.alen_ivllen = cpu_to_le32(aq->assoclen);
1546 	sec_sqe->type2.auth_src_offset = cpu_to_le16(0x0);
1547 	sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1548 
1549 	sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1550 }
1551 
sec_auth_bd_fill_xcm_v3(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe3 * sqe3)1552 static void sec_auth_bd_fill_xcm_v3(struct sec_auth_ctx *ctx, int dir,
1553 				    struct sec_req *req, struct sec_sqe3 *sqe3)
1554 {
1555 	struct sec_aead_req *a_req = &req->aead_req;
1556 	struct aead_request *aq = a_req->aead_req;
1557 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1558 	size_t authsize = crypto_aead_authsize(tfm);
1559 
1560 	/* C_ICV_Len is MAC size, 0x4 ~ 0x10 */
1561 	sqe3->c_icv_key |= cpu_to_le16((u16)authsize << SEC_MAC_OFFSET_V3);
1562 
1563 	/* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */
1564 	sqe3->a_key_addr = sqe3->c_key_addr;
1565 	sqe3->auth_ivin.a_ivin_addr = cpu_to_le64(a_req->a_ivin_dma);
1566 	sqe3->auth_mac_key |= SEC_NO_AUTH;
1567 
1568 	if (dir)
1569 		sqe3->huk_iv_seq &= SEC_CIPHER_AUTH_V3;
1570 	else
1571 		sqe3->huk_iv_seq |= SEC_AUTH_CIPHER_V3;
1572 
1573 	sqe3->a_len_key = cpu_to_le32(aq->assoclen);
1574 	sqe3->auth_src_offset = cpu_to_le16(0x0);
1575 	sqe3->cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1576 	sqe3->mac_addr = cpu_to_le64(a_req->out_mac_dma);
1577 }
1578 
sec_auth_bd_fill_ex(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe * sec_sqe)1579 static void sec_auth_bd_fill_ex(struct sec_auth_ctx *ctx, int dir,
1580 			       struct sec_req *req, struct sec_sqe *sec_sqe)
1581 {
1582 	struct sec_aead_req *a_req = &req->aead_req;
1583 	struct sec_cipher_req *c_req = &req->c_req;
1584 	struct aead_request *aq = a_req->aead_req;
1585 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1586 	size_t authsize = crypto_aead_authsize(tfm);
1587 
1588 	sec_sqe->type2.a_key_addr = cpu_to_le64(ctx->a_key_dma);
1589 
1590 	sec_sqe->type2.mac_key_alg = cpu_to_le32(authsize / SEC_SQE_LEN_RATE);
1591 
1592 	sec_sqe->type2.mac_key_alg |=
1593 			cpu_to_le32((u32)((ctx->a_key_len) /
1594 			SEC_SQE_LEN_RATE) << SEC_AKEY_OFFSET);
1595 
1596 	sec_sqe->type2.mac_key_alg |=
1597 			cpu_to_le32((u32)(ctx->a_alg) << SEC_AEAD_ALG_OFFSET);
1598 
1599 	if (dir) {
1600 		sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE1 << SEC_AUTH_OFFSET;
1601 		sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1602 	} else {
1603 		sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE2 << SEC_AUTH_OFFSET;
1604 		sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1605 	}
1606 	sec_sqe->type2.alen_ivllen = cpu_to_le32(c_req->c_len + aq->assoclen);
1607 
1608 	sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1609 
1610 	sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1611 }
1612 
sec_aead_bd_fill(struct sec_ctx * ctx,struct sec_req * req)1613 static int sec_aead_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1614 {
1615 	struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1616 	struct sec_sqe *sec_sqe = &req->sec_sqe;
1617 	int ret;
1618 
1619 	ret = sec_skcipher_bd_fill(ctx, req);
1620 	if (unlikely(ret)) {
1621 		dev_err(ctx->dev, "skcipher bd fill is error!\n");
1622 		return ret;
1623 	}
1624 
1625 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM ||
1626 	    ctx->c_ctx.c_mode == SEC_CMODE_GCM)
1627 		sec_auth_bd_fill_xcm(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1628 	else
1629 		sec_auth_bd_fill_ex(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1630 
1631 	return 0;
1632 }
1633 
sec_auth_bd_fill_ex_v3(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe3 * sqe3)1634 static void sec_auth_bd_fill_ex_v3(struct sec_auth_ctx *ctx, int dir,
1635 				   struct sec_req *req, struct sec_sqe3 *sqe3)
1636 {
1637 	struct sec_aead_req *a_req = &req->aead_req;
1638 	struct sec_cipher_req *c_req = &req->c_req;
1639 	struct aead_request *aq = a_req->aead_req;
1640 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1641 	size_t authsize = crypto_aead_authsize(tfm);
1642 
1643 	sqe3->a_key_addr = cpu_to_le64(ctx->a_key_dma);
1644 
1645 	sqe3->auth_mac_key |=
1646 			cpu_to_le32((u32)(authsize /
1647 			SEC_SQE_LEN_RATE) << SEC_MAC_OFFSET_V3);
1648 
1649 	sqe3->auth_mac_key |=
1650 			cpu_to_le32((u32)(ctx->a_key_len /
1651 			SEC_SQE_LEN_RATE) << SEC_AKEY_OFFSET_V3);
1652 
1653 	sqe3->auth_mac_key |=
1654 			cpu_to_le32((u32)(ctx->a_alg) << SEC_AUTH_ALG_OFFSET_V3);
1655 
1656 	if (dir) {
1657 		sqe3->auth_mac_key |= cpu_to_le32((u32)SEC_AUTH_TYPE1);
1658 		sqe3->huk_iv_seq &= SEC_CIPHER_AUTH_V3;
1659 	} else {
1660 		sqe3->auth_mac_key |= cpu_to_le32((u32)SEC_AUTH_TYPE2);
1661 		sqe3->huk_iv_seq |= SEC_AUTH_CIPHER_V3;
1662 	}
1663 	sqe3->a_len_key = cpu_to_le32(c_req->c_len + aq->assoclen);
1664 
1665 	sqe3->cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1666 
1667 	sqe3->mac_addr = cpu_to_le64(a_req->out_mac_dma);
1668 }
1669 
sec_aead_bd_fill_v3(struct sec_ctx * ctx,struct sec_req * req)1670 static int sec_aead_bd_fill_v3(struct sec_ctx *ctx, struct sec_req *req)
1671 {
1672 	struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1673 	struct sec_sqe3 *sec_sqe3 = &req->sec_sqe3;
1674 	int ret;
1675 
1676 	ret = sec_skcipher_bd_fill_v3(ctx, req);
1677 	if (unlikely(ret)) {
1678 		dev_err(ctx->dev, "skcipher bd3 fill is error!\n");
1679 		return ret;
1680 	}
1681 
1682 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM ||
1683 	    ctx->c_ctx.c_mode == SEC_CMODE_GCM)
1684 		sec_auth_bd_fill_xcm_v3(auth_ctx, req->c_req.encrypt,
1685 					req, sec_sqe3);
1686 	else
1687 		sec_auth_bd_fill_ex_v3(auth_ctx, req->c_req.encrypt,
1688 				       req, sec_sqe3);
1689 
1690 	return 0;
1691 }
1692 
sec_aead_callback(struct sec_ctx * c,struct sec_req * req,int err)1693 static void sec_aead_callback(struct sec_ctx *c, struct sec_req *req, int err)
1694 {
1695 	struct aead_request *a_req = req->aead_req.aead_req;
1696 	struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
1697 	size_t authsize = crypto_aead_authsize(tfm);
1698 	struct sec_aead_req *aead_req = &req->aead_req;
1699 	struct sec_cipher_req *c_req = &req->c_req;
1700 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1701 	struct aead_request *backlog_aead_req;
1702 	struct sec_req *backlog_req;
1703 	size_t sz;
1704 
1705 	if (!err && c->c_ctx.c_mode == SEC_CMODE_CBC && c_req->encrypt)
1706 		sec_update_iv(req, SEC_AEAD);
1707 
1708 	/* Copy output mac */
1709 	if (!err && c_req->encrypt) {
1710 		struct scatterlist *sgl = a_req->dst;
1711 
1712 		sz = sg_pcopy_from_buffer(sgl, sg_nents(sgl), aead_req->out_mac,
1713 					  authsize, a_req->cryptlen + a_req->assoclen);
1714 		if (unlikely(sz != authsize)) {
1715 			dev_err(c->dev, "copy out mac err!\n");
1716 			err = -EINVAL;
1717 		}
1718 	}
1719 
1720 	sec_free_req_id(req);
1721 
1722 	while (1) {
1723 		backlog_req = sec_back_req_clear(c, qp_ctx);
1724 		if (!backlog_req)
1725 			break;
1726 
1727 		backlog_aead_req = backlog_req->aead_req.aead_req;
1728 		aead_request_complete(backlog_aead_req, -EINPROGRESS);
1729 		atomic64_inc(&c->sec->debug.dfx.recv_busy_cnt);
1730 	}
1731 
1732 	aead_request_complete(a_req, err);
1733 }
1734 
sec_request_uninit(struct sec_ctx * ctx,struct sec_req * req)1735 static void sec_request_uninit(struct sec_ctx *ctx, struct sec_req *req)
1736 {
1737 	sec_free_req_id(req);
1738 	sec_free_queue_id(ctx, req);
1739 }
1740 
sec_request_init(struct sec_ctx * ctx,struct sec_req * req)1741 static int sec_request_init(struct sec_ctx *ctx, struct sec_req *req)
1742 {
1743 	struct sec_qp_ctx *qp_ctx;
1744 	int queue_id;
1745 
1746 	/* To load balance */
1747 	queue_id = sec_alloc_queue_id(ctx, req);
1748 	qp_ctx = &ctx->qp_ctx[queue_id];
1749 
1750 	req->req_id = sec_alloc_req_id(req, qp_ctx);
1751 	if (unlikely(req->req_id < 0)) {
1752 		sec_free_queue_id(ctx, req);
1753 		return req->req_id;
1754 	}
1755 
1756 	return 0;
1757 }
1758 
sec_process(struct sec_ctx * ctx,struct sec_req * req)1759 static int sec_process(struct sec_ctx *ctx, struct sec_req *req)
1760 {
1761 	struct sec_cipher_req *c_req = &req->c_req;
1762 	int ret;
1763 
1764 	ret = sec_request_init(ctx, req);
1765 	if (unlikely(ret))
1766 		return ret;
1767 
1768 	ret = sec_request_transfer(ctx, req);
1769 	if (unlikely(ret))
1770 		goto err_uninit_req;
1771 
1772 	/* Output IV as decrypto */
1773 	if (!req->c_req.encrypt && (ctx->c_ctx.c_mode == SEC_CMODE_CBC ||
1774 	    ctx->c_ctx.c_mode == SEC_CMODE_CTR))
1775 		sec_update_iv(req, ctx->alg_type);
1776 
1777 	ret = ctx->req_op->bd_send(ctx, req);
1778 	if (unlikely((ret != -EBUSY && ret != -EINPROGRESS) ||
1779 		(ret == -EBUSY && !(req->flag & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
1780 		dev_err_ratelimited(ctx->dev, "send sec request failed!\n");
1781 		goto err_send_req;
1782 	}
1783 
1784 	return ret;
1785 
1786 err_send_req:
1787 	/* As failing, restore the IV from user */
1788 	if (ctx->c_ctx.c_mode == SEC_CMODE_CBC && !req->c_req.encrypt) {
1789 		if (ctx->alg_type == SEC_SKCIPHER)
1790 			memcpy(req->c_req.sk_req->iv, c_req->c_ivin,
1791 			       ctx->c_ctx.ivsize);
1792 		else
1793 			memcpy(req->aead_req.aead_req->iv, c_req->c_ivin,
1794 			       ctx->c_ctx.ivsize);
1795 	}
1796 
1797 	sec_request_untransfer(ctx, req);
1798 err_uninit_req:
1799 	sec_request_uninit(ctx, req);
1800 	return ret;
1801 }
1802 
1803 static const struct sec_req_op sec_skcipher_req_ops = {
1804 	.buf_map	= sec_skcipher_sgl_map,
1805 	.buf_unmap	= sec_skcipher_sgl_unmap,
1806 	.do_transfer	= sec_skcipher_copy_iv,
1807 	.bd_fill	= sec_skcipher_bd_fill,
1808 	.bd_send	= sec_bd_send,
1809 	.callback	= sec_skcipher_callback,
1810 	.process	= sec_process,
1811 };
1812 
1813 static const struct sec_req_op sec_aead_req_ops = {
1814 	.buf_map	= sec_aead_sgl_map,
1815 	.buf_unmap	= sec_aead_sgl_unmap,
1816 	.do_transfer	= sec_aead_set_iv,
1817 	.bd_fill	= sec_aead_bd_fill,
1818 	.bd_send	= sec_bd_send,
1819 	.callback	= sec_aead_callback,
1820 	.process	= sec_process,
1821 };
1822 
1823 static const struct sec_req_op sec_skcipher_req_ops_v3 = {
1824 	.buf_map	= sec_skcipher_sgl_map,
1825 	.buf_unmap	= sec_skcipher_sgl_unmap,
1826 	.do_transfer	= sec_skcipher_copy_iv,
1827 	.bd_fill	= sec_skcipher_bd_fill_v3,
1828 	.bd_send	= sec_bd_send,
1829 	.callback	= sec_skcipher_callback,
1830 	.process	= sec_process,
1831 };
1832 
1833 static const struct sec_req_op sec_aead_req_ops_v3 = {
1834 	.buf_map	= sec_aead_sgl_map,
1835 	.buf_unmap	= sec_aead_sgl_unmap,
1836 	.do_transfer	= sec_aead_set_iv,
1837 	.bd_fill	= sec_aead_bd_fill_v3,
1838 	.bd_send	= sec_bd_send,
1839 	.callback	= sec_aead_callback,
1840 	.process	= sec_process,
1841 };
1842 
sec_skcipher_ctx_init(struct crypto_skcipher * tfm)1843 static int sec_skcipher_ctx_init(struct crypto_skcipher *tfm)
1844 {
1845 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
1846 	int ret;
1847 
1848 	ret = sec_skcipher_init(tfm);
1849 	if (ret)
1850 		return ret;
1851 
1852 	if (ctx->sec->qm.ver < QM_HW_V3) {
1853 		ctx->type_supported = SEC_BD_TYPE2;
1854 		ctx->req_op = &sec_skcipher_req_ops;
1855 	} else {
1856 		ctx->type_supported = SEC_BD_TYPE3;
1857 		ctx->req_op = &sec_skcipher_req_ops_v3;
1858 	}
1859 
1860 	return ret;
1861 }
1862 
sec_skcipher_ctx_exit(struct crypto_skcipher * tfm)1863 static void sec_skcipher_ctx_exit(struct crypto_skcipher *tfm)
1864 {
1865 	sec_skcipher_uninit(tfm);
1866 }
1867 
sec_aead_init(struct crypto_aead * tfm)1868 static int sec_aead_init(struct crypto_aead *tfm)
1869 {
1870 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1871 	int ret;
1872 
1873 	crypto_aead_set_reqsize(tfm, sizeof(struct sec_req));
1874 	ctx->alg_type = SEC_AEAD;
1875 	ctx->c_ctx.ivsize = crypto_aead_ivsize(tfm);
1876 	if (ctx->c_ctx.ivsize < SEC_AIV_SIZE ||
1877 	    ctx->c_ctx.ivsize > SEC_IV_SIZE) {
1878 		pr_err("get error aead iv size!\n");
1879 		return -EINVAL;
1880 	}
1881 
1882 	ret = sec_ctx_base_init(ctx);
1883 	if (ret)
1884 		return ret;
1885 	if (ctx->sec->qm.ver < QM_HW_V3) {
1886 		ctx->type_supported = SEC_BD_TYPE2;
1887 		ctx->req_op = &sec_aead_req_ops;
1888 	} else {
1889 		ctx->type_supported = SEC_BD_TYPE3;
1890 		ctx->req_op = &sec_aead_req_ops_v3;
1891 	}
1892 
1893 	ret = sec_auth_init(ctx);
1894 	if (ret)
1895 		goto err_auth_init;
1896 
1897 	ret = sec_cipher_init(ctx);
1898 	if (ret)
1899 		goto err_cipher_init;
1900 
1901 	return ret;
1902 
1903 err_cipher_init:
1904 	sec_auth_uninit(ctx);
1905 err_auth_init:
1906 	sec_ctx_base_uninit(ctx);
1907 	return ret;
1908 }
1909 
sec_aead_exit(struct crypto_aead * tfm)1910 static void sec_aead_exit(struct crypto_aead *tfm)
1911 {
1912 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1913 
1914 	sec_cipher_uninit(ctx);
1915 	sec_auth_uninit(ctx);
1916 	sec_ctx_base_uninit(ctx);
1917 }
1918 
sec_aead_ctx_init(struct crypto_aead * tfm,const char * hash_name)1919 static int sec_aead_ctx_init(struct crypto_aead *tfm, const char *hash_name)
1920 {
1921 	struct aead_alg *alg = crypto_aead_alg(tfm);
1922 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1923 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1924 	const char *aead_name = alg->base.cra_name;
1925 	int ret;
1926 
1927 	ret = sec_aead_init(tfm);
1928 	if (ret) {
1929 		pr_err("hisi_sec2: aead init error!\n");
1930 		return ret;
1931 	}
1932 
1933 	a_ctx->hash_tfm = crypto_alloc_shash(hash_name, 0, 0);
1934 	if (IS_ERR(a_ctx->hash_tfm)) {
1935 		dev_err(ctx->dev, "aead alloc shash error!\n");
1936 		sec_aead_exit(tfm);
1937 		return PTR_ERR(a_ctx->hash_tfm);
1938 	}
1939 
1940 	a_ctx->fallback_aead_tfm = crypto_alloc_aead(aead_name, 0,
1941 						     CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
1942 	if (IS_ERR(a_ctx->fallback_aead_tfm)) {
1943 		dev_err(ctx->dev, "aead driver alloc fallback tfm error!\n");
1944 		crypto_free_shash(ctx->a_ctx.hash_tfm);
1945 		sec_aead_exit(tfm);
1946 		return PTR_ERR(a_ctx->fallback_aead_tfm);
1947 	}
1948 
1949 	return 0;
1950 }
1951 
sec_aead_ctx_exit(struct crypto_aead * tfm)1952 static void sec_aead_ctx_exit(struct crypto_aead *tfm)
1953 {
1954 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1955 
1956 	crypto_free_aead(ctx->a_ctx.fallback_aead_tfm);
1957 	crypto_free_shash(ctx->a_ctx.hash_tfm);
1958 	sec_aead_exit(tfm);
1959 }
1960 
sec_aead_xcm_ctx_init(struct crypto_aead * tfm)1961 static int sec_aead_xcm_ctx_init(struct crypto_aead *tfm)
1962 {
1963 	struct aead_alg *alg = crypto_aead_alg(tfm);
1964 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1965 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1966 	const char *aead_name = alg->base.cra_name;
1967 	int ret;
1968 
1969 	ret = sec_aead_init(tfm);
1970 	if (ret) {
1971 		dev_err(ctx->dev, "hisi_sec2: aead xcm init error!\n");
1972 		return ret;
1973 	}
1974 
1975 	a_ctx->fallback_aead_tfm = crypto_alloc_aead(aead_name, 0,
1976 						     CRYPTO_ALG_NEED_FALLBACK |
1977 						     CRYPTO_ALG_ASYNC);
1978 	if (IS_ERR(a_ctx->fallback_aead_tfm)) {
1979 		dev_err(ctx->dev, "aead driver alloc fallback tfm error!\n");
1980 		sec_aead_exit(tfm);
1981 		return PTR_ERR(a_ctx->fallback_aead_tfm);
1982 	}
1983 
1984 	return 0;
1985 }
1986 
sec_aead_xcm_ctx_exit(struct crypto_aead * tfm)1987 static void sec_aead_xcm_ctx_exit(struct crypto_aead *tfm)
1988 {
1989 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1990 
1991 	crypto_free_aead(ctx->a_ctx.fallback_aead_tfm);
1992 	sec_aead_exit(tfm);
1993 }
1994 
sec_aead_sha1_ctx_init(struct crypto_aead * tfm)1995 static int sec_aead_sha1_ctx_init(struct crypto_aead *tfm)
1996 {
1997 	return sec_aead_ctx_init(tfm, "sha1");
1998 }
1999 
sec_aead_sha256_ctx_init(struct crypto_aead * tfm)2000 static int sec_aead_sha256_ctx_init(struct crypto_aead *tfm)
2001 {
2002 	return sec_aead_ctx_init(tfm, "sha256");
2003 }
2004 
sec_aead_sha512_ctx_init(struct crypto_aead * tfm)2005 static int sec_aead_sha512_ctx_init(struct crypto_aead *tfm)
2006 {
2007 	return sec_aead_ctx_init(tfm, "sha512");
2008 }
2009 
sec_skcipher_cryptlen_check(struct sec_ctx * ctx,struct sec_req * sreq)2010 static int sec_skcipher_cryptlen_check(struct sec_ctx *ctx,
2011 	struct sec_req *sreq)
2012 {
2013 	u32 cryptlen = sreq->c_req.sk_req->cryptlen;
2014 	struct device *dev = ctx->dev;
2015 	u8 c_mode = ctx->c_ctx.c_mode;
2016 	int ret = 0;
2017 
2018 	switch (c_mode) {
2019 	case SEC_CMODE_XTS:
2020 		if (unlikely(cryptlen < AES_BLOCK_SIZE)) {
2021 			dev_err(dev, "skcipher XTS mode input length error!\n");
2022 			ret = -EINVAL;
2023 		}
2024 		break;
2025 	case SEC_CMODE_ECB:
2026 	case SEC_CMODE_CBC:
2027 		if (unlikely(cryptlen & (AES_BLOCK_SIZE - 1))) {
2028 			dev_err(dev, "skcipher AES input length error!\n");
2029 			ret = -EINVAL;
2030 		}
2031 		break;
2032 	case SEC_CMODE_CFB:
2033 	case SEC_CMODE_OFB:
2034 	case SEC_CMODE_CTR:
2035 		if (unlikely(ctx->sec->qm.ver < QM_HW_V3)) {
2036 			dev_err(dev, "skcipher HW version error!\n");
2037 			ret = -EINVAL;
2038 		}
2039 		break;
2040 	default:
2041 		ret = -EINVAL;
2042 	}
2043 
2044 	return ret;
2045 }
2046 
sec_skcipher_param_check(struct sec_ctx * ctx,struct sec_req * sreq)2047 static int sec_skcipher_param_check(struct sec_ctx *ctx, struct sec_req *sreq)
2048 {
2049 	struct skcipher_request *sk_req = sreq->c_req.sk_req;
2050 	struct device *dev = ctx->dev;
2051 	u8 c_alg = ctx->c_ctx.c_alg;
2052 
2053 	if (unlikely(!sk_req->src || !sk_req->dst ||
2054 		     sk_req->cryptlen > MAX_INPUT_DATA_LEN)) {
2055 		dev_err(dev, "skcipher input param error!\n");
2056 		return -EINVAL;
2057 	}
2058 	sreq->c_req.c_len = sk_req->cryptlen;
2059 
2060 	if (ctx->pbuf_supported && sk_req->cryptlen <= SEC_PBUF_SZ)
2061 		sreq->use_pbuf = true;
2062 	else
2063 		sreq->use_pbuf = false;
2064 
2065 	if (c_alg == SEC_CALG_3DES) {
2066 		if (unlikely(sk_req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) {
2067 			dev_err(dev, "skcipher 3des input length error!\n");
2068 			return -EINVAL;
2069 		}
2070 		return 0;
2071 	} else if (c_alg == SEC_CALG_AES || c_alg == SEC_CALG_SM4) {
2072 		return sec_skcipher_cryptlen_check(ctx, sreq);
2073 	}
2074 
2075 	dev_err(dev, "skcipher algorithm error!\n");
2076 
2077 	return -EINVAL;
2078 }
2079 
sec_skcipher_soft_crypto(struct sec_ctx * ctx,struct skcipher_request * sreq,bool encrypt)2080 static int sec_skcipher_soft_crypto(struct sec_ctx *ctx,
2081 				    struct skcipher_request *sreq, bool encrypt)
2082 {
2083 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
2084 	SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, c_ctx->fbtfm);
2085 	struct device *dev = ctx->dev;
2086 	int ret;
2087 
2088 	if (!c_ctx->fbtfm) {
2089 		dev_err_ratelimited(dev, "the soft tfm isn't supported in the current system.\n");
2090 		return -EINVAL;
2091 	}
2092 
2093 	skcipher_request_set_sync_tfm(subreq, c_ctx->fbtfm);
2094 
2095 	/* software need sync mode to do crypto */
2096 	skcipher_request_set_callback(subreq, sreq->base.flags,
2097 				      NULL, NULL);
2098 	skcipher_request_set_crypt(subreq, sreq->src, sreq->dst,
2099 				   sreq->cryptlen, sreq->iv);
2100 	if (encrypt)
2101 		ret = crypto_skcipher_encrypt(subreq);
2102 	else
2103 		ret = crypto_skcipher_decrypt(subreq);
2104 
2105 	skcipher_request_zero(subreq);
2106 
2107 	return ret;
2108 }
2109 
sec_skcipher_crypto(struct skcipher_request * sk_req,bool encrypt)2110 static int sec_skcipher_crypto(struct skcipher_request *sk_req, bool encrypt)
2111 {
2112 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(sk_req);
2113 	struct sec_req *req = skcipher_request_ctx(sk_req);
2114 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
2115 	int ret;
2116 
2117 	if (!sk_req->cryptlen) {
2118 		if (ctx->c_ctx.c_mode == SEC_CMODE_XTS)
2119 			return -EINVAL;
2120 		return 0;
2121 	}
2122 
2123 	req->flag = sk_req->base.flags;
2124 	req->c_req.sk_req = sk_req;
2125 	req->c_req.encrypt = encrypt;
2126 	req->ctx = ctx;
2127 
2128 	ret = sec_skcipher_param_check(ctx, req);
2129 	if (unlikely(ret))
2130 		return -EINVAL;
2131 
2132 	if (unlikely(ctx->c_ctx.fallback))
2133 		return sec_skcipher_soft_crypto(ctx, sk_req, encrypt);
2134 
2135 	return ctx->req_op->process(ctx, req);
2136 }
2137 
sec_skcipher_encrypt(struct skcipher_request * sk_req)2138 static int sec_skcipher_encrypt(struct skcipher_request *sk_req)
2139 {
2140 	return sec_skcipher_crypto(sk_req, true);
2141 }
2142 
sec_skcipher_decrypt(struct skcipher_request * sk_req)2143 static int sec_skcipher_decrypt(struct skcipher_request *sk_req)
2144 {
2145 	return sec_skcipher_crypto(sk_req, false);
2146 }
2147 
2148 #define SEC_SKCIPHER_GEN_ALG(sec_cra_name, sec_set_key, sec_min_key_size, \
2149 	sec_max_key_size, ctx_init, ctx_exit, blk_size, iv_size)\
2150 {\
2151 	.base = {\
2152 		.cra_name = sec_cra_name,\
2153 		.cra_driver_name = "hisi_sec_"sec_cra_name,\
2154 		.cra_priority = SEC_PRIORITY,\
2155 		.cra_flags = CRYPTO_ALG_ASYNC |\
2156 		 CRYPTO_ALG_NEED_FALLBACK,\
2157 		.cra_blocksize = blk_size,\
2158 		.cra_ctxsize = sizeof(struct sec_ctx),\
2159 		.cra_module = THIS_MODULE,\
2160 	},\
2161 	.init = ctx_init,\
2162 	.exit = ctx_exit,\
2163 	.setkey = sec_set_key,\
2164 	.decrypt = sec_skcipher_decrypt,\
2165 	.encrypt = sec_skcipher_encrypt,\
2166 	.min_keysize = sec_min_key_size,\
2167 	.max_keysize = sec_max_key_size,\
2168 	.ivsize = iv_size,\
2169 }
2170 
2171 #define SEC_SKCIPHER_ALG(name, key_func, min_key_size, \
2172 	max_key_size, blk_size, iv_size) \
2173 	SEC_SKCIPHER_GEN_ALG(name, key_func, min_key_size, max_key_size, \
2174 	sec_skcipher_ctx_init, sec_skcipher_ctx_exit, blk_size, iv_size)
2175 
2176 static struct sec_skcipher sec_skciphers[] = {
2177 	{
2178 		.alg_msk = BIT(0),
2179 		.alg = SEC_SKCIPHER_ALG("ecb(aes)", sec_setkey_aes_ecb, AES_MIN_KEY_SIZE,
2180 					AES_MAX_KEY_SIZE, AES_BLOCK_SIZE, 0),
2181 	},
2182 	{
2183 		.alg_msk = BIT(1),
2184 		.alg = SEC_SKCIPHER_ALG("cbc(aes)", sec_setkey_aes_cbc, AES_MIN_KEY_SIZE,
2185 					AES_MAX_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2186 	},
2187 	{
2188 		.alg_msk = BIT(2),
2189 		.alg = SEC_SKCIPHER_ALG("ctr(aes)", sec_setkey_aes_ctr,	AES_MIN_KEY_SIZE,
2190 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2191 	},
2192 	{
2193 		.alg_msk = BIT(3),
2194 		.alg = SEC_SKCIPHER_ALG("xts(aes)", sec_setkey_aes_xts,	SEC_XTS_MIN_KEY_SIZE,
2195 					SEC_XTS_MAX_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2196 	},
2197 	{
2198 		.alg_msk = BIT(4),
2199 		.alg = SEC_SKCIPHER_ALG("ofb(aes)", sec_setkey_aes_ofb,	AES_MIN_KEY_SIZE,
2200 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2201 	},
2202 	{
2203 		.alg_msk = BIT(5),
2204 		.alg = SEC_SKCIPHER_ALG("cfb(aes)", sec_setkey_aes_cfb,	AES_MIN_KEY_SIZE,
2205 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2206 	},
2207 	{
2208 		.alg_msk = BIT(12),
2209 		.alg = SEC_SKCIPHER_ALG("cbc(sm4)", sec_setkey_sm4_cbc,	AES_MIN_KEY_SIZE,
2210 					AES_MIN_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2211 	},
2212 	{
2213 		.alg_msk = BIT(13),
2214 		.alg = SEC_SKCIPHER_ALG("ctr(sm4)", sec_setkey_sm4_ctr, AES_MIN_KEY_SIZE,
2215 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2216 	},
2217 	{
2218 		.alg_msk = BIT(14),
2219 		.alg = SEC_SKCIPHER_ALG("xts(sm4)", sec_setkey_sm4_xts,	SEC_XTS_MIN_KEY_SIZE,
2220 					SEC_XTS_MIN_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2221 	},
2222 	{
2223 		.alg_msk = BIT(15),
2224 		.alg = SEC_SKCIPHER_ALG("ofb(sm4)", sec_setkey_sm4_ofb,	AES_MIN_KEY_SIZE,
2225 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2226 	},
2227 	{
2228 		.alg_msk = BIT(16),
2229 		.alg = SEC_SKCIPHER_ALG("cfb(sm4)", sec_setkey_sm4_cfb,	AES_MIN_KEY_SIZE,
2230 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2231 	},
2232 	{
2233 		.alg_msk = BIT(23),
2234 		.alg = SEC_SKCIPHER_ALG("ecb(des3_ede)", sec_setkey_3des_ecb, SEC_DES3_3KEY_SIZE,
2235 					SEC_DES3_3KEY_SIZE, DES3_EDE_BLOCK_SIZE, 0),
2236 	},
2237 	{
2238 		.alg_msk = BIT(24),
2239 		.alg = SEC_SKCIPHER_ALG("cbc(des3_ede)", sec_setkey_3des_cbc, SEC_DES3_3KEY_SIZE,
2240 					SEC_DES3_3KEY_SIZE, DES3_EDE_BLOCK_SIZE,
2241 					DES3_EDE_BLOCK_SIZE),
2242 	},
2243 };
2244 
aead_iv_demension_check(struct aead_request * aead_req)2245 static int aead_iv_demension_check(struct aead_request *aead_req)
2246 {
2247 	u8 cl;
2248 
2249 	cl = aead_req->iv[0] + 1;
2250 	if (cl < IV_CL_MIN || cl > IV_CL_MAX)
2251 		return -EINVAL;
2252 
2253 	if (cl < IV_CL_MID && aead_req->cryptlen >> (BYTE_BITS * cl))
2254 		return -EOVERFLOW;
2255 
2256 	return 0;
2257 }
2258 
sec_aead_spec_check(struct sec_ctx * ctx,struct sec_req * sreq)2259 static int sec_aead_spec_check(struct sec_ctx *ctx, struct sec_req *sreq)
2260 {
2261 	struct aead_request *req = sreq->aead_req.aead_req;
2262 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2263 	size_t sz = crypto_aead_authsize(tfm);
2264 	u8 c_mode = ctx->c_ctx.c_mode;
2265 	struct device *dev = ctx->dev;
2266 	int ret;
2267 
2268 	/* Hardware does not handle cases where authsize is less than 4 bytes */
2269 	if (unlikely(sz < MIN_MAC_LEN)) {
2270 		sreq->aead_req.fallback = true;
2271 		return -EINVAL;
2272 	}
2273 
2274 	if (unlikely(req->cryptlen + req->assoclen > MAX_INPUT_DATA_LEN ||
2275 	    req->assoclen > SEC_MAX_AAD_LEN)) {
2276 		dev_err(dev, "aead input spec error!\n");
2277 		return -EINVAL;
2278 	}
2279 
2280 	if (c_mode == SEC_CMODE_CCM) {
2281 		if (unlikely(req->assoclen > SEC_MAX_CCM_AAD_LEN)) {
2282 			dev_err_ratelimited(dev, "CCM input aad parameter is too long!\n");
2283 			return -EINVAL;
2284 		}
2285 		ret = aead_iv_demension_check(req);
2286 		if (ret) {
2287 			dev_err(dev, "aead input iv param error!\n");
2288 			return ret;
2289 		}
2290 	}
2291 
2292 	if (sreq->c_req.encrypt)
2293 		sreq->c_req.c_len = req->cryptlen;
2294 	else
2295 		sreq->c_req.c_len = req->cryptlen - sz;
2296 	if (c_mode == SEC_CMODE_CBC) {
2297 		if (unlikely(sreq->c_req.c_len & (AES_BLOCK_SIZE - 1))) {
2298 			dev_err(dev, "aead crypto length error!\n");
2299 			return -EINVAL;
2300 		}
2301 	}
2302 
2303 	return 0;
2304 }
2305 
sec_aead_param_check(struct sec_ctx * ctx,struct sec_req * sreq)2306 static int sec_aead_param_check(struct sec_ctx *ctx, struct sec_req *sreq)
2307 {
2308 	struct aead_request *req = sreq->aead_req.aead_req;
2309 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2310 	size_t authsize = crypto_aead_authsize(tfm);
2311 	struct device *dev = ctx->dev;
2312 	u8 c_alg = ctx->c_ctx.c_alg;
2313 
2314 	if (unlikely(!req->src || !req->dst)) {
2315 		dev_err(dev, "aead input param error!\n");
2316 		return -EINVAL;
2317 	}
2318 
2319 	if (ctx->sec->qm.ver == QM_HW_V2) {
2320 		if (unlikely(!req->cryptlen || (!sreq->c_req.encrypt &&
2321 			     req->cryptlen <= authsize))) {
2322 			sreq->aead_req.fallback = true;
2323 			return -EINVAL;
2324 		}
2325 	}
2326 
2327 	/* Support AES or SM4 */
2328 	if (unlikely(c_alg != SEC_CALG_AES && c_alg != SEC_CALG_SM4)) {
2329 		dev_err(dev, "aead crypto alg error!\n");
2330 		return -EINVAL;
2331 	}
2332 
2333 	if (unlikely(sec_aead_spec_check(ctx, sreq)))
2334 		return -EINVAL;
2335 
2336 	if (ctx->pbuf_supported && (req->cryptlen + req->assoclen) <=
2337 		SEC_PBUF_SZ)
2338 		sreq->use_pbuf = true;
2339 	else
2340 		sreq->use_pbuf = false;
2341 
2342 	return 0;
2343 }
2344 
sec_aead_soft_crypto(struct sec_ctx * ctx,struct aead_request * aead_req,bool encrypt)2345 static int sec_aead_soft_crypto(struct sec_ctx *ctx,
2346 				struct aead_request *aead_req,
2347 				bool encrypt)
2348 {
2349 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
2350 	struct aead_request *subreq;
2351 	int ret;
2352 
2353 	subreq = aead_request_alloc(a_ctx->fallback_aead_tfm, GFP_KERNEL);
2354 	if (!subreq)
2355 		return -ENOMEM;
2356 
2357 	aead_request_set_tfm(subreq, a_ctx->fallback_aead_tfm);
2358 	aead_request_set_callback(subreq, aead_req->base.flags,
2359 				  aead_req->base.complete, aead_req->base.data);
2360 	aead_request_set_crypt(subreq, aead_req->src, aead_req->dst,
2361 			       aead_req->cryptlen, aead_req->iv);
2362 	aead_request_set_ad(subreq, aead_req->assoclen);
2363 
2364 	if (encrypt)
2365 		ret = crypto_aead_encrypt(subreq);
2366 	else
2367 		ret = crypto_aead_decrypt(subreq);
2368 	aead_request_free(subreq);
2369 
2370 	return ret;
2371 }
2372 
sec_aead_crypto(struct aead_request * a_req,bool encrypt)2373 static int sec_aead_crypto(struct aead_request *a_req, bool encrypt)
2374 {
2375 	struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
2376 	struct sec_req *req = aead_request_ctx(a_req);
2377 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
2378 	int ret;
2379 
2380 	req->flag = a_req->base.flags;
2381 	req->aead_req.aead_req = a_req;
2382 	req->c_req.encrypt = encrypt;
2383 	req->ctx = ctx;
2384 	req->aead_req.fallback = false;
2385 
2386 	ret = sec_aead_param_check(ctx, req);
2387 	if (unlikely(ret)) {
2388 		if (req->aead_req.fallback)
2389 			return sec_aead_soft_crypto(ctx, a_req, encrypt);
2390 		return -EINVAL;
2391 	}
2392 
2393 	return ctx->req_op->process(ctx, req);
2394 }
2395 
sec_aead_encrypt(struct aead_request * a_req)2396 static int sec_aead_encrypt(struct aead_request *a_req)
2397 {
2398 	return sec_aead_crypto(a_req, true);
2399 }
2400 
sec_aead_decrypt(struct aead_request * a_req)2401 static int sec_aead_decrypt(struct aead_request *a_req)
2402 {
2403 	return sec_aead_crypto(a_req, false);
2404 }
2405 
2406 #define SEC_AEAD_ALG(sec_cra_name, sec_set_key, ctx_init,\
2407 			 ctx_exit, blk_size, iv_size, max_authsize)\
2408 {\
2409 	.base = {\
2410 		.cra_name = sec_cra_name,\
2411 		.cra_driver_name = "hisi_sec_"sec_cra_name,\
2412 		.cra_priority = SEC_PRIORITY,\
2413 		.cra_flags = CRYPTO_ALG_ASYNC |\
2414 		 CRYPTO_ALG_NEED_FALLBACK,\
2415 		.cra_blocksize = blk_size,\
2416 		.cra_ctxsize = sizeof(struct sec_ctx),\
2417 		.cra_module = THIS_MODULE,\
2418 	},\
2419 	.init = ctx_init,\
2420 	.exit = ctx_exit,\
2421 	.setkey = sec_set_key,\
2422 	.setauthsize = sec_aead_setauthsize,\
2423 	.decrypt = sec_aead_decrypt,\
2424 	.encrypt = sec_aead_encrypt,\
2425 	.ivsize = iv_size,\
2426 	.maxauthsize = max_authsize,\
2427 }
2428 
2429 static struct sec_aead sec_aeads[] = {
2430 	{
2431 		.alg_msk = BIT(6),
2432 		.alg = SEC_AEAD_ALG("ccm(aes)", sec_setkey_aes_ccm, sec_aead_xcm_ctx_init,
2433 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE,
2434 				    AES_BLOCK_SIZE),
2435 	},
2436 	{
2437 		.alg_msk = BIT(7),
2438 		.alg = SEC_AEAD_ALG("gcm(aes)", sec_setkey_aes_gcm, sec_aead_xcm_ctx_init,
2439 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, SEC_AIV_SIZE,
2440 				    AES_BLOCK_SIZE),
2441 	},
2442 	{
2443 		.alg_msk = BIT(17),
2444 		.alg = SEC_AEAD_ALG("ccm(sm4)", sec_setkey_sm4_ccm, sec_aead_xcm_ctx_init,
2445 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE,
2446 				    AES_BLOCK_SIZE),
2447 	},
2448 	{
2449 		.alg_msk = BIT(18),
2450 		.alg = SEC_AEAD_ALG("gcm(sm4)", sec_setkey_sm4_gcm, sec_aead_xcm_ctx_init,
2451 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, SEC_AIV_SIZE,
2452 				    AES_BLOCK_SIZE),
2453 	},
2454 	{
2455 		.alg_msk = BIT(43),
2456 		.alg = SEC_AEAD_ALG("authenc(hmac(sha1),cbc(aes))", sec_setkey_aes_cbc_sha1,
2457 				    sec_aead_sha1_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2458 				    AES_BLOCK_SIZE, SHA1_DIGEST_SIZE),
2459 	},
2460 	{
2461 		.alg_msk = BIT(44),
2462 		.alg = SEC_AEAD_ALG("authenc(hmac(sha256),cbc(aes))", sec_setkey_aes_cbc_sha256,
2463 				    sec_aead_sha256_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2464 				    AES_BLOCK_SIZE, SHA256_DIGEST_SIZE),
2465 	},
2466 	{
2467 		.alg_msk = BIT(45),
2468 		.alg = SEC_AEAD_ALG("authenc(hmac(sha512),cbc(aes))", sec_setkey_aes_cbc_sha512,
2469 				    sec_aead_sha512_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2470 				    AES_BLOCK_SIZE, SHA512_DIGEST_SIZE),
2471 	},
2472 };
2473 
sec_unregister_skcipher(u64 alg_mask,int end)2474 static void sec_unregister_skcipher(u64 alg_mask, int end)
2475 {
2476 	int i;
2477 
2478 	for (i = 0; i < end; i++)
2479 		if (sec_skciphers[i].alg_msk & alg_mask)
2480 			crypto_unregister_skcipher(&sec_skciphers[i].alg);
2481 }
2482 
sec_register_skcipher(u64 alg_mask)2483 static int sec_register_skcipher(u64 alg_mask)
2484 {
2485 	int i, ret, count;
2486 
2487 	count = ARRAY_SIZE(sec_skciphers);
2488 
2489 	for (i = 0; i < count; i++) {
2490 		if (!(sec_skciphers[i].alg_msk & alg_mask))
2491 			continue;
2492 
2493 		ret = crypto_register_skcipher(&sec_skciphers[i].alg);
2494 		if (ret)
2495 			goto err;
2496 	}
2497 
2498 	return 0;
2499 
2500 err:
2501 	sec_unregister_skcipher(alg_mask, i);
2502 
2503 	return ret;
2504 }
2505 
sec_unregister_aead(u64 alg_mask,int end)2506 static void sec_unregister_aead(u64 alg_mask, int end)
2507 {
2508 	int i;
2509 
2510 	for (i = 0; i < end; i++)
2511 		if (sec_aeads[i].alg_msk & alg_mask)
2512 			crypto_unregister_aead(&sec_aeads[i].alg);
2513 }
2514 
sec_register_aead(u64 alg_mask)2515 static int sec_register_aead(u64 alg_mask)
2516 {
2517 	int i, ret, count;
2518 
2519 	count = ARRAY_SIZE(sec_aeads);
2520 
2521 	for (i = 0; i < count; i++) {
2522 		if (!(sec_aeads[i].alg_msk & alg_mask))
2523 			continue;
2524 
2525 		ret = crypto_register_aead(&sec_aeads[i].alg);
2526 		if (ret)
2527 			goto err;
2528 	}
2529 
2530 	return 0;
2531 
2532 err:
2533 	sec_unregister_aead(alg_mask, i);
2534 
2535 	return ret;
2536 }
2537 
sec_register_to_crypto(struct hisi_qm * qm)2538 int sec_register_to_crypto(struct hisi_qm *qm)
2539 {
2540 	u64 alg_mask;
2541 	int ret = 0;
2542 
2543 	alg_mask = sec_get_alg_bitmap(qm, SEC_DRV_ALG_BITMAP_HIGH_IDX,
2544 				      SEC_DRV_ALG_BITMAP_LOW_IDX);
2545 
2546 
2547 	ret = sec_register_skcipher(alg_mask);
2548 	if (ret)
2549 		return ret;
2550 
2551 	ret = sec_register_aead(alg_mask);
2552 	if (ret)
2553 		sec_unregister_skcipher(alg_mask, ARRAY_SIZE(sec_skciphers));
2554 
2555 	return ret;
2556 }
2557 
sec_unregister_from_crypto(struct hisi_qm * qm)2558 void sec_unregister_from_crypto(struct hisi_qm *qm)
2559 {
2560 	u64 alg_mask;
2561 
2562 	alg_mask = sec_get_alg_bitmap(qm, SEC_DRV_ALG_BITMAP_HIGH_IDX,
2563 				      SEC_DRV_ALG_BITMAP_LOW_IDX);
2564 
2565 	sec_unregister_aead(alg_mask, ARRAY_SIZE(sec_aeads));
2566 	sec_unregister_skcipher(alg_mask, ARRAY_SIZE(sec_skciphers));
2567 }
2568