51 broke -E logic completely, rewise it
[tfcrypt.git] / tfc_conf.c
1 /*
2  * tfcrypt -- high security Threefish encryption tool.
3  *
4  * tfcrypt is copyrighted:
5  * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
6  *
7  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include "tfcrypt.h"
30
31 void read_defaults(const char *path, tfc_yesno noerr)
32 {
33         static char ln[4096];
34         char *s, *d, *t, *stoi;
35         FILE *f;
36         tfc_yesno valid = NO;
37
38         f = fopen(path, "r");
39         if (!f) {
40                 if (noerr == YES) return;
41                 xerror(NO, NO, YES, "%s", path);
42         }
43
44         while (1) {
45                 memset(ln, 0, sizeof(ln));
46                 if (xfgets(ln, sizeof(ln), f) != YES) break;
47
48                 if (valid == NO) {
49                         if (!strcmp(ln, "# tfcrypt.defs")) valid = YES;
50                         continue;
51                 }
52
53                 if (str_empty(ln) || ln[0] == '#') continue;
54
55                 s = ln;
56                 d = strchr(s, '=');
57                 if (!d) continue;
58                 *d = 0; d++;
59
60                 /* yay! GOTO hell! You'll "like" it! */
61 _spc1:          t = strchr(s, ' ');
62                 if (!t) goto _spc2;
63                 *t = 0; goto _spc1;
64 _spc2:          t = strchr(d, ' ');
65                 if (!t) goto _nspc;
66                 *t = 0; d = t+1; goto _spc2;
67 _nspc:
68                 if (!strcmp(s, "nr_turns")) {
69                         nr_turns = strtoul(d, &stoi, 10);
70                         if (!str_empty(stoi)) xerror(NO, YES, YES, "[%s] nr_turns=%s: invalid number of turns", path, d);
71                 }
72                 else if (!strcmp(s, "ctr_mode")) {
73                         if (!strcasecmp(d, "ctr"))
74                                 ctr_mode = TFC_MODE_CTR;
75                         else if (!strcasecmp(d, "stream"))
76                                 ctr_mode = TFC_MODE_STREAM;
77                         else if (!strcasecmp(d, "cbc"))
78                                 ctr_mode = TFC_MODE_CBC;
79                         else if (!strcasecmp(d, "pcbc"))
80                                 ctr_mode = TFC_MODE_PCBC;
81                         else if (!strcasecmp(d, "ecb"))
82                                 ctr_mode = TFC_MODE_ECB;
83                         else if (!strcasecmp(d, "xts"))
84                                 ctr_mode = TFC_MODE_XTS;
85                         else xerror(NO, YES, YES, "[%s] ctr_mode=%s: invalid mode of operation", path, d);
86                 }
87                 else if (!strcmp(s, "tfc_salt")) {
88                         memset(tfc_salt, 0, TFC_MAX_SALT);
89                         tfc_saltsz = base64_decode((char *)tfc_salt, TFC_MAX_SALT, d, strlen(d));
90                 }
91                 else if (!strcmp(s, "macbits")) {
92                         macbits = strtoul(d, &stoi, 10);
93                         if (macbits == 0 || !str_empty(stoi) || macbits < 8
94                         || macbits > TF_MAX_BITS || macbits % 8)
95                                 xerror(NO, YES, YES, "[%s] macbits=%s: invalid MAC bits setting", path, d);
96                 }
97                 else if (!strcmp(s, "do_full_key")) {
98                         if (!strcasecmp(d, "yes")) do_full_key = YES;
99                         else if (!strcasecmp(d, "no")) do_full_key = NO;
100                 }
101                 else xerror(NO, YES, YES, "[%s] %s: unknown keyword", path, s);
102         }
103
104         memset(ln, 0, sizeof(ln));
105         fclose(f);
106 }
107
108 void hash_defaults(char *uhash, size_t szuhash)
109 {
110         struct skein sk;
111         char shash[56];
112         const char *mode;
113         tfc_byte hash[TF_FROM_BITS(256)];
114
115         skein_init(&sk, 256);
116
117         skein_update(&sk, tfc_salt, tfc_saltsz);
118
119         memset(shash, 0, sizeof(shash));
120         sprintf(shash, "%zu", nr_turns);
121         skein_update(&sk, shash, strlen(shash));
122
123         mode = tfc_modename(ctr_mode);
124         skein_update(&sk, mode, strlen(mode));
125
126         memset(shash, 0, sizeof(shash));
127         sprintf(shash, "%zu", macbits);
128         skein_update(&sk, shash, strlen(shash));
129
130         skein_update(&sk, do_full_key ? "1" : "0", 1);
131
132         skein_final(hash, &sk);
133         memset(shash, 0, sizeof(shash));
134         base64_encode(shash, (const char *)hash, sizeof(hash));
135         memset(hash, 0, sizeof(hash));
136
137         xstrlcpy(uhash, shash, szuhash);
138 }