HC32L17x的LL驱动库之aes

#ifndef HC32L1XX_LL_AES_H_
#define HC32L1XX_LL_AES_H_

#ifdef __cplusplus
extern "C" {
#endif 
	
	#include "hc32l1xx.h"
	#include "hc32l1xx_ll_bus.h"
	///
	
	#define  LL_AES_KEY_128				AES_CR_KEYSIZE_128
	#define  LL_AES_KEY_192				AES_CR_KEYSIZE_192
	#define  LL_AES_KEY_256				AES_CR_KEYSIZE_256

	#define  LL_AES_KEY_0				0
	#define  LL_AES_KEY_1				1
	#define  LL_AES_KEY_2				2
	#define  LL_AES_KEY_3				3
	#define  LL_AES_KEY_4				4
	#define  LL_AES_KEY_5				5
	#define  LL_AES_KEY_6				6
	#define  LL_AES_KEY_7				7

	#define  LL_AES_DATA_0				0
	#define  LL_AES_DATA_1				1
	#define  LL_AES_DATA_2				2
	#define  LL_AES_DATA_3				3

	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline void LL_AES_Start(AES_TypeDef* AESx)
	{
		SET_BIT(AESx->CR, AES_CR_START);
	}
	
	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline uint32_t LL_AES_IsActiveFlag(AES_TypeDef* AESx)
	{
		return (uint32_t)(READ_BIT(AESx->CR, AES_CR_START)== AES_CR_START);
	}
	
	///
	//函		数: 
	//功		能: 加密运算
	//输入参	数: 
	//输出参	数: 
	//说		明: 
	//
	static inline void LL_AES_Enable_Encrypt(AES_TypeDef* AESx)
	{
		CLEAR_BIT(AESx->CR, AES_CR_MODE);
	}

	///
	//函		数: 
	//功		能: 解密运算
	//输入参	数: 
	//输出参	数: 
	//说		明: 
	//
	static inline void LL_AES_Enable_Decrypt(AES_TypeDef* AESx)
	{
		SET_BIT(AESx->CR, AES_CR_MODE);
	}

	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline void LL_AES_SetKeySize(AES_TypeDef* AESx,uint32_t keysize)
	{
		MODIFY_REG(AESx->CR, AES_CR_KEYSIZE, keysize);
	}
	
	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline uint32_t LL_AES_GetKeySize(AES_TypeDef* AESx)
	{
		return (uint32_t)READ_BIT(AESx->CR, AES_CR_KEYSIZE);
	}

	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline void LL_AES_SetKey(AES_TypeDef* AESx,uint32_t Key,uint32_t Index)
	{
		WRITE_REG(AESx->KEY[Index], Key);
	}

	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline uint32_t LL_AES_GetKey(AES_TypeDef* AESx,uint32_t Index)
	{
		return (uint32_t)READ_REG(AESx->KEY[Index]);
	}

	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline void LL_AES_SetData(AES_TypeDef* AESx, uint32_t Data, uint32_t Index)
	{
		WRITE_REG(AESx->DR[Index], Data);
	}
	
	///
	//函		数:
	//功		能:
	//输入参数:
	//输出参数:
	//说		明:
	//

	static inline uint32_t LL_AES_GetData(AES_TypeDef* AESx, uint32_t Index)
	{
		return (uint32_t)READ_REG(AESx->DR[Index]);
	}

	//===插队扫描设置
	typedef struct
	{
		uint32_t KeySize;				//---密钥大小
		uint32_t *Data;					//---加密结果或者解密结果
		uint32_t *Key;					//---密钥
		uint32_t *Cipher;				//---密文
	} LL_AES_InitTypeDef;

	//===函数定义
	uint8_t LL_AES_DeInit(AES_TypeDef *AESx);
	void LL_AES_StructInit(LL_AES_InitTypeDef* AES_InitStruct);
	uint8_t LL_AES_Encrypt(AES_TypeDef *AESx, LL_AES_InitTypeDef *AES_InitStruct);
	uint8_t LL_AES_Decrypt(AES_TypeDef* AESx, LL_AES_InitTypeDef* AES_InitStruct);

	///
#ifdef __cplusplus
}
#endif 

#endif /* HC32L1XX_LL_PCNT_H_ */

#include "hc32l1xx_ll_aes.h"

///
//函		数: 
//功		能: 
//输入参	数: 
//输出参	数: 
//说		明: 
//
uint8_t LL_AES_DeInit(AES_TypeDef *AESx)
{
	LL_PER0_GRP1_ForceReset(LL_PER0_GRP1_PERIPH_AES);
	LL_PER0_GRP1_ReleaseReset(LL_PER0_GRP1_PERIPH_AES);
	return 0;
}

///
//函		数: 
//功		能: 
//输入参	数: 
//输出参	数: 
//说		明: 
//

void LL_AES_StructInit(LL_AES_InitTypeDef* AES_InitStruct)
{
	AES_InitStruct->KeySize =LL_AES_KEY_128;
	AES_InitStruct->Data=NULL;
	AES_InitStruct->Key=NULL;
	AES_InitStruct->Cipher = NULL;
}

