Commit 4aa6dc90 authored by Eric Biggers's avatar Eric Biggers Committed by Herbert Xu
Browse files

crypto: chacha - centralize the skcipher wrappers for arch code



Following the example of the crc32 and crc32c code, make the crypto
subsystem register both generic and architecture-optimized chacha20,
xchacha20, and xchacha12 skcipher algorithms, all implemented on top of
the appropriate library functions.  This eliminates the need for every
architecture to implement the same skcipher glue code.

To register the architecture-optimized skciphers only when
architecture-optimized code is actually being used, add a function
chacha_is_arch_optimized() and make each arch implement it.  Change each
architecture's ChaCha module_init function to arch_initcall so that the
CPU feature detection is guaranteed to run before
chacha_is_arch_optimized() gets called by crypto/chacha.c.  In the case
of s390, remove the CPU feature based module autoloading, which is no
longer needed since the module just gets pulled in via function linkage.

Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent ceba0eda
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -287,6 +287,13 @@ static struct skcipher_alg neon_algs[] = {
	}
};

bool chacha_is_arch_optimized(void)
{
	/* We always can use at least the ARM scalar implementation. */
	return true;
}
EXPORT_SYMBOL(chacha_is_arch_optimized);

static int __init chacha_simd_mod_init(void)
{
	int err = 0;
@@ -333,7 +340,7 @@ static void __exit chacha_simd_mod_fini(void)
	}
}

module_init(chacha_simd_mod_init);
arch_initcall(chacha_simd_mod_init);
module_exit(chacha_simd_mod_fini);

MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (scalar and NEON accelerated)");
+7 −1
Original line number Diff line number Diff line
@@ -206,6 +206,12 @@ static struct skcipher_alg algs[] = {
	}
};

bool chacha_is_arch_optimized(void)
{
	return static_key_enabled(&have_neon);
}
EXPORT_SYMBOL(chacha_is_arch_optimized);

static int __init chacha_simd_mod_init(void)
{
	if (!cpu_have_named_feature(ASIMD))
@@ -223,7 +229,7 @@ static void __exit chacha_simd_mod_fini(void)
		crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
}

module_init(chacha_simd_mod_init);
arch_initcall(chacha_simd_mod_init);
module_exit(chacha_simd_mod_fini);

MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (NEON accelerated)");
+7 −1
Original line number Diff line number Diff line
@@ -120,6 +120,12 @@ static struct skcipher_alg algs[] = {
	}
};

bool chacha_is_arch_optimized(void)
{
	return true;
}
EXPORT_SYMBOL(chacha_is_arch_optimized);

static int __init chacha_simd_mod_init(void)
{
	return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
@@ -132,7 +138,7 @@ static void __exit chacha_simd_mod_fini(void)
		crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
}

module_init(chacha_simd_mod_init);
arch_initcall(chacha_simd_mod_init);
module_exit(chacha_simd_mod_fini);

MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (MIPS accelerated)");
+7 −1
Original line number Diff line number Diff line
@@ -189,6 +189,12 @@ static struct skcipher_alg algs[] = {
	}
};

bool chacha_is_arch_optimized(void)
{
	return static_key_enabled(&have_p10);
}
EXPORT_SYMBOL(chacha_is_arch_optimized);

static int __init chacha_p10_init(void)
{
	if (!cpu_has_feature(CPU_FTR_ARCH_31))
@@ -207,7 +213,7 @@ static void __exit chacha_p10_exit(void)
	crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
}

module_init(chacha_p10_init);
arch_initcall(chacha_p10_init);
module_exit(chacha_p10_exit);

MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (P10 accelerated)");
+7 −1
Original line number Diff line number Diff line
@@ -49,6 +49,12 @@ void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src, unsigned int bytes,
}
EXPORT_SYMBOL(chacha_crypt_arch);

bool chacha_is_arch_optimized(void)
{
	return static_key_enabled(&use_zvkb);
}
EXPORT_SYMBOL(chacha_is_arch_optimized);

static int __init riscv64_chacha_mod_init(void)
{
	if (riscv_isa_extension_available(NULL, ZVKB) &&
@@ -56,7 +62,7 @@ static int __init riscv64_chacha_mod_init(void)
		static_branch_enable(&use_zvkb);
	return 0;
}
module_init(riscv64_chacha_mod_init);
arch_initcall(riscv64_chacha_mod_init);

MODULE_DESCRIPTION("ChaCha stream cipher (RISC-V optimized)");
MODULE_AUTHOR("Jerry Shih <jerry.shih@sifive.com>");
Loading