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