1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
236cf515bSGary R Hook /*
336cf515bSGary R Hook  * AMD Cryptographic Coprocessor (CCP) AES GCM crypto API support
436cf515bSGary R Hook  *
568cc652fSGary R Hook  * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
636cf515bSGary R Hook  *
736cf515bSGary R Hook  * Author: Gary R Hook <gary.hook@amd.com>
836cf515bSGary R Hook  */
936cf515bSGary R Hook 
1036cf515bSGary R Hook #include <linux/module.h>
1136cf515bSGary R Hook #include <linux/sched.h>
1236cf515bSGary R Hook #include <linux/delay.h>
1336cf515bSGary R Hook #include <linux/scatterlist.h>
1436cf515bSGary R Hook #include <linux/crypto.h>
1536cf515bSGary R Hook #include <crypto/internal/aead.h>
1636cf515bSGary R Hook #include <crypto/algapi.h>
1736cf515bSGary R Hook #include <crypto/aes.h>
1836cf515bSGary R Hook #include <crypto/ctr.h>
19cf0bd0aeSCorentin LABBE #include <crypto/gcm.h>
2036cf515bSGary R Hook #include <crypto/scatterwalk.h>
2136cf515bSGary R Hook 
2236cf515bSGary R Hook #include "ccp-crypto.h"
2336cf515bSGary R Hook 
2436cf515bSGary R Hook static int ccp_aes_gcm_complete(struct crypto_async_request *async_req, int ret)
2536cf515bSGary R Hook {
2636cf515bSGary R Hook 	return ret;
2736cf515bSGary R Hook }
2836cf515bSGary R Hook 
2936cf515bSGary R Hook static int ccp_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
3036cf515bSGary R Hook 			      unsigned int key_len)
3136cf515bSGary R Hook {
3236cf515bSGary R Hook 	struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
3336cf515bSGary R Hook 
3436cf515bSGary R Hook 	switch (key_len) {
3536cf515bSGary R Hook 	case AES_KEYSIZE_128:
3636cf515bSGary R Hook 		ctx->u.aes.type = CCP_AES_TYPE_128;
3736cf515bSGary R Hook 		break;
3836cf515bSGary R Hook 	case AES_KEYSIZE_192:
3936cf515bSGary R Hook 		ctx->u.aes.type = CCP_AES_TYPE_192;
4036cf515bSGary R Hook 		break;
4136cf515bSGary R Hook 	case AES_KEYSIZE_256:
4236cf515bSGary R Hook 		ctx->u.aes.type = CCP_AES_TYPE_256;
4336cf515bSGary R Hook 		break;
4436cf515bSGary R Hook 	default:
4536cf515bSGary R Hook 		crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
4636cf515bSGary R Hook 		return -EINVAL;
4736cf515bSGary R Hook 	}
4836cf515bSGary R Hook 
4936cf515bSGary R Hook 	ctx->u.aes.mode = CCP_AES_MODE_GCM;
5036cf515bSGary R Hook 	ctx->u.aes.key_len = key_len;
5136cf515bSGary R Hook 
5236cf515bSGary R Hook 	memcpy(ctx->u.aes.key, key, key_len);
5336cf515bSGary R Hook 	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
5436cf515bSGary R Hook 
5536cf515bSGary R Hook 	return 0;
5636cf515bSGary R Hook }
5736cf515bSGary R Hook 
5836cf515bSGary R Hook static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
5936cf515bSGary R Hook 				   unsigned int authsize)
6036cf515bSGary R Hook {
6136cf515bSGary R Hook 	return 0;
6236cf515bSGary R Hook }
6336cf515bSGary R Hook 
6436cf515bSGary R Hook static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
6536cf515bSGary R Hook {
6636cf515bSGary R Hook 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
6736cf515bSGary R Hook 	struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
6836cf515bSGary R Hook 	struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
6936cf515bSGary R Hook 	struct scatterlist *iv_sg = NULL;
7036cf515bSGary R Hook 	unsigned int iv_len = 0;
7136cf515bSGary R Hook 	int i;
7236cf515bSGary R Hook 	int ret = 0;
7336cf515bSGary R Hook 
7436cf515bSGary R Hook 	if (!ctx->u.aes.key_len)
7536cf515bSGary R Hook 		return -EINVAL;
7636cf515bSGary R Hook 
7736cf515bSGary R Hook 	if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
7836cf515bSGary R Hook 		return -EINVAL;
7936cf515bSGary R Hook 
8036cf515bSGary R Hook 	if (!req->iv)
8136cf515bSGary R Hook 		return -EINVAL;
8236cf515bSGary R Hook 
8336cf515bSGary R Hook 	/*
8436cf515bSGary R Hook 	 * 5 parts:
8536cf515bSGary R Hook 	 *   plaintext/ciphertext input
8636cf515bSGary R Hook 	 *   AAD
8736cf515bSGary R Hook 	 *   key
8836cf515bSGary R Hook 	 *   IV
8936cf515bSGary R Hook 	 *   Destination+tag buffer
9036cf515bSGary R Hook 	 */
9136cf515bSGary R Hook 
9236cf515bSGary R Hook 	/* Prepare the IV: 12 bytes + an integer (counter) */
93cf0bd0aeSCorentin LABBE 	memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
9436cf515bSGary R Hook 	for (i = 0; i < 3; i++)
95cf0bd0aeSCorentin LABBE 		rctx->iv[i + GCM_AES_IV_SIZE] = 0;
9636cf515bSGary R Hook 	rctx->iv[AES_BLOCK_SIZE - 1] = 1;
9736cf515bSGary R Hook 
9836cf515bSGary R Hook 	/* Set up a scatterlist for the IV */
9936cf515bSGary R Hook 	iv_sg = &rctx->iv_sg;
10036cf515bSGary R Hook 	iv_len = AES_BLOCK_SIZE;
10136cf515bSGary R Hook 	sg_init_one(iv_sg, rctx->iv, iv_len);
10236cf515bSGary R Hook 
10336cf515bSGary R Hook 	/* The AAD + plaintext are concatenated in the src buffer */
10436cf515bSGary R Hook 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
10536cf515bSGary R Hook 	INIT_LIST_HEAD(&rctx->cmd.entry);
10636cf515bSGary R Hook 	rctx->cmd.engine = CCP_ENGINE_AES;
10736cf515bSGary R Hook 	rctx->cmd.u.aes.type = ctx->u.aes.type;
10836cf515bSGary R Hook 	rctx->cmd.u.aes.mode = ctx->u.aes.mode;
10936cf515bSGary R Hook 	rctx->cmd.u.aes.action = encrypt;
11036cf515bSGary R Hook 	rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
11136cf515bSGary R Hook 	rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
11236cf515bSGary R Hook 	rctx->cmd.u.aes.iv = iv_sg;
11336cf515bSGary R Hook 	rctx->cmd.u.aes.iv_len = iv_len;
11436cf515bSGary R Hook 	rctx->cmd.u.aes.src = req->src;
11536cf515bSGary R Hook 	rctx->cmd.u.aes.src_len = req->cryptlen;
11636cf515bSGary R Hook 	rctx->cmd.u.aes.aad_len = req->assoclen;
11736cf515bSGary R Hook 
11836cf515bSGary R Hook 	/* The cipher text + the tag are in the dst buffer */
11936cf515bSGary R Hook 	rctx->cmd.u.aes.dst = req->dst;
12036cf515bSGary R Hook 
12136cf515bSGary R Hook 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
12236cf515bSGary R Hook 
12336cf515bSGary R Hook 	return ret;
12436cf515bSGary R Hook }
12536cf515bSGary R Hook 
12636cf515bSGary R Hook static int ccp_aes_gcm_encrypt(struct aead_request *req)
12736cf515bSGary R Hook {
12836cf515bSGary R Hook 	return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
12936cf515bSGary R Hook }
13036cf515bSGary R Hook 
13136cf515bSGary R Hook static int ccp_aes_gcm_decrypt(struct aead_request *req)
13236cf515bSGary R Hook {
13336cf515bSGary R Hook 	return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
13436cf515bSGary R Hook }
13536cf515bSGary R Hook 
13636cf515bSGary R Hook static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
13736cf515bSGary R Hook {
13836cf515bSGary R Hook 	struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
13936cf515bSGary R Hook 
14036cf515bSGary R Hook 	ctx->complete = ccp_aes_gcm_complete;
14136cf515bSGary R Hook 	ctx->u.aes.key_len = 0;
14236cf515bSGary R Hook 
14336cf515bSGary R Hook 	crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
14436cf515bSGary R Hook 
14536cf515bSGary R Hook 	return 0;
14636cf515bSGary R Hook }
14736cf515bSGary R Hook 
14836cf515bSGary R Hook static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
14936cf515bSGary R Hook {
15036cf515bSGary R Hook }
15136cf515bSGary R Hook 
15236cf515bSGary R Hook static struct aead_alg ccp_aes_gcm_defaults = {
15336cf515bSGary R Hook 	.setkey = ccp_aes_gcm_setkey,
15436cf515bSGary R Hook 	.setauthsize = ccp_aes_gcm_setauthsize,
15536cf515bSGary R Hook 	.encrypt = ccp_aes_gcm_encrypt,
15636cf515bSGary R Hook 	.decrypt = ccp_aes_gcm_decrypt,
15736cf515bSGary R Hook 	.init = ccp_aes_gcm_cra_init,
158cf0bd0aeSCorentin LABBE 	.ivsize = GCM_AES_IV_SIZE,
15936cf515bSGary R Hook 	.maxauthsize = AES_BLOCK_SIZE,
16036cf515bSGary R Hook 	.base = {
16136cf515bSGary R Hook 		.cra_flags	= CRYPTO_ALG_TYPE_ABLKCIPHER |
16236cf515bSGary R Hook 				  CRYPTO_ALG_ASYNC |
16336cf515bSGary R Hook 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
16436cf515bSGary R Hook 				  CRYPTO_ALG_NEED_FALLBACK,
16536cf515bSGary R Hook 		.cra_blocksize	= AES_BLOCK_SIZE,
16636cf515bSGary R Hook 		.cra_ctxsize	= sizeof(struct ccp_ctx),
16736cf515bSGary R Hook 		.cra_priority	= CCP_CRA_PRIORITY,
16836cf515bSGary R Hook 		.cra_type	= &crypto_ablkcipher_type,
16936cf515bSGary R Hook 		.cra_exit	= ccp_aes_gcm_cra_exit,
17036cf515bSGary R Hook 		.cra_module	= THIS_MODULE,
17136cf515bSGary R Hook 	},
17236cf515bSGary R Hook };
17336cf515bSGary R Hook 
17436cf515bSGary R Hook struct ccp_aes_aead_def {
17536cf515bSGary R Hook 	enum ccp_aes_mode mode;
17636cf515bSGary R Hook 	unsigned int version;
17736cf515bSGary R Hook 	const char *name;
17836cf515bSGary R Hook 	const char *driver_name;
17936cf515bSGary R Hook 	unsigned int blocksize;
18036cf515bSGary R Hook 	unsigned int ivsize;
18136cf515bSGary R Hook 	struct aead_alg *alg_defaults;
18236cf515bSGary R Hook };
18336cf515bSGary R Hook 
18436cf515bSGary R Hook static struct ccp_aes_aead_def aes_aead_algs[] = {
18536cf515bSGary R Hook 	{
18636cf515bSGary R Hook 		.mode		= CCP_AES_MODE_GHASH,
18736cf515bSGary R Hook 		.version	= CCP_VERSION(5, 0),
18836cf515bSGary R Hook 		.name		= "gcm(aes)",
18936cf515bSGary R Hook 		.driver_name	= "gcm-aes-ccp",
19036cf515bSGary R Hook 		.blocksize	= 1,
19136cf515bSGary R Hook 		.ivsize		= AES_BLOCK_SIZE,
19236cf515bSGary R Hook 		.alg_defaults	= &ccp_aes_gcm_defaults,
19336cf515bSGary R Hook 	},
19436cf515bSGary R Hook };
19536cf515bSGary R Hook 
19636cf515bSGary R Hook static int ccp_register_aes_aead(struct list_head *head,
19736cf515bSGary R Hook 				 const struct ccp_aes_aead_def *def)
19836cf515bSGary R Hook {
19936cf515bSGary R Hook 	struct ccp_crypto_aead *ccp_aead;
20036cf515bSGary R Hook 	struct aead_alg *alg;
20136cf515bSGary R Hook 	int ret;
20236cf515bSGary R Hook 
20336cf515bSGary R Hook 	ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
20436cf515bSGary R Hook 	if (!ccp_aead)
20536cf515bSGary R Hook 		return -ENOMEM;
20636cf515bSGary R Hook 
20736cf515bSGary R Hook 	INIT_LIST_HEAD(&ccp_aead->entry);
20836cf515bSGary R Hook 
20936cf515bSGary R Hook 	ccp_aead->mode = def->mode;
21036cf515bSGary R Hook 
21136cf515bSGary R Hook 	/* Copy the defaults and override as necessary */
21236cf515bSGary R Hook 	alg = &ccp_aead->alg;
21336cf515bSGary R Hook 	*alg = *def->alg_defaults;
21436cf515bSGary R Hook 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
21536cf515bSGary R Hook 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
21636cf515bSGary R Hook 		 def->driver_name);
21736cf515bSGary R Hook 	alg->base.cra_blocksize = def->blocksize;
21836cf515bSGary R Hook 	alg->base.cra_ablkcipher.ivsize = def->ivsize;
21936cf515bSGary R Hook 
22036cf515bSGary R Hook 	ret = crypto_register_aead(alg);
22136cf515bSGary R Hook 	if (ret) {
22236cf515bSGary R Hook 		pr_err("%s ablkcipher algorithm registration error (%d)\n",
22336cf515bSGary R Hook 		       alg->base.cra_name, ret);
22436cf515bSGary R Hook 		kfree(ccp_aead);
22536cf515bSGary R Hook 		return ret;
22636cf515bSGary R Hook 	}
22736cf515bSGary R Hook 
22836cf515bSGary R Hook 	list_add(&ccp_aead->entry, head);
22936cf515bSGary R Hook 
23036cf515bSGary R Hook 	return 0;
23136cf515bSGary R Hook }
23236cf515bSGary R Hook 
23336cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head)
23436cf515bSGary R Hook {
23536cf515bSGary R Hook 	int i, ret;
23636cf515bSGary R Hook 	unsigned int ccpversion = ccp_version();
23736cf515bSGary R Hook 
23836cf515bSGary R Hook 	for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
23936cf515bSGary R Hook 		if (aes_aead_algs[i].version > ccpversion)
24036cf515bSGary R Hook 			continue;
24136cf515bSGary R Hook 		ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
24236cf515bSGary R Hook 		if (ret)
24336cf515bSGary R Hook 			return ret;
24436cf515bSGary R Hook 	}
24536cf515bSGary R Hook 
24636cf515bSGary R Hook 	return 0;
24736cf515bSGary R Hook }
248