3 #ifndef __ZMQ_CURVE_CLIENT_TOOLS_HPP_INCLUDED__
4 #define __ZMQ_CURVE_CLIENT_TOOLS_HPP_INCLUDED__
8 #if defined(ZMQ_USE_LIBSODIUM)
12 #if crypto_box_NONCEBYTES != 24 || crypto_box_PUBLICKEYBYTES != 32 \
13 || crypto_box_SECRETKEYBYTES != 32 || crypto_box_ZEROBYTES != 32 \
14 || crypto_box_BOXZEROBYTES != 16
15 #error "CURVE library not built properly"
26 struct curve_client_tools_t
28 static int produce_hello (
void *
data_,
29 const uint8_t *server_key_,
30 const uint64_t cn_nonce_,
31 const uint8_t *cn_public_,
32 const uint8_t *cn_secret_)
34 uint8_t hello_nonce[crypto_box_NONCEBYTES];
35 std::vector<uint8_t, secure_allocator_t<uint8_t> > hello_plaintext (
36 crypto_box_ZEROBYTES + 64, 0);
37 uint8_t hello_box[crypto_box_BOXZEROBYTES + 80];
40 memcpy (hello_nonce,
"CurveZMQHELLO---", 16);
45 crypto_box (hello_box, &hello_plaintext[0], hello_plaintext.size (),
46 hello_nonce, server_key_, cn_secret_);
50 uint8_t *hello =
static_cast<uint8_t *
> (
data_);
52 memcpy (hello,
"\x05HELLO", 6);
54 memcpy (hello + 6,
"\1\0", 2);
56 memset (hello + 8, 0, 72);
58 memcpy (hello + 80, cn_public_, crypto_box_PUBLICKEYBYTES);
60 memcpy (hello + 112, hello_nonce + 16, 8);
62 memcpy (hello + 120, hello_box + crypto_box_BOXZEROBYTES, 80);
67 static int process_welcome (
const uint8_t *msg_data_,
69 const uint8_t *server_key_,
70 const uint8_t *cn_secret_,
75 if (msg_size_ != 168) {
80 uint8_t welcome_nonce[crypto_box_NONCEBYTES];
81 std::vector<uint8_t, secure_allocator_t<uint8_t> > welcome_plaintext (
82 crypto_box_ZEROBYTES + 128);
83 uint8_t welcome_box[crypto_box_BOXZEROBYTES + 144];
86 memset (welcome_box, 0, crypto_box_BOXZEROBYTES);
87 memcpy (welcome_box + crypto_box_BOXZEROBYTES, msg_data_ + 24, 144);
89 memcpy (welcome_nonce,
"WELCOME-", 8);
90 memcpy (welcome_nonce + 8, msg_data_ + 8, 16);
92 int rc = crypto_box_open (&welcome_plaintext[0], welcome_box,
93 sizeof welcome_box, welcome_nonce,
94 server_key_, cn_secret_);
100 memcpy (cn_server_, &welcome_plaintext[crypto_box_ZEROBYTES], 32);
101 memcpy (cn_cookie_, &welcome_plaintext[crypto_box_ZEROBYTES + 32],
105 rc = crypto_box_beforenm (cn_precom_, cn_server_, cn_secret_);
111 static int produce_initiate (
void *
data_,
113 const uint64_t cn_nonce_,
114 const uint8_t *server_key_,
115 const uint8_t *public_key_,
116 const uint8_t *secret_key_,
117 const uint8_t *cn_public_,
118 const uint8_t *cn_secret_,
119 const uint8_t *cn_server_,
120 const uint8_t *cn_cookie_,
121 const uint8_t *metadata_plaintext_,
122 const size_t metadata_length_)
124 uint8_t vouch_nonce[crypto_box_NONCEBYTES];
125 std::vector<uint8_t, secure_allocator_t<uint8_t> > vouch_plaintext (
126 crypto_box_ZEROBYTES + 64);
127 uint8_t vouch_box[crypto_box_BOXZEROBYTES + 80];
130 std::fill (vouch_plaintext.begin (),
131 vouch_plaintext.begin () + crypto_box_ZEROBYTES, 0);
132 memcpy (&vouch_plaintext[crypto_box_ZEROBYTES], cn_public_, 32);
133 memcpy (&vouch_plaintext[crypto_box_ZEROBYTES + 32], server_key_, 32);
135 memset (vouch_nonce, 0, crypto_box_NONCEBYTES);
136 memcpy (vouch_nonce,
"VOUCH---", 8);
137 randombytes (vouch_nonce + 8, 16);
140 crypto_box (vouch_box, &vouch_plaintext[0], vouch_plaintext.size (),
141 vouch_nonce, cn_server_, secret_key_);
145 uint8_t initiate_nonce[crypto_box_NONCEBYTES];
146 std::vector<uint8_t> initiate_box (crypto_box_BOXZEROBYTES + 144
148 std::vector<uint8_t, secure_allocator_t<uint8_t> > initiate_plaintext (
149 crypto_box_ZEROBYTES + 128 + metadata_length_);
152 std::fill (initiate_plaintext.begin (),
153 initiate_plaintext.begin () + crypto_box_ZEROBYTES, 0);
157 #pragma GCC diagnostic ignored "-Warray-bounds"
158 #pragma GCC diagnostic ignored "-Wstringop-overflow="
160 memcpy (&initiate_plaintext[crypto_box_ZEROBYTES], public_key_, 32);
161 memcpy (&initiate_plaintext[crypto_box_ZEROBYTES + 32], vouch_nonce + 8,
163 memcpy (&initiate_plaintext[crypto_box_ZEROBYTES + 48],
164 vouch_box + crypto_box_BOXZEROBYTES, 80);
165 if (metadata_length_) {
166 memcpy (&initiate_plaintext[crypto_box_ZEROBYTES + 48 + 80],
167 metadata_plaintext_, metadata_length_);
170 #pragma GCC diagnostic pop
171 #pragma GCC diagnostic pop
174 memcpy (initiate_nonce,
"CurveZMQINITIATE", 16);
177 rc = crypto_box (&initiate_box[0], &initiate_plaintext[0],
178 crypto_box_ZEROBYTES + 128 + metadata_length_,
179 initiate_nonce, cn_server_, cn_secret_);
184 uint8_t *initiate =
static_cast<uint8_t *
> (
data_);
187 == 113 + 128 + crypto_box_BOXZEROBYTES + metadata_length_);
189 memcpy (initiate,
"\x08INITIATE", 9);
191 memcpy (initiate + 9, cn_cookie_, 96);
193 memcpy (initiate + 105, initiate_nonce + 16, 8);
195 memcpy (initiate + 113, &initiate_box[crypto_box_BOXZEROBYTES],
196 128 + metadata_length_ + crypto_box_BOXZEROBYTES);
201 static bool is_handshake_command_welcome (
const uint8_t *msg_data_,
202 const size_t msg_size_)
204 return is_handshake_command (msg_data_, msg_size_,
"\7WELCOME");
207 static bool is_handshake_command_ready (
const uint8_t *msg_data_,
208 const size_t msg_size_)
210 return is_handshake_command (msg_data_, msg_size_,
"\5READY");
213 static bool is_handshake_command_error (
const uint8_t *msg_data_,
214 const size_t msg_size_)
216 return is_handshake_command (msg_data_, msg_size_,
"\5ERROR");
220 curve_client_tools_t (
221 const uint8_t (&curve_public_key_)[crypto_box_PUBLICKEYBYTES],
222 const uint8_t (&curve_secret_key_)[crypto_box_SECRETKEYBYTES],
223 const uint8_t (&curve_server_key_)[crypto_box_PUBLICKEYBYTES])
226 memcpy (public_key, curve_public_key_, crypto_box_PUBLICKEYBYTES);
227 memcpy (secret_key, curve_secret_key_, crypto_box_SECRETKEYBYTES);
228 memcpy (server_key, curve_server_key_, crypto_box_PUBLICKEYBYTES);
231 memset (cn_secret, 0, crypto_box_SECRETKEYBYTES);
232 memset (cn_public, 0, crypto_box_PUBLICKEYBYTES);
233 rc = crypto_box_keypair (cn_public, cn_secret);
237 int produce_hello (
void *
data_,
const uint64_t cn_nonce_)
const
239 return produce_hello (
data_, server_key, cn_nonce_, cn_public,
243 int process_welcome (
const uint8_t *msg_data_,
247 return process_welcome (msg_data_, msg_size_, server_key, cn_secret,
248 cn_server, cn_cookie, cn_precom_);
251 int produce_initiate (
void *
data_,
253 const uint64_t cn_nonce_,
254 const uint8_t *metadata_plaintext_,
255 const size_t metadata_length_)
const
257 return produce_initiate (
data_, size_, cn_nonce_, server_key,
258 public_key, secret_key, cn_public, cn_secret,
259 cn_server, cn_cookie, metadata_plaintext_,
264 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
267 uint8_t secret_key[crypto_box_SECRETKEYBYTES];
270 uint8_t cn_public[crypto_box_PUBLICKEYBYTES];
273 uint8_t cn_secret[crypto_box_SECRETKEYBYTES];
276 uint8_t server_key[crypto_box_PUBLICKEYBYTES];
279 uint8_t cn_server[crypto_box_PUBLICKEYBYTES];
282 uint8_t cn_cookie[16 + 80];
286 static bool is_handshake_command (
const uint8_t *msg_data_,
287 const size_t msg_size_,
290 return msg_size_ >= (N - 1) && !memcmp (msg_data_,
prefix_, N - 1);