BLAKE2b library API
Subsystems (8)
Eric Biggers
maintainercover BLAKE2b library API
This series can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git blake2b-lib-v1
This series adds BLAKE2b support to lib/crypto/ and reimplements the blake2b-* crypto_shash algorithms on top of it.
To prepare for that, patches 1-4 clean up the BLAKE2s library code a bit, and patch 5 adds some missing 64-bit byteorder helper functions. Patches 6-8 add the BLAKE2b library API (closely mirroring the BLAKE2s one), and patch 9 makes crypto_shash use it. As usual, the library APIs are documented (with kerneldoc) and tested (with KUnit).
With that done, all of btrfs's checksum algorithms have library APIs. So patch 10 converts btrfs to use the library APIs instead of shash. This has quite a few benefits, as detailed in that patch.
Patches 1-9 are targeting libcrypto-next for 6.19. Patch 10 can go through the btrfs tree later.
Eric Biggers (10):
lib/crypto: blake2s: Adjust parameter order of blake2s() lib/crypto: blake2s: Rename blake2s_state to blake2s_ctx lib/crypto: blake2s: Drop excessive const & rename block => data lib/crypto: blake2s: Document the BLAKE2s library API
byteorder: Add le64_to_cpu_array() and cpu_to_le64_array() lib/crypto: blake2b: Add BLAKE2b library functions lib/crypto: arm/blake2b: Migrate optimized code into library lib/crypto: tests: Add KUnit tests for BLAKE2b
crypto: blake2b - Reimplement using library API
btrfs: switch to library APIs for checksums
arch/arm/crypto/Kconfig | 16 -
arch/arm/crypto/Makefile | 2 -
arch/arm/crypto/blake2b-neon-glue.c | 104 ------
crypto/Kconfig | 3 +-
crypto/Makefile | 3 +-
crypto/blake2b.c | 111 ++++++
crypto/blake2b_generic.c | 192 ----------
crypto/testmgr.c | 4 +
drivers/char/random.c | 6 +-
drivers/net/wireguard/cookie.c | 18 +-
drivers/net/wireguard/noise.c | 32 +-
fs/btrfs/Kconfig | 8 +-
fs/btrfs/compression.c | 1 -
fs/btrfs/disk-io.c | 68 +---
fs/btrfs/file-item.c | 4 -
fs/btrfs/fs.c | 97 ++++-
fs/btrfs/fs.h | 23 +-
fs/btrfs/inode.c | 13 +-
fs/btrfs/scrub.c | 16 +-
fs/btrfs/super.c | 4 -
fs/btrfs/sysfs.c | 6 +-
include/crypto/blake2b.h | 143 ++++++--
include/crypto/blake2s.h | 126 +++++--
include/crypto/internal/blake2b.h | 101 ------
include/linux/byteorder/generic.h | 16 +
lib/crypto/Kconfig | 11 +
lib/crypto/Makefile | 10 +
.../crypto/arm}/blake2b-neon-core.S | 29 +-
lib/crypto/arm/blake2b.h | 41 +++
lib/crypto/arm/blake2s-core.S | 14 +-
lib/crypto/arm/blake2s.h | 4 +-
lib/crypto/blake2b.c | 174 +++++++++
lib/crypto/blake2s.c | 66 ++--
lib/crypto/tests/Kconfig | 9 +
lib/crypto/tests/Makefile | 1 +
lib/crypto/tests/blake2b-testvecs.h | 342 ++++++++++++++++++
lib/crypto/tests/blake2b_kunit.c | 133 +++++++
lib/crypto/tests/blake2s_kunit.c | 39 +-
lib/crypto/x86/blake2s.h | 22 +-
scripts/crypto/gen-hash-testvecs.py | 29 +-
40 files changed, 1333 insertions(+), 708 deletions(-)
delete mode 100644 arch/arm/crypto/blake2b-neon-glue.c
create mode 100644 crypto/blake2b.c
delete mode 100644 crypto/blake2b_generic.c
delete mode 100644 include/crypto/internal/blake2b.h
rename {arch/arm/crypto => lib/crypto/arm}/blake2b-neon-core.S (94%)
create mode 100644 lib/crypto/arm/blake2b.h
create mode 100644 lib/crypto/blake2b.c
create mode 100644 lib/crypto/tests/blake2b-testvecs.h
create mode 100644 lib/crypto/tests/blake2b_kunit.cbase-commit: 123fa1574bccee87da735d13e89c931e88288b40
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 1/10 lib/crypto: blake2s: Adjust parameter order of blake2s()
Reorder the parameters of blake2s() from (out, in, key, outlen, inlen, keylen) to (key, keylen, in, inlen, out, outlen).
This aligns BLAKE2s with the common conventions of pairing buffers and their lengths, and having outputs follow inputs. This is widely used elsewhere in lib/crypto/ and crypto/, and even elsewhere in the BLAKE2s code itself such as blake2s_init_key() and blake2s_final(). So blake2s() was a bit of an exception.
Notably, this results in the same order as hmac_*_usingrawkey().
Note that since the type signature changed, it's not possible for a blake2s() call site to be silently missed.
--- drivers/char/random.c | 4 ++-- drivers/net/wireguard/cookie.c | 4 ++-- drivers/net/wireguard/noise.c | 4 ++-- include/crypto/blake2s.h | 6 +++--- lib/crypto/tests/blake2s_kunit.c | 16 ++++++++-------- 5 files changed, 17 insertions(+), 17 deletions(-)
drivers/char/random.c +2 -2
@@ -699,21 +699,21 @@ static void extract_entropy(void *buf, size_t len)/* seed = HASHPRF(last_key, entropy_input) */ blake2s_final (&input_pool.hash, seed);/* next_key = HASHPRF(seed, RDSEED || 0) */ block.counter =0 ; -blake2s (next_key, (u8 *)&block, seed,sizeof (next_key),sizeof (block),sizeof (seed)); +blake2s (seed,sizeof (seed), (const u8 *)&block,sizeof (block), next_key,sizeof (next_key));blake2s_init_key (&input_pool.hash,BLAKE2S_HASH_SIZE , next_key,sizeof (next_key));spin_unlock_irqrestore (&input_pool.lock, flags);memzero_explicit (next_key,sizeof (next_key));while (len) { i =min_t (size_t, len,BLAKE2S_HASH_SIZE );/* output = HASHPRF(seed, RDSEED || ++counter) */ ++block.counter; -blake2s (buf, (u8 *)&block, seed, i,sizeof (block),sizeof (seed)); +blake2s (seed,sizeof (seed), (const u8 *)&block,sizeof (block), buf, i); len -= i; buf += i; }memzero_explicit (seed,sizeof (seed));
drivers/net/wireguard/cookie.c +2 -2
@@ -75,19 +75,19 @@ void wg_cookie_init(struct cookie *cookie)static void compute_mac1 (u8 mac1[COOKIE_LEN ],const void *message,size_t len,const u8 key[NOISE_SYMMETRIC_KEY_LEN ]) { len = len -sizeof (struct message_macs ) + offsetof(struct message_macs , mac1); -blake2s (mac1, message, key,COOKIE_LEN , len,NOISE_SYMMETRIC_KEY_LEN ); +blake2s (key,NOISE_SYMMETRIC_KEY_LEN , message, len, mac1,COOKIE_LEN ); }static void compute_mac2 (u8 mac2[COOKIE_LEN ],const void *message,size_t len,const u8 cookie[COOKIE_LEN ]) { len = len -sizeof (struct message_macs ) + offsetof(struct message_macs , mac2); -blake2s (mac2, message, cookie,COOKIE_LEN , len,COOKIE_LEN ); +blake2s (cookie,COOKIE_LEN , message, len, mac2,COOKIE_LEN ); }static void make_cookie (u8 cookie[COOKIE_LEN ],struct sk_buff *skb,struct cookie_checker *checker) {
drivers/net/wireguard/noise.c +2 -2
@@ -33,12 +33,12 @@ static atomic64_t keypair_counter = ATOMIC64_INIT(0);void __init wg_noise_init (void ) {struct blake2s_state blake; -blake2s (handshake_init_chaining_key, handshake_name,NULL , -NOISE_HASH_LEN ,sizeof (handshake_name),0 ); +blake2s (NULL ,0 , handshake_name,sizeof (handshake_name), + handshake_init_chaining_key,NOISE_HASH_LEN );blake2s_init (&blake,NOISE_HASH_LEN );blake2s_update (&blake, handshake_init_chaining_key,NOISE_HASH_LEN );blake2s_update (&blake, identifier_name,sizeof (identifier_name));blake2s_final (&blake, handshake_init_hash); }
include/crypto/blake2s.h +3 -3
@@ -84,13 +84,13 @@ static inline void blake2s_init_key(struct blake2s_state *state, }void blake2s_update (struct blake2s_state *state,const u8 *in,size_t inlen);void blake2s_final (struct blake2s_state *state,u8 *out); -static inline void blake2s (u8 *out,const u8 *in,const u8 *key, -const size_t outlen,const size_t inlen, -const size_t keylen) +static inline void blake2s (const u8 *key,const size_t keylen, +const u8 *in,const size_t inlen, +u8 *out,const size_t outlen) {struct blake2s_state state; WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen || outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
lib/crypto/tests/blake2s_kunit.c +8 -8
@@ -12,11 +12,11 @@*/ static void blake2s_default (const u8 *data,size_t len,u8 out[BLAKE2S_HASH_SIZE ]) { -blake2s (out, data,NULL ,BLAKE2S_HASH_SIZE , len,0 ); +blake2s (NULL ,0 , data, len, out,BLAKE2S_HASH_SIZE ); }static void blake2s_init_default (struct blake2s_state *state) {blake2s_init (state,BLAKE2S_HASH_SIZE );
@@ -50,11 +50,11 @@ static void test_blake2s_all_key_and_hash_lens(struct kunit *test)rand_bytes_seeded_from_len (data, data_len);blake2s_init (&main_state,BLAKE2S_HASH_SIZE );for (int key_len =0 ; key_len <=BLAKE2S_KEY_SIZE ; key_len++) {rand_bytes_seeded_from_len (key, key_len);for (int out_len =1 ; out_len <=BLAKE2S_HASH_SIZE ; out_len++) { -blake2s (hash, data, key, out_len, data_len, key_len); +blake2s (key, key_len, data, data_len, hash, out_len);blake2s_update (&main_state, hash, out_len); } }blake2s_final (&main_state, main_hash); KUNIT_ASSERT_MEMEQ(test, main_hash, blake2s_keyed_testvec_consolidated,
@@ -78,14 +78,14 @@ static void test_blake2s_with_guarded_key_buf(struct kunit *test)struct blake2s_state state;rand_bytes (key, key_len);memcpy (guarded_key, key, key_len); -blake2s (hash1, test_buf, key, -BLAKE2S_HASH_SIZE , data_len, key_len); -blake2s (hash2, test_buf, guarded_key, -BLAKE2S_HASH_SIZE , data_len, key_len); +blake2s (key, key_len, test_buf, data_len, + hash1,BLAKE2S_HASH_SIZE ); +blake2s (guarded_key, key_len, test_buf, data_len, + hash2,BLAKE2S_HASH_SIZE );KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2S_HASH_SIZE );blake2s_init_key (&state,BLAKE2S_HASH_SIZE , guarded_key, key_len);blake2s_update (&state, test_buf, data_len);
@@ -105,12 +105,12 @@ static void test_blake2s_with_guarded_out_buf(struct kunit *test)rand_bytes (test_buf, data_len);for (int out_len =1 ; out_len <=BLAKE2S_HASH_SIZE ; out_len++) {u8 hash[BLAKE2S_HASH_SIZE ];u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len]; -blake2s (hash, test_buf,NULL , out_len, data_len,0 ); -blake2s (guarded_hash, test_buf,NULL , out_len, data_len,0 ); +blake2s (NULL ,0 , test_buf, data_len, hash, out_len); +blake2s (NULL ,0 , test_buf, data_len, guarded_hash, out_len);KUNIT_ASSERT_MEMEQ (test, hash, guarded_hash, out_len); } }static struct kunit_case blake2s_test_cases[] = {
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 2/10 lib/crypto: blake2s: Rename blake2s_state to blake2s_ctx
In reply to Eric BiggersFor consistency with the SHA-1, SHA-2, SHA-3 (in development), and MD5 library APIs, rename blake2s_state to blake2s_ctx.
As a refresher, the ctx name:
- Is a bit shorter.
- Avoids confusion with the compression function state, which is also often called the state (but is just part of the full context).
- Is consistent with OpenSSL.
Not a big deal, of course. But consistency is nice. With a BLAKE2b library API about to be added, this is a convenient time to update this.
--- drivers/char/random.c | 2 +- drivers/net/wireguard/cookie.c | 14 ++++---- drivers/net/wireguard/noise.c | 28 +++++++-------- include/crypto/blake2s.h | 59 ++++++++++++++++---------------- lib/crypto/arm/blake2s-core.S | 10 +++--- lib/crypto/arm/blake2s.h | 4 +-- lib/crypto/blake2s.c | 58 +++++++++++++++---------------- lib/crypto/tests/blake2s_kunit.c | 23 ++++++------- lib/crypto/x86/blake2s.h | 12 +++---- 9 files changed, 104 insertions(+), 106 deletions(-)
drivers/char/random.c +1 -1
@@ -634,11 +634,11 @@ enum {POOL_READY_BITS =POOL_BITS ,/* When crng_init->CRNG_READY */ POOL_EARLY_BITS =POOL_READY_BITS /2 /* When crng_init->CRNG_EARLY */ };static struct { -struct blake2s_state hash; +struct blake2s_ctx hash;spinlock_t lock;unsigned int init_bits; } input_pool = { .hash.h = {BLAKE2S_IV0 ^ (0x01010000 |BLAKE2S_HASH_SIZE ),BLAKE2S_IV1 ,BLAKE2S_IV2 ,BLAKE2S_IV3 ,BLAKE2S_IV4 ,
drivers/net/wireguard/cookie.c +7 -7
@@ -31,11 +31,11 @@ static const u8 cookie_key_label[COOKIE_KEY_LABEL_LEN] __nonstring = "cookie--";static void precompute_key (u8 key[NOISE_SYMMETRIC_KEY_LEN ],const u8 pubkey[NOISE_PUBLIC_KEY_LEN ],const u8 label[COOKIE_KEY_LABEL_LEN ]) { -struct blake2s_state blake; +struct blake2s_ctx blake;blake2s_init (&blake,NOISE_SYMMETRIC_KEY_LEN );blake2s_update (&blake, label,COOKIE_KEY_LABEL_LEN );blake2s_update (&blake, pubkey,NOISE_PUBLIC_KEY_LEN );blake2s_final (&blake, key);
@@ -89,11 +89,11 @@ static void compute_mac2(u8 mac2[COOKIE_LEN], const void *message, size_t len, }static void make_cookie (u8 cookie[COOKIE_LEN ],struct sk_buff *skb,struct cookie_checker *checker) { -struct blake2s_state state; +struct blake2s_ctx blake;if (wg_birthdate_has_expired (checker->secret_birthdate,COOKIE_SECRET_MAX_AGE )) {down_write (&checker->secret_lock); checker->secret_birthdate =ktime_get_coarse_boottime_ns ();
@@ -101,19 +101,19 @@ static void make_cookie(u8 cookie[COOKIE_LEN], struct sk_buff *skb,up_write (&checker->secret_lock); }down_read (&checker->secret_lock); -blake2s_init_key (&state,COOKIE_LEN , checker->secret,NOISE_HASH_LEN ); +blake2s_init_key (&blake,COOKIE_LEN , checker->secret,NOISE_HASH_LEN );if (skb->protocol ==htons (ETH_P_IP )) -blake2s_update (&state, (u8 *)&ip_hdr (skb)->saddr, +blake2s_update (&blake, (u8 *)&ip_hdr (skb)->saddr,sizeof (struct in_addr ));else if (skb->protocol ==htons (ETH_P_IPV6 )) -blake2s_update (&state, (u8 *)&ipv6_hdr (skb)->saddr, +blake2s_update (&blake, (u8 *)&ipv6_hdr (skb)->saddr,sizeof (struct in6_addr )); -blake2s_update (&state, (u8 *)&udp_hdr (skb)->source,sizeof (__be16)); -blake2s_final (&state, cookie); +blake2s_update (&blake, (u8 *)&udp_hdr (skb)->source,sizeof (__be16)); +blake2s_final (&blake, cookie);up_read (&checker->secret_lock); } enum cookie_mac_state wg_cookie_validate_packet(struct cookie_checker *checker,
drivers/net/wireguard/noise.c +14 -14
@@ -31,11 +31,11 @@ static u8 handshake_init_hash[NOISE_HASH_LEN] __ro_after_init;static u8 handshake_init_chaining_key[NOISE_HASH_LEN ]__ro_after_init ;static atomic64_t keypair_counter =ATOMIC64_INIT (0 );void __init wg_noise_init (void ) { -struct blake2s_state blake; +struct blake2s_ctx blake;blake2s (NULL ,0 , handshake_name,sizeof (handshake_name), handshake_init_chaining_key,NOISE_HASH_LEN );blake2s_init (&blake,NOISE_HASH_LEN );blake2s_update (&blake, handshake_init_chaining_key,NOISE_HASH_LEN );
@@ -302,37 +302,37 @@ void wg_noise_set_static_identity_private_key( static_identity->static_public, private_key); }static void hmac (u8 *out,const u8 *in,const u8 *key,const size_t inlen,const size_t keylen) { -struct blake2s_state state; +struct blake2s_ctx blake;u8 x_key[BLAKE2S_BLOCK_SIZE ]__aligned(__alignof__(u32)) = {0 };u8 i_hash[BLAKE2S_HASH_SIZE ]__aligned(__alignof__(u32)) ;int i;if (keylen >BLAKE2S_BLOCK_SIZE ) { -blake2s_init (&state,BLAKE2S_HASH_SIZE ); -blake2s_update (&state, key, keylen); -blake2s_final (&state, x_key); +blake2s_init (&blake,BLAKE2S_HASH_SIZE ); +blake2s_update (&blake, key, keylen); +blake2s_final (&blake, x_key); }else memcpy (x_key, key, keylen);for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; ++i) x_key[i] ^=0x36 ; -blake2s_init (&state,BLAKE2S_HASH_SIZE ); -blake2s_update (&state, x_key,BLAKE2S_BLOCK_SIZE ); -blake2s_update (&state, in, inlen); -blake2s_final (&state, i_hash); +blake2s_init (&blake,BLAKE2S_HASH_SIZE ); +blake2s_update (&blake, x_key,BLAKE2S_BLOCK_SIZE ); +blake2s_update (&blake, in, inlen); +blake2s_final (&blake, i_hash);for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; ++i) x_key[i] ^=0x5c ^0x36 ; -blake2s_init (&state,BLAKE2S_HASH_SIZE ); -blake2s_update (&state, x_key,BLAKE2S_BLOCK_SIZE ); -blake2s_update (&state, i_hash,BLAKE2S_HASH_SIZE ); -blake2s_final (&state, i_hash); +blake2s_init (&blake,BLAKE2S_HASH_SIZE ); +blake2s_update (&blake, x_key,BLAKE2S_BLOCK_SIZE ); +blake2s_update (&blake, i_hash,BLAKE2S_HASH_SIZE ); +blake2s_final (&blake, i_hash);memcpy (out, i_hash,BLAKE2S_HASH_SIZE );memzero_explicit (x_key,BLAKE2S_BLOCK_SIZE );memzero_explicit (i_hash,BLAKE2S_HASH_SIZE ); }
@@ -429,11 +429,11 @@ static bool __must_check mix_precomputed_dh(u8 chaining_key[NOISE_HASH_LEN],return true; }static void mix_hash (u8 hash[NOISE_HASH_LEN ],const u8 *src,size_t src_len) { -struct blake2s_state blake; +struct blake2s_ctx blake;blake2s_init (&blake,NOISE_HASH_LEN );blake2s_update (&blake, hash,NOISE_HASH_LEN );blake2s_update (&blake, src, src_len);blake2s_final (&blake, hash);
include/crypto/blake2s.h +29 -30
104 lines not on this page · raw message
lib/crypto/arm/blake2s-core.S +5 -5
@@ -168,24 +168,24 @@ \s12, \s13, \s14, \s15 __strd r10, r11, sp, 20 .endm // -// void blake2s_compress(struct blake2s_state *state, +// void blake2s_compress(struct blake2s_ctx *ctx, // const u8 *block, size_t nblocks, u32 inc); // -// Only the first three fields of struct blake2s_state are used: +// Only the first three fields of struct blake2s_ctx are used: // u32 h[8]; (inout) // u32 t[2]; (inout) // u32 f[2]; (in) // .align 5 ENTRY(blake2s_compress) push {r0-r2,r4-r11,lr} // keep this an even number .Lnext_block: - // r0 is 'state' + // r0 is 'ctx' // r1 is 'block' // r3 is 'inc' // Load and increment the counter t[0..1]. __ldrd r10, r11, r0, 32
@@ -209,11 +209,11 @@ ENTRY(blake2s_compress) .Lcopy_block_done: str r1, [sp, #68] // Update message pointer // Calculate v[8..15]. Push v[9..15] onto the stack, and leave space // for spilling v[8..9]. Leave v[8..9] in r8-r9. - mov r14, r0 // r14 = state + mov r14, r0 // r14 = ctx adr r12, .Lblake2s_IV ldmia r12!, {r8-r9} // load IV[0..1] __ldrd r0, r1, r14, 40 // load f[0..1] ldm r12, {r2-r7} // load IV[3..7] eor r4, r4, r10 // v[12] = IV[4] ^ t[0]
@@ -273,11 +273,11 @@ ENTRY(blake2s_compress) stm r14, {r0-r3} // store new h[4..7] // Advance to the next block, if there is one. Note that if there are // multiple blocks, then 'inc' (the counter increment amount) must be // 64. So we can simply set it to 64 without re-loading it. - ldm sp, {r0, r1, r2} // load (state, block, nblocks) + ldm sp, {r0, r1, r2} // load (ctx, block, nblocks) mov r3, #64 // set 'inc' subs r2, r2, #1 // nblocks-- str r2, [sp, #8] bne .Lnext_block // nblocks != 0?
lib/crypto/arm/blake2s.h +2 -2
@@ -1,5 +1,5 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ /* defined in blake2s-core.S */ -void blake2s_compress (struct blake2s_state *state,const u8 *block, -size_t nblocks,u32 inc); +void blake2s_compress (struct blake2s_ctx *ctx, +const u8 *block,size_t nblocks,u32 inc);
lib/crypto/blake2s.c +29 -29
130 lines not on this page · raw message
lib/crypto/tests/blake2s_kunit.c +11 -12
@@ -15,21 +15,21 @@ static void blake2s_default(const u8 *data, size_t len, u8 out[BLAKE2S_HASH_SIZE]) {blake2s (NULL ,0 , data, len, out,BLAKE2S_HASH_SIZE ); } -static void blake2s_init_default (struct blake2s_state *state) +static void blake2s_init_default (struct blake2s_ctx *ctx) { -blake2s_init (state,BLAKE2S_HASH_SIZE ); +blake2s_init (ctx,BLAKE2S_HASH_SIZE ); }/* * Generate the HASH_KUNIT_CASES using hash-test-template.h. These test BLAKE2s * with a key length of 0 and a hash length of BLAKE2S_HASH_SIZE. */ #define HASH blake2s_default -#define HASH_CTX blake2s_state +#define HASH_CTX blake2s_ctx#define HASH_SIZE BLAKE2S_HASH_SIZE#define HASH_INIT blake2s_init_default#define HASH_UPDATE blake2s_update#define HASH_FINAL blake2s_final#include "hash-test-template.h"
@@ -42,23 +42,23 @@ static void test_blake2s_all_key_and_hash_lens(struct kunit *test) {const size_t data_len =100 ;u8 *data = &test_buf[0 ];u8 *key = data + data_len;u8 *hash = key +BLAKE2S_KEY_SIZE ; -struct blake2s_state main_state; +struct blake2s_ctx main_ctx;u8 main_hash[BLAKE2S_HASH_SIZE ];rand_bytes_seeded_from_len (data, data_len); -blake2s_init (&main_state,BLAKE2S_HASH_SIZE ); +blake2s_init (&main_ctx,BLAKE2S_HASH_SIZE );for (int key_len =0 ; key_len <=BLAKE2S_KEY_SIZE ; key_len++) {rand_bytes_seeded_from_len (key, key_len);for (int out_len =1 ; out_len <=BLAKE2S_HASH_SIZE ; out_len++) {blake2s (key, key_len, data, data_len, hash, out_len); -blake2s_update (&main_state, hash, out_len); +blake2s_update (&main_ctx, hash, out_len); } } -blake2s_final (&main_state, main_hash); +blake2s_final (&main_ctx, main_hash);KUNIT_ASSERT_MEMEQ (test, main_hash, blake2s_keyed_testvec_consolidated,BLAKE2S_HASH_SIZE ); }/*
@@ -73,25 +73,24 @@ static void test_blake2s_with_guarded_key_buf(struct kunit *test)for (int key_len =0 ; key_len <=BLAKE2S_KEY_SIZE ; key_len++) {u8 key[BLAKE2S_KEY_SIZE ];u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len];u8 hash1[BLAKE2S_HASH_SIZE ];u8 hash2[BLAKE2S_HASH_SIZE ]; -struct blake2s_state state; +struct blake2s_ctx ctx;rand_bytes (key, key_len);memcpy (guarded_key, key, key_len);blake2s (key, key_len, test_buf, data_len, hash1,BLAKE2S_HASH_SIZE );blake2s (guarded_key, key_len, test_buf, data_len, hash2,BLAKE2S_HASH_SIZE );KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2S_HASH_SIZE ); -blake2s_init_key (&state,BLAKE2S_HASH_SIZE , - guarded_key, key_len); -blake2s_update (&state, test_buf, data_len); -blake2s_final (&state, hash2); +blake2s_init_key (&ctx,BLAKE2S_HASH_SIZE , guarded_key, key_len); +blake2s_update (&ctx, test_buf, data_len); +blake2s_final (&ctx, hash2);KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2S_HASH_SIZE ); } }/*
lib/crypto/x86/blake2s.h +6 -6
@@ -9,40 +9,40 @@#include <asm/simd.h> #include <linux/jump_label.h> #include <linux/kernel.h> #include <linux/sizes.h> -asmlinkage void blake2s_compress_ssse3(struct blake2s_state *state, +asmlinkage void blake2s_compress_ssse3(struct blake2s_ctx *ctx,const u8 *block,const size_t nblocks,const u32 inc); -asmlinkage void blake2s_compress_avx512(struct blake2s_state *state, +asmlinkage void blake2s_compress_avx512(struct blake2s_ctx *ctx,const u8 *block,const size_t nblocks,const u32 inc);static __ro_after_init DEFINE_STATIC_KEY_FALSE (blake2s_use_ssse3);static __ro_after_init DEFINE_STATIC_KEY_FALSE (blake2s_use_avx512); -static void blake2s_compress (struct blake2s_state *state,const u8 *block, +static void blake2s_compress (struct blake2s_ctx *ctx,const u8 *block,size_t nblocks,const u32 inc) {/* SIMD disables preemption, so relax after processing each page. */ BUILD_BUG_ON (SZ_4K /BLAKE2S_BLOCK_SIZE <8 );if (!static_branch_likely (&blake2s_use_ssse3) || !may_use_simd ()) { -blake2s_compress_generic (state, block, nblocks, inc); +blake2s_compress_generic (ctx, block, nblocks, inc);return ; }do {const size_t blocks =min_t (size_t, nblocks,SZ_4K /BLAKE2S_BLOCK_SIZE );kernel_fpu_begin ();if (static_branch_likely (&blake2s_use_avx512)) -blake2s_compress_avx512 (state, block, blocks, inc); +blake2s_compress_avx512 (ctx, block, blocks, inc);else -blake2s_compress_ssse3 (state, block, blocks, inc); +blake2s_compress_ssse3 (ctx, block, blocks, inc);kernel_fpu_end (); nblocks -= blocks; block += blocks *BLAKE2S_BLOCK_SIZE ; }while (nblocks);
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 3/10 lib/crypto: blake2s: Drop excessive const & rename block => data
In reply to Eric BiggersA couple more small cleanups to the BLAKE2s code before these things get propagated into the BLAKE2b code:
- Drop 'const' from some non-pointer function parameters. It was a bit excessive and not conventional.
- Rename 'block' argument of blake2s_compress*() to 'data'. This is for consistency with the SHA-* code, and also to avoid the implication that it points to a singular "block".
No functional changes.
--- include/crypto/blake2s.h | 13 ++++++------- lib/crypto/arm/blake2s-core.S | 6 +++--- lib/crypto/arm/blake2s.h | 2 +- lib/crypto/blake2s.c | 12 ++++++------ lib/crypto/x86/blake2s.h | 18 ++++++++---------- 5 files changed, 24 insertions(+), 27 deletions(-)
include/crypto/blake2s.h +6 -7
@@ -65,31 +65,30 @@ static inline void __blake2s_init(struct blake2s_ctx *ctx, size_t outlen,memset (&ctx->buf[keylen],0 ,BLAKE2S_BLOCK_SIZE - keylen); ctx->buflen =BLAKE2S_BLOCK_SIZE ; } } -static inline void blake2s_init (struct blake2s_ctx *ctx,const size_t outlen) +static inline void blake2s_init (struct blake2s_ctx *ctx,size_t outlen) {__blake2s_init (ctx, outlen,NULL ,0 ); } -static inline void blake2s_init_key (struct blake2s_ctx *ctx, -const size_t outlen,const void *key, -const size_t keylen) +static inline void blake2s_init_key (struct blake2s_ctx *ctx,size_t outlen, +const void *key,size_t keylen) {WARN_ON (IS_ENABLED (DEBUG ) && (!outlen || outlen >BLAKE2S_HASH_SIZE || !key || !keylen || keylen >BLAKE2S_KEY_SIZE ));__blake2s_init (ctx, outlen, key, keylen); }void blake2s_update (struct blake2s_ctx *ctx,const u8 *in,size_t inlen);void blake2s_final (struct blake2s_ctx *ctx,u8 *out); -static inline void blake2s (const u8 *key,const size_t keylen, -const u8 *in,const size_t inlen, -u8 *out,const size_t outlen) +static inline void blake2s (const u8 *key,size_t keylen, +const u8 *in,size_t inlen, +u8 *out,size_t outlen) {struct blake2s_ctx ctx; WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen || outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
lib/crypto/arm/blake2s-core.S +3 -3
@@ -169,11 +169,11 @@ __strd r10, r11, sp, 20 .endm // // void blake2s_compress(struct blake2s_ctx *ctx, -// const u8 *block, size_t nblocks, u32 inc); +// const u8 *data, size_t nblocks, u32 inc); // // Only the first three fields of struct blake2s_ctx are used: // u32 h[8]; (inout) // u32 t[2]; (inout) // u32 f[2]; (in)
@@ -182,11 +182,11 @@ ENTRY(blake2s_compress) push {r0-r2,r4-r11,lr} // keep this an even number .Lnext_block: // r0 is 'ctx' - // r1 is 'block' + // r1 is 'data' // r3 is 'inc' // Load and increment the counter t[0..1]. __ldrd r10, r11, r0, 32 adds r10, r10, r3
@@ -273,11 +273,11 @@ ENTRY(blake2s_compress) stm r14, {r0-r3} // store new h[4..7] // Advance to the next block, if there is one. Note that if there are // multiple blocks, then 'inc' (the counter increment amount) must be // 64. So we can simply set it to 64 without re-loading it. - ldm sp, {r0, r1, r2} // load (ctx, block, nblocks) + ldm sp, {r0, r1, r2} // load (ctx, data, nblocks) mov r3, #64 // set 'inc' subs r2, r2, #1 // nblocks-- str r2, [sp, #8] bne .Lnext_block // nblocks != 0?
lib/crypto/arm/blake2s.h +1 -1
@@ -1,5 +1,5 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ /* defined in blake2s-core.S */ void blake2s_compress (struct blake2s_ctx *ctx, -const u8 *block,size_t nblocks,u32 inc); +const u8 *data,size_t nblocks,u32 inc);
lib/crypto/blake2s.c +6 -6
@@ -27,31 +27,30 @@ static const u8 blake2s_sigma[10][16] = { { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, }; -static inline void blake2s_increment_counter (struct blake2s_ctx *ctx, -const u32 inc) +static inline void blake2s_increment_counter (struct blake2s_ctx *ctx,u32 inc) { ctx->t[0 ] += inc; ctx->t[1 ] += (ctx->t[0 ] < inc); }static void __maybe_unused -blake2s_compress_generic (struct blake2s_ctx *ctx,const u8 *block, -size_t nblocks,const u32 inc) +blake2s_compress_generic (struct blake2s_ctx *ctx, +const u8 *data,size_t nblocks,u32 inc) {u32 m[16 ];u32 v[16 ];int i;WARN_ON (IS_ENABLED (DEBUG ) && (nblocks >1 && inc !=BLAKE2S_BLOCK_SIZE ));while (nblocks >0 ) {blake2s_increment_counter (ctx, inc); -memcpy (m, block,BLAKE2S_BLOCK_SIZE ); +memcpy (m, data,BLAKE2S_BLOCK_SIZE );le32_to_cpu_array (m,ARRAY_SIZE (m));memcpy (v, ctx->h,32 ); v[8 ] =BLAKE2S_IV0 ; v[9 ] =BLAKE2S_IV1 ; v[10 ] =BLAKE2S_IV2 ;
@@ -97,11 +96,11 @@ blake2s_compress_generic(struct blake2s_ctx *ctx, const u8 *block,#undef ROUNDfor (i =0 ; i <8 ; ++i) ctx->h[i] ^= v[i] ^ v[i +8 ]; - block +=BLAKE2S_BLOCK_SIZE ; + data +=BLAKE2S_BLOCK_SIZE ; --nblocks; } }#ifdef CONFIG_CRYPTO_LIB_BLAKE2S_ARCH
@@ -128,10 +127,11 @@ void blake2s_update(struct blake2s_ctx *ctx, const u8 *in, size_t inlen) in += fill; inlen -= fill; }if (inlen >BLAKE2S_BLOCK_SIZE ) {const size_t nblocks =DIV_ROUND_UP (inlen,BLAKE2S_BLOCK_SIZE ); +blake2s_compress (ctx, in, nblocks -1 ,BLAKE2S_BLOCK_SIZE ); in +=BLAKE2S_BLOCK_SIZE * (nblocks -1 ); inlen -=BLAKE2S_BLOCK_SIZE * (nblocks -1 ); }memcpy (ctx->buf + ctx->buflen, in, inlen);
lib/crypto/x86/blake2s.h +8 -10
@@ -10,43 +10,41 @@#include <linux/jump_label.h> #include <linux/kernel.h> #include <linux/sizes.h> asmlinkage void blake2s_compress_ssse3(struct blake2s_ctx *ctx, -const u8 *block,const size_t nblocks, -const u32 inc); +const u8 *data,size_t nblocks,u32 inc); asmlinkage void blake2s_compress_avx512(struct blake2s_ctx *ctx, -const u8 *block,const size_t nblocks, -const u32 inc); +const u8 *data,size_t nblocks,u32 inc);static __ro_after_init DEFINE_STATIC_KEY_FALSE (blake2s_use_ssse3);static __ro_after_init DEFINE_STATIC_KEY_FALSE (blake2s_use_avx512); -static void blake2s_compress (struct blake2s_ctx *ctx,const u8 *block, -size_t nblocks,const u32 inc) +static void blake2s_compress (struct blake2s_ctx *ctx, +const u8 *data,size_t nblocks,u32 inc) {/* SIMD disables preemption, so relax after processing each page. */ BUILD_BUG_ON (SZ_4K /BLAKE2S_BLOCK_SIZE <8 );if (!static_branch_likely (&blake2s_use_ssse3) || !may_use_simd ()) { -blake2s_compress_generic (ctx, block, nblocks, inc); +blake2s_compress_generic (ctx, data, nblocks, inc);return ; }do {const size_t blocks =min_t (size_t, nblocks,SZ_4K /BLAKE2S_BLOCK_SIZE );kernel_fpu_begin ();if (static_branch_likely (&blake2s_use_avx512)) -blake2s_compress_avx512 (ctx, block, blocks, inc); +blake2s_compress_avx512 (ctx, data, blocks, inc);else -blake2s_compress_ssse3 (ctx, block, blocks, inc); +blake2s_compress_ssse3 (ctx, data, blocks, inc);kernel_fpu_end (); + data += blocks *BLAKE2S_BLOCK_SIZE ; nblocks -= blocks; - block += blocks *BLAKE2S_BLOCK_SIZE ; }while (nblocks); }#define blake2s_mod_init_arch blake2s_mod_init_archstatic void blake2s_mod_init_arch (void )
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 4/10 lib/crypto: blake2s: Document the BLAKE2s library API
In reply to Eric BiggersAdd kerneldoc for the BLAKE2s library API.
--- include/crypto/blake2s.h | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
include/crypto/blake2s.h +58 -0
@@ -20,10 +20,19 @@ enum blake2s_lengths {BLAKE2S_160_HASH_SIZE =20 ,BLAKE2S_224_HASH_SIZE =28 , BLAKE2S_256_HASH_SIZE = 32, }; +/** +* struct blake2s_ctx - Context for hashing a message with BLAKE2s +* @h: compression function state +* @t: block counter +* @f: finalization indicator +* @buf: partial block buffer; 'buflen' bytes are valid +* @buflen: number of bytes buffered in @buf +* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE +*/ struct blake2s_ctx {/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */ u32 h[8 ];u32 t[2 ];u32 f[2 ];
@@ -65,27 +74,76 @@ static inline void __blake2s_init(struct blake2s_ctx *ctx, size_t outlen,memset (&ctx->buf[keylen],0 ,BLAKE2S_BLOCK_SIZE - keylen); ctx->buflen =BLAKE2S_BLOCK_SIZE ; } } +/** +* blake2s_init() - Initialize a BLAKE2s context for a new message (unkeyed) +* @ctx: the context to initialize +* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE +* +* Context: Any context. +*/ static inline void blake2s_init (struct blake2s_ctx *ctx,size_t outlen) {__blake2s_init (ctx, outlen,NULL ,0 ); } +/** +* blake2s_init_key() - Initialize a BLAKE2s context for a new message (keyed) +* @ctx: the context to initialize +* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE +* @key: the key +* @keylen: the key length in bytes, at most BLAKE2S_KEY_SIZE +* +* Context: Any context. +*/ static inline void blake2s_init_key (struct blake2s_ctx *ctx,size_t outlen,const void *key,size_t keylen) {WARN_ON (IS_ENABLED (DEBUG ) && (!outlen || outlen >BLAKE2S_HASH_SIZE || !key || !keylen || keylen >BLAKE2S_KEY_SIZE ));__blake2s_init (ctx, outlen, key, keylen); } +/** +* blake2s_update() - Update a BLAKE2s context with message data +* @ctx: the context to update; must have been initialized +* @in: the message data +* @inlen: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ void blake2s_update (struct blake2s_ctx *ctx,const u8 *in,size_t inlen); + +/** +* blake2s_final() - Finish computing a BLAKE2s hash +* @ctx: the context to finalize; must have been initialized +* @out: (output) the resulting BLAKE2s hash. Its length will be equal to the +* @outlen that was passed to blake2s_init() or blake2s_init_key(). +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ void blake2s_final (struct blake2s_ctx *ctx,u8 *out); +/** +* blake2s() - Compute BLAKE2s hash in one shot +* @key: the key, or NULL for an unkeyed hash +* @keylen: the key length in bytes (at most BLAKE2S_KEY_SIZE), or 0 for an +* unkeyed hash +* @in: the message data +* @inlen: the data length in bytes +* @out: (output) the resulting BLAKE2s hash, with length @outlen +* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE +* +* Context: Any context. +*/ static inline void blake2s (const u8 *key,size_t keylen,const u8 *in,size_t inlen,u8 *out,size_t outlen) {struct blake2s_ctx ctx;
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 5/10 byteorder: Add le64_to_cpu_array() and cpu_to_le64_array()
In reply to Eric BiggersAdd le64_to_cpu_array() and cpu_to_le64_array(). These mirror the corresponding 32-bit functions.
These will be used by the BLAKE2b code.
--- include/linux/byteorder/generic.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/byteorder/generic.h +16 -0
@@ -171,10 +171,26 @@ static inline void cpu_to_le32_array(u32 *buf, unsigned int words)__cpu_to_le32s (buf); buf++; } } +static inline void le64_to_cpu_array (u64 *buf,unsigned int words) +{ +while (words--) { +__le64_to_cpus (buf); + buf++; + } +} + +static inline void cpu_to_le64_array (u64 *buf,unsigned int words) +{ +while (words--) { +__cpu_to_le64s (buf); + buf++; + } +} +static inline void memcpy_from_le32 (u32 *dst,const __le32 *src,size_t words) {size_t i;for (i =0 ; i < words; i++)
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 6/10 lib/crypto: blake2b: Add BLAKE2b library functions
In reply to Eric BiggersAdd a library API for BLAKE2b, closely modeled after the BLAKE2s API.
This will allow in-kernel users such as btrfs to use BLAKE2b without going through the generic crypto layer. In addition, as usual the BLAKE2b crypto_shash algorithms will be reimplemented on top of this.
Note: to create lib/crypto/blake2b.c I made a copy of lib/crypto/blake2s.c and made the updates from BLAKE2s => BLAKE2b. This way, the BLAKE2s and BLAKE2b code is kept consistent. Therefore, it borrows the SPDX-License-Identifier and Copyright from lib/crypto/blake2s.c rather than crypto/blake2b_generic.c.
The library API uses 'struct blake2b_ctx', consistent with other lib/crypto/ APIs. The existing 'struct blake2b_state' will be removed once the blake2b crypto_shash algorithms are updated to stop using it.
--- include/crypto/blake2b.h | 133 ++++++++++++++++++++--- include/crypto/internal/blake2b.h | 17 ++- lib/crypto/Kconfig | 10 ++ lib/crypto/Makefile | 9 ++ lib/crypto/blake2b.c | 174 ++++++++++++++++++++++++++++++ 5 files changed, 330 insertions(+), 13 deletions(-) create mode 100644 lib/crypto/blake2b.c
include/crypto/blake2b.h +121 -12
@@ -26,10 +26,29 @@ enum blake2b_lengths {BLAKE2B_256_HASH_SIZE =32 ,BLAKE2B_384_HASH_SIZE =48 , BLAKE2B_512_HASH_SIZE = 64, }; +/** +* struct blake2b_ctx - Context for hashing a message with BLAKE2b +* @h: compression function state +* @t: block counter +* @f: finalization indicator +* @buf: partial block buffer; 'buflen' bytes are valid +* @buflen: number of bytes buffered in @buf +* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE +*/ +struct blake2b_ctx { +/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */ +u64 h[8 ]; +u64 t[2 ]; +u64 f[2 ]; +u8 buf[BLAKE2B_BLOCK_SIZE ]; +unsigned int buflen; +unsigned int outlen; +}; +enum blake2b_iv {BLAKE2B_IV0 =0x6A09E667F3BCC908ULL ,BLAKE2B_IV1 =0xBB67AE8584CAA73BULL ,BLAKE2B_IV2 =0x3C6EF372FE94F82BULL , BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,
@@ -37,21 +56,111 @@ enum blake2b_iv {BLAKE2B_IV5 =0x9B05688C2B3E6C1FULL ,BLAKE2B_IV6 =0x1F83D9ABFB41BD6BULL , BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL, }; -static inline void __blake2b_init (struct blake2b_state *state,size_t outlen, -size_t keylen) +static inline void __blake2b_init (struct blake2b_ctx *ctx,size_t outlen, +const void *key,size_t keylen) +{ + ctx->h[0 ] =BLAKE2B_IV0 ^ (0x01010000 | keylen <<8 | outlen); + ctx->h[1 ] =BLAKE2B_IV1 ; + ctx->h[2 ] =BLAKE2B_IV2 ; + ctx->h[3 ] =BLAKE2B_IV3 ; + ctx->h[4 ] =BLAKE2B_IV4 ; + ctx->h[5 ] =BLAKE2B_IV5 ; + ctx->h[6 ] =BLAKE2B_IV6 ; + ctx->h[7 ] =BLAKE2B_IV7 ; + ctx->t[0 ] =0 ; + ctx->t[1 ] =0 ; + ctx->f[0 ] =0 ; + ctx->f[1 ] =0 ; + ctx->buflen =0 ; + ctx->outlen = outlen; +if (keylen) { +memcpy (ctx->buf, key, keylen); +memset (&ctx->buf[keylen],0 ,BLAKE2B_BLOCK_SIZE - keylen); + ctx->buflen =BLAKE2B_BLOCK_SIZE ; + } +} + +/** +* blake2b_init() - Initialize a BLAKE2b context for a new message (unkeyed) +* @ctx: the context to initialize +* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE +* +* Context: Any context. +*/ +static inline void blake2b_init (struct blake2b_ctx *ctx,size_t outlen) +{ +__blake2b_init (ctx, outlen,NULL ,0 ); +} + +/** +* blake2b_init_key() - Initialize a BLAKE2b context for a new message (keyed) +* @ctx: the context to initialize +* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE +* @key: the key +* @keylen: the key length in bytes, at most BLAKE2B_KEY_SIZE +* +* Context: Any context. +*/ +static inline void blake2b_init_key (struct blake2b_ctx *ctx,size_t outlen, +const void *key,size_t keylen) +{ +WARN_ON (IS_ENABLED (DEBUG ) && (!outlen || outlen >BLAKE2B_HASH_SIZE || + !key || !keylen || keylen >BLAKE2B_KEY_SIZE )); + +__blake2b_init (ctx, outlen, key, keylen); +} + +/** +* blake2b_update() - Update a BLAKE2b context with message data +* @ctx: the context to update; must have been initialized +* @in: the message data +* @inlen: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ +void blake2b_update (struct blake2b_ctx *ctx,const u8 *in,size_t inlen); + +/** +* blake2b_final() - Finish computing a BLAKE2b hash +* @ctx: the context to finalize; must have been initialized +* @out: (output) the resulting BLAKE2b hash. Its length will be equal to the +* @outlen that was passed to blake2b_init() or blake2b_init_key(). +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ +void blake2b_final (struct blake2b_ctx *ctx,u8 *out); + +/** +* blake2b() - Compute BLAKE2b hash in one shot +* @key: the key, or NULL for an unkeyed hash +* @keylen: the key length in bytes (at most BLAKE2B_KEY_SIZE), or 0 for an +* unkeyed hash +* @in: the message data +* @inlen: the data length in bytes +* @out: (output) the resulting BLAKE2b hash, with length @outlen +* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE +* +* Context: Any context. +*/ +static inline void blake2b (const u8 *key,size_t keylen, +const u8 *in,size_t inlen, +u8 *out,size_t outlen) { - state->h[0 ] =BLAKE2B_IV0 ^ (0x01010000 | keylen <<8 | outlen); - state->h[1 ] =BLAKE2B_IV1 ; - state->h[2 ] =BLAKE2B_IV2 ; - state->h[3 ] =BLAKE2B_IV3 ; - state->h[4 ] =BLAKE2B_IV4 ; - state->h[5 ] =BLAKE2B_IV5 ; - state->h[6 ] =BLAKE2B_IV6 ; - state->h[7 ] =BLAKE2B_IV7 ; - state->t[0 ] =0 ; - state->t[1 ] =0 ; +struct blake2b_ctx ctx; + +WARN_ON (IS_ENABLED (DEBUG ) && ((!in && inlen >0 ) || !out || !outlen || + outlen >BLAKE2B_HASH_SIZE || keylen >BLAKE2B_KEY_SIZE || + (!key && keylen))); + +__blake2b_init (&ctx, outlen, key, keylen); +blake2b_update (&ctx, in, inlen); +blake2b_final (&ctx, out); }#endif /* _CRYPTO_BLAKE2B_H */
include/crypto/internal/blake2b.h +16 -1
@@ -55,17 +55,32 @@ static inline int crypto_blake2b_setkey(struct crypto_shash *tfm, tctx->keylen = keylen;return 0 ; } +static inline void __crypto_blake2b_init (struct blake2b_state *state, +size_t outlen,size_t keylen) +{ + state->h[0 ] =BLAKE2B_IV0 ^ (0x01010000 | keylen <<8 | outlen); + state->h[1 ] =BLAKE2B_IV1 ; + state->h[2 ] =BLAKE2B_IV2 ; + state->h[3 ] =BLAKE2B_IV3 ; + state->h[4 ] =BLAKE2B_IV4 ; + state->h[5 ] =BLAKE2B_IV5 ; + state->h[6 ] =BLAKE2B_IV6 ; + state->h[7 ] =BLAKE2B_IV7 ; + state->t[0 ] =0 ; + state->t[1 ] =0 ; +} +static inline int crypto_blake2b_init (struct shash_desc *desc) {const struct blake2b_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct blake2b_state *state =shash_desc_ctx (desc);unsigned int outlen =crypto_shash_digestsize (desc->tfm); -__blake2b_init (state, outlen, tctx->keylen); +__crypto_blake2b_init (state, outlen, tctx->keylen);return tctx->keylen ?crypto_shash_update (desc, tctx->key,BLAKE2B_BLOCK_SIZE ) :0 ; } static inline int crypto_blake2b_update_bo(struct shash_desc *desc,
lib/crypto/Kconfig +10 -0
@@ -26,10 +26,20 @@ config CRYPTO_LIB_ARC4 tristate config CRYPTO_LIB_GF128MUL tristate +config CRYPTO_LIB_BLAKE2B + tristate + help + The BLAKE2b library functions. Select this if your module uses any of + the functions from <crypto/blake2b.h>. + +config CRYPTO_LIB_BLAKE2B_ARCH + bool + depends on CRYPTO_LIB_BLAKE2B && !UML + # BLAKE2s support is always built-in, so there's no CRYPTO_LIB_BLAKE2S option. config CRYPTO_LIB_BLAKE2S_ARCH bool depends on !UML
lib/crypto/Makefile +9 -0
@@ -29,10 +29,19 @@ libarc4-y := arc4.o obj-$(CONFIG_CRYPTO_LIB_GF128MUL) += gf128mul.o ################################################################################ +obj-$(CONFIG_CRYPTO_LIB_BLAKE2B) += libblake2b.o +libblake2b-y := blake2b.o +CFLAGS_blake2b.o := -Wframe-larger-than=4096 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105930 +ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2B_ARCH),y) +CFLAGS_blake2b.o += -I$(src)/$(SRCARCH) +endif # CONFIG_CRYPTO_LIB_BLAKE2B_ARCH + +################################################################################ + # blake2s is used by the /dev/random driver which is always builtin obj-y += blake2s.o ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2S_ARCH),y) CFLAGS_blake2s.o += -I$(src)/$(SRCARCH) obj-$(CONFIG_ARM) += arm/blake2s-core.o
lib/crypto/blake2b.c new file +174 -0
174 lines not on this page · raw message
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 7/10 lib/crypto: arm/blake2b: Migrate optimized code into library
In reply to Eric BiggersMigrate the arm-optimized BLAKE2b code from arch/arm/crypto/ to lib/crypto/arm/. This makes the BLAKE2b library able to use it, and it also simplifies the code because it's easier to integrate with the library than crypto_shash.
This temporarily makes the arm-optimized BLAKE2b code unavailable via crypto_shash. A later commit reimplements the blake2b-* crypto_shash algorithms on top of the BLAKE2b library API, making it available again.
Note that as per the lib/crypto/ convention, the optimized code is now enabled by default. So, this also fixes the longstanding issue where the optimized BLAKE2b code was not enabled by default.
To see the diff from arch/arm/crypto/blake2b-neon-glue.c to lib/crypto/arm/blake2b.h, view this commit with 'git show -M10'.
---
arch/arm/crypto/Kconfig | 16 ---
arch/arm/crypto/Makefile | 2 -
arch/arm/crypto/blake2b-neon-glue.c | 104 ------------------
lib/crypto/Kconfig | 1 +
lib/crypto/Makefile | 1 +
.../crypto/arm}/blake2b-neon-core.S | 29 ++---
lib/crypto/arm/blake2b.h | 41 +++++++
7 files changed, 59 insertions(+), 135 deletions(-)
delete mode 100644 arch/arm/crypto/blake2b-neon-glue.c
rename {arch/arm/crypto => lib/crypto/arm}/blake2b-neon-core.S (94%)
create mode 100644 lib/crypto/arm/blake2b.harch/arm/crypto/Kconfig +0 -16
@@ -31,26 +31,10 @@ config CRYPTO_NHPOLY1305_NEON NHPoly1305 hash function (Adiantum) Architecture: arm using: - NEON (Advanced SIMD) extensions -config CRYPTO_BLAKE2B_NEON - tristate "Hash functions: BLAKE2b (NEON)" - depends on KERNEL_MODE_NEON - select CRYPTO_BLAKE2B - help - BLAKE2b cryptographic hash function (RFC 7693) - - Architecture: arm using - - NEON (Advanced SIMD) extensions - - BLAKE2b digest algorithm optimized with ARM NEON instructions. - On ARM processors that have NEON support but not the ARMv8 - Crypto Extensions, typically this BLAKE2b implementation is - much faster than the SHA-2 family and slightly faster than - SHA-1. - config CRYPTO_AES_ARM tristate "Ciphers: AES" select CRYPTO_ALGAPI select CRYPTO_AES help
arch/arm/crypto/Makefile +0 -2
@@ -3,17 +3,15 @@ # Arch-specific CryptoAPI modules. # obj-$(CONFIG_CRYPTO_AES_ARM) += aes-arm.o obj-$(CONFIG_CRYPTO_AES_ARM_BS) += aes-arm-bs.o -obj-$(CONFIG_CRYPTO_BLAKE2B_NEON) += blake2b-neon.o obj-$(CONFIG_CRYPTO_NHPOLY1305_NEON) += nhpoly1305-neon.o obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o obj-$(CONFIG_CRYPTO_GHASH_ARM_CE) += ghash-arm-ce.o aes-arm-y := aes-cipher-core.o aes-cipher-glue.o aes-arm-bs-y := aes-neonbs-core.o aes-neonbs-glue.o -blake2b-neon-y := blake2b-neon-core.o blake2b-neon-glue.o aes-arm-ce-y := aes-ce-core.o aes-ce-glue.o ghash-arm-ce-y := ghash-ce-core.o ghash-ce-glue.o nhpoly1305-neon-y := nh-neon-core.o nhpoly1305-neon-glue.o
arch/arm/crypto/blake2b-neon-glue.c deleted +0 -104
104 lines not on this page · raw message
lib/crypto/Kconfig +1 -0
@@ -35,10 +35,11 @@ config CRYPTO_LIB_BLAKE2B the functions from <crypto/blake2b.h>. config CRYPTO_LIB_BLAKE2B_ARCH bool depends on CRYPTO_LIB_BLAKE2B && !UML + default y if ARM && KERNEL_MODE_NEON # BLAKE2s support is always built-in, so there's no CRYPTO_LIB_BLAKE2S option. config CRYPTO_LIB_BLAKE2S_ARCH bool
lib/crypto/Makefile +1 -0
@@ -34,10 +34,11 @@ obj-$(CONFIG_CRYPTO_LIB_GF128MUL) += gf128mul.o obj-$(CONFIG_CRYPTO_LIB_BLAKE2B) += libblake2b.o libblake2b-y := blake2b.o CFLAGS_blake2b.o := -Wframe-larger-than=4096 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105930 ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2B_ARCH),y) CFLAGS_blake2b.o += -I$(src)/$(SRCARCH) +obj-$(CONFIG_ARM) += arm/blake2b-neon-core.o endif # CONFIG_CRYPTO_LIB_BLAKE2B_ARCH ################################################################################ # blake2s is used by the /dev/random driver which is always builtin
lib/crypto/arm/blake2b-neon-core.S renamed from arch/arm/crypto/blake2b-neon-core.S +16 -13
@@ -1,8 +1,11 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ /* - * BLAKE2b digest algorithm, NEON accelerated + * BLAKE2b digest algorithm optimized with ARM NEON instructions. On ARM + * processors that have NEON support but not the ARMv8 Crypto Extensions, + * typically this BLAKE2b implementation is much faster than the SHA-2 family + * and slightly faster than SHA-1. * * Copyright 2020 Google LLC * * Author: Eric Biggers <[email protected]> */
@@ -11,12 +14,12 @@ .text .fpu neon // The arguments to blake2b_compress_neon() - STATE .req r0 - BLOCK .req r1 + CTX .req r0 + DATA .req r1 NBLOCKS .req r2 INC .req r3 // Pointers to the rotation tables ROR24_TABLE .req r4
@@ -232,14 +235,14 @@ vld1.8 {q8-q9}, [sp, :256] .endif .endm // -// void blake2b_compress_neon(struct blake2b_state *state, -// const u8 *block, size_t nblocks, u32 inc); +// void blake2b_compress_neon(struct blake2b_ctx *ctx, +// const u8 *data, size_t nblocks, u32 inc); // -// Only the first three fields of struct blake2b_state are used: +// Only the first three fields of struct blake2b_ctx are used: // u64 h[8]; (inout) // u64 t[2]; (inout) // u64 f[2]; (in) // .align 5
@@ -253,11 +256,11 @@ ENTRY(blake2b_compress_neon) mov sp, ip adr ROR24_TABLE, .Lror24_table adr ROR16_TABLE, .Lror16_table - mov ip, STATE + mov ip, CTX vld1.64 {q0-q1}, [ip]! // Load h[0..3] vld1.64 {q2-q3}, [ip]! // Load h[4..7] .Lnext_block: adr r10, .Lblake2b_IV vld1.64 {q14-q15}, [ip] // Load t[0..1] and f[0..1]
@@ -279,18 +282,18 @@ ENTRY(blake2b_compress_neon) // registers than the state registers, as the message doesn't change. // Therefore we store a copy of the first 32 bytes of the message block // (q8-q9) in an aligned buffer on the stack so that they can be // reloaded when needed. (We could just reload directly from the // message buffer, but it's faster to use aligned loads.) - vld1.8 {q8-q9}, [BLOCK]! + vld1.8 {q8-q9}, [DATA]! veor q6, q6, q14 // v[12..13] = IV[4..5] ^ t[0..1] - vld1.8 {q10-q11}, [BLOCK]! + vld1.8 {q10-q11}, [DATA]! veor q7, q7, q15 // v[14..15] = IV[6..7] ^ f[0..1] - vld1.8 {q12-q13}, [BLOCK]! + vld1.8 {q12-q13}, [DATA]! vst1.8 {q8-q9}, [sp, :256] - mov ip, STATE - vld1.8 {q14-q15}, [BLOCK]! + mov ip, CTX + vld1.8 {q14-q15}, [DATA]! // Execute the rounds. Each round is provided the order in which it // needs to use the message words. _blake2b_round 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 _blake2b_round 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3
@@ -317,11 +320,11 @@ ENTRY(blake2b_compress_neon) vld1.64 {q10-q11}, [ip] // Load old h[4..7] veor q2, q2, q6 // v[4..5] ^= v[12..13] veor q3, q3, q7 // v[6..7] ^= v[14..15] veor q0, q0, q8 // v[0..1] ^= h[0..1] veor q1, q1, q9 // v[2..3] ^= h[2..3] - mov ip, STATE + mov ip, CTX subs NBLOCKS, NBLOCKS, #1 // nblocks-- vst1.64 {q0-q1}, [ip]! // Store new h[0..3] veor q2, q2, q10 // v[4..5] ^= h[4..5] veor q3, q3, q11 // v[6..7] ^= h[6..7] vst1.64 {q2-q3}, [ip]! // Store new h[4..7]
lib/crypto/arm/blake2b.h new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 8/10 lib/crypto: tests: Add KUnit tests for BLAKE2b
In reply to Eric BiggersAdd a KUnit test suite for the BLAKE2b library API, mirroring the BLAKE2s test suite very closely.
As with the BLAKE2s test suite, a benchmark is included.
--- lib/crypto/tests/Kconfig | 9 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/blake2b-testvecs.h | 342 ++++++++++++++++++++++++++++ lib/crypto/tests/blake2b_kunit.c | 133 +++++++++++ scripts/crypto/gen-hash-testvecs.py | 29 +-- 5 files changed, 501 insertions(+), 13 deletions(-) create mode 100644 lib/crypto/tests/blake2b-testvecs.h create mode 100644 lib/crypto/tests/blake2b_kunit.c
lib/crypto/tests/Kconfig +9 -0
@@ -1,7 +1,16 @@ # SPDX-License-Identifier: GPL-2.0-or-later +config CRYPTO_LIB_BLAKE2B_KUNIT_TEST + tristate "KUnit tests for BLAKE2b" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + select CRYPTO_LIB_BENCHMARK_VISIBLE + select CRYPTO_LIB_BLAKE2B + help + KUnit tests for the BLAKE2b cryptographic hash function. + config CRYPTO_LIB_BLAKE2S_KUNIT_TEST tristate "KUnit tests for BLAKE2s" if !KUNIT_ALL_TESTS depends on KUNIT default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS select CRYPTO_LIB_BENCHMARK_VISIBLE
lib/crypto/tests/Makefile +1 -0
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-or-later +obj-$(CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o
lib/crypto/tests/blake2b-testvecs.h new file +342 -0
342 lines not on this page · raw message
lib/crypto/tests/blake2b_kunit.c new file +133 -0
133 lines not on this page · raw message
scripts/crypto/gen-hash-testvecs.py +16 -13
@@ -83,12 +83,12 @@ def print_c_struct_u8_array_field(name, value): print(f'\t\t.{name} = {{') print_bytes('\t\t\t', value, 8) print('\t\t},') def alg_digest_size_const(alg): - if alg == 'blake2s': - return 'BLAKE2S_HASH_SIZE' + if alg.startswith('blake2'): + return f'{alg.upper()}_HASH_SIZE' return f'{alg.upper()}_DIGEST_SIZE' def gen_unkeyed_testvecs(alg): print('') print('static const struct {')
@@ -122,23 +122,26 @@ def gen_hmac_testvecs(alg): ctx.update(mac) print_static_u8_array_definition( f'hmac_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]', ctx.digest()) -BLAKE2S_KEY_SIZE = 32 -BLAKE2S_HASH_SIZE = 32 - -def gen_additional_blake2s_testvecs(): +def gen_additional_blake2_testvecs(alg): + if alg == 'blake2s': + (max_key_size, max_hash_size) = (32, 32) + elif alg == 'blake2b': + (max_key_size, max_hash_size) = (64, 64) + else: + raise ValueError(f'Unsupported alg: {alg}') hashes = b'' - for key_len in range(BLAKE2S_KEY_SIZE + 1): - for out_len in range(1, BLAKE2S_HASH_SIZE + 1): - h = hashlib.blake2s(digest_size=out_len, key=rand_bytes(key_len)) + for key_len in range(max_key_size + 1): + for out_len in range(1, max_hash_size + 1): + h = hashlib.new(alg, digest_size=out_len, key=rand_bytes(key_len)) h.update(rand_bytes(100)) hashes += h.digest() print_static_u8_array_definition( - 'blake2s_keyed_testvec_consolidated[BLAKE2S_HASH_SIZE]', - compute_hash('blake2s', hashes)) + f'{alg}_keyed_testvec_consolidated[{alg_digest_size_const(alg)}]', + compute_hash(alg, hashes)) def gen_additional_poly1305_testvecs(): key = b'\xff' * POLY1305_KEY_SIZE data = b'' ctx = Poly1305(key)
@@ -158,11 +161,11 @@ if len(sys.argv) != 2: alg = sys.argv[1] print('/* SPDX-License-Identifier: GPL-2.0-or-later */') print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */') gen_unkeyed_testvecs(alg) -if alg == 'blake2s': - gen_additional_blake2s_testvecs() +if alg.startswith('blake2'): + gen_additional_blake2_testvecs(alg) elif alg == 'poly1305': gen_additional_poly1305_testvecs() else: gen_hmac_testvecs(alg)
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 9/10 crypto: blake2b - Reimplement using library API
In reply to Eric BiggersReplace blake2b_generic.c with a new file blake2b.c which implements the BLAKE2b crypto_shash algorithms on top of the BLAKE2b library API.
Change the driver name suffix from "-generic" to "-lib" to reflect that these algorithms now just use the (possibly arch-optimized) library.
This closely mirrors crypto/{md5,sha1,sha256,sha512}.c.
Remove include/crypto/internal/blake2b.h since it is no longer used. Likewise, remove struct blake2b_state from include/crypto/blake2b.h.
Omit support for import_core and export_core, since there are no legacy drivers that need these for these algorithms.
--- crypto/Kconfig | 1 + crypto/Makefile | 3 +- crypto/blake2b.c | 111 +++++++++++++++++ crypto/blake2b_generic.c | 192 ------------------------------ crypto/testmgr.c | 4 + include/crypto/blake2b.h | 10 -- include/crypto/internal/blake2b.h | 116 ------------------ 7 files changed, 117 insertions(+), 320 deletions(-) create mode 100644 crypto/blake2b.c delete mode 100644 crypto/blake2b_generic.c delete mode 100644 include/crypto/internal/blake2b.h
crypto/Kconfig +1 -0
@@ -879,10 +879,11 @@ endmenu menu "Hashes, digests, and MACs" config CRYPTO_BLAKE2B tristate "BLAKE2b" select CRYPTO_HASH + select CRYPTO_LIB_BLAKE2B help BLAKE2b cryptographic hash function (RFC 7693) BLAKE2b is optimized for 64-bit platforms and can produce digests of any size between 1 and 64 bytes. The keyed hash is also implemented.
crypto/Makefile +1 -2
@@ -81,12 +81,11 @@ obj-$(CONFIG_CRYPTO_SHA512) += sha512.o obj-$(CONFIG_CRYPTO_SHA3) += sha3_generic.o obj-$(CONFIG_CRYPTO_SM3_GENERIC) += sm3_generic.o 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_BLAKE2B) += blake2b_generic.o -CFLAGS_blake2b_generic.o := -Wframe-larger-than=4096 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105930 +obj-$(CONFIG_CRYPTO_BLAKE2B) += blake2b.o obj-$(CONFIG_CRYPTO_ECB) += ecb.o obj-$(CONFIG_CRYPTO_CBC) += cbc.o obj-$(CONFIG_CRYPTO_PCBC) += pcbc.o obj-$(CONFIG_CRYPTO_CTS) += cts.o obj-$(CONFIG_CRYPTO_LRW) += lrw.o
crypto/blake2b.c new file +111 -0
111 lines not on this page · raw message
crypto/blake2b_generic.c deleted +0 -192
192 lines not on this page · raw message
crypto/testmgr.c +4 -0
@@ -4330,31 +4330,35 @@ 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", + .generic_driver = "blake2b-160-lib", .test = alg_test_hash, .fips_allowed = 0, .suite = { .hash = __VECS(blake2b_160_tv_template) } }, { .alg = "blake2b-256", + .generic_driver = "blake2b-256-lib", .test = alg_test_hash, .fips_allowed = 0, .suite = { .hash = __VECS(blake2b_256_tv_template) } }, { .alg = "blake2b-384", + .generic_driver = "blake2b-384-lib", .test = alg_test_hash, .fips_allowed = 0, .suite = { .hash = __VECS(blake2b_384_tv_template) } }, { .alg = "blake2b-512", + .generic_driver = "blake2b-512-lib", .test = alg_test_hash, .fips_allowed = 0, .suite = { .hash =__VECS (blake2b_512_tv_template) }
include/crypto/blake2b.h +0 -10
@@ -5,24 +5,14 @@#include <linux/bug.h> #include <linux/types.h> #include <linux/string.h> -struct blake2b_state { -/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */ -u64 h[8 ]; -u64 t[2 ]; -/* The true state ends here. The rest is temporary storage. */ -u64 f[2 ]; -}; -enum blake2b_lengths {BLAKE2B_BLOCK_SIZE =128 ,BLAKE2B_HASH_SIZE =64 ,BLAKE2B_KEY_SIZE =64 , -BLAKE2B_STATE_SIZE = offsetof(struct blake2b_state , f), -BLAKE2B_DESC_SIZE =sizeof (struct blake2b_state ),BLAKE2B_160_HASH_SIZE =20 ,BLAKE2B_256_HASH_SIZE =32 ,BLAKE2B_384_HASH_SIZE =48 ,BLAKE2B_512_HASH_SIZE =64 ,
include/crypto/internal/blake2b.h deleted +0 -116
116 lines not on this page · raw message
Signature · 1 line
2.51.1.dirty
Eric Biggers
maintainerPATCH 10/10 btrfs: switch to library APIs for checksums
In reply to Eric BiggersMake btrfs use the library APIs instead of crypto_shash, for all checksum computations. This has many benefits:
- Allows future checksum types, e.g. XXH3 or CRC64, to be more easily supported. Only a library API will be needed, not crypto_shash too.
- Eliminates the overhead of the generic crypto layer, including an indirect call for every function call and other API overhead. A microbenchmark of btrfs_check_read_bio() with crc32c checksums shows a speedup from 658 cycles to 608 cycles per 4096-byte block.
- Decreases the stack usage of btrfs by reducing the size of checksum contexts from 384 bytes to 240 bytes, and by eliminating the need for some functions to declare a checksum context at all.
- Increases reliability. The library functions always succeed and return void. In contrast, crypto_shash can fail and return errors. Also, the library functions are guaranteed to be available when btrfs is loaded; there's no longer any need to use module softdeps to try to work around the crypto modules sometimes not being loaded.
- Fixes a bug where blake2b checksums didn't work on kernels booted with fips=1. Since btrfs checksums are for integrity only, it's fine for them to use non-FIPS-approved algorithms.
Note that with having to handle 4 algorithms instead of just 1-2, this commit does result in a slightly positive diffstat. That being said, this wouldn't have been the case if btrfs had actually checked for errors from crypto_shash, which technically it should have been doing.
--- crypto/Kconfig | 2 - fs/btrfs/Kconfig | 8 ++-- fs/btrfs/compression.c | 1 - fs/btrfs/disk-io.c | 68 ++++++++--------------------- fs/btrfs/file-item.c | 4 -- fs/btrfs/fs.c | 97 ++++++++++++++++++++++++++++++++++++------ fs/btrfs/fs.h | 23 +++++++--- fs/btrfs/inode.c | 13 +++--- fs/btrfs/scrub.c | 16 +++---- fs/btrfs/super.c | 4 -- fs/btrfs/sysfs.c | 6 +-- 11 files changed, 136 insertions(+), 106 deletions(-)
crypto/Kconfig +0 -2
@@ -892,12 +892,10 @@ config CRYPTO_BLAKE2B - blake2b-160 - blake2b-256 - blake2b-384 - blake2b-512 - Used by the btrfs filesystem. - See https://blake2.net for further information. config CRYPTO_CMAC tristate "CMAC (Cipher-based MAC)" select CRYPTO_HASH
fs/btrfs/Kconfig +3 -5
@@ -2,24 +2,22 @@ config BTRFS_FS tristate "Btrfs filesystem support" select BLK_CGROUP_PUNT_BIO select CRC32 - select CRYPTO - select CRYPTO_CRC32C - select CRYPTO_XXHASH - select CRYPTO_SHA256 - select CRYPTO_BLAKE2B + select CRYPTO_LIB_BLAKE2B + select CRYPTO_LIB_SHA256 select ZLIB_INFLATE select ZLIB_DEFLATE select LZO_COMPRESS select LZO_DECOMPRESS select ZSTD_COMPRESS select ZSTD_DECOMPRESS select FS_IOMAP select RAID6_PQ select XOR_BLOCKS + select XXHASH depends on PAGE_SIZE_LESS_THAN_256KB help Btrfs is a general purpose copy-on-write filesystem with extents, writable snapshotting, support for multiple devices and many more
fs/btrfs/compression.c +0 -1
@@ -19,11 +19,10 @@#include <linux/psi.h> #include <linux/slab.h> #include <linux/sched/mm.h> #include <linux/log2.h> #include <linux/shrinker.h> -#include <crypto/hash.h> #include "misc.h" #include "ctree.h" #include "fs.h" #include "btrfs_inode.h" #include "bio.h"
fs/btrfs/disk-io.c +17 -51
206 lines not on this page · raw message
fs/btrfs/file-item.c +0 -4
@@ -6,11 +6,10 @@#include <linux/bio.h> #include <linux/slab.h> #include <linux/pagemap.h> #include <linux/highmem.h> #include <linux/sched/mm.h> -#include <crypto/hash.h> #include "messages.h" #include "ctree.h" #include "disk-io.h" #include "transaction.h" #include "bio.h"
@@ -770,11 +769,10 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,int btrfs_csum_one_bio (struct btrfs_bio *bbio) {struct btrfs_ordered_extent *ordered = bbio->ordered;struct btrfs_inode *inode = bbio->inode;struct btrfs_fs_info *fs_info = inode->root->fs_info; -SHASH_DESC_ON_STACK (shash, fs_info->csum_shash);struct bio *bio = &bbio->bio;struct btrfs_ordered_sum *sums;struct bvec_iter iter = bio->bi_iter;phys_addr_t paddr;const u32 blocksize = fs_info->sectorsize;
@@ -793,12 +791,10 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio)INIT_LIST_HEAD (&sums->list); sums->logical = bio->bi_iter.bi_sector <<SECTOR_SHIFT ; index =0 ; - shash->tfm = fs_info->csum_shash; -btrfs_bio_for_each_block (paddr, bio, &iter, blocksize) {btrfs_calculate_block_csum (fs_info, paddr, sums->sums + index); index += fs_info->csum_size; }
fs/btrfs/fs.c +83 -14
130 lines not on this page · raw message
fs/btrfs/fs.h +18 -5
@@ -1,10 +1,12 @@/* SPDX-License-Identifier: GPL-2.0 */ #ifndef BTRFS_FS_H #define BTRFS_FS_H +#include <crypto/blake2b.h> +#include <crypto/sha2.h> #include <linux/blkdev.h> #include <linux/sizes.h> #include <linux/time64.h> #include <linux/compiler.h> #include <linux/math.h>
@@ -22,21 +24,21 @@#include <linux/workqueue.h> #include <linux/wait.h> #include <linux/wait_bit.h> #include <linux/sched.h> #include <linux/rbtree.h> +#include <linux/xxhash.h> #include <uapi/linux/btrfs.h> #include <uapi/linux/btrfs_tree.h> #include "extent-io-tree.h" #include "async-thread.h" #include "block-rsv.h" struct inode ;struct super_block ;struct kobject ;struct reloc_control ; -struct crypto_shash ;struct ulist ;struct btrfs_device ;struct btrfs_block_group ;struct btrfs_root ;struct btrfs_fs_devices ;
@@ -827,13 +829,14 @@ struct btrfs_fs_info {u32 sectorsize;/* ilog2 of sectorsize, use to avoid 64bit division */ u32 sectorsize_bits;u32 block_min_order;u32 block_max_order; +u32 stripesize;u32 csum_size;u32 csums_per_leaf; -u32 stripesize; +u16 csum_type;/* * Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular * filesystem, on zoned it depends on the device constraints. */
@@ -841,12 +844,10 @@ struct btrfs_fs_info {/* Block groups and devices containing active swapfiles. */ spinlock_t swapfile_pins_lock;struct rb_root swapfile_pins; -struct crypto_shash *csum_shash; -/* Type of exclusive operation running, protected by super_lock */ enum btrfs_exclusive_operation exclusive_operation;/* * Zone size > 0 when in ZONED mode, otherwise it's used for a check
@@ -1034,12 +1035,24 @@ void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,int btrfs_check_ioctl_vol_args_path (const struct btrfs_ioctl_vol_args *vol_args);u16 btrfs_csum_type_size (u16 type);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 __attribute_const__ btrfs_get_num_csums(void); +struct btrfs_csum_ctx { +u16 csum_type; +union { +u32 crc32; +struct xxh64_state xxh64; +struct sha256_ctx sha256; +struct blake2b_ctx blake2b; + }; +}; +void btrfs_csum (u16 csum_type,const u8 *data,size_t len,u8 *out); +void btrfs_csum_init (struct btrfs_csum_ctx *ctx,u16 csum_type); +void btrfs_csum_update (struct btrfs_csum_ctx *ctx,const u8 *data,size_t len); +void btrfs_csum_final (struct btrfs_csum_ctx *ctx,u8 *out);static inline bool btrfs_is_empty_uuid (const u8 *uuid) {return uuid_is_null ((const uuid_t *)uuid); }
fs/btrfs/inode.c +6 -7
@@ -1,11 +1,10 @@// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2007 Oracle. All rights reserved. */ -#include <crypto/hash.h> #include <linux/kernel.h> #include <linux/bio.h> #include <linux/blk-cgroup.h> #include <linux/file.h> #include <linux/fs.h>
@@ -3333,33 +3332,33 @@ int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered)void btrfs_calculate_block_csum (struct btrfs_fs_info *fs_info,phys_addr_t paddr,u8 *dest) {struct folio *folio =page_folio (phys_to_page (paddr));const u32 blocksize = fs_info->sectorsize; -SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); +struct btrfs_csum_ctx csum; - shash->tfm = fs_info->csum_shash;/* The full block must be inside the folio. */ ASSERT (offset_in_folio (folio, paddr) + blocksize <=folio_size (folio));if (folio_test_partial_kmap (folio)) {size_t cur = paddr; -crypto_shash_init (shash); +btrfs_csum_init (&csum, fs_info->csum_type);while (cur < paddr + blocksize) {void *kaddr;size_t len =min (paddr + blocksize - cur,PAGE_SIZE -offset_in_page (cur)); kaddr =kmap_local_folio (folio,offset_in_folio (folio, cur)); -crypto_shash_update (shash, kaddr, len); +btrfs_csum_update (&csum, kaddr, len);kunmap_local (kaddr); cur += len; } -crypto_shash_final (shash, dest); +btrfs_csum_final (&csum, dest); }else { -crypto_shash_digest (shash,phys_to_virt (paddr), blocksize, dest); +btrfs_csum (fs_info->csum_type,phys_to_virt (paddr), blocksize, + dest); } }/* * Verify the checksum for a single sector without any extra action that depend * on the type of I/O.
fs/btrfs/scrub.c +7 -9
@@ -4,11 +4,10 @@*/ #include <linux/blkdev.h> #include <linux/ratelimit.h> #include <linux/sched/mm.h> -#include <crypto/hash.h> #include "ctree.h" #include "discard.h" #include "volumes.h" #include "disk-io.h" #include "ordered-data.h"
@@ -718,11 +717,11 @@ static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nrstruct btrfs_fs_info *fs_info = stripe->bg->fs_info;const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits);void *first_kaddr =scrub_stripe_get_kaddr (stripe, sector_nr);struct btrfs_header *header = first_kaddr; -SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); +struct btrfs_csum_ctx csum;u8 on_disk_csum[BTRFS_CSUM_SIZE ];u8 calculated_csum[BTRFS_CSUM_SIZE ];/* * Here we don't have a good way to attach the pages (and subpages)
@@ -760,21 +759,20 @@ static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr header->chunk_tree_uuid, fs_info->chunk_tree_uuid);return ; }/* Now check tree block csum. */ - shash->tfm = fs_info->csum_shash; -crypto_shash_init (shash); -crypto_shash_update (shash, first_kaddr +BTRFS_CSUM_SIZE , - fs_info->sectorsize -BTRFS_CSUM_SIZE ); +btrfs_csum_init (&csum, fs_info->csum_type); +btrfs_csum_update (&csum, first_kaddr +BTRFS_CSUM_SIZE , + fs_info->sectorsize -BTRFS_CSUM_SIZE );for (int i = sector_nr +1 ; i < sector_nr + sectors_per_tree; i++) { -crypto_shash_update (shash,scrub_stripe_get_kaddr (stripe, i), - fs_info->sectorsize); +btrfs_csum_update (&csum,scrub_stripe_get_kaddr (stripe, i), + fs_info->sectorsize); } -crypto_shash_final (shash, calculated_csum); +btrfs_csum_final (&csum, calculated_csum);if (memcmp (calculated_csum, on_disk_csum, fs_info->csum_size) !=0 ) {scrub_bitmap_set_meta_error (stripe, sector_nr, sectors_per_tree);scrub_bitmap_set_error (stripe, sector_nr, sectors_per_tree); btrfs_warn_rl(fs_info, "scrub: tree block %llu mirror %u has bad csum, has " CSUM_FMT " want " CSUM_FMT,
fs/btrfs/super.c +0 -4
@@ -2636,9 +2636,5 @@ static int __init init_btrfs_fs(void)late_initcall (init_btrfs_fs); module_exit(exit_btrfs_fs) MODULE_DESCRIPTION("B-Tree File System (BTRFS)");MODULE_LICENSE ("GPL" ); -MODULE_SOFTDEP ("pre: crc32c" ); -MODULE_SOFTDEP ("pre: xxhash64" ); -MODULE_SOFTDEP ("pre: sha256" ); -MODULE_SOFTDEP ("pre: blake2b-256" );
fs/btrfs/sysfs.c +2 -4
@@ -8,11 +8,10 @@#include <linux/slab.h> #include <linux/spinlock.h> #include <linux/completion.h> #include <linux/bug.h> #include <linux/list.h> -#include <crypto/hash.h> #include "messages.h" #include "ctree.h" #include "discard.h" #include "disk-io.h" #include "send.h"
@@ -1250,14 +1249,13 @@ BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);static ssize_t btrfs_checksum_show (struct kobject *kobj,struct kobj_attribute *a,char *buf) {struct btrfs_fs_info *fs_info =to_fs_info (kobj);u16 csum_type =btrfs_super_csum_type (fs_info->super_copy); +const char *csum_name =btrfs_super_csum_name (csum_type); -return sysfs_emit (buf,"%s (%s)\n" , -btrfs_super_csum_name (csum_type), -crypto_shash_driver_name (fs_info->csum_shash)); +return sysfs_emit (buf,"%s (%s-lib)\n" , csum_name, csum_name); } BTRFS_ATTR(, checksum, btrfs_checksum_show); static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
Signature · 1 line
2.51.1.dirty
Jason A. Donenfeld
maintainerRe: [PATCH 01/10] lib/crypto: blake2s: Adjust parameter order of blake2s()
In reply to Eric BiggersQuoted 2 lines · Eric Biggers
Reorder the parameters of
blake2s()from (out, in, key, outlen, inlen, keylen) to (key, keylen, in, inlen, out, outlen).
No objections to putting the size next to the argument. That makes sense. But the order really should be:
out, outlen, in, inlen, key, keylen
in order to match normal APIs that output data. The output argument goes first. The input argument goes next. Auxiliary information goes after.
Eric Biggers
maintainerRe: [PATCH 01/10] lib/crypto: blake2s: Adjust parameter order of blake2s()
Quoted 11 lines · Jason A. Donenfeld
Quoted 2 lines · Eric Biggers
Reorder the parameters of
blake2s()from (out, in, key, outlen, inlen, keylen) to (key, keylen, in, inlen, out, outlen).No objections to putting the size next to the argument. That makes sense. But the order really should be:
out, outlen, in, inlen, key, keylen
in order to match normal APIs that output data. The output argument goes first. The input argument goes next. Auxiliary information goes after.
In general, both conventions are common. But in the other hashing functions in the kernel, we've been using output last. I'd like to prioritize making it consistent with:
md5()
sha1()
sha224()
sha256()
sha384()
sha512()
hmac_md5()
hmac_sha1()
hmac_sha224()
hmac_sha256()
hmac_sha384()
hmac_sha512()
hmac_md5_usingrawkey()
hmac_sha1_usingrawkey()
hmac_sha224_usingrawkey()
hmac_sha256_usingrawkey()
hmac_sha384_usingrawkey()
hmac_sha512_usingrawkey()
crypto_shash_finup()
crypto_shash_digest()
crypto_shash_tfm_digest()
[and the SHA-3 functions in David's patchset]- Eric
Eric Biggers
maintainerRe: [PATCH 07/10] lib/crypto: arm/blake2b: Migrate optimized code into library
In reply to Eric Biggerslib/crypto/Makefile obj-$(CONFIG_CRYPTO_LIB_GF128MUL) += gf128mul.o · line 40
Quoted 12 lines · Eric Biggers
lib/crypto/Makefile +1
-0@@ -34,10 +34,11 @@ obj-$(CONFIG_CRYPTO_LIB_GF128MUL) += gf128mul.o obj-$(CONFIG_CRYPTO_LIB_BLAKE2B) += libblake2b.o libblake2b-y := blake2b.o CFLAGS_blake2b.o := -Wframe-larger-than=4096 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105930 ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2B_ARCH),y) CFLAGS_blake2b.o += -I$(src)/$(SRCARCH) +obj-$(CONFIG_ARM) += arm/blake2b-neon-core.o endif # CONFIG_CRYPTO_LIB_BLAKE2B_ARCH
Correction: it should be
libblake2b-$(CONFIG_ARM) += arm/blake2b-neon-core.o
- Eric
Jason A. Donenfeld
maintainerRe: [PATCH 01/10] lib/crypto: blake2s: Adjust parameter order of blake2s()
In reply to Eric BiggersHi Eric,
Quoted 8 lines · Eric Biggers
Quoted 7 lines · Jason A. Donenfeld
Quoted 2 lines · Eric Biggers
Reorder the parameters of
blake2s()from (out, in, key, outlen, inlen, keylen) to (key, keylen, in, inlen, out, outlen).No objections to putting the size next to the argument. That makes sense. But the order really should be:
Quoted 8 lines
Quoted 4 lines
out, outlen, in, inlen, key, keylen
in order to match normal APIs that output data. The output argument goes first. The input argument goes next. Auxiliary information goes after.
In general, both conventions are common. But in the other hashing functions in the kernel, we've been using output last. I'd like to prioritize making it consistent with:
Hm. I don't like that. But I guess if that's what every-single-other-hash-function-does, then blake2s should follow the convention, to avoid churn of adding something new?
I went looking at C crypto libraries to see what generally the trend is, and I saw that crypto_hash from nacl and libsodium and supercop do out, in, as does cryptlib, but beyond that, most libraries don't provide an all-in-one-interface but only have init/update/final. So however you see fit, I guess; I don't want to hold up progress.
Jason
kernel test robot
build botRe: [PATCH 08/10] lib/crypto: tests: Add KUnit tests for BLAKE2b
In reply to Eric Biggerskernel test robot noticed the following build errors:
Hi Eric,
kernel test robot noticed the following build errors:
[auto build test ERROR on 123fa1574bccee87da735d13e89c931e88288b40]
url: https://github.com/intel-lab-lkp/linux/commits/Eric-Biggers/lib-crypto-blake2s-Adjust-parameter-order-of-blake2s/20251018-124015 base: 123fa1574bccee87da735d13e89c931e88288b40 patch link: https://lore.kernel.org/r/20251018043106.375964-9-ebiggers%40kernel.org patch subject: [PATCH 08/10] lib/crypto: tests: Add KUnit tests for BLAKE2b config: arm-allmodconfig (https://download.01.org/0day-ci/archive/20251022/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251022/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>, old ones prefixed by <<):
WARNING: modpost: missing MODULE_DESCRIPTION() in arch/arm/probes/kprobes/test-kprobes.o
>> ERROR: modpost: "blake2b_compress_neon" [lib/crypto/libblake2b.ko] undefined!
ERROR: modpost: "__ffsdi2" [drivers/spi/spi-amlogic-spifc-a4.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH 10/10] btrfs: switch to library APIs for checksums
In reply to Eric BiggersQuoted 25 lines · Eric Biggers
Make btrfs use the library APIs instead of
crypto_shash, for all checksum computations. This has many benefits:- Allows future checksum types, e.g. XXH3 or CRC64, to be more easily supported. Only a library API will be needed, not
crypto_shashtoo.- Eliminates the overhead of the generic crypto layer, including an indirect call for every function call and other API overhead. A microbenchmark of
btrfs_check_read_bio()with crc32c checksums shows a speedup from 658 cycles to 608 cycles per 4096-byte block.- Decreases the stack usage of btrfs by reducing the size of checksum contexts from 384 bytes to 240 bytes, and by eliminating the need for some functions to declare a checksum context at all.
- Increases reliability. The library functions always succeed and return void. In contrast,
crypto_shashcan fail and return errors. Also, the library functions are guaranteed to be available when btrfs is loaded; there's no longer any need to use module softdeps to try to work around the crypto modules sometimes not being loaded.- Fixes a bug where blake2b checksums didn't work on kernels booted with fips=1. Since btrfs checksums are for integrity only, it's fine for them to use non-FIPS-approved algorithms.
Quoted 6 lines
Note that with having to handle 4 algorithms instead of just 1-2, this commit does result in a slightly positive diffstat. That being said, this wouldn't have been the case if btrfs had actually checked for errors from
crypto_shash, which technically it should have been doing.Signed-off-by Eric Biggers <[email protected]>
Thanks, this simplifies quite a few things. I'd like to take it via the btrfs tree as there may be the hash additions (XXH3, BLAKE3) but currently I'm not sure if it won't make things more complicated. I haven't started the kernel part yet so I can use this patchset for development and rebase once it's merged.
Quoted 21 lines · Eric Biggers
This series can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git blake2b-lib-v1
This series adds BLAKE2b support to
lib/crypto/and reimplements the blake2b-*crypto_shashalgorithms on top of it.To prepare for that, patches 1-4 clean up the BLAKE2s library code a bit, and patch 5 adds some missing 64-bit byteorder helper functions. Patches 6-8 add the BLAKE2b library API (closely mirroring the BLAKE2s one), and patch 9 makes
crypto_shashuse it. As usual, the library APIs are documented (with kerneldoc) and tested (with KUnit).With that done, all of btrfs's checksum algorithms have library APIs. So patch 10 converts btrfs to use the library APIs instead of shash. This has quite a few benefits, as detailed in that patch.
Patches 1-9 are targeting libcrypto-next for 6.19. Patch 10 can go through the btrfs tree later.
Quoted 12 lines
Eric Biggers (10):
lib/crypto: blake2s: Adjust parameter order ofblake2s()lib/crypto: blake2s: Renameblake2s_statetoblake2s_ctxlib/crypto: blake2s: Drop excessive const & rename block => data lib/crypto: blake2s: Document the BLAKE2s library API
byteorder: Addle64_to_cpu_array()andcpu_to_le64_array()lib/crypto: blake2b: Add BLAKE2b library functions lib/crypto: arm/blake2b: Migrate optimized code into library lib/crypto: tests: Add KUnit tests for BLAKE2b
crypto: blake2b - Reimplement using library API
btrfs: switch to library APIs for checksums
Eric Biggers
maintainerRe: [PATCH 10/10] btrfs: switch to library APIs for checksums
In reply to David SterbaQuoted 31 lines · David Sterba
Quoted 30 lines · Eric Biggers
Make btrfs use the library APIs instead of
crypto_shash, for all checksum computations. This has many benefits:- Allows future checksum types, e.g. XXH3 or CRC64, to be more easily supported. Only a library API will be needed, not
crypto_shashtoo.- Eliminates the overhead of the generic crypto layer, including an indirect call for every function call and other API overhead. A microbenchmark of
btrfs_check_read_bio()with crc32c checksums shows a speedup from 658 cycles to 608 cycles per 4096-byte block.- Decreases the stack usage of btrfs by reducing the size of checksum contexts from 384 bytes to 240 bytes, and by eliminating the need for some functions to declare a checksum context at all.
- Increases reliability. The library functions always succeed and return void. In contrast,
crypto_shashcan fail and return errors. Also, the library functions are guaranteed to be available when btrfs is loaded; there's no longer any need to use module softdeps to try to work around the crypto modules sometimes not being loaded.- Fixes a bug where blake2b checksums didn't work on kernels booted with fips=1. Since btrfs checksums are for integrity only, it's fine for them to use non-FIPS-approved algorithms.
Note that with having to handle 4 algorithms instead of just 1-2, this commit does result in a slightly positive diffstat. That being said, this wouldn't have been the case if btrfs had actually checked for errors from
crypto_shash, which technically it should have been doing.
Quoted 7 lines
Quoted 1 line
Signed-off-by Eric Biggers <[email protected]>Thanks, this simplifies quite a few things. I'd like to take it via the btrfs tree as there may be the hash additions (XXH3, BLAKE3) but currently I'm not sure if it won't make things more complicated. I haven't started the kernel part yet so I can use this patchset for development and rebase once it's merged.
Great. I'm planning to take patches 1-9 through libcrypto-next for 6.19. You can then take patch 10 through the btrfs tree for 6.20. Does that sound good? We can work out the XXH3 and BLAKE3 support later. If you'd like to add another checksum algorithm, I'd suggest picking just one. btrfs already supports an awful lot of choices for the checksum. But we can discuss that later.
- Eric
Re: [PATCH 10/10] btrfs: switch to library APIs for checksums
Quoted 9 lines · Eric Biggers
Quoted 5 lines
Thanks, this simplifies quite a few things. I'd like to take it via the btrfs tree as there may be the hash additions (XXH3, BLAKE3) but currently I'm not sure if it won't make things more complicated. I haven't started the kernel part yet so I can use this patchset for development and rebase once it's merged.
Great. I'm planning to take patches 1-9 through libcrypto-next for 6.19. You can then take patch 10 through the btrfs tree for 6.20. Does that sound good?
Yes, the 6.20 schedule works better for me.
Quoted 4 lines
We can work out the XXH3 and BLAKE3 support later. If you'd like to add another checksum algorithm, I'd suggest picking just one. btrfs already supports an awful lot of choices for the checksum. But we can discuss that later.
Yes, I'v deleted long answer to that, it would be better to discuss that separately once the xxh3 and blake3 get posted.
Eric Biggers
maintainerQuoted 20 lines · Eric Biggers
This series can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git blake2b-lib-v1
This series adds BLAKE2b support to
lib/crypto/and reimplements the blake2b-*crypto_shashalgorithms on top of it.To prepare for that, patches 1-4 clean up the BLAKE2s library code a bit, and patch 5 adds some missing 64-bit byteorder helper functions. Patches 6-8 add the BLAKE2b library API (closely mirroring the BLAKE2s one), and patch 9 makes
crypto_shashuse it. As usual, the library APIs are documented (with kerneldoc) and tested (with KUnit).With that done, all of btrfs's checksum algorithms have library APIs. So patch 10 converts btrfs to use the library APIs instead of shash. This has quite a few benefits, as detailed in that patch.
Patches 1-9 are targeting libcrypto-next for 6.19. Patch 10 can go through the btrfs tree later.
Quoted 11 lines
Eric Biggers (10):
lib/crypto: blake2s: Adjust parameter order ofblake2s()lib/crypto: blake2s: Renameblake2s_statetoblake2s_ctxlib/crypto: blake2s: Drop excessive const & rename block => data lib/crypto: blake2s: Document the BLAKE2s library API
byteorder: Addle64_to_cpu_array()andcpu_to_le64_array()lib/crypto: blake2b: Add BLAKE2b library functions lib/crypto: arm/blake2b: Migrate optimized code into library lib/crypto: tests: Add KUnit tests for BLAKE2b
crypto: blake2b - Reimplement using library API
btrfs: switch to library APIs for checksums
Applied patches 1-9 (i.e., all except the btrfs patch) to
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next
I folded the following fixup into patch 7 to address
https://lore.kernel.org/r/20251019163249.GD1604@sol/ and
https://lore.kernel.org/r/[email protected]/
lib/crypto/Makefile +1 -1
@@ -37,5 +37,5 @@ CFLAGS_blake2b.o := -Wframe-larger-than=4096 # https://gcc.gnu.org/bugzilla/sho ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2B_ARCH),y) CFLAGS_blake2b.o += -I$(src)/$(SRCARCH) -obj-$(CONFIG_ARM) += arm/blake2b-neon-core.o +libblake2b-$(CONFIG_ARM) += arm/blake2b-neon-core.o endif # CONFIG_CRYPTO_LIB_BLAKE2B_ARCH