Skip to contentSkip to conversation

Add cryptographic hash and signature verification kfuncs to BPF

PATCH In review Daniel Hodges 4 participants 10 messages Feb 25 – Jun 10

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v8 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.

v1: https://lore.kernel.org/bpf/[email protected]/

v2: https://lore.kernel.org/bpf/[email protected]/
- 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: https://lore.kernel.org/bpf/[email protected]/
- 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: https://lore.kernel.org/bpf/[email protected]/
- 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

v5: https://lore.kernel.org/bpf/[email protected]/
- Fixed bisectability: moved bpf_crypto_type_id enum and type_id field introduction to the hash module commit, before it's used by hash kfunc
- Renamed kfuncs from bpf_ecdsa_* to bpf_sig_* since signature verification is not ECDSA-specific (Vadim)
- Added NULL checks in bpf_crypto_sig wrapper functions for optional digest_size and max_size callbacks to prevent NULL pointer dereference
- Added extra validation in bpf_sig_digestsize/bpf_sig_maxsize kfuncs to return -EOPNOTSUPP when underlying algorithm returns 0
- Renamed test files from ecdsa_verify to sig_verify for consistency

v6: https://lore.kernel.org/bpf/[email protected]/
- Fixed bisectability issue flagged by CI: squash hash module and hash kfunc commits so NULL checks in bpf_crypto_ctx_create() are present before the hash type is registered (bot+bpf-ci)
- Squash signature module and signature kfunc commits for the same reason
- This reduces the series from 7 to 4 commits while preserving all functionality
- Added type_id checks to bpf_crypto_encrypt() and bpf_crypto_decrypt() to prevent NULL pointer dereference when called with non-skcipher contexts

v7: https://lore.kernel.org/bpf/[email protected]/
- Moved crypto/bpf_crypto_skcipher.c type_id assignment from signature patch to hash patch for better logical organization
- Added missing CONFIG_CRYPTO_SIG, CONFIG_CRYPTO_SIG2, CONFIG_CRYPTO_ECDSA, and CONFIG_CRYPTO_ECRDSA to selftest config to ensure signature verification tests can build properly

v8:
- Fixed bpf_sig_keysize() documentation to correctly state return value is in bits, not bytes, matching the underlying crypto_sig_keysize() API (bot+bpf-ci)

Daniel Hodges (4):
bpf: Add hash kfunc for cryptographic hashing selftests/bpf: Add tests for bpf_crypto_hash kfunc
bpf: Add signature verification kfuncs selftests/bpf: Add tests for signature verification kfuncs

 MAINTAINERS                                   |   6 +
 crypto/Makefile                               |   6 +
 crypto/bpf_crypto_shash.c                     |  96 ++++++
 crypto/bpf_crypto_sig.c                       |  89 ++++++
 crypto/bpf_crypto_skcipher.c                  |   1 +
 include/linux/bpf_crypto.h                    |  13 +
 kernel/bpf/crypto.c                           | 210 ++++++++++++-
 tools/testing/selftests/bpf/config            |   6 +
 .../selftests/bpf/prog_tests/crypto_hash.c    | 210 +++++++++++++
 .../selftests/bpf/prog_tests/sig_verify.c     | 163 ++++++++++
 .../selftests/bpf/progs/crypto_common.h       |   8 +
 .../testing/selftests/bpf/progs/crypto_hash.c | 231 ++++++++++++++
 .../testing/selftests/bpf/progs/sig_verify.c  | 286 ++++++++++++++++++
 13 files changed, 1316 insertions(+), 9 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/sig_verify.c
 create mode 100644 tools/testing/selftests/bpf/progs/crypto_hash.c
 create mode 100644 tools/testing/selftests/bpf/progs/sig_verify.c

--
2.52.0

#1 lore raw

PATCH v8 1/4 bpf: Add hash kfunc for cryptographic hashing

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.

The bpf_crypto_type interface is extended 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

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.

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

---
 MAINTAINERS                  |  1 +
 crypto/Makefile              |  3 ++
 crypto/bpf_crypto_shash.c    | 96 ++++++++++++++++++++++++++++++++++++
 crypto/bpf_crypto_skcipher.c |  1 +
 include/linux/bpf_crypto.h   |  9 ++++
 kernel/bpf/crypto.c          | 93 ++++++++++++++++++++++++++++++----
 6 files changed, 194 insertions(+), 9 deletions(-)
 create mode 100644 crypto/bpf_crypto_shash.c
