bpf: crypto: Use AES-CBC and AES-ECB libraries
Subsystems (6)
PATCH v5 1/1 bpf: crypto: Use AES-CBC and AES-ECB libraries
BPF crypto was implemented using the lskcipher API, which doesn't seem to be going anywhere. lskcipher supports only ARC4 and block ciphers in CBC and ECB mode, and only with unoptimized implementations.
Library APIs also have been found to be a much better approach, for a variety of reasons, including reduced overhead, greater flexibility, and having to be explicit about the crypto algorithms that are supported.
We can safely ignore the theoretical ARC4 and non-AES block cipher support in BPF crypto as unused, which leaves AES-CBC and AES-ECB. AES-CBC was stated to be needed for decrypting packets using a homebrew UDP-based protocol
(https://lore.kernel.org/r/[email protected]/).
AES-ECB is used by the BPF self-tests, and it was stated to maybe be used in the future for QUIC-LB
(https://lore.kernel.org/r/[email protected]/).
Those reasons don't make much sense either, especially AES-ECB which isn't appropriate in new systems and should be dropped. Regardless, let's assume that both AES-CBC and AES-ECB need to be kept for now.
There are library APIs for both of these now, which are much easier to use and more efficient. Reimplement BPF crypto on top of them, greatly simplifying the code. As part of this, the bpf_crypto_type abstraction layer is removed, as it's not useful.
This removes the only user of the lskcipher API, so follow-up patches will be able to remove that code as well.
---
Changed in v5:
- Go back to v1's approach of using 'select' statements to select the library code, as requested by Alexei at
https://lore.kernel.org/bpf/[email protected]/
- Put aes_key at beginning of struct to address bot bikeshedding
- Rebase onto latest bpf-next
Changed in v4:
- Switch back to the embedded struct used in v1 and v2.
Changed in v3:
- Update the #ifdef in kernel/bpf/verifier.c.
- Store the key behind a void pointer so that BPF programs can't read it via BTF, as requested by the bpf-ci AI review.
- Update the selftests kconfig.
- Use WARN_ON_ONCE instead of WARN_ON.
Changed in v2:
- Use 'depends on' instead of 'select', to match the usual convention for bpf kfuncs.
- Keep using EOPNOTSUPP for unsupported algorithms.
- Avoid reading *err_ret after it's been written.
- Clarify that non-AES block ciphers are dropped too.
- Mention that lskcipher will be deleted later.
- Update a comment.
MAINTAINERS | 2 - crypto/Makefile | 3 - crypto/bpf_crypto_skcipher.c | 83 --------- include/linux/bpf_crypto.h | 24 --- kernel/bpf/Kconfig | 9 + kernel/bpf/Makefile | 4 +- kernel/bpf/crypto.c | 288 +++++++++++++---------------- kernel/bpf/verifier.c | 2 +- tools/testing/selftests/bpf/config | 2 +- 9 files changed, 136 insertions(+), 281 deletions(-) delete mode 100644 crypto/bpf_crypto_skcipher.c delete mode 100644 include/linux/bpf_crypto.h
MAINTAINERS +0 -2
@@ -4936,8 +4936,6 @@ BPF [CRYPTO] M: Vadim Fedorenko <[email protected]> L: [email protected] S: Maintained -F: crypto/bpf_crypto_skcipher.c -F: include/linux/bpf_crypto.h F: kernel/bpf/crypto.c BPF [DOCUMENTATION] (Related to Standardization)
crypto/Makefile +0 -3
@@ -22,9 +22,6 @@ crypto_skcipher-y += lskcipher.o crypto_skcipher-y += skcipher.o obj-$(CONFIG_CRYPTO_SKCIPHER2) += crypto_skcipher.o -ifeq ($(CONFIG_BPF_SYSCALL),y) -obj-$(CONFIG_CRYPTO_SKCIPHER2) += bpf_crypto_skcipher.o -endif obj-$(CONFIG_CRYPTO_SEQIV) += seqiv.o obj-$(CONFIG_CRYPTO_ECHAINIV) += echainiv.o
crypto/bpf_crypto_skcipher.c deleted +0 -83
83 lines not on this page · raw message
include/linux/bpf_crypto.h deleted +0 -24
@@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ -#ifndef _BPF_CRYPTO_H -#define _BPF_CRYPTO_H - -struct bpf_crypto_type { -void *(*alloc_tfm)(const char *algo); -void (*free_tfm)(void *tfm); -int (*has_algo)(const char *algo); -int (*setkey)(void *tfm,const u8 *key,unsigned int keylen); -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); -unsigned int (*ivsize)(void *tfm); -unsigned int (*statesize)(void *tfm); -u32 (*get_flags)(void *tfm); -struct module *owner; -char name[14 ]; -}; - -int bpf_crypto_register_type (const struct bpf_crypto_type *type); -int bpf_crypto_unregister_type (const struct bpf_crypto_type *type); - -#endif /* _BPF_CRYPTO_H */
kernel/bpf/Kconfig +9 -0
@@ -91,6 +91,15 @@ config BPF_UNPRIV_DEFAULT_OFF If you are unsure how to answer this question, answer Y. +config BPF_CRYPTO + def_bool y + depends on BPF_SYSCALL + select CRYPTO_LIB_AES_CBC + select CRYPTO_LIB_AES_ECB + help + Provide the kfuncs needed for BPF programs to encrypt and decrypt + data. The supported algorithms are AES-CBC and AES-ECB. + source "kernel/bpf/preload/Kconfig" config BPF_LSM
kernel/bpf/Makefile +1 -3
@@ -58,9 +58,7 @@ obj-$(CONFIG_BPF_SYSCALL) += cpumask.o # semantics within pahole are revisited accordingly. obj-${CONFIG_BPF_LSM} += bpf_lsm_proto.o bpf_lsm.o endif -ifneq ($(CONFIG_CRYPTO),) -obj-$(CONFIG_BPF_SYSCALL) += crypto.o -endif +obj-$(CONFIG_BPF_CRYPTO) += crypto.o obj-$(CONFIG_BPF_PRELOAD) += preload/ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
kernel/bpf/crypto.c +124 -164
420 lines not on this page · raw message
kernel/bpf/verifier.c +1 -1
@@ -4742,7 +4742,7 @@ BTF_ID(struct, cgroup) BTF_ID(struct, bpf_cpumask)#endif BTF_ID(struct, task_struct) -#ifdef CONFIG_CRYPTO +#ifdef CONFIG_BPF_CRYPTO BTF_ID(struct, bpf_crypto_ctx)#endif #ifdef CONFIG_INET
tools/testing/selftests/bpf/config +1 -1
@@ -18,7 +18,7 @@ CONFIG_CRYPTO_SHA512=y CONFIG_CRYPTO_USER_API=y CONFIG_CRYPTO_USER_API_HASH=y CONFIG_CRYPTO_USER_API_SKCIPHER=y -CONFIG_CRYPTO_SKCIPHER=y +CONFIG_CRYPTO_CBC=y CONFIG_CRYPTO_ECB=y CONFIG_CRYPTO_AES=y CONFIG_DEBUG_INFO=y
base-commit: ce5cbbef271efd5968e8027ffefddb3246e9bc29
Signature · 1 line
2.55.0
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Thu, 24 Sep 2026 11:42:54 -0700 you wrote:
BPF crypto was implemented using the lskcipher API, which doesn't seem to be going anywhere. lskcipher supports only ARC4 and block ciphers in CBC and ECB mode, and only with unoptimized implementations.
Library APIs also have been found to be a much better approach, for a variety of reasons, including reduced overhead, greater flexibility, and having to be explicit about the crypto algorithms that are supported.
[...]
Here is the summary with links:
- [bpf-next,v5] bpf: crypto: Use AES-CBC and AES-ECB libraries
https://git.kernel.org/bpf/bpf-next/c/d5c101840af8
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html