示例#1
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32l1xx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */     
       
  /* Initialize the LCD */ 
  STM32L152D_LCD_Init();
 
  /* Initialize LEDs available on STM32L15X-EVAL board ************************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Enable AES AHB clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_AES, ENABLE);

  /*============================================================================
                              Encryption mode
   ===========================================================================*/

#if defined AES_ECB
  AES_ECB_Encrypt(EncryptionKey, PlainText, AES_TEXT_SIZE, CipherText);
#elif defined AES_CBC
  AES_CBC_Encrypt(EncryptionKey, InitVector, PlainText, AES_TEXT_SIZE, CipherText);
#else /* CTR Mode */
  AES_CTR_Encrypt(EncryptionKey, InitVector, PlainText, AES_TEXT_SIZE, CipherText);
#endif

  /* Clear the LCD */
  LCD_Clear(LCD_COLOR_WHITE);
  /* Set the Back Color */
  LCD_SetBackColor(LCD_COLOR_BLUE);

  /* Read the CipherText and check content correctness */
#if defined AES_ECB
  if(Buffercmp(ExpectedCipherText, CipherText, AES_TEXT_SIZE) != ERROR)
#elif defined AES_CBC
  if(Buffercmp(ExpectedCipherText_CBC, CipherText, AES_TEXT_SIZE) != ERROR) 
#else /* CTR */
  if(Buffercmp(ExpectedCipherText_CTR, CipherText, AES_TEXT_SIZE) != ERROR)
#endif
  {
    /* OK */
    /* Turn on LED1 */
    STM_EVAL_LEDOn(LED1);
    /* Set the Text Color */
    LCD_SetTextColor(LCD_COLOR_GREEN);
    LCD_DisplayStringLine(LCD_LINE_2, "      Success       ");
  }
  else
  { 
    /* KO */
    /* Turn on LED2 */
    STM_EVAL_LEDOn(LED2);
    /* Set the Text Color */
    LCD_SetTextColor(LCD_COLOR_RED);
    LCD_DisplayStringLine(LCD_LINE_2, "       Failed       ");
  }

  LCD_DisplayStringLine(LCD_LINE_1, "     Encryption     ");
#if defined AES_ECB
    LCD_DisplayStringLine(LCD_LINE_0, "      ECB Mode      ");
#elif defined AES_CBC
    LCD_DisplayStringLine(LCD_LINE_0, "      CBC Mode      ");
#else /* CTR */
    LCD_DisplayStringLine(LCD_LINE_0, "      CTR Mode      ");
#endif

  /*============================================================================
                              Decryption mode
   ===========================================================================*/

#if defined AES_ECB
  AES_ECB_Decrypt(EncryptionKey, CipherText, AES_TEXT_SIZE, ComputedPlainText);
#elif defined AES_CBC
  AES_CBC_Decrypt(EncryptionKey, InitVector, CipherText, AES_TEXT_SIZE, ComputedPlainText);
#else /* CTR Mode */
  AES_CTR_Decrypt(EncryptionKey, InitVector, CipherText, AES_TEXT_SIZE, ComputedPlainText);
#endif
  
  /* Read the ComputedPlainText and check content correctness */
  if(Buffercmp(PlainText, ComputedPlainText, AES_TEXT_SIZE) != ERROR)
  {
    /* OK */
    /* Turn on LED3 */
    STM_EVAL_LEDOn(LED3);
    /* Set the Text Color */
    LCD_SetTextColor(LCD_COLOR_GREEN);    
    LCD_DisplayStringLine(LCD_LINE_6, "      Success       ");
  }
  else
  { 
    /* KO */
    /* Turn on LED4 */
    STM_EVAL_LEDOn(LED4);
    /* Set the Text Color */
    LCD_SetTextColor(LCD_COLOR_RED);
    LCD_DisplayStringLine(LCD_LINE_6, "       Failed       ");
  }

  LCD_DisplayStringLine(LCD_LINE_5, "     Decryption     ");
#if defined AES_ECB
    LCD_DisplayStringLine(LCD_LINE_4, "      ECB Mode      "); 
#elif defined AES_CBC
    LCD_DisplayStringLine(LCD_LINE_4, "      CBC Mode      "); 
#else /* CTR */
    LCD_DisplayStringLine(LCD_LINE_4, "      CTR Mode      ");
