Skip to content

Commit 7bcb2c9

Browse files
ebiggersherbertx
authored andcommitted
crypto: algapi - use common mechanism for inheriting flags
The flag CRYPTO_ALG_ASYNC is "inherited" in the sense that when a template is instantiated, the template will have CRYPTO_ALG_ASYNC set if any of the algorithms it uses has CRYPTO_ALG_ASYNC set. We'd like to add a second flag (CRYPTO_ALG_ALLOCATES_MEMORY) that gets "inherited" in the same way. This is difficult because the handling of CRYPTO_ALG_ASYNC is hardcoded everywhere. Address this by: - Add CRYPTO_ALG_INHERITED_FLAGS, which contains the set of flags that have these inheritance semantics. - Add crypto_algt_inherited_mask(), for use by template ->create() methods. It returns any of these flags that the user asked to be unset and thus must be passed in the 'mask' to crypto_grab_*(). - Also modify crypto_check_attr_type() to handle computing the 'mask' so that most templates can just use this. - Make crypto_grab_*() propagate these flags to the template instance being created so that templates don't have to do this themselves. Make crypto/simd.c propagate these flags too, since it "wraps" another algorithm, similar to a template. Based on a patch by Mikulas Patocka <mpatocka@redhat.com> (https://lore.kernel.org/r/alpine.LRH.2.02.2006301414580.30526@file01.intranet.prod.int.rdu2.redhat.com). Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 4688111 commit 7bcb2c9

23 files changed

Lines changed: 153 additions & 234 deletions

‎crypto/adiantum.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,6 @@ static bool adiantum_supported_algorithms(struct skcipher_alg *streamcipher_alg,
490490

491491
static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
492492
{
493-
struct crypto_attr_type *algt;
494493
u32 mask;
495494
const char *nhpoly1305_name;
496495
struct skcipher_instance *inst;
@@ -500,14 +499,9 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
500499
struct shash_alg *hash_alg;
501500
int err;
502501

503-
algt = crypto_get_attr_type(tb);
504-
if (IS_ERR(algt))
505-
return PTR_ERR(algt);
506-
507-
if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
508-
return -EINVAL;
509-
510-
mask = crypto_requires_sync(algt->type, algt->mask);
502+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
503+
if (err)
504+
return err;
511505

512506
inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
513507
if (!inst)
@@ -565,8 +559,6 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
565559
hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
566560
goto err_free_inst;
567561

568-
inst->alg.base.cra_flags = streamcipher_alg->base.cra_flags &
569-
CRYPTO_ALG_ASYNC;
570562
inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
571563
inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx);
572564
inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask |

‎crypto/algapi.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
690690
spawn->mask = mask;
691691
spawn->next = inst->spawns;
692692
inst->spawns = spawn;
693+
inst->alg.cra_flags |=
694+
(alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
693695
err = 0;
694696
}
695697
up_write(&crypto_alg_sem);
@@ -816,7 +818,23 @@ struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
816818
}
817819
EXPORT_SYMBOL_GPL(crypto_get_attr_type);
818820

819-
int crypto_check_attr_type(struct rtattr **tb, u32 type)
821+
/**
822+
* crypto_check_attr_type() - check algorithm type and compute inherited mask
823+
* @tb: the template parameters
824+
* @type: the algorithm type the template would be instantiated as
825+
* @mask_ret: (output) the mask that should be passed to crypto_grab_*()
826+
* to restrict the flags of any inner algorithms
827+
*
828+
* Validate that the algorithm type the user requested is compatible with the
829+
* one the template would actually be instantiated as. E.g., if the user is
830+
* doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because
831+
* the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm.
832+
*
833+
* Also compute the mask to use to restrict the flags of any inner algorithms.
834+
*
835+
* Return: 0 on success; -errno on failure
836+
*/
837+
int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret)
820838
{
821839
struct crypto_attr_type *algt;
822840

@@ -827,6 +845,7 @@ int crypto_check_attr_type(struct rtattr **tb, u32 type)
827845
if ((algt->type ^ type) & algt->mask)
828846
return -EINVAL;
829847

848+
*mask_ret = crypto_algt_inherited_mask(algt);
830849
return 0;
831850
}
832851
EXPORT_SYMBOL_GPL(crypto_check_attr_type);