MAINTAINERS +1 -0
index 55af015174a5..6c78aae1bc6c 100644
@@ -4801,6 +4801,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
index 04e269117589..4db6a76d4c3e 100644
@@ -32,6 +32,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 +96 -0

96 lines not on this page · raw message

crypto/bpf_crypto_skcipher.c +1 -0
index a88798d3e8c8..79d310fbcc48 100644
@@ -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 +9 -0
index a41e71d4e2d9..cf2c66f9782b 100644
@@ -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);
@@ -11,10 +17,13 @@ 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;
+	enum bpf_crypto_type_id type_id;
 	char name[14];
 };
 
kernel/bpf/crypto.c +84 -9

141 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH bpf-next v8 1/4] bpf: Add hash kfunc for cryptographic hashing

bpf_crypto_shash_alloc_tfm() creates one bpf_shash_ctx per bpf_crypto_ctx, with a single embedded shash_desc. Multiple BPF programs can acquire references to the same bpf_crypto_ctx concurrently via…
Quoted 15 lines
crypto/bpf_crypto_shash.c new file +11 -0
@@ -0,0 +1,96 @@
+// 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;
+};

[ ... ]

Quoted 2 lines
crypto/bpf_crypto_shash.c new file +2 -0
+static void *bpf_crypto_shash_alloc_tfm(const char *algo)
+{

[ ... ]

Quoted 5 lines
crypto/bpf_crypto_shash.c new file +5 -0
+	ctx->tfm = tfm;
+	ctx->desc.tfm = tfm;
+
+	return ctx;
+}

[ ... ]

crypto/bpf_crypto_shash.c line 53

Quoted 7 lines
crypto/bpf_crypto_shash.c new file +7 -0
+static int bpf_crypto_shash_hash(void *tfm, const u8 *data, u8 *out,
+				 unsigned int len)
+{
+	struct bpf_shash_ctx *ctx = tfm;
+
+	return crypto_shash_digest(&ctx->desc, data, len, out);
+}

bpf_crypto_shash_alloc_tfm() creates one bpf_shash_ctx per bpf_crypto_ctx, with a single embedded shash_desc. Multiple BPF programs can acquire references to the same bpf_crypto_ctx concurrently via bpf_crypto_ctx_acquire() (which just does refcount_inc_not_zero), and the hash kfunc is marked KF_RCU which provides no mutual exclusion.

When two CPUs call bpf_crypto_hash() with the same context, both end up in crypto_shash_digest() using the same desc:

CPU0 CPU1
bpf_crypto_hash(ctx) bpf_crypto_hash(ctx)

    bpf_crypto_shash_hash()        bpf_crypto_shash_hash()
      crypto_shash_digest(desc)      crypto_shash_digest(desc)
        alg->digest(desc, ...)         alg->digest(desc, ...)
        // writes desc->__ctx          // writes desc->__ctx
        memset(desc->__ctx, 0)         memset(desc->__ctx, 0)

crypto_shash_op_and_zero() in crypto/shash.c writes to shash_desc_ctx(desc) via the algorithm callback and then zeroes it with memset. Both CPUs race on this buffer, producing silently corrupted hash output.

The existing encrypt/decrypt path does not have this problem because crypto_lskcipher_encrypt() treats the tfm as read-only (the key schedule is pre-computed) and the mutable IV state is passed per-call through the siv dynptr.

Would it make sense to allocate a shash_desc per-call on the stack, or use a per-cpu descriptor, to avoid this race?

Quoted 3 lines
kernel/bpf/crypto.c +0 -0

[ ... ]

Quoted 14 lines
kernel/bpf/crypto.c +14 -0
+#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)
+{

[ ... ]

kernel/bpf/crypto.c __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx, · line 420

Quoted 3 lines
kernel/bpf/crypto.c +3 -0
+	return ctx->type->hash(ctx->tfm, data_ptr, out_ptr, data_len);
+}
+#endif /* CONFIG_CRYPTO_HASH2 */

---
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/22414975518

AI-authorship-score: low
AI-authorship-explanation: Eight revision iterations with active reviewer engagement on lore.kernel.org and incremental changes addressing feedback are consistent with human-driven development.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: Data race on shared shash_desc in bpf_crypto_hash() produces silently corrupted cryptographic hash output when two CPUs use the same context concurrently, which could affect integrity checking and authentication use cases.

#3 lore raw

