aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 53f2844d201c92c1dd88861d7d5c985dd31c8ffa (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
/* 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 <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "argtable3.h"
#include "inexact.h"
#include "tests.h"

/* global arg_xxx structs */
struct arg_lit *help, *version, *gen, *dencrypt, *ddecrypt, *test, *base64, *weak,
    *nopassword, *symmetric;
struct arg_file *seckey, *pubkey, *infile, *outfile;
struct arg_end *end;
struct arg_int *taglen, *noncelen, *cipherlen;

int file_exist (const char *filename);

int main(int argc, char *argv[]) {
    /* the global arg_xxx structs are initialised within the argtable */
    void *argtable[] = {
        help        = arg_litn("h", "help", 0, 1, "display this help and exit"),
        version     = arg_litn("v", "version", 0, 1, "display version info and exit"),
        gen         = arg_litn("g", "genkeys", 0, 1, "generate keys"),
        dencrypt    = arg_litn("e", "encrypt", 0, 1, "encrypt data"),
        ddecrypt    = arg_litn("d", "decrypt", 0, 1, "decrypt data"),
        symmetric   = arg_litn("s", "symmetric", 0, 1, "symmetric encryption with password"),
        seckey      = arg_filen("k", "seckey", "secretkey", 0, 1, "secret key file"),
        pubkey      = arg_filen("p", "pubkey", "publickey", 0, 1, "public key iles"),
        taglen      = arg_intn("t", "taglen", "<64,128,192,256>", 0, 1, "authentication message tag length in bits (default: 256)"),
        noncelen    = arg_intn("n", "noncelen", "<n>", 0, 1, "random nonce length in bytes (default: 32, must be >= 16)"),
        cipherlen   = arg_intn("c", "cipherlen", "<n>", 0, 1, "set random nonce length for <n> bytes output ciphertext size"),
        base64      = arg_litn(NULL, "base64", 0, 1, "use base64 format without transformation"),
        test        = arg_litn(NULL, "test", 0, 1, "test crypto and encoding internal functions"),
        nopassword  = arg_litn(NULL, "no-password", 0, 1, "generate secret key without password"),
        weak        = arg_litn("w", "weak", 0, 1, "use weak length for nonce and auth tag (-n 4 -t 32)"),
        infile      = arg_filen("i", "input-file", "<infile>", 0, 1, "input file (default: stdin)"),
        outfile     = arg_filen("o", "output-file", "<outfile>", 0, 1, "output file (default: stdout)"),
        end = arg_end(20),
    };

    int exitcode = 0;
    const char progname[] = "inexact";
    const char ver[] = "beta 1.02";
    FILE *fo = NULL;

    int nerrors;
    nerrors = arg_parse(argc, argv, argtable);

    /* special case: '--help' takes precedence over error reporting */
    if (help->count > 0) {
        printf("Usage: %s", progname);
        arg_print_syntax(stdout, argtable, "\n");
        printf(
            "INadvisable EXperimental Asymmetric Crypto Tool, by "
            "<ben@hackade.org>.\n\n");
        arg_print_glossary(stdout, argtable, "  %-25s %s\n");
        exitcode = 0;
        goto exit;
    }

    /* If the parser returned any errors then display them and exit */
    if (nerrors > 0) {
        /* Display the error details contained in the arg_end struct.*/
        arg_print_errors(stdout, end, progname);
        printf("Try '%s --help' for more information.\n", progname);
        exitcode = 1;
        goto exit;
    }

    /* check if an action is specified */
    int action_count = gen->count + ddecrypt->count + dencrypt->count +
                       version->count + help->count + test->count;
    if (action_count == 0) {
        printf("Missing parameters.\n");
        printf("Try '%s --help' for more information.\n", progname);
        exitcode = 1;
        goto exit;
    }

    if (version->count == 1) {
        printf("version: %s\n", ver);
        exitcode = 0;
        goto exit;
    }

    if (test->count > 0) {
        exitcode = test_all();
        goto exit;
    }

    /* check if more than one action is specified */
    if (action_count > 1) {
        printf("Invalid options.\n");
        printf("Try '%s --help' for more information.\n", progname);
        exitcode = 1;
        goto exit;
    }

    if (symmetric->count == 1) {
        if (gen->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (pubkey->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (seckey->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (nopassword->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
    }

    /* check if a secret key is specified */
    if (seckey->count == 0 && symmetric->count == 0) {
        printf("Missing secret key file operand.\n");
        printf("Try '%s --help' for more information.\n", progname);
        exitcode = 1;
        goto exit;
    }

    /* check if public key is specified */
    if (pubkey->count == 0 && symmetric->count == 0) {
        printf("Missing public key file operand.\n");
        printf("Try '%s --help' for more information.\n", progname);
        exitcode = 1;
        goto exit;
    }

    int nopassword_flag = (nopassword->count == 1);

    /* generate action */
    if (gen->count == 1) {
        if (base64->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (infile->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (outfile->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (taglen->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (noncelen->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (file_exist(seckey->filename[0]) == 1) {
            char ch;
            printf("Overwrite '%s' ? ", seckey->filename[0]);
            int res = scanf("%c", &ch);
            if ((ch != 'Y' && ch != 'y') || res == 0) {
                exitcode = 1;
                goto exit;
            }
        }
        if (file_exist(pubkey->filename[0]) == 1) {
            char ch;
            printf("Overwrite '%s' ? ", pubkey->filename[0]);
            int res = scanf(" %c", &ch);
            if ((ch != 'Y' && ch != 'y') || res == 0) {
                exitcode = 1;
                goto exit;
            }
        }

        return generate_keys(seckey->filename[0], pubkey->filename[0],
                             nopassword_flag);
    }

    /* read input infile data */
    unsigned char *data = NULL;
    int data_len = 0;

    if (infile->count == 0) {
        /* read stdin */
        int cap = 4096, len = 0;
        data = malloc(cap * sizeof(unsigned char));
        if (data == NULL) {
            printf("malloc %d bytes failed.\n", cap);
            exitcode = 1;
            goto exit;
        }
        int c = 0;
        do {
            c = fgetc(stdin);
            data[len] = c;
            if (++len == cap) {
                cap *= 2;
                data = realloc(data, cap * sizeof(unsigned char));
                if (data == NULL) {
                    printf("realloc %d bytes failed.\n", cap);
                    exitcode = 1;
                    goto exit;
                }
            }
        } while (!feof(stdin));
        fclose(stdin);
        data = realloc(data, len * sizeof(unsigned char));
        if (data == NULL) {
            printf("realloc %d bytes failed.\n", cap);
            exitcode = 1;
            goto exit;
        }
        data[len - 1] = '\0';
        data_len = len - 1;
    } else if (file_exist(infile->filename[0]) == 0) {
        printf("Input file '%s' not found.\n", infile->filename[0]);
        exitcode = 1;
        goto exit;
    } else {
        FILE *fi = fopen(infile->filename[0], "rb");
        if (fi == NULL) {
            printf("open file '%s' failed: %s.\n", infile->filename[0],
                   strerror(errno));
            exitcode = 1;
            goto exit;
        }
        int rsb = fseek(fi, 0L, SEEK_END);
        if (rsb != 0) {
            printf("seek to end file '%s' failed: %s.\n", infile->filename[0],
                   strerror(errno));
            fclose(fi);
            exitcode = 1;
            goto exit;
        }
        data_len = ftell(fi);
        if (data_len == -1) {
            printf("tell file '%s' failed: %s.\n", infile->filename[0],
                   strerror(errno));
            fclose(fi);
            exitcode = 1;
            goto exit;
        }
        int rse = fseek(fi, 0L, SEEK_SET);
        if (rse != 0) {
            printf("seek file to begin'%s' failed: %s.\n", infile->filename[0],
                   strerror(errno));
            fclose(fi);
            exitcode = 1;
            goto exit;
        }
        data = malloc(data_len);
        if (data == NULL) {
            printf("malloc %d bytes failed.\n", data_len);
            fclose(fi);
            exitcode = 1;
            goto exit;
        }
        size_t readed = fread(data, 1, data_len, fi);
        if (readed != data_len) {
            printf("read file '%s' failed: %s.\n", infile->filename[0],
                   strerror(errno));
            fclose(fi);
            exitcode = 1;
            goto exit;
        }
        fclose(fi);
    }

    /* check size limitation for input data (32 bits) */
    if (data_len > 0xbfffffff) {
        printf("Size of data too big.\n");
        exitcode = 1;
        goto exit;
    }

    int auth_tag_len = 32;
    if (taglen->count == 1) {
        if (ddecrypt->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (dencrypt->count == 0) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        int tag_len_value = taglen->ival[0];
        if (tag_len_value != 64 && tag_len_value != 128 &&
            tag_len_value != 192 && tag_len_value != 256) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        } else {
            auth_tag_len = tag_len_value / 8;
        }
    }

    /* output file */
    if (outfile->count == 0) {
        fo = stdout;
    } else {
        fo = fopen(outfile->filename[0], "wb");
        if (fo == NULL) {
            printf("open file '%s' failed: %s.\n", outfile->filename[0],
                   strerror(errno));
            exitcode = 1;
            goto exit;
        }
    }

    const int rand_nonce_len_min = 16;
    int rand_nonce_len = 32;
    if (noncelen->count == 1) {
        if (ddecrypt->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (dencrypt->count == 0) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (cipherlen->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        int noncelen_value = noncelen->ival[0];
        if (noncelen_value < rand_nonce_len_min) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        } else {
            rand_nonce_len = noncelen_value;
        }
    }

    int base64_transformation = (base64->count == 0);
    if (cipherlen->count == 1) {
        if (dencrypt->count == 0 || noncelen->count == 1 ||
            base64->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        size_t cipherlen_b64_value = cipherlen->ival[0];
        size_t cipherlen_value = cipherlen_b64_value * 3 / 4;

        size_t part0_len = 9;
        size_t part1_len = data_len + auth_tag_len + rand_nonce_len;
        size_t total_encrypted_len = part0_len + part1_len;
        size_t total_encrypted_len_without_rand1 =
            total_encrypted_len - rand_nonce_len;

        if (symmetric->count == 1) {
            total_encrypted_len_without_rand1 =
                total_encrypted_len_without_rand1 + 64;
        }

        size_t rand_nonce_len_needed =
            cipherlen_value - total_encrypted_len_without_rand1;

        if (total_encrypted_len_without_rand1 > cipherlen_value ||
            rand_nonce_len_needed < rand_nonce_len_min) {
            unsigned long total_cipherlen_b64_min =
                (total_encrypted_len_without_rand1 + rand_nonce_len_min) * 4 /
                3;
            printf("Insufficient nonce length, cipherlen must be > %lu\n",
                   total_cipherlen_b64_min);
            exitcode = 2;
            goto exit;
        } else {
            rand_nonce_len = rand_nonce_len_needed;
        }
    }

    if (weak->count == 1) {
        if (dencrypt->count == 0 || noncelen->count == 1 ||
            taglen->count == 1 || cipherlen->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        rand_nonce_len = 4;
        auth_tag_len = 4;
    }

    unsigned char *secretkey = malloc(32);
    unsigned char *publickey = malloc(32);
    unsigned char *salt = malloc(32);

    if (secretkey == NULL || publickey == NULL || salt == NULL) {
        printf("Malloc failed\n");
        exitcode = 1;
        goto exit;
    }

    if (dencrypt->count == 1) {
        unsigned char *psalt = NULL;

        if (symmetric->count == 1) {
            if (get_symmetrickeys(salt, secretkey, publickey) != 0) {
                printf("Symmetric keys generation failed.\n");
                exitcode = 1;
                goto exit;
            }
            psalt = &salt[0];
        } else {
            if (file_exist(seckey->filename[0]) == 0) {
                printf("Secret key file '%s' not found.\n",
                       seckey->filename[0]);
                exitcode = 1;
                goto exit;
            }
            if (get_seckey(seckey->filename[0], secretkey, NULL) == 1) {
                printf("File '%s': invalid key.\n", seckey->filename[0]);
                exitcode = 1;
                goto exit;
            }
            if (file_exist(pubkey->filename[0]) == 0) {
                printf("Public key file '%s' not found.\n",
                       pubkey->filename[0]);
                exitcode = 1;
                goto exit;
            }
            if (get_pubkey(pubkey->filename[0], publickey) == 1) {
                printf("File '%s': invalid key.\n", pubkey->filename[0]);
                exitcode = 1;
                goto exit;
            }
        }
        size_t encrypted_len = 0;
        unsigned char *encrypted = encrypt_data(
            secretkey, publickey, psalt, data, data_len, rand_nonce_len,
            auth_tag_len, base64_transformation, &encrypted_len);
        if (encrypted == NULL) {
            printf("Encryption failed.\n");
            exitcode = 1;
            goto exit;
        }
        fwrite(encrypted, 1, encrypted_len, fo);
        if (base64_transformation) {
            fwrite("\n", 1, 1, fo);
        }
        fflush(fo);
        memset(encrypted, 0, encrypted_len);
        memset(secretkey, 0, 32);
        memset(publickey, 0, 32);
        memset(data, 0, data_len);
        free(encrypted);
    }

    if (ddecrypt->count == 1) {
        if (base64->count == 1) {
            printf("Invalid options.\n");
            printf("Try '%s --help' for more information.\n", progname);
            exitcode = 1;
            goto exit;
        }
        if (symmetric->count == 1) {
            if (check_get_symmetrickeys(data, data_len, secretkey, publickey) !=
                0) {
                exitcode = 1;
                goto exit;
            }
        } else {
            if (file_exist(seckey->filename[0]) == 0) {
                printf("Secret key file '%s' not found.\n",
                       seckey->filename[0]);
                exitcode = 1;
                goto exit;
            }
            if (get_seckey(seckey->filename[0], secretkey, NULL) == 1) {
                printf("File '%s': invalid key.\n", seckey->filename[0]);
                exitcode = 1;
                goto exit;
            }
            if (file_exist(pubkey->filename[0]) == 0) {
                printf("Public key file '%s' not found.\n",
                       pubkey->filename[0]);
                exitcode = 1;
                goto exit;
            }
            if (get_pubkey(pubkey->filename[0], publickey) == 1) {
                printf("File '%s': invalid key.\n", pubkey->filename[0]);
                exitcode = 1;
                goto exit;
            }
        }

        size_t decrypted_len = 0;
        unsigned char *decrypted =
            decrypt_data(secretkey, publickey, data, data_len,
                         (symmetric->count == 1), &decrypted_len);

        if (decrypted == NULL || decrypted_len == 0) {
            printf("Decryption failed.\n");
            exitcode = 1;
            goto exit;
        }
        fwrite(decrypted, 1, decrypted_len, fo);
        fflush(fo);
        memset(decrypted, 0, decrypted_len);
        memset(secretkey, 0, 32);
        memset(publickey, 0, 32);
        memset(salt, 0, 32);
        memset(data, 0, data_len);
        free(decrypted);
        free(secretkey);
        free(salt);
        free(publickey);
    }

exit:
    if (fo != NULL) fclose(fo);

    /* deallocate each non-null entry in argtable[] */
    arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
    return exitcode;
}

int file_exist(const char *filename) {
    int exist = 0;
    FILE *file;
    if ((file = fopen(filename, "r")) == NULL) {
        if (errno != ENOENT) {
            printf("Some other error occured");
        }
    } else {
        exist = 1;
        fclose(file);
    }
    return exist;
}