From: Andrey Rys Date: Mon, 4 Feb 2019 15:18:00 +0000 (+0700) Subject: -O okey=val: offset into key (useful for block device keys, such as USB sticks). X-Git-Url: https://jxself.org/git/?p=tfcrypt.git;a=commitdiff_plain;h=a7a8ae5408eed270854e69adf40ccaf3a401f830 -O okey=val: offset into key (useful for block device keys, such as USB sticks). sksum: now honors -O iseek= too to seek into source. --- diff --git a/VERSION b/VERSION index 45a4fb7..ec63514 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -8 +9 diff --git a/tfc_error.c b/tfc_error.c index e331315..e1793fe 100644 --- a/tfc_error.c +++ b/tfc_error.c @@ -252,6 +252,7 @@ void usage(void) tfc_say(" oseek=val: seek destination file/device by these val bytes."); tfc_say(" count=val: process only these val bytes, both input and output."); tfc_say(" xkey=val: take only val bytes from user keyfile."); + tfc_say(" okey=val: seek the key before reading it (usually a device)."); tfc_say(" xctr=val: specify size in bytes of initial counter prepended or read."); tfc_say(" -P: plain IO mode: disable encryption/decryption code at all."); tfc_say("\n"); diff --git a/tfc_skein.c b/tfc_skein.c index b3b2019..fe86c20 100644 --- a/tfc_skein.c +++ b/tfc_skein.c @@ -49,7 +49,7 @@ void tf_key_tweak_compat(void *key) 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]; @@ -62,9 +62,14 @@ tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize re 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) goto _fail; + } } if (key) skein_init_key(&sk, key, bits); @@ -226,7 +231,7 @@ _dothat: } if (status_timer) setup_next_alarm(status_timer); - if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, maxlen) != YES) { + if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, iseek, maxlen) != YES) { xerror(YES, NO, YES, "%s", fname); exitcode = 1; continue; @@ -278,7 +283,7 @@ _dothat: } _dohash: if (status_timer) setup_next_alarm(status_timer); - if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, maxlen) != YES) { + if (skeinfd(hash, bits, mackey_opt ? mackey : NULL, fd, iseek, maxlen) != YES) { xerror(YES, NO, YES, "%s", fargv[x]); exitcode = 1; continue; diff --git a/tfc_vars.c b/tfc_vars.c index 5f75952..d7b4818 100644 --- a/tfc_vars.c +++ b/tfc_vars.c @@ -46,13 +46,14 @@ tfc_fsize total_processed_src, total_processed_dst; tfc_fsize delta_processed; tfc_fsize genrandom_nr_bytes, genzero_nr_bytes; tfc_fsize rdpos = NOFSIZE; +tfc_fsize maxkeylen = NOFSIZE, keyoffset; int sfd, kfd = -1, dfd = 1; struct stat s_stat; size_t blksize = TFC_BLKSIZE, xtsblocks = TFC_XTSBLOCKS; char pwdask[512], pwdagain[512]; size_t lio, lrem, ldone, lblock; -size_t maxkeylen = NOSIZE, ctrsz = NOSIZE; +size_t ctrsz = NOSIZE; struct sigaction sigact; diff --git a/tfcrypt.c b/tfcrypt.c index 8608cf1..df62420 100644 --- a/tfcrypt.c +++ b/tfcrypt.c @@ -328,13 +328,25 @@ int main(int argc, char **argv) s += 5; maxkeylen = tfc_humanfsize(s, &stoi); if (!str_empty(stoi)) { - maxkeylen = (size_t)tfc_fnamesize(s, YES); - maxkeylen = (size_t)tfc_modifysize((tfc_fsize)maxkeylen, strchr(s, ':')); + maxkeylen = tfc_fnamesize(s, YES); + maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':')); if (maxkeylen == NOSIZE) xerror(NO, YES, YES, "%s: invalid key length value", s); } - else maxkeylen = (size_t)tfc_modifysize((tfc_fsize)maxkeylen, strchr(s, ':')); + else maxkeylen = tfc_modifysize(maxkeylen, strchr(s, ':')); + } + else if (!strncmp(s, "okey", 4) && *(s+4) == '=') { + s += 5; + keyoffset = tfc_humanfsize(s, &stoi); + if (!str_empty(stoi)) { + keyoffset = tfc_fnamesize(s, YES); + keyoffset = tfc_modifysize(keyoffset, strchr(s, ':')); + if (keyoffset == NOFSIZE) + xerror(NO, YES, YES, + "%s: invalid key offset value", s); + } + else keyoffset = tfc_modifysize(keyoffset, strchr(s, ':')); } else if (!strncmp(s, "xctr", 4) && *(s+4) == '=') { s += 5; @@ -814,7 +826,7 @@ _pwdagain: memset(&getps, 0, sizeof(struct getpasswd_state)); memset(pwdagain, 0, sizeof(pwdagain)); } else { - if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, maxkeylen) != YES) + if (skeinfd(key, TFC_KEY_BITS, mackey_opt ? mackey : NULL, kfd, keyoffset, maxkeylen) != YES) xerror(NO, NO, YES, "hashing key"); } diff --git a/tfcrypt.h b/tfcrypt.h index c469638..bf0d6d6 100644 --- a/tfcrypt.h +++ b/tfcrypt.h @@ -139,12 +139,13 @@ extern tfc_fsize total_processed_src, total_processed_dst; extern tfc_fsize delta_processed; extern tfc_fsize genrandom_nr_bytes, genzero_nr_bytes; extern tfc_fsize rdpos; +extern tfc_fsize maxkeylen, keyoffset; extern int sfd, kfd, dfd; extern struct stat s_stat; extern size_t blksize, xtsblocks; extern char pwdask[512], pwdagain[512]; extern size_t lio, lrem, ldone, lblock; -extern size_t maxkeylen, ctrsz; +extern size_t ctrsz; extern struct sigaction sigact; extern size_t sksum_turns; extern int do_edcrypt, do_stop, quiet, error_action; @@ -200,7 +201,7 @@ void change_status_timer(int signal); void setup_next_alarm(tfc_useconds useconds); void skein(void *hash, size_t bits, const void *key, const void *data, size_t szdata); void tf_key_tweak_compat(void *key); -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); void gen_write_bytes(const char *foutname, tfc_fsize offset, tfc_fsize nrbytes); void do_edbase64(char **fargv);