-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecchat.h
308 lines (250 loc) · 6.17 KB
/
ecchat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef ECCHAT_H
#define ECCHAT_H
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
#else
#include <arpa/inet.h>
#include <alloca.h>
#endif
#include "inttypes.h"
#define ECCHAT_ID_MAXLEN 8
#define ECCHAT_MSG_MAXLEN 1024
#define ECCHAT_ECP_LEN 66
#define ECCHAT_ECKEY_LEN (ECCHAT_ECP_LEN * 2)
#define ECCHAT_CMSG_BS 16 /* aes-256-cbc block size */
#define ECCHAT_CMSG_IV_LEN ECCHAT_CMSG_BS
#define ECCHAT_CMSG_HMAC_LEN 32
#define ECCHAT_CMSG_INIT_SIGN_LEN (4 * ECCHAT_ECP_LEN)
#define ECCHAT_KEEPALIVE_INTERVAL_SEC 6
#define ECCHAT_INACTIVITY_TIMO_SEC 12
#define ECCHAT_KEEPALIVE_INTERVAL_MSEC (ECCHAT_KEEPALIVE_INTERVAL_SEC * 1000)
#define ECCHAT_INACTIVITY_TIMO_MSEC (ECCHAT_INACTIVITY_TIMO_SEC * 1000)
#define ECCHAT_TCP_PORT_DEF 9542
#define __packed __attribute__((packed))
#define __unused __attribute__((unused))
#if defined(_WIN64) || defined(_WIN32)
#define err(fmt, args...) \
do { \
fprintf(stderr, "%s: " fmt, __FUNCTION__, ##args); \
fflush(stderr); \
} while (0)
#define nfo(fmt, args...) \
do { \
printf("%s: " fmt, __FUNCTION__, ##args); \
fflush(stdout); \
} while (0)
#else /* not windows */
#define err(fmt, args...) \
fprintf(stderr, "%s: " fmt, __FUNCTION__, ##args)
#define nfo(fmt, args...) \
printf("%s: " fmt, __FUNCTION__, ##args)
#endif
#define err_errno(fmt, args...) \
err(fmt ": %s\n", ##args, strerror(errno))
enum ecchat_hdr_type {
MSG_TYPE_CLIST,
MSG_TYPE_PING,
MSG_TYPE_PING_ACK,
MSG_TYPE_MBOX,
MSG_TYPE_MBOX_MSG,
MSG_TYPE_CMSG_REQUEST,
MSG_TYPE_CMSG_RESPONSE,
MSG_TYPE_CMSG_MSG,
MSG_TYPE_CMSG_ACK,
__MSG_TYPE_MAX
};
static const char *ecchat_hdr_type_names[] = {
"clist",
"ping",
"ping ack",
"mbox",
"mbox msg",
"cmsg request",
"cmsg response",
"cmsg msg",
"cmsg ack",
};
typedef struct {
char id[ECCHAT_ID_MAXLEN];
} ecchat_id_t;
struct ecchat_hdr {
u8 type;
u8 version;
u16 len;
} __packed;
struct ecchat_cmsg_ack {
ecchat_id_t id;
u16 msgid;
} __packed;
struct ecchat_cmsg_msg {
ecchat_id_t id;
} __packed;
struct ecchat_cmsg_init {
ecchat_id_t id;
unsigned char pubkey_x[ECCHAT_ECP_LEN];
unsigned char pubkey_y[ECCHAT_ECP_LEN];
unsigned char offline_pubkey_x[ECCHAT_ECP_LEN];
unsigned char offline_pubkey_y[ECCHAT_ECP_LEN];
unsigned char sig[ECCHAT_ECKEY_LEN];
/* + contact cert of the signer in DER ASN1 format */
} __packed;
/* header in encrypted message from eechat_cmsg_msg
*/
struct ecchat_msghdr {
u16 len;
u16 id;
} __packed;
struct ecchat_msg_clist_entry {
ecchat_id_t id;
u16 status;
} __packed;
struct ecchat_msg_ack {
struct ecchat_hdr hdr;
struct ecchat_cmsg_ack ack;
} __packed;
struct ecchat_clist_entry {
ecchat_id_t id;
void *ref;
};
struct ecchat_clist {
struct ecchat_clist_entry *entries;
unsigned n_entries;
};
static inline unsigned ecchat_msglen(struct ecchat_hdr *hdr)
{
return hdr->len + sizeof(*hdr);
}
static inline int ecchat_id_cmp(const ecchat_id_t *id1, const ecchat_id_t *id2)
{
/* return memcmp(id1, id2, sizeof(ecchat_id_t)); */
const long long *i1 = (long long *)id1;
const long long *i2 = (long long *)id2;
if (*i1 > *i2)
return 1;
else if (*i1 < *i2)
return -1;
return 0;
}
static inline void ecchat_id2str(char *dst, const ecchat_id_t *src)
{
unsigned i;
for (i = 0; i < ECCHAT_ID_MAXLEN && src->id[i] != 0; i++)
dst[i] = src->id[i];
dst[i] = 0;
}
static inline void ecchat_str2id(ecchat_id_t *dst, const char *src)
{
memset(dst, 0, sizeof(ecchat_id_t));
strncpy(dst->id, src, sizeof(dst->id));
}
static const char * ecchat_hdr_type_str(u16 type)
{
if (type >= __MSG_TYPE_MAX)
return "unknown";
return ecchat_hdr_type_names[type];
}
static inline int hex_to_bin(char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
ch = tolower(ch);
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
return -1;
}
static inline int hex2bin(char *dst, const char *src, size_t count)
{
while (count--) {
int hi = hex_to_bin(*src++);
int lo = hex_to_bin(*src++);
if ((hi < 0) || (lo < 0))
return -1;
*dst++ = (hi << 4) | lo;
}
return 0;
}
static inline void bin_to_str(char *dst, const char *src, uint len)
{
uint pos;
uint i;
for (i = pos = 0; i < len; i++)
pos += sprintf(&dst[pos], "%02x", (unsigned char)src[i]);
}
static inline void hexdump(const char *prefix,
const unsigned char *src,
const unsigned len)
{
char *b = (char *)alloca((len * 2) + 4);
bin_to_str(b, (char *)src, len);
printf("%s: %s\n", prefix, b);
}
static inline const char * ecchat_idstr(const ecchat_id_t *id)
{
static char idstr[ECCHAT_ID_MAXLEN + 2];
ecchat_id2str(idstr, id);
return idstr;
}
static inline void ecchat_clist_print(struct ecchat_clist *cl)
{
unsigned i;
for (i = 0; i < cl->n_entries; i++) {
struct ecchat_clist_entry *e = &cl->entries[i];
const char *idstr = ecchat_idstr(&e->id);
printf("clist[%02u]: %s (%p)\n", i, idstr, e->ref);
}
}
static inline void ecchat_id_print(const ecchat_id_t *id)
{
printf("contact: %s\n", ecchat_idstr(id));
}
static inline void ecchat_hdr_print(struct ecchat_hdr *msg)
{
printf("type=%s version=%hx len=%u\n",
ecchat_hdr_type_str(msg->type), msg->version, msg->len);
}
static inline void ecchat_hdr_to_host_endian(struct ecchat_hdr *msg)
{
msg->len = ntohs(msg->len);
}
static inline void ecchat_hdr_to_net_endian(struct ecchat_hdr *msg)
{
msg->len = htons(msg->len);
}
static inline void ecchat_msghdr_to_net_endian(struct ecchat_msghdr *hdr)
{
hdr->len = htons(hdr->len);
hdr->id = htons(hdr->id);
}
static inline void ecchat_msghdr_to_host_endian(struct ecchat_msghdr *hdr)
{
hdr->len = ntohs(hdr->len);
hdr->id = ntohs(hdr->id);
}
static inline uint ecchat_pad(uint len, uint padlen)
{
const uint mask = padlen - 1;
return ((len + mask) & ~mask);
}
static inline uint ecchat_msg_padded(uint plain_len)
{
return ecchat_pad(plain_len, ECCHAT_CMSG_BS);
}
static inline uint ecchat_cmsg_len(uint msglen)
{
return sizeof(struct ecchat_hdr) +
sizeof(struct ecchat_cmsg_msg) +
ECCHAT_CMSG_IV_LEN +
ecchat_msg_padded(msglen) +
ECCHAT_CMSG_HMAC_LEN;
}
static inline unsigned ecchat_cmsg_init_len(unsigned contact_cert_len)
{
return sizeof(struct ecchat_hdr) +
sizeof(struct ecchat_cmsg_init) +
contact_cert_len;
}
#endif