tfprng: expose range formula in form of macros to build custom range generators.
[tfcrypt.git] / tfcrypt.c
index 3fdf9c65ad893d4b78513ccd6a615101dffeaa26..3534bb836c76b8ee3b9dbdca3215f728a9b2c731 100644 (file)
--- a/tfcrypt.c
+++ b/tfcrypt.c
@@ -51,6 +51,38 @@ static int getps_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
        return 0;
 }
 
+static inline int isctrlchr(int c)
+{
+       if (c == 9) return 0;
+       if (c >= 0 && c <= 31) return 1;
+       if (c == 127) return 1;
+       return 0;
+}
+
+static int getps_plain_filter(struct getpasswd_state *getps, char chr, size_t pos)
+{
+       int x;
+
+       x = getps_filter(getps, chr, pos);
+       if (x != 1) return x;
+
+       if (pos < getps->pwlen && !isctrlchr(chr))
+               write(getps->efd, &chr, sizeof(char));
+       return 1;
+}
+
+static int getps_plain_hex_filter(struct getpasswd_state *getps, char chr, size_t pos)
+{
+       int x;
+
+       x = getps_hex_filter(getps, chr, pos);
+       if (x != 1) return x;
+
+       if (pos < getps->pwlen && !isctrlchr(chr))
+               write(getps->efd, &chr, sizeof(char));
+       return 1;
+}
+
 int main(int argc, char **argv)
 {
        int c;
@@ -73,7 +105,7 @@ _baddfname:
        memset(s, 0, n);
 
        opterr = 0;
-       while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:TPkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
+       while ((c = getopt(argc, argv, "L:s:aU:C:r:K:t:Pkzxc:l:qedn:vV:pwE:O:S:AmM:R:Z:WHD:")) != -1) {
                switch (c) {
                        case 'L':
                                read_defaults(optarg, NO);
@@ -161,10 +193,6 @@ _baddfname:
                                tweakf = optarg;
                                do_full_key = NO;
                                break;
-                       case 'T':
-                               tfc_saltsz = 0;
-                               do_full_key = NO;
-                               break;
                        case 'l':
                                if (maxlen != NOFSIZE) break;
 
@@ -229,6 +257,10 @@ _baddfname:
                                                mac_pw_prompt = s+10;
                                        else if (!strcmp(s, "shorthex"))
                                                do_full_hexdump = NO;
+                                       else if (!strcmp(s, "fullkey"))
+                                               do_full_key = YES;
+                                       else if (!strcmp(s, "showsecrets"))
+                                               show_secrets = YES;
                                        else if (!strncmp(s, "iobs", 4) && *(s+4) == '=') {
                                                s += 5;
                                                blksize = (size_t)tfc_humanfsize(s, &stoi);
@@ -344,6 +376,17 @@ _baddfname:
                                                if (counter_opt == TFC_CTR_HEAD)
                                                        maxlen += TF_BLOCK_SIZE;
                                        }
+                                       else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
+                                               s += 7;
+                                               ftrunc_dfd = tfc_humanfsize(s, &stoi);
+                                               if (!str_empty(stoi)) {
+                                                       ftrunc_dfd = tfc_fnamesize(s, YES);
+                                                       ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
+                                                       if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
+                                                       "%s: invalid ftrunc value", s);
+                                               }
+                                               else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
+                                       }
                                        else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
                                                s += 5;
                                                maxkeylen = tfc_humanfsize(s, &stoi);
@@ -574,8 +617,8 @@ _mkragain:          lio = xread(mkfd, pblk, lrem);
                getps.passwd = pwdask;
                getps.pwlen = sizeof(pwdask)-1;
                getps.echo = mac_pw_prompt ? mac_pw_prompt : "Enter MAC password: ";
-               getps.charfilter = getps_filter;
-               getps.maskchar = 'x';
+               getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
+               getps.maskchar = (show_secrets == YES) ? 0 : 'x';
                getps.flags = GETP_WAITFILL;
                n = xgetpasswd(&getps);
                if (n == NOSIZE) xerror(NO, NO, YES, "getting MAC password");
@@ -769,8 +812,8 @@ _xts2keyaskstr:     memset(&getps, 0, sizeof(struct getpasswd_state));
                getps.passwd = (char *)pblk;
                getps.pwlen = n;
                getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (str): ";
-               getps.charfilter = getps_filter;
-               getps.maskchar = 'x';
+               getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
+               getps.maskchar = (show_secrets == YES) ? 0 : 'x';
                getps.flags = GETP_WAITFILL;
                n = xgetpasswd(&getps);
                if (n == NOSIZE) xerror(NO, NO, YES, "getting string rawkey");
@@ -793,8 +836,8 @@ _rawkey_hex_again:
                getps.passwd = pwdask;
                getps.pwlen = (TF_FROM_BITS(TFC_KEY_BITS)*2);
                getps.echo = pw_prompt ? pw_prompt : "Enter rawkey (hex): ";
-               getps.charfilter = getps_hex_filter;
-               getps.maskchar = 'x';
+               getps.charfilter = (show_secrets == YES) ? getps_plain_hex_filter : getps_hex_filter;
+               getps.maskchar = (show_secrets == YES) ? 0 : 'x';
                getps.flags = GETP_WAITFILL;
                n = xgetpasswd(&getps);
                if (n == NOSIZE) xerror(NO, NO, YES, "getting hex rawkey");
@@ -819,8 +862,8 @@ _pwdagain:  memset(&getps, 0, sizeof(struct getpasswd_state));
                getps.passwd = pwdask;
                getps.pwlen = sizeof(pwdask)-1;
                getps.echo = pw_prompt ? pw_prompt : "Enter password: ";
-               getps.charfilter = getps_filter;
-               getps.maskchar = 'x';
+               getps.charfilter = (show_secrets == YES) ? getps_plain_filter : getps_filter;
+               getps.maskchar = (show_secrets == YES) ? 0 : 'x';
                getps.flags = GETP_WAITFILL;
                n = xgetpasswd(&getps);
                if (n == NOSIZE) xerror(NO, NO, YES, "getting password");
@@ -918,20 +961,20 @@ _xts2genkey:      if (xwrite(krfd, pblk, TF_FROM_BITS(TFC_KEY_BITS)) == NOSIZE) xerro
                tf_tweak_set(key, tweak);
        }
        if (ctr_mode == TFC_MODE_ECB) goto _ctrskip2;
