mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-02-19 04:31:36 +08:00
[add] RNG, CRC, HASH, CRYP driver.
This commit is contained in:
parent
fd20299507
commit
6d8dbb6756
@ -34,8 +34,8 @@
|
||||
#define HAL_MODULE_ENABLED
|
||||
#define HAL_ADC_MODULE_ENABLED
|
||||
/*#define HAL_CEC_MODULE_ENABLED */
|
||||
/*#define HAL_CRC_MODULE_ENABLED */
|
||||
/*#define HAL_CRYP_MODULE_ENABLED */
|
||||
#define HAL_CRC_MODULE_ENABLED
|
||||
#define HAL_CRYP_MODULE_ENABLED
|
||||
#define HAL_DAC_MODULE_ENABLED
|
||||
#define HAL_DCMI_MODULE_ENABLED
|
||||
/*#define HAL_DSI_MODULE_ENABLED */
|
||||
@ -43,7 +43,7 @@
|
||||
/*#define HAL_DTS_MODULE_ENABLED */
|
||||
/*#define HAL_ETH_MODULE_ENABLED */
|
||||
#define HAL_FDCAN_MODULE_ENABLED
|
||||
/*#define HAL_HASH_MODULE_ENABLED */
|
||||
#define HAL_HASH_MODULE_ENABLED
|
||||
/*#define HAL_HCD_MODULE_ENABLED */
|
||||
#define HAL_HSEM_MODULE_ENABLED
|
||||
#define HAL_I2C_MODULE_ENABLED
|
||||
@ -56,7 +56,7 @@
|
||||
/*#define HAL_NOR_MODULE_ENABLED */
|
||||
/*#define HAL_PCD_MODULE_ENABLED */
|
||||
#define HAL_QSPI_MODULE_ENABLED
|
||||
/*#define HAL_RNG_MODULE_ENABLED */
|
||||
#define HAL_RNG_MODULE_ENABLED
|
||||
#define HAL_SAI_MODULE_ENABLED
|
||||
#define HAL_SD_MODULE_ENABLED
|
||||
/*#define HAL_MMC_MODULE_ENABLED */
|
||||
|
@ -26,6 +26,9 @@
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN TD */
|
||||
DMA_HandleTypeDef hdma_hash_in = {0};
|
||||
DMA_HandleTypeDef hdma_cryp_in = {0};
|
||||
DMA_HandleTypeDef hdma_cryp_out = {0};
|
||||
DMA_HandleTypeDef hdma_sai2_a = {0};
|
||||
DMA_HandleTypeDef hdma_sai2_b = {0};
|
||||
DMA_HandleTypeDef hdma_sai4_a = {0};
|
||||
@ -1820,6 +1823,251 @@ void HAL_DFSDM_ChannelMspDeInit(DFSDM_Channel_HandleTypeDef* hdfsdm_channel)
|
||||
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_13);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief HASH MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hhash: HASH handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_HASH_MspInit(HASH_HandleTypeDef* hhash)
|
||||
{
|
||||
/* USER CODE BEGIN HASH2_MspInit 0 */
|
||||
/* USER CODE END HASH2_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_HASH2_CLK_ENABLE();
|
||||
/* USER CODE BEGIN HASH2_MspInit 1 */
|
||||
__HAL_RCC_DMAMUX_CLK_ENABLE();
|
||||
|
||||
/* Peripheral DMA init*/
|
||||
hdma_hash_in.Instance = DMA2_Stream7;
|
||||
hdma_hash_in.Init.Request = DMA_REQUEST_HASH2_IN;
|
||||
hdma_hash_in.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_hash_in.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_hash_in.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_hash_in.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
||||
hdma_hash_in.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
|
||||
hdma_hash_in.Init.Mode = DMA_NORMAL;
|
||||
hdma_hash_in.Init.Priority = DMA_PRIORITY_HIGH;
|
||||
hdma_hash_in.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
|
||||
hdma_hash_in.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
|
||||
hdma_hash_in.Init.MemBurst = DMA_MBURST_SINGLE;
|
||||
hdma_hash_in.Init.PeriphBurst = DMA_PBURST_SINGLE;
|
||||
|
||||
if (HAL_DMA_DeInit(&hdma_hash_in) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA_Init(&hdma_hash_in) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(hhash,hdmain,hdma_hash_in);
|
||||
/* USER CODE END HASH2_MspInit 1 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief HASH MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hhash: HASH handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_HASH_MspDeInit(HASH_HandleTypeDef* hhash)
|
||||
{
|
||||
/* USER CODE BEGIN HASH2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END HASH2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_HASH2_CLK_DISABLE();
|
||||
/* USER CODE BEGIN HASH2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END HASH2_MspDeInit 1 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hcrc: CRC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_CRC_MspInit(CRC_HandleTypeDef* hcrc)
|
||||
{
|
||||
if(hcrc->Instance==CRC2)
|
||||
{
|
||||
/* USER CODE BEGIN CRC2_MspInit 0 */
|
||||
|
||||
/* USER CODE END CRC2_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_CRC2_CLK_ENABLE();
|
||||
/* USER CODE BEGIN CRC2_MspInit 1 */
|
||||
|
||||
/* USER CODE END CRC2_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CRC MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hcrc: CRC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_CRC_MspDeInit(CRC_HandleTypeDef* hcrc)
|
||||
{
|
||||
if(hcrc->Instance==CRC2)
|
||||
{
|
||||
/* USER CODE BEGIN CRC2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END CRC2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_CRC2_CLK_DISABLE();
|
||||
/* USER CODE BEGIN CRC2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END CRC2_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RNG MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hrng: RNG handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng)
|
||||
{
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
||||
if(hrng->Instance==RNG2)
|
||||
{
|
||||
/* USER CODE BEGIN RNG2_MspInit 0 */
|
||||
|
||||
/* USER CODE END RNG2_MspInit 0 */
|
||||
if(IS_ENGINEERING_BOOT_MODE())
|
||||
{
|
||||
/** Initializes the peripherals clock
|
||||
*/
|
||||
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RNG2;
|
||||
PeriphClkInit.Rng2ClockSelection = RCC_RNG2CLKSOURCE_CSI;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_RNG2_CLK_ENABLE();
|
||||
/* USER CODE BEGIN RNG2_MspInit 1 */
|
||||
|
||||
/* USER CODE END RNG2_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RNG MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hrng: RNG handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng)
|
||||
{
|
||||
if(hrng->Instance==RNG2)
|
||||
{
|
||||
/* USER CODE BEGIN RNG2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END RNG2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_RNG2_CLK_DISABLE();
|
||||
/* USER CODE BEGIN RNG2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END RNG2_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if defined (CRYP1) || defined (CRYP2)
|
||||
void HAL_CRYP_MspInit(CRYP_HandleTypeDef* hcryp)
|
||||
{
|
||||
if(hcryp->Instance==CRYP2)
|
||||
{
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_CRYP2_CLK_ENABLE();
|
||||
__HAL_RCC_DMAMUX_CLK_ENABLE();
|
||||
|
||||
/* Peripheral DMA init*/
|
||||
hdma_cryp_in.Instance = DMA2_Stream6;
|
||||
hdma_cryp_in.Init.Request = DMA_REQUEST_CRYP2_IN;
|
||||
hdma_cryp_in.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_cryp_in.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_cryp_in.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_cryp_in.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
||||
hdma_cryp_in.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
|
||||
hdma_cryp_in.Init.Mode = DMA_NORMAL;
|
||||
hdma_cryp_in.Init.Priority = DMA_PRIORITY_HIGH;
|
||||
hdma_cryp_in.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
if (HAL_DMA_DeInit(&hdma_cryp_in) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA_Init(&hdma_cryp_in) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(hcryp,hdmain,hdma_cryp_in);
|
||||
|
||||
hdma_cryp_out.Instance = DMA2_Stream5;
|
||||
hdma_cryp_out.Init.Request = DMA_REQUEST_CRYP2_OUT;
|
||||
hdma_cryp_out.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||
hdma_cryp_out.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_cryp_out.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_cryp_out.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
||||
hdma_cryp_out.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
|
||||
hdma_cryp_out.Init.Mode = DMA_NORMAL;
|
||||
hdma_cryp_out.Init.Priority = DMA_PRIORITY_VERY_HIGH;
|
||||
hdma_cryp_out.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
if (HAL_DMA_DeInit(&hdma_cryp_out) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_DMA_Init(&hdma_cryp_out) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(hcryp,hdmaout,hdma_cryp_out);
|
||||
|
||||
/* USER CODE BEGIN CRYP_MspInit 1 */
|
||||
|
||||
/* USER CODE END CRYP_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef* hcryp)
|
||||
{
|
||||
|
||||
if(hcryp->Instance==CRYP2)
|
||||
{
|
||||
/* USER CODE BEGIN CRYP_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END CRYP_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_CRYP2_CLK_DISABLE();
|
||||
|
||||
/* Peripheral DMA DeInit*/
|
||||
HAL_DMA_DeInit(hcryp->hdmain);
|
||||
HAL_DMA_DeInit(hcryp->hdmaout);
|
||||
}
|
||||
/* USER CODE BEGIN CRYP_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END CRYP_MspDeInit 1 */
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
|
@ -61,6 +61,9 @@ if GetDepend(['BSP_USING_WWDG']):
|
||||
if GetDepend(['BSP_USING_EXTI']):
|
||||
src += Glob('ports/drv_exti.c')
|
||||
|
||||
if GetDepend(['BSP_USING_RNG']) or GetDepend(['BSP_USING_HASH']) or GetDepend(['BSP_USING_CRC']) or GetDepend(['BSP_USING_CRYP']):
|
||||
src += Glob('ports/crypto_sample.c')
|
||||
|
||||
if GetDepend(['BSP_USING_OPENAMP']):
|
||||
src += Glob('CubeMX_Config/CM4/Src/ipcc.c')
|
||||
src += Glob('CubeMX_Config/CM4/Src/openamp.c')
|
||||
|
365
bsp/stm32/stm32mp157a-st-ev1/board/ports/crypto_sample.c
Normal file
365
bsp/stm32/stm32mp157a-st-ev1/board/ports/crypto_sample.c
Normal file
@ -0,0 +1,365 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2020-06-27 thread-liu first version
|
||||
*/
|
||||
|
||||
#include <board.h>
|
||||
|
||||
#include "drv_crypto.h"
|
||||
#include <hwcrypto.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(BSP_USING_RNG)
|
||||
static rt_err_t hw_rng_sample(int random_num)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
int i = 0, num0 = 0, num1 = 0;
|
||||
|
||||
if (random_num == 0)
|
||||
{
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
for (i = 0; i< random_num; i++)
|
||||
{
|
||||
result = rt_hwcrypto_rng_update();
|
||||
rt_kprintf("%d ", result);
|
||||
result%2 ? num1++ : num0++;
|
||||
}
|
||||
rt_kprintf("\neven numbers : %d, odd numbers: %d\n",num1, num0);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_CRC)
|
||||
static void hw_crc_sample(uint8_t *temp, int size)
|
||||
{
|
||||
struct rt_hwcrypto_ctx *ctx;
|
||||
rt_uint32_t result = 0;
|
||||
|
||||
struct hwcrypto_crc_cfg cfg =
|
||||
{
|
||||
.last_val = 0xFFFFFFFF,
|
||||
.poly = 0x04C11DB7,
|
||||
.width = 32,
|
||||
.xorout = 0x00000000,
|
||||
.flags = 0,
|
||||
};
|
||||
|
||||
ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
|
||||
rt_hwcrypto_crc_cfg(ctx, &cfg);
|
||||
|
||||
result = rt_hwcrypto_crc_update(ctx, temp, size);
|
||||
|
||||
rt_kprintf("crc result: %x \n", result);
|
||||
|
||||
rt_hwcrypto_crc_destroy(ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BSP_USING_HASH)
|
||||
static void hw_hash_sample()
|
||||
{
|
||||
int i = 0;
|
||||
struct rt_hwcrypto_ctx *ctx = RT_NULL;
|
||||
const uint8_t hash_input[] = "RT-Thread was born in 2006, it is an open source, neutral, and community-based real-time operating system (RTOS).";
|
||||
|
||||
static uint8_t sha1_output[20];
|
||||
static uint8_t sha1_except[20] = {0xff, 0x3c, 0x95, 0x54, 0x95, 0xf0, 0xad,
|
||||
0x02, 0x1b, 0xa8, 0xbc, 0xa2, 0x2e, 0xa5,
|
||||
0xb0, 0x62, 0x1b, 0xdf, 0x7f, 0xec};
|
||||
|
||||
static uint8_t md5_output[16];
|
||||
static uint8_t md5_except[16] = {0x40, 0x86, 0x03, 0x80, 0x0d, 0x8c, 0xb9,
|
||||
0x4c, 0xd6, 0x7d, 0x28, 0xfc, 0xf6, 0xc3,
|
||||
0xac, 0x8b};
|
||||
|
||||
static uint8_t sha224_output[28];
|
||||
static uint8_t sha224_except[28] = {0x6f, 0x62, 0x52, 0x7d, 0x80, 0xe6,
|
||||
0x9f, 0x82, 0x78, 0x7a, 0x46, 0x91,
|
||||
0xb0, 0xe9, 0x64, 0x89, 0xe6, 0xc3,
|
||||
0x6b, 0x7e, 0xcf, 0xca, 0x11, 0x42,
|
||||
0xc8, 0x77, 0x13, 0x79};
|
||||
static uint8_t sha256_output[32];
|
||||
static uint8_t sha256_except[32] = {0x74, 0x19, 0xb9, 0x0e, 0xd1, 0x46,
|
||||
0x37, 0x0a, 0x55, 0x18, 0x26, 0x6c,
|
||||
0x50, 0xd8, 0x71, 0x34, 0xfa, 0x1f,
|
||||
0x5f, 0x5f, 0xe4, 0x9a, 0xe9, 0x40,
|
||||
0x0a, 0x7d, 0xa0, 0x26, 0x1b, 0x86,
|
||||
0x67, 0x45};
|
||||
rt_kprintf("Hash Test start: \n");
|
||||
rt_kprintf("Hash Test string: \n");
|
||||
for (i = 0; i < sizeof(hash_input); i++)
|
||||
{
|
||||
rt_kprintf("%c", hash_input[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
|
||||
/* sh1 test*/
|
||||
ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
|
||||
if (ctx == RT_NULL)
|
||||
{
|
||||
rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_SHA1);
|
||||
return ;
|
||||
}
|
||||
rt_kprintf("Create sha1 type success!\n");
|
||||
rt_kprintf("Except sha1 result: \n");
|
||||
for (i = 0; i < sizeof(sha1_except); i++)
|
||||
{
|
||||
rt_kprintf("%x ", sha1_except[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
/* start sha1 */
|
||||
rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
|
||||
/* get sha1 result */
|
||||
rt_hwcrypto_hash_finish(ctx, sha1_output, rt_strlen((char const *)sha1_output));
|
||||
|
||||
rt_kprintf("Actual sha1 result: \n");
|
||||
for (i = 0; i < sizeof(sha1_output); i++)
|
||||
{
|
||||
rt_kprintf("%x ", sha1_output[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
if(rt_memcmp(sha1_output, sha1_except, sizeof(sha1_except)/sizeof(sha1_except[0])) != 0)
|
||||
{
|
||||
rt_kprintf("Hash type sha1 Test error, The actual result is not equal to the except result\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Hash type sha1 Test success, The actual result is equal to the except result\n");
|
||||
}
|
||||
/* deinit hash*/
|
||||
rt_hwcrypto_hash_destroy(ctx);
|
||||
|
||||
/* md5 test*/
|
||||
ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
|
||||
if (ctx == RT_NULL)
|
||||
{
|
||||
rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_MD5);
|
||||
return ;
|
||||
}
|
||||
rt_kprintf("Create md5 type success!\n");
|
||||
rt_kprintf("Except md5 result: \n");
|
||||
for (i = 0; i < sizeof(md5_except); i++)
|
||||
{
|
||||
rt_kprintf("%x ", md5_except[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
/* start md5 */
|
||||
rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
|
||||
/* get md5 result */
|
||||
rt_hwcrypto_hash_finish(ctx, md5_output, rt_strlen((char const *)md5_output));
|
||||
|
||||
rt_kprintf("Actual md5 result: \n");
|
||||
for (i = 0; i < sizeof(md5_output); i++)
|
||||
{
|
||||
rt_kprintf("%x ", md5_output[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
if(rt_memcmp(md5_output, md5_except, sizeof(md5_except)/sizeof(md5_except[0])) != 0)
|
||||
{
|
||||
rt_kprintf("Hash type md5 Test error, The actual result is not equal to the except result\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Hash type md5 Test success, The actual result is equal to the except result\n");
|
||||
}
|
||||
/* deinit hash*/
|
||||
rt_hwcrypto_hash_destroy(ctx);
|
||||
|
||||
/* sha224 test */
|
||||
ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA224);
|
||||
if (ctx == RT_NULL)
|
||||
{
|
||||
rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_SHA224);
|
||||
return ;
|
||||
}
|
||||
rt_kprintf("Create sha224 type success!\n");
|
||||
rt_kprintf("Except sha224 result: \n");
|
||||
for (i = 0; i < sizeof(sha224_except); i++)
|
||||
{
|
||||
rt_kprintf("%x ", sha224_except[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
/* start sha224 */
|
||||
rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
|
||||
/* get sha224 result */
|
||||
rt_hwcrypto_hash_finish(ctx, sha224_output, rt_strlen((char const *)sha224_output));
|
||||
|
||||
rt_kprintf("Actual sha224 result: \n");
|
||||
for (i = 0; i < sizeof(sha224_output); i++)
|
||||
{
|
||||
rt_kprintf("%x ", sha224_output[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
if(rt_memcmp(sha224_output, sha224_except, sizeof(sha224_except)/sizeof(sha224_except[0])) != 0)
|
||||
{
|
||||
rt_kprintf("Hash type sha224 Test error, The actual result is not equal to the except result\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Hash type sha224 Test success, The actual result is equal to the except result\n");
|
||||
}
|
||||
rt_hwcrypto_hash_destroy(ctx);
|
||||
|
||||
/* sha256 test*/
|
||||
ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA256);
|
||||
if (ctx == RT_NULL)
|
||||
{
|
||||
rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_SHA256);
|
||||
return ;
|
||||
}
|
||||
|
||||
rt_kprintf("Create sha256 type success!\n");
|
||||
rt_kprintf("Except sha256 result: \n");
|
||||
for (i = 0; i < sizeof(sha256_except); i++)
|
||||
{
|
||||
rt_kprintf("%x ", sha256_except[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
/* start sha256 */
|
||||
rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
|
||||
/* get sha256 result */
|
||||
rt_hwcrypto_hash_finish(ctx, sha256_output, rt_strlen((char const *)sha256_output));
|
||||
|
||||
rt_kprintf("Actual sha256 result: \n");
|
||||
for (i = 0; i < sizeof(sha256_output); i++)
|
||||
{
|
||||
rt_kprintf("%x ", sha256_output[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
|
||||
if(rt_memcmp(sha256_output, sha256_except, sizeof(sha256_except)/sizeof(sha256_except[0])) != 0)
|
||||
{
|
||||
rt_kprintf("Hash type sha256 Test error, The actual result is not equal to the except result\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Hash type sha256 Test success, The actual result is equal to the except result\n");
|
||||
}
|
||||
rt_hwcrypto_hash_destroy(ctx);
|
||||
rt_kprintf("Hash Test over!\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
static int crypto(int argc, char **argv)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
static rt_device_t device = RT_NULL;
|
||||
char *result_str;
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
if (!strcmp(argv[1], "probe"))
|
||||
{
|
||||
if (argc == 3)
|
||||
{
|
||||
char *dev_name = argv[2];
|
||||
device = rt_device_find(dev_name);
|
||||
result_str = (device == RT_NULL) ? "failure" : "success";
|
||||
rt_kprintf("probe %s %s \n", argv[2], result_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("crypto probe <crypto_name> - probe crypto by name\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
rt_kprintf("Please using 'crypto probe <crypto_name>' first\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if (!strcmp(argv[1], "rng"))
|
||||
{
|
||||
#if defined (BSP_USING_RNG)
|
||||
if (argc == 3)
|
||||
{
|
||||
result = hw_rng_sample(atoi(argv[2]));
|
||||
if(result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("please input a legal number, not <%d>\n", atoi(argv[2]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("rng <number> - generate <number> digital\n");
|
||||
}
|
||||
|
||||
#else
|
||||
rt_kprintf("please enable RNG first!\n");
|
||||
#endif
|
||||
}
|
||||
else if (!strcmp(argv[1], "crc"))
|
||||
{
|
||||
#if defined (BSP_USING_CRC)
|
||||
int size = 0, i = 0;
|
||||
if (argc > 3)
|
||||
{
|
||||
size = argc - 2;
|
||||
uint8_t *data = rt_malloc(size);
|
||||
if (data)
|
||||
{
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
data[i] = strtol(argv[2 + i], NULL, 0);
|
||||
}
|
||||
hw_crc_sample(data, size);
|
||||
rt_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Low memory!\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("crypto crc data1 ... dataN - calculate data1 ... dataN crc\n");
|
||||
}
|
||||
#else
|
||||
rt_kprintf("please enable CRC first!\n");
|
||||
#endif
|
||||
}
|
||||
else if (!strcmp(argv[1], "hash"))
|
||||
{
|
||||
#if defined (BSP_USING_HASH)
|
||||
if (argc == 3)
|
||||
{
|
||||
hw_hash_sample();
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("crypto hash sample - hash use sample\n");
|
||||
}
|
||||
#else
|
||||
rt_kprintf("please enable CRC first!\n");
|
||||
#endif
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Unknown command. Please enter 'crypto' for help\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Usage: \n");
|
||||
rt_kprintf("crypto probe <crypto_name> - probe crypto by name\n");
|
||||
rt_kprintf("crypto rng number - generate numbers digital\n");
|
||||
rt_kprintf("crypto crc data1 ... dataN - calculate data1 ... dataN crc\n");
|
||||
rt_kprintf("crypto hash sample - hash use sample\n");
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
MSH_CMD_EXPORT(crypto, crypto function);
|
Loading…
x
Reference in New Issue
Block a user