dedup

deduplicating backup program
git clone git://git.2f30.org/dedup
Log | Files | Refs | README | LICENSE

commit 9be48e61285e87fff2638f5b2acee3e15a00046c
parent 873ae0109133cf38b38ad3a0466bfed825462b19
Author: sin <sin@2f30.org>
Date:   Fri,  3 May 2019 15:11:01 +0100

Error reporting for bencrypt.c

Diffstat:
Mbencrypt.c | 48++++++++++++++++++++++++++++++++++++------------
1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/bencrypt.c b/bencrypt.c @@ -99,24 +99,32 @@ becreat(struct bctx *bctx, char *path, int mode, struct bparam *bpar) int type; /* Determine algorithm type */ - if (strcasecmp(bpar->ealgo, "none") == 0) + if (strcasecmp(bpar->ealgo, "none") == 0) { type = EDNONETYPE; - else if (strcasecmp(bpar->ealgo, "XChaCha20-Poly1305") == 0) + } else if (strcasecmp(bpar->ealgo, "XChaCha20-Poly1305") == 0) { type = EDCHACHATYPE; - else + } else { + bseterr("invalid encryption type: %s", bpar->ealgo); return -1; + } /* Ensure that if caller requested encryption, a key was provided */ - if (type != EDNONETYPE && bpar->key == NULL) + if (type != EDNONETYPE && bpar->key == NULL) { + bseterr("expected encryption key"); return -1; + } - if (sodium_init() < 0) + if (sodium_init() < 0) { + bseterr("crypto library failed to initialize"); return -1; + } /* Allocate and initialize encryption context */ bctx->ectx = calloc(1, sizeof(struct ectx)); - if (bctx->ectx == NULL) + if (bctx->ectx == NULL) { + bseterr("out of memory"); return -1; + } ectx = bctx->ectx; ectx->type = type; if (bpar->key != NULL) @@ -138,8 +146,10 @@ beopen(struct bctx *bctx, char *path, int flags, int mode, struct bparam *bpar) /* Allocate and initialize encryption context */ bctx->ectx = calloc(1, sizeof(struct ectx)); - if (bctx->ectx == NULL) + if (bctx->ectx == NULL) { + bseterr("out of memory"); return -1; + } ectx = bctx->ectx; if (bpar->key != NULL) memcpy(ectx->key, bpar->key, KEYSIZE); @@ -158,6 +168,7 @@ beopen(struct bctx *bctx, char *path, int flags, int mode, struct bparam *bpar) else { bops->close(bctx); free(ectx); + bseterr("invalid encryption type: %s", bpar->ealgo); return -1; } @@ -165,6 +176,7 @@ beopen(struct bctx *bctx, char *path, int flags, int mode, struct bparam *bpar) if (ectx->type != EDNONETYPE && bpar->key == NULL) { bops->close(bctx); free(ectx); + bseterr("expected encryption key"); return -1; } @@ -182,17 +194,21 @@ beput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) /* Calculate size of encrypted block */ ectx = bctx->ectx; - if (ectx->type == EDNONETYPE) + if (ectx->type == EDNONETYPE) { en = n; - else if (ectx->type == EDCHACHATYPE) + } else if (ectx->type == EDCHACHATYPE) { en = n + crypto_aead_xchacha20poly1305_ietf_ABYTES; - else + } else { + bseterr("invalid encryption type: %d", ectx->type); return -1; + } /* Allocate encrypted block */ ebuf = malloc(EDSIZE + en); - if (ebuf == NULL) + if (ebuf == NULL) { + bseterr("out of memory"); return -1; + } /* Prepend the encryption descriptor */ ed.type = ectx->type; @@ -204,6 +220,7 @@ beput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) randombytes_buf(ed.nonce, sizeof(ed.nonce)); } else { free(ebuf); + bseterr("invalid encryption type: %d", ectx->type); return -1; } packed(ebuf, &ed); @@ -220,6 +237,7 @@ beput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) assert(elen == en); } else { free(ebuf); + bseterr("invalid encryption type: %d", ectx->type); return -1; } @@ -244,8 +262,10 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) /* Calculate maximum size of encrypted block */ size = EDSIZE + *n + crypto_aead_xchacha20poly1305_ietf_ABYTES; ebuf = malloc(size); - if (ebuf == NULL) + if (ebuf == NULL) { + bseterr("out of memory"); return -1; + } /* Read encrypted block */ bops = bstorageops(); @@ -261,6 +281,7 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) dn = ed.size; if (*n < dn) { free(ebuf); + bseterr("buffer too small"); return -1; } memcpy(buf, &ebuf[EDSIZE], dn); @@ -271,6 +292,7 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) dn = ed.size - crypto_aead_xchacha20poly1305_ietf_ABYTES; if (*n < dn) { free(ebuf); + bseterr("buffer too small"); return -1; } @@ -281,12 +303,14 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) ebuf, EDSIZE, ed.nonce, ectx->key) < 0) { free(ebuf); + bseterr("authentication or decryption failed"); return -1; } assert(dn == dlen); } else { free(ebuf); + bseterr("invalid encryption type: %d", ed.type); return -1; }