64 lines
1.7 KiB
C
64 lines
1.7 KiB
C
#ifndef _AES_H_
|
||
#define _AES_H_
|
||
|
||
#include <stdint.h>
|
||
#include <stddef.h>
|
||
|
||
// AES-128配置
|
||
#define AES_BLOCKLEN 16 // Block length in bytes (AES is 128-bit block)
|
||
#define AES_KEYLEN 16 // Key length in bytes (AES-128)
|
||
#define AES_keyExpSize 176 // Expanded key size (44 * 4 bytes)
|
||
|
||
struct AES_ctx
|
||
{
|
||
uint8_t RoundKey[AES_keyExpSize]; // 扩展密钥
|
||
uint8_t Iv[AES_BLOCKLEN]; // IV向量(独立存储,标准做法)
|
||
};
|
||
|
||
/**
|
||
* @brief 初始化AES上下文(仅密钥)
|
||
* @param ctx AES上下文结构
|
||
* @param key 16字节密钥
|
||
*/
|
||
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
|
||
|
||
/**
|
||
* @brief 初始化AES上下文(密钥+IV)
|
||
* @param ctx AES上下文结构
|
||
* @param key 16字节密钥
|
||
* @param iv 16字节初始化向量
|
||
*/
|
||
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
|
||
|
||
/**
|
||
* @brief AES-ECB加密(单个块)
|
||
* @param ctx AES上下文
|
||
* @param buf 要加密的数据(16字节块)
|
||
*/
|
||
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
|
||
|
||
/**
|
||
* @brief AES-ECB解密(单个块)
|
||
* @param ctx AES上下文
|
||
* @param buf 要解密的数据(16字节块)
|
||
*/
|
||
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
|
||
|
||
/**
|
||
* @brief AES-CBC加密(多个块)
|
||
* @param ctx AES上下文
|
||
* @param buf 要加密的数据
|
||
* @param length 数据长度(必须是16的倍数)
|
||
*/
|
||
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
|
||
|
||
/**
|
||
* @brief AES-CBC解密(多个块)
|
||
* @param ctx AES上下文
|
||
* @param buf 要解密的数据
|
||
* @param length 数据长度(必须是16的倍数)
|
||
*/
|
||
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
|
||
|
||
#endif // _AES_H_
|