[其他ST产品] 移植STM32官方加密库STM32Cryptographic

[复制链接]
 楼主| 实际测量不符 发表于 2023-11-28 14:48 | 显示全部楼层 |阅读模式
概述
ST官方在很多年前就推出了自己的加密库,配合ST芯片用起来非常方便,支持ST的所有MCU,官方已经给出了例程,移植起来非常简单方便,其他厂家Cotex-M内核芯片应该也可以使用吧,没试过,各位看官可以试一下,我使用的是最新版的V4.0.2 / 13-March-2023,下面也是以该版本进行移植。

3845465658ce730a46.png

STM32 Cryptographic V4版本下载(建议使用V4,比上一代优化了):

X-CUBE-CRYPTOLIB - STM32 cryptographic firmware library software expansion for STM32Cube - STMicroelectronics

V3版本下载:

X-CUBE-CRYPTO-V3 - STM32 cryptographic firmware library software expansion for STM32Cube (UM1924) - STMicroelectronics



所有Cryptolib V4相关的文档都通过STM32 MCU Wiki以网页形式提供,不再提供单独的UM和其他文档,官方文档:


Category:Cryptographic library - stm32mcu (stmicroelectronics.cn)



 楼主| 实际测量不符 发表于 2023-11-28 14:49 | 显示全部楼层
V3版本与V4版本比较

7660965658d4f176da.png

5936665658d5bbba7f.png
 楼主| 实际测量不符 发表于 2023-11-28 14:49 | 显示全部楼层
V4的参考例程则类似Cube/X-Cube包的结构,在Projects目录下的各个系列的子目录中提供。
新版本示例工程支持的系列包括STM32G0,STM32G4
STM32H7
STM32L0,STM32L1,STM32L4,STM32L5,STM32U5 (V4.0.1中包含)
STM32WB,STM32WL

7277465658d7259b5d.png
 楼主| 实际测量不符 发表于 2023-11-28 14:49 | 显示全部楼层
移植
下载下加密库后,其文件夹各内容如下所示:

2192265658d899a0f7.png
 楼主| 实际测量不符 发表于 2023-11-28 14:50 | 显示全部楼层
在【projects】文件夹下有很多ST的芯片的不同加密算法的加密实例,我们自己写程序时就可以参考里面的实例进行修改和使用,如下所示:
2027665658d9c5444d.png 1238065658da0abdf0.png
 楼主| 实际测量不符 发表于 2023-11-28 14:50 | 显示全部楼层
我们移植主要使用的是【\Middlewares\ST\STM32_Cryptographic】这个文件里的内容,你可以简单粗暴直接把STM32_Cryptographic文件夹复制到工程文件里,也可以只复制【include】【lib】和【interface】这3个文件夹到工程文件里,我们加密算法使用的就是这两个文件夹里的文件。其中需要注意的是【interface】这个文件夹里存放的【cmox_low_level_template.c】文件是芯片CRC使能和失能的配置文件,ST的加密库需要打开CRC才能使用,我们需要根据所使用的芯片编修改该文件;【include】文件存放的是一些头文件,不需要修改,【lib】文件夹存放的是加密库文件,根据不同的内核选择加密库;注意:V3版本的加密库使用的不是以内核来划分的。
 楼主| 实际测量不符 发表于 2023-11-28 14:50 | 显示全部楼层
修改Keil工程添加文件:
1.将【include】文件中的头文件包含在工程中

5351865658db72935b.png
 楼主| 实际测量不符 发表于 2023-11-28 14:50 | 显示全部楼层
2.添加加密库lib文件中.a后缀的库,我这里是M4内核的
9001465658dc89faff.png
 楼主| 实际测量不符 发表于 2023-11-28 14:51 | 显示全部楼层
如果添加的.a后缀编译不识别就进行如下操作配置成库文件:
8702565658dd631c62.png
3291065658ddb68a10.png
 楼主| 实际测量不符 发表于 2023-11-28 14:51 | 显示全部楼层
3.将cmox_low_level_template.c改成cmox_low_level.c文件添加到工程中(改不改都无所谓,添加进来就行),然后在cmox_ll_init()函数内修改为自己使用芯片的CRC使能配置;
 楼主| 实际测量不符 发表于 2023-11-28 14:51 | 显示全部楼层
