11d6b8a6fSTom Lendacky /*
21d6b8a6fSTom Lendacky  * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
31d6b8a6fSTom Lendacky  *
41d6b8a6fSTom Lendacky  * Copyright (C) 2013 Advanced Micro Devices, Inc.
51d6b8a6fSTom Lendacky  *
61d6b8a6fSTom Lendacky  * Author: Tom Lendacky <thomas.lendacky@amd.com>
71d6b8a6fSTom Lendacky  *
81d6b8a6fSTom Lendacky  * This program is free software; you can redistribute it and/or modify
91d6b8a6fSTom Lendacky  * it under the terms of the GNU General Public License version 2 as
101d6b8a6fSTom Lendacky  * published by the Free Software Foundation.
111d6b8a6fSTom Lendacky  */
121d6b8a6fSTom Lendacky 
131d6b8a6fSTom Lendacky #include <linux/module.h>
141d6b8a6fSTom Lendacky #include <linux/sched.h>
151d6b8a6fSTom Lendacky #include <linux/delay.h>
161d6b8a6fSTom Lendacky #include <linux/scatterlist.h>
171d6b8a6fSTom Lendacky #include <linux/crypto.h>
181d6b8a6fSTom Lendacky #include <crypto/algapi.h>
191d6b8a6fSTom Lendacky #include <crypto/aes.h>
201d6b8a6fSTom Lendacky #include <crypto/scatterwalk.h>
211d6b8a6fSTom Lendacky 
221d6b8a6fSTom Lendacky #include "ccp-crypto.h"
231d6b8a6fSTom Lendacky 
241d6b8a6fSTom Lendacky struct ccp_aes_xts_def {
251d6b8a6fSTom Lendacky 	const char *name;
261d6b8a6fSTom Lendacky 	const char *drv_name;
271d6b8a6fSTom Lendacky };
281d6b8a6fSTom Lendacky 
291d6b8a6fSTom Lendacky static struct ccp_aes_xts_def aes_xts_algs[] = {
301d6b8a6fSTom Lendacky 	{
311d6b8a6fSTom Lendacky 		.name		= "xts(aes)",
321d6b8a6fSTom Lendacky 		.drv_name	= "xts-aes-ccp",
331d6b8a6fSTom Lendacky 	},
341d6b8a6fSTom Lendacky };
351d6b8a6fSTom Lendacky 
361d6b8a6fSTom Lendacky struct ccp_unit_size_map {
371d6b8a6fSTom Lendacky 	unsigned int size;
381d6b8a6fSTom Lendacky 	u32 value;
391d6b8a6fSTom Lendacky };
401d6b8a6fSTom Lendacky 
411d6b8a6fSTom Lendacky static struct ccp_unit_size_map unit_size_map[] = {
421d6b8a6fSTom Lendacky 	{
431d6b8a6fSTom Lendacky 		.size	= 4096,
441d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE_4096,
451d6b8a6fSTom Lendacky 	},
461d6b8a6fSTom Lendacky 	{
471d6b8a6fSTom Lendacky 		.size	= 2048,
481d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE_2048,
491d6b8a6fSTom Lendacky 	},
501d6b8a6fSTom Lendacky 	{
511d6b8a6fSTom Lendacky 		.size	= 1024,
521d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE_1024,
531d6b8a6fSTom Lendacky 	},
541d6b8a6fSTom Lendacky 	{
551d6b8a6fSTom Lendacky 		.size	= 512,
561d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE_512,
571d6b8a6fSTom Lendacky 	},
581d6b8a6fSTom Lendacky 	{
591d6b8a6fSTom Lendacky 		.size	= 256,
601d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
611d6b8a6fSTom Lendacky 	},
621d6b8a6fSTom Lendacky 	{
631d6b8a6fSTom Lendacky 		.size	= 128,
641d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
651d6b8a6fSTom Lendacky 	},
661d6b8a6fSTom Lendacky 	{
671d6b8a6fSTom Lendacky 		.size	= 64,
681d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
691d6b8a6fSTom Lendacky 	},
701d6b8a6fSTom Lendacky 	{
711d6b8a6fSTom Lendacky 		.size	= 32,
721d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
731d6b8a6fSTom Lendacky 	},
741d6b8a6fSTom Lendacky 	{
751d6b8a6fSTom Lendacky 		.size	= 16,
761d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE_16,
771d6b8a6fSTom Lendacky 	},
781d6b8a6fSTom Lendacky 	{
791d6b8a6fSTom Lendacky 		.size	= 1,
801d6b8a6fSTom Lendacky 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
811d6b8a6fSTom Lendacky 	},
821d6b8a6fSTom Lendacky };
831d6b8a6fSTom Lendacky 
841d6b8a6fSTom Lendacky static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
851d6b8a6fSTom Lendacky {
861d6b8a6fSTom Lendacky 	struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
871d6b8a6fSTom Lendacky 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
881d6b8a6fSTom Lendacky 
891d6b8a6fSTom Lendacky 	if (ret)
901d6b8a6fSTom Lendacky 		return ret;
911d6b8a6fSTom Lendacky 
921d6b8a6fSTom Lendacky 	memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
931d6b8a6fSTom Lendacky 
941d6b8a6fSTom Lendacky 	return 0;
951d6b8a6fSTom Lendacky }
961d6b8a6fSTom Lendacky 
971d6b8a6fSTom Lendacky static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
981d6b8a6fSTom Lendacky 			      unsigned int key_len)
991d6b8a6fSTom Lendacky {
1001d6b8a6fSTom Lendacky 	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
1011d6b8a6fSTom Lendacky 
1021d6b8a6fSTom Lendacky 	/* Only support 128-bit AES key with a 128-bit Tweak key,
1031d6b8a6fSTom Lendacky 	 * otherwise use the fallback
1041d6b8a6fSTom Lendacky 	 */
1051d6b8a6fSTom Lendacky 	switch (key_len) {
1061d6b8a6fSTom Lendacky 	case AES_KEYSIZE_128 * 2:
1071d6b8a6fSTom Lendacky 		memcpy(ctx->u.aes.key, key, key_len);
1081d6b8a6fSTom Lendacky 		break;
1091d6b8a6fSTom Lendacky 	}
1101d6b8a6fSTom Lendacky 	ctx->u.aes.key_len = key_len / 2;
1111d6b8a6fSTom Lendacky 	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
1121d6b8a6fSTom Lendacky 
1131d6b8a6fSTom Lendacky 	return crypto_ablkcipher_setkey(ctx->u.aes.tfm_ablkcipher, key,
1141d6b8a6fSTom Lendacky 					key_len);
1151d6b8a6fSTom Lendacky }
1161d6b8a6fSTom Lendacky 
1171d6b8a6fSTom Lendacky static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
1181d6b8a6fSTom Lendacky 			     unsigned int encrypt)
1191d6b8a6fSTom Lendacky {
1201d6b8a6fSTom Lendacky 	struct crypto_tfm *tfm =
1211d6b8a6fSTom Lendacky 		crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
1221d6b8a6fSTom Lendacky 	struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
1231d6b8a6fSTom Lendacky 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
1241d6b8a6fSTom Lendacky 	unsigned int unit;
125ab6a11a7STom Lendacky 	u32 unit_size;
1261d6b8a6fSTom Lendacky 	int ret;
1271d6b8a6fSTom Lendacky 
128369f3dabSTom Lendacky 	if (!ctx->u.aes.key_len)
1291d6b8a6fSTom Lendacky 		return -EINVAL;
1301d6b8a6fSTom Lendacky 
131369f3dabSTom Lendacky 	if (req->nbytes & (AES_BLOCK_SIZE - 1))
1321d6b8a6fSTom Lendacky 		return -EINVAL;
1331d6b8a6fSTom Lendacky 
134369f3dabSTom Lendacky 	if (!req->info)
1351d6b8a6fSTom Lendacky 		return -EINVAL;
1361d6b8a6fSTom Lendacky 
137ab6a11a7STom Lendacky 	unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
138ab6a11a7STom Lendacky 	if (req->nbytes <= unit_size_map[0].size) {
139ab6a11a7STom Lendacky 		for (unit = 0; unit < ARRAY_SIZE(unit_size_map); unit++) {
140ab6a11a7STom Lendacky 			if (!(req->nbytes & (unit_size_map[unit].size - 1))) {
141ab6a11a7STom Lendacky 				unit_size = unit_size_map[unit].value;
1421d6b8a6fSTom Lendacky 				break;
143ab6a11a7STom Lendacky 			}
144ab6a11a7STom Lendacky 		}
145ab6a11a7STom Lendacky 	}
1461d6b8a6fSTom Lendacky 
147ab6a11a7STom Lendacky 	if ((unit_size == CCP_XTS_AES_UNIT_SIZE__LAST) ||
1481d6b8a6fSTom Lendacky 	    (ctx->u.aes.key_len != AES_KEYSIZE_128)) {
1491d6b8a6fSTom Lendacky 		/* Use the fallback to process the request for any
1501d6b8a6fSTom Lendacky 		 * unsupported unit sizes or key sizes
1511d6b8a6fSTom Lendacky 		 */
1521d6b8a6fSTom Lendacky 		ablkcipher_request_set_tfm(req, ctx->u.aes.tfm_ablkcipher);
1531d6b8a6fSTom Lendacky 		ret = (encrypt) ? crypto_ablkcipher_encrypt(req) :
1541d6b8a6fSTom Lendacky 				  crypto_ablkcipher_decrypt(req);
1551d6b8a6fSTom Lendacky 		ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
1561d6b8a6fSTom Lendacky 
1571d6b8a6fSTom Lendacky 		return ret;
1581d6b8a6fSTom Lendacky 	}
1591d6b8a6fSTom Lendacky 
1601d6b8a6fSTom Lendacky 	memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
1611d6b8a6fSTom Lendacky 	sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
1621d6b8a6fSTom Lendacky 
1631d6b8a6fSTom Lendacky 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
1641d6b8a6fSTom Lendacky 	INIT_LIST_HEAD(&rctx->cmd.entry);
1651d6b8a6fSTom Lendacky 	rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
1661d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
1671d6b8a6fSTom Lendacky 					   : CCP_AES_ACTION_DECRYPT;
168ab6a11a7STom Lendacky 	rctx->cmd.u.xts.unit_size = unit_size;
1691d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
1701d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
1711d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.iv = &rctx->iv_sg;
1721d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
1731d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.src = req->src;
1741d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.src_len = req->nbytes;
1751d6b8a6fSTom Lendacky 	rctx->cmd.u.xts.dst = req->dst;
1761d6b8a6fSTom Lendacky 
1771d6b8a6fSTom Lendacky 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
1781d6b8a6fSTom Lendacky 
1791d6b8a6fSTom Lendacky 	return ret;
1801d6b8a6fSTom Lendacky }
1811d6b8a6fSTom Lendacky 
1821d6b8a6fSTom Lendacky static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
1831d6b8a6fSTom Lendacky {
1841d6b8a6fSTom Lendacky 	return ccp_aes_xts_crypt(req, 1);
1851d6b8a6fSTom Lendacky }
1861d6b8a6fSTom Lendacky 
1871d6b8a6fSTom Lendacky static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
1881d6b8a6fSTom Lendacky {
1891d6b8a6fSTom Lendacky 	return ccp_aes_xts_crypt(req, 0);
1901d6b8a6fSTom Lendacky }
1911d6b8a6fSTom Lendacky 
1921d6b8a6fSTom Lendacky static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
1931d6b8a6fSTom Lendacky {
1941d6b8a6fSTom Lendacky 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
1951d6b8a6fSTom Lendacky 	struct crypto_ablkcipher *fallback_tfm;
1961d6b8a6fSTom Lendacky 
1971d6b8a6fSTom Lendacky 	ctx->complete = ccp_aes_xts_complete;
1981d6b8a6fSTom Lendacky 	ctx->u.aes.key_len = 0;
1991d6b8a6fSTom Lendacky 
200b4168a19SMarek Vasut 	fallback_tfm = crypto_alloc_ablkcipher(crypto_tfm_alg_name(tfm), 0,
2011d6b8a6fSTom Lendacky 					       CRYPTO_ALG_ASYNC |
2021d6b8a6fSTom Lendacky 					       CRYPTO_ALG_NEED_FALLBACK);
2031d6b8a6fSTom Lendacky 	if (IS_ERR(fallback_tfm)) {
2041d6b8a6fSTom Lendacky 		pr_warn("could not load fallback driver %s\n",
205b4168a19SMarek Vasut 			crypto_tfm_alg_name(tfm));
2061d6b8a6fSTom Lendacky 		return PTR_ERR(fallback_tfm);
2071d6b8a6fSTom Lendacky 	}
2081d6b8a6fSTom Lendacky 	ctx->u.aes.tfm_ablkcipher = fallback_tfm;
2091d6b8a6fSTom Lendacky 
2101d6b8a6fSTom Lendacky 	tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx) +
2111d6b8a6fSTom Lendacky 				      fallback_tfm->base.crt_ablkcipher.reqsize;
2121d6b8a6fSTom Lendacky 
2131d6b8a6fSTom Lendacky 	return 0;
2141d6b8a6fSTom Lendacky }
2151d6b8a6fSTom Lendacky 
2161d6b8a6fSTom Lendacky static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
2171d6b8a6fSTom Lendacky {
2181d6b8a6fSTom Lendacky 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
2191d6b8a6fSTom Lendacky 
2201d6b8a6fSTom Lendacky 	if (ctx->u.aes.tfm_ablkcipher)
2211d6b8a6fSTom Lendacky 		crypto_free_ablkcipher(ctx->u.aes.tfm_ablkcipher);
2221d6b8a6fSTom Lendacky 	ctx->u.aes.tfm_ablkcipher = NULL;
2231d6b8a6fSTom Lendacky }
2241d6b8a6fSTom Lendacky 
2251d6b8a6fSTom Lendacky static int ccp_register_aes_xts_alg(struct list_head *head,
2261d6b8a6fSTom Lendacky 				    const struct ccp_aes_xts_def *def)
2271d6b8a6fSTom Lendacky {
2281d6b8a6fSTom Lendacky 	struct ccp_crypto_ablkcipher_alg *ccp_alg;
2291d6b8a6fSTom Lendacky 	struct crypto_alg *alg;
2301d6b8a6fSTom Lendacky 	int ret;
2311d6b8a6fSTom Lendacky 
2321d6b8a6fSTom Lendacky 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
2331d6b8a6fSTom Lendacky 	if (!ccp_alg)
2341d6b8a6fSTom Lendacky 		return -ENOMEM;
2351d6b8a6fSTom Lendacky 
2361d6b8a6fSTom Lendacky 	INIT_LIST_HEAD(&ccp_alg->entry);
2371d6b8a6fSTom Lendacky 
2381d6b8a6fSTom Lendacky 	alg = &ccp_alg->alg;
2391d6b8a6fSTom Lendacky 
2401d6b8a6fSTom Lendacky 	snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
2411d6b8a6fSTom Lendacky 	snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
2421d6b8a6fSTom Lendacky 		 def->drv_name);
2431d6b8a6fSTom Lendacky 	alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
2441d6b8a6fSTom Lendacky 			 CRYPTO_ALG_KERN_DRIVER_ONLY |
2451d6b8a6fSTom Lendacky 			 CRYPTO_ALG_NEED_FALLBACK;
2461d6b8a6fSTom Lendacky 	alg->cra_blocksize = AES_BLOCK_SIZE;
2471d6b8a6fSTom Lendacky 	alg->cra_ctxsize = sizeof(struct ccp_ctx);
2481d6b8a6fSTom Lendacky 	alg->cra_priority = CCP_CRA_PRIORITY;
2491d6b8a6fSTom Lendacky 	alg->cra_type = &crypto_ablkcipher_type;
2501d6b8a6fSTom Lendacky 	alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
2511d6b8a6fSTom Lendacky 	alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
2521d6b8a6fSTom Lendacky 	alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
2531d6b8a6fSTom Lendacky 	alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
2541d6b8a6fSTom Lendacky 	alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
2551d6b8a6fSTom Lendacky 	alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
2561d6b8a6fSTom Lendacky 	alg->cra_init = ccp_aes_xts_cra_init;
2571d6b8a6fSTom Lendacky 	alg->cra_exit = ccp_aes_xts_cra_exit;
2581d6b8a6fSTom Lendacky 	alg->cra_module = THIS_MODULE;
2591d6b8a6fSTom Lendacky 
2601d6b8a6fSTom Lendacky 	ret = crypto_register_alg(alg);
2611d6b8a6fSTom Lendacky 	if (ret) {
2621d6b8a6fSTom Lendacky 		pr_err("%s ablkcipher algorithm registration error (%d)\n",
2631d6b8a6fSTom Lendacky 		       alg->cra_name, ret);
2641d6b8a6fSTom Lendacky 		kfree(ccp_alg);
2651d6b8a6fSTom Lendacky 		return ret;
2661d6b8a6fSTom Lendacky 	}
2671d6b8a6fSTom Lendacky 
2681d6b8a6fSTom Lendacky 	list_add(&ccp_alg->entry, head);
2691d6b8a6fSTom Lendacky 
2701d6b8a6fSTom Lendacky 	return 0;
2711d6b8a6fSTom Lendacky }
2721d6b8a6fSTom Lendacky 
2731d6b8a6fSTom Lendacky int ccp_register_aes_xts_algs(struct list_head *head)
2741d6b8a6fSTom Lendacky {
2751d6b8a6fSTom Lendacky 	int i, ret;
2761d6b8a6fSTom Lendacky 
2771d6b8a6fSTom Lendacky 	for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
2781d6b8a6fSTom Lendacky 		ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
2791d6b8a6fSTom Lendacky 		if (ret)
2801d6b8a6fSTom Lendacky 			return ret;
2811d6b8a6fSTom Lendacky 	}
2821d6b8a6fSTom Lendacky 
2831d6b8a6fSTom Lendacky 	return 0;
2841d6b8a6fSTom Lendacky }
285