ext4: allow readdir()'s of large empty directories to be interrupted
[linux/fpc-iii.git] / drivers / crypto / ccp / ccp-crypto-sha.c
blobb5ad72897dc25b8d11642315f3e087af32d1c845
1 /*
2 * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
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>
24 #include "ccp-crypto.h"
26 static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
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);
33 if (ret)
34 goto e_free;
36 if (rctx->hash_rem) {
37 /* Save remaining data to buffer */
38 unsigned int offset = rctx->nbytes - rctx->hash_rem;
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;
47 /* Update result area if supplied */
48 if (req->result)
49 memcpy(req->result, rctx->ctx, digest_size);
51 e_free:
52 sg_free_table(&rctx->data_sg);
54 return ret;
57 static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
58 unsigned int final)
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;
71 len = (u64)rctx->buf_count + (u64)nbytes;
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;
78 return 0;
81 rctx->src = req->src;
82 rctx->nbytes = nbytes;
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;
93 /* Initialize the context scatterlist */
94 sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
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)
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;
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;
114 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
115 if (!sg) {
116 ret = -EINVAL;
117 goto e_free;
119 sg_mark_end(sg);
121 sg = rctx->data_sg.sgl;
122 } else if (rctx->buf_count) {
123 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
125 sg = &rctx->buf_sg;
126 } else if (nbytes) {
127 sg = req->src;
130 rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
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;
148 rctx->first = 0;
150 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
152 return ret;
154 e_free:
155 sg_free_table(&rctx->data_sg);
157 return ret;
160 static int ccp_sha_init(struct ahash_request *req)
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));
170 memset(rctx, 0, sizeof(*rctx));
172 rctx->type = alg->type;
173 rctx->first = 1;
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;
181 return 0;
184 static int ccp_sha_update(struct ahash_request *req)
186 return ccp_do_sha_update(req, req->nbytes, 0);
189 static int ccp_sha_final(struct ahash_request *req)
191 return ccp_do_sha_update(req, 0, 1);
194 static int ccp_sha_finup(struct ahash_request *req)
196 return ccp_do_sha_update(req, req->nbytes, 1);
199 static int ccp_sha_digest(struct ahash_request *req)
201 int ret;
203 ret = ccp_sha_init(req);
204 if (ret)
205 return ret;
207 return ccp_sha_finup(req);
210 static int ccp_sha_export(struct ahash_request *req, void *out)
212 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
213 struct ccp_sha_exp_ctx state;
215 state.type = rctx->type;
216 state.msg_bits = rctx->msg_bits;
217 state.first = rctx->first;
218 memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
219 state.buf_count = rctx->buf_count;
220 memcpy(state.buf, rctx->buf, sizeof(state.buf));
222 /* 'out' may not be aligned so memcpy from local variable */
223 memcpy(out, &state, sizeof(state));
225 return 0;
228 static int ccp_sha_import(struct ahash_request *req, const void *in)
230 struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
231 struct ccp_sha_exp_ctx state;
233 /* 'in' may not be aligned so memcpy to local variable */
234 memcpy(&state, in, sizeof(state));
236 memset(rctx, 0, sizeof(*rctx));
237 rctx->type = state.type;
238 rctx->msg_bits = state.msg_bits;
239 rctx->first = state.first;
240 memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
241 rctx->buf_count = state.buf_count;
242 memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
244 return 0;
247 static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
248 unsigned int key_len)
250 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
251 struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
253 SHASH_DESC_ON_STACK(sdesc, shash);
255 unsigned int block_size = crypto_shash_blocksize(shash);
256 unsigned int digest_size = crypto_shash_digestsize(shash);
257 int i, ret;
259 /* Set to zero until complete */
260 ctx->u.sha.key_len = 0;
262 /* Clear key area to provide zero padding for keys smaller
263 * than the block size
265 memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
267 if (key_len > block_size) {
268 /* Must hash the input key */
269 sdesc->tfm = shash;
270 sdesc->flags = crypto_ahash_get_flags(tfm) &
271 CRYPTO_TFM_REQ_MAY_SLEEP;
273 ret = crypto_shash_digest(sdesc, key, key_len,
274 ctx->u.sha.key);
275 if (ret) {
276 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
277 return -EINVAL;
280 key_len = digest_size;
281 } else {
282 memcpy(ctx->u.sha.key, key, key_len);
285 for (i = 0; i < block_size; i++) {
286 ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36;
287 ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
290 sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
291 ctx->u.sha.opad_count = block_size;
293 ctx->u.sha.key_len = key_len;
295 return 0;
298 static int ccp_sha_cra_init(struct crypto_tfm *tfm)
300 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
301 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
303 ctx->complete = ccp_sha_complete;
304 ctx->u.sha.key_len = 0;
306 crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
308 return 0;
311 static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
315 static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
317 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
318 struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
319 struct crypto_shash *hmac_tfm;
321 hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
322 if (IS_ERR(hmac_tfm)) {
323 pr_warn("could not load driver %s need for HMAC support\n",
324 alg->child_alg);
325 return PTR_ERR(hmac_tfm);
328 ctx->u.sha.hmac_tfm = hmac_tfm;
330 return ccp_sha_cra_init(tfm);
333 static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
335 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
337 if (ctx->u.sha.hmac_tfm)
338 crypto_free_shash(ctx->u.sha.hmac_tfm);
340 ccp_sha_cra_exit(tfm);
343 struct ccp_sha_def {
344 unsigned int version;
345 const char *name;
346 const char *drv_name;
347 enum ccp_sha_type type;
348 u32 digest_size;
349 u32 block_size;
352 static struct ccp_sha_def sha_algs[] = {
354 .version = CCP_VERSION(3, 0),
355 .name = "sha1",
356 .drv_name = "sha1-ccp",
357 .type = CCP_SHA_TYPE_1,
358 .digest_size = SHA1_DIGEST_SIZE,
359 .block_size = SHA1_BLOCK_SIZE,
362 .version = CCP_VERSION(3, 0),
363 .name = "sha224",
364 .drv_name = "sha224-ccp",
365 .type = CCP_SHA_TYPE_224,
366 .digest_size = SHA224_DIGEST_SIZE,
367 .block_size = SHA224_BLOCK_SIZE,
370 .version = CCP_VERSION(3, 0),
371 .name = "sha256",
372 .drv_name = "sha256-ccp",
373 .type = CCP_SHA_TYPE_256,
374 .digest_size = SHA256_DIGEST_SIZE,
375 .block_size = SHA256_BLOCK_SIZE,
379 static int ccp_register_hmac_alg(struct list_head *head,
380 const struct ccp_sha_def *def,
381 const struct ccp_crypto_ahash_alg *base_alg)
383 struct ccp_crypto_ahash_alg *ccp_alg;
384 struct ahash_alg *alg;
385 struct hash_alg_common *halg;
386 struct crypto_alg *base;
387 int ret;
389 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
390 if (!ccp_alg)
391 return -ENOMEM;
393 /* Copy the base algorithm and only change what's necessary */
394 *ccp_alg = *base_alg;
395 INIT_LIST_HEAD(&ccp_alg->entry);
397 strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
399 alg = &ccp_alg->alg;
400 alg->setkey = ccp_sha_setkey;
402 halg = &alg->halg;
404 base = &halg->base;
405 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
406 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
407 def->drv_name);
408 base->cra_init = ccp_hmac_sha_cra_init;
409 base->cra_exit = ccp_hmac_sha_cra_exit;
411 ret = crypto_register_ahash(alg);
412 if (ret) {
413 pr_err("%s ahash algorithm registration error (%d)\n",
414 base->cra_name, ret);
415 kfree(ccp_alg);
416 return ret;
419 list_add(&ccp_alg->entry, head);
421 return ret;
424 static int ccp_register_sha_alg(struct list_head *head,
425 const struct ccp_sha_def *def)
427 struct ccp_crypto_ahash_alg *ccp_alg;
428 struct ahash_alg *alg;
429 struct hash_alg_common *halg;
430 struct crypto_alg *base;
431 int ret;
433 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
434 if (!ccp_alg)
435 return -ENOMEM;
437 INIT_LIST_HEAD(&ccp_alg->entry);
439 ccp_alg->type = def->type;
441 alg = &ccp_alg->alg;
442 alg->init = ccp_sha_init;
443 alg->update = ccp_sha_update;
444 alg->final = ccp_sha_final;
445 alg->finup = ccp_sha_finup;
446 alg->digest = ccp_sha_digest;
447 alg->export = ccp_sha_export;
448 alg->import = ccp_sha_import;
450 halg = &alg->halg;
451 halg->digestsize = def->digest_size;
452 halg->statesize = sizeof(struct ccp_sha_exp_ctx);
454 base = &halg->base;
455 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
456 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
457 def->drv_name);
458 base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
459 CRYPTO_ALG_KERN_DRIVER_ONLY |
460 CRYPTO_ALG_NEED_FALLBACK;
461 base->cra_blocksize = def->block_size;
462 base->cra_ctxsize = sizeof(struct ccp_ctx);
463 base->cra_priority = CCP_CRA_PRIORITY;
464 base->cra_type = &crypto_ahash_type;
465 base->cra_init = ccp_sha_cra_init;
466 base->cra_exit = ccp_sha_cra_exit;
467 base->cra_module = THIS_MODULE;
469 ret = crypto_register_ahash(alg);
470 if (ret) {
471 pr_err("%s ahash algorithm registration error (%d)\n",
472 base->cra_name, ret);
473 kfree(ccp_alg);
474 return ret;
477 list_add(&ccp_alg->entry, head);
479 ret = ccp_register_hmac_alg(head, def, ccp_alg);
481 return ret;
484 int ccp_register_sha_algs(struct list_head *head)
486 int i, ret;
487 unsigned int ccpversion = ccp_version();
489 for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
490 if (sha_algs[i].version > ccpversion)
491 continue;
492 ret = ccp_register_sha_alg(head, &sha_algs[i]);
493 if (ret)
494 return ret;
497 return 0;