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

目录

一、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;
}

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: x-cube-mcsdk-ful_5.4.6.zip是一款蓝牙物联网软件开发套件。它通过使用蓝牙技术,将智能设备(如手机、平板电脑等)与其他设备(如传感器、灯、温度计等)连接起来,实现智能家居、智能照明等应用。此套件提供了多种模块,包括设备管理、蓝牙连接、数据传输等,方便开发者快速地构建自己的应用。此套件也可以进行二次开发,添加更多自定义功能。它支持多种开发板(如STM32)和操作系统(如Windows、Linux、macOS等),易于使用。此套件的功能丰富,适合各种物联网应用程序的开发。此外,它还具有高度移植性和灵活性,适合初学者和专业开发者使用。综上所述,x-cube-mcsdk-ful_5.4.6.zip是一款优秀的蓝牙物联网软件开发套件,将在未来智能家居、智能照明、智能医疗等应用扮演重要角色。 ### 回答2: x-cube-mcsdk-ful_5.4.6.zip是为STM32微控制器开发的全功能SDK。这个SDK提供了丰富的API和驱动程序,方便了开发人员在STM32上实现与云端的连接。通过这个SDK,开发人员可以快速地将他们的STM32电路板连接到云平台上,实现数据的收集和控制。这个SDK提供了许多有用的组件,如MQTT协议、OTA升级功能、数据推送、云端诊断等等,使得开发人员可以更容易地将STM32嵌入式系统与云服务进行集成。同时,这个SDK还提供了丰富的例程和API文档,使开发人员可以更好地理解和使用SDK。总之,x-cube-mcsdk-ful_5.4.6.zip是一款功能十分强大的SDK,可以显著地提高STM32嵌入式系统与云服务之间的连接效率和稳定性,从而满足用户对于互联设备的需求。 ### 回答3: x-cube-mcsdk-ful_5.4.6.zip是意法半导体提供的一款软件开发工具包,用于开发物联网设备和应用程序。该工具包提供了丰富的软件组件和示例应用,支持多种硬件平台和通信协议。使用x-cube-mcsdk-ful_5.4.6.zip可以简化物联网设备和应用程序的开发过程,提高开发效率和软件质量。该工具包主要包括以下几个方面: 1.物联网平台:x-cube-mcsdk-ful_5.4.6.zip提供了完整的物联网平台解决方案,支持设备和云端的快速集成和连接。 2.通信协议:x-cube-mcsdk-ful_5.4.6.zip支持MQTT、CoAP等通信协议,可以保证设备和云端的稳定通信和数据传输。 3.软件组件:x-cube-mcsdk-ful_5.4.6.zip包括了多个软件组件,如TLS、JSON解析、文件系统等,可以满足不同的开发需求,并且易于集成和使用。 4.示例应用:x-cube-mcsdk-ful_5.4.6.zip提供了多个示例应用,如温湿度监测、智能插座控制等,可以帮助开发者快速搭建自己的物联网应用。 总之,x-cube-mcsdk-ful_5.4.6.zip是一款非常实用的物联网开发工具包,它可以为开发者提供丰富的软件组件和示例应用,帮助他们快速搭建物联网设备和应用程序,实现设备和云端之间的通信和数据传输。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鱼的猫丿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值