From 4a25ae3064b4ba95ae0d4ddfcacd97b4431705f2 Mon Sep 17 00:00:00 2001 From: Mike Szczys Date: Fri, 18 Oct 2024 17:25:42 -0500 Subject: [PATCH] port: linux: implement sha256 API Signed-off-by: Mike Szczys --- port/linux/golioth_sdk/CMakeLists.txt | 2 +- port/linux/golioth_sys_linux.c | 101 ++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/port/linux/golioth_sdk/CMakeLists.txt b/port/linux/golioth_sdk/CMakeLists.txt index 5e17c67a6..758e993e5 100644 --- a/port/linux/golioth_sdk/CMakeLists.txt +++ b/port/linux/golioth_sdk/CMakeLists.txt @@ -54,5 +54,5 @@ target_include_directories(golioth_sdk ${zcbor_dir} ) target_link_libraries(golioth_sdk - PRIVATE coap-3 pthread rt) + PRIVATE coap-3 pthread rt crypto) target_compile_definitions(golioth_sdk PRIVATE -DHEATSHRINK_DYNAMIC_ALLOC=0) diff --git a/port/linux/golioth_sys_linux.c b/port/linux/golioth_sys_linux.c index 634316c37..2e6e4e888 100644 --- a/port/linux/golioth_sys_linux.c +++ b/port/linux/golioth_sys_linux.c @@ -5,8 +5,10 @@ */ #include +#include #include #include +#include #include #include #include @@ -15,6 +17,8 @@ #include #include #include +#include "../utils/hex.h" + #define TAG "golioth_sys_linux" @@ -375,6 +379,103 @@ void golioth_sys_thread_destroy(golioth_sys_thread_t thread) // process exits. } +/*-------------------------------------------------- + * Hash + *------------------------------------------------*/ + +struct golioth_hash +{ + EVP_MD_CTX *mdctx; +}; + +golioth_sys_sha256_t golioth_sys_sha256_create(void) +{ + struct golioth_hash *hash; + + hash = golioth_sys_malloc(sizeof(*hash)); + if (!hash) + { + return NULL; + } + + hash->mdctx = EVP_MD_CTX_new(); + if (!hash->mdctx) + { + free(hash); + return NULL; + } + + golioth_sys_sha256_init(hash); + + return (golioth_sys_sha256_t) hash; +} + +void golioth_sys_sha256_init(golioth_sys_sha256_t sha_ctx) +{ + if (!sha_ctx) + { + return; + } + + struct golioth_hash *hash = sha_ctx; + + EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL); + EVP_DigestInit_ex2(hash->mdctx, md, NULL); +} + +void golioth_sys_sha256_free(golioth_sys_sha256_t sha_ctx) +{ + if (!sha_ctx) + { + return; + } + + struct golioth_hash *hash = sha_ctx; + EVP_MD_CTX_free(hash->mdctx); + free(hash); +} + +enum golioth_status golioth_sys_sha256_update(golioth_sys_sha256_t sha_ctx, + uint8_t *input, + size_t len) +{ + if (!sha_ctx || !input) + { + return GOLIOTH_ERR_NULL; + } + + struct golioth_hash *hash = sha_ctx; + int err = EVP_DigestUpdate(hash->mdctx, 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; + } + + struct golioth_hash *hash = sha_ctx; + int err = EVP_DigestFinal_ex(hash->mdctx, output, NULL); + 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 *------------------------------------------------*/