1 /*
2  * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
3  *
4  * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
5  *
6  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7  * Author: Gary R Hook <gary.hook@amd.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/scatterlist.h>
18 #include <linux/crypto.h>
19 #include <crypto/algapi.h>
20 #include <crypto/hash.h>
21 #include <crypto/hmac.h>
22 #include <crypto/internal/hash.h>
23 #include <crypto/sha.h>
24 #include <crypto/scatterwalk.h>
25 
26 #include "ccp-crypto.h"
27 
28 static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
29 {
30 	struct ahash_request *req = ahash_request_cast(async_req);
31 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
32 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
33 	unsigned int digest_size = crypto_ahash_digestsize(tfm);
34 
35 	if (ret)
36 		goto e_free;
37 
38 	if (rctx->hash_rem) {
39 		/* Save remaining data to buffer */
40 		unsigned int offset = rctx->nbytes - rctx->hash_rem;
41 
42 		scatterwalk_map_and_copy(rctx->buf, rctx->src,
43 					 offset, rctx->hash_rem, 0);
44 		rctx->buf_count = rctx->hash_rem;
45 	} else {
46 		rctx->buf_count = 0;
47 	}
48 
49 	/* Update result area if supplied */
50 	if (req->result && rctx->final)
51 		memcpy(req->result, rctx->ctx, digest_size);
52 
53 e_free:
54 	sg_free_table(&rctx->data_sg);
55 
56 	return ret;
57 }
58 
59 static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
60 			     unsigned int final)
61 {
62 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
63 	struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
64 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
65 	struct scatterlist *sg;
66 	unsigned int block_size =
67 		crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
68 	unsigned int sg_count;
69 	gfp_t gfp;
70 	u64 len;
71 	int ret;
72 
73 	len = (u64)rctx->buf_count + (u64)nbytes;
74 
75 	if (!final && (len <= block_size)) {
76 		scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
77 					 0, nbytes, 0);
78 		rctx->buf_count += nbytes;
79 
80 		return 0;
81 	}
82 
83 	rctx->src = req->src;
84 	rctx->nbytes = nbytes;
85 
86 	rctx->final = final;
87 	rctx->hash_rem = final ? 0 : len & (block_size - 1);
88 	rctx->hash_cnt = len - rctx->hash_rem;
89 	if (!final && !rctx->hash_rem) {
90 		/* CCP can't do zero length final, so keep some data around */
91 		rctx->hash_cnt -= block_size;
92 		rctx->hash_rem = block_size;
93 	}
94 
95 	/* Initialize the context scatterlist */
96 	sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
97 
98 	sg = NULL;
99 	if (rctx->buf_count && nbytes) {
100 		/* Build the data scatterlist table - allocate enough entries
101 		 * for both data pieces (buffer and input data)
102 		 */
103 		gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
104 			GFP_KERNEL : GFP_ATOMIC;
105 		sg_count = sg_nents(req->src) + 1;
106 		ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
107 		if (ret)
108 			return ret;
109 
110 		sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
111 		sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
112 		if (!sg) {
113 			ret = -EINVAL;
114 			goto e_free;
115 		}
116 		sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
117 		if (!sg) {
118 			ret = -EINVAL;
119 			goto e_free;
120 		}
121 		sg_mark_end(sg);
122 
123 		sg = rctx->data_sg.sgl;
124 	} else if (rctx->buf_count) {
125 		sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
126 
127 		sg = &rctx->buf_sg;
128 	} else if (nbytes) {
129 		sg = req->src;
130 	}
131 
132 	rctx->msg_bits += (rctx->hash_cnt << 3);	/* Total in bits */
133 
134 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
135 	INIT_LIST_HEAD(&rctx->cmd.entry);
136 	rctx->cmd.engine = CCP_ENGINE_SHA;
137 	rctx->cmd.u.sha.type = rctx->type;
138 	rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
139 
140 	switch (rctx->type) {
141 	case CCP_SHA_TYPE_1:
142 		rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE;
143 		break;
144 	case CCP_SHA_TYPE_224:
145 		rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE;
146 		break;
147 	case CCP_SHA_TYPE_256:
148 		rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE;
149 		break;
150 	case CCP_SHA_TYPE_384:
151 		rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE;
152 		break;
153 	case CCP_SHA_TYPE_512:
154 		rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE;
155 		break;
156 	default:
157 		/* Should never get here */
158 		break;
159 	}
160 
161 	rctx->cmd.u.sha.src = sg;
162 	rctx->cmd.u.sha.src_len = rctx->hash_cnt;
163 	rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
164 		&ctx->u.sha.opad_sg : NULL;
165 	rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
166 		ctx->u.sha.opad_count : 0;
167 	rctx->cmd.u.sha.first = rctx->first;
168 	rctx->cmd.u.sha.final = rctx->final;
169 	rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
170 
171 	rctx->first = 0;
172 
173 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
174 
175 	return ret;
176 
177 e_free:
178 	sg_free_table(&rctx->data_sg);
179 
180 	return ret;
181 }
182 
183 static int ccp_sha_init(struct ahash_request *req)
184 {
185 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
186 	struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
187 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
188 	struct ccp_crypto_ahash_alg *alg =
189 		ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
190 	unsigned int block_size =
191 		crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
192 
193 	memset(rctx, 0, sizeof(*rctx));
194 
195 	rctx->type = alg->type;
196 	rctx->first = 1;
197 
198 	if (ctx->u.sha.key_len) {
199 		/* Buffer the HMAC key for first update */
200 		memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
201 		rctx->buf_count = block_size;
202 	}
203 
204 	return 0;
205 }
206 
207 static int ccp_sha_update(struct ahash_request *req)
208 {
209 	return ccp_do_sha_update(req, req->nbytes, 0);
210 }
211 
212 static int ccp_sha_final(struct ahash_request *req)
213 {
214 	return ccp_do_sha_update(req, 0, 1);
215 }
216 
217 static int ccp_sha_finup(struct ahash_request *req)
218 {
219 	return ccp_do_sha_update(req, req->nbytes, 1);
220 }
221 
222 static int ccp_sha_digest(struct ahash_request *req)
223 {
224 	int ret;
225 
226 	ret = ccp_sha_init(req);
227 	if (ret)
228 		return ret;
229 
230 	return ccp_sha_finup(req);
231 }
232 
233 static int ccp_sha_export(struct ahash_request *req, void *out)
234 {
235 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
236 	struct ccp_sha_exp_ctx state;
237 
238 	/* Don't let anything leak to 'out' */
239 	memset(&state, 0, sizeof(state));
240 
241 	state.type = rctx->type;
242 	state.msg_bits = rctx->msg_bits;
243 	state.first = rctx->first;
244 	memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
245 	state.buf_count = rctx->buf_count;
246 	memcpy(state.buf, rctx->buf, sizeof(state.buf));
247 
248 	/* 'out' may not be aligned so memcpy from local variable */
249 	memcpy(out, &state, sizeof(state));
250 
251 	return 0;
252 }
253 
254 static int ccp_sha_import(struct ahash_request *req, const void *in)
255 {
256 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
257 	struct ccp_sha_exp_ctx state;
258 
259 	/* 'in' may not be aligned so memcpy to local variable */
260 	memcpy(&state, in, sizeof(state));
261 
262 	memset(rctx, 0, sizeof(*rctx));
263 	rctx->type = state.type;
264 	rctx->msg_bits = state.msg_bits;
265 	rctx->first = state.first;
266 	memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
267 	rctx->buf_count = state.buf_count;
268 	memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
269 
270 	return 0;
271 }
272 
273 static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
274 			  unsigned int key_len)
275 {
276 	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
277 	struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
278 
279 	SHASH_DESC_ON_STACK(sdesc, shash);
280 
281 	unsigned int block_size = crypto_shash_blocksize(shash);
282 	unsigned int digest_size = crypto_shash_digestsize(shash);
283 	int i, ret;
284 
285 	/* Set to zero until complete */
286 	ctx->u.sha.key_len = 0;
287 
288 	/* Clear key area to provide zero padding for keys smaller
289 	 * than the block size
290 	 */
291 	memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
292 
293 	if (key_len > block_size) {
294 		/* Must hash the input key */
295 		sdesc->tfm = shash;
296 
297 		ret = crypto_shash_digest(sdesc, key, key_len,
298 					  ctx->u.sha.key);
299 		if (ret) {
300 			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
301 			return -EINVAL;
302 		}
303 
304 		key_len = digest_size;
305 	} else {
306 		memcpy(ctx->u.sha.key, key, key_len);
307 	}
308 
309 	for (i = 0; i < block_size; i++) {
310 		ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE;
311 		ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE;
312 	}
313 
314 	sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
315 	ctx->u.sha.opad_count = block_size;
316 
317 	ctx->u.sha.key_len = key_len;
318 
319 	return 0;
320 }
321 
322 static int ccp_sha_cra_init(struct crypto_tfm *tfm)
323 {
324 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
325 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
326 
327 	ctx->complete = ccp_sha_complete;
328 	ctx->u.sha.key_len = 0;
329 
330 	crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
331 
332 	return 0;
333 }
334 
335 static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
336 {
337 }
338 
339 static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
340 {
341 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
342 	struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
343 	struct crypto_shash *hmac_tfm;
344 
345 	hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
346 	if (IS_ERR(hmac_tfm)) {
347 		pr_warn("could not load driver %s need for HMAC support\n",
348 			alg->child_alg);
349 		return PTR_ERR(hmac_tfm);
350 	}
351 
352 	ctx->u.sha.hmac_tfm = hmac_tfm;
353 
354 	return ccp_sha_cra_init(tfm);
355 }
356 
357 static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
358 {
359 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
360 
361 	if (ctx->u.sha.hmac_tfm)
362 		crypto_free_shash(ctx->u.sha.hmac_tfm);
363 
364 	ccp_sha_cra_exit(tfm);
365 }
366 
367 struct ccp_sha_def {
368 	unsigned int version;
369 	const char *name;
370 	const char *drv_name;
371 	enum ccp_sha_type type;
372 	u32 digest_size;
373 	u32 block_size;
374 };
375 
376 static struct ccp_sha_def sha_algs[] = {
377 	{
378 		.version	= CCP_VERSION(3, 0),
379 		.name		= "sha1",
380 		.drv_name	= "sha1-ccp",
381 		.type		= CCP_SHA_TYPE_1,
382 		.digest_size	= SHA1_DIGEST_SIZE,
383 		.block_size	= SHA1_BLOCK_SIZE,
384 	},
385 	{
386 		.version	= CCP_VERSION(3, 0),
387 		.name		= "sha224",
388 		.drv_name	= "sha224-ccp",
389 		.type		= CCP_SHA_TYPE_224,
390 		.digest_size	= SHA224_DIGEST_SIZE,
391 		.block_size	= SHA224_BLOCK_SIZE,
392 	},
393 	{
394 		.version	= CCP_VERSION(3, 0),
395 		.name		= "sha256",
396 		.drv_name	= "sha256-ccp",
397 		.type		= CCP_SHA_TYPE_256,
398 		.digest_size	= SHA256_DIGEST_SIZE,
399 		.block_size	= SHA256_BLOCK_SIZE,
400 	},
401 	{
402 		.version	= CCP_VERSION(5, 0),
403 		.name		= "sha384",
404 		.drv_name	= "sha384-ccp",
405 		.type		= CCP_SHA_TYPE_384,
406 		.digest_size	= SHA384_DIGEST_SIZE,
407 		.block_size	= SHA384_BLOCK_SIZE,
408 	},
409 	{
410 		.version	= CCP_VERSION(5, 0),
411 		.name		= "sha512",
412 		.drv_name	= "sha512-ccp",
413 		.type		= CCP_SHA_TYPE_512,
414 		.digest_size	= SHA512_DIGEST_SIZE,
415 		.block_size	= SHA512_BLOCK_SIZE,
416 	},
417 };
418 
419 static int ccp_register_hmac_alg(struct list_head *head,
420 				 const struct ccp_sha_def *def,
421 				 const struct ccp_crypto_ahash_alg *base_alg)
422 {
423 	struct ccp_crypto_ahash_alg *ccp_alg;
424 	struct ahash_alg *alg;
425 	struct hash_alg_common *halg;
426 	struct crypto_alg *base;
427 	int ret;
428 
429 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
430 	if (!ccp_alg)
431 		return -ENOMEM;
432 
433 	/* Copy the base algorithm and only change what's necessary */
434 	*ccp_alg = *base_alg;
435 	INIT_LIST_HEAD(&ccp_alg->entry);
436 
437 	strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
438 
439 	alg = &ccp_alg->alg;
440 	alg->setkey = ccp_sha_setkey;
441 
442 	halg = &alg->halg;
443 
444 	base = &halg->base;
445 	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
446 	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
447 		 def->drv_name);
448 	base->cra_init = ccp_hmac_sha_cra_init;
449 	base->cra_exit = ccp_hmac_sha_cra_exit;
450 
451 	ret = crypto_register_ahash(alg);
452 	if (ret) {
453 		pr_err("%s ahash algorithm registration error (%d)\n",
454 		       base->cra_name, ret);
455 		kfree(ccp_alg);
456 		return ret;
457 	}
458 
459 	list_add(&ccp_alg->entry, head);
460 
461 	return ret;
462 }
463 
464 static int ccp_register_sha_alg(struct list_head *head,
465 				const struct ccp_sha_def *def)
466 {
467 	struct ccp_crypto_ahash_alg *ccp_alg;
468 	struct ahash_alg *alg;
469 	struct hash_alg_common *halg;
470 	struct crypto_alg *base;
471 	int ret;
472 
473 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
474 	if (!ccp_alg)
475 		return -ENOMEM;
476 
477 	INIT_LIST_HEAD(&ccp_alg->entry);
478 
479 	ccp_alg->type = def->type;
480 
481 	alg = &ccp_alg->alg;
482 	alg->init = ccp_sha_init;
483 	alg->update = ccp_sha_update;
484 	alg->final = ccp_sha_final;
485 	alg->finup = ccp_sha_finup;
486 	alg->digest = ccp_sha_digest;
487 	alg->export = ccp_sha_export;
488 	alg->import = ccp_sha_import;
489 
490 	halg = &alg->halg;
491 	halg->digestsize = def->digest_size;
492 	halg->statesize = sizeof(struct ccp_sha_exp_ctx);
493 
494 	base = &halg->base;
495 	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
496 	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
497 		 def->drv_name);
498 	base->cra_flags = CRYPTO_ALG_ASYNC |
499 			  CRYPTO_ALG_KERN_DRIVER_ONLY |
500 			  CRYPTO_ALG_NEED_FALLBACK;
501 	base->cra_blocksize = def->block_size;
502 	base->cra_ctxsize = sizeof(struct ccp_ctx);
503 	base->cra_priority = CCP_CRA_PRIORITY;
504 	base->cra_init = ccp_sha_cra_init;
505 	base->cra_exit = ccp_sha_cra_exit;
506 	base->cra_module = THIS_MODULE;
507 
508 	ret = crypto_register_ahash(alg);
509 	if (ret) {
510 		pr_err("%s ahash algorithm registration error (%d)\n",
511 		       base->cra_name, ret);
512 		kfree(ccp_alg);
513 		return ret;
514 	}
515 
516 	list_add(&ccp_alg->entry, head);
517 
518 	ret = ccp_register_hmac_alg(head, def, ccp_alg);
519 
520 	return ret;
521 }
522 
523 int ccp_register_sha_algs(struct list_head *head)
524 {
525 	int i, ret;
526 	unsigned int ccpversion = ccp_version();
527 
528 	for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
529 		if (sha_algs[i].version > ccpversion)
530 			continue;
531 		ret = ccp_register_sha_alg(head, &sha_algs[i]);
532 		if (ret)
533 			return ret;
534 	}
535 
536 	return 0;
537 }
538