4.3.2 AES Engine
Use the AES engine to encrypt/decrypt a stream of bytes. Using the HAL APIs, the user can perform Cipher Block Chaining (CBC) or Electronic Codebook (ECB) encryption and decryption with the
HAL_AES_EncryptReq()
function. The following code is an explanation of the inputs with an example:void HAL_AES_EncryptReq(HAL_AES_EncryptReq_t *reqParams_s)
The user can perform the following actions in the enum with the above API:
/** Defines HAL AES commands such as setkey, cbc encrypt, cbc decrypt etc */ typedef enum { SM_SET_KEY_COMMAND = 0, SM_ECB_ENCRYPT_COMMAND, SM_CBC_ENCRYPT_COMMAND, SM_ECB_DECRYPT_COMMAND, SM_CBC_DECRYPT_COMMAND, } HAL_AesCmd_t;
- To set the key in the AES engine before performing encryption/decryption:
/* Declare a memory for Encrypt request*/ HAL_AES_EncryptReq_t appSetKeyReq; /* define a callback after key is set*/ void setAesKeyConf(void) { .. } appSetKeyReq.aesCmd = SM_SET_KEY_COMMAND; appSetKeyReq.text = &key; // 16 bytes of key appSetKeyReq.blockcount = 1; // 128-bit block appSetKeyReq.encryptConf = setAesKeyConf; HAL_AES_EncryptReq(&appSetKeyReq);
- To perform ECB encryption after setting the key:
/* Declare a memory for Encrypt request*/ HAL_AES_EncryptReq_t appEncryptReq; /* define a callback after key is set*/ void aesEncryptConf(void) { // Action to be taken after the encryption is completed for a block .. .. } appEncryptReq.aesCmd = SM_ECB_ENCRYPT_COMMAND; appEncryptReq.text = &text; // 16 bytes of text to encrypt appEncryptReq.blockcount = 1; // 128-bit block appEncryptReq.encryptConf = aesEncryptConf; HAL_AES_EncryptReq(&appEncryptReq);