18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AMD Cryptographic Coprocessor (CCP) AES GCM crypto API support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Gary R Hook <gary.hook@amd.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/sched.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
148c2ecf20Sopenharmony_ci#include <linux/crypto.h>
158c2ecf20Sopenharmony_ci#include <crypto/internal/aead.h>
168c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
178c2ecf20Sopenharmony_ci#include <crypto/aes.h>
188c2ecf20Sopenharmony_ci#include <crypto/ctr.h>
198c2ecf20Sopenharmony_ci#include <crypto/gcm.h>
208c2ecf20Sopenharmony_ci#include <crypto/scatterwalk.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "ccp-crypto.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_complete(struct crypto_async_request *async_req, int ret)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	return ret;
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
308c2ecf20Sopenharmony_ci			      unsigned int key_len)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	switch (key_len) {
358c2ecf20Sopenharmony_ci	case AES_KEYSIZE_128:
368c2ecf20Sopenharmony_ci		ctx->u.aes.type = CCP_AES_TYPE_128;
378c2ecf20Sopenharmony_ci		break;
388c2ecf20Sopenharmony_ci	case AES_KEYSIZE_192:
398c2ecf20Sopenharmony_ci		ctx->u.aes.type = CCP_AES_TYPE_192;
408c2ecf20Sopenharmony_ci		break;
418c2ecf20Sopenharmony_ci	case AES_KEYSIZE_256:
428c2ecf20Sopenharmony_ci		ctx->u.aes.type = CCP_AES_TYPE_256;
438c2ecf20Sopenharmony_ci		break;
448c2ecf20Sopenharmony_ci	default:
458c2ecf20Sopenharmony_ci		return -EINVAL;
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	ctx->u.aes.mode = CCP_AES_MODE_GCM;
498c2ecf20Sopenharmony_ci	ctx->u.aes.key_len = key_len;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	memcpy(ctx->u.aes.key, key, key_len);
528c2ecf20Sopenharmony_ci	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return 0;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
588c2ecf20Sopenharmony_ci				   unsigned int authsize)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	switch (authsize) {
618c2ecf20Sopenharmony_ci	case 16:
628c2ecf20Sopenharmony_ci	case 15:
638c2ecf20Sopenharmony_ci	case 14:
648c2ecf20Sopenharmony_ci	case 13:
658c2ecf20Sopenharmony_ci	case 12:
668c2ecf20Sopenharmony_ci	case 8:
678c2ecf20Sopenharmony_ci	case 4:
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	default:
708c2ecf20Sopenharmony_ci		return -EINVAL;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
798c2ecf20Sopenharmony_ci	struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
808c2ecf20Sopenharmony_ci	struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
818c2ecf20Sopenharmony_ci	struct scatterlist *iv_sg = NULL;
828c2ecf20Sopenharmony_ci	unsigned int iv_len = 0;
838c2ecf20Sopenharmony_ci	int i;
848c2ecf20Sopenharmony_ci	int ret = 0;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (!ctx->u.aes.key_len)
878c2ecf20Sopenharmony_ci		return -EINVAL;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
908c2ecf20Sopenharmony_ci		return -EINVAL;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (!req->iv)
938c2ecf20Sopenharmony_ci		return -EINVAL;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/*
968c2ecf20Sopenharmony_ci	 * 5 parts:
978c2ecf20Sopenharmony_ci	 *   plaintext/ciphertext input
988c2ecf20Sopenharmony_ci	 *   AAD
998c2ecf20Sopenharmony_ci	 *   key
1008c2ecf20Sopenharmony_ci	 *   IV
1018c2ecf20Sopenharmony_ci	 *   Destination+tag buffer
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* Prepare the IV: 12 bytes + an integer (counter) */
1058c2ecf20Sopenharmony_ci	memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
1068c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++)
1078c2ecf20Sopenharmony_ci		rctx->iv[i + GCM_AES_IV_SIZE] = 0;
1088c2ecf20Sopenharmony_ci	rctx->iv[AES_BLOCK_SIZE - 1] = 1;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Set up a scatterlist for the IV */
1118c2ecf20Sopenharmony_ci	iv_sg = &rctx->iv_sg;
1128c2ecf20Sopenharmony_ci	iv_len = AES_BLOCK_SIZE;
1138c2ecf20Sopenharmony_ci	sg_init_one(iv_sg, rctx->iv, iv_len);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* The AAD + plaintext are concatenated in the src buffer */
1168c2ecf20Sopenharmony_ci	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
1178c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&rctx->cmd.entry);
1188c2ecf20Sopenharmony_ci	rctx->cmd.engine = CCP_ENGINE_AES;
1198c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.authsize = crypto_aead_authsize(tfm);
1208c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.type = ctx->u.aes.type;
1218c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.mode = ctx->u.aes.mode;
1228c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.action = encrypt;
1238c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
1248c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
1258c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.iv = iv_sg;
1268c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.iv_len = iv_len;
1278c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.src = req->src;
1288c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.src_len = req->cryptlen;
1298c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.aad_len = req->assoclen;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* The cipher text + the tag are in the dst buffer */
1328c2ecf20Sopenharmony_ci	rctx->cmd.u.aes.dst = req->dst;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return ret;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_encrypt(struct aead_request *req)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_decrypt(struct aead_request *req)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	ctx->complete = ccp_aes_gcm_complete;
1548c2ecf20Sopenharmony_ci	ctx->u.aes.key_len = 0;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return 0;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic struct aead_alg ccp_aes_gcm_defaults = {
1668c2ecf20Sopenharmony_ci	.setkey = ccp_aes_gcm_setkey,
1678c2ecf20Sopenharmony_ci	.setauthsize = ccp_aes_gcm_setauthsize,
1688c2ecf20Sopenharmony_ci	.encrypt = ccp_aes_gcm_encrypt,
1698c2ecf20Sopenharmony_ci	.decrypt = ccp_aes_gcm_decrypt,
1708c2ecf20Sopenharmony_ci	.init = ccp_aes_gcm_cra_init,
1718c2ecf20Sopenharmony_ci	.ivsize = GCM_AES_IV_SIZE,
1728c2ecf20Sopenharmony_ci	.maxauthsize = AES_BLOCK_SIZE,
1738c2ecf20Sopenharmony_ci	.base = {
1748c2ecf20Sopenharmony_ci		.cra_flags	= CRYPTO_ALG_ASYNC |
1758c2ecf20Sopenharmony_ci				  CRYPTO_ALG_ALLOCATES_MEMORY |
1768c2ecf20Sopenharmony_ci				  CRYPTO_ALG_KERN_DRIVER_ONLY |
1778c2ecf20Sopenharmony_ci				  CRYPTO_ALG_NEED_FALLBACK,
1788c2ecf20Sopenharmony_ci		.cra_blocksize	= AES_BLOCK_SIZE,
1798c2ecf20Sopenharmony_ci		.cra_ctxsize	= sizeof(struct ccp_ctx),
1808c2ecf20Sopenharmony_ci		.cra_priority	= CCP_CRA_PRIORITY,
1818c2ecf20Sopenharmony_ci		.cra_exit	= ccp_aes_gcm_cra_exit,
1828c2ecf20Sopenharmony_ci		.cra_module	= THIS_MODULE,
1838c2ecf20Sopenharmony_ci	},
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistruct ccp_aes_aead_def {
1878c2ecf20Sopenharmony_ci	enum ccp_aes_mode mode;
1888c2ecf20Sopenharmony_ci	unsigned int version;
1898c2ecf20Sopenharmony_ci	const char *name;
1908c2ecf20Sopenharmony_ci	const char *driver_name;
1918c2ecf20Sopenharmony_ci	unsigned int blocksize;
1928c2ecf20Sopenharmony_ci	unsigned int ivsize;
1938c2ecf20Sopenharmony_ci	struct aead_alg *alg_defaults;
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic struct ccp_aes_aead_def aes_aead_algs[] = {
1978c2ecf20Sopenharmony_ci	{
1988c2ecf20Sopenharmony_ci		.mode		= CCP_AES_MODE_GHASH,
1998c2ecf20Sopenharmony_ci		.version	= CCP_VERSION(5, 0),
2008c2ecf20Sopenharmony_ci		.name		= "gcm(aes)",
2018c2ecf20Sopenharmony_ci		.driver_name	= "gcm-aes-ccp",
2028c2ecf20Sopenharmony_ci		.blocksize	= 1,
2038c2ecf20Sopenharmony_ci		.ivsize		= AES_BLOCK_SIZE,
2048c2ecf20Sopenharmony_ci		.alg_defaults	= &ccp_aes_gcm_defaults,
2058c2ecf20Sopenharmony_ci	},
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int ccp_register_aes_aead(struct list_head *head,
2098c2ecf20Sopenharmony_ci				 const struct ccp_aes_aead_def *def)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	struct ccp_crypto_aead *ccp_aead;
2128c2ecf20Sopenharmony_ci	struct aead_alg *alg;
2138c2ecf20Sopenharmony_ci	int ret;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
2168c2ecf20Sopenharmony_ci	if (!ccp_aead)
2178c2ecf20Sopenharmony_ci		return -ENOMEM;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ccp_aead->entry);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	ccp_aead->mode = def->mode;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* Copy the defaults and override as necessary */
2248c2ecf20Sopenharmony_ci	alg = &ccp_aead->alg;
2258c2ecf20Sopenharmony_ci	*alg = *def->alg_defaults;
2268c2ecf20Sopenharmony_ci	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
2278c2ecf20Sopenharmony_ci	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
2288c2ecf20Sopenharmony_ci		 def->driver_name);
2298c2ecf20Sopenharmony_ci	alg->base.cra_blocksize = def->blocksize;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	ret = crypto_register_aead(alg);
2328c2ecf20Sopenharmony_ci	if (ret) {
2338c2ecf20Sopenharmony_ci		pr_err("%s aead algorithm registration error (%d)\n",
2348c2ecf20Sopenharmony_ci		       alg->base.cra_name, ret);
2358c2ecf20Sopenharmony_ci		kfree(ccp_aead);
2368c2ecf20Sopenharmony_ci		return ret;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	list_add(&ccp_aead->entry, head);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	return 0;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ciint ccp_register_aes_aeads(struct list_head *head)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	int i, ret;
2478c2ecf20Sopenharmony_ci	unsigned int ccpversion = ccp_version();
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
2508c2ecf20Sopenharmony_ci		if (aes_aead_algs[i].version > ccpversion)
2518c2ecf20Sopenharmony_ci			continue;
2528c2ecf20Sopenharmony_ci		ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
2538c2ecf20Sopenharmony_ci		if (ret)
2548c2ecf20Sopenharmony_ci			return ret;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci}
259