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
26static 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
51e_free:
52	sg_free_table(&rctx->data_sg);
53
54	return ret;
55}
56
57static 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		sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
111		sg_mark_end(sg);
112
113		sg = rctx->data_sg.sgl;
114	} else if (rctx->buf_count) {
115		sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
116
117		sg = &rctx->buf_sg;
118	} else if (nbytes) {
119		sg = req->src;
120	}
121
122	rctx->msg_bits += (rctx->hash_cnt << 3);	/* Total in bits */
123
124	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
125	INIT_LIST_HEAD(&rctx->cmd.entry);
126	rctx->cmd.engine = CCP_ENGINE_SHA;
127	rctx->cmd.u.sha.type = rctx->type;
128	rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
129	rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx);
130	rctx->cmd.u.sha.src = sg;
131	rctx->cmd.u.sha.src_len = rctx->hash_cnt;
132	rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
133		&ctx->u.sha.opad_sg : NULL;
134	rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
135		ctx->u.sha.opad_count : 0;
136	rctx->cmd.u.sha.first = rctx->first;
137	rctx->cmd.u.sha.final = rctx->final;
138	rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
139
140	rctx->first = 0;
141
142	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
143
144	return ret;
145}
146
147static int ccp_sha_init(struct ahash_request *req)
148{
149	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
150	struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
151	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
152	struct ccp_crypto_ahash_alg *alg =
153		ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
154	unsigned int block_size =
155		crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
156
157	memset(rctx, 0, sizeof(*rctx));
158
159	rctx->type = alg->type;
160	rctx->first = 1;
161
162	if (ctx->u.sha.key_len) {
163		/* Buffer the HMAC key for first update */
164		memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
165		rctx->buf_count = block_size;
166	}
167
168	return 0;
169}
170
171static int ccp_sha_update(struct ahash_request *req)
172{
173	return ccp_do_sha_update(req, req->nbytes, 0);
174}
175
176static int ccp_sha_final(struct ahash_request *req)
177{
178	return ccp_do_sha_update(req, 0, 1);
179}
180
181static int ccp_sha_finup(struct ahash_request *req)
182{
183	return ccp_do_sha_update(req, req->nbytes, 1);
184}
185
186static int ccp_sha_digest(struct ahash_request *req)
187{
188	int ret;
189
190	ret = ccp_sha_init(req);
191	if (ret)
192		return ret;
193
194	return ccp_sha_finup(req);
195}
196
197static int ccp_sha_export(struct ahash_request *req, void *out)
198{
199	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
200	struct ccp_sha_exp_ctx state;
201
202	/* Don't let anything leak to 'out' */
203	memset(&state, 0, sizeof(state));
204
205	state.type = rctx->type;
206	state.msg_bits = rctx->msg_bits;
207	state.first = rctx->first;
208	memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
209	state.buf_count = rctx->buf_count;
210	memcpy(state.buf, rctx->buf, sizeof(state.buf));
211
212	/* 'out' may not be aligned so memcpy from local variable */
213	memcpy(out, &state, sizeof(state));
214
215	return 0;
216}
217
218static int ccp_sha_import(struct ahash_request *req, const void *in)
219{
220	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
221	struct ccp_sha_exp_ctx state;
222
223	/* 'in' may not be aligned so memcpy to local variable */
224	memcpy(&state, in, sizeof(state));
225
226	memset(rctx, 0, sizeof(*rctx));
227	rctx->type = state.type;
228	rctx->msg_bits = state.msg_bits;
229	rctx->first = state.first;
230	memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
231	rctx->buf_count = state.buf_count;
232	memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
233
234	return 0;
235}
236
237static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
238			  unsigned int key_len)
239{
240	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
241	struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
242
243	SHASH_DESC_ON_STACK(sdesc, shash);
244
245	unsigned int block_size = crypto_shash_blocksize(shash);
246	unsigned int digest_size = crypto_shash_digestsize(shash);
247	int i, ret;
248
249	/* Set to zero until complete */
250	ctx->u.sha.key_len = 0;
251
252	/* Clear key area to provide zero padding for keys smaller
253	 * than the block size
254	 */
255	memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
256
257	if (key_len > block_size) {
258		/* Must hash the input key */
259		sdesc->tfm = shash;
260		sdesc->flags = crypto_ahash_get_flags(tfm) &
261			CRYPTO_TFM_REQ_MAY_SLEEP;
262
263		ret = crypto_shash_digest(sdesc, key, key_len,
264					  ctx->u.sha.key);
265		if (ret) {
266			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
267			return -EINVAL;
268		}
269
270		key_len = digest_size;
271	} else {
272		memcpy(ctx->u.sha.key, key, key_len);
273	}
274
275	for (i = 0; i < block_size; i++) {
276		ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36;
277		ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
278	}
279
280	sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
281	ctx->u.sha.opad_count = block_size;
282
283	ctx->u.sha.key_len = key_len;
284
285	return 0;
286}
287
288static int ccp_sha_cra_init(struct crypto_tfm *tfm)
289{
290	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
291	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
292
293	ctx->complete = ccp_sha_complete;
294	ctx->u.sha.key_len = 0;
295
296	crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
297
298	return 0;
299}
300
301static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
302{
303}
304
305static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
306{
307	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
308	struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
309	struct crypto_shash *hmac_tfm;
310
311	hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
312	if (IS_ERR(hmac_tfm)) {
313		pr_warn("could not load driver %s need for HMAC support\n",
314			alg->child_alg);
315		return PTR_ERR(hmac_tfm);
316	}
317
318	ctx->u.sha.hmac_tfm = hmac_tfm;
319
320	return ccp_sha_cra_init(tfm);
321}
322
323static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
324{
325	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
326
327	if (ctx->u.sha.hmac_tfm)
328		crypto_free_shash(ctx->u.sha.hmac_tfm);
329
330	ccp_sha_cra_exit(tfm);
331}
332
333struct ccp_sha_def {
334	const char *name;
335	const char *drv_name;
336	enum ccp_sha_type type;
337	u32 digest_size;
338	u32 block_size;
339};
340
341static struct ccp_sha_def sha_algs[] = {
342	{
343		.name		= "sha1",
344		.drv_name	= "sha1-ccp",
345		.type		= CCP_SHA_TYPE_1,
346		.digest_size	= SHA1_DIGEST_SIZE,
347		.block_size	= SHA1_BLOCK_SIZE,
348	},
349	{
350		.name		= "sha224",
351		.drv_name	= "sha224-ccp",
352		.type		= CCP_SHA_TYPE_224,
353		.digest_size	= SHA224_DIGEST_SIZE,
354		.block_size	= SHA224_BLOCK_SIZE,
355	},
356	{
357		.name		= "sha256",
358		.drv_name	= "sha256-ccp",
359		.type		= CCP_SHA_TYPE_256,
360		.digest_size	= SHA256_DIGEST_SIZE,
361		.block_size	= SHA256_BLOCK_SIZE,
362	},
363};
364
365static int ccp_register_hmac_alg(struct list_head *head,
366				 const struct ccp_sha_def *def,
367				 const struct ccp_crypto_ahash_alg *base_alg)
368{
369	struct ccp_crypto_ahash_alg *ccp_alg;
370	struct ahash_alg *alg;
371	struct hash_alg_common *halg;
372	struct crypto_alg *base;
373	int ret;
374
375	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
376	if (!ccp_alg)
377		return -ENOMEM;
378
379	/* Copy the base algorithm and only change what's necessary */
380	*ccp_alg = *base_alg;
381	INIT_LIST_HEAD(&ccp_alg->entry);
382
383	strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
384
385	alg = &ccp_alg->alg;
386	alg->setkey = ccp_sha_setkey;
387
388	halg = &alg->halg;
389
390	base = &halg->base;
391	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
392	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
393		 def->drv_name);
394	base->cra_init = ccp_hmac_sha_cra_init;
395	base->cra_exit = ccp_hmac_sha_cra_exit;
396
397	ret = crypto_register_ahash(alg);
398	if (ret) {
399		pr_err("%s ahash algorithm registration error (%d)\n",
400		       base->cra_name, ret);
401		kfree(ccp_alg);
402		return ret;
403	}
404
405	list_add(&ccp_alg->entry, head);
406
407	return ret;
408}
409
410static int ccp_register_sha_alg(struct list_head *head,
411				const struct ccp_sha_def *def)
412{
413	struct ccp_crypto_ahash_alg *ccp_alg;
414	struct ahash_alg *alg;
415	struct hash_alg_common *halg;
416	struct crypto_alg *base;
417	int ret;
418
419	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
420	if (!ccp_alg)
421		return -ENOMEM;
422
423	INIT_LIST_HEAD(&ccp_alg->entry);
424
425	ccp_alg->type = def->type;
426
427	alg = &ccp_alg->alg;
428	alg->init = ccp_sha_init;
429	alg->update = ccp_sha_update;
430	alg->final = ccp_sha_final;
431	alg->finup = ccp_sha_finup;
432	alg->digest = ccp_sha_digest;
433	alg->export = ccp_sha_export;
434	alg->import = ccp_sha_import;
435
436	halg = &alg->halg;
437	halg->digestsize = def->digest_size;
438	halg->statesize = sizeof(struct ccp_sha_exp_ctx);
439
440	base = &halg->base;
441	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
442	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
443		 def->drv_name);
444	base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
445			  CRYPTO_ALG_KERN_DRIVER_ONLY |
446			  CRYPTO_ALG_NEED_FALLBACK;
447	base->cra_blocksize = def->block_size;
448	base->cra_ctxsize = sizeof(struct ccp_ctx);
449	base->cra_priority = CCP_CRA_PRIORITY;
450	base->cra_type = &crypto_ahash_type;
451	base->cra_init = ccp_sha_cra_init;
452	base->cra_exit = ccp_sha_cra_exit;
453	base->cra_module = THIS_MODULE;
454
455	ret = crypto_register_ahash(alg);
456	if (ret) {
457		pr_err("%s ahash algorithm registration error (%d)\n",
458		       base->cra_name, ret);
459		kfree(ccp_alg);
460		return ret;
461	}
462
463	list_add(&ccp_alg->entry, head);
464
465	ret = ccp_register_hmac_alg(head, def, ccp_alg);
466
467	return ret;
468}
469
470int ccp_register_sha_algs(struct list_head *head)
471{
472	int i, ret;
473
474	for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
475		ret = ccp_register_sha_alg(head, &sha_algs[i]);
476		if (ret)
477			return ret;
478	}
479
480	return 0;
481}
482