]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
lib/crypto: s390/sha1: Migrate optimized code into library
authorEric Biggers <ebiggers@kernel.org>
Sat, 12 Jul 2025 23:23:02 +0000 (16:23 -0700)
committerEric Biggers <ebiggers@kernel.org>
Mon, 14 Jul 2025 18:11:49 +0000 (11:11 -0700)
Instead of exposing the s390-optimized SHA-1 code via s390-specific
crypto_shash algorithms, instead just implement the sha1_blocks()
library function.  This is much simpler, it makes the SHA-1 library
functions be s390-optimized, and it fixes the longstanding issue where
the s390-optimized SHA-1 code was disabled by default.  SHA-1 still
remains available through crypto_shash, but individual architectures no
longer need to handle it.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250712232329.818226-12-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
arch/s390/configs/debug_defconfig
arch/s390/configs/defconfig
arch/s390/crypto/Kconfig
arch/s390/crypto/Makefile
arch/s390/crypto/sha1_s390.c [deleted file]
lib/crypto/Kconfig
lib/crypto/s390/sha1.h [new file with mode: 0644]

index ef313c30b375c2317a42975c64f110ef14390526..a7db7ed2872051fba210f894de809d4fc9bf4104 100644 (file)
@@ -804,7 +804,6 @@ CONFIG_CRYPTO_USER_API_HASH=m
 CONFIG_CRYPTO_USER_API_SKCIPHER=m
 CONFIG_CRYPTO_USER_API_RNG=m
 CONFIG_CRYPTO_USER_API_AEAD=m
-CONFIG_CRYPTO_SHA1_S390=m
 CONFIG_CRYPTO_SHA3_256_S390=m
 CONFIG_CRYPTO_SHA3_512_S390=m
 CONFIG_CRYPTO_GHASH_S390=m
index b6fa341bb03b60de2a844af673ad5aca7c7d2ab5..0217ed5616bf458a3efd88acd37f9bc6986e0d9a 100644 (file)
@@ -791,7 +791,6 @@ CONFIG_CRYPTO_USER_API_HASH=m
 CONFIG_CRYPTO_USER_API_SKCIPHER=m
 CONFIG_CRYPTO_USER_API_RNG=m
 CONFIG_CRYPTO_USER_API_AEAD=m
-CONFIG_CRYPTO_SHA1_S390=m
 CONFIG_CRYPTO_SHA3_256_S390=m
 CONFIG_CRYPTO_SHA3_512_S390=m
 CONFIG_CRYPTO_GHASH_S390=m
index 4557514fbac35481521a851da73688b606c427d1..03f73fbd38b62691270e8911b52dcc5668359206 100644 (file)
@@ -2,16 +2,6 @@
 
 menu "Accelerated Cryptographic Algorithms for CPU (s390)"
 
-config CRYPTO_SHA1_S390
-       tristate "Hash functions: SHA-1"
-       select CRYPTO_HASH
-       help
-         SHA-1 secure hash algorithm (FIPS 180)
-
-         Architecture: s390
-
-         It is available as of z990.
-
 config CRYPTO_SHA3_256_S390
        tristate "Hash functions: SHA3-224 and SHA3-256"
        select CRYPTO_HASH
index 473d64c0982af084730bddbf7816aa04f3230052..1e5a1038d49146cd88fc85e4d5c86449cc176998 100644 (file)
@@ -3,7 +3,6 @@
 # Cryptographic API
 #
 
-obj-$(CONFIG_CRYPTO_SHA1_S390) += sha1_s390.o sha_common.o
 obj-$(CONFIG_CRYPTO_SHA3_256_S390) += sha3_256_s390.o sha_common.o
 obj-$(CONFIG_CRYPTO_SHA3_512_S390) += sha3_512_s390.o sha_common.o
 obj-$(CONFIG_CRYPTO_DES_S390) += des_s390.o
