1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-21 12:40:04 +00:00

Box: implemented laikaB_unlock() & laikaB_lock()

This commit is contained in:
CPunch 2022-04-28 18:10:15 -05:00
parent 4333d03a51
commit 36c3c8a65f
3 changed files with 29 additions and 11 deletions

View File

@ -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

View File

@ -4,7 +4,11 @@
#include <inttypes.h>
#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

View File

@ -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;