Files
bishe-STM32/Hardware/aes.h
T
2026-05-01 11:15:54 +08:00

66 lines
1.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef _AES_H_
#define _AES_H_
#include <stdint.h>
#include <stddef.h>
// 使用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字节密钥
*/
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
/**
* @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的倍数)
* @param iv 初始化向量(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的倍数)
* @param iv 初始化向量(16字节)
*/
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字节初始化向量
*/
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
#endif // _AES_H_