cancel
Showing results for 
Search instead for 
Did you mean: 

"AES_CMAC_Encrypt_Finish" function always return bad_context error

NCho
Associate

Hello. All

I'm using STM32L151 MCU, stm32 crypto library v.3.0.0.

I implemented CMAC according to the sample code but it returned "AES_ERR_BAD_CONTEXT". 

I could not find any reason.. please let me know to solve this problem.!!

Here is my code for CMAC encryption. 

static AESCMACctx_stt AEScmacCtx = { 0 };

// Static function declarations -----------------------

// Static function definitions ------------------------

static int32_t AesCmacInit( uint8_t * key )

{

int32_t result = AES_SUCCESS;

AEScmacCtx.mFlags = E_SK_DEFAULT; // 0x00000000

AEScmacCtx.mKeySize = CRL_AES128_KEY; // 16

AEScmacCtx.pmKey = key;

AEScmacCtx.mTagSize = CRL_AES_BLOCK; // 16

result = AES_CMAC_Encrypt_Init(&AEScmacCtx);

return result;

}

// Module interface function definitions --------------

int32_t AesCmacEncrypt( uint8_t * plainData, int32_t plainSize, uint8_t * key, uint8_t * cmacData, int32_t * cmacSize )

{

int32_t result = AES_SUCCESS;

result = AesCmacInit( key );

if( result != AES_SUCCESS )

{

return result

}

AEScmacCtx.mFlags |= E_SK_FINAL_APPEND; // 0x00000020

result = AES_CMAC_Encrypt_Append(&AEScmacCtx, plainData, plainSize );

if( result != AES_SUCCESS )

{

return result;

}

result = AES_CMAC_Encrypt_Finish(&AEScmacCtx, cmacData, cmacSize);

if( result != AES_SUCCESS) // <- always return AES_ERR_BAD_CONTEXT

{

return result;

}

return result;

}

1 REPLY 1
NCho
Associate

I found it!! ​

To use CMAC, should define All 3 types of AES in "config.h"

#define INCLUDE_AES128      ((uint16_t)0x0004) /*!< AES functions with key size of 128 bit are included in the library. */

#define INCLUDE_AES192      ((uint16_t)0x0008) /*!< AES functions with key size of 192 bit are included in the library. */

#define INCLUDE_AES256      ((uint16_t)0x0010) /*!< AES functions with key size of 256 bit are included in the library. */