1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * geniv: Shared IV generator code
5 * This file provides common code to IV generators such as seqiv.
7 * Copyright (c) 2007-2019 Herbert Xu <herbert@gondor.apana.org.au>
10 #include <crypto/internal/geniv.h>
11 #include <crypto/internal/rng.h>
12 #include <crypto/null.h>
13 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/slab.h>
19 static int aead_geniv_setkey(struct crypto_aead *tfm,
20 const u8 *key, unsigned int keylen)
22 struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
24 return crypto_aead_setkey(ctx->child, key, keylen);
27 static int aead_geniv_setauthsize(struct crypto_aead *tfm,
28 unsigned int authsize)
30 struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
32 return crypto_aead_setauthsize(ctx->child, authsize);
35 static void aead_geniv_free(struct aead_instance *inst)
37 crypto_drop_aead(aead_instance_ctx(inst));
41 struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
44 struct crypto_aead_spawn *spawn;
45 struct aead_instance *inst;
48 unsigned int maxauthsize;
52 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
56 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
58 return ERR_PTR(-ENOMEM);
60 spawn = aead_instance_ctx(inst);
62 err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
63 crypto_attr_alg_name(tb[1]), 0, mask);
67 alg = crypto_spawn_aead_alg(spawn);
69 ivsize = crypto_aead_alg_ivsize(alg);
70 maxauthsize = crypto_aead_alg_maxauthsize(alg);
73 if (ivsize < sizeof(u64))
77 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
78 "%s(%s)", tmpl->name, alg->base.cra_name) >=
81 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
82 "%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
86 inst->alg.base.cra_priority = alg->base.cra_priority;
87 inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
88 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
89 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
91 inst->alg.setkey = aead_geniv_setkey;
92 inst->alg.setauthsize = aead_geniv_setauthsize;
94 inst->alg.ivsize = ivsize;
95 inst->alg.maxauthsize = maxauthsize;
97 inst->free = aead_geniv_free;
103 aead_geniv_free(inst);
107 EXPORT_SYMBOL_GPL(aead_geniv_alloc);
109 int aead_init_geniv(struct crypto_aead *aead)
111 struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead);
112 struct aead_instance *inst = aead_alg_instance(aead);
113 struct crypto_aead *child;
116 spin_lock_init(&ctx->lock);
118 err = crypto_get_default_rng();
122 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
123 crypto_aead_ivsize(aead));
124 crypto_put_default_rng();
128 ctx->sknull = crypto_get_default_null_skcipher();
129 err = PTR_ERR(ctx->sknull);
130 if (IS_ERR(ctx->sknull))
133 child = crypto_spawn_aead(aead_instance_ctx(inst));
134 err = PTR_ERR(child);
139 crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) +
140 sizeof(struct aead_request));
148 crypto_put_default_null_skcipher();
151 EXPORT_SYMBOL_GPL(aead_init_geniv);
153 void aead_exit_geniv(struct crypto_aead *tfm)
155 struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
157 crypto_free_aead(ctx->child);
158 crypto_put_default_null_skcipher();
160 EXPORT_SYMBOL_GPL(aead_exit_geniv);
162 MODULE_LICENSE("GPL");
163 MODULE_DESCRIPTION("Shared IV generator code");