+
+       if (counter_opt == TFC_CTR_ZERO) memset(ctr, 0, ctrsz);
+
        tfc_data_to_words64(&iseek_blocks, sizeof(iseek_blocks));
        tf_ctr_set(ctr, &iseek_blocks, sizeof(iseek_blocks));
 
-       switch (counter_opt) {
-               case TFC_CTR_SHOW:
-                       switch (do_outfmt) {
-                               case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
-                               case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
-                               case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
-                       }
-                       break;
-               case TFC_CTR_RAND: tfc_getrandom(ctr, ctrsz); break;
-               case TFC_CTR_ZERO: memset(ctr, 0, ctrsz); break;
+       if (counter_opt == TFC_CTR_SHOW) {
+               switch (do_outfmt) {
+                       case TFC_OUTFMT_B64: tfc_printbase64(stderr, ctr, ctrsz, YES); break;
+                       case TFC_OUTFMT_RAW: xwrite(2, ctr, ctrsz); break;
+                       case TFC_OUTFMT_HEX: mehexdump(ctr, ctrsz, ctrsz, YES); break;
+               }
        }
+       else if (counter_opt == TFC_CTR_RAND) tfc_getrandom(ctr, ctrsz);
 
 _ctrskip2:
        if (kfd != -1) {
@@ -1245,6 +1288,7 @@ _nomac:
 
        if (do_preserve_time) fcopy_matime(dfd, &s_stat);
        xclose(sfd);
+       if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);
        xclose(dfd);
 
        xexit(exitcode);