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