diff --git a/doc/magic.header.txt b/doc/magic.header.txt index b986f77..669f09e 100644 --- a/doc/magic.header.txt +++ b/doc/magic.header.txt @@ -18,8 +18,9 @@ Magic data: 16->20 LZMA Properties Encoded (lc,lp,pb,fb, and dictionary size) 21 Flag that md5sum hash is stored at the end of the archive 22 Flag that the data is encrypted -23->30 Time in seconds (first 8 bits of salt) -31->38 Time in useconds (last 8 bits of salt) +23->27 Time in seconds (first 5 bytes of salt) +28->29 Encoded number of hash loops (bytes 6-7 of salt) +30->38 Random data (last 9 bytes of salt) Rzip chunk data: 0 Data offsets byte width diff --git a/lrzip.c b/lrzip.c index 25566e4..e451565 100644 --- a/lrzip.c +++ b/lrzip.c @@ -35,7 +35,6 @@ #include #endif #include -#include #include "md5.h" #include "rzip.h" @@ -46,39 +45,8 @@ #define MAGIC_LEN (39) -/* Determine how many times to hash the password when encrypting, based on - * the date such that we increase the number of loops according to Moore's - * law relative to when the data is encrypted. It is then stored as a two - * byte value in the header */ -#define MOORE 1.835 // world constant [TIMES per YEAR] -#define ARBITRARY 1000000 // number of sha2 calls per one second in 2011 -#define T_ZERO 1293840000 // seconds since epoch in 2011 - -#define SECONDS_IN_A_YEAR (365*86400) -#define MOORE_TIMES_PER_SECOND pow (MOORE, 1.0 / SECONDS_IN_A_YEAR) -#define ARBITRARY_AT_EPOCH (ARBITRARY * pow (MOORE_TIMES_PER_SECOND, -T_ZERO)) - -static i64 nloops(i64 seconds, uchar *b1, uchar *b2) -{ - i64 nloops_encoded, nloops; - int nbits; - - nloops = ARBITRARY_AT_EPOCH * pow(MOORE_TIMES_PER_SECOND, seconds); - nbits = log (nloops) / M_LN2; - *b1 = nbits - 7; - *b2 = nloops >> *b1; - nloops_encoded = (i64)*b2 << (i64)*b1; - return nloops_encoded; -} - -static i64 enc_loops(uchar b1, uchar b2) -{ - return (i64)b2 << (i64)b1; -} - static char *make_magic(rzip_control *control) { - struct timeval tv; char *magic; magic = calloc(MAGIC_LEN, 1); @@ -109,16 +77,7 @@ static char *make_magic(rzip_control *control) if (control->encrypt) magic[22] = 1; - if (unlikely(gettimeofday(&tv, NULL))) - fatal("Failed to gettimeofday in write_magic\n"); - control->secs = tv.tv_sec; - /* The first 6 bytes of the salt is random data. The last 2 bytes - * encode how many times to hash the password */ - get_rand(control->salt, 6); - control->encloops = nloops(control->secs, control->salt + 6, control->salt + 7); - - memcpy(&magic[23], &control->secs, 8); - memcpy(&magic[31], &control->salt, 8); + memcpy(&magic[23], &control->salt, 16); return magic; } @@ -186,13 +145,20 @@ static void get_magicver05(rzip_control *control, char *magic) control->flags |= FLAG_MD5; } +static i64 enc_loops(uchar b1, uchar b2) +{ + return (i64)b2 << (i64)b1; +} + static void get_magicver06(rzip_control *control, char *magic) { if (magic[22] == 1) control->encrypt = 1; - memcpy(&control->secs, &magic[23], 8); - memcpy(&control->salt, &magic[31], 8); - print_maxverbose("Seconds %lld\n", control->secs); + memcpy(control->salt, &magic[23], 16); + memcpy(&control->secs, control->salt, 5); + print_maxverbose("Storage time in seconds %lld\n", control->secs); + control->encloops = enc_loops(control->salt[5], control->salt[6]); + print_maxverbose("Encryption hash loops %lld\n", control->encloops); } void read_magic(rzip_control *control, int fd_in, i64 *expected_size) @@ -824,8 +790,6 @@ next_chunk: cratio = (long double)expected_size / (long double)infile_size; print_output("%s:\nlrzip version: %d.%d file\n", infilecopy, control->major_version, control->minor_version); - if (control->secs) - print_maxverbose("Storage time seconds: %lld\n", control->secs); print_output("Compression: "); if (ctype == CTYPE_NONE) diff --git a/lrzip_private.h b/lrzip_private.h index 4a8e72a..0c6e724 100644 --- a/lrzip_private.h +++ b/lrzip_private.h @@ -196,7 +196,7 @@ struct rzip_control { uchar loop_byte1; uchar loop_byte2; i64 secs; - uchar salt[8]; + uchar salt[16]; unsigned char eof; unsigned char magic_written; md5_ctx ctx; diff --git a/main.c b/main.c index 0e8d7ad..50e5e98 100644 --- a/main.c +++ b/main.c @@ -35,7 +35,7 @@ #ifdef HAVE_SYS_RESOURCE_H # include #endif - +#include #include "rzip.h" #include "lrzip.h" @@ -267,6 +267,8 @@ static void show_summary(void) if (UNLIMITED) print_verbose("Using Unlimited Window size\n"); } + print_maxverbose("Storage time in seconds %lld\n", control.secs); + print_maxverbose("Encryption hash loops %lld\n", control.encloops); } } @@ -425,9 +427,34 @@ static void read_config( struct rzip_control *control ) */ } +/* Determine how many times to hash the password when encrypting, based on + * the date such that we increase the number of loops according to Moore's + * law relative to when the data is encrypted. It is then stored as a two + * byte value in the header */ +#define MOORE 1.835 // world constant [TIMES per YEAR] +#define ARBITRARY 1000000 // number of sha2 calls per one second in 2011 +#define T_ZERO 1293840000 // seconds since epoch in 2011 + +#define SECONDS_IN_A_YEAR (365*86400) +#define MOORE_TIMES_PER_SECOND pow (MOORE, 1.0 / SECONDS_IN_A_YEAR) +#define ARBITRARY_AT_EPOCH (ARBITRARY * pow (MOORE_TIMES_PER_SECOND, -T_ZERO)) + +static i64 nloops(i64 seconds, uchar *b1, uchar *b2) +{ + i64 nloops_encoded, nloops; + int nbits; + + nloops = ARBITRARY_AT_EPOCH * pow(MOORE_TIMES_PER_SECOND, seconds); + nbits = log (nloops) / M_LN2; + *b1 = nbits - 7; + *b2 = nloops >> *b1; + nloops_encoded = (i64)*b2 << (i64)*b1; + return nloops_encoded; +} + int main(int argc, char *argv[]) { - struct timeval start_time, end_time; + struct timeval start_time, end_time, tv; struct sigaction handler; double seconds,total_time; // for timers int c, i; @@ -454,6 +481,16 @@ int main(int argc, char *argv[]) control.page_size = PAGE_SIZE; control.nice_val = 19; + /* The first 5 bytes of the salt is the time in seconds. + * The next 2 bytes encode how many times to hash the password. + * The last 9 bytes are random data, making 16 bytes of salt */ + if (unlikely(gettimeofday(&tv, NULL))) + fatal("Failed to gettimeofday in main\n"); + control.secs = tv.tv_sec; + memcpy(control.salt, &control.secs, 5); + control.encloops = nloops(control.secs, control.salt + 5, control.salt + 6); + get_rand(control.salt + 7, 9); + /* generate crc table */ CrcGenerateTable();