‎crypto/authenc.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ static void crypto_authenc_free(struct aead_instance *inst)
372372
static int crypto_authenc_create(struct crypto_template *tmpl,
373373
struct rtattr **tb)
374374
{
375-
struct crypto_attr_type *algt;
376375
u32 mask;
377376
struct aead_instance *inst;
378377
struct authenc_instance_ctx *ctx;
@@ -381,14 +380,9 @@ static int crypto_authenc_create(struct crypto_template *tmpl,
381380
struct skcipher_alg *enc;
382381
int err;
383382

384-
algt = crypto_get_attr_type(tb);
385-
if (IS_ERR(algt))
386-
return PTR_ERR(algt);
387-
388-
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
389-
return -EINVAL;
390-
391-
mask = crypto_requires_sync(algt->type, algt->mask);
383+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
384+
if (err)
385+
return err;
392386

393387
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
394388
if (!inst)
@@ -423,8 +417,6 @@ static int crypto_authenc_create(struct crypto_template *tmpl,
423417
enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
424418
goto err_free_inst;
425419

426-
inst->alg.base.cra_flags = (auth_base->cra_flags |
427-
enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
428420
inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
429421
auth_base->cra_priority;
430422
inst->alg.base.cra_blocksize = enc->base.cra_blocksize;

‎crypto/authencesn.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ static void crypto_authenc_esn_free(struct aead_instance *inst)
390390
static int crypto_authenc_esn_create(struct crypto_template *tmpl,
391391
struct rtattr **tb)
392392
{
393-
struct crypto_attr_type *algt;
394393
u32 mask;
395394
struct aead_instance *inst;
396395
struct authenc_esn_instance_ctx *ctx;
@@ -399,14 +398,9 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl,
399398
struct skcipher_alg *enc;
400399
int err;
401400

402-
algt = crypto_get_attr_type(tb);
403-
if (IS_ERR(algt))
404-
return PTR_ERR(algt);
405-
406-
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
407-
return -EINVAL;
408-
409-
mask = crypto_requires_sync(algt->type, algt->mask);
401+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
402+
if (err)
403+
return err;
410404

411405
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
412406
if (!inst)
@@ -437,8 +431,6 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl,
437431
enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
438432
goto err_free_inst;
439433

440-
inst->alg.base.cra_flags = (auth_base->cra_flags |
441-
enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
442434
inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
443435
auth_base->cra_priority;
444436
inst->alg.base.cra_blocksize = enc->base.cra_blocksize;

‎crypto/ccm.c‎

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -447,30 +447,24 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
447447
const char *ctr_name,
448448
const char *mac_name)
449449
{
450-
struct crypto_attr_type *algt;
451450
u32 mask;
452451
struct aead_instance *inst;
453452
struct ccm_instance_ctx *ictx;
454453
struct skcipher_alg *ctr;
455454
struct hash_alg_common *mac;
456455
int err;
457456

458-
algt = crypto_get_attr_type(tb);
459-
if (IS_ERR(algt))
460-
return PTR_ERR(algt);
461-
462-
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
463-
return -EINVAL;
464-
465-
mask = crypto_requires_sync(algt->type, algt->mask);
457+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
458+
if (err)
459+
return err;
466460

467461
inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
468462
if (!inst)
469463
return -ENOMEM;
470464
ictx = aead_instance_ctx(inst);
471465

472466
err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst),
473-
mac_name, 0, CRYPTO_ALG_ASYNC);
467+
mac_name, 0, mask | CRYPTO_ALG_ASYNC);
474468
if (err)
475469
goto err_free_inst;
476470
mac = crypto_spawn_ahash_alg(&ictx->mac);
@@ -507,7 +501,6 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
507501
mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
508502
goto err_free_inst;
509503

510-
inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
511504
inst->alg.base.cra_priority = (mac->base.cra_priority +
512505
ctr->base.cra_priority) / 2;
513506
inst->alg.base.cra_blocksize = 1;
@@ -712,21 +705,15 @@ static void crypto_rfc4309_free(struct aead_instance *inst)
712705
static int crypto_rfc4309_create(struct crypto_template *tmpl,
713706
struct rtattr **tb)
714707
{
715-
struct crypto_attr_type *algt;
716708
u32 mask;
717709
struct aead_instance *inst;
718710
struct crypto_aead_spawn *spawn;
719711
struct aead_alg *alg;
720712
int err;
721713

722-
algt = crypto_get_attr_type(tb);
723-
if (IS_ERR(algt))
724-
return PTR_ERR(algt);
725-
726-
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
727-
return -EINVAL;
728-
729-
mask = crypto_requires_sync(algt->type, algt->mask);
714+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
715+
if (err)
716+
return err;
730717

731718
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
732719
if (!inst)
@@ -759,7 +746,6 @@ static int crypto_rfc4309_create(struct crypto_template *tmpl,
759746
CRYPTO_MAX_ALG_NAME)
760747
goto err_free_inst;
761748

762-
inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
763749
inst->alg.base.cra_priority = alg->base.cra_priority;
764750
inst->alg.base.cra_blocksize = 1;
765751
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
@@ -878,9 +864,10 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
878864
struct shash_instance *inst;
879865
struct crypto_cipher_spawn *spawn;
880866
struct crypto_alg *alg;
867+
u32 mask;
881868
int err;
882869

883-
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
870+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
884871
if (err)
885872
return err;
886873

@@ -890,7 +877,7 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
890877
spawn = shash_instance_ctx(inst);
891878

892879
err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
893-
crypto_attr_alg_name(tb[1]), 0, 0);
880+
crypto_attr_alg_name(tb[1]), 0, mask);
894881
if (err)
895882
goto err_free_inst;
896883
alg = crypto_spawn_cipher_alg(spawn);

‎crypto/chacha20poly1305.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ static void chachapoly_free(struct aead_instance *inst)
555555
static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
556556
const char *name, unsigned int ivsize)
557557
{
558-
struct crypto_attr_type *algt;
559558
u32 mask;
560559
struct aead_instance *inst;
561560
struct chachapoly_instance_ctx *ctx;
@@ -566,14 +565,9 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
566565
if (ivsize > CHACHAPOLY_IV_SIZE)
567566
return -EINVAL;
568567

569-
algt = crypto_get_attr_type(tb);
570-
if (IS_ERR(algt))
571-
return PTR_ERR(algt);
572-
573-
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
574-
return -EINVAL;
575-
576-
mask = crypto_requires_sync(algt->type, algt->mask);
568+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
569+
if (err)
570+
return err;
577571

578572
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
579573
if (!inst)
@@ -613,8 +607,6 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
613607
poly->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
614608
goto err_free_inst;
615609

616-
inst->alg.base.cra_flags = (chacha->base.cra_flags |
617-
poly->base.cra_flags) & CRYPTO_ALG_ASYNC;
618610
inst->alg.base.cra_priority = (chacha->base.cra_priority +
619611
poly->base.cra_priority) / 2;
620612
inst->alg.base.cra_blocksize = 1;

‎crypto/cmac.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,10 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
225225
struct crypto_cipher_spawn *spawn;
226226
struct crypto_alg *alg;
227227
unsigned long alignmask;
228+
u32 mask;
228229
int err;
229230

230-
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
231+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
231232
if (err)
232233
return err;
233234

@@ -237,7 +238,7 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
237238
spawn = shash_instance_ctx(inst);
238239

239240
err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
240-
crypto_attr_alg_name(tb[1]), 0, 0);
241+
crypto_attr_alg_name(tb[1]), 0, mask);
241242
if (err)
242243
goto err_free_inst;
243244
alg = crypto_spawn_cipher_alg(spawn);

0 commit comments

Comments
 (0)