原cmox_low_level_template.c文件:
  1. #include "cmox_init.h"
  2. #include "cmox_low_level.h"
  3. /* #include "stm32<series>xx_hal.h" */

  4. /**
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          CMOX library low level initialization
  6.   * @param          pArg User defined parameter that is transmitted from initialize service
  7.   * @retval         Initialization status: [url=home.php?mod=space&uid=144993]@ref[/url] CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL
  8.   */
  9. cmox_init_retval_t cmox_ll_init(void *pArg)
  10. {
  11.   (void)pArg;
  12.   /* Ensure CRC is enabled for cryptographic processing */
  13.   __HAL_RCC_CRC_RELEASE_RESET();
  14.   __HAL_RCC_CRC_CLK_ENABLE();
  15.   return CMOX_INIT_SUCCESS;
  16. }

  17. /**
  18.   * @brief          CMOX library low level de-initialization
  19.   * @param          pArg User defined parameter that is transmitted from finalize service
  20.   * @retval         De-initialization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL
  21.   */
  22. cmox_init_retval_t cmox_ll_deInit(void *pArg)
  23. {
  24.   (void)pArg;
  25.   /* Do not turn off CRC to avoid side effect on other SW parts using it */
  26.   return CMOX_INIT_SUCCESS;
  27. }
 楼主| 实际测量不符 发表于 2023-11-28 14:51 | 显示全部楼层
修改为我使用的F411芯片的CRC使能:

/**
  ******************************************************************************
  * @file    cmox_low_level_template.c
  * @brief
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

#include "cmox_init.h"
#include "cmox_low_level.h"
#include "stm32f4xx.h"
/* #include "stm32<series>xx_hal.h" */

/**
  * @brief          CMOX library low level initialization
  * @param          pArg User defined parameter that is transmitted from initialize service
  * @retval         Initialization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL
  */
cmox_init_retval_t cmox_ll_init(void *pArg)
{
  (void)pArg;
  /* Ensure CRC is enabled for cryptographic processing */
        RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_CRC, DISABLE);   //失能CRC
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);    //使能CRC时钟
  return CMOX_INIT_SUCCESS;
}

/**
  * @brief          CMOX library low level de-initialization
  * @param          pArg User defined parameter that is transmitted from finalize service
  * @retval         De-initialization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL
  */
cmox_init_retval_t cmox_ll_deInit(void *pArg)
{
  (void)pArg;
  /* Do not turn off CRC to avoid side effect on other SW parts using it */
  return CMOX_INIT_SUCCESS;
}

 楼主| 实际测量不符 发表于 2023-11-28 14:51 | 显示全部楼层
移植步骤就做完了,下面进行验证
 楼主| 实际测量不符 发表于 2023-11-28 14:52 | 显示全部楼层
