1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2990672d4SGary R Hook /*
3990672d4SGary R Hook  * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support
4990672d4SGary R Hook  *
568cc652fSGary R Hook  * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
6990672d4SGary R Hook  *
7990672d4SGary R Hook  * Author: Gary R Hook <ghook@amd.com>
8990672d4SGary R Hook  */
9990672d4SGary R Hook 
10990672d4SGary R Hook #include <linux/module.h>
11990672d4SGary R Hook #include <linux/sched.h>
12990672d4SGary R Hook #include <linux/delay.h>
13990672d4SGary R Hook #include <linux/scatterlist.h>
14990672d4SGary R Hook #include <linux/crypto.h>
15990672d4SGary R Hook #include <crypto/algapi.h>
16990672d4SGary R Hook #include <crypto/scatterwalk.h>
17b5250416SArd Biesheuvel #include <crypto/internal/des.h>
18990672d4SGary R Hook 
19990672d4SGary R Hook #include "ccp-crypto.h"
20990672d4SGary R Hook 
ccp_des3_complete(struct crypto_async_request * async_req,int ret)21990672d4SGary R Hook static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
22990672d4SGary R Hook {
23be9fe620SArd Biesheuvel 	struct skcipher_request *req = skcipher_request_cast(async_req);
24*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(
25*99c6b20eSHerbert Xu 		crypto_skcipher_reqtfm(req));
26*99c6b20eSHerbert Xu 	struct ccp_des3_req_ctx *rctx = skcipher_request_ctx_dma(req);
27990672d4SGary R Hook 
28990672d4SGary R Hook 	if (ret)
29990672d4SGary R Hook 		return ret;
30990672d4SGary R Hook 
31990672d4SGary R Hook 	if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
32be9fe620SArd Biesheuvel 		memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE);
33990672d4SGary R Hook 
34990672d4SGary R Hook 	return 0;
35990672d4SGary R Hook }
36990672d4SGary R Hook 
ccp_des3_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int key_len)37be9fe620SArd Biesheuvel static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
38990672d4SGary R Hook 		unsigned int key_len)
39990672d4SGary R Hook {
40be9fe620SArd Biesheuvel 	struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
41*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
4276a329cdSHerbert Xu 	int err;
43990672d4SGary R Hook 
44be9fe620SArd Biesheuvel 	err = verify_skcipher_des3_key(tfm, key);
45b5250416SArd Biesheuvel 	if (err)
4676a329cdSHerbert Xu 		return err;
47990672d4SGary R Hook 
48990672d4SGary R Hook 	/* It's not clear that there is any support for a keysize of 112.
49990672d4SGary R Hook 	 * If needed, the caller should make K1 == K3
50990672d4SGary R Hook 	 */
51990672d4SGary R Hook 	ctx->u.des3.type = CCP_DES3_TYPE_168;
52990672d4SGary R Hook 	ctx->u.des3.mode = alg->mode;
53990672d4SGary R Hook 	ctx->u.des3.key_len = key_len;
54990672d4SGary R Hook 
55990672d4SGary R Hook 	memcpy(ctx->u.des3.key, key, key_len);
56990672d4SGary R Hook 	sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len);
57990672d4SGary R Hook 
58990672d4SGary R Hook 	return 0;
59990672d4SGary R Hook }
60990672d4SGary R Hook 
ccp_des3_crypt(struct skcipher_request * req,bool encrypt)61be9fe620SArd Biesheuvel static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt)
62990672d4SGary R Hook {
63be9fe620SArd Biesheuvel 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
64*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
65*99c6b20eSHerbert Xu 	struct ccp_des3_req_ctx *rctx = skcipher_request_ctx_dma(req);
66990672d4SGary R Hook 	struct scatterlist *iv_sg = NULL;
67990672d4SGary R Hook 	unsigned int iv_len = 0;
68990672d4SGary R Hook 
69990672d4SGary R Hook 	if (!ctx->u.des3.key_len)
70990672d4SGary R Hook 		return -EINVAL;
71990672d4SGary R Hook 
72990672d4SGary R Hook 	if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) ||
73990672d4SGary R Hook 	     (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) &&
74be9fe620SArd Biesheuvel 	    (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1)))
75990672d4SGary R Hook 		return -EINVAL;
76990672d4SGary R Hook 
77990672d4SGary R Hook 	if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
78be9fe620SArd Biesheuvel 		if (!req->iv)
79990672d4SGary R Hook 			return -EINVAL;
80990672d4SGary R Hook 
81be9fe620SArd Biesheuvel 		memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE);
82990672d4SGary R Hook 		iv_sg = &rctx->iv_sg;
83990672d4SGary R Hook 		iv_len = DES3_EDE_BLOCK_SIZE;
84990672d4SGary R Hook 		sg_init_one(iv_sg, rctx->iv, iv_len);
85990672d4SGary R Hook 	}
86990672d4SGary R Hook 
87990672d4SGary R Hook 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
88990672d4SGary R Hook 	INIT_LIST_HEAD(&rctx->cmd.entry);
89990672d4SGary R Hook 	rctx->cmd.engine = CCP_ENGINE_DES3;
90990672d4SGary R Hook 	rctx->cmd.u.des3.type = ctx->u.des3.type;
91990672d4SGary R Hook 	rctx->cmd.u.des3.mode = ctx->u.des3.mode;
92990672d4SGary R Hook 	rctx->cmd.u.des3.action = (encrypt)
93990672d4SGary R Hook 				  ? CCP_DES3_ACTION_ENCRYPT
94990672d4SGary R Hook 				  : CCP_DES3_ACTION_DECRYPT;
95990672d4SGary R Hook 	rctx->cmd.u.des3.key = &ctx->u.des3.key_sg;
96990672d4SGary R Hook 	rctx->cmd.u.des3.key_len = ctx->u.des3.key_len;
97990672d4SGary R Hook 	rctx->cmd.u.des3.iv = iv_sg;
98990672d4SGary R Hook 	rctx->cmd.u.des3.iv_len = iv_len;
99990672d4SGary R Hook 	rctx->cmd.u.des3.src = req->src;
100be9fe620SArd Biesheuvel 	rctx->cmd.u.des3.src_len = req->cryptlen;
101990672d4SGary R Hook 	rctx->cmd.u.des3.dst = req->dst;
102990672d4SGary R Hook 
1036a40fb0dSye xingchen 	return ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
104990672d4SGary R Hook }
105990672d4SGary R Hook 
ccp_des3_encrypt(struct skcipher_request * req)106be9fe620SArd Biesheuvel static int ccp_des3_encrypt(struct skcipher_request *req)
107990672d4SGary R Hook {
108990672d4SGary R Hook 	return ccp_des3_crypt(req, true);
109990672d4SGary R Hook }
110990672d4SGary R Hook 
ccp_des3_decrypt(struct skcipher_request * req)111be9fe620SArd Biesheuvel static int ccp_des3_decrypt(struct skcipher_request *req)
112990672d4SGary R Hook {
113990672d4SGary R Hook 	return ccp_des3_crypt(req, false);
114990672d4SGary R Hook }
115990672d4SGary R Hook 
ccp_des3_init_tfm(struct crypto_skcipher * tfm)116be9fe620SArd Biesheuvel static int ccp_des3_init_tfm(struct crypto_skcipher *tfm)
117990672d4SGary R Hook {
118*99c6b20eSHerbert Xu 	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
119990672d4SGary R Hook 
120990672d4SGary R Hook 	ctx->complete = ccp_des3_complete;
121990672d4SGary R Hook 	ctx->u.des3.key_len = 0;
122990672d4SGary R Hook 
123*99c6b20eSHerbert Xu 	crypto_skcipher_set_reqsize_dma(tfm, sizeof(struct ccp_des3_req_ctx));
124990672d4SGary R Hook 
125990672d4SGary R Hook 	return 0;
126990672d4SGary R Hook }
127990672d4SGary R Hook 
128be9fe620SArd Biesheuvel static const struct skcipher_alg ccp_des3_defaults = {
129990672d4SGary R Hook 	.setkey			= ccp_des3_setkey,
130990672d4SGary R Hook 	.encrypt		= ccp_des3_encrypt,
131990672d4SGary R Hook 	.decrypt		= ccp_des3_decrypt,
132990672d4SGary R Hook 	.min_keysize		= DES3_EDE_KEY_SIZE,
133990672d4SGary R Hook 	.max_keysize		= DES3_EDE_KEY_SIZE,
134be9fe620SArd Biesheuvel 	.init			= ccp_des3_init_tfm,
135be9fe620SArd Biesheuvel 
136be9fe620SArd Biesheuvel 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
137b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY |
138be9fe620SArd Biesheuvel 				  CRYPTO_ALG_KERN_DRIVER_ONLY |
139be9fe620SArd Biesheuvel 				  CRYPTO_ALG_NEED_FALLBACK,
140be9fe620SArd Biesheuvel 	.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
141*99c6b20eSHerbert Xu 	.base.cra_ctxsize	= sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
142be9fe620SArd Biesheuvel 	.base.cra_priority	= CCP_CRA_PRIORITY,
143be9fe620SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
144990672d4SGary R Hook };
145990672d4SGary R Hook 
146990672d4SGary R Hook struct ccp_des3_def {
147990672d4SGary R Hook 	enum ccp_des3_mode mode;
148990672d4SGary R Hook 	unsigned int version;
149990672d4SGary R Hook 	const char *name;
150990672d4SGary R Hook 	const char *driver_name;
151990672d4SGary R Hook 	unsigned int blocksize;
152990672d4SGary R Hook 	unsigned int ivsize;
153be9fe620SArd Biesheuvel 	const struct skcipher_alg *alg_defaults;
154990672d4SGary R Hook };
155990672d4SGary R Hook 
156be9fe620SArd Biesheuvel static const struct ccp_des3_def des3_algs[] = {
157990672d4SGary R Hook 	{
158990672d4SGary R Hook 		.mode		= CCP_DES3_MODE_ECB,
159990672d4SGary R Hook 		.version	= CCP_VERSION(5, 0),
160990672d4SGary R Hook 		.name		= "ecb(des3_ede)",
161990672d4SGary R Hook 		.driver_name	= "ecb-des3-ccp",
162990672d4SGary R Hook 		.blocksize	= DES3_EDE_BLOCK_SIZE,
163990672d4SGary R Hook 		.ivsize		= 0,
164990672d4SGary R Hook 		.alg_defaults	= &ccp_des3_defaults,
165990672d4SGary R Hook 	},
166990672d4SGary R Hook 	{
167990672d4SGary R Hook 		.mode		= CCP_DES3_MODE_CBC,
168990672d4SGary R Hook 		.version	= CCP_VERSION(5, 0),
169990672d4SGary R Hook 		.name		= "cbc(des3_ede)",
170990672d4SGary R Hook 		.driver_name	= "cbc-des3-ccp",
171990672d4SGary R Hook 		.blocksize	= DES3_EDE_BLOCK_SIZE,
172990672d4SGary R Hook 		.ivsize		= DES3_EDE_BLOCK_SIZE,
173990672d4SGary R Hook 		.alg_defaults	= &ccp_des3_defaults,
174990672d4SGary R Hook 	},
175990672d4SGary R Hook };
176990672d4SGary R Hook 
ccp_register_des3_alg(struct list_head * head,const struct ccp_des3_def * def)177990672d4SGary R Hook static int ccp_register_des3_alg(struct list_head *head,
178990672d4SGary R Hook 				 const struct ccp_des3_def *def)
179990672d4SGary R Hook {
180be9fe620SArd Biesheuvel 	struct ccp_crypto_skcipher_alg *ccp_alg;
181be9fe620SArd Biesheuvel 	struct skcipher_alg *alg;
182990672d4SGary R Hook 	int ret;
183990672d4SGary R Hook 
184990672d4SGary R Hook 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
185990672d4SGary R Hook 	if (!ccp_alg)
186990672d4SGary R Hook 		return -ENOMEM;
187990672d4SGary R Hook 
188990672d4SGary R Hook 	INIT_LIST_HEAD(&ccp_alg->entry);
189990672d4SGary R Hook 
190990672d4SGary R Hook 	ccp_alg->mode = def->mode;
191990672d4SGary R Hook 
192990672d4SGary R Hook 	/* Copy the defaults and override as necessary */
193990672d4SGary R Hook 	alg = &ccp_alg->alg;
194990672d4SGary R Hook 	*alg = *def->alg_defaults;
195be9fe620SArd Biesheuvel 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
196be9fe620SArd Biesheuvel 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
197990672d4SGary R Hook 			def->driver_name);
198be9fe620SArd Biesheuvel 	alg->base.cra_blocksize = def->blocksize;
199be9fe620SArd Biesheuvel 	alg->ivsize = def->ivsize;
200990672d4SGary R Hook 
201be9fe620SArd Biesheuvel 	ret = crypto_register_skcipher(alg);
202990672d4SGary R Hook 	if (ret) {
203be9fe620SArd Biesheuvel 		pr_err("%s skcipher algorithm registration error (%d)\n",
204be9fe620SArd Biesheuvel 				alg->base.cra_name, ret);
205990672d4SGary R Hook 		kfree(ccp_alg);
206990672d4SGary R Hook 		return ret;
207990672d4SGary R Hook 	}
208990672d4SGary R Hook 
209990672d4SGary R Hook 	list_add(&ccp_alg->entry, head);
210990672d4SGary R Hook 
211990672d4SGary R Hook 	return 0;
212990672d4SGary R Hook }
213990672d4SGary R Hook 
ccp_register_des3_algs(struct list_head * head)214990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head)
215990672d4SGary R Hook {
216990672d4SGary R Hook 	int i, ret;
217990672d4SGary R Hook 	unsigned int ccpversion = ccp_version();
218990672d4SGary R Hook 
219990672d4SGary R Hook 	for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
220990672d4SGary R Hook 		if (des3_algs[i].version > ccpversion)
221990672d4SGary R Hook 			continue;
222990672d4SGary R Hook 		ret = ccp_register_des3_alg(head, &des3_algs[i]);
223990672d4SGary R Hook 		if (ret)
224990672d4SGary R Hook 			return ret;
225990672d4SGary R Hook 	}
226990672d4SGary R Hook 
227990672d4SGary R Hook 	return 0;
228990672d4SGary R Hook }
229