tfcrypt: fixing hash output stream.
[tfcrypt.git] / tfc_random.c
1 /*
2  * tfcrypt -- high security Threefish encryption tool.
3  *
4  * tfcrypt is copyrighted:
5  * Copyright (C) 2012-2018 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 get_urandom(const char *src, void *buf, size_t size)
32 {
33         tfc_byte *ubuf = buf;
34         int fd = -1;
35         size_t sz = size, rd;
36
37         if (src == NULL) fd = -1;
38         else fd = open(src, O_RDONLY);
39
40         if (fd == -1) fd = open("/dev/urandom", O_RDONLY);
41         if (fd == -1) fd = open("/dev/arandom", O_RDONLY);
42         if (fd == -1) fd = open("/dev/prandom", O_RDONLY);
43         if (fd == -1) fd = open("/dev/srandom", O_RDONLY);
44         if (fd == -1) fd = open("/dev/random", O_RDONLY);
45         if (fd == -1) xerror(NO, YES, YES, "random source is required (tried %s)", src);
46
47 _again: rd = read(fd, ubuf, sz);
48         if (rd < sz && rd != NOSIZE) {
49                 ubuf += rd;
50                 sz -= rd;
51                 goto _again;
52         }
53
54         xclose(fd);
55 }
56
57 static tfc_yesno tfc_random_initialised;
58
59 static void tfc_initrandom(const char *rndsrc)
60 {
61         tfc_byte k[TF_KEY_SIZE];
62
63         if (tfc_random_initialised == YES) return;
64
65         get_urandom(rndsrc, k, TF_KEY_SIZE);
66         tf_prng_seedkey(k);
67         memset(k, 0, TF_KEY_SIZE);
68
69         tfc_random_initialised = YES;
70 }
71
72 void tfc_finirandom(void)
73 {
74         tf_prng_seedkey(NULL);
75         tfc_random_initialised = NO;
76 }
77
78 void tfc_getrandom(void *buf, size_t sz)
79 {
80         if (tfc_random_initialised == NO) tfc_initrandom(randsource);
81         tf_prng_genrandom(buf, sz);
82 }
83
84 void gen_write_bytes(const char *foutname, tfc_fsize offset, tfc_fsize nrbytes)
85 {
86         int fd, x;
87         size_t lblock;
88
89         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
90         memset(&sigact, 0, sizeof(sigact));
91         sigact.sa_flags = SA_RESTART;
92         sigact.sa_handler = print_crypt_status;
93         sigaction(SIGUSR1, &sigact, NULL);
94         sigaction(SIGTSTP, &sigact, NULL);
95         sigaction(SIGALRM, &sigact, NULL);
96         if (status_timer) setup_next_alarm(status_timer);
97         sigact.sa_handler = change_status_width;
98         sigaction(SIGQUIT, &sigact, NULL);
99         sigact.sa_handler = change_status_timer;
100         sigaction(SIGUSR2, &sigact, NULL);
101         if (quiet == NO) {
102                 sigact.sa_handler = print_crypt_status;
103                 sigaction(SIGINT, &sigact, NULL);
104                 sigaction(SIGTERM, &sigact, NULL);
105         }
106         else {
107                 sigact.sa_handler = exit_sigterm;
108                 sigaction(SIGINT, &sigact, NULL);
109                 sigaction(SIGTERM, &sigact, NULL);
110         }
111         memset(&sigact, 0, sizeof(struct sigaction));
112
113         tfc_getcurtime(&delta_time);
114
115         if (do_less_stats) do_less_stats = NO;
116         else do_less_stats = YES;
117
118         if (!foutname) {
119                 fd = 1;
120                 foutname = TFC_STDOUT_NAME;
121         }
122         else if (!strcmp(foutname, "-")) {
123                 fd = 1;
124                 foutname = TFC_STDOUT_NAME;
125         }
126         else fd = open(foutname, O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
127         if (fd == -1) xerror(NO, NO, YES, "%s", foutname);
128
129         if (offset) {
130                 if (lseek(fd, offset, SEEK_SET) == -1)
131                         xerror(YES, NO, NO, "%s: seek failed", foutname);
132         }
133
134         if (ctr_mode == TFC_MODE_PLAIN) memset(srcblk, 0, sizeof(srcblk));
135
136         if (verbose) tfc_nfsay(stderr, "%s: writing %lld bytes to %s ... ",
137                 progname, nrbytes, foutname);
138
139         errno = 0;
140         do_stop = NO;
141         while (1) {
142                 if (do_stop) break;
143                 lblock = blk_len_adj(nrbytes, total_processed_src, sizeof(srcblk));
144
145                 if (ctr_mode != TFC_MODE_PLAIN) tfc_getrandom(srcblk, lblock);
146
147                 if (write(fd, srcblk, lblock) == -1)
148                         xerror(NO, NO, YES, "%s", foutname);
149                 if (do_fsync && fsync(fd) == -1) xerror(NO, NO, YES, "%s", foutname);
150
151                 total_processed_src += lblock;
152                 delta_processed += lblock;
153                 total_processed_dst = total_processed_src;
154                 if (total_processed_src >= nrbytes) break;
155         }
156
157         if (verbose) tfc_esay("done!");
158         if (verbose || status_timer) print_crypt_status(0);
159
160         xclose(fd);
161         xexit(0);
162 }