Linux加密框架crypto crypto_alg|cipher_alg數據結構|AES例子

  • 加密框架將算法的屬性抽象為算法說明數據結構struct crypto_alg,加密框架中的每一個算法(基礎算法和衍生算法)都表示為一個算法說明數據結構的實例,因此將struct crypto_alg稱為通用算法說明數據結構
  • 后續章節中如無特殊說明,算法說明數據結構和通用算法數據結構均指的是struct crypto_alg
  • crypto.h - include/linux/crypto.h - Linux source code (v5.15.11) - Bootlin
/*** struct crypto_alg - definition of a cryptograpic cipher algorithm* @cra_flags: Flags describing this transformation. See include/linux/crypto.h*	       CRYPTO_ALG_* flags for the flags which go in here. Those are*	       used for fine-tuning the description of the transformation*	       algorithm.* @cra_blocksize: Minimum block size of this transformation. The size in bytes*		   of the smallest possible unit which can be transformed with*		   this algorithm. The users must respect this value.*		   In case of HASH transformation, it is possible for a smaller*		   block than @cra_blocksize to be passed to the crypto API for*		   transformation, in case of any other transformation type, an* 		   error will be returned upon any attempt to transform smaller*		   than @cra_blocksize chunks.* @cra_ctxsize: Size of the operational context of the transformation. This*		 value informs the kernel crypto API about the memory size*		 needed to be allocated for the transformation context.* @cra_alignmask: Alignment mask for the input and output data buffer. The data*		   buffer containing the input data for the algorithm must be*		   aligned to this alignment mask. The data buffer for the*		   output data must be aligned to this alignment mask. Note that*		   the Crypto API will do the re-alignment in software, but*		   only under special conditions and there is a performance hit.*		   The re-alignment happens at these occasions for different*		   @cra_u types: cipher -- For both input data and output data*		   buffer; ahash -- For output hash destination buf; shash --*		   For output hash destination buf.*		   This is needed on hardware which is flawed by design and*		   cannot pick data from arbitrary addresses.* @cra_priority: Priority of this transformation implementation. In case*		  multiple transformations with same @cra_name are available to*		  the Crypto API, the kernel will use the one with highest*		  @cra_priority.* @cra_name: Generic name (usable by multiple implementations) of the*	      transformation algorithm. This is the name of the transformation*	      itself. This field is used by the kernel when looking up the*	      providers of particular transformation.* @cra_driver_name: Unique name of the transformation provider. This is the*		     name of the provider of the transformation. This can be any*		     arbitrary value, but in the usual case, this contains the*		     name of the chip or provider and the name of the*		     transformation algorithm.* @cra_type: Type of the cryptographic transformation. This is a pointer to*	      struct crypto_type, which implements callbacks common for all*	      transformation types. There are multiple options, such as*	      &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type.*	      This field might be empty. In that case, there are no common*	      callbacks. This is the case for: cipher, compress, shash.* @cra_u: Callbacks implementing the transformation. This is a union of*	   multiple structures. Depending on the type of transformation selected*	   by @cra_type and @cra_flags above, the associated structure must be*	   filled with callbacks. This field might be empty. This is the case*	   for ahash, shash.* @cra_init: Initialize the cryptographic transformation object. This function*	      is used to 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.* @cra_exit: Deinitialize the cryptographic transformation object. This is a*	      counterpart to @cra_init, used to remove various changes set in*	      @cra_init.* @cra_u.cipher: Union member which contains a single-block symmetric cipher*		  definition. See @struct @cipher_alg.* @cra_u.compress: Union member which contains a (de)compression algorithm.*		    See @struct @compress_alg.* @cra_module: Owner of this transformation implementation. Set to THIS_MODULE* @cra_list: internally used* @cra_users: internally used* @cra_refcnt: internally used* @cra_destroy: internally used** @stats: union of all possible crypto_istat_xxx structures* @stats.aead:		statistics for AEAD algorithm* @stats.akcipher:	statistics for akcipher algorithm* @stats.cipher:	statistics for cipher algorithm* @stats.compress:	statistics for compress algorithm* @stats.hash:		statistics for hash algorithm* @stats.rng:		statistics for rng algorithm* @stats.kpp:		statistics for KPP algorithm** The struct crypto_alg describes a generic Crypto API algorithm and is common* for all of the transformations. Any variable not documented here shall not* be used by a cipher implementation as it is internal to the Crypto API.*/struct crypto_alg {struct list_head cra_list;struct list_head cra_users;u32 cra_flags;unsigned int cra_blocksize;unsigned int cra_ctxsize;unsigned int cra_alignmask;int cra_priority;atomic_t cra_refcnt;char cra_name[CRYPTO_MAX_ALG_NAME];char cra_driver_name[CRYPTO_MAX_ALG_NAME];const struct crypto_type *cra_type;union {struct ablkcipher_alg ablkcipher;struct aead_alg aead;struct blkcipher_alg blkcipher;struct cipher_alg cipher;struct compress_alg compress;struct rng_alg rng;} cra_u;int (*cra_init)(struct crypto_tfm *tfm);void (*cra_exit)(struct crypto_tfm *tfm);void (*cra_destroy)(struct crypto_alg *alg);struct module *cra_module;
}
struct crypto_alg {struct list_head cra_list;struct list_head cra_users;u32 cra_flags;unsigned int cra_blocksize;unsigned int cra_ctxsize;unsigned int cra_alignmask;int cra_priority;refcount_t cra_refcnt;char cra_name[CRYPTO_MAX_ALG_NAME];char cra_driver_name[CRYPTO_MAX_ALG_NAME];const struct crypto_type *cra_type;union {struct cipher_alg cipher;struct compress_alg compress;} cra_u;int (*cra_init)(struct crypto_tfm *tfm);void (*cra_exit)(struct crypto_tfm *tfm);void (*cra_destroy)(struct crypto_alg *alg);struct module *cra_module;#ifdef CONFIG_CRYPTO_STATSunion {struct crypto_istat_aead aead;struct crypto_istat_akcipher akcipher;struct crypto_istat_cipher cipher;struct crypto_istat_compress compress;struct crypto_istat_hash hash;struct crypto_istat_rng rng;struct crypto_istat_kpp kpp;} stats;
#endif /* CONFIG_CRYPTO_STATS */} CRYPTO_MINALIGN_ATTR;
  • ?內核版本 V5.15.1

數據結構struct crypto_alg中各成員變量含義如下所示,其中前綴cra為crypto_alg的縮寫