///
//函		数: 
//功		能: 加密运算
//输入参	数: 
//输出参	数: 
//说		明: 
//
uint8_t LL_AES_Encrypt(AES_TypeDef *AESx, LL_AES_InitTypeDef *AES_InitStruct)
{
	//---密钥配置
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_0], LL_AES_KEY_0);
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_1], LL_AES_KEY_1);
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_2], LL_AES_KEY_2);
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_3], LL_AES_KEY_3);
	//---密钥大小
	if ((AES_InitStruct->KeySize==1)||(AES_InitStruct->KeySize== LL_AES_KEY_192))
	{
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_4], LL_AES_KEY_4);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_5], LL_AES_KEY_5);
		LL_AES_SetKeySize(AESx, LL_AES_KEY_192);
	}
	else if ((AES_InitStruct->KeySize == 2) || (AES_InitStruct->KeySize == LL_AES_KEY_256))
	{
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_4], LL_AES_KEY_4);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_5], LL_AES_KEY_5);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_6], LL_AES_KEY_6);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_7], LL_AES_KEY_7);
		LL_AES_SetKeySize(AESx, LL_AES_KEY_256);
	}
	else
	{
		LL_AES_SetKeySize(AESx, LL_AES_KEY_128);
	}
	//---加密数据
	LL_AES_SetData(AESx, AES_InitStruct->Data[LL_AES_DATA_0], LL_AES_DATA_0);
	LL_AES_SetData(AESx, AES_InitStruct->Data[LL_AES_DATA_1], LL_AES_DATA_1);
	LL_AES_SetData(AESx, AES_InitStruct->Data[LL_AES_DATA_2], LL_AES_DATA_2);
	LL_AES_SetData(AESx, AES_InitStruct->Data[LL_AES_DATA_3], LL_AES_DATA_3);

	//---模式配置
	LL_AES_Enable_Encrypt(AESx);
	//---启动运算
	LL_AES_Start(AESx);
	//---等待运算完成
	while (1)
	{
		//---等待运算完成
		if (LL_AES_IsActiveFlag(AESx)==0)
		{
			break;
		}
	}
	//---获取密文
	AES_InitStruct->Cipher[LL_AES_DATA_0]=LL_AES_GetData(AESx, LL_AES_DATA_0);
	AES_InitStruct->Cipher[LL_AES_DATA_1]=LL_AES_GetData(AESx, LL_AES_DATA_1);
	AES_InitStruct->Cipher[LL_AES_DATA_2]=LL_AES_GetData(AESx, LL_AES_DATA_2);
	AES_InitStruct->Cipher[LL_AES_DATA_3]=LL_AES_GetData(AESx, LL_AES_DATA_3);
	return 0;
}

///
//函		数: 
//功		能: 解密运算
//输入参	数: 
//输出参	数: 
//说		明: 
//
uint8_t LL_AES_Decrypt(AES_TypeDef *AESx, LL_AES_InitTypeDef *AES_InitStruct)
{
	//---密钥配置
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_0], LL_AES_KEY_0);
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_1], LL_AES_KEY_1);
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_2], LL_AES_KEY_2);
	LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_3], LL_AES_KEY_3);
	//---密钥大小
	if ((AES_InitStruct->KeySize == 1) || (AES_InitStruct->KeySize == LL_AES_KEY_192))
	{
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_4], LL_AES_KEY_4);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_5], LL_AES_KEY_5);
		LL_AES_SetKeySize(AESx, LL_AES_KEY_192);
	}
	else if ((AES_InitStruct->KeySize == 2) || (AES_InitStruct->KeySize == LL_AES_KEY_256))
	{
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_4], LL_AES_KEY_4);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_5], LL_AES_KEY_5);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_6], LL_AES_KEY_6);
		LL_AES_SetKey(AESx, AES_InitStruct->Key[LL_AES_KEY_7], LL_AES_KEY_7);
		LL_AES_SetKeySize(AESx, LL_AES_KEY_256);
	}
	else
	{
		LL_AES_SetKeySize(AESx, LL_AES_KEY_128);
	}
	//---解密数据
	LL_AES_SetData(AESx, AES_InitStruct->Cipher[LL_AES_DATA_0], LL_AES_DATA_0);
	LL_AES_SetData(AESx, AES_InitStruct->Cipher[LL_AES_DATA_1], LL_AES_DATA_1);
	LL_AES_SetData(AESx, AES_InitStruct->Cipher[LL_AES_DATA_2], LL_AES_DATA_2);
	LL_AES_SetData(AESx, AES_InitStruct->Cipher[LL_AES_DATA_3], LL_AES_DATA_3);

	//---模式配置
	LL_AES_Enable_Decrypt(AESx);
	//---启动运算
	LL_AES_Start(AESx);
	//---等待运算完成
	while (1)
	{
		//---等待运算完成
		if (LL_AES_IsActiveFlag(AESx) == 0)
		{
			break;
		}
	}
	//---获取解密数据
	AES_InitStruct->Data[LL_AES_DATA_0] = LL_AES_GetData(AESx, LL_AES_DATA_0);
	AES_InitStruct->Data[LL_AES_DATA_1] = LL_AES_GetData(AESx, LL_AES_DATA_1);
	AES_InitStruct->Data[LL_AES_DATA_2] = LL_AES_GetData(AESx, LL_AES_DATA_2);
	AES_InitStruct->Data[LL_AES_DATA_3] = LL_AES_GetData(AESx, LL_AES_DATA_3);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值