亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? hal_rf_security.c

?? cc2430最新的例子程序
?? C
字號:
/***********************************************************************************

  Filename:       hal_rf_security.c

  Description:    CC2430 CCM security

***********************************************************************************/

/***********************************************************************************
* INCLUDES
*/
#include "hal_board.h"

#include "hal_rf.h"
#include "hal_rf_security.h"
#include "util.h"
#include "string.h"


/***********************************************************************************
* CONSTANTS AND DEFINES
*/


// Staring AES operation
#define AES_START()             st (ENCCS |= 0x01;)

// Setting the mode of the AES operation
#define AES_SET_MODE(mode)      st( ENCCS &= ~0x70; ENCCS |= mode; )

// Starting or stopping encryption or decryption
#define AES_SET_OPERATION(op)   st( ENCCS = (ENCCS & ~0x07) | op; )

// Poll the AES ready bit
#define AES_RDY()              ( ENCCS & 8)

// Encryption mode
#define AES_MODE_CBC            0x00
#define AES_MODE_CFB            0x10
#define AES_MODE_OFB            0x20
#define AES_MODE_CTR            0x30
#define AES_MODE_ECB            0x40
#define AES_MODE_CBCMAC         0x50

// Operations
#define AES_ENCRYPT             0x00
#define AES_DECRYPT             0x02
#define AES_LOAD_KEY            0x04
#define AES_LOAD_IV             0x06



// Convert MIC to MIC length
#define MIC_2_MICLEN(m)         ( BV((m&3)+1) & ~3 )


/***********************************************************************************
* LOCAL DATA
*/
static  uint8 nonceTx[16];
static  uint8 nonceRx[16];
static  uint8 IV[16];
static  uint8 cipherText[128];
static  uint8 buf[128];


/***********************************************************************************
* LOCAL FUNCTIONS
*/
static void halAesEncrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut,
                          uint8 *pInitVector);
static void halAesDecrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut,
                          uint8 *pInitVector);
static uint8 generateAuthData(uint8 c, uint8 *pIv, uint8 *pData, uint8 f, uint8 lm);




/***********************************************************************************
* GLOBAL FUNCTIONS
*/
extern void halRfAppendTxBuf(uint8* pData, uint8 length);


/***********************************************************************************
* @fn      halRfSecurityInit
*
* @brief   Security init. Write nonces and key to chip.
*
* @param   none
*
* @return  none
*/
void halRfSecurityInit(uint8* pKey, uint8* pNonceRx, uint8* pNonceTx)
{
    uint8 i;

    AES_SET_OPERATION(AES_LOAD_KEY);

    // Starting loading of key or vector.
    AES_START();

    // Store key and nonces
    for (i=0; i<NONCE_LENGTH; i++) {
        nonceRx[i]= *pNonceRx;
        nonceTx[i]= *pNonceTx;
        pNonceTx++;
        pNonceRx++;
        ENCDI = *pKey++;
    }
}



/***********************************************************************************
* @fn      halRfWriteTxBufSecure
*
* @brief   Encrypt and authenticate plaintext then fill TX buffer
*
* @param   uint8* data - data buffer. This must be allocated by caller.
*          uint8 length - number of bytes
*          uint8 c - number of bytes to encrypt
*          uint8 f - number of bytes to authenticate
*          uint8 m - integrity code (m=1,2,3 gives lenght of integrity
*                   field l(m)= 4,8,16)
*
* @return  none
*/
void halRfWriteTxBufSecure(uint8* pPkt, uint8 length, uint8 c, uint8 f, uint8 m)
{
    uint8 i,lm;

    // Generate B(i) vector
    memset(buf,0,sizeof(buf));
    lm= MIC_2_MICLEN(m);
    i= generateAuthData(c,nonceTx,pPkt+1,f,lm);

    // Calculate CBC-MAC value
    memset(IV,0,sizeof(IV));
    halAesEncrypt(AES_MODE_CBCMAC,buf, i, cipherText, IV);

    // Encrypt authentication tag
    memcpy(IV,nonceTx,16);                      // Use nonce as IV
    IV[0]= 0x01;                                // Flag for CTR
    IV[15]= 0;                                  // Counter= 0

    memcpy(buf,cipherText,16);
    halAesEncrypt(AES_MODE_CTR,buf, 16, cipherText, IV);

    // Encrypt plaintext
    memset(buf+i,0,16-(i&0xf));                 // Zero padding
    IV[15]= 1;                                  // Counter= 1
    halAesEncrypt(AES_MODE_CTR,&buf[i-c], c, &cipherText[i-c], IV);

    // Fill TX buffer
    halRfWriteTxBuf(pPkt,1);                    // Length byte
    halRfAppendTxBuf(pPkt+1,f);                 // MPDU
    halRfAppendTxBuf(&cipherText[i-c],c);       // Payload
    halRfAppendTxBuf(cipherText,lm);            // MIC
}