验证加密库
验证的话可以直接在移植文件夹的【Projects】内找到相同内核的芯片选择一个你要使用的加密方式将其main文件拷贝到你的工程中修改后进行验证,我这里使用的是AES加密,程序如下:
  1. #include "main.h"
  2. #include "communicate.h"
  3. #include "cmox_crypto.h"

  4. int lv_usart1_init(void);
  5. int lv_delay_init(void);

  6. /* Global variables ----------------------------------------------------------*/
  7. /* CBC context handle */
  8. cmox_cbc_handle_t Cbc_Ctx;

  9. //__IO TestStatus glob_status = FAILED;
  10. /* Private typedef -----------------------------------------------------------*/
  11. /* Private defines -----------------------------------------------------------*/
  12. #define CHUNK_SIZE  48u   /* Chunk size (in bytes) when data to encrypt or decrypt are processed by chunk */
  13. /* Private macros ------------------------------------------------------------*/
  14. /* Private variables ---------------------------------------------------------*/

  15. //密钥
  16. const uint8_t Key[] =
  17. {
  18.     0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  19. };
  20. //偏移量
  21. const uint8_t IV[] =
  22. {
  23.     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  24. };
  25. const uint8_t Plaintext[] =
  26. {
  27.     0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  28.     0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  29.     0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  30.     0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  31. };
  32. const uint8_t Expected_Ciphertext[] =
  33. {
  34.     0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  35.     0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  36.     0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  37.     0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7
  38. };

  39. /* Computed data buffer */
  40. uint8_t Computed_Ciphertext[sizeof(Expected_Ciphertext)];
  41. uint8_t Computed_Plaintext[sizeof(Plaintext)];


  42. RCC_ClocksTypeDef  get_rcc_clock;


  43. int main(void)
  44. {
  45.     cmox_cipher_retval_t retval;
  46.     size_t computed_size;
  47.     /* General cipher context */
  48.     cmox_cipher_handle_t *cipher_ctx;
  49.     /* Index for piecemeal processing */
  50.     uint32_t index;


  51.     lv_usart1_init();
  52.     lv_delay_init();

  53.     /*开启CRC,使用ST加密库需要开启CRC*/
  54.     if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
  55.     {
  56.         printf("init error\r\n");
  57.     }
  58.        
  59.         /***************************************************************************************************
  60.         * CRC-None加密
  61.         ***************************************************************************************************/
  62.     retval = cmox_cipher_encrypt(CMOX_AESSMALL_CBC_ENC_ALGO,                  /* Use AES CBC algorithm */
  63.                                  Plaintext, sizeof(Plaintext)-1,           /* Plaintext to encrypt */
  64.                                  Key, sizeof(Key),                       /* AES key to use */
  65.                                  IV, sizeof(IV),                         /* Initialization vector */
  66.                                  Computed_Ciphertext, &computed_size);   /* Data buffer to receive generated ciphertext */

  67.     /* Verify API returned value */
  68.     if (retval != CMOX_CIPHER_SUCCESS)
  69.     {
  70.         printf("CBC ERROR\r\n");
  71.     }

  72.     printf("CBC encrypt data:\r\n");
  73.     for(int i=0; i<computed_size; i++)
  74.     {
  75.         printf("%x",Computed_Ciphertext[i]);
  76.     }
  77.     printf("\r\n");
  78.     /* Compute directly the plaintext passing all the needed parameters */
  79.     /* Note: CMOX_AES_CBC_DEC_ALGO refer to the default AES implementation
  80.      * selected in cmox_default_config.h. To use a specific implementation, user can
  81.      * directly choose:
  82.      * - CMOX_AESFAST_CBC_DEC_ALGO to select the AES fast implementation
  83.      * - CMOX_AESSMALL_CBC_DEC_ALGO to select the AES small implementation
  84.      */
  85.        
  86.         /********************************************************************************
  87.         *CRC-None解密
  88.         ********************************************************************************/
  89.     retval = cmox_cipher_decrypt(CMOX_AES_CBC_DEC_ALGO,                 /* Use AES CBC algorithm */
  90.                                  Computed_Ciphertext, computed_size, /* Ciphertext to decrypt */
  91.                                  Key, sizeof(Key),                      /* AES key to use */
  92.                                  IV, sizeof(IV),                        /* Initialization vector */
  93.                                  Computed_Plaintext, &computed_size);   /* Data buffer to receive generated plaintext */

  94.     /* Verify API returned value */
  95.     if (retval != CMOX_CIPHER_SUCCESS)
  96.     {
  97.         printf("CBC decrypt ERROR\r\n");
  98.     }
  99.     printf("CBC decrypt data:\r\n");

  100.     {
  101.         for(int i=0; i<computed_size; i++)
  102.         {
  103.             printf("%x",Computed_Plaintext[i]);
  104.         }
  105.     }
  106.     printf("\r\ncomputed_size:%d\r\n",computed_size);

  107.     /*******************************************************************************************
  108.     *
  109.     *SHA256演示
  110.     ********************************************************************************************/
  111.     const uint8_t Message[] =
  112.     {
  113.         0x6b, 0x91, 0x8f, 0xb1, 0xa5, 0xad, 0x1f, 0x9c, 0x5e, 0x5d, 0xbd, 0xf1, 0x0a, 0x93, 0xa9, 0xc8,
  114.         0xf6, 0xbc, 0xa8, 0x9f, 0x37, 0xe7, 0x9c, 0x9f, 0xe1, 0x2a, 0x57, 0x22, 0x79, 0x41, 0xb1, 0x73,
  115.         0xac, 0x79, 0xd8, 0xd4, 0x40, 0xcd, 0xe8, 0xc6, 0x4c, 0x4e, 0xbc, 0x84, 0xa4, 0xc8, 0x03, 0xd1,
  116.         0x98, 0xa2, 0x96, 0xf3, 0xde, 0x06, 0x09, 0x00, 0xcc, 0x42, 0x7f, 0x58, 0xca, 0x6e, 0xc3, 0x73,
  117.         0x08, 0x4f, 0x95, 0xdd, 0x6c, 0x7c, 0x42, 0x7e, 0xcf, 0xbf, 0x78, 0x1f, 0x68, 0xbe, 0x57, 0x2a,
  118.         0x88, 0xdb, 0xcb, 0xb1, 0x88, 0x58, 0x1a, 0xb2, 0x00, 0xbf, 0xb9, 0x9a, 0x3a, 0x81, 0x64, 0x07,
  119.         0xe7, 0xdd, 0x6d, 0xd2, 0x10, 0x03, 0x55, 0x4d, 0x4f, 0x7a, 0x99, 0xc9, 0x3e, 0xbf, 0xce, 0x5c,
  120.         0x30, 0x2f, 0xf0, 0xe1, 0x1f, 0x26, 0xf8, 0x3f, 0xe6, 0x69, 0xac, 0xef, 0xb0, 0xc1, 0xbb, 0xb8,
  121.         0xb1, 0xe9, 0x09, 0xbd, 0x14, 0xaa, 0x48, 0xba, 0x34, 0x45, 0xc8, 0x8b, 0x0e, 0x11, 0x90, 0xee,
  122.         0xf7, 0x65, 0xad, 0x89, 0x8a, 0xb8, 0xca, 0x2f, 0xe5, 0x07, 0x01, 0x5f, 0x15, 0x78, 0xf1, 0x0d,
  123.         0xce, 0x3c, 0x11, 0xa5, 0x5f, 0xb9, 0x43, 0x4e, 0xe6, 0xe9, 0xad, 0x6c, 0xc0, 0xfd, 0xc4, 0x68,
  124.         0x44, 0x47, 0xa9, 0xb3, 0xb1, 0x56, 0xb9, 0x08, 0x64, 0x63, 0x60, 0xf2, 0x4f, 0xec, 0x2d, 0x8f,
  125.         0xa6, 0x9e, 0x2c, 0x93, 0xdb, 0x78, 0x70, 0x8f, 0xcd, 0x2e, 0xef, 0x74, 0x3d, 0xcb, 0x93, 0x53,
  126.         0x81, 0x9b, 0x8d, 0x66, 0x7c, 0x48, 0xed, 0x54, 0xcd, 0x43, 0x6f, 0xb1, 0x47, 0x65, 0x98, 0xc4,
  127.         0xa1, 0xd7, 0x02, 0x8e, 0x6f, 0x2f, 0xf5, 0x07, 0x51, 0xdb, 0x36, 0xab, 0x6b, 0xc3, 0x24, 0x35,
  128.         0x15, 0x2a, 0x00, 0xab, 0xd3, 0xd5, 0x8d, 0x9a, 0x87, 0x70, 0xd9, 0xa3, 0xe5, 0x2d, 0x5a, 0x36,
  129.         0x28, 0xae, 0x3c, 0x9e, 0x03, 0x25
  130.     };
  131.     uint8_t computed_hash[CMOX_SHA256_SIZE];

  132.     retval = cmox_hash_compute(CMOX_SHA256_ALGO,         /* Use SHA256 algorithm */
  133.                                Message, sizeof(Message), /* Message to digest */
  134.                                computed_hash,            /* Data buffer to receive digest data */
  135.                                CMOX_SHA256_SIZE,         /* Expected digest size */
  136.                                &computed_size);          /* Size of computed digest */

  137.     /* Verify API returned value */
  138.     if (retval != CMOX_HASH_SUCCESS)
  139.     {
  140.         printf("HASH ERROR\r\n");
  141.     }
  142.     printf("HASH data:\r\n");
  143.     for(int i=0; i<computed_size; i++)
  144.     {
  145.         printf("%x ",computed_hash[i]);
  146.     }

  147.     while(1)
  148.     {


  149.     }

  150. }

 楼主| 实际测量不符 发表于 2023-11-28 14:52 | 显示全部楼层
结果:

8580265658e2811f2b.png

验证:

AES加密
7979365658e3e7ae27.png
 楼主| 实际测量不符 发表于 2023-11-28 14:53 | 显示全部楼层
SHA256
5432965658e4ba0dff.png
童雨竹 发表于 2024-8-16 08:02 | 显示全部楼层

做多层板可将做好的两块双面板用特制的粘合剂“压合”起来
Wordsworth 发表于 2024-8-16 09:05 | 显示全部楼层

清除与电镀动作都会在化学过程中完成
Clyde011 发表于 2024-8-16 10:08 | 显示全部楼层

负片转印(Subtractive transfer)的方式将设计好的PCB线路板的线路底片“印刷”在金属导体上。
公羊子丹 发表于 2024-8-16 11:01 | 显示全部楼层

将整个表面铺上一层薄薄的铜箔,并且把多余的部份给消除
您需要登录后才可以回帖 登录 | 注册

本版积分规则

48

主题

605

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

48

主题

605

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部