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