/***********************************************************************************
* @fn      halRfReadRxBufSecure
*
* @brief   Decrypts and reverse authenticates with CCM then reads out received
*          frame
*
* @param   uint8* pPkt - data buffer. This must be allocated by caller.
*          uint8 length - number of bytes
*          uint8 encrLength - number of bytes to decrypt
*          uint8 authLength - number of bytes to reverse authenticate
*          uuint8 m - ets length of integrity code (m=1,2,3 gives lenght of integrity
*                   field 4,8,16)
*
* @return  SUCCESS or FAILED
*/
uint8 halRfReadRxBufSecure(uint8* data, uint8 length, uint8 c, uint8 f, uint8 m)
{
    uint8 lm,l,i;

    // Get received packet
    halRfReadRxBuf(data,length);

    // Update RX nonce; frame counter
    nonceRx[12]= data[10];
    nonceRx[11]= data[11];
    nonceRx[10]= data[12];
    nonceRx[9]= data[13];
    // Update RX nonce; source address
    nonceRx[7]= data[8];
    nonceRx[8]= data[7];

    // Decrypt authentication tag
    memcpy(IV,nonceRx,16);
    IV[15]= 0;                        // Counter= 0
    IV[0]= 0x01;                      // Flag field for decryption
    lm=MIC_2_MICLEN(m);               // Length of authentication tag

    halAesDecrypt(AES_MODE_CTR,data+c+f, lm, buf, IV);
    memcpy(data+c+f,buf,lm);

    // Prepare payload for decryption
    memcpy(cipherText,data+f,c);

    // Zero padding
    l= (c & 0x0f )==0 ? c : (c&0xf0) + 0x10;
    for (i=c; i<l; i++)
        cipherText[i]= 0x00;

    // Decrypt payload
    IV[15]= 1;                       // Counter= 1
    halAesDecrypt(AES_MODE_CTR,cipherText, c, buf, IV);
    memcpy(data+f,buf,c);

    // Generate B(i) vector
    memset(buf,0,sizeof(buf));
    i= generateAuthData(c,nonceRx,data,f,lm);

    // Calculate CBC-MAC value
    memset(IV,0,sizeof(IV));
    halAesEncrypt(AES_MODE_CBCMAC,buf, i, cipherText, IV);

    // Check authentication data
    return memcmp(cipherText,data+c+f,lm)==0 ? SUCCESS : FAILED;
}


/***********************************************************************************
* @fn      halRfIncNonceTx
*
* @brief   Increments frame counter field of stored nonce TX
*
* @param   none
*
* @return  none
*/
void halRfIncNonceTx(void)
{
    uint32 *pCount;

    pCount= (uint32*)&nonceTx[9];
    utilReverseBuf((uint8*)pCount,4);
    (*pCount)++;
    utilReverseBuf((uint8*)pCount,4);
}




/***********************************************************************************
* LOCAL FUNCTIONS
*/


static void halAesLoadBlock(BYTE* pData, uint8 op)
{
   UINT8 i;

   // Set operation
   AES_SET_OPERATION(op);
   // Starting loading of key or vector.
   AES_START();

   // loading the data (key or vector)
   for(i = 0; i < 16; i++){
      ENCDI = pData[i];
   }

   return;
}



static void halAesOperation(uint8 oper,uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector)
{
   uint16 i;
   uint8 j, k;
   uint8 mode;
   uint16 nbrOfBlocks;
   uint16 convertedBlock;

   nbrOfBlocks = length / 0x10;

   if((length % 0x10) != 0){
      // length not multiplum of 16, convert one block extra with zeropadding
      nbrOfBlocks++;
   }

   // Loading the IV.
   halAesLoadBlock(pInitVector, AES_LOAD_IV);

   // Start the operation
   AES_SET_OPERATION(oper);

   // Getting the operation mode.
   mode = ENCCS & 0x70;

   for(convertedBlock = 0; convertedBlock < nbrOfBlocks; convertedBlock++){
      // Starting the conversion.
      AES_START();

      i = convertedBlock * 16;
      // Counter, Output Feedback and Cipher Feedback operates on 4 bytes and not 16 bytes.
      if((mode == AES_MODE_CFB) || (mode == AES_MODE_OFB) || (mode == AES_MODE_CTR)) {

         for(j = 0; j < 4; j++){
            // Writing the input data with zero-padding
            for(k = 0; k < 4; k++){
               ENCDI = ((i + 4*j + k < length) ? pDataIn[i + 4*j + k] : 0x00 );
            }

            // Read out data for every 4th byte
            for(k = 0; k < 4; k++){
               pDataOut[i + 4*j + k] = ENCDO;
            }

         }
      }
      else if (mode == AES_MODE_CBCMAC){
         // Writing the input data with zero-padding
         for(j = 0; j < 16; j++){
            ENCDI = ((i + j < length) ? pDataIn[i + j] : 0x00 );
         }
         // The last block of the CBC-MAC is computed by using CBC mode.
         if(convertedBlock == nbrOfBlocks - 2){
            AES_SET_MODE(AES_MODE_CBC);
            // wait for data ready
            halMcuWaitUs(1);
         }
         // The CBC-MAC does not produce an output on the n-1 first blocks
         // only the last block is read out
         else if(convertedBlock == nbrOfBlocks - 1){

             // wait for data ready
             halMcuWaitUs(1);
            for(j = 0; j < 16; j++){
               pDataOut[j] = ENCDO;
            }
         }
      } // ECB or CBC
      else{
         // Writing the input data with zero-padding
         for(j = 0; j < 16; j++){
            ENCDI = ((i+j < length) ? pDataIn[i+j] : 0x00 );
         }

         // wait for data ready
         halMcuWaitUs(1);

         // Read out data
         for(j = 0; j < 16; j++){
            pDataOut[i+j] = ENCDO;
         }
      }
   }
}

