-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
312 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef _MIMIC_KMOD_CRYPTO_H | ||
#define _MIMIC_KMOD_CRYPTO_H | ||
|
||
#if defined(_MIMIC_KMOD) | ||
#include <crypto/skcipher.h> | ||
#include <linux/refcount.h> | ||
#elif defined(_MIMIC_BPF) | ||
// clang-format off | ||
#include "bpf/vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
// clang-format on | ||
#endif | ||
|
||
struct mimic_crypto_state { | ||
refcount_t rc; | ||
struct crypto_skcipher* tfm; | ||
}; | ||
|
||
#ifdef _MIMIC_BPF | ||
|
||
#if defined(MIMIC_CHECKSUM_HACK_kfunc) | ||
struct mimic_crypto_state* mimic_crypto_state_create(void) __ksym; | ||
int mimic_crypto_set_key(struct mimic_crypto_state* state, void* key, __u32 key__sz) __ksym; | ||
void mimic_crypto_state_release(struct mimic_crypto_state* state) __ksym; | ||
int mimic_encrypt_wg_header(struct __sk_buff* skb_bpf, __u32 offset, void* iv, __u32 iv__sz, | ||
struct mimic_crypto_state* state) __ksym; | ||
int mimic_decrypt_wg_header(struct xdp_md* xdp_bpf, __u32 offset, void* iv, __u32 iv__sz, | ||
struct mimic_crypto_state* state) __ksym; | ||
|
||
#elif defined(MIMIC_CHECKSUM_HACK_kprobe) | ||
#error to be implemented | ||
#endif | ||
|
||
#endif | ||
|
||
#endif // _MIMIC_KMOD_CRYPTO_H |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef _MIMIC_KMOD_IMPL_H | ||
#define _MIMIC_KMOD_IMPL_H | ||
|
||
int impl_init(void); | ||
void impl_exit(void); | ||
|
||
#endif // _MIMIC_KMOD_IMPL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef _MIMIC_KMOD_KFUNC_KFUNC_H | ||
#define _MIMIC_KMOD_KFUNC_KFUNC_H | ||
|
||
#include <linux/btf.h> // IWYU pragma: export | ||
|
||
#ifndef __bpf_kfunc | ||
#define __bpf_kfunc __used noinline | ||
#endif | ||
|
||
#ifndef __bpf_kfunc_start_defs | ||
#define __bpf_kfunc_start_defs() \ | ||
__diag_push(); \ | ||
__diag_ignore_all("-Wmissing-declarations", \ | ||
"Global kfuncs as their definitions will be in BTF"); \ | ||
__diag_ignore_all("-Wmissing-prototypes", "Global kfuncs as their definitions will be in BTF") | ||
#endif | ||
|
||
#ifndef __bpf_kfunc_end_defs | ||
#define __bpf_kfunc_end_defs() __diag_pop() | ||
#endif | ||
|
||
#ifndef BTF_KFUNCS_START | ||
#define BTF_KFUNCS_START BTF_SET8_START | ||
#endif | ||
|
||
#ifndef BTF_KFUNCS_END | ||
#define BTF_KFUNCS_END BTF_SET8_END | ||
#endif | ||
|
||
#endif // _MIMIC_KMOD_KFUNC_KFUNC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <crypto/skcipher.h> | ||
#include <linux/bpf.h> | ||
#include <linux/btf.h> | ||
#include <linux/btf_ids.h> | ||
#include <linux/cfi.h> | ||
#include <linux/crypto.h> | ||
#include <linux/err.h> | ||
#include <linux/gfp_types.h> | ||
#include <linux/random.h> | ||
#include <linux/refcount.h> | ||
#include <linux/scatterlist.h> | ||
#include <linux/skbuff.h> | ||
#include <linux/slab.h> | ||
#include <linux/types.h> | ||
#include <net/xdp.h> | ||
|
||
#include "../crypto.h" | ||
#include "crypto.h" | ||
|
||
static int skcipher(void* data, size_t size, int (*process)(struct skcipher_request*)) { | ||
int ret = 0; | ||
struct crypto_skcipher* tfm = NULL; | ||
struct skcipher_request* req = NULL; | ||
struct scatterlist sg; | ||
|
||
if (IS_ERR(tfm = crypto_alloc_skcipher("chacha20", 0, 0))) { | ||
pr_err("error allocating chacha20 handle: %ld\n", PTR_ERR(tfm)); | ||
return PTR_ERR(tfm); | ||
} | ||
|
||
__u8 iv[16] = {}, key[32] = {}; | ||
// get_random_bytes(iv, sizeof(iv)); | ||
// get_random_bytes(key, sizeof(key)); | ||
|
||
if ((ret = crypto_skcipher_setkey(tfm, key, sizeof(key))) < 0) { | ||
pr_err("error setting key: %d\n", ret); | ||
goto cleanup; | ||
} | ||
|
||
req = skcipher_request_alloc(tfm, GFP_ATOMIC); | ||
if (!req) { | ||
ret = -ENOMEM; | ||
goto cleanup; | ||
} | ||
|
||
sg_init_one(&sg, data, size); | ||
skcipher_request_set_crypt(req, &sg, &sg, size, iv); | ||
if ((ret = process(req)) < 0) { | ||
pr_err("error encrypting/decrypting data: %d\n", ret); | ||
goto cleanup; | ||
} | ||
|
||
ret = 0; | ||
cleanup: | ||
crypto_free_skcipher(tfm); | ||
skcipher_request_free(req); | ||
return ret; | ||
} | ||
|
||
struct mimic_crypto_state* mimic_crypto_state_create(void) { | ||
struct mimic_crypto_state* state = kzalloc(sizeof(*state), GFP_KERNEL); | ||
state->rc = (typeof(state->rc))REFCOUNT_INIT(1); | ||
state->tfm = crypto_alloc_skcipher("chacha20", 0, 0); | ||
if (IS_ERR(state->tfm)) { | ||
kfree(state); | ||
return NULL; | ||
} | ||
return state; | ||
} | ||
|
||
int mimic_crypto_set_key(struct mimic_crypto_state* state, void* key, __u32 key__sz) { | ||
return crypto_skcipher_setkey(state->tfm, key, key__sz); | ||
} | ||
|
||
void mimic_crypto_state_release(struct mimic_crypto_state* state) { | ||
if (refcount_dec_and_test(&state->rc)) { | ||
crypto_free_skcipher(state->tfm); | ||
kfree(state); | ||
} | ||
} | ||
|
||
void mimic_crypto_state_dtor(void* p) { mimic_crypto_state_release(p); } | ||
CFI_NOSEAL(mimic_crypto_state_dtor); | ||
|
||
int mimic_encrypt_wg_header(struct __sk_buff* skb_bpf, __u32 offset, void* iv, __u32 iv__sz, struct mimic_crypto_state* state) { | ||
struct sk_buff* skb = (typeof(skb))skb_bpf; | ||
return skcipher(skb->data + offset, 16, crypto_skcipher_encrypt); | ||
} | ||
|
||
int mimic_decrypt_wg_header(struct xdp_md* xdp_bpf, __u32 offset, void* iv, __u32 iv__sz, struct mimic_crypto_state* state) { | ||
struct xdp_buff* xdp = (typeof(xdp))xdp_bpf; | ||
return skcipher(xdp->data + offset, 16, crypto_skcipher_decrypt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef _MIMIC_KMOD_KFUNC_CRYPTO_H | ||
#define _MIMIC_KMOD_KFUNC_CRYPTO_H | ||
|
||
#include <linux/bpf.h> | ||
#include <linux/btf.h> | ||
|
||
#include "../crypto.h" | ||
#include "common.h" | ||
|
||
__bpf_kfunc_start_defs(); | ||
|
||
__bpf_kfunc struct mimic_crypto_state* mimic_crypto_state_create(void); | ||
__bpf_kfunc int mimic_crypto_set_key(struct mimic_crypto_state* state, void* key, __u32 key__sz); | ||
__bpf_kfunc void mimic_crypto_state_release(struct mimic_crypto_state* state); | ||
__bpf_kfunc void mimic_crypto_state_dtor(void* p); | ||
__bpf_kfunc int mimic_encrypt_wg_header(struct __sk_buff* skb_bpf, __u32 offset, void* iv, __u32 iv__sz, struct mimic_crypto_state* state); | ||
__bpf_kfunc int mimic_decrypt_wg_header(struct xdp_md* xdp_bpf, __u32 offset, void* iv, __u32 iv__sz, struct mimic_crypto_state* state); | ||
|
||
__bpf_kfunc_end_defs(); | ||
|
||
#endif // _MIMIC_KMOD_KFUNC_CRYPTO_H |
Oops, something went wrong.