PATCH v8 2/4 selftests/bpf: Add tests for bpf_crypto_hash kfunc

In reply to Daniel Hodges

Add 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    | 210 ++++++++++++++++
 .../selftests/bpf/progs/crypto_common.h       |   2 +
 .../testing/selftests/bpf/progs/crypto_hash.c | 231 ++++++++++++++++++
 5 files changed, 447 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
index 6c78aae1bc6c..fd72648719a7 100644
@@ -4805,6 +4805,8 @@ F:	crypto/bpf_crypto_shash.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
index 24855381290d..dcfdca7daea3 100644
@@ -13,6 +13,8 @@ CONFIG_BPF_SYSCALL=y
 CONFIG_CGROUP_BPF=y
 CONFIG_CRYPTO_HMAC=y
 CONFIG_CRYPTO_SHA256=y
+CONFIG_CRYPTO_SHA512=y
+CONFIG_CRYPTO_HASH2=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 +210 -0

210 lines not on this page · raw message

tools/testing/selftests/bpf/progs/crypto_common.h +2 -0
index 57dd7a68a8c3..2f04f08f890b 100644
@@ -15,6 +15,8 @@ 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;
 
 struct __crypto_ctx_value {
 	struct bpf_crypto_ctx __kptr * ctx;
tools/testing/selftests/bpf/progs/crypto_hash.c new file +231 -0

231 lines not on this page · raw message

Signature · 1 line
2.52.0
#4 lore raw

PATCH v8 3/4 bpf: Add signature verification kfuncs

In reply to Daniel Hodges

Add a 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.

The module provides:
- alloc_tfm/free_tfm for crypto_sig transform lifecycle
- has_algo to check algorithm availability
- setkey for public key configuration
- verify for signature verification
- get_flags for crypto API flags

Introduce bpf_sig_verify, bpf_sig_keysize, bpf_sig_digestsize, and bpf_sig_maxsize kfuncs enabling BPF programs to verify digital signatures using the kernel's crypto infrastructure.

Add enum bpf_crypto_type_id for runtime type checking to ensure operations are performed on the correct crypto context type. The enum values are assigned to all crypto type modules (skcipher, hash, sig).

The verify kfunc takes a crypto context (initialized with the sig type and appropriate algorithm like "ecdsa-nist-p256"), a message digest, and a signature. These kfuncs support any signature algorithm registered with the crypto subsystem (e.g., ECDSA, RSA).

---
 MAINTAINERS                |   1 +
 crypto/Makefile            |   3 +
 crypto/bpf_crypto_sig.c    |  89 ++++++++++++++++++++++++++++
 include/linux/bpf_crypto.h |   4 ++
 kernel/bpf/crypto.c        | 117 +++++++++++++++++++++++++++++++++++++
 5 files changed, 214 insertions(+)
 create mode 100644 crypto/bpf_crypto_sig.c
MAINTAINERS +1 -0
index fd72648719a7..2d7d4bc3e7b3 100644
@@ -4802,6 +4802,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
index 4db6a76d4c3e..b75963692bc2 100644
@@ -38,6 +38,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 +89 -0

89 lines not on this page · raw message

include/linux/bpf_crypto.h +4 -0
index cf2c66f9782b..e0f946926f69 100644
@@ -18,9 +18,13 @@ struct bpf_crypto_type {
 	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);
+	int (*verify)(void *tfm, const u8 *sig, unsigned int sig_len,
+		      const u8 *msg, unsigned int msg_len);
 	unsigned int (*ivsize)(void *tfm);
 	unsigned int (*statesize)(void *tfm);
 	unsigned int (*digestsize)(void *tfm);
+	unsigned int (*keysize)(void *tfm);
+	unsigned int (*maxsize)(void *tfm);
 	u32 (*get_flags)(void *tfm);
 	struct module *owner;
 	enum bpf_crypto_type_id type_id;
kernel/bpf/crypto.c +117 -0

129 lines not on this page · raw message

Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH bpf-next v8 3/4] bpf: Add signature verification kfuncs

This paragraph appears to be inaccurate. The enum bpf_crypto_type_id and all three values (BPF_CRYPTO_TYPE_SKCIPHER, BPF_CRYPTO_TYPE_HASH, BPF_CRYPTO_TYPE_SIG) were added in the earlier commit…
Quoted 3 lines

Add enum bpf_crypto_type_id for runtime type checking to ensure operations are performed on the correct crypto context type. The enum values are assigned to all crypto type modules (skcipher, hash, sig).

