Skip to contentSkip to conversation

BLAKE2b generic implementation

PATCH Superseded David Sterba 3 participants 5 messages Oct 23, 2019

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover BLAKE2b generic implementation

The patchset adds blake2b reference implementation and test vectors.

V6:

Patch 2/2: test vectors fixed to actually match the proposed table of key and plaintext combinations. I shamelessly copied the test vector value format that Ard uses for the blake2s test vectors. The array blake2b_ordered_sequence can be shared between 2s and 2b but as the patchsets go separate, unification would have to happen once both are merged.

Tested on x86_64 with KASAN and SLUB_DEBUG.

V1: https://lore.kernel.org/linux-crypto/[email protected]/
V2: https://lore.kernel.org/linux-crypto/e31c2030fcfa7f409b2c81adf8f179a8a55a584a.1570184333.git.dsterba@suse.com/
V3: https://lore.kernel.org/linux-crypto/e7f46def436c2c705c0b2cac3324f817efa4717d.1570715842.git.dsterba@suse.com/
V4: https://lore.kernel.org/linux-crypto/[email protected]/
V5: https://lore.kernel.org/linux-crypto/[email protected]/

David Sterba (2):
crypto: add blake2b generic implementation
crypto: add test vectors for blake2b

 crypto/Kconfig           |  17 ++
 crypto/Makefile          |   1 +
 crypto/blake2b_generic.c | 413 +++++++++++++++++++++++++++++++++++++++
 crypto/testmgr.c         |  28 +++
 crypto/testmgr.h         | 307 +++++++++++++++++++++++++++++
 include/crypto/blake2b.h |  46 +++++
 6 files changed, 812 insertions(+)
 create mode 100644 crypto/blake2b_generic.c
 create mode 100644 include/crypto/blake2b.h
Signature · 1 line
2.23.0
#1 lore raw

PATCH v6 1/2 crypto: add blake2b generic implementation

The patch brings support of several BLAKE2 variants (2b with various digest lengths). The keyed digest is supported, using tfm->setkey call. The in-tree user will be btrfs (for checksumming), we're going to use the BLAKE2b-256 variant.

