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 
ccp_aes_gcm_complete(struct crypto_async_request * async_req,int ret)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 
ccp_aes_gcm_setkey(struct crypto_aead * tfm,const u8 * key,unsigned int key_len)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 {
32*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_aead_ctx_dma(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 		return -EINVAL;
4636cf515bSGary R Hook 	}
4736cf515bSGary R Hook 
4836cf515bSGary R Hook 	ctx->u.aes.mode = CCP_AES_MODE_GCM;
4936cf515bSGary R Hook 	ctx->u.aes.key_len = key_len;
5036cf515bSGary R Hook 
5136cf515bSGary R Hook 	memcpy(ctx->u.aes.key, key, key_len);
5236cf515bSGary R Hook 	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
5336cf515bSGary R Hook 
5436cf515bSGary R Hook 	return 0;
5536cf515bSGary R Hook }
5636cf515bSGary R Hook 
ccp_aes_gcm_setauthsize(struct crypto_aead * tfm,unsigned int authsize)5736cf515bSGary R Hook static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
5836cf515bSGary R Hook 				   unsigned int authsize)
5936cf515bSGary R Hook {
609f00baf7SGary R Hook 	switch (authsize) {
619f00baf7SGary R Hook 	case 16:
629f00baf7SGary R Hook 	case 15:
639f00baf7SGary R Hook 	case 14:
649f00baf7SGary R Hook 	case 13:
659f00baf7SGary R Hook 	case 12:
669f00baf7SGary R Hook 	case 8:
679f00baf7SGary R Hook 	case 4:
689f00baf7SGary R Hook 		break;
699f00baf7SGary R Hook 	default:
709f00baf7SGary R Hook 		return -EINVAL;
719f00baf7SGary R Hook 	}
729f00baf7SGary R Hook 
7336cf515bSGary R Hook 	return 0;
7436cf515bSGary R Hook }
7536cf515bSGary R Hook 
ccp_aes_gcm_crypt(struct aead_request * req,bool encrypt)7636cf515bSGary R Hook static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
7736cf515bSGary R Hook {
7836cf515bSGary R Hook 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
79*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_aead_ctx_dma(tfm);
80*99c6b20eSHerbert Xu 	struct ccp_aes_req_ctx *rctx = aead_request_ctx_dma(req);
8136cf515bSGary R Hook 	struct scatterlist *iv_sg = NULL;
8236cf515bSGary R Hook 	unsigned int iv_len = 0;
8336cf515bSGary R Hook 	int i;
8436cf515bSGary R Hook 	int ret = 0;
8536cf515bSGary R Hook 
8636cf515bSGary R Hook 	if (!ctx->u.aes.key_len)
8736cf515bSGary R Hook 		return -EINVAL;
8836cf515bSGary R Hook 
8936cf515bSGary R Hook 	if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
9036cf515bSGary R Hook 		return -EINVAL;
9136cf515bSGary R Hook 
9236cf515bSGary R Hook 	if (!req->iv)
9336cf515bSGary R Hook 		return -EINVAL;
9436cf515bSGary R Hook 
9536cf515bSGary R Hook 	/*
9636cf515bSGary R Hook 	 * 5 parts:
9736cf515bSGary R Hook 	 *   plaintext/ciphertext input
9836cf515bSGary R Hook 	 *   AAD
9936cf515bSGary R Hook 	 *   key
10036cf515bSGary R Hook 	 *   IV
10136cf515bSGary R Hook 	 *   Destination+tag buffer
10236cf515bSGary R Hook 	 */
10336cf515bSGary R Hook 
10436cf515bSGary R Hook 	/* Prepare the IV: 12 bytes + an integer (counter) */
105cf0bd0aeSCorentin LABBE 	memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
10636cf515bSGary R Hook 	for (i = 0; i < 3; i++)
107cf0bd0aeSCorentin LABBE 		rctx->iv[i + GCM_AES_IV_SIZE] = 0;
10836cf515bSGary R Hook 	rctx->iv[AES_BLOCK_SIZE - 1] = 1;
10936cf515bSGary R Hook 
11036cf515bSGary R Hook 	/* Set up a scatterlist for the IV */
11136cf515bSGary R Hook 	iv_sg = &rctx->iv_sg;
11236cf515bSGary R Hook 	iv_len = AES_BLOCK_SIZE;
11336cf515bSGary R Hook 	sg_init_one(iv_sg, rctx->iv, iv_len);
11436cf515bSGary R Hook 
11536cf515bSGary R Hook 	/* The AAD + plaintext are concatenated in the src buffer */
11636cf515bSGary R Hook 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
11736cf515bSGary R Hook 	INIT_LIST_HEAD(&rctx->cmd.entry);
11836cf515bSGary R Hook 	rctx->cmd.engine = CCP_ENGINE_AES;
1199f00baf7SGary R Hook 	rctx->cmd.u.aes.authsize = crypto_aead_authsize(tfm);
12036cf515bSGary R Hook 	rctx->cmd.u.aes.type = ctx->u.aes.type;
12136cf515bSGary R Hook 	rctx->cmd.u.aes.mode = ctx->u.aes.mode;
12236cf515bSGary R Hook 	rctx->cmd.u.aes.action = encrypt;
12336cf515bSGary R Hook 	rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
12436cf515bSGary R Hook 	rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
12536cf515bSGary R Hook 	rctx->cmd.u.aes.iv = iv_sg;
12636cf515bSGary R Hook 	rctx->cmd.u.aes.iv_len = iv_len;
12736cf515bSGary R Hook 	rctx->cmd.u.aes.src = req->src;
12836cf515bSGary R Hook 	rctx->cmd.u.aes.src_len = req->cryptlen;
12936cf515bSGary R Hook 	rctx->cmd.u.aes.aad_len = req->assoclen;
13036cf515bSGary R Hook 
13136cf515bSGary R Hook 	/* The cipher text + the tag are in the dst buffer */
13236cf515bSGary R Hook 	rctx->cmd.u.aes.dst = req->dst;
13336cf515bSGary R Hook 
13436cf515bSGary R Hook 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
13536cf515bSGary R Hook 
13636cf515bSGary R Hook 	return ret;
13736cf515bSGary R Hook }
13836cf515bSGary R Hook 
ccp_aes_gcm_encrypt(struct aead_request * req)13936cf515bSGary R Hook static int ccp_aes_gcm_encrypt(struct aead_request *req)
14036cf515bSGary R Hook {
14136cf515bSGary R Hook 	return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
14236cf515bSGary R Hook }
14336cf515bSGary R Hook 
ccp_aes_gcm_decrypt(struct aead_request * req)14436cf515bSGary R Hook static int ccp_aes_gcm_decrypt(struct aead_request *req)
14536cf515bSGary R Hook {
14636cf515bSGary R Hook 	return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
14736cf515bSGary R Hook }
14836cf515bSGary R Hook 
ccp_aes_gcm_cra_init(struct crypto_aead * tfm)14936cf515bSGary R Hook static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
15036cf515bSGary R Hook {
151*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_aead_ctx_dma(tfm);
15236cf515bSGary R Hook 
15336cf515bSGary R Hook 	ctx->complete = ccp_aes_gcm_complete;
15436cf515bSGary R Hook 	ctx->u.aes.key_len = 0;
15536cf515bSGary R Hook 
156*99c6b20eSHerbert Xu 	crypto_aead_set_reqsize_dma(tfm, sizeof(struct ccp_aes_req_ctx));
15736cf515bSGary R Hook 
15836cf515bSGary R Hook 	return 0;
15936cf515bSGary R Hook }
16036cf515bSGary R Hook 
ccp_aes_gcm_cra_exit(struct crypto_tfm * tfm)16136cf515bSGary R Hook static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
16236cf515bSGary R Hook {
16336cf515bSGary R Hook }
16436cf515bSGary R Hook 
16536cf515bSGary R Hook static struct aead_alg ccp_aes_gcm_defaults = {
16636cf515bSGary R Hook 	.setkey = ccp_aes_gcm_setkey,
16736cf515bSGary R Hook 	.setauthsize = ccp_aes_gcm_setauthsize,
16836cf515bSGary R Hook 	.encrypt = ccp_aes_gcm_encrypt,
16936cf515bSGary R Hook 	.decrypt = ccp_aes_gcm_decrypt,
17036cf515bSGary R Hook 	.init = ccp_aes_gcm_cra_init,
171cf0bd0aeSCorentin LABBE 	.ivsize = GCM_AES_IV_SIZE,
17236cf515bSGary R Hook 	.maxauthsize = AES_BLOCK_SIZE,
17336cf515bSGary R Hook 	.base = {
174be9fe620SArd Biesheuvel 		.cra_flags	= CRYPTO_ALG_ASYNC |
175b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY |
17636cf515bSGary R Hook 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
17736cf515bSGary R Hook 				  CRYPTO_ALG_NEED_FALLBACK,
17836cf515bSGary R Hook 		.cra_blocksize	= AES_BLOCK_SIZE,
179*99c6b20eSHerbert Xu 		.cra_ctxsize	= sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
18036cf515bSGary R Hook 		.cra_priority	= CCP_CRA_PRIORITY,
18136cf515bSGary R Hook 		.cra_exit	= ccp_aes_gcm_cra_exit,
18236cf515bSGary R Hook 		.cra_module	= THIS_MODULE,
18336cf515bSGary R Hook 	},
18436cf515bSGary R Hook };
18536cf515bSGary R Hook 
18636cf515bSGary R Hook struct ccp_aes_aead_def {
18736cf515bSGary R Hook 	enum ccp_aes_mode mode;
18836cf515bSGary R Hook 	unsigned int version;
18936cf515bSGary R Hook 	const char *name;
19036cf515bSGary R Hook 	const char *driver_name;
19136cf515bSGary R Hook 	unsigned int blocksize;
19236cf515bSGary R Hook 	unsigned int ivsize;
19336cf515bSGary R Hook 	struct aead_alg *alg_defaults;
19436cf515bSGary R Hook };
19536cf515bSGary R Hook 
19636cf515bSGary R Hook static struct ccp_aes_aead_def aes_aead_algs[] = {
19736cf515bSGary R Hook 	{
19836cf515bSGary R Hook 		.mode		= CCP_AES_MODE_GHASH,
19936cf515bSGary R Hook 		.version	= CCP_VERSION(5, 0),
20036cf515bSGary R Hook 		.name		= "gcm(aes)",
20136cf515bSGary R Hook 		.driver_name	= "gcm-aes-ccp",
20236cf515bSGary R Hook 		.blocksize	= 1,
20336cf515bSGary R Hook 		.ivsize		= AES_BLOCK_SIZE,
20436cf515bSGary R Hook 		.alg_defaults	= &ccp_aes_gcm_defaults,
20536cf515bSGary R Hook 	},
20636cf515bSGary R Hook };
20736cf515bSGary R Hook 
ccp_register_aes_aead(struct list_head * head,const struct ccp_aes_aead_def * def)20836cf515bSGary R Hook static int ccp_register_aes_aead(struct list_head *head,
20936cf515bSGary R Hook 				 const struct ccp_aes_aead_def *def)
21036cf515bSGary R Hook {
21136cf515bSGary R Hook 	struct ccp_crypto_aead *ccp_aead;
21236cf515bSGary R Hook 	struct aead_alg *alg;
21336cf515bSGary R Hook 	int ret;
21436cf515bSGary R Hook 
21536cf515bSGary R Hook 	ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
21636cf515bSGary R Hook 	if (!ccp_aead)
21736cf515bSGary R Hook 		return -ENOMEM;
21836cf515bSGary R Hook 
21936cf515bSGary R Hook 	INIT_LIST_HEAD(&ccp_aead->entry);
22036cf515bSGary R Hook 
22136cf515bSGary R Hook 	ccp_aead->mode = def->mode;
22236cf515bSGary R Hook 
22336cf515bSGary R Hook 	/* Copy the defaults and override as necessary */
22436cf515bSGary R Hook 	alg = &ccp_aead->alg;
22536cf515bSGary R Hook 	*alg = *def->alg_defaults;
22636cf515bSGary R Hook 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
22736cf515bSGary R Hook 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
22836cf515bSGary R Hook 		 def->driver_name);
22936cf515bSGary R Hook 	alg->base.cra_blocksize = def->blocksize;
23036cf515bSGary R Hook 
23136cf515bSGary R Hook 	ret = crypto_register_aead(alg);
23236cf515bSGary R Hook 	if (ret) {
233be9fe620SArd Biesheuvel 		pr_err("%s aead algorithm registration error (%d)\n",
23436cf515bSGary R Hook 		       alg->base.cra_name, ret);
23536cf515bSGary R Hook 		kfree(ccp_aead);
23636cf515bSGary R Hook 		return ret;
23736cf515bSGary R Hook 	}
23836cf515bSGary R Hook 
23936cf515bSGary R Hook 	list_add(&ccp_aead->entry, head);
24036cf515bSGary R Hook 
24136cf515bSGary R Hook 	return 0;
24236cf515bSGary R Hook }
24336cf515bSGary R Hook 
ccp_register_aes_aeads(struct list_head * head)24436cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head)
24536cf515bSGary R Hook {
24636cf515bSGary R Hook 	int i, ret;
24736cf515bSGary R Hook 	unsigned int ccpversion = ccp_version();
24836cf515bSGary R Hook 
24936cf515bSGary R Hook 	for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
25036cf515bSGary R Hook 		if (aes_aead_algs[i].version > ccpversion)
25136cf515bSGary R Hook 			continue;
25236cf515bSGary R Hook 		ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
25336cf515bSGary R Hook 		if (ret)
25436cf515bSGary R Hook 			return ret;
25536cf515bSGary R Hook 	}
25636cf515bSGary R Hook 
25736cf515bSGary R Hook 	return 0;
25836cf515bSGary R Hook }
259