crypto: add NEON-optimized BLAKE2b
Subsystems (2)
PATCH 1/5 crypto: blake2b - rename constants for consistency with blake2s
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Rename some BLAKE2b-related constants to be consistent with the names used in the BLAKE2s implementation (see include/crypto/blake2s.h):
BLAKE2B_*_DIGEST_SIZE => BLAKE2B_*_HASH_SIZE BLAKE2B_BLOCKBYTES => BLAKE2B_BLOCK_SIZE BLAKE2B_KEYBYTES => BLAKE2B_KEY_SIZE
--- crypto/blake2b_generic.c | 58 +++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 28 deletions(-)
crypto/blake2b_generic.c +30 -28
@@ -25,21 +25,22 @@#include <linux/bitops.h> #include <crypto/internal/hash.h> -#define BLAKE2B_160_DIGEST_SIZE (160 / 8) -#define BLAKE2B_256_DIGEST_SIZE (256 / 8) -#define BLAKE2B_384_DIGEST_SIZE (384 / 8) -#define BLAKE2B_512_DIGEST_SIZE (512 / 8) - -enum blake2b_constant { -BLAKE2B_BLOCKBYTES =128 , -BLAKE2B_KEYBYTES =64 , + +enum blake2b_lengths { +BLAKE2B_BLOCK_SIZE =128 , +BLAKE2B_KEY_SIZE =64 , + +BLAKE2B_160_HASH_SIZE =20 , +BLAKE2B_256_HASH_SIZE =32 , +BLAKE2B_384_HASH_SIZE =48 , +BLAKE2B_512_HASH_SIZE =64 , };struct blake2b_state {u64 h[8 ];u64 t[2 ];u64 f[2 ]; -u8 buf[BLAKE2B_BLOCKBYTES ]; +u8 buf[BLAKE2B_BLOCK_SIZE ];size_t buflen; };
@@ -96,7 +97,7 @@ static void blake2b_increment_counter(struct blake2b_state *S, const u64 inc) }while (0 ) static void blake2b_compress(struct blake2b_state *S, -const u8 block[BLAKE2B_BLOCKBYTES ]) +const u8 block[BLAKE2B_BLOCK_SIZE ]) {u64 m[16 ];u64 v[16 ];
@@ -140,7 +141,7 @@ static void blake2b_compress(struct blake2b_state *S,#undef ROUNDstruct blake2b_tfm_ctx { -u8 key[BLAKE2B_KEYBYTES ]; +u8 key[BLAKE2B_KEY_SIZE ];unsigned int keylen; };
@@ -149,7 +150,7 @@ static int blake2b_setkey(struct crypto_shash *tfm, const u8 *key, {struct blake2b_tfm_ctx *tctx =crypto_shash_ctx (tfm); -if (keylen ==0 || keylen >BLAKE2B_KEYBYTES ) +if (keylen ==0 || keylen >BLAKE2B_KEY_SIZE )return -EINVAL ;memcpy (tctx->key, key, keylen);
@@ -176,7 +177,7 @@ static int blake2b_init(struct shash_desc *desc)* _final will process it */ memcpy (state->buf, tctx->key, tctx->keylen); - state->buflen =BLAKE2B_BLOCKBYTES ; + state->buflen =BLAKE2B_BLOCK_SIZE ; }return 0 ; }
@@ -186,7 +187,7 @@ static int blake2b_update(struct shash_desc *desc, const u8 *in, {struct blake2b_state *state =shash_desc_ctx (desc);const size_t left = state->buflen; -const size_t fill =BLAKE2B_BLOCKBYTES - left; +const size_t fill =BLAKE2B_BLOCK_SIZE - left;if (!inlen)return 0 ;
@@ -195,16 +196,16 @@ static int blake2b_update(struct shash_desc *desc, const u8 *in, state->buflen =0 ;/* Fill buffer */ memcpy (state->buf + left, in, fill); -blake2b_increment_counter (state,BLAKE2B_BLOCKBYTES ); +blake2b_increment_counter (state,BLAKE2B_BLOCK_SIZE );/* Compress */ blake2b_compress (state, state->buf); in += fill; inlen -= fill; -while (inlen >BLAKE2B_BLOCKBYTES ) { -blake2b_increment_counter (state,BLAKE2B_BLOCKBYTES ); +while (inlen >BLAKE2B_BLOCK_SIZE ) { +blake2b_increment_counter (state,BLAKE2B_BLOCK_SIZE );blake2b_compress (state, in); - in +=BLAKE2B_BLOCKBYTES ; - inlen -=BLAKE2B_BLOCKBYTES ; + in +=BLAKE2B_BLOCK_SIZE ; + inlen -=BLAKE2B_BLOCK_SIZE ; } }memcpy (state->buf + state->buflen, in, inlen);
@@ -223,7 +224,8 @@ static int blake2b_final(struct shash_desc *desc, u8 *out)/* Set last block */ state->f[0 ] = (u64)-1 ;/* Padding */ -memset (state->buf + state->buflen,0 ,BLAKE2B_BLOCKBYTES - state->buflen); +memset (state->buf + state->buflen,0 , +BLAKE2B_BLOCK_SIZE - state->buflen);blake2b_compress (state, state->buf);/* Avoid temporary buffer and switch the internal output to LE order */
@@ -240,10 +242,10 @@ static struct shash_alg blake2b_algs[] = { .base.cra_driver_name = "blake2b-160-generic", .base.cra_priority = 100, .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, - .base.cra_blocksize = BLAKE2B_BLOCKBYTES, + .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), .base.cra_module = THIS_MODULE, - .digestsize = BLAKE2B_160_DIGEST_SIZE, + .digestsize = BLAKE2B_160_HASH_SIZE, .setkey = blake2b_setkey, .init = blake2b_init, .update = blake2b_update,
@@ -254,10 +256,10 @@ static struct shash_alg blake2b_algs[] = { .base.cra_driver_name = "blake2b-256-generic", .base.cra_priority = 100, .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, - .base.cra_blocksize = BLAKE2B_BLOCKBYTES, + .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), .base.cra_module = THIS_MODULE, - .digestsize = BLAKE2B_256_DIGEST_SIZE, + .digestsize = BLAKE2B_256_HASH_SIZE, .setkey = blake2b_setkey, .init = blake2b_init, .update = blake2b_update,
@@ -268,10 +270,10 @@ static struct shash_alg blake2b_algs[] = { .base.cra_driver_name = "blake2b-384-generic", .base.cra_priority = 100, .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, - .base.cra_blocksize = BLAKE2B_BLOCKBYTES, + .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), .base.cra_module = THIS_MODULE, - .digestsize = BLAKE2B_384_DIGEST_SIZE, + .digestsize = BLAKE2B_384_HASH_SIZE, .setkey = blake2b_setkey, .init = blake2b_init, .update = blake2b_update,
@@ -282,10 +284,10 @@ static struct shash_alg blake2b_algs[] = { .base.cra_driver_name = "blake2b-512-generic", .base.cra_priority = 100, .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, - .base.cra_blocksize = BLAKE2B_BLOCKBYTES, + .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), .base.cra_module = THIS_MODULE, - .digestsize = BLAKE2B_512_DIGEST_SIZE, + .digestsize = BLAKE2B_512_HASH_SIZE, .setkey = blake2b_setkey, .init = blake2b_init, .update = blake2b_update,
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 2/5 crypto: blake2b - define shash_alg structs using macros
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
The shash_alg structs for the four variants of BLAKE2b are identical except for the algorithm name, driver name, and digest size. So, avoid code duplication by using a macro to define these structs.
--- crypto/blake2b_generic.c | 82 ++++++++++++---------------------------- 1 file changed, 25 insertions(+), 57 deletions(-)
crypto/blake2b_generic.c +25 -57
@@ -236,64 +236,32 @@ static int blake2b_final(struct shash_desc *desc, u8 *out)return 0 ; } -static struct shash_alg blake2b_algs[] = { - { - .base.cra_name ="blake2b-160" , - .base.cra_driver_name ="blake2b-160-generic" , - .base.cra_priority =100 , - .base.cra_flags =CRYPTO_ALG_OPTIONAL_KEY , - .base.cra_blocksize =BLAKE2B_BLOCK_SIZE , - .base.cra_ctxsize =sizeof (struct blake2b_tfm_ctx ), - .base.cra_module =THIS_MODULE , - .digestsize =BLAKE2B_160_HASH_SIZE , - .setkey = blake2b_setkey, - .init = blake2b_init, - .update = blake2b_update, - .final = blake2b_final, - .descsize =sizeof (struct blake2b_state ), - }, { - .base.cra_name ="blake2b-256" , - .base.cra_driver_name ="blake2b-256-generic" , - .base.cra_priority =100 , - .base.cra_flags =CRYPTO_ALG_OPTIONAL_KEY , - .base.cra_blocksize =BLAKE2B_BLOCK_SIZE , - .base.cra_ctxsize =sizeof (struct blake2b_tfm_ctx ), - .base.cra_module =THIS_MODULE , - .digestsize =BLAKE2B_256_HASH_SIZE , - .setkey = blake2b_setkey, - .init = blake2b_init, - .update = blake2b_update, - .final = blake2b_final, - .descsize =sizeof (struct blake2b_state ), - }, { - .base.cra_name ="blake2b-384" , - .base.cra_driver_name ="blake2b-384-generic" , - .base.cra_priority =100 , - .base.cra_flags =CRYPTO_ALG_OPTIONAL_KEY , - .base.cra_blocksize =BLAKE2B_BLOCK_SIZE , - .base.cra_ctxsize =sizeof (struct blake2b_tfm_ctx ), - .base.cra_module =THIS_MODULE , - .digestsize =BLAKE2B_384_HASH_SIZE , - .setkey = blake2b_setkey, - .init = blake2b_init, - .update = blake2b_update, - .final = blake2b_final, - .descsize =sizeof (struct blake2b_state ), - }, { - .base.cra_name ="blake2b-512" , - .base.cra_driver_name ="blake2b-512-generic" , - .base.cra_priority =100 , - .base.cra_flags =CRYPTO_ALG_OPTIONAL_KEY , - .base.cra_blocksize =BLAKE2B_BLOCK_SIZE , - .base.cra_ctxsize =sizeof (struct blake2b_tfm_ctx ), - .base.cra_module =THIS_MODULE , - .digestsize =BLAKE2B_512_HASH_SIZE , - .setkey = blake2b_setkey, - .init = blake2b_init, - .update = blake2b_update, - .final = blake2b_final, - .descsize =sizeof (struct blake2b_state ), +#define BLAKE2B_ALG (name, driver_name, digest_size) \ + { \ + .base.cra_name = name, \ + .base.cra_driver_name = driver_name, \ + .base.cra_priority = 100, \ + .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, \ + .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, \ + .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), \ + .base.cra_module = THIS_MODULE, \ + .digestsize = digest_size, \ + .setkey = blake2b_setkey, \ + .init = blake2b_init, \ + .update = blake2b_update, \ + .final = blake2b_final, \ + .descsize = sizeof(struct blake2b_state), \ } + +static struct shash_alg blake2b_algs[] = { +BLAKE2B_ALG ("blake2b-160" ,"blake2b-160-generic" , +BLAKE2B_160_HASH_SIZE ), +BLAKE2B_ALG ("blake2b-256" ,"blake2b-256-generic" , +BLAKE2B_256_HASH_SIZE ), +BLAKE2B_ALG ("blake2b-384" ,"blake2b-384-generic" , +BLAKE2B_384_HASH_SIZE ), +BLAKE2B_ALG ("blake2b-512" ,"blake2b-512-generic" , +BLAKE2B_512_HASH_SIZE ), };static int __init blake2b_mod_init (void )
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
cover crypto: add NEON-optimized BLAKE2b
This patchset adds a NEON implementation of BLAKE2b for 32-bit ARM. Patches 1-4 prepare for it by making some updates to the generic implementation, while patch 5 adds the actual NEON implementation.
On Cortex-A7 (which these days is the most common ARM processor that doesn't have the ARMv8 Crypto Extensions), this is over twice as fast as SHA-256, and slightly faster than SHA-1. It is also almost three times as fast as the generic implementation of BLAKE2b:
Algorithm Cycles per byte (on 4096-byte messages) =================== ======================================= blake2b-256-neon 14.1 sha1-neon 16.4 sha1-asm 20.8 blake2s-256-generic 26.1 sha256-neon 28.9 sha256-asm 32.1 blake2b-256-generic 39.9
This implementation isn't directly based on any other implementation, but it borrows some ideas from previous NEON code I've written as well as from chacha-neon-core.S. At least on Cortex-A7, it is faster than the other NEON implementations of BLAKE2b I'm aware of (the implementation in the BLAKE2 official repository using intrinsics, and Andrew Moon's implementation which can be found in SUPERCOP).
NEON-optimized BLAKE2b is useful because there is interest in using BLAKE2b-256 for dm-verity on low-end Android devices (specifically, devices that lack the ARMv8 Crypto Extensions) to replace SHA-1. On these devices, the performance cost of upgrading to SHA-256 may be unacceptable, whereas BLAKE2b-256 would actually improve performance.
Although BLAKE2b is intended for 64-bit platforms (unlike BLAKE2s which is intended for 32-bit platforms), on 32-bit ARM processors with NEON, BLAKE2b is actually faster than BLAKE2s. This is because NEON supports 64-bit operations, and because BLAKE2s's block size is too small for NEON to be helpful for it. The best I've been able to do with BLAKE2s on Cortex-A7 is 19.0 cpb with an optimized scalar implementation.
(I didn't try BLAKE2sp and BLAKE3, which in theory would be faster, but they're more complex as they require running multiple hashes at once. Note that BLAKE2b already uses all the NEON bandwidth on the Cortex-A7, so I expect that any speedup from BLAKE2sp or BLAKE3 would come only from the smaller number of rounds, not from the extra parallelism.)
This patchset was tested on a Raspberry Pi 2, including with CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y.
Eric Biggers (5):
crypto: blake2b - rename constants for consistency with blake2s
crypto: blake2b - define shash_alg structs using macros
crypto: blake2b - export helpers for optimized implementations
crypto: blake2b - update file comment
crypto: arm/blake2b - add NEON-optimized BLAKE2b implementation
arch/arm/crypto/Kconfig | 10 + arch/arm/crypto/Makefile | 2 + arch/arm/crypto/blake2b-neon-core.S | 357 ++++++++++++++++++++++++++++ arch/arm/crypto/blake2b-neon-glue.c | 105 ++++++++ crypto/blake2b_generic.c | 205 +++++++--------- include/crypto/blake2b.h | 54 +++++ 6 files changed, 619 insertions(+), 114 deletions(-) create mode 100644 arch/arm/crypto/blake2b-neon-core.S create mode 100644 arch/arm/crypto/blake2b-neon-glue.c create mode 100644 include/crypto/blake2b.h
base-commit: 3db1a3fa98808aa90f95ec3e0fa2fc7abf28f5c9
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 4/5 crypto: blake2b - update file comment
From: Eric Biggers <[email protected]>
The file comment for blake2b_generic.c makes it sound like it's the reference implementation of BLAKE2b with only minor changes. But it's actually been changed a lot. Update the comment to make this clearer.
--- crypto/blake2b_generic.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
crypto/blake2b_generic.c +10 -13
@@ -1,21 +1,18 @@// SPDX-License-Identifier: (GPL-2.0-only OR Apache-2.0) /* -* BLAKE2b reference source code package - reference C implementations +* Generic implementation of the BLAKE2b digest algorithm. Based on the BLAKE2b +* reference implementation, but it has been heavily modified for use in the +* kernel. The reference implementation was: * -* 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: +* 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 : https://www.apache.org/licenses/LICENSE-2.0 +* - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 +* - OpenSSL license : https://www.openssl.org/source/license.html +* - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0 * -* More information about the BLAKE2 hash function can be found at -* https://blake2.net. -* -* Note: the original sources have been modified for inclusion in linux kernel -* in terms of coding style, using generic helpers and simplifications of error -* handling. +* More information about BLAKE2 can be found at https://blake2.net. */ #include <asm/unaligned.h>
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 3/5 crypto: blake2b - export helpers for optimized implementations
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
In preparation for adding architecture-specific implementations of BLAKE2b, create a header <crypto/blake2b.h> that contains common constants, structs, and helper functions for BLAKE2b.
Furthermore, export the BLAKE2b generic setkey(), init(), update(), and final() functions, and add functions __crypto_blake2b_update() and __crypto_blake2b_final() which take a pointer to a blake2b_compress_blocks_t function.
This way, optimized implementations of BLAKE2b only have to provide an implementation of blake2b_compress_blocks_t. (This is modeled on how the nhpoly1305 implementations work. Also, the prototype of blake2b_compress_blocks_t is meant to be similar to that of blake2s_compress_arch().)
--- crypto/blake2b_generic.c | 100 +++++++++++++++++++++------------------ include/crypto/blake2b.h | 54 +++++++++++++++++++++ 2 files changed, 109 insertions(+), 45 deletions(-) create mode 100644 include/crypto/blake2b.h
crypto/blake2b_generic.c +55 -45
@@ -23,27 +23,9 @@#include <linux/string.h> #include <linux/kernel.h> #include <linux/bitops.h> +#include <crypto/blake2b.h> #include <crypto/internal/hash.h> - -enum blake2b_lengths { -BLAKE2B_BLOCK_SIZE =128 , -BLAKE2B_KEY_SIZE =64 , - -BLAKE2B_160_HASH_SIZE =20 , -BLAKE2B_256_HASH_SIZE =32 , -BLAKE2B_384_HASH_SIZE =48 , -BLAKE2B_512_HASH_SIZE =64 , -}; - -struct blake2b_state { -u64 h[8 ]; -u64 t[2 ]; -u64 f[2 ]; -u8 buf[BLAKE2B_BLOCK_SIZE ]; -size_t buflen; -}; -static const u64 blake2b_IV[8 ] = {0x6a09e667f3bcc908ULL ,0xbb67ae8584caa73bULL ,0x3c6ef372fe94f82bULL ,0xa54ff53a5f1d36f1ULL ,
@@ -96,8 +78,8 @@ static void blake2b_increment_counter(struct blake2b_state *S, const u64 inc)G (r,7 ,v[3 ],v[4 ],v[9 ],v[14 ]); \ }while (0 ) -static void blake2b_compress(struct blake2b_state *S, -const u8 block[BLAKE2B_BLOCK_SIZE ]) +static void blake2b_compress_generic(struct blake2b_state *S, +const u8 block[BLAKE2B_BLOCK_SIZE ]) {u64 m[16 ];u64 v[16 ];
@@ -140,12 +122,18 @@ static void blake2b_compress(struct blake2b_state *S,#undef G#undef ROUND -struct blake2b_tfm_ctx { -u8 key[BLAKE2B_KEY_SIZE ]; -unsigned int keylen; -}; +static void blake2b_compress_blocks_generic (struct blake2b_state *S , +const u8 *in,size_t nblocks, +unsigned int inc) +{ +do { +blake2b_increment_counter (S , inc); +blake2b_compress_generic (S , in); + in +=BLAKE2B_BLOCK_SIZE ; + }while (--nblocks); +} -static int blake2b_setkey (struct crypto_shash *tfm,const u8 *key, +int crypto_blake2b_setkey (struct crypto_shash *tfm,const u8 *key,unsigned int keylen) {struct blake2b_tfm_ctx *tctx =crypto_shash_ctx (tfm);
@@ -158,8 +146,9 @@ static int blake2b_setkey(struct crypto_shash *tfm, const u8 *key,return 0 ; } +EXPORT_SYMBOL_GPL (crypto_blake2b_setkey); -static int blake2b_init (struct shash_desc *desc) +int crypto_blake2b_init (struct shash_desc *desc) {struct blake2b_tfm_ctx *tctx =crypto_shash_ctx (desc->tfm);struct blake2b_state *state =shash_desc_ctx (desc);
@@ -181,9 +170,19 @@ static int blake2b_init(struct shash_desc *desc) }return 0 ; } +EXPORT_SYMBOL_GPL (crypto_blake2b_init); + +int crypto_blake2b_update (struct shash_desc *desc, +const u8 *in,unsigned int inlen) +{ +return __crypto_blake2b_update (desc, in, inlen, + blake2b_compress_blocks_generic); +} +EXPORT_SYMBOL_GPL (crypto_blake2b_update); -static int blake2b_update (struct shash_desc *desc,const u8 *in, -unsigned int inlen) +int __crypto_blake2b_update (struct shash_desc *desc, +const u8 *in,unsigned int inlen, +blake2b_compress_blocks_t compress) {struct blake2b_state *state =shash_desc_ctx (desc);const size_t left = state->buflen;
@@ -194,18 +193,19 @@ static int blake2b_update(struct shash_desc *desc, const u8 *in,if (inlen > fill) { state->buflen =0 ; -/* Fill buffer */ memcpy (state->buf + left, in, fill); -blake2b_increment_counter (state,BLAKE2B_BLOCK_SIZE ); -/* Compress */ -blake2b_compress (state, state->buf); + (*compress)(state, state->buf,1 ,BLAKE2B_BLOCK_SIZE ); in += fill; inlen -= fill; -while (inlen >BLAKE2B_BLOCK_SIZE ) { -blake2b_increment_counter (state,BLAKE2B_BLOCK_SIZE ); -blake2b_compress (state, in); - in +=BLAKE2B_BLOCK_SIZE ; - inlen -=BLAKE2B_BLOCK_SIZE ; +if (inlen >BLAKE2B_BLOCK_SIZE ) { +/* Hash one less (full) block than strictly possible */ +size_t nbytes =round_up (inlen -BLAKE2B_BLOCK_SIZE , +BLAKE2B_BLOCK_SIZE ); + + (*compress)(state, in, nbytes /BLAKE2B_BLOCK_SIZE , +BLAKE2B_BLOCK_SIZE ); + in += nbytes; + inlen -= nbytes; } }memcpy (state->buf + state->buflen, in, inlen);
@@ -213,20 +213,29 @@ static int blake2b_update(struct shash_desc *desc, const u8 *in,return 0 ; } +EXPORT_SYMBOL_GPL (__crypto_blake2b_update); -static int blake2b_final (struct shash_desc *desc,u8 *out) +int crypto_blake2b_final (struct shash_desc *desc,u8 *out) +{ +return __crypto_blake2b_final (desc, out, + blake2b_compress_blocks_generic); +} +EXPORT_SYMBOL_GPL (crypto_blake2b_final); + +int __crypto_blake2b_final (struct shash_desc *desc,u8 *out, +blake2b_compress_blocks_t compress) {struct blake2b_state *state =shash_desc_ctx (desc);const int digestsize =crypto_shash_digestsize (desc->tfm);size_t i; -blake2b_increment_counter (state, state->buflen);/* Set last block */ state->f[0 ] = (u64)-1 ;/* Padding */ memset (state->buf + state->buflen,0 ,BLAKE2B_BLOCK_SIZE - state->buflen); -blake2b_compress (state, state->buf); + + (*compress)(state, state->buf,1 , state->buflen);/* Avoid temporary buffer and switch the internal output to LE order */ for (i =0 ; i <ARRAY_SIZE (state->h); i++)
@@ -235,6 +244,7 @@ static int blake2b_final(struct shash_desc *desc, u8 *out)memcpy (out, state->h, digestsize);return 0 ; } +EXPORT_SYMBOL_GPL (__crypto_blake2b_final);#define BLAKE2B_ALG (name, driver_name, digest_size) \ { \
@@ -246,10 +256,10 @@ static int blake2b_final(struct shash_desc *desc, u8 *out) .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), \ .base.cra_module = THIS_MODULE, \ .digestsize = digest_size, \ - .setkey = blake2b_setkey, \ - .init = blake2b_init, \ - .update = blake2b_update, \ - .final = blake2b_final, \ + .setkey = crypto_blake2b_setkey, \ + .init = crypto_blake2b_init, \ + .update = crypto_blake2b_update, \ + .final = crypto_blake2b_final, \ .descsize = sizeof(struct blake2b_state), \ }
include/crypto/blake2b.h new file +54 -0
54 lines not on this page · raw message
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 5/5 crypto: arm/blake2b - add NEON-optimized BLAKE2b implementation
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add a NEON-optimized implementation of BLAKE2b.
On Cortex-A7 (which these days is the most common ARM processor that doesn't have the ARMv8 Crypto Extensions), this is over twice as fast as SHA-256, and slightly faster than SHA-1. It is also almost three times as fast as the generic implementation of BLAKE2b:
Algorithm Cycles per byte (on 4096-byte messages) =================== ======================================= blake2b-256-neon 14.1 sha1-neon 16.4 sha1-asm 20.8 blake2s-256-generic 26.1 sha256-neon 28.9 sha256-asm 32.1 blake2b-256-generic 39.9
This implementation isn't directly based on any other implementation, but it borrows some ideas from previous NEON code I've written as well as from chacha-neon-core.S. At least on Cortex-A7, it is faster than the other NEON implementations of BLAKE2b I'm aware of (the implementation in the BLAKE2 official repository using intrinsics, and Andrew Moon's implementation which can be found in SUPERCOP).
NEON-optimized BLAKE2b is useful because there is interest in using BLAKE2b-256 for dm-verity on low-end Android devices (specifically, devices that lack the ARMv8 Crypto Extensions) to replace SHA-1. On these devices, the performance cost of upgrading to SHA-256 may be unacceptable, whereas BLAKE2b-256 would actually improve performance.
Although BLAKE2b is intended for 64-bit platforms (unlike BLAKE2s which is intended for 32-bit platforms), on 32-bit ARM processors with NEON, BLAKE2b is actually faster than BLAKE2s. This is because NEON supports 64-bit operations, and because BLAKE2s's block size is too small for NEON to be helpful for it. The best I've been able to do with BLAKE2s on Cortex-A7 is 19.0 cpb with an optimized scalar implementation.
(I didn't try BLAKE2sp and BLAKE3, which in theory would be faster, but they're more complex as they require running multiple hashes at once. Note that BLAKE2b already uses all the NEON bandwidth on the Cortex-A7, so I expect that any speedup from BLAKE2sp or BLAKE3 would come only from the smaller number of rounds, not from the extra parallelism.)
--- arch/arm/crypto/Kconfig | 10 + arch/arm/crypto/Makefile | 2 + arch/arm/crypto/blake2b-neon-core.S | 357 ++++++++++++++++++++++++++++ arch/arm/crypto/blake2b-neon-glue.c | 105 ++++++++ 4 files changed, 474 insertions(+) create mode 100644 arch/arm/crypto/blake2b-neon-core.S create mode 100644 arch/arm/crypto/blake2b-neon-glue.c
arch/arm/crypto/Kconfig +10 -0
@@ -62,6 +62,16 @@ config CRYPTO_SHA512_ARM SHA-512 secure hash standard (DFIPS 180-2) implemented using optimized ARM assembler and NEON, when available. +config CRYPTO_BLAKE2B_NEON + tristate "BLAKE2b digest algorithm (ARM NEON)" + depends on KERNEL_MODE_NEON + select CRYPTO_BLAKE2B + help + 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 SHA-2 and slightly faster than SHA-1. + config CRYPTO_AES_ARM tristate "Scalar AES cipher for ARM" select CRYPTO_ALGAPI
arch/arm/crypto/Makefile +2 -0
@@ -9,6 +9,7 @@ obj-$(CONFIG_CRYPTO_SHA1_ARM) += sha1-arm.o obj-$(CONFIG_CRYPTO_SHA1_ARM_NEON) += sha1-arm-neon.o obj-$(CONFIG_CRYPTO_SHA256_ARM) += sha256-arm.o obj-$(CONFIG_CRYPTO_SHA512_ARM) += sha512-arm.o +obj-$(CONFIG_CRYPTO_BLAKE2B_NEON) += blake2b-neon.o obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha-neon.o obj-$(CONFIG_CRYPTO_POLY1305_ARM) += poly1305-arm.o obj-$(CONFIG_CRYPTO_NHPOLY1305_NEON) += nhpoly1305-neon.o
@@ -29,6 +30,7 @@ sha256-arm-neon-$(CONFIG_KERNEL_MODE_NEON) := sha256_neon_glue.o sha256-arm-y := sha256-core.o sha256_glue.o $(sha256-arm-neon-y) sha512-arm-neon-$(CONFIG_KERNEL_MODE_NEON) := sha512-neon-glue.o sha512-arm-y := sha512-core.o sha512-glue.o $(sha512-arm-neon-y) +blake2b-neon-y := blake2b-neon-core.o blake2b-neon-glue.o sha1-arm-ce-y := sha1-ce-core.o sha1-ce-glue.o sha2-arm-ce-y := sha2-ce-core.o sha2-ce-glue.o aes-arm-ce-y := aes-ce-core.o aes-ce-glue.o
arch/arm/crypto/blake2b-neon-core.S new file +357 -0
357 lines not on this page · raw message
arch/arm/crypto/blake2b-neon-glue.c new file +105 -0
105 lines not on this page · raw message
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 32 lines · Eric Biggers
This patchset adds a NEON implementation of BLAKE2b for 32-bit ARM. Patches 1-4 prepare for it by making some updates to the generic implementation, while patch 5 adds the actual NEON implementation.
On Cortex-A7 (which these days is the most common ARM processor that doesn't have the ARMv8 Crypto Extensions), this is over twice as fast as SHA-256, and slightly faster than SHA-1. It is also almost three times as fast as the generic implementation of BLAKE2b:
Algorithm Cycles per byte (on 4096-byte messages) =================== ======================================= blake2b-256-neon 14.1 sha1-neon 16.4 sha1-asm 20.8 blake2s-256-generic 26.1 sha256-neon 28.9 sha256-asm 32.1 blake2b-256-generic 39.9This implementation isn't directly based on any other implementation, but it borrows some ideas from previous NEON code I've written as well as from
chacha-neon-core.S. At least on Cortex-A7, it is faster than the other NEON implementations of BLAKE2b I'm aware of (the implementation in the BLAKE2 official repository using intrinsics, and Andrew Moon's implementation which can be found in SUPERCOP).NEON-optimized BLAKE2b is useful because there is interest in using BLAKE2b-256 for dm-verity on low-end Android devices (specifically, devices that lack the ARMv8 Crypto Extensions) to replace SHA-1. On these devices, the performance cost of upgrading to SHA-256 may be unacceptable, whereas BLAKE2b-256 would actually improve performance.
Quoted 6 lines
Although BLAKE2b is intended for 64-bit platforms (unlike BLAKE2s which is intended for 32-bit platforms), on 32-bit ARM processors with NEON, BLAKE2b is actually faster than BLAKE2s. This is because NEON supports 64-bit operations, and because BLAKE2s's block size is too small for NEON to be helpful for it. The best I've been able to do with BLAKE2s on Cortex-A7 is 19.0 cpb with an optimized scalar implementation.
By the way, if people are interested in having my ARM scalar implementation of BLAKE2s in the kernel too, I can send a patchset for that too. It just ended up being slower than BLAKE2b and SHA-1, so it wasn't as good for the use case mentioned above. If it were to be added as "blake2s-256-arm", we'd have:
Algorithm Cycles per byte (on 4096-byte messages) =================== ======================================= blake2b-256-neon 14.1 sha1-neon 16.4 blake2s-256-arm 19.0 sha1-asm 20.8 blake2s-256-generic 26.1 sha256-neon 28.9 sha256-asm 32.1 blake2b-256-generic 39.9
Re: [PATCH 5/5] crypto: arm/blake2b - add NEON-optimized BLAKE2b implementation
In reply to Eric BiggersQuoted 8 lines · Eric Biggers
arch/arm/crypto/blake2b-neon-core.S new file +8
-0+// Execute one round of BLAKE2b by updating the state matrix v[0..15] in the +// NEON registers q0-q7. The message block is in q8..q15. The stack pointer +// points to a 32-byte aligned buffer containing a copy of q8 and q9, so that +// they can be reloaded if q8 and q9 are used as temporary registers. The macro +// arguments s0-s15 give the order in which the message words are used in this +// round. 'final' is "true" if this is the final round, i.e. round 12 of 12. +.macro _blake2b_round s0, s1, s2, s3, s4, s5, s6, s7, \ + s8, s9, s10, s11, s12, s13, s14, s15, final="false"
[...]
Quoted 5 lines
arch/arm/crypto/blake2b-neon-core.S new file +5
-0+ // Reloading q8-q9 can be skipped on the final round. +.if \final != "true" + vld1.8 {q8-q9}, [sp, :256] +.endif +.endm
[...]
Review
Quoted 2 lines
(quoted diff) +2
-0+ _blake2b_round 14, 10, 4, 8, 9, 15, 13, 6, \ + 1, 12, 0, 2, 11, 7, 5, 3, "true"
Apparently using the strings "true" and "false" here sometimes causes a build error where they get treated as symbols
(https://lists.01.org/hyperkitty/list/[email protected]/thread/2JPD4H3VFBSKWPUCPEPRAXBVMSR2UCQI/),
though somehow it worked fine for me. I'll change this to use 1 and 0 instead.
- Eric
Hi Eric,
Quoted 4 lines · Eric Biggers
By the way, if people are interested in having my ARM scalar implementation of BLAKE2s in the kernel too, I can send a patchset for that too. It just ended up being slower than BLAKE2b and SHA-1, so it wasn't as good for the use case mentioned above. If it were to be added as "blake2s-256-arm", we'd have:
I'd certainly be interested in this. Any rough idea how it performs for pretty small messages compared to the generic implementation? 100-140 byte ranges? Is the speedup about the same as for longer messages because this doesn't parallelize across multiple blocks?
Jason
Quoted 8 lines · Jason A. Donenfeld
Hi Eric,
Quoted 4 lines · Eric Biggers
By the way, if people are interested in having my ARM scalar implementation of BLAKE2s in the kernel too, I can send a patchset for that too. It just ended up being slower than BLAKE2b and SHA-1, so it wasn't as good for the use case mentioned above. If it were to be added as "blake2s-256-arm", we'd have:
Quoted 5 lines
I'd certainly be interested in this. Any rough idea how it performs for pretty small messages compared to the generic implementation? 100-140 byte ranges? Is the speedup about the same as for longer messages because this doesn't parallelize across multiple blocks?
It does one block at a time, and there isn't much overhead, so yes the speedup on short messages should be about the same as on long messages.
I did a couple quick userspace benchmarks and got (still on Cortex-A7):
100-byte messages:
BLAKE2s ARM: 28.9 cpb
BLAKE2s generic: 42.4 cpb
140-byte messages:
BLAKE2s ARM: 29.5 cpb
BLAKE2s generic: 44.0 cpb
The results in the kernel may differ a bit, but probably not by much.
- Eric
Quoted 25 lines · Eric Biggers
Quoted 8 lines · Jason A. Donenfeld
Hi Eric,
Quoted 4 lines · Eric Biggers
By the way, if people are interested in having my ARM scalar implementation of BLAKE2s in the kernel too, I can send a patchset for that too. It just ended up being slower than BLAKE2b and SHA-1, so it wasn't as good for the use case mentioned above. If it were to be added as "blake2s-256-arm", we'd have:
Quoted 5 lines
I'd certainly be interested in this. Any rough idea how it performs for pretty small messages compared to the generic implementation? 100-140 byte ranges? Is the speedup about the same as for longer messages because this doesn't parallelize across multiple blocks?
It does one block at a time, and there isn't much overhead, so yes the speedup on short messages should be about the same as on long messages.
I did a couple quick userspace benchmarks and got (still on Cortex-A7):
100-byte messages:
BLAKE2s ARM: 28.9 cpb
BLAKE2s generic: 42.4 cpb
Quoted 5 lines
140-byte messages:
BLAKE2s ARM: 29.5 cpb
BLAKE2s generic: 44.0 cpbThe results in the kernel may differ a bit, but probably not by much.
That's certainly a nice improvement though, and I'd very much welcome the faster implementation.
Jason
Re: [PATCH 1/5] crypto: blake2b - rename constants for consistency with blake2s
In reply to Eric BiggersQuoted 10 lines · Eric Biggers
From: Eric Biggers <[email protected]>
Rename some BLAKE2b-related constants to be consistent with the names used in the BLAKE2s implementation (see
include/crypto/blake2s.h):BLAKE2B_*_DIGEST_SIZE => BLAKE2B_*_HASH_SIZE BLAKE2B_BLOCKBYTES => BLAKE2B_BLOCK_SIZE BLAKE2B_KEYBYTES => BLAKE2B_KEY_SIZESigned-off-by Eric Biggers <[email protected]>
Re: [PATCH 2/5] crypto: blake2b - define shash_alg structs using macros
In reply to Eric BiggersQuoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The
shash_algstructs for the four variants of BLAKE2b are identical except for the algorithm name, driver name, and digest size. So, avoid code duplication by using a macro to define these structs.Signed-off-by Eric Biggers <[email protected]>
crypto/blake2b_generic.c static int blake2b_final(struct shash_desc *desc, u8 *out) · line 258
Quoted 3 lines
crypto/blake2b_generic.c +3
-0+static struct shash_alg blake2b_algs[] = { +BLAKE2B_ALG ("blake2b-160" ,"blake2b-160-generic" , +BLAKE2B_160_HASH_SIZE ),
Spelling out the algo names as string is better as it is greppable and matches the module name, compared to using the #stringify macro operator.
Quoted 12 lines
crypto/blake2b_generic.c +6
-0+BLAKE2B_ALG ("blake2b-256" ,"blake2b-256-generic" , +BLAKE2B_256_HASH_SIZE ), +BLAKE2B_ALG ("blake2b-384" ,"blake2b-384-generic" , +BLAKE2B_384_HASH_SIZE ), +BLAKE2B_ALG ("blake2b-512" ,"blake2b-512-generic" , + BLAKE2B_512_HASH_SIZE), };static int __init blake2b_mod_init (void )Signature · 1 line
2.29.2
Re: [PATCH 3/5] crypto: blake2b - export helpers for optimized implementations
In reply to Eric BiggersQuoted 11 lines · Eric Biggers
From: Eric Biggers <[email protected]>
In preparation for adding architecture-specific implementations of BLAKE2b, create a header <
crypto/blake2b.h> that contains common constants, structs, and helper functions for BLAKE2b.Furthermore, export the BLAKE2b generic
setkey(),init(),update(), andfinal()functions, and add functions__crypto_blake2b_update()and__crypto_blake2b_final()which take a pointer to ablake2b_compress_blocks_tfunction.
Quoted 7 lines
This way, optimized implementations of BLAKE2b only have to provide an implementation of
blake2b_compress_blocks_t. (This is modeled on how the nhpoly1305 implementations work. Also, the prototype ofblake2b_compress_blocks_tis meant to be similar to that ofblake2s_compress_arch().)Signed-off-by Eric Biggers <[email protected]>
Re: [PATCH 4/5] crypto: blake2b - update file comment
In reply to Eric BiggersQuoted 5 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The file comment for
blake2b_generic.cmakes it sound like it's the reference implementation of BLAKE2b with only minor changes. But it's actually been changed a lot. Update the comment to make this clearer.
After your changes I agree it appropriate to reword it.
Quoted 1 line
Signed-off-by Eric Biggers <[email protected]>
Re: [PATCH 2/5] crypto: blake2b - define shash_alg structs using macros
In reply to David SterbaQuoted 11 lines · David Sterba
Quoted 7 lines · Eric Biggers
From: Eric Biggers <[email protected]>
The
shash_algstructs for the four variants of BLAKE2b are identical except for the algorithm name, driver name, and digest size. So, avoid code duplication by using a macro to define these structs.Signed-off-by Eric Biggers <[email protected]>Reviewed-by David Sterba <[email protected]>
Quoted 8 lines
crypto/blake2b_generic.c static int blake2b_final(struct shash_desc *desc, u8 *out) · line 258
Quoted 3 lines
crypto/blake2b_generic.c +3
-0+static struct shash_alg blake2b_algs[] = { +BLAKE2B_ALG ("blake2b-160" ,"blake2b-160-generic" , +BLAKE2B_160_HASH_SIZE ),Spelling out the algo names as string is better as it is greppable and matches the module name, compared to using the #stringify macro operator.
Yes, I considered trying to make it so that BLAKE2B_ALG(n) would generate the names and constant for blake2b-$n, but it seemed better to keep it greppable.
- Eric
Re: [PATCH 3/5] crypto: blake2b - export helpers for optimized implementations
In reply to David SterbaQuoted 18 lines · David Sterba
Quoted 17 lines · Eric Biggers
From: Eric Biggers <[email protected]>
In preparation for adding architecture-specific implementations of BLAKE2b, create a header <
crypto/blake2b.h> that contains common constants, structs, and helper functions for BLAKE2b.Furthermore, export the BLAKE2b generic
setkey(),init(),update(), andfinal()functions, and add functions__crypto_blake2b_update()and__crypto_blake2b_final()which take a pointer to ablake2b_compress_blocks_tfunction.This way, optimized implementations of BLAKE2b only have to provide an implementation of
blake2b_compress_blocks_t. (This is modeled on how the nhpoly1305 implementations work. Also, the prototype ofblake2b_compress_blocks_tis meant to be similar to that ofblake2s_compress_arch().)
Quoted 3 lines
Quoted 1 line
Signed-off-by Eric Biggers <[email protected]>Reviewed-by David Sterba <[email protected]>
I ended up making some changes to this patch in v2, to keep things consistent with what I decided to do for BLAKE2s, so I didn't add your Reviewed-by to this patch. If you could review the new version too that would be great -- thanks!
- Eric