📄 aes.c
字号:
/******************************************************************************
Filename: aes.c
Target: cc2430
Revised: 16/12-2005
Revision: 1.0
Description:
This application show how to use the AES (Advanced Encryption Standard) module.
The program first encrypts a short string, and then decrypts it afterwards using
the HAL library. Then a longer string is encrypted and decrypted using DMA.
******************************************************************************/
#include <string.h>
#include "RF04EB.h"
#include "app_ex.h"
#include "menu.h"
#include "lcd128_64.h"
#include "hal.h"
#include "cul.h"
#define STRING_LENGTH 16
#define STRING_LENGTH_DMA 4*16
#define LENGTH_IV 16
#define LENGTH_KEY 16
void aes_main(void);
//key an initialization vector (IV)
BYTE key[LENGTH_KEY] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x11};
BYTE IV[LENGTH_IV ] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
volatile BOOL dmaFinished;
void initAes(void);
void setFinished(void);
void aes(void);
void dmaAes(void);
void scrollText(char *string, UINT8 length);
/******************************************************************************
* @fn initAes
*
* @brief
* Initializes components for use with the AES encryption routine
* application example (e.g. LED's, LCD, PushButton).
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
void initAes(void)
{
SET_MAIN_CLOCK_SOURCE(CRYSTAL);
INIT_BUTTON();
INIT_GLED();
}
/******************************************************************************
* @fn aes_main
*
* @brief
* Main function of the AES encryption application example.
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
#ifdef COMPLETE_APPLICATION
void aes_main(void){
#else
void main(void){
#endif
initAes();
ClearScreen();
GLED = LED_OFF;
Print6(0,10," ---AES TEST--- ",1);
aes();
return;
}
/******************************************************************************
* @fn aes
*
* @brief
* This function encrypts and decrypts a string with use of the AES
* module and the hal library
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
void aes(void)
{
char plainText[STRING_LENGTH];
char cipherText[STRING_LENGTH];
//clearing both strings
memset(plainText, 0, STRING_LENGTH);
memset(cipherText, 0, STRING_LENGTH);
//Inserting, printing plaintext and waiting for input
strcpy(plainText, (char*)"www.c51rf.com");
// Print6(3,20, "Plain text",1);
// Print6(4,20, (INT8U *)plainText,1);
if(LanguageSel == 1)
{
Print6(3,20, "Plain text:",1);
Print6(4,18, (INT8U *)plainText,1);
}
else
{
Print(1,8,"原字符串:",1);
Print6(4,18, (INT8U *)plainText,1);
}
Print6(7,10,"<UP TO CONTINUE>",1);
while(K_UP != ScanKey());
while(ScanKey() != 0Xff);
halWait(5);
// Setting the correct mode for AES and loading the key
AES_SETMODE(CTR);
halAesLoadKeyOrInitVector(key, TRUE);
GLED = LED_OFF;
//encrypt text and clearing plain text
halAesEncrDecr((BYTE *)plainText, STRING_LENGTH, (BYTE *)cipherText, IV, ENCRYPT);
memset(plainText, 0, STRING_LENGTH);
//printing ciphertext and waiting for user
if(LanguageSel == 1)
{
Print6(3,20, "Cipher text:",1);
Print6(4,18, (INT8U *)cipherText,1);
}
else
{
Print(1,8,"加密后: ",1);
Print6(4,18, (INT8U *)cipherText,1);
}
// lcdUpdate((char*)"Cipher text:", cipherText);
Print6(7,10,"<OK TO CONTINUE>",1);
while(K_OK != ScanKey());
while(ScanKey() != 0Xff);
halWait(5);
GLED = LED_ON;
//decrypt text, printing result and waiting for user
halAesEncrDecr((BYTE *)cipherText, STRING_LENGTH, (BYTE *)plainText, IV, DECRYPT);
Print6(4,20, " ",1);
if(LanguageSel == 1)
{
Print6(3,20, "Plain text:",1);
Print6(4,18, (INT8U *)plainText,1);
}
else
{
Print(1,8,"解密后: ",1);
Print6(4,18, (INT8U *)plainText,1);
}
Print6(7,10,"<CANCEL TO ESC> ",1);
while(K_CANCEL != ScanKey());
while(ScanKey() != 0Xff);
halWait(5);
GLED = LED_OFF;
return;
}
/******************************************************************************
* @fn setFinished
*
* @brief
* DMA callback routine.
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
void setFinished(void)
{
dmaFinished = TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -