#ifndef _AES_H_ #define _AES_H_ #include #include // 使用AES-128(16字节密钥) #define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only #define AES_KEYLEN 16 // Key length in bytes (AES-128) #define AES_keyExpSize 176 struct AES_ctx { uint8_t RoundKey[AES_keyExpSize]; uint8_t Iv[AES_BLOCKLEN]; // 独立的IV存储(标准做法,与OpenSSL兼容) }; /** * @brief 初始化AES上下文 * @param ctx AES上下文结构 * @param key 16字节密钥 */ // 函数作用:根据 16 字节密钥初始化 AES 上下文并生成轮密钥。 void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); /** * @brief AES-ECB加密(单个块) * @param ctx AES上下文 * @param buf 要加密的数据(16字节块) */ // 函数作用:使用 AES-ECB 模式加密单个 16 字节数据块。 void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf); /** * @brief AES-ECB解密(单个块) * @param ctx AES上下文 * @param buf 要解密的数据(16字节块) */ // 函数作用:使用 AES-ECB 模式解密单个 16 字节数据块。 void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf); /** * @brief AES-CBC加密(多个块) * @param ctx AES上下文 * @param buf 要加密的数据 * @param length 数据长度(必须是16的倍数) * @param iv 初始化向量(16字节) */ // 函数作用:使用上下文中的 IV 按 AES-CBC 模式加密指定长度的缓冲区。 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的倍数) * @param iv 初始化向量(16字节) */ // 函数作用:使用上下文中的 IV 按 AES-CBC 模式解密指定长度的缓冲区。 void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length); /** * @brief 初始化AES上下文(CBC模式) * @param ctx AES上下文结构 * @param key 16字节密钥 * @param iv 16字节初始化向量 */ // 函数作用:使用 16 字节密钥和 16 字节 IV 初始化 AES-CBC 上下文。 void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); #endif // _AES_H_