SHA-256 library improvements
Subsystems (12)
Eric Biggers
maintainerv2 cover SHA-256 library improvements
This series is also available at:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git sha256-lib-cleanup-v2
This series improves the SHA-224 and SHA-256 library code to be consistent with what I did for SHA-384 and SHA-512. This includes:
- Use stronger typing in the SHA-224 and SHA-256 functions.
- Add support for HMAC-SHA224 and HMAC-SHA256. (I'll send a separate patch with KUnit test cases for this.)
- Make the old-school crypto API's support for sha224 and sha256 just use the actual library API, instead of unsafe low-level functions.
- Consolidate the CPU-based SHA-224 and SHA-256 code into a single module, with better inlining and dead code elimination.
- Properly document the SHA-224 and SHA-256 functions.
- Other changes to synchronize the code with SHA-384 and SHA-512.
Changed in v2:
- Dropped sha224_kunit.c changes; it will be added later in the history
- Dropped some patches that I folded into the SHA-512 series
- Removed redundant checks of IS_ENABLED(CONFIG_KERNEL_MODE_NEON)
- Removed obsolete setting of -DARCH for sha256.o
- Fixed a commit title to mention sha256 instead of sha512
- Excluded HMAC-SHA{224,256} code from purgatory, where it isn't needed
Eric Biggers (14):
libceph: Rename hmac_sha256() to ceph_hmac_sha256() cxl/test: Simplify fw_buf_checksum_show()
lib/crypto: sha256: Reorder some code
lib/crypto: sha256: Remove sha256_blocks_simd() lib/crypto: sha256: Add sha224() and sha224_update() lib/crypto: sha256: Make library API use strongly-typed contexts lib/crypto: sha256: Propagate sha256_block_state type to
implementations
lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support
crypto: sha256 - Wrap library and add HMAC support
crypto: sha256 - Use same state format as legacy drivers lib/crypto: sha256: Remove sha256_is_arch_optimized() lib/crypto: sha256: Consolidate into single module lib/crypto: sha256: Sync sha256_update() with sha512_update() lib/crypto: sha256: Document the SHA-224 and SHA-256 API
arch/mips/cavium-octeon/Kconfig | 6 -
arch/mips/cavium-octeon/crypto/Makefile | 1 -
arch/riscv/purgatory/purgatory.c | 8 +-
arch/s390/purgatory/purgatory.c | 2 +-
arch/x86/purgatory/purgatory.c | 2 +-
crypto/Kconfig | 4 +-
crypto/Makefile | 1 -
crypto/sha256.c | 371 +++++++++-------
crypto/testmgr.c | 12 +
drivers/char/tpm/tpm2-sessions.c | 12 +-
drivers/crypto/img-hash.c | 4 +-
drivers/crypto/starfive/jh7110-hash.c | 8 +-
include/crypto/internal/sha2.h | 66 ---
include/crypto/sha2.h | 390 +++++++++++++++--
kernel/kexec_file.c | 10 +-
lib/crypto/Kconfig | 34 +-
lib/crypto/Makefile | 39 +-
lib/crypto/arm/Kconfig | 7 -
lib/crypto/arm/Makefile | 8 +-
lib/crypto/arm/sha256-armv4.pl | 20 +-
lib/crypto/arm/sha256-ce.S | 2 +-
lib/crypto/arm/sha256.c | 64 ---
lib/crypto/arm/sha256.h | 46 ++
lib/crypto/arm64/Kconfig | 6 -
lib/crypto/arm64/Makefile | 9 +-
lib/crypto/arm64/sha2-armv8.pl | 2 +-
lib/crypto/arm64/sha256-ce.S | 2 +-
lib/crypto/arm64/sha256.c | 75 ----
lib/crypto/arm64/sha256.h | 57 +++
lib/crypto/arm64/sha512.h | 6 +-
.../crypto/mips/sha256.h | 20 +-
lib/crypto/powerpc/Kconfig | 6 -
lib/crypto/powerpc/Makefile | 3 -
lib/crypto/powerpc/{sha256.c => sha256.h} | 19 +-
lib/crypto/riscv/Kconfig | 8 -
lib/crypto/riscv/Makefile | 3 -
.../sha256-riscv64-zvknha_or_zvknhb-zvkb.S | 2 +-
lib/crypto/riscv/sha256.c | 67 ---
lib/crypto/riscv/sha256.h | 42 ++
lib/crypto/s390/Kconfig | 6 -
lib/crypto/s390/Makefile | 3 -
lib/crypto/s390/sha256.c | 47 --
lib/crypto/s390/sha256.h | 28 ++
lib/crypto/sha256-generic.c | 138 ------
lib/crypto/sha256.c | 413 ++++++++++++++++--
lib/crypto/sparc/Kconfig | 8 -
lib/crypto/sparc/Makefile | 4 -
lib/crypto/sparc/{sha256.c => sha256.h} | 37 +-
lib/crypto/x86/Kconfig | 8 -
lib/crypto/x86/Makefile | 3 -
lib/crypto/x86/sha256-avx-asm.S | 2 +-
lib/crypto/x86/sha256-avx2-asm.S | 2 +-
lib/crypto/x86/sha256-ni-asm.S | 2 +-
lib/crypto/x86/sha256-ssse3-asm.S | 2 +-
lib/crypto/x86/sha256.c | 80 ----
lib/crypto/x86/sha256.h | 55 +++
net/ceph/messenger_v2.c | 12 +-
tools/testing/cxl/test/mem.c | 21 +-
58 files changed, 1307 insertions(+), 1008 deletions(-)
delete mode 100644 include/crypto/internal/sha2.h
delete mode 100644 lib/crypto/arm/sha256.c
create mode 100644 lib/crypto/arm/sha256.h
delete mode 100644 lib/crypto/arm64/sha256.c
create mode 100644 lib/crypto/arm64/sha256.h
rename arch/mips/cavium-octeon/crypto/octeon-sha256.c => lib/crypto/mips/sha256.h (76%)
rename lib/crypto/powerpc/{sha256.c => sha256.h} (76%)
delete mode 100644 lib/crypto/riscv/sha256.c
create mode 100644 lib/crypto/riscv/sha256.h
delete mode 100644 lib/crypto/s390/sha256.c
create mode 100644 lib/crypto/s390/sha256.h
delete mode 100644 lib/crypto/sha256-generic.c
delete mode 100644 lib/crypto/sparc/Kconfig
delete mode 100644 lib/crypto/sparc/Makefile
rename lib/crypto/sparc/{sha256.c => sha256.h} (53%)
delete mode 100644 lib/crypto/x86/sha256.c
create mode 100644 lib/crypto/x86/sha256.hSignature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 1/14 libceph: Rename hmac_sha256() to ceph_hmac_sha256()
Rename hmac_sha256() to ceph_hmac_sha256(), to avoid a naming conflict with the upcoming hmac_sha256() library function.
This code will be able to use the HMAC-SHA256 library, but that's left for a later commit.
--- net/ceph/messenger_v2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/ceph/messenger_v2.c +6 -6
@@ -791,12 +791,12 @@ static int setup_crypto(struct ceph_connection *con, con_secret +CEPH_GCM_KEY_LEN +CEPH_GCM_IV_LEN , CEPH_GCM_IV_LEN);return 0 ;/* auth_x, secure mode */ } -static int hmac_sha256 (struct ceph_connection *con,const struct kvec *kvecs, -int kvec_cnt,u8 *hmac) +static int ceph_hmac_sha256 (struct ceph_connection *con, +const struct kvec *kvecs,int kvec_cnt,u8 *hmac) {SHASH_DESC_ON_STACK (desc, con->v2.hmac_tfm);/* tfm arg is ignored */ int ret;int i;
@@ -1460,12 +1460,12 @@ static int prepare_auth_signature(struct ceph_connection *con) buf =alloc_conn_buf (con,head_onwire_len (SHA256_DIGEST_SIZE ,con_secure (con)));if (!buf)return -ENOMEM ; - ret =hmac_sha256 (con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt, -CTRL_BODY (buf)); + ret =ceph_hmac_sha256 (con, con->v2.in_sign_kvecs, + con->v2.in_sign_kvec_cnt,CTRL_BODY (buf));if (ret)return ret;return prepare_control (con,FRAME_TAG_AUTH_SIGNATURE , buf,SHA256_DIGEST_SIZE );
@@ -2458,12 +2458,12 @@ static int process_auth_signature(struct ceph_connection *con,if (con->state !=CEPH_CON_S_V2_AUTH_SIGNATURE ) { con->error_msg ="protocol error, unexpected auth_signature" ;return -EINVAL ; } - ret =hmac_sha256 (con, con->v2.out_sign_kvecs, - con->v2.out_sign_kvec_cnt, hmac); + ret =ceph_hmac_sha256 (con, con->v2.out_sign_kvecs, + con->v2.out_sign_kvec_cnt, hmac);if (ret)return ret;ceph_decode_need (&p, end,SHA256_DIGEST_SIZE , bad);if (crypto_memneq (p, hmac,SHA256_DIGEST_SIZE )) {
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 2/14 cxl/test: Simplify fw_buf_checksum_show()
In reply to Eric BiggersFirst, just use sha256() instead of a sequence of sha256_init(), sha256_update(), and sha256_final(). The result is the same.
Second, use *phN instead of open-coding the conversion of bytes to hex.
--- tools/testing/cxl/test/mem.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-)
tools/testing/cxl/test/mem.c +2 -19
@@ -1826,31 +1826,14 @@ static DEVICE_ATTR_RW(security_lock);static ssize_t fw_buf_checksum_show (struct device *dev,struct device_attribute *attr,char *buf) {struct cxl_mockmem_data *mdata =dev_get_drvdata (dev);u8 hash[SHA256_DIGEST_SIZE ]; -unsigned char *hstr, *hptr; -struct sha256_state sctx; -ssize_t written =0 ; -int i; - -sha256_init (&sctx); -sha256_update (&sctx, mdata->fw, mdata->fw_size); -sha256_final (&sctx, hash); - - hstr =kzalloc ((SHA256_DIGEST_SIZE *2 ) +1 ,GFP_KERNEL ); -if (!hstr) -return -ENOMEM ; - - hptr = hstr; -for (i =0 ; i <SHA256_DIGEST_SIZE ; i++) - hptr +=sprintf (hptr,"%02x" , hash[i]); - written =sysfs_emit (buf,"%s\n" , hstr); +sha256 (mdata->fw, mdata->fw_size, hash); -kfree (hstr); -return written; +return sysfs_emit (buf,"%*phN\n" ,SHA256_DIGEST_SIZE , hash); }static DEVICE_ATTR_RO (fw_buf_checksum); static ssize_t sanitize_timeout_show(struct device *dev,
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 3/14 lib/crypto: sha256: Reorder some code
In reply to Eric BiggersFirst, move the declarations of sha224_init/update/final to be just above the corresponding SHA-256 code, matching the order that I used for SHA-384 and SHA-512. In sha2.h, the end result is that SHA-224, SHA-256, SHA-384, and SHA-512 are all in the logical order.
Second, move sha224_block_init() and sha256_block_init() to be just below crypto_sha256_state. In later changes, these functions as well as struct crypto_sha256_state will no longer be used by the library functions. They'll remain just for some legacy offload drivers. This gets them into a logical place in the file for that.
No code changes other than reordering.
--- include/crypto/sha2.h | 60 +++++++++++++++++++++---------------------- lib/crypto/sha256.c | 12 ++++----- 2 files changed, 36 insertions(+), 36 deletions(-)
include/crypto/sha2.h +30 -30
@@ -69,10 +69,36 @@ extern const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE];struct crypto_sha256_state {u32 state[SHA256_STATE_WORDS ];u64 count; }; +static inline void sha224_block_init (struct crypto_sha256_state *sctx) +{ + sctx->state[0 ] =SHA224_H0 ; + sctx->state[1 ] =SHA224_H1 ; + sctx->state[2 ] =SHA224_H2 ; + sctx->state[3 ] =SHA224_H3 ; + sctx->state[4 ] =SHA224_H4 ; + sctx->state[5 ] =SHA224_H5 ; + sctx->state[6 ] =SHA224_H6 ; + sctx->state[7 ] =SHA224_H7 ; + sctx->count =0 ; +} + +static inline void sha256_block_init (struct crypto_sha256_state *sctx) +{ + sctx->state[0 ] =SHA256_H0 ; + sctx->state[1 ] =SHA256_H1 ; + sctx->state[2 ] =SHA256_H2 ; + sctx->state[3 ] =SHA256_H3 ; + sctx->state[4 ] =SHA256_H4 ; + sctx->state[5 ] =SHA256_H5 ; + sctx->state[6 ] =SHA256_H6 ; + sctx->state[7 ] =SHA256_H7 ; + sctx->count =0 ; +} + struct sha256_state { union { struct crypto_sha256_state ctx; struct { u32 state[SHA256_STATE_WORDS];
@@ -86,51 +112,25 @@ struct sha512_state {u64 state[SHA512_DIGEST_SIZE /8 ];u64 count[2 ];u8 buf[SHA512_BLOCK_SIZE ]; }; -static inline void sha256_block_init (struct crypto_sha256_state *sctx) +static inline void sha224_init (struct sha256_state *sctx) { - sctx->state[0 ] =SHA256_H0 ; - sctx->state[1 ] =SHA256_H1 ; - sctx->state[2 ] =SHA256_H2 ; - sctx->state[3 ] =SHA256_H3 ; - sctx->state[4 ] =SHA256_H4 ; - sctx->state[5 ] =SHA256_H5 ; - sctx->state[6 ] =SHA256_H6 ; - sctx->state[7 ] =SHA256_H7 ; - sctx->count =0 ; +sha224_block_init (&sctx->ctx); } +/* Simply use sha256_update as it is equivalent to sha224_update. */ +void sha224_final (struct sha256_state *sctx,u8 out[SHA224_DIGEST_SIZE ]);static inline void sha256_init (struct sha256_state *sctx) {sha256_block_init (&sctx->ctx); }void sha256_update (struct sha256_state *sctx,const u8 *data,size_t len);void sha256_final (struct sha256_state *sctx,u8 out[SHA256_DIGEST_SIZE ]);void sha256 (const u8 *data,size_t len,u8 out[SHA256_DIGEST_SIZE ]); -static inline void sha224_block_init (struct crypto_sha256_state *sctx) -{ - sctx->state[0 ] =SHA224_H0 ; - sctx->state[1 ] =SHA224_H1 ; - sctx->state[2 ] =SHA224_H2 ; - sctx->state[3 ] =SHA224_H3 ; - sctx->state[4 ] =SHA224_H4 ; - sctx->state[5 ] =SHA224_H5 ; - sctx->state[6 ] =SHA224_H6 ; - sctx->state[7 ] =SHA224_H7 ; - sctx->count =0 ; -} - -static inline void sha224_init (struct sha256_state *sctx) -{ -sha224_block_init (&sctx->ctx); -} -/* Simply use sha256_update as it is equivalent to sha224_update. */ -void sha224_final (struct sha256_state *sctx,u8 out[SHA224_DIGEST_SIZE ]); -/* State for the SHA-512 (and SHA-384) compression function */ struct sha512_block_state {u64 h[8 ]; };
lib/crypto/sha256.c +6 -6
@@ -56,22 +56,22 @@ static inline void __sha256_final(struct sha256_state *sctx, u8 *out,sha256_finup (&sctx->ctx, sctx->buf, partial, out, digest_size,sha256_purgatory (), false);memzero_explicit (sctx,sizeof (*sctx)); } -void sha256_final (struct sha256_state *sctx,u8 out[SHA256_DIGEST_SIZE ]) -{ -__sha256_final (sctx, out,SHA256_DIGEST_SIZE ); -} -EXPORT_SYMBOL (sha256_final); -void sha224_final (struct sha256_state *sctx,u8 out[SHA224_DIGEST_SIZE ]) {__sha256_final (sctx, out,SHA224_DIGEST_SIZE ); }EXPORT_SYMBOL (sha224_final); +void sha256_final (struct sha256_state *sctx,u8 out[SHA256_DIGEST_SIZE ]) +{ +__sha256_final (sctx, out,SHA256_DIGEST_SIZE ); +} +EXPORT_SYMBOL (sha256_final); +void sha256 (const u8 *data,size_t len,u8 out[SHA256_DIGEST_SIZE ]) {struct sha256_state sctx;sha256_init (&sctx);
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 4/14 lib/crypto: sha256: Remove sha256_blocks_simd()
In reply to Eric BiggersInstead of having both sha256_blocks_arch() and sha256_blocks_simd(), instead have just sha256_blocks_arch() which uses the most efficient implementation that is available in the calling context.
This is simpler, as it reduces the API surface. It's also safer, since sha256_blocks_arch() just works in all contexts, including contexts where the FPU/SIMD/vector registers cannot be used. This doesn't mean that SHA-256 computations *should* be done in such contexts, but rather we should just do the right thing instead of corrupting a random task's registers. Eliminating this footgun and simplifying the code is well worth the very small performance cost of doing the check.
Note: in the case of arm and arm64, what used to be sha256_blocks_arch() is renamed back to its original name of sha256_block_data_order(). sha256_blocks_arch() is now used for the higher-level dispatch function. This renaming also required an update to lib/crypto/arm64/sha512.h, since sha2-armv8.pl is shared by both SHA-256 and SHA-512.
--- include/crypto/internal/sha2.h | 6 ------ lib/crypto/Kconfig | 8 -------- lib/crypto/arm/Kconfig | 1 - lib/crypto/arm/sha256-armv4.pl | 20 ++++++++++---------- lib/crypto/arm/sha256.c | 14 +++++++------- lib/crypto/arm64/Kconfig | 1 - lib/crypto/arm64/sha2-armv8.pl | 2 +- lib/crypto/arm64/sha256.c | 14 +++++++------- lib/crypto/arm64/sha512.h | 6 +++--- lib/crypto/riscv/Kconfig | 1 - lib/crypto/riscv/sha256.c | 12 +++--------- lib/crypto/x86/Kconfig | 1 - lib/crypto/x86/sha256.c | 12 +++--------- 13 files changed, 34 insertions(+), 64 deletions(-)
include/crypto/internal/sha2.h +0 -6
@@ -1,11 +1,10 @@/* SPDX-License-Identifier: GPL-2.0-only */ #ifndef _CRYPTO_INTERNAL_SHA2_H#define _CRYPTO_INTERNAL_SHA2_H -#include <crypto/internal/simd.h> #include <crypto/sha2.h> #include <linux/compiler_attributes.h> #include <linux/string.h> #include <linux/types.h> #include <linux/unaligned.h>
@@ -20,22 +19,17 @@ static inline bool sha256_is_arch_optimized(void)#endif void sha256_blocks_generic (u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks);void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks); -void sha256_blocks_simd (u32 state[SHA256_STATE_WORDS ], -const u8 *data,size_t nblocks); static __always_inline void sha256_choose_blocks(u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks,bool force_generic,bool force_simd) {if (!IS_ENABLED (CONFIG_CRYPTO_ARCH_HAVE_LIB_SHA256 ) || force_generic)sha256_blocks_generic (state, data, nblocks); -else if (IS_ENABLED (CONFIG_CRYPTO_ARCH_HAVE_LIB_SHA256_SIMD ) && - (force_simd ||crypto_simd_usable ())) -sha256_blocks_simd (state, data, nblocks);else sha256_blocks_arch (state, data, nblocks); } static __always_inline void sha256_finup(
lib/crypto/Kconfig +0 -8
@@ -148,18 +148,10 @@ config CRYPTO_ARCH_HAVE_LIB_SHA256 bool help Declares whether the architecture provides an arch-specific accelerated implementation of the SHA-256 library interface. -config CRYPTO_ARCH_HAVE_LIB_SHA256_SIMD - bool - help - Declares whether the architecture provides an arch-specific - accelerated implementation of the SHA-256 library interface - that is SIMD-based and therefore not usable in hardirq - context. - config CRYPTO_LIB_SHA256_GENERIC tristate default CRYPTO_LIB_SHA256 if !CRYPTO_ARCH_HAVE_LIB_SHA256 help This symbol can be selected by arch implementations of the SHA-256
lib/crypto/arm/Kconfig +0 -1
@@ -26,6 +26,5 @@ config CRYPTO_POLY1305_ARM config CRYPTO_SHA256_ARM tristate depends on !CPU_V7M default CRYPTO_LIB_SHA256 select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256_SIMD
lib/crypto/arm/sha256-armv4.pl +10 -10
@@ -202,22 +202,22 @@ K256: .word 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 .size K256,.-K256 .word 0 @ terminator #if __ARM_MAX_ARCH__>=7 && !defined(__KERNEL__) .LOPENSSL_armcap: -.word OPENSSL_armcap_P-sha256_blocks_arch +.word OPENSSL_armcap_P-sha256_block_data_order #endif .align 5 -.global sha256_blocks_arch -.type sha256_blocks_arch,%function -sha256_blocks_arch: -.Lsha256_blocks_arch: +.global sha256_block_data_order +.type sha256_block_data_order,%function +sha256_block_data_order: +.Lsha256_block_data_order: #if __ARM_ARCH__<7 - sub r3,pc,#8 @ sha256_blocks_arch + sub r3,pc,#8 @ sha256_block_data_order #else - adr r3,.Lsha256_blocks_arch + adr r3,.Lsha256_block_data_order #endif #if __ARM_MAX_ARCH__>=7 && !defined(__KERNEL__) ldr r12,.LOPENSSL_armcap ldr r12,[r3,r12] @ OPENSSL_armcap_P tst r12,#ARMV8_SHA256
@@ -280,11 +280,11 @@ $code.=<<___; ldmia sp!,{r4-r11,lr} tst lr,#1 moveq pc,lr @ be binary compatible with V4, yet bx lr @ interoperable with Thumb ISA:-) #endif -.size sha256_blocks_arch,.-sha256_blocks_arch +.size sha256_block_data_order,.-sha256_block_data_order ___ ###################################################################### # NEON stuff # {{{
@@ -468,12 +468,12 @@ $code.=<<___; sha256_block_data_order_neon: .LNEON: stmdb sp!,{r4-r12,lr} sub $H,sp,#16*4+16 - adr $Ktbl,.Lsha256_blocks_arch - sub $Ktbl,$Ktbl,#.Lsha256_blocks_arch-K256 + adr $Ktbl,.Lsha256_block_data_order + sub $Ktbl,$Ktbl,#.Lsha256_block_data_order-K256 bic $H,$H,#15 @ align for 128-bit stores mov $t2,sp mov sp,$H @ alloca add $len,$inp,$len,lsl#6 @ len to point at the end of inp
lib/crypto/arm/sha256.c +7 -7
@@ -4,40 +4,40 @@* * Copyright 2025 Google LLC */ #include <asm/neon.h> #include <crypto/internal/sha2.h> +#include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> -asmlinkage void sha256_blocks_arch(u32 state[SHA256_STATE_WORDS], -const u8 *data,size_t nblocks); -EXPORT_SYMBOL_GPL (sha256_blocks_arch); +asmlinkage void sha256_block_data_order(u32 state[SHA256_STATE_WORDS], +const u8 *data,size_t nblocks); asmlinkage void sha256_block_data_order_neon(u32 state[SHA256_STATE_WORDS],const u8 *data,size_t nblocks); asmlinkage void sha256_ce_transform(u32 state[SHA256_STATE_WORDS],const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_ce); -void sha256_blocks_simd (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) && -static_branch_likely (&have_neon)) { +static_branch_likely (&have_neon) &&crypto_simd_usable ()) {kernel_neon_begin ();if (static_branch_likely (&have_ce))sha256_ce_transform (state, data, nblocks);else sha256_block_data_order_neon (state, data, nblocks);kernel_neon_end (); }else { -sha256_blocks_arch (state, data, nblocks); +sha256_block_data_order (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_simd); +EXPORT_SYMBOL_GPL (sha256_blocks_arch);bool sha256_is_arch_optimized (void ) {/* We always can use at least the ARM scalar implementation. */ return true;
lib/crypto/arm64/Kconfig +0 -1
@@ -15,6 +15,5 @@ config CRYPTO_POLY1305_NEON config CRYPTO_SHA256_ARM64 tristate default CRYPTO_LIB_SHA256 select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256_SIMD
lib/crypto/arm64/sha2-armv8.pl +1 -1
@@ -93,11 +93,11 @@ if ($output =~ /512/) { @sigma1=(17,19,10); $rounds=64; $reg_t="w"; } -$func="sha${BITS}_blocks_arch"; +$func="sha${BITS}_block_data_order"; ($ctx,$inp,$num,$Ktbl)=map("x$_",(0..2,30)); @X=map("$reg_t$_",(3..15,0..2)); @V=($A,$B,$C,$D,$E,$F,$G,$H)=map("$reg_t$_",(20..27));
lib/crypto/arm64/sha256.c +7 -7
@@ -4,29 +4,29 @@* * Copyright 2025 Google LLC */ #include <asm/neon.h> #include <crypto/internal/sha2.h> +#include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> -asmlinkage void sha256_blocks_arch(u32 state[SHA256_STATE_WORDS], -const u8 *data,size_t nblocks); -EXPORT_SYMBOL_GPL (sha256_blocks_arch); +asmlinkage void sha256_block_data_order(u32 state[SHA256_STATE_WORDS], +const u8 *data,size_t nblocks); asmlinkage void sha256_block_neon(u32 state[SHA256_STATE_WORDS],const u8 *data,size_t nblocks); asmlinkage size_t __sha256_ce_transform(u32 state[SHA256_STATE_WORDS],const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_ce); -void sha256_blocks_simd (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) && -static_branch_likely (&have_neon)) { +static_branch_likely (&have_neon) &&crypto_simd_usable ()) {if (static_branch_likely (&have_ce)) { do { size_t rem; kernel_neon_begin();
@@ -40,14 +40,14 @@ void sha256_blocks_simd(u32 state[SHA256_STATE_WORDS],kernel_neon_begin ();sha256_block_neon (state, data, nblocks);kernel_neon_end (); } }else { -sha256_blocks_arch (state, data, nblocks); +sha256_block_data_order (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_simd); +EXPORT_SYMBOL_GPL (sha256_blocks_arch);bool sha256_is_arch_optimized (void ) {/* We always can use at least the ARM64 scalar implementation. */ return true;
lib/crypto/arm64/sha512.h +3 -3
@@ -9,12 +9,12 @@#include <crypto/internal/simd.h> #include <linux/cpufeature.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_sha512_insns); -asmlinkage void sha512_blocks_arch(struct sha512_block_state *state, -const u8 *data,size_t nblocks); +asmlinkage void sha512_block_data_order(struct sha512_block_state *state, +const u8 *data,size_t nblocks); asmlinkage size_t __sha512_ce_transform(struct sha512_block_state *state,const u8 *data,size_t nblocks);static void sha512_blocks (struct sha512_block_state *state,const u8 *data,size_t nblocks)
@@ -30,11 +30,11 @@ static void sha512_blocks(struct sha512_block_state *state,kernel_neon_end (); data += (nblocks - rem) *SHA512_BLOCK_SIZE ; nblocks = rem; }while (nblocks); }else { -sha512_blocks_arch (state, data, nblocks); +sha512_block_data_order (state, data, nblocks); } }#ifdef CONFIG_KERNEL_MODE_NEON #define sha512_mod_init_arch sha512_mod_init_arch
lib/crypto/riscv/Kconfig +0 -1
@@ -10,7 +10,6 @@ config CRYPTO_CHACHA_RISCV64 config CRYPTO_SHA256_RISCV64 tristate depends on 64BIT && RISCV_ISA_V && TOOLCHAIN_HAS_VECTOR_CRYPTO default CRYPTO_LIB_SHA256 select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256_SIMD select CRYPTO_LIB_SHA256_GENERIC
lib/crypto/riscv/sha256.c +3 -9
@@ -9,36 +9,30 @@* Author: Jerry Shih <[email protected]> */ #include <asm/vector.h> #include <crypto/internal/sha2.h> +#include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> asmlinkage void sha256_transform_zvknha_or_zvknhb_zvkb(u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_extensions); -void sha256_blocks_simd (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks) { -if (static_branch_likely (&have_extensions)) { +if (static_branch_likely (&have_extensions) &&crypto_simd_usable ()) {kernel_vector_begin ();sha256_transform_zvknha_or_zvknhb_zvkb (state, data, nblocks);kernel_vector_end (); }else {sha256_blocks_generic (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_simd); - -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], -const u8 *data,size_t nblocks) -{ -sha256_blocks_generic (state, data, nblocks); -}EXPORT_SYMBOL_GPL (sha256_blocks_arch);bool sha256_is_arch_optimized (void ) {return static_key_enabled (&have_extensions);
lib/crypto/x86/Kconfig +0 -1
@@ -28,7 +28,6 @@ config CRYPTO_POLY1305_X86_64 config CRYPTO_SHA256_X86_64 tristate depends on 64BIT default CRYPTO_LIB_SHA256 select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256_SIMD select CRYPTO_LIB_SHA256_GENERIC
lib/crypto/x86/sha256.c +3 -9
@@ -4,10 +4,11 @@* * Copyright 2025 Google LLC */ #include <asm/fpu/api.h> #include <crypto/internal/sha2.h> +#include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/static_call.h> asmlinkage void sha256_transform_ssse3(u32 state[SHA256_STATE_WORDS],
@@ -21,28 +22,21 @@ asmlinkage void sha256_ni_transform(u32 state[SHA256_STATE_WORDS],static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_sha256_x86);DEFINE_STATIC_CALL (sha256_blocks_x86, sha256_transform_ssse3); -void sha256_blocks_simd (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks) { -if (static_branch_likely (&have_sha256_x86)) { +if (static_branch_likely (&have_sha256_x86) &&crypto_simd_usable ()) {kernel_fpu_begin ();static_call (sha256_blocks_x86)(state, data, nblocks);kernel_fpu_end (); }else {sha256_blocks_generic (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_simd); - -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], -const u8 *data,size_t nblocks) -{ -sha256_blocks_generic (state, data, nblocks); -}EXPORT_SYMBOL_GPL (sha256_blocks_arch);bool sha256_is_arch_optimized (void ) {return static_key_enabled (&have_sha256_x86);
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 5/14 lib/crypto: sha256: Add sha224() and sha224_update()
In reply to Eric BiggersAdd a one-shot SHA-224 computation function sha224(), for consistency with sha256(), sha384(), and sha512() which all already exist.
Similarly, add sha224_update(). While for now it's identical to sha256_update(), omitting it makes the API harder to use since users have to "know" which functions are the same between SHA-224 and SHA-256. Also, this is a prerequisite for using different context types for each.
--- include/crypto/sha2.h | 10 ++++++++-- lib/crypto/sha256.c | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
include/crypto/sha2.h +8 -2
@@ -112,22 +112,28 @@ struct sha512_state {u64 state[SHA512_DIGEST_SIZE /8 ];u64 count[2 ];u8 buf[SHA512_BLOCK_SIZE ]; }; +void sha256_update (struct sha256_state *sctx,const u8 *data,size_t len); +static inline void sha224_init (struct sha256_state *sctx) {sha224_block_init (&sctx->ctx); } -/* Simply use sha256_update as it is equivalent to sha224_update. */ +static inline void sha224_update (struct sha256_state *sctx, +const u8 *data,size_t len) +{ +sha256_update (sctx, data, len); +}void sha224_final (struct sha256_state *sctx,u8 out[SHA224_DIGEST_SIZE ]); +void sha224 (const u8 *data,size_t len,u8 out[SHA224_DIGEST_SIZE ]);static inline void sha256_init (struct sha256_state *sctx) {sha256_block_init (&sctx->ctx); } -void sha256_update (struct sha256_state *sctx,const u8 *data,size_t len);void sha256_final (struct sha256_state *sctx,u8 out[SHA256_DIGEST_SIZE ]);void sha256 (const u8 *data,size_t len,u8 out[SHA256_DIGEST_SIZE ]);/* State for the SHA-512 (and SHA-384) compression function */ struct sha512_block_state {
lib/crypto/sha256.c +10 -0
@@ -68,10 +68,20 @@ void sha256_final(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE]) {__sha256_final (sctx, out,SHA256_DIGEST_SIZE ); }EXPORT_SYMBOL (sha256_final); +void sha224 (const u8 *data,size_t len,u8 out[SHA224_DIGEST_SIZE ]) +{ +struct sha256_state sctx; + +sha224_init (&sctx); +sha224_update (&sctx, data, len); +sha224_final (&sctx, out); +} +EXPORT_SYMBOL (sha224); +void sha256 (const u8 *data,size_t len,u8 out[SHA256_DIGEST_SIZE ]) {struct sha256_state sctx;sha256_init (&sctx);
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 6/14 lib/crypto: sha256: Make library API use strongly-typed contexts
In reply to Eric BiggersCurrently the SHA-224 and SHA-256 library functions can be mixed arbitrarily, even in ways that are incorrect, for example using sha224_init() and sha256_final(). This is because they operate on the same structure, sha256_state.
Introduce stronger typing, as I did for SHA-384 and SHA-512.
Also as I did for SHA-384 and SHA-512, use the names *_ctx instead of *_state. The *_ctx names have the following small benefits:
- They're shorter.
- They avoid an ambiguity with the compression function state.
- They're consistent with the well-known OpenSSL API.
- Users usually name the variable 'sctx' anyway, which suggests that *_ctx would be the more natural name for the actual struct.
Therefore: update the SHA-224 and SHA-256 APIs, implementation, and calling code accordingly.
In the new structs, also strongly-type the compression function state.
--- arch/riscv/purgatory/purgatory.c | 8 +-- arch/s390/purgatory/purgatory.c | 2 +- arch/x86/purgatory/purgatory.c | 2 +- crypto/sha256.c | 16 ++--- drivers/char/tpm/tpm2-sessions.c | 12 ++-- include/crypto/sha2.h | 52 ++++++++++++---- kernel/kexec_file.c | 10 ++-- lib/crypto/sha256.c | 100 ++++++++++++++++++++++--------- 8 files changed, 139 insertions(+), 63 deletions(-)
arch/riscv/purgatory/purgatory.c +4 -4
@@ -18,18 +18,18 @@ u8 purgatory_sha256_digest[SHA256_DIGEST_SIZE] __section(".kexec-purgatory"); struct kexec_sha_region purgatory_sha_regions[KEXEC_SEGMENT_MAX] __section(".kexec-purgatory");static int verify_sha256_digest (void ) {struct kexec_sha_region *ptr, *end; -struct sha256_state ss; +struct sha256_ctx sctx;u8 digest[SHA256_DIGEST_SIZE ]; -sha256_init (&ss); +sha256_init (&sctx); end = purgatory_sha_regions +ARRAY_SIZE (purgatory_sha_regions);for (ptr = purgatory_sha_regions; ptr < end; ptr++) -sha256_update (&ss, (uint8_t *)(ptr->start), ptr->len); -sha256_final (&ss, digest); +sha256_update (&sctx, (uint8_t *)(ptr->start), ptr->len); +sha256_final (&sctx, digest);if (memcmp (digest, purgatory_sha256_digest,sizeof (digest)) !=0 )return 1 ;return 0 ; }
arch/s390/purgatory/purgatory.c +1 -1
@@ -14,11 +14,11 @@int verify_sha256_digest (void ) {struct kexec_sha_region *ptr, *end;u8 digest[SHA256_DIGEST_SIZE ]; -struct sha256_state sctx; +struct sha256_ctx sctx;sha256_init (&sctx); end = purgatory_sha_regions +ARRAY_SIZE (purgatory_sha_regions);for (ptr = purgatory_sha_regions; ptr < end; ptr++)
arch/x86/purgatory/purgatory.c +1 -1
@@ -23,11 +23,11 @@ struct kexec_sha_region purgatory_sha_regions[KEXEC_SEGMENT_MAX] __section(".kexstatic int verify_sha256_digest (void ) {struct kexec_sha_region *ptr, *end;u8 digest[SHA256_DIGEST_SIZE ]; -struct sha256_state sctx; +struct sha256_ctx sctx;sha256_init (&sctx); end = purgatory_sha_regions +ARRAY_SIZE (purgatory_sha_regions);for (ptr = purgatory_sha_regions; ptr < end; ptr++)
crypto/sha256.c +8 -8
@@ -135,28 +135,28 @@ static int crypto_sha224_final_lib(struct shash_desc *desc, u8 *out)return 0 ; }static int crypto_sha256_import_lib (struct shash_desc *desc,const void *in) { -struct sha256_state *sctx =shash_desc_ctx (desc); +struct __sha256_ctx *sctx =shash_desc_ctx (desc);const u8 *p = in;memcpy (sctx, p,sizeof (*sctx)); p +=sizeof (*sctx); - sctx->count += *p; + sctx->bytecount += *p;return 0 ; }static int crypto_sha256_export_lib (struct shash_desc *desc,void *out) { -struct sha256_state *sctx0 =shash_desc_ctx (desc); -struct sha256_state sctx = *sctx0; +struct __sha256_ctx *sctx0 =shash_desc_ctx (desc); +struct __sha256_ctx sctx = *sctx0;unsigned int partial;u8 *p = out; - partial = sctx.count %SHA256_BLOCK_SIZE ; - sctx.count -= partial; + partial = sctx.bytecount %SHA256_BLOCK_SIZE ; + sctx.bytecount -= partial;memcpy (p, &sctx,sizeof (sctx)); p +=sizeof (sctx); *p = partial;return 0 ; }
@@ -199,11 +199,11 @@ static struct shash_alg algs[] = { .digestsize = SHA256_DIGEST_SIZE, .init = crypto_sha256_init, .update = crypto_sha256_update_lib, .final = crypto_sha256_final_lib, .digest = crypto_sha256_digest_lib, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct sha256_ctx), .statesize =sizeof (struct crypto_sha256_state ) + SHA256_BLOCK_SIZE + 1, .import = crypto_sha256_import_lib, .export = crypto_sha256_export_lib, },
@@ -214,11 +214,11 @@ static struct shash_alg algs[] = { .base.cra_module = THIS_MODULE, .digestsize = SHA224_DIGEST_SIZE, .init = crypto_sha224_init, .update = crypto_sha256_update_lib, .final = crypto_sha224_final_lib, - .descsize = sizeof(struct sha256_state), + .descsize = sizeof(struct sha224_ctx), .statesize =sizeof (struct crypto_sha256_state ) + SHA256_BLOCK_SIZE + 1, .import = crypto_sha256_import_lib, .export = crypto_sha256_export_lib, },
drivers/char/tpm/tpm2-sessions.c +6 -6
@@ -388,11 +388,11 @@ static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,* It turns out the crypto hmac(sha256) is hard for us to consume * because it assumes a fixed key and the TPM seems to change the key * on every operation, so we weld the hmac init and final functions in * here to give it the same usage characteristics as a regular hash */ -static void tpm2_hmac_init (struct sha256_state *sctx,u8 *key,u32 key_len) +static void tpm2_hmac_init (struct sha256_ctx *sctx,u8 *key,u32 key_len) {u8 pad[SHA256_BLOCK_SIZE ];int i;sha256_init (sctx);
@@ -404,11 +404,11 @@ static void tpm2_hmac_init(struct sha256_state *sctx, u8 *key, u32 key_len) pad[i] ^=HMAC_IPAD_VALUE ; }sha256_update (sctx, pad,sizeof (pad)); } -static void tpm2_hmac_final (struct sha256_state *sctx,u8 *key,u32 key_len, +static void tpm2_hmac_final (struct sha256_ctx *sctx,u8 *key,u32 key_len,u8 *out) {u8 pad[SHA256_BLOCK_SIZE ];int i;
@@ -438,11 +438,11 @@ static void tpm2_KDFa(u8 *key, u32 key_len, const char *label, u8 *u, {u32 counter =1 ;const __be32 bits =cpu_to_be32 (bytes *8 );while (bytes >0 ) { -struct sha256_state sctx; +struct sha256_ctx sctx;__be32 c =cpu_to_be32 (counter);tpm2_hmac_init (&sctx, key, key_len);sha256_update (&sctx, (u8 *)&c,sizeof (c));sha256_update (&sctx, label,strlen (label)+1 );
@@ -465,11 +465,11 @@ static void tpm2_KDFa(u8 *key, u32 key_len, const char *label, u8 *u,* in this KDF. */ static void tpm2_KDFe (u8 z[EC_PT_SZ ],const char *str,u8 *pt_u,u8 *pt_v,u8 *out) { -struct sha256_state sctx; +struct sha256_ctx sctx;/* * this should be an iterative counter, but because we know * we're only taking 32 bytes for the point using a sha256 * hash which is also 32 bytes, there's only one loop */
@@ -590,11 +590,11 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)struct tpm_header *head = (struct tpm_header *)buf->data;off_t offset_s =TPM_HEADER_SIZE , offset_p;u8 *hmac =NULL ;u32 attrs;u8 cphash[SHA256_DIGEST_SIZE ]; -struct sha256_state sctx; +struct sha256_ctx sctx;if (!auth)return ;/* save the command code in BE format */
@@ -748,11 +748,11 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,struct tpm_header *head = (struct tpm_header *)buf->data;struct tpm2_auth *auth = chip->auth;off_t offset_s, offset_p;u8 rphash[SHA256_DIGEST_SIZE ];u32 attrs, cc; -struct sha256_state sctx; +struct sha256_ctx sctx;u16 tag =be16_to_cpu (head->tag);int parm_len, len, i, handles;if (!auth)return rc;
include/crypto/sha2.h +41 -11
70 lines not on this page · raw message
kernel/kexec_file.c +5 -5
@@ -749,11 +749,11 @@ int kexec_add_buffer(struct kexec_buf *kbuf) }/* Calculate and store the digest of segments */ static int kexec_calculate_store_digests (struct kimage *image) { -struct sha256_state state; +struct sha256_ctx sctx;int ret =0 , i, j, zero_buf_sz, sha_region_sz;size_t nullsz;u8 digest[SHA256_DIGEST_SIZE ];void *zero_buf;struct kexec_sha_region *sha_regions;
@@ -768,11 +768,11 @@ static int kexec_calculate_store_digests(struct kimage *image) sha_region_sz =KEXEC_SEGMENT_MAX *sizeof (struct kexec_sha_region ); sha_regions =vzalloc (sha_region_sz);if (!sha_regions)return -ENOMEM ; -sha256_init (&state); +sha256_init (&sctx);for (j = i =0 ; i < image->nr_segments; i++) {struct kexec_segment *ksegment;#ifdef CONFIG_CRASH_HOTPLUG
@@ -794,11 +794,11 @@ static int kexec_calculate_store_digests(struct kimage *image)* the current index */ if (check_ima_segment_index (image, i))continue ; -sha256_update (&state, ksegment->kbuf, ksegment->bufsz); +sha256_update (&sctx, ksegment->kbuf, ksegment->bufsz);/* * Assume rest of the buffer is filled with zero and * update digest accordingly. */
@@ -806,20 +806,20 @@ static int kexec_calculate_store_digests(struct kimage *image)while (nullsz) {unsigned long bytes = nullsz;if (bytes > zero_buf_sz) bytes = zero_buf_sz; -sha256_update (&state, zero_buf, bytes); +sha256_update (&sctx, zero_buf, bytes); nullsz -= bytes; } sha_regions[j].start = ksegment->mem; sha_regions[j].len = ksegment->memsz; j++; } -sha256_final (&state, digest); +sha256_final (&sctx, digest); ret =kexec_purgatory_get_set_symbol (image,"purgatory_sha_regions" , sha_regions, sha_region_sz,0 );if (ret) goto out_free_sha_regions;
lib/crypto/sha256.c +73 -27
148 lines not on this page · raw message
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 7/14 lib/crypto: sha256: Propagate sha256_block_state type to implementations
In reply to Eric BiggersThe previous commit made the SHA-256 compression function state be strongly typed, but it wasn't propagated all the way down to the implementations of it. Do that now.
--- .../mips/cavium-octeon/crypto/octeon-sha256.c | 2 +- include/crypto/internal/sha2.h | 8 +++---- lib/crypto/arm/sha256-ce.S | 2 +- lib/crypto/arm/sha256.c | 8 +++---- lib/crypto/arm64/sha256-ce.S | 2 +- lib/crypto/arm64/sha256.c | 8 +++---- lib/crypto/powerpc/sha256.c | 2 +- .../sha256-riscv64-zvknha_or_zvknhb-zvkb.S | 2 +- lib/crypto/riscv/sha256.c | 7 +++--- lib/crypto/s390/sha256.c | 2 +- lib/crypto/sha256-generic.c | 24 ++++++++++++++----- lib/crypto/sparc/sha256.c | 4 ++-- lib/crypto/x86/sha256-avx-asm.S | 2 +- lib/crypto/x86/sha256-avx2-asm.S | 2 +- lib/crypto/x86/sha256-ni-asm.S | 2 +- lib/crypto/x86/sha256-ssse3-asm.S | 2 +- lib/crypto/x86/sha256.c | 10 ++++---- 17 files changed, 51 insertions(+), 38 deletions(-)
arch/mips/cavium-octeon/crypto/octeon-sha256.c +1 -1
@@ -20,11 +20,11 @@/* * We pass everything as 64-bit. OCTEON can handle misaligned data. */ -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {struct octeon_cop2_state cop2_state;u64 *state64 = (u64 *)state;unsigned long flags;
include/crypto/internal/sha2.h +4 -4
@@ -15,23 +15,23 @@ bool sha256_is_arch_optimized(void);static inline bool sha256_is_arch_optimized (void ) {return false; }#endif -void sha256_blocks_generic (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_generic (struct sha256_block_state *state,const u8 *data,size_t nblocks); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks); static __always_inline void sha256_choose_blocks(u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks,bool force_generic,bool force_simd) {if (!IS_ENABLED (CONFIG_CRYPTO_ARCH_HAVE_LIB_SHA256 ) || force_generic) -sha256_blocks_generic (state, data, nblocks); +sha256_blocks_generic ((struct sha256_block_state *)state, data, nblocks);else -sha256_blocks_arch (state, data, nblocks); +sha256_blocks_arch ((struct sha256_block_state *)state, data, nblocks); } static __always_inline void sha256_finup( struct crypto_sha256_state *sctx, u8 buf[SHA256_BLOCK_SIZE], size_t len, u8 out[SHA256_DIGEST_SIZE], size_t digest_size,
lib/crypto/arm/sha256-ce.S +1 -1
@@ -65,11 +65,11 @@ .word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 .word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208 .word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 /* - * void sha256_ce_transform(u32 state[SHA256_STATE_WORDS], + * void sha256_ce_transform(struct sha256_block_state *state, * const u8 *data, size_t nblocks); */ ENTRY(sha256_ce_transform) /* load state */ vld1.32 {dga-dgb}, [r0]
lib/crypto/arm/sha256.c +4 -4
@@ -8,21 +8,21 @@#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> -asmlinkage void sha256_block_data_order(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_block_data_order(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage void sha256_block_data_order_neon(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_block_data_order_neon(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage void sha256_ce_transform(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_ce_transform(struct sha256_block_state *state,const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_ce); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&crypto_simd_usable ()) {kernel_neon_begin ();
lib/crypto/arm64/sha256-ce.S +1 -1
@@ -69,11 +69,11 @@ .word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 .word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208 .word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 /* - * size_t __sha256_ce_transform(u32 state[SHA256_STATE_WORDS], + * size_t __sha256_ce_transform(struct sha256_block_state *state, * const u8 *data, size_t nblocks); */ .text SYM_FUNC_START(__sha256_ce_transform) /* load round constants */
lib/crypto/arm64/sha256.c +4 -4
@@ -8,21 +8,21 @@#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> -asmlinkage void sha256_block_data_order(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_block_data_order(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage void sha256_block_neon(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_block_neon(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage size_t __sha256_ce_transform(u32 state[SHA256_STATE_WORDS], +asmlinkage size_t __sha256_ce_transform(struct sha256_block_state *state,const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_ce); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&crypto_simd_usable ()) {if (static_branch_likely (&have_ce)) {
lib/crypto/powerpc/sha256.c +1 -1
@@ -40,11 +40,11 @@ static void spe_end(void)disable_kernel_spe ();/* reenable preemption */ preempt_enable (); } -void sha256_blocks_arch(u32 state[SHA256_STATE_WORDS], +void sha256_blocks_arch(struct sha256_block_state *state, const u8 *data, size_t nblocks) { do { /* cut input data into smaller blocks */ u32 unit = min_t(size_t, nblocks,
lib/crypto/riscv/sha256-riscv64-zvknha_or_zvknhb-zvkb.S +1 -1
@@ -104,11 +104,11 @@ sha256_4rounds \last, \k1, W1, W2, W3, W0 sha256_4rounds \last, \k2, W2, W3, W0, W1 sha256_4rounds \last, \k3, W3, W0, W1, W2 .endm -// void sha256_transform_zvknha_or_zvknhb_zvkb(u32 state[SHA256_STATE_WORDS], +// void sha256_transform_zvknha_or_zvknhb_zvkb(struct sha256_block_state *state, // const u8 *data, size_t nblocks); SYM_FUNC_START(sha256_transform_zvknha_or_zvknhb_zvkb) // Load the round constants into K0-K15. vsetivli zero, 4, e32, m1, ta, ma
lib/crypto/riscv/sha256.c +4 -3
@@ -13,16 +13,17 @@#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> -asmlinkage void sha256_transform_zvknha_or_zvknhb_zvkb( -u32 state[SHA256_STATE_WORDS ],const u8 *data,size_t nblocks); +asmlinkage void +sha256_transform_zvknha_or_zvknhb_zvkb (struct sha256_block_state *state, +const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_extensions); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {if (static_branch_likely (&have_extensions) &&crypto_simd_usable ()) {kernel_vector_begin ();sha256_transform_zvknha_or_zvknhb_zvkb (state, data, nblocks);
lib/crypto/s390/sha256.c +1 -1
@@ -10,11 +10,11 @@#include <linux/kernel.h> #include <linux/module.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_cpacf_sha256); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {if (static_branch_likely (&have_cpacf_sha256))cpacf_kimd (CPACF_KIMD_SHA_256 , state, data, nblocks *SHA256_BLOCK_SIZE );
lib/crypto/sha256-generic.c +18 -6
@@ -68,11 +68,11 @@ static inline void BLEND_OP(int I, u32 *W) t2 =e0 (a) +Maj (a, b, c); \ d += t1; \ h = t1 + t2; \ }while (0 ) -static void sha256_block_generic(u32 state[SHA256_STATE_WORDS], +static void sha256_block_generic(struct sha256_block_state *state,const u8 *input,u32 W [64 ]) {u32 a, b, c, d, e, f, g, h;int i;
@@ -99,12 +99,18 @@ static void sha256_block_generic(u32 state[SHA256_STATE_WORDS],BLEND_OP (i +6 ,W );BLEND_OP (i +7 ,W ); }/* load the state into our registers */ - a = state[0 ]; b = state[1 ]; c = state[2 ]; d = state[3 ]; - e = state[4 ]; f = state[5 ]; g = state[6 ]; h = state[7 ]; + a = state->h[0 ]; + b = state->h[1 ]; + c = state->h[2 ]; + d = state->h[3 ]; + e = state->h[4 ]; + f = state->h[5 ]; + g = state->h[6 ]; + h = state->h[7 ];/* now iterate */ for (i =0 ; i <64 ; i +=8 ) {SHA256_ROUND (i +0 , a, b, c, d, e, f, g, h);SHA256_ROUND (i +1 , h, a, b, c, d, e, f, g);
@@ -114,15 +120,21 @@ static void sha256_block_generic(u32 state[SHA256_STATE_WORDS],SHA256_ROUND (i +5 , d, e, f, g, h, a, b, c);SHA256_ROUND (i +6 , c, d, e, f, g, h, a, b);SHA256_ROUND (i +7 , b, c, d, e, f, g, h, a); } - state[0 ] += a; state[1 ] += b; state[2 ] += c; state[3 ] += d; - state[4 ] += e; state[5 ] += f; state[6 ] += g; state[7 ] += h; + state->h[0 ] += a; + state->h[1 ] += b; + state->h[2 ] += c; + state->h[3 ] += d; + state->h[4 ] += e; + state->h[5 ] += f; + state->h[6 ] += g; + state->h[7 ] += h; } -void sha256_blocks_generic (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_generic (struct sha256_block_state *state,const u8 *data,size_t nblocks) {u32 W [64 ]; do {
lib/crypto/sparc/sha256.c +2 -2
@@ -17,14 +17,14 @@#include <linux/kernel.h> #include <linux/module.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_sha256_opcodes); -asmlinkage void sha256_sparc64_transform(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_sparc64_transform(struct sha256_block_state *state,const u8 *data,size_t nblocks); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {if (static_branch_likely (&have_sha256_opcodes))sha256_sparc64_transform (state, data, nblocks);else
lib/crypto/x86/sha256-avx-asm.S +1 -1
@@ -339,11 +339,11 @@ a = TMP_ add y0, h # h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS .endm ######################################################################## -## void sha256_transform_avx(u32 state[SHA256_STATE_WORDS], +## void sha256_transform_avx(struct sha256_block_state *state, ## const u8 *data, size_t nblocks); ######################################################################## .text SYM_FUNC_START(sha256_transform_avx) ANNOTATE_NOENDBR # since this is called only via static_call
lib/crypto/x86/sha256-avx2-asm.S +1 -1
@@ -516,11 +516,11 @@ STACK_SIZE = _CTX + _CTX_SIZE ROTATE_ARGS .endm ######################################################################## -## void sha256_transform_rorx(u32 state[SHA256_STATE_WORDS], +## void sha256_transform_rorx(struct sha256_block_state *state, ## const u8 *data, size_t nblocks); ######################################################################## .text SYM_FUNC_START(sha256_transform_rorx) ANNOTATE_NOENDBR # since this is called only via static_call
lib/crypto/x86/sha256-ni-asm.S +1 -1
@@ -104,11 +104,11 @@ * input data, and the number of 64-byte blocks to process. Once all blocks * have been processed, the state is updated with the new state. This function * only processes complete blocks. State initialization, buffering of partial * blocks, and digest finalization is expected to be handled elsewhere. * - * void sha256_ni_transform(u32 state[SHA256_STATE_WORDS], + * void sha256_ni_transform(struct sha256_block_state *state, * const u8 *data, size_t nblocks); */ .text SYM_FUNC_START(sha256_ni_transform) ANNOTATE_NOENDBR # since this is called only via static_call
lib/crypto/x86/sha256-ssse3-asm.S +1 -1
@@ -346,11 +346,11 @@ a = TMP_ add y0, h # h = h + S1 + CH + k + w + S0 + MAJ ROTATE_ARGS .endm ######################################################################## -## void sha256_transform_ssse3(u32 state[SHA256_STATE_WORDS], +## void sha256_transform_ssse3(struct sha256_block_state *state, ## const u8 *data, size_t nblocks); ######################################################################## .text SYM_FUNC_START(sha256_transform_ssse3) ANNOTATE_NOENDBR # since this is called only via static_call
lib/crypto/x86/sha256.c +5 -5
@@ -9,24 +9,24 @@#include <crypto/internal/simd.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/static_call.h> -asmlinkage void sha256_transform_ssse3(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_transform_ssse3(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage void sha256_transform_avx(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_transform_avx(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage void sha256_transform_rorx(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_transform_rorx(struct sha256_block_state *state,const u8 *data,size_t nblocks); -asmlinkage void sha256_ni_transform(u32 state[SHA256_STATE_WORDS], +asmlinkage void sha256_ni_transform(struct sha256_block_state *state,const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_sha256_x86);DEFINE_STATIC_CALL (sha256_blocks_x86, sha256_transform_ssse3); -void sha256_blocks_arch (u32 state[SHA256_STATE_WORDS ], +void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks) {if (static_branch_likely (&have_sha256_x86) &&crypto_simd_usable ()) {kernel_fpu_begin ();static_call (sha256_blocks_x86)(state, data, nblocks);
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 8/14 lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support
In reply to Eric BiggersSince HMAC support is commonly needed and is fairly simple, include it as a first-class citizen of the SHA-256 library.
The API supports both incremental and one-shot computation, and either preparing the key ahead of time or just using a raw key. The implementation is much more streamlined than crypto/hmac.c.
I've kept it consistent with the HMAC-SHA384 and HMAC-SHA512 code as much as possible.
Testing of these functions will be via sha224_kunit and sha256_kunit, added by a later commit.
--- include/crypto/sha2.h | 222 ++++++++++++++++++++++++++++++++++++++++++ lib/crypto/sha256.c | 147 +++++++++++++++++++++++++++- 2 files changed, 364 insertions(+), 5 deletions(-)
include/crypto/sha2.h +222 -0
@@ -129,10 +129,26 @@ struct __sha256_ctx {u64 bytecount;u8 buf[SHA256_BLOCK_SIZE ]__aligned(__alignof__(__be64)) ; };void __sha256_update (struct __sha256_ctx *ctx,const u8 *data,size_t len); +/* +* HMAC key and message context structs, shared by HMAC-SHA224 and HMAC-SHA256. +* The hmac_sha224_* and hmac_sha256_* structs wrap this one so that the API has +* proper typing and doesn't allow mixing the functions arbitrarily. +*/ +struct __hmac_sha256_key { +struct sha256_block_state istate; +struct sha256_block_state ostate; +}; +struct __hmac_sha256_ctx { +struct __sha256_ctx sha_ctx; +struct sha256_block_state ostate; +}; +void __hmac_sha256_init (struct __hmac_sha256_ctx *ctx, +const struct __hmac_sha256_key *key); +/** * struct sha224_ctx - Context for hashing a message with SHA-224 * @ctx: private */ struct sha224_ctx {
@@ -146,10 +162,113 @@ static inline void sha224_update(struct sha224_ctx *ctx,__sha256_update (&ctx->ctx, data, len); }void sha224_final (struct sha224_ctx *ctx,u8 out[SHA224_DIGEST_SIZE ]);void sha224 (const u8 *data,size_t len,u8 out[SHA224_DIGEST_SIZE ]); +/** +* struct hmac_sha224_key - Prepared key for HMAC-SHA224 +* @key: private +*/ +struct hmac_sha224_key { +struct __hmac_sha256_key key; +}; + +/** +* struct hmac_sha224_ctx - Context for computing HMAC-SHA224 of a message +* @ctx: private +*/ +struct hmac_sha224_ctx { +struct __hmac_sha256_ctx ctx; +}; + +/** +* hmac_sha224_preparekey() - Prepare a key for HMAC-SHA224 +* @key: (output) the key structure to initialize +* @raw_key: the raw HMAC-SHA224 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* +* Note: the caller is responsible for zeroizing both the struct hmac_sha224_key +* and the raw key once they are no longer needed. +* +* Context: Any context. +*/ +void hmac_sha224_preparekey (struct hmac_sha224_key *key, +const u8 *raw_key,size_t raw_key_len); + +/** +* hmac_sha224_init() - Initialize an HMAC-SHA224 context for a new message +* @ctx: (output) the HMAC context to initialize +* @key: the prepared HMAC key +* +* If you don't need incremental computation, consider hmac_sha224() instead. +* +* Context: Any context. +*/ +static inline void hmac_sha224_init (struct hmac_sha224_ctx *ctx, +const struct hmac_sha224_key *key) +{ +__hmac_sha256_init (&ctx->ctx, &key->key); +} + +/** +* hmac_sha224_update() - Update an HMAC-SHA224 context with message data +* @ctx: the HMAC context to update; must have been initialized +* @data: the message data +* @data_len: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ +static inline void hmac_sha224_update (struct hmac_sha224_ctx *ctx, +const u8 *data,size_t data_len) +{ +__sha256_update (&ctx->ctx.sha_ctx, data, data_len); +} + +/** +* hmac_sha224_final() - Finish computing an HMAC-SHA224 value +* @ctx: the HMAC context to finalize; must have been initialized +* @out: (output) the resulting HMAC-SHA224 value +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ +void hmac_sha224_final (struct hmac_sha224_ctx *ctx,u8 out[SHA224_DIGEST_SIZE ]); + +/** +* hmac_sha224() - Compute HMAC-SHA224 in one shot, using a prepared key +* @key: the prepared HMAC key +* @data: the message data +* @data_len: the data length in bytes +* @out: (output) the resulting HMAC-SHA224 value +* +* If you're using the key only once, consider using hmac_sha224_usingrawkey(). +* +* Context: Any context. +*/ +void hmac_sha224 (const struct hmac_sha224_key *key, +const u8 *data,size_t data_len,u8 out[SHA224_DIGEST_SIZE ]); + +/** +* hmac_sha224_usingrawkey() - Compute HMAC-SHA224 in one shot, using a raw key +* @raw_key: the raw HMAC-SHA224 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* @data: the message data +* @data_len: the data length in bytes +* @out: (output) the resulting HMAC-SHA224 value +* +* If you're using the key multiple times, prefer to use +* hmac_sha224_preparekey() followed by multiple calls to hmac_sha224() instead. +* +* Context: Any context. +*/ +void hmac_sha224_usingrawkey (const u8 *raw_key,size_t raw_key_len, +const u8 *data,size_t data_len, +u8 out[SHA224_DIGEST_SIZE ]); +/** * struct sha256_ctx - Context for hashing a message with SHA-256 * @ctx: private */ struct sha256_ctx {
@@ -163,10 +282,113 @@ static inline void sha256_update(struct sha256_ctx *ctx,__sha256_update (&ctx->ctx, data, len); }void sha256_final (struct sha256_ctx *ctx,u8 out[SHA256_DIGEST_SIZE ]);void sha256 (const u8 *data,size_t len,u8 out[SHA256_DIGEST_SIZE ]); +/** +* struct hmac_sha256_key - Prepared key for HMAC-SHA256 +* @key: private +*/ +struct hmac_sha256_key { +struct __hmac_sha256_key key; +}; + +/** +* struct hmac_sha256_ctx - Context for computing HMAC-SHA256 of a message +* @ctx: private +*/ +struct hmac_sha256_ctx { +struct __hmac_sha256_ctx ctx; +}; + +/** +* hmac_sha256_preparekey() - Prepare a key for HMAC-SHA256 +* @key: (output) the key structure to initialize +* @raw_key: the raw HMAC-SHA256 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* +* Note: the caller is responsible for zeroizing both the struct hmac_sha256_key +* and the raw key once they are no longer needed. +* +* Context: Any context. +*/ +void hmac_sha256_preparekey (struct hmac_sha256_key *key, +const u8 *raw_key,size_t raw_key_len); + +/** +* hmac_sha256_init() - Initialize an HMAC-SHA256 context for a new message +* @ctx: (output) the HMAC context to initialize +* @key: the prepared HMAC key +* +* If you don't need incremental computation, consider hmac_sha256() instead. +* +* Context: Any context. +*/ +static inline void hmac_sha256_init (struct hmac_sha256_ctx *ctx, +const struct hmac_sha256_key *key) +{ +__hmac_sha256_init (&ctx->ctx, &key->key); +} + +/** +* hmac_sha256_update() - Update an HMAC-SHA256 context with message data +* @ctx: the HMAC context to update; must have been initialized +* @data: the message data +* @data_len: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ +static inline void hmac_sha256_update (struct hmac_sha256_ctx *ctx, +const u8 *data,size_t data_len) +{ +__sha256_update (&ctx->ctx.sha_ctx, data, data_len); +} + +/** +* hmac_sha256_final() - Finish computing an HMAC-SHA256 value +* @ctx: the HMAC context to finalize; must have been initialized +* @out: (output) the resulting HMAC-SHA256 value +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ +void hmac_sha256_final (struct hmac_sha256_ctx *ctx,u8 out[SHA256_DIGEST_SIZE ]); + +/** +* hmac_sha256() - Compute HMAC-SHA256 in one shot, using a prepared key +* @key: the prepared HMAC key +* @data: the message data +* @data_len: the data length in bytes +* @out: (output) the resulting HMAC-SHA256 value +* +* If you're using the key only once, consider using hmac_sha256_usingrawkey(). +* +* Context: Any context. +*/ +void hmac_sha256 (const struct hmac_sha256_key *key, +const u8 *data,size_t data_len,u8 out[SHA256_DIGEST_SIZE ]); + +/** +* hmac_sha256_usingrawkey() - Compute HMAC-SHA256 in one shot, using a raw key +* @raw_key: the raw HMAC-SHA256 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* @data: the message data +* @data_len: the data length in bytes +* @out: (output) the resulting HMAC-SHA256 value +* +* If you're using the key multiple times, prefer to use +* hmac_sha256_preparekey() followed by multiple calls to hmac_sha256() instead. +* +* Context: Any context. +*/ +void hmac_sha256_usingrawkey (const u8 *raw_key,size_t raw_key_len, +const u8 *data,size_t data_len, +u8 out[SHA256_DIGEST_SIZE ]); +/* State for the SHA-512 (and SHA-384) compression function */ struct sha512_block_state {u64 h[8 ]; };
lib/crypto/sha256.c +142 -5
173 lines not on this page · raw message
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 9/14 crypto: sha256 - Wrap library and add HMAC support
In reply to Eric BiggersLike I did for crypto/sha512.c, rework crypto/sha256.c to simply wrap the normal library functions instead of accessing the low-level arch- optimized and generic block functions directly. Also add support for HMAC-SHA224 and HMAC-SHA256, again just wrapping the library functions.
Since the replacement crypto_shash algorithms are implemented using the (potentially arch-optimized) library functions, give them driver names ending with "-lib" rather than "-generic". Update crypto/testmgr.c and a couple odd drivers to take this change in driver name into account.
Besides the above cases which are accounted for, there are no known cases where the driver names were being depended on. There is potential for confusion for people manually checking /proc/crypto (e.g.
https://lore.kernel.org/r/[email protected]/),
but really people just need to get used to the driver name not being meaningful for the software algorithms. Historically, the optimized code was disabled by default, so there was some purpose to checking whether it was enabled or not. However, this is now fixed for all SHA-2 algorithms, and the library code just always does the right thing. E.g. if the CPU supports SHA-256 instructions, they are used.
This change does also mean that the generic partial block handling code in crypto/shash.c, which got added in 6.16, no longer gets used. But that's fine; the library has to implement the partial block handling anyway, and it's better to do it in the library since the block size and other properties of the algorithm are all fixed at compile time there, resulting in more streamlined code.
--- crypto/Kconfig | 4 +- crypto/Makefile | 1 - crypto/sha256.c | 286 ++++++++++++-------------- crypto/testmgr.c | 12 ++ drivers/crypto/img-hash.c | 4 +- drivers/crypto/starfive/jh7110-hash.c | 8 +- 6 files changed, 148 insertions(+), 167 deletions(-)
crypto/Kconfig +2 -2
@@ -990,13 +990,13 @@ config CRYPTO_SHA1 config CRYPTO_SHA256 tristate "SHA-224 and SHA-256" select CRYPTO_HASH select CRYPTO_LIB_SHA256 - select CRYPTO_LIB_SHA256_GENERIC help - SHA-224 and SHA-256 secure hash algorithms (FIPS 180, ISO/IEC 10118-3) + SHA-224 and SHA-256 secure hash algorithms (FIPS 180, ISO/IEC + 10118-3), including HMAC support. This is required for IPsec AH (XFRM_AH) and IPsec ESP (XFRM_ESP). Used by the btrfs filesystem, Ceph, NFS, and SMB. config CRYPTO_SHA512
crypto/Makefile +0 -1
@@ -75,11 +75,10 @@ obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o obj-$(CONFIG_CRYPTO_MD4) += md4.o obj-$(CONFIG_CRYPTO_MD5) += md5.o obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o obj-$(CONFIG_CRYPTO_SHA256) += sha256.o -CFLAGS_sha256.o += -DARCH=$(ARCH) 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
crypto/sha256.c +128 -158
411 lines not on this page · raw message
crypto/testmgr.c +12 -0
@@ -4268,45 +4268,51 @@ static const struct alg_test_desc alg_test_descs[] = { .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))", .test = alg_test_null, .fips_allowed = 1, }, { .alg = "authenc(hmac(sha224),cbc(des))", + .generic_driver = "authenc(hmac-sha224-lib,cbc(des-generic))", .test = alg_test_aead, .suite = { .aead = __VECS(hmac_sha224_des_cbc_tv_temp) } }, { .alg = "authenc(hmac(sha224),cbc(des3_ede))", + .generic_driver = "authenc(hmac-sha224-lib,cbc(des3_ede-generic))", .test = alg_test_aead, .suite = { .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp) } }, { .alg = "authenc(hmac(sha256),cbc(aes))", + .generic_driver = "authenc(hmac-sha256-lib,cbc(aes-generic))", .test = alg_test_aead, .fips_allowed = 1, .suite = { .aead = __VECS(hmac_sha256_aes_cbc_tv_temp) } }, { .alg = "authenc(hmac(sha256),cbc(des))", + .generic_driver = "authenc(hmac-sha256-lib,cbc(des-generic))", .test = alg_test_aead, .suite = { .aead = __VECS(hmac_sha256_des_cbc_tv_temp) } }, { .alg = "authenc(hmac(sha256),cbc(des3_ede))", + .generic_driver = "authenc(hmac-sha256-lib,cbc(des3_ede-generic))", .test = alg_test_aead, .suite = { .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp) } }, { .alg = "authenc(hmac(sha256),ctr(aes))", .test = alg_test_null, .fips_allowed = 1, }, { .alg = "authenc(hmac(sha256),cts(cbc(aes)))", + .generic_driver = "authenc(hmac-sha256-lib,cts(cbc(aes-generic)))", .test = alg_test_aead, .suite = { .aead = __VECS(krb5_test_aes128_cts_hmac_sha256_128) } }, {
@@ -5013,17 +5019,19 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .sig = __VECS(ecrdsa_tv_template) } }, { .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)", + .generic_driver = "essiv(authenc(hmac-sha256-lib,cbc(aes-generic)),sha256-lib)", .test = alg_test_aead, .fips_allowed = 1, .suite = { .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp) } }, { .alg = "essiv(cbc(aes),sha256)", + .generic_driver = "essiv(cbc(aes-generic),sha256-lib)", .test = alg_test_skcipher, .fips_allowed = 1, .suite = { .cipher =__VECS (essiv_aes_cbc_tv_template) }
@@ -5119,17 +5127,19 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(hmac_sha1_tv_template) } }, { .alg = "hmac(sha224)", + .generic_driver = "hmac-sha224-lib", .test = alg_test_hash, .fips_allowed = 1, .suite = { .hash = __VECS(hmac_sha224_tv_template) } }, { .alg = "hmac(sha256)", + .generic_driver = "hmac-sha256-lib", .test = alg_test_hash, .fips_allowed = 1, .suite = { .hash =__VECS (hmac_sha256_tv_template) }
@@ -5457,17 +5467,19 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(sha1_tv_template) } }, { .alg = "sha224", + .generic_driver = "sha224-lib", .test = alg_test_hash, .fips_allowed = 1, .suite = { .hash = __VECS(sha224_tv_template) } }, { .alg = "sha256", + .generic_driver = "sha256-lib", .test = alg_test_hash, .fips_allowed = 1, .suite = { .hash =__VECS (sha256_tv_template) }
drivers/crypto/img-hash.c +2 -2
@@ -708,16 +708,16 @@ static int img_hash_cra_sha1_init(struct crypto_tfm *tfm)return img_hash_cra_init (tfm,"sha1-generic" ); }static int img_hash_cra_sha224_init (struct crypto_tfm *tfm) { -return img_hash_cra_init (tfm,"sha224-generic" ); +return img_hash_cra_init (tfm,"sha224-lib" ); }static int img_hash_cra_sha256_init (struct crypto_tfm *tfm) { -return img_hash_cra_init (tfm,"sha256-generic" ); +return img_hash_cra_init (tfm,"sha256-lib" ); }static void img_hash_cra_exit (struct crypto_tfm *tfm) {struct img_hash_ctx *tctx =crypto_tfm_ctx (tfm);
drivers/crypto/starfive/jh7110-hash.c +4 -4
@@ -491,17 +491,17 @@ static int starfive_hash_setkey(struct crypto_ahash *hash,return starfive_hash_long_setkey (ctx, key, keylen, alg_name); }static int starfive_sha224_init_tfm (struct crypto_ahash *hash) { -return starfive_hash_init_tfm (hash,"sha224-generic" , +return starfive_hash_init_tfm (hash,"sha224-lib" ,STARFIVE_HASH_SHA224 ,0 ); }static int starfive_sha256_init_tfm (struct crypto_ahash *hash) { -return starfive_hash_init_tfm (hash,"sha256-generic" , +return starfive_hash_init_tfm (hash,"sha256-lib" ,STARFIVE_HASH_SHA256 ,0 ); }static int starfive_sha384_init_tfm (struct crypto_ahash *hash) {
@@ -521,17 +521,17 @@ static int starfive_sm3_init_tfm(struct crypto_ahash *hash) STARFIVE_HASH_SM3, 0); }static int starfive_hmac_sha224_init_tfm (struct crypto_ahash *hash) { -return starfive_hash_init_tfm (hash,"hmac(sha224-generic)" , +return starfive_hash_init_tfm (hash,"hmac-sha224-lib" ,STARFIVE_HASH_SHA224 ,1 ); }static int starfive_hmac_sha256_init_tfm (struct crypto_ahash *hash) { -return starfive_hash_init_tfm (hash,"hmac(sha256-generic)" , +return starfive_hash_init_tfm (hash,"hmac-sha256-lib" ,STARFIVE_HASH_SHA256 ,1 ); }static int starfive_hmac_sha384_init_tfm (struct crypto_ahash *hash) {
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 10/14 crypto: sha256 - Use same state format as legacy drivers
In reply to Eric BiggersMake the export and import functions for the sha224, sha256, hmac(sha224), and hmac(sha256) shash algorithms use the same format as the padlock-sha and nx-sha256 drivers, as required by Herbert.
--- crypto/sha256.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
crypto/sha256.c +95 -0
@@ -11,10 +11,47 @@#include <crypto/internal/hash.h> #include <crypto/sha2.h> #include <linux/kernel.h> #include <linux/module.h> +/* +* Export and import functions. crypto_shash wants a particular format that +* matches that used by some legacy drivers. It currently is the same as the +* library SHA context, except the value in bytecount must be block-aligned and +* the remainder must be stored in an extra u8 appended to the struct. +*/ + +#define SHA256_SHASH_STATE_SIZE 105 +static_assert (offsetof(struct __sha256_ctx , state) ==0 ); +static_assert (offsetof(struct __sha256_ctx , bytecount) ==32 ); +static_assert (offsetof(struct __sha256_ctx , buf) ==40 ); +static_assert (sizeof (struct __sha256_ctx ) +1 ==SHA256_SHASH_STATE_SIZE ); + +static int __crypto_sha256_export (const struct __sha256_ctx *ctx0,void *out) +{ +struct __sha256_ctx ctx = *ctx0; +unsigned int partial; +u8 *p = out; + + partial = ctx.bytecount %SHA256_BLOCK_SIZE ; + ctx.bytecount -= partial; +memcpy (p, &ctx,sizeof (ctx)); + p +=sizeof (ctx); + *p = partial; +return 0 ; +} + +static int __crypto_sha256_import (struct __sha256_ctx *ctx,const void *in) +{ +const u8 *p = in; + +memcpy (ctx, p,sizeof (*ctx)); + p +=sizeof (*ctx); + ctx->bytecount += *p; +return 0 ; +} +/* SHA-224 */ const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE ] = {0xd1 ,0x4a ,0x02 ,0x8c ,0x2a ,0x3a ,0x2b ,0xc9 ,0x47 ,0x61 ,0x02 ,0xbb ,0x28 ,0x82 ,0x34 ,0xc4 ,0x15 ,0xa2 ,
@@ -49,10 +86,20 @@ static int crypto_sha224_digest(struct shash_desc *desc, {sha224 (data, len, out);return 0 ; } +static int crypto_sha224_export (struct shash_desc *desc,void *out) +{ +return __crypto_sha256_export (&SHA224_CTX (desc)->ctx, out); +} + +static int crypto_sha224_import (struct shash_desc *desc,const void *in) +{ +return __crypto_sha256_import (&SHA224_CTX (desc)->ctx, in); +} +/* SHA-256 */ const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE ] = {0xe3 ,0xb0 ,0xc4 ,0x42 ,0x98 ,0xfc ,0x1c ,0x14 ,0x9a ,0xfb ,0xf4 ,0xc8 ,0x99 ,0x6f ,0xb9 ,0x24 ,
@@ -87,10 +134,20 @@ static int crypto_sha256_digest(struct shash_desc *desc, {sha256 (data, len, out);return 0 ; } +static int crypto_sha256_export (struct shash_desc *desc,void *out) +{ +return __crypto_sha256_export (&SHA256_CTX (desc)->ctx, out); +} + +static int crypto_sha256_import (struct shash_desc *desc,const void *in) +{ +return __crypto_sha256_import (&SHA256_CTX (desc)->ctx, in); +} +/* HMAC-SHA224 */ #define HMAC_SHA224_KEY (tfm) ((struct hmac_sha224_key *)crypto_shash_ctx(tfm))#define HMAC_SHA224_CTX (desc) ((struct hmac_sha224_ctx *)shash_desc_ctx(desc))
@@ -126,10 +183,23 @@ static int crypto_hmac_sha224_digest(struct shash_desc *desc, {hmac_sha224 (HMAC_SHA224_KEY (desc->tfm), data, len, out);return 0 ; } +static int crypto_hmac_sha224_export (struct shash_desc *desc,void *out) +{ +return __crypto_sha256_export (&HMAC_SHA224_CTX (desc)->ctx.sha_ctx, out); +} + +static int crypto_hmac_sha224_import (struct shash_desc *desc,const void *in) +{ +struct hmac_sha224_ctx *ctx =HMAC_SHA224_CTX (desc); + + ctx->ctx.ostate =HMAC_SHA224_KEY (desc->tfm)->key.ostate; +return __crypto_sha256_import (&ctx->ctx.sha_ctx, in); +} +/* HMAC-SHA256 */ #define HMAC_SHA256_KEY (tfm) ((struct hmac_sha256_key *)crypto_shash_ctx(tfm))#define HMAC_SHA256_CTX (desc) ((struct hmac_sha256_ctx *)shash_desc_ctx(desc))
@@ -165,10 +235,23 @@ static int crypto_hmac_sha256_digest(struct shash_desc *desc, {hmac_sha256 (HMAC_SHA256_KEY (desc->tfm), data, len, out);return 0 ; } +static int crypto_hmac_sha256_export (struct shash_desc *desc,void *out) +{ +return __crypto_sha256_export (&HMAC_SHA256_CTX (desc)->ctx.sha_ctx, out); +} + +static int crypto_hmac_sha256_import (struct shash_desc *desc,const void *in) +{ +struct hmac_sha256_ctx *ctx =HMAC_SHA256_CTX (desc); + + ctx->ctx.ostate =HMAC_SHA256_KEY (desc->tfm)->key.ostate; +return __crypto_sha256_import (&ctx->ctx.sha_ctx, in); +} +/* Algorithm definitions */ static struct shash_alg algs[] = { { .base.cra_name ="sha224" ,
@@ -179,11 +262,14 @@ static struct shash_alg algs[] = { .digestsize = SHA224_DIGEST_SIZE, .init = crypto_sha224_init, .update = crypto_sha224_update, .final = crypto_sha224_final, .digest = crypto_sha224_digest, + .export = crypto_sha224_export, + .import = crypto_sha224_import, .descsize = sizeof(struct sha224_ctx), + .statesize = SHA256_SHASH_STATE_SIZE, }, { .base.cra_name = "sha256", .base.cra_driver_name = "sha256-lib", .base.cra_priority = 300,
@@ -192,11 +278,14 @@ static struct shash_alg algs[] = { .digestsize = SHA256_DIGEST_SIZE, .init = crypto_sha256_init, .update = crypto_sha256_update, .final = crypto_sha256_final, .digest = crypto_sha256_digest, + .export = crypto_sha256_export, + .import = crypto_sha256_import, .descsize = sizeof(struct sha256_ctx), + .statesize = SHA256_SHASH_STATE_SIZE, }, { .base.cra_name = "hmac(sha224)", .base.cra_driver_name = "hmac-sha224-lib", .base.cra_priority = 300,
@@ -207,11 +296,14 @@ static struct shash_alg algs[] = { .setkey = crypto_hmac_sha224_setkey, .init = crypto_hmac_sha224_init, .update = crypto_hmac_sha224_update, .final = crypto_hmac_sha224_final, .digest = crypto_hmac_sha224_digest, + .export = crypto_hmac_sha224_export, + .import = crypto_hmac_sha224_import, .descsize = sizeof(struct hmac_sha224_ctx), + .statesize = SHA256_SHASH_STATE_SIZE, }, { .base.cra_name = "hmac(sha256)", .base.cra_driver_name = "hmac-sha256-lib", .base.cra_priority = 300,
@@ -222,11 +314,14 @@ static struct shash_alg algs[] = { .setkey = crypto_hmac_sha256_setkey, .init = crypto_hmac_sha256_init, .update = crypto_hmac_sha256_update, .final = crypto_hmac_sha256_final, .digest = crypto_hmac_sha256_digest, + .export = crypto_hmac_sha256_export, + .import = crypto_hmac_sha256_import, .descsize = sizeof(struct hmac_sha256_ctx), + .statesize = SHA256_SHASH_STATE_SIZE, }, };static int __init crypto_sha256_mod_init (void ) {
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 11/14 lib/crypto: sha256: Remove sha256_is_arch_optimized()
In reply to Eric BiggersRemove sha256_is_arch_optimized(), since it is no longer used.
--- arch/mips/cavium-octeon/crypto/octeon-sha256.c | 6 ------ include/crypto/internal/sha2.h | 8 -------- lib/crypto/arm/sha256.c | 7 ------- lib/crypto/arm64/sha256.c | 7 ------- lib/crypto/powerpc/sha256.c | 6 ------ lib/crypto/riscv/sha256.c | 6 ------ lib/crypto/s390/sha256.c | 6 ------ lib/crypto/sparc/sha256.c | 6 ------ lib/crypto/x86/sha256.c | 6 ------ 9 files changed, 58 deletions(-)
arch/mips/cavium-octeon/crypto/octeon-sha256.c +0 -6
@@ -59,14 +59,8 @@ void sha256_blocks_arch(struct sha256_block_state *state, state64[3 ] =read_octeon_64bit_hash_dword (3 );octeon_crypto_disable (&cop2_state, flags); }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -return octeon_has_crypto (); -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("SHA-256 Secure Hash Algorithm (OCTEON)" );MODULE_AUTHOR ("Aaro Koskinen <[email protected]>" );
include/crypto/internal/sha2.h +0 -8
@@ -7,18 +7,10 @@#include <linux/compiler_attributes.h> #include <linux/string.h> #include <linux/types.h> #include <linux/unaligned.h> -#if IS_ENABLED (CONFIG_CRYPTO_ARCH_HAVE_LIB_SHA256 ) -bool sha256_is_arch_optimized (void ); -#else -static inline bool sha256_is_arch_optimized (void ) -{ -return false; -} -#endif void sha256_blocks_generic (struct sha256_block_state *state,const u8 *data,size_t nblocks);void sha256_blocks_arch (struct sha256_block_state *state,const u8 *data,size_t nblocks);
lib/crypto/arm/sha256.c +0 -7
@@ -35,17 +35,10 @@ void sha256_blocks_arch(struct sha256_block_state *state,sha256_block_data_order (state, data, nblocks); } }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -/* We always can use at least the ARM scalar implementation. */ -return true; -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -static int __init sha256_arm_mod_init (void ) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) && (elf_hwcap &HWCAP_NEON )) {static_branch_enable (&have_neon);if (elf_hwcap2 &HWCAP2_SHA2 )
lib/crypto/arm64/sha256.c +0 -7
@@ -45,17 +45,10 @@ void sha256_blocks_arch(struct sha256_block_state *state,sha256_block_data_order (state, data, nblocks); } }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -/* We always can use at least the ARM64 scalar implementation. */ -return true; -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -static int __init sha256_arm64_mod_init (void ) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&cpu_have_named_feature (ASIMD )) {static_branch_enable (&have_neon);
lib/crypto/powerpc/sha256.c +0 -6
@@ -58,13 +58,7 @@ void sha256_blocks_arch(struct sha256_block_state *state, nblocks -= unit; }while (nblocks); }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -return true; -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("SHA-256 Secure Hash Algorithm, SPE optimized" );
lib/crypto/riscv/sha256.c +0 -6
@@ -32,16 +32,10 @@ void sha256_blocks_arch(struct sha256_block_state *state,sha256_blocks_generic (state, data, nblocks); } }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -return static_key_enabled (&have_extensions); -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -static int __init riscv64_sha256_mod_init (void ) {/* Both zvknha and zvknhb provide the SHA-256 instructions. */ if ((riscv_isa_extension_available(NULL, ZVKNHA) || riscv_isa_extension_available(NULL, ZVKNHB)) &&
lib/crypto/s390/sha256.c +0 -6
@@ -21,16 +21,10 @@ void sha256_blocks_arch(struct sha256_block_state *state,else sha256_blocks_generic (state, data, nblocks); }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -return static_key_enabled (&have_cpacf_sha256); -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -static int __init sha256_s390_mod_init (void ) {if (cpu_have_feature (S390_CPU_FEATURE_MSA ) &&cpacf_query_func (CPACF_KIMD ,CPACF_KIMD_SHA_256 ))static_branch_enable (&have_cpacf_sha256);
lib/crypto/sparc/sha256.c +0 -6
@@ -30,16 +30,10 @@ void sha256_blocks_arch(struct sha256_block_state *state,else sha256_blocks_generic (state, data, nblocks); }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -return static_key_enabled (&have_sha256_opcodes); -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -static int __init sha256_sparc64_mod_init (void ) {unsigned long cfr;if (!(sparc64_elf_hwcap &HWCAP_SPARC_CRYPTO ))
lib/crypto/x86/sha256.c +0 -6
@@ -35,16 +35,10 @@ void sha256_blocks_arch(struct sha256_block_state *state,sha256_blocks_generic (state, data, nblocks); } }EXPORT_SYMBOL_GPL (sha256_blocks_arch); -bool sha256_is_arch_optimized (void ) -{ -return static_key_enabled (&have_sha256_x86); -} -EXPORT_SYMBOL_GPL (sha256_is_arch_optimized); -static int __init sha256_x86_mod_init (void ) {if (boot_cpu_has (X86_FEATURE_SHA_NI )) {static_call_update (sha256_blocks_x86, sha256_ni_transform); } else if (cpu_has_xfeatures(XFEATURE_MASK_SSE |
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 12/14 lib/crypto: sha256: Consolidate into single module
In reply to Eric BiggersConsolidate the CPU-based SHA-256 code into a single module, following what I did with SHA-512:
- Each arch now provides a header file lib/crypto/$(SRCARCH)/sha256.h, replacing lib/crypto/$(SRCARCH)/sha256.c. The header defines sha256_blocks() and optionally sha256_mod_init_arch(). It is included by lib/crypto/sha256.c, and thus the code gets built into the single libsha256 module, with proper inlining and dead code elimination.
- sha256_blocks_generic() is moved from lib/crypto/sha256-generic.c into lib/crypto/sha256.c. It's now a static function marked with __maybe_unused, so the compiler automatically eliminates it in any cases where it's not used.
- Whether arch-optimized SHA-256 is buildable is now controlled centrally by lib/crypto/Kconfig instead of by lib/crypto/$(SRCARCH)/Kconfig. The conditions for enabling it remain the same as before, and it remains enabled by default.
- Any additional arch-specific translation units for the optimized SHA-256 code (such as assembly files) are now compiled by lib/crypto/Makefile instead of lib/crypto/$(SRCARCH)/Makefile.
---
arch/mips/cavium-octeon/Kconfig | 6 -
arch/mips/cavium-octeon/crypto/Makefile | 1 -
include/crypto/internal/sha2.h | 52 ------
lib/crypto/Kconfig | 26 ++-
lib/crypto/Makefile | 39 ++++-
lib/crypto/arm/Kconfig | 6 -
lib/crypto/arm/Makefile | 8 +-
lib/crypto/arm/{sha256.c => sha256.h} | 27 +---
lib/crypto/arm64/Kconfig | 5 -
lib/crypto/arm64/Makefile | 9 +-
lib/crypto/arm64/{sha256.c => sha256.h} | 29 ++--
.../crypto/mips/sha256.h | 14 +-
lib/crypto/powerpc/Kconfig | 6 -
lib/crypto/powerpc/Makefile | 3 -
lib/crypto/powerpc/{sha256.c => sha256.h} | 13 +-
lib/crypto/riscv/Kconfig | 7 -
lib/crypto/riscv/Makefile | 3 -
lib/crypto/riscv/{sha256.c => sha256.h} | 24 +--
lib/crypto/s390/Kconfig | 6 -
lib/crypto/s390/Makefile | 3 -
lib/crypto/s390/{sha256.c => sha256.h} | 23 +--
lib/crypto/sha256-generic.c | 150 ------------------
lib/crypto/sha256.c | 146 +++++++++++++++--
lib/crypto/sparc/Kconfig | 8 -
lib/crypto/sparc/Makefile | 4 -
lib/crypto/sparc/{sha256.c => sha256.h} | 29 +---
lib/crypto/x86/Kconfig | 7 -
lib/crypto/x86/Makefile | 3 -
lib/crypto/x86/{sha256.c => sha256.h} | 29 +---
29 files changed, 226 insertions(+), 460 deletions(-)
delete mode 100644 include/crypto/internal/sha2.h
rename lib/crypto/arm/{sha256.c => sha256.h} (64%)
rename lib/crypto/arm64/{sha256.c => sha256.h} (67%)
rename arch/mips/cavium-octeon/crypto/octeon-sha256.c => lib/crypto/mips/sha256.h (80%)
rename lib/crypto/powerpc/{sha256.c => sha256.h} (80%)
rename lib/crypto/riscv/{sha256.c => sha256.h} (63%)
rename lib/crypto/s390/{sha256.c => sha256.h} (50%)
delete mode 100644 lib/crypto/sha256-generic.c
delete mode 100644 lib/crypto/sparc/Kconfig
delete mode 100644 lib/crypto/sparc/Makefile
rename lib/crypto/sparc/{sha256.c => sha256.h} (62%)
rename lib/crypto/x86/{sha256.c => sha256.h} (70%)arch/mips/cavium-octeon/Kconfig +0 -6
@@ -21,16 +21,10 @@ config CAVIUM_OCTEON_CVMSEG_SIZE local memory; the larger CVMSEG is, the smaller the cache is. This selects the size of CVMSEG LM, which is in cache blocks. The legally range is from zero to 54 cache blocks (i.e. CVMSEG LM is between zero and 6192 bytes). -config CRYPTO_SHA256_OCTEON - tristate - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_LIB_SHA256_GENERIC - endif # CPU_CAVIUM_OCTEON if CAVIUM_OCTEON_SOC config CAVIUM_OCTEON_LOCK_L2
arch/mips/cavium-octeon/crypto/Makefile +0 -1
@@ -5,6 +5,5 @@ obj-y += octeon-crypto.o obj-$(CONFIG_CRYPTO_MD5_OCTEON) += octeon-md5.o obj-$(CONFIG_CRYPTO_SHA1_OCTEON) += octeon-sha1.o -obj-$(CONFIG_CRYPTO_SHA256_OCTEON) += octeon-sha256.o
include/crypto/internal/sha2.h deleted +0 -52
52 lines not on this page · raw message
lib/crypto/Kconfig +10 -16
@@ -142,24 +142,21 @@ config CRYPTO_LIB_SHA256 help Enable the SHA-256 library interface. This interface may be fulfilled by either the generic implementation or an arch-specific one, if one is available and enabled. -config CRYPTO_ARCH_HAVE_LIB_SHA256 +config CRYPTO_LIB_SHA256_ARCH bool - help - Declares whether the architecture provides an arch-specific - accelerated implementation of the SHA-256 library interface. - -config CRYPTO_LIB_SHA256_GENERIC - tristate - default CRYPTO_LIB_SHA256 if !CRYPTO_ARCH_HAVE_LIB_SHA256 - help - This symbol can be selected by arch implementations of the SHA-256 - library interface that require the generic code as a fallback, e.g., - for SIMD implementations. If no arch specific implementation is - enabled, this implementation serves the users of CRYPTO_LIB_SHA256. + depends on CRYPTO_LIB_SHA256 && !UML + default y if ARM && !CPU_V7M + default y if ARM64 + default y if MIPS && CPU_CAVIUM_OCTEON + default y if PPC && SPE + default y if RISCV && 64BIT && RISCV_ISA_V && TOOLCHAIN_HAS_VECTOR_CRYPTO + default y if S390 + default y if SPARC64 + default y if X86_64 config CRYPTO_LIB_SHA512 tristate help The SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512 library functions.
@@ -197,13 +194,10 @@ if RISCV source "lib/crypto/riscv/Kconfig" endif if S390 source "lib/crypto/s390/Kconfig" endif -if SPARC -source "lib/crypto/sparc/Kconfig" -endif if X86 source "lib/crypto/x86/Kconfig" endif endif
lib/crypto/Makefile +34 -5
@@ -64,15 +64,43 @@ libpoly1305-generic-$(CONFIG_ARCH_SUPPORTS_INT128) := poly1305-donna64.o libpoly1305-generic-y += poly1305-generic.o obj-$(CONFIG_CRYPTO_LIB_SHA1) += libsha1.o libsha1-y := sha1.o -obj-$(CONFIG_CRYPTO_LIB_SHA256) += libsha256.o -libsha256-y := sha256.o +################################################################################ -obj-$(CONFIG_CRYPTO_LIB_SHA256_GENERIC) += libsha256-generic.o -libsha256-generic-y := sha256-generic.o +obj-$(CONFIG_CRYPTO_LIB_SHA256) += libsha256.o +libsha256-y := sha256.o +ifeq ($(CONFIG_CRYPTO_LIB_SHA256_ARCH),y) +CFLAGS_sha256.o += -I$(src)/$(SRCARCH) + +ifeq ($(CONFIG_ARM),y) +libsha256-y += arm/sha256-ce.o arm/sha256-core.o +$(obj)/arm/sha256-core.S: $(src)/arm/sha256-armv4.pl + $(call cmd,perlasm) +clean-files += arm/sha256-core.S +AFLAGS_arm/sha256-core.o += $(aflags-thumb2-y) +endif + +ifeq ($(CONFIG_ARM64),y) +libsha256-y += arm64/sha256-core.o +$(obj)/arm64/sha256-core.S: $(src)/arm64/sha2-armv8.pl + $(call cmd,perlasm_with_args) +clean-files += arm64/sha256-core.S +libsha256-$(CONFIG_KERNEL_MODE_NEON) += arm64/sha256-ce.o +endif + +libsha256-$(CONFIG_PPC) += powerpc/sha256-spe-asm.o +libsha256-$(CONFIG_RISCV) += riscv/sha256-riscv64-zvknha_or_zvknhb-zvkb.o +libsha256-$(CONFIG_SPARC) += sparc/sha256_asm.o +libsha256-$(CONFIG_X86) += x86/sha256-ssse3-asm.o \ + x86/sha256-avx-asm.o \ + x86/sha256-avx2-asm.o \ + x86/sha256-ni-asm.o +endif # CONFIG_CRYPTO_LIB_SHA256_ARCH + +################################################################################ obj-$(CONFIG_CRYPTO_LIB_SHA512) += libsha512.o libsha512-y := sha512.o ifeq ($(CONFIG_CRYPTO_LIB_SHA512_ARCH),y) CFLAGS_sha512.o += -I$(src)/$(SRCARCH)
@@ -98,10 +126,12 @@ libsha512-$(CONFIG_SPARC) += sparc/sha512_asm.o libsha512-$(CONFIG_X86) += x86/sha512-ssse3-asm.o \ x86/sha512-avx-asm.o \ x86/sha512-avx2-asm.o endif # CONFIG_CRYPTO_LIB_SHA512_ARCH +################################################################################ + obj-$(CONFIG_MPILIB) += mpi/ obj-$(CONFIG_CRYPTO_SELFTESTS_FULL) += simd.o obj-$(CONFIG_CRYPTO_LIB_SM3) += libsm3.o
@@ -111,7 +141,6 @@ obj-$(CONFIG_ARM) += arm/ obj-$(CONFIG_ARM64) += arm64/ obj-$(CONFIG_MIPS) += mips/ obj-$(CONFIG_PPC) += powerpc/ obj-$(CONFIG_RISCV) += riscv/ obj-$(CONFIG_S390) += s390/ -obj-$(CONFIG_SPARC) += sparc/ obj-$(CONFIG_X86) += x86/
lib/crypto/arm/Kconfig +0 -6
@@ -20,11 +20,5 @@ config CRYPTO_CHACHA20_NEON config CRYPTO_POLY1305_ARM tristate default CRYPTO_LIB_POLY1305 select CRYPTO_ARCH_HAVE_LIB_POLY1305 - -config CRYPTO_SHA256_ARM - tristate - depends on !CPU_V7M - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256
lib/crypto/arm/Makefile +1 -7
@@ -8,25 +8,19 @@ chacha-neon-y := chacha-scalar-core.o chacha-glue.o chacha-neon-$(CONFIG_KERNEL_MODE_NEON) += chacha-neon-core.o obj-$(CONFIG_CRYPTO_POLY1305_ARM) += poly1305-arm.o poly1305-arm-y := poly1305-core.o poly1305-glue.o -obj-$(CONFIG_CRYPTO_SHA256_ARM) += sha256-arm.o -sha256-arm-y := sha256.o sha256-core.o -sha256-arm-$(CONFIG_KERNEL_MODE_NEON) += sha256-ce.o - quiet_cmd_perl = PERL $@ cmd_perl = $(PERL) $(<) > $(@) $(obj)/%-core.S: $(src)/%-armv4.pl $(call cmd,perl) -clean-files += poly1305-core.S sha256-core.S +clean-files += poly1305-core.S aflags-thumb2-$(CONFIG_THUMB2_KERNEL) := -U__thumb2__ -D__thumb2__=1 # massage the perlasm code a bit so we only get the NEON routine if we need it poly1305-aflags-$(CONFIG_CPU_V7) := -U__LINUX_ARM_ARCH__ -D__LINUX_ARM_ARCH__=5 poly1305-aflags-$(CONFIG_KERNEL_MODE_NEON) := -U__LINUX_ARM_ARCH__ -D__LINUX_ARM_ARCH__=7 AFLAGS_poly1305-core.o += $(poly1305-aflags-y) $(aflags-thumb2-y) - -AFLAGS_sha256-core.o += $(aflags-thumb2-y)
lib/crypto/arm/sha256.h renamed from lib/crypto/arm/sha256.c +8 -19
@@ -1,16 +1,13 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 optimized for ARM * * Copyright 2025 Google LLC */ #include <asm/neon.h> -#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> -#include <linux/kernel.h> -#include <linux/module.h> asmlinkage void sha256_block_data_order(struct sha256_block_state *state,const u8 *data,size_t nblocks); asmlinkage void sha256_block_data_order_neon(struct sha256_block_state *state,const u8 *data,size_t nblocks);
@@ -18,12 +15,12 @@ asmlinkage void sha256_ce_transform(struct sha256_block_state *state, const u8 *data, size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_ce); -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&crypto_simd_usable ()) {kernel_neon_begin ();if (static_branch_likely (&have_ce))
@@ -33,25 +30,17 @@ void sha256_blocks_arch(struct sha256_block_state *state,kernel_neon_end (); }else {sha256_block_data_order (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); -static int __init sha256_arm_mod_init (void ) +#ifdef CONFIG_KERNEL_MODE_NEON +#define sha256_mod_init_arch sha256_mod_init_arch +static inline void sha256_mod_init_arch (void ) { -if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) && (elf_hwcap &HWCAP_NEON )) { +if (elf_hwcap &HWCAP_NEON ) {static_branch_enable (&have_neon);if (elf_hwcap2 &HWCAP2_SHA2 )static_branch_enable (&have_ce); } -return 0 ; } -subsys_initcall (sha256_arm_mod_init); - -static void __exit sha256_arm_mod_exit(void) -{ -} -module_exit (sha256_arm_mod_exit); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 optimized for ARM" ); +#endif /* CONFIG_KERNEL_MODE_NEON */
lib/crypto/arm64/Kconfig +0 -5
@@ -10,10 +10,5 @@ config CRYPTO_CHACHA20_NEON config CRYPTO_POLY1305_NEON tristate depends on KERNEL_MODE_NEON default CRYPTO_LIB_POLY1305 select CRYPTO_ARCH_HAVE_LIB_POLY1305 - -config CRYPTO_SHA256_ARM64 - tristate - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256
lib/crypto/arm64/Makefile +1 -8
@@ -6,19 +6,12 @@ chacha-neon-y := chacha-neon-core.o chacha-neon-glue.o obj-$(CONFIG_CRYPTO_POLY1305_NEON) += poly1305-neon.o poly1305-neon-y := poly1305-core.o poly1305-glue.o AFLAGS_poly1305-core.o += -Dpoly1305_init=poly1305_block_init_arch AFLAGS_poly1305-core.o += -Dpoly1305_emit=poly1305_emit_arch -obj-$(CONFIG_CRYPTO_SHA256_ARM64) += sha256-arm64.o -sha256-arm64-y := sha256.o sha256-core.o -sha256-arm64-$(CONFIG_KERNEL_MODE_NEON) += sha256-ce.o - quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $(<) void $(@) $(obj)/%-core.S: $(src)/%-armv8.pl $(call cmd,perlasm) -$(obj)/sha256-core.S: $(src)/sha2-armv8.pl - $(call cmd,perlasm) - -clean-files += poly1305-core.S sha256-core.S +clean-files += poly1305-core.S
lib/crypto/arm64/sha256.h renamed from lib/crypto/arm64/sha256.c +9 -20
@@ -1,16 +1,14 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 optimized for ARM64 * * Copyright 2025 Google LLC */ #include <asm/neon.h> -#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> -#include <linux/kernel.h> -#include <linux/module.h> +#include <linux/cpufeature.h> asmlinkage void sha256_block_data_order(struct sha256_block_state *state,const u8 *data,size_t nblocks); asmlinkage void sha256_block_neon(struct sha256_block_state *state,const u8 *data,size_t nblocks);
@@ -18,12 +16,12 @@ asmlinkage size_t __sha256_ce_transform(struct sha256_block_state *state, const u8 *data, size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_ce); -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&crypto_simd_usable ()) {if (static_branch_likely (&have_ce)) { do {
@@ -43,26 +41,17 @@ void sha256_blocks_arch(struct sha256_block_state *state, } }else {sha256_block_data_order (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); -static int __init sha256_arm64_mod_init (void ) +#ifdef CONFIG_KERNEL_MODE_NEON +#define sha256_mod_init_arch sha256_mod_init_arch +static inline void sha256_mod_init_arch (void ) { -if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) && -cpu_have_named_feature (ASIMD )) { +if (cpu_have_named_feature (ASIMD )) {static_branch_enable (&have_neon);if (cpu_have_named_feature (SHA2 ))static_branch_enable (&have_ce); } -return 0 ; } -subsys_initcall (sha256_arm64_mod_init); - -static void __exit sha256_arm64_mod_exit(void) -{ -} -module_exit (sha256_arm64_mod_exit); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 optimized for ARM64" ); +#endif /* CONFIG_KERNEL_MODE_NEON */
lib/crypto/mips/sha256.h renamed from arch/mips/cavium-octeon/crypto/octeon-sha256.c +3 -11
@@ -1,6 +1,6 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 Secure Hash Algorithm. * * Adapted for OCTEON by Aaro Koskinen <[email protected]>. *
@@ -12,20 +12,17 @@* SHA224 Support Copyright 2007 Intel Corporation <[email protected]> */ #include <asm/octeon/crypto.h> #include <asm/octeon/octeon.h> -#include <crypto/internal/sha2.h> -#include <linux/kernel.h> -#include <linux/module.h> /* * We pass everything as 64-bit. OCTEON can handle misaligned data. */ -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {struct octeon_cop2_state cop2_state;u64 *state64 = (u64 *)state;unsigned long flags;
@@ -57,10 +54,5 @@ void sha256_blocks_arch(struct sha256_block_state *state, state64[1 ] =read_octeon_64bit_hash_dword (1 ); state64[2 ] =read_octeon_64bit_hash_dword (2 ); state64[3 ] =read_octeon_64bit_hash_dword (3 );octeon_crypto_disable (&cop2_state, flags); } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 Secure Hash Algorithm (OCTEON)" ); -MODULE_AUTHOR ("Aaro Koskinen <[email protected]>" );
lib/crypto/powerpc/Kconfig +0 -6
@@ -12,11 +12,5 @@ config CRYPTO_POLY1305_P10 depends on PPC64 && CPU_LITTLE_ENDIAN && VSX depends on BROKEN # Needs to be fixed to work in softirq context default CRYPTO_LIB_POLY1305 select CRYPTO_ARCH_HAVE_LIB_POLY1305 select CRYPTO_LIB_POLY1305_GENERIC - -config CRYPTO_SHA256_PPC_SPE - tristate - depends on SPE - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256
lib/crypto/powerpc/Makefile +0 -3
@@ -3,8 +3,5 @@ obj-$(CONFIG_CRYPTO_CHACHA20_P10) += chacha-p10-crypto.o chacha-p10-crypto-y := chacha-p10-glue.o chacha-p10le-8x.o obj-$(CONFIG_CRYPTO_POLY1305_P10) += poly1305-p10-crypto.o poly1305-p10-crypto-y := poly1305-p10-glue.o poly1305-p10le_64.o - -obj-$(CONFIG_CRYPTO_SHA256_PPC_SPE) += sha256-ppc-spe.o -sha256-ppc-spe-y := sha256.o sha256-spe-asm.o
lib/crypto/powerpc/sha256.h renamed from lib/crypto/powerpc/sha256.c +3 -10
@@ -1,19 +1,16 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 Secure Hash Algorithm, SPE optimized * * Based on generic implementation. The assembler module takes care * about the SPE registers so it can run from interrupt context. * * Copyright (c) 2015 Markus Stockhausen <[email protected]> */ #include <asm/switch_to.h> -#include <crypto/internal/sha2.h> -#include <linux/kernel.h> -#include <linux/module.h> #include <linux/preempt.h> /* * MAX_BYTES defines the number of bytes that are allowed to be processed * between preempt_disable() and preempt_enable(). SHA256 takes ~2,000
@@ -40,12 +37,12 @@ static void spe_end(void)disable_kernel_spe ();/* reenable preemption */ preempt_enable (); } -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) { do { /* cut input data into smaller blocks */ u32 unit = min_t(size_t, nblocks, MAX_BYTES / SHA256_BLOCK_SIZE);
@@ -56,9 +53,5 @@ void sha256_blocks_arch(struct sha256_block_state *state, data += unit *SHA256_BLOCK_SIZE ; nblocks -= unit; }while (nblocks); } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 Secure Hash Algorithm, SPE optimized" );
lib/crypto/riscv/Kconfig +0 -7
@@ -4,12 +4,5 @@ config CRYPTO_CHACHA_RISCV64 tristate depends on 64BIT && RISCV_ISA_V && TOOLCHAIN_HAS_VECTOR_CRYPTO default CRYPTO_LIB_CHACHA select CRYPTO_ARCH_HAVE_LIB_CHACHA select CRYPTO_LIB_CHACHA_GENERIC - -config CRYPTO_SHA256_RISCV64 - tristate - depends on 64BIT && RISCV_ISA_V && TOOLCHAIN_HAS_VECTOR_CRYPTO - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_LIB_SHA256_GENERIC
lib/crypto/riscv/Makefile +0 -3
@@ -1,7 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_CRYPTO_CHACHA_RISCV64) += chacha-riscv64.o chacha-riscv64-y := chacha-riscv64-glue.o chacha-riscv64-zvkb.o - -obj-$(CONFIG_CRYPTO_SHA256_RISCV64) += sha256-riscv64.o -sha256-riscv64-y := sha256.o sha256-riscv64-zvknha_or_zvknhb-zvkb.o
lib/crypto/riscv/sha256.h renamed from lib/crypto/riscv/sha256.c +5 -19
@@ -1,6 +1,6 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 (RISC-V accelerated) * * Copyright (C) 2022 VRULL GmbH * Author: Heiko Stuebner <[email protected]>
@@ -8,49 +8,35 @@* Copyright (C) 2023 SiFive, Inc. * Author: Jerry Shih <[email protected]> */ #include <asm/vector.h> -#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> -#include <linux/kernel.h> -#include <linux/module.h> asmlinkage voidsha256_transform_zvknha_or_zvknhb_zvkb (struct sha256_block_state *state,const u8 *data,size_t nblocks);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_extensions); -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {if (static_branch_likely (&have_extensions) &&crypto_simd_usable ()) {kernel_vector_begin ();sha256_transform_zvknha_or_zvknhb_zvkb (state, data, nblocks);kernel_vector_end (); }else {sha256_blocks_generic (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); -static int __init riscv64_sha256_mod_init (void ) +#define sha256_mod_init_arch sha256_mod_init_arch +static inline void sha256_mod_init_arch (void ) {/* Both zvknha and zvknhb provide the SHA-256 instructions. */ if ((riscv_isa_extension_available (NULL ,ZVKNHA ) ||riscv_isa_extension_available (NULL ,ZVKNHB )) &&riscv_isa_extension_available (NULL ,ZVKB ) &&riscv_vector_vlen () >=128 )static_branch_enable (&have_extensions); -return 0 ; } -subsys_initcall (riscv64_sha256_mod_init); - -static void __exit riscv64_sha256_mod_exit(void) -{ -} -module_exit (riscv64_sha256_mod_exit); - -MODULE_DESCRIPTION ("SHA-256 (RISC-V accelerated)" ); -MODULE_AUTHOR ("Heiko Stuebner <[email protected]>" ); -MODULE_LICENSE ("GPL" );
lib/crypto/s390/Kconfig +0 -6
@@ -3,11 +3,5 @@ config CRYPTO_CHACHA_S390 tristate default CRYPTO_LIB_CHACHA select CRYPTO_LIB_CHACHA_GENERIC select CRYPTO_ARCH_HAVE_LIB_CHACHA - -config CRYPTO_SHA256_S390 - tristate - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_LIB_SHA256_GENERIC
lib/crypto/s390/Makefile +0 -3
@@ -1,7 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_CRYPTO_CHACHA_S390) += chacha_s390.o chacha_s390-y := chacha-glue.o chacha-s390.o - -obj-$(CONFIG_CRYPTO_SHA256_S390) += sha256-s390.o -sha256-s390-y := sha256.o
lib/crypto/s390/sha256.h renamed from lib/crypto/s390/sha256.c +5 -18
@@ -1,41 +1,28 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 optimized using the CP Assist for Cryptographic Functions (CPACF) * * Copyright 2025 Google LLC */ #include <asm/cpacf.h> -#include <crypto/internal/sha2.h> #include <linux/cpufeature.h> -#include <linux/kernel.h> -#include <linux/module.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_cpacf_sha256); -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {if (static_branch_likely (&have_cpacf_sha256))cpacf_kimd (CPACF_KIMD_SHA_256 , state, data, nblocks *SHA256_BLOCK_SIZE );else sha256_blocks_generic (state, data, nblocks); } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); -static int __init sha256_s390_mod_init (void ) +#define sha256_mod_init_arch sha256_mod_init_arch +static inline void sha256_mod_init_arch (void ) {if (cpu_have_feature (S390_CPU_FEATURE_MSA ) &&cpacf_query_func (CPACF_KIMD ,CPACF_KIMD_SHA_256 ))static_branch_enable (&have_cpacf_sha256); -return 0 ; } -subsys_initcall (sha256_s390_mod_init); - -static void __exit sha256_s390_mod_exit(void) -{ -} -module_exit (sha256_s390_mod_exit); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 using the CP Assist for Cryptographic Functions (CPACF)" );
lib/crypto/sha256-generic.c deleted +0 -150
150 lines not on this page · raw message
lib/crypto/sha256.c +132 -14
188 lines not on this page · raw message
lib/crypto/sparc/Kconfig deleted +0 -8
@@ -1,8 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-only - -config CRYPTO_SHA256_SPARC64 - tristate - depends on SPARC64 - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_LIB_SHA256_GENERIC
lib/crypto/sparc/Makefile deleted +0 -4
@@ -1,4 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-only - -obj-$(CONFIG_CRYPTO_SHA256_SPARC64) += sha256-sparc64.o -sha256-sparc64-y := sha256.o sha256_asm.o
lib/crypto/sparc/sha256.h renamed from lib/crypto/sparc/sha256.c +7 -22
@@ -1,58 +1,43 @@ -// SPDX-License-Identifier: GPL-2.0-only +/* SPDX-License-Identifier: GPL-2.0-only */ /* * SHA-256 accelerated using the sparc64 sha256 opcodes * * Copyright (c) Jean-Luc Cooke <[email protected]> * Copyright (c) Andrew McDonald <[email protected]> * Copyright (c) 2002 James Morris <[email protected]> * SHA224 Support Copyright 2007 Intel Corporation <[email protected]> */ -#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt -#include <asm/elf.h> #include <asm/opcodes.h> #include <asm/pstate.h> -#include <crypto/internal/sha2.h> -#include <linux/kernel.h> -#include <linux/module.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_sha256_opcodes); asmlinkage void sha256_sparc64_transform(struct sha256_block_state *state,const u8 *data,size_t nblocks); -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {if (static_branch_likely (&have_sha256_opcodes))sha256_sparc64_transform (state, data, nblocks);else sha256_blocks_generic (state, data, nblocks); } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); -static int __init sha256_sparc64_mod_init (void ) +#define sha256_mod_init_arch sha256_mod_init_arch +static inline void sha256_mod_init_arch (void ) {unsigned long cfr;if (!(sparc64_elf_hwcap &HWCAP_SPARC_CRYPTO )) -return 0 ; +return ; __asm__ __volatile__("rd %%asr26, %0" :"=r" (cfr));if (!(cfr &CFR_SHA256 )) -return 0 ; +return ;static_branch_enable (&have_sha256_opcodes);pr_info ("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n" ); -return 0 ; } -subsys_initcall (sha256_sparc64_mod_init); - -static void __exit sha256_sparc64_mod_exit(void) -{ -} -module_exit (sha256_sparc64_mod_exit); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 accelerated using the sparc64 sha256 opcodes" );
lib/crypto/x86/Kconfig +0 -7
@@ -22,12 +22,5 @@ config CRYPTO_CHACHA20_X86_64 config CRYPTO_POLY1305_X86_64 tristate depends on 64BIT default CRYPTO_LIB_POLY1305 select CRYPTO_ARCH_HAVE_LIB_POLY1305 - -config CRYPTO_SHA256_X86_64 - tristate - depends on 64BIT - default CRYPTO_LIB_SHA256 - select CRYPTO_ARCH_HAVE_LIB_SHA256 - select CRYPTO_LIB_SHA256_GENERIC
lib/crypto/x86/Makefile +0 -3
@@ -8,13 +8,10 @@ chacha-x86_64-y := chacha-avx2-x86_64.o chacha-ssse3-x86_64.o chacha-avx512vl-x8 obj-$(CONFIG_CRYPTO_POLY1305_X86_64) += poly1305-x86_64.o poly1305-x86_64-y := poly1305-x86_64-cryptogams.o poly1305_glue.o targets += poly1305-x86_64-cryptogams.S -obj-$(CONFIG_CRYPTO_SHA256_X86_64) += sha256-x86_64.o -sha256-x86_64-y := sha256.o sha256-ssse3-asm.o sha256-avx-asm.o sha256-avx2-asm.o sha256-ni-asm.o - quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $< > $@ $(obj)/%.S: $(src)/%.pl FORCE $(call if_changed,perlasm)
lib/crypto/x86/sha256.h renamed from lib/crypto/x86/sha256.c +8 -21
@@ -1,16 +1,13 @@ -// SPDX-License-Identifier: GPL-2.0-or-later +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * SHA-256 optimized for x86_64 * * Copyright 2025 Google LLC */ #include <asm/fpu/api.h> -#include <crypto/internal/sha2.h> #include <crypto/internal/simd.h> -#include <linux/kernel.h> -#include <linux/module.h> #include <linux/static_call.h> asmlinkage void sha256_transform_ssse3(struct sha256_block_state *state,const u8 *data,size_t nblocks); asmlinkage void sha256_transform_avx(struct sha256_block_state *state,
@@ -22,47 +19,37 @@ asmlinkage void sha256_ni_transform(struct sha256_block_state *state,static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_sha256_x86);DEFINE_STATIC_CALL (sha256_blocks_x86, sha256_transform_ssse3); -void sha256_blocks_arch (struct sha256_block_state *state, -const u8 *data,size_t nblocks) +static void sha256_blocks (struct sha256_block_state *state, +const u8 *data,size_t nblocks) {if (static_branch_likely (&have_sha256_x86) &&crypto_simd_usable ()) {kernel_fpu_begin ();static_call (sha256_blocks_x86)(state, data, nblocks);kernel_fpu_end (); }else {sha256_blocks_generic (state, data, nblocks); } } -EXPORT_SYMBOL_GPL (sha256_blocks_arch); -static int __init sha256_x86_mod_init (void ) +#define sha256_mod_init_arch sha256_mod_init_arch +static inline void sha256_mod_init_arch (void ) {if (boot_cpu_has (X86_FEATURE_SHA_NI )) {static_call_update (sha256_blocks_x86, sha256_ni_transform); - }else if (cpu_has_xfeatures (XFEATURE_MASK_SSE | -XFEATURE_MASK_YMM ,NULL ) && + }else if (cpu_has_xfeatures (XFEATURE_MASK_SSE |XFEATURE_MASK_YMM , +NULL ) &&boot_cpu_has (X86_FEATURE_AVX )) {if (boot_cpu_has (X86_FEATURE_AVX2 ) &&boot_cpu_has (X86_FEATURE_BMI2 ))static_call_update (sha256_blocks_x86, sha256_transform_rorx);else static_call_update (sha256_blocks_x86, sha256_transform_avx); }else if (!boot_cpu_has (X86_FEATURE_SSSE3 )) { -return 0 ; +return ; }static_branch_enable (&have_sha256_x86); -return 0 ; } -subsys_initcall (sha256_x86_mod_init); - -static void __exit sha256_x86_mod_exit(void) -{ -} -module_exit (sha256_x86_mod_exit); - -MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("SHA-256 optimized for x86_64" );
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 13/14 lib/crypto: sha256: Sync sha256_update() with sha512_update()
In reply to Eric BiggersThe BLOCK_HASH_UPDATE_BLOCKS macro is difficult to read. For now, let's just write the update explicitly in the straightforward way, mirroring sha512_update(). It's possible that we'll bring back a macro for this later, but it needs to be properly justified and hopefully a bit more readable.
--- lib/crypto/sha256.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-)
lib/crypto/sha256.c +25 -3
@@ -8,11 +8,10 @@* Copyright (c) 2014 Red Hat Inc. * Copyright 2025 Google LLC */ #include <crypto/hmac.h> -#include <crypto/internal/blockhash.h> #include <crypto/sha2.h> #include <linux/export.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/string.h>
@@ -178,12 +177,35 @@ EXPORT_SYMBOL_GPL(sha256_init);void __sha256_update (struct __sha256_ctx *ctx,const u8 *data,size_t len) {size_t partial = ctx->bytecount %SHA256_BLOCK_SIZE ; ctx->bytecount += len; -BLOCK_HASH_UPDATE_BLOCKS (sha256_blocks, &ctx->state, data, len, -SHA256_BLOCK_SIZE , ctx->buf, partial); + +if (partial + len >=SHA256_BLOCK_SIZE ) { +size_t nblocks; + +if (partial) { +size_t l =SHA256_BLOCK_SIZE - partial; + +memcpy (&ctx->buf[partial], data, l); + data += l; + len -= l; + +sha256_blocks (&ctx->state, ctx->buf,1 ); + } + + nblocks = len /SHA256_BLOCK_SIZE ; + len %=SHA256_BLOCK_SIZE ; + +if (nblocks) { +sha256_blocks (&ctx->state, data, nblocks); + data += nblocks *SHA256_BLOCK_SIZE ; + } + partial =0 ; + } +if (len) +memcpy (&ctx->buf[partial], data, len); }EXPORT_SYMBOL (__sha256_update);static void __sha256_final (struct __sha256_ctx *ctx,u8 *out,size_t digest_size)
Signature · 1 line
2.50.0
Eric Biggers
maintainerPATCH v2 14/14 lib/crypto: sha256: Document the SHA-224 and SHA-256 API
In reply to Eric BiggersAdd kerneldoc comments, consistent with the kerneldoc comments of the SHA-384 and SHA-512 API.
--- include/crypto/sha2.h | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
include/crypto/sha2.h +76 -0
@@ -153,17 +153,55 @@ void __hmac_sha256_init(struct __hmac_sha256_ctx *ctx,*/ struct sha224_ctx {struct __sha256_ctx ctx; }; +/** +* sha224_init() - Initialize a SHA-224 context for a new message +* @ctx: the context to initialize +* +* If you don't need incremental computation, consider sha224() instead. +* +* Context: Any context. +*/ void sha224_init (struct sha224_ctx *ctx); + +/** +* sha224_update() - Update a SHA-224 context with message data +* @ctx: the context to update; must have been initialized +* @data: the message data +* @len: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ static inline void sha224_update (struct sha224_ctx *ctx,const u8 *data,size_t len) {__sha256_update (&ctx->ctx, data, len); } + +/** +* sha224_final() - Finish computing a SHA-224 message digest +* @ctx: the context to finalize; must have been initialized +* @out: (output) the resulting SHA-224 message digest +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ void sha224_final (struct sha224_ctx *ctx,u8 out[SHA224_DIGEST_SIZE ]); + +/** +* sha224() - Compute SHA-224 message digest in one shot +* @data: the message data +* @len: the data length in bytes +* @out: (output) the resulting SHA-224 message digest +* +* Context: Any context. +*/ void sha224 (const u8 *data,size_t len,u8 out[SHA224_DIGEST_SIZE ]);/** * struct hmac_sha224_key - Prepared key for HMAC-SHA224 * @key: private
@@ -273,17 +311,55 @@ void hmac_sha224_usingrawkey(const u8 *raw_key, size_t raw_key_len,*/ struct sha256_ctx {struct __sha256_ctx ctx; }; +/** +* sha256_init() - Initialize a SHA-256 context for a new message +* @ctx: the context to initialize +* +* If you don't need incremental computation, consider sha256() instead. +* +* Context: Any context. +*/ void sha256_init (struct sha256_ctx *ctx); + +/** +* sha256_update() - Update a SHA-256 context with message data +* @ctx: the context to update; must have been initialized +* @data: the message data +* @len: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ static inline void sha256_update (struct sha256_ctx *ctx,const u8 *data,size_t len) {__sha256_update (&ctx->ctx, data, len); } + +/** +* sha256_final() - Finish computing a SHA-256 message digest +* @ctx: the context to finalize; must have been initialized +* @out: (output) the resulting SHA-256 message digest +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ void sha256_final (struct sha256_ctx *ctx,u8 out[SHA256_DIGEST_SIZE ]); + +/** +* sha256() - Compute SHA-256 message digest in one shot +* @data: the message data +* @len: the data length in bytes +* @out: (output) the resulting SHA-256 message digest +* +* Context: Any context. +*/ void sha256 (const u8 *data,size_t len,u8 out[SHA256_DIGEST_SIZE ]);/** * struct hmac_sha256_key - Prepared key for HMAC-SHA256 * @key: private
Signature · 1 line
2.50.0
Eric Biggers
maintainerQuoted 30 lines · Eric Biggers
This series is also available at:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git sha256-lib-cleanup-v2
This series improves the SHA-224 and SHA-256 library code to be consistent with what I did for SHA-384 and SHA-512. This includes:
- Use stronger typing in the SHA-224 and SHA-256 functions.
- Add support for HMAC-SHA224 and HMAC-SHA256. (I'll send a separate patch with KUnit test cases for this.)
- Make the old-school crypto API's support for sha224 and sha256 just use the actual library API, instead of unsafe low-level functions.
- Consolidate the CPU-based SHA-224 and SHA-256 code into a single module, with better inlining and dead code elimination.
- Properly document the SHA-224 and SHA-256 functions.
- Other changes to synchronize the code with SHA-384 and SHA-512.
Changed in v2:
- Droppedsha224_kunit.cchanges; it will be added later in the history
- Dropped some patches that I folded into the SHA-512 series
- Removed redundant checks ofIS_ENABLED(CONFIG_KERNEL_MODE_NEON)
- Removed obsolete setting of -DARCH for sha256.o
- Fixed a commit title to mention sha256 instead of sha512
- Excluded HMAC-SHA{224,256} code from purgatory, where it isn't needed
Quoted 16 lines
Eric Biggers (14):
libceph: Renamehmac_sha256()toceph_hmac_sha256()cxl/test: Simplifyfw_buf_checksum_show()
lib/crypto: sha256: Reorder some code
lib/crypto: sha256: Removesha256_blocks_simd()lib/crypto: sha256: Addsha224()andsha224_update()lib/crypto: sha256: Make library API use strongly-typed contexts lib/crypto: sha256: Propagatesha256_block_statetype to
implementations
lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support
crypto: sha256 - Wrap library and add HMAC support
crypto: sha256 - Use same state format as legacy drivers lib/crypto: sha256: Removesha256_is_arch_optimized()lib/crypto: sha256: Consolidate into single module lib/crypto: sha256: Syncsha256_update()withsha512_update()lib/crypto: sha256: Document the SHA-224 and SHA-256 API
FYI, applied to libcrypto-next. Reviews and acks would be greatly appreciated, though!
To fix https://lore.kernel.org/r/[email protected]/
I applied the following fixup to
"lib/crypto: sha256: Propagate sha256_block_state type to implementations".
lib/crypto/powerpc/sha256.c +2 -1
@@ -26,7 +26,8 @@*/ #define MAX_BYTES 1024 -extern void ppc_spe_sha256_transform (u32 *state,const u8 *src,u32 blocks); +extern void ppc_spe_sha256_transform (struct sha256_block_state *state, +const u8 *src,u32 blocks);static void spe_begin (void ) {
Quoted 31 lines · Eric Biggers
This series is also available at:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git sha256-lib-cleanup-v2
This series improves the SHA-224 and SHA-256 library code to be consistent with what I did for SHA-384 and SHA-512. This includes:
- Use stronger typing in the SHA-224 and SHA-256 functions.
- Add support for HMAC-SHA224 and HMAC-SHA256. (I'll send a separate patch with KUnit test cases for this.)
- Make the old-school crypto API's support for sha224 and sha256 just use the actual library API, instead of unsafe low-level functions.
- Consolidate the CPU-based SHA-224 and SHA-256 code into a single module, with better inlining and dead code elimination.
- Properly document the SHA-224 and SHA-256 functions.
- Other changes to synchronize the code with SHA-384 and SHA-512.
Changed in v2:
- Droppedsha224_kunit.cchanges; it will be added later in the history
- Dropped some patches that I folded into the SHA-512 series
- Removed redundant checks ofIS_ENABLED(CONFIG_KERNEL_MODE_NEON)
- Removed obsolete setting of -DARCH for sha256.o
- Fixed a commit title to mention sha256 instead of sha512
- Excluded HMAC-SHA{224,256} code from purgatory, where it isn't needed
Quoted 17 lines
Eric Biggers (14):
libceph: Renamehmac_sha256()toceph_hmac_sha256()cxl/test: Simplifyfw_buf_checksum_show()
lib/crypto: sha256: Reorder some code
lib/crypto: sha256: Removesha256_blocks_simd()lib/crypto: sha256: Addsha224()andsha224_update()lib/crypto: sha256: Make library API use strongly-typed contexts lib/crypto: sha256: Propagatesha256_block_statetype to
implementations
lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support
crypto: sha256 - Wrap library and add HMAC support
crypto: sha256 - Use same state format as legacy drivers lib/crypto: sha256: Removesha256_is_arch_optimized()lib/crypto: sha256: Consolidate into single module lib/crypto: sha256: Syncsha256_update()withsha512_update()lib/crypto: sha256: Document the SHA-224 and SHA-256 API
Quoted 80 lines
arch/mips/cavium-octeon/Kconfig | 6 - arch/mips/cavium-octeon/crypto/Makefile | 1 - arch/riscv/purgatory/purgatory.c | 8 +- arch/s390/purgatory/purgatory.c | 2 +- arch/x86/purgatory/purgatory.c | 2 +- crypto/Kconfig | 4 +- crypto/Makefile | 1 - crypto/sha256.c | 371 +++++++++------- crypto/testmgr.c | 12 + drivers/char/tpm/tpm2-sessions.c | 12 +- drivers/crypto/img-hash.c | 4 +- drivers/crypto/starfive/jh7110-hash.c | 8 +- include/crypto/internal/sha2.h | 66 --- include/crypto/sha2.h | 390 +++++++++++++++-- kernel/kexec_file.c | 10 +- lib/crypto/Kconfig | 34 +- lib/crypto/Makefile | 39 +- lib/crypto/arm/Kconfig | 7 - lib/crypto/arm/Makefile | 8 +- lib/crypto/arm/sha256-armv4.pl | 20 +- lib/crypto/arm/sha256-ce.S | 2 +- lib/crypto/arm/sha256.c | 64 --- lib/crypto/arm/sha256.h | 46 ++ lib/crypto/arm64/Kconfig | 6 - lib/crypto/arm64/Makefile | 9 +- lib/crypto/arm64/sha2-armv8.pl | 2 +- lib/crypto/arm64/sha256-ce.S | 2 +- lib/crypto/arm64/sha256.c | 75 ---- lib/crypto/arm64/sha256.h | 57 +++ lib/crypto/arm64/sha512.h | 6 +- .../crypto/mips/sha256.h | 20 +- lib/crypto/powerpc/Kconfig | 6 - lib/crypto/powerpc/Makefile | 3 - lib/crypto/powerpc/{sha256.c => sha256.h} | 19 +- lib/crypto/riscv/Kconfig | 8 - lib/crypto/riscv/Makefile | 3 - .../sha256-riscv64-zvknha_or_zvknhb-zvkb.S | 2 +- lib/crypto/riscv/sha256.c | 67 --- lib/crypto/riscv/sha256.h | 42 ++ lib/crypto/s390/Kconfig | 6 - lib/crypto/s390/Makefile | 3 - lib/crypto/s390/sha256.c | 47 -- lib/crypto/s390/sha256.h | 28 ++ lib/crypto/sha256-generic.c | 138 ------ lib/crypto/sha256.c | 413 ++++++++++++++++-- lib/crypto/sparc/Kconfig | 8 - lib/crypto/sparc/Makefile | 4 - lib/crypto/sparc/{sha256.c => sha256.h} | 37 +- lib/crypto/x86/Kconfig | 8 - lib/crypto/x86/Makefile | 3 - lib/crypto/x86/sha256-avx-asm.S | 2 +- lib/crypto/x86/sha256-avx2-asm.S | 2 +- lib/crypto/x86/sha256-ni-asm.S | 2 +- lib/crypto/x86/sha256-ssse3-asm.S | 2 +- lib/crypto/x86/sha256.c | 80 ---- lib/crypto/x86/sha256.h | 55 +++ net/ceph/messenger_v2.c | 12 +- tools/testing/cxl/test/mem.c | 21 +- 58 files changed, 1307 insertions(+), 1008 deletions(-) delete mode 100644 include/crypto/internal/sha2.h delete mode 100644 lib/crypto/arm/sha256.c create mode 100644 lib/crypto/arm/sha256.h delete mode 100644 lib/crypto/arm64/sha256.c create mode 100644 lib/crypto/arm64/sha256.h rename arch/mips/cavium-octeon/crypto/octeon-sha256.c => lib/crypto/mips/sha256.h (76%) rename lib/crypto/powerpc/{sha256.c => sha256.h} (76%) delete mode 100644 lib/crypto/riscv/sha256.c create mode 100644 lib/crypto/riscv/sha256.h delete mode 100644 lib/crypto/s390/sha256.c create mode 100644 lib/crypto/s390/sha256.h delete mode 100644 lib/crypto/sha256-generic.c delete mode 100644 lib/crypto/sparc/Kconfig delete mode 100644 lib/crypto/sparc/Makefile rename lib/crypto/sparc/{sha256.c => sha256.h} (53%) delete mode 100644 lib/crypto/x86/sha256.c create mode 100644 lib/crypto/x86/sha256.h--
2.50.0