This commit is contained in:
2026-05-21 08:07:16 +08:00
parent 90908cf018
commit 84d8874a38
32 changed files with 271 additions and 27 deletions
+18
View File
@@ -54,6 +54,7 @@ static const uint8_t Rcon[11] = {
#define getSBoxInvert(num) (rsbox[(num)])
// 密钥扩展
/* 函数作用:根据 AES-128 主密钥生成每轮加密使用的轮密钥。 */
static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
{
unsigned i, j, k;
@@ -102,6 +103,7 @@ static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
}
// AddRoundKey
/* 函数作用:将当前状态矩阵与指定轮密钥进行异或。 */
static void AddRoundKey(uint8_t round, uint8_t* state, const uint8_t* RoundKey)
{
uint8_t i, j;
@@ -113,6 +115,7 @@ static void AddRoundKey(uint8_t round, uint8_t* state, const uint8_t* RoundKey)
}
// SubBytes
/* 函数作用:使用 AES S 盒对状态矩阵进行字节替换。 */
static void SubBytes(uint8_t* state)
{
uint8_t i;
@@ -122,6 +125,7 @@ static void SubBytes(uint8_t* state)
}
// ShiftRows
/* 函数作用:执行 AES 加密流程中的行移位变换。 */
static void ShiftRows(uint8_t* state)
{
uint8_t temp;
@@ -150,12 +154,14 @@ static void ShiftRows(uint8_t* state)
}
// xtime (用于MixColumns)
/* 函数作用:在 AES 有限域中执行乘以 2 的运算。 */
static uint8_t xtime(uint8_t x)
{
return ((x << 1) ^ (((x >> 7) & 1) * 0x1b));
}
// MixColumns
/* 函数作用:执行 AES 加密流程中的列混淆变换。 */
static void MixColumns(uint8_t* state)
{
uint8_t i;
@@ -184,6 +190,7 @@ static void MixColumns(uint8_t* state)
}
// Cipher - AES加密核心
/* 函数作用:对 16 字节状态块执行完整 AES 加密。 */
static void Cipher(uint8_t* state, const uint8_t* RoundKey)
{
uint8_t round = 0;
@@ -205,6 +212,7 @@ static void Cipher(uint8_t* state, const uint8_t* RoundKey)
AddRoundKey(10, state, RoundKey);
}
/* 函数作用:在 AES 有限域中执行两个字节的乘法。 */
static uint8_t Multiply(uint8_t x, uint8_t y)
{
return (((y & 1) * x) ^
@@ -214,6 +222,7 @@ static uint8_t Multiply(uint8_t x, uint8_t y)
(((y >> 4) & 1) * xtime(xtime(xtime(xtime(x))))));
}
/* 函数作用:使用 AES 逆 S 盒对状态矩阵进行逆字节替换。 */
static void InvSubBytes(uint8_t* state)
{
uint8_t i;
@@ -222,6 +231,7 @@ static void InvSubBytes(uint8_t* state)
}
}
/* 函数作用:执行 AES 解密流程中的逆行移位变换。 */
static void InvShiftRows(uint8_t* state)
{
uint8_t temp;
@@ -245,6 +255,7 @@ static void InvShiftRows(uint8_t* state)
state[15] = temp;
}
/* 函数作用:执行 AES 解密流程中的逆列混淆变换。 */
static void InvMixColumns(uint8_t* state)
{
uint8_t i;
@@ -263,6 +274,7 @@ static void InvMixColumns(uint8_t* state)
}
}
/* 函数作用:对 16 字节状态块执行完整 AES 解密。 */
static void InvCipher(uint8_t* state, const uint8_t* RoundKey)
{
uint8_t round;
@@ -282,22 +294,26 @@ static void InvCipher(uint8_t* state, const uint8_t* RoundKey)
// ========== 公开API函数 ==========
/* 函数作用:初始化 AES 上下文并生成轮密钥。 */
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key)
{
KeyExpansion(ctx->RoundKey, key);
}
/* 函数作用:初始化 AES 上下文并设置 CBC 模式 IV。 */
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv)
{
KeyExpansion(ctx->RoundKey, key);
memcpy(ctx->Iv, iv, AES_BLOCKLEN);
}
/* 函数作用:对单个 16 字节数据块执行 AES-ECB 加密。 */
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf)
{
Cipher(buf, ctx->RoundKey);
}
/* 函数作用:使用 AES-CBC 模式加密指定长度的数据缓冲区。 */
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
{
size_t i;
@@ -317,11 +333,13 @@ void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
}
}
/* 函数作用:对单个 16 字节数据块执行 AES-ECB 解密。 */
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf)
{
InvCipher(buf, ctx->RoundKey);
}
/* 函数作用:使用 AES-CBC 模式解密指定长度的数据缓冲区。 */
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
{
size_t i;