aboutsummaryrefslogtreecommitdiff
path: root/ncrypt.c
blob: 64b4391d04e8ad9e0e2cbd1b8161b82b691e8b0c (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include "ncrypt.h"

#include <endian.h>
#include <openssl/conf.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define OPENSSL_DUMP(ptr, siz)                                                                                         \
    do                                                                                                                 \
    {                                                                                                                  \
        fprintf(stderr, "Raw output (%s, %zu):\n", #ptr, siz);                                                         \
        BIO_dump_indent_fp(stderr, ptr, siz, 2);                                                                       \
        fputc('\n', stderr);                                                                                           \
    } while (0);
#define OPENSSL_ERROR(retval)                                                                                          \
    do                                                                                                                 \
    {                                                                                                                  \
        fprintf(stderr, "OpenSSL Error: %s\n", ERR_error_string(ERR_get_error(), NULL));                               \
    } while (0);

union iv
{
    struct
    {
        uint32_t upper;
        uint64_t lower;
    } __attribute__((__packed__)) numeric;
    unsigned char buffer[NCRYPT_AES_IVLEN];
};

union packet
{
    unsigned char raw[NCRYPT_PACKET_BUFFER_SIZE];
    struct
    {
        unsigned char iv[NCRYPT_AES_IVLEN];
        unsigned char tag[NCRYPT_TAG_SIZE];
        unsigned char data[NCRYPT_BUFFER_SIZE];
    } __attribute__((__packed__));
} __attribute__((__packed__));

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
_Static_assert(sizeof(((union iv *)0)->buffer) == sizeof(((union iv *)0)->numeric),
               "IV buffer must be of the same size as the numerics");
#endif

static inline nDPIsrvd_hashkey peer_build_hashkey(struct nDPIsrvd_address const * const peer_address)
{
    uint32_t hash = nDPIsrvd_HASHKEY_SEED;

    socklen_t slen = peer_address->size;
    while (slen-- > 0)
    {
        hash = ((hash << 5) + hash) + ((uint8_t *)&peer_address->raw)[slen];
    }

    return hash;
}

int ncrypt_add_peer(struct ncrypt * const nc, struct nDPIsrvd_address const * const peer_address)
{
    nDPIsrvd_hashkey peer_key = peer_build_hashkey(peer_address);
    if (peer_key == nDPIsrvd_HASHKEY_SEED)
    {
        return -1;
    }

    struct peer * peer = (struct peer *)calloc(1, sizeof(*peer));
    if (peer == NULL)
    {
        return -2;
    }

    peer->hash_key = peer_key;
    peer->address = *peer_address;
    HASH_ADD_INT(nc->peers, hash_key, peer);
    return 0;
}

struct peer * ncrypt_get_peer(struct ncrypt * const nc, struct nDPIsrvd_address const * const peer_address)
{
    nDPIsrvd_hashkey peer_key = peer_build_hashkey(peer_address);
    if (peer_key == nDPIsrvd_HASHKEY_SEED)
    {
        return NULL;
    }

    struct peer * peer;
    HASH_FIND_INT(nc->peers, &peer_key, peer);
    if (peer == NULL)
    {
        return NULL;
    }

    return peer;
}

int ncrypt_keygen(unsigned char priv_key[NCRYPT_X25519_KEYLEN], unsigned char pub_key[NCRYPT_X25519_KEYLEN])
{
    EVP_PKEY * const pkey = EVP_PKEY_Q_keygen(NULL, NULL, "X25519");
    size_t klen = NCRYPT_X25519_KEYLEN;

    if (EVP_PKEY_get_raw_private_key(pkey, priv_key, &klen) == 0 || klen != NCRYPT_X25519_KEYLEN)
    {
        EVP_PKEY_free(pkey);
        return -1;
    }
    if (EVP_PKEY_get_raw_public_key(pkey, pub_key, &klen) == 0 || klen != NCRYPT_X25519_KEYLEN)
    {
        return -2;
        EVP_PKEY_free(pkey);
    }

    EVP_PKEY_free(pkey);
    return 0;
}

int ncrypt_load_privkey(char const * const private_key_file, unsigned char priv_key[NCRYPT_X25519_KEYLEN])
{
    FILE * const pkfp = fopen(private_key_file, "r+b");
    EVP_PKEY * pkey = NULL;
    size_t klen = NCRYPT_X25519_KEYLEN;

    if (pkfp == NULL)
    {
        return -1;
    }

    pkey = PEM_read_PrivateKey(pkfp, NULL, NULL, NULL);
    if (pkey == NULL)
    {
        fclose(pkfp);
        return -2;
    }
    fclose(pkfp);

    if (EVP_PKEY_get_raw_private_key(pkey, priv_key, &klen) == 0 || klen != NCRYPT_X25519_KEYLEN)
    {
        EVP_PKEY_free(pkey);
        return -3;
    }

    EVP_PKEY_free(pkey);
    return 0;
}

int ncrypt_load_pubkey(char const * const public_key_file, unsigned char pub_key[NCRYPT_X25519_KEYLEN])
{
    FILE * const pkfp = fopen(public_key_file, "r+b");
    EVP_PKEY * pkey = NULL;
    size_t klen = NCRYPT_X25519_KEYLEN;

    if (pkfp == NULL)
    {
        return -1;
    }

    pkey = PEM_read_PUBKEY(pkfp, NULL, NULL, NULL);
    if (pkey == NULL)
    {
        fclose(pkfp);
        return -2;
    }
    fclose(pkfp);

    if (EVP_PKEY_get_raw_public_key(pkey, pub_key, &klen) == 0 || klen != NCRYPT_X25519_KEYLEN)
    {
        EVP_PKEY_free(pkey);
        return -3;
    }

    EVP_PKEY_free(pkey);
    return 0;
}

static int init_iv(struct peer * const peer)
{
    FILE * rnd_fp;

    rnd_fp = fopen("/dev/random", "r+b");

    if (rnd_fp == NULL)
    {
        return -1;
    }

    if (fread(&peer->iv[0], sizeof(peer->iv[0]), sizeof(peer->iv) / sizeof(peer->iv[0]), rnd_fp) != NCRYPT_AES_IVLEN)
    {
        fclose(rnd_fp);
        return -2;
    }

    fclose(rnd_fp);

    return 0;
}

static void next_iv(struct peer * const peer)
{
    union iv * const iv = (union iv *)&peer->iv[0];

    uint64_t lower = be64toh(iv->numeric.lower);
    lower++;
    iv->numeric.lower = htobe64(lower);

    if (iv->numeric.lower == 0)
    {
        uint32_t upper = be32toh(iv->numeric.upper);
        upper++;
        iv->numeric.upper = htobe32(upper);
    }
}

int ncrypt_init(struct ncrypt * const nc,
                unsigned char local_priv_key[NCRYPT_X25519_KEYLEN],
                unsigned char remote_pub_key[NCRYPT_X25519_KEYLEN])
{
    int rv = 0;
    EVP_PKEY_CTX * key_ctx = NULL;
    size_t pub_key_datalen = 0;
    size_t secret_len = 0;
    struct
    {
        EVP_PKEY * priv_key;
        unsigned char pub_key[NCRYPT_X25519_KEYLEN];
    } local = {.priv_key = NULL, .pub_key = {}};
    struct
    {
        EVP_PKEY * pub_key;
    } remote = {.pub_key = NULL};

    if (nc->libctx != NULL)
    {
        return -1;
    }
    nc->libctx = OSSL_LIB_CTX_new();
    if (nc->libctx == NULL)
    {
        return -2;
    }

    local.priv_key =
        EVP_PKEY_new_raw_private_key_ex(nc->libctx, "X25519", nc->propq, local_priv_key, NCRYPT_X25519_KEYLEN);
    if (local.priv_key == NULL)
    {
        return -3;
    }

    if (EVP_PKEY_get_octet_string_param(
            local.priv_key, OSSL_PKEY_PARAM_PUB_KEY, local.pub_key, sizeof(local.pub_key), &pub_key_datalen) == 0)
    {
        rv = -4;
        goto error;
    }
    if (pub_key_datalen != NCRYPT_X25519_KEYLEN)
    {
        rv = -5;
        goto error;
    }

    remote.pub_key =
        EVP_PKEY_new_raw_public_key_ex(nc->libctx, "X25519", nc->propq, remote_pub_key, NCRYPT_X25519_KEYLEN);
    if (remote.pub_key == NULL)
    {
        rv = -6;
        goto error;
    }

    key_ctx = EVP_PKEY_CTX_new_from_pkey(nc->libctx, local.priv_key, nc->propq);
    if (key_ctx == NULL)
    {
        rv = -7;
        goto error;
    }

    if (EVP_PKEY_derive_init(key_ctx) == 0)
    {
        rv = -8;
        goto error;
    }

    if (EVP_PKEY_derive_set_peer(key_ctx, remote.pub_key) == 0)
    {
        rv = -9;
        goto error;
    }

    if (EVP_PKEY_derive(key_ctx, NULL, &secret_len) == 0)
    {
        rv = -10;
        goto error;
    }
    if (secret_len != NCRYPT_X25519_KEYLEN)
    {
        rv = -11;
        goto error;
    }

    if (EVP_PKEY_derive(key_ctx, nc->shared_secret, &secret_len) == 0)
    {
        rv = -12;
        OPENSSL_cleanse(nc->shared_secret, NCRYPT_X25519_KEYLEN);
        goto error;
    }

    OPENSSL_cleanse(local_priv_key, NCRYPT_X25519_KEYLEN);
    OPENSSL_cleanse(remote_pub_key, NCRYPT_X25519_KEYLEN);

error:
    EVP_PKEY_CTX_free(key_ctx);
    EVP_PKEY_free(local.priv_key);
    EVP_PKEY_free(remote.pub_key);

    return rv;
}

int ncrypt_init_encrypt(struct ncrypt * const nc, struct aes * const aes)
{
    aes->ctx = EVP_CIPHER_CTX_new();
    if (aes->ctx == NULL)
    {
        return -3;
    }

    if (EVP_EncryptInit_ex(aes->ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) == 0)
    {
        return -4;
    }

    if (EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_IVLEN, NCRYPT_AES_IVLEN, NULL) == 0)
    {
        return -5;
    }

    if (EVP_EncryptInit_ex(aes->ctx, NULL, NULL, nc->shared_secret, NULL) == 0)
    {
        return -6;
    }

    return 0;
}

int ncrypt_init_encrypt2(struct ncrypt * const nc, struct nDPIsrvd_address * const peer_address)
{
    struct peer * const peer = ncrypt_get_peer(nc, peer_address);

    if (peer == NULL)
    {
        return -1;
    }

    if (init_iv(peer) != 0)
    {
        return -2;
    }

    return ncrypt_init_encrypt(nc, &peer->aes);
}

int ncrypt_init_decrypt(struct ncrypt * const nc, struct aes * const aes)
{
    aes->ctx = EVP_CIPHER_CTX_new();
    if (aes->ctx == NULL)
    {
        return -2;
    }

    if (EVP_DecryptInit_ex(aes->ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) == 0)
    {
        return -3;
    }

    if (EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_IVLEN, NCRYPT_AES_IVLEN, NULL) == 0)
    {
        return -4;
    }

    if (EVP_DecryptInit_ex(aes->ctx, NULL, NULL, nc->shared_secret, NULL) == 0)
    {
        return -5;
    }

    return 0;
}

int ncrypt_init_decrypt2(struct ncrypt * const nc, struct nDPIsrvd_address * const peer_address)
{
    struct peer * const peer = ncrypt_get_peer(nc, peer_address);

    if (peer == NULL)
    {
        return -1;
    }

    return ncrypt_init_decrypt(nc, &peer->aes);
}

void ncrypt_free_aes(struct aes * const aes)
{
    EVP_CIPHER_CTX_free(aes->ctx);
    aes->ctx = NULL;
}

static void cleanup_peers(struct ncrypt * const nc)
{
    struct peer * current_peer;
    struct peer * ctmp;

    if (nc->peers == NULL)
    {
        return;
    }

    HASH_ITER(hh, nc->peers, current_peer, ctmp)
    {
        ncrypt_free_aes(&current_peer->aes);
        HASH_DEL(nc->peers, current_peer);
        free(current_peer);
    }
}

void ncrypt_free(struct ncrypt * const nc)
{
    OPENSSL_cleanse(nc->shared_secret, NCRYPT_X25519_KEYLEN);

    if (nc->libctx != NULL)
    {
        OSSL_LIB_CTX_free(nc->libctx);
        nc->libctx = NULL;
    }

    cleanup_peers(nc);
}

static int encrypt(struct aes * const aes,
                   char const * const plaintext,
                   size_t plaintext_size,
                   unsigned char const iv[NCRYPT_AES_IVLEN],
                   unsigned char encrypted[NCRYPT_BUFFER_SIZE],
                   unsigned char tag[NCRYPT_TAG_SIZE])
{
    int encrypted_used;
    int remaining;

    if (EVP_EncryptInit_ex(aes->ctx, NULL, NULL, NULL, iv) == 0)
    {
        return -2;
    }

    if (EVP_EncryptUpdate(aes->ctx, encrypted, &encrypted_used, (const unsigned char *)plaintext, plaintext_size) == 0)
    {
        return -3;
    }

    if (EVP_EncryptFinal_ex(aes->ctx, encrypted + encrypted_used, &remaining) == 0)
    {
        return -4;
    }

    if (EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_GET_TAG, NCRYPT_TAG_SIZE, tag) == 0)
    {
        return -5;
    }

    return encrypted_used + remaining;
}

