diff --git a/lib/include/laika.h b/lib/include/laika.h index 9018989..b859515 100644 --- a/lib/include/laika.h +++ b/lib/include/laika.h @@ -18,7 +18,7 @@ # define LAIKA_DEBUG(...) ((void)0) /* no op */ #endif -#ifndef _WIN32 +#ifndef _MSC_VER # define LAIKA_FORCEINLINE __attribute__((always_inline)) inline #else # define LAIKA_FORCEINLINE __forceinline diff --git a/lib/include/lbox.h b/lib/include/lbox.h index d763a43..3779558 100644 --- a/lib/include/lbox.h +++ b/lib/include/lbox.h @@ -4,7 +4,11 @@ #include #include "laika.h" +#include "lmem.h" #include "lvm.h" +#include "lsodium.h" + +#define LAIKA_BOX_HEAPSIZE 256 /* Laika Box: Laika Boxes are obfuscated storage mediums where data is only in memory for a very short amount of time. @@ -18,18 +22,25 @@ */ struct sLaikaB_box { - uint8_t *data; - uint8_t *unlockedData; - struct sLaikaV_vm vm; + uint8_t unlockedData[LAIKA_BOX_HEAPSIZE]; + uint8_t code[LAIKA_VM_CODESIZE]; }; -LAIKA_FORCEINLINE void laikaB_unlock(struct sLaikaB_box *box) { +LAIKA_FORCEINLINE void* laikaB_unlock(struct sLaikaB_box *box, void *data) { + struct sLaikaV_vm vm = {.pc = 0}; + memcpy(vm.code, box->code, LAIKA_VM_CODESIZE); + /* boxes have 2 reserved constants, 0 for the output, 1 for the input */ + vm.constList[0].ptr = box->unlockedData; + vm.constList[1].ptr = data; + + laikaV_execute(&vm); + return (void*)box->unlockedData; } -/* safely free's allocated buffer using libsodium's api for clearing sensitive data from memory */ -LAIKA_FORCEINLINE void laikaB_lock(struct sLaikaB_box *box) { - +/* safely zeros the unlockedData using libsodium's api for clearing sensitive data from memory */ +LAIKA_FORCEINLINE void* laikaB_lock(struct sLaikaB_box *box) { + sodium_memzero(box->unlockedData, LAIKA_BOX_HEAPSIZE); } #endif \ No newline at end of file diff --git a/lib/include/lvm.h b/lib/include/lvm.h index 177ab10..7ca40c3 100644 --- a/lib/include/lvm.h +++ b/lib/include/lvm.h @@ -30,11 +30,11 @@ struct sLaikaV_vm { int pc; }; -#define LAIKA_MAKE_VM(consts, code) (struct sLaikaV_vm)({.constList = consts, .code = code, .pc = 0, .stack = {}}) +#define LAIKA_MAKE_VM(_consts, _code) (struct sLaikaV_vm)({.constList = _consts, .code = _code, .pc = 0, .stack = {}}) /* constants */ -#define LAIKA_MAKE_VM_INT(i) (struct sLaikaV_vm_val)({.i = i}) -#define LAIKA_MAKE_VM_PTR(ptr) (struct sLaikaV_vm_val)({.ptr = ptr}) +#define LAIKA_MAKE_VM_INT(_i) (struct sLaikaV_vm_val)({.i = _i}) +#define LAIKA_MAKE_VM_PTR(_ptr) (struct sLaikaV_vm_val)({.ptr = _ptr}) /* instructions */ #define LAIKA_MAKE_VM_IA(opcode, a) opcode, a #define LAIKA_MAKE_VM_IAB(opcode, a, b) opcode, a, b @@ -43,6 +43,7 @@ struct sLaikaV_vm { enum { OP_EXIT, OP_LOADCONST, /* stk_indx[uint8_t] = const_indx[uint8_t] */ + OP_PUSHLIT, /* stk_indx[uint8_t].i = uint8_t */ OP_READ, /* stk_indx[uint8_t] = *(int8_t*)stk_indx[uint8_t] */ OP_WRITE, /* *(uint8_t*)stk_indx[uint8_t] = stk_indx[uint8_t] */ @@ -83,6 +84,12 @@ LAIKA_FORCEINLINE void laikaV_execute(struct sLaikaV_vm *vm) { vm->stack[indx] = vm->constList[constIndx]; break; } + case OP_PUSHLIT: { + uint8_t indx = READBYTE; + uint8_t lit = READBYTE; + vm->stack[indx].i = lit; + break; + } case OP_READ: { uint8_t indx = READBYTE; uint8_t ptr = READBYTE;