Linux加密框架crypto AES代碼相關

?例子

  • aes_generic.c - crypto/aes_generic.c - Linux source code (v5.15.11) - Bootlin
static struct crypto_alg aes_alg = {.cra_name		=	"aes",.cra_driver_name	=	"aes-generic",.cra_priority		=	100,.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,.cra_blocksize		=	AES_BLOCK_SIZE,.cra_ctxsize		=	sizeof(struct crypto_aes_ctx),.cra_module		=	THIS_MODULE,.cra_u			=	{.cipher = {.cia_min_keysize	=	AES_MIN_KEY_SIZE,.cia_max_keysize	=	AES_MAX_KEY_SIZE,.cia_setkey		=	crypto_aes_set_key,.cia_encrypt		=	crypto_aes_encrypt,.cia_decrypt		=	crypto_aes_decrypt}}
};
  • 從上述定義可知AES算法的屬性信息,如下所示:
    • a)算法名為"aes",算法驅動名為"aes-generic"。
    • b)算法的優先級為100? 使用的時候,如未特殊指定,按照優先級高低進行使用
    • c)算法的分組長度為AES_BLOCK_SIZE(16)字節
    • d)算法類型為CRYPTO_ALG_TYPE_CIPHER(即分組算法),其個性化屬性數據結構為struct cipher_alg,定義如下所示:

cipher_alg

/*** DOC: Block Cipher Algorithm Definitions** These data structures define modular crypto algorithm implementations,* managed via crypto_register_alg() and crypto_unregister_alg().*//*** struct cipher_alg - single-block symmetric ciphers definition* @cia_min_keysize: Minimum key size supported by the transformation. This is*		     the smallest key length supported by this transformation*		     algorithm. This must be set to one of the pre-defined*		     values as this is not hardware specific. Possible values*		     for this field can be found via git grep "_MIN_KEY_SIZE"*		     include/crypto/* @cia_max_keysize: Maximum key size supported by the transformation. This is*		    the largest key length supported by this transformation*		    algorithm. This must be set to one of the pre-defined values*		    as this is not hardware specific. Possible values for this*		    field can be found via git grep "_MAX_KEY_SIZE"*		    include/crypto/* @cia_setkey: Set key for the transformation. This function is used to either*	        program a supplied key into the hardware or store the key in the*	        transformation context for programming it later. Note that this*	        function does modify the transformation context. This function*	        can be called multiple times during the existence of the*	        transformation object, so one must make sure the key is properly*	        reprogrammed into the hardware. This function is also*	        responsible for checking the key length for validity.* @cia_encrypt: Encrypt a single block. This function is used to encrypt a*		 single block of data, which must be @cra_blocksize big. This*		 always operates on a full @cra_blocksize and it is not possible*		 to encrypt a block of smaller size. The supplied buffers must*		 therefore also be at least of @cra_blocksize size. Both the*		 input and output buffers are always aligned to @cra_alignmask.*		 In case either of the input or output buffer supplied by user*		 of the crypto API is not aligned to @cra_alignmask, the crypto*		 API will re-align the buffers. The re-alignment means that a*		 new buffer will be allocated, the data will be copied into the*		 new buffer, then the processing will happen on the new buffer,*		 then the data will be copied back into the original buffer and*		 finally the new buffer will be freed. In case a software*		 fallback was put in place in the @cra_init call, this function*		 might need to use the fallback if the algorithm doesn't support*		 all of the key sizes. In case the key was stored in*		 transformation context, the key might need to be re-programmed*		 into the hardware in this function. This function shall not*		 modify the transformation context, as this function may be*		 called in parallel with the same transformation object.* @cia_decrypt: Decrypt a single block. This is a reverse counterpart to*		 @cia_encrypt, and the conditions are exactly the same.** All fields are mandatory and must be filled.*/
struct cipher_alg {unsigned int cia_min_keysize;unsigned int cia_max_keysize;int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,unsigned int keylen);void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
};

分組算法個性化屬性包括2個參數和3個算法 接口,如上所示:

2個參數

  • 分組算法輸入密鑰長度的下限cia_min_keysize和上限cia_max_keysize
  • AES算法密鑰長度的下限和上限分別為AES_MIN_KEY_SIZE(16)、AES_MAX_KEY_SIZE(32),但實際上AES算法只支持16B(128bit)、24B(192bit)和32B(256bit)三種密鑰長度,如果輸入密鑰的長度為其他值,在進行密鑰擴展(crypto_aes_expand_key)時將返回參數錯誤,如下所示:

