Merge pull request #1790 from hintjens/master

Problem: use of libsodium vs. tweetnacl is confused
This commit is contained in:
Constantin Rack 2016-02-11 14:00:09 +01:00
commit dd4b93ddd8
16 changed files with 612 additions and 609 deletions

2
.gitignore vendored
View File

@ -127,6 +127,8 @@ test_udp
test_large_msg test_large_msg
test_use_fd_ipc test_use_fd_ipc
test_use_fd_tcp test_use_fd_tcp
test_pre_allocated_fd_ipc
test_pre_allocated_fd_tcp
tests/test*.log tests/test*.log
tests/test*.trs tests/test*.trs
src/platform.hpp* src/platform.hpp*

View File

@ -12,34 +12,44 @@ if(APPLE)
option (ZMQ_BUILD_FRAMEWORK "Build as OS X framework" ON) option (ZMQ_BUILD_FRAMEWORK "Build as OS X framework" ON)
endif () endif ()
option(WITH_SODIUM "Build with libsodium" ON) # Select curve encryption library, defaults to tweetnacl
option(WITH_TWEETNACL "Build with tweetnacl" ON) # To use libsodium instead, use --with-libsodium (must be installed)
# To disable curve, use --disable-curve
if(WITH_SODIUM) option (WITH_LIBSODIUM "Use libsodium instead of built-in tweetnacl" OFF)
option (ENABLE_CURVE "Enable CURVE security" ON)
if (NOT ENABLE_CURVE)
message (STATUS "CURVE security is disabled")
elseif (WITH_SODIUM)
find_package (Sodium) find_package (Sodium)
if (SODIUM_FOUND) if (SODIUM_FOUND)
add_definitions(-DHAVE_LIBSODIUM) message (STATUS "Using libsodium for CURVE security")
add_definitions (-DZMQ_HAVE_CURVE -DHAVE_LIBSODIUM)
include_directories (${SODIUM_INCLUDE_DIRS}) include_directories (${SODIUM_INCLUDE_DIRS})
# On Solaris, libsodium depends on libssp
if (${CMAKE_SYSTEM_NAME} matches "SunOS")
target_link_libraries (libzmq ssp)
endif () endif ()
endif()
if(WITH_TWEETNACL)
message(STATUS "Building with TweetNaCL")
set(USE_TWEETNACL ON)
add_definitions(-DHAVE_TWEETNACL)
include_directories(
tweetnacl/contrib/randombytes
tweetnacl/src
)
set(TWEETNACL_SOURCES
tweetnacl/src/tweetnacl.c
)
if(WIN32)
list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/winrandom.c)
else () else ()
list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/devurandom.c) message (FATAL_ERROR
"libsodium is not installed. Install it, then run CMake again")
endif ()
else ()
message (STATUS "Using tweetnacl for CURVE security")
add_definitions (-DZMQ_HAVE_CURVE -DHAVE_TWEETNACL)
include_directories (tweetnacl/contrib/randombytes tweetnacl/src)
list (APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/tweetnacl/src/tweetnacl.c)
# TODO: this should be a single coherent source file
if (WIN32)
list (APPEND sources
${CMAKE_CURRENT_SOURCE_DIR}/tweetnacl/contrib/randombytes/winrandom.c)
else ()
list (APPEND sources
${CMAKE_CURRENT_SOURCE_DIR}/tweetnacl/contrib/randombytes/devurandom.c)
endif () endif ()
endif () endif ()
@ -48,39 +58,52 @@ set(POLLER "" CACHE STRING "Choose polling system. valid values are
include (CheckFunctionExists) include (CheckFunctionExists)
include (CheckTypeSize) include (CheckTypeSize)
if (POLLER STREQUAL "") if (POLLER STREQUAL "")
set (CMAKE_REQUIRED_INCLUDES sys/event.h) set (CMAKE_REQUIRED_INCLUDES sys/event.h)
check_function_exists (kqueue HAVE_KQUEUE) check_function_exists (kqueue HAVE_KQUEUE)
set (CMAKE_REQUIRED_INCLUDES) set (CMAKE_REQUIRED_INCLUDES)
if (HAVE_KQUEUE) if (HAVE_KQUEUE)
set (POLLER "kqueue") set (POLLER "kqueue")
else() endif()
endif ()
if (POLLER STREQUAL "")
set (CMAKE_REQUIRED_INCLUDES sys/epoll.h) set (CMAKE_REQUIRED_INCLUDES sys/epoll.h)
check_function_exists (epoll_create HAVE_EPOLL) check_function_exists (epoll_create HAVE_EPOLL)
set (CMAKE_REQUIRED_INCLUDES) set (CMAKE_REQUIRED_INCLUDES)
if (HAVE_EPOLL) if (HAVE_EPOLL)
set (POLLER "epoll") set (POLLER "epoll")
else() endif ()
endif ()
if (POLLER STREQUAL "")
set (CMAKE_REQUIRED_INCLUDES sys/devpoll.h) set (CMAKE_REQUIRED_INCLUDES sys/devpoll.h)
check_type_size ("struct pollfd" DEVPOLL) check_type_size ("struct pollfd" DEVPOLL)
set (CMAKE_REQUIRED_INCLUDES) set (CMAKE_REQUIRED_INCLUDES)
if (HAVE_DEVPOLL) if (HAVE_DEVPOLL)
set (POLLER "devpoll") set (POLLER "devpoll")
else() endif ()
endif ()
if (POLLER STREQUAL "")
set (CMAKE_REQUIRED_INCLUDES poll.h) set (CMAKE_REQUIRED_INCLUDES poll.h)
check_function_exists (poll HAVE_POLL) check_function_exists (poll HAVE_POLL)
set (CMAKE_REQUIRED_INCLUDES) set (CMAKE_REQUIRED_INCLUDES)
if (HAVE_POLL) if (HAVE_POLL)
set (POLLER "poll") set (POLLER "poll")
else() endif ()
endif ()
if (POLLER STREQUAL "")
if (WIN32) if (WIN32)
set (CMAKE_REQUIRED_INCLUDES winsock2.h) set (CMAKE_REQUIRED_INCLUDES winsock2.h)
set (HAVE_SELECT 1) set (HAVE_SELECT 1)
else () else ()
set (CMAKE_REQUIRED_INCLUDES sys/select.h) set (CMAKE_REQUIRED_INCLUDES sys/select.h)
check_function_exists (select HAVE_SELECT) check_function_exists (select HAVE_SELECT)
endif()
set (CMAKE_REQUIRED_INCLUDES) set (CMAKE_REQUIRED_INCLUDES)
endif ()
if (HAVE_SELECT) if (HAVE_SELECT)
set (POLLER "select") set (POLLER "select")
else () else ()
@ -88,22 +111,17 @@ if(POLLER STREQUAL "")
"Could not autodetect polling method") "Could not autodetect polling method")
endif () endif ()
endif () endif ()
endif()
endif()
endif()
endif()
if( NOT POLLER STREQUAL "kqueue" if (POLLER STREQUAL "kqueue"
AND NOT POLLER STREQUAL "epoll" OR POLLER STREQUAL "epoll"
AND NOT POLLER STREQUAL "devpoll" OR POLLER STREQUAL "devpoll"
AND NOT POLLER STREQUAL "poll" OR POLLER STREQUAL "poll"
AND NOT POLLER STREQUAL "select") OR POLLER STREQUAL "select")
message(FATAL_ERROR "Invalid polling method") message (STATUS "Detected ${POLLER} polling method")
endif()
if(NOT ${POLLER} STREQUAL "")
string (TOUPPER ${POLLER} UPPER_POLLER) string (TOUPPER ${POLLER} UPPER_POLLER)
set (ZMQ_USE_${UPPER_POLLER} 1) set (ZMQ_USE_${UPPER_POLLER} 1)
else ()
message (FATAL_ERROR "Invalid polling method")
endif () endif ()
set (ZMQ_CMAKE_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/Modules) set (ZMQ_CMAKE_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/Modules)
@ -194,6 +212,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
zmq_check_cxx_flag_prepend ("-Wextra") zmq_check_cxx_flag_prepend ("-Wextra")
endif () endif ()
# TODO: why is -Wno-long-long defined differently than in configure.ac?
zmq_check_cxx_flag_prepend ("-Wno-long-long") zmq_check_cxx_flag_prepend ("-Wno-long-long")
zmq_check_cxx_flag_prepend ("-Wno-uninitialized") zmq_check_cxx_flag_prepend ("-Wno-uninitialized")
@ -356,7 +375,7 @@ set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/lib)
# platform specifics # platform specifics
if (WIN32) if (WIN32)
# NB: May require tweaking for highly connected applications. # Socket limit is 4K (can be raised arbitrarily)
add_definitions (-DFD_SETSIZE=4096) add_definitions (-DFD_SETSIZE=4096)
add_definitions (-D_CRT_SECURE_NO_WARNINGS) add_definitions (-D_CRT_SECURE_NO_WARNINGS)
endif () endif ()
@ -528,17 +547,14 @@ foreach(source ${cxx-sources})
list (APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/src/${source}) list (APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/src/${source})
endforeach () endforeach ()
if(USE_TWEETNACL)
foreach(source ${TWEETNACL_SOURCES})
list(APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/${source})
endforeach()
endif()
foreach (source ${rc-sources}) foreach (source ${rc-sources})
list (APPEND sources ${CMAKE_CURRENT_BINARY_DIR}/${source}) list (APPEND sources ${CMAKE_CURRENT_BINARY_DIR}/${source})
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/src/${source}.in ${CMAKE_CURRENT_BINARY_DIR}/${source}) configure_file (${CMAKE_CURRENT_SOURCE_DIR}/src/${source}.in ${CMAKE_CURRENT_BINARY_DIR}/${source})
endforeach () endforeach ()
# Delete any src/platform.hpp left by configure
file (REMOVE ${CMAKE_CURRENT_SOURCE_DIR}/src/platform.hpp)
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/platform.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/platform.hpp) configure_file (${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/platform.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/platform.hpp)
list (APPEND sources ${CMAKE_CURRENT_BINARY_DIR}/platform.hpp) list (APPEND sources ${CMAKE_CURRENT_BINARY_DIR}/platform.hpp)

View File

@ -269,11 +269,6 @@ src_libzmq_la_CPPFLAGS =
src_libzmq_la_CXXFLAGS = @LIBZMQ_EXTRA_CXXFLAGS@ src_libzmq_la_CXXFLAGS = @LIBZMQ_EXTRA_CXXFLAGS@
src_libzmq_la_LIBADD = src_libzmq_la_LIBADD =
if HAVE_SODIUM
src_libzmq_la_CPPFLAGS += ${sodium_CFLAGS}
src_libzmq_la_LIBADD += ${sodium_LIBS}
endif
if USE_TWEETNACL if USE_TWEETNACL
src_libzmq_la_SOURCES += \ src_libzmq_la_SOURCES += \
tweetnacl/src/tweetnacl.c \ tweetnacl/src/tweetnacl.c \
@ -283,6 +278,11 @@ src_libzmq_la_CXXFLAGS += \
-I$(top_builddir)/tweetnacl/src -I$(top_builddir)/tweetnacl/src
endif endif
if USE_LIBSODIUM
src_libzmq_la_CPPFLAGS += ${sodium_CFLAGS}
src_libzmq_la_LIBADD += ${sodium_LIBS}
endif
if HAVE_PGM if HAVE_PGM
src_libzmq_la_CPPFLAGS += ${pgm_CFLAGS} src_libzmq_la_CPPFLAGS += ${pgm_CFLAGS}
src_libzmq_la_LIBADD += ${pgm_LIBS} src_libzmq_la_LIBADD += ${pgm_LIBS}

View File

@ -67,8 +67,6 @@ LIBZMQ_CHECK_ENABLE_DEBUG
# Check wheter to enable code coverage # Check wheter to enable code coverage
LIBZMQ_WITH_GCOV LIBZMQ_WITH_GCOV
AC_MSG_CHECKING([if TIPC is available and supports nonblocking connect]) AC_MSG_CHECKING([if TIPC is available and supports nonblocking connect])
AC_RUN_IFELSE( AC_RUN_IFELSE(
@ -103,7 +101,6 @@ AC_RUN_IFELSE(
AC_MSG_RESULT([$libzmq_tipc_support]) AC_MSG_RESULT([$libzmq_tipc_support])
AC_ARG_WITH([relaxed], AC_ARG_WITH([relaxed],
[AS_HELP_STRING([--with-relaxed], [AS_HELP_STRING([--with-relaxed],
[Switch off pedantic compiler])], [Switch off pedantic compiler])],
@ -423,58 +420,49 @@ if test "x$require_libgssapi_krb5_ext" != "xno"; then
AC_MSG_ERROR(libgssapi_krb5 is needed for GSSAPI security)) AC_MSG_ERROR(libgssapi_krb5 is needed for GSSAPI security))
fi fi
# build using libsodium # Select curve encryption library, defaults to tweetnacl
have_sodium_library="no" # To use libsodium instead, use --with-libsodium (must be installed)
# To disable curve, use --disable-curve
AC_ARG_WITH([libsodium], [AS_HELP_STRING([--with-libsodium], AC_ARG_WITH([libsodium],
[require libzmq build with libsodium crypto library. Requires pkg-config [default=check]])], AS_HELP_STRING([--with-libsodium], [Use libsodium instead of built-in tweetnacl [default=no]]))
[require_libsodium_ext=$withval],
[require_libsodium_ext=check])
AC_ARG_WITH([tweetnacl], [AS_HELP_STRING([--with-tweetnacl], AS_IF([test "x$with_libsodium" = "xyes"], [
[build libzmq with bundled tweetnacl crypto library [default=no]])], PKG_CHECK_MODULES([sodium], [libsodium], [libsodium_found=yes], [
[require_libsodium_ext=no AC_MSG_ERROR(libsodium is not installed. Install it, then run configure again)
with_tweetnacl=yes ])
AC_MSG_CHECKING(for sodium)
AC_MSG_RESULT(tweetnacl)],
[with_tweetnacl=check])
# conditionally require libsodium package
if test "x$require_libsodium_ext" != "xno"; then
PKG_CHECK_MODULES([sodium], [libsodium],
[
have_sodium_library=yes
with_tweetnacl=no
],
[
if test "x$require_libsodium_ext" == "xyes"; then
AC_MSG_ERROR(libsodium has been requested but not found)
else
AC_MSG_RESULT([ libsodium not found, using tweetnacl])
have_sodium_library=no
with_tweetnacl=yes
fi
]) ])
fi
if test "x$have_sodium_library" != "xno"; then AC_ARG_ENABLE([curve],
AC_DEFINE(HAVE_LIBSODIUM, 1, [The libsodium library is to be used.]) AS_HELP_STRING([--disable-curve], [Disable CURVE security [default=no]]))
# ssp library is required for libsodium on Solaris-like systems if test "x$enable_curve" == "xno"; then
curve_library=""
AC_MSG_NOTICE([CURVE security is disabled])
elif test "x$with_libsodium" == "xyes"; then
AC_MSG_NOTICE([Using libsodium for CURVE security])
AC_DEFINE(ZMQ_HAVE_CURVE, [1], [Using curve encryption])
AC_DEFINE(HAVE_LIBSODIUM, [1], [Using libsodium for curve encryption])
curve_library="libsodium"
# On Solaris, libsodium depends on libssp
case "${host_os}" in case "${host_os}" in
*solaris*) *solaris*)
LDFLAGS="-lssp $LDFLAGS" LDFLAGS="-lssp $LDFLAGS"
CPPFLAGS="$CPPFLAGS -Wno-long-long" CPPFLAGS="-Wno-long-long $CPPFLAGS"
;; ;;
esac esac
elif test "x$with_tweetnacl" != "xno"; then else
AC_DEFINE(HAVE_LIBSODIUM, 1, [Sodium is provided by tweetnacl.]) AC_MSG_NOTICE([Using tweetnacl for CURVE security])
AC_DEFINE(HAVE_TWEETNACL, 1, [Using tweetnacl.]) AC_DEFINE(ZMQ_HAVE_CURVE, [1], [Using curve encryption])
libzmq_pedantic="no" AC_DEFINE(HAVE_TWEETNACL, [1], [Using tweetnacl for curve encryption])
curve_library="tweetnacl"
libzmq_pedantic="no" # Disable pedantic warnings
fi fi
AM_CONDITIONAL(HAVE_SODIUM, test "x$have_sodium_library" != "xno") AM_CONDITIONAL(USE_LIBSODIUM, test "$curve_library" == "sodium")
AM_CONDITIONAL(USE_TWEETNACL, test "x$with_tweetnacl" != "xno") AM_CONDITIONAL(USE_TWEETNACL, test "$curve_library" == "tweetnacl")
# build using pgm # build using pgm
have_pgm_library="no" have_pgm_library="no"
@ -507,8 +495,6 @@ AC_ARG_WITH([norm],
[with_norm_ext=$withval], [with_norm_ext=$withval],
[with_norm_ext=no]) [with_norm_ext=no])
AC_MSG_CHECKING("with_norm_ext = ${with_norm_ext}") AC_MSG_CHECKING("with_norm_ext = ${with_norm_ext}")
if test "x$with_norm_ext" != "xno"; then if test "x$with_norm_ext" != "xno"; then

View File

@ -48,13 +48,11 @@
#include "err.hpp" #include "err.hpp"
#include "msg.hpp" #include "msg.hpp"
#ifdef HAVE_LIBSODIUM #if defined (HAVE_TWEETNACL)
#ifdef HAVE_TWEETNACL
# include "randombytes.h" # include "randombytes.h"
#else #elif defined (HAVE_LIBSODIUM)
# include "sodium.h" # include "sodium.h"
#endif #endif
#endif
#ifdef ZMQ_HAVE_VMCI #ifdef ZMQ_HAVE_VMCI
#include <vmci_sockets.h> #include <vmci_sockets.h>
@ -127,7 +125,7 @@ zmq::ctx_t::~ctx_t ()
// If we've done any Curve encryption, we may have a file handle // If we've done any Curve encryption, we may have a file handle
// to /dev/urandom open that needs to be cleaned up. // to /dev/urandom open that needs to be cleaned up.
#ifdef HAVE_LIBSODIUM #ifdef ZMQ_HAVE_CURVE
randombytes_close (); randombytes_close ();
#endif #endif

View File

@ -29,7 +29,7 @@
#include "platform.hpp" #include "platform.hpp"
#ifdef HAVE_LIBSODIUM #ifdef ZMQ_HAVE_CURVE
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp" #include "windows.hpp"

View File

@ -30,14 +30,15 @@
#ifndef __ZMQ_CURVE_CLIENT_HPP_INCLUDED__ #ifndef __ZMQ_CURVE_CLIENT_HPP_INCLUDED__
#define __ZMQ_CURVE_CLIENT_HPP_INCLUDED__ #define __ZMQ_CURVE_CLIENT_HPP_INCLUDED__
#ifdef ZMQ_HAVE_CURVE
#include "platform.hpp" #include "platform.hpp"
#include "mutex.hpp" #include "mutex.hpp"
#ifdef HAVE_LIBSODIUM #if defined (HAVE_TWEETNACL)
#ifdef HAVE_TWEETNACL
# include "tweetnacl_base.h" # include "tweetnacl_base.h"
# include "randombytes.h" # include "randombytes.h"
#else #elif defined (HAVE_LIBSODIUM)
# include "sodium.h" # include "sodium.h"
#endif #endif

View File

@ -29,7 +29,7 @@
#include "platform.hpp" #include "platform.hpp"
#ifdef HAVE_LIBSODIUM #ifdef ZMQ_HAVE_CURVE
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp" #include "windows.hpp"

View File

@ -30,15 +30,17 @@
#ifndef __ZMQ_CURVE_SERVER_HPP_INCLUDED__ #ifndef __ZMQ_CURVE_SERVER_HPP_INCLUDED__
#define __ZMQ_CURVE_SERVER_HPP_INCLUDED__ #define __ZMQ_CURVE_SERVER_HPP_INCLUDED__
#ifdef ZMQ_HAVE_CURVE
#include "platform.hpp" #include "platform.hpp"
#ifdef HAVE_LIBSODIUM #if defined (HAVE_TWEETNACL)
#ifdef HAVE_TWEETNACL
# include "tweetnacl_base.h" # include "tweetnacl_base.h"
# include "randombytes.h" # include "randombytes.h"
#else #elif defined (HAVE_LIBSODIUM)
# include "sodium.h" # include "sodium.h"
#endif #endif
#if crypto_box_NONCEBYTES != 24 \ #if crypto_box_NONCEBYTES != 24 \
|| crypto_box_PUBLICKEYBYTES != 32 \ || crypto_box_PUBLICKEYBYTES != 32 \
|| crypto_box_SECRETKEYBYTES != 32 \ || crypto_box_SECRETKEYBYTES != 32 \

View File

@ -403,8 +403,8 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
} }
break; break;
// If libsodium isn't installed, these options provoke EINVAL // If curve encryption isn't built, these options provoke EINVAL
# ifdef HAVE_LIBSODIUM #ifdef ZMQ_HAVE_CURVE
case ZMQ_CURVE_SERVER: case ZMQ_CURVE_SERVER:
if (is_int && (value == 0 || value == 1)) { if (is_int && (value == 0 || value == 1)) {
as_server = value; as_server = value;
@ -888,8 +888,8 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
} }
break; break;
// If libsodium isn't installed, these options provoke EINVAL // If curve encryption isn't built, these options provoke EINVAL
# ifdef HAVE_LIBSODIUM #ifdef ZMQ_HAVE_CURVE
case ZMQ_CURVE_SERVER: case ZMQ_CURVE_SERVER:
if (is_int) { if (is_int) {
*value = as_server && mechanism == ZMQ_CURVE; *value = as_server && mechanism == ZMQ_CURVE;

View File

@ -682,7 +682,7 @@ bool zmq::stream_engine_t::handshake ()
plain_client_t (options); plain_client_t (options);
alloc_assert (mechanism); alloc_assert (mechanism);
} }
#ifdef HAVE_LIBSODIUM #ifdef ZMQ_HAVE_CURVE
else else
if (options.mechanism == ZMQ_CURVE if (options.mechanism == ZMQ_CURVE
&& memcmp (greeting_recv + 12, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) { && memcmp (greeting_recv + 12, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {

View File

@ -1211,7 +1211,8 @@ int zmq_poller_wait (void *poller_, zmq_poller_event_t *event, long timeout_)
return -1; return -1;
} }
zmq::socket_poller_t::event_t e = {}; zmq::socket_poller_t::event_t e;
memset (&e, 0, sizeof (e));
int rc = ((zmq::socket_poller_t*)poller_)->wait (&e, timeout_); int rc = ((zmq::socket_poller_t*)poller_)->wait (&e, timeout_);
@ -1360,7 +1361,7 @@ int zmq_has (const char *capability)
if (strcmp (capability, "norm") == 0) if (strcmp (capability, "norm") == 0)
return true; return true;
#endif #endif
#if defined (HAVE_LIBSODIUM) #if defined (ZMQ_HAVE_CURVE)
if (strcmp (capability, "curve") == 0) if (strcmp (capability, "curve") == 0)
return true; return true;
#endif #endif

View File

@ -43,14 +43,12 @@
#include "windows.hpp" #include "windows.hpp"
#endif #endif
#ifdef HAVE_LIBSODIUM #if defined (HAVE_TWEETNACL)
#ifdef HAVE_TWEETNACL
# include "tweetnacl_base.h" # include "tweetnacl_base.h"
#else # include "randombytes.h"
#elif defined (HAVE_LIBSODIUM)
# include "sodium.h" # include "sodium.h"
#endif #endif
#endif
void zmq_sleep (int seconds_) void zmq_sleep (int seconds_)
{ {
@ -185,17 +183,17 @@ uint8_t *zmq_z85_decode (uint8_t *dest, const char *string)
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Generate a public/private keypair with libsodium. // Generate a public/private keypair with tweetnacl or libsodium.
// Generated keys will be 40 byte z85-encoded strings. // Generated keys will be 40 byte z85-encoded strings.
// Returns 0 on success, -1 on failure, setting errno. // Returns 0 on success, -1 on failure, setting errno.
// Sets errno = ENOTSUP in the absence of libsodium. // Sets errno = ENOTSUP in the absence of a CURVE library.
int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key) int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key)
{ {
#ifdef HAVE_LIBSODIUM #if defined (ZMQ_HAVE_CURVE)
# if crypto_box_PUBLICKEYBYTES != 32 \ # if crypto_box_PUBLICKEYBYTES != 32 \
|| crypto_box_SECRETKEYBYTES != 32 || crypto_box_SECRETKEYBYTES != 32
# error "libsodium not built correctly" # error "CURVE encryption library not built correctly"
# endif # endif
uint8_t public_key [32]; uint8_t public_key [32];
@ -210,7 +208,7 @@ int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key)
zmq_z85_encode (z85_secret_key, secret_key, 32); zmq_z85_encode (z85_secret_key, secret_key, 32);
return 0; return 0;
#else // requires libsodium #else
(void) z85_public_key, (void) z85_secret_key; (void) z85_public_key, (void) z85_secret_key;
errno = ENOTSUP; errno = ENOTSUP;
return -1; return -1;

View File

@ -55,7 +55,7 @@ int main (void)
assert (!zmq_has ("norm")); assert (!zmq_has ("norm"));
#endif #endif
#if defined (HAVE_LIBSODIUM) #if defined (ZMQ_HAVE_CURVE)
assert (zmq_has ("curve")); assert (zmq_has ("curve"));
#else #else
assert (!zmq_has ("curve")); assert (!zmq_has ("curve"));

View File

@ -102,11 +102,10 @@ static void zap_handler (void *handler)
int main (void) int main (void)
{ {
#ifndef HAVE_LIBSODIUM #ifndef ZMQ_HAVE_CURVE
printf ("libsodium not installed, skipping CURVE test\n"); printf ("CURVE encryption not installed, skipping test\n");
return 0; return 0;
#endif #endif
// Generate new keypairs for this test // Generate new keypairs for this test
int rc = zmq_curve_keypair (client_public, client_secret); int rc = zmq_curve_keypair (client_public, client_secret);
assert (rc == 0); assert (rc == 0);