xref: /linux/tools/testing/selftests/bpf/progs/crypto_bench.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
18000e627SVadim Fedorenko // SPDX-License-Identifier: GPL-2.0
28000e627SVadim Fedorenko /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
38000e627SVadim Fedorenko 
48000e627SVadim Fedorenko #include "vmlinux.h"
58000e627SVadim Fedorenko #include "bpf_tracing_net.h"
68000e627SVadim Fedorenko #include <bpf/bpf_helpers.h>
78000e627SVadim Fedorenko #include <bpf/bpf_endian.h>
88000e627SVadim Fedorenko #include <bpf/bpf_tracing.h>
98000e627SVadim Fedorenko #include "bpf_misc.h"
108000e627SVadim Fedorenko #include "bpf_kfuncs.h"
118000e627SVadim Fedorenko #include "crypto_common.h"
128000e627SVadim Fedorenko 
138000e627SVadim Fedorenko const volatile unsigned int len = 16;
148000e627SVadim Fedorenko char cipher[128] = {};
158000e627SVadim Fedorenko u32 key_len, authsize;
168000e627SVadim Fedorenko char dst[256] = {};
178000e627SVadim Fedorenko u8 key[256] = {};
188000e627SVadim Fedorenko long hits = 0;
198000e627SVadim Fedorenko int status;
208000e627SVadim Fedorenko 
218000e627SVadim Fedorenko SEC("syscall")
crypto_setup(void * args)228000e627SVadim Fedorenko int crypto_setup(void *args)
238000e627SVadim Fedorenko {
248000e627SVadim Fedorenko 	struct bpf_crypto_ctx *cctx;
258000e627SVadim Fedorenko 	struct bpf_crypto_params params = {
268000e627SVadim Fedorenko 		.type = "skcipher",
278000e627SVadim Fedorenko 		.key_len = key_len,
288000e627SVadim Fedorenko 		.authsize = authsize,
298000e627SVadim Fedorenko 	};
308000e627SVadim Fedorenko 	int err = 0;
318000e627SVadim Fedorenko 
328000e627SVadim Fedorenko 	status = 0;
338000e627SVadim Fedorenko 
348000e627SVadim Fedorenko 	if (!cipher[0] || !key_len || key_len > 256) {
358000e627SVadim Fedorenko 		status = -EINVAL;
368000e627SVadim Fedorenko 		return 0;
378000e627SVadim Fedorenko 	}
388000e627SVadim Fedorenko 
398000e627SVadim Fedorenko 	__builtin_memcpy(&params.algo, cipher, sizeof(cipher));
408000e627SVadim Fedorenko 	__builtin_memcpy(&params.key, key, sizeof(key));
418000e627SVadim Fedorenko 	cctx = bpf_crypto_ctx_create(&params, sizeof(params), &err);
428000e627SVadim Fedorenko 
438000e627SVadim Fedorenko 	if (!cctx) {
448000e627SVadim Fedorenko 		status = err;
458000e627SVadim Fedorenko 		return 0;
468000e627SVadim Fedorenko 	}
478000e627SVadim Fedorenko 
488000e627SVadim Fedorenko 	err = crypto_ctx_insert(cctx);
498000e627SVadim Fedorenko 	if (err && err != -EEXIST)
508000e627SVadim Fedorenko 		status = err;
518000e627SVadim Fedorenko 
528000e627SVadim Fedorenko 	return 0;
538000e627SVadim Fedorenko }
548000e627SVadim Fedorenko 
558000e627SVadim Fedorenko SEC("tc")
crypto_encrypt(struct __sk_buff * skb)568000e627SVadim Fedorenko int crypto_encrypt(struct __sk_buff *skb)
578000e627SVadim Fedorenko {
588000e627SVadim Fedorenko 	struct __crypto_ctx_value *v;
598000e627SVadim Fedorenko 	struct bpf_crypto_ctx *ctx;
60*9b560751SVadim Fedorenko 	struct bpf_dynptr psrc, pdst;
618000e627SVadim Fedorenko 
628000e627SVadim Fedorenko 	v = crypto_ctx_value_lookup();
638000e627SVadim Fedorenko 	if (!v) {
648000e627SVadim Fedorenko 		status = -ENOENT;
658000e627SVadim Fedorenko 		return 0;
668000e627SVadim Fedorenko 	}
678000e627SVadim Fedorenko 
688000e627SVadim Fedorenko 	ctx = v->ctx;
698000e627SVadim Fedorenko 	if (!ctx) {
708000e627SVadim Fedorenko 		status = -ENOENT;
718000e627SVadim Fedorenko 		return 0;
728000e627SVadim Fedorenko 	}
738000e627SVadim Fedorenko 
748000e627SVadim Fedorenko 	bpf_dynptr_from_skb(skb, 0, &psrc);
758000e627SVadim Fedorenko 	bpf_dynptr_from_mem(dst, len, 0, &pdst);
768000e627SVadim Fedorenko 
77*9b560751SVadim Fedorenko 	status = bpf_crypto_encrypt(ctx, &psrc, &pdst, NULL);
788000e627SVadim Fedorenko 	__sync_add_and_fetch(&hits, 1);
798000e627SVadim Fedorenko 
808000e627SVadim Fedorenko 	return 0;
818000e627SVadim Fedorenko }
828000e627SVadim Fedorenko 
838000e627SVadim Fedorenko SEC("tc")
crypto_decrypt(struct __sk_buff * skb)848000e627SVadim Fedorenko int crypto_decrypt(struct __sk_buff *skb)
858000e627SVadim Fedorenko {
86*9b560751SVadim Fedorenko 	struct bpf_dynptr psrc, pdst;
878000e627SVadim Fedorenko 	struct __crypto_ctx_value *v;
888000e627SVadim Fedorenko 	struct bpf_crypto_ctx *ctx;
898000e627SVadim Fedorenko 
908000e627SVadim Fedorenko 	v = crypto_ctx_value_lookup();
918000e627SVadim Fedorenko 	if (!v)
928000e627SVadim Fedorenko 		return -ENOENT;
938000e627SVadim Fedorenko 
948000e627SVadim Fedorenko 	ctx = v->ctx;
958000e627SVadim Fedorenko 	if (!ctx)
968000e627SVadim Fedorenko 		return -ENOENT;
978000e627SVadim Fedorenko 
988000e627SVadim Fedorenko 	bpf_dynptr_from_skb(skb, 0, &psrc);
998000e627SVadim Fedorenko 	bpf_dynptr_from_mem(dst, len, 0, &pdst);
1008000e627SVadim Fedorenko 
101*9b560751SVadim Fedorenko 	status = bpf_crypto_decrypt(ctx, &psrc, &pdst, NULL);
1028000e627SVadim Fedorenko 	__sync_add_and_fetch(&hits, 1);
1038000e627SVadim Fedorenko 
1048000e627SVadim Fedorenko 	return 0;
1058000e627SVadim Fedorenko }
1068000e627SVadim Fedorenko 
1078000e627SVadim Fedorenko char __license[] SEC("license") = "GPL";
108