Skip to content

Commit

Permalink
Merge pull request #3805 from sigiesec/curve-zerocopy
Browse files Browse the repository at this point in the history
CURVE: Reduce number of memory allocations and message copies
  • Loading branch information
bluca authored Feb 4, 2020
2 parents f1513f9 + 4177bf7 commit 06bdebf
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 101 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ endif()
# To disable curve, use --disable-curve

option(WITH_LIBSODIUM "Use libsodium instead of built-in tweetnacl" OFF)
option(WITH_LIBSODIUM_STATIC "Use static libsodium library" OFF)
option(ENABLE_CURVE "Enable CURVE security" ON)

if(NOT ENABLE_CURVE)
Expand All @@ -183,6 +184,9 @@ elseif(WITH_LIBSODIUM)
if(SODIUM_FOUND)
message(STATUS "Using libsodium for CURVE security")
include_directories(${SODIUM_INCLUDE_DIRS})
if(WITH_LIBSODIUM_STATIC)
add_compile_definitions(SODIUM_STATIC)
endif()
set(ZMQ_USE_LIBSODIUM 1)
set(ZMQ_HAVE_CURVE 1)
else()
Expand Down
12 changes: 11 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,8 @@ test_apps += \
unittests/unittest_mtrie \
unittests/unittest_ip_resolver \
unittests/unittest_udp_address \
unittests/unittest_radix_tree
unittests/unittest_radix_tree \
unittests/unittest_curve_encoding

unittests_unittest_poller_SOURCES = unittests/unittest_poller.cpp
unittests_unittest_poller_CPPFLAGS = -I$(top_srcdir)/src ${TESTUTIL_CPPFLAGS} $(CODE_COVERAGE_CPPFLAGS)
Expand Down Expand Up @@ -1140,6 +1141,15 @@ unittests_unittest_radix_tree_LDADD = \
$(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD} \
$(CODE_COVERAGE_LDFLAGS)

unittests_unittest_curve_encoding_SOURCES = unittests/unittest_curve_encoding.cpp
unittests_unittest_curve_encoding_CPPFLAGS = -I$(top_srcdir)/src ${TESTUTIL_CPPFLAGS} $(CODE_COVERAGE_CPPFLAGS)
unittests_unittest_curve_encoding_CXXFLAGS = $(CODE_COVERAGE_CXXFLAGS)
unittests_unittest_curve_encoding_LDADD = \
${TESTUTIL_LIBS} \
$(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD} \
$(CODE_COVERAGE_LDFLAGS)
endif

check_PROGRAMS = ${test_apps}
Expand Down
15 changes: 6 additions & 9 deletions src/curve_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int zmq::curve_client_t::produce_hello (msg_t *msg_)
int rc = msg_->init_size (200);
errno_assert (rc == 0);

rc = _tools.produce_hello (msg_->data (), cn_nonce);
rc = _tools.produce_hello (msg_->data (), get_and_inc_nonce ());
if (rc == -1) {
session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
Expand All @@ -150,15 +150,14 @@ int zmq::curve_client_t::produce_hello (msg_t *msg_)
return -1;
}

cn_nonce++;

return 0;
}

int zmq::curve_client_t::process_welcome (const uint8_t *msg_data_,
size_t msg_size_)
{
const int rc = _tools.process_welcome (msg_data_, msg_size_, cn_precom);
const int rc = _tools.process_welcome (msg_data_, msg_size_,
get_writable_precom_buffer ());

if (rc == -1) {
session->get_socket ()->event_handshake_failed_protocol (
Expand Down Expand Up @@ -186,7 +185,7 @@ int zmq::curve_client_t::produce_initiate (msg_t *msg_)
int rc = msg_->init_size (msg_size);
errno_assert (rc == 0);

rc = _tools.produce_initiate (msg_->data (), msg_size, cn_nonce,
rc = _tools.produce_initiate (msg_->data (), msg_size, get_and_inc_nonce (),
&metadata_plaintext[0], metadata_length);

if (-1 == rc) {
Expand All @@ -197,8 +196,6 @@ int zmq::curve_client_t::produce_initiate (msg_t *msg_)
return -1;
}

cn_nonce++;

return 0;
}

Expand Down Expand Up @@ -227,10 +224,10 @@ int zmq::curve_client_t::process_ready (const uint8_t *msg_data_,

memcpy (ready_nonce, "CurveZMQREADY---", 16);
memcpy (ready_nonce + 16, msg_data_ + 6, 8);
cn_peer_nonce = get_uint64 (msg_data_ + 6);
set_peer_nonce (get_uint64 (msg_data_ + 6));

int rc = crypto_box_open_afternm (&ready_plaintext[0], &ready_box[0], clen,
ready_nonce, cn_precom);
ready_nonce, get_precom_buffer ());

if (rc != 0) {
session->get_socket ()->event_handshake_failed_protocol (
Expand Down
Loading

0 comments on commit 06bdebf

Please sign in to comment.