int ncrypt_encrypt(struct aes * const aes,
                   char const * const plaintext,
                   size_t plaintext_size,
                   unsigned char const iv[NCRYPT_AES_IVLEN],
                   unsigned char encrypted[NCRYPT_BUFFER_SIZE],
                   unsigned char tag[NCRYPT_TAG_SIZE])
{
    if (plaintext_size > NCRYPT_BUFFER_SIZE)
    {
        return -1;
    }

    return encrypt(aes, plaintext, plaintext_size, iv, encrypted, tag);
}

static int decrypt(struct aes * const aes,
                   unsigned char const * const encrypted,
                   size_t encrypt_size,
                   unsigned char const iv[NCRYPT_AES_IVLEN],
                   unsigned char tag[NCRYPT_TAG_SIZE],
                   char plaintext[NCRYPT_BUFFER_SIZE])
{
    int decrypted_used;
    int remaining;

    if (EVP_DecryptInit_ex(aes->ctx, NULL, NULL, NULL, iv) == 0)
    {
        return -2;
    }

    if (EVP_DecryptUpdate(aes->ctx, (unsigned char *)plaintext, &decrypted_used, encrypted, encrypt_size) == 0)
    {
        return -3;
    }

    if (EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG, NCRYPT_TAG_SIZE, tag) == 0)
    {
        return -4;
    }

    if (EVP_DecryptFinal_ex(aes->ctx, (unsigned char *)plaintext + decrypted_used, &remaining) == 0)
    {
        return -5;
    }

    return decrypted_used + remaining;
}

