51 broke -E logic completely, rewise it
[tfcrypt.git] / tfc_random.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 static void print_crypt_status_genrnd(int signal)
32 {
33         print_crypt_status(signal);
34 }
35
36 static void exit_sigterm_genrnd(int signal)
37 {
38         exit_sigterm(signal);
39 }
40
41 static void get_urandom(const char *src, void *buf, size_t size)
42 {
43         tfc_byte *ubuf = buf;
44         int fd = -1;
45         size_t sz = size, rd;
46
47         if (src == NULL) fd = -1;
48         else fd = open(src, O_RDONLY);
49
50         if (fd == -1) fd = open("/dev/urandom", O_RDONLY);
51         if (fd == -1) fd = open("/dev/arandom", O_RDONLY);
52         if (fd == -1) fd = open("/dev/prandom", O_RDONLY);
53         if (fd == -1) fd = open("/dev/srandom", O_RDONLY);
54         if (fd == -1) fd = open("/dev/random", O_RDONLY);
55         if (fd == -1) xerror(NO, YES, YES, "random source is required (tried %s)", src);
56
57 _again: rd = xread(fd, ubuf, sz);
58         if (rd < sz && rd != NOSIZE) {
59                 ubuf += rd;
60                 sz -= rd;
61                 goto _again;
62         }
63
64         xclose(fd);
65 }
66
67 static tfc_yesno tfc_random_initialised;
68
69 static void tfc_initrandom(const char *rndsrc)
70 {
71         tfc_byte k[TF_KEY_SIZE];
72
73         if (tfc_random_initialised == YES) return;
74
75         get_urandom(rndsrc, k, TF_KEY_SIZE);
76         tf_prng_seedkey(k);
77         memset(k, 0, TF_KEY_SIZE);
78
79         tfc_random_initialised = YES;
80 }
81
82 void tfc_finirandom(void)
83 {
84         tf_prng_seedkey(NULL);
85         tfc_random_initialised = NO;
86 }
87
88 void tfc_getrandom(void *buf, size_t sz)
89 {
90         if (tfc_random_initialised == NO) tfc_initrandom(randsource);
91         tf_prng_genrandom(buf, sz);
92 }
93
94 void gen_write_bytes(const char *foutname, tfc_fsize offset, tfc_fsize nrbytes)
95 {
96         static tfc_fsize wrpos = NOFSIZE;
97         int fd, x;
98         size_t lblock, lio, lrem;
99         tfc_byte *pblk;
100
101         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
102         memset(&sigact, 0, sizeof(sigact));
103         sigact.sa_flags = SA_RESTART;
104         sigact.sa_handler = print_crypt_status;
105         sigaction(SIGUSR1, &sigact, NULL);
106         sigaction(SIGALRM, &sigact, NULL);
107         if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
108         sigact.sa_handler = change_status_width;
109         sigaction(SIGQUIT, &sigact, NULL);
110         sigact.sa_handler = change_status_timer;
111         sigaction(SIGUSR2, &sigact, NULL);
112         if (quiet == NO) {
113                 sigact.sa_handler = print_crypt_status_genrnd;
114                 sigaction(SIGINT, &sigact, NULL);
115                 sigaction(SIGTERM, &sigact, NULL);
116                 sigaction(SIGTSTP, &sigact, NULL);
117         }
118         else {
119                 sigact.sa_handler = exit_sigterm_genrnd;
120                 sigaction(SIGINT, &sigact, NULL);
121                 sigaction(SIGTERM, &sigact, NULL);
122                 sigact.sa_handler = handle_sigtstp;
123                 sigaction(SIGTSTP, &sigact, NULL);
124         }
125         memset(&sigact, 0, sizeof(struct sigaction));
126
127         tfc_getcurtime(&delta_time);
128
129         if (do_less_stats) do_less_stats = NO;
130         else do_less_stats = YES;
131
132         if (!foutname) {
133                 fd = 1;
134                 foutname = TFC_STDOUT_NAME;
135         }
136         else if (!strcmp(foutname, "-")) {
137                 fd = 1;
138                 foutname = TFC_STDOUT_NAME;
139         }
140         else fd = xopen(foutname, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
141
142         if (offset) {
143                 if (lseek(fd, offset, SEEK_SET) == -1)
144                         xerror(ignore_seek_errors, NO, NO, "%s: seek failed", foutname);
145         }
146
147         if (do_edcrypt == TFC_DO_PLAIN) memset(srcblk, 0, sizeof(srcblk));
148
149         if (verbose) tfc_nfsay(stderr, "%s: writing %lld bytes to %s ... ",
150                 tfc_format_pid(progname), nrbytes, foutname);
151
152         errno = 0;
153         do_stop = NO;
154         while (1) {
155                 if (do_stop) break;
156                 pblk = srcblk;
157                 lblock = lrem = blk_len_adj(nrbytes, total_processed_src, blksize);
158
159                 if (do_edcrypt != TFC_DO_PLAIN) tfc_getrandom(srcblk, lblock);
160
161                 if (error_action == TFC_ERRACT_SYNC) wrpos = tfc_fdgetpos(fd);
162 _wagain:        lio = xwrite(fd, pblk, lrem);
163                 if (lio == NOSIZE) {
164                         if (errno != EIO && catch_all_errors != YES)
165                                 xerror(NO, NO, YES, "%s", foutname);
166                         switch (error_action) {
167                                 case TFC_ERRACT_CONT: xerror(YES, NO, YES, "%s", foutname); goto _wagain; break;
168                                 case TFC_ERRACT_SYNC:
169                                 case TFC_ERRACT_LSYNC:
170                                         xerror(YES, NO, YES, "%s", foutname);
171                                         if (wrpos == NOFSIZE) lseek(fd, lblock, SEEK_CUR);
172                                         else lseek(fd, wrpos + lblock, SEEK_SET);
173                                         break;
174                                 default: xerror(NO, NO, YES, "%s", foutname); break;
175                         }
176                 }
177                 if (do_fsync && fsync(fd) == -1) xerror(NO, NO, YES, "%s", foutname);
178                 if (lio < lrem) {
179                         pblk += lio;
180                         lrem -= lio;
181                         goto _wagain;
182                 }
183
184                 total_processed_src += lblock;
185                 delta_processed += lblock;
186                 total_processed_dst = total_processed_src;
187                 if (total_processed_src >= nrbytes) break;
188         }
189
190         if (verbose) tfc_esay("done!");
191         if (verbose || status_timer) {
192                 print_crypt_status(TFC_SIGSTAT);
193                 tfc_esay("\n");
194         }
195
196         xclose(fd);
197         xexit(0);
198 }