STM32:en.x-cube-cryptolib ST加密库在Truestudio中的移植以及加密例程的使用

tech2025-12-05  20

目录

一、en.x-cube-cryptolib ST加密库下载地址

二、在Truestudio中的移植步骤

1、加密库的移植 

2、Truestudio中的设置

(1)路径的添加,将STM32加密库的 inc文件夹添加到编译路径中

(2)将对应的 .a库文件添加到工程中

(3)Truestudio修改配置

三、加密例程的使用

1、直接查看官方库Project对应给的你需要的加密类型的例程

(1)如AES-ECB


一、en.x-cube-cryptolib ST加密库下载地址

链接:https://pan.baidu.com/s/1Eo-1i4nhqGZOahPpXQ4Aug 提取码:o97c 

二、在Truestudio中的移植步骤

将STM32加密库放在自己工程目录下添加STM32加密库的头文件路劲(仅需要添加STM32加密库的 inc文件夹)添加STM32加密库的lib文件                                                                                                                                                  使用前,添加#include "crypto.h",然后根据加密库给的对应的例程,使用对应的加密解密函数

1、加密库的移植 

自己根据工程情况来选择使用哪一个固件库包:

AccHw_Crypto 适用部分带有硬件加速STM32 即带有FPU的STM32可以选择使用该库 Fw_Crypto        则是STM32全系列通用的固件库建议:带有FPU例如:STM32F4系列 使用 AccHw_Crypto 中的 libSTM32CryptographicV3.0.0_CM4_GCC_FPU.a 库。即:带有FPU的尽量使用带有FPU的库,没有的再自行选择使用通用的。

下面使用的是通用加密固件库作为示例:

Middlewares/ST/目录下 


2、Truestudio中的设置

(1)路径的添加,将STM32加密库的 inc文件夹添加到编译路径中

(2)将对应的 .a库文件添加到工程中

(3)Truestudio修改配置

如果直接编译可能会报如下错误:

主要原因:选择了不合适的加密库,可以做一些修改然后继续使用该库,如果修改配置过后还是与工程有冲突,请更换带有硬件加速的加密库。


修改配置步骤:

将C/C++ Build 、Settings、ToolSettings 中,将所有Target中的 Floating point 从Hardware implementation改成 Mix HW/SW implementation,然后就可以编译通过了。

 注:除了以上的解决方法外,就本人用的芯片(STM32F407VG)而言,出现上述错误后,更改了配置还是跟工程有冲突,后来更换带有硬件加速的加密库  libSTM32CryptographicV3.0.0_CM4_GCC_FPU.a 就没有出错了。

三、加密例程的使用

1、直接查看官方库Project对应给的你需要的加密类型的例程

(1)如AES-ECB

