]> Git Repo - linux.git/blob - tools/testing/selftests/bpf/progs/crypto_bench.c
Linux 6.14-rc3
[linux.git] / tools / testing / selftests / bpf / progs / crypto_bench.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3
4 #include "vmlinux.h"
5 #include "bpf_tracing_net.h"
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_tracing.h>
9 #include "bpf_misc.h"
10 #include "bpf_kfuncs.h"
11 #include "crypto_common.h"
12
13 const volatile unsigned int len = 16;
14 char cipher[128] = {};
15 u32 key_len, authsize;
16 char dst[256] = {};
17 u8 key[256] = {};
18 long hits = 0;
19 int status;
20
21 SEC("syscall")
22 int crypto_setup(void *args)
23 {
24         struct bpf_crypto_ctx *cctx;
25         struct bpf_crypto_params params = {
26                 .type = "skcipher",
27                 .key_len = key_len,
28                 .authsize = authsize,
29         };
30         int err = 0;
31
32         status = 0;
33
34         if (!cipher[0] || !key_len || key_len > 256) {
35                 status = -EINVAL;
36                 return 0;
37         }
38
39         __builtin_memcpy(&params.algo, cipher, sizeof(cipher));
40         __builtin_memcpy(&params.key, key, sizeof(key));
41         cctx = bpf_crypto_ctx_create(&params, sizeof(params), &err);
42
43         if (!cctx) {
44                 status = err;
45                 return 0;
46         }
47
48         err = crypto_ctx_insert(cctx);
49         if (err && err != -EEXIST)
50                 status = err;
51
52         return 0;
53 }
54
55 SEC("tc")
56 int crypto_encrypt(struct __sk_buff *skb)
57 {
58         struct __crypto_ctx_value *v;
59         struct bpf_crypto_ctx *ctx;
60         struct bpf_dynptr psrc, pdst;
61
62         v = crypto_ctx_value_lookup();
63         if (!v) {
64                 status = -ENOENT;
65                 return 0;
66         }
67
68         ctx = v->ctx;
69         if (!ctx) {
70                 status = -ENOENT;
71                 return 0;
72         }
73
74         bpf_dynptr_from_skb(skb, 0, &psrc);
75         bpf_dynptr_from_mem(dst, len, 0, &pdst);
76
77         status = bpf_crypto_encrypt(ctx, &psrc, &pdst, NULL);
78         __sync_add_and_fetch(&hits, 1);
79
80         return 0;
81 }
82
83 SEC("tc")
84 int crypto_decrypt(struct __sk_buff *skb)
85 {
86         struct bpf_dynptr psrc, pdst;
87         struct __crypto_ctx_value *v;
88         struct bpf_crypto_ctx *ctx;
89
90         v = crypto_ctx_value_lookup();
91         if (!v)
92                 return -ENOENT;
93
94         ctx = v->ctx;
95         if (!ctx)
96                 return -ENOENT;
97
98         bpf_dynptr_from_skb(skb, 0, &psrc);
99         bpf_dynptr_from_mem(dst, len, 0, &pdst);
100
101         status = bpf_crypto_decrypt(ctx, &psrc, &pdst, NULL);
102         __sync_add_and_fetch(&hits, 1);
103
104         return 0;
105 }
106
107 char __license[] SEC("license") = "GPL";
This page took 0.03728 seconds and 4 git commands to generate.