Skip to content

Commit

Permalink
port: linux: 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 338de80 commit 7af79bb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion port/linux/golioth_sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
74 changes: 74 additions & 0 deletions port/linux/golioth_sys_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/

#include <golioth/golioth_sys.h>
#include <golioth/golioth_status.h>
#include <assert.h>
#include <errno.h>
#include <openssl/evp.h>
#include <poll.h>
#include <pthread.h>
#include <semaphore.h>
Expand All @@ -15,6 +17,8 @@
#include <sys/eventfd.h>
#include <time.h>
#include <unistd.h>
#include "../utils/hex.h"


#define TAG "golioth_sys_linux"

Expand Down Expand Up @@ -375,6 +379,76 @@ void golioth_sys_thread_destroy(golioth_sys_thread_t thread)
// process exits.
}

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

golioth_sys_sha256_t golioth_sys_sha256_create(void)
{
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
if (!mdctx)
{
return NULL;
}

EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
EVP_DigestInit_ex2(mdctx, md, NULL);

return (golioth_sys_sha256_t) mdctx;
}

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

EVP_MD_CTX *mdctx = sha_ctx;
EVP_MD_CTX_free(mdctx);
}

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;
}

EVP_MD_CTX *mdctx = sha_ctx;
int err = EVP_DigestUpdate(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;
}

EVP_MD_CTX *mdctx = sha_ctx;
int err = EVP_DigestFinal_ex(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
*------------------------------------------------*/
Expand Down

0 comments on commit 7af79bb

Please sign in to comment.