Search code examples
cstm32hal

How to set and delete AES key within AES HAL


I have the following auto-generated (from HAL) pKeyAES array (there is also an initVectorAES that looks the same):

__ALIGN_BEGIN static const uint8_t pKeyAES[16] __ALIGN_END = {
                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                        0x00,0x00,0x00,0x00,0x00,0x00};

Along with the init function such as:

void Hal_MX_init (void){
  hcryp.Instance = AES;
  hcryp.Init.DataType = CRYP_DATATYPE_8B;
  hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
  hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  hcryp.Init.pKey = (uint8_t *)pKeyAES;
  hcryp.Init.pInitVect = (uint8_t *)pInitVectAES;
  if (HAL_CRYP_Init(&hcryp) != HAL_OK)
  {
    Error_Handler();
  }
}

Now I want to create a new function in this file generated by the HAL so that the "key" is always kept within the HAL. To do this I am thinking of using the pKeyAES to always keep the "key". However, I am not really sure on how to do this; for instace, if I want to create a new "set key" or "delete key" method, how would this look like when doing it within the HAL?

For instance, for "set key" I think it would be best to store it within the pKeyAES (having an in-param for getting the new key). And for "delete key" function, I would suspect you could "delete"/reset anything that is currently stored in pKeyAES array? However, I'm not entirely sure how to accomplish this code-wise.

Also, when doing "set-key" would I need to do something with the "initVector" that is provided as well, or just keep it as it is (currently holding the same value as the pKeyAES).

Any help, tips etc. Would be most helpful.


I can guess that I have to create a new init function for set key, such as "void setKey_init (){}" and add the same stuff from hcryp such as in the example code provided above; but where I have the new Key as a param input variable. hcryp.Init.pKey is a uint8_t pointer key; where I want my new key to be set. But my problem is how it would look code-wise to make it the most efficient when setting a new key here. I don't want to point to a key outside of my aes.c file (risky), but rather have a key sent in that I can add to the struct and init with the function.


Solution

  • If I understand correctly, it should be quite simple. Having a pointer to a new key (const uint8_t *pNewKeyAES), copy it to the pKeyAES storage and perform the initialization. In the following, if pNewKeyAESis NULL, the key is set to the all-zeroes key (corresponding to deleting the key):

    #include <string.h>
    #include <stdint.h>
    
    // Discard `const` from pKeyAES to be able to modify it
    __ALIGN_BEGIN static uint8_t pKeyAES[16] __ALIGN_END = {
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00};
    
    void setKey_init (const uint8_t *pNewKeyAES)
    {
        if(pNewKeyAES != NULL)
        {  // Use new key
            memcpy(pKeyAES, pNewKeyAES, CRYP_KEYSIZE_128B);
        }
        else
        {  // Delete key
            memset(pKeyAES, 0, CRYP_KEYSIZE_128B);
        }
        Hal_MX_init();  // As defined in the question, may also copy contents here
    }