Added Z85 support

The use of binary for CURVE keys is painful; you cannot easily copy
these in e.g. email, or use them directly in source code. There are
various encoding possibilities. Base16 and Base64 are not optimal.
Ascii85 is not safe for source (it generates quotes and escapes).

So, I've designed a new Base85 encoding, Z85, which is safe to use
in code and elsewhere, and I've modified libzmq to use this where
it also uses binary keys (in get/setsockopt).

Very simply, if you use a 32-byte value, it's Base256 (binary),
and if you use a 40-byte value, it's Base85 (Z85).

I've put the Z85 codec into z85_codec.hpp, it's not elegant C++
but it is minimal and it works. Feel free to rewrap as a real class
if this annoys you.
This commit is contained in:
Pieter Hintjens
2013-06-28 22:10:22 +02:00
parent 76df045950
commit 7041770108
13 changed files with 417 additions and 82 deletions

View File

@@ -21,6 +21,7 @@
#include "options.hpp"
#include "err.hpp"
#include "z85_codec.hpp"
zmq::options_t::options_t () :
sndhwm (1000),
@@ -285,7 +286,7 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
}
break;
// If libsodium isn't installed, these options provoke EINVAL
// If libsodium isn't installed, these options provoke EINVAL
# ifdef HAVE_LIBSODIUM
case ZMQ_CURVE_SERVER:
if (is_int && (value == 0 || value == 1)) {
@@ -301,6 +302,12 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
mechanism = ZMQ_CURVE;
return 0;
}
else
if (optvallen_ == CURVE_KEYSIZE_Z85) {
Z85_decode (curve_public_key, (char *) optval_);
mechanism = ZMQ_CURVE;
return 0;
}
break;
case ZMQ_CURVE_SECRETKEY:
@@ -309,6 +316,12 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
mechanism = ZMQ_CURVE;
return 0;
}
else
if (optvallen_ == CURVE_KEYSIZE_Z85) {
Z85_decode (curve_secret_key, (char *) optval_);
mechanism = ZMQ_CURVE;
return 0;
}
break;
case ZMQ_CURVE_SERVERKEY:
@@ -318,6 +331,13 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
mechanism = ZMQ_CURVE;
return 0;
}
else
if (optvallen_ == CURVE_KEYSIZE_Z85) {
Z85_decode (curve_server_key, (char *) optval_);
as_server = 0;
mechanism = ZMQ_CURVE;
return 0;
}
break;
# endif
}
@@ -539,7 +559,7 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
}
break;
// If libsodium isn't installed, these options provoke EINVAL
// If libsodium isn't installed, these options provoke EINVAL
# ifdef HAVE_LIBSODIUM
case ZMQ_CURVE_SERVER:
if (is_int) {
@@ -553,6 +573,11 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
memcpy (optval_, curve_public_key, CURVE_KEYSIZE);
return 0;
}
else
if (*optvallen_ == CURVE_KEYSIZE_Z85) {
Z85_encode ((char *) optval_, curve_public_key, CURVE_KEYSIZE);
return 0;
}
break;
case ZMQ_CURVE_SECRETKEY:
@@ -560,6 +585,11 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
memcpy (optval_, curve_secret_key, CURVE_KEYSIZE);
return 0;
}
else
if (*optvallen_ == CURVE_KEYSIZE_Z85) {
Z85_encode ((char *) optval_, curve_secret_key, CURVE_KEYSIZE);
return 0;
}
break;
case ZMQ_CURVE_SERVERKEY:
@@ -567,6 +597,11 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
memcpy (optval_, curve_server_key, CURVE_KEYSIZE);
return 0;
}
else
if (*optvallen_ == CURVE_KEYSIZE_Z85) {
Z85_encode ((char *) optval_, curve_server_key, CURVE_KEYSIZE);
return 0;
}
break;
# endif
}