This paragraph appears to be inaccurate. The enum bpf_crypto_type_id and all three values (BPF_CRYPTO_TYPE_SKCIPHER, BPF_CRYPTO_TYPE_HASH, BPF_CRYPTO_TYPE_SIG) were added in the earlier commit ddee9f01e40e ("bpf: Add hash kfunc for cryptographic hashing"), along with the type_id field in struct bpf_crypto_type and the assignment to the skcipher module.

Should this paragraph be removed or reworded to reflect what this commit actually introduces?

Quoted 3 lines
kernel/bpf/crypto.c +0 -0

[ ... ]

kernel/bpf/crypto.c __bpf_kfunc int bpf_sig_maxsize(struct bpf_crypto_ctx *ctx) · line 557

Quoted 10 lines
kernel/bpf/crypto.c +6 -0
@@ -438,6 +549,12 @@ __bpf_kfunc int bpf_sig_maxsize(struct bpf_crypto_ctx *ctx)
 #if IS_ENABLED(CONFIG_CRYPTO_HASH2)
 BTF_ID_FLAGS(func, bpf_crypto_hash, KF_RCU)
 #endif
+#if IS_ENABLED(CONFIG_CRYPTO_SIG2)
+BTF_ID_FLAGS(func, bpf_sig_verify, KF_RCU)
+BTF_ID_FLAGS(func, bpf_sig_keysize)
+BTF_ID_FLAGS(func, bpf_sig_digestsize)
+BTF_ID_FLAGS(func, bpf_sig_maxsize)
+#endif

This isn't a bug, but the existing kfuncs in this file follow the bpf_crypto_ naming convention: bpf_crypto_encrypt, bpf_crypto_decrypt, bpf_crypto_hash. The new kfuncs use bpf_sig_ instead. Would bpf_crypto_sig_verify (and similar) be more consistent for API discoverability?

---
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/22414975518

AI-authorship-score: medium
AI-authorship-explanation: Formulaic boilerplate code and polished commit message suggest possible AI assistance, but iterative v1-v8 development with lore interaction and a human-like commit message inaccuracy pattern indicate primarily human authorship.
issues-found: 2
issue-severity-score: low
issue-severity-explanation: Commit message inaccuracy about enum addition and kfunc naming inconsistency are documentation and style issues with no runtime impact.

#6 lore raw

PATCH v8 4/4 selftests/bpf: Add tests for signature verification kfuncs

In reply to Daniel Hodges

Add tests for the signature verification kfuncs:

1. test_ecdsa_verify_valid_signature: Verifies that a valid ECDSA signature over a known message hash is correctly verified using the P-256 curve with a test vector.

2. test_ecdsa_verify_invalid_signature: Verifies that an invalid signature (with modified r component) is correctly rejected.

3. test_ecdsa_size_queries: Tests the bpf_sig_keysize(), bpf_sig_digestsize(), and bpf_sig_maxsize() kfuncs to ensure they return valid positive values for a P-256 ECDSA context.

4. test_ecdsa_on_hash_ctx: Tests that calling bpf_sig_verify on a hash context fails with -EINVAL due to type mismatch.

5. test_ecdsa_keysize_on_hash_ctx: Tests that calling bpf_sig_keysize on a hash context fails with -EINVAL due to type mismatch.

6. test_ecdsa_zero_len_msg: Tests that zero-length message is rejected.

7. test_ecdsa_zero_len_sig: Tests that zero-length signature is rejected.

The test uses the p1363(ecdsa-nist-p256) algorithm with a known NIST P-256 test vector for reliable and reproducible testing.

---
 MAINTAINERS                                   |   2 +
 tools/testing/selftests/bpf/config            |   4 +
 .../selftests/bpf/prog_tests/sig_verify.c     | 163 ++++++++++
 .../selftests/bpf/progs/crypto_common.h       |   6 +
 .../testing/selftests/bpf/progs/sig_verify.c  | 286 ++++++++++++++++++
 5 files changed, 461 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sig_verify.c
 create mode 100644 tools/testing/selftests/bpf/progs/sig_verify.c
MAINTAINERS +2 -0
index 2d7d4bc3e7b3..8aed567f868a 100644
@@ -4807,7 +4807,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/sig_verify.c
 F:	tools/testing/selftests/bpf/progs/crypto_hash.c
+F:	tools/testing/selftests/bpf/progs/sig_verify.c
 
 BPF [DOCUMENTATION] (Related to Standardization)
 R:	David Vernet <[email protected]>
