xref: /linux/drivers/crypto/qce/aead.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
19363efb4SThara Gopinath // SPDX-License-Identifier: GPL-2.0-only
29363efb4SThara Gopinath 
39363efb4SThara Gopinath /*
49363efb4SThara Gopinath  * Copyright (C) 2021, Linaro Limited. All rights reserved.
59363efb4SThara Gopinath  */
69363efb4SThara Gopinath #include <linux/dma-mapping.h>
79363efb4SThara Gopinath #include <linux/interrupt.h>
89363efb4SThara Gopinath #include <crypto/gcm.h>
99363efb4SThara Gopinath #include <crypto/authenc.h>
109363efb4SThara Gopinath #include <crypto/internal/aead.h>
119363efb4SThara Gopinath #include <crypto/internal/des.h>
129363efb4SThara Gopinath #include <crypto/sha1.h>
139363efb4SThara Gopinath #include <crypto/sha2.h>
149363efb4SThara Gopinath #include <crypto/scatterwalk.h>
159363efb4SThara Gopinath #include "aead.h"
169363efb4SThara Gopinath 
179363efb4SThara Gopinath #define CCM_NONCE_ADATA_SHIFT		6
189363efb4SThara Gopinath #define CCM_NONCE_AUTHSIZE_SHIFT	3
199363efb4SThara Gopinath #define MAX_CCM_ADATA_HEADER_LEN        6
209363efb4SThara Gopinath 
219363efb4SThara Gopinath static LIST_HEAD(aead_algs);
229363efb4SThara Gopinath 
qce_aead_done(void * data)239363efb4SThara Gopinath static void qce_aead_done(void *data)
249363efb4SThara Gopinath {
259363efb4SThara Gopinath 	struct crypto_async_request *async_req = data;
269363efb4SThara Gopinath 	struct aead_request *req = aead_request_cast(async_req);
27*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
289363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
299363efb4SThara Gopinath 	struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
309363efb4SThara Gopinath 	struct qce_device *qce = tmpl->qce;
319363efb4SThara Gopinath 	struct qce_result_dump *result_buf = qce->dma.result_buf;
329363efb4SThara Gopinath 	enum dma_data_direction dir_src, dir_dst;
339363efb4SThara Gopinath 	bool diff_dst;
349363efb4SThara Gopinath 	int error;
359363efb4SThara Gopinath 	u32 status;
369363efb4SThara Gopinath 	unsigned int totallen;
379363efb4SThara Gopinath 	unsigned char tag[SHA256_DIGEST_SIZE] = {0};
389363efb4SThara Gopinath 	int ret = 0;
399363efb4SThara Gopinath 
409363efb4SThara Gopinath 	diff_dst = (req->src != req->dst) ? true : false;
419363efb4SThara Gopinath 	dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
429363efb4SThara Gopinath 	dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
439363efb4SThara Gopinath 
449363efb4SThara Gopinath 	error = qce_dma_terminate_all(&qce->dma);
459363efb4SThara Gopinath 	if (error)
469363efb4SThara Gopinath 		dev_dbg(qce->dev, "aead dma termination error (%d)\n",
479363efb4SThara Gopinath 			error);
489363efb4SThara Gopinath 	if (diff_dst)
499363efb4SThara Gopinath 		dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
509363efb4SThara Gopinath 
519363efb4SThara Gopinath 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
529363efb4SThara Gopinath 
539363efb4SThara Gopinath 	if (IS_CCM(rctx->flags)) {
549363efb4SThara Gopinath 		if (req->assoclen) {
559363efb4SThara Gopinath 			sg_free_table(&rctx->src_tbl);
569363efb4SThara Gopinath 			if (diff_dst)
579363efb4SThara Gopinath 				sg_free_table(&rctx->dst_tbl);
589363efb4SThara Gopinath 		} else {
599363efb4SThara Gopinath 			if (!(IS_DECRYPT(rctx->flags) && !diff_dst))
609363efb4SThara Gopinath 				sg_free_table(&rctx->dst_tbl);
619363efb4SThara Gopinath 		}
629363efb4SThara Gopinath 	} else {
639363efb4SThara Gopinath 		sg_free_table(&rctx->dst_tbl);
649363efb4SThara Gopinath 	}
659363efb4SThara Gopinath 
669363efb4SThara Gopinath 	error = qce_check_status(qce, &status);
679363efb4SThara Gopinath 	if (error < 0 && (error != -EBADMSG))
689363efb4SThara Gopinath 		dev_err(qce->dev, "aead operation error (%x)\n", status);
699363efb4SThara Gopinath 
709363efb4SThara Gopinath 	if (IS_ENCRYPT(rctx->flags)) {
719363efb4SThara Gopinath 		totallen = req->cryptlen + req->assoclen;
729363efb4SThara Gopinath 		if (IS_CCM(rctx->flags))
739363efb4SThara Gopinath 			scatterwalk_map_and_copy(rctx->ccmresult_buf, req->dst,
749363efb4SThara Gopinath 						 totallen, ctx->authsize, 1);
759363efb4SThara Gopinath 		else
769363efb4SThara Gopinath 			scatterwalk_map_and_copy(result_buf->auth_iv, req->dst,
779363efb4SThara Gopinath 						 totallen, ctx->authsize, 1);
789363efb4SThara Gopinath 
799363efb4SThara Gopinath 	} else if (!IS_CCM(rctx->flags)) {
809363efb4SThara Gopinath 		totallen = req->cryptlen + req->assoclen - ctx->authsize;
819363efb4SThara Gopinath 		scatterwalk_map_and_copy(tag, req->src, totallen, ctx->authsize, 0);
829363efb4SThara Gopinath 		ret = memcmp(result_buf->auth_iv, tag, ctx->authsize);
839363efb4SThara Gopinath 		if (ret) {
849363efb4SThara Gopinath 			pr_err("Bad message error\n");
859363efb4SThara Gopinath 			error = -EBADMSG;
869363efb4SThara Gopinath 		}
879363efb4SThara Gopinath 	}
889363efb4SThara Gopinath 
899363efb4SThara Gopinath 	qce->async_req_done(qce, error);
909363efb4SThara Gopinath }
919363efb4SThara Gopinath 
929363efb4SThara Gopinath static struct scatterlist *
qce_aead_prepare_result_buf(struct sg_table * tbl,struct aead_request * req)939363efb4SThara Gopinath qce_aead_prepare_result_buf(struct sg_table *tbl, struct aead_request *req)
949363efb4SThara Gopinath {
95*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
969363efb4SThara Gopinath 	struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
979363efb4SThara Gopinath 	struct qce_device *qce = tmpl->qce;
989363efb4SThara Gopinath 
999363efb4SThara Gopinath 	sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
1009363efb4SThara Gopinath 	return qce_sgtable_add(tbl, &rctx->result_sg, QCE_RESULT_BUF_SZ);
1019363efb4SThara Gopinath }
1029363efb4SThara Gopinath 
1039363efb4SThara Gopinath static struct scatterlist *
qce_aead_prepare_ccm_result_buf(struct sg_table * tbl,struct aead_request * req)1049363efb4SThara Gopinath qce_aead_prepare_ccm_result_buf(struct sg_table *tbl, struct aead_request *req)
1059363efb4SThara Gopinath {
106*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
1079363efb4SThara Gopinath 
1089363efb4SThara Gopinath 	sg_init_one(&rctx->result_sg, rctx->ccmresult_buf, QCE_BAM_BURST_SIZE);
1099363efb4SThara Gopinath 	return qce_sgtable_add(tbl, &rctx->result_sg, QCE_BAM_BURST_SIZE);
1109363efb4SThara Gopinath }
1119363efb4SThara Gopinath 
1129363efb4SThara Gopinath static struct scatterlist *
qce_aead_prepare_dst_buf(struct aead_request * req)1139363efb4SThara Gopinath qce_aead_prepare_dst_buf(struct aead_request *req)
1149363efb4SThara Gopinath {
115*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
1169363efb4SThara Gopinath 	struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
1179363efb4SThara Gopinath 	struct qce_device *qce = tmpl->qce;
1189363efb4SThara Gopinath 	struct scatterlist *sg, *msg_sg, __sg[2];
1199363efb4SThara Gopinath 	gfp_t gfp;
1209363efb4SThara Gopinath 	unsigned int assoclen = req->assoclen;
1219363efb4SThara Gopinath 	unsigned int totallen;
1229363efb4SThara Gopinath 	int ret;
1239363efb4SThara Gopinath 
1249363efb4SThara Gopinath 	totallen = rctx->cryptlen + assoclen;
1259363efb4SThara Gopinath 	rctx->dst_nents = sg_nents_for_len(req->dst, totallen);
1269363efb4SThara Gopinath 	if (rctx->dst_nents < 0) {
1279363efb4SThara Gopinath 		dev_err(qce->dev, "Invalid numbers of dst SG.\n");
1289363efb4SThara Gopinath 		return ERR_PTR(-EINVAL);
1299363efb4SThara Gopinath 	}
1309363efb4SThara Gopinath 	if (IS_CCM(rctx->flags))
1319363efb4SThara Gopinath 		rctx->dst_nents += 2;
1329363efb4SThara Gopinath 	else
1339363efb4SThara Gopinath 		rctx->dst_nents += 1;
1349363efb4SThara Gopinath 
1359363efb4SThara Gopinath 	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1369363efb4SThara Gopinath 						GFP_KERNEL : GFP_ATOMIC;
1379363efb4SThara Gopinath 	ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
1389363efb4SThara Gopinath 	if (ret)
1399363efb4SThara Gopinath 		return ERR_PTR(ret);
1409363efb4SThara Gopinath 
1419363efb4SThara Gopinath 	if (IS_CCM(rctx->flags) && assoclen) {
1429363efb4SThara Gopinath 		/* Get the dst buffer */
1439363efb4SThara Gopinath 		msg_sg = scatterwalk_ffwd(__sg, req->dst, assoclen);
1449363efb4SThara Gopinath 
1459363efb4SThara Gopinath 		sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->adata_sg,
1469363efb4SThara Gopinath 				     rctx->assoclen);
1479363efb4SThara Gopinath 		if (IS_ERR(sg)) {
1489363efb4SThara Gopinath 			ret = PTR_ERR(sg);
1499363efb4SThara Gopinath 			goto dst_tbl_free;
1509363efb4SThara Gopinath 		}
1519363efb4SThara Gopinath 		/* dst buffer */
1529363efb4SThara Gopinath 		sg = qce_sgtable_add(&rctx->dst_tbl, msg_sg, rctx->cryptlen);
1539363efb4SThara Gopinath 		if (IS_ERR(sg)) {
1549363efb4SThara Gopinath 			ret = PTR_ERR(sg);
1559363efb4SThara Gopinath 			goto dst_tbl_free;
1569363efb4SThara Gopinath 		}
1579363efb4SThara Gopinath 		totallen = rctx->cryptlen + rctx->assoclen;
1589363efb4SThara Gopinath 	} else {
1599363efb4SThara Gopinath 		if (totallen) {
1609363efb4SThara Gopinath 			sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, totallen);
1619363efb4SThara Gopinath 			if (IS_ERR(sg))
1629363efb4SThara Gopinath 				goto dst_tbl_free;
1639363efb4SThara Gopinath 		}
1649363efb4SThara Gopinath 	}
1659363efb4SThara Gopinath 	if (IS_CCM(rctx->flags))
1669363efb4SThara Gopinath 		sg = qce_aead_prepare_ccm_result_buf(&rctx->dst_tbl, req);
1679363efb4SThara Gopinath 	else
1689363efb4SThara Gopinath 		sg = qce_aead_prepare_result_buf(&rctx->dst_tbl, req);
1699363efb4SThara Gopinath 
1709363efb4SThara Gopinath 	if (IS_ERR(sg))
1719363efb4SThara Gopinath 		goto dst_tbl_free;
1729363efb4SThara Gopinath 
1739363efb4SThara Gopinath 	sg_mark_end(sg);
1749363efb4SThara Gopinath 	rctx->dst_sg = rctx->dst_tbl.sgl;
1759363efb4SThara Gopinath 	rctx->dst_nents = sg_nents_for_len(rctx->dst_sg, totallen) + 1;
1769363efb4SThara Gopinath 
1779363efb4SThara Gopinath 	return sg;
1789363efb4SThara Gopinath 
1799363efb4SThara Gopinath dst_tbl_free:
1809363efb4SThara Gopinath 	sg_free_table(&rctx->dst_tbl);
1819363efb4SThara Gopinath 	return sg;
1829363efb4SThara Gopinath }
1839363efb4SThara Gopinath 
1849363efb4SThara Gopinath static int
qce_aead_ccm_prepare_buf_assoclen(struct aead_request * req)1859363efb4SThara Gopinath qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
1869363efb4SThara Gopinath {
1879363efb4SThara Gopinath 	struct scatterlist *sg, *msg_sg, __sg[2];
1889363efb4SThara Gopinath 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
189*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
1909363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
1919363efb4SThara Gopinath 	unsigned int assoclen = rctx->assoclen;
1929363efb4SThara Gopinath 	unsigned int adata_header_len, cryptlen, totallen;
1939363efb4SThara Gopinath 	gfp_t gfp;
1949363efb4SThara Gopinath 	bool diff_dst;
1959363efb4SThara Gopinath 	int ret;
1969363efb4SThara Gopinath 
1979363efb4SThara Gopinath 	if (IS_DECRYPT(rctx->flags))
1989363efb4SThara Gopinath 		cryptlen = rctx->cryptlen + ctx->authsize;
1999363efb4SThara Gopinath 	else
2009363efb4SThara Gopinath 		cryptlen = rctx->cryptlen;
2019363efb4SThara Gopinath 	totallen = cryptlen + req->assoclen;
2029363efb4SThara Gopinath 
2039363efb4SThara Gopinath 	/* Get the msg */
2049363efb4SThara Gopinath 	msg_sg = scatterwalk_ffwd(__sg, req->src, req->assoclen);
2059363efb4SThara Gopinath 
2069363efb4SThara Gopinath 	rctx->adata = kzalloc((ALIGN(assoclen, 16) + MAX_CCM_ADATA_HEADER_LEN) *
2079363efb4SThara Gopinath 			       sizeof(unsigned char), GFP_ATOMIC);
2089363efb4SThara Gopinath 	if (!rctx->adata)
2099363efb4SThara Gopinath 		return -ENOMEM;
2109363efb4SThara Gopinath 
2119363efb4SThara Gopinath 	/*
2129363efb4SThara Gopinath 	 * Format associated data (RFC3610 and NIST 800-38C)
2139363efb4SThara Gopinath 	 * Even though specification allows for AAD to be up to 2^64 - 1 bytes,
2149363efb4SThara Gopinath 	 * the assoclen field in aead_request is unsigned int and thus limits
2159363efb4SThara Gopinath 	 * the AAD to be up to 2^32 - 1 bytes. So we handle only two scenarios
2169363efb4SThara Gopinath 	 * while forming the header for AAD.
2179363efb4SThara Gopinath 	 */
2189363efb4SThara Gopinath 	if (assoclen < 0xff00) {
2199363efb4SThara Gopinath 		adata_header_len = 2;
2209363efb4SThara Gopinath 		*(__be16 *)rctx->adata = cpu_to_be16(assoclen);
2219363efb4SThara Gopinath 	} else {
2229363efb4SThara Gopinath 		adata_header_len = 6;
2239363efb4SThara Gopinath 		*(__be16 *)rctx->adata = cpu_to_be16(0xfffe);
2249363efb4SThara Gopinath 		*(__be32 *)(rctx->adata + 2) = cpu_to_be32(assoclen);
2259363efb4SThara Gopinath 	}
2269363efb4SThara Gopinath 
2279363efb4SThara Gopinath 	/* Copy the associated data */
2289363efb4SThara Gopinath 	if (sg_copy_to_buffer(req->src, sg_nents_for_len(req->src, assoclen),
2299363efb4SThara Gopinath 			      rctx->adata + adata_header_len,
2309363efb4SThara Gopinath 			      assoclen) != assoclen)
2319363efb4SThara Gopinath 		return -EINVAL;
2329363efb4SThara Gopinath 
2339363efb4SThara Gopinath 	/* Pad associated data to block size */
2349363efb4SThara Gopinath 	rctx->assoclen = ALIGN(assoclen + adata_header_len, 16);
2359363efb4SThara Gopinath 
2369363efb4SThara Gopinath 	diff_dst = (req->src != req->dst) ? true : false;
2379363efb4SThara Gopinath 
2389363efb4SThara Gopinath 	if (diff_dst)
2399363efb4SThara Gopinath 		rctx->src_nents = sg_nents_for_len(req->src, totallen) + 1;
2409363efb4SThara Gopinath 	else
2419363efb4SThara Gopinath 		rctx->src_nents = sg_nents_for_len(req->src, totallen) + 2;
2429363efb4SThara Gopinath 
2439363efb4SThara Gopinath 	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC;
2449363efb4SThara Gopinath 	ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
2459363efb4SThara Gopinath 	if (ret)
2469363efb4SThara Gopinath 		return ret;
2479363efb4SThara Gopinath 
2489363efb4SThara Gopinath 	/* Associated Data */
2499363efb4SThara Gopinath 	sg_init_one(&rctx->adata_sg, rctx->adata, rctx->assoclen);
2509363efb4SThara Gopinath 	sg = qce_sgtable_add(&rctx->src_tbl, &rctx->adata_sg,
2519363efb4SThara Gopinath 			     rctx->assoclen);
2529363efb4SThara Gopinath 	if (IS_ERR(sg)) {
2539363efb4SThara Gopinath 		ret = PTR_ERR(sg);
2549363efb4SThara Gopinath 		goto err_free;
2559363efb4SThara Gopinath 	}
2569363efb4SThara Gopinath 	/* src msg */
2579363efb4SThara Gopinath 	sg = qce_sgtable_add(&rctx->src_tbl, msg_sg, cryptlen);
2589363efb4SThara Gopinath 	if (IS_ERR(sg)) {
2599363efb4SThara Gopinath 		ret = PTR_ERR(sg);
2609363efb4SThara Gopinath 		goto err_free;
2619363efb4SThara Gopinath 	}
2629363efb4SThara Gopinath 	if (!diff_dst) {
2639363efb4SThara Gopinath 		/*
2649363efb4SThara Gopinath 		 * For decrypt, when src and dst buffers are same, there is already space
2659363efb4SThara Gopinath 		 * in the buffer for padded 0's which is output in lieu of
2669363efb4SThara Gopinath 		 * the MAC that is input. So skip the below.
2679363efb4SThara Gopinath 		 */
2689363efb4SThara Gopinath 		if (!IS_DECRYPT(rctx->flags)) {
2699363efb4SThara Gopinath 			sg = qce_aead_prepare_ccm_result_buf(&rctx->src_tbl, req);
2709363efb4SThara Gopinath 			if (IS_ERR(sg)) {
2719363efb4SThara Gopinath 				ret = PTR_ERR(sg);
2729363efb4SThara Gopinath 				goto err_free;
2739363efb4SThara Gopinath 			}
2749363efb4SThara Gopinath 		}
2759363efb4SThara Gopinath 	}
2769363efb4SThara Gopinath 	sg_mark_end(sg);
2779363efb4SThara Gopinath 	rctx->src_sg = rctx->src_tbl.sgl;
2789363efb4SThara Gopinath 	totallen = cryptlen + rctx->assoclen;
2799363efb4SThara Gopinath 	rctx->src_nents = sg_nents_for_len(rctx->src_sg, totallen);
2809363efb4SThara Gopinath 
2819363efb4SThara Gopinath 	if (diff_dst) {
2829363efb4SThara Gopinath 		sg = qce_aead_prepare_dst_buf(req);
2835c0ecc2eSWei Yongjun 		if (IS_ERR(sg)) {
2845c0ecc2eSWei Yongjun 			ret = PTR_ERR(sg);
2859363efb4SThara Gopinath 			goto err_free;
2865c0ecc2eSWei Yongjun 		}
2879363efb4SThara Gopinath 	} else {
2889363efb4SThara Gopinath 		if (IS_ENCRYPT(rctx->flags))
2899363efb4SThara Gopinath 			rctx->dst_nents = rctx->src_nents + 1;
2909363efb4SThara Gopinath 		else
2919363efb4SThara Gopinath 			rctx->dst_nents = rctx->src_nents;
2929363efb4SThara Gopinath 		rctx->dst_sg = rctx->src_sg;
2939363efb4SThara Gopinath 	}
2949363efb4SThara Gopinath 
2959363efb4SThara Gopinath 	return 0;
2969363efb4SThara Gopinath err_free:
2979363efb4SThara Gopinath 	sg_free_table(&rctx->src_tbl);
2989363efb4SThara Gopinath 	return ret;
2999363efb4SThara Gopinath }
3009363efb4SThara Gopinath 
qce_aead_prepare_buf(struct aead_request * req)3019363efb4SThara Gopinath static int qce_aead_prepare_buf(struct aead_request *req)
3029363efb4SThara Gopinath {
303*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
3049363efb4SThara Gopinath 	struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
3059363efb4SThara Gopinath 	struct qce_device *qce = tmpl->qce;
3069363efb4SThara Gopinath 	struct scatterlist *sg;
3079363efb4SThara Gopinath 	bool diff_dst = (req->src != req->dst) ? true : false;
3089363efb4SThara Gopinath 	unsigned int totallen;
3099363efb4SThara Gopinath 
3109363efb4SThara Gopinath 	totallen = rctx->cryptlen + rctx->assoclen;
3119363efb4SThara Gopinath 
3129363efb4SThara Gopinath 	sg = qce_aead_prepare_dst_buf(req);
3139363efb4SThara Gopinath 	if (IS_ERR(sg))
3149363efb4SThara Gopinath 		return PTR_ERR(sg);
3159363efb4SThara Gopinath 	if (diff_dst) {
3169363efb4SThara Gopinath 		rctx->src_nents = sg_nents_for_len(req->src, totallen);
3179363efb4SThara Gopinath 		if (rctx->src_nents < 0) {
3189363efb4SThara Gopinath 			dev_err(qce->dev, "Invalid numbers of src SG.\n");
3199363efb4SThara Gopinath 			return -EINVAL;
3209363efb4SThara Gopinath 		}
3219363efb4SThara Gopinath 		rctx->src_sg = req->src;
3229363efb4SThara Gopinath 	} else {
3239363efb4SThara Gopinath 		rctx->src_nents = rctx->dst_nents - 1;
3249363efb4SThara Gopinath 		rctx->src_sg = rctx->dst_sg;
3259363efb4SThara Gopinath 	}
3269363efb4SThara Gopinath 	return 0;
3279363efb4SThara Gopinath }
3289363efb4SThara Gopinath 
qce_aead_ccm_prepare_buf(struct aead_request * req)3299363efb4SThara Gopinath static int qce_aead_ccm_prepare_buf(struct aead_request *req)
3309363efb4SThara Gopinath {
331*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
3329363efb4SThara Gopinath 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3339363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
3349363efb4SThara Gopinath 	struct scatterlist *sg;
3359363efb4SThara Gopinath 	bool diff_dst = (req->src != req->dst) ? true : false;
3369363efb4SThara Gopinath 	unsigned int cryptlen;
3379363efb4SThara Gopinath 
3389363efb4SThara Gopinath 	if (rctx->assoclen)
3399363efb4SThara Gopinath 		return qce_aead_ccm_prepare_buf_assoclen(req);
3409363efb4SThara Gopinath 
3419363efb4SThara Gopinath 	if (IS_ENCRYPT(rctx->flags))
3429363efb4SThara Gopinath 		return qce_aead_prepare_buf(req);
3439363efb4SThara Gopinath 
3449363efb4SThara Gopinath 	cryptlen = rctx->cryptlen + ctx->authsize;
3459363efb4SThara Gopinath 	if (diff_dst) {
3469363efb4SThara Gopinath 		rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
3479363efb4SThara Gopinath 		rctx->src_sg = req->src;
3489363efb4SThara Gopinath 		sg = qce_aead_prepare_dst_buf(req);
3499363efb4SThara Gopinath 		if (IS_ERR(sg))
3509363efb4SThara Gopinath 			return PTR_ERR(sg);
3519363efb4SThara Gopinath 	} else {
3529363efb4SThara Gopinath 		rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
3539363efb4SThara Gopinath 		rctx->src_sg = req->src;
3549363efb4SThara Gopinath 		rctx->dst_nents = rctx->src_nents;
3559363efb4SThara Gopinath 		rctx->dst_sg = rctx->src_sg;
3569363efb4SThara Gopinath 	}
3579363efb4SThara Gopinath 
3589363efb4SThara Gopinath 	return 0;
3599363efb4SThara Gopinath }
3609363efb4SThara Gopinath 
qce_aead_create_ccm_nonce(struct qce_aead_reqctx * rctx,struct qce_aead_ctx * ctx)3619363efb4SThara Gopinath static int qce_aead_create_ccm_nonce(struct qce_aead_reqctx *rctx, struct qce_aead_ctx *ctx)
3629363efb4SThara Gopinath {
3639363efb4SThara Gopinath 	unsigned int msglen_size, ivsize;
3649363efb4SThara Gopinath 	u8 msg_len[4];
3659363efb4SThara Gopinath 	int i;
3669363efb4SThara Gopinath 
3679363efb4SThara Gopinath 	if (!rctx || !rctx->iv)
3689363efb4SThara Gopinath 		return -EINVAL;
3699363efb4SThara Gopinath 
3709363efb4SThara Gopinath 	msglen_size = rctx->iv[0] + 1;
3719363efb4SThara Gopinath 
3729363efb4SThara Gopinath 	/* Verify that msg len size is valid */
3739363efb4SThara Gopinath 	if (msglen_size < 2 || msglen_size > 8)
3749363efb4SThara Gopinath 		return -EINVAL;
3759363efb4SThara Gopinath 
3769363efb4SThara Gopinath 	ivsize = rctx->ivsize;
3779363efb4SThara Gopinath 
3789363efb4SThara Gopinath 	/*
3799363efb4SThara Gopinath 	 * Clear the msglen bytes in IV.
3809363efb4SThara Gopinath 	 * Else the h/w engine and nonce will use any stray value pending there.
3819363efb4SThara Gopinath 	 */
3829363efb4SThara Gopinath 	if (!IS_CCM_RFC4309(rctx->flags)) {
3839363efb4SThara Gopinath 		for (i = 0; i < msglen_size; i++)
3849363efb4SThara Gopinath 			rctx->iv[ivsize - i - 1] = 0;
3859363efb4SThara Gopinath 	}
3869363efb4SThara Gopinath 
3879363efb4SThara Gopinath 	/*
3889363efb4SThara Gopinath 	 * The crypto framework encodes cryptlen as unsigned int. Thus, even though
3899363efb4SThara Gopinath 	 * spec allows for upto 8 bytes to encode msg_len only 4 bytes are needed.
3909363efb4SThara Gopinath 	 */
3919363efb4SThara Gopinath 	if (msglen_size > 4)
3929363efb4SThara Gopinath 		msglen_size = 4;
3939363efb4SThara Gopinath 
3949363efb4SThara Gopinath 	memcpy(&msg_len[0], &rctx->cryptlen, 4);
3959363efb4SThara Gopinath 
3969363efb4SThara Gopinath 	memcpy(&rctx->ccm_nonce[0], rctx->iv, rctx->ivsize);
3979363efb4SThara Gopinath 	if (rctx->assoclen)
3989363efb4SThara Gopinath 		rctx->ccm_nonce[0] |= 1 << CCM_NONCE_ADATA_SHIFT;
3999363efb4SThara Gopinath 	rctx->ccm_nonce[0] |= ((ctx->authsize - 2) / 2) <<
4009363efb4SThara Gopinath 				CCM_NONCE_AUTHSIZE_SHIFT;
4019363efb4SThara Gopinath 	for (i = 0; i < msglen_size; i++)
4029363efb4SThara Gopinath 		rctx->ccm_nonce[QCE_MAX_NONCE - i - 1] = msg_len[i];
4039363efb4SThara Gopinath 
4049363efb4SThara Gopinath 	return 0;
4059363efb4SThara Gopinath }
4069363efb4SThara Gopinath 
4079363efb4SThara Gopinath static int
qce_aead_async_req_handle(struct crypto_async_request * async_req)4089363efb4SThara Gopinath qce_aead_async_req_handle(struct crypto_async_request *async_req)
4099363efb4SThara Gopinath {
4109363efb4SThara Gopinath 	struct aead_request *req = aead_request_cast(async_req);
411*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
4129363efb4SThara Gopinath 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
4139363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
4149363efb4SThara Gopinath 	struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
4159363efb4SThara Gopinath 	struct qce_device *qce = tmpl->qce;
4169363efb4SThara Gopinath 	enum dma_data_direction dir_src, dir_dst;
4179363efb4SThara Gopinath 	bool diff_dst;
4189363efb4SThara Gopinath 	int dst_nents, src_nents, ret;
4199363efb4SThara Gopinath 
4209363efb4SThara Gopinath 	if (IS_CCM_RFC4309(rctx->flags)) {
4219363efb4SThara Gopinath 		memset(rctx->ccm_rfc4309_iv, 0, QCE_MAX_IV_SIZE);
4229363efb4SThara Gopinath 		rctx->ccm_rfc4309_iv[0] = 3;
4239363efb4SThara Gopinath 		memcpy(&rctx->ccm_rfc4309_iv[1], ctx->ccm4309_salt, QCE_CCM4309_SALT_SIZE);
4249363efb4SThara Gopinath 		memcpy(&rctx->ccm_rfc4309_iv[4], req->iv, 8);
4259363efb4SThara Gopinath 		rctx->iv = rctx->ccm_rfc4309_iv;
4269363efb4SThara Gopinath 		rctx->ivsize = AES_BLOCK_SIZE;
4279363efb4SThara Gopinath 	} else {
4289363efb4SThara Gopinath 		rctx->iv = req->iv;
4299363efb4SThara Gopinath 		rctx->ivsize = crypto_aead_ivsize(tfm);
4309363efb4SThara Gopinath 	}
4319363efb4SThara Gopinath 	if (IS_CCM_RFC4309(rctx->flags))
4329363efb4SThara Gopinath 		rctx->assoclen = req->assoclen - 8;
4339363efb4SThara Gopinath 	else
4349363efb4SThara Gopinath 		rctx->assoclen = req->assoclen;
4359363efb4SThara Gopinath 
4369363efb4SThara Gopinath 	diff_dst = (req->src != req->dst) ? true : false;
4379363efb4SThara Gopinath 	dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
4389363efb4SThara Gopinath 	dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
4399363efb4SThara Gopinath 
4409363efb4SThara Gopinath 	if (IS_CCM(rctx->flags)) {
4419363efb4SThara Gopinath 		ret = qce_aead_create_ccm_nonce(rctx, ctx);
4429363efb4SThara Gopinath 		if (ret)
4439363efb4SThara Gopinath 			return ret;
4449363efb4SThara Gopinath 	}
4459363efb4SThara Gopinath 	if (IS_CCM(rctx->flags))
4469363efb4SThara Gopinath 		ret = qce_aead_ccm_prepare_buf(req);
4479363efb4SThara Gopinath 	else
4489363efb4SThara Gopinath 		ret = qce_aead_prepare_buf(req);
4499363efb4SThara Gopinath 
4509363efb4SThara Gopinath 	if (ret)
4519363efb4SThara Gopinath 		return ret;
4529363efb4SThara Gopinath 	dst_nents = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
453417f62f6SJack Wang 	if (!dst_nents) {
454417f62f6SJack Wang 		ret = -EIO;
4559363efb4SThara Gopinath 		goto error_free;
4565c0ecc2eSWei Yongjun 	}
4579363efb4SThara Gopinath 
4589363efb4SThara Gopinath 	if (diff_dst) {
4599363efb4SThara Gopinath 		src_nents = dma_map_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
4605c0ecc2eSWei Yongjun 		if (src_nents < 0) {
4615c0ecc2eSWei Yongjun 			ret = src_nents;
4629363efb4SThara Gopinath 			goto error_unmap_dst;
4635c0ecc2eSWei Yongjun 		}
4649363efb4SThara Gopinath 	} else {
4659363efb4SThara Gopinath 		if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
4669363efb4SThara Gopinath 			src_nents = dst_nents;
4679363efb4SThara Gopinath 		else
4689363efb4SThara Gopinath 			src_nents = dst_nents - 1;
4699363efb4SThara Gopinath 	}
4709363efb4SThara Gopinath 
4719363efb4SThara Gopinath 	ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents, rctx->dst_sg, dst_nents,
4729363efb4SThara Gopinath 			       qce_aead_done, async_req);
4739363efb4SThara Gopinath 	if (ret)
4749363efb4SThara Gopinath 		goto error_unmap_src;
4759363efb4SThara Gopinath 
4769363efb4SThara Gopinath 	qce_dma_issue_pending(&qce->dma);
4779363efb4SThara Gopinath 
4789363efb4SThara Gopinath 	ret = qce_start(async_req, tmpl->crypto_alg_type);
4799363efb4SThara Gopinath 	if (ret)
4809363efb4SThara Gopinath 		goto error_terminate;
4819363efb4SThara Gopinath 
4829363efb4SThara Gopinath 	return 0;
4839363efb4SThara Gopinath 
4849363efb4SThara Gopinath error_terminate:
4859363efb4SThara Gopinath 	qce_dma_terminate_all(&qce->dma);
4869363efb4SThara Gopinath error_unmap_src:
4879363efb4SThara Gopinath 	if (diff_dst)
4889363efb4SThara Gopinath 		dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
4899363efb4SThara Gopinath error_unmap_dst:
4909363efb4SThara Gopinath 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
4919363efb4SThara Gopinath error_free:
4929363efb4SThara Gopinath 	if (IS_CCM(rctx->flags) && rctx->assoclen) {
4939363efb4SThara Gopinath 		sg_free_table(&rctx->src_tbl);
4949363efb4SThara Gopinath 		if (diff_dst)
4959363efb4SThara Gopinath 			sg_free_table(&rctx->dst_tbl);
4969363efb4SThara Gopinath 	} else {
4979363efb4SThara Gopinath 		sg_free_table(&rctx->dst_tbl);
4989363efb4SThara Gopinath 	}
4999363efb4SThara Gopinath 	return ret;
5009363efb4SThara Gopinath }
5019363efb4SThara Gopinath 
qce_aead_crypt(struct aead_request * req,int encrypt)5029363efb4SThara Gopinath static int qce_aead_crypt(struct aead_request *req, int encrypt)
5039363efb4SThara Gopinath {
5049363efb4SThara Gopinath 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
505*18daae5bSHerbert Xu 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
5069363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
5079363efb4SThara Gopinath 	struct qce_alg_template *tmpl = to_aead_tmpl(tfm);
5089363efb4SThara Gopinath 	unsigned int blocksize = crypto_aead_blocksize(tfm);
5099363efb4SThara Gopinath 
5109363efb4SThara Gopinath 	rctx->flags  = tmpl->alg_flags;
5119363efb4SThara Gopinath 	rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
5129363efb4SThara Gopinath 
5139363efb4SThara Gopinath 	if (encrypt)
5149363efb4SThara Gopinath 		rctx->cryptlen = req->cryptlen;
5159363efb4SThara Gopinath 	else
5169363efb4SThara Gopinath 		rctx->cryptlen = req->cryptlen - ctx->authsize;
5179363efb4SThara Gopinath 
5189363efb4SThara Gopinath 	/* CE does not handle 0 length messages */
5199363efb4SThara Gopinath 	if (!rctx->cryptlen) {
5209363efb4SThara Gopinath 		if (!(IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags)))
521b51dcf05SThara Gopinath 			ctx->need_fallback = true;
522b51dcf05SThara Gopinath 	}
523b51dcf05SThara Gopinath 
524b51dcf05SThara Gopinath 	/* If fallback is needed, schedule and exit */
525b51dcf05SThara Gopinath 	if (ctx->need_fallback) {
526b51dcf05SThara Gopinath 		/* Reset need_fallback in case the same ctx is used for another transaction */
527b51dcf05SThara Gopinath 		ctx->need_fallback = false;
528b51dcf05SThara Gopinath 
529b51dcf05SThara Gopinath 		aead_request_set_tfm(&rctx->fallback_req, ctx->fallback);
530b51dcf05SThara Gopinath 		aead_request_set_callback(&rctx->fallback_req, req->base.flags,
531b51dcf05SThara Gopinath 					  req->base.complete, req->base.data);
532b51dcf05SThara Gopinath 		aead_request_set_crypt(&rctx->fallback_req, req->src,
533b51dcf05SThara Gopinath 				       req->dst, req->cryptlen, req->iv);
534b51dcf05SThara Gopinath 		aead_request_set_ad(&rctx->fallback_req, req->assoclen);
535b51dcf05SThara Gopinath 
536b51dcf05SThara Gopinath 		return encrypt ? crypto_aead_encrypt(&rctx->fallback_req) :
537b51dcf05SThara Gopinath 				 crypto_aead_decrypt(&rctx->fallback_req);
5389363efb4SThara Gopinath 	}
5399363efb4SThara Gopinath 
5409363efb4SThara Gopinath 	/*
5419363efb4SThara Gopinath 	 * CBC algorithms require message lengths to be
5429363efb4SThara Gopinath 	 * multiples of block size.
5439363efb4SThara Gopinath 	 */
5449363efb4SThara Gopinath 	if (IS_CBC(rctx->flags) && !IS_ALIGNED(rctx->cryptlen, blocksize))
5459363efb4SThara Gopinath 		return -EINVAL;
5469363efb4SThara Gopinath 
5479363efb4SThara Gopinath 	/* RFC4309 supported AAD size 16 bytes/20 bytes */
5489363efb4SThara Gopinath 	if (IS_CCM_RFC4309(rctx->flags))
5499363efb4SThara Gopinath 		if (crypto_ipsec_check_assoclen(req->assoclen))
5509363efb4SThara Gopinath 			return -EINVAL;
5519363efb4SThara Gopinath 
5529363efb4SThara Gopinath 	return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
5539363efb4SThara Gopinath }
5549363efb4SThara Gopinath 
qce_aead_encrypt(struct aead_request * req)5559363efb4SThara Gopinath static int qce_aead_encrypt(struct aead_request *req)
5569363efb4SThara Gopinath {
5579363efb4SThara Gopinath 	return qce_aead_crypt(req, 1);
5589363efb4SThara Gopinath }
5599363efb4SThara Gopinath 
qce_aead_decrypt(struct aead_request * req)5609363efb4SThara Gopinath static int qce_aead_decrypt(struct aead_request *req)
5619363efb4SThara Gopinath {
5629363efb4SThara Gopinath 	return qce_aead_crypt(req, 0);
5639363efb4SThara Gopinath }
5649363efb4SThara Gopinath 
qce_aead_ccm_setkey(struct crypto_aead * tfm,const u8 * key,unsigned int keylen)5659363efb4SThara Gopinath static int qce_aead_ccm_setkey(struct crypto_aead *tfm, const u8 *key,
5669363efb4SThara Gopinath 			       unsigned int keylen)
5679363efb4SThara Gopinath {
5689363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
5699363efb4SThara Gopinath 	unsigned long flags = to_aead_tmpl(tfm)->alg_flags;
5709363efb4SThara Gopinath 
5719363efb4SThara Gopinath 	if (IS_CCM_RFC4309(flags)) {
5729363efb4SThara Gopinath 		if (keylen < QCE_CCM4309_SALT_SIZE)
5739363efb4SThara Gopinath 			return -EINVAL;
5749363efb4SThara Gopinath 		keylen -= QCE_CCM4309_SALT_SIZE;
5759363efb4SThara Gopinath 		memcpy(ctx->ccm4309_salt, key + keylen, QCE_CCM4309_SALT_SIZE);
5769363efb4SThara Gopinath 	}
5779363efb4SThara Gopinath 
578b51dcf05SThara Gopinath 	if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256 && keylen != AES_KEYSIZE_192)
5799363efb4SThara Gopinath 		return -EINVAL;
5809363efb4SThara Gopinath 
5819363efb4SThara Gopinath 	ctx->enc_keylen = keylen;
5829363efb4SThara Gopinath 	ctx->auth_keylen = keylen;
5839363efb4SThara Gopinath 
5849363efb4SThara Gopinath 	memcpy(ctx->enc_key, key, keylen);
5859363efb4SThara Gopinath 	memcpy(ctx->auth_key, key, keylen);
5869363efb4SThara Gopinath 
587b51dcf05SThara Gopinath 	if (keylen == AES_KEYSIZE_192)
588b51dcf05SThara Gopinath 		ctx->need_fallback = true;
589b51dcf05SThara Gopinath 
590b51dcf05SThara Gopinath 	return IS_CCM_RFC4309(flags) ?
591b51dcf05SThara Gopinath 		crypto_aead_setkey(ctx->fallback, key, keylen + QCE_CCM4309_SALT_SIZE) :
592b51dcf05SThara Gopinath 		crypto_aead_setkey(ctx->fallback, key, keylen);
5939363efb4SThara Gopinath }
5949363efb4SThara Gopinath 
qce_aead_setkey(struct crypto_aead * tfm,const u8 * key,unsigned int keylen)5959363efb4SThara Gopinath static int qce_aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
5969363efb4SThara Gopinath {
5979363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
5989363efb4SThara Gopinath 	struct crypto_authenc_keys authenc_keys;
5999363efb4SThara Gopinath 	unsigned long flags = to_aead_tmpl(tfm)->alg_flags;
6009363efb4SThara Gopinath 	u32 _key[6];
6019363efb4SThara Gopinath 	int err;
6029363efb4SThara Gopinath 
6039363efb4SThara Gopinath 	err = crypto_authenc_extractkeys(&authenc_keys, key, keylen);
6049363efb4SThara Gopinath 	if (err)
6059363efb4SThara Gopinath 		return err;
6069363efb4SThara Gopinath 
6079363efb4SThara Gopinath 	if (authenc_keys.enckeylen > QCE_MAX_KEY_SIZE ||
6089363efb4SThara Gopinath 	    authenc_keys.authkeylen > QCE_MAX_KEY_SIZE)
6099363efb4SThara Gopinath 		return -EINVAL;
6109363efb4SThara Gopinath 
6119363efb4SThara Gopinath 	if (IS_DES(flags)) {
6129363efb4SThara Gopinath 		err = verify_aead_des_key(tfm, authenc_keys.enckey, authenc_keys.enckeylen);
6139363efb4SThara Gopinath 		if (err)
6149363efb4SThara Gopinath 			return err;
6159363efb4SThara Gopinath 	} else if (IS_3DES(flags)) {
6169363efb4SThara Gopinath 		err = verify_aead_des3_key(tfm, authenc_keys.enckey, authenc_keys.enckeylen);
6179363efb4SThara Gopinath 		if (err)
6189363efb4SThara Gopinath 			return err;
6199363efb4SThara Gopinath 		/*
6209363efb4SThara Gopinath 		 * The crypto engine does not support any two keys
6219363efb4SThara Gopinath 		 * being the same for triple des algorithms. The
6229363efb4SThara Gopinath 		 * verify_skcipher_des3_key does not check for all the
623b51dcf05SThara Gopinath 		 * below conditions. Schedule fallback in this case.
6249363efb4SThara Gopinath 		 */
6259363efb4SThara Gopinath 		memcpy(_key, authenc_keys.enckey, DES3_EDE_KEY_SIZE);
6269363efb4SThara Gopinath 		if (!((_key[0] ^ _key[2]) | (_key[1] ^ _key[3])) ||
6279363efb4SThara Gopinath 		    !((_key[2] ^ _key[4]) | (_key[3] ^ _key[5])) ||
6289363efb4SThara Gopinath 		    !((_key[0] ^ _key[4]) | (_key[1] ^ _key[5])))
629b51dcf05SThara Gopinath 			ctx->need_fallback = true;
6309363efb4SThara Gopinath 	} else if (IS_AES(flags)) {
6319363efb4SThara Gopinath 		/* No random key sizes */
6329363efb4SThara Gopinath 		if (authenc_keys.enckeylen != AES_KEYSIZE_128 &&
633b51dcf05SThara Gopinath 		    authenc_keys.enckeylen != AES_KEYSIZE_192 &&
6349363efb4SThara Gopinath 		    authenc_keys.enckeylen != AES_KEYSIZE_256)
6359363efb4SThara Gopinath 			return -EINVAL;
636b51dcf05SThara Gopinath 		if (authenc_keys.enckeylen == AES_KEYSIZE_192)
637b51dcf05SThara Gopinath 			ctx->need_fallback = true;
6389363efb4SThara Gopinath 	}
6399363efb4SThara Gopinath 
6409363efb4SThara Gopinath 	ctx->enc_keylen = authenc_keys.enckeylen;
6419363efb4SThara Gopinath 	ctx->auth_keylen = authenc_keys.authkeylen;
6429363efb4SThara Gopinath 
6439363efb4SThara Gopinath 	memcpy(ctx->enc_key, authenc_keys.enckey, authenc_keys.enckeylen);
6449363efb4SThara Gopinath 
6459363efb4SThara Gopinath 	memset(ctx->auth_key, 0, sizeof(ctx->auth_key));
6469363efb4SThara Gopinath 	memcpy(ctx->auth_key, authenc_keys.authkey, authenc_keys.authkeylen);
6479363efb4SThara Gopinath 
648b51dcf05SThara Gopinath 	return crypto_aead_setkey(ctx->fallback, key, keylen);
6499363efb4SThara Gopinath }
6509363efb4SThara Gopinath 
qce_aead_setauthsize(struct crypto_aead * tfm,unsigned int authsize)6519363efb4SThara Gopinath static int qce_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
6529363efb4SThara Gopinath {
6539363efb4SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
6549363efb4SThara Gopinath 	unsigned long flags = to_aead_tmpl(tfm)->alg_flags;
6559363efb4SThara Gopinath 
6569363efb4SThara Gopinath 	if (IS_CCM(flags)) {
6579363efb4SThara Gopinath 		if (authsize < 4 || authsize > 16 || authsize % 2)
6589363efb4SThara Gopinath 			return -EINVAL;
6599363efb4SThara Gopinath 		if (IS_CCM_RFC4309(flags) && (authsize < 8 || authsize % 4))
6609363efb4SThara Gopinath 			return -EINVAL;
6619363efb4SThara Gopinath 	}
6629363efb4SThara Gopinath 	ctx->authsize = authsize;
663b51dcf05SThara Gopinath 
664b51dcf05SThara Gopinath 	return crypto_aead_setauthsize(ctx->fallback, authsize);
6659363efb4SThara Gopinath }
6669363efb4SThara Gopinath 
qce_aead_init(struct crypto_aead * tfm)6679363efb4SThara Gopinath static int qce_aead_init(struct crypto_aead *tfm)
6689363efb4SThara Gopinath {
669b51dcf05SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
670b51dcf05SThara Gopinath 
671b51dcf05SThara Gopinath 	ctx->need_fallback = false;
672b51dcf05SThara Gopinath 	ctx->fallback = crypto_alloc_aead(crypto_tfm_alg_name(&tfm->base),
673b51dcf05SThara Gopinath 					  0, CRYPTO_ALG_NEED_FALLBACK);
674b51dcf05SThara Gopinath 
675b51dcf05SThara Gopinath 	if (IS_ERR(ctx->fallback))
676b51dcf05SThara Gopinath 		return PTR_ERR(ctx->fallback);
677b51dcf05SThara Gopinath 
678*18daae5bSHerbert Xu 	crypto_aead_set_reqsize_dma(tfm, sizeof(struct qce_aead_reqctx) +
679b51dcf05SThara Gopinath 					 crypto_aead_reqsize(ctx->fallback));
6809363efb4SThara Gopinath 	return 0;
6819363efb4SThara Gopinath }
6829363efb4SThara Gopinath 
qce_aead_exit(struct crypto_aead * tfm)683b51dcf05SThara Gopinath static void qce_aead_exit(struct crypto_aead *tfm)
684b51dcf05SThara Gopinath {
685b51dcf05SThara Gopinath 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
686b51dcf05SThara Gopinath 
687b51dcf05SThara Gopinath 	crypto_free_aead(ctx->fallback);
688b51dcf05SThara Gopinath }
689b51dcf05SThara Gopinath 
6909363efb4SThara Gopinath struct qce_aead_def {
6919363efb4SThara Gopinath 	unsigned long flags;
6929363efb4SThara Gopinath 	const char *name;
6939363efb4SThara Gopinath 	const char *drv_name;
6949363efb4SThara Gopinath 	unsigned int blocksize;
6959363efb4SThara Gopinath 	unsigned int chunksize;
6969363efb4SThara Gopinath 	unsigned int ivsize;
6979363efb4SThara Gopinath 	unsigned int maxauthsize;
6989363efb4SThara Gopinath };
6999363efb4SThara Gopinath 
7009363efb4SThara Gopinath static const struct qce_aead_def aead_def[] = {
7019363efb4SThara Gopinath 	{
7029363efb4SThara Gopinath 		.flags          = QCE_ALG_DES | QCE_MODE_CBC | QCE_HASH_SHA1_HMAC,
7039363efb4SThara Gopinath 		.name           = "authenc(hmac(sha1),cbc(des))",
7049363efb4SThara Gopinath 		.drv_name       = "authenc-hmac-sha1-cbc-des-qce",
7059363efb4SThara Gopinath 		.blocksize      = DES_BLOCK_SIZE,
7069363efb4SThara Gopinath 		.ivsize         = DES_BLOCK_SIZE,
7079363efb4SThara Gopinath 		.maxauthsize	= SHA1_DIGEST_SIZE,
7089363efb4SThara Gopinath 	},
7099363efb4SThara Gopinath 	{
7109363efb4SThara Gopinath 		.flags          = QCE_ALG_3DES | QCE_MODE_CBC | QCE_HASH_SHA1_HMAC,
7119363efb4SThara Gopinath 		.name           = "authenc(hmac(sha1),cbc(des3_ede))",
7129363efb4SThara Gopinath 		.drv_name       = "authenc-hmac-sha1-cbc-3des-qce",
7139363efb4SThara Gopinath 		.blocksize      = DES3_EDE_BLOCK_SIZE,
7149363efb4SThara Gopinath 		.ivsize         = DES3_EDE_BLOCK_SIZE,
7159363efb4SThara Gopinath 		.maxauthsize	= SHA1_DIGEST_SIZE,
7169363efb4SThara Gopinath 	},
7179363efb4SThara Gopinath 	{
7189363efb4SThara Gopinath 		.flags          = QCE_ALG_DES | QCE_MODE_CBC | QCE_HASH_SHA256_HMAC,
7199363efb4SThara Gopinath 		.name           = "authenc(hmac(sha256),cbc(des))",
7209363efb4SThara Gopinath 		.drv_name       = "authenc-hmac-sha256-cbc-des-qce",
7219363efb4SThara Gopinath 		.blocksize      = DES_BLOCK_SIZE,
7229363efb4SThara Gopinath 		.ivsize         = DES_BLOCK_SIZE,
7239363efb4SThara Gopinath 		.maxauthsize	= SHA256_DIGEST_SIZE,
7249363efb4SThara Gopinath 	},
7259363efb4SThara Gopinath 	{
7269363efb4SThara Gopinath 		.flags          = QCE_ALG_3DES | QCE_MODE_CBC | QCE_HASH_SHA256_HMAC,
7279363efb4SThara Gopinath 		.name           = "authenc(hmac(sha256),cbc(des3_ede))",
7289363efb4SThara Gopinath 		.drv_name       = "authenc-hmac-sha256-cbc-3des-qce",
7299363efb4SThara Gopinath 		.blocksize      = DES3_EDE_BLOCK_SIZE,
7309363efb4SThara Gopinath 		.ivsize         = DES3_EDE_BLOCK_SIZE,
7319363efb4SThara Gopinath 		.maxauthsize	= SHA256_DIGEST_SIZE,
7329363efb4SThara Gopinath 	},
7339363efb4SThara Gopinath 	{
7349363efb4SThara Gopinath 		.flags          =  QCE_ALG_AES | QCE_MODE_CBC | QCE_HASH_SHA256_HMAC,
7359363efb4SThara Gopinath 		.name           = "authenc(hmac(sha256),cbc(aes))",
7369363efb4SThara Gopinath 		.drv_name       = "authenc-hmac-sha256-cbc-aes-qce",
7379363efb4SThara Gopinath 		.blocksize      = AES_BLOCK_SIZE,
7389363efb4SThara Gopinath 		.ivsize         = AES_BLOCK_SIZE,
7399363efb4SThara Gopinath 		.maxauthsize	= SHA256_DIGEST_SIZE,
7409363efb4SThara Gopinath 	},
7419363efb4SThara Gopinath 	{
7429363efb4SThara Gopinath 		.flags          =  QCE_ALG_AES | QCE_MODE_CCM,
7439363efb4SThara Gopinath 		.name           = "ccm(aes)",
7449363efb4SThara Gopinath 		.drv_name       = "ccm-aes-qce",
7459363efb4SThara Gopinath 		.blocksize	= 1,
7469363efb4SThara Gopinath 		.ivsize         = AES_BLOCK_SIZE,
7479363efb4SThara Gopinath 		.maxauthsize	= AES_BLOCK_SIZE,
7489363efb4SThara Gopinath 	},
7499363efb4SThara Gopinath 	{
7509363efb4SThara Gopinath 		.flags          =  QCE_ALG_AES | QCE_MODE_CCM | QCE_MODE_CCM_RFC4309,
7519363efb4SThara Gopinath 		.name           = "rfc4309(ccm(aes))",
7529363efb4SThara Gopinath 		.drv_name       = "rfc4309-ccm-aes-qce",
7539363efb4SThara Gopinath 		.blocksize	= 1,
7549363efb4SThara Gopinath 		.ivsize         = 8,
7559363efb4SThara Gopinath 		.maxauthsize	= AES_BLOCK_SIZE,
7569363efb4SThara Gopinath 	},
7579363efb4SThara Gopinath };
7589363efb4SThara Gopinath 
qce_aead_register_one(const struct qce_aead_def * def,struct qce_device * qce)7599363efb4SThara Gopinath static int qce_aead_register_one(const struct qce_aead_def *def, struct qce_device *qce)
7609363efb4SThara Gopinath {
7619363efb4SThara Gopinath 	struct qce_alg_template *tmpl;
7629363efb4SThara Gopinath 	struct aead_alg *alg;
7639363efb4SThara Gopinath 	int ret;
7649363efb4SThara Gopinath 
7659363efb4SThara Gopinath 	tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
7669363efb4SThara Gopinath 	if (!tmpl)
7679363efb4SThara Gopinath 		return -ENOMEM;
7689363efb4SThara Gopinath 
7699363efb4SThara Gopinath 	alg = &tmpl->alg.aead;
7709363efb4SThara Gopinath 
7719363efb4SThara Gopinath 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
7729363efb4SThara Gopinath 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
7739363efb4SThara Gopinath 		 def->drv_name);
7749363efb4SThara Gopinath 
7759363efb4SThara Gopinath 	alg->base.cra_blocksize		= def->blocksize;
7769363efb4SThara Gopinath 	alg->chunksize			= def->chunksize;
7779363efb4SThara Gopinath 	alg->ivsize			= def->ivsize;
7789363efb4SThara Gopinath 	alg->maxauthsize		= def->maxauthsize;
7799363efb4SThara Gopinath 	if (IS_CCM(def->flags))
7809363efb4SThara Gopinath 		alg->setkey		= qce_aead_ccm_setkey;
7819363efb4SThara Gopinath 	else
7829363efb4SThara Gopinath 		alg->setkey		= qce_aead_setkey;
7839363efb4SThara Gopinath 	alg->setauthsize		= qce_aead_setauthsize;
7849363efb4SThara Gopinath 	alg->encrypt			= qce_aead_encrypt;
7859363efb4SThara Gopinath 	alg->decrypt			= qce_aead_decrypt;
7869363efb4SThara Gopinath 	alg->init			= qce_aead_init;
787b51dcf05SThara Gopinath 	alg->exit			= qce_aead_exit;
7889363efb4SThara Gopinath 
7899363efb4SThara Gopinath 	alg->base.cra_priority		= 300;
7909363efb4SThara Gopinath 	alg->base.cra_flags		= CRYPTO_ALG_ASYNC |
7919363efb4SThara Gopinath 					  CRYPTO_ALG_ALLOCATES_MEMORY |
792b51dcf05SThara Gopinath 					  CRYPTO_ALG_KERN_DRIVER_ONLY |
793b51dcf05SThara Gopinath 					  CRYPTO_ALG_NEED_FALLBACK;
7949363efb4SThara Gopinath 	alg->base.cra_ctxsize		= sizeof(struct qce_aead_ctx);
7959363efb4SThara Gopinath 	alg->base.cra_alignmask		= 0;
7969363efb4SThara Gopinath 	alg->base.cra_module		= THIS_MODULE;
7979363efb4SThara Gopinath 
7989363efb4SThara Gopinath 	INIT_LIST_HEAD(&tmpl->entry);
7999363efb4SThara Gopinath 	tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_AEAD;
8009363efb4SThara Gopinath 	tmpl->alg_flags = def->flags;
8019363efb4SThara Gopinath 	tmpl->qce = qce;
8029363efb4SThara Gopinath 
8039363efb4SThara Gopinath 	ret = crypto_register_aead(alg);
8049363efb4SThara Gopinath 	if (ret) {
8059363efb4SThara Gopinath 		dev_err(qce->dev, "%s registration failed\n", alg->base.cra_name);
8064a9dbd02SChengfeng Ye 		kfree(tmpl);
8079363efb4SThara Gopinath 		return ret;
8089363efb4SThara Gopinath 	}
8099363efb4SThara Gopinath 
8109363efb4SThara Gopinath 	list_add_tail(&tmpl->entry, &aead_algs);
8119363efb4SThara Gopinath 	dev_dbg(qce->dev, "%s is registered\n", alg->base.cra_name);
8129363efb4SThara Gopinath 	return 0;
8139363efb4SThara Gopinath }
8149363efb4SThara Gopinath 
qce_aead_unregister(struct qce_device * qce)8159363efb4SThara Gopinath static void qce_aead_unregister(struct qce_device *qce)
8169363efb4SThara Gopinath {
8179363efb4SThara Gopinath 	struct qce_alg_template *tmpl, *n;
8189363efb4SThara Gopinath 
8199363efb4SThara Gopinath 	list_for_each_entry_safe(tmpl, n, &aead_algs, entry) {
8209363efb4SThara Gopinath 		crypto_unregister_aead(&tmpl->alg.aead);
8219363efb4SThara Gopinath 		list_del(&tmpl->entry);
8229363efb4SThara Gopinath 		kfree(tmpl);
8239363efb4SThara Gopinath 	}
8249363efb4SThara Gopinath }
8259363efb4SThara Gopinath 
qce_aead_register(struct qce_device * qce)8269363efb4SThara Gopinath static int qce_aead_register(struct qce_device *qce)
8279363efb4SThara Gopinath {
8289363efb4SThara Gopinath 	int ret, i;
8299363efb4SThara Gopinath 
8309363efb4SThara Gopinath 	for (i = 0; i < ARRAY_SIZE(aead_def); i++) {
8319363efb4SThara Gopinath 		ret = qce_aead_register_one(&aead_def[i], qce);
8329363efb4SThara Gopinath 		if (ret)
8339363efb4SThara Gopinath 			goto err;
8349363efb4SThara Gopinath 	}
8359363efb4SThara Gopinath 
8369363efb4SThara Gopinath 	return 0;
8379363efb4SThara Gopinath err:
8389363efb4SThara Gopinath 	qce_aead_unregister(qce);
8399363efb4SThara Gopinath 	return ret;
8409363efb4SThara Gopinath }
8419363efb4SThara Gopinath 
8429363efb4SThara Gopinath const struct qce_algo_ops aead_ops = {
8439363efb4SThara Gopinath 	.type = CRYPTO_ALG_TYPE_AEAD,
8449363efb4SThara Gopinath 	.register_algs = qce_aead_register,
8459363efb4SThara Gopinath 	.unregister_algs = qce_aead_unregister,
8469363efb4SThara Gopinath 	.async_req_handle = qce_aead_async_req_handle,
8479363efb4SThara Gopinath };
848