Linux加密框架 crypto 哈希算法说明 同步哈希shash_alg | 异步哈希 ahash_alg | 通用部分抽象 hash_alg_common

参考链接

  • Linux加密框架中的主要数据结构(二)_家有一希的博客-CSDN博客

定义

  • 通用算法说明数据结构crypto_alg的联合体成员变量cra_u中包含多种算法的个性化属性,如分组算法、块加密算法、压缩算法、伪随机数算法等,但不包含哈希算法的个性化属性
  • Linux加密框架crypto crypto_alg|cipher_alg数据结构|AES例子_CHYabc123456hh的博客-CSDN博客
  • 加密框架以通用算法说明数据结构crypto_alg为基类定义了哈希算法说明数据结构,根据算法实现不同哈希算法说明分为同步哈希(synchronous hash)算法说明数据结构struct shash_alg和异步哈希(asynchronous hash)算法说明数据结构struct ahash_alg。
  • 哈希算法 的 结构 需要派生继承自 通用数据结构 crypto_alg
  • hash.h - include/crypto/hash.h - Linux source code (v5.15.11) - Bootlin

同步哈希(synchronous hash)算法说明数据结构struct shash_alg

/*** struct shash_alg - synchronous message digest definition* @init: see struct ahash_alg* @update: see struct ahash_alg* @final: see struct ahash_alg* @finup: see struct ahash_alg* @digest: see struct ahash_alg* @export: see struct ahash_alg* @import: see struct ahash_alg* @setkey: see struct ahash_alg* @init_tfm: Initialize the cryptographic transformation object.*	      This function is called only once at the instantiation*	      time, right after the transformation context was*	      allocated. In case the cryptographic hardware has*	      some special requirements which need to be handled*	      by software, this function shall check for the precise*	      requirement of the transformation and put any software*	      fallbacks in place.* @exit_tfm: Deinitialize the cryptographic transformation object.*	      This is a counterpart to @init_tfm, used to remove*	      various changes set in @init_tfm.* @digestsize: see struct ahash_alg* @statesize: see struct ahash_alg* @descsize: Size of the operational state for the message digest. This state* 	      size is the memory size that needs to be allocated for*	      shash_desc.__ctx* @base: internally used*/
struct shash_alg {int (*init)(struct shash_desc *desc);int (*update)(struct shash_desc *desc, const u8 *data,unsigned int len);int (*final)(struct shash_desc *desc, u8 *out);int (*finup)(struct shash_desc *desc, const u8 *data,unsigned int len, u8 *out);int (*digest)(struct shash_desc *desc, const u8 *data,unsigned int len, u8 *out);int (*export)(struct shash_desc *desc, void *out);int (*import)(struct shash_desc *desc, const void *in);int (*setkey)(struct crypto_shash *tfm, const u8 *key,unsigned int keylen);int (*init_tfm)(struct crypto_shash *tfm);void (*exit_tfm)(struct crypto_shash *tfm);unsigned int descsize;/* These fields must match hash_alg_common. */unsigned int digestsize__attribute__ ((aligned(__alignof__(struct hash_alg_common))));unsigned int statesize;struct crypto_alg base;
};

异步哈希(asynchronous hash)算法说明数据结构struct ahash_alg

