From ff5a5ed054e9e2285e354ceaae7c8b7da1c15add Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 19 Mar 2011 14:04:22 +1100 Subject: [PATCH] Use separate lrz_encrypt and lrz_decrypt wrappers to lrz_crypt. --- runzip.c | 2 +- rzip.c | 2 +- stream.c | 4 ++-- util.c | 16 ++++++++++++++-- util.h | 3 ++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/runzip.c b/runzip.c index 360068f..12324fa 100644 --- a/runzip.c +++ b/runzip.c @@ -377,7 +377,7 @@ i64 runzip_fd(rzip_control *control, int fd_in, int fd_out, int fd_hist, i64 exp if (unlikely(read_1g(control, fd_in, md5_stored, MD5_DIGEST_SIZE) != MD5_DIGEST_SIZE)) fatal("Failed to read md5 data in runzip_fd\n"); if (ENCRYPT) - lrz_crypt(control, md5_stored, MD5_DIGEST_SIZE, control->pass_hash, 0); + lrz_decrypt(control, md5_stored, MD5_DIGEST_SIZE, control->pass_hash); for (i = 0; i < MD5_DIGEST_SIZE; i++) if (md5_stored[i] != md5_resblock[i]) { print_output("MD5 CHECK FAILED.\nStored:"); diff --git a/rzip.c b/rzip.c index 7b1c235..83500cf 100644 --- a/rzip.c +++ b/rzip.c @@ -985,7 +985,7 @@ retry: } /* When encrypting data, we encrypt the MD5 value as well */ if (ENCRYPT) - lrz_crypt(control, md5_resblock, MD5_DIGEST_SIZE, control->pass_hash, 1); + lrz_encrypt(control, md5_resblock, MD5_DIGEST_SIZE, control->pass_hash); if (unlikely(write_1g(control, md5_resblock, MD5_DIGEST_SIZE) != MD5_DIGEST_SIZE)) fatal("Failed to write md5 in rzip_fd\n"); diff --git a/stream.c b/stream.c index 8bf50c3..c7b34d9 100644 --- a/stream.c +++ b/stream.c @@ -1195,7 +1195,7 @@ retry: get_rand(cti->salt, 8); memcpy(cti->salt + 8, &cti->c_len, 8); memcpy(cti->salt + 16, &cti->s_len, 8); - lrz_crypt(control, cti->s_buf, padded_len, cti->salt, 1); + lrz_encrypt(control, cti->s_buf, padded_len, cti->salt); } /* If compression fails for whatever reason multithreaded, then wait @@ -1465,7 +1465,7 @@ fill_another: return -1; if (ENCRYPT) - lrz_crypt(control, s_buf, padded_len, salt, 0); + lrz_decrypt(control, s_buf, padded_len, salt); ucthread[s->uthread_no].s_buf = s_buf; ucthread[s->uthread_no].c_len = c_len; diff --git a/util.c b/util.c index 9287c25..097f3d6 100644 --- a/util.c +++ b/util.c @@ -55,6 +55,8 @@ #include "sha4.h" #include "aes.h" +#define LRZ_DECRYPT (0) +#define LRZ_ENCRYPT (1) static const char *infile = NULL; static char delete_infile = 0; @@ -164,7 +166,7 @@ static void xor128 (void *pa, const void *pb) a [1] ^= b [1]; } -void lrz_crypt(rzip_control *control, uchar *buf, i64 len, uchar *salt, int encrypt) +static void lrz_crypt(rzip_control *control, uchar *buf, i64 len, uchar *salt, int encrypt) { /* Encryption requires CBC_LEN blocks so we can use ciphertext * stealing to not have to pad the block */ @@ -190,7 +192,7 @@ void lrz_crypt(rzip_control *control, uchar *buf, i64 len, uchar *salt, int encr M = len % CBC_LEN; N = len - M; - if (encrypt) { + if (encrypt == LRZ_ENCRYPT) { print_maxverbose("Encrypting data \n"); if (unlikely(aes_setkey_enc(&aes_ctx, key, 128))) failure("Failed to aes_setkey_enc in lrz_crypt\n"); @@ -234,6 +236,16 @@ void lrz_crypt(rzip_control *control, uchar *buf, i64 len, uchar *salt, int encr munlock(key, HASH_LEN + BLOCKSALT_LEN); } +inline void lrz_encrypt(rzip_control *control, uchar *buf, i64 len, uchar *salt) +{ + lrz_crypt(control, buf, len, salt, LRZ_ENCRYPT); +} + +inline void lrz_decrypt(rzip_control *control, uchar *buf, i64 len, uchar *salt) +{ + lrz_crypt(control, buf, len, salt, LRZ_DECRYPT); +} + void lrz_keygen(rzip_control *control, const uchar *passphrase) { int i, j; diff --git a/util.h b/util.h index 1c95bdb..fb6050b 100644 --- a/util.h +++ b/util.h @@ -29,7 +29,8 @@ void fatal(const char *format, ...); void failure(const char *format, ...); void round_to_page(i64 *size); void get_rand(uchar *buf, int len); -void lrz_crypt(rzip_control *control, uchar *buf, i64 len, uchar *salt, int encrypt); +inline void lrz_encrypt(rzip_control *control, uchar *buf, i64 len, uchar *salt); +inline void lrz_decrypt(rzip_control *control, uchar *buf, i64 len, uchar *salt); void lrz_keygen(rzip_control *control, const uchar *passphrase); #endif