HMAC算法模板实例
- HMAC算法模板的创建实例的接口是hmac_create函数
- hmac.c - crypto/hmac.c - Linux source code (v5.15.11) - Bootlin hmac_create
- 输入的参数包括 算法模板 tmpl 和 算法模板实例参数 tb
- hmac_cretae函数返回的结果为0表示算法模板实例已经创建注册
- 算法模板 tmpl
struct crypto_template {struct list_head list;struct hlist_head instances;struct module *module;int (*create)(struct crypto_template *tmpl, struct rtattr **tb);char name[CRYPTO_MAX_ALG_NAME];
};
- 算法模板实例参数 tb 显示如下
struct rtattr {unsigned short rta_len;unsigned short rta_type;
};
- hmac_cretae函数显示如下
static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
{struct shash_instance *inst;struct crypto_shash_spawn *spawn;struct crypto_alg *alg;struct shash_alg *salg;u32 mask;int err;int ds;int ss;err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);if (err)return err;inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);if (!inst)return -ENOMEM;spawn = shash_instance_ctx(inst);err = crypto_grab_shash(spawn, shash_crypto_instance(inst),crypto_attr_alg_name(tb[1]), 0, mask);if (err)goto err_free_inst;salg = crypto_spawn_shash_alg(spawn);alg = &salg->base;/* The underlying hash algorithm must not require a key */err = -EINVAL;if (crypto_shash_alg_needs_key(salg))goto err_free_inst;ds = salg->digestsize;ss = salg->statesize;if (ds > alg->cra_blocksize ||ss < alg->cra_blocksize)goto err_free_inst;err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);if (err)goto err_free_inst;inst->alg.base.cra_priority = alg->cra_priority;inst->alg.base.cra_blocksize = alg->cra_blocksize;inst->alg.base.cra_alignmask = alg->cra_alignmask;ss = ALIGN(ss, alg->cra_alignmask + 1);inst->alg.digestsize = ds;inst->alg.statesize = ss;inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +ALIGN(ss * 2, crypto_tfm_ctx_alignment());inst->alg.init = hmac_init;inst->alg.update = hmac_update;inst->alg.final = hmac_final;inst->alg.finup = hmac_finup;inst->alg.export = hmac_export;inst->alg.import = hmac_import;inst->alg.setkey = hmac_setkey;inst->alg.init_tfm = hmac_init_tfm;inst->alg.exit_tfm = hmac_exit_tfm;inst->free = shash_free_singlespawn_instance;err = shash_register_instance(tmpl, inst);if (err) {
err_free_inst:shash_free_singlespawn_instance(inst);}return err;
}
hmac_create函数创建并且注册的流程如下图所示
hmac_create函数的调用流程
参考链接
- Linux加密框架中的主要数据结构(四)_家有一希的博客-CSDN博客