#endif

  while (1)
  {
  }
}
示例#2
0
/*************************************************
* Function: ZC_RecvDataFromClient
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ZC_RecvDataFromClient(u32 ClientId, u8 *pu8Data, u32 u32DataLen)
{
    u32 u32RetVal;
    ZC_MessageHead *pstruMsg;
    ZC_MessageOptHead struOpt;
    ZC_SsessionInfo struSessionMsg;
    ZC_SecHead *pstruHead;
    u16 u16Len;
    u32 u32CiperLen;
    u8 *pu8Key;
    ZC_SendParam struParam;
    u8 u8Iv[ZC_HS_SESSION_KEY_LEN];
    u16 crc;

    /*can hanle it,get aes key*/
    
    ZC_GetStoreInfor(ZC_GET_TYPE_TOKENKEY, &pu8Key);

    u32RetVal = ZC_CheckClientIdle(ClientId);
    if (ZC_RET_ERROR == u32RetVal)
    {
        pstruHead = (ZC_SecHead*)(g_u8MsgBuildBuffer);
        
        EVENT_BuildMsg(ZC_CODE_ERR, 0, g_u8MsgBuildBuffer + sizeof(ZC_SecHead), &u16Len, 
            NULL, 0);
        memcpy(u8Iv, pu8Key, ZC_HS_SESSION_KEY_LEN);
        AES_CBC_Encrypt(g_u8MsgBuildBuffer + sizeof(ZC_SecHead), u16Len,
            pu8Key, ZC_HS_SESSION_KEY_LEN,
            u8Iv, ZC_HS_SESSION_KEY_LEN,
            g_u8MsgBuildBuffer + sizeof(ZC_SecHead), &u32CiperLen);

        pstruHead->u8SecType = ZC_SEC_ALG_AES;
        pstruHead->u16TotalMsg = ZC_HTONS((u16)u32CiperLen);

        struParam.u8NeedPoll = 0;            
        g_struProtocolController.pstruMoudleFun->pfunSendTcpData(ClientId, g_u8MsgBuildBuffer, 
            u32CiperLen + sizeof(ZC_SecHead), &struParam);
        return;            
    }
    
    /*set client busy*/
    ZC_SetClientBusy(ClientId);
    
    u32RetVal = MSG_RecvData(&g_struClientBuffer, pu8Data, u32DataLen);
    if ((MSG_BUFFER_FULL == g_struClientBuffer.u8Status)&&(ZC_RET_OK == u32RetVal))
    {  
        do
        {
            pstruHead = (ZC_SecHead *)g_struClientBuffer.u8MsgBuffer;

            if (ZC_HTONS(pstruHead->u16TotalMsg) >= MSG_BULID_BUFFER_MAXLEN)
            {
                break;
            }
         
            memcpy(u8Iv, pu8Key, ZC_HS_SESSION_KEY_LEN);
            AES_CBC_Decrypt(g_struClientBuffer.u8MsgBuffer + sizeof(ZC_SecHead), ZC_HTONS(pstruHead->u16TotalMsg), 
                pu8Key, ZC_HS_SESSION_KEY_LEN, 
                u8Iv, ZC_HS_SESSION_KEY_LEN, 
                g_u8MsgBuildBuffer, &u32CiperLen);

            pstruMsg = (ZC_MessageHead*)(g_u8MsgBuildBuffer);
            if(ZC_RET_ERROR == PCT_CheckCrc(pstruMsg->TotalMsgCrc, (u8 *)(pstruMsg + 1), ZC_HTONS(pstruMsg->Payloadlen)))
            {
                break;
            }

            pstruMsg->Payloadlen = ZC_HTONS(ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageOptHead) + sizeof(ZC_SsessionInfo));
            if (ZC_HTONS(pstruMsg->Payloadlen) > MSG_BULID_BUFFER_MAXLEN)
            {
                break;
            }

            pstruMsg->OptNum = pstruMsg->OptNum + 1;
            struOpt.OptCode = ZC_HTONS(ZC_OPT_SSESSION);
            struOpt.OptLen = ZC_HTONS(sizeof(ZC_SsessionInfo));
            struSessionMsg.u32SsessionId = ZC_HTONL(ClientId);

            u16Len = 0;
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, pstruMsg, sizeof(ZC_MessageHead));
 
            /*insert opt*/
            u16Len += sizeof(ZC_MessageHead);
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, 
                 &struOpt, sizeof(ZC_MessageOptHead));
            u16Len += sizeof(ZC_MessageOptHead);
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, 
                 &struSessionMsg, sizeof(struSessionMsg));
 
            /*copy message*/
            u16Len += sizeof(struSessionMsg);    
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, 
                (u8*)(pstruMsg+1), ZC_HTONS(pstruMsg->Payloadlen) - (sizeof(ZC_MessageOptHead) + sizeof(struSessionMsg)));   
 
            u16Len += ZC_HTONS(pstruMsg->Payloadlen) - (sizeof(ZC_MessageOptHead) + sizeof(struSessionMsg));     
            g_struClientBuffer.u32Len = u16Len;

            crc = crc16_ccitt(g_struClientBuffer.u8MsgBuffer+sizeof(ZC_MessageHead),u16Len - sizeof(ZC_MessageHead));
            pstruMsg =  (ZC_MessageHead*)(g_struClientBuffer.u8MsgBuffer);
            pstruMsg->TotalMsgCrc[0]=(crc&0xff00)>>8;
            pstruMsg->TotalMsgCrc[1]=(crc&0xff);

            ZC_TraceData(g_struClientBuffer.u8MsgBuffer, g_struClientBuffer.u32Len);
            
            /*send to moudle*/
            g_struProtocolController.pstruMoudleFun->pfunSendToMoudle(g_struClientBuffer.u8MsgBuffer, g_struClientBuffer.u32Len);
        }while(0);
        g_struClientBuffer.u8Status = MSG_BUFFER_IDLE;
        g_struClientBuffer.u32Len = 0;
        ZC_SetClientFree(ClientId);

    }
  
    return;
}