三個算法接口?

  • ?分組算法的算法接口,包括密鑰設置接口cia_setkey、加密接口cia_encrypt和解密接口cia_decrypt,算法運行的上下文空間由算法實例tfm提供。
  • AES算法的三個算法接口分別為crypto_aes_set_key、aes_encrypt和aes_decrypt。從AES算法加密接口和解密接口的實現流程來看,每次處理一個分組(16B)的數據。
  • AES算法運行的上下文空間是數據結構struct crypto_aes_ctx的一個實例,該數據結構定義(root/include/crypto/aes.h)如下所示:
  • aes.h - include/crypto/aes.h - Linux source code (v5.15.11) - Bootlin

crypto_aes_ctx

/** Please ensure that the first two fields are 16-byte aligned* relative to the start of the structure, i.e., don't move them!*/
struct crypto_aes_ctx {u32 key_enc[AES_MAX_KEYLENGTH_U32];u32 key_dec[AES_MAX_KEYLENGTH_U32];u32 key_length;
};
  • AES算法上下文數據結構包括密鑰擴展后的加密密鑰key_enc和解密密鑰key_dec以及輸入的密鑰長度key_length。
  • 注:由于算法應用不會直接使用AES算法的算法接口,因此其算法說明aes_alg未設置算法類型常量cra_type。?

crypto_aes_set_key?

aes_expandkey

  • aes.c - lib/crypto/aes.c - Linux source code (v5.15.11) - Bootlin
/*** aes_expandkey - Expands the AES key as described in FIPS-197* @ctx:	The location where the computed key will be stored.* @in_key:	The supplied key.* @key_len:	The length of the supplied key.** Returns 0 on success. The function fails only if an invalid key size (or* pointer) is supplied.* The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes* key schedule plus a 16 bytes key which is used before the first round).* The decryption key is prepared for the "Equivalent Inverse Cipher" as* described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is* for the initial combination, the second slot for the first round and so on.*/
int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,unsigned int key_len)
{u32 kwords = key_len / sizeof(u32);u32 rc, i, j;int err;err = aes_check_keylen(key_len);if (err)return err;ctx->key_length = key_len;for (i = 0; i < kwords; i++)ctx->key_enc[i] = get_unaligned_le32(in_key + i * sizeof(u32));for (i = 0, rc = 1; i < 10; i++, rc = mul_by_x(rc)) {u32 *rki = ctx->key_enc + (i * kwords);u32 *rko = rki + kwords;rko[0] = ror32(subw(rki[kwords - 1]), 8) ^ rc ^ rki[0];rko[1] = rko[0] ^ rki[1];rko[2] = rko[1] ^ rki[2];rko[3] = rko[2] ^ rki[3];if (key_len == AES_KEYSIZE_192) {if (i >= 7)break;rko[4] = rko[3] ^ rki[4];rko[5] = rko[4] ^ rki[5];} else if (key_len == AES_KEYSIZE_256) {if (i >= 6)break;rko[4] = subw(rko[3]) ^ rki[4];rko[5] = rko[4] ^ rki[5];rko[6] = rko[5] ^ rki[6];rko[7] = rko[6] ^ rki[7];}}/** Generate the decryption keys for the Equivalent Inverse Cipher.* This involves reversing the order of the round keys, and applying* the Inverse Mix Columns transformation to all but the first and* the last one.*/ctx->key_dec[0] = ctx->key_enc[key_len + 24];ctx->key_dec[1] = ctx->key_enc[key_len + 25];ctx->key_dec[2] = ctx->key_enc[key_len + 26];ctx->key_dec[3] = ctx->key_enc[key_len + 27];for (i = 4, j = key_len + 20; j > 0; i += 4, j -= 4) {ctx->key_dec[i]     = inv_mix_columns(ctx->key_enc[j]);ctx->key_dec[i + 1] = inv_mix_columns(ctx->key_enc[j + 1]);ctx->key_dec[i + 2] = inv_mix_columns(ctx->key_enc[j + 2]);ctx->key_dec[i + 3] = inv_mix_columns(ctx->key_enc[j + 3]);}ctx->key_dec[i]     = ctx->key_enc[0];ctx->key_dec[i + 1] = ctx->key_enc[1];ctx->key_dec[i + 2] = ctx->key_enc[2];ctx->key_dec[i + 3] = ctx->key_enc[3];return 0;
}
EXPORT_SYMBOL(aes_expandkey);

aes_encrypt?

  • aes.c - lib/crypto/aes.c - Linux source code (v5.15.11) - Bootlin
