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

crypto: s390/sha3 - Remove superseded SHA-3 code

The SHA-3 library now utilizes the same s390 SHA-3 acceleration
capabilities as the arch/s390/crypto/ SHA-3 crypto_shash algorithms.
Moreover, crypto/sha3.c now uses the SHA-3 library. The result is that
all SHA-3 APIs are now s390-accelerated without any need for the old
SHA-3 code in arch/s390/crypto/. Remove this superseded code.

Also update the s390 defconfig and debug_defconfig files to enable
CONFIG_CRYPTO_SHA3 instead of CONFIG_CRYPTO_SHA3_256_S390 and
CONFIG_CRYPTO_SHA3_512_S390. This makes it so that the s390-optimized
SHA-3 continues to be built when either of these defconfigs is used.

Tested-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20251026055032.1413733-16-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+2 -508
+1 -2
arch/s390/configs/debug_defconfig
··· 792 792 CONFIG_CRYPTO_MD5=y 793 793 CONFIG_CRYPTO_MICHAEL_MIC=m 794 794 CONFIG_CRYPTO_RMD160=m 795 + CONFIG_CRYPTO_SHA3=m 795 796 CONFIG_CRYPTO_SM3_GENERIC=m 796 797 CONFIG_CRYPTO_WP512=m 797 798 CONFIG_CRYPTO_XCBC=m ··· 806 805 CONFIG_CRYPTO_USER_API_SKCIPHER=m 807 806 CONFIG_CRYPTO_USER_API_RNG=m 808 807 CONFIG_CRYPTO_USER_API_AEAD=m 809 - CONFIG_CRYPTO_SHA3_256_S390=m 810 - CONFIG_CRYPTO_SHA3_512_S390=m 811 808 CONFIG_CRYPTO_GHASH_S390=m 812 809 CONFIG_CRYPTO_AES_S390=m 813 810 CONFIG_CRYPTO_DES_S390=m
+1 -2
arch/s390/configs/defconfig
··· 776 776 CONFIG_CRYPTO_MD5=y 777 777 CONFIG_CRYPTO_MICHAEL_MIC=m 778 778 CONFIG_CRYPTO_RMD160=m 779 + CONFIG_CRYPTO_SHA3=m 779 780 CONFIG_CRYPTO_SM3_GENERIC=m 780 781 CONFIG_CRYPTO_WP512=m 781 782 CONFIG_CRYPTO_XCBC=m ··· 791 790 CONFIG_CRYPTO_USER_API_SKCIPHER=m 792 791 CONFIG_CRYPTO_USER_API_RNG=m 793 792 CONFIG_CRYPTO_USER_API_AEAD=m 794 - CONFIG_CRYPTO_SHA3_256_S390=m 795 - CONFIG_CRYPTO_SHA3_512_S390=m 796 793 CONFIG_CRYPTO_GHASH_S390=m 797 794 CONFIG_CRYPTO_AES_S390=m 798 795 CONFIG_CRYPTO_DES_S390=m
-20
arch/s390/crypto/Kconfig
··· 2 2 3 3 menu "Accelerated Cryptographic Algorithms for CPU (s390)" 4 4 5 - config CRYPTO_SHA3_256_S390 6 - tristate "Hash functions: SHA3-224 and SHA3-256" 7 - select CRYPTO_HASH 8 - help 9 - SHA3-224 and SHA3-256 secure hash algorithms (FIPS 202) 10 - 11 - Architecture: s390 12 - 13 - It is available as of z14. 14 - 15 - config CRYPTO_SHA3_512_S390 16 - tristate "Hash functions: SHA3-384 and SHA3-512" 17 - select CRYPTO_HASH 18 - help 19 - SHA3-384 and SHA3-512 secure hash algorithms (FIPS 202) 20 - 21 - Architecture: s390 22 - 23 - It is available as of z14. 24 - 25 5 config CRYPTO_GHASH_S390 26 6 tristate "Hash functions: GHASH" 27 7 select CRYPTO_HASH
-2
arch/s390/crypto/Makefile
··· 3 3 # Cryptographic API 4 4 # 5 5 6 - obj-$(CONFIG_CRYPTO_SHA3_256_S390) += sha3_256_s390.o sha_common.o 7 - obj-$(CONFIG_CRYPTO_SHA3_512_S390) += sha3_512_s390.o sha_common.o 8 6 obj-$(CONFIG_CRYPTO_DES_S390) += des_s390.o 9 7 obj-$(CONFIG_CRYPTO_AES_S390) += aes_s390.o 10 8 obj-$(CONFIG_CRYPTO_PAES_S390) += paes_s390.o
-51
arch/s390/crypto/sha.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0+ */ 2 - /* 3 - * Cryptographic API. 4 - * 5 - * s390 generic implementation of the SHA Secure Hash Algorithms. 6 - * 7 - * Copyright IBM Corp. 2007 8 - * Author(s): Jan Glauber (jang@de.ibm.com) 9 - */ 10 - #ifndef _CRYPTO_ARCH_S390_SHA_H 11 - #define _CRYPTO_ARCH_S390_SHA_H 12 - 13 - #include <crypto/hash.h> 14 - #include <crypto/sha2.h> 15 - #include <crypto/sha3.h> 16 - #include <linux/build_bug.h> 17 - #include <linux/types.h> 18 - 19 - /* must be big enough for the largest SHA variant */ 20 - #define CPACF_MAX_PARMBLOCK_SIZE SHA3_STATE_SIZE 21 - #define SHA_MAX_BLOCK_SIZE SHA3_224_BLOCK_SIZE 22 - 23 - struct s390_sha_ctx { 24 - u64 count; /* message length in bytes */ 25 - union { 26 - u32 state[CPACF_MAX_PARMBLOCK_SIZE / sizeof(u32)]; 27 - struct { 28 - u64 state[SHA512_DIGEST_SIZE / sizeof(u64)]; 29 - u64 count_hi; 30 - } sha512; 31 - struct { 32 - __le64 state[SHA3_STATE_SIZE / sizeof(u64)]; 33 - } sha3; 34 - }; 35 - int func; /* KIMD function to use */ 36 - bool first_message_part; 37 - }; 38 - 39 - struct shash_desc; 40 - 41 - int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data, 42 - unsigned int len); 43 - int s390_sha_finup(struct shash_desc *desc, const u8 *src, unsigned int len, 44 - u8 *out); 45 - 46 - static inline void __check_s390_sha_ctx_size(void) 47 - { 48 - BUILD_BUG_ON(S390_SHA_CTX_SIZE != sizeof(struct s390_sha_ctx)); 49 - } 50 - 51 - #endif
-157
arch/s390/crypto/sha3_256_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. 2019 9 - * Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com) 10 - */ 11 - #include <asm/cpacf.h> 12 - #include <crypto/internal/hash.h> 13 - #include <crypto/sha3.h> 14 - #include <linux/cpufeature.h> 15 - #include <linux/errno.h> 16 - #include <linux/kernel.h> 17 - #include <linux/module.h> 18 - #include <linux/string.h> 19 - 20 - #include "sha.h" 21 - 22 - static int s390_sha3_256_init(struct shash_desc *desc) 23 - { 24 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 25 - 26 - sctx->first_message_part = test_facility(86); 27 - if (!sctx->first_message_part) 28 - memset(sctx->state, 0, sizeof(sctx->state)); 29 - sctx->count = 0; 30 - sctx->func = CPACF_KIMD_SHA3_256; 31 - 32 - return 0; 33 - } 34 - 35 - static int sha3_256_export(struct shash_desc *desc, void *out) 36 - { 37 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 38 - union { 39 - u8 *u8; 40 - u64 *u64; 41 - } p = { .u8 = out }; 42 - int i; 43 - 44 - if (sctx->first_message_part) { 45 - memset(out, 0, SHA3_STATE_SIZE); 46 - return 0; 47 - } 48 - for (i = 0; i < SHA3_STATE_SIZE / 8; i++) 49 - put_unaligned(le64_to_cpu(sctx->sha3.state[i]), p.u64++); 50 - return 0; 51 - } 52 - 53 - static int sha3_256_import(struct shash_desc *desc, const void *in) 54 - { 55 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 56 - union { 57 - const u8 *u8; 58 - const u64 *u64; 59 - } p = { .u8 = in }; 60 - int i; 61 - 62 - for (i = 0; i < SHA3_STATE_SIZE / 8; i++) 63 - sctx->sha3.state[i] = cpu_to_le64(get_unaligned(p.u64++)); 64 - sctx->count = 0; 65 - sctx->first_message_part = 0; 66 - sctx->func = CPACF_KIMD_SHA3_256; 67 - 68 - return 0; 69 - } 70 - 71 - static int sha3_224_import(struct shash_desc *desc, const void *in) 72 - { 73 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 74 - 75 - sha3_256_import(desc, in); 76 - sctx->func = CPACF_KIMD_SHA3_224; 77 - return 0; 78 - } 79 - 80 - static struct shash_alg sha3_256_alg = { 81 - .digestsize = SHA3_256_DIGEST_SIZE, /* = 32 */ 82 - .init = s390_sha3_256_init, 83 - .update = s390_sha_update_blocks, 84 - .finup = s390_sha_finup, 85 - .export = sha3_256_export, 86 - .import = sha3_256_import, 87 - .descsize = S390_SHA_CTX_SIZE, 88 - .statesize = SHA3_STATE_SIZE, 89 - .base = { 90 - .cra_name = "sha3-256", 91 - .cra_driver_name = "sha3-256-s390", 92 - .cra_priority = 300, 93 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 94 - .cra_blocksize = SHA3_256_BLOCK_SIZE, 95 - .cra_module = THIS_MODULE, 96 - } 97 - }; 98 - 99 - static int s390_sha3_224_init(struct shash_desc *desc) 100 - { 101 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 102 - 103 - s390_sha3_256_init(desc); 104 - sctx->func = CPACF_KIMD_SHA3_224; 105 - return 0; 106 - } 107 - 108 - static struct shash_alg sha3_224_alg = { 109 - .digestsize = SHA3_224_DIGEST_SIZE, 110 - .init = s390_sha3_224_init, 111 - .update = s390_sha_update_blocks, 112 - .finup = s390_sha_finup, 113 - .export = sha3_256_export, /* same as for 256 */ 114 - .import = sha3_224_import, /* function code different! */ 115 - .descsize = S390_SHA_CTX_SIZE, 116 - .statesize = SHA3_STATE_SIZE, 117 - .base = { 118 - .cra_name = "sha3-224", 119 - .cra_driver_name = "sha3-224-s390", 120 - .cra_priority = 300, 121 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 122 - .cra_blocksize = SHA3_224_BLOCK_SIZE, 123 - .cra_module = THIS_MODULE, 124 - } 125 - }; 126 - 127 - static int __init sha3_256_s390_init(void) 128 - { 129 - int ret; 130 - 131 - if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_256)) 132 - return -ENODEV; 133 - 134 - ret = crypto_register_shash(&sha3_256_alg); 135 - if (ret < 0) 136 - goto out; 137 - 138 - ret = crypto_register_shash(&sha3_224_alg); 139 - if (ret < 0) 140 - crypto_unregister_shash(&sha3_256_alg); 141 - out: 142 - return ret; 143 - } 144 - 145 - static void __exit sha3_256_s390_fini(void) 146 - { 147 - crypto_unregister_shash(&sha3_224_alg); 148 - crypto_unregister_shash(&sha3_256_alg); 149 - } 150 - 151 - module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha3_256_s390_init); 152 - module_exit(sha3_256_s390_fini); 153 - 154 - MODULE_ALIAS_CRYPTO("sha3-256"); 155 - MODULE_ALIAS_CRYPTO("sha3-224"); 156 - MODULE_LICENSE("GPL"); 157 - MODULE_DESCRIPTION("SHA3-256 and SHA3-224 Secure Hash Algorithm");
-157
arch/s390/crypto/sha3_512_s390.c
··· 1 - // SPDX-License-Identifier: GPL-2.0+ 2 - /* 3 - * Cryptographic API. 4 - * 5 - * s390 implementation of the SHA512 and SHA384 Secure Hash Algorithm. 6 - * 7 - * Copyright IBM Corp. 2019 8 - * Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com) 9 - */ 10 - #include <asm/cpacf.h> 11 - #include <crypto/internal/hash.h> 12 - #include <crypto/sha3.h> 13 - #include <linux/cpufeature.h> 14 - #include <linux/errno.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_sha3_512_init(struct shash_desc *desc) 22 - { 23 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 24 - 25 - sctx->first_message_part = test_facility(86); 26 - if (!sctx->first_message_part) 27 - memset(sctx->state, 0, sizeof(sctx->state)); 28 - sctx->count = 0; 29 - sctx->func = CPACF_KIMD_SHA3_512; 30 - 31 - return 0; 32 - } 33 - 34 - static int sha3_512_export(struct shash_desc *desc, void *out) 35 - { 36 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 37 - union { 38 - u8 *u8; 39 - u64 *u64; 40 - } p = { .u8 = out }; 41 - int i; 42 - 43 - if (sctx->first_message_part) { 44 - memset(out, 0, SHA3_STATE_SIZE); 45 - return 0; 46 - } 47 - for (i = 0; i < SHA3_STATE_SIZE / 8; i++) 48 - put_unaligned(le64_to_cpu(sctx->sha3.state[i]), p.u64++); 49 - return 0; 50 - } 51 - 52 - static int sha3_512_import(struct shash_desc *desc, const void *in) 53 - { 54 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 55 - union { 56 - const u8 *u8; 57 - const u64 *u64; 58 - } p = { .u8 = in }; 59 - int i; 60 - 61 - for (i = 0; i < SHA3_STATE_SIZE / 8; i++) 62 - sctx->sha3.state[i] = cpu_to_le64(get_unaligned(p.u64++)); 63 - sctx->count = 0; 64 - sctx->first_message_part = 0; 65 - sctx->func = CPACF_KIMD_SHA3_512; 66 - 67 - return 0; 68 - } 69 - 70 - static int sha3_384_import(struct shash_desc *desc, const void *in) 71 - { 72 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 73 - 74 - sha3_512_import(desc, in); 75 - sctx->func = CPACF_KIMD_SHA3_384; 76 - return 0; 77 - } 78 - 79 - static struct shash_alg sha3_512_alg = { 80 - .digestsize = SHA3_512_DIGEST_SIZE, 81 - .init = s390_sha3_512_init, 82 - .update = s390_sha_update_blocks, 83 - .finup = s390_sha_finup, 84 - .export = sha3_512_export, 85 - .import = sha3_512_import, 86 - .descsize = S390_SHA_CTX_SIZE, 87 - .statesize = SHA3_STATE_SIZE, 88 - .base = { 89 - .cra_name = "sha3-512", 90 - .cra_driver_name = "sha3-512-s390", 91 - .cra_priority = 300, 92 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 93 - .cra_blocksize = SHA3_512_BLOCK_SIZE, 94 - .cra_module = THIS_MODULE, 95 - } 96 - }; 97 - 98 - MODULE_ALIAS_CRYPTO("sha3-512"); 99 - 100 - static int s390_sha3_384_init(struct shash_desc *desc) 101 - { 102 - struct s390_sha_ctx *sctx = shash_desc_ctx(desc); 103 - 104 - s390_sha3_512_init(desc); 105 - sctx->func = CPACF_KIMD_SHA3_384; 106 - return 0; 107 - } 108 - 109 - static struct shash_alg sha3_384_alg = { 110 - .digestsize = SHA3_384_DIGEST_SIZE, 111 - .init = s390_sha3_384_init, 112 - .update = s390_sha_update_blocks, 113 - .finup = s390_sha_finup, 114 - .export = sha3_512_export, /* same as for 512 */ 115 - .import = sha3_384_import, /* function code different! */ 116 - .descsize = S390_SHA_CTX_SIZE, 117 - .statesize = SHA3_STATE_SIZE, 118 - .base = { 119 - .cra_name = "sha3-384", 120 - .cra_driver_name = "sha3-384-s390", 121 - .cra_priority = 300, 122 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 123 - .cra_blocksize = SHA3_384_BLOCK_SIZE, 124 - .cra_ctxsize = sizeof(struct s390_sha_ctx), 125 - .cra_module = THIS_MODULE, 126 - } 127 - }; 128 - 129 - MODULE_ALIAS_CRYPTO("sha3-384"); 130 - 131 - static int __init init(void) 132 - { 133 - int ret; 134 - 135 - if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_512)) 136 - return -ENODEV; 137 - ret = crypto_register_shash(&sha3_512_alg); 138 - if (ret < 0) 139 - goto out; 140 - ret = crypto_register_shash(&sha3_384_alg); 141 - if (ret < 0) 142 - crypto_unregister_shash(&sha3_512_alg); 143 - out: 144 - return ret; 145 - } 146 - 147 - static void __exit fini(void) 148 - { 149 - crypto_unregister_shash(&sha3_512_alg); 150 - crypto_unregister_shash(&sha3_384_alg); 151 - } 152 - 153 - module_cpu_feature_match(S390_CPU_FEATURE_MSA, init); 154 - module_exit(fini); 155 - 156 - MODULE_LICENSE("GPL"); 157 - MODULE_DESCRIPTION("SHA3-512 and SHA3-384 Secure Hash Algorithm");
-117
arch/s390/crypto/sha_common.c
··· 1 - // SPDX-License-Identifier: GPL-2.0+ 2 - /* 3 - * Cryptographic API. 4 - * 5 - * s390 generic implementation of the SHA Secure Hash Algorithms. 6 - * 7 - * Copyright IBM Corp. 2007 8 - * Author(s): Jan Glauber (jang@de.ibm.com) 9 - */ 10 - 11 - #include <crypto/internal/hash.h> 12 - #include <linux/export.h> 13 - #include <linux/module.h> 14 - #include <asm/cpacf.h> 15 - #include "sha.h" 16 - 17 - int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data, 18 - unsigned int len) 19 - { 20 - unsigned int bsize = crypto_shash_blocksize(desc->tfm); 21 - struct s390_sha_ctx *ctx = shash_desc_ctx(desc); 22 - unsigned int n; 23 - int fc; 24 - 25 - fc = ctx->func; 26 - if (ctx->first_message_part) 27 - fc |= CPACF_KIMD_NIP; 28 - 29 - /* process as many blocks as possible */ 30 - n = (len / bsize) * bsize; 31 - ctx->count += n; 32 - switch (ctx->func) { 33 - case CPACF_KLMD_SHA_512: 34 - case CPACF_KLMD_SHA3_384: 35 - if (ctx->count < n) 36 - ctx->sha512.count_hi++; 37 - break; 38 - } 39 - cpacf_kimd(fc, ctx->state, data, n); 40 - ctx->first_message_part = 0; 41 - return len - n; 42 - } 43 - EXPORT_SYMBOL_GPL(s390_sha_update_blocks); 44 - 45 - static int s390_crypto_shash_parmsize(int func) 46 - { 47 - switch (func) { 48 - case CPACF_KLMD_SHA_1: 49 - return 20; 50 - case CPACF_KLMD_SHA_256: 51 - return 32; 52 - case CPACF_KLMD_SHA_512: 53 - return 64; 54 - case CPACF_KLMD_SHA3_224: 55 - case CPACF_KLMD_SHA3_256: 56 - case CPACF_KLMD_SHA3_384: 57 - case CPACF_KLMD_SHA3_512: 58 - return 200; 59 - default: 60 - return -EINVAL; 61 - } 62 - } 63 - 64 - int s390_sha_finup(struct shash_desc *desc, const u8 *src, unsigned int len, 65 - u8 *out) 66 - { 67 - struct s390_sha_ctx *ctx = shash_desc_ctx(desc); 68 - int mbl_offset, fc; 69 - u64 bits; 70 - 71 - ctx->count += len; 72 - 73 - bits = ctx->count * 8; 74 - mbl_offset = s390_crypto_shash_parmsize(ctx->func); 75 - if (mbl_offset < 0) 76 - return -EINVAL; 77 - 78 - mbl_offset = mbl_offset / sizeof(u32); 79 - 80 - /* set total msg bit length (mbl) in CPACF parmblock */ 81 - switch (ctx->func) { 82 - case CPACF_KLMD_SHA_512: 83 - /* The SHA512 parmblock has a 128-bit mbl field. */ 84 - if (ctx->count < len) 85 - ctx->sha512.count_hi++; 86 - ctx->sha512.count_hi <<= 3; 87 - ctx->sha512.count_hi |= ctx->count >> 61; 88 - mbl_offset += sizeof(u64) / sizeof(u32); 89 - fallthrough; 90 - case CPACF_KLMD_SHA_1: 91 - case CPACF_KLMD_SHA_256: 92 - memcpy(ctx->state + mbl_offset, &bits, sizeof(bits)); 93 - break; 94 - case CPACF_KLMD_SHA3_224: 95 - case CPACF_KLMD_SHA3_256: 96 - case CPACF_KLMD_SHA3_384: 97 - case CPACF_KLMD_SHA3_512: 98 - break; 99 - default: 100 - return -EINVAL; 101 - } 102 - 103 - fc = ctx->func; 104 - fc |= test_facility(86) ? CPACF_KLMD_DUFOP : 0; 105 - if (ctx->first_message_part) 106 - fc |= CPACF_KLMD_NIP; 107 - cpacf_klmd(fc, ctx->state, src, len); 108 - 109 - /* copy digest to out */ 110 - memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm)); 111 - 112 - return 0; 113 - } 114 - EXPORT_SYMBOL_GPL(s390_sha_finup); 115 - 116 - MODULE_LICENSE("GPL"); 117 - MODULE_DESCRIPTION("s390 SHA cipher common functions");