1 /*
2  * AMD Cryptographic Coprocessor (CCP) RSA crypto API support
3  *
4  * Copyright (C) 2017 Advanced Micro Devices, Inc.
5  *
6  * Author: Gary R Hook <gary.hook@amd.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/scatterlist.h>
16 #include <linux/crypto.h>
17 #include <crypto/algapi.h>
18 #include <crypto/internal/rsa.h>
19 #include <crypto/internal/akcipher.h>
20 #include <crypto/akcipher.h>
21 #include <crypto/scatterwalk.h>
22 
23 #include "ccp-crypto.h"
24 
25 static inline struct akcipher_request *akcipher_request_cast(
26 	struct crypto_async_request *req)
27 {
28 	return container_of(req, struct akcipher_request, base);
29 }
30 
31 static inline int ccp_copy_and_save_keypart(u8 **kpbuf, unsigned int *kplen,
32 					    const u8 *buf, size_t sz)
33 {
34 	int nskip;
35 
36 	for (nskip = 0; nskip < sz; nskip++)
37 		if (buf[nskip])
38 			break;
39 	*kplen = sz - nskip;
40 	*kpbuf = kmemdup(buf + nskip, *kplen, GFP_KERNEL);
41 	if (!*kpbuf)
42 		return -ENOMEM;
43 
44 	return 0;
45 }
46 
47 static int ccp_rsa_complete(struct crypto_async_request *async_req, int ret)
48 {
49 	struct akcipher_request *req = akcipher_request_cast(async_req);
50 	struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
51 
52 	if (ret)
53 		return ret;
54 
55 	req->dst_len = rctx->cmd.u.rsa.key_size >> 3;
56 
57 	return 0;
58 }
59 
60 static unsigned int ccp_rsa_maxsize(struct crypto_akcipher *tfm)
61 {
62 	struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
63 
64 	return ctx->u.rsa.n_len;
65 }
66 
67 static int ccp_rsa_crypt(struct akcipher_request *req, bool encrypt)
68 {
69 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
70 	struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
71 	struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
72 	int ret = 0;
73 
74 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
75 	INIT_LIST_HEAD(&rctx->cmd.entry);
76 	rctx->cmd.engine = CCP_ENGINE_RSA;
77 
78 	rctx->cmd.u.rsa.key_size = ctx->u.rsa.key_len; /* in bits */
79 	if (encrypt) {
80 		rctx->cmd.u.rsa.exp = &ctx->u.rsa.e_sg;
81 		rctx->cmd.u.rsa.exp_len = ctx->u.rsa.e_len;
82 	} else {
83 		rctx->cmd.u.rsa.exp = &ctx->u.rsa.d_sg;
84 		rctx->cmd.u.rsa.exp_len = ctx->u.rsa.d_len;
85 	}
86 	rctx->cmd.u.rsa.mod = &ctx->u.rsa.n_sg;
87 	rctx->cmd.u.rsa.mod_len = ctx->u.rsa.n_len;
88 	rctx->cmd.u.rsa.src = req->src;
89 	rctx->cmd.u.rsa.src_len = req->src_len;
90 	rctx->cmd.u.rsa.dst = req->dst;
91 
92 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
93 
94 	return ret;
95 }
96 
97 static int ccp_rsa_encrypt(struct akcipher_request *req)
98 {
99 	return ccp_rsa_crypt(req, true);
100 }
101 
102 static int ccp_rsa_decrypt(struct akcipher_request *req)
103 {
104 	return ccp_rsa_crypt(req, false);
105 }
106 
107 static int ccp_check_key_length(unsigned int len)
108 {
109 	/* In bits */
110 	if (len < 8 || len > 4096)
111 		return -EINVAL;
112 	return 0;
113 }
114 
115 static void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx)
116 {
117 	/* Clean up old key data */
118 	kzfree(ctx->u.rsa.e_buf);
119 	ctx->u.rsa.e_buf = NULL;
120 	ctx->u.rsa.e_len = 0;
121 	kzfree(ctx->u.rsa.n_buf);
122 	ctx->u.rsa.n_buf = NULL;
123 	ctx->u.rsa.n_len = 0;
124 	kzfree(ctx->u.rsa.d_buf);
125 	ctx->u.rsa.d_buf = NULL;
126 	ctx->u.rsa.d_len = 0;
127 }
128 
129 static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
130 			  unsigned int keylen, bool private)
131 {
132 	struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
133 	struct rsa_key raw_key;
134 	int ret;
135 
136 	ccp_rsa_free_key_bufs(ctx);
137 	memset(&raw_key, 0, sizeof(raw_key));
138 
139 	/* Code borrowed from crypto/rsa.c */
140 	if (private)
141 		ret = rsa_parse_priv_key(&raw_key, key, keylen);
142 	else
143 		ret = rsa_parse_pub_key(&raw_key, key, keylen);
144 	if (ret)
145 		goto n_key;
146 
147 	ret = ccp_copy_and_save_keypart(&ctx->u.rsa.n_buf, &ctx->u.rsa.n_len,
148 					raw_key.n, raw_key.n_sz);
149 	if (ret)
150 		goto key_err;
151 	sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len);
152 
153 	ctx->u.rsa.key_len = ctx->u.rsa.n_len << 3; /* convert to bits */
154 	if (ccp_check_key_length(ctx->u.rsa.key_len)) {
155 		ret = -EINVAL;
156 		goto key_err;
157 	}
158 
159 	ret = ccp_copy_and_save_keypart(&ctx->u.rsa.e_buf, &ctx->u.rsa.e_len,
160 					raw_key.e, raw_key.e_sz);
161 	if (ret)
162 		goto key_err;
163 	sg_init_one(&ctx->u.rsa.e_sg, ctx->u.rsa.e_buf, ctx->u.rsa.e_len);
164 
165 	if (private) {
166 		ret = ccp_copy_and_save_keypart(&ctx->u.rsa.d_buf,
167 						&ctx->u.rsa.d_len,
168 						raw_key.d, raw_key.d_sz);
169 		if (ret)
170 			goto key_err;
171 		sg_init_one(&ctx->u.rsa.d_sg,
172 			    ctx->u.rsa.d_buf, ctx->u.rsa.d_len);
173 	}
174 
175 	return 0;
176 
177 key_err:
178 	ccp_rsa_free_key_bufs(ctx);
179 
180 n_key:
181 	return ret;
182 }
183 
184 static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
185 			      unsigned int keylen)
186 {
187 	return ccp_rsa_setkey(tfm, key, keylen, true);
188 }
189 
190 static int ccp_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
191 			     unsigned int keylen)
192 {
193 	return ccp_rsa_setkey(tfm, key, keylen, false);
194 }
195 
196 static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm)
197 {
198 	struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
199 
200 	akcipher_set_reqsize(tfm, sizeof(struct ccp_rsa_req_ctx));
201 	ctx->complete = ccp_rsa_complete;
202 
203 	return 0;
204 }
205 
206 static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm)
207 {
208 	struct ccp_ctx *ctx = crypto_tfm_ctx(&tfm->base);
209 
210 	ccp_rsa_free_key_bufs(ctx);
211 }
212 
213 static struct akcipher_alg ccp_rsa_defaults = {
214 	.encrypt = ccp_rsa_encrypt,
215 	.decrypt = ccp_rsa_decrypt,
216 	.set_pub_key = ccp_rsa_setpubkey,
217 	.set_priv_key = ccp_rsa_setprivkey,
218 	.max_size = ccp_rsa_maxsize,
219 	.init = ccp_rsa_init_tfm,
220 	.exit = ccp_rsa_exit_tfm,
221 	.base = {
222 		.cra_name = "rsa",
223 		.cra_driver_name = "rsa-ccp",
224 		.cra_priority = CCP_CRA_PRIORITY,
225 		.cra_module = THIS_MODULE,
226 		.cra_ctxsize = 2 * sizeof(struct ccp_ctx),
227 	},
228 };
229 
230 struct ccp_rsa_def {
231 	unsigned int version;
232 	const char *name;
233 	const char *driver_name;
234 	unsigned int reqsize;
235 	struct akcipher_alg *alg_defaults;
236 };
237 
238 static struct ccp_rsa_def rsa_algs[] = {
239 	{
240 		.version	= CCP_VERSION(3, 0),
241 		.name		= "rsa",
242 		.driver_name	= "rsa-ccp",
243 		.reqsize	= sizeof(struct ccp_rsa_req_ctx),
244 		.alg_defaults	= &ccp_rsa_defaults,
245 	}
246 };
247 
248 static int ccp_register_rsa_alg(struct list_head *head,
249 			        const struct ccp_rsa_def *def)
250 {
251 	struct ccp_crypto_akcipher_alg *ccp_alg;
252 	struct akcipher_alg *alg;
253 	int ret;
254 
255 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
256 	if (!ccp_alg)
257 		return -ENOMEM;
258 
259 	INIT_LIST_HEAD(&ccp_alg->entry);
260 
261 	alg = &ccp_alg->alg;
262 	*alg = *def->alg_defaults;
263 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
264 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
265 		 def->driver_name);
266 	ret = crypto_register_akcipher(alg);
267 	if (ret) {
268 		pr_err("%s akcipher algorithm registration error (%d)\n",
269 		       alg->base.cra_name, ret);
270 		kfree(ccp_alg);
271 		return ret;
272 	}
273 
274 	list_add(&ccp_alg->entry, head);
275 
276 	return 0;
277 }
278 
279 int ccp_register_rsa_algs(struct list_head *head)
280 {
281 	int i, ret;
282 	unsigned int ccpversion = ccp_version();
283 
284 	/* Register the RSA algorithm in standard mode
285 	 * This works for CCP v3 and later
286 	 */
287 	for (i = 0; i < ARRAY_SIZE(rsa_algs); i++) {
288 		if (rsa_algs[i].version > ccpversion)
289 			continue;
290 		ret = ccp_register_rsa_alg(head, &rsa_algs[i]);
291 		if (ret)
292 			return ret;
293 	}
294 
295 	return 0;
296 }
297