Files
kunlun/common/utils/iot_sha1.c
2024-09-28 14:24:04 +08:00

124 lines
3.0 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* os_shim header files */
#include "os_types_api.h"
/* iot common header files */
#include "iot_pkt_api.h"
#include "iot_config_api.h"
#include "iot_utils_api.h"
#include "iot_errno_api.h"
#include "iot_sha1_api.h"
#if (HW_PLATFORM == HW_PLATFORM_SILICON)
#include "mbedtls/sha1.h"
#define IOT_SHA1_LEN (20)
iot_sha1_h iot_sha1_start(void)
{
iot_pkt_t *pkt = NULL;
mbedtls_sha1_context *ctx;
pkt = iot_pkt_alloc(sizeof(mbedtls_sha1_context), IOT_DRIVER_MID);
if (pkt) {
ctx = (mbedtls_sha1_context*)iot_pkt_put(pkt,
sizeof(mbedtls_sha1_context));
mbedtls_sha1_starts(ctx);
}
return pkt;
}
void iot_sha1_end(iot_sha1_h handle)
{
iot_pkt_t *pkt;
pkt = handle;
if (pkt) {
iot_pkt_free(pkt);
}
}
uint32_t iot_sha1_update(iot_sha1_h handle, const uint8_t *data,
uint32_t len)
{
uint32_t ret = ERR_FAIL;
iot_pkt_t *pkt;
mbedtls_sha1_context *ctx;
pkt = handle;
if (pkt == NULL || iot_pkt_data_len(pkt) != sizeof(*ctx)) {
goto out;
}
ctx = (mbedtls_sha1_context*)iot_pkt_data(pkt);
mbedtls_sha1_update(ctx, data, len);
ret = ERR_OK;
out:
return ret;
}
uint32_t iot_sha1_finish(iot_sha1_h handle, uint8_t *data, uint32_t len)
{
uint32_t ret = ERR_FAIL, min_len;
uint8_t output[IOT_SHA1_LEN];
iot_pkt_t *pkt;
mbedtls_sha1_context *ctx;
pkt = handle;
if (pkt == NULL || iot_pkt_data_len(pkt) != sizeof(*ctx)) {
goto out;
}
ctx = (mbedtls_sha1_context*)iot_pkt_data(pkt);
mbedtls_sha1_finish(ctx, output);
min_len = min(IOT_SHA1_LEN, len);
os_mem_cpy(data, output, min_len);
ret = ERR_OK;
out:
return ret;
}
#else /* HW_PLATFORM == HW_PLATFORM_SILICON */
iot_sha1_h iot_sha1_start(void)
{
return NULL;
}
void iot_sha1_end(iot_sha1_h handle)
{
(void)handle;
}
uint32_t iot_sha1_update(iot_sha1_h handle, const uint8_t *data,
uint32_t len)
{
(void)handle;
(void)data;
(void)len;
return ERR_NOSUPP;
}
uint32_t iot_sha1_finish(iot_sha1_h handle, uint8_t *data, uint32_t len)
{
(void)handle;
(void)data;
(void)len;
return ERR_NOSUPP;
}
#endif /* HW_PLATFORM == HW_PLATFORM_SILICON */