xref: /openbmc/linux/drivers/crypto/rockchip/rk3288_crypto_skcipher.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1ce0183cbSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0-only
2ce0183cbSArd Biesheuvel /*
3ce0183cbSArd Biesheuvel  * Crypto acceleration support for Rockchip RK3288
4ce0183cbSArd Biesheuvel  *
5ce0183cbSArd Biesheuvel  * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
6ce0183cbSArd Biesheuvel  *
7ce0183cbSArd Biesheuvel  * Author: Zain Wang <zain.wang@rock-chips.com>
8ce0183cbSArd Biesheuvel  *
9ce0183cbSArd Biesheuvel  * Some ideas are from marvell-cesa.c and s5p-sss.c driver.
10ce0183cbSArd Biesheuvel  */
11*1a15d26cSHerbert Xu 
12*1a15d26cSHerbert Xu #include <crypto/engine.h>
13*1a15d26cSHerbert Xu #include <crypto/internal/skcipher.h>
1457d67c6eSCorentin Labbe #include <crypto/scatterwalk.h>
15*1a15d26cSHerbert Xu #include <linux/device.h>
16*1a15d26cSHerbert Xu #include <linux/err.h>
17*1a15d26cSHerbert Xu #include <linux/kernel.h>
18*1a15d26cSHerbert Xu #include <linux/string.h>
19ce0183cbSArd Biesheuvel #include "rk3288_crypto.h"
20ce0183cbSArd Biesheuvel 
21ce0183cbSArd Biesheuvel #define RK_CRYPTO_DEC			BIT(0)
22ce0183cbSArd Biesheuvel 
rk_cipher_need_fallback(struct skcipher_request * req)2368ef8af0SCorentin Labbe static int rk_cipher_need_fallback(struct skcipher_request *req)
2468ef8af0SCorentin Labbe {
2568ef8af0SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2648d904d4SCorentin Labbe 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
27*1a15d26cSHerbert Xu 	struct rk_crypto_tmp *algt = container_of(alg, struct rk_crypto_tmp, alg.skcipher.base);
2868ef8af0SCorentin Labbe 	struct scatterlist *sgs, *sgd;
2968ef8af0SCorentin Labbe 	unsigned int stodo, dtodo, len;
309dcd71c8SCorentin Labbe 	unsigned int bs = crypto_skcipher_blocksize(tfm);
3168ef8af0SCorentin Labbe 
3268ef8af0SCorentin Labbe 	if (!req->cryptlen)
3368ef8af0SCorentin Labbe 		return true;
3468ef8af0SCorentin Labbe 
3568ef8af0SCorentin Labbe 	len = req->cryptlen;
3668ef8af0SCorentin Labbe 	sgs = req->src;
3768ef8af0SCorentin Labbe 	sgd = req->dst;
3868ef8af0SCorentin Labbe 	while (sgs && sgd) {
3968ef8af0SCorentin Labbe 		if (!IS_ALIGNED(sgs->offset, sizeof(u32))) {
4048d904d4SCorentin Labbe 			algt->stat_fb_align++;
4168ef8af0SCorentin Labbe 			return true;
4268ef8af0SCorentin Labbe 		}
4368ef8af0SCorentin Labbe 		if (!IS_ALIGNED(sgd->offset, sizeof(u32))) {
4448d904d4SCorentin Labbe 			algt->stat_fb_align++;
4568ef8af0SCorentin Labbe 			return true;
4668ef8af0SCorentin Labbe 		}
4768ef8af0SCorentin Labbe 		stodo = min(len, sgs->length);
4868ef8af0SCorentin Labbe 		if (stodo % bs) {
4948d904d4SCorentin Labbe 			algt->stat_fb_len++;
5068ef8af0SCorentin Labbe 			return true;
5168ef8af0SCorentin Labbe 		}
5268ef8af0SCorentin Labbe 		dtodo = min(len, sgd->length);
5368ef8af0SCorentin Labbe 		if (dtodo % bs) {
5448d904d4SCorentin Labbe 			algt->stat_fb_len++;
5568ef8af0SCorentin Labbe 			return true;
5668ef8af0SCorentin Labbe 		}
5768ef8af0SCorentin Labbe 		if (stodo != dtodo) {
5848d904d4SCorentin Labbe 			algt->stat_fb_sgdiff++;
5968ef8af0SCorentin Labbe 			return true;
6068ef8af0SCorentin Labbe 		}
6168ef8af0SCorentin Labbe 		len -= stodo;
6268ef8af0SCorentin Labbe 		sgs = sg_next(sgs);
6368ef8af0SCorentin Labbe 		sgd = sg_next(sgd);
6468ef8af0SCorentin Labbe 	}
6568ef8af0SCorentin Labbe 	return false;
6668ef8af0SCorentin Labbe }
6768ef8af0SCorentin Labbe 
rk_cipher_fallback(struct skcipher_request * areq)6868ef8af0SCorentin Labbe static int rk_cipher_fallback(struct skcipher_request *areq)
6968ef8af0SCorentin Labbe {
7068ef8af0SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
7168ef8af0SCorentin Labbe 	struct rk_cipher_ctx *op = crypto_skcipher_ctx(tfm);
7268ef8af0SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(areq);
7348d904d4SCorentin Labbe 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
74*1a15d26cSHerbert Xu 	struct rk_crypto_tmp *algt = container_of(alg, struct rk_crypto_tmp, alg.skcipher.base);
7568ef8af0SCorentin Labbe 	int err;
7668ef8af0SCorentin Labbe 
7748d904d4SCorentin Labbe 	algt->stat_fb++;
7848d904d4SCorentin Labbe 
7968ef8af0SCorentin Labbe 	skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
8068ef8af0SCorentin Labbe 	skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
8168ef8af0SCorentin Labbe 				      areq->base.complete, areq->base.data);
8268ef8af0SCorentin Labbe 	skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
8368ef8af0SCorentin Labbe 				   areq->cryptlen, areq->iv);
8468ef8af0SCorentin Labbe 	if (rctx->mode & RK_CRYPTO_DEC)
8568ef8af0SCorentin Labbe 		err = crypto_skcipher_decrypt(&rctx->fallback_req);
8668ef8af0SCorentin Labbe 	else
8768ef8af0SCorentin Labbe 		err = crypto_skcipher_encrypt(&rctx->fallback_req);
8868ef8af0SCorentin Labbe 	return err;
8968ef8af0SCorentin Labbe }
9068ef8af0SCorentin Labbe 
rk_cipher_handle_req(struct skcipher_request * req)912e3b1495SCorentin Labbe static int rk_cipher_handle_req(struct skcipher_request *req)
92ce0183cbSArd Biesheuvel {
932d3c756aSCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
949dcd71c8SCorentin Labbe 	struct rk_crypto_info *rkc;
959dcd71c8SCorentin Labbe 	struct crypto_engine *engine;
9657d67c6eSCorentin Labbe 
9768ef8af0SCorentin Labbe 	if (rk_cipher_need_fallback(req))
9868ef8af0SCorentin Labbe 		return rk_cipher_fallback(req);
9968ef8af0SCorentin Labbe 
1009dcd71c8SCorentin Labbe 	rkc = get_rk_crypto();
1019dcd71c8SCorentin Labbe 
1029dcd71c8SCorentin Labbe 	engine = rkc->engine;
1032d3c756aSCorentin Labbe 	rctx->dev = rkc;
1042d3c756aSCorentin Labbe 
10557d67c6eSCorentin Labbe 	return crypto_transfer_skcipher_request_to_engine(engine, req);
106ce0183cbSArd Biesheuvel }
107ce0183cbSArd Biesheuvel 
rk_aes_setkey(struct crypto_skcipher * cipher,const u8 * key,unsigned int keylen)108ce0183cbSArd Biesheuvel static int rk_aes_setkey(struct crypto_skcipher *cipher,
109ce0183cbSArd Biesheuvel 			 const u8 *key, unsigned int keylen)
110ce0183cbSArd Biesheuvel {
111ce0183cbSArd Biesheuvel 	struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
112ce0183cbSArd Biesheuvel 	struct rk_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
113ce0183cbSArd Biesheuvel 
114ce0183cbSArd Biesheuvel 	if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
115674f368aSEric Biggers 	    keylen != AES_KEYSIZE_256)
116ce0183cbSArd Biesheuvel 		return -EINVAL;
117ce0183cbSArd Biesheuvel 	ctx->keylen = keylen;
118d6b23cceSCorentin Labbe 	memcpy(ctx->key, key, keylen);
11968ef8af0SCorentin Labbe 
12068ef8af0SCorentin Labbe 	return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen);
121ce0183cbSArd Biesheuvel }
122ce0183cbSArd Biesheuvel 
rk_des_setkey(struct crypto_skcipher * cipher,const u8 * key,unsigned int keylen)123ce0183cbSArd Biesheuvel static int rk_des_setkey(struct crypto_skcipher *cipher,
124ce0183cbSArd Biesheuvel 			 const u8 *key, unsigned int keylen)
125ce0183cbSArd Biesheuvel {
126ce0183cbSArd Biesheuvel 	struct rk_cipher_ctx *ctx = crypto_skcipher_ctx(cipher);
127ce0183cbSArd Biesheuvel 	int err;
128ce0183cbSArd Biesheuvel 
129ce0183cbSArd Biesheuvel 	err = verify_skcipher_des_key(cipher, key);
130ce0183cbSArd Biesheuvel 	if (err)
131ce0183cbSArd Biesheuvel 		return err;
132ce0183cbSArd Biesheuvel 
133ce0183cbSArd Biesheuvel 	ctx->keylen = keylen;
134d6b23cceSCorentin Labbe 	memcpy(ctx->key, key, keylen);
13568ef8af0SCorentin Labbe 
13668ef8af0SCorentin Labbe 	return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen);
137ce0183cbSArd Biesheuvel }
138ce0183cbSArd Biesheuvel 
rk_tdes_setkey(struct crypto_skcipher * cipher,const u8 * key,unsigned int keylen)139ce0183cbSArd Biesheuvel static int rk_tdes_setkey(struct crypto_skcipher *cipher,
140ce0183cbSArd Biesheuvel 			  const u8 *key, unsigned int keylen)
141ce0183cbSArd Biesheuvel {
142ce0183cbSArd Biesheuvel 	struct rk_cipher_ctx *ctx = crypto_skcipher_ctx(cipher);
143ce0183cbSArd Biesheuvel 	int err;
144ce0183cbSArd Biesheuvel 
145ce0183cbSArd Biesheuvel 	err = verify_skcipher_des3_key(cipher, key);
146ce0183cbSArd Biesheuvel 	if (err)
147ce0183cbSArd Biesheuvel 		return err;
148ce0183cbSArd Biesheuvel 
149ce0183cbSArd Biesheuvel 	ctx->keylen = keylen;
150d6b23cceSCorentin Labbe 	memcpy(ctx->key, key, keylen);
151d6b23cceSCorentin Labbe 
15268ef8af0SCorentin Labbe 	return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen);
153ce0183cbSArd Biesheuvel }
154ce0183cbSArd Biesheuvel 
rk_aes_ecb_encrypt(struct skcipher_request * req)155ce0183cbSArd Biesheuvel static int rk_aes_ecb_encrypt(struct skcipher_request *req)
156ce0183cbSArd Biesheuvel {
15787e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
158ce0183cbSArd Biesheuvel 
15987e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_AES_ECB_MODE;
1602e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
161ce0183cbSArd Biesheuvel }
162ce0183cbSArd Biesheuvel 
rk_aes_ecb_decrypt(struct skcipher_request * req)163ce0183cbSArd Biesheuvel static int rk_aes_ecb_decrypt(struct skcipher_request *req)
164ce0183cbSArd Biesheuvel {
16587e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
166ce0183cbSArd Biesheuvel 
16787e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_AES_ECB_MODE | RK_CRYPTO_DEC;
1682e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
169ce0183cbSArd Biesheuvel }
170ce0183cbSArd Biesheuvel 
rk_aes_cbc_encrypt(struct skcipher_request * req)171ce0183cbSArd Biesheuvel static int rk_aes_cbc_encrypt(struct skcipher_request *req)
172ce0183cbSArd Biesheuvel {
17387e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
174ce0183cbSArd Biesheuvel 
17587e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_AES_CBC_MODE;
1762e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
177ce0183cbSArd Biesheuvel }
178ce0183cbSArd Biesheuvel 
rk_aes_cbc_decrypt(struct skcipher_request * req)179ce0183cbSArd Biesheuvel static int rk_aes_cbc_decrypt(struct skcipher_request *req)
180ce0183cbSArd Biesheuvel {
18187e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
182ce0183cbSArd Biesheuvel 
18387e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_AES_CBC_MODE | RK_CRYPTO_DEC;
1842e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
185ce0183cbSArd Biesheuvel }
186ce0183cbSArd Biesheuvel 
rk_des_ecb_encrypt(struct skcipher_request * req)187ce0183cbSArd Biesheuvel static int rk_des_ecb_encrypt(struct skcipher_request *req)
188ce0183cbSArd Biesheuvel {
18987e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
190ce0183cbSArd Biesheuvel 
19187e356c4SCorentin Labbe 	rctx->mode = 0;
1922e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
193ce0183cbSArd Biesheuvel }
194ce0183cbSArd Biesheuvel 
rk_des_ecb_decrypt(struct skcipher_request * req)195ce0183cbSArd Biesheuvel static int rk_des_ecb_decrypt(struct skcipher_request *req)
196ce0183cbSArd Biesheuvel {
19787e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
198ce0183cbSArd Biesheuvel 
19987e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_DEC;
2002e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
201ce0183cbSArd Biesheuvel }
202ce0183cbSArd Biesheuvel 
rk_des_cbc_encrypt(struct skcipher_request * req)203ce0183cbSArd Biesheuvel static int rk_des_cbc_encrypt(struct skcipher_request *req)
204ce0183cbSArd Biesheuvel {
20587e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
206ce0183cbSArd Biesheuvel 
20787e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_TDES_CHAINMODE_CBC;
2082e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
209ce0183cbSArd Biesheuvel }
210ce0183cbSArd Biesheuvel 
rk_des_cbc_decrypt(struct skcipher_request * req)211ce0183cbSArd Biesheuvel static int rk_des_cbc_decrypt(struct skcipher_request *req)
212ce0183cbSArd Biesheuvel {
21387e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
214ce0183cbSArd Biesheuvel 
21587e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_TDES_CHAINMODE_CBC | RK_CRYPTO_DEC;
2162e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
217ce0183cbSArd Biesheuvel }
218ce0183cbSArd Biesheuvel 
rk_des3_ede_ecb_encrypt(struct skcipher_request * req)219ce0183cbSArd Biesheuvel static int rk_des3_ede_ecb_encrypt(struct skcipher_request *req)
220ce0183cbSArd Biesheuvel {
22187e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
222ce0183cbSArd Biesheuvel 
22387e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_TDES_SELECT;
2242e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
225ce0183cbSArd Biesheuvel }
226ce0183cbSArd Biesheuvel 
rk_des3_ede_ecb_decrypt(struct skcipher_request * req)227ce0183cbSArd Biesheuvel static int rk_des3_ede_ecb_decrypt(struct skcipher_request *req)
228ce0183cbSArd Biesheuvel {
22987e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
230ce0183cbSArd Biesheuvel 
23187e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_TDES_SELECT | RK_CRYPTO_DEC;
2322e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
233ce0183cbSArd Biesheuvel }
234ce0183cbSArd Biesheuvel 
rk_des3_ede_cbc_encrypt(struct skcipher_request * req)235ce0183cbSArd Biesheuvel static int rk_des3_ede_cbc_encrypt(struct skcipher_request *req)
236ce0183cbSArd Biesheuvel {
23787e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
238ce0183cbSArd Biesheuvel 
23987e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_TDES_SELECT | RK_CRYPTO_TDES_CHAINMODE_CBC;
2402e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
241ce0183cbSArd Biesheuvel }
242ce0183cbSArd Biesheuvel 
rk_des3_ede_cbc_decrypt(struct skcipher_request * req)243ce0183cbSArd Biesheuvel static int rk_des3_ede_cbc_decrypt(struct skcipher_request *req)
244ce0183cbSArd Biesheuvel {
24587e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
246ce0183cbSArd Biesheuvel 
24787e356c4SCorentin Labbe 	rctx->mode = RK_CRYPTO_TDES_SELECT | RK_CRYPTO_TDES_CHAINMODE_CBC |
248ce0183cbSArd Biesheuvel 		    RK_CRYPTO_DEC;
2492e3b1495SCorentin Labbe 	return rk_cipher_handle_req(req);
250ce0183cbSArd Biesheuvel }
251ce0183cbSArd Biesheuvel 
rk_cipher_hw_init(struct rk_crypto_info * dev,struct skcipher_request * req)252a7fa0644SCorentin Labbe static void rk_cipher_hw_init(struct rk_crypto_info *dev, struct skcipher_request *req)
253ce0183cbSArd Biesheuvel {
254ce0183cbSArd Biesheuvel 	struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
255ce0183cbSArd Biesheuvel 	struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
25687e356c4SCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(req);
257ce0183cbSArd Biesheuvel 	struct rk_cipher_ctx *ctx = crypto_skcipher_ctx(cipher);
25857d67c6eSCorentin Labbe 	u32 block, conf_reg = 0;
259ce0183cbSArd Biesheuvel 
260ce0183cbSArd Biesheuvel 	block = crypto_tfm_alg_blocksize(tfm);
261ce0183cbSArd Biesheuvel 
262ce0183cbSArd Biesheuvel 	if (block == DES_BLOCK_SIZE) {
26387e356c4SCorentin Labbe 		rctx->mode |= RK_CRYPTO_TDES_FIFO_MODE |
264ce0183cbSArd Biesheuvel 			     RK_CRYPTO_TDES_BYTESWAP_KEY |
265ce0183cbSArd Biesheuvel 			     RK_CRYPTO_TDES_BYTESWAP_IV;
26687e356c4SCorentin Labbe 		CRYPTO_WRITE(dev, RK_CRYPTO_TDES_CTRL, rctx->mode);
267ea389be9SCorentin Labbe 		memcpy_toio(dev->reg + RK_CRYPTO_TDES_KEY1_0, ctx->key, ctx->keylen);
268ce0183cbSArd Biesheuvel 		conf_reg = RK_CRYPTO_DESSEL;
269ce0183cbSArd Biesheuvel 	} else {
27087e356c4SCorentin Labbe 		rctx->mode |= RK_CRYPTO_AES_FIFO_MODE |
271ce0183cbSArd Biesheuvel 			     RK_CRYPTO_AES_KEY_CHANGE |
272ce0183cbSArd Biesheuvel 			     RK_CRYPTO_AES_BYTESWAP_KEY |
273ce0183cbSArd Biesheuvel 			     RK_CRYPTO_AES_BYTESWAP_IV;
274ce0183cbSArd Biesheuvel 		if (ctx->keylen == AES_KEYSIZE_192)
27587e356c4SCorentin Labbe 			rctx->mode |= RK_CRYPTO_AES_192BIT_key;
276ce0183cbSArd Biesheuvel 		else if (ctx->keylen == AES_KEYSIZE_256)
27787e356c4SCorentin Labbe 			rctx->mode |= RK_CRYPTO_AES_256BIT_key;
27887e356c4SCorentin Labbe 		CRYPTO_WRITE(dev, RK_CRYPTO_AES_CTRL, rctx->mode);
279ea389be9SCorentin Labbe 		memcpy_toio(dev->reg + RK_CRYPTO_AES_KEY_0, ctx->key, ctx->keylen);
280ce0183cbSArd Biesheuvel 	}
281ce0183cbSArd Biesheuvel 	conf_reg |= RK_CRYPTO_BYTESWAP_BTFIFO |
282ce0183cbSArd Biesheuvel 		    RK_CRYPTO_BYTESWAP_BRFIFO;
283ce0183cbSArd Biesheuvel 	CRYPTO_WRITE(dev, RK_CRYPTO_CONF, conf_reg);
284ce0183cbSArd Biesheuvel 	CRYPTO_WRITE(dev, RK_CRYPTO_INTENA,
285ce0183cbSArd Biesheuvel 		     RK_CRYPTO_BCDMA_ERR_ENA | RK_CRYPTO_BCDMA_DONE_ENA);
286ce0183cbSArd Biesheuvel }
287ce0183cbSArd Biesheuvel 
crypto_dma_start(struct rk_crypto_info * dev,struct scatterlist * sgs,struct scatterlist * sgd,unsigned int todo)28857d67c6eSCorentin Labbe static void crypto_dma_start(struct rk_crypto_info *dev,
28957d67c6eSCorentin Labbe 			     struct scatterlist *sgs,
29057d67c6eSCorentin Labbe 			     struct scatterlist *sgd, unsigned int todo)
291ce0183cbSArd Biesheuvel {
29257d67c6eSCorentin Labbe 	CRYPTO_WRITE(dev, RK_CRYPTO_BRDMAS, sg_dma_address(sgs));
29357d67c6eSCorentin Labbe 	CRYPTO_WRITE(dev, RK_CRYPTO_BRDMAL, todo);
29457d67c6eSCorentin Labbe 	CRYPTO_WRITE(dev, RK_CRYPTO_BTDMAS, sg_dma_address(sgd));
295ce0183cbSArd Biesheuvel 	CRYPTO_WRITE(dev, RK_CRYPTO_CTRL, RK_CRYPTO_BLOCK_START |
296ce0183cbSArd Biesheuvel 		     _SBF(RK_CRYPTO_BLOCK_START, 16));
297ce0183cbSArd Biesheuvel }
298ce0183cbSArd Biesheuvel 
rk_cipher_run(struct crypto_engine * engine,void * async_req)29957d67c6eSCorentin Labbe static int rk_cipher_run(struct crypto_engine *engine, void *async_req)
300ce0183cbSArd Biesheuvel {
30157d67c6eSCorentin Labbe 	struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
30257d67c6eSCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
30357d67c6eSCorentin Labbe 	struct rk_cipher_rctx *rctx = skcipher_request_ctx(areq);
30457d67c6eSCorentin Labbe 	struct scatterlist *sgs, *sgd;
305ce0183cbSArd Biesheuvel 	int err = 0;
30657d67c6eSCorentin Labbe 	int ivsize = crypto_skcipher_ivsize(tfm);
30757d67c6eSCorentin Labbe 	int offset;
30857d67c6eSCorentin Labbe 	u8 iv[AES_BLOCK_SIZE];
30957d67c6eSCorentin Labbe 	u8 biv[AES_BLOCK_SIZE];
31057d67c6eSCorentin Labbe 	u8 *ivtouse = areq->iv;
31157d67c6eSCorentin Labbe 	unsigned int len = areq->cryptlen;
31257d67c6eSCorentin Labbe 	unsigned int todo;
31348d904d4SCorentin Labbe 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
314*1a15d26cSHerbert Xu 	struct rk_crypto_tmp *algt = container_of(alg, struct rk_crypto_tmp, alg.skcipher.base);
3152d3c756aSCorentin Labbe 	struct rk_crypto_info *rkc = rctx->dev;
31648d904d4SCorentin Labbe 
3179dcd71c8SCorentin Labbe 	err = pm_runtime_resume_and_get(rkc->dev);
3189dcd71c8SCorentin Labbe 	if (err)
3199dcd71c8SCorentin Labbe 		return err;
3209dcd71c8SCorentin Labbe 
32148d904d4SCorentin Labbe 	algt->stat_req++;
3229dcd71c8SCorentin Labbe 	rkc->nreq++;
323ce0183cbSArd Biesheuvel 
32457d67c6eSCorentin Labbe 	ivsize = crypto_skcipher_ivsize(tfm);
32557d67c6eSCorentin Labbe 	if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
32687e356c4SCorentin Labbe 		if (rctx->mode & RK_CRYPTO_DEC) {
32757d67c6eSCorentin Labbe 			offset = areq->cryptlen - ivsize;
32857d67c6eSCorentin Labbe 			scatterwalk_map_and_copy(rctx->backup_iv, areq->src,
32957d67c6eSCorentin Labbe 						 offset, ivsize, 0);
33057d67c6eSCorentin Labbe 		}
331ce0183cbSArd Biesheuvel 	}
332ce0183cbSArd Biesheuvel 
33357d67c6eSCorentin Labbe 	sgs = areq->src;
33457d67c6eSCorentin Labbe 	sgd = areq->dst;
33557d67c6eSCorentin Labbe 
33657d67c6eSCorentin Labbe 	while (sgs && sgd && len) {
33757d67c6eSCorentin Labbe 		if (!sgs->length) {
33857d67c6eSCorentin Labbe 			sgs = sg_next(sgs);
33957d67c6eSCorentin Labbe 			sgd = sg_next(sgd);
34057d67c6eSCorentin Labbe 			continue;
34157d67c6eSCorentin Labbe 		}
34257d67c6eSCorentin Labbe 		if (rctx->mode & RK_CRYPTO_DEC) {
34357d67c6eSCorentin Labbe 			/* we backup last block of source to be used as IV at next step */
34457d67c6eSCorentin Labbe 			offset = sgs->length - ivsize;
34557d67c6eSCorentin Labbe 			scatterwalk_map_and_copy(biv, sgs, offset, ivsize, 0);
34657d67c6eSCorentin Labbe 		}
34757d67c6eSCorentin Labbe 		if (sgs == sgd) {
348c018c7a9SCorentin Labbe 			err = dma_map_sg(rkc->dev, sgs, 1, DMA_BIDIRECTIONAL);
34957d67c6eSCorentin Labbe 			if (err <= 0) {
35057d67c6eSCorentin Labbe 				err = -EINVAL;
35157d67c6eSCorentin Labbe 				goto theend_iv;
35257d67c6eSCorentin Labbe 			}
35357d67c6eSCorentin Labbe 		} else {
354c018c7a9SCorentin Labbe 			err = dma_map_sg(rkc->dev, sgs, 1, DMA_TO_DEVICE);
35557d67c6eSCorentin Labbe 			if (err <= 0) {
35657d67c6eSCorentin Labbe 				err = -EINVAL;
35757d67c6eSCorentin Labbe 				goto theend_iv;
35857d67c6eSCorentin Labbe 			}
359c018c7a9SCorentin Labbe 			err = dma_map_sg(rkc->dev, sgd, 1, DMA_FROM_DEVICE);
36057d67c6eSCorentin Labbe 			if (err <= 0) {
36157d67c6eSCorentin Labbe 				err = -EINVAL;
36257d67c6eSCorentin Labbe 				goto theend_sgs;
36357d67c6eSCorentin Labbe 			}
36457d67c6eSCorentin Labbe 		}
36557d67c6eSCorentin Labbe 		err = 0;
366c018c7a9SCorentin Labbe 		rk_cipher_hw_init(rkc, areq);
36757d67c6eSCorentin Labbe 		if (ivsize) {
368ce0183cbSArd Biesheuvel 			if (ivsize == DES_BLOCK_SIZE)
369c018c7a9SCorentin Labbe 				memcpy_toio(rkc->reg + RK_CRYPTO_TDES_IV_0, ivtouse, ivsize);
37057d67c6eSCorentin Labbe 			else
371c018c7a9SCorentin Labbe 				memcpy_toio(rkc->reg + RK_CRYPTO_AES_IV_0, ivtouse, ivsize);
372ce0183cbSArd Biesheuvel 		}
373c018c7a9SCorentin Labbe 		reinit_completion(&rkc->complete);
374c018c7a9SCorentin Labbe 		rkc->status = 0;
375ce0183cbSArd Biesheuvel 
37657d67c6eSCorentin Labbe 		todo = min(sg_dma_len(sgs), len);
37757d67c6eSCorentin Labbe 		len -= todo;
378c018c7a9SCorentin Labbe 		crypto_dma_start(rkc, sgs, sgd, todo / 4);
379c018c7a9SCorentin Labbe 		wait_for_completion_interruptible_timeout(&rkc->complete,
38057d67c6eSCorentin Labbe 							  msecs_to_jiffies(2000));
381c018c7a9SCorentin Labbe 		if (!rkc->status) {
382c018c7a9SCorentin Labbe 			dev_err(rkc->dev, "DMA timeout\n");
38357d67c6eSCorentin Labbe 			err = -EFAULT;
38457d67c6eSCorentin Labbe 			goto theend;
385ce0183cbSArd Biesheuvel 		}
38657d67c6eSCorentin Labbe 		if (sgs == sgd) {
387c018c7a9SCorentin Labbe 			dma_unmap_sg(rkc->dev, sgs, 1, DMA_BIDIRECTIONAL);
388ce0183cbSArd Biesheuvel 		} else {
389c018c7a9SCorentin Labbe 			dma_unmap_sg(rkc->dev, sgs, 1, DMA_TO_DEVICE);
390c018c7a9SCorentin Labbe 			dma_unmap_sg(rkc->dev, sgd, 1, DMA_FROM_DEVICE);
391ce0183cbSArd Biesheuvel 		}
39257d67c6eSCorentin Labbe 		if (rctx->mode & RK_CRYPTO_DEC) {
39357d67c6eSCorentin Labbe 			memcpy(iv, biv, ivsize);
39457d67c6eSCorentin Labbe 			ivtouse = iv;
39557d67c6eSCorentin Labbe 		} else {
39657d67c6eSCorentin Labbe 			offset = sgd->length - ivsize;
39757d67c6eSCorentin Labbe 			scatterwalk_map_and_copy(iv, sgd, offset, ivsize, 0);
39857d67c6eSCorentin Labbe 			ivtouse = iv;
39957d67c6eSCorentin Labbe 		}
40057d67c6eSCorentin Labbe 		sgs = sg_next(sgs);
40157d67c6eSCorentin Labbe 		sgd = sg_next(sgd);
40257d67c6eSCorentin Labbe 	}
40357d67c6eSCorentin Labbe 
40457d67c6eSCorentin Labbe 	if (areq->iv && ivsize > 0) {
40557d67c6eSCorentin Labbe 		offset = areq->cryptlen - ivsize;
40657d67c6eSCorentin Labbe 		if (rctx->mode & RK_CRYPTO_DEC) {
40757d67c6eSCorentin Labbe 			memcpy(areq->iv, rctx->backup_iv, ivsize);
40857d67c6eSCorentin Labbe 			memzero_explicit(rctx->backup_iv, ivsize);
40957d67c6eSCorentin Labbe 		} else {
41057d67c6eSCorentin Labbe 			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
41157d67c6eSCorentin Labbe 						 ivsize, 0);
41257d67c6eSCorentin Labbe 		}
41357d67c6eSCorentin Labbe 	}
41457d67c6eSCorentin Labbe 
41557d67c6eSCorentin Labbe theend:
4169dcd71c8SCorentin Labbe 	pm_runtime_put_autosuspend(rkc->dev);
4179dcd71c8SCorentin Labbe 
41857d67c6eSCorentin Labbe 	local_bh_disable();
41957d67c6eSCorentin Labbe 	crypto_finalize_skcipher_request(engine, areq, err);
42057d67c6eSCorentin Labbe 	local_bh_enable();
42157d67c6eSCorentin Labbe 	return 0;
42257d67c6eSCorentin Labbe 
42357d67c6eSCorentin Labbe theend_sgs:
42457d67c6eSCorentin Labbe 	if (sgs == sgd) {
425c018c7a9SCorentin Labbe 		dma_unmap_sg(rkc->dev, sgs, 1, DMA_BIDIRECTIONAL);
42657d67c6eSCorentin Labbe 	} else {
427c018c7a9SCorentin Labbe 		dma_unmap_sg(rkc->dev, sgs, 1, DMA_TO_DEVICE);
428c018c7a9SCorentin Labbe 		dma_unmap_sg(rkc->dev, sgd, 1, DMA_FROM_DEVICE);
42957d67c6eSCorentin Labbe 	}
43057d67c6eSCorentin Labbe theend_iv:
431ce0183cbSArd Biesheuvel 	return err;
432ce0183cbSArd Biesheuvel }
433ce0183cbSArd Biesheuvel 
rk_cipher_tfm_init(struct crypto_skcipher * tfm)434a7fa0644SCorentin Labbe static int rk_cipher_tfm_init(struct crypto_skcipher *tfm)
435ce0183cbSArd Biesheuvel {
436ce0183cbSArd Biesheuvel 	struct rk_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
43768ef8af0SCorentin Labbe 	const char *name = crypto_tfm_alg_name(&tfm->base);
4389dcd71c8SCorentin Labbe 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
439*1a15d26cSHerbert Xu 	struct rk_crypto_tmp *algt = container_of(alg, struct rk_crypto_tmp, alg.skcipher.base);
440ce0183cbSArd Biesheuvel 
44168ef8af0SCorentin Labbe 	ctx->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
44268ef8af0SCorentin Labbe 	if (IS_ERR(ctx->fallback_tfm)) {
4439dcd71c8SCorentin Labbe 		dev_err(algt->dev->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
44468ef8af0SCorentin Labbe 			name, PTR_ERR(ctx->fallback_tfm));
44568ef8af0SCorentin Labbe 		return PTR_ERR(ctx->fallback_tfm);
44668ef8af0SCorentin Labbe 	}
44768ef8af0SCorentin Labbe 
44868ef8af0SCorentin Labbe 	tfm->reqsize = sizeof(struct rk_cipher_rctx) +
44968ef8af0SCorentin Labbe 		crypto_skcipher_reqsize(ctx->fallback_tfm);
45068ef8af0SCorentin Labbe 
451c50ef141SCorentin Labbe 	return 0;
452ce0183cbSArd Biesheuvel }
453ce0183cbSArd Biesheuvel 
rk_cipher_tfm_exit(struct crypto_skcipher * tfm)454a7fa0644SCorentin Labbe static void rk_cipher_tfm_exit(struct crypto_skcipher *tfm)
455ce0183cbSArd Biesheuvel {
456ce0183cbSArd Biesheuvel 	struct rk_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
457ce0183cbSArd Biesheuvel 
458d6b23cceSCorentin Labbe 	memzero_explicit(ctx->key, ctx->keylen);
45968ef8af0SCorentin Labbe 	crypto_free_skcipher(ctx->fallback_tfm);
460ce0183cbSArd Biesheuvel }
461ce0183cbSArd Biesheuvel 
462ce0183cbSArd Biesheuvel struct rk_crypto_tmp rk_ecb_aes_alg = {
4636d55c4a2SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
464*1a15d26cSHerbert Xu 	.alg.skcipher.base = {
465ce0183cbSArd Biesheuvel 		.base.cra_name		= "ecb(aes)",
466ce0183cbSArd Biesheuvel 		.base.cra_driver_name	= "ecb-aes-rk",
467ce0183cbSArd Biesheuvel 		.base.cra_priority	= 300,
46868ef8af0SCorentin Labbe 		.base.cra_flags		= CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
469ce0183cbSArd Biesheuvel 		.base.cra_blocksize	= AES_BLOCK_SIZE,
470ce0183cbSArd Biesheuvel 		.base.cra_ctxsize	= sizeof(struct rk_cipher_ctx),
471ce0183cbSArd Biesheuvel 		.base.cra_alignmask	= 0x0f,
472ce0183cbSArd Biesheuvel 		.base.cra_module	= THIS_MODULE,
473ce0183cbSArd Biesheuvel 
474a7fa0644SCorentin Labbe 		.init			= rk_cipher_tfm_init,
475a7fa0644SCorentin Labbe 		.exit			= rk_cipher_tfm_exit,
476ce0183cbSArd Biesheuvel 		.min_keysize		= AES_MIN_KEY_SIZE,
477ce0183cbSArd Biesheuvel 		.max_keysize		= AES_MAX_KEY_SIZE,
478ce0183cbSArd Biesheuvel 		.setkey			= rk_aes_setkey,
479ce0183cbSArd Biesheuvel 		.encrypt		= rk_aes_ecb_encrypt,
480ce0183cbSArd Biesheuvel 		.decrypt		= rk_aes_ecb_decrypt,
481*1a15d26cSHerbert Xu 	},
482*1a15d26cSHerbert Xu 	.alg.skcipher.op = {
483*1a15d26cSHerbert Xu 		.do_one_request = rk_cipher_run,
484*1a15d26cSHerbert Xu 	},
485ce0183cbSArd Biesheuvel };
486ce0183cbSArd Biesheuvel 
487ce0183cbSArd Biesheuvel struct rk_crypto_tmp rk_cbc_aes_alg = {
4886d55c4a2SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
489*1a15d26cSHerbert Xu 	.alg.skcipher.base = {
490ce0183cbSArd Biesheuvel 		.base.cra_name		= "cbc(aes)",
491ce0183cbSArd Biesheuvel 		.base.cra_driver_name	= "cbc-aes-rk",
492ce0183cbSArd Biesheuvel 		.base.cra_priority	= 300,
49368ef8af0SCorentin Labbe 		.base.cra_flags		= CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
494ce0183cbSArd Biesheuvel 		.base.cra_blocksize	= AES_BLOCK_SIZE,
495ce0183cbSArd Biesheuvel 		.base.cra_ctxsize	= sizeof(struct rk_cipher_ctx),
496ce0183cbSArd Biesheuvel 		.base.cra_alignmask	= 0x0f,
497ce0183cbSArd Biesheuvel 		.base.cra_module	= THIS_MODULE,
498ce0183cbSArd Biesheuvel 
499a7fa0644SCorentin Labbe 		.init			= rk_cipher_tfm_init,
500a7fa0644SCorentin Labbe 		.exit			= rk_cipher_tfm_exit,
501ce0183cbSArd Biesheuvel 		.min_keysize		= AES_MIN_KEY_SIZE,
502ce0183cbSArd Biesheuvel 		.max_keysize		= AES_MAX_KEY_SIZE,
503ce0183cbSArd Biesheuvel 		.ivsize			= AES_BLOCK_SIZE,
504ce0183cbSArd Biesheuvel 		.setkey			= rk_aes_setkey,
505ce0183cbSArd Biesheuvel 		.encrypt		= rk_aes_cbc_encrypt,
506ce0183cbSArd Biesheuvel 		.decrypt		= rk_aes_cbc_decrypt,
507*1a15d26cSHerbert Xu 	},
508*1a15d26cSHerbert Xu 	.alg.skcipher.op = {
509*1a15d26cSHerbert Xu 		.do_one_request = rk_cipher_run,
510*1a15d26cSHerbert Xu 	},
511ce0183cbSArd Biesheuvel };
512ce0183cbSArd Biesheuvel 
513ce0183cbSArd Biesheuvel struct rk_crypto_tmp rk_ecb_des_alg = {
5146d55c4a2SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
515*1a15d26cSHerbert Xu 	.alg.skcipher.base = {
516ce0183cbSArd Biesheuvel 		.base.cra_name		= "ecb(des)",
517ce0183cbSArd Biesheuvel 		.base.cra_driver_name	= "ecb-des-rk",
518ce0183cbSArd Biesheuvel 		.base.cra_priority	= 300,
51968ef8af0SCorentin Labbe 		.base.cra_flags		= CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
520ce0183cbSArd Biesheuvel 		.base.cra_blocksize	= DES_BLOCK_SIZE,
521ce0183cbSArd Biesheuvel 		.base.cra_ctxsize	= sizeof(struct rk_cipher_ctx),
522ce0183cbSArd Biesheuvel 		.base.cra_alignmask	= 0x07,
523ce0183cbSArd Biesheuvel 		.base.cra_module	= THIS_MODULE,
524ce0183cbSArd Biesheuvel 
525a7fa0644SCorentin Labbe 		.init			= rk_cipher_tfm_init,
526a7fa0644SCorentin Labbe 		.exit			= rk_cipher_tfm_exit,
527ce0183cbSArd Biesheuvel 		.min_keysize		= DES_KEY_SIZE,
528ce0183cbSArd Biesheuvel 		.max_keysize		= DES_KEY_SIZE,
529ce0183cbSArd Biesheuvel 		.setkey			= rk_des_setkey,
530ce0183cbSArd Biesheuvel 		.encrypt		= rk_des_ecb_encrypt,
531ce0183cbSArd Biesheuvel 		.decrypt		= rk_des_ecb_decrypt,
532*1a15d26cSHerbert Xu 	},
533*1a15d26cSHerbert Xu 	.alg.skcipher.op = {
534*1a15d26cSHerbert Xu 		.do_one_request = rk_cipher_run,
535*1a15d26cSHerbert Xu 	},
536ce0183cbSArd Biesheuvel };
537ce0183cbSArd Biesheuvel 
538ce0183cbSArd Biesheuvel struct rk_crypto_tmp rk_cbc_des_alg = {
5396d55c4a2SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
540*1a15d26cSHerbert Xu 	.alg.skcipher.base = {
541ce0183cbSArd Biesheuvel 		.base.cra_name		= "cbc(des)",
542ce0183cbSArd Biesheuvel 		.base.cra_driver_name	= "cbc-des-rk",
543ce0183cbSArd Biesheuvel 		.base.cra_priority	= 300,
54468ef8af0SCorentin Labbe 		.base.cra_flags		= CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
545ce0183cbSArd Biesheuvel 		.base.cra_blocksize	= DES_BLOCK_SIZE,
546ce0183cbSArd Biesheuvel 		.base.cra_ctxsize	= sizeof(struct rk_cipher_ctx),
547ce0183cbSArd Biesheuvel 		.base.cra_alignmask	= 0x07,
548ce0183cbSArd Biesheuvel 		.base.cra_module	= THIS_MODULE,
549ce0183cbSArd Biesheuvel 
550a7fa0644SCorentin Labbe 		.init			= rk_cipher_tfm_init,
551a7fa0644SCorentin Labbe 		.exit			= rk_cipher_tfm_exit,
552ce0183cbSArd Biesheuvel 		.min_keysize		= DES_KEY_SIZE,
553ce0183cbSArd Biesheuvel 		.max_keysize		= DES_KEY_SIZE,
554ce0183cbSArd Biesheuvel 		.ivsize			= DES_BLOCK_SIZE,
555ce0183cbSArd Biesheuvel 		.setkey			= rk_des_setkey,
556ce0183cbSArd Biesheuvel 		.encrypt		= rk_des_cbc_encrypt,
557ce0183cbSArd Biesheuvel 		.decrypt		= rk_des_cbc_decrypt,
558*1a15d26cSHerbert Xu 	},
559*1a15d26cSHerbert Xu 	.alg.skcipher.op = {
560*1a15d26cSHerbert Xu 		.do_one_request = rk_cipher_run,
561*1a15d26cSHerbert Xu 	},
562ce0183cbSArd Biesheuvel };
563ce0183cbSArd Biesheuvel 
564ce0183cbSArd Biesheuvel struct rk_crypto_tmp rk_ecb_des3_ede_alg = {
5656d55c4a2SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
566*1a15d26cSHerbert Xu 	.alg.skcipher.base = {
567ce0183cbSArd Biesheuvel 		.base.cra_name		= "ecb(des3_ede)",
568ce0183cbSArd Biesheuvel 		.base.cra_driver_name	= "ecb-des3-ede-rk",
569ce0183cbSArd Biesheuvel 		.base.cra_priority	= 300,
57068ef8af0SCorentin Labbe 		.base.cra_flags		= CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
571ce0183cbSArd Biesheuvel 		.base.cra_blocksize	= DES_BLOCK_SIZE,
572ce0183cbSArd Biesheuvel 		.base.cra_ctxsize	= sizeof(struct rk_cipher_ctx),
573ce0183cbSArd Biesheuvel 		.base.cra_alignmask	= 0x07,
574ce0183cbSArd Biesheuvel 		.base.cra_module	= THIS_MODULE,
575ce0183cbSArd Biesheuvel 
576a7fa0644SCorentin Labbe 		.init			= rk_cipher_tfm_init,
577a7fa0644SCorentin Labbe 		.exit			= rk_cipher_tfm_exit,
578ce0183cbSArd Biesheuvel 		.min_keysize		= DES3_EDE_KEY_SIZE,
579ce0183cbSArd Biesheuvel 		.max_keysize		= DES3_EDE_KEY_SIZE,
580ce0183cbSArd Biesheuvel 		.setkey			= rk_tdes_setkey,
581ce0183cbSArd Biesheuvel 		.encrypt		= rk_des3_ede_ecb_encrypt,
582ce0183cbSArd Biesheuvel 		.decrypt		= rk_des3_ede_ecb_decrypt,
583*1a15d26cSHerbert Xu 	},
584*1a15d26cSHerbert Xu 	.alg.skcipher.op = {
585*1a15d26cSHerbert Xu 		.do_one_request = rk_cipher_run,
586*1a15d26cSHerbert Xu 	},
587ce0183cbSArd Biesheuvel };
588ce0183cbSArd Biesheuvel 
589ce0183cbSArd Biesheuvel struct rk_crypto_tmp rk_cbc_des3_ede_alg = {
5906d55c4a2SCorentin Labbe 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
591*1a15d26cSHerbert Xu 	.alg.skcipher.base = {
592ce0183cbSArd Biesheuvel 		.base.cra_name		= "cbc(des3_ede)",
593ce0183cbSArd Biesheuvel 		.base.cra_driver_name	= "cbc-des3-ede-rk",
594ce0183cbSArd Biesheuvel 		.base.cra_priority	= 300,
59568ef8af0SCorentin Labbe 		.base.cra_flags		= CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
596ce0183cbSArd Biesheuvel 		.base.cra_blocksize	= DES_BLOCK_SIZE,
597ce0183cbSArd Biesheuvel 		.base.cra_ctxsize	= sizeof(struct rk_cipher_ctx),
598ce0183cbSArd Biesheuvel 		.base.cra_alignmask	= 0x07,
599ce0183cbSArd Biesheuvel 		.base.cra_module	= THIS_MODULE,
600ce0183cbSArd Biesheuvel 
601a7fa0644SCorentin Labbe 		.init			= rk_cipher_tfm_init,
602a7fa0644SCorentin Labbe 		.exit			= rk_cipher_tfm_exit,
603ce0183cbSArd Biesheuvel 		.min_keysize		= DES3_EDE_KEY_SIZE,
604ce0183cbSArd Biesheuvel 		.max_keysize		= DES3_EDE_KEY_SIZE,
605ce0183cbSArd Biesheuvel 		.ivsize			= DES_BLOCK_SIZE,
606ce0183cbSArd Biesheuvel 		.setkey			= rk_tdes_setkey,
607ce0183cbSArd Biesheuvel 		.encrypt		= rk_des3_ede_cbc_encrypt,
608ce0183cbSArd Biesheuvel 		.decrypt		= rk_des3_ede_cbc_decrypt,
609*1a15d26cSHerbert Xu 	},
610*1a15d26cSHerbert Xu 	.alg.skcipher.op = {
611*1a15d26cSHerbert Xu 		.do_one_request = rk_cipher_run,
612*1a15d26cSHerbert Xu 	},
613ce0183cbSArd Biesheuvel };
614