【STM32H5开发板】算法硬件加速测试
<p>STM32H5硬件配置有SHA1、SHA224、SHA256、SHA384、SHA512、HMAC等更高级的运算等硬件加速单元,SHA算法是常用的安全验证算法,在密码、加密等方面较为常用。如果使用单片机进行算法的演算会占用很长的机时,而且效率也较差。本次测试主要是测试SHA256的运算,同时也测试真随机数的生成和CRC16的计算加速等。</p><p>首先 打开HASH哈希、RNG随机数生成、CRC校验等功能,程序还需要将结果通过串口输出,打开了LED1和USART3端口。</p>
<p>代码生成后进入printf的定义和准备测试数据。</p>
<pre>
<code class="language-cpp">/* USER CODE BEGIN PTD */
__ALIGN_BEGIN const uint8_t aInput[] __ALIGN_END = "STM32H5 is a high-performance microcontrollers family based on Arm Cortex-M33 32-bit RISC core. It offers native and installable security services.";
__ALIGN_BEGIN static uint8_t aSHA256Digest __ALIGN_END;
__ALIGN_BEGIN static uint8_t aExpectSHA256Digest __ALIGN_END = {0x02, 0xfc, 0xa4, 0x29, 0x66, 0x37, 0xc1, 0xb0, 0xc2,
0x8a, 0xbb, 0xe4, 0xc4, 0x15, 0x40, 0xfb, 0x8c, 0x0a,
0xb2, 0x48, 0xf1, 0xf9, 0x99, 0xa0, 0x16, 0x81, 0xd1,
0xe5, 0x1c, 0x70, 0xfe, 0x7c};
/* USER CODE END PTD */
/* USER CODE BEGIN PV */
__IO uint32_t uwCRCValue = 0;
__IO uint32_t uwRNGValue = 0;
/* USER CODE END PV */</code></pre>
<p>准备数据时需要注意HASH算法的结果需要做对齐操作,这个不知道是不是必须地,我没有发现这样的要求文档。</p>
<p>初始化程序注意事项:</p>
<pre>
<code class="language-cpp">/**
* @brief CRC Initialization Function
* @param None
* @retval None
*/
static void MX_CRC_Init(void)
{
/* USER CODE BEGIN CRC_Init 0 */
/* USER CODE END CRC_Init 0 */
/* USER CODE BEGIN CRC_Init 1 */
/* USER CODE END CRC_Init 1 */
hcrc.Instance = CRC;
hcrc.Init.CRCLength = CRC_POLYLENGTH_16B; //as I have a 16bit polynome
hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
if (HAL_CRC_Init(&hcrc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CRC_Init 2 */
/* USER CODE END CRC_Init 2 */
}
</code></pre>
<p>CRC程序位数的设置是通过</p>
<p>hcrc.Init.CRCLength = CRC_POLYLENGTH_16B; //as I have a 16bit polynome</p>
<p>设置的,如果使用其它的位数更换定义。默认是32B,</p>
<p>如果使用自定义的"校验和式"通过</p>
<p>hcrc.Init.GeneratingPolynomial=</p>
<p>进行定义,这个CRC功能是非常完善的,不像某些国产单片机只能演算固定的“和式”和位数,这种设计几乎没有什么用。</p>
<pre>
<code class="language-cpp">/**
* @brief HASH Initialization Function
* @param None
* @retval None
*/
static void MX_HASH_Init(void)
{
/* USER CODE BEGIN HASH_Init 0 */
/* USER CODE END HASH_Init 0 */
/* USER CODE BEGIN HASH_Init 1 */
/* USER CODE END HASH_Init 1 */
hhash.Instance = HASH;
hhash.Init.DataType = HASH_NO_SWAP;
hhash.Init.Algorithm = HASH_ALGOSELECTION_SHA1;
if (HAL_HASH_Init(&hhash) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN HASH_Init 2 */
/* USER CODE END HASH_Init 2 */
}
</code></pre>
<p>HASH加速单元的设置,也是可以通过初始化来选择算法地。</p>
<p>hhash.Init.Algorithm = HASH_ALGOSELECTION_SHA1;</p>
<p>定义使用何种算法,其它的可以更换。</p>
<pre>
<code class="language-cpp">/** @defgroup HASH_Algorithm_Selection HASH algorithm selection
* @{
*/
#define HASH_ALGOSELECTION_SHA1 0x00000000U /*!< HASH function is SHA1 */
#define HASH_ALGOSELECTION_SHA224 HASH_CR_ALGO_1 /*!< HASH function is SHA224 */
#define HASH_ALGOSELECTION_SHA256 (HASH_CR_ALGO_0 | HASH_CR_ALGO_1) /*!< HASH function is SHA256 */
#if defined (HASH_CR_ALGO_2)
#define HASH_ALGOSELECTION_SHA384 (HASH_CR_ALGO_2 |HASH_CR_ALGO_3)/*!< HASH function is SHA384 */
#define HASH_ALGOSELECTION_SHA512_224 (HASH_CR_ALGO_0 | HASH_CR_ALGO_2 |HASH_CR_ALGO_3)
/*!< HASH function is SHA512_224 */
#define HASH_ALGOSELECTION_SHA512_256 (HASH_CR_ALGO_1 | HASH_CR_ALGO_2 |HASH_CR_ALGO_3)
/*!< HASH function is SHA512_256 */
#define HASH_ALGOSELECTION_SHA512 HASH_CR_ALGO /*!< HASH function is SHA512 */
#endif /* defined (HASH_CR_ALGO_2) */</code></pre>
<p>非常的方便,hhash.Init.DataType = HASH_NO_SWAP;是定义的数据格式。</p>
<p>随机数生成</p>
<pre>
<code class="language-cpp">/**
* @brief RNG Initialization Function
* @param None
* @retval None
*/
static void MX_RNG_Init(void)
{
/* USER CODE BEGIN RNG_Init 0 */
/* USER CODE END RNG_Init 0 */
/* USER CODE BEGIN RNG_Init 1 */
/* USER CODE END RNG_Init 1 */
hrng.Instance = RNG;
hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
if (HAL_RNG_Init(&hrng) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RNG_Init 2 */
/* USER CODE END RNG_Init 2 */
}</code></pre>
<p>然后是调用各功能。</p>
<pre>
<code class="language-cpp">/* USER CODE BEGIN 2 */
printf("Start a sha256!\n");
/* Start HASH computation using DMA transfer */
if (HAL_HASH_Start(&hhash, (uint8_t*)aInput, strlen((char const*)aInput), aSHA256Digest,0xFFFF) != HAL_OK)
{
Error_Handler();
}
while (HAL_HASH_GetState(&hhash) != HAL_HASH_STATE_READY);
if(HAL_RNG_GenerateRandomNumber(&hrng,(uint32_t*)&uwRNGValue) != HAL_OK)
{
Error_Handler();
}
while (HAL_RNG_GetState(&hrng) != HAL_RNG_STATE_READY);
printf("Gnerate Random Number:0x%X\n",uwRNGValue);
uwCRCValue = HAL_CRC_Calculate(&hcrc,(uint32_t*)aInput,strlen((char const*)aInput));
printf("Generate CRC Value:0x%X\n",uwCRCValue);
printf("Gnerate SHA256\n Diges:");
for(int i=0;i<32;i++)
{
printf("%02X",aSHA256Digest);
}
printf("\n");</code></pre>
<p>输出结果:</p>
<p> </p>
<p> </p>
<p>这个CRC功能是非常完善的,不像某些国产单片机只能演算固定的“和式”和位数,这种设计几乎没有什么用。</p>
<p>哈哈</p>
<p>哪个国产啊</p>
页:
[1]