Skip to content

Commit

Permalink
port: freertos: implement sha256 API
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Oct 23, 2024
1 parent 81cad76 commit 338de80
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions port/freertos/golioth_sys_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <golioth/golioth_sys.h>
#include <golioth/golioth_status.h>
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include <timers.h>
#include <string.h> // memset
#include "../utils/hex.h"
#include "mbedtls/sha256.h"

/*--------------------------------------------------
* Time
Expand Down Expand Up @@ -192,6 +195,76 @@ void golioth_sys_thread_destroy(golioth_sys_thread_t thread)
vTaskDelete((TaskHandle_t) thread);
}

/*-------------------------------------------------
* Hash
*------------------------------------------------*/

golioth_sys_sha256_t golioth_sys_sha256_create(void)
{
mbedtls_sha256_context *hash = golioth_sys_malloc(sizeof(mbedtls_sha256_context));
if (!hash)
{
return NULL;
}

mbedtls_sha256_init(hash);
mbedtls_sha256_starts(hash, 0);

return (golioth_sys_sha256_t) hash;
}

void golioth_sys_sha256_destroy(golioth_sys_sha256_t sha_ctx)
{
if (!sha_ctx)
{
return;
}

mbedtls_sha256_context *hash = sha_ctx;
mbedtls_sha256_free(hash);
}

enum golioth_status golioth_sys_sha256_update(golioth_sys_sha256_t sha_ctx,
const uint8_t *input,
size_t len)
{
if (!sha_ctx || !input)
{
return GOLIOTH_ERR_NULL;
}

mbedtls_sha256_context *hash = sha_ctx;
int err = mbedtls_sha256_update(hash, input, len);
if (err)
{
return GOLIOTH_ERR_FAIL;
}

return GOLIOTH_OK;
}

enum golioth_status golioth_sys_sha256_finish(golioth_sys_sha256_t sha_ctx, uint8_t *output)
{
if (!sha_ctx || !output)
{
return GOLIOTH_ERR_NULL;
}

mbedtls_sha256_context *hash = sha_ctx;
int err = mbedtls_sha256_finish(hash, output);
if (err)
{
return GOLIOTH_ERR_FAIL;
}

return GOLIOTH_OK;
}

size_t golioth_sys_hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
{
return hex2bin(hex, hexlen, buf, buflen);
}

/*--------------------------------------------------
* Misc
*------------------------------------------------*/
Expand Down

0 comments on commit 338de80

Please sign in to comment.