2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
7 * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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.
31 size_t blk_len_adj(tfc_fsize filelen, tfc_fsize read_already, size_t blklen)
33 if (filelen == NOFSIZE) return blklen;
34 return ((filelen - read_already) >= blklen) ? blklen : (filelen - read_already);
37 tfc_yesno xor_shrink(void *dst, size_t szdst, const void *src, size_t szsrc)
39 unsigned char *udst = dst;
40 const unsigned char *usrc = src;
43 if ((szsrc % szdst) != 0) return NO;
46 memmove(dst, src, szsrc);
52 memset(dst, 0, szdst);
53 for (x = 0; x < (szsrc / szdst); x++) {
54 for (y = 0; y < szdst; y++) udst[y] ^= usrc[(x*szdst)+y];
60 tfc_yesno str_empty(const char *str)
62 if (!*str) return YES;
66 int xxopen(tfc_yesno noerr, const char *pathname, int flags)
70 if ((flags & O_WRONLY || flags & O_RDWR)) {
71 if (read_only == YES) flags = O_RDONLY;
72 else flags |= write_flags;
76 r = open(pathname, flags, 0666);
77 if (noerr == NO && r == -1) xerror(NO, NO, YES, "%s", pathname);
81 int xopen(const char *pathname, int flags)
83 return xxopen(NO, pathname, flags);
89 if (close(fd) == -1) xerror(YES, NO, NO, "close(%d)", fd);
92 const char *tfc_modename(int mode)
95 case TFC_MODE_CTR: return "CTR";
96 case TFC_MODE_STREAM: return "STREAM";
97 case TFC_MODE_XTS: return "XTS";
98 case TFC_MODE_ECB: return "ECB";
99 case TFC_MODE_CBC: return "CBC";
100 case TFC_MODE_OCB: return "OCB";
106 void tfc_getcurtime(tfc_useconds *tx)
109 memset(&t, 0, sizeof(t));
111 clock_gettime(CLOCK_MONOTONIC, &t);
112 *tx = (tfc_useconds)t.tv_sec * 1000000 + (t.tv_nsec / 1000);
114 memset(&t, 0, sizeof(t));
117 char *tfc_format_time(tfc_useconds t)
119 tfc_useconds secs, dsecs;
120 unsigned days, hours, minutes, seconds;
123 secs = (tfc_useconds)TFC_UTODSECS(t);
124 dsecs = (tfc_useconds)(t - (secs * 1000000));
127 hours = (secs / 3600) % 24;
128 minutes = (secs / 60) % 60;
131 if (days > 0) sprintf(r, "%ud,%02u:%02u:%02u.%04u", days, hours, minutes, seconds, (unsigned)(dsecs / 100));
132 else if (hours > 0) sprintf(r, "%02u:%02u:%02u.%04u", hours, minutes, seconds, (unsigned)(dsecs / 100));
133 else if (minutes > 0) sprintf(r, "%02u:%02u.%04u", minutes, seconds, (unsigned)(dsecs / 100));
134 else sprintf(r, "%02u.%04u", seconds, (unsigned)(dsecs / 100));
139 char *tfc_format_pid(const char *str)
144 n = xstrlcpy(r, str, sizeof(r));
145 if (show_pid == YES && sizeof(r)-n >= 22) sprintf(r+n, "[%lu]", (unsigned long)progpid);
150 tfc_fsize tfc_fdsize(int fd)
154 cur = lseek(fd, 0L, SEEK_CUR);
155 l = lseek(fd, 0L, SEEK_SET);
156 if (l == -1) return NOFSIZE;
157 l = lseek(fd, 0L, SEEK_END);
158 if (l == -1) return NOFSIZE;
159 lseek(fd, cur, SEEK_SET);
164 tfc_fsize tfc_fdgetpos(int fd)
168 t = lseek(fd, 0L, SEEK_CUR);
169 if (t == (off_t)-1) return NOFSIZE;
173 tfc_fsize tfc_fnamesize(char *fname, tfc_yesno noexit)
179 if (!fname) return 0;
181 s = strchr(fname, ':');
182 if (s && s[1] && (s[1] == '+' || s[1] == '-' || s[1] == '*' || s[1] == '/')) {
187 fnmfd = xxopen(YES, fname, O_RDONLY);
188 if (s) memcpy(s, T, 2);
190 xerror(noexit, NO, YES, "%s", fname);
193 ret = tfc_fdsize(fnmfd);
194 if (ret == NOFSIZE) {
195 xerror(noexit, NO, YES, "%s: not a seekable file", fname);
203 tfc_fsize tfc_modifysize(tfc_fsize szmodify, const char *szspec)
209 if (szmodify == NOFSIZE) return NOFSIZE;
210 if (!szspec) return szmodify;
213 if (*s != ':') return szmodify;
215 if (!(*s == '+' || *s == '-' || *s == '*' || *s == '/')) return szmodify;
218 if (strchr(s, '/') || strchr(s, '.')) return szmodify;
220 t = tfc_humanfsize(s, &stoi);
221 if (!str_empty(stoi)) return szmodify;
224 case '+': szmodify += t; break;
225 case '-': szmodify -= t; break;
226 case '*': szmodify *= t; break;
227 case '/': szmodify /= t; break;
234 void fcopy_matime(int fd, const struct stat *st)
236 struct timeval times[2];
238 times[1].tv_sec = times[0].tv_sec = st->st_mtime;
239 times[1].tv_usec = times[0].tv_usec = 0;
240 if (futimes(fd, times) == -1) xerror(YES, NO, YES, "futimes(%d)", fd);
243 static void char_to_nul(char *s, size_t l, int c)
245 while (*s && l) { if (*s == c) { *s = 0; break; } s++; l--; }
248 tfc_yesno xfgets(char *s, size_t n, FILE *f)
252 if (fgets(s, (int)n, f) == s) {
253 char_to_nul(s, n, '\n');
260 tfc_yesno isbase64(const char *s)
263 if (*s >= 'g' && *s <= 'z') return YES;
264 if (*s >= 'G' && *s <= 'Z') return YES;
265 if (*s == '+' || *s == '/' || *s == '=') return YES;
271 static int chrbin(char x)
273 if (x >= '0' && x <= '9')
275 if (x >= 'A' && x <= 'F')
277 if (x >= 'a' && x <= 'f')
282 void hex2bin(void *d, const char *s)
290 x = (x << 4) ^ chrbin(*s);