static void halAesEncrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector)
{
    AES_SET_MODE(mode);
    halAesOperation(AES_ENCRYPT,pDataIn,length,pDataOut,pInitVector);
}


static void halAesDecrypt(uint8 mode, uint8 *pDataIn, uint16 length, uint8 *pDataOut, uint8 *pInitVector)
{
    AES_SET_MODE(mode);
    halAesOperation(AES_DECRYPT,pDataIn,length,pDataOut,pInitVector);
}



static uint8 generateAuthData(uint8 c, uint8 *pIv, uint8 *pData, uint8 f, uint8 lm)
{
    uint8 i,j,l;

    // Prepare input for CBC-MAC, B0, Bi.... Bn
    memcpy(buf,pIv,16);             // B0

    // Create flag
    buf[0]=  0x01;                  // L'= L - 1 (L=2)
    if (f>0)
        buf[0]|= 0x40;              // Adata= f > 0
    buf[0]|=  ( (lm-2) / 2 ) <<3;   // M'= (lm-2) / 2;

    buf[14]= 0x00;
    buf[15]= c;

    // Add authentication data
    buf[16]= 0;
    buf[17]= f;
    memcpy(&buf[18],pData,f);

    // .... zero padding
    i= 18 + f;
	l= (i & 0x0f )==0 ? i : (i&0xf0) + 0x10;
    while(i<l)
        buf[i++]= 0x00;

    // Add command payload
    j= 0;
    while(i<l+c) {
        buf[i++]= pData[f+j];
        j++;
    }

    return i;
}