/*** aes_encrypt - Encrypt a single AES block* @ctx:	Context struct containing the key schedule* @out:	Buffer to store the ciphertext* @in:		Buffer containing the plaintext*/
void aes_encrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in)
{const u32 *rkp = ctx->key_enc + 4;int rounds = 6 + ctx->key_length / 4;u32 st0[4], st1[4];int round;st0[0] = ctx->key_enc[0] ^ get_unaligned_le32(in);st0[1] = ctx->key_enc[1] ^ get_unaligned_le32(in + 4);st0[2] = ctx->key_enc[2] ^ get_unaligned_le32(in + 8);st0[3] = ctx->key_enc[3] ^ get_unaligned_le32(in + 12);/** Force the compiler to emit data independent Sbox references,* by xoring the input with Sbox values that are known to add up* to zero. This pulls the entire Sbox into the D-cache before any* data dependent lookups are done.*/st0[0] ^= aes_sbox[ 0] ^ aes_sbox[ 64] ^ aes_sbox[134] ^ aes_sbox[195];st0[1] ^= aes_sbox[16] ^ aes_sbox[ 82] ^ aes_sbox[158] ^ aes_sbox[221];st0[2] ^= aes_sbox[32] ^ aes_sbox[ 96] ^ aes_sbox[160] ^ aes_sbox[234];st0[3] ^= aes_sbox[48] ^ aes_sbox[112] ^ aes_sbox[186] ^ aes_sbox[241];for (round = 0;; round += 2, rkp += 8) {st1[0] = mix_columns(subshift(st0, 0)) ^ rkp[0];st1[1] = mix_columns(subshift(st0, 1)) ^ rkp[1];st1[2] = mix_columns(subshift(st0, 2)) ^ rkp[2];st1[3] = mix_columns(subshift(st0, 3)) ^ rkp[3];if (round == rounds - 2)break;st0[0] = mix_columns(subshift(st1, 0)) ^ rkp[4];st0[1] = mix_columns(subshift(st1, 1)) ^ rkp[5];st0[2] = mix_columns(subshift(st1, 2)) ^ rkp[6];st0[3] = mix_columns(subshift(st1, 3)) ^ rkp[7];}put_unaligned_le32(subshift(st1, 0) ^ rkp[4], out);put_unaligned_le32(subshift(st1, 1) ^ rkp[5], out + 4);put_unaligned_le32(subshift(st1, 2) ^ rkp[6], out + 8);put_unaligned_le32(subshift(st1, 3) ^ rkp[7], out + 12);
}
EXPORT_SYMBOL(aes_encrypt);

aes_decrypt?

  • aes.c - lib/crypto/aes.c - Linux source code (v5.15.11) - Bootlin
/*** aes_decrypt - Decrypt a single AES block* @ctx:	Context struct containing the key schedule* @out:	Buffer to store the plaintext* @in:		Buffer containing the ciphertext*/
void aes_decrypt(const struct crypto_aes_ctx *ctx, u8 *out, const u8 *in)
{const u32 *rkp = ctx->key_dec + 4;int rounds = 6 + ctx->key_length / 4;u32 st0[4], st1[4];int round;st0[0] = ctx->key_dec[0] ^ get_unaligned_le32(in);st0[1] = ctx->key_dec[1] ^ get_unaligned_le32(in + 4);st0[2] = ctx->key_dec[2] ^ get_unaligned_le32(in + 8);st0[3] = ctx->key_dec[3] ^ get_unaligned_le32(in + 12);/** Force the compiler to emit data independent Sbox references,* by xoring the input with Sbox values that are known to add up* to zero. This pulls the entire Sbox into the D-cache before any* data dependent lookups are done.*/st0[0] ^= aes_inv_sbox[ 0] ^ aes_inv_sbox[ 64] ^ aes_inv_sbox[129] ^ aes_inv_sbox[200];st0[1] ^= aes_inv_sbox[16] ^ aes_inv_sbox[ 83] ^ aes_inv_sbox[150] ^ aes_inv_sbox[212];st0[2] ^= aes_inv_sbox[32] ^ aes_inv_sbox[ 96] ^ aes_inv_sbox[160] ^ aes_inv_sbox[236];st0[3] ^= aes_inv_sbox[48] ^ aes_inv_sbox[112] ^ aes_inv_sbox[187] ^ aes_inv_sbox[247];for (round = 0;; round += 2, rkp += 8) {st1[0] = inv_mix_columns(inv_subshift(st0, 0)) ^ rkp[0];st1[1] = inv_mix_columns(inv_subshift(st0, 1)) ^ rkp[1];st1[2] = inv_mix_columns(inv_subshift(st0, 2)) ^ rkp[2];st1[3] = inv_mix_columns(inv_subshift(st0, 3)) ^ rkp[3];if (round == rounds - 2)break;st0[0] = inv_mix_columns(inv_subshift(st1, 0)) ^ rkp[4];st0[1] = inv_mix_columns(inv_subshift(st1, 1)) ^ rkp[5];st0[2] = inv_mix_columns(inv_subshift(st1, 2)) ^ rkp[6];st0[3] = inv_mix_columns(inv_subshift(st1, 3)) ^ rkp[7];}put_unaligned_le32(inv_subshift(st1, 0) ^ rkp[4], out);put_unaligned_le32(inv_subshift(st1, 1) ^ rkp[5], out + 4);put_unaligned_le32(inv_subshift(st1, 2) ^ rkp[6], out + 8);put_unaligned_le32(inv_subshift(st1, 3) ^ rkp[7], out + 12);
}
EXPORT_SYMBOL(aes_decrypt);

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

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

