Add cryptographic hash and signature verification kfuncs to BPF
Subsystems (6)
v3 cover Add cryptographic hash and signature verification kfuncs to BPF
This series extends BPF's cryptographic capabilities by adding kfuncs for SHA hashing and ECDSA signature verification. These functions enable BPF programs to perform cryptographic operations for use cases such as content verification, integrity checking, and data authentication.
BPF programs increasingly need to verify data integrity and authenticity in networking, security, and observability contexts. While BPF already supports symmetric encryption/decryption, it lacks support for:
1. Cryptographic hashing - needed for content verification, fingerprinting, and preparing message digests for signature operations
2. Asymmetric signature verification - needed to verify signed data without requiring the signing key in the datapath
These capabilities enable use cases such as:
- Verifying signed network packets or application data in XDP/TC programs
- Implementing integrity checks in tracing and security monitoring
- Building zero-trust security models where BPF programs verify credentials
- Content-addressed storage and deduplication in BPF-based filesystems
Implementation:
The implementation follows BPF's existing crypto patterns:
1. Uses bpf_dynptr for safe memory access without page fault risks
2. Leverages the kernel's existing crypto library (lib/crypto/sha256.c and crypto/ecdsa.c) rather than reimplementing algorithms
3. Provides context-based API for ECDSA to enable key reuse and support multiple program types (syscall, XDP, TC)
4. Includes comprehensive selftests with NIST test vectors
Patch 1: bpf: Extend bpf_crypto_type with hash operations
- Adds hash operation callbacks to bpf_crypto_type structure
- Adds hash() and digestsize() function pointers
- Must come before crypto module to maintain bisectability
Patch 2: crypto: Add BPF hash algorithm type registration module
- Adds bpf_crypto_shash module in crypto/ subsystem
- Registers hash type with BPF crypto infrastructure
- Enables hash algorithm access through unified bpf_crypto_type interface
- Implements callbacks: alloc_tfm, free_tfm, hash, digestsize, get_flags
- Manages shash_desc lifecycle internally
Patch 3: bpf: Add SHA hash kfunc for cryptographic hashing
- Adds bpf_crypto_hash() kfunc for SHA-256/384/512
- Updates bpf_crypto_ctx_create() to support keyless operations
- Protected by CONFIG_CRYPTO_HASH2 guards
- Uses kernel's crypto library implementations
- Fixed u64 types for dynptr sizes to prevent truncation
Patch 4: selftests/bpf: Add tests for bpf_crypto_hash kfunc
- Tests basic functionality with NIST "abc" test vectors
- Validates error handling for invalid parameters (zero-length input)
- Ensures correct hash output for SHA-256, SHA-384, and SHA-512
- Adds CONFIG_CRYPTO_HASH2 and CONFIG_CRYPTO_SHA512 to selftest config
- Refactored test setup code to reduce duplication
Patch 5: bpf: Add ECDSA signature verification kfuncs
- Context-based API: bpf_ecdsa_ctx_create/acquire/release pattern
- Supports NIST curves (P-256, P-384, P-521)
- Adds bpf_ecdsa_verify() for signature verification
- Includes size query functions: keysize, digestsize, maxsize
- Enables use in non-sleepable contexts via pre-allocated contexts
- Uses crypto_sig API with p1363 format (r || s signatures)
Patch 6: selftests/bpf: Add tests for ECDSA signature verification
- Tests valid signature acceptance with RFC 6979 test vectors for P-256
- Tests invalid signature rejection
- Tests size query functions (keysize, digestsize, maxsize)
- Uses well-known NIST test vectors with "sample" message
- Adds CONFIG_CRYPTO_ECDSA to selftest config
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
Daniel Hodges (6):
bpf: Extend bpf_crypto_type with hash operations
crypto: Add BPF hash algorithm type registration module
bpf: Add SHA 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
crypto/Makefile | 3 + crypto/bpf_crypto_shash.c | 95 ++++++ include/linux/bpf_crypto.h | 2 + kernel/bpf/crypto.c | 306 +++++++++++++++++- tools/testing/selftests/bpf/config | 3 + .../selftests/bpf/prog_tests/crypto_hash.c | 147 +++++++++ .../selftests/bpf/prog_tests/ecdsa_verify.c | 75 +++++ .../testing/selftests/bpf/progs/crypto_hash.c | 142 ++++++++ .../selftests/bpf/progs/ecdsa_verify.c | 160 +++++++++ 9 files changed, 925 insertions(+), 8 deletions(-) create mode 100644 crypto/bpf_crypto_shash.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 v3 1/6 bpf: Extend bpf_crypto_type with hash operations
Add hash operation callbacks to bpf_crypto_type structure:
- hash(): Performs hashing operation on input data
- digestsize(): Returns the output size for the hash algorithm
These additions enable BPF programs to use cryptographic hash functions through the unified bpf_crypto_type interface, supporting use cases such as content verification, integrity checking, and data authentication.
--- include/linux/bpf_crypto.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/bpf_crypto.h +2 -0
@@ -11,8 +11,10 @@ struct bpf_crypto_type {int (*setauthsize)(void *tfm,unsigned int authsize);int (*encrypt)(void *tfm,const u8 *src,u8 *dst,unsigned int len,u8 *iv);int (*decrypt)(void *tfm,const u8 *src,u8 *dst,unsigned int len,u8 *iv); +int (*hash)(void *tfm,const u8 *data,u8 *out,unsigned int len);unsigned int (*ivsize)(void *tfm);unsigned int (*statesize)(void *tfm); +unsigned int (*digestsize)(void *tfm);u32 (*get_flags)(void *tfm);struct module *owner;char name[14 ];
Signature · 1 line
2.51.0
PATCH v3 2/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.
--- crypto/Makefile | 3 ++ crypto/bpf_crypto_shash.c | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 crypto/bpf_crypto_shash.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
[email protected]
AI reviewRe: [PATCH bpf-next v3 2/6] crypto: Add BPF hash algorithm type registration module
The bpf_crypto_type implementation leaves several function pointers as NULL (setkey, setauthsize, encrypt, decrypt, ivsize, statesize), which is appropriate for plain hash algorithms. However, this…
Quoted 6 lines
crypto/bpf_crypto_shash.c new file +0
-0@@ -0,0 +1,95 @@
[ ... ]
crypto/bpf_crypto_shash.c line 78
Quoted 11 lines
crypto/bpf_crypto_shash.c new file +11
-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, + .digestsize = bpf_crypto_shash_digestsize, + .get_flags = bpf_crypto_shash_get_flags, + .owner =THIS_MODULE , + .name ="hash" , +};
The bpf_crypto_type implementation leaves several function pointers as NULL (setkey, setauthsize, encrypt, decrypt, ivsize, statesize), which is appropriate for plain hash algorithms. However, this creates an interface contract mismatch with bpf_crypto_ctx_create() in kernel/bpf/crypto.c.
Looking at the caller bpf_crypto_ctx_create():
Line 174-177:
if (!params->key_len || params->key_len > sizeof(params->key)) {
*err = -EINVAL;
goto err_module_put;
}Can hash contexts be created? This check requires key_len to be non-zero, but plain hash algorithms like SHA256 don't use keys. This prevents creating hash contexts entirely.
Line 186-189:
ctx->tfm = type->alloc_tfm(params->algo);
if (IS_ERR(ctx->tfm)) {
*err = PTR_ERR(ctx->tfm);
goto err_free_tfm;Can bpf_crypto_shash_free_tfm handle ERR_PTR? When alloc_tfm fails and returns ERR_PTR, the code stores it in ctx->tfm then jumps to err_free_tfm which calls type->free_tfm(ctx->tfm). In bpf_crypto_shash_free_tfm(), the code dereferences ctx->tfm without checking for ERR_PTR, which will crash:
static void bpf_crypto_shash_free_tfm(void *tfm)
{
struct bpf_shash_ctx *ctx = tfm;
crypto_free_shash(ctx->tfm); /* dereferences ctx */
kfree(ctx);
} Line 198:
*err = type->setkey(ctx->tfm, params->key, params->key_len);Does this dereference NULL? The hash implementation doesn't provide setkey (it's NULL in bpf_crypto_shash_type), but this line calls it unconditionally.
Line 207:
ctx->siv_len = type->ivsize(ctx->tfm) + type->statesize(ctx->tfm);Do these dereference NULL? The hash implementation doesn't provide ivsize or statesize (both NULL), but this line calls them unconditionally.
Note: These issues are fixed in commit 14cc32f873e0 ("bpf: Add SHA hash kfunc for cryptographic hashing") which makes the setkey, ivsize, and statesize calls conditional, and fixes the ERR_PTR handling by changing the error path from "goto err_free_tfm" to "goto err_free_ctx".
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20015426139
Song Liu
reviewerRe: [PATCH bpf-next v3 2/6] crypto: Add BPF hash algorithm type registration module
In reply to Daniel HodgesQuoted 65 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]>--- crypto/Makefile | 3 ++ crypto/bpf_crypto_shash.c | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 crypto/bpf_crypto_shash.ccrypto/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.ocrypto/bpf_crypto_shash.c new file +22
-0@@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <linux/types.h> +#include <linux/module.h> +#include <linux/bpf_crypto.h> +#include <crypto/hash.h> + +struct bpf_shash_ctx { +struct crypto_shash *tfm; +struct shash_desc desc; +}; + +static void *bpf_crypto_shash_alloc_tfm (const char *algo) +{ +struct bpf_shash_ctx *ctx; +struct crypto_shash *tfm; + + tfm =crypto_alloc_shash (algo,0 ,0 ); +if (IS_ERR (tfm)) +return tfm; + + ctx =kzalloc (sizeof (*ctx) +crypto_shash_descsize (tfm),GFP_KERNEL );
crypto/bpf_crypto_shash.c line 32
Quoted 10 lines
crypto/bpf_crypto_shash.c new file +10
-0+if (!ctx) { +crypto_free_shash (tfm); +return ERR_PTR (-ENOMEM ); + } + + ctx->tfm = tfm; + ctx->desc.tfm = tfm; + +return ctx; +}
What if we let bpf_crypto_shash_alloc_tfm() return a "struct shash_desc"? shash_desc->tfm is already struct crypto_shash. This way, we don't need bpf_shash_ctx any more. Would this work?
Thanks,
Song
Re: [PATCH bpf-next v3 2/6] crypto: Add BPF hash algorithm type registration module
Quoted 81 lines · Song Liu
Quoted 65 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]>--- crypto/Makefile | 3 ++ crypto/bpf_crypto_shash.c | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 crypto/bpf_crypto_shash.ccrypto/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.ocrypto/bpf_crypto_shash.c new file +22
-0@@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <linux/types.h> +#include <linux/module.h> +#include <linux/bpf_crypto.h> +#include <crypto/hash.h> + +struct bpf_shash_ctx { +struct crypto_shash *tfm; +struct shash_desc desc; +}; + +static void *bpf_crypto_shash_alloc_tfm (const char *algo) +{ +struct bpf_shash_ctx *ctx; +struct crypto_shash *tfm; + + tfm =crypto_alloc_shash (algo,0 ,0 ); +if (IS_ERR (tfm)) +return tfm; + + ctx =kzalloc (sizeof (*ctx) +crypto_shash_descsize (tfm),GFP_KERNEL );crypto/bpf_crypto_shash.c line 32
Quoted 10 lines
crypto/bpf_crypto_shash.c new file +10
-0+if (!ctx) { +crypto_free_shash (tfm); +return ERR_PTR (-ENOMEM ); + } + + ctx->tfm = tfm; + ctx->desc.tfm = tfm; + +return ctx; +}What if we let
bpf_crypto_shash_alloc_tfm()return a "structshash_desc"?shash_desc->tfmis already structcrypto_shash. This way, we don't needbpf_shash_ctxany more. Would this work?
Quoted 2 lines
Thanks,
Song
Yeah, that's much cleaner than the void * return.
-Daniel
PATCH v3 3/6 bpf: Add SHA 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 | 76 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-)
kernel/bpf/crypto.c +68 -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,54 @@ __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; +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 ) +return -EINVAL ; + +if (!ctx->type->digestsize) +return -EOPNOTSUPP ; + +unsigned int 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 +415,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 +442,7 @@ 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); + 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
Re: [PATCH bpf-next v3 3/6] bpf: Add SHA hash kfunc for cryptographic hashing
Quoted 15 lines · Daniel Hodges
Extend
bpf_crypto_typestructure with hash operations:
-hash(): Performs hashing operation
-digestsize(): Returns hash output sizeUpdate
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.
Quoted 4 lines
Signed-off-by Daniel Hodges <[email protected]>--- kernel/bpf/crypto.c | 76 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-)
Quoted 119 lines
kernel/bpf/crypto.c +68
-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,54 @@ __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; +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 ) +return -EINVAL ; + +if (!ctx->type->digestsize) +return -EOPNOTSUPP ; + +unsigned int 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 +415,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 +442,7 @@ 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); + 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,
Song Liu
reviewerRe: [PATCH bpf-next v3 3/6] bpf: Add SHA hash kfunc for cryptographic hashing
In reply to Daniel HodgesOn Sun, Dec 7, 2025 at 7:01 PM Daniel Hodges <[email protected]> wrote:
[...]
Quoted 4 lines
kernel/bpf/crypto.c +4
-0+ +if (!ctx->type->hash) +return -EOPNOTSUPP ; +
kernel/bpf/crypto.c __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx, · line 386
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+ data_len =__bpf_dynptr_size (data_kern); + out_len =__bpf_dynptr_size (out_kern); + +if (data_len ==0 ) +return -EINVAL ; + +if (!ctx->type->digestsize) +return -EOPNOTSUPP ; + +unsigned int digestsize = ctx->type->digestsize (ctx->tfm);
./scripts/checkpatch.pl will complain about this:
WARNING: Missing a blank line after declarations
#109: FILE: kernel/bpf/crypto.c:387:
+ unsigned int digestsize = ctx->type->digestsize(ctx->tfm);
+ if (out_len < digestsize)
Please run ./scripts/checkpatch.pl on all the patches. It also highlights some other issues, such as we need to update the MAINTAINERS file.
Also, we don't want variable declaration in the middle of a code block.
Quoted 3 lines
kernel/bpf/crypto.c +3
-0+if (out_len < digestsize) +return -EINVAL ; +
[...]
kernel/bpf/crypto.c static int __init crypto_kfunc_init(void) · line 445
Quoted 6 lines
static const struct
btf_kfunc_id_setcrypt_kfunc_set= {kernel/bpf/crypto.c +1
-0@@ -383,6 +442,7 @@ 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); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &crypt_kfunc_set);
This enables all kfuncs in crypt_kfunc_set for BPF_PROG_TYPE_SYSCALL. We need a clear explanation why this is needed.
Thanks,
Song
Re: [PATCH bpf-next v3 3/6] bpf: Add SHA hash kfunc for cryptographic hashing
Quoted 44 lines · Song Liu
On Sun, Dec 7, 2025 at 7:01 PM Daniel Hodges <[email protected]> wrote:
[...]Quoted 4 lines
kernel/bpf/crypto.c +4
-0+ +if (!ctx->type->hash) +return -EOPNOTSUPP ; +kernel/bpf/crypto.c __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx, · line 386
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+ data_len =__bpf_dynptr_size (data_kern); + out_len =__bpf_dynptr_size (out_kern); + +if (data_len ==0 ) +return -EINVAL ; + +if (!ctx->type->digestsize) +return -EOPNOTSUPP ; + +unsigned int digestsize = ctx->type->digestsize (ctx->tfm);
./scripts/checkpatch.plwill complain about this:WARNING: Missing a blank line after declarations
#109: FILE:kernel/bpf/crypto.c:387:kernel/bpf/crypto.c +2
-0+unsigned int digestsize = ctx->type->digestsize (ctx->tfm); +if (out_len < digestsize)Please run
./scripts/checkpatch.plon all the patches. It also highlights some other issues, such as we need to update theMAINTAINERSfile.Also, we don't want variable declaration in the middle of a code block.
Quoted 3 lines
kernel/bpf/crypto.c +3
-0+if (out_len < digestsize) +return -EINVAL ; +[...]
kernel/bpf/crypto.c static int __init crypto_kfunc_init(void) · line 445
Quoted 6 lines
static const struct
btf_kfunc_id_setcrypt_kfunc_set= {kernel/bpf/crypto.c +1
-0@@ -383,6 +442,7 @@ 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); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &crypt_kfunc_set);This enables all kfuncs in
crypt_kfunc_setforBPF_PROG_TYPE_SYSCALL. We need a clear explanation why this is needed.
Quoted 2 lines
Thanks,
Song
Sounds good! It might take me a day or so for the next set as I'm traveling.
PATCH v3 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.
--- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/crypto_hash.c | 147 ++++++++++++++++++ .../testing/selftests/bpf/progs/crypto_hash.c | 142 +++++++++++++++++ 3 files changed, 291 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_hash.c create mode 100644 tools/testing/selftests/bpf/progs/crypto_hash.c
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_hash.c new file +142 -0
142 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v3 5/6 bpf: Add ECDSA signature verification kfuncs
In reply to Daniel HodgesAdd context-based ECDSA signature verification kfuncs:
- bpf_ecdsa_ctx_create(): Creates reusable ECDSA context with public key
- bpf_ecdsa_verify(): Verifies signatures using the context
- bpf_ecdsa_ctx_acquire(): Increments context reference count
- bpf_ecdsa_ctx_release(): Releases context with RCU safety
The ECDSA implementation supports NIST curves (P-256, P-384, P-521) and uses the kernel's crypto_sig API. Public keys must be in uncompressed format (0x04 || x || y), and signatures are in r || s format.
--- kernel/bpf/crypto.c | 230 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+)
kernel/bpf/crypto.c +230 -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,21 @@ struct bpf_crypto_ctx {refcount_t usage; }; +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) +/** +* struct bpf_ecdsa_ctx - refcounted BPF ECDSA context structure +* @tfm: The crypto_sig transform for ECDSA operations +* @rcu: The RCU head used to free the context with RCU safety +* @usage: Object reference counter. When the refcount goes to 0, the +* memory is released with RCU safety. +*/ +struct bpf_ecdsa_ctx { +struct crypto_sig *tfm; +struct rcu_head rcu; +refcount_t usage; +}; +#endif +int bpf_crypto_register_type (const struct bpf_crypto_type *type) {struct bpf_crypto_type_list *node;
@@ -399,12 +415,206 @@ __bpf_kfunc int bpf_crypto_hash(struct bpf_crypto_ctx *ctx, }#endif /* CONFIG_CRYPTO_HASH2 */ +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) +/** +* bpf_ecdsa_ctx_create() - Create a BPF ECDSA verification context +* @algo_name: bpf_dynptr to the algorithm name (e.g., "p1363(ecdsa-nist-p256)") +* @public_key: bpf_dynptr to the public key in uncompressed format (0x04 || x || y) +* Must be 65 bytes for P-256, 97 for P-384, 133 for P-521 +* @err: Pointer to store error code on failure +* +* Creates an ECDSA verification context that can be reused for multiple +* signature verifications. This function uses GFP_KERNEL allocation and +* can only be called from sleepable BPF programs. Uses bpf_dynptr to ensure +* safe memory access without risk of page faults. +*/ +__bpf_kfunc struct bpf_ecdsa_ctx * +bpf_ecdsa_ctx_create (const struct bpf_dynptr *algo_name, +const struct bpf_dynptr *public_key,int *err) +{ +const struct bpf_dynptr_kern *algo_kern = (struct bpf_dynptr_kern *)algo_name; +const struct bpf_dynptr_kern *key_kern = (struct bpf_dynptr_kern *)public_key; +struct bpf_ecdsa_ctx *ctx; +const char *algo_ptr; +const u8 *key_ptr; +u32 algo_len, key_len; +char algo[64 ]; +int ret; + +if (!err) +return NULL ; + + algo_len =__bpf_dynptr_size (algo_kern); + key_len =__bpf_dynptr_size (key_kern); + +if (algo_len ==0 || algo_len >=sizeof (algo)) { + *err = -EINVAL ; +return NULL ; + } + +if (key_len <65 ) { + *err = -EINVAL ; +return NULL ; + } + + algo_ptr =__bpf_dynptr_data (algo_kern, algo_len); +if (!algo_ptr) { + *err = -EINVAL ; +return NULL ; + } + + key_ptr =__bpf_dynptr_data (key_kern, key_len); +if (!key_ptr) { + *err = -EINVAL ; +return NULL ; + } + +if (key_ptr[0 ] !=0x04 ) { + *err = -EINVAL ; +return NULL ; + } + +memcpy (algo, algo_ptr, algo_len); + algo[algo_len] ='\0' ; + + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) { + *err = -ENOMEM ; +return NULL ; + } + + ctx->tfm =crypto_alloc_sig (algo,0 ,0 ); +if (IS_ERR (ctx->tfm)) { + *err =PTR_ERR (ctx->tfm); +kfree (ctx); +return NULL ; + } + + ret =crypto_sig_set_pubkey (ctx->tfm, key_ptr, key_len); +if (ret) { + *err = ret; +crypto_free_sig (ctx->tfm); +kfree (ctx); +return NULL ; + } + +refcount_set (&ctx->usage,1 ); + *err =0 ; +return ctx; +} + +/** +* bpf_ecdsa_verify() - Verify ECDSA signature using pre-allocated context +* @ctx: ECDSA context created by bpf_ecdsa_ctx_create() +* @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_ecdsa_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; +const u8 *msg_ptr, *sig_ptr; +u32 msg_len, sig_len; + +if (!ctx) +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 ; + +return crypto_sig_verify (ctx->tfm, sig_ptr, sig_len, msg_ptr, msg_len); +} + +__bpf_kfunc struct bpf_ecdsa_ctx * +bpf_ecdsa_ctx_acquire (struct bpf_ecdsa_ctx *ctx) +{ +if (!refcount_inc_not_zero (&ctx->usage)) +return NULL ; +return ctx; +} + +static void ecdsa_free_cb (struct rcu_head *head) +{ + struct bpf_ecdsa_ctx *ctx = container_of(head, struct bpf_ecdsa_ctx, rcu); + +crypto_free_sig (ctx->tfm); +kfree (ctx); +} + +__bpf_kfunc void bpf_ecdsa_ctx_release(struct bpf_ecdsa_ctx *ctx) +{ +if (refcount_dec_and_test (&ctx->usage)) +call_rcu (&ctx->rcu, ecdsa_free_cb); +} + +/** +* 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_ecdsa_ctx *ctx) +{ +if (!ctx) +return -EINVAL ; + +return crypto_sig_keysize (ctx->tfm); +} + +/** +* bpf_ecdsa_digestsize() - Get the maximum digest size for ECDSA context +* @ctx: ECDSA context +*/ +__bpf_kfunc int bpf_ecdsa_digestsize(struct bpf_ecdsa_ctx *ctx) +{ +if (!ctx) +return -EINVAL ; + +return crypto_sig_digestsize (ctx->tfm); +} + +/** +* bpf_ecdsa_maxsize() - Get the maximum signature size for ECDSA context +* @ctx: ECDSA context +*/ +__bpf_kfunc int bpf_ecdsa_maxsize(struct bpf_ecdsa_ctx *ctx) +{ +if (!ctx) +return -EINVAL ; + +return crypto_sig_maxsize (ctx->tfm); +} +#endif /* CONFIG_CRYPTO_ECDSA */ +__bpf_kfunc_end_defs (); BTF_KFUNCS_START(crypt_init_kfunc_btf_ids) BTF_ID_FLAGS(func, bpf_crypto_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_crypto_ctx_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_crypto_ctx_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) +#if IS_ENABLED(CONFIG_CRYPTO_ECDSA) +BTF_ID_FLAGS(func, bpf_ecdsa_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_ecdsa_ctx_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_ecdsa_ctx_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) +#endif BTF_KFUNCS_END(crypt_init_kfunc_btf_ids) static const struct btf_kfunc_id_set crypt_init_kfunc_set = {
@@ -418,6 +628,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, 0) +BTF_ID_FLAGS(func, bpf_ecdsa_keysize, 0) +BTF_ID_FLAGS(func, bpf_ecdsa_digestsize, 0) +BTF_ID_FLAGS(func, bpf_ecdsa_maxsize, 0) +#endif BTF_KFUNCS_END (crypt_kfunc_btf_ids) static const struct btf_kfunc_id_set crypt_kfunc_set = {
@@ -428,6 +644,10 @@ static const struct btf_kfunc_id_set crypt_kfunc_set = {BTF_ID_LIST (bpf_crypto_dtor_ids) BTF_ID(struct, bpf_crypto_ctx)BTF_ID (func, bpf_crypto_ctx_release) +#if IS_ENABLED(CONFIG_CRYPTO_ECDSA) +BTF_ID (struct, bpf_ecdsa_ctx) +BTF_ID (func, bpf_ecdsa_ctx_release) +#endif static int __init crypto_kfunc_init(void) {
@@ -437,6 +657,12 @@ static int __init crypto_kfunc_init(void) .btf_id = bpf_crypto_dtor_ids[0], .kfunc_btf_id = bpf_crypto_dtor_ids[1] }, +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) + { + .btf_id = bpf_crypto_dtor_ids[2], + .kfunc_btf_id = bpf_crypto_dtor_ids[3] + }, +#endif }; ret =register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &crypt_kfunc_set);
@@ -445,6 +671,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);return ret ?:register_btf_id_dtor_kfuncs (bpf_crypto_dtors,ARRAY_SIZE (bpf_crypto_dtors),THIS_MODULE );
Signature · 1 line
2.51.0
Song Liu
reviewerRe: [PATCH bpf-next v3 5/6] bpf: Add ECDSA signature verification kfuncs
Quoted 36 lines · Daniel Hodges
Add context-based ECDSA signature verification kfuncs:
-bpf_ecdsa_ctx_create(): Creates reusable ECDSA context with public key
-bpf_ecdsa_verify(): Verifies signatures using the context
-bpf_ecdsa_ctx_acquire(): Increments context reference count
-bpf_ecdsa_ctx_release(): Releases context with RCU safetyThe ECDSA implementation supports NIST curves (P-256, P-384, P-521) and uses the kernel's
crypto_sigAPI. Public keys must be in uncompressed format (0x04|| x || y), and signatures are in r || s format.Signed-off-by Daniel Hodges <[email protected]>--- kernel/bpf/crypto.c | 230 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+)kernel/bpf/crypto.c +5
-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,21 @@ struct bpf_crypto_ctx {refcount_t usage; }; +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) +/** +* struct bpf_ecdsa_ctx - refcounted BPF ECDSA context structure +* @tfm: The crypto_sig transform for ECDSA operations
kernel/bpf/crypto.c struct bpf_crypto_ctx { · line 74
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+* @rcu: The RCU head used to free the context with RCU safety +* @usage: Object reference counter. When the refcount goes to 0, the +* memory is released with RCU safety. +*/ +struct bpf_ecdsa_ctx { +struct crypto_sig *tfm; +struct rcu_head rcu; +refcount_t usage; +}; +#endif
Can we use bpf_crypto_ctx for ECDSA?
Thanks,
Song
Re: [PATCH bpf-next v3 5/6] bpf: Add ECDSA signature verification kfuncs
Quoted 50 lines · Song Liu
Quoted 36 lines · Daniel Hodges
Add context-based ECDSA signature verification kfuncs:
-bpf_ecdsa_ctx_create(): Creates reusable ECDSA context with public key
-bpf_ecdsa_verify(): Verifies signatures using the context
-bpf_ecdsa_ctx_acquire(): Increments context reference count
-bpf_ecdsa_ctx_release(): Releases context with RCU safetyThe ECDSA implementation supports NIST curves (P-256, P-384, P-521) and uses the kernel's
crypto_sigAPI. Public keys must be in uncompressed format (0x04|| x || y), and signatures are in r || s format.Signed-off-by Daniel Hodges <[email protected]>--- kernel/bpf/crypto.c | 230 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+)kernel/bpf/crypto.c +5
-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,21 @@ struct bpf_crypto_ctx {refcount_t usage; }; +#if IS_ENABLED (CONFIG_CRYPTO_ECDSA ) +/** +* struct bpf_ecdsa_ctx - refcounted BPF ECDSA context structure +* @tfm: The crypto_sig transform for ECDSA operations kernel/bpf/crypto.c struct bpf_crypto_ctx { · line 74
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+* @rcu: The RCU head used to free the context with RCU safety +* @usage: Object reference counter. When the refcount goes to 0, the +* memory is released with RCU safety. +*/ +struct bpf_ecdsa_ctx { +struct crypto_sig *tfm; +struct rcu_head rcu; +refcount_t usage; +}; +#endif Can we use
bpf_crypto_ctxfor ECDSA?
Quoted 2 lines
Thanks,
Song
Sure thing!
-Daniel
PATCH v3 6/6 selftests/bpf: Add tests for ECDSA signature verification kfuncs
In reply to Daniel HodgesAdd 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.
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/ecdsa_verify.c | 75 ++++++++ .../selftests/bpf/progs/ecdsa_verify.c | 160 ++++++++++++++++++ 3 files changed, 236 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/ecdsa_verify.c create mode 100644 tools/testing/selftests/bpf/progs/ecdsa_verify.c
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 +160 -0
160 lines not on this page · raw message
Signature · 1 line
2.51.0