tools/testing/selftests/bpf/config +4 -0
index dcfdca7daea3..947c424425c2 100644
@@ -21,6 +21,10 @@ CONFIG_CRYPTO_USER_API_SKCIPHER=y
 CONFIG_CRYPTO_SKCIPHER=y
 CONFIG_CRYPTO_ECB=y
 CONFIG_CRYPTO_AES=y
+CONFIG_CRYPTO_SIG=y
+CONFIG_CRYPTO_SIG2=y
+CONFIG_CRYPTO_ECDSA=y
+CONFIG_CRYPTO_ECRDSA=y
 CONFIG_DEBUG_INFO=y
 CONFIG_DEBUG_INFO_BTF=y
 CONFIG_DEBUG_INFO_DWARF4=y
tools/testing/selftests/bpf/prog_tests/sig_verify.c new file +163 -0

163 lines not on this page · raw message

tools/testing/selftests/bpf/progs/crypto_common.h +6 -0
index 2f04f08f890b..3849083f2d23 100644
@@ -17,6 +17,12 @@ 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_sig_verify(struct bpf_crypto_ctx *ctx,
+		     const struct bpf_dynptr *message,
+		     const struct bpf_dynptr *signature) __ksym;
+int bpf_sig_keysize(struct bpf_crypto_ctx *ctx) __ksym;
+int bpf_sig_digestsize(struct bpf_crypto_ctx *ctx) __ksym;
+int bpf_sig_maxsize(struct bpf_crypto_ctx *ctx) __ksym;
 
 struct __crypto_ctx_value {
 	struct bpf_crypto_ctx __kptr * ctx;
tools/testing/selftests/bpf/progs/sig_verify.c new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
2.52.0
#7 lore raw
In reply to Daniel Hodges
Quoted 11 lines · Daniel Hodges

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

Hi Daniel,

I found your series because I was about to implement something similar like your hashing implementation. In other words, I'd be very happy to see this patchset move forward.

Taking an initial look at your hashing patches, I'm wondering: the usual interface to hash/digest algorithms is to have three functions: an init() function to set up state, an update() function that can be called multiple times to hash new bytes, and a finalize() function that creates the actual hash. Depending on the algorithm, some of them (esp. finalize) may be no-ops. Often, a fourth function, like hash(), is provided as convenience, doing one init/update/finalize cycle when all data to be hashed is already available.

I think we should provide the same init/update/finalize interface in bpf as well to make the API more flexible. That would require splitting out the shash_desc from the (mostly static) context. But doing so would also address the review comment from bpf-ci bot to patch 1. WDYT?

Thanks,
Felix

#8 lore raw
Quoted 9 lines · Felix Maurer

Hi Daniel,

I found your series because I was about to implement something similar like your hashing implementation. In other words, I'd be very happy to see this patchset move forward.

Taking an initial look at your hashing patches, I'm wondering: the usual interface to hash/digest algorithms is to have three functions: an init() function to set up state

Doesn't bpf_crypto_ctx_create already provide the initialization? I was trying to make that pattern work by adding the bpf_crypto_type_id to make the code a little more maintainable.

Quoted 4 lines

an update() function that can be called multiple times to hash new bytes, and a finalize() function that creates the actual hash. Depending on the algorithm, some of them (esp. finalize) may be no-ops. Often, a fourth function, like hash(), is provided

I think the bpf_crypto_encrypt should cover that along with the bpf_crypto_hash in the first patch.

Quoted 4 lines

I think we should provide the same init/update/finalize interface in bpf as well to make the API more flexible. That would require splitting out the shash_desc from the (mostly static) context. But doing so would also address the review comment from bpf-ci bot to patch 1. WDYT?

I was trying to make things work with the existing bpf_crypto_ctx lifecycle. IIRC in the V1/V2 of the series there was a separate struct but it was suggested to integrate the changes into bpf_crypto_ctx. Regarding the bpf-ci bot I think it's somewhat valid, but you could solve that by putting the bpf_crypto_ctx in a per CPU map or protecting it with a bpf spinlock. I didn't hear back from the crypto folks so I sort of left things as is. If you want to give it a go feel free. It would also be helpful to hear about what your use case is. Thanks for taking a look!

-Daniel

#9 lore raw

Hi Daniel,

Sorry for taking long to get back to you, I've been on vacation for a while.

Quoted 1 line

It would also be helpful to hear about what your use case is.

I'll put this one first, as it probably informs my thinking quite a bit. My use case is accessing hash functions from the networking bpf hooks, mostly XDP and tc. I'm less interested in the cryptographic hash functions (although I think we should support them as well), but checksums like CRC32c which have efficient implementations in the kernel and are not straightforward to implement in bpf.

Quoted 7 lines
Quoted 3 lines

Taking an initial look at your hashing patches, I'm wondering: the usual interface to hash/digest algorithms is to have three functions: an init() function to set up state

Doesn't bpf_crypto_ctx_create already provide the initialization? I was trying to make that pattern work by adding the bpf_crypto_type_id to make the code a little more maintainable.

No, bpf_crypto_ctx_create() doesn't do the initialization I was referring to, it only creates the tfm that is later used to do the hashing. The actual init() of the hash function happens inside crypto_shash_digest(). More details at the end of my reply.

Quoted 7 lines
Quoted 4 lines

an update() function that can be called multiple times to hash new bytes, and a finalize() function that creates the actual hash. Depending on the algorithm, some of them (esp. finalize) may be no-ops. Often, a fourth function, like hash(), is provided

I think the bpf_crypto_encrypt should cover that along with the bpf_crypto_hash in the first patch.

Not sure if I understand you correctly, bpf_crypto_encrypt() shouldn't be callable for a hashing crypto context at all?

Quoted 8 lines
Quoted 4 lines

I think we should provide the same init/update/finalize interface in bpf as well to make the API more flexible. That would require splitting out the shash_desc from the (mostly static) context. But doing so would also address the review comment from bpf-ci bot to patch 1. WDYT?

I was trying to make things work with the existing bpf_crypto_ctx lifecycle. IIRC in the V1/V2 of the series there was a separate struct but it was suggested to integrate the changes into bpf_crypto_ctx.

Yes, I see that the idea is to integrate into the existing life cycle and I appreciate that! But IIUC, they are subtly different at the moment. Just to be sure, the idea of your first two patches is to provide an interface to shash that supports the do-all-at-once function hash()/digest() (i.e., internally do init+update+finalize), pretty much the equivalent of the existing encrypt() + decrypt() interface to skcipher, right? I.e. not supporting each and every use case for now but just the case where all data is available and we want to hash/encrypt/ decrypt in one go?

With that assumption, the current implementation for skcipher looks like
this: we have bpf_crypto_ctx_create(). It sets up a bpf_crypto_ctx with the chosen algorithm, the right key, etc. It must be called from a sleepable bpf program because it may require to load the kernel module with the requested algorithm. The prepared bpf_crypto_ctx can be stored in a map so it can be used from other (non-sleepable) bpf programs, e.g., XDP programs.

When the bpf_crypto_ctx is used with encrypt()/decrypt(), both functions have the siv argument. It's a dynptr to some memory, containing the IV for algorithms that need it, but also has room for the *state* if the algorithm needs it. This means, the state is provided (fresh) for each invocation of encrypt/decrypt.

Your proposed implementation for shash, bpf_crypto_ctx_create() calls bpf_crypto_shash_alloc_tfm() which creates the tfm (just as above, needs to be in a sleepable bpf program to potentially load modules), but it also allocates the shash_desc which has room for some algorithm state if needed. This mixes the static, long-living, reusable tfm with the per-invocation shash_desc. shash is safe to use with the same tfm concurrently exactly because the state is stored in the shash_desc, which must not be used concurrently. Think of the shash_desc to be the equivalent of the siv for skcipher.

Therefore, I think the memory for shash_desc.__ctx should come from a dynptr as well. Do you agree or did I miss something?

Btw, your bpf_crypto_shash_alloc_tfm() never sets shash_desc.__ctx, so that's a bug anyways.

Quoted 3 lines

Regarding the bpf-ci bot I think it's somewhat valid, but you could solve that by putting the bpf_crypto_ctx in a per CPU map or protecting it with a bpf spinlock.

Two things for that: for networking use cases, needing a lock because of implementation details doesn't sound very intriguing. Plus, if per-cpu or a lock are required, that's currently not enforced. This means an invalid/unsafe bpf program could pass the verifier, right?

Quoted 1 line

If you want to give it a go feel free.

If you're still interested in pursuing this, please go ahead with your work, I don't want to take it from you. Only if you're explicitly not interested anymore, I'll go ahead and try to build something on top of you work. Just let me know :)

Thanks,
Felix