Skip to content

Commit 46309d8

Browse files
committed
crypto: cryptd - Use shash algorithms
This patch changes cryptd to use shash algorithms instead of the legacy hash interface. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 7eddf95 commit 46309d8

1 file changed

Lines changed: 84 additions & 80 deletions

File tree

‎crypto/cryptd.c‎

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ struct cryptd_instance_ctx {
3939
struct cryptd_queue *queue;
4040
};
4141

42+
struct hashd_instance_ctx {
43+
struct crypto_shash_spawn spawn;
44+
struct cryptd_queue *queue;
45+
};
46+
4247
struct cryptd_blkcipher_ctx {
4348
struct crypto_blkcipher *child;
4449
};
@@ -48,11 +53,12 @@ struct cryptd_blkcipher_request_ctx {
4853
};
4954

5055
struct cryptd_hash_ctx {
51-
struct crypto_hash *child;
56+
struct crypto_shash *child;
5257
};
5358

5459
struct cryptd_hash_request_ctx {
5560
crypto_completion_t complete;
61+
struct shash_desc desc;
5662
};
5763

5864
static void cryptd_queue_worker(struct work_struct *work);
@@ -250,13 +256,12 @@ static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
250256
}
251257

252258
static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
253-
struct cryptd_queue *queue)
259+
unsigned int tail)
254260
{
255261
struct crypto_instance *inst;
256-
struct cryptd_instance_ctx *ctx;
257262
int err;
258263

259-
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
264+
inst = kzalloc(sizeof(*inst) + tail, GFP_KERNEL);
260265
if (!inst) {
261266
inst = ERR_PTR(-ENOMEM);
262267
goto out;
@@ -267,14 +272,6 @@ static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
267272
"cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
268273
goto out_free_inst;
269274

270-
ctx = crypto_instance_ctx(inst);
271-
err = crypto_init_spawn(&ctx->spawn, alg, inst,
272-
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
273-
if (err)
274-
goto out_free_inst;
275-
276-
ctx->queue = queue;
277-
278275
memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
279276

280277
inst->alg.cra_priority = alg->cra_priority + 50;
@@ -293,18 +290,28 @@ static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
293290
static struct crypto_instance *cryptd_alloc_blkcipher(
294291
struct rtattr **tb, struct cryptd_queue *queue)
295292
{
293+
struct cryptd_instance_ctx *ctx;
296294
struct crypto_instance *inst;
297295
struct crypto_alg *alg;
296+
int err;
298297

299298
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
300299
CRYPTO_ALG_TYPE_MASK);
301300
if (IS_ERR(alg))
302301
return ERR_CAST(alg);
303302

304-
inst = cryptd_alloc_instance(alg, queue);
303+
inst = cryptd_alloc_instance(alg, sizeof(*ctx));
305304
if (IS_ERR(inst))
306305
goto out_put_alg;
307306

307+
ctx = crypto_instance_ctx(inst);
308+
ctx->queue = queue;
309+
310+
err = crypto_init_spawn(&ctx->spawn, alg, inst,
311+
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
312+
if (err)
313+
goto out_free_inst;
314+
308315
inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
309316
inst->alg.cra_type = &crypto_ablkcipher_type;
310317

@@ -326,46 +333,51 @@ static struct crypto_instance *cryptd_alloc_blkcipher(
326333
out_put_alg:
327334
crypto_mod_put(alg);
328335
return inst;
336+
337+
out_free_inst:
338+
kfree(inst);
339+
inst = ERR_PTR(err);
340+
goto out_put_alg;
329341
}
330342

331343
static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
332344
{
333345
struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
334-
struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
335-
struct crypto_spawn *spawn = &ictx->spawn;
346+
struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
347+
struct crypto_shash_spawn *spawn = &ictx->spawn;
336348
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
337-
struct crypto_hash *cipher;
349+
struct crypto_shash *hash;
338350

339-
cipher = crypto_spawn_hash(spawn);
340-
if (IS_ERR(cipher))
341-
return PTR_ERR(cipher);
351+
hash = crypto_spawn_shash(spawn);
352+
if (IS_ERR(hash))
353+
return PTR_ERR(hash);
342354

343-
ctx->child = cipher;
344-
tfm->crt_ahash.reqsize =
345-
sizeof(struct cryptd_hash_request_ctx);
355+
ctx->child = hash;
356+
tfm->crt_ahash.reqsize = sizeof(struct cryptd_hash_request_ctx) +
357+
crypto_shash_descsize(hash);
346358
return 0;
347359
}
348360

349361
static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
350362
{
351363
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
352364

353-
crypto_free_hash(ctx->child);
365+
crypto_free_shash(ctx->child);
354366
}
355367

356368
static int cryptd_hash_setkey(struct crypto_ahash *parent,
357369
const u8 *key, unsigned int keylen)
358370
{
359371
struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
360-
struct crypto_hash *child = ctx->child;
372+
struct crypto_shash *child = ctx->child;
361373
int err;
362374

363-
crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
364-
crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
365-
CRYPTO_TFM_REQ_MASK);
366-
err = crypto_hash_setkey(child, key, keylen);
367-
crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
368-
CRYPTO_TFM_RES_MASK);
375+
crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
376+
crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
377+
CRYPTO_TFM_REQ_MASK);
378+
err = crypto_shash_setkey(child, key, keylen);
379+
crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
380+
CRYPTO_TFM_RES_MASK);
369381
return err;
370382
}
371383

