Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: s390/sha256 - implement library instead of shash

Instead of providing crypto_shash algorithms for the arch-optimized
SHA-256 code, instead implement the SHA-256 library. This is much
simpler, it makes the SHA-256 library functions be arch-optimized, and
it fixes the longstanding issue where the arch-optimized SHA-256 was
disabled by default. SHA-256 still remains available through
crypto_shash, but individual architectures no longer need to handle it.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
b9eac03e bf52d938

+55 -157
-1
arch/s390/configs/debug_defconfig
··· 795 795 CONFIG_CRYPTO_USER_API_AEAD=m 796 796 CONFIG_CRYPTO_SHA512_S390=m 797 797 CONFIG_CRYPTO_SHA1_S390=m 798 - CONFIG_CRYPTO_SHA256_S390=m 799 798 CONFIG_CRYPTO_SHA3_256_S390=m 800 799 CONFIG_CRYPTO_SHA3_512_S390=m 801 800 CONFIG_CRYPTO_GHASH_S390=m
-1
arch/s390/configs/defconfig
··· 782 782 CONFIG_CRYPTO_USER_API_AEAD=m 783 783 CONFIG_CRYPTO_SHA512_S390=m 784 784 CONFIG_CRYPTO_SHA1_S390=m 785 - CONFIG_CRYPTO_SHA256_S390=m 786 785 CONFIG_CRYPTO_SHA3_256_S390=m 787 786 CONFIG_CRYPTO_SHA3_512_S390=m 788 787 CONFIG_CRYPTO_GHASH_S390=m
-10
arch/s390/crypto/Kconfig
··· 22 22 23 23 It is available as of z990. 24 24 25 - config CRYPTO_SHA256_S390 26 - tristate "Hash functions: SHA-224 and SHA-256" 27 - select CRYPTO_HASH 28 - help 29 - SHA-224 and SHA-256 secure hash algorithms (FIPS 180) 30 - 31 - Architecture: s390 32 - 33 - It is available as of z9. 34 - 35 25 config CRYPTO_SHA3_256_S390 36 26 tristate "Hash functions: SHA3-224 and SHA3-256" 37 27 select CRYPTO_HASH
-1
arch/s390/crypto/Makefile
··· 4 4 # 5 5 6 6 obj-$(CONFIG_CRYPTO_SHA1_S390) += sha1_s390.o sha_common.o 7 - obj-$(CONFIG_CRYPTO_SHA256_S390) += sha256_s390.o sha_common.o 8 7 obj-$(CONFIG_CRYPTO_SHA512_S390) += sha512_s390.o sha_common.o 9 8 obj-$(CONFIG_CRYPTO_SHA3_256_S390) += sha3_256_s390.o sha_common.o 10 9 obj-$(CONFIG_CRYPTO_SHA3_512_S390) += sha3_512_s390.o sha_common.o
-144
arch/s390/crypto/sha256_s390.c
··· 1 - // SPDX-License-Identifier: GPL-2.0+ 2 - /* 3 - * Cryptographic API. 4 - * 5 - * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm. 6 - * 7 - * s390 Version: 8 - * Copyright IBM Corp. 2005, 2011 9 - * Author(s): Jan Glauber (jang@de.ibm.com) 10 - */ 11 - #include <asm/cpacf.h> 12 - #include <crypto/internal/hash.h> 13 - #include <crypto/sha2.h> 14 - #include <linux/cpufeature.h> 15 - #include <linux/kernel.h> 16 - #include <linux/module.h> 17 - #include <linux/string.h> 18 - 19 - #include "sha.h" 20 - 21 - static int s390_sha256_init(struct shash_desc *desc) 22 - { 23 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 24 - 25 - sctx->state[0] = SHA256_H0; 26 - sctx->state[1] = SHA256_H1; 27 - sctx->state[2] = SHA256_H2; 28 - sctx->state[3] = SHA256_H3; 29 - sctx->state[4] = SHA256_H4; 30 - sctx->state[5] = SHA256_H5; 31 - sctx->state[6] = SHA256_H6; 32 - sctx->state[7] = SHA256_H7; 33 - sctx->count = 0; 34 - sctx->func = CPACF_KIMD_SHA_256; 35 - 36 - return 0; 37 - } 38 - 39 - static int sha256_export(struct shash_desc *desc, void *out) 40 - { 41 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 42 - struct crypto_sha256_state *octx = out; 43 - 44 - octx->count = sctx->count; 45 - memcpy(octx->state, sctx->state, sizeof(octx->state)); 46 - return 0; 47 - } 48 - 49 - static int sha256_import(struct shash_desc *desc, const void *in) 50 - { 51 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 52 - const struct crypto_sha256_state *ictx = in; 53 - 54 - sctx->count = ictx->count; 55 - memcpy(sctx->state, ictx->state, sizeof(ictx->state)); 56 - sctx->func = CPACF_KIMD_SHA_256; 57 - return 0; 58 - } 59 - 60 - static struct shash_alg sha256_alg = { 61 - .digestsize = SHA256_DIGEST_SIZE, 62 - .init = s390_sha256_init, 63 - .update = s390_sha_update_blocks, 64 - .finup = s390_sha_finup, 65 - .export = sha256_export, 66 - .import = sha256_import, 67 - .descsize = S390_SHA_CTX_SIZE, 68 - .statesize = sizeof(struct crypto_sha256_state), 69 - .base = { 70 - .cra_name = "sha256", 71 - .cra_driver_name= "sha256-s390", 72 - .cra_priority = 300, 73 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 74 - .cra_blocksize = SHA256_BLOCK_SIZE, 75 - .cra_module = THIS_MODULE, 76 - } 77 - }; 78 - 79 - static int s390_sha224_init(struct shash_desc *desc) 80 - { 81 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 82 - 83 - sctx->state[0] = SHA224_H0; 84 - sctx->state[1] = SHA224_H1; 85 - sctx->state[2] = SHA224_H2; 86 - sctx->state[3] = SHA224_H3; 87 - sctx->state[4] = SHA224_H4; 88 - sctx->state[5] = SHA224_H5; 89 - sctx->state[6] = SHA224_H6; 90 - sctx->state[7] = SHA224_H7; 91 - sctx->count = 0; 92 - sctx->func = CPACF_KIMD_SHA_256; 93 - 94 - return 0; 95 - } 96 - 97 - static struct shash_alg sha224_alg = { 98 - .digestsize = SHA224_DIGEST_SIZE, 99 - .init = s390_sha224_init, 100 - .update = s390_sha_update_blocks, 101 - .finup = s390_sha_finup, 102 - .export = sha256_export, 103 - .import = sha256_import, 104 - .descsize = S390_SHA_CTX_SIZE, 105 - .statesize = sizeof(struct crypto_sha256_state), 106 - .base = { 107 - .cra_name = "sha224", 108 - .cra_driver_name= "sha224-s390", 109 - .cra_priority = 300, 110 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 111 - .cra_blocksize = SHA224_BLOCK_SIZE, 112 - .cra_module = THIS_MODULE, 113 - } 114 - }; 115 - 116 - static int __init sha256_s390_init(void) 117 - { 118 - int ret; 119 - 120 - if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256)) 121 - return -ENODEV; 122 - ret = crypto_register_shash(&sha256_alg); 123 - if (ret < 0) 124 - goto out; 125 - ret = crypto_register_shash(&sha224_alg); 126 - if (ret < 0) 127 - crypto_unregister_shash(&sha256_alg); 128 - out: 129 - return ret; 130 - } 131 - 132 - static void __exit sha256_s390_fini(void) 133 - { 134 - crypto_unregister_shash(&sha224_alg); 135 - crypto_unregister_shash(&sha256_alg); 136 - } 137 - 138 - module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha256_s390_init); 139 - module_exit(sha256_s390_fini); 140 - 141 - MODULE_ALIAS_CRYPTO("sha256"); 142 - MODULE_ALIAS_CRYPTO("sha224"); 143 - MODULE_LICENSE("GPL"); 144 - MODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");
+6
arch/s390/lib/crypto/Kconfig
··· 5 5 default CRYPTO_LIB_CHACHA 6 6 select CRYPTO_LIB_CHACHA_GENERIC 7 7 select CRYPTO_ARCH_HAVE_LIB_CHACHA 8 + 9 + config CRYPTO_SHA256_S390 10 + tristate 11 + default CRYPTO_LIB_SHA256 12 + select CRYPTO_ARCH_HAVE_LIB_SHA256 13 + select CRYPTO_LIB_SHA256_GENERIC
+2
arch/s390/lib/crypto/Makefile
··· 2 2 3 3 obj-$(CONFIG_CRYPTO_CHACHA_S390) += chacha_s390.o 4 4 chacha_s390-y := chacha-glue.o chacha-s390.o 5 + 6 + obj-$(CONFIG_CRYPTO_SHA256_S390) += sha256.o
+47
arch/s390/lib/crypto/sha256.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * SHA-256 optimized using the CP Assist for Cryptographic Functions (CPACF) 4 + * 5 + * Copyright 2025 Google LLC 6 + */ 7 + #include <asm/cpacf.h> 8 + #include <crypto/internal/sha2.h> 9 + #include <linux/cpufeature.h> 10 + #include <linux/kernel.h> 11 + #include <linux/module.h> 12 + 13 + static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_cpacf_sha256); 14 + 15 + void sha256_blocks_arch(u32 state[SHA256_STATE_WORDS], 16 + const u8 *data, size_t nblocks) 17 + { 18 + if (static_branch_likely(&have_cpacf_sha256)) 19 + cpacf_kimd(CPACF_KIMD_SHA_256, state, data, 20 + nblocks * SHA256_BLOCK_SIZE); 21 + else 22 + sha256_blocks_generic(state, data, nblocks); 23 + } 24 + EXPORT_SYMBOL(sha256_blocks_arch); 25 + 26 + bool sha256_is_arch_optimized(void) 27 + { 28 + return static_key_enabled(&have_cpacf_sha256); 29 + } 30 + EXPORT_SYMBOL(sha256_is_arch_optimized); 31 + 32 + static int __init sha256_s390_mod_init(void) 33 + { 34 + if (cpu_have_feature(S390_CPU_FEATURE_MSA) && 35 + cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256)) 36 + static_branch_enable(&have_cpacf_sha256); 37 + return 0; 38 + } 39 + arch_initcall(sha256_s390_mod_init); 40 + 41 + static void __exit sha256_s390_mod_exit(void) 42 + { 43 + } 44 + module_exit(sha256_s390_mod_exit); 45 + 46 + MODULE_LICENSE("GPL"); 47 + MODULE_DESCRIPTION("SHA-256 using the CP Assist for Cryptographic Functions (CPACF)");