The code is reference implementation taken from the official sources and modified in terms of kernel coding style (whitespace, comments, uintXX_t -> uXX types, removed unused prototypes and #ifdefs, removed testing code, changed secure_zero_memory -> memzero_explicit, used own helpers for unaligned reads/writes and rotations).

Further changes removed sanity checks of key length or output size, these values are verified in the crypto API callbacks or are hardcoded in shash_alg and not exposed to users.

---
 crypto/Kconfig           |  17 ++
 crypto/Makefile          |   1 +
 crypto/blake2b_generic.c | 413 +++++++++++++++++++++++++++++++++++++++
 include/crypto/blake2b.h |  46 +++++
 4 files changed, 477 insertions(+)
 create mode 100644 crypto/blake2b_generic.c
 create mode 100644 include/crypto/blake2b.h
crypto/Kconfig +17 -0
index ad86463de715..d37eba22f436 100644
@@ -610,6 +610,23 @@ config CRYPTO_XXHASH
 	  xxHash non-cryptographic hash algorithm. Extremely fast, working at
 	  speeds close to RAM limits.
 
+config CRYPTO_BLAKE2B
+	tristate "BLAKE2b digest algorithm"
+	select CRYPTO_HASH
+	help
+	  Implementation of cryptographic hash function BLAKE2b (or just BLAKE2),
+	  optimized for 64bit platforms and can produce digests of any size
+	  between 1 to 64.  The keyed hash is also implemented.
+
+	  This module provides the following algorithms:
+
+	  - blake2b-160
+	  - blake2b-256
+	  - blake2b-384
+	  - blake2b-512
+
+	  See https://blake2.net for further information.
+
 config CRYPTO_CRCT10DIF
 	tristate "CRCT10DIF algorithm"
 	select CRYPTO_HASH
crypto/Makefile +1 -0
index 0d2cdd523fd9..dc010b1a8e41 100644
@@ -74,6 +74,7 @@ obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o
 obj-$(CONFIG_CRYPTO_WP512) += wp512.o
 CFLAGS_wp512.o := $(call cc-option,-fno-schedule-insns)  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149
 obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o
+obj-$(CONFIG_CRYPTO_BLAKE2B) += blake2b_generic.o
 obj-$(CONFIG_CRYPTO_GF128MUL) += gf128mul.o
 obj-$(CONFIG_CRYPTO_ECB) += ecb.o
 obj-$(CONFIG_CRYPTO_CBC) += cbc.o
crypto/blake2b_generic.c new file +413 -0

413 lines not on this page · raw message

include/crypto/blake2b.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.23.0
#2 lore raw

PATCH v6 2/2 crypto: add test vectors for blake2b

In reply to David Sterba

Test vectors for blake2b with various digest sizes. As the algorithm is the same up to the digest calculation, the key and input data length is distributed in a way that tests all combinanions of the two over the digest sizes.

Based on the suggestion from Eric, the following input sizes are tested [0, 1, 7, 15, 64, 247, 256], where blake2b blocksize is 128, so the padded and the non-padded input buffers are tested.

          blake2b-160  blake2b-256  blake2b-384  blake2b-512
         ---------------------------------------------------
len=0   | klen=0       klen=1       klen=32      klen=64
len=1   | klen=32      klen=64      klen=0       klen=1
len=7   | klen=64      klen=0       klen=1       klen=32
len=15  | klen=1       klen=32      klen=64      klen=0
len=64  | klen=0       klen=1       klen=32      klen=64
len=247 | klen=32      klen=64      klen=0       klen=1
len=256 | klen=64      klen=0       klen=1       klen=32

Where key:

- klen=0: empty key
- klen=1: 1 byte value 0x42, 'B'
- klen=32: first 32 bytes of the default key, sequence 00..1f
- klen=64: default key, sequence 00..3f

The unkeyed vectors are ordered before keyed, as this is required by testmgr.

---
 crypto/testmgr.c |  28 +++++
 crypto/testmgr.h | 307 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 335 insertions(+)
crypto/testmgr.c +28 -0
index c39e39e55dc2..0f956780a673 100644
@@ -4022,6 +4022,34 @@ static const struct alg_test_desc alg_test_descs[] = {
 		.alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
 		.test = alg_test_null,
 		.fips_allowed = 1,
+	}, {
+		.alg = "blake2b-160",
+		.test = alg_test_hash,
+		.fips_allowed = 0,
+		.suite = {
+			.hash = __VECS(blake2b_160_tv_template)
+		}
+	}, {
+		.alg = "blake2b-256",
+		.test = alg_test_hash,
+		.fips_allowed = 0,
+		.suite = {
+			.hash = __VECS(blake2b_256_tv_template)
+		}
+	}, {
+		.alg = "blake2b-384",
+		.test = alg_test_hash,
+		.fips_allowed = 0,
+		.suite = {
+			.hash = __VECS(blake2b_384_tv_template)
+		}
+	}, {
+		.alg = "blake2b-512",
+		.test = alg_test_hash,
+		.fips_allowed = 0,
+		.suite = {
+			.hash = __VECS(blake2b_512_tv_template)
+		}
 	}, {
 		.alg = "cbc(aes)",
 		.test = alg_test_skcipher,
crypto/testmgr.h +307 -0

311 lines not on this page · raw message

Signature · 1 line
2.23.0
Tested-by on the v6 series
#3 lore raw
In reply to David Sterba
Quoted 5 lines · David Sterba

The patchset adds blake2b reference implementation and test vectors.

V6:

Quoted 9 lines

Patch 2/2: test vectors fixed to actually match the proposed table of key and plaintext combinations. I shamelessly copied the test vector value format that Ard uses for the blake2s test vectors. The array blake2b_ordered_sequence can be shared between 2s and 2b but as the patchsets go separate, unification would have to happen once both are merged.

Tested on x86_64 with KASAN and SLUB_DEBUG.

Quoted 10 lines

V1: https://lore.kernel.org/linux-crypto/[email protected]/
V2: https://lore.kernel.org/linux-crypto/e31c2030fcfa7f409b2c81adf8f179a8a55a584a.1570184333.git.dsterba@suse.com/
V3: https://lore.kernel.org/linux-crypto/e7f46def436c2c705c0b2cac3324f817efa4717d.1570715842.git.dsterba@suse.com/
V4: https://lore.kernel.org/linux-crypto/[email protected]/
V5: https://lore.kernel.org/linux-crypto/[email protected]/

David Sterba (2):
crypto: add blake2b generic implementation
crypto: add test vectors for blake2b

Quoted 6 lines
 crypto/Kconfig           |  17 ++
 crypto/Makefile          |   1 +
 crypto/blake2b_generic.c | 413 +++++++++++++++++++++++++++++++++++++++
 crypto/testmgr.c         |  28 +++
 crypto/testmgr.h         | 307 +++++++++++++++++++++++++++++
 include/crypto/blake2b.h |  46 +++++

Final nit: do we need this header file at all? Could we move the contents into crypto/blake2b_generic.c? Or is the btrfs code going to #include it?

Quoted 7 lines
 6 files changed, 812 insertions(+)
 create mode 100644 crypto/blake2b_generic.c
 create mode 100644 include/crypto/blake2b.h

--
2.23.0

#4 lore raw
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line · David Sterba

Tested on x86_64 with KASAN and SLUB_DEBUG.

Thanks!

Quoted 10 lines
Quoted 6 lines
 crypto/Kconfig           |  17 ++
 crypto/Makefile          |   1 +
 crypto/blake2b_generic.c | 413 +++++++++++++++++++++++++++++++++++++++
 crypto/testmgr.c         |  28 +++
 crypto/testmgr.h         | 307 +++++++++++++++++++++++++++++
 include/crypto/blake2b.h |  46 +++++

Final nit: do we need this header file at all? Could we move the contents into crypto/blake2b_generic.c? Or is the btrfs code going to #include it?

The only interesting part for btrfs would be the definition of BLAKE2B_256_DIGEST_SIZE instead of hardcoding the number. As the patches go through separate trees I have to use the hardcoded number anyway.

The header would make sense for the library version of blake2b, similar to what the wireguard blake2s patches do, but there's no need for that right now so I guess the header can be folded to .c.