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 19, 2024
1 parent d281c56 commit 5417aff
Show file tree
Hide file tree
Showing 2 changed files with 102 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)
101 changes: 101 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,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
*------------------------------------------------*/
Expand Down

0 comments on commit 5417aff

Please sign in to comment.