[CC3200]

CC3200 DES Demo

[复制链接]
437|14
手机看帖
扫描二维码
随时随地手机跟帖
dingbo95|  楼主 | 2019-7-31 22:11 | 显示全部楼层 |阅读模式
DES 模块提供了硬件加速的数据加密和解密功能。模块运行单一 DES 或三倍 DES(3
DES)算法符合 FIPS 46-3 标准和支持以下操作模式:
 电子码(ECB)
 密码块链接(CBC)
 密码反馈(CFB )

使用特权

评论回复

相关帖子

dingbo95|  楼主 | 2019-7-31 22:11 | 显示全部楼层
基于对称算法 DES 模块,这意味着加密和解密密钥是相同的。加密数据意味着从纯
文本转换为一种特殊的形式称为密文。解密密文意味着将加密的数据改回到原来的纯文
本形式。

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:14 | 显示全部楼层
应用说明
应用程序的引用使用 DES DriverLib 函数。 开发人员/用户可以参考这个简单的应
用程序和重用这个功能在应用程序。这个应用程序在有或没有“Uart 终端” 的情况下都
可以使用。

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:15 | 显示全部楼层
如果用户希望使用“Uart 终端”输入,按照下面知识操作,通过定义“USER_INPUT”
在 des_main.c 文件中。
• desdemo: 此命令允许用户运行 DES 功能。 需要一个参数 des-mode:
-des-mode 是用户可以选择的 DES 算法,值可以是 ECB、 CBC、 CFB、 TECB、 TCBC 或者
TCFB。

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:17 | 显示全部楼层
设置一个串行通信程序(超级终端/ TeraTerm)。终端设置如下
4655d41a312be86b.png

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:20 | 显示全部楼层
运行参考程序(Flashing the bin/IAR/CCS)。在超级终端上,出现一个提示:需要下达 DES 命令, 可以看到结果。 运行结果如下图所示:
977645d41a39607233.png

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:22 | 显示全部楼层
volatile static bool g_bContextInIntFlag;
static volatile bool g_bDataInIntFlag;
static volatile bool g_bDataOutIntFlag;

#if defined(ccs)
extern void (* const g_pfnVectors[])(void);
#endif
#if defined(ewarm)
extern uVectorEntry __vector_table;
#endif

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:23 | 显示全部楼层
void DESIntHandler(void);
void DESCrypt(unsigned int uiConfig,unsigned char *puiKey1,unsigned char *puiData,
        unsigned char *puiResult,unsigned int uiDataLength,unsigned char *uiIV);
unsigned char * LoadDefaultValues(unsigned int ui32Config,unsigned int *uiConfig,
                        unsigned char **uiIV,unsigned char **puiKey1,unsigned int
                                        *uiDataLength,unsigned char **puiResult);
static void BoardInit(void);

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:25 | 显示全部楼层
void DESIntHandler(void);
void DESCrypt(unsigned int uiConfig,unsigned char *puiKey1,unsigned char *puiData,
        unsigned char *puiResult,unsigned int uiDataLength,unsigned char *uiIV);
unsigned char * LoadDefaultValues(unsigned int ui32Config,unsigned int *uiConfig,
                        unsigned char **uiIV,unsigned char **puiKey1,unsigned int
                                        *uiDataLength,unsigned char **puiResult);
