51 broke -E logic completely, rewise it
[tfcrypt.git] / tfc_skein.c
index b3b2019d5875d446d3704ea854f8511c72f0ce34..a3685783d28333bf58aa5de9b1c32007e8081e9f 100644 (file)
@@ -2,7 +2,7 @@
  * tfcrypt -- high security Threefish encryption tool.
  *
  * tfcrypt is copyrighted:
- * Copyright (C) 2012-2018 Andrey Rys. All rights reserved.
+ * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
  *
  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
  *
 #include "tfcrypt.h"
 #include "tfcore.h"
 
+static void exit_sigterm_skein(int signal)
+{
+       exit_sigterm(signal);
+}
+
 void skein(void *hash, size_t bits, const void *key, const void *data, size_t szdata)
 {
        struct skein sk;
@@ -39,17 +44,7 @@ void skein(void *hash, size_t bits, const void *key, const void *data, size_t sz
        skein_final(hash, &sk);
 }
 
-void tf_key_tweak_compat(void *key)
-{
-       TF_UNIT_TYPE *ukey = key, c = THREEFISH_CONST;
-       size_t x;
-
-       for (x = 0; x < TF_NR_BLOCK_UNITS; x++) c ^= ukey[x];
-       ukey[x] = c;
-       ukey[TF_TWEAK_WORD3] = ukey[TF_TWEAK_WORD1] ^ ukey[TF_TWEAK_WORD2];
-}
-
-tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize readto)
+tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize offset, tfc_fsize readto)
 {
        static tfc_byte skblk[TFC_BLKSIZE];
 
@@ -57,29 +52,35 @@ tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize re
        tfc_byte *pblk;
        size_t ldone, lblock, lrem, lio;
        tfc_fsize total = 0;
-       tfc_yesno stop;
 
        if (ctr_mode == TFC_MODE_SKSUM) total_processed_src = total_processed_dst = delta_processed = 0;
 
        if (fd == -1) goto _fail;
-       if (fd > 2 && readto == NOFSIZE) {
-               readto = tfc_fdsize(fd);
-               if (readto == NOFSIZE) goto _fail;
+       if (fd > 2) {
+               if (readto == NOFSIZE) {
+                       readto = tfc_fdsize(fd);
+                       if (readto == NOFSIZE) goto _fail;
+               }
+               if (offset != 0 && offset != NOFSIZE) {
+                       if (lseek(fd, (off_t)offset, SEEK_SET) == -1) {
+                               if (ignore_seek_errors == NO) goto _fail;
+                       }
+               }
        }
 
        if (key) skein_init_key(&sk, key, bits);
        else skein_init(&sk, bits);
 
        errno = 0;
-       stop = NO;
+       do_stop = NO;
        while (1) {
-               if (stop) break;
+               if (do_stop) break;
                pblk = skblk;
                lblock = lrem = blk_len_adj(readto, total, TFC_BLKSIZE);
                ldone = 0;
                if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(fd);
 _again:                lio = xread(fd, pblk, lrem);
-               if (lio == 0) stop = YES;
+               if (lio == 0) do_stop = YES;
                if (lio != NOSIZE) ldone += lio;
                else {
                        if (errno != EIO && catch_all_errors != YES) goto _fail;
@@ -115,7 +116,10 @@ _again:            lio = xread(fd, pblk, lrem);
 
        skein_final(hash, &sk);
        if (ctr_mode == TFC_MODE_SKSUM) {
-               if (verbose || status_timer) print_crypt_status(-1);
+               if (verbose || status_timer) {
+                       print_crypt_status(TFC_SIGSTAT);
+                       tfc_esay("\n");
+               }
                total_processed_src = total_processed_dst = delta_processed = 0;
        }
        memset(skblk, 0, TFC_BLKSIZE);
@@ -173,15 +177,16 @@ _dothat:
        sigact.sa_flags = SA_RESTART;
        sigact.sa_handler = print_crypt_status;
        sigaction(SIGUSR1, &sigact, NULL);
-       sigaction(SIGTSTP, &sigact, NULL);
        sigaction(SIGALRM, &sigact, NULL);
        sigact.sa_handler = change_status_width;
        sigaction(SIGQUIT, &sigact, NULL);
        sigact.sa_handler = change_status_timer;
        sigaction(SIGUSR2, &sigact, NULL);
-       sigact.sa_handler = exit_sigterm;
+       sigact.sa_handler = exit_sigterm_skein;
        sigaction(SIGINT, &sigact, NULL);
        sigaction(SIGTERM, &sigact, NULL);
+       sigact.sa_handler = handle_sigtstp;
+       sigaction(SIGTSTP, &sigact, NULL);
        memset(&sigact, 0, sizeof(struct sigaction));
 
        tfc_getcurtime(&delta_time);
@@ -197,8 +202,7 @@ _dothat:
 
                while (1) {
                        memset(sksblk, 0, sizeof(sksblk));
-                       x = xfgets(sksblk, sizeof(sksblk), f);
-                       if (x == 0) break;
+                       if (xfgets(sksblk, sizeof(sksblk), f) != YES) break;
 
                        s = d = sksblk; t = NULL;
                        shash = fname = NULL;
@@ -218,15 +222,15 @@ _dothat:
                        s = strchr(shash, ' ');
                        if (s && s[1] == ' ') *s = 0;
 
-                       fd = open(fname, O_RDONLY | O_LARGEFILE);
+                       fd = xxopen(YES, fname, O_RDONLY | O_LARGEFILE);
                        if (fd == -1) {
                                xerror(YES, NO, YES, "%s", fname);
                                exitcode = 1;
                                continue;
                        }
 
-                       if (status_timer) setup_next_alarm(status_timer);
-                       if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, maxlen) != YES) {
+                       if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
+                       if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, iseek, maxlen) != YES) {
                                xerror(YES, NO, YES, "%s", fname);
                                exitcode = 1;
                                continue;
@@ -255,7 +259,7 @@ _dothat:
                fclose(f);
                if (failed) {
                        tfc_esay("%s: WARNING: %u of %u computed checksums did NOT match",
-                               progname, failed, totaltested);
+                               tfc_format_pid(progname), failed, totaltested);
                        exitcode = 1;
                }
                xexit(exitcode);
@@ -270,15 +274,15 @@ _dothat:
 
        for (x = 0; fargv[x] && xx; x++) {
                if (!strcmp(fargv[x], "-")) fd = 0;
-               else fd = open(fargv[x], O_RDONLY | O_LARGEFILE);
+               else fd = xxopen(YES, fargv[x], O_RDONLY | O_LARGEFILE);
                if (fd == -1) {
                        xerror(YES, NO, YES, "%s", fargv[x]);
                        exitcode = 1;
                        continue;
                }
 
-_dohash:       if (status_timer) setup_next_alarm(status_timer);
-               if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, maxlen) != YES) {
+_dohash:       if (status_timer) setup_next_alarm(status_timer > 1000000 ? 1000000 : status_timer);
+               if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, iseek, maxlen) != YES) {
                        xerror(YES, NO, YES, "%s", fargv[x]);
                        exitcode = 1;
                        continue;