/******************************************************************************
 * @fn      SSP_CCM_Auth_Encrypt
 *
 * @brief   Generates CCM Authentication tag U.
 *
 * input parameters
 * @param encrypt if set to 'true' then run encryption and set to 'flase' for
 * authentication only.
 * @param   Mval    - Length of authentication field in octets [0,2,4,6,8,10,12,14 or 16]
 * @param   N       - Pointer to 13-byte Nonce
 * @param   M       - Pointer to octet string 'm'
 * @param   len_m   - Length of M[] in octets
 * @param   A       - Pointer to octet string 'a'
 * @param   len_a   - Length of A[] in octets
 * @param   AesKey  - Pointer to AES Key or Pointer to Key Expansion buffer.
 * @param   Cstate  - Pointer to output buffer
 * @param   ccmLVal - ccm L Value to be used.
 *
 * output parameters
 *
 * @param   Cstate[]    - The first Mval bytes contain Authentication Tag T
 *
 * @return  ZStatus_t
 *
 */
uint8 SSP_CCM_Auth_Encrypt (bool encrypt, uint8 Mval, uint8 *N, uint8 *M, uint16 len_m, uint8 *A,
                    uint16 len_a, uint8 *AesKey, uint8 *Cstate, uint8 ccmLVal)
{

  unsigned char status;

  if((status = CCMAuthEncryptStart(encrypt, Mval, N, M,  len_m, A, len_a, 0,
                                       Cstate, ccmLVal, false)) != AES_SUCCESS)
  {
    return status;
  }

  do
  {
    ASM_NOP;
  }while(!(CCMAuthEncryptCheckResult()));

  if((status = CCMAuthEncryptGetResult(Mval, len_m, Cstate)) != AES_SUCCESS)
  {
    return status;
  }
  return AES_SUCCESS;
}
Example #2
0
//*****************************************************************************
//
// AES-CCM encrypt example
//
// param   bEncrypt           if set to true run encryption  
// param   pui8Key            pointer to Key
// param   ui8Mval            length of authentication
// param   pui8N              Nonce
// param   pui8M              input message
// param   ui16LenM           length of message
// param   pui8A              Additional data
// param   ui16LenA           length of additional data
// param   ui8KeyLocation     location in Key RAM. Must be one of
//                            the following:
//                            KEY_AREA_0
//                            KEY_AREA_1
//                            KEY_AREA_2
//                            KEY_AREA_3
//                            KEY_AREA_4
//                            KEY_AREA_5
//                            KEY_AREA_6
//                            KEY_AREA_7
// param   ui8CCMLVal         Lval for ccm
// param   ui8CCMIntEnable    set to true to enable interrupts and false to disable
// param   pui8ExpectedOutput pointer to expected output
// param   pui8Cstate         authentication tag
// param   ui8CCMIntEnable    set true/false to enable/disable interrupts
// param   pui8ExpectedOutput pointer to Expected Output
//
// return  AES_SUCCESS if successful
//
//*****************************************************************************
uint8_t CCMEncryptExample(bool bEncrypt,
                          uint8_t* pui8Key,
                          uint8_t ui8Mval,
                          uint8_t *pui8N,
                          uint8_t *pui8M,
                          uint16_t ui16LenM,
                          uint8_t *pui8A,
                          uint16_t ui16LenA,
                          uint8_t ui8KeyLocation,
                          uint8_t *pui8Cstate,
                          uint8_t ui8CCMLVal,
                          uint8_t ui8CCMIntEnable,
                          uint8_t *pui8ExpectedOutput)
{
    uint8_t status;
    if(ui8CCMIntEnable)
    {
        //
        // Register AES interrupt
        //
        IntRegister(INT_AES, CCMIntHandler);
        
        //
        // example using interrupt service routine
        //
        if((status = AESLoadKey((uint8_t*)pui8Key, ui8KeyLocation)) != 
           AES_SUCCESS)
        {
            return status;
        }
        
        if((status = CCMAuthEncryptStart (bEncrypt, ui8Mval, pui8N, pui8M, 
                                          ui16LenM, pui8A, ui16LenA,
                                          ui8KeyLocation, pui8Cstate, 
                                          ui8CCMLVal, ui8CCMIntEnable)) 
           != AES_SUCCESS)
        {
            return status;
        }
        
        //
        // wait for completion of the operation
        //
        do
        {
            ASM_NOP;
        }while(ui8CCMIntHandler == 0 ); 
        
        ui8CCMIntHandler = 0;
        
        if((status = CCMAuthEncryptGetResult(ui8Mval, ui16LenM, pui8Cstate)) 
           != AES_SUCCESS)
        {
            return status;
        }
    }
    else
    {
        //
        // Unregister AES interrupt
        //
        IntUnregister(INT_AES);
        
        // 
        // example using polling
        //
        if((status = AESLoadKey((uint8_t*)pui8Key, ui8KeyLocation)) 
           != AES_SUCCESS)
        {
            return status;
        }
        if((status = CCMAuthEncryptStart(bEncrypt, ui8Mval, pui8N, pui8M, 
                                          ui16LenM, pui8A, ui16LenA,
                                          ui8KeyLocation, pui8Cstate, 
                                          ui8CCMLVal, ui8CCMIntEnable)) 
           != AES_SUCCESS)
        {
            return status;
        }
        
        //
        // wait for completion of the operation
        //
        do
        {
            ASM_NOP;
        }while(!(CCMAuthEncryptCheckResult()));
        
        
        if((status = CCMAuthEncryptGetResult(ui8Mval, ui16LenM, pui8Cstate)) != 
           AES_SUCCESS)
        {
            return status;
        }
    }
    
    if (CCMMemCmp(pui8M,  (uint8_t const *)pui8ExpectedOutput, ui16LenM) == 
        false)
    {
        return AES_CCM_TEST_ERROR;
    }
    
    //
    // Verify CCM output
    //
    if(bEncrypt)
    {
        if (CCMMemCmp(pui8Cstate, (uint8_t const *)pui8ExpectedOutput + 
                      ui16LenM, ui8Mval) == false)
        {
            return AES_CCM_TEST_ERROR;
        }
    }
    else
    {
        if (CCMMemCmp(pui8Cstate, (uint8_t const *)pui8ExpectedOutput,
                      ui8Mval) == false)
        {
            return AES_CCM_TEST_ERROR;
        }
    }
    
    return AES_SUCCESS;
}