162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * HCTR2 length-preserving encryption mode
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2021 Google LLC
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * HCTR2 is a length-preserving encryption mode that is efficient on
1162306a36Sopenharmony_ci * processors with instructions to accelerate AES and carryless
1262306a36Sopenharmony_ci * multiplication, e.g. x86 processors with AES-NI and CLMUL, and ARM
1362306a36Sopenharmony_ci * processors with the ARMv8 crypto extensions.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * For more details, see the paper: "Length-preserving encryption with HCTR2"
1662306a36Sopenharmony_ci * (https://eprint.iacr.org/2021/1441.pdf)
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <crypto/internal/cipher.h>
2062306a36Sopenharmony_ci#include <crypto/internal/hash.h>
2162306a36Sopenharmony_ci#include <crypto/internal/skcipher.h>
2262306a36Sopenharmony_ci#include <crypto/polyval.h>
2362306a36Sopenharmony_ci#include <crypto/scatterwalk.h>
2462306a36Sopenharmony_ci#include <linux/module.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define BLOCKCIPHER_BLOCK_SIZE		16
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * The specification allows variable-length tweaks, but Linux's crypto API
3062306a36Sopenharmony_ci * currently only allows algorithms to support a single length.  The "natural"
3162306a36Sopenharmony_ci * tweak length for HCTR2 is 16, since that fits into one POLYVAL block for
3262306a36Sopenharmony_ci * the best performance.  But longer tweaks are useful for fscrypt, to avoid
3362306a36Sopenharmony_ci * needing to derive per-file keys.  So instead we use two blocks, or 32 bytes.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci#define TWEAK_SIZE		32
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct hctr2_instance_ctx {
3862306a36Sopenharmony_ci	struct crypto_cipher_spawn blockcipher_spawn;
3962306a36Sopenharmony_ci	struct crypto_skcipher_spawn xctr_spawn;
4062306a36Sopenharmony_ci	struct crypto_shash_spawn polyval_spawn;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct hctr2_tfm_ctx {
4462306a36Sopenharmony_ci	struct crypto_cipher *blockcipher;
4562306a36Sopenharmony_ci	struct crypto_skcipher *xctr;
4662306a36Sopenharmony_ci	struct crypto_shash *polyval;
4762306a36Sopenharmony_ci	u8 L[BLOCKCIPHER_BLOCK_SIZE];
4862306a36Sopenharmony_ci	int hashed_tweak_offset;
4962306a36Sopenharmony_ci	/*
5062306a36Sopenharmony_ci	 * This struct is allocated with extra space for two exported hash
5162306a36Sopenharmony_ci	 * states.  Since the hash state size is not known at compile-time, we
5262306a36Sopenharmony_ci	 * can't add these to the struct directly.
5362306a36Sopenharmony_ci	 *
5462306a36Sopenharmony_ci	 * hashed_tweaklen_divisible;
5562306a36Sopenharmony_ci	 * hashed_tweaklen_remainder;
5662306a36Sopenharmony_ci	 */
5762306a36Sopenharmony_ci};
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistruct hctr2_request_ctx {
6062306a36Sopenharmony_ci	u8 first_block[BLOCKCIPHER_BLOCK_SIZE];
6162306a36Sopenharmony_ci	u8 xctr_iv[BLOCKCIPHER_BLOCK_SIZE];
6262306a36Sopenharmony_ci	struct scatterlist *bulk_part_dst;
6362306a36Sopenharmony_ci	struct scatterlist *bulk_part_src;
6462306a36Sopenharmony_ci	struct scatterlist sg_src[2];
6562306a36Sopenharmony_ci	struct scatterlist sg_dst[2];
6662306a36Sopenharmony_ci	/*
6762306a36Sopenharmony_ci	 * Sub-request sizes are unknown at compile-time, so they need to go
6862306a36Sopenharmony_ci	 * after the members with known sizes.
6962306a36Sopenharmony_ci	 */
7062306a36Sopenharmony_ci	union {
7162306a36Sopenharmony_ci		struct shash_desc hash_desc;
7262306a36Sopenharmony_ci		struct skcipher_request xctr_req;
7362306a36Sopenharmony_ci	} u;
7462306a36Sopenharmony_ci	/*
7562306a36Sopenharmony_ci	 * This struct is allocated with extra space for one exported hash
7662306a36Sopenharmony_ci	 * state.  Since the hash state size is not known at compile-time, we
7762306a36Sopenharmony_ci	 * can't add it to the struct directly.
7862306a36Sopenharmony_ci	 *
7962306a36Sopenharmony_ci	 * hashed_tweak;
8062306a36Sopenharmony_ci	 */
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic inline u8 *hctr2_hashed_tweaklen(const struct hctr2_tfm_ctx *tctx,
8462306a36Sopenharmony_ci					bool has_remainder)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	u8 *p = (u8 *)tctx + sizeof(*tctx);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	if (has_remainder) /* For messages not a multiple of block length */
8962306a36Sopenharmony_ci		p += crypto_shash_statesize(tctx->polyval);
9062306a36Sopenharmony_ci	return p;
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic inline u8 *hctr2_hashed_tweak(const struct hctr2_tfm_ctx *tctx,
9462306a36Sopenharmony_ci				     struct hctr2_request_ctx *rctx)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	return (u8 *)rctx + tctx->hashed_tweak_offset;
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/*
10062306a36Sopenharmony_ci * The input data for each HCTR2 hash step begins with a 16-byte block that
10162306a36Sopenharmony_ci * contains the tweak length and a flag that indicates whether the input is evenly
10262306a36Sopenharmony_ci * divisible into blocks.  Since this implementation only supports one tweak
10362306a36Sopenharmony_ci * length, we precompute the two hash states resulting from hashing the two
10462306a36Sopenharmony_ci * possible values of this initial block.  This reduces by one block the amount of
10562306a36Sopenharmony_ci * data that needs to be hashed for each encryption/decryption
10662306a36Sopenharmony_ci *
10762306a36Sopenharmony_ci * These precomputed hashes are stored in hctr2_tfm_ctx.
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_cistatic int hctr2_hash_tweaklen(struct hctr2_tfm_ctx *tctx, bool has_remainder)
11062306a36Sopenharmony_ci{
11162306a36Sopenharmony_ci	SHASH_DESC_ON_STACK(shash, tfm->polyval);
11262306a36Sopenharmony_ci	__le64 tweak_length_block[2];
11362306a36Sopenharmony_ci	int err;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	shash->tfm = tctx->polyval;
11662306a36Sopenharmony_ci	memset(tweak_length_block, 0, sizeof(tweak_length_block));
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	tweak_length_block[0] = cpu_to_le64(TWEAK_SIZE * 8 * 2 + 2 + has_remainder);
11962306a36Sopenharmony_ci	err = crypto_shash_init(shash);
12062306a36Sopenharmony_ci	if (err)
12162306a36Sopenharmony_ci		return err;
12262306a36Sopenharmony_ci	err = crypto_shash_update(shash, (u8 *)tweak_length_block,
12362306a36Sopenharmony_ci				  POLYVAL_BLOCK_SIZE);
12462306a36Sopenharmony_ci	if (err)
12562306a36Sopenharmony_ci		return err;
12662306a36Sopenharmony_ci	return crypto_shash_export(shash, hctr2_hashed_tweaklen(tctx, has_remainder));
12762306a36Sopenharmony_ci}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistatic int hctr2_setkey(struct crypto_skcipher *tfm, const u8 *key,
13062306a36Sopenharmony_ci			unsigned int keylen)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
13362306a36Sopenharmony_ci	u8 hbar[BLOCKCIPHER_BLOCK_SIZE];
13462306a36Sopenharmony_ci	int err;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
13762306a36Sopenharmony_ci	crypto_cipher_set_flags(tctx->blockcipher,
13862306a36Sopenharmony_ci				crypto_skcipher_get_flags(tfm) &
13962306a36Sopenharmony_ci				CRYPTO_TFM_REQ_MASK);
14062306a36Sopenharmony_ci	err = crypto_cipher_setkey(tctx->blockcipher, key, keylen);
14162306a36Sopenharmony_ci	if (err)
14262306a36Sopenharmony_ci		return err;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	crypto_skcipher_clear_flags(tctx->xctr, CRYPTO_TFM_REQ_MASK);
14562306a36Sopenharmony_ci	crypto_skcipher_set_flags(tctx->xctr,
14662306a36Sopenharmony_ci				  crypto_skcipher_get_flags(tfm) &
14762306a36Sopenharmony_ci				  CRYPTO_TFM_REQ_MASK);
14862306a36Sopenharmony_ci	err = crypto_skcipher_setkey(tctx->xctr, key, keylen);
14962306a36Sopenharmony_ci	if (err)
15062306a36Sopenharmony_ci		return err;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	memset(hbar, 0, sizeof(hbar));
15362306a36Sopenharmony_ci	crypto_cipher_encrypt_one(tctx->blockcipher, hbar, hbar);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	memset(tctx->L, 0, sizeof(tctx->L));
15662306a36Sopenharmony_ci	tctx->L[0] = 0x01;
15762306a36Sopenharmony_ci	crypto_cipher_encrypt_one(tctx->blockcipher, tctx->L, tctx->L);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	crypto_shash_clear_flags(tctx->polyval, CRYPTO_TFM_REQ_MASK);
16062306a36Sopenharmony_ci	crypto_shash_set_flags(tctx->polyval, crypto_skcipher_get_flags(tfm) &
16162306a36Sopenharmony_ci			       CRYPTO_TFM_REQ_MASK);
16262306a36Sopenharmony_ci	err = crypto_shash_setkey(tctx->polyval, hbar, BLOCKCIPHER_BLOCK_SIZE);
16362306a36Sopenharmony_ci	if (err)
16462306a36Sopenharmony_ci		return err;
16562306a36Sopenharmony_ci	memzero_explicit(hbar, sizeof(hbar));
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	return hctr2_hash_tweaklen(tctx, true) ?: hctr2_hash_tweaklen(tctx, false);
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic int hctr2_hash_tweak(struct skcipher_request *req)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
17362306a36Sopenharmony_ci	const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
17462306a36Sopenharmony_ci	struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
17562306a36Sopenharmony_ci	struct shash_desc *hash_desc = &rctx->u.hash_desc;
17662306a36Sopenharmony_ci	int err;
17762306a36Sopenharmony_ci	bool has_remainder = req->cryptlen % POLYVAL_BLOCK_SIZE;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	hash_desc->tfm = tctx->polyval;
18062306a36Sopenharmony_ci	err = crypto_shash_import(hash_desc, hctr2_hashed_tweaklen(tctx, has_remainder));
18162306a36Sopenharmony_ci	if (err)
18262306a36Sopenharmony_ci		return err;
18362306a36Sopenharmony_ci	err = crypto_shash_update(hash_desc, req->iv, TWEAK_SIZE);
18462306a36Sopenharmony_ci	if (err)
18562306a36Sopenharmony_ci		return err;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	// Store the hashed tweak, since we need it when computing both
18862306a36Sopenharmony_ci	// H(T || N) and H(T || V).
18962306a36Sopenharmony_ci	return crypto_shash_export(hash_desc, hctr2_hashed_tweak(tctx, rctx));
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic int hctr2_hash_message(struct skcipher_request *req,
19362306a36Sopenharmony_ci			      struct scatterlist *sgl,
19462306a36Sopenharmony_ci			      u8 digest[POLYVAL_DIGEST_SIZE])
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	static const u8 padding[BLOCKCIPHER_BLOCK_SIZE] = { 0x1 };
19762306a36Sopenharmony_ci	struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
19862306a36Sopenharmony_ci	struct shash_desc *hash_desc = &rctx->u.hash_desc;
19962306a36Sopenharmony_ci	const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
20062306a36Sopenharmony_ci	struct sg_mapping_iter miter;
20162306a36Sopenharmony_ci	unsigned int remainder = bulk_len % BLOCKCIPHER_BLOCK_SIZE;
20262306a36Sopenharmony_ci	int i;
20362306a36Sopenharmony_ci	int err = 0;
20462306a36Sopenharmony_ci	int n = 0;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	sg_miter_start(&miter, sgl, sg_nents(sgl),
20762306a36Sopenharmony_ci		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
20862306a36Sopenharmony_ci	for (i = 0; i < bulk_len; i += n) {
20962306a36Sopenharmony_ci		sg_miter_next(&miter);
21062306a36Sopenharmony_ci		n = min_t(unsigned int, miter.length, bulk_len - i);
21162306a36Sopenharmony_ci		err = crypto_shash_update(hash_desc, miter.addr, n);
21262306a36Sopenharmony_ci		if (err)
21362306a36Sopenharmony_ci			break;
21462306a36Sopenharmony_ci	}
21562306a36Sopenharmony_ci	sg_miter_stop(&miter);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	if (err)
21862306a36Sopenharmony_ci		return err;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	if (remainder) {
22162306a36Sopenharmony_ci		err = crypto_shash_update(hash_desc, padding,
22262306a36Sopenharmony_ci					  BLOCKCIPHER_BLOCK_SIZE - remainder);
22362306a36Sopenharmony_ci		if (err)
22462306a36Sopenharmony_ci			return err;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci	return crypto_shash_final(hash_desc, digest);
22762306a36Sopenharmony_ci}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_cistatic int hctr2_finish(struct skcipher_request *req)
23062306a36Sopenharmony_ci{
23162306a36Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
23262306a36Sopenharmony_ci	const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
23362306a36Sopenharmony_ci	struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
23462306a36Sopenharmony_ci	u8 digest[POLYVAL_DIGEST_SIZE];
23562306a36Sopenharmony_ci	struct shash_desc *hash_desc = &rctx->u.hash_desc;
23662306a36Sopenharmony_ci	int err;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	// U = UU ^ H(T || V)
23962306a36Sopenharmony_ci	// or M = MM ^ H(T || N)
24062306a36Sopenharmony_ci	hash_desc->tfm = tctx->polyval;
24162306a36Sopenharmony_ci	err = crypto_shash_import(hash_desc, hctr2_hashed_tweak(tctx, rctx));
24262306a36Sopenharmony_ci	if (err)
24362306a36Sopenharmony_ci		return err;
24462306a36Sopenharmony_ci	err = hctr2_hash_message(req, rctx->bulk_part_dst, digest);
24562306a36Sopenharmony_ci	if (err)
24662306a36Sopenharmony_ci		return err;
24762306a36Sopenharmony_ci	crypto_xor(rctx->first_block, digest, BLOCKCIPHER_BLOCK_SIZE);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	// Copy U (or M) into dst scatterlist
25062306a36Sopenharmony_ci	scatterwalk_map_and_copy(rctx->first_block, req->dst,
25162306a36Sopenharmony_ci				 0, BLOCKCIPHER_BLOCK_SIZE, 1);
25262306a36Sopenharmony_ci	return 0;
25362306a36Sopenharmony_ci}
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_cistatic void hctr2_xctr_done(void *data, int err)
25662306a36Sopenharmony_ci{
25762306a36Sopenharmony_ci	struct skcipher_request *req = data;
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	if (!err)
26062306a36Sopenharmony_ci		err = hctr2_finish(req);
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	skcipher_request_complete(req, err);
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_cistatic int hctr2_crypt(struct skcipher_request *req, bool enc)
26662306a36Sopenharmony_ci{
26762306a36Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
26862306a36Sopenharmony_ci	const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
26962306a36Sopenharmony_ci	struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
27062306a36Sopenharmony_ci	u8 digest[POLYVAL_DIGEST_SIZE];
27162306a36Sopenharmony_ci	int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
27262306a36Sopenharmony_ci	int err;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci	// Requests must be at least one block
27562306a36Sopenharmony_ci	if (req->cryptlen < BLOCKCIPHER_BLOCK_SIZE)
27662306a36Sopenharmony_ci		return -EINVAL;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	// Copy M (or U) into a temporary buffer
27962306a36Sopenharmony_ci	scatterwalk_map_and_copy(rctx->first_block, req->src,
28062306a36Sopenharmony_ci				 0, BLOCKCIPHER_BLOCK_SIZE, 0);
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	// Create scatterlists for N and V
28362306a36Sopenharmony_ci	rctx->bulk_part_src = scatterwalk_ffwd(rctx->sg_src, req->src,
28462306a36Sopenharmony_ci					       BLOCKCIPHER_BLOCK_SIZE);
28562306a36Sopenharmony_ci	rctx->bulk_part_dst = scatterwalk_ffwd(rctx->sg_dst, req->dst,
28662306a36Sopenharmony_ci					       BLOCKCIPHER_BLOCK_SIZE);
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	// MM = M ^ H(T || N)
28962306a36Sopenharmony_ci	// or UU = U ^ H(T || V)
29062306a36Sopenharmony_ci	err = hctr2_hash_tweak(req);
29162306a36Sopenharmony_ci	if (err)
29262306a36Sopenharmony_ci		return err;
29362306a36Sopenharmony_ci	err = hctr2_hash_message(req, rctx->bulk_part_src, digest);
29462306a36Sopenharmony_ci	if (err)
29562306a36Sopenharmony_ci		return err;
29662306a36Sopenharmony_ci	crypto_xor(digest, rctx->first_block, BLOCKCIPHER_BLOCK_SIZE);
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	// UU = E(MM)
29962306a36Sopenharmony_ci	// or MM = D(UU)
30062306a36Sopenharmony_ci	if (enc)
30162306a36Sopenharmony_ci		crypto_cipher_encrypt_one(tctx->blockcipher, rctx->first_block,
30262306a36Sopenharmony_ci					  digest);
30362306a36Sopenharmony_ci	else
30462306a36Sopenharmony_ci		crypto_cipher_decrypt_one(tctx->blockcipher, rctx->first_block,
30562306a36Sopenharmony_ci					  digest);
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	// S = MM ^ UU ^ L
30862306a36Sopenharmony_ci	crypto_xor(digest, rctx->first_block, BLOCKCIPHER_BLOCK_SIZE);
30962306a36Sopenharmony_ci	crypto_xor_cpy(rctx->xctr_iv, digest, tctx->L, BLOCKCIPHER_BLOCK_SIZE);
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci	// V = XCTR(S, N)
31262306a36Sopenharmony_ci	// or N = XCTR(S, V)
31362306a36Sopenharmony_ci	skcipher_request_set_tfm(&rctx->u.xctr_req, tctx->xctr);
31462306a36Sopenharmony_ci	skcipher_request_set_crypt(&rctx->u.xctr_req, rctx->bulk_part_src,
31562306a36Sopenharmony_ci				   rctx->bulk_part_dst, bulk_len,
31662306a36Sopenharmony_ci				   rctx->xctr_iv);
31762306a36Sopenharmony_ci	skcipher_request_set_callback(&rctx->u.xctr_req,
31862306a36Sopenharmony_ci				      req->base.flags,
31962306a36Sopenharmony_ci				      hctr2_xctr_done, req);
32062306a36Sopenharmony_ci	return crypto_skcipher_encrypt(&rctx->u.xctr_req) ?:
32162306a36Sopenharmony_ci		hctr2_finish(req);
32262306a36Sopenharmony_ci}
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_cistatic int hctr2_encrypt(struct skcipher_request *req)
32562306a36Sopenharmony_ci{
32662306a36Sopenharmony_ci	return hctr2_crypt(req, true);
32762306a36Sopenharmony_ci}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cistatic int hctr2_decrypt(struct skcipher_request *req)
33062306a36Sopenharmony_ci{
33162306a36Sopenharmony_ci	return hctr2_crypt(req, false);
33262306a36Sopenharmony_ci}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_cistatic int hctr2_init_tfm(struct crypto_skcipher *tfm)
33562306a36Sopenharmony_ci{
33662306a36Sopenharmony_ci	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
33762306a36Sopenharmony_ci	struct hctr2_instance_ctx *ictx = skcipher_instance_ctx(inst);
33862306a36Sopenharmony_ci	struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
33962306a36Sopenharmony_ci	struct crypto_skcipher *xctr;
34062306a36Sopenharmony_ci	struct crypto_cipher *blockcipher;
34162306a36Sopenharmony_ci	struct crypto_shash *polyval;
34262306a36Sopenharmony_ci	unsigned int subreq_size;
34362306a36Sopenharmony_ci	int err;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	xctr = crypto_spawn_skcipher(&ictx->xctr_spawn);
34662306a36Sopenharmony_ci	if (IS_ERR(xctr))
34762306a36Sopenharmony_ci		return PTR_ERR(xctr);
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
35062306a36Sopenharmony_ci	if (IS_ERR(blockcipher)) {
35162306a36Sopenharmony_ci		err = PTR_ERR(blockcipher);
35262306a36Sopenharmony_ci		goto err_free_xctr;
35362306a36Sopenharmony_ci	}
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	polyval = crypto_spawn_shash(&ictx->polyval_spawn);
35662306a36Sopenharmony_ci	if (IS_ERR(polyval)) {
35762306a36Sopenharmony_ci		err = PTR_ERR(polyval);
35862306a36Sopenharmony_ci		goto err_free_blockcipher;
35962306a36Sopenharmony_ci	}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	tctx->xctr = xctr;
36262306a36Sopenharmony_ci	tctx->blockcipher = blockcipher;
36362306a36Sopenharmony_ci	tctx->polyval = polyval;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	BUILD_BUG_ON(offsetofend(struct hctr2_request_ctx, u) !=
36662306a36Sopenharmony_ci				 sizeof(struct hctr2_request_ctx));
36762306a36Sopenharmony_ci	subreq_size = max(sizeof_field(struct hctr2_request_ctx, u.hash_desc) +
36862306a36Sopenharmony_ci			  crypto_shash_descsize(polyval),
36962306a36Sopenharmony_ci			  sizeof_field(struct hctr2_request_ctx, u.xctr_req) +
37062306a36Sopenharmony_ci			  crypto_skcipher_reqsize(xctr));
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	tctx->hashed_tweak_offset = offsetof(struct hctr2_request_ctx, u) +
37362306a36Sopenharmony_ci				    subreq_size;
37462306a36Sopenharmony_ci	crypto_skcipher_set_reqsize(tfm, tctx->hashed_tweak_offset +
37562306a36Sopenharmony_ci				    crypto_shash_statesize(polyval));
37662306a36Sopenharmony_ci	return 0;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_cierr_free_blockcipher:
37962306a36Sopenharmony_ci	crypto_free_cipher(blockcipher);
38062306a36Sopenharmony_cierr_free_xctr:
38162306a36Sopenharmony_ci	crypto_free_skcipher(xctr);
38262306a36Sopenharmony_ci	return err;
38362306a36Sopenharmony_ci}
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_cistatic void hctr2_exit_tfm(struct crypto_skcipher *tfm)
38662306a36Sopenharmony_ci{
38762306a36Sopenharmony_ci	struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	crypto_free_cipher(tctx->blockcipher);
39062306a36Sopenharmony_ci	crypto_free_skcipher(tctx->xctr);
39162306a36Sopenharmony_ci	crypto_free_shash(tctx->polyval);
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_cistatic void hctr2_free_instance(struct skcipher_instance *inst)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	struct hctr2_instance_ctx *ictx = skcipher_instance_ctx(inst);
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	crypto_drop_cipher(&ictx->blockcipher_spawn);
39962306a36Sopenharmony_ci	crypto_drop_skcipher(&ictx->xctr_spawn);
40062306a36Sopenharmony_ci	crypto_drop_shash(&ictx->polyval_spawn);
40162306a36Sopenharmony_ci	kfree(inst);
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_cistatic int hctr2_create_common(struct crypto_template *tmpl,
40562306a36Sopenharmony_ci			       struct rtattr **tb,
40662306a36Sopenharmony_ci			       const char *xctr_name,
40762306a36Sopenharmony_ci			       const char *polyval_name)
40862306a36Sopenharmony_ci{
40962306a36Sopenharmony_ci	u32 mask;
41062306a36Sopenharmony_ci	struct skcipher_instance *inst;
41162306a36Sopenharmony_ci	struct hctr2_instance_ctx *ictx;
41262306a36Sopenharmony_ci	struct skcipher_alg *xctr_alg;
41362306a36Sopenharmony_ci	struct crypto_alg *blockcipher_alg;
41462306a36Sopenharmony_ci	struct shash_alg *polyval_alg;
41562306a36Sopenharmony_ci	char blockcipher_name[CRYPTO_MAX_ALG_NAME];
41662306a36Sopenharmony_ci	int len;
41762306a36Sopenharmony_ci	int err;
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
42062306a36Sopenharmony_ci	if (err)
42162306a36Sopenharmony_ci		return err;
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci	inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
42462306a36Sopenharmony_ci	if (!inst)
42562306a36Sopenharmony_ci		return -ENOMEM;
42662306a36Sopenharmony_ci	ictx = skcipher_instance_ctx(inst);
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	/* Stream cipher, xctr(block_cipher) */
42962306a36Sopenharmony_ci	err = crypto_grab_skcipher(&ictx->xctr_spawn,
43062306a36Sopenharmony_ci				   skcipher_crypto_instance(inst),
43162306a36Sopenharmony_ci				   xctr_name, 0, mask);
43262306a36Sopenharmony_ci	if (err)
43362306a36Sopenharmony_ci		goto err_free_inst;
43462306a36Sopenharmony_ci	xctr_alg = crypto_spawn_skcipher_alg(&ictx->xctr_spawn);
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	err = -EINVAL;
43762306a36Sopenharmony_ci	if (strncmp(xctr_alg->base.cra_name, "xctr(", 5))
43862306a36Sopenharmony_ci		goto err_free_inst;
43962306a36Sopenharmony_ci	len = strscpy(blockcipher_name, xctr_alg->base.cra_name + 5,
44062306a36Sopenharmony_ci		      sizeof(blockcipher_name));
44162306a36Sopenharmony_ci	if (len < 1)
44262306a36Sopenharmony_ci		goto err_free_inst;
44362306a36Sopenharmony_ci	if (blockcipher_name[len - 1] != ')')
44462306a36Sopenharmony_ci		goto err_free_inst;
44562306a36Sopenharmony_ci	blockcipher_name[len - 1] = 0;
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	/* Block cipher, e.g. "aes" */
44862306a36Sopenharmony_ci	err = crypto_grab_cipher(&ictx->blockcipher_spawn,
44962306a36Sopenharmony_ci				 skcipher_crypto_instance(inst),
45062306a36Sopenharmony_ci				 blockcipher_name, 0, mask);
45162306a36Sopenharmony_ci	if (err)
45262306a36Sopenharmony_ci		goto err_free_inst;
45362306a36Sopenharmony_ci	blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci	/* Require blocksize of 16 bytes */
45662306a36Sopenharmony_ci	err = -EINVAL;
45762306a36Sopenharmony_ci	if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
45862306a36Sopenharmony_ci		goto err_free_inst;
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	/* Polyval ε-∆U hash function */
46162306a36Sopenharmony_ci	err = crypto_grab_shash(&ictx->polyval_spawn,
46262306a36Sopenharmony_ci				skcipher_crypto_instance(inst),
46362306a36Sopenharmony_ci				polyval_name, 0, mask);
46462306a36Sopenharmony_ci	if (err)
46562306a36Sopenharmony_ci		goto err_free_inst;
46662306a36Sopenharmony_ci	polyval_alg = crypto_spawn_shash_alg(&ictx->polyval_spawn);
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	/* Ensure Polyval is being used */
46962306a36Sopenharmony_ci	err = -EINVAL;
47062306a36Sopenharmony_ci	if (strcmp(polyval_alg->base.cra_name, "polyval") != 0)
47162306a36Sopenharmony_ci		goto err_free_inst;
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	/* Instance fields */
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	err = -ENAMETOOLONG;
47662306a36Sopenharmony_ci	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "hctr2(%s)",
47762306a36Sopenharmony_ci		     blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
47862306a36Sopenharmony_ci		goto err_free_inst;
47962306a36Sopenharmony_ci	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
48062306a36Sopenharmony_ci		     "hctr2_base(%s,%s)",
48162306a36Sopenharmony_ci		     xctr_alg->base.cra_driver_name,
48262306a36Sopenharmony_ci		     polyval_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
48362306a36Sopenharmony_ci		goto err_free_inst;
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
48662306a36Sopenharmony_ci	inst->alg.base.cra_ctxsize = sizeof(struct hctr2_tfm_ctx) +
48762306a36Sopenharmony_ci				     polyval_alg->statesize * 2;
48862306a36Sopenharmony_ci	inst->alg.base.cra_alignmask = xctr_alg->base.cra_alignmask |
48962306a36Sopenharmony_ci				       polyval_alg->base.cra_alignmask;
49062306a36Sopenharmony_ci	/*
49162306a36Sopenharmony_ci	 * The hash function is called twice, so it is weighted higher than the
49262306a36Sopenharmony_ci	 * xctr and blockcipher.
49362306a36Sopenharmony_ci	 */
49462306a36Sopenharmony_ci	inst->alg.base.cra_priority = (2 * xctr_alg->base.cra_priority +
49562306a36Sopenharmony_ci				       4 * polyval_alg->base.cra_priority +
49662306a36Sopenharmony_ci				       blockcipher_alg->cra_priority) / 7;
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	inst->alg.setkey = hctr2_setkey;
49962306a36Sopenharmony_ci	inst->alg.encrypt = hctr2_encrypt;
50062306a36Sopenharmony_ci	inst->alg.decrypt = hctr2_decrypt;
50162306a36Sopenharmony_ci	inst->alg.init = hctr2_init_tfm;
50262306a36Sopenharmony_ci	inst->alg.exit = hctr2_exit_tfm;
50362306a36Sopenharmony_ci	inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(xctr_alg);
50462306a36Sopenharmony_ci	inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(xctr_alg);
50562306a36Sopenharmony_ci	inst->alg.ivsize = TWEAK_SIZE;
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	inst->free = hctr2_free_instance;
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	err = skcipher_register_instance(tmpl, inst);
51062306a36Sopenharmony_ci	if (err) {
51162306a36Sopenharmony_cierr_free_inst:
51262306a36Sopenharmony_ci		hctr2_free_instance(inst);
51362306a36Sopenharmony_ci	}
51462306a36Sopenharmony_ci	return err;
51562306a36Sopenharmony_ci}
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_cistatic int hctr2_create_base(struct crypto_template *tmpl, struct rtattr **tb)
51862306a36Sopenharmony_ci{
51962306a36Sopenharmony_ci	const char *xctr_name;
52062306a36Sopenharmony_ci	const char *polyval_name;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	xctr_name = crypto_attr_alg_name(tb[1]);
52362306a36Sopenharmony_ci	if (IS_ERR(xctr_name))
52462306a36Sopenharmony_ci		return PTR_ERR(xctr_name);
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	polyval_name = crypto_attr_alg_name(tb[2]);
52762306a36Sopenharmony_ci	if (IS_ERR(polyval_name))
52862306a36Sopenharmony_ci		return PTR_ERR(polyval_name);
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	return hctr2_create_common(tmpl, tb, xctr_name, polyval_name);
53162306a36Sopenharmony_ci}
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_cistatic int hctr2_create(struct crypto_template *tmpl, struct rtattr **tb)
53462306a36Sopenharmony_ci{
53562306a36Sopenharmony_ci	const char *blockcipher_name;
53662306a36Sopenharmony_ci	char xctr_name[CRYPTO_MAX_ALG_NAME];
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	blockcipher_name = crypto_attr_alg_name(tb[1]);
53962306a36Sopenharmony_ci	if (IS_ERR(blockcipher_name))
54062306a36Sopenharmony_ci		return PTR_ERR(blockcipher_name);
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	if (snprintf(xctr_name, CRYPTO_MAX_ALG_NAME, "xctr(%s)",
54362306a36Sopenharmony_ci		    blockcipher_name) >= CRYPTO_MAX_ALG_NAME)
54462306a36Sopenharmony_ci		return -ENAMETOOLONG;
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci	return hctr2_create_common(tmpl, tb, xctr_name, "polyval");
54762306a36Sopenharmony_ci}
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_cistatic struct crypto_template hctr2_tmpls[] = {
55062306a36Sopenharmony_ci	{
55162306a36Sopenharmony_ci		/* hctr2_base(xctr_name, polyval_name) */
55262306a36Sopenharmony_ci		.name = "hctr2_base",
55362306a36Sopenharmony_ci		.create = hctr2_create_base,
55462306a36Sopenharmony_ci		.module = THIS_MODULE,
55562306a36Sopenharmony_ci	}, {
55662306a36Sopenharmony_ci		/* hctr2(blockcipher_name) */
55762306a36Sopenharmony_ci		.name = "hctr2",
55862306a36Sopenharmony_ci		.create = hctr2_create,
55962306a36Sopenharmony_ci		.module = THIS_MODULE,
56062306a36Sopenharmony_ci	}
56162306a36Sopenharmony_ci};
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_cistatic int __init hctr2_module_init(void)
56462306a36Sopenharmony_ci{
56562306a36Sopenharmony_ci	return crypto_register_templates(hctr2_tmpls, ARRAY_SIZE(hctr2_tmpls));
56662306a36Sopenharmony_ci}
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_cistatic void __exit hctr2_module_exit(void)
56962306a36Sopenharmony_ci{
57062306a36Sopenharmony_ci	return crypto_unregister_templates(hctr2_tmpls,
57162306a36Sopenharmony_ci					   ARRAY_SIZE(hctr2_tmpls));
57262306a36Sopenharmony_ci}
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_cisubsys_initcall(hctr2_module_init);
57562306a36Sopenharmony_cimodule_exit(hctr2_module_exit);
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ciMODULE_DESCRIPTION("HCTR2 length-preserving encryption mode");
57862306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
57962306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("hctr2");
58062306a36Sopenharmony_ciMODULE_IMPORT_NS(CRYPTO_INTERNAL);
581