mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 06:44:00 -04:00
lib/crypto: gf128hash: Rename polyval module to gf128hash
Currently, the standalone GHASH code is coupled with crypto_shash. This has resulted in unnecessary complexity and overhead, as well as the code being unavailable to library code such as the AES-GCM library. Like was done with POLYVAL, it needs to find a new home in lib/crypto/. GHASH and POLYVAL are closely related and can each be implemented in terms of each other. Optimized code for one can be reused with the other. But also since GHASH tends to be difficult to implement directly due to its unnatural bit order, most modern GHASH implementations (including the existing arm, arm64, powerpc, and x86 optimized GHASH code, and the new generic GHASH code I'll be adding) actually reinterpret the GHASH computation as an equivalent POLYVAL computation, pre and post-processing the inputs and outputs to map to/from POLYVAL. Given this close relationship, it makes sense to group the GHASH and POLYVAL code together in the same module. This gives us a wide range of options for implementing them, reusing code between the two and properly utilizing whatever instructions each architecture provides. Thus, GHASH support will be added to the library module that is currently called "polyval". Rename it to an appropriate name: "gf128hash". Rename files, options, functions, etc. where appropriate to reflect the upcoming sharing with GHASH. (Note: polyval_kunit is not renamed, as ghash_kunit will be added alongside it instead.) Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20260319061723.1140720-2-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
This commit is contained in:
@@ -686,7 +686,7 @@ config CRYPTO_ECB
|
||||
config CRYPTO_HCTR2
|
||||
tristate "HCTR2"
|
||||
select CRYPTO_XCTR
|
||||
select CRYPTO_LIB_POLYVAL
|
||||
select CRYPTO_LIB_GF128HASH
|
||||
select CRYPTO_MANAGER
|
||||
help
|
||||
HCTR2 length-preserving encryption mode
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
* (https://eprint.iacr.org/2021/1441.pdf)
|
||||
*/
|
||||
|
||||
#include <crypto/gf128hash.h>
|
||||
#include <crypto/internal/cipher.h>
|
||||
#include <crypto/internal/skcipher.h>
|
||||
#include <crypto/polyval.h>
|
||||
#include <crypto/scatterwalk.h>
|
||||
#include <linux/module.h>
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
/*
|
||||
* POLYVAL library API
|
||||
* GF(2^128) polynomial hashing: GHASH and POLYVAL
|
||||
*
|
||||
* Copyright 2025 Google LLC
|
||||
*/
|
||||
|
||||
#ifndef _CRYPTO_POLYVAL_H
|
||||
#define _CRYPTO_POLYVAL_H
|
||||
#ifndef _CRYPTO_GF128HASH_H
|
||||
#define _CRYPTO_GF128HASH_H
|
||||
|
||||
#include <linux/string.h>
|
||||
#include <linux/types.h>
|
||||
@@ -44,7 +44,7 @@ struct polyval_elem {
|
||||
* exponentiation repeats the POLYVAL dot operation, with its "extra" x^-128.
|
||||
*/
|
||||
struct polyval_key {
|
||||
#ifdef CONFIG_CRYPTO_LIB_POLYVAL_ARCH
|
||||
#ifdef CONFIG_CRYPTO_LIB_GF128HASH_ARCH
|
||||
#ifdef CONFIG_ARM64
|
||||
/** @h_powers: Powers of the hash key H^8 through H^1 */
|
||||
struct polyval_elem h_powers[8];
|
||||
@@ -54,10 +54,10 @@ struct polyval_key {
|
||||
#else
|
||||
#error "Unhandled arch"
|
||||
#endif
|
||||
#else /* CONFIG_CRYPTO_LIB_POLYVAL_ARCH */
|
||||
#else /* CONFIG_CRYPTO_LIB_GF128HASH_ARCH */
|
||||
/** @h: The hash key H */
|
||||
struct polyval_elem h;
|
||||
#endif /* !CONFIG_CRYPTO_LIB_POLYVAL_ARCH */
|
||||
#endif /* !CONFIG_CRYPTO_LIB_GF128HASH_ARCH */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -84,7 +84,7 @@ struct polyval_ctx {
|
||||
*
|
||||
* Context: Any context.
|
||||
*/
|
||||
#ifdef CONFIG_CRYPTO_LIB_POLYVAL_ARCH
|
||||
#ifdef CONFIG_CRYPTO_LIB_GF128HASH_ARCH
|
||||
void polyval_preparekey(struct polyval_key *key,
|
||||
const u8 raw_key[POLYVAL_BLOCK_SIZE]);
|
||||
|
||||
@@ -187,4 +187,4 @@ static inline void polyval(const struct polyval_key *key,
|
||||
polyval_final(&ctx, out);
|
||||
}
|
||||
|
||||
#endif /* _CRYPTO_POLYVAL_H */
|
||||
#endif /* _CRYPTO_GF128HASH_H */
|
||||
@@ -110,6 +110,18 @@ config CRYPTO_LIB_CURVE25519_GENERIC
|
||||
config CRYPTO_LIB_DES
|
||||
tristate
|
||||
|
||||
config CRYPTO_LIB_GF128HASH
|
||||
tristate
|
||||
help
|
||||
The GHASH and POLYVAL library functions. Select this if your module
|
||||
uses any of the functions from <crypto/gf128hash.h>.
|
||||
|
||||
config CRYPTO_LIB_GF128HASH_ARCH
|
||||
bool
|
||||
depends on CRYPTO_LIB_GF128HASH && !UML
|
||||
default y if ARM64
|
||||
default y if X86_64
|
||||
|
||||
config CRYPTO_LIB_MD5
|
||||
tristate
|
||||
help
|
||||
@@ -178,18 +190,6 @@ config CRYPTO_LIB_POLY1305_RSIZE
|
||||
default 9 if ARM || ARM64
|
||||
default 1
|
||||
|
||||
config CRYPTO_LIB_POLYVAL
|
||||
tristate
|
||||
help
|
||||
The POLYVAL library functions. Select this if your module uses any of
|
||||
the functions from <crypto/polyval.h>.
|
||||
|
||||
config CRYPTO_LIB_POLYVAL_ARCH
|
||||
bool
|
||||
depends on CRYPTO_LIB_POLYVAL && !UML
|
||||
default y if ARM64
|
||||
default y if X86_64
|
||||
|
||||
config CRYPTO_LIB_CHACHA20POLY1305
|
||||
tristate
|
||||
select CRYPTO_LIB_CHACHA
|
||||
|
||||
@@ -154,6 +154,16 @@ libdes-y := des.o
|
||||
|
||||
################################################################################
|
||||
|
||||
obj-$(CONFIG_CRYPTO_LIB_GF128HASH) += libgf128hash.o
|
||||
libgf128hash-y := gf128hash.o
|
||||
ifeq ($(CONFIG_CRYPTO_LIB_GF128HASH_ARCH),y)
|
||||
CFLAGS_gf128hash.o += -I$(src)/$(SRCARCH)
|
||||
libgf128hash-$(CONFIG_ARM64) += arm64/polyval-ce-core.o
|
||||
libgf128hash-$(CONFIG_X86) += x86/polyval-pclmul-avx.o
|
||||
endif
|
||||
|
||||
################################################################################
|
||||
|
||||
obj-$(CONFIG_CRYPTO_LIB_MD5) += libmd5.o
|
||||
libmd5-y := md5.o
|
||||
ifeq ($(CONFIG_CRYPTO_LIB_MD5_ARCH),y)
|
||||
@@ -251,16 +261,6 @@ clean-files += arm/poly1305-core.S \
|
||||
|
||||
################################################################################
|
||||
|
||||
obj-$(CONFIG_CRYPTO_LIB_POLYVAL) += libpolyval.o
|
||||
libpolyval-y := polyval.o
|
||||
ifeq ($(CONFIG_CRYPTO_LIB_POLYVAL_ARCH),y)
|
||||
CFLAGS_polyval.o += -I$(src)/$(SRCARCH)
|
||||
libpolyval-$(CONFIG_ARM64) += arm64/polyval-ce-core.o
|
||||
libpolyval-$(CONFIG_X86) += x86/polyval-pclmul-avx.o
|
||||
endif
|
||||
|
||||
################################################################################
|
||||
|
||||
obj-$(CONFIG_CRYPTO_LIB_SHA1) += libsha1.o
|
||||
libsha1-y := sha1.o
|
||||
ifeq ($(CONFIG_CRYPTO_LIB_SHA1_ARCH),y)
|
||||
|
||||
@@ -72,8 +72,8 @@ static void polyval_blocks_arch(struct polyval_elem *acc,
|
||||
}
|
||||
}
|
||||
|
||||
#define polyval_mod_init_arch polyval_mod_init_arch
|
||||
static void polyval_mod_init_arch(void)
|
||||
#define gf128hash_mod_init_arch gf128hash_mod_init_arch
|
||||
static void gf128hash_mod_init_arch(void)
|
||||
{
|
||||
if (cpu_have_named_feature(PMULL))
|
||||
static_branch_enable(&have_pmull);
|
||||
@@ -1,11 +1,11 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* POLYVAL library functions
|
||||
* GF(2^128) polynomial hashing: GHASH and POLYVAL
|
||||
*
|
||||
* Copyright 2025 Google LLC
|
||||
*/
|
||||
|
||||
#include <crypto/polyval.h>
|
||||
#include <crypto/gf128hash.h>
|
||||
#include <linux/export.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/string.h>
|
||||
@@ -218,8 +218,8 @@ polyval_blocks_generic(struct polyval_elem *acc, const struct polyval_elem *key,
|
||||
}
|
||||
|
||||
/* Include the arch-optimized implementation of POLYVAL, if one is available. */
|
||||
#ifdef CONFIG_CRYPTO_LIB_POLYVAL_ARCH
|
||||
#include "polyval.h" /* $(SRCARCH)/polyval.h */
|
||||
#ifdef CONFIG_CRYPTO_LIB_GF128HASH_ARCH
|
||||
#include "gf128hash.h" /* $(SRCARCH)/gf128hash.h */
|
||||
void polyval_preparekey(struct polyval_key *key,
|
||||
const u8 raw_key[POLYVAL_BLOCK_SIZE])
|
||||
{
|
||||
@@ -238,7 +238,7 @@ EXPORT_SYMBOL_GPL(polyval_preparekey);
|
||||
|
||||
static void polyval_mul(struct polyval_ctx *ctx)
|
||||
{
|
||||
#ifdef CONFIG_CRYPTO_LIB_POLYVAL_ARCH
|
||||
#ifdef CONFIG_CRYPTO_LIB_GF128HASH_ARCH
|
||||
polyval_mul_arch(&ctx->acc, ctx->key);
|
||||
#else
|
||||
polyval_mul_generic(&ctx->acc, &ctx->key->h);
|
||||
@@ -248,7 +248,7 @@ static void polyval_mul(struct polyval_ctx *ctx)
|
||||
static void polyval_blocks(struct polyval_ctx *ctx,
|
||||
const u8 *data, size_t nblocks)
|
||||
{
|
||||
#ifdef CONFIG_CRYPTO_LIB_POLYVAL_ARCH
|
||||
#ifdef CONFIG_CRYPTO_LIB_GF128HASH_ARCH
|
||||
polyval_blocks_arch(&ctx->acc, ctx->key, data, nblocks);
|
||||
#else
|
||||
polyval_blocks_generic(&ctx->acc, &ctx->key->h, data, nblocks);
|
||||
@@ -289,19 +289,19 @@ void polyval_final(struct polyval_ctx *ctx, u8 out[POLYVAL_BLOCK_SIZE])
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(polyval_final);
|
||||
|
||||
#ifdef polyval_mod_init_arch
|
||||
static int __init polyval_mod_init(void)
|
||||
#ifdef gf128hash_mod_init_arch
|
||||
static int __init gf128hash_mod_init(void)
|
||||
{
|
||||
polyval_mod_init_arch();
|
||||
gf128hash_mod_init_arch();
|
||||
return 0;
|
||||
}
|
||||
subsys_initcall(polyval_mod_init);
|
||||
subsys_initcall(gf128hash_mod_init);
|
||||
|
||||
static void __exit polyval_mod_exit(void)
|
||||
static void __exit gf128hash_mod_exit(void)
|
||||
{
|
||||
}
|
||||
module_exit(polyval_mod_exit);
|
||||
module_exit(gf128hash_mod_exit);
|
||||
#endif
|
||||
|
||||
MODULE_DESCRIPTION("POLYVAL almost-XOR-universal hash function");
|
||||
MODULE_DESCRIPTION("GF(2^128) polynomial hashing: GHASH and POLYVAL");
|
||||
MODULE_LICENSE("GPL");
|
||||
@@ -69,7 +69,7 @@ config CRYPTO_LIB_POLY1305_KUNIT_TEST
|
||||
|
||||
config CRYPTO_LIB_POLYVAL_KUNIT_TEST
|
||||
tristate "KUnit tests for POLYVAL" if !KUNIT_ALL_TESTS
|
||||
depends on KUNIT && CRYPTO_LIB_POLYVAL
|
||||
depends on KUNIT && CRYPTO_LIB_GF128HASH
|
||||
default KUNIT_ALL_TESTS
|
||||
select CRYPTO_LIB_BENCHMARK_VISIBLE
|
||||
help
|
||||
@@ -122,11 +122,11 @@ config CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT
|
||||
select CRYPTO_LIB_AES_CBC_MACS
|
||||
select CRYPTO_LIB_BLAKE2B
|
||||
select CRYPTO_LIB_CURVE25519
|
||||
select CRYPTO_LIB_GF128HASH
|
||||
select CRYPTO_LIB_MD5
|
||||
select CRYPTO_LIB_MLDSA
|
||||
select CRYPTO_LIB_NH
|
||||
select CRYPTO_LIB_POLY1305
|
||||
select CRYPTO_LIB_POLYVAL
|
||||
select CRYPTO_LIB_SHA1
|
||||
select CRYPTO_LIB_SHA256
|
||||
select CRYPTO_LIB_SHA512
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
/*
|
||||
* Copyright 2025 Google LLC
|
||||
*/
|
||||
#include <crypto/polyval.h>
|
||||
#include <crypto/gf128hash.h>
|
||||
#include "polyval-testvecs.h"
|
||||
|
||||
/*
|
||||
|
||||
@@ -74,8 +74,8 @@ static void polyval_blocks_arch(struct polyval_elem *acc,
|
||||
}
|
||||
}
|
||||
|
||||
#define polyval_mod_init_arch polyval_mod_init_arch
|
||||
static void polyval_mod_init_arch(void)
|
||||
#define gf128hash_mod_init_arch gf128hash_mod_init_arch
|
||||
static void gf128hash_mod_init_arch(void)
|
||||
{
|
||||
if (boot_cpu_has(X86_FEATURE_PCLMULQDQ) &&
|
||||
boot_cpu_has(X86_FEATURE_AVX))
|
||||
Reference in New Issue
Block a user