rt-thread-official/bsp/essemi/es8p508x/libraries/Library/Source/lib_aes.c

223 lines
4.9 KiB
C
Raw Normal View History

2019-11-07 10:00:14 +08:00
/***************************************************************
*Copyright (C), 2017, Shanghai Eastsoft Microelectronics Co., Ltd.
*<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: AE
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: V1.00
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: 2017/07/14
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ES8P508оƬ
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧϰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾʹ<EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>κη<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ρ<EFBFBD>
***************************************************************/
#include "lib_aes.h"
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_Init
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_InitStruct <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_Init(AES_InitStruType * AES_InitStruct)
{
AES_Reset();
AES->CON.ENCRYPT = AES_InitStruct->MODE;
return;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_WriteKey
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AESд<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_KEY <EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_WriteKey(uint32_t *AES_KEY)
{
uint32_t *address = AES_KEY;
AES->KEY3.AES_KEY3 = *address;
address ++;
AES->KEY2.AES_KEY2 = *address;
address ++;
AES->KEY1.AES_KEY1 = *address;
address ++;
AES->KEY0.AES_KEY0 = *address;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_ReadKey
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_DATA<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_ReadKey(uint32_t * AES_KEY)
{
uint32_t *address = AES_KEY;
*address = AES->KEY3.AES_KEY3;
address ++;
*address = AES->KEY2.AES_KEY2;
address ++;
*address = AES->KEY1.AES_KEY1;
address ++;
*address = AES->KEY0.AES_KEY0;
return;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_WriteData
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AESд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_DATA <EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_WriteData(uint32_t *AES_DATA)
{
uint32_t *address = AES_DATA;
AES->DATA3.AES_DATA3 = *address;
address ++;
AES->DATA2.AES_DATA2 = *address;
address ++;
AES->DATA1.AES_DATA1 = *address;
address ++;
AES->DATA0.AES_DATA0 = *address;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_WriteData
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_DATA<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_ReadData(uint32_t * AES_DATA)
{
uint32_t *address = AES_DATA;
*address = AES->DATA3.AES_DATA3;
address ++;
*address = AES->DATA2.AES_DATA2;
address ++;
*address = AES->DATA1.AES_DATA1;
address ++;
*address = AES->DATA0.AES_DATA0;
return;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_ITConfig
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD>ж<EFBFBD>ʹ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_IE
NewState Enable/Disable
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_ITConfig(AES_TYPE_IT AES_IE, TYPE_FUNCEN NewState)
{
uint32_t Word = AES->CON.Word&0xffffff8e;
if(AES->CON.GO_DONE == 1)
{
return;
}
if (NewState == ENABLE)
Word |= (uint32_t)AES_IE;
else
Word &= ~(uint32_t)AES_IE;
AES->CON.Word = Word;
return;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_GetFlagStatus
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD>жϱ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>IFName
AES_IF_IF <EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>жϱ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
FlagStatus AES_GetFlagStatus(AES_TYPE_IF IFName)
{
if(AES->CON.Word & IFName) {
return SET;
}
return RESET;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_ClearITPendingBit
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD>жϱ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>IFName
AES_IF_DONE <EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_ClearITPendingBit(void)
{
/* <20><><EFBFBD>ǵ<EFBFBD>IF<49><46>GO_DONE<4E><45>ͬһ<CDAC><D2BB>word<72>У<EFBFBD><D0A3><EFBFBD>IF<49><46>ֵ<EFBFBD><D6B5><EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>GO_DONE<EFBFBD><EFBFBD>1ʱ<EFBFBD><EFBFBD>ȡֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GO_DONE<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>ٶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD>ֵ
* <EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD>
*/
if(AES->CON.GO_DONE == SET)
{
return;
}
AES->CON.IF = SET;
return;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_GetDoneStatus
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>/<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>AES_DONE_NO <EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
AES_DONE_YES δ<EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><EFBFBD>߼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
AES_TYPE_DONE AES_GetDoneStatus(void)
{
if(AES->CON.GO_DONE == 1)
{
return AES_DONE_NO;
}
return AES_DONE_YES;
}
/***************************************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES_Reset
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AES<EFBFBD><EFBFBD>λ
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
***************************************************************/
void AES_Reset(void)
{
AES->DATA0.Word = 0x00000000;
AES->DATA1.Word = 0x00000000;
AES->DATA2.Word = 0x00000000;
AES->DATA3.Word = 0x00000000;
AES->KEY0.Word = 0x00000000;
AES->KEY1.Word = 0x00000000;
AES->KEY2.Word = 0x00000000;
AES->KEY3.Word = 0x00000000;
AES->CON.Word = 0x00000000;
}