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