Skip to content

Commit 856e3f4

Browse files
committed
crypto: seqiv - Add support for new AEAD interface
This patch converts the seqiv IV generator to work with the new AEAD interface where IV generators are just normal AEAD algorithms. Full backwards compatibility is paramount at this point since no users have yet switched over to the new interface. Nor can they switch to the new interface until IV generation is fully supported by it. So this means we are adding two versions of seqiv alongside the existing one. The first one is the one that will be used when the underlying AEAD algorithm has switched over to the new AEAD interface. The second one handles the current case where the underlying AEAD algorithm still uses the old interface. Both versions export themselves through the new AEAD interface. Signed-off-by: Herbert Xu <[email protected]>
1 parent 74412fd commit 856e3f4

File tree

4 files changed

+443
-51
lines changed

4 files changed

+443
-51
lines changed

crypto/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ config CRYPTO_SEQIV
221221
tristate "Sequence Number IV Generator"
222222
select CRYPTO_AEAD
223223
select CRYPTO_BLKCIPHER
224+
select CRYPTO_NULL
224225
select CRYPTO_RNG
225226
help
226227
This IV generator generates an IV based on a sequence number by

crypto/aead.c

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,16 @@ static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
378378
return crypto_grab_spawn(&spawn->base, name, type, mask);
379379
}
380380

381-
struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
382-
struct rtattr **tb, u32 type,
383-
u32 mask)
381+
struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
382+
struct rtattr **tb, u32 type, u32 mask)
384383
{
385384
const char *name;
386385
struct crypto_aead_spawn *spawn;
387386
struct crypto_attr_type *algt;
388-
struct crypto_instance *inst;
389-
struct crypto_alg *alg;
387+
struct aead_instance *inst;
388+
struct aead_alg *alg;
389+
unsigned int ivsize;
390+
unsigned int maxauthsize;
390391
int err;
391392

392393
algt = crypto_get_attr_type(tb);
@@ -405,20 +406,28 @@ struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
405406
if (!inst)
406407
return ERR_PTR(-ENOMEM);
407408

408-
spawn = crypto_instance_ctx(inst);
409+
spawn = aead_instance_ctx(inst);
409410

410411
/* Ignore async algorithms if necessary. */
411412
mask |= crypto_requires_sync(algt->type, algt->mask);
412413

413-
crypto_set_aead_spawn(spawn, inst);
414+
crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
414415
err = crypto_grab_nivaead(spawn, name, type, mask);
415416
if (err)
416417
goto err_free_inst;
417418

418-
alg = crypto_aead_spawn_alg(spawn);
419+
alg = crypto_spawn_aead_alg(spawn);
420+
421+
if (alg->base.cra_aead.encrypt) {
422+
ivsize = alg->base.cra_aead.ivsize;
423+
maxauthsize = alg->base.cra_aead.maxauthsize;
424+
} else {
425+
ivsize = alg->ivsize;
426+
maxauthsize = alg->maxauthsize;
427+
}
419428

420429
err = -EINVAL;
421-
if (!alg->cra_aead.ivsize)
430+
if (!ivsize)
422431
goto err_drop_alg;
423432

424433
/*
@@ -427,39 +436,56 @@ struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
427436
* template name and double-check the IV generator.
428437
*/
429438
if (algt->mask & CRYPTO_ALG_GENIV) {
430-
if (strcmp(tmpl->name, alg->cra_aead.geniv))
439+
if (!alg->base.cra_aead.encrypt)
440+
goto err_drop_alg;
441+
if (strcmp(tmpl->name, alg->base.cra_aead.geniv))
431442
goto err_drop_alg;
432443

433-
memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
434-
memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
444+
memcpy(inst->alg.base.cra_name, alg->base.cra_name,
435445
CRYPTO_MAX_ALG_NAME);
436-
} else {
437-
err = -ENAMETOOLONG;
438-
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
439-
"%s(%s)", tmpl->name, alg->cra_name) >=
440-
CRYPTO_MAX_ALG_NAME)
441-
goto err_drop_alg;
442-
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
443-
"%s(%s)", tmpl->name, alg->cra_driver_name) >=
444-
CRYPTO_MAX_ALG_NAME)
445-
goto err_drop_alg;
446+
memcpy(inst->alg.base.cra_driver_name,
447+
alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME);
448+
449+
inst->alg.base.cra_flags = CRYPTO_ALG_TYPE_AEAD |
450+
CRYPTO_ALG_GENIV;
451+
inst->alg.base.cra_flags |= alg->base.cra_flags &
452+
CRYPTO_ALG_ASYNC;
453+
inst->alg.base.cra_priority = alg->base.cra_priority;
454+
inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
455+
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
456+
inst->alg.base.cra_type = &crypto_aead_type;
457+
458+
inst->alg.base.cra_aead.ivsize = ivsize;
459+
inst->alg.base.cra_aead.maxauthsize = maxauthsize;
460+
461+
inst->alg.base.cra_aead.setkey = alg->base.cra_aead.setkey;
462+
inst->alg.base.cra_aead.setauthsize =
463+
alg->base.cra_aead.setauthsize;
464+
inst->alg.base.cra_aead.encrypt = alg->base.cra_aead.encrypt;
465+
inst->alg.base.cra_aead.decrypt = alg->base.cra_aead.decrypt;
466+
467+
goto out;
446468
}
447469

448-
inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV;
449-
inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
450-
inst->alg.cra_priority = alg->cra_priority;
451-
inst->alg.cra_blocksize = alg->cra_blocksize;
452-
inst->alg.cra_alignmask = alg->cra_alignmask;
453-
inst->alg.cra_type = &crypto_aead_type;
470+
err = -ENAMETOOLONG;
471+
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
472+
"%s(%s)", tmpl->name, alg->base.cra_name) >=
473+
CRYPTO_MAX_ALG_NAME)
474+
goto err_drop_alg;
475+
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
476+
"%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
477+
CRYPTO_MAX_ALG_NAME)
478+
goto err_drop_alg;
454479

455-
inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
456-
inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
457-
inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
480+
inst->alg.base.cra_flags = CRYPTO_ALG_TYPE_AEAD;
481+
inst->alg.base.cra_flags |= alg->base.cra_flags & CRYPTO_ALG_ASYNC;
482+
inst->alg.base.cra_priority = alg->base.cra_priority;
483+
inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
484+
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
485+
inst->alg.base.cra_type = &crypto_new_aead_type;
458486

459-
inst->alg.cra_aead.setkey = alg->cra_aead.setkey;
460-
inst->alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
461-
inst->alg.cra_aead.encrypt = alg->cra_aead.encrypt;
462-
inst->alg.cra_aead.decrypt = alg->cra_aead.decrypt;
487+
inst->alg.ivsize = ivsize;
488+
inst->alg.maxauthsize = maxauthsize;
463489

464490
out:
465491
return inst;
@@ -473,9 +499,9 @@ struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
473499
}
474500
EXPORT_SYMBOL_GPL(aead_geniv_alloc);
475501

476-
void aead_geniv_free(struct crypto_instance *inst)
502+
void aead_geniv_free(struct aead_instance *inst)
477503
{
478-
crypto_drop_aead(crypto_instance_ctx(inst));
504+
crypto_drop_aead(aead_instance_ctx(inst));
479505
kfree(inst);
480506
}
481507
EXPORT_SYMBOL_GPL(aead_geniv_free);

0 commit comments

Comments
 (0)