#include "crypto.h" /* Private typedef -----------------------------------------------------------*/ typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; /* Private define ------------------------------------------------------------*/ #define PLAINTEXT_LENGTH 64 /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ const uint8_t Plaintext[PLAINTEXT_LENGTH] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; /* Key to be used for AES encryption/decryption */ uint8_t Key[CRL_AES256_KEY] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4, }; /* Buffer to store the output data */ uint8_t OutputMessage[PLAINTEXT_LENGTH]; /* Size of the output data */ uint32_t OutputMessageLength = 0; const uint8_t Expected_Ciphertext[PLAINTEXT_LENGTH] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8, 0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26, 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70, 0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9, 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d, 0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff, 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7 }; /* Private function prototypes -----------------------------------------------*/ int32_t STM32_AES_ECB_Encrypt(uint8_t* InputMessage, uint32_t InputMessageLength, uint8_t *AES256_Key, uint8_t *OutputMessage, uint32_t *OutputMessageLength); int32_t STM32_AES_ECB_Decrypt(uint8_t* InputMessage, uint32_t InputMessageLength, uint8_t *AES256_Key, uint8_t *OutputMessage, uint32_t *OutputMessageLength); TestStatus Buffercmp(const uint8_t* pBuffer, uint8_t* pBuffer1, uint16_t BufferLength); /* Private function -----------------------------------------------*/ /** * @brief AES ECB Encryption example. * @param InputMessage: pointer to input message to be encrypted. * @param InputMessageLength: input data message length in byte. * @param AES128_Key: pointer to the AES key to be used in the operation * @param OutputMessage: pointer to output parameter that will handle the encrypted message * @param OutputMessageLength: pointer to encrypted message length. * @retval error status: can be AES_SUCCESS if success or one of * AES_ERR_BAD_INPUT_SIZE, AES_ERR_BAD_OPERATION, AES_ERR_BAD_CONTEXT * AES_ERR_BAD_PARAMETER if error occured. */ int32_t STM32_AES_ECB_Encrypt(uint8_t* InputMessage, uint32_t InputMessageLength, uint8_t *AES256_Key, uint8_t *OutputMessage, uint32_t *OutputMessageLength) { AESECBctx_stt AESctx; uint32_t error_status = AES_SUCCESS; int32_t outputLength = 0; /* Set flag field to default value */ AESctx.mFlags = E_SK_DEFAULT; /* Set key size to 32 (corresponding to AES-256) */ AESctx.mKeySize = 32; /* Initialize the operation, by passing the key. * Third parameter is NULL because ECB doesn't use any IV */ error_status = AES_ECB_Encrypt_Init(&AESctx, AES256_Key, NULL ); /* check for initialization errors */ if (error_status == AES_SUCCESS) { /* Encrypt Data */ error_status = AES_ECB_Encrypt_Append(&AESctx, InputMessage, InputMessageLength, OutputMessage, &outputLength); if (error_status == AES_SUCCESS) { /* Write the number of data written*/ *OutputMessageLength = outputLength; /* Do the Finalization */ error_status = AES_ECB_Encrypt_Finish(&AESctx, OutputMessage + *OutputMessageLength, &outputLength); /* Add data written to the information to be returned */ *OutputMessageLength += outputLength; } } return error_status; } /** * @brief AES ECB Decryption example. * @param InputMessage: pointer to input message to be decrypted. * @param InputMessageLength: input data message length in byte. * @param AES128_Key: pointer to the AES key to be used in the operation * @param OutputMessage: pointer to output parameter that will handle the decrypted message * @param OutputMessageLength: pointer to decrypted message length. * @retval error status: can be AES_SUCCESS if success or one of * AES_ERR_BAD_INPUT_SIZE, AES_ERR_BAD_OPERATION, AES_ERR_BAD_CONTEXT * AES_ERR_BAD_PARAMETER if error occured. */ int32_t STM32_AES_ECB_Decrypt(uint8_t* InputMessage, uint32_t InputMessageLength, uint8_t *AES256_Key, uint8_t *OutputMessage, uint32_t *OutputMessageLength) { AESECBctx_stt AESctx; uint32_t error_status = AES_SUCCESS; int32_t outputLength = 0; /* Set flag field to default value */ AESctx.mFlags = E_SK_DEFAULT; /* Set key size to 32 (corresponding to AES-256) */ AESctx.mKeySize = 32; /* Initialize the operation, by passing the key. * Third parameter is NULL because ECB doesn't use any IV */ error_status = AES_ECB_Decrypt_Init(&AESctx, AES256_Key, NULL ); /* check for initialization errors */ if (error_status == AES_SUCCESS) { /* Decrypt Data */ error_status = AES_ECB_Decrypt_Append(&AESctx, InputMessage, InputMessageLength, OutputMessage, &outputLength); if (error_status == AES_SUCCESS) { /* Write the number of data written*/ *OutputMessageLength = outputLength; /* Do the Finalization */ error_status = AES_ECB_Decrypt_Finish(&AESctx, OutputMessage + *OutputMessageLength, &outputLength); /* Add data written to the information to be returned */ *OutputMessageLength += outputLength; } } return error_status; } /** * @brief Compares two buffers. * @param pBuffer, pBuffer1: buffers to be compared. * @param BufferLength: buffer's length * @retval PASSED: pBuffer identical to pBuffer1 * FAILED: pBuffer differs from pBuffer1 */ TestStatus Buffercmp(const uint8_t* pBuffer, uint8_t* pBuffer1, uint16_t BufferLength) { while (BufferLength--) { if (*pBuffer != *pBuffer1) { return FAILED; } pBuffer++; pBuffer1++; } return PASSED; }

 

最新回复(0)