18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * CTS: Cipher Text Stealing mode
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * COPYRIGHT (c) 2008
58c2ecf20Sopenharmony_ci * The Regents of the University of Michigan
68c2ecf20Sopenharmony_ci * ALL RIGHTS RESERVED
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Permission is granted to use, copy, create derivative works
98c2ecf20Sopenharmony_ci * and redistribute this software and such derivative works
108c2ecf20Sopenharmony_ci * for any purpose, so long as the name of The University of
118c2ecf20Sopenharmony_ci * Michigan is not used in any advertising or publicity
128c2ecf20Sopenharmony_ci * pertaining to the use of distribution of this software
138c2ecf20Sopenharmony_ci * without specific, written prior authorization.  If the
148c2ecf20Sopenharmony_ci * above copyright notice or any other identification of the
158c2ecf20Sopenharmony_ci * University of Michigan is included in any copy of any
168c2ecf20Sopenharmony_ci * portion of this software, then the disclaimer below must
178c2ecf20Sopenharmony_ci * also be included.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
208c2ecf20Sopenharmony_ci * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
218c2ecf20Sopenharmony_ci * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
228c2ecf20Sopenharmony_ci * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
238c2ecf20Sopenharmony_ci * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
248c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
258c2ecf20Sopenharmony_ci * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
268c2ecf20Sopenharmony_ci * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
278c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
288c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
298c2ecf20Sopenharmony_ci * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
308c2ecf20Sopenharmony_ci * SUCH DAMAGES.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Derived from various:
348c2ecf20Sopenharmony_ci *	Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * This is the Cipher Text Stealing mode as described by
398c2ecf20Sopenharmony_ci * Section 8 of rfc2040 and referenced by rfc3962.
408c2ecf20Sopenharmony_ci * rfc3962 includes errata information in its Appendix A.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
448c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
458c2ecf20Sopenharmony_ci#include <linux/err.h>
468c2ecf20Sopenharmony_ci#include <linux/init.h>
478c2ecf20Sopenharmony_ci#include <linux/kernel.h>
488c2ecf20Sopenharmony_ci#include <linux/log2.h>
498c2ecf20Sopenharmony_ci#include <linux/module.h>
508c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
518c2ecf20Sopenharmony_ci#include <crypto/scatterwalk.h>
528c2ecf20Sopenharmony_ci#include <linux/slab.h>
538c2ecf20Sopenharmony_ci#include <linux/compiler.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct crypto_cts_ctx {
568c2ecf20Sopenharmony_ci	struct crypto_skcipher *child;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistruct crypto_cts_reqctx {
608c2ecf20Sopenharmony_ci	struct scatterlist sg[2];
618c2ecf20Sopenharmony_ci	unsigned offset;
628c2ecf20Sopenharmony_ci	struct skcipher_request subreq;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
688c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
698c2ecf20Sopenharmony_ci	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
708c2ecf20Sopenharmony_ci	struct crypto_skcipher *child = ctx->child;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
738c2ecf20Sopenharmony_ci			 crypto_skcipher_alignmask(tfm) + 1);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
778c2ecf20Sopenharmony_ci			     unsigned int keylen)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
808c2ecf20Sopenharmony_ci	struct crypto_skcipher *child = ctx->child;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
838c2ecf20Sopenharmony_ci	crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
848c2ecf20Sopenharmony_ci					 CRYPTO_TFM_REQ_MASK);
858c2ecf20Sopenharmony_ci	return crypto_skcipher_setkey(child, key, keylen);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct skcipher_request *req = areq->data;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (err == -EINPROGRESS)
938c2ecf20Sopenharmony_ci		return;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	skcipher_request_complete(req, err);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic int cts_cbc_encrypt(struct skcipher_request *req)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
1018c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1028c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
1038c2ecf20Sopenharmony_ci	int bsize = crypto_skcipher_blocksize(tfm);
1048c2ecf20Sopenharmony_ci	u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32));
1058c2ecf20Sopenharmony_ci	struct scatterlist *sg;
1068c2ecf20Sopenharmony_ci	unsigned int offset;
1078c2ecf20Sopenharmony_ci	int lastn;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	offset = rctx->offset;
1108c2ecf20Sopenharmony_ci	lastn = req->cryptlen - offset;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
1138c2ecf20Sopenharmony_ci	scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	memset(d, 0, bsize);
1168c2ecf20Sopenharmony_ci	scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
1198c2ecf20Sopenharmony_ci	memzero_explicit(d, sizeof(d));
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	skcipher_request_set_callback(subreq, req->base.flags &
1228c2ecf20Sopenharmony_ci					      CRYPTO_TFM_REQ_MAY_BACKLOG,
1238c2ecf20Sopenharmony_ci				      cts_cbc_crypt_done, req);
1248c2ecf20Sopenharmony_ci	skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
1258c2ecf20Sopenharmony_ci	return crypto_skcipher_encrypt(subreq);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	struct skcipher_request *req = areq->data;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (err)
1338c2ecf20Sopenharmony_ci		goto out;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	err = cts_cbc_encrypt(req);
1368c2ecf20Sopenharmony_ci	if (err == -EINPROGRESS || err == -EBUSY)
1378c2ecf20Sopenharmony_ci		return;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciout:
1408c2ecf20Sopenharmony_ci	skcipher_request_complete(req, err);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int crypto_cts_encrypt(struct skcipher_request *req)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1468c2ecf20Sopenharmony_ci	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
1478c2ecf20Sopenharmony_ci	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
1488c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
1498c2ecf20Sopenharmony_ci	int bsize = crypto_skcipher_blocksize(tfm);
1508c2ecf20Sopenharmony_ci	unsigned int nbytes = req->cryptlen;
1518c2ecf20Sopenharmony_ci	unsigned int offset;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	skcipher_request_set_tfm(subreq, ctx->child);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (nbytes < bsize)
1568c2ecf20Sopenharmony_ci		return -EINVAL;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if (nbytes == bsize) {
1598c2ecf20Sopenharmony_ci		skcipher_request_set_callback(subreq, req->base.flags,
1608c2ecf20Sopenharmony_ci					      req->base.complete,
1618c2ecf20Sopenharmony_ci					      req->base.data);
1628c2ecf20Sopenharmony_ci		skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
1638c2ecf20Sopenharmony_ci					   req->iv);
1648c2ecf20Sopenharmony_ci		return crypto_skcipher_encrypt(subreq);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	offset = rounddown(nbytes - 1, bsize);
1688c2ecf20Sopenharmony_ci	rctx->offset = offset;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	skcipher_request_set_callback(subreq, req->base.flags,
1718c2ecf20Sopenharmony_ci				      crypto_cts_encrypt_done, req);
1728c2ecf20Sopenharmony_ci	skcipher_request_set_crypt(subreq, req->src, req->dst,
1738c2ecf20Sopenharmony_ci				   offset, req->iv);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	return crypto_skcipher_encrypt(subreq) ?:
1768c2ecf20Sopenharmony_ci	       cts_cbc_encrypt(req);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int cts_cbc_decrypt(struct skcipher_request *req)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
1828c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1838c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
1848c2ecf20Sopenharmony_ci	int bsize = crypto_skcipher_blocksize(tfm);
1858c2ecf20Sopenharmony_ci	u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32));
1868c2ecf20Sopenharmony_ci	struct scatterlist *sg;
1878c2ecf20Sopenharmony_ci	unsigned int offset;
1888c2ecf20Sopenharmony_ci	u8 *space;
1898c2ecf20Sopenharmony_ci	int lastn;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	offset = rctx->offset;
1928c2ecf20Sopenharmony_ci	lastn = req->cryptlen - offset;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/* 1. Decrypt Cn-1 (s) to create Dn */
1978c2ecf20Sopenharmony_ci	scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
1988c2ecf20Sopenharmony_ci	space = crypto_cts_reqctx_space(req);
1998c2ecf20Sopenharmony_ci	crypto_xor(d + bsize, space, bsize);
2008c2ecf20Sopenharmony_ci	/* 2. Pad Cn with zeros at the end to create C of length BB */
2018c2ecf20Sopenharmony_ci	memset(d, 0, bsize);
2028c2ecf20Sopenharmony_ci	scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
2038c2ecf20Sopenharmony_ci	/* 3. Exclusive-or Dn with C to create Xn */
2048c2ecf20Sopenharmony_ci	/* 4. Select the first Ln bytes of Xn to create Pn */
2058c2ecf20Sopenharmony_ci	crypto_xor(d + bsize, d, lastn);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
2088c2ecf20Sopenharmony_ci	memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
2098c2ecf20Sopenharmony_ci	/* 6. Decrypt En to create Pn-1 */
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
2128c2ecf20Sopenharmony_ci	memzero_explicit(d, sizeof(d));
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	skcipher_request_set_callback(subreq, req->base.flags &
2158c2ecf20Sopenharmony_ci					      CRYPTO_TFM_REQ_MAY_BACKLOG,
2168c2ecf20Sopenharmony_ci				      cts_cbc_crypt_done, req);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
2198c2ecf20Sopenharmony_ci	return crypto_skcipher_decrypt(subreq);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	struct skcipher_request *req = areq->data;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	if (err)
2278c2ecf20Sopenharmony_ci		goto out;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	err = cts_cbc_decrypt(req);
2308c2ecf20Sopenharmony_ci	if (err == -EINPROGRESS || err == -EBUSY)
2318c2ecf20Sopenharmony_ci		return;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ciout:
2348c2ecf20Sopenharmony_ci	skcipher_request_complete(req, err);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic int crypto_cts_decrypt(struct skcipher_request *req)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2408c2ecf20Sopenharmony_ci	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
2418c2ecf20Sopenharmony_ci	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
2428c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
2438c2ecf20Sopenharmony_ci	int bsize = crypto_skcipher_blocksize(tfm);
2448c2ecf20Sopenharmony_ci	unsigned int nbytes = req->cryptlen;
2458c2ecf20Sopenharmony_ci	unsigned int offset;
2468c2ecf20Sopenharmony_ci	u8 *space;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	skcipher_request_set_tfm(subreq, ctx->child);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (nbytes < bsize)
2518c2ecf20Sopenharmony_ci		return -EINVAL;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (nbytes == bsize) {
2548c2ecf20Sopenharmony_ci		skcipher_request_set_callback(subreq, req->base.flags,
2558c2ecf20Sopenharmony_ci					      req->base.complete,
2568c2ecf20Sopenharmony_ci					      req->base.data);
2578c2ecf20Sopenharmony_ci		skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
2588c2ecf20Sopenharmony_ci					   req->iv);
2598c2ecf20Sopenharmony_ci		return crypto_skcipher_decrypt(subreq);
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	skcipher_request_set_callback(subreq, req->base.flags,
2638c2ecf20Sopenharmony_ci				      crypto_cts_decrypt_done, req);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	space = crypto_cts_reqctx_space(req);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	offset = rounddown(nbytes - 1, bsize);
2688c2ecf20Sopenharmony_ci	rctx->offset = offset;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (offset <= bsize)
2718c2ecf20Sopenharmony_ci		memcpy(space, req->iv, bsize);
2728c2ecf20Sopenharmony_ci	else
2738c2ecf20Sopenharmony_ci		scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
2748c2ecf20Sopenharmony_ci					 bsize, 0);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	skcipher_request_set_crypt(subreq, req->src, req->dst,
2778c2ecf20Sopenharmony_ci				   offset, req->iv);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return crypto_skcipher_decrypt(subreq) ?:
2808c2ecf20Sopenharmony_ci	       cts_cbc_decrypt(req);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
2868c2ecf20Sopenharmony_ci	struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
2878c2ecf20Sopenharmony_ci	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
2888c2ecf20Sopenharmony_ci	struct crypto_skcipher *cipher;
2898c2ecf20Sopenharmony_ci	unsigned reqsize;
2908c2ecf20Sopenharmony_ci	unsigned bsize;
2918c2ecf20Sopenharmony_ci	unsigned align;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	cipher = crypto_spawn_skcipher(spawn);
2948c2ecf20Sopenharmony_ci	if (IS_ERR(cipher))
2958c2ecf20Sopenharmony_ci		return PTR_ERR(cipher);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	ctx->child = cipher;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	align = crypto_skcipher_alignmask(tfm);
3008c2ecf20Sopenharmony_ci	bsize = crypto_skcipher_blocksize(cipher);
3018c2ecf20Sopenharmony_ci	reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
3028c2ecf20Sopenharmony_ci			crypto_skcipher_reqsize(cipher),
3038c2ecf20Sopenharmony_ci			crypto_tfm_ctx_alignment()) +
3048c2ecf20Sopenharmony_ci		  (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	crypto_skcipher_set_reqsize(tfm, reqsize);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	return 0;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	crypto_free_skcipher(ctx->child);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic void crypto_cts_free(struct skcipher_instance *inst)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	crypto_drop_skcipher(skcipher_instance_ctx(inst));
3218c2ecf20Sopenharmony_ci	kfree(inst);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct crypto_skcipher_spawn *spawn;
3278c2ecf20Sopenharmony_ci	struct skcipher_instance *inst;
3288c2ecf20Sopenharmony_ci	struct skcipher_alg *alg;
3298c2ecf20Sopenharmony_ci	u32 mask;
3308c2ecf20Sopenharmony_ci	int err;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
3338c2ecf20Sopenharmony_ci	if (err)
3348c2ecf20Sopenharmony_ci		return err;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
3378c2ecf20Sopenharmony_ci	if (!inst)
3388c2ecf20Sopenharmony_ci		return -ENOMEM;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	spawn = skcipher_instance_ctx(inst);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
3438c2ecf20Sopenharmony_ci				   crypto_attr_alg_name(tb[1]), 0, mask);
3448c2ecf20Sopenharmony_ci	if (err)
3458c2ecf20Sopenharmony_ci		goto err_free_inst;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	alg = crypto_spawn_skcipher_alg(spawn);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	err = -EINVAL;
3508c2ecf20Sopenharmony_ci	if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
3518c2ecf20Sopenharmony_ci		goto err_free_inst;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (strncmp(alg->base.cra_name, "cbc(", 4))
3548c2ecf20Sopenharmony_ci		goto err_free_inst;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
3578c2ecf20Sopenharmony_ci				  &alg->base);
3588c2ecf20Sopenharmony_ci	if (err)
3598c2ecf20Sopenharmony_ci		goto err_free_inst;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	inst->alg.base.cra_priority = alg->base.cra_priority;
3628c2ecf20Sopenharmony_ci	inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
3638c2ecf20Sopenharmony_ci	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	inst->alg.ivsize = alg->base.cra_blocksize;
3668c2ecf20Sopenharmony_ci	inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
3678c2ecf20Sopenharmony_ci	inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
3688c2ecf20Sopenharmony_ci	inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	inst->alg.init = crypto_cts_init_tfm;
3738c2ecf20Sopenharmony_ci	inst->alg.exit = crypto_cts_exit_tfm;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	inst->alg.setkey = crypto_cts_setkey;
3768c2ecf20Sopenharmony_ci	inst->alg.encrypt = crypto_cts_encrypt;
3778c2ecf20Sopenharmony_ci	inst->alg.decrypt = crypto_cts_decrypt;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	inst->free = crypto_cts_free;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	err = skcipher_register_instance(tmpl, inst);
3828c2ecf20Sopenharmony_ci	if (err) {
3838c2ecf20Sopenharmony_cierr_free_inst:
3848c2ecf20Sopenharmony_ci		crypto_cts_free(inst);
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci	return err;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic struct crypto_template crypto_cts_tmpl = {
3908c2ecf20Sopenharmony_ci	.name = "cts",
3918c2ecf20Sopenharmony_ci	.create = crypto_cts_create,
3928c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
3938c2ecf20Sopenharmony_ci};
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic int __init crypto_cts_module_init(void)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	return crypto_register_template(&crypto_cts_tmpl);
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic void __exit crypto_cts_module_exit(void)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	crypto_unregister_template(&crypto_cts_tmpl);
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cisubsys_initcall(crypto_cts_module_init);
4068c2ecf20Sopenharmony_cimodule_exit(crypto_cts_module_exit);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
4098c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
4108c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("cts");
411