相關文章

python語言print函數_Python 的 print 函數

Python 2.x 系列已經停止維護了&#xff0c; python 3.x 系列正在成為主流&#xff0c;盡管有些項目還是python2.x 的&#xff0c;之后寫Python 代碼為了保持兼容性&#xff0c;還是盡量和Python 3 標準保持一致作為一個Python newbee 而言&#xff0c; python 2.x 和 3.x 的 …

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

加密框架將算法的屬性抽象為算法說明數據結構struct crypto_alg&#xff0c;加密框架中的每一個算法&#xff08;基礎算法和衍生算法&#xff09;都表示為一個算法說明數據結構的實例&#xff0c;因此將struct crypto_alg稱為通用算法說明數據結構。后續章節中如無特殊說明&…

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

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

Linux加密框架 crypto RC4

參考鏈接 arc4.h Linux加密框架中的主要數據結構&#xff08;一&#xff09;_家有一希的博客-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的方法

原文件&#xff1a;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,原文件數據比較多&#xff0c;是一個125行&#xff0c;45類float數字。代碼&#xff1a;# -*- coding…

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

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

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

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

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

參考鏈接 Linux加密框架 crypto 哈希算法說明 同步哈希shash_alg | 異步哈希 ahash_alg | 通用部分抽象 hash_alg_common_CHYabc123456hh的博客-CSDN博客Linux加密框架中的主要數據結構&#xff08;二&#xff09;_家有一希的博客-CSDN博客 MD5 md5.h - include/crypto/md5.h …

事務沒提交的數據查的出來嗎?_“金三銀四”面試官:說說事務的ACID,什么是臟讀、幻讀?...

一、事務事務是數據庫管理系統執行過程中的一個邏輯單位&#xff0c;由一個有限的數據庫操作序列構成。--摘自百科在MySQL里&#xff0c;事務是在引擎層面實現&#xff0c;比如MyIsam不支持&#xff0c;InnoDB支持面試清單&#xff08;Java崗&#xff09;&#xff1a;JavaJVM數…

Linux加密框架 crypto 算法模板

參考鏈接 Linux加密框架中的主要數據結構&#xff08;三&#xff09;_家有一希的博客-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中有很好的文檔記錄。后一個函數很短&#xff0c;可以很容易地修改…

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

參考鏈接 Linux加密框架中的主要數據結構&#xff08;三&#xff09;_家有一希的博客-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:字符串相乘

算法的重要性&#xff0c;我就不多說了吧&#xff0c;想去大廠&#xff0c;就必須要經過基礎知識和業務邏輯面試算法面試。所以&#xff0c;為了提高大家的算法能力&#xff0c;這個公眾號后續每天帶大家做一道算法題&#xff0c;題目就從LeetCode上面選 &#xff01;今天和大家…

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

參考鏈接 Linux加密框架中的主要數據結構&#xff08;三&#xff09;_家有一希的博客-CSDN博客Linux加密框架 crypto 算法模板_CHYabc123456hh的博客-CSDN博客 HMAC算法模板 hmac.c - crypto/hmac.c - Linux source code (v5.15.11) - Bootlinhmac.c - crypto/hmac.c - Linux…

判斷非負整數是否是3的倍數_五年級數學因數與倍數知識點匯總與解題方法技巧...

在日常教學過程中&#xff0c;我發現孩子們和某些家長對學習數學的方法有一些誤區&#xff0c;就是覺著數學&#xff0c;單純就是邏輯思維&#xff0c;只要多做練習題就能學好&#xff0c;但是不是這樣的&#xff0c;低年級的學生&#xff0c;學習數學還是以背誦為主&#xff0…

tcp通訊一次最多能發送多少數據?_關于TCP/IP,必須知道的十個知識點

本文整理了一些TCP/IP協議簇中需要必知必會的十大問題&#xff0c;既是面試高頻問題&#xff0c;又是程序員必備基礎素養。一、TCP/IP模型TCP/IP協議模型&#xff08;Transmission Control Protocol/Internet Protocol&#xff09;&#xff0c;包含了一系列構成互聯網基礎的網絡…

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.我們…