Skip to content

Commit 3c08fee

Browse files
committed
crypto: seqiv - Add seqniv
This patch adds a new IV generator seqniv which is identical to seqiv except that it skips the IV when authenticating. This is intended to be used by algorithms such as rfc4106 that does the IV authentication implicitly. Note that the code used for seqniv is in fact identical to the compatibility case for seqiv. Signed-off-by: Herbert Xu <[email protected]>
1 parent 856e3f4 commit 3c08fee

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

crypto/seqiv.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ static void seqiv_aead_exit(struct crypto_tfm *tfm)
584584
}
585585

586586
static struct crypto_template seqiv_tmpl;
587+
static struct crypto_template seqniv_tmpl;
587588

588589
static struct crypto_instance *seqiv_ablkcipher_alloc(struct rtattr **tb)
589590
{
@@ -710,6 +711,51 @@ static struct crypto_instance *seqiv_alloc(struct rtattr **tb)
710711
goto out;
711712
}
712713

714+
static struct crypto_instance *seqniv_alloc(struct rtattr **tb)
715+
{
716+
struct aead_instance *inst;
717+
struct crypto_aead_spawn *spawn;
718+
struct aead_alg *alg;
719+
int err;
720+
721+
err = crypto_get_default_rng();
722+
if (err)
723+
return ERR_PTR(err);
724+
725+
inst = aead_geniv_alloc(&seqniv_tmpl, tb, 0, 0);
726+
727+
if (IS_ERR(inst))
728+
goto put_rng;
729+
730+
if (inst->alg.ivsize < sizeof(u64)) {
731+
aead_geniv_free(inst);
732+
inst = ERR_PTR(-EINVAL);
733+
goto put_rng;
734+
}
735+
736+
spawn = aead_instance_ctx(inst);
737+
alg = crypto_spawn_aead_alg(spawn);
738+
739+
inst->alg.setkey = seqiv_aead_setkey;
740+
inst->alg.setauthsize = seqiv_aead_setauthsize;
741+
inst->alg.encrypt = seqiv_aead_encrypt_compat_first;
742+
inst->alg.decrypt = seqiv_aead_decrypt_compat;
743+
744+
inst->alg.base.cra_init = seqiv_aead_compat_init;
745+
inst->alg.base.cra_exit = seqiv_aead_compat_exit;
746+
747+
inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
748+
inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
749+
inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
750+
751+
out:
752+
return aead_crypto_instance(inst);
753+
754+
put_rng:
755+
crypto_put_default_rng();
756+
goto out;
757+
}
758+
713759
static void seqiv_free(struct crypto_instance *inst)
714760
{
715761
if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
@@ -726,9 +772,31 @@ static struct crypto_template seqiv_tmpl = {
726772
.module = THIS_MODULE,
727773
};
728774

775+
static struct crypto_template seqniv_tmpl = {
776+
.name = "seqniv",
777+
.alloc = seqniv_alloc,
778+
.free = seqiv_free,
779+
.module = THIS_MODULE,
780+
};
781+
729782
static int __init seqiv_module_init(void)
730783
{
731-
return crypto_register_template(&seqiv_tmpl);
784+
int err;
785+
786+
err = crypto_register_template(&seqiv_tmpl);
787+
if (err)
788+
goto out;
789+
790+
err = crypto_register_template(&seqniv_tmpl);
791+
if (err)
792+
goto out_undo_niv;
793+
794+
out:
795+
return err;
796+
797+
out_undo_niv:
798+
crypto_unregister_template(&seqiv_tmpl);
799+
goto out;
732800
}
733801

734802
static void __exit seqiv_module_exit(void)
@@ -742,3 +810,4 @@ module_exit(seqiv_module_exit);
742810
MODULE_LICENSE("GPL");
743811
MODULE_DESCRIPTION("Sequence Number IV Generator");
744812
MODULE_ALIAS_CRYPTO("seqiv");
813+
MODULE_ALIAS_CRYPTO("seqniv");

0 commit comments

Comments
 (0)