aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Frank Denis <github@pureftpd.org>2017-12-30 08:53:57 +0100
committerGravatar Frank Denis <github@pureftpd.org>2017-12-30 08:54:12 +0100
commitd648e4122e8191f109b1407933da7e0486416d93 (patch)
tree20b73a46f14a2202dd5db7b04738a6f1bb1c0474 /src
parent8c8da901528659820ff40f923fe3a4b69d76e94a (diff)
downloadtweetpipe-d648e4122e8191f109b1407933da7e0486416d93.tar.gz
Helpers
Diffstat (limited to 'src')
-rw-r--r--src/common.h14
-rw-r--r--src/encpipe.c32
2 files changed, 25 insertions, 21 deletions
diff --git a/src/common.h b/src/common.h
index 8181c42..7a9c555 100644
--- a/src/common.h
+++ b/src/common.h
@@ -25,4 +25,18 @@ store32_le(uint8_t dst[4], uint32_t w)
dst[3] = (uint8_t) w;
}
+static void
+die(const char *msg)
+{
+ fprintf(stderr, "%s\n", msg);
+ exit(1);
+}
+
+static void
+diex(const char *msg)
+{
+ perror(msg);
+ exit(1);
+}
+
#endif
diff --git a/src/encpipe.c b/src/encpipe.c
index d3d4b0d..2cdd387 100644
--- a/src/encpipe.c
+++ b/src/encpipe.c
@@ -83,8 +83,7 @@ derive_key(Context *ctx)
if (hydro_pwhash_deterministic(ctx->key, sizeof ctx->key, ctx->password, password_len,
HYDRO_CONTEXT, master_key, PWHASH_OPSLIMIT, PWHASH_MEMLIMIT,
PWHASH_THREADS) != 0) {
- fprintf(stderr, "Password hashing failed\n");
- exit(1);
+ die("Password hashing failed");
}
hydro_memzero(ctx->password, password_len);
}
@@ -106,13 +105,11 @@ stream_encrypt(Context *ctx)
STORE32_LE(chunk_size_p, (uint32_t) chunk_size);
if (hydro_secretbox_encrypt(chunk, chunk, chunk_size, chunk_id, HYDRO_CONTEXT, ctx->key) !=
0) {
- fprintf(stderr, "Encryption error\n");
- exit(1);
+ die("Encryption error");
}
if (safe_write(ctx->fd_out, chunk_size_p,
4 + hydro_secretbox_HEADERBYTES + (size_t) chunk_size, -1) < 0) {
- perror("write()");
- exit(1);
+ diex("write()");
}
if (chunk_size == 0) {
break;
@@ -120,8 +117,7 @@ stream_encrypt(Context *ctx)
chunk_id++;
}
if (chunk_size < 0) {
- perror("read()");
- exit(1);
+ diex("read()");
}
return 0;
}
@@ -155,28 +151,24 @@ stream_decrypt(Context *ctx)
chunk_id, HYDRO_CONTEXT, ctx->key) != 0) {
fprintf(stderr, "Unable to decrypt chunk #%" PRIu64 " - ", chunk_id);
if (chunk_id == 0) {
- fprintf(stderr, "Wrong password or key?\n");
+ die("Wrong password or key?");
} else {
- fprintf(stderr, "Corrupted or incomplete file?\n");
+ die("Corrupted or incomplete file?");
}
- exit(1);
}
if (chunk_size == 0) {
break;
}
if (safe_write(ctx->fd_out, chunk, chunk_size, -1) < 0) {
- perror("write()");
- exit(1);
+ diex("write()");
}
chunk_id++;
}
if (readnb < 0) {
- perror("read()");
- exit(1);
+ diex("read()");
}
if (chunk_size != 0) {
- fprintf(stderr, "Premature end of file\n");
- exit(1);
+ die("Premature end of file");
}
return 0;
}
@@ -187,8 +179,7 @@ main(int argc, char *argv[])
Context ctx;
if (hydro_init() < 0) {
- fprintf(stderr, "Unable to initialize the crypto library");
- exit(1);
+ diex("Unable to initialize the crypto library");
}
memset(&ctx, 0, sizeof ctx);
options_parse(&ctx, argc, argv);
@@ -200,8 +191,7 @@ main(int argc, char *argv[])
ctx.sizeof_buf = MAX_BUFFER_SIZE;
}
if ((ctx.buf = malloc(ctx.sizeof_buf)) == NULL) {
- perror("malloc()");
- exit(1);
+ diex("malloc()");
}
assert(sizeof HYDRO_CONTEXT == hydro_secretbox_CONTEXTBYTES);