Compare commits

..

2 Commits

Author SHA1 Message Date
Giulio De Pasquale
fd45b5c54f Aggiunta opzione DEBG 2017-05-25 17:53:49 +02:00
Giulio De Pasquale
2bc3b17123 tea-encrypt.c ora cicla tutta la stringa 2017-05-25 16:59:28 +02:00
5 changed files with 95 additions and 18 deletions

View File

@ -3,6 +3,7 @@ import re
import struct import struct
import IPython import IPython
import copy import copy
import argparse
class AssemblerException(Exception): class AssemblerException(Exception):
@ -125,7 +126,8 @@ class VMAssembler:
for f in self.functions: for f in self.functions:
print("FUNCTION {}".format(f.name)) print("FUNCTION {}".format(f.name))
for idx, ins in enumerate(f.instructions): for idx, ins in enumerate(f.instructions):
print("{}:\t{}".format(hex(f.offset+ f.offset_of_instruction(idx)), ins)) print("{}:\t{}".format(
hex(f.offset + f.offset_of_instruction(idx)), ins))
def imm2reg(self, instruction): def imm2reg(self, instruction):
""" """
@ -479,6 +481,7 @@ ops_sizes = {"reg2reg": 2,
"immonly": 3, "immonly": 3,
"jump": 3, "jump": 3,
"single": 1} "single": 1}
ops = [VMComponent(le[0], i, le[1]) for i, le in enumerate(op_names)] ops = [VMComponent(le[0], i, le[1]) for i, le in enumerate(op_names)]
regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)] regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)]
instruction_re = re.compile( instruction_re = re.compile(
@ -488,24 +491,34 @@ immediate_re = re.compile("(?:0x)?[0-9a-fA-F]+$")
alpha_re = re.compile("^[a-zA-Z]*$") alpha_re = re.compile("^[a-zA-Z]*$")
register_re = re.compile("(^[rRsS][0-4]$)|([iIrRsS][pP]$)") register_re = re.compile("(^[rRsS][0-4]$)|([iIrRsS][pP]$)")
label_re = re.compile("^([a-zA-Z]+)\:(?:\ *\#.*)?$") label_re = re.compile("^([a-zA-Z]+)\:(?:\ *\#.*)?$")
symcall_re = re.compile("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$") symcall_re = re.compile(
"^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$")
commentline_re = re.compile("^\ *\#.*") commentline_re = re.compile("^\ *\#.*")
def main(): def main():
if len(sys.argv) < 4: parser = argparse.ArgumentParser(description='Optional app description')
print("Usage: {} opcodes_key file_to_assemble output".format( parser.add_argument(
sys.argv[0])) 'opcodes_key', help='The key used to encrypt the opcodes')
return parser.add_argument('asmfile', help='The Pasticciotto assembly file')
parser.add_argument('outfile', help='The output file')
parser.add_argument('--debug', action='store_true',
help='Enables the DEBG opcode')
args = parser.parse_args()
with open(sys.argv[2], 'r') as f: if args.debug:
global ops
ops.append(VMComponent("DEBG", len(ops), "single"))
with open(args.asmfile, 'r') as f:
filedata = f.readlines() filedata = f.readlines()
filedata = [x.strip() for x in filedata if x.strip() and not commentline_re.match(x)] filedata = [x.strip() for x in filedata if x.strip()
and not commentline_re.match(x)]
vma = VMAssembler(sys.argv[1], filedata) vma = VMAssembler(args.opcodes_key, filedata)
print(vma.functions) print(vma.functions)
vma.parse() vma.parse()
with open(sys.argv[3], 'wb') as f: with open(args.outfile, 'wb') as f:
f.write(vma.assembled_code) f.write(vma.assembled_code)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -6,7 +6,7 @@
void encrypt(uint16_t *v) { void encrypt(uint16_t *v) {
uint16_t v0 = v[0], v1 = v[1], sum = 0, i; /* set up */ uint16_t v0 = v[0], v1 = v[1], sum = 0, i; /* set up */
uint16_t delta= 0x626f; uint16_t delta = 0x626f;
uint16_t k0 = 0x7065; // "pe" uint16_t k0 = 0x7065; // "pe"
uint16_t k1 = 0x7065; // "pe" uint16_t k1 = 0x7065; // "pe"
uint16_t k2 = 0x7275; // "ru" uint16_t k2 = 0x7275; // "ru"
@ -16,7 +16,7 @@ void encrypt(uint16_t *v) {
sum += delta; sum += delta;
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1); v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3); v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
printf("Intermediate v0: 0x%x | v1: 0x%x\n", v0, v1); //printf("Intermediate v0: 0x%x | v1: 0x%x\n", v0, v1);
} }
printf("SUM: 0x%x\n", sum); printf("SUM: 0x%x\n", sum);
printf("v0: 0x%x, v1: 0x%x\n", v0, v1); printf("v0: 0x%x, v1: 0x%x\n", v0, v1);
@ -27,7 +27,6 @@ void encrypt(uint16_t *v) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
uint8_t *buf; uint8_t *buf;
uint32_t buflen, i; uint32_t buflen, i;
if (argc != 2) { if (argc != 2) {
printf("Usage: %s text_to_encrypt", argv[0]); printf("Usage: %s text_to_encrypt", argv[0]);
exit(1); exit(1);
@ -35,7 +34,10 @@ int main(int argc, char *argv[]) {
buflen = strlen(argv[1]); buflen = strlen(argv[1]);
buf = (uint8_t *)malloc(buflen); buf = (uint8_t *)malloc(buflen);
memcpy(buf, argv[1], buflen); memcpy(buf, argv[1], buflen);
encrypt((uint16_t *)buf); for (i = 0; i < buflen; i++) {
printf("----\n");
encrypt((uint16_t *)&buf[i]);
}
printf("Result:\n"); printf("Result:\n");
for (i = 0; i < buflen; i++) { for (i = 0; i < buflen; i++) {
printf("%02x", buf[i]); printf("%02x", buf[i]);

View File

@ -26,6 +26,5 @@ int main(int argc, char *argv[]) {
bytecode_if.read((char *)bytecode, bytecode_size); bytecode_if.read((char *)bytecode, bytecode_size);
VM vm((uint8_t *)argv[1], bytecode, bytecode_size); VM vm((uint8_t *)argv[1], bytecode, bytecode_size);
vm.run(); vm.run();
vm.status();
return 0; return 0;
} }

View File

@ -1,6 +1,7 @@
/* /*
MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
*/ */
#ifndef DBG
enum OPS_STARTING_VALUES { enum OPS_STARTING_VALUES {
MOVI, MOVI,
MOVR, MOVR,
@ -52,7 +53,60 @@ enum OPS_STARTING_VALUES {
GRMN, GRMN,
NUM_OPS NUM_OPS
}; };
#else
enum OPS_STARTING_VALUES {
MOVI,
MOVR,
LODI,
LODR,
STRI,
STRR,
ADDI,
ADDR,
SUBI,
SUBR,
ANDB,
ANDW,
ANDR,
YORB,
YORW,
YORR,
XORB,
XORW,
XORR,
NOTR,
MULI,
MULR,
DIVI,
DIVR,
SHLI,
SHLR,
SHRI,
SHRR,
PUSH,
POOP,
CMPB,
CMPW,
CMPR,
JMPI,
JMPR,
JPAI,
JPAR,
JPBI,
JPBR,
JPEI,
JPER,
JPNI,
JPNR,
CALL,
RETN,
SHIT,
NOPE,
GRMN,
DEBG,
NUM_OPS
};
#endif
uint8_t OPS[NUM_OPS]; uint8_t OPS[NUM_OPS];
/* /*
@ -116,3 +170,4 @@ INSTRUCTION SIZES
#define SHIT_SIZE SINGLE #define SHIT_SIZE SINGLE
#define NOPE_SIZE SINGLE #define NOPE_SIZE SINGLE
#define GRMN_SIZE SINGLE #define GRMN_SIZE SINGLE
#define DEBG_SIZE SINGLE

View File

@ -3,6 +3,7 @@
#include "opcodes.h" #include "opcodes.h"
#include "vmas.h" #include "vmas.h"
#include <string.h> #include <string.h>
#include <unistd.h>
void VM::encryptOpcodes(uint8_t *key) { void VM::encryptOpcodes(uint8_t *key) {
uint8_t arr[256]; uint8_t arr[256];
@ -946,7 +947,14 @@ void VM::run(void) {
finished = true; finished = true;
} else if (opcode == OPS[NOPE]) { } else if (opcode == OPS[NOPE]) {
regs[IP] += NOPE_SIZE; regs[IP] += NOPE_SIZE;
} else { }
#ifdef DBG
else if (opcode == OPS[DEBG]) {
status();
regs[IP] += DEBG_SIZE;
}
#endif
else {
DBG_ERROR(("WAT: 0x%x\n", as.code[regs[IP]])); DBG_ERROR(("WAT: 0x%x\n", as.code[regs[IP]]));
finished = true; finished = true;
} }