/*** struct ahash_alg - asynchronous message digest definition* @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the*	  state of the HASH transformation at the beginning. This shall fill in*	  the internal structures used during the entire duration of the whole*	  transformation. No data processing happens at this point. Driver code*	  implementation must not use req->result.* @update: **[mandatory]** Push a chunk of data into the driver for transformation. This*	   function actually pushes blocks of data from upper layers into the*	   driver, which then passes those to the hardware as seen fit. This*	   function must not finalize the HASH transformation by calculating the*	   final message digest as this only adds more data into the*	   transformation. This function shall not modify the transformation*	   context, as this function may be called in parallel with the same*	   transformation object. Data processing can happen synchronously*	   [SHASH] or asynchronously [AHASH] at this point. Driver must not use*	   req->result.* @final: **[mandatory]** Retrieve result from the driver. This function finalizes the*	   transformation and retrieves the resulting hash from the driver and*	   pushes it back to upper layers. No data processing happens at this*	   point unless hardware requires it to finish the transformation*	   (then the data buffered by the device driver is processed).* @finup: **[optional]** Combination of @update and @final. This function is effectively a*	   combination of @update and @final calls issued in sequence. As some*	   hardware cannot do @update and @final separately, this callback was*	   added to allow such hardware to be used at least by IPsec. Data*	   processing can happen synchronously [SHASH] or asynchronously [AHASH]*	   at this point.* @digest: Combination of @init and @update and @final. This function*	    effectively behaves as the entire chain of operations, @init,*	    @update and @final issued in sequence. Just like @finup, this was*	    added for hardware which cannot do even the @finup, but can only do*	    the whole transformation in one run. Data processing can happen*	    synchronously [SHASH] or asynchronously [AHASH] at this point.* @setkey: Set optional key used by the hashing algorithm. Intended to push*	    optional key used by the hashing algorithm from upper layers into*	    the driver. This function can store the key in the transformation*	    context or can outright program it into the hardware. In the former*	    case, one must be careful to program the key into the hardware at*	    appropriate time and one must be careful that .setkey() can be*	    called multiple times during the existence of the transformation*	    object. Not  all hashing algorithms do implement this function as it*	    is only needed for keyed message digests. SHAx/MDx/CRCx do NOT*	    implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement*	    this function. This function must be called before any other of the*	    @init, @update, @final, @finup, @digest is called. No data*	    processing happens at this point.* @export: Export partial state of the transformation. This function dumps the*	    entire state of the ongoing transformation into a provided block of*	    data so it can be @import 'ed back later on. This is useful in case*	    you want to save partial result of the transformation after*	    processing certain amount of data and reload this partial result*	    multiple times later on for multiple re-use. No data processing*	    happens at this point. Driver must not use req->result.* @import: Import partial state of the transformation. This function loads the*	    entire state of the ongoing transformation from a provided block of*	    data so the transformation can continue from this point onward. No*	    data processing happens at this point. Driver must not use*	    req->result.* @init_tfm: Initialize the cryptographic transformation object.*	      This function is called only once at the instantiation*	      time, right after the transformation context was*	      allocated. In case the cryptographic hardware has*	      some special requirements which need to be handled*	      by software, this function shall check for the precise*	      requirement of the transformation and put any software*	      fallbacks in place.* @exit_tfm: Deinitialize the cryptographic transformation object.*	      This is a counterpart to @init_tfm, used to remove*	      various changes set in @init_tfm.* @halg: see struct hash_alg_common*/
struct ahash_alg {int (*init)(struct ahash_request *req);int (*update)(struct ahash_request *req);int (*final)(struct ahash_request *req);int (*finup)(struct ahash_request *req);int (*digest)(struct ahash_request *req);int (*export)(struct ahash_request *req, void *out);int (*import)(struct ahash_request *req, const void *in);int (*setkey)(struct crypto_ahash *tfm, const u8 *key,unsigned int keylen);int (*init_tfm)(struct crypto_ahash *tfm);void (*exit_tfm)(struct crypto_ahash *tfm);struct hash_alg_common halg;
};

算法接口

  • init:      三段式调用的初始化接口;
  • update:三段式调用的计算更新接口;
  • final:    三段式调用的结束(输出)接口;
  • finup:   两段式调用的计算更新和结束(输出)接口;   是将final和update合在一起的
  • digest:  一段式调用的摘要计算接口;
  • export: 上下文环境导出接口;
  • import: 上下文环境导入接口;
  • setkey: HMAC密钥设置接口。
  • init_tfm:初始化加密转换对象
  • exit_tfm:  取消初始化加密转换对象

汇总

  • 同步哈希算法说明数据结构中的算法接口为哈希算法接口全集,包括最小集的三段式调用接口(init、update和final),也包括在最小集基础上衍生出来的两段式调用接口(init和finup)以及一段式调用接口(digest)。
  • 每种哈希算法只需要实现算法接口的最小集(init、update和final)即可,即实现三段式调用接口即可,在注册算法时将使用默认的算法接口作为算法未定义接口的实现。
  • 以MD5算法为例,其算法说明只定义了init、update和final三段式调用接口,未定义finup和digest等接口,这样在注册算法时将使用同步哈希算法默认接口shash_finup_unaligned和shash_diget_unaligned作为MD5算法的finup和digest等接口的实现
  • 分析代码,shash_finup_unaligned和shash_diget_unaligned等接口都是在算法已实现的三段式调用接口基础上实现具体功能的。同步哈希算法的上下文运行空间由同步哈希算法描述符desc提供。
  • 静态分组算法和动态分组算法(即块加密算法)对应不同的个性化属性数据结构不同,静态哈希算法和动态哈希算法(即HMAC算法)对应相同的个性化属性数据结构。
  • 个性化属性数据结构中的算法接口是静态哈希算法和动态哈希算法的算法接口合集,如静态哈希算法不用实现setkey接口(将默认的shash_no_setkey作为setkey接口实现),而HMAC算法是与密钥相关的,涉及到密钥输入必须实现setkey接口。由于在分时分段计算(如HMAC运算)中,需要更新或切换哈希算法的上下文环境(与具体哈希算法实现相关),因此哈希算法还必须实现import和export两个接口。在加密框架支持的哈希算法中只有MD4算法未import和export两个接口,因此MD4算法无法支持分时分段调用,也无法实现HMAC运算
  • 注:所谓同步指发出一个功能调用时,在没有得到结果之前,该调用不会返回。当一个异步调用发出后,需要其他部件协作或需要等待一段时间,因此调用者不能立刻得到结果,但调用会立刻返回。等处理完成后,由部件通过状态通知和回调来通知调用者
  • 注:加密框架将同步哈希算法说明和异步哈希算法说明的通用部分抽象为数据结构hash_alg_common,如下所示,其成员变量与数据结构struct shash_alg最后三个成员变量相同。   最新版文直接将 这三个成员变量使用封装好的结构体 hash_alg_common 进行替代

同步和异步通用部分抽象   hash_alg_common

  • hash.h - include/crypto/hash.h - Linux source code (v5.15.11) - Bootlin
/*** struct hash_alg_common - define properties of message digest* @digestsize: Size of the result of the transformation. A buffer of this size*	        must be available to the @final and @finup calls, so they can*	        store the resulting hash into it. For various predefined sizes,*	        search include/crypto/ using*	        git grep _DIGEST_SIZE include/crypto.* @statesize: Size of the block for partial state of the transformation. A*	       buffer of this size must be passed to the @export function as it*	       will save the partial state of the transformation into it. On the*	       other side, the @import function will load the state from a*	       buffer of this size as well.* @base: Start of data structure of cipher algorithm. The common data*	  structure of crypto_alg contains information common to all ciphers.*	  The hash_alg_common data structure now adds the hash-specific*	  information.*/
struct hash_alg_common {unsigned int digestsize;unsigned int statesize;struct crypto_alg base;
};
  • hash_alg_common内部包含crypto_alg结构体,这个结构体是通用密码学密文统一的结构体,哈希在其通用的基础之上添加了 哈希独有的地方
  • crypto.h - include/linux/crypto.h - Linux source code (v5.15.11) - Bootlin
  • 通用结构体定义如下

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446100.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux加密框架 crypto 哈希算法举例 MD5

参考链接 Linux加密框架 crypto 哈希算法说明 同步哈希shash_alg | 异步哈希 ahash_alg | 通用部分抽象 hash_alg_common_CHYabc123456hh的博客-CSDN博客Linux加密框架中的主要数据结构(二)_家有一希的博客-CSDN博客 MD5 md5.h - include/crypto/md5.h …

事务没提交的数据查的出来吗?_“金三银四”面试官:说说事务的ACID,什么是脏读、幻读?...

一、事务事务是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成。--摘自百科在MySQL里,事务是在引擎层面实现,比如MyIsam不支持,InnoDB支持面试清单(Java岗):JavaJVM数…

Linux加密框架 crypto 算法模板

