📄 hal_aes.c
字号:
* @brief Writes the key into AES engine
*
* input parameters
*
* @param AesKey - Pointer to AES Key.
*
* @return None
*/
void ssp_HW_KeyInit( uint8 *AesKey )
{
AES_SETMODE(ECB);
AesLoadKey( AesKey );
}
/******************************************************************************
* @fn sspAesEncryptHW
*
* @brief Encrypts 16 byte block using AES encryption engine
*
* input parameters
*
* @param AesKey - Pointer to AES Key.
* @param Cstate - Pointer to input data.
*
* output parameters
*
* @param Cstate - Pointer to encrypted data.
*
* @return None
*
*/
void sspAesEncryptHW( uint8 *AesKey, uint8 *Cstate )
{
/* Setup DMA for AES encryption */
AesDmaSetup( Cstate, STATE_BLENGTH, Cstate, STATE_BLENGTH );
AES_SET_ENCR_DECR_KEY_IV( AES_ENCRYPT );
/* Kick it off, block until DMA is done */
HAL_DMA_CLEAR_IRQ( HAL_DMA_AES_OUT );
AES_START();
while( !HAL_DMA_CHECK_IRQ( HAL_DMA_AES_OUT ) );
}
#if (defined SW_AES_AND_KEY_EXP) && (SW_AES_AND_KEY_EXP == TRUE)
/******************************************************************************
* @fn sspKeyExpansion
*
* @brief Performs key expansion to create the entire Key Schedule.
*
* input parameters
*
* @param AesKey - Pointer to AES Key.
* @param KeyExp - Pointer Key Expansion buffer. Size = 176 bytes
*
* output parameters
*
* @param KeyExp[] - Key Schedule
*
* @return None
*
*/
void sspKeyExpansion( uint8 *AesKey, uint8 *KeyExp )
{
uint8 temp[4], i, t, rc;
osal_memcpy( KeyExp, AesKey, 16 );
rc = 0; // index to RCon[] table
for (i=16; i < KEY_EXP_LENGTH; i+=16)
{
// RotByte, SubByte, Rcon
t= SBOX(KeyExp[i-4]);
temp[0] = SBOX(KeyExp[i-3]) ^ RCON(rc++);
temp[1] = SBOX(KeyExp[i-2]);
temp[2] = SBOX(KeyExp[i-1]);
temp[3] = t;
KeyExp[i] = KeyExp[i-16] ^ temp[0];
KeyExp[i+1] = KeyExp[i-15] ^ temp[1];
KeyExp[i+2] = KeyExp[i-14] ^ temp[2];
KeyExp[i+3] = KeyExp[i-13] ^ temp[3];
KeyExp[i+4] = KeyExp[i-12] ^ KeyExp[i];
KeyExp[i+5] = KeyExp[i-11] ^ KeyExp[i+1];
KeyExp[i+6] = KeyExp[i-10] ^ KeyExp[i+2];
KeyExp[i+7] = KeyExp[i-9] ^ KeyExp[i+3];
KeyExp[i+8] = KeyExp[i-8] ^ KeyExp[i+4];
KeyExp[i+9] = KeyExp[i-7] ^ KeyExp[i+5];
KeyExp[i+10] = KeyExp[i-6] ^ KeyExp[i+6];
KeyExp[i+11] = KeyExp[i-5] ^ KeyExp[i+7];
KeyExp[i+12] = KeyExp[i-4] ^ KeyExp[i+8];
KeyExp[i+13] = KeyExp[i-3] ^ KeyExp[i+9];
KeyExp[i+14] = KeyExp[i-2] ^ KeyExp[i+10];
KeyExp[i+15] = KeyExp[i-1] ^ KeyExp[i+11];
}
}
/******************************************************************************
* @fn sspAesEncryptKeyExp
*
* @brief Performs AES-128 encryption using Key Expansion buffer. The
* plaintext input will be overwritten with the ciphertext output.
*
* input parameters
*
* @param KeyExp - Pointer to Key Expansion buffer.
* @param Cstate - Pointer to plaintext input.
*
* output parameters
*
* @param Cstate[] - Ciphertext output
*
* @return None
*
*/
void sspAesEncryptKeyExp( uint8 *KeyExp, uint8 *Cstate )
{
uint8 i, round;
for (round=0; round < 9; round++)
{
AddRoundKeySubBytes( KeyExp, Cstate );
KeyExp += 16;
ShiftRows( Cstate );
MixColumns( Cstate );
}
AddRoundKeySubBytes( KeyExp, Cstate );
KeyExp += 16;
ShiftRows( Cstate );
for (i=0; i < 16; i++) Cstate[i] ^= KeyExp[i];
}
#endif
#if (defined SOFTWARE_AES) && (SOFTWARE_AES == TRUE)
/******************************************************************************
* @fn sspAesEncryptBasic
*
* @brief Performs AES-128 encryption without using Key Expansion. The
* plaintext input will be overwritten with the ciphertext output.
*
* input parameters
*
* @param AesKey - Pointer to AES Key.
* @param Cstate - Pointer to plaintext input.
*
* output parameters
*
* @param Cstate[] - Ciphertext output
*
* @return None
*
*/
void sspAesEncryptBasic( uint8 *AesKey, uint8 *Cstate )
{
uint8 RKBuff[KEY_BLENGTH];
uint8 i, round;
osal_memcpy( RKBuff, AesKey, 16 );
for (round=0; round < 9; round++)
{
AddRoundKeySubBytes( RKBuff, Cstate );
ShiftRows( Cstate );
MixColumns( Cstate );
RoundKey( RKBuff, round );
}
AddRoundKeySubBytes( RKBuff, Cstate );
ShiftRows( Cstate );
RoundKey( RKBuff, round );
for (i=0; i < 16; i++) Cstate[i] ^= RKBuff[i];
}
/******************************************************************************
* @fn RoundKey
*
* @brief Generates the next Key Schedule based on the current Key Schedule.
*
* input parameters
*
* @param W - Pointer to the current Key Schedule.
* @param rc - Round counter.
*
* output parameters
*
* @param W[] - Next Key Schedule
*
* @return None
*
*/
void RoundKey( uint8 *W, uint8 rc )
{
uint8 temp[4], t;
// RotByte, SubByte, Rcon
t = SBOX(W[12]);
temp[0] = SBOX(W[13]) ^ RCON(rc);
temp[1] = SBOX(W[14]);
temp[2] = SBOX(W[15]);
temp[3] = t;
W[0] ^= temp[0];
W[1] ^= temp[1];
W[2] ^= temp[2];
W[3] ^= temp[3];
W[4] ^= W[0];
W[5] ^= W[1];
W[6] ^= W[2];
W[7] ^= W[3];
W[8] ^= W[4];
W[9] ^= W[5];
W[10] ^= W[6];
W[11] ^= W[7];
W[12] ^= W[8];
W[13] ^= W[9];
W[14] ^= W[10];
W[15] ^= W[11];
}
#endif
#if ((defined SOFTWARE_AES) && (SOFTWARE_AES == TRUE)) || ((defined SW_AES_AND_KEY_EXP) && (SW_AES_AND_KEY_EXP == TRUE))
/******************************************************************************
* @fn AddRoundKeySubBytes
*
* @brief Performs the AddRoundKey and SubBytes function.
*
* input parameters
*
* @param KeySch - Pointer to the Key Schedule.
* @param Cstate - Pointer to cipher state.
*
* output parameters
*
* @param Cstate[] - updated cipher state
*
* @return None
*
*/
void AddRoundKeySubBytes( uint8 *KeySch, uint8 *Cstate )
{
uint8 i;
for (i=0; i < 16; i++)
{
Cstate[i] = SBOX(Cstate[i] ^ KeySch[i]);
}
}
/******************************************************************************
* @fn ShiftRows
*
* @brief Performs the ShiftRows function on the cipher state.
*
* input parameters
*
* @param Cstate - The current cipher state
*
* output parameters
*
* @param Cstate[] - Updated cipher state
*
* @return None
*
*/
void ShiftRows( uint8 *Cstate )
{
uint8 temp;
// Row 0 is not shifted
// Row 1 is shifted down by 1
temp = Cstate[1];
Cstate[1] = Cstate[5];
Cstate[5] = Cstate[9];
Cstate[9] = Cstate[13];
Cstate[13] = temp;
// Row 2 is shifted down by 2
temp = Cstate[2];
Cstate[2] = Cstate[10];
Cstate[10] = temp;
temp = Cstate[6];
Cstate[6] = Cstate[14];
Cstate[14] = temp;
// Row 3 is shifted down by 3
temp = Cstate[3];
Cstate[3] = Cstate[15];
Cstate[15] = Cstate[11];
Cstate[11] = Cstate[7];
Cstate[7] = temp;
}
/******************************************************************************
* @fn MixColumns
*
* @brief Performs the MixColumns function on the cipher state.
*
* input parameters
*
* @param Cstate - The current cipher state
*
* output parameters
*
* @param Cstate[] - Updated cipher state
*
* @return None
*
*/
void MixColumns( uint8 *Cstate )
{
uint8 r, c, t[4];
for (c=0; c < 16; c+=4)
{
for (r=0; r < 4; r++) t[r] = Cstate[c+r];
Cstate[c] = MUL2(t[0]) ^ MUL3(t[1]) ^ t[2] ^ t[3];
Cstate[c+1] = MUL2(t[1]) ^ MUL3(t[2]) ^ t[3] ^ t[0];
Cstate[c+2] = MUL2(t[2]) ^ MUL3(t[3]) ^ t[0] ^ t[1];
Cstate[c+3] = MUL2(t[3]) ^ MUL3(t[0]) ^ t[1] ^ t[2];
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -