diff options
author | segfault <segfault@secmail.pro> | 2019-04-13 21:14:59 +0200 |
---|---|---|
committer | segfault <segfault@secmail.pro> | 2019-05-01 01:41:39 +0200 |
commit | 21144d5cb548f8fad5583e77fcce51e2e0a707e9 (patch) | |
tree | 9b2b8b5d55b3fb891999fde24882508643c83e81 /funccrypt.h |
initial commit
Signed-off-by: segfault <segfault@secmail.pro>
Diffstat (limited to 'funccrypt.h')
-rw-r--r-- | funccrypt.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/funccrypt.h b/funccrypt.h new file mode 100644 index 0000000..6b1f95c --- /dev/null +++ b/funccrypt.h @@ -0,0 +1,77 @@ +#ifndef FUNCCRYPT_H +#define FUNCCRYPT_H 1 + +#include <stdlib.h> +#include <stdint.h> + +#if !defined(__GNUC__) || !defined(__GNUC_MINOR__) +#error "This is only verified to work with GCC compiler!" +#endif + +/* Force GCC struct for MingW compilers and pack them, + * which means the struct is 1-byte aligned. + */ +#define GCC_PACKED __attribute__((packed, gcc_struct)) + +typedef struct crypt_header { + uint64_t key; + uint8_t crpyted; + uint32_t marker; + uint64_t func_body[0]; +} GCC_PACKED crypt_header; + +typedef enum crypt_return { + CRET_ERROR, CRET_PROLOGUE, CRET_EPILOGUE, CRET_CHECK, CRET_OK +} crypt_return; + +#define CRYPT_FUNC_MAXSIZ 0x100 +#define CRYPT_FUNC(fn) \ + crypt_func((void *)fn) +#define CRYPT_PROLOGUE(fn) \ + crypt_return __cr; \ + { \ + __cr = CRYPT_FUNC(fn); \ + if (__cr != CRET_OK) \ + asm volatile goto("jmp %l0 \n" \ + : : : : cr_epilogue); \ + asm volatile goto("jmp %l0 \n" \ + : : : : cr_prologue); \ + asm volatile( \ + ".byte " \ + /* key: */ "0x00, 0x00, 0x00, 0x00," \ + "0x00, 0x00, 0x00, 0x00," \ + /* crypted: */ "0x00," \ + /* marker: */ "0xDE, 0xC0, 0xDE, 0xC0; \n" \ + ); \ + } \ + cr_prologue: { +#define CRYPT_EPILOGUE(fn) \ + } { \ + asm volatile goto("jmp %l0 \n" \ + : : : : cr_epilogue); \ + asm volatile( \ + ".byte " \ + /* Insert encryption pad, so we can find the end marker, + * while the function body is encrypted. + * (XOR 64 bit encryption == 8 byte) + */ \ + "0x00, 0x00, 0x00, 0x00," \ + "0x00, 0x00, 0x00, 0x00," \ + /* marker: */ "0xFE, 0xCA, 0xFE, 0xCA; \n" \ + ); \ + } \ + cr_epilogue: \ + CRYPT_FUNC(fn); + +#define CRYPT_FNDEF(name, ...) \ + void name( __VA_ARGS__ ) { \ + CRYPT_PROLOGUE( name ); +#define CRYPT_FNEND(name) \ + CRYPT_EPILOGUE(name); \ + } + +extern const char *crypt_strs[]; +extern void printHexBuf(uint8_t *buf, size_t siz, size_t chars_per_line); +extern crypt_return crypt_func(void *fn_start); + +#endif |