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 static void print_crypt_status_genrnd(int signal)
33 if (signal == SIGTERM || signal == SIGINT) {
34 if (xexit_no_nl == YES) xexit_no_nl = NO;
36 print_crypt_status(signal);
39 static void exit_sigterm_genrnd(int signal)
41 if (xexit_no_nl == YES) xexit_no_nl = NO;
45 static void get_urandom(const char *src, void *buf, size_t size)
51 if (src == NULL) fd = -1;
52 else fd = open(src, O_RDONLY);
54 if (fd == -1) fd = open("/dev/urandom", O_RDONLY);
55 if (fd == -1) fd = open("/dev/arandom", O_RDONLY);
56 if (fd == -1) fd = open("/dev/prandom", O_RDONLY);
57 if (fd == -1) fd = open("/dev/srandom", O_RDONLY);
58 if (fd == -1) fd = open("/dev/random", O_RDONLY);
59 if (fd == -1) xerror(NO, YES, YES, "random source is required (tried %s)", src);
61 _again: rd = xread(fd, ubuf, sz);
62 if (rd < sz && rd != NOSIZE) {
71 static tfc_yesno tfc_random_initialised;
73 static void tfc_initrandom(const char *rndsrc)
75 tfc_byte k[TF_KEY_SIZE];
77 if (tfc_random_initialised == YES) return;
79 get_urandom(rndsrc, k, TF_KEY_SIZE);
81 memset(k, 0, TF_KEY_SIZE);
83 tfc_random_initialised = YES;
86 void tfc_finirandom(void)
88 tf_prng_seedkey(NULL);
89 tfc_random_initialised = NO;
92 void tfc_getrandom(void *buf, size_t sz)
94 if (tfc_random_initialised == NO) tfc_initrandom(randsource);
95 tf_prng_genrandom(buf, sz);
98 void gen_write_bytes(const char *foutname, tfc_fsize offset, tfc_fsize nrbytes)
100 static tfc_fsize wrpos = NOFSIZE;
102 size_t lblock, lio, lrem;
107 for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
108 memset(&sigact, 0, sizeof(sigact));
109 sigact.sa_flags = SA_RESTART;
110 sigact.sa_handler = print_crypt_status;
111 sigaction(SIGUSR1, &sigact, NULL);
112 sigaction(SIGALRM, &sigact, NULL);
113 if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
114 sigact.sa_handler = change_status_width;
115 sigaction(SIGQUIT, &sigact, NULL);
116 sigact.sa_handler = change_status_timer;
117 sigaction(SIGUSR2, &sigact, NULL);
119 sigact.sa_handler = print_crypt_status_genrnd;
120 sigaction(SIGINT, &sigact, NULL);
121 sigaction(SIGTERM, &sigact, NULL);
122 sigaction(SIGTSTP, &sigact, NULL);
125 sigact.sa_handler = exit_sigterm_genrnd;
126 sigaction(SIGINT, &sigact, NULL);
127 sigaction(SIGTERM, &sigact, NULL);
128 sigact.sa_handler = handle_sigtstp;
129 sigaction(SIGTSTP, &sigact, NULL);
131 memset(&sigact, 0, sizeof(struct sigaction));
133 tfc_getcurtime(&delta_time);
135 if (do_less_stats) do_less_stats = NO;
136 else do_less_stats = YES;
140 foutname = TFC_STDOUT_NAME;
142 else if (!strcmp(foutname, "-")) {
144 foutname = TFC_STDOUT_NAME;
146 else fd = xopen(foutname, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
149 if (lseek(fd, offset, SEEK_SET) == -1)
150 xerror(ignore_seek_errors, NO, NO, "%s: seek failed", foutname);
153 if (ctr_mode == TFC_MODE_PLAIN) memset(srcblk, 0, sizeof(srcblk));
155 if (verbose) tfc_nfsay(stderr, "%s: writing %lld bytes to %s ... ",
156 tfc_format_pid(progname), nrbytes, foutname);
163 lblock = lrem = blk_len_adj(nrbytes, total_processed_src, blksize);
165 if (ctr_mode != TFC_MODE_PLAIN) tfc_getrandom(srcblk, lblock);
167 if (error_action == TFC_ERRACT_SYNC) wrpos = tfc_fdgetpos(fd);
168 _wagain: lio = xwrite(fd, pblk, lrem);
170 if (errno != EIO && catch_all_errors != YES)
171 xerror(NO, NO, YES, "%s", foutname);
172 switch (error_action) {
173 case TFC_ERRACT_CONT: xerror(YES, NO, YES, "%s", foutname); goto _wagain; break;
174 case TFC_ERRACT_SYNC:
175 case TFC_ERRACT_LSYNC:
176 xerror(YES, NO, YES, "%s", foutname);
177 if (wrpos == NOFSIZE) lseek(fd, lblock, SEEK_CUR);
178 else lseek(fd, wrpos + lblock, SEEK_SET);
180 default: xerror(NO, NO, YES, "%s", foutname); break;
183 if (do_fsync && fsync(fd) == -1) xerror(NO, NO, YES, "%s", foutname);
190 total_processed_src += lblock;
191 delta_processed += lblock;
192 total_processed_dst = total_processed_src;
193 if (total_processed_src >= nrbytes) break;
196 if (verbose) tfc_esay("done!");
197 if (verbose || status_timer) {
198 print_crypt_status(0);