static void BoardInit(void);

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:25 | 显示全部楼层
void
DESIntHandler(void)
{
    uint32_t ui32IntStatus;

    //
    // Read the DES masked interrupt status.
    //
    ui32IntStatus = MAP_DESIntStatus(DES_BASE, true);

    //
    // set flags depending on the interrupt source.
    //
    if(ui32IntStatus & DES_INT_CONTEXT_IN)
    {
        MAP_DESIntDisable(DES_BASE, DES_INT_CONTEXT_IN);
        g_bContextInIntFlag = true;
        
    }
    if(ui32IntStatus & DES_INT_DATA_IN)
    {
        MAP_DESIntDisable(DES_BASE, DES_INT_DATA_IN);
        g_bDataInIntFlag = true;
    }
    if(ui32IntStatus & DES_INT_DATA_OUT)
    {
        MAP_DESIntDisable(DES_BASE, DES_INT_DATA_OUT);
        g_bDataOutIntFlag = true;
        
    }
   

}

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:26 | 显示全部楼层
unsigned char *
LoadDefaultValues(unsigned int ui32Config,unsigned int *uiConfig,unsigned char **uiIV,
    unsigned char **puiKey1,unsigned int *uiDataLength,unsigned char **puiResult)
{
     unsigned char *uiData;
     
     *uiConfig=ui32Config;
     
     //
     // Read the Key
     //
     *puiKey1=psDESTestVectors.pui32Key;
     
     //
     // Read the Initialization vector
     //
     *uiIV=&psDESTestVectors.pui32IV[0];
     
     //
     // Read DataLength and allocate Result and Data Variables accordingly
     //
     *uiDataLength=psDESTestVectors.ui32DataLength;
     *puiResult=(unsigned char*)malloc(*uiDataLength);
     if(*puiResult != NULL)
     {
         memset(*puiResult,0,*uiDataLength);
     }
     else
     {
         //Failed to allocate memory
         UART_PRINT("Failed to allocate memory");
         return 0;
     }
     uiData=(unsigned char*)malloc(*uiDataLength);
     if(uiData != NULL)
     {
         memset(uiData,0,*uiDataLength);
     }
     else
     {
         //Failed to allocate memory
         UART_PRINT("Failed to allocate memory");
         return 0;
     }
     
     //
     // Copy Plain Text/ Cipher Text into the Data variables based on Encryption
     // or Decryption
     //
     if(ui32Config & DES_CFG_DIR_ENCRYPT)
        memcpy(uiData,psDESTestVectors.pui32PlainText,*uiDataLength);
     else
       memcpy(uiData,psDESTestVectors.pui32CipherText,*uiDataLength);
     return uiData;
      
}

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:26 | 显示全部楼层
void
DESCrypt(unsigned int uiConfig,unsigned char *puiKey1,unsigned char *puiData,
        unsigned char *puiResult,unsigned int uiDataLength,unsigned char *uiIV)
{
    //
    // Step1:  Enable Interrupts
    // Step2:  Wait for Context Ready Inteerupt
    // Step3:  Set the Configuration Parameters (Direction,AES Mode)
    // Step4:  Set the Initialization Vector
    // Step5:  Write Key
    // Step6:  Start the Crypt Process
    //
   
    //
    // Clear the flags.
    //
    g_bContextInIntFlag = false;
    g_bDataInIntFlag = false;
    g_bDataOutIntFlag = false;

    //
    // Enable all interrupts.
    //
    MAP_DESIntEnable(DES_BASE, DES_INT_CONTEXT_IN |
                 DES_INT_DATA_IN |
                 DES_INT_DATA_OUT);

    //
    // Wait for the context in flag.
    //
    while(!g_bContextInIntFlag)
    {
    }
   
    //
    // Configure the DES module.
    //
    MAP_DESConfigSet(DES_BASE, uiConfig);
   
    //
    // Set the key.
    //
    MAP_DESKeySet(DES_BASE, puiKey1);

    //
    // Write the initial value registers if needed.
    //
    if((uiConfig & DES_CFG_MODE_CBC) ||
        (uiConfig & DES_CFG_MODE_CFB))
    {
        MAP_DESIVSet(DES_BASE, uiIV);
    }

    MAP_DESDataProcess(DES_BASE, puiData, puiResult,uiDataLength);


}

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:27 | 显示全部楼层
static void
DisplayBanner(char * AppName)
{
    Report("\n\n\n\r");
    Report("\t\t *************************************************\n\r");
    Report("\t\t     CC3200 %s Application       \n\r", AppName);
    Report("\t\t *************************************************\n\r");
    Report("\n\n\n\r");
}

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:27 | 显示全部楼层
static void
BoardInit(void)
{
/* In case of TI-RTOS vector table is initialize by OS itself */
#ifndef USE_TIRTOS
    //
    // Set vector table base
    //
#if defined(ccs)
    MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
#endif
#if defined(ewarm)
    MAP_IntVTableBaseSet((unsigned long)&__vector_table);
#endif
#endif
    //
    // Enable Processor
    //
    MAP_IntMasterEnable();
    MAP_IntEnable(FAULT_SYSTICK);

    PRCMCC3200MCUInit();
}

