/**
  ******************************************************************************
  * @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));

	hmac_result.ac_pass  = (ret == 0x0)?1U:0U;
	APP_LOG(TS_OFF, VLEVEL_M, "\r\nHMAC Verify Success = %u \r\n", hmac_result.ac_pass);

	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****/
