lib: add BLAKE2 hash support
Subsystems (2)
PATCH 1/2 lib: add BLAKE2 hash support
The code is cross-ported from BLAKE2 reference implementation
(https://github.com/BLAKE2/BLAKE2).
With minimal change to remove unused macros/features.
Currently there is only one user inside U-boot (btrfs), and since it only utilize BLAKE2B, all other favors are all removed.
--- include/u-boot/blake2.h | 94 ++++++++++++ lib/Kconfig | 8 + lib/Makefile | 1 + lib/blake2/blake2-impl.h | 163 ++++++++++++++++++++ lib/blake2/blake2b.c | 311 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 577 insertions(+) create mode 100644 include/u-boot/blake2.h create mode 100644 lib/blake2/blake2-impl.h create mode 100644 lib/blake2/blake2b.c
include/u-boot/blake2.h new file +94 -0
@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: CC0-1.0 */ +/* +BLAKE2 reference source code package - reference C implementations + +Copyright 2012, Samuel Neves <[email protected]>. You may use this under the +terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at +your option. The terms of these licenses can be found at: + +- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 +- OpenSSL license : https://www.openssl.org/source/license.html +- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + +More information about the BLAKE2 hash function can be found at +https://blake2.net. +*/ + +/* +* Cross-ported from BLAKE2 (https://github.com/BLAKE2/BLAKE2). +* Modifications includes: +* +* - Remove unsupported compilers like MSC/CPP +* - Remove blake2s/blake2sp/blake2bp +* This blake2 implementation is mostly for btrfs, which only utilizes +* blake2b. +*/ +#ifndef BLAKE2_H +#define BLAKE2_H + +#include <stddef.h> +#include <stdint.h> + +#if defined(_MSC_VER) +#define BLAKE2_PACKED (x) __pragma(pack(push, 1)) x __pragma(pack(pop)) +#else +#define BLAKE2_PACKED (x) x __attribute__((packed)) +#endif + +enum blake2b_constant + { +BLAKE2B_BLOCKBYTES =128 , +BLAKE2B_OUTBYTES =64 , +BLAKE2B_KEYBYTES =64 , +BLAKE2B_SALTBYTES =16 , +BLAKE2B_PERSONALBYTES =16 + }; + +typedef struct blake2b_state__ + { +uint64_t h[8 ]; +uint64_t t[2 ]; +uint64_t f[2 ]; +uint8_t buf[BLAKE2B_BLOCKBYTES ]; +size_t buflen; +size_t outlen; +uint8_t last_node; + }blake2b_state ; + +BLAKE2_PACKED (struct blake2b_param__ + { +uint8_t digest_length;/* 1 */ +uint8_t key_length;/* 2 */ +uint8_t fanout;/* 3 */ +uint8_t depth;/* 4 */ +uint32_t leaf_length;/* 8 */ +uint32_t node_offset;/* 12 */ +uint32_t xof_length;/* 16 */ +uint8_t node_depth;/* 17 */ +uint8_t inner_length;/* 18 */ +uint8_t reserved[14 ];/* 32 */ +uint8_t salt[BLAKE2B_SALTBYTES ];/* 48 */ +uint8_t personal[BLAKE2B_PERSONALBYTES ];/* 64 */ + }); + +typedef struct blake2b_param__ blake2b_param ; + +/* Padded structs result in a compile-time error */ +enum { +BLAKE2_DUMMY_2 =1 /(int )(sizeof (blake2b_param) ==BLAKE2B_OUTBYTES ) + }; + +/* Streaming API */ +int blake2b_init (blake2b_state *S ,size_t outlen ); +int blake2b_init_key (blake2b_state *S ,size_t outlen,const void *key,size_t keylen ); +int blake2b_init_param (blake2b_state *S ,const blake2b_param *P ); +int blake2b_update (blake2b_state *S ,const void *in,size_t inlen ); +int blake2b_final (blake2b_state *S ,void *out,size_t outlen ); + +/* Simple API */ +int blake2b (void *out,size_t outlen,const void *in,size_t inlen,const void *key,size_t keylen ); + +/* This is simply an alias for blake2b */ +int blake2 (void *out,size_t outlen,const void *in,size_t inlen,const void *key,size_t keylen ); + +#endif
lib/Kconfig +8 -0
@@ -357,6 +357,14 @@ endmenu menu "Hashing Support" +config BLAKE2 + bool "Enable BLAKE2 support" + help + This option enables support of hashing using BLAKE2B algorithm. + The hash is calculated in software. + The BLAKE2 algorithm produces a hash value (digest) between 1 and + 64 bytes. + config SHA1 bool "Enable SHA1 support" help
lib/Makefile +1 -0
@@ -63,6 +63,7 @@ obj-$(CONFIG_$(SPL_)MD5) += md5.o obj-$(CONFIG_ECDSA) += ecdsa/ obj-$(CONFIG_$(SPL_)RSA) += rsa/ obj-$(CONFIG_HASH) += hash-checksum.o +obj-$(CONFIG_BLAKE2) += blake2/blake2b.o obj-$(CONFIG_SHA1) += sha1.o obj-$(CONFIG_SHA256) += sha256.o obj-$(CONFIG_SHA512) += sha512.o
lib/blake2/blake2-impl.h new file +163 -0
163 lines not on this page · raw message
lib/blake2/blake2b.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 2/2 fs/btrfs: add dependency on BLAKE2 hash
Now btrfs can utilize the newly intorudced BLAKE2 hash.
--- fs/btrfs/Kconfig | 1 + fs/btrfs/crypto/hash.c | 14 ++++++++++++++ fs/btrfs/crypto/hash.h | 1 + fs/btrfs/disk-io.c | 2 ++ 4 files changed, 18 insertions(+)
fs/btrfs/Kconfig +1 -0
@@ -6,6 +6,7 @@ config FS_BTRFS select ZSTD select RBTREE select SHA256 + select BLAKE2 help This provides a single-device read-only BTRFS support. BTRFS is a next-generation Linux file system based on the copy-on-write
fs/btrfs/crypto/hash.c +14 -0
@@ -4,6 +4,7 @@#include <linux/unaligned/access_ok.h> #include <linux/types.h> #include <u-boot/sha256.h> +#include <u-boot/blake2.h> #include <u-boot/crc.h> static u32 btrfs_crc32c_table[256 ];
@@ -39,6 +40,19 @@ int hash_xxhash(const u8 *buf, size_t length, u8 *out)return 0 ; } +/* We use the full CSUM_SIZE(32) for BLAKE2B */ +#define BTRFS_BLAKE2_HASH_SIZE 32 +int hash_blake2 (const u8 *buf,size_t length,u8 *out) +{ +blake2b_state S ; + +blake2b_init (&S ,BTRFS_BLAKE2_HASH_SIZE ); +blake2b_update (&S , buf, length); +blake2b_final (&S , out,BTRFS_BLAKE2_HASH_SIZE ); + +return 0 ; +} +int hash_crc32c (const u8 *buf,size_t length,u8 *out) {u32 crc;
fs/btrfs/crypto/hash.h +1 -0
@@ -9,6 +9,7 @@ void btrfs_hash_init(void);int hash_crc32c (const u8 *buf,size_t length,u8 *out);int hash_xxhash (const u8 *buf,size_t length,u8 *out);int hash_sha256 (const u8 *buf,size_t length,u8 *out); +int hash_blake2 (const u8 *buf,size_t length,u8 *out);u32 crc32c (u32 seed,const void * data,size_t len);
fs/btrfs/disk-io.c +2 -0
@@ -126,6 +126,8 @@ int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)return hash_xxhash (data, len, out);case BTRFS_CSUM_TYPE_SHA256 :return hash_sha256 (data, len, out); +case BTRFS_CSUM_TYPE_BLAKE2 : +return hash_blake2 (data, len, out);default :printf ("Unknown csum type %d\n" , csum_type);return -EINVAL ;
Signature · 1 line
2.34.1
Re: [PATCH 2/2] fs/btrfs: add dependency on BLAKE2 hash
Quoted 3 lines · Qu Wenruo
Now btrfs can utilize the newly intorudced BLAKE2 hash.
Signed-off-by Qu Wenruo <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
Quoted 11 lines · Qu Wenruo
The code is cross-ported from BLAKE2 reference implementation
(https://github.com/BLAKE2/BLAKE2).With minimal change to remove unused macros/features.
Currently there is only one user inside U-boot (btrfs), and since it only utilize BLAKE2B, all other favors are all removed.
Signed-off-by Qu Wenruo <[email protected]>[trini: Rename ROUND to R to avoid clash with <
linux/bitops.h>Signed-off-by Tom Rini <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom