Skip to content

Commit

Permalink
add zmq_curve_keypair to zmq_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Sep 29, 2013
1 parent 30d3488 commit d1a927a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/zmq_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ ZMQ_EXPORT char *zmq_z85_encode (char *dest, uint8_t *data, size_t size);
/* Decode data with Z85 encoding. Returns decoded data */
ZMQ_EXPORT uint8_t *zmq_z85_decode (uint8_t *dest, char *string);

/* Generate z85-encoded public and private keypair with libsodium. */
/* Returns 0 on success. */
ZMQ_EXPORT int zmq_curve_keypair (char* z85_public_key, char *z85_secret_key);

#undef ZMQ_EXPORT

#ifdef __cplusplus
Expand Down
37 changes: 36 additions & 1 deletion src/zmq_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#else
#include "windows.hpp"
#endif
#ifdef HAVE_LIBSODIUM
# include <sodium.h>
#endif


void zmq_sleep (int seconds_)
{
Expand Down Expand Up @@ -125,7 +129,7 @@ char *zmq_z85_encode (char *dest, uint8_t *data, size_t size)
return dest;
}


// --------------------------------------------------------------------------
// Decode an encoded string into a binary frame; dest must be at least
// strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string)
Expand Down Expand Up @@ -153,3 +157,34 @@ uint8_t *zmq_z85_decode (uint8_t *dest, char *string)
assert (byte_nbr == strlen (string) * 4 / 5);
return dest;
}

// --------------------------------------------------------------------------
// Generate a public/private keypair with libsodium.
// Generated keys will be 40 byte z85-encoded strings.
// Returns 0 on success, -1 on failure, setting errno.
// Sets errno = ENOTSUP in the absence of libsodium.

int zmq_curve_keypair (char* z85_public_key, char *z85_secret_key)
{
#ifdef HAVE_LIBSODIUM
# if crypto_box_PUBLICKEYBYTES != 32 \
|| crypto_box_SECRETKEYBYTES != 32
# error "libsodium not built correctly"
# endif

uint8_t public_key [32];
uint8_t secret_key [32];

int rc = crypto_box_keypair (public_key, secret_key);
// is there a sensible errno to set here?
if (rc) return rc;

zmq_z85_encode (z85_public_key, public_key, 32);
zmq_z85_encode (z85_secret_key, secret_key, 32);

return 0;
#else // requires libsodium
errno = ENOTSUP;
return -1;
#endif
}

0 comments on commit d1a927a

Please sign in to comment.