Skip to contentSkip to conversation

Add BLAKE2 checksumming support

PATCH New David Sterba 1 participant 5 messages Oct 21, 2019

Subsystems (3)
lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Add BLAKE2 checksumming support

The two patches apply on top of Johaness' series adding SHA256. To make it actually work the patch adding BLAKE2 to kernel is needed. You can find it here (v5) https://patchwork.kernel.org/patch/11188109/ .

WARNING: This is for testing only!

All in one can be pulled from

git://github.com/kdave/btrfs-devel preview/checksums-5

and the btrfs-progs patches are in the devel branch

git://github.com/kdave/btrfs-progs devel

David Sterba (2):
btrfs: add member for a specific checksum driver
btrfs: add blake2b to checksumming algorithms

 fs/btrfs/ctree.c                | 14 ++++++++++++++
 fs/btrfs/ctree.h                |  1 +
 fs/btrfs/disk-io.c              |  7 ++++---
 fs/btrfs/super.c                |  1 +
 include/uapi/linux/btrfs_tree.h |  1 +
 5 files changed, 21 insertions(+), 3 deletions(-)
Signature · 1 line
2.23.0
author maintainer
#1 lore raw

PATCH 1/2 btrfs: add member for a specific checksum driver

Currently all the checksum algorithms generate a fixed size digest size and we use it. The on-disk format can hold up to BTRFS_CSUM_SIZE bytes and BLAKE2b produces digest of 512 bits by default. We can't do that and will use the blake2b-256, this needs to be passed to the crypto API.

Separate that from the base algorithm name and add a member to request specific driver, in this case with the digest size.

The only place that uses the driver name is the crypto API setup.

---
 fs/btrfs/ctree.c   | 12 ++++++++++++
 fs/btrfs/ctree.h   |  1 +
 fs/btrfs/disk-io.c |  6 +++---
 3 files changed, 16 insertions(+), 3 deletions(-)
