Skip to main content
Associate II
December 9, 2024
Question

HAL_CRYPEx_AES() maximum number of output bytes?

  • December 9, 2024
  • 1 reply
  • 620 views

I am using an STM32L4S9ZIJX part and attempting to encrypt a 256KB buffer (actually 262,144 - 16 = 262,128 bytes) using AES256 CTR mode in polling mode.

I created a wrapper function to perform the encryption:

 

int encrypt_CTR_mode(uint8_t* key, uint8_t* IV, uint8_t* plaintext, uint32_t pt_length,
 uint8_t* ciphertext)
{
 // Deinit old parameters
 if (HAL_CRYP_DeInit(&hcryp) != HAL_OK)
 {
 LOG(error, "ERROR: HAL_CRYP_DeInit() failed\n");
 return -1;
 }

 // Setup new params
 hcryp.Init.DataType = CRYP_DATATYPE_32B;
 hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
 hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
 hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
 hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
 hcryp.Init.pKey = key;
 hcryp.Init.pInitVect = IV;

 // Init new parameters
 if (HAL_CRYP_Init(&hcryp) != HAL_OK)
 {
 LOG(error, "ERROR: HAL_CRYP_Init() failed\n");
 return -1;
 }

 // Encrypt plaintext
 HAL_StatusTypeDef rc;
 rc = HAL_CRYPEx_AES(&hcryp, plaintext, pt_length, ciphertext, 5000);
 if (rc != HAL_OK)
 {
 LOG(error, "ERROR: HAL_CRYPEx_AES() -- %d\n", rc);
 return -1;
 }

 return 0;
}

 

 

The function returns successfully but I only get 65,520 (0xFFF0) output bytes even though I'm passing in 262,128 to HAL_CRYPEx_AES().  Is there some limitation for the maximum output length of HAL_CRYPEx_AES()?  I can't find any such limitation documented.

 

 

1 reply

dsorberAuthor
Associate II
December 9, 2024

Whoops... I see my problem: HAL_CRYPEx_AES() size parameter is a uint16_t.

Tesla DeLorean
Guru
December 9, 2024

Thankfully the source is exposed, and deeper hcryp->CrypInCount is uint32_t

You could perhaps also decompose the data into blocks

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..