哈希算法应用示例(三)
- 创建异步哈希算法实例hmac_md5_ahash(二)
在crypto_alloc_tfm函数中,查找到符合条件的算法alg后,调用crypto_create_tfm函数根据查找到的算法alg(即hmac_md5_alg)和算法类型常量frontend(&crypto_ahash_type)创建指定类型的算法实例tfm(即hmac_md5_ahash)。注意:crypto_alloc_tfm函数并不解释新创建的算法实例的类型,其返回值为通用的内存地址(void *),由调用者将其转换为具体类型算法实例。
在crypto_create_tfm函数中,创建并初始化算法实例,具体流程如下:
1)申请内存空间
申请的内存空间包括通用算法实例、算法实例的个性化部分以及上下文空间,如下所示。
tfmsize = frontend->tfmsize;
total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
其中算法实例的个性化部分以及上下文空间均通过输入的算法类型常量frontend获取。此时输入的异步哈希算法类型常量crypto_ahash_type定义如下所示。
const struct crypto_type crypto_ahash_type = {
.extsize = crypto_ahash_extsize,