zmq_utils.cpp 9.61 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
25 26 27 28 29

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31

32
#include "macros.hpp"
33 34
#include "clock.hpp"
#include "err.hpp"
35
#include "thread.hpp"
36 37
#include "atomic_counter.hpp"
#include "atomic_ptr.hpp"
38
#include "random.hpp"
39
#include <assert.h>
40
#include <new>
41 42

#if !defined ZMQ_HAVE_WINDOWS
43
#include <unistd.h>
44
#endif
Frank's avatar
Frank committed
45

46
#if defined(ZMQ_USE_TWEETNACL)
47
#include "tweetnacl.h"
48
#elif defined(ZMQ_USE_LIBSODIUM)
49
#include "sodium.h"
50 51
#endif

52 53 54 55 56 57 58 59 60 61 62
void zmq_sleep (int seconds_)
{
#if defined ZMQ_HAVE_WINDOWS
    Sleep (seconds_ * 1000);
#else
    sleep (seconds_);
#endif
}

void *zmq_stopwatch_start ()
{
63
    uint64_t *watch = (uint64_t *) malloc (sizeof (uint64_t));
64 65
    alloc_assert (watch);
    *watch = zmq::clock_t::now_us ();
66
    return (void *) watch;
67 68 69 70 71
}

unsigned long zmq_stopwatch_stop (void *watch_)
{
    uint64_t end = zmq::clock_t::now_us ();
72
    uint64_t start = *(uint64_t *) watch_;
73 74 75
    free (watch_);
    return (unsigned long) (end - start);
}
76

77
void *zmq_threadstart (zmq_thread_fn *func, void *arg)
78
{
79 80 81
    zmq::thread_t *thread = new (std::nothrow) zmq::thread_t;
    alloc_assert (thread);
    thread->start (func, arg);
82 83 84
    return thread;
}

85
void zmq_threadclose (void *thread)
86
{
87 88 89
    zmq::thread_t *pThread = static_cast<zmq::thread_t *> (thread);
    pThread->stop ();
    LIBZMQ_DELETE (pThread);
90
}
91 92 93 94

//  Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding

//  Maps base 256 to base 85
95 96 97 98 99 100 101 102 103
static char encoder[85 + 1] = {"0123456789"
                               "abcdefghij"
                               "klmnopqrst"
                               "uvwxyzABCD"
                               "EFGHIJKLMN"
                               "OPQRSTUVWX"
                               "YZ.-:+=^!/"
                               "*?&<>()[]{"
                               "}@%$#"};
104 105 106

//  Maps base 85 to base 256
//  We chop off lower 32 and higher 128 ranges
107
//  0xFF denotes invalid characters within this range
108 109 110 111 112 113 114 115 116
static uint8_t decoder[96] = {
  0xFF, 0x44, 0xFF, 0x54, 0x53, 0x52, 0x48, 0xFF, 0x4B, 0x4C, 0x46, 0x41,
  0xFF, 0x3F, 0x3E, 0x45, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  0x08, 0x09, 0x40, 0xFF, 0x49, 0x42, 0x4A, 0x47, 0x51, 0x24, 0x25, 0x26,
  0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
  0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x4D,
  0xFF, 0x4E, 0x43, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
  0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
  0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x4F, 0xFF, 0x50, 0xFF, 0xFF};
117 118 119 120 121

//  --------------------------------------------------------------------------
//  Encode a binary frame as a string; destination string MUST be at least
//  size * 5 / 4 bytes long plus 1 byte for the null terminator. Returns
//  dest. Size must be a multiple of 4.
122
//  Returns NULL and sets errno = EINVAL for invalid input.
123

124
char *zmq_z85_encode (char *dest, const uint8_t *data, size_t size)
125
{
126 127 128 129
    if (size % 4 != 0) {
        errno = EINVAL;
        return NULL;
    }
130 131 132 133 134
    unsigned int char_nbr = 0;
    unsigned int byte_nbr = 0;
    uint32_t value = 0;
    while (byte_nbr < size) {
        //  Accumulate value in base 256 (binary)
135
        value = value * 256 + data[byte_nbr++];
136 137 138 139
        if (byte_nbr % 4 == 0) {
            //  Output value in base 85
            unsigned int divisor = 85 * 85 * 85 * 85;
            while (divisor) {
140
                dest[char_nbr++] = encoder[value / divisor % 85];
141 142 143 144 145 146
                divisor /= 85;
            }
            value = 0;
        }
    }
    assert (char_nbr == size * 5 / 4);
147
    dest[char_nbr] = 0;
148 149 150
    return dest;
}

151

152 153
//  --------------------------------------------------------------------------
//  Decode an encoded string into a binary frame; dest must be at least
154
//  strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string)
155
//  must be a multiple of 5.
156
//  Returns NULL and sets errno = EINVAL for invalid input.
157

