Skip to content

Commit d9e1670

Browse files
committed
crypto: hmac - Use init_tfm/exit_tfm interface
This patch switches hmac over to the new init_tfm/exit_tfm interface as opposed to cra_init/cra_exit. This way the shash API can make sure that descsize does not exceed the maximum. This patch also adds the API helper shash_alg_instance. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Reviewed-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 9689569 commit d9e1670

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

‎crypto/hmac.c‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,11 @@ static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
138138
crypto_shash_finup(desc, out, ds, out);
139139
}
140140

141-
static int hmac_init_tfm(struct crypto_tfm *tfm)
141+
static int hmac_init_tfm(struct crypto_shash *parent)
142142
{
143-
struct crypto_shash *parent = __crypto_shash_cast(tfm);
144143
struct crypto_shash *hash;
145-
struct crypto_instance *inst = (void *)tfm->__crt_alg;
146-
struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
144+
struct shash_instance *inst = shash_alg_instance(parent);
145+
struct crypto_shash_spawn *spawn = shash_instance_ctx(inst);
147146
struct hmac_ctx *ctx = hmac_ctx(parent);
148147

149148
hash = crypto_spawn_shash(spawn);
@@ -152,18 +151,14 @@ static int hmac_init_tfm(struct crypto_tfm *tfm)
152151

153152
parent->descsize = sizeof(struct shash_desc) +
154153
crypto_shash_descsize(hash);
155-
if (WARN_ON(parent->descsize > HASH_MAX_DESCSIZE)) {
156-
crypto_free_shash(hash);
157-
return -EINVAL;
158-
}
159154

160155
ctx->hash = hash;
161156
return 0;
162157
}
163158

164-
static void hmac_exit_tfm(struct crypto_tfm *tfm)
159+
static void hmac_exit_tfm(struct crypto_shash *parent)
165160
{
166-
struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
161+
struct hmac_ctx *ctx = hmac_ctx(parent);
167162
crypto_free_shash(ctx->hash);
168163
}
169164

@@ -217,16 +212,15 @@ static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
217212
inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
218213
ALIGN(ss * 2, crypto_tfm_ctx_alignment());
219214

220-
inst->alg.base.cra_init = hmac_init_tfm;
221-
inst->alg.base.cra_exit = hmac_exit_tfm;
222-
223215
inst->alg.init = hmac_init;
224216
inst->alg.update = hmac_update;
225217
inst->alg.final = hmac_final;
226218
inst->alg.finup = hmac_finup;
227219
inst->alg.export = hmac_export;
228220
inst->alg.import = hmac_import;
229221
inst->alg.setkey = hmac_setkey;
222+
inst->alg.init_tfm = hmac_init_tfm;
223+
inst->alg.exit_tfm = hmac_exit_tfm;
230224

231225
err = shash_register_instance(tmpl, inst);
232226
if (err) {

‎include/crypto/internal/hash.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ static inline struct shash_instance *shash_instance(
220220
struct shash_instance, alg);
221221
}
222222

223+
static inline struct shash_instance *shash_alg_instance(
224+
struct crypto_shash *shash)
225+
{
226+
return shash_instance(crypto_tfm_alg_instance(&shash->base));
227+
}
228+
223229
static inline void *shash_instance_ctx(struct shash_instance *inst)
224230
{
225231
return crypto_instance_ctx(shash_crypto_instance(inst));

0 commit comments

Comments
 (0)