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