fs/btrfs/ctree.c +12 -0
index c1561097e5a3..c211216b4524 100644
@@ -32,6 +32,7 @@ static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
 static const struct btrfs_csums {
 	u16		size;
 	const char	*name;
+	const char	*driver;
 } btrfs_csums[] = {
 	[BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
 	[BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
@@ -53,6 +54,17 @@ const char *btrfs_super_csum_name(u16 csum_type)
 	return btrfs_csums[csum_type].name;
 }
 
+/*
+ * Return driver name if defined, otherwise the name that's also a valid driver
+ * name
+ */
+const char *btrfs_super_csum_driver(u16 csum_type)
+{
+	/* csum type is validated at mount time */
+	return btrfs_csums[csum_type].driver ?:
+		btrfs_csums[csum_type].name;
+}
+
 size_t __const btrfs_get_num_csums(void)
 {
 	return ARRAY_SIZE(btrfs_csums);
fs/btrfs/ctree.h +1 -0
index b17944995e09..4a842c0fa062 100644
@@ -2163,6 +2163,7 @@ BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
 
 int btrfs_super_csum_size(const struct btrfs_super_block *s);
 const char *btrfs_super_csum_name(u16 csum_type);
+const char *btrfs_super_csum_driver(u16 csum_type);
 size_t __const btrfs_get_num_csums(void);
 
 
fs/btrfs/disk-io.c +3 -3
index 21fab336f8ca..948ed5ac0794 100644
@@ -2219,13 +2219,13 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
 static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
 {
 	struct crypto_shash *csum_shash;
-	const char *csum_name = btrfs_super_csum_name(csum_type);
+	const char *csum_driver = btrfs_super_csum_driver(csum_type);
 
-	csum_shash = crypto_alloc_shash(csum_name, 0, 0);
+	csum_shash = crypto_alloc_shash(csum_driver, 0, 0);
 
 	if (IS_ERR(csum_shash)) {
 		btrfs_err(fs_info, "error allocating %s hash for checksum",
-			  csum_name);
+			  csum_driver);
 		return PTR_ERR(csum_shash);
 	}
 
Signature · 1 line
2.23.0
author maintainer
#2 lore raw

PATCH 2/2 btrfs: add blake2b to checksumming algorithms

In reply to David Sterba

Add blake2b (with 256 bit digest) to the list of possible checksumming algorithms used by BTRFS.

---
 fs/btrfs/ctree.c                | 2 ++
 fs/btrfs/disk-io.c              | 1 +
 fs/btrfs/super.c                | 1 +
 include/uapi/linux/btrfs_tree.h | 1 +
 4 files changed, 5 insertions(+)
fs/btrfs/ctree.c +2 -0
index c211216b4524..5b6e86aaf2e1 100644
@@ -37,6 +37,8 @@ static const struct btrfs_csums {
 	[BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
 	[BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
 	[BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
+	[BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
+				     .driver = "blake2b-256" },
 };
 
 int btrfs_super_csum_size(const struct btrfs_super_block *s)
fs/btrfs/disk-io.c +1 -0
index 948ed5ac0794..7c345c4bc817 100644
@@ -354,6 +354,7 @@ static bool btrfs_supported_super_csum(u16 csum_type)
 	case BTRFS_CSUM_TYPE_CRC32:
 	case BTRFS_CSUM_TYPE_XXHASH:
 	case BTRFS_CSUM_TYPE_SHA256:
+	case BTRFS_CSUM_TYPE_BLAKE2:
 		return true;
 	default:
 		return false;
fs/btrfs/super.c +1 -0
index 36440336c08b..5c6796caebb5 100644
@@ -2464,3 +2464,4 @@ MODULE_LICENSE("GPL");
 MODULE_SOFTDEP("pre: crc32c");
 MODULE_SOFTDEP("pre: xxhash64");
 MODULE_SOFTDEP("pre: sha256");
+MODULE_SOFTDEP("pre: blake2b");
include/uapi/linux/btrfs_tree.h +1 -0
index e2823c9b2061..5160be1d7332 100644
@@ -304,6 +304,7 @@ enum btrfs_csum_type {
 	BTRFS_CSUM_TYPE_CRC32	= 0,
 	BTRFS_CSUM_TYPE_XXHASH	= 1,
 	BTRFS_CSUM_TYPE_SHA256	= 2,
+	BTRFS_CSUM_TYPE_BLAKE2	= 3,
 };
 
 /*
Signature · 1 line
2.23.0
author maintainer
#3 lore raw

PATCH 3/4 btrfs-progs: add blake2b support

In reply to David Sterba

Add definition, crypto wrappers and support to mkfs for blake2 for checksumming. There are 2 aliases either blake2 or blake2b.

---
 cmds/inspect-dump-super.c |  1 +
 crypto/hash.c             | 12 ++++++++++++
 crypto/hash.h             |  1 +
 ctree.c                   |  1 +
 ctree.h                   |  1 +
 disk-io.c                 |  2 ++
 mkfs/main.c               |  3 +++
 7 files changed, 21 insertions(+)
cmds/inspect-dump-super.c +1 -0
index 47f56f78934f..fc06488dde32 100644
@@ -317,6 +317,7 @@ static bool is_valid_csum_type(u16 csum_type)
 	case BTRFS_CSUM_TYPE_CRC32:
 	case BTRFS_CSUM_TYPE_XXHASH:
 	case BTRFS_CSUM_TYPE_SHA256:
+	case BTRFS_CSUM_TYPE_BLAKE2:
 		return true;
 	default:
 		return false;
crypto/hash.c +12 -0
index ee4c7f4e7112..48623c798739 100644
@@ -2,6 +2,7 @@
 #include "crypto/crc32c.h"
 #include "crypto/xxhash.h"
 #include "crypto/sha.h"
+#include "crypto/blake2.h"
 
 int hash_crc32c(const u8* buf, size_t length, u8 *out)
 {
@@ -37,3 +38,14 @@ int hash_sha256(const u8 *buf, size_t len, u8 *out)
 
 	return 0;
 }
+
+int hash_blake2b(const u8 *buf, size_t len, u8 *out)
+{
+	blake2b_state S;
+
+	blake2b_init(&S, CRYPTO_HASH_SIZE_MAX);
+	blake2b_update(&S, buf, len);
+	blake2b_final(&S, out, CRYPTO_HASH_SIZE_MAX);
+
+	return 0;
+}
crypto/hash.h +1 -0
index 49d586541af9..fefccbd59a09 100644
@@ -8,5 +8,6 @@
 int hash_crc32c(const u8 *buf, size_t length, u8 *out);
 int hash_xxhash(const u8 *buf, size_t length, u8 *out);
 int hash_sha256(const u8 *buf, size_t length, u8 *out);
+int hash_blake2b(const u8 *buf, size_t length, u8 *out);
 
 #endif
ctree.c +1 -0
index 19052e8ec128..97b44d48dc85 100644
@@ -45,6 +45,7 @@ static const struct btrfs_csum {
 	[BTRFS_CSUM_TYPE_CRC32]		= {  4, "crc32c" },
 	[BTRFS_CSUM_TYPE_XXHASH]	= {  8, "xxhash64" },
 	[BTRFS_CSUM_TYPE_SHA256]	= { 32, "sha256" },
+	[BTRFS_CSUM_TYPE_BLAKE2]	= { 32, "blake2" },
 };
 
 u16 btrfs_super_csum_size(const struct btrfs_super_block *sb)
ctree.h +1 -0
index f7f7e2963b0e..b99ba11279ad 100644
@@ -169,6 +169,7 @@ enum btrfs_csum_type {
 	BTRFS_CSUM_TYPE_CRC32		= 0,
 	BTRFS_CSUM_TYPE_XXHASH		= 1,
 	BTRFS_CSUM_TYPE_SHA256		= 2,
+	BTRFS_CSUM_TYPE_BLAKE2		= 3,
 };
 
 #define BTRFS_EMPTY_DIR_SIZE 0
disk-io.c +2 -0
index 62ec8c1a84bf..a9744af90a43 100644
@@ -150,6 +150,8 @@ int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)
 		return hash_xxhash(data, len, out);
 	case BTRFS_CSUM_TYPE_SHA256:
 		return hash_sha256(data, len, out);
+	case BTRFS_CSUM_TYPE_BLAKE2:
+		return hash_blake2b(data, len, out);
 	default:
 		fprintf(stderr, "ERROR: unknown csum type: %d\n", csum_type);
 		ASSERT(0);
mkfs/main.c +3 -0
index 607cfe3e0cf5..1a4578412b41 100644
@@ -397,6 +397,9 @@ static enum btrfs_csum_type parse_csum_type(const char *s)
 		return BTRFS_CSUM_TYPE_XXHASH;
 	} else if (strcasecmp(s, "sha256") == 0) {
 		return BTRFS_CSUM_TYPE_SHA256;
+	} else if (strcasecmp(s, "blake2b") == 0 ||
+		   strcasecmp(s, "blake2") == 0) {
+		return BTRFS_CSUM_TYPE_BLAKE2;
 	} else {
 		error("unknown csum type %s", s);
 		exit(1);
Signature · 1 line
2.23.0
author maintainer
#4 lore raw

PATCH 3/4 btrfs-progs: add blake2b reference implementation

In reply to David Sterba

Upstream commit 997fa5ba1e14b52c554fb03ce39e579e6f27b90c, git repository: git://github.com/BLAKE2/BLAKE2

The reference implemetation added in this patch is unchanged and will be modified only to compile in current code base and with minimal other modifications in case of future sync with upstream code. IOW, the coding style should stay as-is and does not conform to the other btrfs-progs code. This is an exception for xxhash and sha256 code as well.

---
 Makefile             |   3 +-
 crypto/blake2-impl.h | 160 ++++++++++++++++++
 crypto/blake2.h      | 195 ++++++++++++++++++++++
 crypto/blake2b-ref.c | 379 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 736 insertions(+), 1 deletion(-)
 create mode 100644 crypto/blake2-impl.h
 create mode 100644 crypto/blake2.h
 create mode 100644 crypto/blake2b-ref.c
Makefile +2 -1
index 9104764e974c..e9d01d9838d9 100644
@@ -153,7 +153,8 @@ libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
 		   kernel-lib/radix-tree.o extent-cache.o extent_io.o \
 		   crypto/crc32c.o common/messages.o \
 		   uuid-tree.o utils-lib.o common/rbtree-utils.o \
-		   crypto/hash.o crypto/xxhash.o crypto/sha224-256.o
+		   crypto/hash.o crypto/xxhash.o crypto/sha224-256.o \
+		   crypto/blake2b-ref.o
 libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
 	       crypto/crc32c.h kernel-lib/list.h kerncompat.h \
 	       kernel-lib/radix-tree.h kernel-lib/sizes.h kernel-lib/raid56.h \
crypto/blake2-impl.h new file +160 -0

160 lines not on this page · raw message

crypto/blake2.h new file +195 -0

195 lines not on this page · raw message

crypto/blake2b-ref.c new file +379 -0

379 lines not on this page · raw message

Signature · 1 line
2.23.0