int ncrypt_decrypt(struct aes * const aes,
                   unsigned char const * const encrypted,
                   size_t encrypt_size,
                   unsigned char const iv[NCRYPT_AES_IVLEN],
                   unsigned char tag[NCRYPT_TAG_SIZE],
                   char plaintext[NCRYPT_BUFFER_SIZE])
{
    if (encrypt_size > NCRYPT_BUFFER_SIZE)
    {
        return -1;
    }

    return decrypt(aes, encrypted, encrypt_size, iv, tag, plaintext);
}

int ncrypt_dgram_send(struct ncrypt * const nc, int fd, char const * const plaintext, size_t plaintext_size)
{
    if (plaintext_size > NCRYPT_BUFFER_SIZE)
    {
        return -1;
    }

    int retval = 0;
    struct peer * current_peer;
    struct peer * tmp_peer;
    union packet encrypted;
    HASH_ITER(hh, nc->peers, current_peer, tmp_peer)
    {
        int encrypted_used =
            encrypt(&current_peer->aes, plaintext, plaintext_size, current_peer->iv, encrypted.data, encrypted.tag);
        if (encrypted_used < 0 || encrypted_used > (int)NCRYPT_BUFFER_SIZE)
        {
            current_peer->crypto_errors++;
            retval++;
            continue;
        }

        memcpy(encrypted.iv, current_peer->iv, NCRYPT_AES_IVLEN);
        ssize_t bytes_written = sendto(fd,
                                       encrypted.raw,
                                       NCRYPT_PACKET_OVERHEAD + encrypted_used,
                                       0,
                                       &current_peer->address.raw,
                                       current_peer->address.size);
        next_iv(current_peer);

        if (bytes_written < 0)
        {
            current_peer->send_errors++;
            retval++;
            continue;
        }
        if (bytes_written != NCRYPT_PACKET_OVERHEAD + encrypted_used)
        {
            current_peer->partial_writes++;
            retval++;
            continue;
        }
    }

    return retval;
}

int ncrypt_dgram_recv(struct ncrypt * const nc, int fd, char * const plaintext, size_t plaintext_size)
{
    if (plaintext_size > NCRYPT_BUFFER_SIZE)
    {
        return -1;
    }

    struct nDPIsrvd_address remote = {.size = sizeof(remote.raw)};
    union packet encrypted;
    ssize_t bytes_read = recvfrom(fd, encrypted.raw, sizeof(encrypted.raw), 0, &remote.raw, &remote.size);

    if (bytes_read < 0)
    {
        return -2;
    }
    if (bytes_read < NCRYPT_PACKET_MIN_SIZE)
    {
        return -3;
    }
    if (plaintext_size < (size_t)bytes_read - NCRYPT_PACKET_OVERHEAD)
    {
        return -4;
    }

    struct peer * peer = ncrypt_get_peer(nc, &remote);
    if (peer == NULL)
    {
        if (ncrypt_add_peer(nc, &remote) != 0)
        {
            return -5;
        }
        peer = ncrypt_get_peer(nc, &remote);
        ncrypt_init_decrypt(nc, &peer->aes);
    }

    if (memcmp(peer->iv, encrypted.iv, NCRYPT_AES_IVLEN) != 0)
    {
        peer->iv_mismatches++;
        memcpy(peer->iv, encrypted.iv, NCRYPT_AES_IVLEN);
    }
    int decrypted_used =
        decrypt(&peer->aes, encrypted.data, bytes_read - NCRYPT_PACKET_OVERHEAD, peer->iv, encrypted.tag, plaintext);
    next_iv(peer);

    if (decrypted_used < 0)
    {
        return -6;
    }

    return 0;
}