@@ -385,21 +397,19 @@ static int cryptd_hash_enqueue(struct ahash_request *req,
385397

386398
static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
387399
{
388-
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
389-
struct crypto_hash *child = ctx->child;
390-
struct ahash_request *req = ahash_request_cast(req_async);
391-
struct cryptd_hash_request_ctx *rctx;
392-
struct hash_desc desc;
393-
394-
rctx = ahash_request_ctx(req);
400+
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
401+
struct crypto_shash *child = ctx->child;
402+
struct ahash_request *req = ahash_request_cast(req_async);
403+
struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
404+
struct shash_desc *desc = &rctx->desc;
395405

396406
if (unlikely(err == -EINPROGRESS))
397407
goto out;
398408

399-
desc.tfm = child;
400-
desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
409+
desc->tfm = child;
410+
desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
401411

402-
err = crypto_hash_crt(child)->init(&desc);
412+
err = crypto_shash_init(desc);
403413

404414
req->base.complete = rctx->complete;
405415

@@ -416,23 +426,15 @@ static int cryptd_hash_init_enqueue(struct ahash_request *req)
416426

417427
static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
418428
{
419-
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
420-
struct crypto_hash *child = ctx->child;
421-
struct ahash_request *req = ahash_request_cast(req_async);
429+
struct ahash_request *req = ahash_request_cast(req_async);
422430
struct cryptd_hash_request_ctx *rctx;
423-
struct hash_desc desc;
424431

425432
rctx = ahash_request_ctx(req);
426433

427434
if (unlikely(err == -EINPROGRESS))
428435
goto out;
429436

430-
desc.tfm = child;
431-
desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
432-
433-
err = crypto_hash_crt(child)->update(&desc,
434-
req->src,
435-
req->nbytes);
437+
err = shash_ahash_update(req, &rctx->desc);
436438

437439
req->base.complete = rctx->complete;
438440

@@ -449,21 +451,13 @@ static int cryptd_hash_update_enqueue(struct ahash_request *req)
449451

450452
static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
451453
{
452-
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
453-
struct crypto_hash *child = ctx->child;
454-
struct ahash_request *req = ahash_request_cast(req_async);
455-
struct cryptd_hash_request_ctx *rctx;
456-
struct hash_desc desc;
457-
458-
rctx = ahash_request_ctx(req);
454+
struct ahash_request *req = ahash_request_cast(req_async);
455+
struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
459456

460457
if (unlikely(err == -EINPROGRESS))
461458
goto out;
462459

463-
desc.tfm = child;
464-
desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
465-
466-
err = crypto_hash_crt(child)->final(&desc, req->result);
460+
err = crypto_shash_final(&rctx->desc, req->result);
467461

468462
req->base.complete = rctx->complete;
469463

@@ -480,24 +474,19 @@ static int cryptd_hash_final_enqueue(struct ahash_request *req)
480474

481475
static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
482476
{
483-
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
484-
struct crypto_hash *child = ctx->child;
485-
struct ahash_request *req = ahash_request_cast(req_async);
486-
struct cryptd_hash_request_ctx *rctx;
487-
struct hash_desc desc;
488-
489-
rctx = ahash_request_ctx(req);
477+
struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
478+
struct crypto_shash *child = ctx->child;
479+
struct ahash_request *req = ahash_request_cast(req_async);
480+
struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
481+
struct shash_desc *desc = &rctx->desc;
490482

491483
if (unlikely(err == -EINPROGRESS))
492484
goto out;
493485

494-
desc.tfm = child;
495-
desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
486+
desc->tfm = child;
487+
desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
496488

497-
err = crypto_hash_crt(child)->digest(&desc,
498-
req->src,
499-
req->nbytes,
500-
req->result);
489+
err = shash_ahash_digest(req, desc);
501490

502491
req->base.complete = rctx->complete;
503492

@@ -515,22 +504,32 @@ static int cryptd_hash_digest_enqueue(struct ahash_request *req)
515504
static struct crypto_instance *cryptd_alloc_hash(
516505
struct rtattr **tb, struct cryptd_queue *queue)
517506
{
507+
struct hashd_instance_ctx *ctx;
518508
struct crypto_instance *inst;
509+
struct shash_alg *salg;
519510
struct crypto_alg *alg;
511+
int err;
520512

521-
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
522-
CRYPTO_ALG_TYPE_HASH_MASK);
523-
if (IS_ERR(alg))
524-
return ERR_PTR(PTR_ERR(alg));
513+
salg = shash_attr_alg(tb[1], 0, 0);
514+
if (IS_ERR(salg))
515+
return ERR_CAST(salg);
525516

526-
inst = cryptd_alloc_instance(alg, queue);
517+
alg = &salg->base;
518+
inst = cryptd_alloc_instance(alg, sizeof(*ctx));
527519
if (IS_ERR(inst))
528520
goto out_put_alg;
529521

522+
ctx = crypto_instance_ctx(inst);
523+
ctx->queue = queue;
524+
525+
err = crypto_init_shash_spawn(&ctx->spawn, salg, inst);
526+
if (err)
527+
goto out_free_inst;
528+
530529
inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
531530
inst->alg.cra_type = &crypto_ahash_type;
532531

533-
inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
532+
inst->alg.cra_ahash.digestsize = salg->digestsize;
534533
inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
535534

536535
inst->alg.cra_init = cryptd_hash_init_tfm;
@@ -545,6 +544,11 @@ static struct crypto_instance *cryptd_alloc_hash(
545544
out_put_alg:
546545
crypto_mod_put(alg);
547546
return inst;
547+
548+
out_free_inst:
549+
kfree(inst);
550+
inst = ERR_PTR(err);
551+
goto out_put_alg;
548552
}
549553

550554
static struct cryptd_queue queue;

0 commit comments

Comments
 (0)