add CMOX HMAC code

This commit is contained in:
Yunhorn 2023-10-11 18:21:19 +08:00
parent 02e48e7ebf
commit 2a4dd64a45
4 changed files with 2100 additions and 0 deletions

View File

@ -0,0 +1,45 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : sts_cmox_hmac_sha.h *
* @author Yunhorn (r) Technology Limited Application Team *
* @brief Yunhorn (r) SmarToilets (r) HMAC-SHA1 Process file. *
* Description : Hash Message Authentication Code SHA *
******************************************************************************
* @attention
*
* Copyright (c) 2023 Yunhorn Technology Limited.
* Copyright (c) 2023 Shenzhen Yunhorn Technology Co., Ltd.
* 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. *
******************************************************************************
*/
/* USER CODE END Header */
#ifndef __sts_cmox_hmac_sha_h
#define __sts_cmox_hmac_sha_h
#include <stdlib.h>
#include <stdio.h>
#include "cmox_crypto.h"
typedef struct {
uint8_t hmac_tag[20];
uint8_t hmac_tag_size;
uint8_t ac_pass;
} hmac_result_t;
void STS_HMAC_TESTING(void);
uint32_t sts_hmac_sha1(const uint8_t *Key, int key_length, const uint8_t *Message, int message_length, hmac_result_t *hmac_result);
uint32_t sts_hmac_verify(void);
#endif
/**
* @}
*/
/**
* @}
*/

51
Core/Src/cmox_low_level.c Normal file
View File

@ -0,0 +1,51 @@
/**
******************************************************************************
* @file cmox_low_level.c
* @author MCD Application Team
* @brief This file contains Low level functions for CMOX initialization
******************************************************************************
* @attention
*
* Copyright (c) 2023 Yunhorn Technology Limited.
* Copyright (c) 2023 Shenzhen Yunhorn Technology Co., Ltd.
* 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 "stm32wlxx_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 */
__HAL_RCC_CRC_RELEASE_RESET();
__HAL_RCC_CRC_CLK_ENABLE();
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;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -0,0 +1,90 @@
/**
******************************************************************************
* @file sts_cmox_hmac_sha.c *
* @author Yunhorn (r) Technology Limited Application Team *
* @brief Yunhorn (r) SmarToilets (r) HMAC-SHA1 Process file. *
******************************************************************************
* @attention
*
* Copyright (c) 2023 Yunhorn Technology Limited.
* Copyright (c) 2023 Shenzhen Yunhorn Technology Co., Ltd.
* All rights reserved.
******************************************************************************
*/
#include "main.h"
#include "string.h"
#include "cmox_init.h"
#include "cmox_low_level.h"
#include "stm32wlxx_hal.h"
#include "sts_cmox_hmac_sha.h"
#include "yunhorn_sts_prd_conf.h"
#include "sys_app.h"
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const uint8_t mKey[] =
{
0x59,0x75,0x33,0x6e,0x31,0x48,0x34,0x4f,0x31,0x52,0x35,0x4e,0x39,0x53,0x32,0x54,0x36,0x53
};
uint8_t Computed_Tag_SHA1[YUNHORN_STS_AC_CODE_SIZE]={0x0};
extern volatile uint8_t sts_ac_code[YUNHORN_STS_AC_CODE_SIZE];
hmac_result_t hmac_result;
uint32_t sts_hmac_verify(void)
{
uint8_t i=0;
uint8_t uid[8]="";
uint32_t ret=0;
hmac_result.ac_pass = 60;
hmac_result.hmac_tag_size = 0;
GetUniqueId(uid);
ret = sts_hmac_sha1((const uint8_t *) mKey, sizeof(mKey), (const uint8_t*)(uid+4), 4, &hmac_result);
for (i=0;i<sizeof(Computed_Tag_SHA1); i++) {
APP_LOG(TS_OFF,VLEVEL_M,"0x%02x ", hmac_result.hmac_tag[i]);
}
ret = memcmp(hmac_result.hmac_tag, (void *)sts_ac_code, sizeof(sts_ac_code));
if (ret !=0)
{
hmac_result.ac_pass =0U;
APP_LOG(TS_OFF, VLEVEL_M, "\r\nHMAC Verify Error \r\n");
} else
{
ret =0;
hmac_result.ac_pass = 1U;
APP_LOG(TS_OFF, VLEVEL_M, "\r\nHMAC Verify Success\r\n");
}
return ret;
}
uint32_t sts_hmac_sha1(const uint8_t *key, int key_length, const uint8_t *message, int message_length, hmac_result_t *hmac_result)
{
cmox_mac_retval_t retval=0;
size_t computed_size=0;
/* Initialize cryptographic library */
if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
{
retval = 1;
return retval;
}
/* Compute directly the authentication tag passing all the needed parameters */
retval = cmox_mac_compute(CMOX_HMAC_SHA1_ALGO, /* Use HMAC SHA256 algorithm */
message, message_length, /* Message to authenticate */
key, key_length, /* HMAC Key to use */
NULL, 0, /* Custom data */
Computed_Tag_SHA1, /* Data buffer to receive generated authnetication tag */
sizeof(Computed_Tag_SHA1), /* Expected authentication tag size */
&computed_size); /* Generated tag size */
memcpy(hmac_result->hmac_tag, Computed_Tag_SHA1, sizeof(Computed_Tag_SHA1));
hmac_result->hmac_tag_size = computed_size;
return retval;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

File diff suppressed because one or more lines are too long