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.pswp.cn/news/446100.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446100.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446100.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python 列表間隔取值_python list數據等間隔抽取并新建list存儲的例子

原始數據如下:[e3cd, e547, e63d, 0ffd, e39b, e539, e5be, 0dd2, e3d6, e52e, e5f8, 0000, e404, e52b, e63d, 0312, e38b]將其分割為4路數據,分別存儲在fetal1、fetal2、mother1、ECG的列表中,各列表對齊,不能整除于4的數據舍去…

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…

python找最長的字符串_為Python找到最長重復字符串的有效方法(從Pearls編程)

我的解決方案是基于后綴數組。它是由最長公共前綴的兩倍前綴構成的。最壞情況下的復雜度是O(n(logn)^2)。任務”伊利亞特.mb.txt“在我的筆記本上花了4秒鐘。代碼在函數suffix_array和longest_common_substring中有很好的文檔記錄。后一個函數很短,可以很容易地修改…

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處于用戶態的程序想要調用處于內核態的密碼算法&…

python成語填空_python定期循環成語?

我有一個工作單位我希望每N秒發生一次.如果我使用簡單化minute 60while True:doSomeWork()time.sleep(minute)取決于doSomeWork()花費的時間,實際循環周期將是一分鐘加上那個時間.如果doSomeWork()所花費的時間不是確定性的,則工作周期更加難以預測.我想做的就是這樣minute 6…

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表示算法模板實例已經創建注冊算法模…

python判斷密碼強度并輸出_密碼強度判斷

[python]代碼庫def pdsz(cd):nnnn Falsefor c in cd:if c.isnumeric():nnnn Truebreakreturn nnnndef pdzm(cd):nnnn Falsefor c in cd:if c.isupper():nnnn Truebreakreturn nnnndef pdhh(cd):nnnn Falsefor c in cd:if c.islower():nnnn Truebreakreturn nnnndef main(…

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…

python 方法的實例_python調用自定義函數的實例操作

在python中&#xff0c;想要調用自定義函數必須先聲明&#xff0c;然后才能調用。使用函數時&#xff0c;只要按照函數定義的形式&#xff0c;向函數傳遞必需的參數&#xff0c;就可以調用函數完成相應的功能或者獲得函數返回的處理結果。(1)聲明函數python中使用 def 可以聲明…

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;使用…