Skip to content

Commit 9cd899a

Browse files
committed
crypto: cryptd - Switch to template create API
This patch changes cryptd to use the template->create function instead of alloc in anticipation for the switch to new style ahash algorithms. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 52861c7 commit 9cd899a

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

‎crypto/cryptd.c‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
287287
goto out;
288288
}
289289

290-
static struct crypto_instance *cryptd_alloc_blkcipher(
291-
struct rtattr **tb, struct cryptd_queue *queue)
290+
static int cryptd_create_blkcipher(struct crypto_template *tmpl,
291+
struct rtattr **tb,
292+
struct cryptd_queue *queue)
292293
{
293294
struct cryptd_instance_ctx *ctx;
294295
struct crypto_instance *inst;
@@ -298,7 +299,7 @@ static struct crypto_instance *cryptd_alloc_blkcipher(
298299
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
299300
CRYPTO_ALG_TYPE_MASK);
300301
if (IS_ERR(alg))
301-
return ERR_CAST(alg);
302+
return PTR_ERR(alg);
302303

303304
inst = cryptd_alloc_instance(alg, sizeof(*ctx));
304305
if (IS_ERR(inst))
@@ -330,14 +331,16 @@ static struct crypto_instance *cryptd_alloc_blkcipher(
330331
inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
331332
inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
332333

334+
err = crypto_register_instance(tmpl, inst);
335+
if (err) {
336+
crypto_drop_spawn(&ctx->spawn);
337+
out_free_inst:
338+
kfree(inst);
339+
}
340+
333341
out_put_alg:
334342
crypto_mod_put(alg);
335-
return inst;
336-
337-
out_free_inst:
338-
kfree(inst);
339-
inst = ERR_PTR(err);
340-
goto out_put_alg;
343+
return err;
341344
}
342345

343346
static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
@@ -502,8 +505,8 @@ static int cryptd_hash_digest_enqueue(struct ahash_request *req)
502505
return cryptd_hash_enqueue(req, cryptd_hash_digest);
503506
}
504507

505-
static struct crypto_instance *cryptd_alloc_hash(
506-
struct rtattr **tb, struct cryptd_queue *queue)
508+
static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
509+
struct cryptd_queue *queue)
507510
{
508511
struct hashd_instance_ctx *ctx;
509512
struct crypto_instance *inst;
@@ -513,7 +516,7 @@ static struct crypto_instance *cryptd_alloc_hash(
513516

514517
salg = shash_attr_alg(tb[1], 0, 0);
515518
if (IS_ERR(salg))
516-
return ERR_CAST(salg);
519+
return PTR_ERR(salg);
517520

518521
alg = &salg->base;
519522
inst = cryptd_alloc_instance(alg, sizeof(*ctx));
@@ -542,34 +545,36 @@ static struct crypto_instance *cryptd_alloc_hash(
542545
inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
543546
inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
544547

548+
err = crypto_register_instance(tmpl, inst);
549+
if (err) {
550+
crypto_drop_shash(&ctx->spawn);
551+
out_free_inst:
552+
kfree(inst);
553+
}
554+
545555
out_put_alg:
546556
crypto_mod_put(alg);
547-
return inst;
548-
549-
out_free_inst:
550-
kfree(inst);
551-
inst = ERR_PTR(err);
552-
goto out_put_alg;
557+
return err;
553558
}
554559

555560
static struct cryptd_queue queue;
556561

557-
static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
562+
static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
558563
{
559564
struct crypto_attr_type *algt;
560565

561566
algt = crypto_get_attr_type(tb);
562567
if (IS_ERR(algt))
563-
return ERR_CAST(algt);
568+
return PTR_ERR(algt);
564569

565570
switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
566571
case CRYPTO_ALG_TYPE_BLKCIPHER:
567-
return cryptd_alloc_blkcipher(tb, &queue);
572+
return cryptd_create_blkcipher(tmpl, tb, &queue);
568573
case CRYPTO_ALG_TYPE_DIGEST:
569-
return cryptd_alloc_hash(tb, &queue);
574+
return cryptd_create_hash(tmpl, tb, &queue);
570575
}
571576

572-
return ERR_PTR(-EINVAL);
577+
return -EINVAL;
573578
}
574579

575580
static void cryptd_free(struct crypto_instance *inst)
@@ -582,7 +587,7 @@ static void cryptd_free(struct crypto_instance *inst)
582587

583588
static struct crypto_template cryptd_tmpl = {
584589
.name = "cryptd",
585-
.alloc = cryptd_alloc,
590+
.create = cryptd_create,
586591
.free = cryptd_free,
587592
.module = THIS_MODULE,
588593
};

‎crypto/internal.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ struct crypto_alg *crypto_find_alg(const char *alg_name,
9797
void *crypto_alloc_tfm(const char *alg_name,
9898
const struct crypto_type *frontend, u32 type, u32 mask);
9999

100-
int crypto_register_instance(struct crypto_template *tmpl,
101-
struct crypto_instance *inst);
102-
103100
int crypto_register_notifier(struct notifier_block *nb);
104101
int crypto_unregister_notifier(struct notifier_block *nb);
105102
int crypto_probing_notify(unsigned long val, void *v);

‎include/crypto/algapi.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ int crypto_register_template(struct crypto_template *tmpl);
114114
void crypto_unregister_template(struct crypto_template *tmpl);
115115
struct crypto_template *crypto_lookup_template(const char *name);
116116

117+
int crypto_register_instance(struct crypto_template *tmpl,
118+
struct crypto_instance *inst);
119+
117120
int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
118121
struct crypto_instance *inst, u32 mask);
119122
int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,

0 commit comments

Comments
 (0)