cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F423 Generating AuthTag for AES crypto is not working

milan23
Associate II

Hi All,

I have a problem with tag generating for CRYP_AES_GCM_GMAC. The main crypting is fully working, but AuthTag is generated wrongly. It is strongly depend when the generating is started. When you change for (int i = 0; i < 10000; i++) {}; you will receive different values. I have no idea, what is wrong. (CRC clocks on)

Supposed data:

uint8_t tag[16] = { 19, 74, 107, 250, 55, 74, 33, 198, 182, 189, 43, 1, 146, 146, 24, 92 };

Milan

void TestCryptoAES(void)

{

   CRYP_HandleTypeDef hcryp;

   uint8_t InitVector[AES_BLOCK_SIZE] = { 10, 0, 0, 0, 4, 3, 2, 1, 100, 231, 96, 38, 0, 0, 0, 2 };

   uint8_t   AES128key[AES_BLOCK_SIZE] = { 118, 101, 114, 121, 118, 101, 114, 121, 115, 101, 99, 114, 116, 107, 101, 121 };

   // input "raw" data

   uint8_t   plainText[AES_BLOCK_SIZE] = { 1, 2, 0, 12, 1, 2, 3, 4, 0, 0, 0, 10, 0, 0, 0, 0, };

   // input crypted data

   uint8_t   CryptedText[AES_BLOCK_SIZE] = { 211, 160, 203, 62, 56, 207, 53, 7, 35, 108, 33, 133, 237, 185, 133, 82, };

   // uncrypted data

   uint8_t   header[AES_BLOCK_SIZE] = { 1, 16, 0, 44, 1, 2, 3, 4, 0, 0, 0, 10, 38, 96, 231, 100 };

   uint8_t InitVectorSwapped[AES_BLOCK_SIZE]; // = { 0 ,0 , 0 , 10 ,   1 , 2 , 3 , 4 ,    38 , 96 ,231, 100,     2 , 0 , 0 , 0 };

   uint8_t   AES128keySwapped[AES_BLOCK_SIZE]; // = { 121,114, 101, 118 ,   121, 114, 101, 118,    114, 99 ,101, 115,     121, 101, 107, 116 };

   uint8_t   outputText[AES_BLOCK_SIZE];

   uint8_t   outputTag[AES_BLOCK_SIZE];

   memset(outputText, 0, sizeof(outputText));

   memset(outputTag, 0, sizeof(outputTag));

   byteswap_ulong(InitVector, InitVectorSwapped, AES_BLOCK_SIZE);

   byteswap_ulong(AES128key, AES128keySwapped, AES_BLOCK_SIZE);

   hcryp.Instance = AES;

   hcryp.Init.DataType = CRYP_DATATYPE_8B;

   hcryp.Init.KeySize = CRYP_KEYSIZE_128B;

   hcryp.Init.pKey = (uint32_t*)AES128keySwapped;

   hcryp.Init.pInitVect = (uint32_t*)InitVectorSwapped;

   hcryp.Init.Algorithm = CRYP_AES_GCM_GMAC;

   hcryp.Init.Header = (uint32_t*)header;

   hcryp.Init.HeaderSize = 16;

   hcryp.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE;

   __HAL_RCC_AES_CLK_ENABLE();

   for (int i = 0; i < 10000; i++) {};

   if (HAL_CRYP_Init(&hcryp) != HAL_OK)

   {

       for (;;) {};

   }

   if (HAL_CRYP_Encrypt(&hcryp, (uint32_t*)plainText, 16, (uint32_t*)outputText, 99999) != HAL_OK)

   {

       for (;;) {};

   }

   /* Wait for processing to be done */

   while (HAL_CRYP_GetState(&hcryp) != HAL_CRYP_STATE_READY);

   /* Compute the authentication TAG */

   if (HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)outputTag, 99999) != HAL_OK)

   {

       /* Processing Error */

       for (;;) {};

   }

   while (HAL_CRYP_GetState(&hcryp) != HAL_CRYP_STATE_READY);

   for (;;) {};

}

2 REPLIES 2
NStro.2
Associate II

Is there a Solution?

I have the same issue but only if the length is not a multiple of 4!

Barmak
Associate II

Hi, Same issue here with the plain texts which are not multiples of 4 . Did you find any solution?