Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <crypto/internal/des.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "cipher.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static unsigned int aes_sw_max_len = CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) module_param(aes_sw_max_len, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) MODULE_PARM_DESC(aes_sw_max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		 "Only use hardware for AES requests larger than this "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		 "[0=always use hardware; anything <16 breaks AES-GCM; default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		 __stringify(CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN)"]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static LIST_HEAD(skcipher_algs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static void qce_skcipher_done(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct crypto_async_request *async_req = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct skcipher_request *req = skcipher_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct qce_result_dump *result_buf = qce->dma.result_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	enum dma_data_direction dir_src, dir_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	u32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	bool diff_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	diff_dst = (req->src != req->dst) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	error = qce_dma_terminate_all(&qce->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		dev_dbg(qce->dev, "skcipher dma termination error (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (diff_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	sg_free_table(&rctx->dst_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	error = qce_check_status(qce, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		dev_dbg(qce->dev, "skcipher operation error (%x)\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	memcpy(rctx->iv, result_buf->encr_cntr_iv, rctx->ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	qce->async_req_done(tmpl->qce, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct skcipher_request *req = skcipher_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	enum dma_data_direction dir_src, dir_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	bool diff_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	gfp_t gfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int dst_nents, src_nents, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	rctx->iv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	rctx->ivsize = crypto_skcipher_ivsize(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	rctx->cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	diff_dst = (req->src != req->dst) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	rctx->src_nents = sg_nents_for_len(req->src, req->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (diff_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		rctx->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		rctx->dst_nents = rctx->src_nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (rctx->src_nents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		dev_err(qce->dev, "Invalid numbers of src SG.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return rctx->src_nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (rctx->dst_nents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		dev_err(qce->dev, "Invalid numbers of dst SG.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return -rctx->dst_nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	rctx->dst_nents += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 						GFP_KERNEL : GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, req->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (IS_ERR(sg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ret = PTR_ERR(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			     QCE_RESULT_BUF_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (IS_ERR(sg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		ret = PTR_ERR(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	sg_mark_end(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	rctx->dst_sg = rctx->dst_tbl.sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	dst_nents = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (dst_nents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ret = dst_nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (diff_dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		src_nents = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (src_nents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			ret = src_nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			goto error_unmap_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		rctx->src_sg = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		rctx->src_sg = rctx->dst_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		src_nents = dst_nents - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			       rctx->dst_sg, dst_nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			       qce_skcipher_done, async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		goto error_unmap_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	qce_dma_issue_pending(&qce->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	ret = qce_start(async_req, tmpl->crypto_alg_type, req->cryptlen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		goto error_terminate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) error_terminate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	qce_dma_terminate_all(&qce->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) error_unmap_src:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (diff_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) error_unmap_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) error_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	sg_free_table(&rctx->dst_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct crypto_tfm *tfm = crypto_skcipher_tfm(ablk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	unsigned long flags = to_cipher_tmpl(ablk)->alg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!key || !keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	switch (IS_XTS(flags) ? keylen >> 1 : keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		memcpy(ctx->enc_key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		ctx->enc_keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int qce_des_setkey(struct crypto_skcipher *ablk, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			  unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(ablk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	err = verify_skcipher_des_key(ablk, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ctx->enc_keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	memcpy(ctx->enc_key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int qce_des3_setkey(struct crypto_skcipher *ablk, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			   unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(ablk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	err = verify_skcipher_des3_key(ablk, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	ctx->enc_keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	memcpy(ctx->enc_key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	int keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	rctx->flags = tmpl->alg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	keylen = IS_XTS(rctx->flags) ? ctx->enc_keylen >> 1 : ctx->enc_keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	/* qce is hanging when AES-XTS request len > QCE_SECTOR_SIZE and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * is not a multiple of it; pass such requests to the fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (IS_AES(rctx->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	    (((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	      req->cryptlen <= aes_sw_max_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	     (IS_XTS(rctx->flags) && req->cryptlen > QCE_SECTOR_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	      req->cryptlen % QCE_SECTOR_SIZE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		skcipher_request_set_callback(&rctx->fallback_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 					      req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 					      req->base.complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 					      req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		skcipher_request_set_crypt(&rctx->fallback_req, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 					   req->dst, req->cryptlen, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				crypto_skcipher_decrypt(&rctx->fallback_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int qce_skcipher_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return qce_skcipher_crypt(req, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int qce_skcipher_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return qce_skcipher_crypt(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int qce_skcipher_init(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	/* take the size without the fallback skcipher_request at the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	crypto_skcipher_set_reqsize(tfm, offsetof(struct qce_cipher_reqctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 						  fallback_req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int qce_skcipher_init_fallback(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 					      0, CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (IS_ERR(ctx->fallback))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return PTR_ERR(ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	crypto_skcipher_set_reqsize(tfm, sizeof(struct qce_cipher_reqctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 					 crypto_skcipher_reqsize(ctx->fallback));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void qce_skcipher_exit(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	crypto_free_skcipher(ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct qce_skcipher_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	const char *drv_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	unsigned int chunksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	unsigned int ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	unsigned int min_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	unsigned int max_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static const struct qce_skcipher_def skcipher_def[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		.flags		= QCE_ALG_AES | QCE_MODE_ECB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		.name		= "ecb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		.drv_name	= "ecb-aes-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		.blocksize	= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		.ivsize		= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		.min_keysize	= AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		.max_keysize	= AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		.flags		= QCE_ALG_AES | QCE_MODE_CBC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		.name		= "cbc(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		.drv_name	= "cbc-aes-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		.blocksize	= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		.ivsize		= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		.min_keysize	= AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		.max_keysize	= AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		.flags		= QCE_ALG_AES | QCE_MODE_CTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		.name		= "ctr(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		.drv_name	= "ctr-aes-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		.blocksize	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		.chunksize	= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		.ivsize		= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		.min_keysize	= AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		.max_keysize	= AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		.flags		= QCE_ALG_AES | QCE_MODE_XTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		.name		= "xts(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		.drv_name	= "xts-aes-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		.blocksize	= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		.ivsize		= AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		.min_keysize	= AES_MIN_KEY_SIZE * 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		.max_keysize	= AES_MAX_KEY_SIZE * 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		.flags		= QCE_ALG_DES | QCE_MODE_ECB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		.name		= "ecb(des)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		.drv_name	= "ecb-des-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		.blocksize	= DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		.ivsize		= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		.min_keysize	= DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		.max_keysize	= DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		.flags		= QCE_ALG_DES | QCE_MODE_CBC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		.name		= "cbc(des)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		.drv_name	= "cbc-des-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		.blocksize	= DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		.ivsize		= DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		.min_keysize	= DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		.max_keysize	= DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		.flags		= QCE_ALG_3DES | QCE_MODE_ECB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		.name		= "ecb(des3_ede)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		.drv_name	= "ecb-3des-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		.blocksize	= DES3_EDE_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		.ivsize		= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		.min_keysize	= DES3_EDE_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		.max_keysize	= DES3_EDE_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		.flags		= QCE_ALG_3DES | QCE_MODE_CBC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		.name		= "cbc(des3_ede)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		.drv_name	= "cbc-3des-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		.blocksize	= DES3_EDE_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		.ivsize		= DES3_EDE_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		.min_keysize	= DES3_EDE_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		.max_keysize	= DES3_EDE_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static int qce_skcipher_register_one(const struct qce_skcipher_def *def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				       struct qce_device *qce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct qce_alg_template *tmpl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (!tmpl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	alg = &tmpl->alg.skcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		 def->drv_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	alg->base.cra_blocksize		= def->blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	alg->chunksize			= def->chunksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	alg->ivsize			= def->ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	alg->min_keysize		= def->min_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	alg->max_keysize		= def->max_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	alg->setkey			= IS_3DES(def->flags) ? qce_des3_setkey :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 					  IS_DES(def->flags) ? qce_des_setkey :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 					  qce_skcipher_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	alg->encrypt			= qce_skcipher_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	alg->decrypt			= qce_skcipher_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	alg->base.cra_priority		= 300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	alg->base.cra_flags		= CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 					  CRYPTO_ALG_ALLOCATES_MEMORY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 					  CRYPTO_ALG_KERN_DRIVER_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	alg->base.cra_ctxsize		= sizeof(struct qce_cipher_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	alg->base.cra_alignmask		= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	alg->base.cra_module		= THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (IS_AES(def->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		alg->base.cra_flags    |= CRYPTO_ALG_NEED_FALLBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		alg->init		= qce_skcipher_init_fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		alg->exit		= qce_skcipher_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		alg->init		= qce_skcipher_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	INIT_LIST_HEAD(&tmpl->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_SKCIPHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	tmpl->alg_flags = def->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	tmpl->qce = qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	ret = crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		dev_err(qce->dev, "%s registration failed\n", alg->base.cra_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		kfree(tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	list_add_tail(&tmpl->entry, &skcipher_algs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	dev_dbg(qce->dev, "%s is registered\n", alg->base.cra_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void qce_skcipher_unregister(struct qce_device *qce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	struct qce_alg_template *tmpl, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	list_for_each_entry_safe(tmpl, n, &skcipher_algs, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		crypto_unregister_skcipher(&tmpl->alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		list_del(&tmpl->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		kfree(tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static int qce_skcipher_register(struct qce_device *qce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	for (i = 0; i < ARRAY_SIZE(skcipher_def); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		ret = qce_skcipher_register_one(&skcipher_def[i], qce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	qce_skcipher_unregister(qce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) const struct qce_algo_ops skcipher_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	.register_algs = qce_skcipher_register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	.unregister_algs = qce_skcipher_unregister,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	.async_req_handle = qce_skcipher_async_req_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) };