Add cryptographic hash and signature verification kfuncs to BPF
Subsystems (6)
v4 cover Add cryptographic hash and signature verification kfuncs to BPF
This patch series enhances BPF's cryptographic functionality by introducing kernel functions for SHA hashing and ECDSA signature verification. The changes enable BPF programs to verify data integrity and authenticity across networking, security, and observability use cases.
The series addresses two gaps in BPF's cryptographic toolkit:
1. Cryptographic hashing - supports content verification and message digest preparation
2. Asymmetric signature verification - allows validation of signed data without requiring private keys in the datapath
Use cases include:
- Verifying signed network packets or application data in XDP/TC programs
- Integrity checks within tracing and security monitoring
- Zero-trust security models with BPF-based credential verification
- Content-addressed storage in BPF-based filesystems
The implementation leverages existing BPF patterns: it uses bpf_dynptr for memory safety, reuses kernel crypto libraries (lib/crypto/sha256.c and crypto/ecdsa.c) rather than reimplementing algorithms, and provides context-based APIs supporting multiple program types.
v2:
- Fixed redundant __bpf_dynptr_is_rdonly() checks (Vadim)
- Added BPF hash algorithm type registration module in crypto/ subsystem
- Added CONFIG_CRYPTO_HASH2 guards around bpf_crypto_hash() kfunc and its BTF registration, matching the pattern used for CONFIG_CRYPTO_ECDSA
- Added mandatory digestsize validation for hash operations
v3:
- Fixed patch ordering - header changes now in separate first commit before crypto module to ensure bisectability (bot+bpf-ci)
- Fixed type mismatch - changed u32 to u64 for dynptr sizes in bpf_crypto_hash() to match __bpf_dynptr_size() return type (Mykyta)
- Added CONFIG_CRYPTO_ECDSA to selftest config (Song)
- Refactored test code duplication with setup_skel() helper (Song)
- Added copyright notices to all new files
v4:
- Reused common bpf_crypto_ctx structure for hash and signature operations instead of separate context types (Song)
- Fixed integer truncation in bpf_crypto_hash when data_len > UINT_MAX
- Corrected KF_RCU flags for ECDSA kfuncs (only bpf_ecdsa_verify needs KF_RCU)
- Updated MAINTAINERS file in test patches
- Refactored selftests to use crypto_common.h for kfunc declarations
Daniel Hodges (6):
crypto: Add BPF hash algorithm type registration module
crypto: Add BPF signature algorithm type registration module
bpf: Add hash kfunc for cryptographic hashing selftests/bpf: Add tests for bpf_crypto_hash kfunc
bpf: Add ECDSA signature verification kfuncs selftests/bpf: Add tests for ECDSA signature verification kfuncs
MAINTAINERS | 6 + crypto/Makefile | 6 + crypto/bpf_crypto_shash.c | 96 +++++++++ crypto/bpf_crypto_sig.c | 60 ++++++ crypto/bpf_crypto_skcipher.c | 1 + include/linux/bpf_crypto.h | 7 + kernel/bpf/crypto.c | 193 +++++++++++++++++- tools/testing/selftests/bpf/config | 3 + .../selftests/bpf/prog_tests/crypto_hash.c | 147 +++++++++++++ .../selftests/bpf/prog_tests/ecdsa_verify.c | 75 +++++++ .../selftests/bpf/progs/crypto_common.h | 7 + .../testing/selftests/bpf/progs/crypto_hash.c | 136 ++++++++++++ .../selftests/bpf/progs/ecdsa_verify.c | 157 ++++++++++++++ 13 files changed, 886 insertions(+), 8 deletions(-) create mode 100644 crypto/bpf_crypto_shash.c create mode 100644 crypto/bpf_crypto_sig.c create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_hash.c create mode 100644 tools/testing/selftests/bpf/prog_tests/ecdsa_verify.c create mode 100644 tools/testing/selftests/bpf/progs/crypto_hash.c create mode 100644 tools/testing/selftests/bpf/progs/ecdsa_verify.c
Signature · 1 line
2.51.0
PATCH v4 6/6 selftests/bpf: Add tests for ECDSA signature verification kfuncs
Add selftests to validate the ECDSA signature verification kfuncs introduced in the BPF crypto subsystem. The tests verify both valid signature acceptance and invalid signature rejection using the context-based ECDSA API.
The tests use RFC 6979 test vectors for NIST P-256 (secp256r1) with well-known valid signatures. The algorithm "p1363(ecdsa-nist-p256)" is used to handle standard r||s signature format.
--- MAINTAINERS | 2 + tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/ecdsa_verify.c | 75 +++++++++ .../selftests/bpf/progs/ecdsa_verify.c | 157 ++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/ecdsa_verify.c create mode 100644 tools/testing/selftests/bpf/progs/ecdsa_verify.c
MAINTAINERS +2 -0
@@ -4718,7 +4718,9 @@ F: crypto/bpf_crypto_skcipher.c F: include/linux/bpf_crypto.h F: kernel/bpf/crypto.c F: tools/testing/selftests/bpf/prog_tests/crypto_hash.c +F: tools/testing/selftests/bpf/prog_tests/ecdsa_verify.c F: tools/testing/selftests/bpf/progs/crypto_hash.c +F: tools/testing/selftests/bpf/progs/ecdsa_verify.c BPF [DOCUMENTATION] (Related to Standardization) R: David Vernet <[email protected]>
tools/testing/selftests/bpf/config +1 -0
@@ -15,6 +15,7 @@ CONFIG_CRYPTO_HMAC=y CONFIG_CRYPTO_HASH2=y CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_SHA512=y +CONFIG_CRYPTO_ECDSA=y CONFIG_CRYPTO_USER_API=y CONFIG_CRYPTO_USER_API_HASH=y CONFIG_CRYPTO_USER_API_SKCIPHER=y
tools/testing/selftests/bpf/prog_tests/ecdsa_verify.c new file +75 -0
75 lines not on this page · raw message
tools/testing/selftests/bpf/progs/ecdsa_verify.c new file +157 -0
157 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 2/6 crypto: Add BPF signature algorithm type registration module
In reply to Daniel HodgesAdd a new bpf_crypto_sig module that registers signature verification algorithms with the BPF crypto type system. This enables signature operations (like ECDSA) to use the unified bpf_crypto_ctx structure instead of requiring separate context types.
The module provides:
- alloc_tfm/free_tfm for crypto_sig transform lifecycle
- has_algo to check algorithm availability
- get_flags for crypto API flags
This allows ECDSA and other signature verification operations to integrate with the existing BPF crypto infrastructure.
--- MAINTAINERS | 1 + crypto/Makefile | 3 +++ crypto/bpf_crypto_sig.c | 59 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 crypto/bpf_crypto_sig.c
MAINTAINERS +1 -0
@@ -4713,6 +4713,7 @@ M: Vadim Fedorenko <[email protected]> L: [email protected] S: Maintained F: crypto/bpf_crypto_shash.c +F: crypto/bpf_crypto_sig.c F: crypto/bpf_crypto_skcipher.c F: include/linux/bpf_crypto.h F: kernel/bpf/crypto.c
crypto/Makefile +3 -0
@@ -36,6 +36,9 @@ endif obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o obj-$(CONFIG_CRYPTO_SIG2) += sig.o +ifeq ($(CONFIG_BPF_SYSCALL),y) +obj-$(CONFIG_CRYPTO_SIG2) += bpf_crypto_sig.o +endif obj-$(CONFIG_CRYPTO_KPP2) += kpp.o obj-$(CONFIG_CRYPTO_HKDF) += hkdf.o
crypto/bpf_crypto_sig.c new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 3/6 bpf: Add hash kfunc for cryptographic hashing
In reply to Daniel HodgesExtend bpf_crypto_type structure with hash operations:
- hash(): Performs hashing operation
- digestsize(): Returns hash output size
Update bpf_crypto_ctx_create() to support keyless operations:
- Hash algorithms don't require keys, unlike ciphers
- Only validates key presence if type->setkey is defined
- Conditionally sets IV/state length for cipher operations only
Add bpf_crypto_hash() kfunc that works with any hash algorithm registered in the kernel's crypto API through the BPF crypto type system. This enables BPF programs to compute cryptographic hashes for use cases such as content verification, integrity checking, and data authentication.
--- kernel/bpf/crypto.c | 78 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-)
kernel/bpf/crypto.c +70 -8
@@ -171,7 +171,12 @@ bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz, goto err_module_put; } -if (!params->key_len || params->key_len >sizeof (params->key)) { +/* Hash operations don't require a key, but cipher operations do */ +if (params->key_len >sizeof (params->key)) { + *err = -EINVAL ; + goto err_module_put; + } +if (!params->key_len && type->setkey) { *err = -EINVAL ; goto err_module_put; }
@@ -195,16 +200,19 @@ bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz, goto err_free_tfm; } - *err = type->setkey (ctx->tfm, params->key, params->key_len); -if (*err) - goto err_free_tfm; +if (params->key_len) { + *err = type->setkey (ctx->tfm, params->key, params->key_len); +if (*err) + goto err_free_tfm; -if (type->get_flags (ctx->tfm) &CRYPTO_TFM_NEED_KEY ) { - *err = -EINVAL ; - goto err_free_tfm; +if (type->get_flags (ctx->tfm) &CRYPTO_TFM_NEED_KEY ) { + *err = -EINVAL ; + goto err_free_tfm; + } } - ctx->siv_len = type->ivsize (ctx->tfm) + type->statesize (ctx->tfm); +if (type->ivsize && type->statesize) + ctx->siv_len = type->ivsize (ctx->tfm) + type->statesize (ctx->tfm);refcount_set (&ctx->usage,1 );
@@ -343,6 +351,55 @@ __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx,return bpf_crypto_crypt (ctx, src_kern, dst_kern, siv_kern, false); } +#if IS_ENABLED (CONFIG_CRYPTO_HASH2 ) +/** +* bpf_crypto_hash() - Compute hash using configured context +* @ctx: The crypto context being used. The ctx must be a trusted pointer. +* @data: bpf_dynptr to the input data to hash. Must be a trusted pointer. +* @out: bpf_dynptr to the output buffer. Must be a trusted pointer. +* +* Computes hash of the input data using the crypto context. The output buffer +* must be at least as large as the digest size of the hash algorithm. +*/ +__bpf_kfunc int bpf_crypto_hash(struct bpf_crypto_ctx *ctx, +const struct bpf_dynptr *data, +const struct bpf_dynptr *out) +{ +const struct bpf_dynptr_kern *data_kern = (struct bpf_dynptr_kern *)data; +const struct bpf_dynptr_kern *out_kern = (struct bpf_dynptr_kern *)out; +unsigned int digestsize; +u64 data_len, out_len; +const u8 *data_ptr; +u8 *out_ptr; + +if (!ctx->type->hash) +return -EOPNOTSUPP ; + + data_len =__bpf_dynptr_size (data_kern); + out_len =__bpf_dynptr_size (out_kern); + +if (data_len ==0 || data_len >UINT_MAX ) +return -EINVAL ; + +if (!ctx->type->digestsize) +return -EOPNOTSUPP ; + + digestsize = ctx->type->digestsize (ctx->tfm); +if (out_len < digestsize) +return -EINVAL ; + + data_ptr =__bpf_dynptr_data (data_kern, data_len); +if (!data_ptr) +return -EINVAL ; + + out_ptr =__bpf_dynptr_data_rw (out_kern, out_len); +if (!out_ptr) +return -EINVAL ; + +return ctx->type->hash (ctx->tfm, data_ptr, out_ptr, data_len); +} +#endif /* CONFIG_CRYPTO_HASH2 */ +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (crypt_init_kfunc_btf_ids)
@@ -359,6 +416,9 @@ static const struct btf_kfunc_id_set crypt_init_kfunc_set = {BTF_KFUNCS_START (crypt_kfunc_btf_ids)BTF_ID_FLAGS (func ,bpf_crypto_decrypt ,KF_RCU )BTF_ID_FLAGS (func, bpf_crypto_encrypt,KF_RCU ) +#if IS_ENABLED(CONFIG_CRYPTO_HASH2) +BTF_ID_FLAGS (func, bpf_crypto_hash,KF_RCU ) +#endifBTF_KFUNCS_END (crypt_kfunc_btf_ids) static const struct btf_kfunc_id_set crypt_kfunc_set = {
@@ -383,6 +443,8 @@ static int __init crypto_kfunc_init(void) ret =register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &crypt_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_ACT , &crypt_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &crypt_kfunc_set); +/* Register for SYSCALL programs to enable testing and debugging */ + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &crypt_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &crypt_init_kfunc_set); return ret ?: register_btf_id_dtor_kfuncs(bpf_crypto_dtors,
Signature · 1 line
2.51.0
PATCH v4 4/6 selftests/bpf: Add tests for bpf_crypto_hash kfunc
In reply to Daniel HodgesAdd selftests to validate the bpf_crypto_hash works properly. The tests verify both correct functionality and proper error handling.
Test Data:
All tests use the well-known NIST test vector input "abc" and validate against the standardized expected outputs for each algorithm. This ensures the BPF kfunc wrappers correctly delegate to the kernel crypto library.
--- MAINTAINERS | 2 + tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/crypto_hash.c | 147 ++++++++++++++++++ .../selftests/bpf/progs/crypto_common.h | 7 + .../testing/selftests/bpf/progs/crypto_hash.c | 136 ++++++++++++++++ 5 files changed, 294 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_hash.c create mode 100644 tools/testing/selftests/bpf/progs/crypto_hash.c
MAINTAINERS +2 -0
@@ -4717,6 +4717,8 @@ F: crypto/bpf_crypto_sig.c F: crypto/bpf_crypto_skcipher.c F: include/linux/bpf_crypto.h F: kernel/bpf/crypto.c +F: tools/testing/selftests/bpf/prog_tests/crypto_hash.c +F: tools/testing/selftests/bpf/progs/crypto_hash.c BPF [DOCUMENTATION] (Related to Standardization) R: David Vernet <[email protected]>
tools/testing/selftests/bpf/config +2 -0
@@ -12,7 +12,9 @@ CONFIG_BPF_SYSCALL=y # CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set CONFIG_CGROUP_BPF=y CONFIG_CRYPTO_HMAC=y +CONFIG_CRYPTO_HASH2=y CONFIG_CRYPTO_SHA256=y +CONFIG_CRYPTO_SHA512=y CONFIG_CRYPTO_USER_API=y CONFIG_CRYPTO_USER_API_HASH=y CONFIG_CRYPTO_USER_API_SKCIPHER=y
tools/testing/selftests/bpf/prog_tests/crypto_hash.c new file +147 -0
147 lines not on this page · raw message
tools/testing/selftests/bpf/progs/crypto_common.h +7 -0
@@ -15,6 +15,13 @@ int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx, const struct bpf_dynptr *src, const struct bpf_dynptr *dst, const struct bpf_dynptr *iv) __ksym; int bpf_crypto_decrypt(struct bpf_crypto_ctx *ctx, const struct bpf_dynptr *src, const struct bpf_dynptr *dst, const struct bpf_dynptr *iv) __ksym; +int bpf_crypto_hash(struct bpf_crypto_ctx *ctx, const struct bpf_dynptr *data, + const struct bpf_dynptr *out) __ksym; +int bpf_ecdsa_verify(struct bpf_crypto_ctx *ctx, const struct bpf_dynptr *message, + const struct bpf_dynptr *signature) __ksym; +int bpf_ecdsa_keysize(struct bpf_crypto_ctx *ctx) __ksym; +int bpf_ecdsa_digestsize(struct bpf_crypto_ctx *ctx) __ksym; +int bpf_ecdsa_maxsize(struct bpf_crypto_ctx *ctx) __ksym;struct __crypto_ctx_value { struct bpf_crypto_ctx __kptr * ctx;
tools/testing/selftests/bpf/progs/crypto_hash.c new file +136 -0
136 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 5/6 bpf: Add ECDSA signature verification kfuncs
In reply to Daniel HodgesAdd support for ECDSA signature verification in BPF programs through the unified bpf_crypto_ctx API.
Changes:
- Add enum bpf_crypto_type_id for efficient type checking
- Update all crypto type modules to set type_id field
- Implement bpf_ecdsa_verify() for signature verification
- Add bpf_ecdsa_keysize(), bpf_ecdsa_digestsize(), bpf_ecdsa_maxsize() helper functions for querying context properties
- Add type_id checks in all ECDSA kfuncs for type safety
- Register ECDSA kfuncs for SCHED_CLS and XDP program types
ECDSA contexts are created using bpf_crypto_ctx_create() with type="sig" and appropriate algorithm (e.g., "p1363(ecdsa-nist-p256)"). The public key is passed via the key/key_len fields in bpf_crypto_params.
This enables BPF programs to perform cryptographic signature verification for use cases such as packet authentication and content validation.
--- crypto/bpf_crypto_shash.c | 1 + crypto/bpf_crypto_sig.c | 1 + crypto/bpf_crypto_skcipher.c | 1 + include/linux/bpf_crypto.h | 7 +++ kernel/bpf/crypto.c | 115 +++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+)
crypto/bpf_crypto_shash.c +1 -0
@@ -74,6 +74,7 @@ static const struct bpf_crypto_type bpf_crypto_shash_type = { .digestsize = bpf_crypto_shash_digestsize, .get_flags = bpf_crypto_shash_get_flags, .owner = THIS_MODULE, + .type_id = BPF_CRYPTO_TYPE_HASH, .name = "hash", };
crypto/bpf_crypto_sig.c +1 -0
@@ -38,6 +38,7 @@ static const struct bpf_crypto_type bpf_crypto_sig_type = { .get_flags = bpf_crypto_sig_get_flags, .setkey = bpf_crypto_sig_setkey, .owner = THIS_MODULE, + .type_id = BPF_CRYPTO_TYPE_SIG, .name = "sig", };
crypto/bpf_crypto_skcipher.c +1 -0
@@ -63,6 +63,7 @@ static const struct bpf_crypto_type bpf_crypto_lskcipher_type = { .statesize = bpf_crypto_lskcipher_statesize, .get_flags = bpf_crypto_lskcipher_get_flags, .owner = THIS_MODULE, + .type_id = BPF_CRYPTO_TYPE_SKCIPHER, .name = "skcipher", };
include/linux/bpf_crypto.h +7 -0
@@ -3,6 +3,12 @@ #ifndef _BPF_CRYPTO_H #define _BPF_CRYPTO_H +enum bpf_crypto_type_id { + BPF_CRYPTO_TYPE_SKCIPHER = 1, + BPF_CRYPTO_TYPE_HASH, + BPF_CRYPTO_TYPE_SIG, +}; + struct bpf_crypto_type { void *(*alloc_tfm)(const char *algo); void (*free_tfm)(void *tfm);
@@ -17,6 +23,7 @@ struct bpf_crypto_type {unsigned int (*digestsize)(void *tfm);u32 (*get_flags)(void *tfm);struct module *owner; +enum bpf_crypto_type_id type_id;char name[14 ]; };
kernel/bpf/crypto.c +115 -0
145 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 1/6 crypto: Add BPF hash algorithm type registration module
In reply to Daniel HodgesAdd bpf_crypto_shash module that registers a hash type with the BPF crypto infrastructure, enabling BPF programs to access kernel hash algorithms through a unified interface.
Update the bpf_crypto_type interface with hash-specific callbacks:
- alloc_tfm: Allocates crypto_shash context with proper descriptor size
- free_tfm: Releases hash transform and context memory
- has_algo: Checks algorithm availability via crypto_has_shash()
- hash: Performs single-shot hashing via crypto_shash_digest()
- digestsize: Returns the output size for the hash algorithm
- get_flags: Exposes transform flags to BPF programs
Update bpf_shash_ctx to contain crypto_shash transform and shash_desc descriptor to accommodate algorithm-specific descriptor requirements.
--- MAINTAINERS | 1 + crypto/Makefile | 3 ++ crypto/bpf_crypto_shash.c | 95 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 crypto/bpf_crypto_shash.c
MAINTAINERS +1 -0
@@ -4712,6 +4712,7 @@ BPF [CRYPTO] M: Vadim Fedorenko <[email protected]> L: [email protected] S: Maintained +F: crypto/bpf_crypto_shash.c F: crypto/bpf_crypto_skcipher.c F: include/linux/bpf_crypto.h F: kernel/bpf/crypto.c
crypto/Makefile +3 -0
@@ -30,6 +30,9 @@ obj-$(CONFIG_CRYPTO_ECHAINIV) += echainiv.o crypto_hash-y += ahash.o crypto_hash-y += shash.o obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o +ifeq ($(CONFIG_BPF_SYSCALL),y) +obj-$(CONFIG_CRYPTO_HASH2) += bpf_crypto_shash.o +endif obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o obj-$(CONFIG_CRYPTO_SIG2) += sig.o
crypto/bpf_crypto_shash.c new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.51.0
Vadim Fedorenko
maintainerRe: [PATCH bpf-next v4 1/6] crypto: Add BPF hash algorithm type registration module
Quoted 22 lines · Daniel Hodges
Add
bpf_crypto_shashmodule that registers a hash type with the BPF crypto infrastructure, enabling BPF programs to access kernel hash algorithms through a unified interface.Update the
bpf_crypto_typeinterface with hash-specific callbacks:
-alloc_tfm: Allocatescrypto_shashcontext with proper descriptor size
-free_tfm: Releases hash transform and context memory
-has_algo: Checks algorithm availability viacrypto_has_shash()
- hash: Performs single-shot hashing viacrypto_shash_digest()
- digestsize: Returns the output size for the hash algorithm
-get_flags: Exposes transform flags to BPF programsUpdate
bpf_shash_ctxto containcrypto_shashtransform andshash_descdescriptor to accommodate algorithm-specific descriptor requirements.Signed-off-by Daniel Hodges <[email protected]>--- MAINTAINERS | 1 + crypto/Makefile | 3 ++ crypto/bpf_crypto_shash.c | 95 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+)create mode 100644
crypto/bpf_crypto_shash.c
[...]
crypto/bpf_crypto_shash.c line 73
Quoted 5 lines
crypto/bpf_crypto_shash.c new file +5
-0+static const struct bpf_crypto_type bpf_crypto_shash_type = { + .alloc_tfm = bpf_crypto_shash_alloc_tfm, + .free_tfm = bpf_crypto_shash_free_tfm, + .has_algo = bpf_crypto_shash_has_algo, + .hash = bpf_crypto_shash_hash,
I don't see a definition of hash() callback in struct bpf_crypto_type
Quoted 5 lines
crypto/bpf_crypto_shash.c new file +5
-0+ .digestsize = bpf_crypto_shash_digestsize, + .get_flags = bpf_crypto_shash_get_flags, + .owner = THIS_MODULE, + .name = "hash", +};
Vadim Fedorenko
maintainerRe: [PATCH bpf-next v4 2/6] crypto: Add BPF signature algorithm type registration module
In reply to Daniel HodgesQuoted 14 lines · Daniel Hodges
Add a new
bpf_crypto_sigmodule that registers signature verification algorithms with the BPF crypto type system. This enables signature operations (like ECDSA) to use the unifiedbpf_crypto_ctxstructure instead of requiring separate context types.The module provides:
- alloc_tfm/free_tfm forcrypto_sigtransform lifecycle
-has_algoto check algorithm availability
-get_flagsfor crypto API flagsThis allows ECDSA and other signature verification operations to integrate with the existing BPF crypto infrastructure.
Signed-off-by Daniel Hodges <[email protected]>
[...]
crypto/bpf_crypto_sig.c line 32
Quoted 4 lines
crypto/bpf_crypto_sig.c new file +4
-0+static int bpf_crypto_sig_setkey (void *tfm,const u8 *key,unsigned int keylen) +{ +return crypto_sig_set_pubkey (tfm, key, keylen); +}
That effectively means that signature verification only is provided for BPF programs? Do we plan to extend API to sign a buffer?
crypto/bpf_crypto_sig.c line 42
Quoted 10 lines
crypto/bpf_crypto_sig.c new file +10
-0+ +static const struct bpf_crypto_type bpf_crypto_sig_type = { + .alloc_tfm = bpf_crypto_sig_alloc_tfm, + .free_tfm = bpf_crypto_sig_free_tfm, + .has_algo = bpf_crypto_sig_has_algo, + .get_flags = bpf_crypto_sig_get_flags, + .setkey = bpf_crypto_sig_setkey, + .owner =THIS_MODULE , + .name ="sig" , +};
I think we have to introduce verify() callback here.
Vadim Fedorenko
maintainerRe: [PATCH bpf-next v4 3/6] bpf: Add hash kfunc for cryptographic hashing
In reply to Daniel HodgesQuoted 3 lines · Daniel Hodges
Extend
bpf_crypto_typestructure with hash operations:
-hash(): Performs hashing operation
-digestsize(): Returns hash output size
well, as I've already mentioned, none of them are actually introduced in the patchset.
Quoted 17 lines
Update
bpf_crypto_ctx_create()to support keyless operations:
- Hash algorithms don't require keys, unlike ciphers
- Only validates key presence iftype->setkeyis defined
- Conditionally sets IV/state length for cipher operations onlyAdd
bpf_crypto_hash()kfunc that works with any hash algorithm registered in the kernel's crypto API through the BPF crypto type system. This enables BPF programs to compute cryptographic hashes for use cases such as content verification, integrity checking, and data authentication.Signed-off-by Daniel Hodges <[email protected]>--- kernel/bpf/crypto.c | 78 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-)
Vadim Fedorenko
maintainerRe: [PATCH bpf-next v4 5/6] bpf: Add ECDSA signature verification kfuncs
In reply to Daniel HodgesQuoted 220 lines · Daniel Hodges
Add support for ECDSA signature verification in BPF programs through the unified
bpf_crypto_ctxAPI.Changes:
- Add enumbpf_crypto_type_idfor efficient type checking
- Update all crypto type modules to settype_idfield
- Implementbpf_ecdsa_verify()for signature verification
- Addbpf_ecdsa_keysize(),bpf_ecdsa_digestsize(),bpf_ecdsa_maxsize()helper functions for querying context properties
- Addtype_idchecks in all ECDSA kfuncs for type safety
- Register ECDSA kfuncs forSCHED_CLSand XDP program typesECDSA contexts are created using
bpf_crypto_ctx_create()with type="sig" and appropriate algorithm (e.g., "p1363(ecdsa-nist-p256)"). The public key is passed via the key/key_len fields inbpf_crypto_params.This enables BPF programs to perform cryptographic signature verification for use cases such as packet authentication and content validation.
Signed-off-by Daniel Hodges <[email protected]>--- crypto/bpf_crypto_shash.c | 1 + crypto/bpf_crypto_sig.c | 1 + crypto/bpf_crypto_skcipher.c | 1 + include/linux/bpf_crypto.h | 7 +++ kernel/bpf/crypto.c | 115 +++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+)crypto/bpf_crypto_shash.c +1
-0@@ -74,6 +74,7 @@ static const struct bpf_crypto_type bpf_crypto_shash_type = { .digestsize = bpf_crypto_shash_digestsize, .get_flags = bpf_crypto_shash_get_flags, .owner = THIS_MODULE, + .type_id = BPF_CRYPTO_TYPE_HASH, .name = "hash", };crypto/bpf_crypto_sig.c +1
-0@@ -38,6 +38,7 @@ static const struct bpf_crypto_type bpf_crypto_sig_type = { .get_flags = bpf_crypto_sig_get_flags, .setkey = bpf_crypto_sig_setkey, .owner = THIS_MODULE, + .type_id = BPF_CRYPTO_TYPE_SIG, .name = "sig", };crypto/bpf_crypto_skcipher.c +1
-0@@ -63,6 +63,7 @@ static const struct bpf_crypto_type bpf_crypto_lskcipher_type = { .statesize = bpf_crypto_lskcipher_statesize, .get_flags = bpf_crypto_lskcipher_get_flags, .owner = THIS_MODULE, + .type_id = BPF_CRYPTO_TYPE_SKCIPHER, .name = "skcipher", };include/linux/bpf_crypto.h +7
-0@@ -3,6 +3,12 @@ #ifndef _BPF_CRYPTO_H #define _BPF_CRYPTO_H +enum bpf_crypto_type_id { + BPF_CRYPTO_TYPE_SKCIPHER = 1, + BPF_CRYPTO_TYPE_HASH, + BPF_CRYPTO_TYPE_SIG, +}; + struct bpf_crypto_type { void *(*alloc_tfm)(const char *algo); void (*free_tfm)(void *tfm);@@ -17,6 +23,7 @@ struct bpf_crypto_type {unsigned int (*digestsize)(void *tfm);u32 (*get_flags)(void *tfm);struct module *owner; +enum bpf_crypto_type_id type_id;char name[14 ]; };kernel/bpf/crypto.c +105
-0@@ -9,6 +9,7 @@#include <linux/scatterlist.h> #include <linux/skbuff.h> #include <crypto/skcipher.h> +#include <crypto/sig.h> struct bpf_crypto_type_list {const struct bpf_crypto_type *type;@@ -57,6 +58,7 @@ struct bpf_crypto_ctx {refcount_t usage; }; +int bpf_crypto_register_type (const struct bpf_crypto_type *type) {struct bpf_crypto_type_list *node;@@ -400,6 +402,109 @@ __bpf_kfunc int bpf_crypto_hash(struct bpf_crypto_ctx *ctx, }#endif /* CONFIG_CRYPTO_HASH2 */ +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) +/** +* bpf_ecdsa_verify() - Verify ECDSA signature using pre-allocated context +* @ctx: ECDSA context created by bpf_crypto_ctx_create() with type "sig" +* @message: bpf_dynptr to the message hash to verify. Must be a trusted pointer. +* @signature: bpf_dynptr to the ECDSA signature in r || s format. Must be a trusted pointer. +* Must be 64 bytes for P-256, 96 for P-384, 132 for P-521 +* +* Verifies an ECDSA signature using a pre-allocated context. This function +* does not allocate memory and can be used in non-sleepable BPF programs. +* Uses bpf_dynptr to ensure safe memory access without risk of page faults. +*/ +__bpf_kfunc int bpf_ecdsa_verify(struct bpf_crypto_ctx *ctx, +const struct bpf_dynptr *message, +const struct bpf_dynptr *signature) +{ +const struct bpf_dynptr_kern *msg_kern = (struct bpf_dynptr_kern *)message; +const struct bpf_dynptr_kern *sig_kern = (struct bpf_dynptr_kern *)signature; +struct crypto_sig *sig_tfm; +const u8 *msg_ptr, *sig_ptr; +u32 msg_len, sig_len; + +if (!ctx) +return -EINVAL ; + +if (ctx->type->type_id !=BPF_CRYPTO_TYPE_SIG ) +return -EINVAL ; + + msg_len =__bpf_dynptr_size (msg_kern); + sig_len =__bpf_dynptr_size (sig_kern); + +if (msg_len ==0 || sig_len ==0 ) +return -EINVAL ; + + msg_ptr =__bpf_dynptr_data (msg_kern, msg_len); +if (!msg_ptr) +return -EINVAL ; + + sig_ptr =__bpf_dynptr_data (sig_kern, sig_len); +if (!sig_ptr) +return -EINVAL ; + + sig_tfm = (struct crypto_sig *)ctx->tfm; +return crypto_sig_verify (sig_tfm, sig_ptr, sig_len, msg_ptr, msg_len); +} + +/** +* bpf_ecdsa_keysize() - Get the key size for ECDSA context +* @ctx: ECDSA context +* +* Returns: Key size in bits, or negative error code on failure +*/ +__bpf_kfunc int bpf_ecdsa_keysize(struct bpf_crypto_ctx *ctx) +{ +struct crypto_sig *sig_tfm; + +if (!ctx) +return -EINVAL ; + +if (ctx->type->type_id !=BPF_CRYPTO_TYPE_SIG ) +return -EINVAL ; + + sig_tfm = (struct crypto_sig *)ctx->tfm; +return crypto_sig_keysize (sig_tfm); +} + +/** +* bpf_ecdsa_digestsize() - Get the maximum digest size for ECDSA context +* @ctx: ECDSA context +*/ +__bpf_kfunc int bpf_ecdsa_digestsize(struct bpf_crypto_ctx *ctx) +{ +struct crypto_sig *sig_tfm; + +if (!ctx) +return -EINVAL ; + +if (ctx->type->type_id !=BPF_CRYPTO_TYPE_SIG ) +return -EINVAL ; + + sig_tfm = (struct crypto_sig *)ctx->tfm; +return crypto_sig_digestsize (sig_tfm); +} + +/** +* bpf_ecdsa_maxsize() - Get the maximum signature size for ECDSA context +* @ctx: ECDSA context +*/ +__bpf_kfunc int bpf_ecdsa_maxsize(struct bpf_crypto_ctx *ctx) +{ +struct crypto_sig *sig_tfm; + +if (!ctx) +return -EINVAL ; + +if (ctx->type->type_id !=BPF_CRYPTO_TYPE_SIG ) +return -EINVAL ; + + sig_tfm = (struct crypto_sig *)ctx->tfm; +return crypto_sig_maxsize (sig_tfm); +} +#endif /* CONFIG_CRYPTO_ECDSA */ +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (crypt_init_kfunc_btf_ids)
kernel/bpf/crypto.c BTF_ID_FLAGS(func, bpf_crypto_encrypt, KF_RCU) · line 531
Quoted 8 lines
kernel/bpf/crypto.c +5
-0@@ -419,6 +524,12 @@ BTF_ID_FLAGS(func, bpf_crypto_encrypt, KF_RCU)#if IS_ENABLED (CONFIG_CRYPTO_HASH2 ) BTF_ID_FLAGS(func, bpf_crypto_hash, KF_RCU)#endif +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) +BTF_ID_FLAGS(func, bpf_ecdsa_verify, KF_RCU) +BTF_ID_FLAGS(func, bpf_ecdsa_keysize, 0) +BTF_ID_FLAGS(func, bpf_ecdsa_digestsize, 0) +BTF_ID_FLAGS (func, bpf_ecdsa_maxsize,0 )
Why keysize/digestsize/maxsize are introduced as kfuncs? My understanding is that only bpf_sig_verify() has to be introduced. And no ECDSA in the name as it's one of the types of signature algos.
kernel/bpf/crypto.c static int __init crypto_kfunc_init(void) · line 564
Quoted 12 lines
kernel/bpf/crypto.c +5
-0+#endif BTF_KFUNCS_END (crypt_kfunc_btf_ids) static const struct btf_kfunc_id_set crypt_kfunc_set = {@@ -447,6 +558,10 @@ static int __init crypto_kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &crypt_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &crypt_init_kfunc_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , + &crypt_init_kfunc_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , + &crypt_init_kfunc_set);
I'm pretty sure it will fail because init functions have sleepable *alloc() calls in the implementation and cannot be called in XDP/SCHED context.
Quoted 3 lines
kernel/bpf/crypto.c +0
-0return ret ?:register_btf_id_dtor_kfuncs (bpf_crypto_dtors,ARRAY_SIZE (bpf_crypto_dtors),THIS_MODULE );
Vadim Fedorenko
maintainerRe: [PATCH bpf-next v4 5/6] bpf: Add ECDSA signature verification kfuncs
In reply to Daniel HodgesQuoted 21 lines · Daniel Hodges
Add support for ECDSA signature verification in BPF programs through the unified
bpf_crypto_ctxAPI.Changes:
- Add enumbpf_crypto_type_idfor efficient type checking
- Update all crypto type modules to settype_idfield
- Implementbpf_ecdsa_verify()for signature verification
- Addbpf_ecdsa_keysize(),bpf_ecdsa_digestsize(),bpf_ecdsa_maxsize()helper functions for querying context properties
- Addtype_idchecks in all ECDSA kfuncs for type safety
- Register ECDSA kfuncs forSCHED_CLSand XDP program typesECDSA contexts are created using
bpf_crypto_ctx_create()with type="sig" and appropriate algorithm (e.g., "p1363(ecdsa-nist-p256)"). The public key is passed via the key/key_len fields inbpf_crypto_params.This enables BPF programs to perform cryptographic signature verification for use cases such as packet authentication and content validation.
Signed-off-by Daniel Hodges <[email protected]>
kernel/bpf/crypto.c struct bpf_crypto_ctx { · line 64
Quoted 7 lines
kernel/bpf/crypto.c +1
-0@@ -57,6 +58,7 @@ struct bpf_crypto_ctx {refcount_t usage; }; +int bpf_crypto_register_type (const struct bpf_crypto_type *type) {struct bpf_crypto_type_list *node;
This chunk is extra empty line - no need for it