Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Gary R Hook <gary.hook@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Author: Tom Lendacky <thomas.lendacky@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <crypto/xts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "ccp-crypto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct ccp_aes_xts_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	const char *drv_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static const struct ccp_aes_xts_def aes_xts_algs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		.name		= "xts(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		.drv_name	= "xts-aes-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct ccp_unit_size_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static struct ccp_unit_size_map xts_unit_sizes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		.size   = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		.value	= CCP_XTS_AES_UNIT_SIZE_16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		.size   = 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		.value	= CCP_XTS_AES_UNIT_SIZE_512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		.size   = 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		.value	= CCP_XTS_AES_UNIT_SIZE_1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		.size   = 2048,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.value	= CCP_XTS_AES_UNIT_SIZE_2048,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		.size   = 4096,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		.value	= CCP_XTS_AES_UNIT_SIZE_4096,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct skcipher_request *req = skcipher_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	memcpy(req->iv, rctx->iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static int ccp_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			      unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	unsigned int ccpversion = ccp_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ret = xts_verify_key(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	/* Version 3 devices support 128-bit keys; version 5 devices can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	 * accommodate 128- and 256-bit keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	switch (key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	case AES_KEYSIZE_128 * 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		memcpy(ctx->u.aes.key, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	case AES_KEYSIZE_256 * 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		if (ccpversion > CCP_VERSION(3, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			memcpy(ctx->u.aes.key, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	ctx->u.aes.key_len = key_len / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return crypto_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int ccp_aes_xts_crypt(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			     unsigned int encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned int ccpversion = ccp_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned int fallback = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	unsigned int unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	u32 unit_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!ctx->u.aes.key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!req->iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	/* Check conditions under which the CCP can fulfill a request. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 * device can handle input plaintext of a length that is a multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	 * of the unit_size, bug the crypto implementation only supports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	 * the unit_size being equal to the input length. This limits the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * number of scenarios we can handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (req->cryptlen == xts_unit_sizes[unit].size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			unit_size = unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	/* The CCP has restrictions on block sizes. Also, a version 3 device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * only supports AES-128 operations; version 5 CCPs support both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * AES-128 and -256 operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (unit_size == CCP_XTS_AES_UNIT_SIZE__LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		fallback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if ((ccpversion < CCP_VERSION(5, 0)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	    (ctx->u.aes.key_len != AES_KEYSIZE_128))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		fallback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if ((ctx->u.aes.key_len != AES_KEYSIZE_128) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	    (ctx->u.aes.key_len != AES_KEYSIZE_256))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		fallback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (fallback) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		/* Use the fallback to process the request for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		 * unsupported unit sizes or key sizes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		skcipher_request_set_tfm(&rctx->fallback_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 					 ctx->u.aes.tfm_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		skcipher_request_set_callback(&rctx->fallback_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 					      req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 					      req->base.complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 					      req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		skcipher_request_set_crypt(&rctx->fallback_req, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 					   req->dst, req->cryptlen, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				crypto_skcipher_decrypt(&rctx->fallback_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	memcpy(rctx->iv, req->iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	INIT_LIST_HEAD(&rctx->cmd.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	rctx->cmd.u.xts.type = CCP_AES_TYPE_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 					   : CCP_AES_ACTION_DECRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	rctx->cmd.u.xts.unit_size = unit_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	rctx->cmd.u.xts.iv = &rctx->iv_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	rctx->cmd.u.xts.src = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	rctx->cmd.u.xts.src_len = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	rctx->cmd.u.xts.dst = req->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int ccp_aes_xts_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return ccp_aes_xts_crypt(req, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int ccp_aes_xts_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return ccp_aes_xts_crypt(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int ccp_aes_xts_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct crypto_skcipher *fallback_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ctx->complete = ccp_aes_xts_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ctx->u.aes.key_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	fallback_tfm = crypto_alloc_skcipher("xts(aes)", 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 					     CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (IS_ERR(fallback_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		pr_warn("could not load fallback driver xts(aes)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return PTR_ERR(fallback_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	ctx->u.aes.tfm_skcipher = fallback_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 					 crypto_skcipher_reqsize(fallback_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void ccp_aes_xts_exit_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	crypto_free_skcipher(ctx->u.aes.tfm_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int ccp_register_aes_xts_alg(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				    const struct ccp_aes_xts_def *def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct ccp_crypto_skcipher_alg *ccp_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!ccp_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	INIT_LIST_HEAD(&ccp_alg->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	alg = &ccp_alg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		 def->drv_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	alg->base.cra_flags	= CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				  CRYPTO_ALG_ALLOCATES_MEMORY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				  CRYPTO_ALG_NEED_FALLBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	alg->base.cra_blocksize	= AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	alg->base.cra_ctxsize	= sizeof(struct ccp_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	alg->base.cra_priority	= CCP_CRA_PRIORITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	alg->base.cra_module	= THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	alg->setkey		= ccp_aes_xts_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	alg->encrypt		= ccp_aes_xts_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	alg->decrypt		= ccp_aes_xts_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	alg->min_keysize	= AES_MIN_KEY_SIZE * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	alg->max_keysize	= AES_MAX_KEY_SIZE * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	alg->ivsize		= AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	alg->init		= ccp_aes_xts_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	alg->exit		= ccp_aes_xts_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ret = crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		pr_err("%s skcipher algorithm registration error (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		       alg->base.cra_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		kfree(ccp_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	list_add(&ccp_alg->entry, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int ccp_register_aes_xts_algs(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }