18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* LRW: as defined by Cyril Guyot in
38c2ecf20Sopenharmony_ci *	http://grouper.ieee.org/groups/1619/email/pdf00017.pdf
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2006 Rik Snel <rsnel@cube.dyndns.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on ecb.c
88c2ecf20Sopenharmony_ci * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci/* This implementation is checked against the test vectors in the above
118c2ecf20Sopenharmony_ci * document and by a test vector provided by Ken Buchanan at
128c2ecf20Sopenharmony_ci * https://www.mail-archive.com/stds-p1619@listserv.ieee.org/msg00173.html
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The test vectors are included in the testing module tcrypt.[ch] */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
178c2ecf20Sopenharmony_ci#include <crypto/scatterwalk.h>
188c2ecf20Sopenharmony_ci#include <linux/err.h>
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/kernel.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <crypto/b128ops.h>
268c2ecf20Sopenharmony_ci#include <crypto/gf128mul.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define LRW_BLOCK_SIZE 16
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct lrw_tfm_ctx {
318c2ecf20Sopenharmony_ci	struct crypto_skcipher *child;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	/*
348c2ecf20Sopenharmony_ci	 * optimizes multiplying a random (non incrementing, as at the
358c2ecf20Sopenharmony_ci	 * start of a new sector) value with key2, we could also have
368c2ecf20Sopenharmony_ci	 * used 4k optimization tables or no optimization at all. In the
378c2ecf20Sopenharmony_ci	 * latter case we would have to store key2 here
388c2ecf20Sopenharmony_ci	 */
398c2ecf20Sopenharmony_ci	struct gf128mul_64k *table;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	/*
428c2ecf20Sopenharmony_ci	 * stores:
438c2ecf20Sopenharmony_ci	 *  key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
448c2ecf20Sopenharmony_ci	 *  key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
458c2ecf20Sopenharmony_ci	 *  key2*{ 0,0,...1,1,1,1,1 }, etc
468c2ecf20Sopenharmony_ci	 * needed for optimized multiplication of incrementing values
478c2ecf20Sopenharmony_ci	 * with key2
488c2ecf20Sopenharmony_ci	 */
498c2ecf20Sopenharmony_ci	be128 mulinc[128];
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct lrw_request_ctx {
538c2ecf20Sopenharmony_ci	be128 t;
548c2ecf20Sopenharmony_ci	struct skcipher_request subreq;
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic inline void lrw_setbit128_bbe(void *b, int bit)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	__set_bit(bit ^ (0x80 -
608c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
618c2ecf20Sopenharmony_ci			 BITS_PER_LONG
628c2ecf20Sopenharmony_ci#else
638c2ecf20Sopenharmony_ci			 BITS_PER_BYTE
648c2ecf20Sopenharmony_ci#endif
658c2ecf20Sopenharmony_ci			), b);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int lrw_setkey(struct crypto_skcipher *parent, const u8 *key,
698c2ecf20Sopenharmony_ci		      unsigned int keylen)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(parent);
728c2ecf20Sopenharmony_ci	struct crypto_skcipher *child = ctx->child;
738c2ecf20Sopenharmony_ci	int err, bsize = LRW_BLOCK_SIZE;
748c2ecf20Sopenharmony_ci	const u8 *tweak = key + keylen - bsize;
758c2ecf20Sopenharmony_ci	be128 tmp = { 0 };
768c2ecf20Sopenharmony_ci	int i;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
798c2ecf20Sopenharmony_ci	crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
808c2ecf20Sopenharmony_ci					 CRYPTO_TFM_REQ_MASK);
818c2ecf20Sopenharmony_ci	err = crypto_skcipher_setkey(child, key, keylen - bsize);
828c2ecf20Sopenharmony_ci	if (err)
838c2ecf20Sopenharmony_ci		return err;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (ctx->table)
868c2ecf20Sopenharmony_ci		gf128mul_free_64k(ctx->table);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* initialize multiplication table for Key2 */
898c2ecf20Sopenharmony_ci	ctx->table = gf128mul_init_64k_bbe((be128 *)tweak);
908c2ecf20Sopenharmony_ci	if (!ctx->table)
918c2ecf20Sopenharmony_ci		return -ENOMEM;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	/* initialize optimization table */
948c2ecf20Sopenharmony_ci	for (i = 0; i < 128; i++) {
958c2ecf20Sopenharmony_ci		lrw_setbit128_bbe(&tmp, i);
968c2ecf20Sopenharmony_ci		ctx->mulinc[i] = tmp;
978c2ecf20Sopenharmony_ci		gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return 0;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * Returns the number of trailing '1' bits in the words of the counter, which is
1058c2ecf20Sopenharmony_ci * represented by 4 32-bit words, arranged from least to most significant.
1068c2ecf20Sopenharmony_ci * At the same time, increments the counter by one.
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * For example:
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * u32 counter[4] = { 0xFFFFFFFF, 0x1, 0x0, 0x0 };
1118c2ecf20Sopenharmony_ci * int i = lrw_next_index(&counter);
1128c2ecf20Sopenharmony_ci * // i == 33, counter == { 0x0, 0x2, 0x0, 0x0 }
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_cistatic int lrw_next_index(u32 *counter)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	int i, res = 0;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
1198c2ecf20Sopenharmony_ci		if (counter[i] + 1 != 0)
1208c2ecf20Sopenharmony_ci			return res + ffz(counter[i]++);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci		counter[i] = 0;
1238c2ecf20Sopenharmony_ci		res += 32;
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/*
1278c2ecf20Sopenharmony_ci	 * If we get here, then x == 128 and we are incrementing the counter
1288c2ecf20Sopenharmony_ci	 * from all ones to all zeros. This means we must return index 127, i.e.
1298c2ecf20Sopenharmony_ci	 * the one corresponding to key2*{ 1,...,1 }.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	return 127;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci * We compute the tweak masks twice (both before and after the ECB encryption or
1368c2ecf20Sopenharmony_ci * decryption) to avoid having to allocate a temporary buffer and/or make
1378c2ecf20Sopenharmony_ci * mutliple calls to the 'ecb(..)' instance, which usually would be slower than
1388c2ecf20Sopenharmony_ci * just doing the lrw_next_index() calls again.
1398c2ecf20Sopenharmony_ci */
1408c2ecf20Sopenharmony_cistatic int lrw_xor_tweak(struct skcipher_request *req, bool second_pass)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	const int bs = LRW_BLOCK_SIZE;
1438c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1448c2ecf20Sopenharmony_ci	const struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
1458c2ecf20Sopenharmony_ci	struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
1468c2ecf20Sopenharmony_ci	be128 t = rctx->t;
1478c2ecf20Sopenharmony_ci	struct skcipher_walk w;
1488c2ecf20Sopenharmony_ci	__be32 *iv;
1498c2ecf20Sopenharmony_ci	u32 counter[4];
1508c2ecf20Sopenharmony_ci	int err;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (second_pass) {
1538c2ecf20Sopenharmony_ci		req = &rctx->subreq;
1548c2ecf20Sopenharmony_ci		/* set to our TFM to enforce correct alignment: */
1558c2ecf20Sopenharmony_ci		skcipher_request_set_tfm(req, tfm);
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	err = skcipher_walk_virt(&w, req, false);
1598c2ecf20Sopenharmony_ci	if (err)
1608c2ecf20Sopenharmony_ci		return err;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	iv = (__be32 *)w.iv;
1638c2ecf20Sopenharmony_ci	counter[0] = be32_to_cpu(iv[3]);
1648c2ecf20Sopenharmony_ci	counter[1] = be32_to_cpu(iv[2]);
1658c2ecf20Sopenharmony_ci	counter[2] = be32_to_cpu(iv[1]);
1668c2ecf20Sopenharmony_ci	counter[3] = be32_to_cpu(iv[0]);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	while (w.nbytes) {
1698c2ecf20Sopenharmony_ci		unsigned int avail = w.nbytes;
1708c2ecf20Sopenharmony_ci		be128 *wsrc;
1718c2ecf20Sopenharmony_ci		be128 *wdst;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci		wsrc = w.src.virt.addr;
1748c2ecf20Sopenharmony_ci		wdst = w.dst.virt.addr;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci		do {
1778c2ecf20Sopenharmony_ci			be128_xor(wdst++, &t, wsrc++);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci			/* T <- I*Key2, using the optimization
1808c2ecf20Sopenharmony_ci			 * discussed in the specification */
1818c2ecf20Sopenharmony_ci			be128_xor(&t, &t,
1828c2ecf20Sopenharmony_ci				  &ctx->mulinc[lrw_next_index(counter)]);
1838c2ecf20Sopenharmony_ci		} while ((avail -= bs) >= bs);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci		if (second_pass && w.nbytes == w.total) {
1868c2ecf20Sopenharmony_ci			iv[0] = cpu_to_be32(counter[3]);
1878c2ecf20Sopenharmony_ci			iv[1] = cpu_to_be32(counter[2]);
1888c2ecf20Sopenharmony_ci			iv[2] = cpu_to_be32(counter[1]);
1898c2ecf20Sopenharmony_ci			iv[3] = cpu_to_be32(counter[0]);
1908c2ecf20Sopenharmony_ci		}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci		err = skcipher_walk_done(&w, avail);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	return err;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic int lrw_xor_tweak_pre(struct skcipher_request *req)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	return lrw_xor_tweak(req, false);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic int lrw_xor_tweak_post(struct skcipher_request *req)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	return lrw_xor_tweak(req, true);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic void lrw_crypt_done(struct crypto_async_request *areq, int err)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct skcipher_request *req = areq->data;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	if (!err) {
2138c2ecf20Sopenharmony_ci		struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
2168c2ecf20Sopenharmony_ci		err = lrw_xor_tweak_post(req);
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	skcipher_request_complete(req, err);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic void lrw_init_crypt(struct skcipher_request *req)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	const struct lrw_tfm_ctx *ctx =
2258c2ecf20Sopenharmony_ci		crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
2268c2ecf20Sopenharmony_ci	struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
2278c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	skcipher_request_set_tfm(subreq, ctx->child);
2308c2ecf20Sopenharmony_ci	skcipher_request_set_callback(subreq, req->base.flags, lrw_crypt_done,
2318c2ecf20Sopenharmony_ci				      req);
2328c2ecf20Sopenharmony_ci	/* pass req->iv as IV (will be used by xor_tweak, ECB will ignore it) */
2338c2ecf20Sopenharmony_ci	skcipher_request_set_crypt(subreq, req->dst, req->dst,
2348c2ecf20Sopenharmony_ci				   req->cryptlen, req->iv);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* calculate first value of T */
2378c2ecf20Sopenharmony_ci	memcpy(&rctx->t, req->iv, sizeof(rctx->t));
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* T <- I*Key2 */
2408c2ecf20Sopenharmony_ci	gf128mul_64k_bbe(&rctx->t, ctx->table);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic int lrw_encrypt(struct skcipher_request *req)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
2468c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	lrw_init_crypt(req);
2498c2ecf20Sopenharmony_ci	return lrw_xor_tweak_pre(req) ?:
2508c2ecf20Sopenharmony_ci		crypto_skcipher_encrypt(subreq) ?:
2518c2ecf20Sopenharmony_ci		lrw_xor_tweak_post(req);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int lrw_decrypt(struct skcipher_request *req)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
2578c2ecf20Sopenharmony_ci	struct skcipher_request *subreq = &rctx->subreq;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	lrw_init_crypt(req);
2608c2ecf20Sopenharmony_ci	return lrw_xor_tweak_pre(req) ?:
2618c2ecf20Sopenharmony_ci		crypto_skcipher_decrypt(subreq) ?:
2628c2ecf20Sopenharmony_ci		lrw_xor_tweak_post(req);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int lrw_init_tfm(struct crypto_skcipher *tfm)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
2688c2ecf20Sopenharmony_ci	struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
2698c2ecf20Sopenharmony_ci	struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
2708c2ecf20Sopenharmony_ci	struct crypto_skcipher *cipher;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	cipher = crypto_spawn_skcipher(spawn);
2738c2ecf20Sopenharmony_ci	if (IS_ERR(cipher))
2748c2ecf20Sopenharmony_ci		return PTR_ERR(cipher);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	ctx->child = cipher;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(cipher) +
2798c2ecf20Sopenharmony_ci					 sizeof(struct lrw_request_ctx));
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return 0;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void lrw_exit_tfm(struct crypto_skcipher *tfm)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (ctx->table)
2898c2ecf20Sopenharmony_ci		gf128mul_free_64k(ctx->table);
2908c2ecf20Sopenharmony_ci	crypto_free_skcipher(ctx->child);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic void lrw_free_instance(struct skcipher_instance *inst)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	crypto_drop_skcipher(skcipher_instance_ctx(inst));
2968c2ecf20Sopenharmony_ci	kfree(inst);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int lrw_create(struct crypto_template *tmpl, struct rtattr **tb)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct crypto_skcipher_spawn *spawn;
3028c2ecf20Sopenharmony_ci	struct skcipher_instance *inst;
3038c2ecf20Sopenharmony_ci	struct skcipher_alg *alg;
3048c2ecf20Sopenharmony_ci	const char *cipher_name;
3058c2ecf20Sopenharmony_ci	char ecb_name[CRYPTO_MAX_ALG_NAME];
3068c2ecf20Sopenharmony_ci	u32 mask;
3078c2ecf20Sopenharmony_ci	int err;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
3108c2ecf20Sopenharmony_ci	if (err)
3118c2ecf20Sopenharmony_ci		return err;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	cipher_name = crypto_attr_alg_name(tb[1]);
3148c2ecf20Sopenharmony_ci	if (IS_ERR(cipher_name))
3158c2ecf20Sopenharmony_ci		return PTR_ERR(cipher_name);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
3188c2ecf20Sopenharmony_ci	if (!inst)
3198c2ecf20Sopenharmony_ci		return -ENOMEM;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	spawn = skcipher_instance_ctx(inst);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
3248c2ecf20Sopenharmony_ci				   cipher_name, 0, mask);
3258c2ecf20Sopenharmony_ci	if (err == -ENOENT) {
3268c2ecf20Sopenharmony_ci		err = -ENAMETOOLONG;
3278c2ecf20Sopenharmony_ci		if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
3288c2ecf20Sopenharmony_ci			     cipher_name) >= CRYPTO_MAX_ALG_NAME)
3298c2ecf20Sopenharmony_ci			goto err_free_inst;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		err = crypto_grab_skcipher(spawn,
3328c2ecf20Sopenharmony_ci					   skcipher_crypto_instance(inst),
3338c2ecf20Sopenharmony_ci					   ecb_name, 0, mask);
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (err)
3378c2ecf20Sopenharmony_ci		goto err_free_inst;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	alg = crypto_skcipher_spawn_alg(spawn);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	err = -EINVAL;
3428c2ecf20Sopenharmony_ci	if (alg->base.cra_blocksize != LRW_BLOCK_SIZE)
3438c2ecf20Sopenharmony_ci		goto err_free_inst;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (crypto_skcipher_alg_ivsize(alg))
3468c2ecf20Sopenharmony_ci		goto err_free_inst;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	err = crypto_inst_setname(skcipher_crypto_instance(inst), "lrw",
3498c2ecf20Sopenharmony_ci				  &alg->base);
3508c2ecf20Sopenharmony_ci	if (err)
3518c2ecf20Sopenharmony_ci		goto err_free_inst;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	err = -EINVAL;
3548c2ecf20Sopenharmony_ci	cipher_name = alg->base.cra_name;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/* Alas we screwed up the naming so we have to mangle the
3578c2ecf20Sopenharmony_ci	 * cipher name.
3588c2ecf20Sopenharmony_ci	 */
3598c2ecf20Sopenharmony_ci	if (!strncmp(cipher_name, "ecb(", 4)) {
3608c2ecf20Sopenharmony_ci		int len;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci		len = strscpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
3638c2ecf20Sopenharmony_ci		if (len < 2)
3648c2ecf20Sopenharmony_ci			goto err_free_inst;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci		if (ecb_name[len - 1] != ')')
3678c2ecf20Sopenharmony_ci			goto err_free_inst;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci		ecb_name[len - 1] = 0;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
3728c2ecf20Sopenharmony_ci			     "lrw(%s)", ecb_name) >= CRYPTO_MAX_ALG_NAME) {
3738c2ecf20Sopenharmony_ci			err = -ENAMETOOLONG;
3748c2ecf20Sopenharmony_ci			goto err_free_inst;
3758c2ecf20Sopenharmony_ci		}
3768c2ecf20Sopenharmony_ci	} else
3778c2ecf20Sopenharmony_ci		goto err_free_inst;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	inst->alg.base.cra_priority = alg->base.cra_priority;
3808c2ecf20Sopenharmony_ci	inst->alg.base.cra_blocksize = LRW_BLOCK_SIZE;
3818c2ecf20Sopenharmony_ci	inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
3828c2ecf20Sopenharmony_ci				       (__alignof__(be128) - 1);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	inst->alg.ivsize = LRW_BLOCK_SIZE;
3858c2ecf20Sopenharmony_ci	inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
3868c2ecf20Sopenharmony_ci				LRW_BLOCK_SIZE;
3878c2ecf20Sopenharmony_ci	inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
3888c2ecf20Sopenharmony_ci				LRW_BLOCK_SIZE;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	inst->alg.base.cra_ctxsize = sizeof(struct lrw_tfm_ctx);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	inst->alg.init = lrw_init_tfm;
3938c2ecf20Sopenharmony_ci	inst->alg.exit = lrw_exit_tfm;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	inst->alg.setkey = lrw_setkey;
3968c2ecf20Sopenharmony_ci	inst->alg.encrypt = lrw_encrypt;
3978c2ecf20Sopenharmony_ci	inst->alg.decrypt = lrw_decrypt;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	inst->free = lrw_free_instance;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	err = skcipher_register_instance(tmpl, inst);
4028c2ecf20Sopenharmony_ci	if (err) {
4038c2ecf20Sopenharmony_cierr_free_inst:
4048c2ecf20Sopenharmony_ci		lrw_free_instance(inst);
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci	return err;
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic struct crypto_template lrw_tmpl = {
4108c2ecf20Sopenharmony_ci	.name = "lrw",
4118c2ecf20Sopenharmony_ci	.create = lrw_create,
4128c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
4138c2ecf20Sopenharmony_ci};
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic int __init lrw_module_init(void)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	return crypto_register_template(&lrw_tmpl);
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic void __exit lrw_module_exit(void)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	crypto_unregister_template(&lrw_tmpl);
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cisubsys_initcall(lrw_module_init);
4268c2ecf20Sopenharmony_cimodule_exit(lrw_module_exit);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4298c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LRW block cipher mode");
4308c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("lrw");
431