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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * sun8i-ss-cipher.c - hardware cryptographic offloader for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Allwinner A80/A83T SoC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * This file add support for AES cipher with 128,192,256 bits keysize in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * CBC and ECB mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * You could find a link for the datasheet in Documentation/arm/sunxi.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/bottom_half.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "sun8i-ss.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static bool sun8i_ss_need_fallback(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct scatterlist *in_sg = areq->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct scatterlist *out_sg = areq->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (areq->cryptlen == 0 || areq->cryptlen % 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (sg_nents(areq->src) > 8 || sg_nents(areq->dst) > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	sg = areq->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	while (sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		if ((sg->length % 16) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		if ((sg_dma_len(sg) % 16) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		if (!IS_ALIGNED(sg->offset, 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	sg = areq->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	while (sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		if ((sg->length % 16) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		if ((sg_dma_len(sg) % 16) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		if (!IS_ALIGNED(sg->offset, 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	/* SS need same numbers of SG (with same length) for source and destination */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	in_sg = areq->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	out_sg = areq->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	while (in_sg && out_sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		if (in_sg->length != out_sg->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		in_sg = sg_next(in_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		out_sg = sg_next(out_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (in_sg || out_sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int sun8i_ss_cipher_fallback(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct sun8i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	algt = container_of(alg, struct sun8i_ss_alg_template, alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	algt->stat_fb++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				      areq->base.complete, areq->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				   areq->cryptlen, areq->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (rctx->op_dir & SS_DECRYPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		err = crypto_skcipher_decrypt(&rctx->fallback_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		err = crypto_skcipher_encrypt(&rctx->fallback_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int sun8i_ss_cipher(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct sun8i_ss_dev *ss = op->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct sun8i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	unsigned int todo, len, offset, ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	void *backup_iv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int nr_sgs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int nr_sgd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	algt = container_of(alg, struct sun8i_ss_alg_template, alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	dev_dbg(ss->dev, "%s %s %u %x IV(%p %u) key=%u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		crypto_tfm_alg_name(areq->base.tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		areq->cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		rctx->op_dir, areq->iv, crypto_skcipher_ivsize(tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		op->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	algt->stat_req++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	rctx->op_mode = ss->variant->op_mode[algt->ss_blockmode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	rctx->method = ss->variant->alg_cipher[algt->ss_algo_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	rctx->keylen = op->keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	rctx->p_key = dma_map_single(ss->dev, op->key, op->keylen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (dma_mapping_error(ss->dev, rctx->p_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		dev_err(ss->dev, "Cannot DMA MAP KEY\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		goto theend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	ivsize = crypto_skcipher_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		rctx->ivlen = ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		rctx->biv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		if (!rctx->biv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			goto theend_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (rctx->op_dir & SS_DECRYPTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			backup_iv = kzalloc(ivsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			if (!backup_iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				goto theend_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			offset = areq->cryptlen - ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			scatterwalk_map_and_copy(backup_iv, areq->src, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 						 ivsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		memcpy(rctx->biv, areq->iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		rctx->p_iv = dma_map_single(ss->dev, rctx->biv, rctx->ivlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 					    DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (dma_mapping_error(ss->dev, rctx->p_iv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			dev_err(ss->dev, "Cannot DMA MAP IV\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			goto theend_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (areq->src == areq->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		nr_sgs = dma_map_sg(ss->dev, areq->src, sg_nents(areq->src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				    DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (nr_sgs <= 0 || nr_sgs > 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			goto theend_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		nr_sgd = nr_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		nr_sgs = dma_map_sg(ss->dev, areq->src, sg_nents(areq->src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				    DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		if (nr_sgs <= 0 || nr_sgs > 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			goto theend_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		nr_sgd = dma_map_sg(ss->dev, areq->dst, sg_nents(areq->dst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				    DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (nr_sgd <= 0 || nr_sgd > 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			dev_err(ss->dev, "Invalid sg number %d\n", nr_sgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			goto theend_sgs;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	len = areq->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	sg = areq->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	while (i < nr_sgs && sg && len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		if (sg_dma_len(sg) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			goto sgs_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		rctx->t_src[i].addr = sg_dma_address(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		todo = min(len, sg_dma_len(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		rctx->t_src[i].len = todo / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		dev_dbg(ss->dev, "%s total=%u SGS(%d %u off=%d) todo=%u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			areq->cryptlen, i, rctx->t_src[i].len, sg->offset, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		len -= todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) sgs_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		dev_err(ss->dev, "remaining len %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto theend_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	len = areq->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	sg = areq->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	while (i < nr_sgd && sg && len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		if (sg_dma_len(sg) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			goto sgd_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		rctx->t_dst[i].addr = sg_dma_address(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		todo = min(len, sg_dma_len(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		rctx->t_dst[i].len = todo / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		dev_dbg(ss->dev, "%s total=%u SGD(%d %u off=%d) todo=%u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			areq->cryptlen, i, rctx->t_dst[i].len, sg->offset, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		len -= todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) sgd_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		dev_err(ss->dev, "remaining len %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		goto theend_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	err = sun8i_ss_run_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) theend_sgs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (areq->src == areq->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		dma_unmap_sg(ss->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		dma_unmap_sg(ss->dev, areq->src, nr_sgs, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		dma_unmap_sg(ss->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) theend_iv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (rctx->p_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		dma_unmap_single(ss->dev, rctx->p_iv, rctx->ivlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				 DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (areq->iv && ivsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (rctx->biv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			offset = areq->cryptlen - ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			if (rctx->op_dir & SS_DECRYPTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				memcpy(areq->iv, backup_iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				kfree_sensitive(backup_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 							 ivsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			kfree(rctx->biv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) theend_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	dma_unmap_single(ss->dev, rctx->p_key, op->keylen, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) theend:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return err;
^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 sun8i_ss_handle_cipher_request(struct crypto_engine *engine, void *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	err = sun8i_ss_cipher(breq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	crypto_finalize_skcipher_request(engine, breq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int sun8i_ss_skdecrypt(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct crypto_engine *engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	int e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	memset(rctx, 0, sizeof(struct sun8i_cipher_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	rctx->op_dir = SS_DECRYPTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (sun8i_ss_need_fallback(areq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return sun8i_ss_cipher_fallback(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	e = sun8i_ss_get_engine_number(op->ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	engine = op->ss->flows[e].engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	rctx->flow = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	return crypto_transfer_skcipher_request_to_engine(engine, areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int sun8i_ss_skencrypt(struct skcipher_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct crypto_engine *engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	memset(rctx, 0, sizeof(struct sun8i_cipher_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	rctx->op_dir = SS_ENCRYPTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (sun8i_ss_need_fallback(areq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return sun8i_ss_cipher_fallback(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	e = sun8i_ss_get_engine_number(op->ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	engine = op->ss->flows[e].engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	rctx->flow = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return crypto_transfer_skcipher_request_to_engine(engine, areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int sun8i_ss_cipher_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct sun8i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	const char *name = crypto_tfm_alg_name(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct skcipher_alg *alg = crypto_skcipher_alg(sktfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	memset(op, 0, sizeof(struct sun8i_cipher_tfm_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	algt = container_of(alg, struct sun8i_ss_alg_template, alg.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	op->ss = algt->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (IS_ERR(op->fallback_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			name, PTR_ERR(op->fallback_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return PTR_ERR(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			 crypto_skcipher_reqsize(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	dev_info(op->ss->dev, "Fallback for %s is %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		 crypto_tfm_alg_driver_name(&sktfm->base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(op->fallback_tfm)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	op->enginectx.op.do_one_request = sun8i_ss_handle_cipher_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	op->enginectx.op.prepare_request = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	op->enginectx.op.unprepare_request = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	err = pm_runtime_resume_and_get(op->ss->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		dev_err(op->ss->dev, "pm error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		goto error_pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) error_pm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	crypto_free_skcipher(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) void sun8i_ss_cipher_exit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	kfree_sensitive(op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	crypto_free_skcipher(op->fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	pm_runtime_put_sync(op->ss->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct sun8i_ss_dev *ss = op->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	switch (keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	case 128 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	case 192 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	case 256 / 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		dev_dbg(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	kfree_sensitive(op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	op->keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (!op->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	struct sun8i_ss_dev *ss = op->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (unlikely(keylen != 3 * DES_KEY_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		dev_dbg(ss->dev, "Invalid keylen %u\n", keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	kfree_sensitive(op->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	op->keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (!op->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }