示例#1
0
/******************************************************************************
 * @fn      ssp_HW_KeyInit
 *
 * @brief   Writes the key into AES engine
 *
 * input parameters
 *
 * @param   AesKey  - Pointer to AES Key.
 *
 * @return  None
 */
void ssp_HW_KeyInit( uint8 *AesKey )
{
  /* Load the AES key 
   * KeyStore has rentention after PM2 
   */
  AESLoadKey( (uint8 *)AesKey, 0);
}
示例#2
0
//*****************************************************************************
//
// brief   AES-CCM decrypt example
//
// param   bDecrypt           if set to true run decryption
// param   pui8Key            pointer to Key
// param   ui8Mval            length of authentication
// param   pui8N              Nonce
// param   C                  input encrypted message
// param   ui16LenC           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 CCMDecryptExamples(bool bDecrypt,
                              uint8_t* pui8Key,
                              uint8_t ui8Mval,
                              uint8_t *pui8N,
                              uint8_t *pui8C,
                              uint16_t ui16LenC,
                              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)
    {    
        //
        // example using interrupt service routine
        //
        if((status = AESLoadKey((uint8_t*)pui8Key, ui8KeyLocation)) != 
           AES_SUCCESS)
        {
            return status;
        }
        
        if((status = CCMInvAuthDecryptStart(bDecrypt, ui8Mval, pui8N, pui8C, 
                                            ui16LenC, 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 = CCMInvAuthDecryptGetResult(ui8Mval, pui8C, ui16LenC, 
                                                pui8Cstate)) != AES_SUCCESS)
        {
            return status;
        }
    } 
    else
    {
        //
        // example using polling
        //
        if((status = AESLoadKey((uint8_t*)pui8Key, 
                                ui8KeyLocation)) != AES_SUCCESS)
        {
            return status;
        }
        if((status = CCMInvAuthDecryptStart(bDecrypt, ui8Mval, pui8N, pui8C, 
                                            ui16LenC, pui8A, ui16LenA, 
                                            ui8KeyLocation, pui8Cstate, 
                                            ui8CCMLVal, 
                                            ui8CCMIntEnable))!= AES_SUCCESS )
        {
            return status;
        }
        
        //
        // wait for completion of the operation
        //
        do
        {
            ASM_NOP;
        }while(!(CCMInvAuthDecryptCheckResult()));
        
        if((status = CCMInvAuthDecryptGetResult(ui8Mval, pui8C, ui16LenC, 
                                                pui8Cstate)) != AES_SUCCESS)
        {
            return status;
        }
    }
    
    //
    // Verify CCM output
    //
    if (CCMMemCmp(pui8C,  (uint8_t const *)pui8ExpectedOutput, 
                  (ui16LenC-ui8Mval)) == false)
    {
        return AES_CCM_TEST_ERROR;
    }
    
    return AES_SUCCESS;
}
示例#3
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;
}