Skip to content

Commit 7822695

Browse files
hodgesdsKernel Patches Daemon
authored andcommitted
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. Signed-off-by: Daniel Hodges <[email protected]>
1 parent df275ea commit 7822695

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

crypto/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ obj-$(CONFIG_CRYPTO_ECHAINIV) += echainiv.o
3030
crypto_hash-y += ahash.o
3131
crypto_hash-y += shash.o
3232
obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
33+
ifeq ($(CONFIG_BPF_SYSCALL),y)
34+
obj-$(CONFIG_CRYPTO_HASH2) += bpf_crypto_shash.o
35+
endif
3336

3437
obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o
3538
obj-$(CONFIG_CRYPTO_SIG2) += sig.o

crypto/bpf_crypto_shash.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
#include <linux/types.h>
4+
#include <linux/module.h>
5+
#include <linux/bpf_crypto.h>
6+
#include <crypto/hash.h>
7+
8+
struct bpf_shash_ctx {
9+
struct crypto_shash *tfm;
10+
struct shash_desc desc;
11+
};
12+
13+
static void *bpf_crypto_shash_alloc_tfm(const char *algo)
14+
{
15+
struct bpf_shash_ctx *ctx;
16+
struct crypto_shash *tfm;
17+
18+
tfm = crypto_alloc_shash(algo, 0, 0);
19+
if (IS_ERR(tfm))
20+
return tfm;
21+
22+
ctx = kzalloc(sizeof(*ctx) + crypto_shash_descsize(tfm), GFP_KERNEL);
23+
if (!ctx) {
24+
crypto_free_shash(tfm);
25+
return ERR_PTR(-ENOMEM);
26+
}
27+
28+
ctx->tfm = tfm;
29+
ctx->desc.tfm = tfm;
30+
31+
return ctx;
32+
}
33+
34+
static void bpf_crypto_shash_free_tfm(void *tfm)
35+
{
36+
struct bpf_shash_ctx *ctx = tfm;
37+
38+
crypto_free_shash(ctx->tfm);
39+
kfree(ctx);
40+
}
41+
42+
static int bpf_crypto_shash_has_algo(const char *algo)
43+
{
44+
return crypto_has_shash(algo, 0, 0);
45+
}
46+
47+
static int bpf_crypto_shash_hash(void *tfm, const u8 *data, u8 *out,
48+
unsigned int len)
49+
{
50+
struct bpf_shash_ctx *ctx = tfm;
51+
52+
return crypto_shash_digest(&ctx->desc, data, len, out);
53+
}
54+
55+
static unsigned int bpf_crypto_shash_digestsize(void *tfm)
56+
{
57+
struct bpf_shash_ctx *ctx = tfm;
58+
59+
return crypto_shash_digestsize(ctx->tfm);
60+
}
61+
62+
static u32 bpf_crypto_shash_get_flags(void *tfm)
63+
{
64+
struct bpf_shash_ctx *ctx = tfm;
65+
66+
return crypto_shash_get_flags(ctx->tfm);
67+
}
68+
69+
static const struct bpf_crypto_type bpf_crypto_shash_type = {
70+
.alloc_tfm = bpf_crypto_shash_alloc_tfm,
71+
.free_tfm = bpf_crypto_shash_free_tfm,
72+
.has_algo = bpf_crypto_shash_has_algo,
73+
.hash = bpf_crypto_shash_hash,
74+
.digestsize = bpf_crypto_shash_digestsize,
75+
.get_flags = bpf_crypto_shash_get_flags,
76+
.owner = THIS_MODULE,
77+
.name = "hash",
78+
};
79+
80+
static int __init bpf_crypto_shash_init(void)
81+
{
82+
return bpf_crypto_register_type(&bpf_crypto_shash_type);
83+
}
84+
85+
static void __exit bpf_crypto_shash_exit(void)
86+
{
87+
int err = bpf_crypto_unregister_type(&bpf_crypto_shash_type);
88+
89+
WARN_ON_ONCE(err);
90+
}
91+
92+
module_init(bpf_crypto_shash_init);
93+
module_exit(bpf_crypto_shash_exit);
94+
MODULE_LICENSE("GPL");
95+
MODULE_DESCRIPTION("Hash algorithm support for BPF");

0 commit comments

Comments
 (0)