1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Cryptographic Coprocessor (CCP) AES crypto API support
4  *
5  * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6  *
7  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/delay.h>
13 #include <linux/scatterlist.h>
14 #include <linux/crypto.h>
15 #include <crypto/algapi.h>
16 #include <crypto/aes.h>
17 #include <crypto/ctr.h>
18 #include <crypto/scatterwalk.h>
19 
20 #include "ccp-crypto.h"
21 
22 static int ccp_aes_complete(struct crypto_async_request *async_req, int ret)
23 {
24 	struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
25 	struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
26 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
27 
28 	if (ret)
29 		return ret;
30 
31 	if (ctx->u.aes.mode != CCP_AES_MODE_ECB)
32 		memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
33 
34 	return 0;
35 }
36 
37 static int ccp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
38 			  unsigned int key_len)
39 {
40 	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
41 	struct ccp_crypto_ablkcipher_alg *alg =
42 		ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm));
43 
44 	switch (key_len) {
45 	case AES_KEYSIZE_128:
46 		ctx->u.aes.type = CCP_AES_TYPE_128;
47 		break;
48 	case AES_KEYSIZE_192:
49 		ctx->u.aes.type = CCP_AES_TYPE_192;
50 		break;
51 	case AES_KEYSIZE_256:
52 		ctx->u.aes.type = CCP_AES_TYPE_256;
53 		break;
54 	default:
55 		crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
56 		return -EINVAL;
57 	}
58 	ctx->u.aes.mode = alg->mode;
59 	ctx->u.aes.key_len = key_len;
60 
61 	memcpy(ctx->u.aes.key, key, key_len);
62 	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
63 
64 	return 0;
65 }
66 
67 static int ccp_aes_crypt(struct ablkcipher_request *req, bool encrypt)
68 {
69 	struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
70 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
71 	struct scatterlist *iv_sg = NULL;
72 	unsigned int iv_len = 0;
73 	int ret;
74 
75 	if (!ctx->u.aes.key_len)
76 		return -EINVAL;
77 
78 	if (((ctx->u.aes.mode == CCP_AES_MODE_ECB) ||
79 	     (ctx->u.aes.mode == CCP_AES_MODE_CBC) ||
80 	     (ctx->u.aes.mode == CCP_AES_MODE_CFB)) &&
81 	    (req->nbytes & (AES_BLOCK_SIZE - 1)))
82 		return -EINVAL;
83 
84 	if (ctx->u.aes.mode != CCP_AES_MODE_ECB) {
85 		if (!req->info)
86 			return -EINVAL;
87 
88 		memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
89 		iv_sg = &rctx->iv_sg;
90 		iv_len = AES_BLOCK_SIZE;
91 		sg_init_one(iv_sg, rctx->iv, iv_len);
92 	}
93 
94 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
95 	INIT_LIST_HEAD(&rctx->cmd.entry);
96 	rctx->cmd.engine = CCP_ENGINE_AES;
97 	rctx->cmd.u.aes.type = ctx->u.aes.type;
98 	rctx->cmd.u.aes.mode = ctx->u.aes.mode;
99 	rctx->cmd.u.aes.action =
100 		(encrypt) ? CCP_AES_ACTION_ENCRYPT : CCP_AES_ACTION_DECRYPT;
101 	rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
102 	rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
103 	rctx->cmd.u.aes.iv = iv_sg;
104 	rctx->cmd.u.aes.iv_len = iv_len;
105 	rctx->cmd.u.aes.src = req->src;
106 	rctx->cmd.u.aes.src_len = req->nbytes;
107 	rctx->cmd.u.aes.dst = req->dst;
108 
109 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
110 
111 	return ret;
112 }
113 
114 static int ccp_aes_encrypt(struct ablkcipher_request *req)
115 {
116 	return ccp_aes_crypt(req, true);
117 }
118 
119 static int ccp_aes_decrypt(struct ablkcipher_request *req)
120 {
121 	return ccp_aes_crypt(req, false);
122 }
123 
124 static int ccp_aes_cra_init(struct crypto_tfm *tfm)
125 {
126 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
127 
128 	ctx->complete = ccp_aes_complete;
129 	ctx->u.aes.key_len = 0;
130 
131 	tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
132 
133 	return 0;
134 }
135 
136 static void ccp_aes_cra_exit(struct crypto_tfm *tfm)
137 {
138 }
139 
140 static int ccp_aes_rfc3686_complete(struct crypto_async_request *async_req,
141 				    int ret)
142 {
143 	struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
144 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
145 
146 	/* Restore the original pointer */
147 	req->info = rctx->rfc3686_info;
148 
149 	return ccp_aes_complete(async_req, ret);
150 }
151 
152 static int ccp_aes_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
153 				  unsigned int key_len)
154 {
155 	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
156 
157 	if (key_len < CTR_RFC3686_NONCE_SIZE)
158 		return -EINVAL;
159 
160 	key_len -= CTR_RFC3686_NONCE_SIZE;
161 	memcpy(ctx->u.aes.nonce, key + key_len, CTR_RFC3686_NONCE_SIZE);
162 
163 	return ccp_aes_setkey(tfm, key, key_len);
164 }
165 
166 static int ccp_aes_rfc3686_crypt(struct ablkcipher_request *req, bool encrypt)
167 {
168 	struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
169 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
170 	u8 *iv;
171 
172 	/* Initialize the CTR block */
173 	iv = rctx->rfc3686_iv;
174 	memcpy(iv, ctx->u.aes.nonce, CTR_RFC3686_NONCE_SIZE);
175 
176 	iv += CTR_RFC3686_NONCE_SIZE;
177 	memcpy(iv, req->info, CTR_RFC3686_IV_SIZE);
178 
179 	iv += CTR_RFC3686_IV_SIZE;
180 	*(__be32 *)iv = cpu_to_be32(1);
181 
182 	/* Point to the new IV */
183 	rctx->rfc3686_info = req->info;
184 	req->info = rctx->rfc3686_iv;
185 
186 	return ccp_aes_crypt(req, encrypt);
187 }
188 
189 static int ccp_aes_rfc3686_encrypt(struct ablkcipher_request *req)
190 {
191 	return ccp_aes_rfc3686_crypt(req, true);
192 }
193 
194 static int ccp_aes_rfc3686_decrypt(struct ablkcipher_request *req)
195 {
196 	return ccp_aes_rfc3686_crypt(req, false);
197 }
198 
199 static int ccp_aes_rfc3686_cra_init(struct crypto_tfm *tfm)
200 {
201 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
202 
203 	ctx->complete = ccp_aes_rfc3686_complete;
204 	ctx->u.aes.key_len = 0;
205 
206 	tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
207 
208 	return 0;
209 }
210 
211 static void ccp_aes_rfc3686_cra_exit(struct crypto_tfm *tfm)
212 {
213 }
214 
215 static struct crypto_alg ccp_aes_defaults = {
216 	.cra_flags	= CRYPTO_ALG_TYPE_ABLKCIPHER |
217 			  CRYPTO_ALG_ASYNC |
218 			  CRYPTO_ALG_KERN_DRIVER_ONLY |
219 			  CRYPTO_ALG_NEED_FALLBACK,
220 	.cra_blocksize	= AES_BLOCK_SIZE,
221 	.cra_ctxsize	= sizeof(struct ccp_ctx),
222 	.cra_priority	= CCP_CRA_PRIORITY,
223 	.cra_type	= &crypto_ablkcipher_type,
224 	.cra_init	= ccp_aes_cra_init,
225 	.cra_exit	= ccp_aes_cra_exit,
226 	.cra_module	= THIS_MODULE,
227 	.cra_ablkcipher	= {
228 		.setkey		= ccp_aes_setkey,
229 		.encrypt	= ccp_aes_encrypt,
230 		.decrypt	= ccp_aes_decrypt,
231 		.min_keysize	= AES_MIN_KEY_SIZE,
232 		.max_keysize	= AES_MAX_KEY_SIZE,
233 	},
234 };
235 
236 static struct crypto_alg ccp_aes_rfc3686_defaults = {
237 	.cra_flags	= CRYPTO_ALG_TYPE_ABLKCIPHER |
238 			   CRYPTO_ALG_ASYNC |
239 			   CRYPTO_ALG_KERN_DRIVER_ONLY |
240 			   CRYPTO_ALG_NEED_FALLBACK,
241 	.cra_blocksize	= CTR_RFC3686_BLOCK_SIZE,
242 	.cra_ctxsize	= sizeof(struct ccp_ctx),
243 	.cra_priority	= CCP_CRA_PRIORITY,
244 	.cra_type	= &crypto_ablkcipher_type,
245 	.cra_init	= ccp_aes_rfc3686_cra_init,
246 	.cra_exit	= ccp_aes_rfc3686_cra_exit,
247 	.cra_module	= THIS_MODULE,
248 	.cra_ablkcipher	= {
249 		.setkey		= ccp_aes_rfc3686_setkey,
250 		.encrypt	= ccp_aes_rfc3686_encrypt,
251 		.decrypt	= ccp_aes_rfc3686_decrypt,
252 		.min_keysize	= AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
253 		.max_keysize	= AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
254 	},
255 };
256 
257 struct ccp_aes_def {
258 	enum ccp_aes_mode mode;
259 	unsigned int version;
260 	const char *name;
261 	const char *driver_name;
262 	unsigned int blocksize;
263 	unsigned int ivsize;
264 	struct crypto_alg *alg_defaults;
265 };
266 
267 static struct ccp_aes_def aes_algs[] = {
268 	{
269 		.mode		= CCP_AES_MODE_ECB,
270 		.version	= CCP_VERSION(3, 0),
271 		.name		= "ecb(aes)",
272 		.driver_name	= "ecb-aes-ccp",
273 		.blocksize	= AES_BLOCK_SIZE,
274 		.ivsize		= 0,
275 		.alg_defaults	= &ccp_aes_defaults,
276 	},
277 	{
278 		.mode		= CCP_AES_MODE_CBC,
279 		.version	= CCP_VERSION(3, 0),
280 		.name		= "cbc(aes)",
281 		.driver_name	= "cbc-aes-ccp",
282 		.blocksize	= AES_BLOCK_SIZE,
283 		.ivsize		= AES_BLOCK_SIZE,
284 		.alg_defaults	= &ccp_aes_defaults,
285 	},
286 	{
287 		.mode		= CCP_AES_MODE_CFB,
288 		.version	= CCP_VERSION(3, 0),
289 		.name		= "cfb(aes)",
290 		.driver_name	= "cfb-aes-ccp",
291 		.blocksize	= AES_BLOCK_SIZE,
292 		.ivsize		= AES_BLOCK_SIZE,
293 		.alg_defaults	= &ccp_aes_defaults,
294 	},
295 	{
296 		.mode		= CCP_AES_MODE_OFB,
297 		.version	= CCP_VERSION(3, 0),
298 		.name		= "ofb(aes)",
299 		.driver_name	= "ofb-aes-ccp",
300 		.blocksize	= 1,
301 		.ivsize		= AES_BLOCK_SIZE,
302 		.alg_defaults	= &ccp_aes_defaults,
303 	},
304 	{
305 		.mode		= CCP_AES_MODE_CTR,
306 		.version	= CCP_VERSION(3, 0),
307 		.name		= "ctr(aes)",
308 		.driver_name	= "ctr-aes-ccp",
309 		.blocksize	= 1,
310 		.ivsize		= AES_BLOCK_SIZE,
311 		.alg_defaults	= &ccp_aes_defaults,
312 	},
313 	{
314 		.mode		= CCP_AES_MODE_CTR,
315 		.version	= CCP_VERSION(3, 0),
316 		.name		= "rfc3686(ctr(aes))",
317 		.driver_name	= "rfc3686-ctr-aes-ccp",
318 		.blocksize	= 1,
319 		.ivsize		= CTR_RFC3686_IV_SIZE,
320 		.alg_defaults	= &ccp_aes_rfc3686_defaults,
321 	},
322 };
323 
324 static int ccp_register_aes_alg(struct list_head *head,
325 				const struct ccp_aes_def *def)
326 {
327 	struct ccp_crypto_ablkcipher_alg *ccp_alg;
328 	struct crypto_alg *alg;
329 	int ret;
330 
331 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
332 	if (!ccp_alg)
333 		return -ENOMEM;
334 
335 	INIT_LIST_HEAD(&ccp_alg->entry);
336 
337 	ccp_alg->mode = def->mode;
338 
339 	/* Copy the defaults and override as necessary */
340 	alg = &ccp_alg->alg;
341 	*alg = *def->alg_defaults;
342 	snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
343 	snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
344 		 def->driver_name);
345 	alg->cra_blocksize = def->blocksize;
346 	alg->cra_ablkcipher.ivsize = def->ivsize;
347 
348 	ret = crypto_register_alg(alg);
349 	if (ret) {
350 		pr_err("%s ablkcipher algorithm registration error (%d)\n",
351 		       alg->cra_name, ret);
352 		kfree(ccp_alg);
353 		return ret;
354 	}
355 
356 	list_add(&ccp_alg->entry, head);
357 
358 	return 0;
359 }
360 
361 int ccp_register_aes_algs(struct list_head *head)
362 {
363 	int i, ret;
364 	unsigned int ccpversion = ccp_version();
365 
366 	for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
367 		if (aes_algs[i].version > ccpversion)
368 			continue;
369 		ret = ccp_register_aes_alg(head, &aes_algs[i]);
370 		if (ret)
371 			return ret;
372 	}
373 
374 	return 0;
375 }
376