mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-04 20:19:47 +08:00
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>
This commit is contained in:
parent
bf52d93865
commit
b9eac03edc
@ -795,7 +795,6 @@ CONFIG_CRYPTO_USER_API_RNG=m
|
||||
CONFIG_CRYPTO_USER_API_AEAD=m
|
||||
CONFIG_CRYPTO_SHA512_S390=m
|
||||
CONFIG_CRYPTO_SHA1_S390=m
|
||||
CONFIG_CRYPTO_SHA256_S390=m
|
||||
CONFIG_CRYPTO_SHA3_256_S390=m
|
||||
CONFIG_CRYPTO_SHA3_512_S390=m
|
||||
CONFIG_CRYPTO_GHASH_S390=m
|
||||
|
@ -782,7 +782,6 @@ CONFIG_CRYPTO_USER_API_RNG=m
|
||||
CONFIG_CRYPTO_USER_API_AEAD=m
|
||||
CONFIG_CRYPTO_SHA512_S390=m
|
||||
CONFIG_CRYPTO_SHA1_S390=m
|
||||
CONFIG_CRYPTO_SHA256_S390=m
|
||||
CONFIG_CRYPTO_SHA3_256_S390=m
|
||||
CONFIG_CRYPTO_SHA3_512_S390=m
|
||||
CONFIG_CRYPTO_GHASH_S390=m
|
||||
|
@ -22,16 +22,6 @@ config CRYPTO_SHA1_S390
|
||||
|
||||
It is available as of z990.
|
||||
|
||||
config CRYPTO_SHA256_S390
|
||||
tristate "Hash functions: SHA-224 and SHA-256"
|
||||
select CRYPTO_HASH
|
||||
help
|
||||
SHA-224 and SHA-256 secure hash algorithms (FIPS 180)
|
||||
|
||||
Architecture: s390
|
||||
|
||||
It is available as of z9.
|
||||
|
||||
config CRYPTO_SHA3_256_S390
|
||||
tristate "Hash functions: SHA3-224 and SHA3-256"
|
||||
select CRYPTO_HASH
|
||||
|
@ -4,7 +4,6 @@
|
||||
#
|
||||
|
||||
obj-$(CONFIG_CRYPTO_SHA1_S390) += sha1_s390.o sha_common.o
|
||||
obj-$(CONFIG_CRYPTO_SHA256_S390) += sha256_s390.o sha_common.o
|
||||
obj-$(CONFIG_CRYPTO_SHA512_S390) += sha512_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
|
||||
|
@ -1,144 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-2.0+
|
||||
/*
|
||||
* Cryptographic API.
|
||||
*
|
||||
* s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
|
||||
*
|
||||
* s390 Version:
|
||||
* Copyright IBM Corp. 2005, 2011
|
||||
* Author(s): Jan Glauber (jang@de.ibm.com)
|
||||
*/
|
||||
#include <asm/cpacf.h>
|
||||
#include <crypto/internal/hash.h>
|
||||
#include <crypto/sha2.h>
|
||||
#include <linux/cpufeature.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/string.h>
|
||||
|
||||
#include "sha.h"
|
||||
|
||||
static int s390_sha256_init(struct shash_desc *desc)
|
||||
{
|
||||
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
|
||||
|
||||
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;
|
||||
sctx->func = CPACF_KIMD_SHA_256;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sha256_export(struct shash_desc *desc, void *out)
|
||||
{
|
||||
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
|
||||
struct crypto_sha256_state *octx = out;
|
||||
|
||||
octx->count = sctx->count;
|
||||
memcpy(octx->state, sctx->state, sizeof(octx->state));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sha256_import(struct shash_desc *desc, const void *in)
|
||||
{
|
||||
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
|
||||
const struct crypto_sha256_state *ictx = in;
|
||||
|
||||
sctx->count = ictx->count;
|
||||
memcpy(sctx->state, ictx->state, sizeof(ictx->state));
|
||||
sctx->func = CPACF_KIMD_SHA_256;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct shash_alg sha256_alg = {
|
||||
.digestsize = SHA256_DIGEST_SIZE,
|
||||
.init = s390_sha256_init,
|
||||
.update = s390_sha_update_blocks,
|
||||
.finup = s390_sha_finup,
|
||||
.export = sha256_export,
|
||||
.import = sha256_import,
|
||||
.descsize = S390_SHA_CTX_SIZE,
|
||||
.statesize = sizeof(struct crypto_sha256_state),
|
||||
.base = {
|
||||
.cra_name = "sha256",
|
||||
.cra_driver_name= "sha256-s390",
|
||||
.cra_priority = 300,
|
||||
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
|
||||
.cra_blocksize = SHA256_BLOCK_SIZE,
|
||||
.cra_module = THIS_MODULE,
|
||||
}
|
||||
};
|
||||
|
||||
static int s390_sha224_init(struct shash_desc *desc)
|
||||
{
|
||||
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
|
||||
|
||||
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;
|
||||
sctx->func = CPACF_KIMD_SHA_256;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct shash_alg sha224_alg = {
|
||||
.digestsize = SHA224_DIGEST_SIZE,
|
||||
.init = s390_sha224_init,
|
||||
.update = s390_sha_update_blocks,
|
||||
.finup = s390_sha_finup,
|
||||
.export = sha256_export,
|
||||
.import = sha256_import,
|
||||
.descsize = S390_SHA_CTX_SIZE,
|
||||
.statesize = sizeof(struct crypto_sha256_state),
|
||||
.base = {
|
||||
.cra_name = "sha224",
|
||||
.cra_driver_name= "sha224-s390",
|
||||
.cra_priority = 300,
|
||||
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
|
||||
.cra_blocksize = SHA224_BLOCK_SIZE,
|
||||
.cra_module = THIS_MODULE,
|
||||
}
|
||||
};
|
||||
|
||||
static int __init sha256_s390_init(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256))
|
||||
return -ENODEV;
|
||||
ret = crypto_register_shash(&sha256_alg);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
ret = crypto_register_shash(&sha224_alg);
|
||||
if (ret < 0)
|
||||
crypto_unregister_shash(&sha256_alg);
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void __exit sha256_s390_fini(void)
|
||||
{
|
||||
crypto_unregister_shash(&sha224_alg);
|
||||
crypto_unregister_shash(&sha256_alg);
|
||||
}
|
||||
|
||||
module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha256_s390_init);
|
||||
module_exit(sha256_s390_fini);
|
||||
|
||||
MODULE_ALIAS_CRYPTO("sha256");
|
||||
MODULE_ALIAS_CRYPTO("sha224");
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");
|
@ -5,3 +5,9 @@ config CRYPTO_CHACHA_S390
|
||||
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
|
||||
|
@ -2,3 +2,5 @@
|
||||
|
||||
obj-$(CONFIG_CRYPTO_CHACHA_S390) += chacha_s390.o
|
||||
chacha_s390-y := chacha-glue.o chacha-s390.o
|
||||
|
||||
obj-$(CONFIG_CRYPTO_SHA256_S390) += sha256.o
|
||||
|
47
arch/s390/lib/crypto/sha256.c
Normal file
47
arch/s390/lib/crypto/sha256.c
Normal file
@ -0,0 +1,47 @@
|
||||
// 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(u32 state[SHA256_STATE_WORDS],
|
||||
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(sha256_blocks_arch);
|
||||
|
||||
bool sha256_is_arch_optimized(void)
|
||||
{
|
||||
return static_key_enabled(&have_cpacf_sha256);
|
||||
}
|
||||
EXPORT_SYMBOL(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);
|
||||
return 0;
|
||||
}
|
||||
arch_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)");
|
Loading…
Reference in New Issue
Block a user