158
uint8_t *zmq_z85_decode (uint8_t *dest, const char *string)
159 160 161 162
{
    unsigned int byte_nbr = 0;
    unsigned int char_nbr = 0;
    uint32_t value = 0;
163
    while (string[char_nbr]) {
164
        //  Accumulate value in base 85
165 166 167 168 169
        if (UINT32_MAX / 85 < value) {
            //  Invalid z85 encoding, represented value exceeds 0xffffffff
            goto error_inval;
        }
        value *= 85;
170 171
        uint8_t index = string[char_nbr++] - 32;
        if (index >= sizeof (decoder)) {
172 173 174
            //  Invalid z85 encoding, character outside range
            goto error_inval;
        }
175
        uint32_t summand = decoder[index];
176 177 178 179 180
        if (summand == 0xFF || summand > (UINT32_MAX - value)) {
            //  Invalid z85 encoding, invalid character or represented value exceeds 0xffffffff
            goto error_inval;
        }
        value += summand;
181 182 183 184
        if (char_nbr % 5 == 0) {
            //  Output value in base 256
            unsigned int divisor = 256 * 256 * 256;
            while (divisor) {
185
                dest[byte_nbr++] = value / divisor % 256;
186 187 188 189 190
                divisor /= 256;
            }
            value = 0;
        }
    }
191 192 193
    if (char_nbr % 5 != 0) {
        goto error_inval;
    }
194 195
    assert (byte_nbr == strlen (string) * 4 / 5);
    return dest;
196 197 198 199

error_inval:
    errno = EINVAL;
    return NULL;
200
}
201 202

//  --------------------------------------------------------------------------
203
//  Generate a public/private keypair with tweetnacl or libsodium.
204 205
//  Generated keys will be 40 byte z85-encoded strings.
//  Returns 0 on success, -1 on failure, setting errno.
206
//  Sets errno = ENOTSUP in the absence of a CURVE library.
207

Pieter Hintjens's avatar
Pieter Hintjens committed
208
int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key)
209
{
210 211 212 213
#if defined(ZMQ_HAVE_CURVE)
#if crypto_box_PUBLICKEYBYTES != 32 || crypto_box_SECRETKEYBYTES != 32
#error "CURVE encryption library not built correctly"
#endif
214

215 216
    uint8_t public_key[32];
    uint8_t secret_key[32];
217

218 219
    zmq::random_open ();

220
    int res = crypto_box_keypair (public_key, secret_key);
221 222
    zmq_z85_encode (z85_public_key, public_key, 32);
    zmq_z85_encode (z85_secret_key, secret_key, 32);
223

224 225
    zmq::random_close ();

226
    return res;
227
#else
228
    (void) z85_public_key, (void) z85_secret_key;
229 230 231 232
    errno = ENOTSUP;
    return -1;
#endif
}
233

234 235 236 237 238 239 240 241
//  --------------------------------------------------------------------------
//  Derive the public key from a private key using tweetnacl or libsodium.
//  Derived key will be 40 byte z85-encoded string.
//  Returns 0 on success, -1 on failure, setting errno.
//  Sets errno = ENOTSUP in the absence of a CURVE library.

int zmq_curve_public (char *z85_public_key, const char *z85_secret_key)
{
242 243 244 245
#if defined(ZMQ_HAVE_CURVE)
#if crypto_box_PUBLICKEYBYTES != 32 || crypto_box_SECRETKEYBYTES != 32
#error "CURVE encryption library not built correctly"
#endif
246 247 248 249

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

250 251
    zmq::random_open ();

252 253 254
    if (zmq_z85_decode (secret_key, z85_secret_key) == NULL)
        return -1;

255 256
    // Return codes are suppressed as none of these can actually fail.
    crypto_scalarmult_base (public_key, secret_key);
257 258
    zmq_z85_encode (z85_public_key, public_key, 32);

259 260
    zmq::random_close ();

261 262 263 264 265 266 267 268
    return 0;
#else
    (void) z85_public_key, (void) z85_secret_key;
    errno = ENOTSUP;
    return -1;
#endif
}

269 270 271 272 273 274

//  --------------------------------------------------------------------------
//  Initialize a new atomic counter, which is set to zero

void *zmq_atomic_counter_new (void)
{
275
    zmq::atomic_counter_t *counter = new (std::nothrow) zmq::atomic_counter_t;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    alloc_assert (counter);
    return counter;
}

//  Se the value of the atomic counter

void zmq_atomic_counter_set (void *counter_, int value_)
{
    ((zmq::atomic_counter_t *) counter_)->set (value_);
}

//  Increment the atomic counter, and return the old value

int zmq_atomic_counter_inc (void *counter_)
{
    return ((zmq::atomic_counter_t *) counter_)->add (1);
}

294 295
//  Decrement the atomic counter and return 1 (if counter >= 1), or
//  0 if counter hit zero.
296

297
int zmq_atomic_counter_dec (void *counter_)
298
{
299
    return ((zmq::atomic_counter_t *) counter_)->sub (1) ? 1 : 0;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}

//  Return actual value of atomic counter

int zmq_atomic_counter_value (void *counter_)
{
    return ((zmq::atomic_counter_t *) counter_)->get ();
}

//  Destroy atomic counter, and set reference to NULL

void zmq_atomic_counter_destroy (void **counter_p_)
{
    delete ((zmq::atomic_counter_t *) *counter_p_);
    *counter_p_ = NULL;
}