Add cryptographic hash and signature verification kfuncs to BPF
Subsystems (6)
v2 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: 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 2: bpf: Add SHA hash kfunc for cryptographic hashing
- Adds bpf_crypto_hash() kfunc for SHA-256/384/512
- Extends bpf_crypto_type structure with hash operations
- Updates bpf_crypto_ctx_create() to support keyless operations
- Protected by CONFIG_CRYPTO_HASH2 guards
- Uses kernel's crypto library implementations
Patch 3: 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
Patch 4: 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 5: 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
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
Test Results
============
All tests pass on x86_64 for both crypto_hash and ecdsa_verify test suites.
Daniel Hodges (5):
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 | 94 ++++++ include/linux/bpf_crypto.h | 2 + kernel/bpf/crypto.c | 306 ++++++++++++++++++++- tools/testing/selftests/bpf/config | 2 + .../testing/selftests/bpf/prog_tests/crypto_hash.c | 158 +++++++++++ .../selftests/bpf/prog_tests/ecdsa_verify.c | 74 +++++ tools/testing/selftests/bpf/progs/crypto_hash.c | 141 ++++++++++ tools/testing/selftests/bpf/progs/ecdsa_verify.c | 159 +++++++++++ 9 files changed, 931 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 v2 1/5 crypto: Add BPF hash algorithm type registration module
Add 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 | 94 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 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 +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.51.0
[email protected]
AI reviewRe: [PATCH bpf-next v2 1/5] crypto: Add BPF hash algorithm type registration module
Does this code compile? The struct bpf_crypto_type definition in include/linux/bpf_crypto.h doesn't have .hash or .digestsize fields at this commit. The commit message says "Update the…
Quoted 4 lines
crypto/Makefile +0
-0
[ ... ]
Quoted 5 lines
crypto/bpf_crypto_shash.c new file +0
-0
[ ... ]
crypto/bpf_crypto_shash.c line 77
Quoted 10 lines
crypto/bpf_crypto_shash.c new file +10
-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" , +};
Does this code compile? The struct bpf_crypto_type definition in include/linux/bpf_crypto.h doesn't have .hash or .digestsize fields at this commit. The commit message says "Update the bpf_crypto_type interface with hash-specific callbacks" but the interface wasn't actually updated in this commit.
Looking forward in the git history, commit 51e5f5be27dc adds these fields to the interface. Should that commit come before this one for bisectability?
---
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/19971392632
Song Liu
reviewerRe: [PATCH bpf-next v2 1/5] crypto: Add BPF hash algorithm type registration module
In reply to Daniel HodgesQuoted 43 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 | 94 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 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 +0
-0
crypto/bpf_crypto_shash.c line 10
Quoted 10 lines
crypto/bpf_crypto_shash.c new file +10
-0@@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-only +#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; +};
Instead of adding bpf_shash_ctx and bpf_ecdsa_ctx, can we extend bpf_crypto_ctx to cover all hash and ECDSA? bpf_crypto_ctx has a const pointer to bpf_crypto_type, so this should be possible?
Thanks,
Song
Re: [PATCH bpf-next v2 1/5] crypto: Add BPF hash algorithm type registration module
Quoted 59 lines · Song Liu
Quoted 43 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 | 94 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 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 +0
-0crypto/bpf_crypto_shash.c line 10
Quoted 10 lines
crypto/bpf_crypto_shash.c new file +10
-0@@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-only +#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; +};Instead of adding
bpf_shash_ctxandbpf_ecdsa_ctx, can we extendbpf_crypto_ctxto cover all hash and ECDSA?bpf_crypto_ctxhas a const pointer tobpf_crypto_type, so this should be possible?
Quoted 2 lines
Thanks,
Song
Sounds good!
PATCH v2 2/5 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.
--- include/linux/bpf_crypto.h | 2 + kernel/bpf/crypto.c | 76 ++++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-)
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 ];
kernel/bpf/crypto.c +68 -8
109 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH bpf-next v2 2/5] bpf: Add SHA hash kfunc for cryptographic hashing
Quoted 100 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.Signed-off-by Daniel Hodges <[email protected]>--- include/linux/bpf_crypto.h | 2 + kernel/bpf/crypto.c | 76 ++++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-)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 ];kernel/bpf/crypto.c +31
-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;
kernel/bpf/crypto.c __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx, · line 378
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+const struct bpf_dynptr_kern *out_kern = (struct bpf_dynptr_kern *)out; +u32 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);
__bpf_dynptr_size() returns u64, as well as __bpf_dynptr_data_rw() takes u64 as length parameter, it may be worth using that type for data_len and out_len.
Quoted 44 lines
kernel/bpf/crypto.c +27
-0+ +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,
Re: [PATCH bpf-next v2 2/5] bpf: Add SHA hash kfunc for cryptographic hashing
Quoted 99 lines · Mykyta Yatsenko
Quoted 98 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.Signed-off-by Daniel Hodges <[email protected]>--- include/linux/bpf_crypto.h | 2 + kernel/bpf/crypto.c | 76 ++++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-)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 ];kernel/bpf/crypto.c +36
-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; +u32 data_len, out_len; +const u8 *data_ptr; +u8 *out_ptr; +
Quoted 8 lines
kernel/bpf/crypto.c __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx, · line 378
Quoted 5 lines
kernel/bpf/crypto.c +5
-0+if (!ctx->type->hash) +return -EOPNOTSUPP ; + + data_len =__bpf_dynptr_size (data_kern); + out_len =__bpf_dynptr_size (out_kern);
__bpf_dynptr_size()returns u64, as well as__bpf_dynptr_data_rw()takes u64 as length parameter, it may be worth using that type fordata_lenandout_len.
Yeah, that makes sense, will queue that up.
PATCH v2 3/5 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 | 158 ++++++++++++++++++ .../testing/selftests/bpf/progs/crypto_hash.c | 141 ++++++++++++++++ 3 files changed, 301 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 +158 -0
158 lines not on this page · raw message
tools/testing/selftests/bpf/progs/crypto_hash.c new file +141 -0
141 lines not on this page · raw message
Signature · 1 line
2.51.0
Song Liu
reviewerRe: [PATCH bpf-next v2 3/5] selftests/bpf: Add tests for bpf_crypto_hash kfunc
Quoted 38 lines · Daniel Hodges
Add selftests to validate the
bpf_crypto_hashworks 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.Signed-off-by Daniel Hodges <[email protected]>--- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/crypto_hash.c | 158 ++++++++++++++++++ .../testing/selftests/bpf/progs/crypto_hash.c | 141 ++++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_hash.c create mode 100644 tools/testing/selftests/bpf/progs/crypto_hash.ctools/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=ytools/testing/selftests/bpf/prog_tests/crypto_hash.c new file +0
-0
tools/testing/selftests/bpf/prog_tests/crypto_hash.c line 1
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/crypto_hash.c new file +1
-0@@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0
Maybe add Copyright here?
Quoted 52 lines
tools/testing/selftests/bpf/prog_tests/crypto_hash.c new file +52
-0+ +#include <test_progs.h> +#include <errno.h> +#include "crypto_hash.skel.h" + +/* NIST test vectors for SHA-256("abc") */ +static const unsigned char expected_sha256[32 ] = { +0xba ,0x78 ,0x16 ,0xbf ,0x8f ,0x01 ,0xcf ,0xea , +0x41 ,0x41 ,0x40 ,0xde ,0x5d ,0xae ,0x22 ,0x23 , +0xb0 ,0x03 ,0x61 ,0xa3 ,0x96 ,0x17 ,0x7a ,0x9c , +0xb4 ,0x10 ,0xff ,0x61 ,0xf2 ,0x00 ,0x15 ,0xad +}; + +/* NIST test vectors for SHA-384("abc") */ +static const unsigned char expected_sha384[48 ] = { +0xcb ,0x00 ,0x75 ,0x3f ,0x45 ,0xa3 ,0x5e ,0x8b , +0xb5 ,0xa0 ,0x3d ,0x69 ,0x9a ,0xc6 ,0x50 ,0x07 , +0x27 ,0x2c ,0x32 ,0xab ,0x0e ,0xde ,0xd1 ,0x63 , +0x1a ,0x8b ,0x60 ,0x5a ,0x43 ,0xff ,0x5b ,0xed , +0x80 ,0x86 ,0x07 ,0x2b ,0xa1 ,0xe7 ,0xcc ,0x23 , +0x58 ,0xba ,0xec ,0xa1 ,0x34 ,0xc8 ,0x25 ,0xa7 +}; + +/* NIST test vectors for SHA-512("abc") */ +static const unsigned char expected_sha512[64 ] = { +0xdd ,0xaf ,0x35 ,0xa1 ,0x93 ,0x61 ,0x7a ,0xba , +0xcc ,0x41 ,0x73 ,0x49 ,0xae ,0x20 ,0x41 ,0x31 , +0x12 ,0xe6 ,0xfa ,0x4e ,0x89 ,0xa9 ,0x7e ,0xa2 , +0x0a ,0x9e ,0xee ,0xe6 ,0x4b ,0x55 ,0xd3 ,0x9a , +0x21 ,0x92 ,0x99 ,0x2a ,0x27 ,0x4f ,0xc1 ,0xa8 , +0x36 ,0xba ,0x3c ,0x23 ,0xa3 ,0xfe ,0xeb ,0xbd , +0x45 ,0x4d ,0x44 ,0x23 ,0x64 ,0x3c ,0xe8 ,0x0e , +0x2a ,0x9a ,0xc9 ,0x4f ,0xa5 ,0x4c ,0xa4 ,0x9f +}; + +static void test_sha256_basic (void ) +{ +struct crypto_hash *skel; +int err, prog_fd; + +LIBBPF_OPTS (bpf_test_run_opts, topts); + + skel =crypto_hash__open_and_load (); +if (!skel) { +/* Skip if kfuncs not available (CONFIG_CRYPTO_HASH2 not set) */ +if (errno ==ENOENT || errno ==EINVAL ) { +test__skip (); +return ; + } +ASSERT_OK_PTR (skel,"crypto_hash__open_and_load" ); +return ; + }
tools/testing/selftests/bpf/prog_tests/crypto_hash.c line 63
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/crypto_hash.c new file +10
-0+ + prog_fd =bpf_program__fd (skel->progs.test_sha256); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"test_sha256" ); +ASSERT_EQ (skel->data->sha256_status,0 ,"sha256_status" ); +ASSERT_EQ (memcmp (skel->bss->sha256_output, expected_sha256,32 ),0 , +"sha256_output_match" ); + +crypto_hash__destroy (skel); +}
nit: We have quite some duplicated code here. Maybe try to reuse some of the code?
Thanks,
Song
PATCH v2 4/5 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
PATCH v2 5/5 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.
--- .../selftests/bpf/prog_tests/ecdsa_verify.c | 74 ++++++++ .../selftests/bpf/progs/ecdsa_verify.c | 159 ++++++++++++++++++ 2 files changed, 233 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/prog_tests/ecdsa_verify.c new file +74 -0
@@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "ecdsa_verify.skel.h" + +static void test_ecdsa_verify_valid_signature (void ) +{ +struct ecdsa_verify *skel; +int err, prog_fd; + +LIBBPF_OPTS (bpf_test_run_opts, topts); + + skel =ecdsa_verify__open_and_load (); +if (!ASSERT_OK_PTR (skel,"ecdsa_verify__open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.test_ecdsa_verify_valid); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"test_ecdsa_verify_valid" ); +ASSERT_EQ (skel->data->ctx_create_status,0 ,"ctx_create_status" ); +ASSERT_EQ (skel->data->verify_result,0 ,"verify_valid_signature" ); + +ecdsa_verify__destroy (skel); +} + +static void test_ecdsa_verify_invalid_signature (void ) +{ +struct ecdsa_verify *skel; +int err, prog_fd; + +LIBBPF_OPTS (bpf_test_run_opts, topts); + + skel =ecdsa_verify__open_and_load (); +if (!ASSERT_OK_PTR (skel,"ecdsa_verify__open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.test_ecdsa_verify_invalid); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"test_ecdsa_verify_invalid" ); +ASSERT_NEQ (skel->data->verify_invalid_result,0 ,"verify_invalid_signature_rejected" ); + +ecdsa_verify__destroy (skel); +} + +static void test_ecdsa_size_queries (void ) +{ +struct ecdsa_verify *skel; +int err, prog_fd; + +LIBBPF_OPTS (bpf_test_run_opts, topts); + + skel =ecdsa_verify__open_and_load (); +if (!ASSERT_OK_PTR (skel,"ecdsa_verify__open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.test_ecdsa_size_queries); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"test_ecdsa_size_queries" ); +ASSERT_EQ (skel->data->keysize_result,256 ,"keysize_p256" ); +ASSERT_EQ (skel->data->digestsize_result,64 ,"digestsize_p256" ); +ASSERT_EQ (skel->data->maxsize_result,64 ,"maxsize_p256" ); + +ecdsa_verify__destroy (skel); +} + +void test_ecdsa_verify (void ) +{ +if (test__start_subtest ("verify_valid_signature" )) +test_ecdsa_verify_valid_signature (); +if (test__start_subtest ("verify_invalid_signature" )) +test_ecdsa_verify_invalid_signature (); +if (test__start_subtest ("size_queries" )) +test_ecdsa_size_queries (); +}
tools/testing/selftests/bpf/progs/ecdsa_verify.c new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.51.0
Song Liu
reviewerRe: [PATCH bpf-next v2 5/5] selftests/bpf: Add tests for ECDSA signature verification kfuncs
Quoted 6 lines · Daniel Hodges
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.
Quoted 8 lines
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.
Signed-off-by Daniel Hodges <[email protected]>--- .../selftests/bpf/prog_tests/ecdsa_verify.c | 74 ++++++++ .../selftests/bpf/progs/ecdsa_verify.c | 159 ++++++++++++++++++
I think we should also add CONFIG_CRYPTO_ECDSA to
selftests/bpf/config?
Thanks,
Song