Skip to main content
JVida.18
Associate II
June 9, 2019
Question

Use of STM32L4 HAL_CRYP hardware in LORAWAN

  • June 9, 2019
  • 3 replies
  • 1356 views

I've tried to find an example code of use of crypto harware accelerator in LORAWAN but i haven't been able to find any . Does someone have an example expecially concerning the use of AES CMAC?

Regards

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
June 9, 2019

If you have a chip with the CYPT unit the examples for the EVAL, and other, boards can be ported into other applications and use cases.

STM32Cube_FW_L4_V1.14.0\Projects\STM32L476G-EVAL\Examples\CRYP\CRYP_GCM_GMAC_CMAC_Modes

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
JVida.18
JVida.18Author
Associate II
June 9, 2019

Thank you, the example is nice for CMAC implementation, regards!

JVida.18
JVida.18Author
Associate II
June 15, 2019

still strugglign with LORAWAN CMAC... i've made this code, but i'm making a mistake...

// original code modified

void LoRaMacComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint32_t *mic )

{

  MicBlockB0[5] = dir;

  MicBlockB0[6] = ( address ) & 0xFF;

  MicBlockB0[7] = ( address >> 8 ) & 0xFF;

  MicBlockB0[8] = ( address >> 16 ) & 0xFF;

  MicBlockB0[9] = ( address >> 24 ) & 0xFF;

  MicBlockB0[10] = ( sequenceCounter ) & 0xFF;

  MicBlockB0[11] = ( sequenceCounter >> 8 ) & 0xFF;

  MicBlockB0[12] = ( sequenceCounter >> 16 ) & 0xFF;

  MicBlockB0[13] = ( sequenceCounter >> 24 ) & 0xFF;

  MicBlockB0[15] = size & 0xFF;

#ifdef USE_HAL_CRYP_MODULE

AES128_CMAC_digest(buffer, size,MicBlockB0, key, Mic);

#else

  AES_CMAC_Init( AesCmacCtx );

  AES_CMAC_SetKey( AesCmacCtx, key );

  AES_CMAC_Update( AesCmacCtx, MicBlockB0, LORAMAC_MIC_BLOCK_B0_SIZE )

  AES_CMAC_Update( AesCmacCtx, buffer, size & 0xFF )

  AES_CMAC_Final( Mic, AesCmacCtx );

#endif

  *mic = ( uint32_t )( ( uint32_t )Mic[3] << 24 | ( uint32_t )Mic[2] << 16 | ( uint32_t )Mic[1] << 8 | ( uint32_t )Mic[0] );

}

//************* then the function using hardware CMAC looks like *****************************

void __attribute__((section(".OSmsk"))) AES128_CMAC_digest(uint8_t* input, uint16_t size, uint8_t* MicBlockB0, const uint8_t* key, uint8_t* output){

hcrypt.Init.DataType   = CRYP_DATATYPE_8B ;

hcrypt.Init.KeySize    = CRYP_KEYSIZE_128B ;

hcrypt.Init.pKey     = key;

hcrypt.Init.OperatingMode = CRYP_ALGOMODE_TAG_GENERATION;

hcrypt.Init.ChainingMode = CRYP_CHAINMODE_AES_CMAC;

hcrypt.Init.GCMCMACPhase = CRYP_GCMCMAC_HEADER_PHASE;

hcrypt.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;

hcrypt.Init.Header    = (uint8_t *)input;

  hcrypt.Init.HeaderSize  = CMAC_HEADER_SIZE;

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

   /* De-initialize then initialize the AES peripheral             */

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

  if (HAL_CRYP_DeInit(&hcrypt) != HAL_OK)

  {

      breakpoint();

  }

  /* Set the CRYP parameters */

  if (HAL_CRYP_Init(&hcrypt) != HAL_OK)

  {

     breakpoint();

  }

  /*----------------------------------------------------------------------------------------------*/

  /* CMAC header phase */

  if (MicBlockB0!=NULL){

   if (HAL_CRYPEx_AES_Auth(&hcrypt, (uint8_t *)MicBlockB0, CMAC_B_SIZE, NULL, 100)!= HAL_OK)

   {

   breakpoint();

   }

  }

  /*----------------------------------------------------------------------------------------------*/

  /* CMAC final phase */

  hcrypt.Init.GCMCMACPhase = CRYP_GCMCMAC_FINAL_PHASE;

  if (HAL_CRYPEx_AES_Auth(&hcrypt, (uint8_t *)input, size, NULL, 100)!= HAL_OK)

  {

   breakpoint();

  }

}

Does anyone made LORAWAN CMAC working using STM32 hardware HAL_CRYP?

Regards