/***********************************************************************************
  Copyright 2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED 揂S IS

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久66热re国产| 精品国产亚洲在线| 中文字幕在线不卡一区二区三区| 精品无人区卡一卡二卡三乱码免费卡| 91精品久久久久久久91蜜桃 | 精品电影一区二区三区| 久久精品一区八戒影视| 日韩电影一二三区| 在线不卡免费av| 国产麻豆一精品一av一免费 | 欧美日韩和欧美的一区二区| 视频在线观看一区二区三区| 777xxx欧美| 国产精品88888| 欧美高清在线精品一区| 99久久99久久综合| 日产欧产美韩系列久久99| 久久久久久99精品| 99久久久久免费精品国产| 亚洲国产成人porn| 一区二区在线观看不卡| 欧美在线视频全部完| 精品一区二区三区日韩| 美脚の诱脚舐め脚责91| 国产精品久久久一本精品| 宅男噜噜噜66一区二区66| 成人精品视频.| 视频一区二区三区在线| 国产精品久久久久久久久搜平片| 欧美色偷偷大香| eeuss国产一区二区三区| 免费成人av资源网| 亚洲国产综合91精品麻豆| 久久精品网站免费观看| 精品国产制服丝袜高跟| 在线不卡的av| 欧美一区二区三区免费在线看| 色婷婷av一区二区| 色综合久久久久久久久| 国产高清亚洲一区| 奇米一区二区三区| 日韩高清不卡在线| 亚洲观看高清完整版在线观看| 中文字幕日本乱码精品影院| 国产精品天美传媒沈樵| 这里只有精品视频在线观看| 欧美色综合网站| 欧美日韩一级黄| 欧美视频一区二区三区四区| 在线视频你懂得一区二区三区| 91在线观看成人| 91电影在线观看| 欧美丝袜自拍制服另类| 日韩亚洲欧美一区二区三区| 日韩欧美国产一区二区在线播放| 欧美刺激脚交jootjob| 精品欧美乱码久久久久久| 亚洲成人精品一区二区| 午夜精品久久久久久久| 久久精品99国产精品| 国产精品一区二区视频| 成人毛片视频在线观看| 精品视频在线视频| 日韩免费观看2025年上映的电影| 精品国产免费人成电影在线观看四季 | 在线精品亚洲一区二区不卡| 欧美三级视频在线| 久久久综合网站| 亚洲高清免费观看| 成人免费视频网站在线观看| 91在线视频观看| 亚洲精品在线免费播放| 一区二区三区四区五区视频在线观看| 精品一区二区三区免费播放| 国产91在线观看| 337p亚洲精品色噜噜噜| 国产农村妇女毛片精品久久麻豆| 亚洲国产成人tv| 93久久精品日日躁夜夜躁欧美| 在线播放国产精品二区一二区四区| 久久综合色8888| 蜜桃av一区二区在线观看 | 亚洲欧洲99久久| 日日摸夜夜添夜夜添精品视频| 丁香激情综合国产| 26uuu亚洲综合色欧美| 热久久久久久久| 欧美日韩三级一区二区| 国产精品素人一区二区| 国产精品一线二线三线精华| 欧美日韩性生活| 欧美激情一区二区| 黑人精品欧美一区二区蜜桃| 日韩精品一区二区在线观看| 午夜久久久久久久久| 成人激情小说网站| 国产亚洲欧美日韩在线一区| 国产成人免费xxxxxxxx| 国产精品久99| 99久久夜色精品国产网站| 欧美国产视频在线| hitomi一区二区三区精品| 国产农村妇女精品| 色94色欧美sute亚洲线路二| 亚洲国产成人tv| 日韩视频免费直播| 国产乱子伦视频一区二区三区| 欧美成人性战久久| 成人av手机在线观看| 亚洲图片有声小说| 欧美性受极品xxxx喷水| 日韩黄色在线观看| 久久中文娱乐网| av爱爱亚洲一区| 午夜精品一区在线观看| 26uuu亚洲综合色| 在线免费观看日本欧美| 麻豆精品一区二区三区| 日韩亚洲欧美一区二区三区| 成人小视频免费在线观看| 亚洲成人7777| 久久久久99精品一区| 欧美性色欧美a在线播放| 国内成人自拍视频| 日韩vs国产vs欧美| 99精品久久99久久久久| 国产精品久久久久天堂| 欧美成人精品1314www| 欧美一二三四区在线| 日韩欧美123| 日本一区二区三区久久久久久久久不| 欧美色综合网站| 成人中文字幕电影| 免费高清不卡av| 亚洲影院免费观看| 中文字幕国产一区| 日韩一区二区麻豆国产| 粉嫩13p一区二区三区| 婷婷亚洲久悠悠色悠在线播放 | 欧美久久久久免费| 色综合久久中文字幕| 99久久免费精品| 欧美亚洲免费在线一区| 精品国产伦一区二区三区观看方式 | 日本一区二区三区在线不卡 | 亚洲国产一区二区三区青草影视| 综合分类小说区另类春色亚洲小说欧美| 久久久久久久久久久99999| 久久久午夜电影| 亚洲私人黄色宅男| 丝袜亚洲精品中文字幕一区| 久久精品国产亚洲一区二区三区| 九九九精品视频| www.亚洲免费av| 欧洲激情一区二区| 日韩欧美国产三级| 亚洲欧美综合在线精品| 午夜视频在线观看一区二区 | 中文字幕不卡在线播放| 欧美国产一区视频在线观看| 亚洲不卡一区二区三区| 日本不卡视频在线观看| 91麻豆自制传媒国产之光| 久久精品人人做人人综合| 亚洲成在人线免费| 欧美性受极品xxxx喷水| 久久综合色天天久久综合图片| 综合亚洲深深色噜噜狠狠网站| 亚洲欧美国产三级| 国产精品一区二区在线看| 91成人免费在线视频| 日韩欧美色综合网站| 亚洲综合一二三区| 国产一区二区久久| 精品精品国产高清一毛片一天堂| 久久久久久久av麻豆果冻| 麻豆精品视频在线观看免费| 欧美三区在线视频| 亚洲男人天堂av| 欧美中文字幕久久| 一卡二卡三卡日韩欧美| 欧美性做爰猛烈叫床潮| 亚洲视频一二三| 成人av电影在线网| 亚洲激情在线播放| 欧美日韩另类国产亚洲欧美一级| 亚洲1区2区3区4区| 欧美日本乱大交xxxxx| 奇米亚洲午夜久久精品| 日韩欧美激情四射| 成人黄色大片在线观看| 国产精品理伦片| 一道本成人在线| 亚洲一区二区精品久久av| 8v天堂国产在线一区二区| 蜜臀av性久久久久av蜜臀妖精| 欧美成人官网二区| 91日韩一区二区三区| 亚洲成人一区在线| 久久影院午夜论|