1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
5 #include "bpf_tracing_net.h"
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_tracing.h>
10 #include "bpf_kfuncs.h"
11 #include "crypto_common.h"
13 const volatile unsigned int len = 16;
14 char cipher[128] = {};
15 u32 key_len, authsize;
22 int crypto_setup(void *args)
24 struct bpf_crypto_ctx *cctx;
25 struct bpf_crypto_params params = {
34 if (!cipher[0] || !key_len || key_len > 256) {
39 __builtin_memcpy(¶ms.algo, cipher, sizeof(cipher));
40 __builtin_memcpy(¶ms.key, key, sizeof(key));
41 cctx = bpf_crypto_ctx_create(¶ms, sizeof(params), &err);
48 err = crypto_ctx_insert(cctx);
49 if (err && err != -EEXIST)
56 int crypto_encrypt(struct __sk_buff *skb)
58 struct __crypto_ctx_value *v;
59 struct bpf_crypto_ctx *ctx;
60 struct bpf_dynptr psrc, pdst;
62 v = crypto_ctx_value_lookup();
74 bpf_dynptr_from_skb(skb, 0, &psrc);
75 bpf_dynptr_from_mem(dst, len, 0, &pdst);
77 status = bpf_crypto_encrypt(ctx, &psrc, &pdst, NULL);
78 __sync_add_and_fetch(&hits, 1);
84 int crypto_decrypt(struct __sk_buff *skb)
86 struct bpf_dynptr psrc, pdst;
87 struct __crypto_ctx_value *v;
88 struct bpf_crypto_ctx *ctx;
90 v = crypto_ctx_value_lookup();
98 bpf_dynptr_from_skb(skb, 0, &psrc);
99 bpf_dynptr_from_mem(dst, len, 0, &pdst);
101 status = bpf_crypto_decrypt(ctx, &psrc, &pdst, NULL);
102 __sync_add_and_fetch(&hits, 1);
107 char __license[] SEC("license") = "GPL";