diff --git a/arch/s390/crypto/sha1_s390.c b/arch/s390/crypto/sha1_s390.c
deleted file mode 100644 (file)
index d229cbd..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Cryptographic API.
- *
- * s390 implementation of the SHA1 Secure Hash Algorithm.
- *
- * Derived from cryptoapi implementation, adapted for in-place
- * scatterlist interface.  Originally based on the public domain
- * implementation written by Steve Reid.
- *
- * s390 Version:
- *   Copyright IBM Corp. 2003, 2007
- *   Author(s): Thomas Spatzier
- *             Jan Glauber (jan.glauber@de.ibm.com)
- *
- * Derived from "crypto/sha1_generic.c"
- *   Copyright (c) Alan Smithee.
- *   Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
- *   Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
- */
-#include <asm/cpacf.h>
-#include <crypto/internal/hash.h>
-#include <crypto/sha1.h>
-#include <linux/cpufeature.h>
-#include <linux/kernel.h>
-#include <linux/module.h>
-
-#include "sha.h"
-
-static int s390_sha1_init(struct shash_desc *desc)
-{
-       struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
-
-       sctx->state[0] = SHA1_H0;
-       sctx->state[1] = SHA1_H1;
-       sctx->state[2] = SHA1_H2;
-       sctx->state[3] = SHA1_H3;
-       sctx->state[4] = SHA1_H4;
-       sctx->count = 0;
-       sctx->func = CPACF_KIMD_SHA_1;
-
-       return 0;
-}
-
-static int s390_sha1_export(struct shash_desc *desc, void *out)
-{
-       struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
-       struct sha1_state *octx = out;
-
-       octx->count = sctx->count;
-       memcpy(octx->state, sctx->state, sizeof(octx->state));
-       return 0;
-}
-
-static int s390_sha1_import(struct shash_desc *desc, const void *in)
-{
-       struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
-       const struct sha1_state *ictx = in;
-
-       sctx->count = ictx->count;
-       memcpy(sctx->state, ictx->state, sizeof(ictx->state));
-       sctx->func = CPACF_KIMD_SHA_1;
-       return 0;
-}
-
-static struct shash_alg alg = {
-       .digestsize     =       SHA1_DIGEST_SIZE,
-       .init           =       s390_sha1_init,
-       .update         =       s390_sha_update_blocks,
-       .finup          =       s390_sha_finup,
-       .export         =       s390_sha1_export,
-       .import         =       s390_sha1_import,
-       .descsize       =       S390_SHA_CTX_SIZE,
-       .statesize      =       SHA1_STATE_SIZE,
-       .base           =       {
-               .cra_name       =       "sha1",
-               .cra_driver_name=       "sha1-s390",
-               .cra_priority   =       300,
-               .cra_flags      =       CRYPTO_AHASH_ALG_BLOCK_ONLY |
-                                       CRYPTO_AHASH_ALG_FINUP_MAX,
-               .cra_blocksize  =       SHA1_BLOCK_SIZE,
-               .cra_module     =       THIS_MODULE,
-       }
-};
-
-static int __init sha1_s390_init(void)
-{
-       if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1))
-               return -ENODEV;
-       return crypto_register_shash(&alg);
-}
-
-static void __exit sha1_s390_fini(void)
-{
-       crypto_unregister_shash(&alg);
-}
-
-module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha1_s390_init);
-module_exit(sha1_s390_fini);
-
-MODULE_ALIAS_CRYPTO("sha1");
-MODULE_LICENSE("GPL");
-MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
index 22ffbdab82d6ecd0be66434be88dbe6a2f9d933b..7da0432d8164387eeb69301a9a67ad6fe4f4710d 100644 (file)
@@ -150,6 +150,7 @@ config CRYPTO_LIB_SHA1_ARCH
        default y if ARM64 && KERNEL_MODE_NEON
        default y if MIPS && CPU_CAVIUM_OCTEON
        default y if PPC
+       default y if S390
 
 config CRYPTO_LIB_SHA256
        tristate
diff --git a/lib/crypto/s390/sha1.h b/lib/crypto/s390/sha1.h
new file mode 100644 (file)
index 0000000..08bd138
--- /dev/null
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * SHA-1 optimized using the CP Assist for Cryptographic Functions (CPACF)
+ *
+ * Copyright 2025 Google LLC
+ */
+#include <asm/cpacf.h>
+#include <linux/cpufeature.h>
+
+static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_cpacf_sha1);
+
+static void sha1_blocks(struct sha1_block_state *state,
+                       const u8 *data, size_t nblocks)
+{
+       if (static_branch_likely(&have_cpacf_sha1))
+               cpacf_kimd(CPACF_KIMD_SHA_1, state, data,
+                          nblocks * SHA1_BLOCK_SIZE);
+       else
+               sha1_blocks_generic(state, data, nblocks);
+}
+
+#define sha1_mod_init_arch sha1_mod_init_arch
+static inline void sha1_mod_init_arch(void)
+{
+       if (cpu_have_feature(S390_CPU_FEATURE_MSA) &&
+           cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1))
+               static_branch_enable(&have_cpacf_sha1);
+}