使用特权

评论回复
dingbo95|  楼主 | 2019-7-31 22:28 | 显示全部楼层
void
main()
{
  
    unsigned int uiConfig,uiDataLength;
    unsigned char *puiKey1,*puiData,*puiResult, *puiIV,
    uiIV[8]={0x6d, 0x8e, 0xca, 0xc4, 0x3b, 0x27, 0xc8, 0x85};
   
#ifdef USER_INPUT
    unsigned int uiKeySize;
    unsigned int ui32CharCount;
    unsigned char * pui8Result;
#endif

    puiIV=&uiIV[0];
   
    //
    // Initialize the board
    //
    BoardInit();
   
    //
    // Configuring UART for Receiving input and displaying output
    // 1. PinMux setting
    // 2. Initialize UART
    // 3. Displaying Banner
    //
    PinMuxConfig();
    InitTerm();
    DisplayBanner(APP_NAME);
   
    //
    // Enable DES Module
    //
    MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK);
   
    //
    // Enable interrupts.
    //
    MAP_DESIntRegister(DES_BASE, DESIntHandler);

    //
    // Read values either from User or from Vector based on macro USER_INPUT
    // defined or not
    //
#ifdef USER_INPUT
    while(FOREVER)
    {

        //
        // Read the values from the user over uart and Populate the variables
        //
        puiData=ReadFromUser(&uiConfig,&uiKeySize,&puiKey1,&uiDataLength,
                                &puiResult);
        if(puiData==NULL)
        {
            continue;
         }
#else
        UART_PRINT("Running DES-ECB  Vectors\n\r");
        
        //
        // Load Default values
        //
        puiData=LoadDefaultValues(DES_CFG_DIR_ENCRYPT | DES_CFG_MODE_ECB |
          DES_CFG_SINGLE,&uiConfig,&puiIV,&puiKey1,&uiDataLength,&puiResult);
#endif

        //
        // Carry out Encryption
        //
        UART_PRINT("\n\r Encryption in progress....");
        DESCrypt(uiConfig,puiKey1,puiData,puiResult,uiDataLength,puiIV);
        UART_PRINT("\n\r Encryption done, cipher text created");
        
        //
        // Copy Result into Data Vector to continue with Decryption. and change
        // config value
        //
        memcpy(puiData,puiResult,uiDataLength);
        uiConfig &= ~(1 << 2);
        
        //
        // Carry out Decrypt Function
        //
        UART_PRINT("\n\r\n\r Decryption in progress....");
        DESCrypt(uiConfig,puiKey1,puiData,puiResult,uiDataLength,uiIV);
        UART_PRINT("\n\r Decryption done");

#ifdef USER_INPUT
        //
        // Display Plain Text
        //
        UART_PRINT("\n\r Text after decryption :");
        pui8Result = (unsigned char *)puiResult;
        for(ui32CharCount=0;ui32CharCount<uiDataLength;ui32CharCount++)
        {
                UART_PRINT("%c",*(pui8Result+ui32CharCount));
        }
        UART_PRINT("\n\r");

    }
#else
    //
    // Comapre Cipher Text and Plain Text with the expected values from
    // predefined vector
    //
    if(memcmp(puiData,psDESTestVectors.pui32CipherText,
                    psDESTestVectors.ui32DataLength)==0)
    {               
        UART_PRINT("\n\r\n\r Encryption verified");
    }
    if(memcmp(puiResult,psDESTestVectors.pui32PlainText,
                        psDESTestVectors.ui32DataLength)==0)
    {
        UART_PRINT("\n\r Decryption verification successful");
    }
   
    while(FOREVER);
#endif
  
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

52

主题

1197

帖子

5

粉丝