From 778652460f7cceb3e760964a890ffd99ec8230e7 Mon Sep 17 00:00:00 2001 From: Juraj Vijtiuk Date: Thu, 28 Dec 2017 11:10:43 +0100 Subject: add mbedtls crypto support Summary: This patch adds support for mbedTLS as a crypto backend for libssh. mbedTLS is an SSL/TLS library that has been designed to mainly be used in embedded systems. It is loosely coupled and has a low memory footprint. mbedTLS also provides a cryptography library (libmbedcrypto) that can be used without the TLS modules. The patch is unfortunately quite big, since several new files had to be added. DSA is disabled at compile time, since mbedTLS doesn't support DSA Patch review and feedback would be appreciated, and if any issues or suggestions appear, I'm willing to work on them. Signed-off-by: Juraj Vijtiuk Test Plan: * The patch has been tested with a Debug and MinSizeRel build, with libssh unit tests, client tests and the pkd tests. * All the tests have been run with valgrind's memcheck, drd and helgrind tools. * The examples/samplessh client works when built with the patch. Reviewers: asn, aris Subscribers: simonsj Differential Revision: https://bugs.libssh.org/D1 --- src/threads.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/threads.c') diff --git a/src/threads.c b/src/threads.c index 062c3b84..b85ac756 100644 --- a/src/threads.c +++ b/src/threads.c @@ -33,6 +33,10 @@ #include "libssh/crypto.h" #include "libssh/threads.h" +#ifdef HAVE_LIBMBEDCRYPTO +#include +#endif + static int threads_noop (void **lock){ (void)lock; return 0; @@ -100,6 +104,28 @@ static int libgcrypt_thread_init(void){ return SSH_OK; } #endif /* GCRYPT_VERSION_NUMBER */ +#elif defined HAVE_LIBMBEDCRYPTO +static int libmbedcrypto_thread_init(void) +{ + if (user_callbacks == NULL) { + return SSH_ERROR; + } + + if (user_callbacks == &ssh_threads_noop) { + return SSH_OK; + } +#ifdef MBEDTLS_THREADING_ALT + else { + mbedtls_threading_set_alt(user_callbacks->mutex_init, + user_callbacks->mutex_destroy, user_callbacks->mutex_lock, + user_callbacks->mutex_unlock); + } +#elif defined MBEDTLS_THREADING_PTHREAD + return SSH_OK; +#else + return SSH_ERROR; +#endif +} #else /* HAVE_LIBGCRYPT */ /* Libcrypto specific stuff */ @@ -181,6 +207,8 @@ int ssh_threads_init(void){ /* Then initialize the crypto libraries threading callbacks */ #ifdef HAVE_LIBGCRYPT ret = libgcrypt_thread_init(); +#elif HAVE_LIBMBEDCRYPTO + ret = libmbedcrypto_thread_init(); #else /* Libcrypto */ ret = libcrypto_thread_init(); #endif @@ -191,6 +219,10 @@ int ssh_threads_init(void){ void ssh_threads_finalize(void){ #ifdef HAVE_LIBGCRYPT +#elif HAVE_LIBMBEDCRYPTO +#ifdef MBEDTLS_THREADING_ALT + mbedtls_threading_free_alt(); +#endif #else libcrypto_thread_finalize(); #endif -- cgit v1.2.3