参考链接 Linux加密框架中的主要数据结构(三)_家有一希的博客-CSDN博客algapi.h - include/crypto/algapi.h - Linux source code (v5.15.11) - Bootlin 定义 struct crypto_template {struct list_head list;struct hlist_head instances;struct modu…

Linux加密框架 crypto 算法模板 CBC模板举例

参考链接 Linux加密框架中的主要数据结构(三)_家有一希的博客-CSDN博客https://blog.csdn.net/CHYabc123456hh/article/details/122194754 CBC算法模板 cbc.c - crypto/cbc.c - Linux source code (v5.15.11) - BootlinCBC算法模板属性 1)CBC算法模板名…

leetcode数组汇总_LeetCode刷题实战43:字符串相乘

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !今天和大家…

Linux加密框架 crypto 算法模板 HMAC模板举例

参考链接 Linux加密框架中的主要数据结构(三)_家有一希的博客-CSDN博客Linux加密框架 crypto 算法模板_CHYabc123456hh的博客-CSDN博客 HMAC算法模板 hmac.c - crypto/hmac.c - Linux source code (v5.15.11) - Bootlinhmac.c - crypto/hmac.c - Linux…

判断非负整数是否是3的倍数_五年级数学因数与倍数知识点汇总与解题方法技巧...

在日常教学过程中,我发现孩子们和某些家长对学习数学的方法有一些误区,就是觉着数学,单纯就是逻辑思维,只要多做练习题就能学好,但是不是这样的,低年级的学生,学习数学还是以背诵为主&#xff0…

tcp通讯一次最多能发送多少数据?_关于TCP/IP,必须知道的十个知识点

本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养。一、TCP/IP模型TCP/IP协议模型(Transmission Control Protocol/Internet Protocol),包含了一系列构成互联网基础的网络…

Linux内核crypto子系统的调用逻辑

testmgr.c - crypto/testmgr.c - Linux source code (v5.15.11) - Bootlin上述代码是内核内部即crypto子系统对外提供密码服务的测试程序调用流程&#xff1a;crypto API <—> crypto core <—> crypto_register_alg处于用户态的程序想要调用处于内核态的密码算法&…

Linux加密框架 crypto算法模板 以及CBC算法模板实例

参考链接 Linux加密框架中的主要数据结构&#xff08;四&#xff09;_家有一希的博客-CSDN博客algapi.h - include/crypto/algapi.h - Linux source code (v5.15.11) - Bootlin struct crypto_instance {struct crypto_alg alg;struct crypto_template *tmpl;union {/* Node i…

tomcat temp 大量 upload 文件_渗透测试之文件上传漏洞总结

文末下载上传环境源码客户端js检查一般都是在网页上写一段javascript脚本&#xff0c;校验上传文件的后缀名&#xff0c;有白名单形式也有黑名单形式。查看源代码可以看到有如下代码对上传文件类型进行了限制&#xff1a;我们可以看到对上传文件类型进行了限制。绕过方法1.我们…

Linux加密框架 crypto算法模板 以及HMAC算法模板实例

HMAC算法模板实例 HMAC算法模板的创建实例的接口是hmac_create函数hmac.c - crypto/hmac.c - Linux source code (v5.15.11) - Bootlin hmac_create输入的参数包括 算法模板 tmpl 和 算法模板实例参数 tbhmac_cretae函数返回的结果为0表示算法模板实例已经创建注册算法模…

linux加密框架 crypto 算法crypto_register_alg的注册流程

算法注册流程 静态算法模块初始化 分组算法模块初始化 AES算法模块&#xff08;aes_generic.c&#xff09;的初始化接口aes_init实现向加密框架注册AES算法的功能&#xff0c;如下所示。aes_generic.c - crypto/aes_generic.c - Linux source code (v5.15.12) - Bootlin sta…

linux加密框架 crypto 静态哈希算法crypto_register_shash注册流程

参考链接 Linux加密框架的算法管理&#xff08;一&#xff09;_家有一希的博客-CSDN博客_linux加密框架设计与实现shash.c - crypto/shash.c - Linux source code (v5.15.12) - Bootlin 函数介绍 crypto_register_shash函数实现向加密框架注册静态哈希算法的功能&#xff0c;…

多个线程访问统一对象的不同方法_C#多线程读写同一文件处理

在多线程访问读写同一个文件时&#xff0c;经常遇到异常&#xff1a;“文件正在由另一进程使用&#xff0c;因此该进程无法访问此文件”。多线程访问统一资源的异常&#xff0c;解决方案1&#xff0c;保证读写操作单线程执行&#xff0c;可以使用lock解决方案2&#xff0c;使用…

linux加密框架 crypto 通用算法注册接口__crypto_register_alg注册流程

函数介绍 __crypto_register_alg函数实现向加密框架注册算法&#xff08;包括静态算法和动态算法&#xff09;的功能&#xff0c;输入参数为算法说明alg&#xff0c;注册成功时返回算法注册用的算法幼虫larval&#xff0c;注册失败时返回失败原因。__crypto_register_alg函数执…

spark官方文档_Spark整合Ray思路漫谈

什么是Ray之前花了大概两到三天把Ray相关的论文&#xff0c;官网文档看了一遍&#xff0c;同时特意去找了一些中文资料看Ray当前在国内的发展情况(以及目前国内大部分人对Ray的认知程度)。先来简单介绍下我对Ray的认知。首先基因很重要&#xff0c;所以我们先需要探查下Ray最初…

linux加密框架 crypto 算法管理 - 算法查找接口 crypto_find_alg

算法查找接口crypto_find_alg 算法实例tfm是算法的一个可运行的副本&#xff0c;因此在创建算法实例前首先要查找确认算法是否已经注册有效&#xff0c;此时算法查找由函数crypto_find_alg实现。补充&#xff1a; struct crypto_tfm *tfm; crypto_tfm类型指针tfm可以理解为指代…

linux加密框架 crypto 算法管理 - 算法查找接口 crypto_alg_mod_lookup

参考链接 Linux加密框架的算法管理&#xff08;二&#xff09;_家有一希的博客-CSDN博客linux加密框架 crypto 算法管理 - 算法查找接口 crypto_find_alg_CHYabc123456hh的博客-CSDN博客 函数介绍 crypto_alg_mod_lookup函数输入参数包括待查找的算法名name、算法类型type和算…

qt triggered信号_Qt之网络编程UDP通信

点击上方“Qt学视觉”&#xff0c;选择“星标”公众号重磅干货&#xff0c;第一时间送达想要学习的同学们还请认真阅读每篇文章&#xff0c;相信你一定会有所收获UDP通信概述UDP(UserDatagramProtocol&#xff0c;用户数据报协议)是轻量的、不可靠的、面向数据报(datagram)、无…