  • crypto_alg是個基類,任何算法都可以基于它派生出衍生類;每個算法都對應著一個struct crypto_alg實例,一般在module_init中調用crypto_register_alg接口將具體的crypto_alg對象添加到crypto_alg_list鏈表中。
  • 1)cra_list:算法管理鏈表節點,向加密框架注冊算法實際上就是將cra_list添加到全局的算法管理鏈表的過程,管理算法鏈表的表頭為crypto_alg_list
  • 2)cra_users:算法用戶鏈表表頭,將由算法根據算法模板創建的新算法視為本算法的一個用戶;此算法被引用的所有crypto_spawn實例鏈表。
  • 3)cra_flag:算法標志,包括算法狀態和算法類型等標志位,其中低4比特表示算法類型;
  • 4)cra_blocksize:算法分組長度,單位:字節;是單個處理數據塊大小
  • 5)cra_ctxsize:算法上下文空間大小,單位:字節;為transformation context大小
  • 6)cra_alignmask:算法輸入輸出數據地址對齊要求屏蔽位,alignmask+1表示地址對齊要求,如算法輸入輸出數據地址要求4字節對齊,則alignmask=3;
  • 7)cra_priority:算法優先級;
  • 8)cra_refcnt:算法引用計數;
  • 9)cra_name[CRYPTO_MAX_ALG_NAME]:算法名,最多為64個字符;
  • 10)cra_driver_name[CRYPTO_MAX_ALG_NAME]:算法驅動名,最多為64個字符。注冊時,如果未指定算法驅動名,則按“算法名-generic”規則定義算法驅動名;
  • 11)cra_type:算法類型,其數據類型為const,因此稱之為算法類型常量。如果算法能夠提供某種密碼服務,必須設置cra_type,并且與cra_flag中的算法類型保持一致;
  • 12)cra_u:算法個性化屬性,聯合體變量,其各成員變量含義如下:
    • a)ablkcipher:異步塊加密算法個性化屬性;
    • b)aead:認證加密算法個性化屬性;
    • c)blkcipher:塊加密算法個性化屬性;
    • d)cipher:分組算法個性化屬性;
    • e)compress:壓縮算法個性化屬性;
    • f)rng:偽隨機數算法個性化屬性;
    • 這里有一個很重要的數據成員cra_u,因為它體現了kernel crypto架構設計者的設計思想:它將四種比較常用的算法類型的處理方式抽象到基類當中,即如果你要添加的算法為這4類,就只需要實現這4類算法所對應的方法,如果不是這4類當中,就需要在基類上做派生,實現特定的crypto_type。具體內核版本不同? ?差異很大
  • 13)cra_init:算法實例初始化接口,由算法模板使用;
  • 14)cra_exit:算法實例析構接口,由算法模板使用;
  • 15)cra_destroy:算法說明實例的銷毀接口,由無驅動的算法說明實例使用,如算法幼蟲;
  • 16)cra_module:算法所屬的模塊,一般為THIS_MODULE,編譯時確定。
  • 算法說明數據結構的成員變量分為通用屬性成員變量和個性化屬性成員變量,通用屬性成員變量如cra_list、cra_users、cra_name、cra_driver_name等,個性化屬性成員變量指的是聯合體成員變量cra_u,包括算法接口和個性化參數等。為方便訪問個性化屬性成員變量,在crypto.h定義了一系列宏,如下所示。
#define cra_cipher	cra_u.cipher
#define cra_compress	cra_u.compress

請使用手機"掃一掃"x

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446104.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446104.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446104.shtml

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

相關文章

python如何運用ols_使用OLS回歸(Python,StatsModels,Pandas)預測未來值

我目前正試圖在Python中實現一個MLR,我不知道如何去應用我發現的未來值的系數。使用OLS回歸(Python,StatsModels,Pandas)預測未來值import pandas as pdimport statsmodels.formula.api as smimport statsmodels.api as sm2TV [230.1, 44.5,…

Linux加密框架 crypto RC4

參考鏈接 arc4.h Linux加密框架中的主要數據結構(一)_家有一希的博客-CSDN博客 頭文件 arc4.h - include/crypto/arc4.h - Linux source code (v5.15.11) - Bootlin實現代碼 arc4.c arc4.c - crypto/arc4.c - Linux source code (v5.15.11) - Bootlin…

python讀txt轉array_python將txt文件讀入為np.array的方法

原文件:7.8094,1.0804,5.7632,0.012269,0.008994,-0.003469,-0.79279,-0.064686,0.11635,0.68827,5.7169,7.9329,0.010264,0.003557,-0.011691,-0.57559,-0.56121,原文件數據比較多,是一個125行,45類float數字。代碼:# -*- coding…

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

參考鏈接 Linux加密框架中的主要數據結構(二)_家有一希的博客-CSDN博客 定義 通用算法說明數據結構crypto_alg的聯合體成員變量cra_u中包含多種算法的個性化屬性,如分組算法、塊加密算法、壓縮算法、偽隨機數算法等,但不包含哈希…

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(…