aboutsummaryrefslogtreecommitdiffstats
path: root/src/inexact.c
blob: 16d617513f6c9f640a1b7736b56b943787ba2588 (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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
/* Inexact source code package.
 *
 * Written in 2019 by <ben@hackade.org>.
 *
 * To the extent possible under law, the author have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#include "inexact.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>  // for chmod only
#include <unistd.h>
#include "argon2.h"
#include "base64.h"
#include "chacha20_drng.h"
#include "curve25519.h"
#include "norx_inexact.h"
#include "readpassphrase.h"
#include "sha3.h"

/*
 * Generate a random private key and derive the X25519 public key.
 * Store result in base64 to files.
 *
 */
int generate_keys(const char *seckey_filename, const char *pubkey_filename,
                  int no_password) {
    unsigned char *privatekey_b64 = NULL;
    unsigned char *publickey_b64 = NULL;
    unsigned char *privatekey_b64t = NULL;
    unsigned char *publickey_b64t = NULL;
    unsigned char salt[32] = {0};
    unsigned char privatekey_buffer[64] = {0};

    char password[256] = {0};
    char *password_out = NULL;
    char password_verif[256] = {0};
    char *password_verif_out = NULL;
    size_t password_len = 0;
    size_t password_verif_len = 0;

    const uint32_t t_cost = 3;
    const uint32_t m_cost = (1 << 12);
    const uint32_t parallelism = 1;

    size_t private_base64_len = 0;
    size_t private_b64t_len = 0;
    size_t public_base64_len = 0;
    size_t public_b64t_len = 0;

    FILE *fs = NULL;
    FILE *fp = NULL;

    struct chacha20_drng *drng = NULL;

    int exitcode = 1;

    curve25519_key privatekey;
    curve25519_key publickey;

    /* Secret (or private) key */

    if (no_password) {
        int ret = drng_chacha20_init(&drng);
        if (ret) {
            printf("Chacha20 allocation failed: %d\n", ret);
            goto exit;
        }
        if (drng_chacha20_get(drng, privatekey, sizeof(curve25519_key))) {
            printf("Getting random numbers failed\n");
            goto exit;
        }

        curve25519_donna_basepoint(publickey, privatekey);

        memcpy(privatekey_buffer, privatekey, sizeof(curve25519_key));
        memcpy(privatekey_buffer + sizeof(curve25519_key), publickey,
               sizeof(curve25519_key));
    } else {
        struct chacha20_drng *drng;
        int ret = drng_chacha20_init(&drng);
        if (ret) {
            printf("Chacha20 allocation failed: %d\n", ret);
            goto exit;
        }
        if (drng_chacha20_get(drng, salt, sizeof(salt))) {
            printf("Getting random numbers failed\n");
            goto exit;
        }

        password_out =
            readpassphrase("Password : ", password, sizeof(password), 0);
        if (password_out != password) {
            printf("password input failed.\n");
            goto exit;
        }
        password_len = strlen(password);

        password_verif_out =
            readpassphrase("Verifying, please re-enter : ", password_verif,
                           sizeof(password_verif), 0);
        if (password_verif_out != password_verif) {
            printf("password input failed.\n");
            goto exit;
        }
        password_verif_len = strlen(password_verif);

        if (password_len != password_verif_len) {
            printf("Mismatch.\n");
            goto exit;
        }

        if (memcmp(password, password_verif, password_len) != 0) {
            printf("Mismatch.\n");
            goto exit;
        }

        int a2res = argon2id_hash_raw(t_cost, m_cost, parallelism, password,
                                      password_len, salt, sizeof(salt),
                                      privatekey, sizeof(curve25519_key));
        if (a2res != ARGON2_OK) {
            printf("argon2 failed.");
            goto exit;
        }

        curve25519_donna_basepoint(publickey, privatekey);
        memcpy(privatekey_buffer, salt, sizeof(salt));
        memcpy(privatekey_buffer + sizeof(salt), publickey,
               sizeof(curve25519_key));
    }

    privatekey_b64 = base64_encode(privatekey_buffer, sizeof(privatekey_buffer),
                                   &private_base64_len);
    if (privatekey_b64 == NULL) {
        printf("base64 encoding failed.\n");
        goto exit;
    }

    privatekey_b64t =
        b64t_encode(privatekey_b64, private_base64_len, &private_b64t_len);
    if (privatekey_b64t == NULL) {
        printf("b64t encoding failed.\n");
        goto exit;
    }

    fs = fopen(seckey_filename, "wb");
    if (fs == NULL) {
        printf("secret key file opening failed: %s.\n", strerror(errno));
        goto exit;
    }

    ssize_t slen = fwrite(privatekey_b64t, 1, private_b64t_len, fs);
    if (slen != private_b64t_len) {
        printf("secret key file writing failed: %s.\n", strerror(errno));
        goto exit;
    }
    if (fwrite("\n", 1, 1, fs) != 1) {
        printf("public key file writing failed: %s.\n", strerror(errno));
        goto exit;
    }

    int res = chmod(seckey_filename, S_IRUSR | S_IWUSR);
    if (res != 0) {
        printf("secret key file chmod failed: %s.\n", strerror(errno));
        goto exit;
    }

    /* Public key */

    publickey_b64 =
        base64_encode(publickey, sizeof(curve25519_key), &public_base64_len);

    if (publickey_b64 == NULL) {
        printf("base64 encoding failed.\n");
        goto exit;
    }

    publickey_b64t =
        b64t_encode(publickey_b64, public_base64_len, &public_b64t_len);

    if (publickey_b64t == NULL) {
        printf("b64t encoding failed.\n");
        goto exit;
    }

    fp = fopen(pubkey_filename, "wb");
    if (fp == NULL) {
        printf("public key file opening failed: %s.\n", strerror(errno));
        goto exit;
    }

    ssize_t plen = fwrite(publickey_b64t, 1, public_b64t_len, fp);
    if (plen != public_b64t_len) {
        printf("public key file writing failed: %s.\n", strerror(errno));
        goto exit;
    }
    if (fwrite("\n", 1, 1, fp) != 1) {
        printf("public key file writing failed: %s.\n", strerror(errno));
        goto exit;
    }

    exitcode = 0;

exit:
    drng_chacha20_destroy(drng);

    memset(privatekey_b64, 0, private_base64_len);
    memset(privatekey_b64t, 0, private_b64t_len);
    memset(privatekey, 0, sizeof(curve25519_key));
    memset(publickey_b64, 0, public_base64_len);
    memset(publickey_b64t, 0, public_b64t_len);
    memset(publickey, 0, sizeof(curve25519_key));
    memset(salt, 0, sizeof(salt));
    memset(privatekey_buffer, 0, sizeof(privatekey_buffer));
    memset(password, 0, sizeof(password));
    memset(password_verif, 0, sizeof(password_verif));

    free(privatekey_b64);
    free(publickey_b64);
    free(privatekey_b64t);
    free(publickey_b64t);

    if (fs != NULL) {
        fclose(fs);
    }
    if (fp != NULL) {
        fclose(fp);
    }

    return exitcode;
}

/*
 * Return key file content in key variable.
 *
 */
int get_seckey(const char *keyfile, unsigned char *skey, unsigned char *pkey) {
    unsigned char *base64_decoded = NULL;
    unsigned char *b64t_decoded = NULL;

    size_t base64_decoded_len = 0;
    size_t b64t_decoded_len = 0;
    size_t password_len = 0;

    FILE *fs = NULL;

    unsigned char salt[32] = {0};
    char password[256] = {0};
    char *password_out = NULL;
    curve25519_key pubkey;
    curve25519_key pubkey_from_secret;
    curve25519_key seckey;

    const uint32_t t_cost = 3;
    const uint32_t m_cost = (1 << 12);
    const uint32_t parallelism = 1;

    int exitcode = 1;

    fs = fopen(keyfile, "rb");
    if (fs == NULL) {
        printf("key file opening failed: %s.\n", strerror(errno));
        goto exit;
    }

    int rsb = fseek(fs, 0L, SEEK_END);
    if (rsb != 0) {
        printf("seek to end file '%s' failed: %s.\n", keyfile, strerror(errno));
        goto exit;
    }

    size_t sz = ftell(fs);
    if (sz == -1) {
        printf("tell file '%s' failed: %s.\n", keyfile, strerror(errno));
        goto exit;
    }

    int rse = fseek(fs, 0L, SEEK_SET);
    if (rse != 0) {
        printf("seek file to begin'%s' failed: %s.\n", keyfile,
               strerror(errno));
        goto exit;
    }

    /* max_size = base64(sizeof(curve25519_key)) = 64 * 4 / 3 + 1 -> 86 */
    unsigned char file_data[87] = {0};

    if (sz > sizeof(file_data)) {
        printf("Bad key size\n");
        goto exit;
    }

    size_t readed = fread(&file_data, 1, sz, fs);
    if (readed != sz) {
        printf("read file '%s' failed: %s.\n", keyfile, strerror(errno));
        goto exit;
    }

    b64t_decoded = b64t_decode(file_data, readed, &b64t_decoded_len);
    if (b64t_decoded == NULL) {
        printf("b64t decoding failed.\n");
        goto exit;
    }

    base64_decoded =
        base64_decode(b64t_decoded, b64t_decoded_len, &base64_decoded_len);
    if (base64_decoded == NULL) {
        printf("base64 decoding failed.\n");
        goto exit;
    }

    if (base64_decoded_len != 64) {
        printf("decoded size mismatch.\n");
        goto exit;
    }

    memcpy(seckey, base64_decoded, sizeof(curve25519_key));
    memcpy(pubkey, base64_decoded + sizeof(curve25519_key),
           sizeof(curve25519_key));
    curve25519_donna_basepoint(pubkey_from_secret, seckey);

    int password_protected_flag =
        (memcmp(pubkey, pubkey_from_secret, sizeof(curve25519_key)) != 0);
    if (password_protected_flag) {
        memcpy(salt, base64_decoded, sizeof(salt));
        password_out =
            readpassphrase("Password : ", password, sizeof(password), 0);
        if (password_out != password) {
            printf("password input failed.\n");
            goto exit;
        }
        password_len = strlen(password);

        int a2res = argon2id_hash_raw(t_cost, m_cost, parallelism, password,
                                      password_len, salt, sizeof(salt), seckey,
                                      sizeof(curve25519_key));
        if (a2res != ARGON2_OK) {
            printf("argon2 failed.");
            goto exit;
        }
        curve25519_donna_basepoint(pubkey_from_secret, seckey);
        if (memcmp(pubkey_from_secret, pubkey, sizeof(curve25519_key)) != 0) {
            printf("Bad password\n");
            goto exit;
        }
    }

    memcpy(skey, seckey, sizeof(curve25519_key));
    if (pkey != NULL) {
        memcpy(pkey, pubkey_from_secret, sizeof(curve25519_key));
    }
    exitcode = 0;

exit:
    memset(file_data, 0, sizeof(file_data));
    memset(base64_decoded, 0, base64_decoded_len);
    memset(b64t_decoded, 0, b64t_decoded_len);
    memset(salt, 0, sizeof(salt));
    memset(password, 0, sizeof(password));
    memset(seckey, 0, sizeof(curve25519_key));
    memset(pubkey, 0, sizeof(curve25519_key));
    memset(pubkey_from_secret, 0, sizeof(curve25519_key));

    if (fs != NULL) {
        fclose(fs);
    }

    free(base64_decoded);
    free(b64t_decoded);

    return exitcode;
}

/*
 * Return key file content in key variable.
 *
 */
int get_pubkey(const char *keyfile, unsigned char *pkey) {
    unsigned char *base64_decoded = NULL;
    unsigned char *b64t_decoded = NULL;

    size_t b64t_decoded_len = 0;
    size_t base64_decoded_len = 0;

    FILE *fs = NULL;

    curve25519_key pubkey;

    int exitcode = 1;

    fs = fopen(keyfile, "rb");
    if (fs == NULL) {
        printf("key file opening failed: %s.\n", strerror(errno));
        goto exit;
    }

    int rsb = fseek(fs, 0L, SEEK_END);
    if (rsb != 0) {
        printf("seek to end file '%s' failed: %s.\n", keyfile, strerror(errno));
        goto exit;
    }

    size_t sz = ftell(fs);
    if (sz == -1) {
        printf("tell file '%s' failed: %s.\n", keyfile, strerror(errno));
        goto exit;
    }

    int rse = fseek(fs, 0L, SEEK_SET);
    if (rse != 0) {
        printf("seek file to begin'%s' failed: %s.\n", keyfile,
               strerror(errno));
        goto exit;
    }

    /* max_size = base64(sizeof(curve25519_key)) = 32 * 4 / 3 + 1 -> 44 */
    unsigned char file_data[44] = {0};

    if (sz > sizeof(file_data)) {
        printf("Bad key size\n");
        goto exit;
    }

    size_t readed = fread(&file_data, 1, sz, fs);
    if (readed != sz) {
        printf("read file '%s' failed: %s.\n", keyfile, strerror(errno));
        goto exit;
    }

    b64t_decoded = b64t_decode(file_data, readed, &b64t_decoded_len);
    if (b64t_decoded == NULL) {
        printf("b64t decoding failed.\n");
        goto exit;
    }

    base64_decoded =
        base64_decode(b64t_decoded, b64t_decoded_len, &base64_decoded_len);
    if (base64_decoded == NULL) {
        printf("base64 decoding failed.\n");
        goto exit;
    }

    if (base64_decoded_len != 32) {
        printf("decoded size mismatch.\n");
        goto exit;
    }

    memcpy(pkey, base64_decoded, 32);

    exitcode = 0;

exit:
    memset(file_data, 0, sizeof(file_data));
    memset(b64t_decoded, 0, b64t_decoded_len);
    memset(base64_decoded, 0, base64_decoded_len);
    memset(pubkey, 0, sizeof(curve25519_key));

    if (fs != NULL) {
        fclose(fs);
    }

    free(b64t_decoded);
    free(base64_decoded);

    return exitcode;
}

/*
 * Encrypt data, return allocated message in base64.
 *
 */
unsigned char *encrypt_data(const unsigned char *seckey,
                            const unsigned char *pubkey,
                            const unsigned char *salt,
                            const unsigned char *data,
                            size_t data_len,            // in bytes
                            size_t rand_len,            // in bytes
                            size_t tag1_len,            // in bytes
                            int base64_transformation,  // 0 or 1
                            size_t *out_encrypted_len) {
    unsigned char *rand = NULL;
    unsigned char *encrypted1 = NULL;
    unsigned char *part1 = NULL;
    unsigned char *encrypted = NULL;
    unsigned char *encrypted_b64 = NULL;
    unsigned char *encrypted_b64t = NULL;
    unsigned char *out = NULL;

    size_t encrypted1_len = 0;
    size_t norx_encrypted1_len = 0;
    size_t tag1_len_bits = tag1_len * 8;
    size_t part1_len = 0;
    size_t part0_len = 0;
    size_t norx_params_encrypted_len = 0;
    size_t encrypted_b64_len = 0;
    size_t encrypted_b64t_len = 0;
    size_t encrypted_len = 0;

    const size_t shared_secret_len = 32;
    unsigned char shared_secret[32] = {0};

    const size_t nonce1_len = 32;
    const size_t tag0_len = 4;
    const size_t tag0_len_bits = tag0_len * 8;
    const size_t params_len = 5;
    const size_t params_encrypted_len = 9;  // params_len + tag0_len
    const size_t nonce0_len = 32;
    const size_t header0_len = 4;
    const size_t encrypted_len_expected = data_len + tag1_len;

    const uint8_t *key1 = 0;
    const uint8_t *nonce1 = 0;
    unsigned char params[5] = {0};
    unsigned char params_encrypted[9] = {0};
    const uint8_t *key0 = 0;
    const uint8_t *nonce0 = 0;
    unsigned char header0[4] = {0};

    struct chacha20_drng *drng = NULL;

    *out_encrypted_len = 0;

    // generate shared secret from DH X25519
    curve25519_donna(shared_secret, seckey, pubkey);

    // generate part 1 random data
    rand = malloc(rand_len);
    if (rand == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }

    int ret = drng_chacha20_init(&drng);
    if (ret) {
        printf("Chacha20 allocation failed: %d\n", ret);
        goto exit;
    }
    if (drng_chacha20_get(drng, rand, rand_len)) {
        printf("Getting random numbers failed\n");
        goto exit;
    }

    // generate part 0 message
    part0_len = params_len + tag0_len;

    params[0] = (rand_len >> 24) & 0xFF;
    params[1] = (rand_len >> 16) & 0xFF;
    params[2] = (rand_len >> 8) & 0xFF;
    params[3] = rand_len & 0xFF;

    params[4] = tag1_len;

    // calculate part length
    encrypted1_len = tag1_len + data_len;
    part1_len = encrypted1_len + rand_len;
    encrypted_len = part0_len + part1_len;

    // generate part 0 header
    header0[0] = (encrypted_len >> 24) & 0xFF;
    header0[1] = (encrypted_len >> 16) & 0xFF;
    header0[2] = (encrypted_len >> 8) & 0xFF;
    header0[3] = encrypted_len & 0xFF;

    // generate nonce 1
    sha3_context nc1;
    sha3_Init256(&nc1);
    sha3_Update(&nc1, params, params_len);
    sha3_Update(&nc1, rand, rand_len);
    nonce1 = sha3_Finalize(&nc1);

    // generate key for part1
    sha3_context kc1;
    sha3_Init256(&kc1);
    sha3_Update(&kc1, nonce1, nonce1_len);
    sha3_Update(&kc1, shared_secret, shared_secret_len);
    key1 = sha3_Finalize(&kc1);

    // encrypt message data
    encrypted1 = malloc(encrypted1_len);
    if (encrypted1 == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }

    norx_aead_encrypt(encrypted1, &norx_encrypted1_len, params, params_len,
                      data, data_len, NULL, 0, nonce1, key1, tag1_len_bits);
    if (encrypted1_len != norx_encrypted1_len ||
        norx_encrypted1_len != encrypted_len_expected) {
        printf("Norx encryption failed.\n");
        goto exit;
    }

    // generate full part 1 buffer
    part1 = malloc(part1_len);
    if (part1 == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }
    memcpy(part1, rand, rand_len);
    memcpy((part1 + rand_len), encrypted1, encrypted1_len);

    // generate part 0 nonce: sha3-256(rand+encrypted1)
    sha3_context nc0;
    sha3_Init256(&nc0);
    sha3_Update(&nc0, part1, part1_len);
    nonce0 = sha3_Finalize(&nc0);

    // generate key for part 0
    sha3_context kc0;
    sha3_Init256(&kc0);
    sha3_Update(&kc0, nonce0, nonce0_len);
    sha3_Update(&kc0, shared_secret, shared_secret_len);
    key0 = sha3_Finalize(&kc0);

    // encrypt params (part 0 message)
    norx_aead_encrypt(params_encrypted, &norx_params_encrypted_len, header0,
                      header0_len, params, params_len, NULL, 0, nonce0, key0,
                      tag0_len_bits);
    if (params_encrypted_len != norx_params_encrypted_len) {
        printf("Norx encryption failed.\n");
        goto exit;
    }

    // symmetric case
    if (salt) {
        encrypted_len = encrypted_len + 64;
    }

    // generate full message buffer
    encrypted = malloc(encrypted_len);
    if (encrypted == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }

    if (salt) {
        memcpy(encrypted, salt, 32);
        memcpy(32 + encrypted, pubkey, 32);
        memcpy(64 + encrypted, params_encrypted, params_encrypted_len);
        memcpy(64 + encrypted + params_encrypted_len, part1, part1_len);
    } else {
        memcpy(encrypted, params_encrypted, params_encrypted_len);
        memcpy(encrypted + params_encrypted_len, part1, part1_len);
    }

    // encode full message in base64
    encrypted_b64 = base64_encode(encrypted, encrypted_len, &encrypted_b64_len);
    if (encrypted_b64 == NULL || encrypted_b64_len == 0 ||
        encrypted_b64_len < encrypted_len) {
        printf("Base64 encoding failed.\n");
        goto exit;
    }

    // transform base64 encoding
    if (base64_transformation) {
        encrypted_b64t =
            b64t_encode(encrypted_b64, encrypted_b64_len, &encrypted_b64t_len);
        if (encrypted_b64t == NULL || encrypted_b64t_len == 0) {
            memset(encrypted_b64, 0, encrypted_b64_len);
            free(encrypted_b64);
            goto exit;
        }
        out = encrypted_b64t;
        *out_encrypted_len = encrypted_b64t_len;
        memset(encrypted_b64, 0, encrypted_b64_len);
        free(encrypted_b64);
    } else {
        out = encrypted_b64;
        *out_encrypted_len = encrypted_b64_len;
    }

exit:
    drng_chacha20_destroy(drng);

    memset(rand, 0, rand_len);
    memset(encrypted1, 0, encrypted1_len);
    memset(part1, 0, part1_len);
    memset(encrypted, 0, encrypted_len);
    memset(params, 0, params_len);
    memset(params_encrypted, 0, params_encrypted_len);
    memset(shared_secret, 0, shared_secret_len);
    memset(header0, 0, header0_len);

    memset(&kc0, 0, sizeof(sha3_context));
    memset(&kc1, 0, sizeof(sha3_context));
    memset(&nc0, 0, sizeof(sha3_context));
    memset(&nc1, 0, sizeof(sha3_context));

    free(rand);
    free(encrypted1);
    free(part1);
    free(encrypted);

    return out;
}

int get_symmetrickeys(unsigned char *salt_out, unsigned char *seckey_out,
                      unsigned char *pubkey_out) {
    struct chacha20_drng *drng;

    const uint32_t t_cost = 3;
    const uint32_t m_cost = (1 << 12);
    const uint32_t parallelism = 1;

    char password[256] = {0};
    char *password_out = NULL;
    char password_verif[256] = {0};
    char *password_verif_out = NULL;
    size_t password_len = 0;
    size_t password_verif_len = 0;

    int exitcode = 1;

    int ret = drng_chacha20_init(&drng);
    if (ret) {
        printf("Chacha20 allocation failed: %d\n", ret);
        goto exit;
    }
    if (drng_chacha20_get(drng, salt_out, 32)) {
        printf("Getting random numbers failed\n");
        goto exit;
    }

    password_out = readpassphrase("Password : ", password, sizeof(password), 0);
    if (password_out != password) {
        printf("password input failed.\n");
        goto exit;
    }
    password_len = strlen(password);

    password_verif_out =
        readpassphrase("Verifying, please re-enter : ", password_verif,
                       sizeof(password_verif), 0);
    if (password_verif_out != password_verif) {
        printf("password input failed.\n");
        goto exit;
    }
    password_verif_len = strlen(password_verif);

    if (password_len != password_verif_len) {
        printf("Mismatch.\n");
        goto exit;
    }

    if (memcmp(password, password_verif, password_len) != 0) {
        printf("Mismatch.\n");
        goto exit;
    }

    int a2res =
        argon2id_hash_raw(t_cost, m_cost, parallelism, password, password_len,
                          salt_out, 32, seckey_out, sizeof(curve25519_key));
    if (a2res != ARGON2_OK) {
        printf("argon2 failed.");
        goto exit;
    }

    curve25519_donna_basepoint(pubkey_out, seckey_out);

    exitcode = 0;

exit:
    drng_chacha20_destroy(drng);
    memset(password, 0, sizeof(password));
    memset(password_verif, 0, sizeof(password_verif));

    return exitcode;
}

int check_get_symmetrickeys(const unsigned char *data, const size_t data_len,
                            unsigned char *seckey_out,
                            unsigned char *pubkey_out) {
    const uint32_t t_cost = 3;
    const uint32_t m_cost = (1 << 12);
    const uint32_t parallelism = 1;

    char password[256] = {0};
    char *password_out = NULL;
    size_t password_len = 0;

    unsigned char *data_b64t_decoded = NULL;
    unsigned char *encrypted = NULL;
    size_t data_b64t_decoded_len = 0;
    size_t encrypted_len = 0;

    unsigned char salt[32] = {0};

    curve25519_key pubkey_from_secret;
    int exitcode = 1;

    data_b64t_decoded = b64t_decode(data, data_len, &data_b64t_decoded_len);

    encrypted =
        base64_decode(data_b64t_decoded, data_b64t_decoded_len, &encrypted_len);
    if (encrypted == NULL) {
        printf("base64 decoding failed.\n");
        goto exit;
    }

    memcpy(salt, encrypted, 32);
    memcpy(pubkey_from_secret, encrypted + 32, 32);

    password_out = readpassphrase("Password : ", password, sizeof(password), 0);
    if (password_out != password) {
        printf("password input failed.\n");
        goto exit;
    }
    password_len = strlen(password);

    int a2res =
        argon2id_hash_raw(t_cost, m_cost, parallelism, password, password_len,
                          salt, 32, seckey_out, sizeof(curve25519_key));
    if (a2res != ARGON2_OK) {
        printf("argon2 failed.");
        goto exit;
    }

    curve25519_donna_basepoint(pubkey_out, seckey_out);
    if (memcmp(pubkey_out, pubkey_from_secret, sizeof(curve25519_key)) != 0) {
        printf("Wrong password\n");
        memset(seckey_out, 0, sizeof(curve25519_key));
        memset(salt, 0, 32);
        memset(pubkey_out, 0, sizeof(curve25519_key));
        goto exit;
    }

    exitcode = 0;

exit:
    memset(password, 0, sizeof(password));
    memset(encrypted, 0, encrypted_len);
    memset(data_b64t_decoded, 0, data_b64t_decoded_len);

    free(encrypted);
    free(data_b64t_decoded);

    return exitcode;
}

/*
 * Decrypt data, return allocated clear text message.
 *
 */
unsigned char *decrypt_data(const unsigned char *seckey,
                            const unsigned char *pubkey,
                            const unsigned char *data, size_t data_len,
                            int symmetric_flag, size_t *data_len_out) {
    unsigned char *data_b64t_decoded = NULL;
    unsigned char *encrypted = NULL;
    unsigned char *part1 = NULL;
    unsigned char *rand = NULL;
    unsigned char *message = NULL;
    unsigned char *encrypted1 = NULL;

    size_t shared_secret_len = 32;
    unsigned char shared_secret[32] = {0};

    size_t encrypted_len = 0;
    size_t data_b64t_decoded_len = 0;
    size_t part1_len = 0;
    size_t rand_len = 0;
    size_t tag1_len = 0;
    size_t tag1_len_bits = 0;
    size_t encrypted1_len = 0;
    size_t message_len = 0;
    size_t norx_message_len = 0;

    const size_t tag1_len_min = 4;
    const size_t rand_len_min = 4;
    const size_t nonce1_len = 32;
    const size_t tag0_len = 4;
    const size_t tag0_len_bits = tag0_len * 8;
    const size_t part0_len = 9;  // params_len + tag0_len;
    const size_t nonce0_len = 32;

    const uint8_t *nonce0 = 0;
    const size_t header0_len = 4;
    unsigned char header0[4] = {0};
    const uint8_t *key0 = 0;
    unsigned char encrypted0[9] = {0};
    const size_t params_len = 5;
    unsigned char params[5];
    size_t norx_params_len = 0;
    const uint8_t *nonce1 = 0;
    const uint8_t *key1 = 0;

    // shared secret DH X25519
    curve25519_donna(shared_secret, seckey, pubkey);

    // decode base64
    data_b64t_decoded = b64t_decode(data, data_len, &data_b64t_decoded_len);

    // decode base64 transformation
    encrypted =
        base64_decode(data_b64t_decoded, data_b64t_decoded_len, &encrypted_len);
    if (encrypted == NULL) {
        printf("base64 decoding failed.\n");
        goto exit;
    }
    if (encrypted_len < part0_len + tag1_len_min + rand_len_min) {
        printf("Size mismatch.\n");
        goto exit;
    }

    if (symmetric_flag) {
        encrypted = encrypted + 64;
        encrypted_len = encrypted_len - 64;
    }

    // part1 message allocation and copy
    part1_len = encrypted_len - part0_len;
    part1 = malloc(part1_len);
    if (part1 == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }
    memcpy(part1, encrypted + part0_len, part1_len);

    // generate part 0 header
    header0[0] = (encrypted_len >> 24) & 0xFF;
    header0[1] = (encrypted_len >> 16) & 0xFF;
    header0[2] = (encrypted_len >> 8) & 0xFF;
    header0[3] = encrypted_len & 0xFF;

    // nonce 0 generation: sha3-256(part1)
    sha3_context nc0;
    sha3_Init256(&nc0);
    sha3_Update(&nc0, part1, part1_len);
    nonce0 = sha3_Finalize(&nc0);

    // generate key 0
    sha3_context kc0;
    sha3_Init256(&kc0);
    sha3_Update(&kc0, nonce0, nonce0_len);
    sha3_Update(&kc0, shared_secret, shared_secret_len);
    key0 = sha3_Finalize(&kc0);

    // decrypt part 0 part (params)
    memcpy(encrypted0, encrypted, part0_len);

    int n0 = norx_aead_decrypt(params, &norx_params_len, header0, header0_len,
                               encrypted0, part0_len, NULL, 0, nonce0, key0,
                               tag0_len_bits);

    if (n0 != 0) {
        printf("Norx0 decryption failed.\n");
        goto exit;
    }

    // get part 1 encryption params
    rand_len =
        (params[0] << 24) + (params[1] << 16) + (params[2] << 8) + params[3];
    tag1_len = params[4];
    tag1_len_bits = tag1_len * 8;

    if (tag1_len != 4 && tag1_len != 8 && tag1_len != 16 && tag1_len != 24 &&
        tag1_len != 32) {
        printf("bad auth tag len.\n");
        goto exit;
    }
    if (rand_len > (encrypted_len - part0_len - tag1_len) ||
        rand_len < rand_len_min) {
        printf("size mismatch.\n");
        goto exit;
    }

    // allocate and copy rand 1
    rand = malloc(rand_len);
    if (rand == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }
    memcpy(rand, encrypted + part0_len, rand_len);

    // generate nonce1: sha3-256(rand)
    sha3_context nc1;
    sha3_Init256(&nc1);
    sha3_Update(&nc1, params, params_len);
    sha3_Update(&nc1, rand, rand_len);
    nonce1 = sha3_Finalize(&nc1);

    // generate key for part1
    sha3_context kc1;
    sha3_Init256(&kc1);
    sha3_Update(&kc1, nonce1, nonce1_len);
    sha3_Update(&kc1, shared_secret, shared_secret_len);
    key1 = sha3_Finalize(&kc1);

    // get encrypted message buffer
    encrypted1 = encrypted + part0_len + rand_len;
    encrypted1_len = encrypted_len - part0_len - rand_len;

    message_len = encrypted1_len - tag1_len;
    message = malloc(message_len);
    if (message == NULL) {
        printf("malloc failed.\n");
        goto exit;
    }

    int n1 = norx_aead_decrypt(message, &norx_message_len, params, params_len,
                               encrypted1, encrypted1_len, NULL, 0, nonce1,
                               key1, tag1_len_bits);
    if (n1 != 0 || norx_message_len != message_len) {
        printf("Norx1 decryption failed.\n");
        goto exit;
    }

    *data_len_out = message_len;

exit:
    memset(data_b64t_decoded, 0, data_b64t_decoded_len);
    memset(encrypted, 0, encrypted_len);
    memset(part1, 0, part1_len);
    memset(rand, 0, rand_len);
    memset(shared_secret, 0, shared_secret_len);
    memset(encrypted0, 0, part0_len);
    memset(params, 0, params_len);
    memset(header0, 0, header0_len);

    memset(&kc0, 0, sizeof(sha3_context));
    memset(&kc1, 0, sizeof(sha3_context));
    memset(&nc0, 0, sizeof(sha3_context));
    memset(&nc1, 0, sizeof(sha3_context));

    free(data_b64t_decoded);
    if (symmetric_flag)
        free(encrypted - 64);
    else
        free(encrypted);
    free(part1);
    free(rand);

    return message;
    return NULL;
}