Skip to content

Commit

Permalink
hmac,md5,sha: add mbedtls backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Jul 7, 2023
1 parent 0432be3 commit d8f1ba8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmake/re-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ find_package(Backtrace)
find_package(Threads REQUIRED)
find_package(ZLIB)
find_package(OpenSSL "1.1.1")
find_package(MbedTLS)

message ("MBEDTLS_FOUND=${MbedTLS_FOUND}")
option(USE_OPENSSL "Enable OpenSSL" ${OPENSSL_FOUND})
option(USE_MBEDTLS "Enable MbedTLS" ${MbedTLS_FOUND})
option(USE_UNIXSOCK "Enable Unix Domain Sockets" ON)
option(USE_TRACE "Enable Tracing helpers" OFF)

Expand Down Expand Up @@ -135,6 +138,12 @@ if(USE_OPENSSL)
)
endif()

if(USE_MBEDTLS)
list(APPEND RE_DEFINITIONS
-DUSE_MBEDTLS
)
endif()

if(USE_UNIXSOCK)
list(APPEND RE_DEFINITIONS
-DHAVE_UNIXSOCK=1
Expand Down
22 changes: 22 additions & 0 deletions src/hmac/hmac_sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#elif defined (WIN32)
#include <windows.h>
#include <wincrypt.h>
#elif defined (USE_MBEDTLS)
#include <mbedtls/md.h>
#include <mbedtls/error.h>
#endif
#include <re_hmac.h>

Expand Down Expand Up @@ -113,6 +116,16 @@ void hmac_sha1(const uint8_t *k, /* secret key */
#elif defined (WIN32)
compute_hash(CALG_SHA1, d, ld,
out, (DWORD)t, k, lk);
#elif defined (MBEDTLS_MD_C)
int err;
(void)t;

err = mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
k, lk, d, ld, out);
if (err)
DEBUG_WARNING("mbedtls_md_hmac: %s\n",
mbedtls_high_level_strerr(err));

#else
(void)k;
(void)lk;
Expand Down Expand Up @@ -147,6 +160,15 @@ void hmac_sha256(const uint8_t *key, size_t key_len,
#elif defined (WIN32)
compute_hash(CALG_SHA_256, data, data_len,
out, (DWORD)out_len, key, key_len);
#elif defined (MBEDTLS_MD_C)
int err;
(void)out_len;

err = mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
key, key_len, data, data_len, out);
if (err)
DEBUG_WARNING("mbedtls_md_hmac: %s\n",
mbedtls_high_level_strerr(err));
#else
(void)key;
(void)key_len;
Expand Down
14 changes: 14 additions & 0 deletions src/md5/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#elif defined (WIN32)
#include <windows.h>
#include <wincrypt.h>
#elif defined (USE_MBEDTLS)
#include <mbedtls/md5.h>
#include <mbedtls/error.h>
#endif
#include <re_types.h>
#include <re_fmt.h>
Expand All @@ -20,6 +23,10 @@
#include <re_md5.h>


#define DEBUG_MODULE "md5"
#define DEBUG_LEVEL 5
#include <re_dbg.h>

/**
* Calculate the MD5 hash from a buffer
*
Expand Down Expand Up @@ -52,6 +59,13 @@ void md5(const uint8_t *d, size_t n, uint8_t *md)

CryptDestroyHash(hash);
CryptReleaseContext(context, 0);
#elif defined (MBEDTLS_MD_C)
int err;

err = mbedtls_md5(d, n, md);
if (err)
DEBUG_WARNING("mbedtls_md5: %s\n",
mbedtls_high_level_strerr(err));
#else
#error missing MD5 backend
#endif
Expand Down
22 changes: 22 additions & 0 deletions src/sha/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@
#elif defined (WIN32)
#include <windows.h>
#include <wincrypt.h>
#elif defined (USE_MBEDTLS)
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
#include <mbedtls/error.h>
#endif
#include <re_sha.h>


#define DEBUG_MODULE "sha"
#define DEBUG_LEVEL 5
#include <re_dbg.h>

#if !defined (USE_OPENSSL) && defined (WIN32)
static void compute_hash(ALG_ID alg_id, const void *data, size_t data_size,
uint8_t *md, DWORD hash_size)
Expand Down Expand Up @@ -52,6 +60,13 @@ void sha1(const uint8_t *d, size_t n, uint8_t *md)
CC_SHA1(d, (uint32_t)n, md);
#elif defined (WIN32)
compute_hash(CALG_SHA1, d, n, md, SHA1_DIGEST_SIZE);
#elif defined (MBEDTLS_MD_C)
int err;

err = mbedtls_sha1(d, n, md);
if (err)
DEBUG_WARNING("mbedtls_sha1: %s\n",
mbedtls_high_level_strerr(err));
#else
(void)d;
(void)n;
Expand All @@ -76,6 +91,13 @@ void sha256(const uint8_t *d, size_t n, uint8_t *md)
CC_SHA256(d, (uint32_t)n, md);
#elif defined (WIN32)
compute_hash(CALG_SHA_256, d, n, md, SHA256_DIGEST_SIZE);
#elif defined (MBEDTLS_MD_C)
int err;

err = mbedtls_sha256(d, n, md, 0);
if (err)
DEBUG_WARNING("mbedtls_sha256: %s\n",
mbedtls_high_level_strerr(err));
#else
(void)d;
(void)n;
Expand Down

0 comments on commit d8f1ba8

Please sign in to comment.