commit 266d6321fe55b0b95675378a7621d0c259d88f06
parent d2dad2c14804f240b36322ceebe3a56b4b480a93
Author: sin <sin@2f30.org>
Date: Wed, 1 May 2019 13:34:50 +0100
Add back blake2s support
Diffstat:
3 files changed, 67 insertions(+), 6 deletions(-)
diff --git a/bstorage.c b/bstorage.c
@@ -48,6 +48,7 @@
#define CNONETYPE 0
#define CSNAPPYTYPE 1
#define HBLAKE2BTYPE 0
+#define HBLAKE2STYPE 1
extern ssize_t xread(int, void *, size_t);
extern ssize_t xwrite(int, void *, size_t);
@@ -102,6 +103,7 @@ struct sctx {
struct bhdr bhdr;
int fd;
int rdonly; /* when set to 1, the bssync() operation is a no-op */
+ int type; /* hash algorithm for new blocks */
};
static int
@@ -120,7 +122,7 @@ static RB_PROTOTYPE(bdcache, bd, rbe, bd_cmp)
static RB_GENERATE(bdcache, bd, rbe, bd_cmp)
static int
-bhash(void *buf, size_t n, unsigned char *md)
+b2bhash(void *buf, size_t n, unsigned char *md)
{
blake2b_state ctx;
@@ -131,6 +133,18 @@ bhash(void *buf, size_t n, unsigned char *md)
return blake2b_final(&ctx, md, MDSIZE);
}
+static int
+b2shash(void *buf, size_t n, unsigned char *md)
+{
+ blake2s_state ctx;
+
+ if (blake2s_init(&ctx, MDSIZE) < 0)
+ return -1;
+ if (blake2s_update(&ctx, buf, n) < 0)
+ return -1;
+ return blake2s_final(&ctx, md, MDSIZE);
+}
+
/* Read block header */
static int
unpackbhdr(int fd, struct bhdr *bhdr)
@@ -316,6 +330,8 @@ bscreat(struct bctx *bctx, char *path, int mode, struct bparam *bpar)
/* Set hash type */
if (strcmp(bpar->halgo, "blake2b") == 0) {
bhdr->flags |= HBLAKE2BTYPE << HALGOSHIFT;
+ } else if (strcmp(bpar->halgo, "blake2s") == 0) {
+ bhdr->flags |= HBLAKE2STYPE << HALGOSHIFT;
} else {
free(sctx);
close(fd);
@@ -323,6 +339,7 @@ bscreat(struct bctx *bctx, char *path, int mode, struct bparam *bpar)
}
bhdr->nbd = 0;
sctx->fd = fd;
+ sctx->type = (bhdr->flags >> HALGOSHIFT) & HALGOMASK;
if (packbhdr(fd, bhdr) < 0) {
free(sctx);
@@ -406,6 +423,9 @@ bsopen(struct bctx *bctx, char *path, int flags, int mode, struct bparam *bpar)
case HBLAKE2BTYPE:
bpar->halgo = "blake2b";
break;
+ case HBLAKE2STYPE:
+ bpar->halgo = "blake2s";
+ break;
default:
free(sctx);
close(fd);
@@ -414,6 +434,7 @@ bsopen(struct bctx *bctx, char *path, int flags, int mode, struct bparam *bpar)
sctx->fd = fd;
sctx->rdonly = flags == O_RDONLY;
+ sctx->type = halgo;
if (initbdcache(sctx) < 0) {
free(sctx);
@@ -434,8 +455,18 @@ bsput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
sctx = bctx->sctx;
- if (bhash(buf, n, key.md) < 0)
+ switch (sctx->type) {
+ case HBLAKE2BTYPE:
+ if (b2bhash(buf, n, key.md) < 0)
+ return -1;
+ break;
+ case HBLAKE2STYPE:
+ if (b2shash(buf, n, key.md) < 0)
+ return -1;
+ break;
+ default:
return -1;
+ }
bd = RB_FIND(bdcache, &sctx->bdcache, &key);
if (bd != NULL) {
@@ -608,7 +639,20 @@ bscheck(struct bctx *bctx, unsigned char *md)
return -1;
}
- if (bhash(buf, bd->size, key.md) < 0) {
+ switch (sctx->type) {
+ case HBLAKE2BTYPE:
+ if (b2bhash(buf, bd->size, key.md) < 0) {
+ free(buf);
+ return -1;
+ }
+ break;
+ case HBLAKE2STYPE:
+ if (b2shash(buf, bd->size, key.md) < 0) {
+ free(buf);
+ return -1;
+ }
+ break;
+ default:
free(buf);
return -1;
}
diff --git a/dotest b/dotest
@@ -89,6 +89,23 @@ test5()
rm -rf "$repo" "$data"
}
+test6()
+{
+ repo=`mktemp -d`
+ data=`mktemp`
+ dd if=/dev/urandom of="$data" bs=1M count=64
+ ./dup-init -H blake2s "$repo"
+ ./dup-pack -r "$repo" snap0 < "$data"
+ ./dup-pack -r "$repo" snap1 < "$data"
+ du -sh "$repo"
+ sum0=`sha1sum "$data" | awk '{print $1}'`
+ sum1=`./dup-unpack -r "$repo" snap0 | sha1sum | awk '{print $1}'`
+ sum2=`./dup-unpack -r "$repo" snap1 | sha1sum | awk '{print $1}'`
+ [ "$sum0" = "$sum1" ]
+ [ "$sum0" = "$sum2" ]
+ rm -rf "$repo" "$data"
+}
+
make
test0
test1
@@ -96,3 +113,4 @@ test2
test3
test4
test5
+test6
diff --git a/dup-init.1 b/dup-init.1
@@ -1,4 +1,4 @@
-.Dd April 25, 2019
+.Dd May 1, 2019
.Dt DUP-INIT 1
.Os
.Sh NAME
@@ -21,8 +21,7 @@ is specified the current working directory is used.
.It Fl H Ar hash
The cryptographic hash function used to identify
unique blocks in the store.
-The supported hash functions are blake2b.
-This flag only has an effect when initializing the repository.
+The supported hash functions are blake2b and blake2s.
By default blake2b is used.
.It Fl Z Ar compressor
The compressor function used to compress the blocks