tfstream: add separate xor_block function
[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, "ecb"))
80                                 ctr_mode = TFC_MODE_ECB;
81                         else if (!strcasecmp(d, "xts"))
82                                 ctr_mode = TFC_MODE_XTS;
83                         else xerror(NO, YES, YES, "[%s] ctr_mode=%s: invalid mode of operation", path, d);
84                 }
85                 else if (!strcmp(s, "tfc_salt")) {
86                         memset(tfc_salt, 0, TFC_MAX_SALT);
87                         tfc_saltsz = base64_decode((char *)tfc_salt, TFC_MAX_SALT, d, strlen(d));
88                 }
89                 else if (!strcmp(s, "macbits")) {
90                         macbits = strtoul(d, &stoi, 10);
91                         if (macbits == 0 || !str_empty(stoi) || macbits < 8
92                         || macbits > TF_MAX_BITS || macbits % 8)
93                                 xerror(NO, YES, YES, "[%s] macbits=%s: invalid MAC bits setting", path, d);
94                 }
95                 else if (!strcmp(s, "do_full_key")) {
96                         if (!strcasecmp(d, "yes")) do_full_key = YES;
97                         else if (!strcasecmp(d, "no")) do_full_key = NO;
98                 }
99                 else xerror(NO, YES, YES, "[%s] %s: unknown keyword", path, s);
100         }
101
102         memset(ln, 0, sizeof(ln));
103         fclose(f);
104 }
105
106 void hash_defaults(char *uhash, size_t szuhash)
107 {
108         struct skein sk;
109         char shash[56];
110         const char *mode;
111         tfc_byte hash[TF_FROM_BITS(256)];
112
113         skein_init(&sk, 256);
114
115         skein_update(&sk, tfc_salt, tfc_saltsz);
116
117         memset(shash, 0, sizeof(shash));
118         sprintf(shash, "%zu", nr_turns);
119         skein_update(&sk, shash, strlen(shash));
120
121         mode = tfc_modename(ctr_mode);
122         skein_update(&sk, mode, strlen(mode));
123
124         memset(shash, 0, sizeof(shash));
125         sprintf(shash, "%zu", macbits);
126         skein_update(&sk, shash, strlen(shash));
127
128         skein_update(&sk, do_full_key ? "1" : "0", 1);
129
130         skein_final(hash, &sk);
131         memset(shash, 0, sizeof(shash));
132         base64_encode(shash, (const char *)hash, sizeof(hash));
133         memset(hash, 0, sizeof(hash));
134
135         xstrlcpy(uhash, shash, szuhash);
136 }